From 6abec0de53f1544e48264173dbee0aea1a2aa14a Mon Sep 17 00:00:00 2001 From: "Corey Mutter @ Eaton" <89539499+eaton-coreymutter@users.noreply.github.com> Date: Tue, 16 Apr 2024 22:24:36 -0400 Subject: [PATCH] feat: Add optional Parent field to Device objects. (#512) * feat: Add optional Parent field to Device objects. BREAKING CHANGE: Parent parameter added to edgex_add_device(), edgex_update_device(). Signed-off-by: Corey Mutter --------- Signed-off-by: Corey Mutter Co-authored-by: Felix Ting <43240457+FelixTing@users.noreply.github.com> --- include/devsdk/devsdk-base.h | 1 + include/edgex/devices.h | 4 ++++ include/edgex/edgex.h | 1 + src/c/devman.c | 5 +++++ src/c/edgex-rest.c | 19 +++++++++++++++++++ src/c/edgex-rest.h | 2 +- src/c/examples/discovery/template.c | 8 ++++---- src/c/metadata.c | 7 ++++++- src/c/metadata.h | 3 +++ 9 files changed, 44 insertions(+), 6 deletions(-) diff --git a/include/devsdk/devsdk-base.h b/include/devsdk/devsdk-base.h index c4f67c58..29563f87 100644 --- a/include/devsdk/devsdk-base.h +++ b/include/devsdk/devsdk-base.h @@ -75,6 +75,7 @@ typedef struct devsdk_commandresult typedef struct devsdk_discovered_device { const char *name; + const char *parent; devsdk_protocols *protocols; const char *description; iot_data_t *properties; diff --git a/include/edgex/devices.h b/include/edgex/devices.h index 565f3fb3..f437bfe4 100644 --- a/include/edgex/devices.h +++ b/include/edgex/devices.h @@ -22,6 +22,7 @@ * response to a request for device discovery. * @param svc The device service. * @param name The name of the new device. + * @param parent The name of the new device's parent, if any. * @param description Optional description of the new device. * @param labels Optional labels for the new device. * @param profile_name Name of the device profile to be used with this device. @@ -38,6 +39,7 @@ void edgex_add_device ( devsdk_service_t *svc, const char *name, + const char *parent, const char *description, const devsdk_strings *labels, const char *profile_name, @@ -60,6 +62,7 @@ void edgex_remove_device_byname (devsdk_service_t *svc, const char *name, devsdk * @brief Update a device's details. * @param svc The device service. * @param name The name of the device to be updated. + * @param parent If set, the name of a new parent for the device. * @param description If set, a new description for the device. * @param labels If set, a new set of labels for the device. * @param profilename If set, a new device profile for the device. @@ -70,6 +73,7 @@ void edgex_update_device ( devsdk_service_t *svc, const char *name, + const char *parent, const char *description, const devsdk_strings *labels, const char *profilename, diff --git a/include/edgex/edgex.h b/include/edgex/edgex.h index 3a4e4dd1..3a12ca74 100644 --- a/include/edgex/edgex.h +++ b/include/edgex/edgex.h @@ -127,6 +127,7 @@ typedef struct edgex_device char *description; devsdk_strings *labels; char *name; + char *parent; edgex_device_operatingstate operatingState; uint64_t origin; edgex_device_autoevents *autos; diff --git a/src/c/devman.c b/src/c/devman.c index 731f567c..e484d012 100644 --- a/src/c/devman.c +++ b/src/c/devman.c @@ -50,6 +50,7 @@ void edgex_add_device ( devsdk_service_t *svc, const char *name, + const char *parent, const char *description, const devsdk_strings *labels, const char *profile_name, @@ -78,6 +79,7 @@ void edgex_add_device &svc->config.endpoints, svc->secretstore, name, + parent, description, labels, locked ? LOCKED : UNLOCKED, @@ -154,6 +156,7 @@ void edgex_update_device ( devsdk_service_t *svc, const char *name, + const char *parent, const char *description, const devsdk_strings *labels, const char *profile_name, @@ -167,6 +170,7 @@ void edgex_update_device &svc->config.endpoints, svc->secretstore, name, + parent, description, labels, profile_name, @@ -220,6 +224,7 @@ void devsdk_add_discovered_devices (devsdk_service_t *svc, uint32_t ndevices, de &svc->config.endpoints, svc->secretstore, devices[i].name, + devices[i].parent, devices[i].description, labels, w->adminstate, diff --git a/src/c/edgex-rest.c b/src/c/edgex-rest.c index 60d24502..a239b4c2 100644 --- a/src/c/edgex-rest.c +++ b/src/c/edgex-rest.c @@ -676,6 +676,14 @@ static edgex_device *device_read (const JSON_Object *obj) { edgex_device *result = malloc (sizeof (edgex_device)); result->name = get_string (obj, "name"); + result->parent = get_string (obj, "parent"); + // If the parent is empty, set it to NULL, helps avoid breakage if core-metadata support + // for the field is not yet in place + if (result->parent && *result->parent == '\0') + { + free (result->parent); + result->parent = NULL; + } result->profile = calloc (1, sizeof (edgex_deviceprofile)); result->profile->name = get_string (obj, "profileName"); result->servicename = get_string (obj, "serviceName"); @@ -718,6 +726,10 @@ static JSON_Value *device_write (const edgex_device *e) json_object_set_string (obj, "adminState", edgex_adminstate_tostring (e->adminState)); json_object_set_string (obj, "operatingState", edgex_operatingstate_tostring (e->operatingState)); json_object_set_string (obj, "name", e->name); + if (e->parent) // omit-if-empty + { + json_object_set_string (obj, "parent", e->parent); + } json_object_set_string (obj, "description", e->description); json_object_set_value (obj, "labels", strings_to_array (e->labels)); json_object_set_uint (obj, "origin", e->origin); @@ -729,6 +741,7 @@ edgex_device *edgex_device_dup (const edgex_device *e) { edgex_device *result = malloc (sizeof (edgex_device)); result->name = strdup (e->name); +result->parent = e->parent ? strdup(e->parent) : NULL; result->description = strdup (e->description); result->labels = devsdk_strings_dup (e->labels); result->protocols = devsdk_protocols_dup (e->protocols); @@ -804,6 +817,10 @@ void edgex_device_free (devsdk_service_t *svc, edgex_device *e) svc->userfns.free_addr (svc->userdata, e->devimpl->address); } free (e->devimpl); + if (e->parent) + { + free (e->parent); + } e = e->next; free (current); } @@ -823,6 +840,7 @@ char *edgex_device_write (const edgex_device *e) char *edgex_device_write_sparse ( const char * name, + const char * parent, const char * description, const devsdk_strings * labels, const char * profile_name @@ -834,6 +852,7 @@ char *edgex_device_write_sparse JSON_Object *obj = json_value_get_object (jval); json_object_set_string (obj, "name", name); + if (parent) { json_object_set_string (obj, "parent", parent); } if (description) { json_object_set_string (obj, "description", description); } if (profile_name) { json_object_set_string (obj, "profileName", profile_name); } if (labels) diff --git a/src/c/edgex-rest.h b/src/c/edgex-rest.h index a2700214..93049bac 100644 --- a/src/c/edgex-rest.h +++ b/src/c/edgex-rest.h @@ -31,7 +31,7 @@ edgex_deviceservice *edgex_deviceservice_read (const char *json); void edgex_deviceservice_free (edgex_deviceservice *e); void edgex_device_autoevents_free (edgex_device_autoevents *e); char *edgex_device_write (const edgex_device *e); -char *edgex_device_write_sparse (const char *name, const char *description, const devsdk_strings *labels, const char *profile_name); +char *edgex_device_write_sparse (const char *name, const char *parent, const char *description, const devsdk_strings *labels, const char *profile_name); edgex_device *edgex_device_dup (const edgex_device *e); devsdk_devices *edgex_device_todevsdk (devsdk_service_t *svc, const edgex_device *e); void edgex_device_free (devsdk_service_t *svc, edgex_device *e); diff --git a/src/c/examples/discovery/template.c b/src/c/examples/discovery/template.c index d20118ed..9688cc58 100644 --- a/src/c/examples/discovery/template.c +++ b/src/c/examples/discovery/template.c @@ -127,10 +127,10 @@ static void template_discover (void *impl) devsdk_discovered_device devs[] = { - { "DiscoveredOne", p1, "First discovered device", NULL }, - { "DiscoveredTwo", p2, "Second discovered device", NULL }, - { "DiscoveredThree", p3, "Third discovered device", NULL }, - { "DiscoveredFour", p4, "Fourth discovered device", NULL } + { "DiscoveredOne", NULL, p1, "First discovered device", NULL }, + { "DiscoveredTwo", NULL, p2, "Second discovered device", NULL }, + { "DiscoveredThree", NULL, p3, "Third discovered device", NULL }, + { "DiscoveredFour", NULL, p4, "Fourth discovered device", NULL } }; devsdk_add_discovered_devices (driver->svc, 4, devs); diff --git a/src/c/metadata.c b/src/c/metadata.c index 3003bb77..ac4db4c3 100644 --- a/src/c/metadata.c +++ b/src/c/metadata.c @@ -367,6 +367,7 @@ char *edgex_metadata_client_add_device edgex_service_endpoints *endpoints, edgex_secret_provider_t * secretprovider, const char *name, + const char *parent, const char *description, const devsdk_strings *labels, edgex_device_adminstate adminstate, @@ -394,6 +395,7 @@ char *edgex_metadata_client_add_device endpoints->metadata.port ); dev->name = (char *)name; + dev->parent = (char *)parent; dev->description = (char *)description; dev->adminState = adminstate; dev->operatingState = UP; @@ -504,6 +506,7 @@ void edgex_metadata_client_add_or_modify_device edgex_service_endpoints * endpoints, edgex_secret_provider_t * secretprovider, const char * name, + const char *parent, const char * description, const devsdk_strings * labels, edgex_device_adminstate adminstate, @@ -525,6 +528,7 @@ void edgex_metadata_client_add_or_modify_device snprintf (url, URL_BUF_SIZE - 1, "http://%s:%u/api/" EDGEX_API_VERSION "/device", endpoints->metadata.host, endpoints->metadata.port); dev->name = (char *)name; + dev->parent = (char *)parent; dev->description = (char *)description; dev->adminState = adminstate; dev->operatingState = UP; @@ -612,6 +616,7 @@ void edgex_metadata_client_update_device edgex_service_endpoints * endpoints, edgex_secret_provider_t * secretprovider, const char * name, + const char *parent, const char * description, const devsdk_strings * labels, const char * profile_name, @@ -633,7 +638,7 @@ void edgex_metadata_client_update_device ); json = edgex_device_write_sparse - (name, description, labels, profile_name); + (name, parent, description, labels, profile_name); iot_data_t *jwt_data = edgex_secrets_request_jwt (secretprovider); ctx.jwt_token = iot_data_string(jwt_data); diff --git a/src/c/metadata.h b/src/c/metadata.h index 922f4d15..c9d881ef 100644 --- a/src/c/metadata.h +++ b/src/c/metadata.h @@ -82,6 +82,7 @@ char * edgex_metadata_client_add_device edgex_service_endpoints * endpoints, edgex_secret_provider_t * secretprovider, const char * name, + const char * parent, const char * description, const devsdk_strings * labels, edgex_device_adminstate adminstate, @@ -101,6 +102,7 @@ void edgex_metadata_client_add_or_modify_device edgex_service_endpoints * endpoints, edgex_secret_provider_t * secretprovider, const char * name, + const char * parent, const char * description, const devsdk_strings * labels, edgex_device_adminstate adminstate, @@ -118,6 +120,7 @@ void edgex_metadata_client_update_device edgex_service_endpoints * endpoints, edgex_secret_provider_t * secretprovider, const char * name, + const char * parent, const char * description, const devsdk_strings * labels, const char * profile_name,