From c7be1ab765908d38f53be8cb74feb04be1d8da8e Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Thu, 13 Apr 2023 14:45:44 +0200 Subject: [PATCH] Remove dependency from router.system method --- docs/guide/json_plugin_v1.md | 4 +- libbbf_api/dmbbf.c | 4 +- libbbf_dm/dmtree/json/tr181.json | 39 ++------------- libbbf_dm/dmtree/tr181/deviceinfo.c | 76 ++++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 51 deletions(-) diff --git a/docs/guide/json_plugin_v1.md b/docs/guide/json_plugin_v1.md index afc78dc4..3954b05e 100644 --- a/docs/guide/json_plugin_v1.md +++ b/docs/guide/json_plugin_v1.md @@ -120,10 +120,10 @@ In above example the object `CWMPManagementServer` has a dependency over the UCI one file => "file:/etc/config/network" multiple files => "file:/etc/config/network,/lib/netifd/proto/dhcp.sh" one ubus => "ubus:router.network" (with method : "ubus:router.network->hosts") -multiple ubus => "ubus:router.system->info,dsl->status,wifi" +multiple ubus => "ubus:system->info,dsl->status,wifi" one package => "opkg:icwmp" multiple packages => "opkg:icwmp,obuspa" -common (files, ubus and package) => "file:/etc/config/network,/etc/config/dhcp;ubus:router.system,dsl->status;opkg:icwmp" +common (files, ubus and package) => "file:/etc/config/network,/etc/config/dhcp;ubus:system,dsl->status;opkg:icwmp" > Note: `dependency` can only be defined for datamodel objects and it can't be used for any leaf components (parameters/commands/events). diff --git a/libbbf_api/dmbbf.c b/libbbf_api/dmbbf.c index 779f1b61..d4bd2c1e 100644 --- a/libbbf_api/dmbbf.c +++ b/libbbf_api/dmbbf.c @@ -381,10 +381,10 @@ static bool check_dependency(const char *conf_obj) /* one file => "file:/etc/config/network" */ /* multiple files => "file:/etc/config/network,/lib/netifd/proto/dhcp.sh" */ /* one ubus => "ubus:router.network" (with method : "ubus:router.network->hosts") */ - /* multiple ubus => "ubus:router.system->info,dsl->status,wifi" */ + /* multiple ubus => "ubus:system->info,dsl->status,wifi" */ /* one package => "opkg:icwmp" */ /* multiple packages => "opkg:icwmp,obuspa" */ - /* common (files, ubus and opkg) => "file:/etc/config/network,/etc/config/dhcp;ubus:router.system,dsl->status;opkg:icwmp" */ + /* common (files, ubus and opkg) => "file:/etc/config/network,/etc/config/dhcp;ubus:system,dsl->status;opkg:icwmp" */ char *pch = NULL, *spch = NULL; char conf_list[512] = {0}; diff --git a/libbbf_dm/dmtree/json/tr181.json b/libbbf_dm/dmtree/json/tr181.json index 0efc27e7..c07672ce 100644 --- a/libbbf_dm/dmtree/json/tr181.json +++ b/libbbf_dm/dmtree/json/tr181.json @@ -1209,18 +1209,7 @@ ], "description": "The total physical volatile RAM, in <>, installed on the device.", "datatype": "unsignedInt", - "unit": "KiB", - "mapping": [ - { - "type": "ubus", - "ubus": { - "object": "router.system", - "method": "memory", - "args": {}, - "key": "total" - } - } - ] + "unit": "KiB" }, "Free": { "type": "unsignedInt", @@ -1233,18 +1222,7 @@ ], "description": "The free physical volatile RAM, in <>, currently available on the device.", "datatype": "unsignedInt", - "unit": "KiB", - "mapping": [ - { - "type": "ubus", - "ubus": { - "object": "router.system", - "method": "memory", - "args": {}, - "key": "free" - } - } - ] + "unit": "KiB" }, "TotalPersistent": { "type": "unsignedInt", @@ -1299,18 +1277,7 @@ "max": 100 } ], - "unit": "percent", - "mapping": [ - { - "type": "ubus", - "ubus": { - "object": "router.system", - "method": "process", - "args": {}, - "key": "cpu_usage" - } - } - ] + "unit": "percent" }, "ProcessNumberOfEntries": { "type": "unsignedInt", diff --git a/libbbf_dm/dmtree/tr181/deviceinfo.c b/libbbf_dm/dmtree/tr181/deviceinfo.c index 7e208584..d442ff66 100644 --- a/libbbf_dm/dmtree/tr181/deviceinfo.c +++ b/libbbf_dm/dmtree/tr181/deviceinfo.c @@ -34,6 +34,13 @@ struct process_entry { int instance; }; +typedef struct jiffy_counts_t { + unsigned long long usr, nic, sys, idle; + unsigned long long iowait, irq, softirq, steal; + unsigned long long total; + unsigned long long busy; +} jiffy_counts_t; + struct Supported_Data_Models { char url[128]; @@ -67,6 +74,57 @@ static int get_linker_process(char* refparam, struct dmctx *ctx, void *data, cha /************************************************************* * COMMON FUNCTIONS **************************************************************/ +static void get_jif_val(jiffy_counts_t *p_jif) +{ + FILE *file = NULL; + char line[128]; + int ret; + + if ((file = fopen("/proc/stat", "r"))) { + while(fgets(line, sizeof(line), file) != NULL) + { + remove_new_line(line); + ret = sscanf(line, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle, + &p_jif->iowait, &p_jif->irq, &p_jif->softirq, &p_jif->steal); + + if (ret >= 4) { + p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle + + p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal; + + p_jif->busy = p_jif->total - p_jif->idle - p_jif->iowait; + break; + } + } + fclose(file); + } +} + +static unsigned int get_cpu_load(jiffy_counts_t *prev_jif, jiffy_counts_t *cur_jif) +{ + unsigned total_diff, cpu; + + total_diff = (unsigned)(cur_jif->total - prev_jif->total); + + if (total_diff == 0) + total_diff = 1; + + cpu = 100 * (unsigned)(cur_jif->busy - prev_jif->busy) / total_diff; + + return cpu; +} + +static unsigned int get_cpu_usage(void) +{ + jiffy_counts_t prev_jif = {0}; + jiffy_counts_t cur_jif = {0}; + + get_jif_val(&prev_jif); + usleep(100000); + get_jif_val(&cur_jif); + + return get_cpu_load(&prev_jif, &cur_jif); +} + static bool is_update_process_allowed(void) { char *tr069_status = NULL; @@ -1080,23 +1138,23 @@ static int get_DeviceInfoFirmwareImage_Status(char *refparam, struct dmctx *ctx, return 0; } -/*#Device.DeviceInfo.MemoryStatus.Total!UBUS:router.system/memory//total*/ static int get_memory_status_total(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) { json_object *res = NULL; - dmubus_call("router.system", "memory", UBUS_ARGS{{}}, 0, &res); + dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); DM_ASSERT(res, *value = "0"); - *value = dmjson_get_value(res, 1, "total"); + char *total = dmjson_get_value(res, 2, "memory", "total"); + dmasprintf(value, "%lu", DM_STRTOUL(total) / 1024); return 0; } -/*#Device.DeviceInfo.MemoryStatus.Free!UBUS:router.system/memory//free*/ static int get_memory_status_free(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) { json_object *res = NULL; - dmubus_call("router.system", "memory", UBUS_ARGS{{}}, 0, &res); + dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); DM_ASSERT(res, *value = "0"); - *value = dmjson_get_value(res, 1, "free"); + char *free = dmjson_get_value(res, 2, "memory", "free"); + dmasprintf(value, "%lu", DM_STRTOUL(free) / 1024); return 0; } @@ -1126,13 +1184,9 @@ static int get_memory_status_free_persistent(char* refparam, struct dmctx *ctx, return 0; } -/*#Device.DeviceInfo.ProcessStatus.CPUUsage!UBUS:router.system/process//cpu_usage*/ static int get_process_cpu_usage(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - json_object *res = NULL; - dmubus_call("router.system", "process", UBUS_ARGS{{}}, 0, &res); - DM_ASSERT(res, *value = "0"); - *value = dmjson_get_value(res, 1, "cpu_usage"); + dmasprintf(value, "%u", get_cpu_usage()); return 0; }