From 2eeb0990616df846da55a154d2fed05c828d168f Mon Sep 17 00:00:00 2001 From: Suvendhu Hansa Date: Tue, 5 Nov 2024 20:10:20 +0530 Subject: [PATCH] Fix cwmp notification sending --- src/notifications.c | 110 ++++++++++++++++++++++++++++++++++++++++++++ src/notifications.h | 1 + src/rpc.c | 7 ++- src/session.c | 10 ---- src/session.h | 41 ++++++++--------- src/ubus_utils.c | 1 - 6 files changed, 135 insertions(+), 35 deletions(-) diff --git a/src/notifications.c b/src/notifications.c index c4e8ee6..bd3ebe9 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -11,6 +11,7 @@ #include #include +#include #include "notifications.h" #include "uci_utils.h" @@ -30,6 +31,7 @@ LIST_HEAD(list_param_obj_notify); static int cr_url_retry = 3; +static void update_notify_file_line(FILE *notify_file, char *param_name, char *param_type, char *param_value, int notification); static void create_list_param_leaf_notify(struct list_head *, void (*fp)(FILE *, char *, char *, char *, int), FILE*); static void send_active_value_change(void); static void add_list_value_change(const char *param_name, const char *param_data, const char *param_type); @@ -402,6 +404,113 @@ void create_list_param_leaf_notify(struct list_head *list_param_leaf_notify, voi } } +static void load_notify_values(struct list_head *notify_list) +{ + if (notify_list == NULL) + return; + + // cppcheck-suppress cert-MSC24-C + FILE *fp = fopen(DM_ENABLED_NOTIFY, "r"); + if (fp == NULL) + return; + + char line[2048] = {0}; + + while (fgets(line, sizeof(line), fp)) { + const char *p_name = NULL, *p_val = NULL, *p_type = NULL; + int p_notif = 0; + + json_object *jobj = json_tokener_parse(line); + if (jobj == NULL) + continue; + + json_object *p_obj = json_object_object_get(jobj, "parameter"); + json_object *v_obj = json_object_object_get(jobj, "value"); + json_object *n_obj = json_object_object_get(jobj, "notification"); + json_object *t_obj = json_object_object_get(jobj, "type"); + + if (p_obj == NULL || v_obj == NULL || n_obj == NULL || t_obj == NULL) { + json_object_put(jobj); + jobj = NULL; + continue; + } + + p_name = json_object_get_string(p_obj); + p_val = json_object_get_string(v_obj); + p_notif = json_object_get_int(n_obj); + p_type = json_object_get_string(t_obj); + + add_dm_parameter_to_list(notify_list, p_name, p_val, p_type, p_notif, 0); + + json_object_put(jobj); + jobj = NULL; + } + + fclose(fp); +} + +static void apply_notify_values(struct list_head *notify_list) +{ + if (notify_list == NULL) + return; + + if (list_empty(notify_list)) + return; + + // cppcheck-suppress cert-MSC24-C + FILE *fp = fopen(DM_ENABLED_NOTIFY, "w"); + if (fp == NULL) + return; + + struct cwmp_dm_parameter *notif_value = NULL; + list_for_each_entry(notif_value, notify_list, list) { + update_notify_file_line(fp, notif_value->name, notif_value->type, notif_value->value, notif_value->notification); + } + + fclose(fp); +} + +void cwmp_update_notify_values(struct list_head *parameter_values_list) +{ + struct cwmp_dm_parameter *param_value = NULL, *notif_value = NULL; + bool value_changed = false; + + LIST_HEAD(notify_list); + + load_notify_values(¬ify_list); + + if (list_empty(¬ify_list)) + return; + + list_for_each_entry(param_value, parameter_values_list, list) { + if (CWMP_STRLEN(param_value->name) == 0) + continue; + + char *inst_path = NULL; + if (CWMP_OK != instantiate_param_name(param_value->name, &inst_path)) + continue; + + list_for_each_entry(notif_value, ¬ify_list, list) { + if (CWMP_STRCMP(inst_path, notif_value->name) == 0) { + if (CWMP_STRCMP(param_value->value, notif_value->value) != 0) { + FREE(notif_value->value); + notif_value->value = strdup(param_value->value); + value_changed = true; + } + + break; + } + } + FREE(inst_path); + } + + if (value_changed == true) { + apply_notify_values(¬ify_list); + } + + cwmp_free_all_dm_parameter_list(¬ify_list); +} + void init_list_param_notify() { int i; @@ -564,6 +673,7 @@ static void get_parameter_value_from_parameters_list(struct list_head *params_li continue; if (strcmp(parameter_name, param_value->name) != 0) continue; + *value = strdup(param_value->value ? param_value->value : ""); *type = strdup(param_value->type ? param_value->type : ""); } diff --git a/src/notifications.h b/src/notifications.h index ada067e..1b0285d 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -52,4 +52,5 @@ void init_list_param_notify(); void reinit_list_param_notify(); void cwmp_prepare_value_change(void); void trigger_periodic_notify_check(); +void cwmp_update_notify_values(struct list_head *parameter_values_list); #endif /* SRC_INC_NOTIFICATIONS_H_ */ diff --git a/src/rpc.c b/src/rpc.c index 1dd9a86..7f5d91b 100755 --- a/src/rpc.c +++ b/src/rpc.c @@ -1207,7 +1207,9 @@ int cwmp_handle_rpc_cpe_set_parameter_values(struct rpc *rpc) goto fault; } - cwmp_set_end_session(END_SESSION_SET_NOTIFICATION_UPDATE | END_SESSION_RELOAD); + /* update values in notify file if any notify enable parameter exist in spv list */ + cwmp_update_notify_values(&list_set_param_value); + if (status == 1) { cwmp_set_end_session(END_SESSION_RESTART_SERVICES); } @@ -1284,7 +1286,8 @@ int cwmp_handle_rpc_cpe_set_parameter_attributes(struct rpc *rpc) goto fault; } - cwmp_set_end_session(END_SESSION_SET_NOTIFICATION_UPDATE | END_SESSION_RESTART_SERVICES | END_SESSION_INIT_NOTIFY); + reinit_list_param_notify(); + cwmp_update_enabled_notify_file(); return 0; fault: diff --git a/src/session.c b/src/session.c index a01e1a0..4922546 100644 --- a/src/session.c +++ b/src/session.c @@ -694,16 +694,6 @@ int run_session_end_func(void) reinit_cwmp_periodic_session_feature(); reinit_heartbeat_procedures(); - if (end_session_flag & END_SESSION_INIT_NOTIFY) { - CWMP_LOG(INFO, "SetParameterAttributes end session: reinit list notify"); - reinit_list_param_notify(); - } - - if (end_session_flag & END_SESSION_SET_NOTIFICATION_UPDATE) { - CWMP_LOG(INFO, "SetParameterAttributes/Values end session: update enabled notify file"); - cwmp_update_enabled_notify_file(); - } - if (end_session_flag & END_SESSION_NSLOOKUP_DIAGNOSTIC) { CWMP_LOG(INFO, "Executing nslookupdiagnostic: end session request"); cwmp_nslookup_diagnostics(); diff --git a/src/session.h b/src/session.h index b78032e..b51a1ab 100644 --- a/src/session.h +++ b/src/session.h @@ -54,30 +54,27 @@ enum end_session_enum { END_SESSION_REBOOT = 1, END_SESSION_EXTERNAL_ACTION = 1 << 1, - END_SESSION_RELOAD = 1 << 2, - END_SESSION_FACTORY_RESET = 1 << 3, - END_SESSION_X_FACTORY_RESET_SOFT = 1 << 4, + END_SESSION_FACTORY_RESET = 1 << 2, + END_SESSION_X_FACTORY_RESET_SOFT = 1 << 3, - END_SESSION_IPPING_DIAGNOSTIC = 1 << 5, - END_SESSION_DOWNLOAD_DIAGNOSTIC = 1 << 6, - END_SESSION_UPLOAD_DIAGNOSTIC = 1 << 7, - END_SESSION_NSLOOKUP_DIAGNOSTIC = 1 << 8, - END_SESSION_TRACEROUTE_DIAGNOSTIC = 1 << 9, - END_SESSION_UDPECHO_DIAGNOSTIC = 1 << 10, - END_SESSION_SERVERSELECTION_DIAGNOSTIC = 1 << 11, - END_SESSION_NEIGBORING_WIFI_DIAGNOSTIC = 1<<12, - END_SESSION_IPLAYERCAPACITY_DIAGNOSTIC = 1 << 13, + END_SESSION_IPPING_DIAGNOSTIC = 1 << 4, + END_SESSION_DOWNLOAD_DIAGNOSTIC = 1 << 5, + END_SESSION_UPLOAD_DIAGNOSTIC = 1 << 6, + END_SESSION_NSLOOKUP_DIAGNOSTIC = 1 << 7, + END_SESSION_TRACEROUTE_DIAGNOSTIC = 1 << 8, + END_SESSION_UDPECHO_DIAGNOSTIC = 1 << 9, + END_SESSION_SERVERSELECTION_DIAGNOSTIC = 1 << 10, + END_SESSION_NEIGBORING_WIFI_DIAGNOSTIC = 1<<11, + END_SESSION_IPLAYERCAPACITY_DIAGNOSTIC = 1 << 12, - END_SESSION_SET_NOTIFICATION_UPDATE = 1 << 14, - END_SESSION_RESTART_SERVICES = 1 << 15, - END_SESSION_INIT_NOTIFY = 1 << 16, - END_SESSION_DOWNLOAD = 1 << 17, - END_SESSION_SCHEDULE_DOWNLOAD = 1 << 18, - END_SESSION_UPLOAD = 1 << 19, - END_SESSION_SCHEDULE_INFORM = 1 << 20, - END_SESSION_CDU = 1 << 21, - END_SESSION_PACKETCAPTURE_DIAGNOSTIC = 1 << 22, - END_SESSION_SELFTEST_DIAGNOSTIC = 1 << 23, + END_SESSION_RESTART_SERVICES = 1 << 13, + END_SESSION_DOWNLOAD = 1 << 14, + END_SESSION_SCHEDULE_DOWNLOAD = 1 << 15, + END_SESSION_UPLOAD = 1 << 16, + END_SESSION_SCHEDULE_INFORM = 1 << 17, + END_SESSION_CDU = 1 << 18, + END_SESSION_PACKETCAPTURE_DIAGNOSTIC = 1 << 19, + END_SESSION_SELFTEST_DIAGNOSTIC = 1 << 20, }; enum enum_session_status diff --git a/src/ubus_utils.c b/src/ubus_utils.c index b6d0b88..b2cd2c9 100644 --- a/src/ubus_utils.c +++ b/src/ubus_utils.c @@ -75,7 +75,6 @@ static int reload_cmd(struct blob_buf *b) { CWMP_LOG(INFO, "triggered ubus reload"); if (cwmp_ctx.session->session_status.last_status == SESSION_RUNNING) { - cwmp_set_end_session(END_SESSION_RELOAD); blobmsg_add_u32(b, "status", 0); blobmsg_add_string(b, "info", "Session running, reload at the end of the session"); } else {