diff --git a/src/c/config.c b/src/c/config.c index 093f7028..8931f5ab 100644 --- a/src/c/config.c +++ b/src/c/config.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022 + * Copyright (c) 2018-2023 * IoTech Ltd * * SPDX-License-Identifier: Apache-2.0 @@ -87,7 +87,6 @@ iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svc iot_data_string_map_add (result, "Service/CORSConfiguration/CORSMaxAge", iot_data_alloc_ui32 (3600)); iot_data_string_map_add (result, "Device/Labels", iot_data_alloc_string ("", IOT_DATA_REF)); - iot_data_string_map_add (result, "Device/UseMessageBus", iot_data_alloc_bool (false)); iot_data_string_map_add (result, "Device/ProfilesDir", iot_data_alloc_string ("", IOT_DATA_REF)); iot_data_string_map_add (result, "Device/DevicesDir", iot_data_alloc_string ("", IOT_DATA_REF)); iot_data_string_map_add (result, "Device/EventQLength", iot_data_alloc_ui32 (0)); @@ -745,7 +744,6 @@ static JSON_Value *edgex_device_config_toJson (devsdk_service_t *svc) json_object_set_boolean (dobj, "UpdateLastConnected", svc->config.device.updatelastconnected); json_object_set_uint (dobj, "EventQLength", svc->config.device.eventqlen); - json_object_set_boolean (dobj, "UseMessageBus", iot_data_string_map_get_bool (svc->config.sdkconf, "Device/UseMessageBus", false)); JSON_Value *lval = json_value_init_array (); JSON_Array *larr = json_value_get_array (lval); diff --git a/src/c/data-rest.c b/src/c/data-rest.c deleted file mode 100644 index fd39ba80..00000000 --- a/src/c/data-rest.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2021 - * IoTech Ltd - * - * SPDX-License-Identifier: Apache-2.0 - * - */ - -#include "data-rest.h" -#include "rest.h" -#include "errorlist.h" - -static void edc_rest_postfn (iot_logger_t *lc, void *address, edgex_event_cooked *event) -{ - edgex_ctx ctx; - const char *baseurl = (const char *)address; - devsdk_error err = EDGEX_OK; - - memset (&ctx, 0, sizeof (edgex_ctx)); - - char *url = malloc (strlen (baseurl) + strlen (event->path) + 1); - strcpy (url, baseurl); - strcat (url, event->path); - switch (event->encoding) - { - case JSON: - { - edgex_http_post (lc, &ctx, url, event->value.json, NULL, &err); - break; - } - case CBOR: - { - edgex_http_postbin (lc, &ctx, url, event->value.cbor.data, event->value.cbor.length, CONTENT_CBOR, NULL, &err); - break; - } - } - edgex_event_cooked_free (event); - free (url); -} - -static void edc_rest_freefn (iot_logger_t *lc, void *address) -{ - free (address); -} - -edgex_data_client_t *edgex_data_client_new_rest (const edgex_device_service_endpoint *e, iot_logger_t *lc, iot_threadpool_t *queue) -{ - edgex_data_client_t *result = malloc (sizeof (edgex_data_client_t)); - result->lc = lc; - result->queue = queue; - result->pf = edc_rest_postfn; - result->ff = edc_rest_freefn; - result->mf = NULL; - char *url = malloc (URL_BUF_SIZE); - snprintf (url, URL_BUF_SIZE - 1, "http://%s:%u/api/v2/event/", e->host, e->port); - result->address = url; - iot_log_info (lc, "Event data will be posted to core-data at %s//", url); - return result; -} diff --git a/src/c/data-rest.h b/src/c/data-rest.h deleted file mode 100644 index 06acf0fc..00000000 --- a/src/c/data-rest.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2021 - * IoTech Ltd - * - * SPDX-License-Identifier: Apache-2.0 - * - */ - -#ifndef _EDGEX_DEVICE_DATA_REST_H_ -#define _EDGEX_DEVICE_DATA_REST_H_ 1 - -#include "data.h" -#include "config.h" - -edgex_data_client_t *edgex_data_client_new_rest (const edgex_device_service_endpoint *e, iot_logger_t *lc, iot_threadpool_t *queue); - -#endif diff --git a/src/c/data.h b/src/c/data.h index 58b2715c..99a00772 100644 --- a/src/c/data.h +++ b/src/c/data.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 + * Copyright (c) 2018-2023 * IoTech Ltd * * SPDX-License-Identifier: Apache-2.0 @@ -84,8 +84,6 @@ typedef struct edgex_data_client_t typedef struct edgex_device_service_endpoint edgex_device_service_endpoint; -edgex_data_client_t *edgex_data_client_new_rest (const edgex_device_service_endpoint *e, iot_logger_t *lc, iot_threadpool_t *queue); - void edgex_data_client_free (edgex_data_client_t *client); void edgex_data_client_add_event (edgex_data_client_t *client, edgex_event_cooked *eventval, devsdk_metrics_t *metrics); diff --git a/src/c/examples/bitfields/res/configuration.toml b/src/c/examples/bitfields/res/configuration.toml index c7180506..ea7e9f36 100644 --- a/src/c/examples/bitfields/res/configuration.toml +++ b/src/c/examples/bitfields/res/configuration.toml @@ -23,3 +23,21 @@ Labels = [ "Bitfields" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-bitfields" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/counters/res/configuration.toml b/src/c/examples/counters/res/configuration.toml index e072466a..bf106ba9 100644 --- a/src/c/examples/counters/res/configuration.toml +++ b/src/c/examples/counters/res/configuration.toml @@ -23,3 +23,21 @@ Labels = [ "Counter" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-counters" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/discovery/res/configuration.toml b/src/c/examples/discovery/res/configuration.toml index a783cb16..508feb6e 100644 --- a/src/c/examples/discovery/res/configuration.toml +++ b/src/c/examples/discovery/res/configuration.toml @@ -30,3 +30,21 @@ Labels = [ "Template" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-discovery" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/file/res/configuration.toml b/src/c/examples/file/res/configuration.toml index d4081b65..d28e9a33 100644 --- a/src/c/examples/file/res/configuration.toml +++ b/src/c/examples/file/res/configuration.toml @@ -13,3 +13,21 @@ [Device] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-file" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/gyro/res/configuration.toml b/src/c/examples/gyro/res/configuration.toml index c19f7c4f..76dd2e8a 100644 --- a/src/c/examples/gyro/res/configuration.toml +++ b/src/c/examples/gyro/res/configuration.toml @@ -23,3 +23,21 @@ Labels = [ "Gyro" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-gyro" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/random/res/configuration.toml b/src/c/examples/random/res/configuration.toml index 575742f6..fe1c81f9 100644 --- a/src/c/examples/random/res/configuration.toml +++ b/src/c/examples/random/res/configuration.toml @@ -24,3 +24,21 @@ Labels = [ "Random" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-random" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/res/configuration.toml b/src/c/examples/res/configuration.toml index be8b4848..e7ba059d 100644 --- a/src/c/examples/res/configuration.toml +++ b/src/c/examples/res/configuration.toml @@ -30,3 +30,21 @@ Labels = [ "Template" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-template" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/examples/terminal/res/configuration.toml b/src/c/examples/terminal/res/configuration.toml index f4b59068..95246f68 100644 --- a/src/c/examples/terminal/res/configuration.toml +++ b/src/c/examples/terminal/res/configuration.toml @@ -23,3 +23,21 @@ Labels = [ "Counter" ] ProfilesDir = "res/profiles" DevicesDir = "res/devices" + +[MessageBus] + Protocol = "redis" + Host = "localhost" + Port = 6379 + Type = "redis" + AuthMode = "usernamepassword" # required for redis messagebus (secure or insecure) + SecretName = "redisdb" + [MessageBus.Optional] + ClientId = "device-terminal" + Qos = 0 # Quality of Sevice values are 0 (At most once), 1 (At least once) or 2 (Exactly once) + KeepAlive = 10 # Seconds (must be 2 or greater) + Retained = false + SkipCertVerify = false + [MessageBus.Topics] + PublishTopicPrefix = "edgex/events/device" # /// will be added to this Publish Topic prefix + CommandRequestTopic = "edgex/device/command/request/device-bacnet/#" # subscribing for inbound command requests + CommandResponseTopicPrefix = "edgex/device/command/response" # publishing outbound command responses; /// will be added to this publish topic prefix diff --git a/src/c/metrics.c b/src/c/metrics.c deleted file mode 100644 index 5bb98b34..00000000 --- a/src/c/metrics.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2019-2022 - * IoTech Ltd - * - * SPDX-License-Identifier: Apache-2.0 - * - */ - -#include "metrics.h" -#include "edgex2.h" -#include "edgex-rest.h" -#include "parson.h" -#include "service.h" -#include "iot/time.h" - -#include -#include - -#ifdef __GNU_LIBRARY__ -#include -#include -#endif - -#include - -static void edgex_metrics_populate (edgex_metricsresponse *m, uint64_t starttime, const char *name) -{ - struct rusage rstats; -#ifdef __GNU_LIBRARY__ - double loads[1]; -#if (__GLIBC__ * 100 + __GLIBC_MINOR__) >= 233 - struct mallinfo2 mi = mallinfo2 (); -#else - struct mallinfo mi = mallinfo (); -#endif - 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; - } - m->svcname = name; -} - -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", MHD_HTTP_OK, NULL); - edgex_metrics_populate (&mr, svc->starttime, svc->name); - - edgex_metricsresponse_write (&mr, reply); -} diff --git a/src/c/metrics.h b/src/c/metrics.h index 0c9aac2b..b11776f0 100644 --- a/src/c/metrics.h +++ b/src/c/metrics.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 + * Copyright (c) 2019-2023 * IoTech Ltd * * SPDX-License-Identifier: Apache-2.0 @@ -10,9 +10,6 @@ #define _EDGEX_DEVICE_METRICS_H_ 1 #include -#include "rest-server.h" - -extern void edgex_device_handler_metricsv2 (void *ctx, const devsdk_http_request *req, devsdk_http_reply *reply); typedef struct devsdk_metrics_t { diff --git a/src/c/service.c b/src/c/service.c index 4f4c5f17..706c18f1 100644 --- a/src/c/service.c +++ b/src/c/service.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022 + * Copyright (c) 2018-2023 * IoTech Ltd * * SPDX-License-Identifier: Apache-2.0 @@ -12,7 +12,6 @@ #include "device.h" #include "discovery.h" #include "callback2.h" -#include "metrics.h" #include "validate.h" #include "errorlist.h" #include "rest-server.h" @@ -549,47 +548,39 @@ static void startConfigured (devsdk_service_t *svc, const devsdk_timeout *deadli svc->eventq = iot_threadpool_alloc (1, svc->config.device.eventqlen, IOT_THREAD_NO_PRIORITY, IOT_THREAD_NO_AFFINITY, svc->logger); iot_threadpool_start (svc->eventq); - /* Wait for metadata and data to be available */ + // Initialize MessageBus client - if (iot_data_string_map_get_bool (svc->config.sdkconf, "Device/UseMessageBus", false)) + const char *bustype = iot_data_string_map_get_string (svc->config.sdkconf, EX_BUS_TYPE); + if (strcmp (bustype, "mqtt") == 0) { - const char *bustype = iot_data_string_map_get_string (svc->config.sdkconf, EX_BUS_TYPE); - if (strcmp (bustype, "mqtt") == 0) - { - svc->dataclient = edgex_data_client_new_mqtt (svc, deadline, svc->eventq); - } - else if (strcmp (bustype, "redis") == 0) - { - svc->dataclient = edgex_data_client_new_redstr (svc, deadline, svc->eventq); - } - else - { - iot_log_error (svc->logger, "Unknown Message Bus type %s", bustype); - } - if (svc->dataclient == NULL) - { - *err = EDGEX_REMOTE_SERVER_DOWN; - return; - } + svc->dataclient = edgex_data_client_new_mqtt (svc, deadline, svc->eventq); + } + else if (strcmp (bustype, "redis") == 0) + { + svc->dataclient = edgex_data_client_new_redstr (svc, deadline, svc->eventq); } else { - if (svc->registry) - { - devsdk_registry_query_service (svc->registry, "core-data", &svc->config.endpoints.data.host, &svc->config.endpoints.data.port, deadline, err); - } - if (err->code || !ping_client (svc->logger, "core-data", &svc->config.endpoints.data, deadline, err)) - { - return; - } - svc->dataclient = edgex_data_client_new_rest (&svc->config.endpoints.data, svc->logger, svc->eventq); + iot_log_error (svc->logger, "Unknown Message Bus type %s", bustype); + } + if (svc->dataclient == NULL) + { + *err = EDGEX_REMOTE_SERVER_DOWN; + return; } + /* Wait for core-data and core-metadata to be available */ + if (!ping_client (svc->logger, "core-metadata", &svc->config.endpoints.metadata, deadline, err)) { return; } + if (!ping_client (svc->logger, "core-data", &svc->config.endpoints.data, deadline, err)) + { + return; + } + *err = EDGEX_OK; /* Register device service in metadata */ @@ -771,8 +762,6 @@ static void startConfigured (devsdk_service_t *svc, const devsdk_timeout *deadli edgex_rest_server_register_handler (svc->daemon, EDGEX_DEV_API2_DISCOVERY, DevSDK_Post, svc, edgex_device_handler_discoveryv2); - 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_API2_CONFIG, DevSDK_Get, svc, edgex_device_handler_configv2); edgex_rest_server_register_handler (svc->daemon, EDGEX_DEV_API2_SECRET, DevSDK_Post, svc, edgex_device_handler_secret); @@ -961,6 +950,10 @@ void devsdk_service_start (devsdk_service_t *svc, iot_data_t *driverdfls, devsdk if (svc->registry) { devsdk_registry_query_service (svc->registry, "core-metadata", &svc->config.endpoints.metadata.host, &svc->config.endpoints.metadata.port, &deadline, err); + if (err->code == 0) + { + devsdk_registry_query_service (svc->registry, "core-data", &svc->config.endpoints.data.host, &svc->config.endpoints.data.port, &deadline, err); + } if (err->code) { toml_free (configtoml); diff --git a/src/c/service.h b/src/c/service.h index 17247407..afa52107 100644 --- a/src/c/service.h +++ b/src/c/service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022 + * Copyright (c) 2018-2023 * IoTech Ltd * * SPDX-License-Identifier: Apache-2.0 @@ -10,7 +10,6 @@ #define _EDGEX_DEVICE_SERVICE_H_ 1 #include "devsdk/devsdk.h" -#include "metrics.h" #include "registry.h" #include "config.h" #include "data.h"