From 0895db688fac9d03883b7be0be09806fedb7b0e6 Mon Sep 17 00:00:00 2001 From: Omar Kallel Date: Thu, 17 Dec 2020 12:10:10 +0100 Subject: [PATCH] Make check_value_change independent from libbbfdm --- README.md | 84 +-------------------------- cwmp.c | 7 +-- datamodel_interface.c | 13 ++++- event.c | 119 ++++++++++++++++++++++++++------------ inc/backupSession.h | 2 +- inc/common.h | 4 +- inc/cwmp.h | 3 +- inc/cwmp_uci.h | 11 ++++ inc/datamodel_interface.h | 9 ++- inc/jshn.h | 13 ++++- inc/ubus.h | 5 +- jshn.c | 54 +++++++++++++++++ ubus.c | 1 + 13 files changed, 192 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index 12973c3..4ae19e3 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ It defines **acs configuration** (like acs url, acs username, etc...). The possi | `insecure_enable` | boolean | if set to **1**, the CPE checks the validity of the ACS certificates. | | `http_disable_100continue` | boolean | if set to **1**, disables the http 100 continue behaviour. | | `dhcp_url` | string | the **url** of ACS server received from the DHCP server Option 43 when **'dhcp_discovery'** option is enabled. This option is updated automatically by the daemon. | +| `ip_version` | string | ip_version of ConnectionRequestURL ### cwmp cpe section ### @@ -295,55 +296,12 @@ root@iopsys:~# icwmpd --help Usage: icwmpd [OPTIONS] -b, --boot-event (CWMP daemon) Start CWMP with BOOT event -g, --get-rpc-methods (CWMP daemon) Start CWMP with GetRPCMethods request to ACS - -c, --command-input (DataModel CLI) Execute data model rpc(s) with commands input - -m, --shell-cli (DataModel CLI) Execute data model RPC command directly from shell. - -a, --alias-based-addressing (DataModel CLI) Alias based addressing supported - -N, --instance-mode-number (DataModel CLI) Instance mode is Number (Enabled by default) - -A, --instance-mode-alias (DataModel CLI) Instance mode is Alias - -M, --amendment (DataModel CLI) Amendment version (Default amendment version is 2) - -U, --upnp (DataModel CLI) Use UPNP data model paths - -u, --user-acl (DataModel CLI) user access level. Default: superadmin - -t, --time-tracking (DataModel CLI) Tracking time of RPC commands - -E, --evaluating-test (DataModel CLI) Evaluating test format - -f, --file (DataModel CLI) Execute data model rpc(s) from file - -w, --wep (WEP KEY GEN) Generate wep keys -h, --help Display this help text -v, --version Display the version root@iopsys:~# ``` -For example `'-m'` option is used to execute the datamodel **(libbbfdm)** RPC method through CLI. - -if -m is followed by an integer different from 0, so the command result is displayed as output as following: - -``` -root@iopsys:~# icwmpd -m 1 get_value Device.Time. -{ "parameter": "Device.Time.CurrentLocalTime", "value": "2020-07-08T08:00:39Z", "type": "xsd:dateTime" } -{ "parameter": "Device.Time.Enable", "value": "1", "type": "xsd:boolean" } -{ "parameter": "Device.Time.LocalTimeZone", "value": "CET-1CEST,M3.5.0,M10.5.0/3", "type": "xsd:string" } -{ "parameter": "Device.Time.NTPServer1", "value": "ntp1.sth.netnod.se", "type": "xsd:string" } -{ "parameter": "Device.Time.NTPServer2", "value": "ntp1.gbg.netnod.se", "type": "xsd:string" } -{ "parameter": "Device.Time.NTPServer3", "value": "", "type": "xsd:string" } -{ "parameter": "Device.Time.NTPServer4", "value": "", "type": "xsd:string" } -{ "parameter": "Device.Time.NTPServer5", "value": "", "type": "xsd:string" } -{ "parameter": "Device.Time.Status", "value": "Synchronized", "type": "xsd:string" } -{ "parameter": "Device.Time.X_IOPSYS_EU_LocalTimeZoneName", "value": "Europe/Stockholm", "type": "xsd:string" } -{ "parameter": "Device.Time.X_IOPSYS_EU_SourceInterface", "value": "", "type": "xsd:string" } -root@iopsys:~# -root@iopsys:~# icwmpd -m 1 set_value key Device.Time.Enable 0 -{ "status": "1" } -root@iopsys:~# -``` -if -m is followed by 0 so no output is displayed in the terminal: - -``` -root@iopsys:~# icwmpd -m 0 set_value setkey Device.Time.Enable 1 -root@iopsys:~# - -``` -And in this case when -m is followed by 0, the icwmpd command is faster. - -Another way to use icwmp cli is via the script `'icwmp'` that can be runned as follow: +There's an icwmp cli that can be called via the script `'icwmp'` as follow: ``` root@iopsys:~# icwmp get Device.Time. @@ -361,43 +319,6 @@ root@iopsys:~# icwmp get Device.Time. root@iopsys:~# ``` -**NOTE: It's the same, whether to use icwmp script or icwmpd command for connecting to datamodel** - -``` -root@iopsys:~# icwmpd -m 1 inform -{ "parameter": "Device.DeviceInfo.HardwareVersion", "value": "DG400PRIMEA", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.Manufacturer", "value": "iopsys", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ManufacturerOUI", "value": "201F31", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ModelName", "value": "DG400PRIMEA", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ProductClass", "value": "DG400PRIME", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ProvisioningCode", "value": "", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SerialNumber", "value": "J814001008", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SoftwareVersion", "value": "DG400PRIME-A-IOPSYS-4.4.0RC1-191205_1004", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SpecVersion", "value": "1.0", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.AliasBasedAddressing", "value": "false", "type": "xsd:boolean" } -{ "parameter": "Device.ManagementServer.ConnReqJabberID", "value": "", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ConnReqXMPPConnection", "value": "Device.XMPP.Connection.", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ConnectionRequestURL", "value": "http://192.168.117.45:7547/", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ParameterKey", "value": "", "type": "xsd:string" } -root@iopsys:~# -root@iopsys:~# icwmp inform -{ "parameter": "Device.DeviceInfo.HardwareVersion", "value": "DG400PRIMEA", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.Manufacturer", "value": "iopsys", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ManufacturerOUI", "value": "201F31", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ModelName", "value": "DG400PRIMEA", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ProductClass", "value": "DG400PRIME", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.ProvisioningCode", "value": "", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SerialNumber", "value": "J814001008", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SoftwareVersion", "value": "DG400PRIME-A-IOPSYS-4.4.0RC1-191205_1004", "type": "xsd:string" } -{ "parameter": "Device.DeviceInfo.SpecVersion", "value": "1.0", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.AliasBasedAddressing", "value": "false", "type": "xsd:boolean" } -{ "parameter": "Device.ManagementServer.ConnReqJabberID", "value": "", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ConnReqXMPPConnection", "value": "Device.XMPP.Connection.", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ConnectionRequestURL", "value": "http://192.168.117.45:7547/", "type": "xsd:string" } -{ "parameter": "Device.ManagementServer.ParameterKey", "value": "", "type": "xsd:string" } -root@iopsys:~# -``` - ## Dependencies ## To successfully build icwmp, the following libraries are needed: @@ -410,7 +331,6 @@ To successfully build icwmp, the following libraries are needed: | libjson-c | https://s3.amazonaws.com/json-c_releases | MIT | | libopenssl | http://ftp.fi.muni.cz/pub/openssl/source/ | OpenSSL | | libcurl | https://dl.uxnr.de/mirror/curl | MIT | -| libbbfdm | https://dev.iopsys.eu/iopsys/bbf.git | LGPL 2.1 | | libmicroxml | https://dev.freecwmp.org/microxml | LGPL 2.0 | | libpthread | | | diff --git a/cwmp.c b/cwmp.c index 3b4385a..595b6e1 100644 --- a/cwmp.c +++ b/cwmp.c @@ -176,7 +176,7 @@ void cwmp_schedule_session (struct cwmp *cwmp) is_notify = check_value_change(); } if(is_notify>0 || access(DM_ENABLED_NOTIFY, F_OK ) < 0) - cwmp_update_enabled_notify_file(cwmp->conf.instance_mode); + cwmp_update_enabled_notify_file(); cwmp_prepare_value_change(cwmp); free_dm_parameter_all_fromlist(&list_value_change); if ((error = cwmp_move_session_to_session_send (cwmp, session))) { @@ -573,7 +573,7 @@ int run_session_end_func () if (end_session_flag & END_SESSION_SET_NOTIFICATION_UPDATE) { CWMP_LOG (INFO,"SetParameterAttributes end session: update enabled notify file"); - cwmp_update_enabled_notify_file(cwmp_main.conf.instance_mode); + cwmp_update_enabled_notify_file(); } if (end_session_flag & END_SESSION_TRANSACTION_COMMIT) { cwmp_transaction_commit(); @@ -675,7 +675,6 @@ int cwmp_exit(void) int main(int argc, char **argv) { - struct cwmp *cwmp = &cwmp_main; int error; pthread_t periodic_event_thread; @@ -692,7 +691,7 @@ int main(int argc, char **argv) struct sigaction act = {0}; if ((error = cwmp_init(argc, argv, cwmp))) - return error; + return error; CWMP_LOG(INFO,"STARTING ICWMP with PID :%d", getpid()); cwmp->start_time = time(NULL); diff --git a/datamodel_interface.c b/datamodel_interface.c index 93c2b76..35d3ddc 100644 --- a/datamodel_interface.c +++ b/datamodel_interface.c @@ -13,6 +13,9 @@ bool transaction_started = false; int transaction_id = 0; +json_object *old_list_notify = NULL; +json_object *actual_list_notify = NULL; + /* * Transaction Functions */ @@ -299,8 +302,14 @@ char* cwmp_set_parameter_attributes(char* parameter_name, char* notification) /* * Init Notify Function */ -int cwmp_update_enabled_notify_file(int instance_mode) +int cwmp_update_enabled_list_notify(int instance_mode, int notify_type) { - int e = cwmp_ubus_call("usp.raw", "list_notify", CWMP_UBUS_ARGS{{"instance_mode", {.int_val=instance_mode}, UBUS_Integer}}, 1, NULL); + int e; + json_object *list_notif_obj = NULL; + e = cwmp_ubus_call("usp.raw", "list_notify", CWMP_UBUS_ARGS{{"instance_mode", {.int_val=instance_mode}, UBUS_Integer}}, 1, &list_notif_obj); + if (notify_type == OLD_LIST_NOTIFY) + json_object_object_get_ex(list_notif_obj, "parameters", &old_list_notify); + else + json_object_object_get_ex(list_notif_obj, "parameters", &actual_list_notify); return e; } diff --git a/event.c b/event.c index ff5174e..24ae623 100644 --- a/event.c +++ b/event.c @@ -140,7 +140,7 @@ void add_dm_parameter_tolist(struct list_head *head, char *param_name, char *par _list_add(&dm_parameter->list, ilist->prev, ilist); if (param_name) dm_parameter->name = strdup(param_name); if (param_data) dm_parameter->data = strdup(param_data); - if (param_type) dm_parameter->type = param_type ? param_type : "xsd:string"; + if (param_type) dm_parameter->type = strdup(param_type ? param_type : "xsd:string"); } void delete_dm_parameter_fromlist(struct cwmp_dm_parameter *dm_parameter) @@ -148,6 +148,7 @@ void delete_dm_parameter_fromlist(struct cwmp_dm_parameter *dm_parameter) list_del(&dm_parameter->list); free(dm_parameter->name); free(dm_parameter->data); + free(dm_parameter->type); free(dm_parameter); } @@ -265,65 +266,109 @@ void cwmp_lwnotification() FREE(msg_out); } +int cwmp_update_enabled_notify_file() +{ + struct cwmp *cwmp = &cwmp_main; + FILE *fp; + json_object *param_obj = NULL, *param_name_obj = NULL, *value_obj = NULL, *type_obj = NULL, *notification_obj = NULL; + + int e = cwmp_update_enabled_list_notify(cwmp->conf.instance_mode, OLD_LIST_NOTIFY); + if (e) + return 0; + remove(DM_ENABLED_NOTIFY); + + fp = fopen(DM_ENABLED_NOTIFY, "a"); + if (fp == NULL) { + return 0; + } + foreach_jsonobj_in_array(param_obj, old_list_notify) { + json_object_object_get_ex(param_obj, "parameter", ¶m_name_obj); + if (!param_name_obj || strlen((char*)json_object_get_string(param_name_obj))<=0) + continue; + json_object_object_get_ex(param_obj, "value", &value_obj); + json_object_object_get_ex(param_obj, "type", &type_obj); + json_object_object_get_ex(param_obj, "notification", ¬ification_obj); + cwmp_json_fprintf(fp, 4, CWMP_JSON_ARGS{{"parameter", (char*)json_object_get_string(param_name_obj)}, {"notification", notification_obj?(char*)json_object_get_string(notification_obj):""}, {"value", value_obj?(char*)json_object_get_string(value_obj):""}, {"type", type_obj?(char*)json_object_get_string(type_obj):""}}); + } + fclose(fp); + return 1; +} + +void get_parameter_value_from_parameters_list(json_object* list_params_obj, char* parameter_name, struct cwmp_dm_parameter **ret_dm_param) +{ + json_object *param_obj = NULL, *param_name_obj = NULL, *value_obj = NULL, *type_obj = NULL; + + foreach_jsonobj_in_array(param_obj, list_params_obj) { + json_object_object_get_ex(param_obj, "parameter", ¶m_name_obj); + if (!param_name_obj || strlen((char*)json_object_get_string(param_name_obj))<=0) + continue; + if (strcmp((char*)json_object_get_string(param_name_obj), parameter_name) != 0) + continue; + *ret_dm_param = (struct cwmp_dm_parameter*) calloc(1,sizeof(struct cwmp_dm_parameter)); + json_object_object_get_ex(param_obj, "value", &value_obj); + (*ret_dm_param)->name = strdup(parameter_name); + (*ret_dm_param)->data = strdup(value_obj?(char*)json_object_get_string(value_obj):""); + json_object_object_get_ex(param_obj, "type", &type_obj); + (*ret_dm_param)->type = strdup(type_obj?(char*)json_object_get_string(type_obj):""); + break; + } +} + int check_value_change(void) { - int fault; FILE *fp; char buf[512]; char *parameter, *notification = NULL, *value = NULL, *jval = NULL; struct cwmp *cwmp = &cwmp_main; - struct dm_parameter *dm_parameter; - struct dmctx dmctx = {0}; - bool first_iteration =true; + struct cwmp_dm_parameter *dm_parameter = NULL; + json_object *buf_json_obj = NULL; int is_notify = 0; fp = fopen(DM_ENABLED_NOTIFY, "r"); if (fp == NULL) return false; - dm_ctx_init(&dmctx, DM_CWMP, cwmp->conf.amd_version, cwmp->conf.instance_mode); + cwmp_update_enabled_list_notify(cwmp->conf.instance_mode, ACTUAL_LIST_NOTIFY); + while (fgets(buf, 512, fp) != NULL) { - if (!first_iteration) - dm_ctx_init_list_parameter(&dmctx); - first_iteration = false; - dmctx.in_param = ""; int len = strlen(buf); if (len) buf[len-1] = '\0'; - dmjson_parse_init(buf); - dmjson_get_string("parameter", &jval); - if(!jval || jval[0] == '\0') + buf_json_obj = json_tokener_parse((char*)buf); + cwmp_json_obj_init(buf, &buf_json_obj); + cwmp_json_get_string(buf_json_obj, "parameter", ¶meter); + if(parameter == NULL || parameter[0] == '\0') continue; - parameter = strdup(jval?jval:""); - dmjson_get_string("value", &jval); - value = strdup(jval?jval:""); - dmjson_get_string("notification", &jval); - notification = strdup(jval?jval:""); - dmjson_parse_fini(); - fault = dmentry_get_parameter_leaf_value(&dmctx, parameter); - if (!fault && dmctx.list_parameter.next != &dmctx.list_parameter) { - dm_parameter = list_entry(dmctx.list_parameter.next, struct dm_parameter, list); - if (notification && strlen(notification)>0 && notification[0] >= '1' && strcmp(dm_parameter->data, value) != 0){ - if (notification[0] == '1' || notification[0] == '2') - add_list_value_change(parameter, dm_parameter->data, dm_parameter->type); - if (notification[0] >= '3' ) - add_lw_list_value_change(parameter, dm_parameter->data, dm_parameter->type); + cwmp_json_get_string(buf_json_obj, "value", &value); + cwmp_json_get_string(buf_json_obj, "notification", ¬ification); - if (notification[0] == '1') - is_notify |= NOTIF_PASSIVE; - if (notification[0] == '2') - is_notify |= NOTIF_ACTIVE; + cwmp_json_obj_clean(&buf_json_obj); + get_parameter_value_from_parameters_list(actual_list_notify, parameter, &dm_parameter); + if (dm_parameter == NULL) + continue; - if (notification[0] == '5' || notification[0] == '6') - is_notify |= NOTIF_LW_ACTIVE; - } + if (notification && (strlen(notification) > 0) && (notification[0] >= '1') && (strcmp(dm_parameter->data, value) != 0)){ + if (notification[0] == '1' || notification[0] == '2') + add_list_value_change(parameter, dm_parameter->data, dm_parameter->type); + if (notification[0] >= '3' ) + add_lw_list_value_change(parameter, dm_parameter->data, dm_parameter->type); + + if (notification[0] == '1') + is_notify |= NOTIF_PASSIVE; + if (notification[0] == '2') + is_notify |= NOTIF_ACTIVE; + + if (notification[0] == '5' || notification[0] == '6') + is_notify |= NOTIF_LW_ACTIVE; } FREE(value); FREE(notification); FREE(parameter); - dm_ctx_clean_list_parameter(&dmctx); + FREE(dm_parameter->name); + FREE(dm_parameter->data); + FREE(dm_parameter->type); + FREE(dm_parameter); } - dm_ctx_clean(&dmctx); fclose(fp); return is_notify; } @@ -645,7 +690,7 @@ void *thread_periodic_check_notify (void *v) pthread_mutex_lock(&(cwmp->mutex_session_send)); is_notify = check_value_change(); if (is_notify > 0) - cwmp_update_enabled_notify_file(cwmp->conf.instance_mode); + cwmp_update_enabled_notify_file(); pthread_mutex_unlock(&(cwmp->mutex_session_send)); if (is_notify & NOTIF_ACTIVE) send_active_value_change(); diff --git a/inc/backupSession.h b/inc/backupSession.h index fe7a0ff..e8ffd28 100644 --- a/inc/backupSession.h +++ b/inc/backupSession.h @@ -4,7 +4,7 @@ * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * - * Copyright (C) 2013-2019 iopsys Software Solutions AB + * Copyright (C) 2013-2020 iopsys Software Solutions AB * Author Mohamed Kallel * Author Ahmed Zribi * diff --git a/inc/common.h b/inc/common.h index 0d0f84b..31bbea0 100644 --- a/inc/common.h +++ b/inc/common.h @@ -1,8 +1,8 @@ #ifndef __COMMON_H #define __COMMON_H #include -#include -#include + +#define DM_ENABLED_NOTIFY "/etc/icwmpd/.dm_enabled_notify" enum cwmp_ret_err { CWMP_OK, /* No Error */ CWMP_GEN_ERR, /* General Error */ diff --git a/inc/cwmp.h b/inc/cwmp.h index d4695c6..06a2dc8 100644 --- a/inc/cwmp.h +++ b/inc/cwmp.h @@ -7,7 +7,7 @@ * Copyright (C) 2013-2019 iopsys Software Solutions AB * Author Mohamed Kallel * Author Ahmed Zribi - * + * Author Omar Kallel */ #ifndef _CWMP_H__ @@ -333,4 +333,5 @@ void cwmp_set_end_session(unsigned int flag); bool event_exist_in_list(struct cwmp *cwmp, int event); void *thread_periodic_check_notify (void *v); void free_dm_parameter_all_fromlist(struct list_head *list); +int cwmp_update_enabled_notify_file(); #endif /* _CWMP_H__ */ diff --git a/inc/cwmp_uci.h b/inc/cwmp_uci.h index 9b87c81..4c5fc85 100644 --- a/inc/cwmp_uci.h +++ b/inc/cwmp_uci.h @@ -1,3 +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) 2013-2020 iopsys Software Solutions AB + * Author Omar Kallel + * + */ + #ifndef __CWMPUCI_H #define __CWMPUCI_H #include diff --git a/inc/datamodel_interface.h b/inc/datamodel_interface.h index 8e4fe2f..67f2359 100644 --- a/inc/datamodel_interface.h +++ b/inc/datamodel_interface.h @@ -9,6 +9,13 @@ #define DM_ROOT_OBJ "Device." extern bool transaction_started; extern int transaction_id; +extern json_object *old_list_notify; +extern json_object *actual_list_notify; + +enum notify_type { + OLD_LIST_NOTIFY, + ACTUAL_LIST_NOTIFY +}; int cwmp_transaction_start(char *app); int cwmp_transaction_commit(); @@ -22,6 +29,6 @@ char* cwmp_delete_object(char* object_name, char* key); char* cwmp_get_parameter_names(char* object_name, bool next_level, json_object **parameters); char* cwmp_get_parameter_attributes(char* parameter_name, json_object **parameters); char* cwmp_set_parameter_attributes(char* parameter_name, char* notification); -int cwmp_update_enabled_notify_file(int instance_mode); +int cwmp_update_enabled_list_notify(int instance_moden, int notify_type); #endif /* SRC_DATAMODELIFACE_H_ */ diff --git a/inc/jshn.h b/inc/jshn.h index 896e3bb..1382c82 100644 --- a/inc/jshn.h +++ b/inc/jshn.h @@ -7,12 +7,19 @@ * Copyright (C) 2013-2019 iopsys Software Solutions AB * Author Mohamed Kallel * Author Ahmed Zribi - * + * Author: Omar Kallel */ #ifndef _JSHN_H__ #define _JSHN_H__ +#include +struct cwmp_json_arg { + char *key; + char *val; +}; + +#define CWMP_JSON_ARGS (struct cwmp_json_arg[]) int cwmp_handle_downloadFault(char *msg); int cwmp_handle_uploadFault(char *msg); int cwmp_handle_dustate_changeFault(char *msg); @@ -24,6 +31,10 @@ int cwmp_handle_getParamAttributes(char *msg); int cwmp_handle_setParamAttributes(char *msg); int cwmp_handle_addObject(char *msg); int cwmp_handle_delObject(char *msg); +void cwmp_json_fprintf(FILE *fp, int argc, struct cwmp_json_arg cwmp_arg[]); +void cwmp_json_get_string(json_object *obj, char* key, char** value); +void cwmp_json_obj_init(char *str, json_object **obj); +void cwmp_json_obj_clean(json_object **obj); #define foreach_jsonobj_in_array(param_obj, parameters) \ int k, array_length = json_object_array_length(parameters); \ diff --git a/inc/ubus.h b/inc/ubus.h index 140a423..b546f83 100644 --- a/inc/ubus.h +++ b/inc/ubus.h @@ -4,10 +4,11 @@ * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * + * Copyright (C) 2013-2020 iopsys Software Solutions AB + * Author Omar Kallel + * * Copyright (C) 2012 Luka Perkov */ - - #ifndef _FREECWMP_UBUS_H__ #define _FREECWMP_UBUS_H__ #include diff --git a/jshn.c b/jshn.c index 47b997c..b968c8c 100644 --- a/jshn.c +++ b/jshn.c @@ -178,3 +178,57 @@ error: jshn_message_delete(); return -1; } + +void cwmp_json_obj_init(char *str, json_object **obj) +{ + if (*obj) { + json_object_put(*obj); + *obj = NULL; + } + *obj = json_tokener_parse(str); +} + +void cwmp_json_obj_clean(json_object **obj) +{ + if (*obj) { + json_object_put(*obj); + *obj = NULL; + } +} +void cwmp_add_json_obj(json_object *json_obj_out, char *object, char *string) +{ + if (object != NULL && string != NULL) { + json_object *json_obj_tmp = json_object_new_string(string); + json_object_object_add(json_obj_out, object, json_obj_tmp); + } +} + +void cwmp_json_fprintf(FILE *fp, int argc, struct cwmp_json_arg cwmp_arg[]) +{ + int i; + char *arg; + json_object *json_obj_out = json_object_new_object(); + if (json_obj_out == NULL) + return; + + if (argc) { + for (i = 0; i < argc; i++) { + cwmp_add_json_obj(json_obj_out, cwmp_arg[i].key, cwmp_arg[i].val); + } + arg = (char *)json_object_to_json_string(json_obj_out); + fprintf(fp, "%s\n", arg); + } + + json_object_put(json_obj_out); +} + +void cwmp_json_get_string(json_object *obj, char* key, char** value) +{ + json_object *key_obj = NULL; + json_object_object_get_ex(obj, key, &key_obj); + *value = strdup(key_obj?(char*)json_object_get_string(key_obj):""); + if (key_obj) { + json_object_put(key_obj); + key_obj = NULL; + } +} diff --git a/ubus.c b/ubus.c index 2c17c59..a13efe6 100644 --- a/ubus.c +++ b/ubus.c @@ -7,6 +7,7 @@ * Copyright (C) 2013-2019 iopsys Software Solutions AB * Author Mohamed Kallel * Author Ahmed Zribi + * Author Omar Kallel * Copyright (C) 2012 Luka Perkov */