Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit ed04317

Browse files
James Prestwoodgrgustaf
authored andcommitted
[ocf] Improve get/platform info on client side (#952)
Fixes #808. Signed-off-by: James Prestwood <[email protected]>
1 parent d4bb75a commit ed04317

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

src/zjs_ocf_client.c

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ typedef enum {
3434

3535
struct client_resource {
3636
char *device_id;
37-
char *resource_type;
3837
char *resource_path;
38+
char *resource_type;
39+
jerry_value_t types_array;
40+
jerry_value_t iface_array;
3941
oc_server_handle_t server;
4042
resource_state state;
4143
jerry_value_t client;
@@ -251,19 +253,22 @@ static struct client_resource *find_resource_by_path(const char *path)
251253
/*
252254
* Create a new resource object
253255
*/
254-
static jerry_value_t create_resource(const char *device_id, const char *path)
256+
static jerry_value_t create_resource(struct client_resource *client)
255257
{
256258
jerry_value_t resource = jerry_create_object();
257259

258-
if (device_id) {
259-
zjs_obj_add_string(resource, device_id, "deviceId");
260+
if (client->device_id) {
261+
zjs_obj_add_string(resource, client->device_id, "deviceId");
260262
}
261-
if (path) {
262-
zjs_obj_add_string(resource, path, "resourcePath");
263+
if (client->resource_path) {
264+
zjs_obj_add_string(resource, client->resource_path, "resourcePath");
263265
}
264266
ZVAL props = jerry_create_object();
265267
zjs_set_property(resource, "properties", props);
266268

269+
zjs_set_property(resource, "resourceTypes", client->types_array);
270+
zjs_set_property(resource, "interfaces", client->iface_array);
271+
267272
DBG_PRINT("id=%s, path=%s, obj number=%lu\n", device_id, path, resource);
268273

269274
return resource;
@@ -290,6 +295,8 @@ static void free_client(const uintptr_t native_p)
290295
if (client->resource_type) {
291296
zjs_free(client->resource_type);
292297
}
298+
jerry_release_value(client->types_array);
299+
jerry_release_value(client->iface_array);
293300
zjs_free(client);
294301
}
295302
}
@@ -424,8 +431,58 @@ static oc_discovery_flags_t discovery(const char *di,
424431
memcpy(cur->resource_path, uri, uri_len);
425432
cur->resource_path[uri_len] = '\0';
426433
}
434+
/*
435+
* Add the array of resource types to newly discovered resource
436+
*/
437+
uint32_t sz = oc_string_array_get_allocated_size(types);
438+
cur->types_array = jerry_create_array(sz);
439+
440+
for (i = 0; i < sz; i++) {
441+
char *t = oc_string_array_get_item(types, i);
442+
ZVAL val = jerry_create_string(t);
443+
jerry_set_property_by_index(cur->types_array, i, val);
444+
}
445+
/*
446+
* Add array of interfaces
447+
*/
448+
sz = 0;
449+
// count up set ifaces
450+
for (i = 1; i < 8; ++i) {
451+
if ((1 << i) & interfaces) {
452+
sz++;
453+
}
454+
}
455+
cur->iface_array = jerry_create_array(sz);
456+
if (interfaces & OC_IF_BASELINE) {
457+
ZVAL val = jerry_create_string("oic.if.baseline");
458+
jerry_set_property_by_index(cur->iface_array, --sz, val);
459+
}
460+
if (interfaces & OC_IF_LL) {
461+
ZVAL val = jerry_create_string("oic.if.ll");
462+
jerry_set_property_by_index(cur->iface_array, --sz, val);
463+
}
464+
if (interfaces & OC_IF_B) {
465+
ZVAL val = jerry_create_string("oic.if.b");
466+
jerry_set_property_by_index(cur->iface_array, --sz, val);
467+
}
468+
if (interfaces & OC_IF_R) {
469+
ZVAL val = jerry_create_string("oic.if.r");
470+
jerry_set_property_by_index(cur->iface_array, --sz, val);
471+
}
472+
if (interfaces & OC_IF_RW) {
473+
ZVAL val = jerry_create_string("oic.if.rw");
474+
jerry_set_property_by_index(cur->iface_array, --sz, val);
475+
}
476+
if (interfaces & OC_IF_A) {
477+
ZVAL val = jerry_create_string("oic.if.a");
478+
jerry_set_property_by_index(cur->iface_array, --sz, val);
479+
}
480+
if (interfaces & OC_IF_S) {
481+
ZVAL val = jerry_create_string("oic.if.s");
482+
jerry_set_property_by_index(cur->iface_array, --sz, val);
483+
}
427484

428-
ZVAL res = create_resource(cur->device_id, cur->resource_path);
485+
ZVAL res = create_resource(cur);
429486
zjs_trigger_event(cur->client, "resourcefound", &res, 1, NULL,
430487
NULL);
431488
zjs_fulfill_promise(h->promise_obj, &res, 1);
@@ -528,8 +585,7 @@ static void ocf_get_handler(oc_client_response_t *data)
528585
if (h && h->res) {
529586
struct client_resource *resource = h->res;
530587
if (data->code == OC_STATUS_OK) {
531-
ZVAL resource_val = create_resource(resource->device_id,
532-
resource->resource_path);
588+
ZVAL resource_val = create_resource(resource);
533589
ZVAL properties_val = get_props_from_response(data);
534590

535591
zjs_set_property(resource_val, "properties", properties_val);
@@ -664,8 +720,7 @@ static void put_finished(oc_client_response_t *data)
664720
if (data->code == OC_STATUS_CHANGED) {
665721
DBG_PRINT("PUT response OK, device_id=%s\n",
666722
resource->device_id);
667-
ZVAL resource_val = create_resource(resource->device_id,
668-
resource->resource_path);
723+
ZVAL resource_val = create_resource(resource);
669724
zjs_fulfill_promise(h->promise_obj, &resource_val, 1);
670725
} else {
671726
ERR_PRINT("PUT response code %d\n", data->code);
@@ -852,6 +907,10 @@ static void ocf_get_platform_info_handler(oc_client_response_t *data)
852907
zjs_obj_add_readonly_string(platform_info, oc_string(rep->value.string), "osVersion");
853908
} else if (strcmp(oc_string(rep->name), "mnfv") == 0) {
854909
zjs_obj_add_readonly_string(platform_info, oc_string(rep->value.string), "firmwareVersion");
910+
} else if (strcmp(oc_string(rep->name), "mnml") == 0) {
911+
zjs_obj_add_readonly_string(platform_info, oc_string(rep->value.string), "manufacturerURL");
912+
} else if (strcmp(oc_string(rep->name), "mnsl") == 0) {
913+
zjs_obj_add_readonly_string(platform_info, oc_string(rep->value.string), "supportURL");
855914
}
856915
DBG_PRINT("%s\n", oc_string(rep->value.string));
857916
break;

0 commit comments

Comments
 (0)