diff --git a/dmtree/tr181/upnp.c b/dmtree/tr181/upnp.c index dd8104fb..2507b6b9 100644 --- a/dmtree/tr181/upnp.c +++ b/dmtree/tr181/upnp.c @@ -1,13 +1,14 @@ /* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 2 of the License, or -* (at your option) any later version. -* -* Copyright (C) 2019 iopsys Software Solutions AB -* Author: Omar Kallel -* Imen Bhiri -*/ + * Copyright (C) 2019 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author Imen Bhiri + * Omar Kallel + * + */ #include #include @@ -16,11 +17,6 @@ #include #include "dmentry.h" #include "upnp.h" -#include - -static int fetch_discovery_devices = 0; -static int fetch_files_infos = 0; -LIST_HEAD(file_info_list); /* *** Device.UPnP. *** */ DMOBJ tUPnPObj[] = { @@ -240,30 +236,35 @@ int get_service_linker(char *refparam, struct dmctx *dmctx, void *data, char *in /*************************************************************/ int browseUPnPDiscoveryRootDeviceInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) { - json_object *res, *devices, *device; + json_object *res = NULL, *devices = NULL, *device = NULL; struct upnpdiscovery upnp_dev = {}; char *descurl = NULL, *st = NULL, *usn = NULL, *is_root_device = NULL, *instance = NULL, *instnbr = NULL, *v = NULL; - char **stparams= NULL, **uuid, **urn; - int i, id=0, length; + char **stparams = NULL, **uuid, **urn; + int i; + size_t length; struct uci_section* dmmap_sect= NULL; dmubus_call("upnpc", "discovery", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) + return 0; json_object_object_get_ex(res, "devices", &devices); + if (devices == NULL) + return 0; size_t nbre_devices = json_object_array_length(devices); if(nbre_devices>0){ check_create_dmmap_package("dmmap_upnp"); for(i=0; i0){ @@ -303,9 +309,9 @@ int browseUPnPDiscoveryDeviceInst(struct dmctx *dmctx, DMNODE *parent_node, void descurl = dmjson_get_value(device, 1, "descurl"); st = dmjson_get_value(device, 1, "st"); usn = dmjson_get_value(device, 1, "usn"); - stparams= strsplit_by_str(usn, "::"); - uuid= strsplit(stparams[0], ":", &lengthuuid); - urn= strsplit(stparams[1], ":", &lengthurn); + stparams = strsplit_by_str(usn, "::"); + uuid = strsplit(stparams[0], ":", &lengthuuid); + urn = strsplit(stparams[1], ":", &lengthurn); dmasprintf(&upnp_dev.descurl, "%s", descurl?descurl:""); dmasprintf(&upnp_dev.st, "%s", st?st:""); dmasprintf(&upnp_dev.usn, "%s", usn?usn:""); @@ -315,7 +321,7 @@ int browseUPnPDiscoveryDeviceInst(struct dmctx *dmctx, DMNODE *parent_node, void dmuci_add_section_bbfdm("dmmap_upnp", "upnp_device", &dmmap_sect, &v); DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "uuid", uuid[1]); } - upnp_dev.dmmap_sect= dmmap_sect; + upnp_dev.dmmap_sect = dmmap_sect; instance = handle_update_instance(1, dmctx, &instnbr, update_instance_alias_bbfdm, 3, dmmap_sect, "upnp_evice_instance", "upnp_device_alias"); if (DM_LINK_INST_OBJ(dmctx, parent_node, &upnp_dev, instance) == DM_STOP) @@ -327,27 +333,32 @@ int browseUPnPDiscoveryDeviceInst(struct dmctx *dmctx, DMNODE *parent_node, void int browseUPnPDiscoveryServiceInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) { - json_object *res, *services, *service; + json_object *res = NULL, *services = NULL, *service = NULL; struct upnpdiscovery upnp_dev = {}; - char *descurl = NULL, *st = NULL, *usn = NULL, *is_root_device = NULL, *instance = NULL, *instnbr = NULL, *v = NULL; - char **stparams= NULL, **uuid, **urn; - int i, id=0, lengthuuid, lengthurn; - struct uci_section* dmmap_sect= NULL; + char *descurl = NULL, *st = NULL, *usn = NULL, *instance = NULL, *instnbr = NULL, *v = NULL; + char **stparams = NULL, **uuid, **urn; + int i; + size_t lengthuuid, lengthurn; + struct uci_section* dmmap_sect = NULL; dmubus_call("upnpc", "discovery", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) + return 0; json_object_object_get_ex(res, "services", &services); + if (services == NULL) + return 0; size_t nbre_services = json_object_array_length(services); if(nbre_services>0){ check_create_dmmap_package("dmmap_upnp"); for(i=0; i0){ check_create_dmmap_package("dmmap_upnp"); for(i=0; i0){ check_create_dmmap_package("dmmap_upnp"); for(i=0; i0){ @@ -470,7 +490,7 @@ int browseUPnPDescriptionServiceInstanceInst(struct dmctx *dmctx, DMNODE *parent dmuci_add_section_bbfdm("dmmap_upnp", "upnp_service_inst", &dmmap_sect, &v); DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "serviceid", dmjson_get_value(service_inst, 1, "serviceId")); } - upnp_services_inst.dmmap_sect= dmmap_sect; + upnp_services_inst.dmmap_sect = dmmap_sect; instance = handle_update_instance(1, dmctx, &instnbr, update_instance_alias_bbfdm, 3, dmmap_sect, "upnp_service_inst_instance", "upnp_service_inst_alias"); if (DM_LINK_INST_OBJ(dmctx, parent_node, &upnp_services_inst, instance) == DM_STOP) @@ -479,7 +499,6 @@ int browseUPnPDescriptionServiceInstanceInst(struct dmctx *dmctx, DMNODE *parent } return 0; } - /************************************************************* * GET & SET PARAM /*************************************************************/ @@ -786,9 +805,18 @@ int get_UPnPDiscovery_RootDeviceNumberOfEntries(char *refparam, struct dmctx *ct { int nbre = 0, i; char *is_root_device = NULL; - json_object *res, *devices, *device; + json_object *res = NULL, *devices = NULL, *device = NULL; + dmubus_call("upnpc", "discovery", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) { + *value = "0"; + return 0; + } json_object_object_get_ex(res, "devices", &devices); + if (devices == NULL) { + *value = "0"; + return 0; + } size_t nbre_devices = json_object_array_length(devices); if(nbre_devices>0){ for(i=0; iuuid; return 0; } int get_UPnPDiscoveryRootDevice_USN(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->usn; return 0; } @@ -851,7 +897,7 @@ int get_UPnPDiscoveryRootDevice_LeaseTime(char *refparam, struct dmctx *ctx, voi int get_UPnPDiscoveryRootDevice_Location(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->descurl; return 0; } @@ -882,14 +928,14 @@ int get_UPnPDiscoveryDevice_Status(char *refparam, struct dmctx *ctx, void *data int get_UPnPDiscoveryDevice_UUID(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->uuid; return 0; } int get_UPnPDiscoveryDevice_USN(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->usn; return 0; } @@ -902,7 +948,7 @@ int get_UPnPDiscoveryDevice_LeaseTime(char *refparam, struct dmctx *ctx, void *d int get_UPnPDiscoveryDevice_Location(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->descurl; return 0; } @@ -933,7 +979,7 @@ int get_UPnPDiscoveryService_Status(char *refparam, struct dmctx *ctx, void *dat int get_UPnPDiscoveryService_USN(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->usn; return 0; } @@ -946,7 +992,7 @@ int get_UPnPDiscoveryService_LeaseTime(char *refparam, struct dmctx *ctx, void * int get_UPnPDiscoveryService_Location(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; *value= dev->descurl; return 0; } @@ -971,7 +1017,7 @@ int get_UPnPDiscoveryService_LastUpdate(char *refparam, struct dmctx *ctx, void int get_UPnPDiscoveryService_ParentDevice(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnpdiscovery *dev= (struct upnp_device *)data; + struct upnpdiscovery *dev = (struct upnpdiscovery *)data; char *rootdevlink = NULL, *devlink = NULL; adm_entry_get_linker_param(ctx, dm_print_path("%s%cUPnP%cDiscovery%cRootDevice%c", dmroot, dm_delim, dm_delim, dm_delim, dm_delim), dev->uuid, &rootdevlink); if(rootdevlink != NULL){ @@ -990,8 +1036,17 @@ int get_UPnPDiscoveryService_ParentDevice(char *refparam, struct dmctx *ctx, voi int get_UPnPDescription_DeviceDescriptionNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { json_object *res, *descriptions; + dmubus_call("upnpc", "description", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) { + *value = "0"; + return 0; + } json_object_object_get_ex(res, "descriptions", &descriptions); + if (descriptions == NULL) { + *value = "0"; + return 0; + } size_t nbre_descriptions = json_object_array_length(descriptions); dmasprintf(value, "%d", nbre_descriptions); return 0; @@ -1000,8 +1055,17 @@ int get_UPnPDescription_DeviceDescriptionNumberOfEntries(char *refparam, struct int get_UPnPDescription_DeviceInstanceNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { json_object *res, *devicesinstances; + dmubus_call("upnpc", "description", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) { + *value = "0"; + return 0; + } json_object_object_get_ex(res, "devicesinstances", &devicesinstances); + if (devicesinstances == NULL) { + *value = "0"; + return 0; + } size_t nbre_devinstances = json_object_array_length(devicesinstances); dmasprintf(value, "%d", nbre_devinstances); return 0; @@ -1010,8 +1074,17 @@ int get_UPnPDescription_DeviceInstanceNumberOfEntries(char *refparam, struct dmc int get_UPnPDescription_ServiceInstanceNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { json_object *res, *servicesinstances; + dmubus_call("upnpc", "description", UBUS_ARGS{{}}, 0, &res); + if (res == NULL) { + *value = "0"; + return 0; + } json_object_object_get_ex(res, "servicesinstances", &servicesinstances); + if (servicesinstances == NULL) { + *value = "0"; + return 0; + } size_t nbre_servinstances = json_object_array_length(servicesinstances); dmasprintf(value, "%d", nbre_servinstances); return 0; @@ -1058,7 +1131,7 @@ int get_UPnPDescriptionDeviceInstance_ParentDevice(char *refparam, struct dmctx int get_UPnPDescriptionDeviceInstance_DiscoveryDevice(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - struct upnp_device_inst *upnpdevinst= (struct upnp_device_inst *)data; + struct upnp_device_inst *upnpdevinst = (struct upnp_device_inst *)data; char **udnarray = NULL; char *rootdevlink = NULL, *devlink = NULL; int length; diff --git a/dmtree/tr181/upnp.h b/dmtree/tr181/upnp.h index ae80146a..c262d616 100644 --- a/dmtree/tr181/upnp.h +++ b/dmtree/tr181/upnp.h @@ -1,12 +1,14 @@ /* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 2 of the License, or -* (at your option) any later version. -* -* Copyright (C) 2019 iopsys Software Solutions AB -* Author: Amin Ben Ramdhane -*/ + * Copyright (C) 2019 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author Imen Bhiri + * Omar Kallel + * + */ #ifndef __UPNP_H #define __UPNP_H