Skip to content

Commit

Permalink
feat: Add optional Parent field to Device objects. (#512)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Signed-off-by: Corey Mutter <[email protected]>
Co-authored-by: Felix Ting <[email protected]>
  • Loading branch information
eaton-coreymutter and FelixTing authored Apr 17, 2024
1 parent 442156b commit 6abec0d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/devsdk/devsdk-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions include/edgex/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions include/edgex/edgex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/c/devman.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -78,6 +79,7 @@ void edgex_add_device
&svc->config.endpoints,
svc->secretstore,
name,
parent,
description,
labels,
locked ? LOCKED : UNLOCKED,
Expand Down Expand Up @@ -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,
Expand All @@ -167,6 +170,7 @@ void edgex_update_device
&svc->config.endpoints,
svc->secretstore,
name,
parent,
description,
labels,
profile_name,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/c/edgex-rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/c/edgex-rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/c/examples/discovery/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/c/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand 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,
Expand All @@ -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;
Expand Down Expand 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,
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/c/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 6abec0d

Please sign in to comment.