Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: REST v2 API common endpoints (#257) #298

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/c/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#define EDGEX_DEV_API2_PING "/api/v2/ping"
#define EDGEX_DEV_API2_CONFIG "/api/v2/config"
#define EDGEX_DEV_API2_METRICS "/api/v2/metrics"
#define EDGEX_DEV_API2_DEVICE "/api/v2/device/{id}/{cmd}"
#define EDGEX_DEV_API2_DEVICE_NAME "/api/v2/device/name/{name}/{cmd}"

Expand Down
19 changes: 9 additions & 10 deletions src/c/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "service.h"
#include "errorlist.h"
#include "edgex-rest.h"
#include "correlation.h"
#include "edgex-logging.h"
#include "devutil.h"
#include "autoevent.h"
Expand Down Expand Up @@ -670,7 +671,7 @@ void edgex_device_freeConfig (devsdk_service_t *svc)
edgex_map_deinit (&svc->config.watchers);
}

static char *edgex_device_serialize_config (devsdk_service_t *svc)
static JSON_Value *edgex_device_config_toJson (devsdk_service_t *svc)
{
JSON_Value *val = json_value_init_object ();
JSON_Object *obj = json_value_get_object (val);
Expand Down Expand Up @@ -763,14 +764,15 @@ static char *edgex_device_serialize_config (devsdk_service_t *svc)
json_object_set_value (obj, "Driver", dval);
}

char *result = json_serialize_to_string (val);
json_value_free (val);
return result;
return val;
}

void edgex_device_handler_config (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply)
{
char *json = edgex_device_serialize_config ((devsdk_service_t *)ctx);
JSON_Value *cval = edgex_device_config_toJson ((devsdk_service_t *)ctx);

char *json = json_serialize_to_string (cval);
json_value_free (cval);
reply->data.bytes = json;
reply->data.size = strlen (json);
reply->content_type = CONTENT_JSON;
Expand All @@ -779,16 +781,13 @@ void edgex_device_handler_config (void *ctx, const devsdk_http_request *req, dev

void edgex_device_handler_configv2 (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply)
{
edgex_baserequest *br;
edgex_configresponse *cr = malloc (sizeof (edgex_configresponse));

br = edgex_baserequest_read (req->data);
edgex_baseresponse_populate ((edgex_baseresponse *)cr, br->requestId, MHD_HTTP_OK, NULL);
cr->config = edgex_device_serialize_config ((devsdk_service_t *)ctx);
edgex_baseresponse_populate ((edgex_baseresponse *)cr, "v2", edgex_device_get_crlid (), MHD_HTTP_OK, NULL);
cr->config = edgex_device_config_toJson ((devsdk_service_t *)ctx);

edgex_configresponse_write (cr, reply);
edgex_configresponse_free (cr);
edgex_baserequest_free (br);
}

void edgex_device_process_configured_devices
Expand Down
6 changes: 3 additions & 3 deletions src/c/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ static void edgex_device_runput2 (devsdk_service_t *svc, edgex_device *dev, cons
if (svc->userfns.puthandler (svc->userdata, dev->name, dev->protocols, cmdinfo->nreqs, cmdinfo->reqs, (const iot_data_t **)results, &e))
{
edgex_baseresponse br;
edgex_baseresponse_populate (&br, "", MHD_HTTP_OK, "Data written successfully");
edgex_baseresponse_populate (&br, "v2", "", MHD_HTTP_OK, "Data written successfully");
edgex_baseresponse_write (&br, reply);
if (svc->config.device.updatelastconnected)
{
Expand Down Expand Up @@ -1186,7 +1186,7 @@ static void edgex_device_v2impl (devsdk_service_t *svc, edgex_device *dev, const
else
{
edgex_data_client_add_event (svc, event);
edgex_baseresponse_populate (&br, "", MHD_HTTP_OK, "Event generated successfully");
edgex_baseresponse_populate (&br, "v2", "", MHD_HTTP_OK, "Event generated successfully");
edgex_baseresponse_write (&br, reply);
}
}
Expand All @@ -1199,7 +1199,7 @@ static void edgex_device_v2impl (devsdk_service_t *svc, edgex_device *dev, const
else
{
edgex_event_cooked_free (event);
edgex_baseresponse_populate (&br, "", MHD_HTTP_OK, "Reading performed successfully");
edgex_baseresponse_populate (&br, "v2", "", MHD_HTTP_OK, "Reading performed successfully");
edgex_baseresponse_write (&br, reply);
}
}
Expand Down
33 changes: 26 additions & 7 deletions src/c/edgex-rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1827,8 +1827,9 @@ void edgex_baserequest_free (edgex_baserequest *e)
}


void edgex_baseresponse_populate (edgex_baseresponse *e, const char *reqId, int code, const char *msg)
void edgex_baseresponse_populate (edgex_baseresponse *e, const char *version, const char *reqId, int code, const char *msg)
{
e->apiVersion = version;
e->requestId = reqId;
e->statusCode = code;
e->message = msg;
Expand All @@ -1848,6 +1849,7 @@ static JSON_Value *baseresponse_write (const edgex_baseresponse *br)
{
JSON_Value *result = json_value_init_object ();
JSON_Object *obj = json_value_get_object (result);
json_object_set_string (obj, "apiVersion", br->apiVersion);
json_object_set_string (obj, "requestId", br->requestId);
json_object_set_uint (obj, "statusCode", br->statusCode);
if (br->message)
Expand Down Expand Up @@ -1911,7 +1913,7 @@ static JSON_Value *configresponse_write (const edgex_configresponse *cr)
{
JSON_Value *result = baseresponse_write ((const edgex_baseresponse *)cr);
JSON_Object *obj = json_value_get_object (result);
json_object_set_string (obj, "config", cr->config);
json_object_set_value (obj, "config", cr->config);
return result;
}

Expand All @@ -1923,11 +1925,28 @@ void edgex_configresponse_write (const edgex_configresponse *cr, devsdk_http_rep

void edgex_configresponse_free (edgex_configresponse *cr)
{
if (cr)
{
free (cr->config);
free (cr);
}
free (cr);
}

static JSON_Value *metricsesponse_write (const edgex_metricsresponse *mr)
{
JSON_Value *result = baseresponse_write ((const edgex_baseresponse *)mr);
JSON_Object *obj = json_value_get_object (result);
#ifdef __GNU_LIBRARY__
json_object_set_uint (obj, "Alloc", mr->alloc);
json_object_set_uint (obj, "TotalAlloc", mr->totalloc);
json_object_set_number (obj, "CpuLoadAvg", mr->loadavg);
#endif
json_object_set_number (obj, "CpuTime", mr->cputime);
json_object_set_number (obj, "CpuAvgUsage", mr->cpuavg);

return result;
}

void edgex_metricsresponse_write (const edgex_metricsresponse *mr, devsdk_http_reply *reply)
{
JSON_Value *val = metricsesponse_write (mr);
value_write (val, reply);
}

#ifdef EDGEX_DEBUG_DUMP
Expand Down
3 changes: 2 additions & 1 deletion src/c/edgex-rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void edgex_watcher_free (edgex_watcher *e);
edgex_baserequest *edgex_baserequest_read (devsdk_http_data d);
void edgex_baserequest_free (edgex_baserequest *e);

void edgex_baseresponse_populate (edgex_baseresponse *e, const char *reqId, int code, const char *msg);
void edgex_baseresponse_populate (edgex_baseresponse *e, const char *version, const char *reqId, int code, const char *msg);

edgex_errorresponse *edgex_errorresponse_create (uint64_t code, char *msg);
void edgex_errorresponse_write (const edgex_errorresponse *er, devsdk_http_reply *reply);
Expand All @@ -67,6 +67,7 @@ void edgex_baseresponse_write (const edgex_baseresponse *br, devsdk_http_reply *
void edgex_pingresponse_write (const edgex_pingresponse *pr, devsdk_http_reply *reply);
void edgex_configresponse_write (const edgex_configresponse *cr, devsdk_http_reply *reply);
void edgex_configresponse_free (edgex_configresponse *cr);
void edgex_metricsresponse_write (const edgex_metricsresponse *mr, devsdk_http_reply *reply);


#ifdef EDGEX_DEBUG_DUMP
Expand Down
14 changes: 13 additions & 1 deletion src/c/edgex2.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define _EDGEX_EDGEX2_H_

#include "edgex/edgex-base.h"
#include "parson.h"

/* Definitions of Data Transfer Objects for v2 REST API */

Expand All @@ -20,6 +21,7 @@ typedef struct

typedef struct
{
const char *apiVersion;
const char *requestId;
uint64_t statusCode;
const char *message;
Expand All @@ -36,7 +38,17 @@ typedef struct
typedef struct
{
edgex_baseresponse base;
char *config;
JSON_Value *config;
} edgex_configresponse;

typedef struct
{
edgex_baseresponse base;
int alloc;
int totalloc;
double loadavg;
double cputime;
double cpuavg;
} edgex_metricsresponse;

#endif
38 changes: 38 additions & 0 deletions src/c/metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
*/

#include "metrics.h"
#include "edgex2.h"
#include "edgex-rest.h"
#include "parson.h"
#include "service.h"
#include "iot/time.h"
#include "correlation.h"

#include <sys/time.h>
#include <sys/resource.h>
Expand Down Expand Up @@ -64,3 +67,38 @@ void edgex_device_handler_metrics (void *ctx, const devsdk_http_request *req, de
reply->content_type = CONTENT_JSON;
reply->code = MHD_HTTP_OK;
}

static void edgex_metrics_populate (edgex_metricsresponse *m, uint64_t starttime)
{
struct rusage rstats;
#ifdef __GNU_LIBRARY__
double loads[1];
struct mallinfo mi = mallinfo ();
m->alloc = mi.uordblks;
m->totalloc = mi.arena + mi.hblkhd;
if (getloadavg (loads, 1) == 1)
{
m->loadavg = loads[0] * 100.0 / get_nprocs();
}
#endif
if (getrusage (RUSAGE_SELF, &rstats) == 0)
{
double walltime = (double)(iot_time_msecs() - starttime) / 1e3;
double cputime = rstats.ru_utime.tv_sec + rstats.ru_stime.tv_sec;
cputime += (double)(rstats.ru_utime.tv_usec + rstats.ru_stime.tv_usec) / 1e6;
m->cputime = cputime;
m->cpuavg = cputime / walltime;
}
}

void edgex_device_handler_metricsv2 (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply)
{
devsdk_service_t *svc = (devsdk_service_t *)ctx;
edgex_metricsresponse mr;
memset (&mr, 0, sizeof (mr));

edgex_baseresponse_populate ((edgex_baseresponse *)&mr, "v2", edgex_device_get_crlid (), MHD_HTTP_OK, NULL);
edgex_metrics_populate (&mr, svc->starttime);

edgex_metricsresponse_write (&mr, reply);
}
1 change: 1 addition & 0 deletions src/c/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
#include <stddef.h>

extern void edgex_device_handler_metrics (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply);
extern void edgex_device_handler_metricsv2 (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply);

#endif
1 change: 1 addition & 0 deletions src/c/rest-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ static int http_handler
}
response = MHD_create_response_from_buffer (reply_size, reply, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header (response, "Content-Type", reply_type);
MHD_add_response_header (response, "X-Correlation-ID", edgex_device_get_crlid ());
MHD_queue_response (conn, status, response);
MHD_destroy_response (response);

Expand Down
8 changes: 4 additions & 4 deletions src/c/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "discovery.h"
#include "callback.h"
#include "metrics.h"
#include "correlation.h"
#include "errorlist.h"
#include "rest-server.h"
#include "profiles.h"
Expand Down Expand Up @@ -272,14 +273,11 @@ static void ping_handler (void *ctx, const devsdk_http_request *req, devsdk_http

static void ping2_handler (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply)
{
edgex_baserequest *br;
edgex_pingresponse pr;

br = edgex_baserequest_read (req->data);
edgex_baseresponse_populate ((edgex_baseresponse *)&pr, br->requestId, MHD_HTTP_OK, NULL);
edgex_baseresponse_populate ((edgex_baseresponse *)&pr, "v2", edgex_device_get_crlid (), MHD_HTTP_OK, NULL);
pr.timestamp = iot_time_secs ();
edgex_pingresponse_write (&pr, reply);
edgex_baserequest_free (br);
}

static void version_handler (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply)
Expand Down Expand Up @@ -559,6 +557,8 @@ static void startConfigured (devsdk_service_t *svc, toml_table_t *config, devsdk
svc->daemon, EDGEX_DEV_API_METRICS, DevSDK_Get, svc, edgex_device_handler_metrics
);

edgex_rest_server_register_handler (svc->daemon, EDGEX_DEV_API2_METRICS, DevSDK_Get, svc, edgex_device_handler_metricsv2);

edgex_rest_server_register_handler
(
svc->daemon, EDGEX_DEV_API_CONFIG, DevSDK_Get, svc, edgex_device_handler_config
Expand Down