freecwmp
[Top] [All Lists]

Re: [PATCH 22/27] update xml handler set parameter values in order to ex

To: mohamed.kallel@pivasoftware.com
Subject: Re: [PATCH 22/27] update xml handler set parameter values in order to execute external command and get response via ubus Contributed by Inteno Broadband Technology AB
From: Luka Perkov <freecwmp@lukaperkov.net>
Date: Sat, 8 Dec 2012 12:06:38 +0100
Cc: freecwmp@linux-mips.org, ahmed.zribi@pivasoftware.com, freecwmp@lukaperkov.net, jogo@openwrt.org
In-reply-to: <1354809292-2467-23-git-send-email-mohamed.kallel@pivasoftware.com>
Mail-followup-to: mohamed.kallel@pivasoftware.com, freecwmp@linux-mips.org, ahmed.zribi@pivasoftware.com, jogo@openwrt.org
Original-recipient: rfc822;freecwmp@linux-mips.org
References: <mohamed.kallel@pivasoftware.com> <1354809292-2467-1-git-send-email-mohamed.kallel@pivasoftware.com> <1354809292-2467-23-git-send-email-mohamed.kallel@pivasoftware.com>
Sender: freecwmp-bounce@linux-mips.org
User-agent: Mutt/1.5.21 (2010-09-15)
Missing commit message. Contributed by must go in commit message and not
in subject.

On Thu, Dec 06, 2012 at 04:54:47PM +0100, Mohamed wrote:
> 
> Signed-off-by: Ahmed ZRIBI <ahmed.zribi@pivasoftware.com>
> Signed-off-by: Mohamed <mohamed.kallel@pivasoftware.com>
> ---
>  src/external.c |   36 ++++++++++++++++++++++++++-
>  src/external.h |    5 +++-
>  src/ubus.c     |   73 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  src/xml.c      |   25 +++++++++++++-----
>  4 files changed, 130 insertions(+), 9 deletions(-)
> 
> diff --git a/src/external.c b/src/external.c
> index 4973d1a..7eec751 100644
> --- a/src/external.c
> +++ b/src/external.c
> @@ -5,6 +5,7 @@
>   *   (at your option) any later version.
>   *
>   *   Copyright (C) 2011 Luka Perkov <freecwmp@lukaperkov.net>
> + *   Copyright (C) 2012 Mohamed Kallel <mohamed.kallel@pivasoftware.com>
>   */
>  
>  #include <errno.h>
> @@ -27,6 +28,8 @@
>  static struct uloop_process uproc;
>  pthread_t ubus_thread;
>  LIST_HEAD(external_list_parameter);
> +char *external_method_status = NULL;
> +char *external_method_fault = NULL;
>  
>  void *thread_uloop_run (void *v)
>  {
> @@ -60,6 +63,18 @@ void external_free_list_parameter()
>       }
>  }
>  
> +void external_set_param_val_resp_status(char *status)
> +{
> +     free(external_method_status);
> +     external_method_status = status ? strdup(status) : NULL;
> +}
> +
> +void external_fetch_set_param_val_resp_status(char **status)
> +{
> +     *status = external_method_status;
> +     external_method_status = NULL;
> +}
> +
>  int external_get_action_data(char *action, char *name, char **value)
>  {
>       struct external_parameter *external_parameter;
> @@ -229,9 +244,25 @@ int external_set_action_write(char *action, char *name, 
> char *value)
>       return 0;
>  }
>  
> -int external_set_action_execute()
> +int external_set_action_execute(char *action)
>  {
>       freecwmp_log_message(NAME, L_NOTICE, "executing set script\n");
> +     FILE *fp;
> +
> +     if (access(fc_script_set_actions, R_OK | W_OK | F_OK) == -1)
> +             return -1;
> +
> +     fp = fopen(fc_script_set_actions, "a");
> +     if (!fp) return -1;
> +
> +#ifdef DUMMY_MODE
> +     fprintf(fp, "/bin/sh `pwd`/%s apply %s\n", fc_script, action);
> +#else
> +     fprintf(fp, "/bin/sh %s apply %s\n", fc_script, action);
> +#endif
> +
> +     fclose(fp);
> +     pthread_create(&ubus_thread, NULL, &thread_uloop_run, NULL);
>  
>       if ((uproc.pid = fork()) == -1) {
>               return -1;
> @@ -258,6 +289,9 @@ int external_set_action_execute()
>               DD("waiting for child to exit");
>       }
>  
> +     pthread_cancel(ubus_thread);
> +     pthread_join(ubus_thread,NULL);
> +
>       // TODO: add some kind of checks
>  
>       if (remove(fc_script_set_actions) != 0)
> diff --git a/src/external.h b/src/external.h
> index 8e82c39..77165ee 100644
> --- a/src/external.h
> +++ b/src/external.h
> @@ -5,6 +5,7 @@
>   *   (at your option) any later version.
>   *
>   *   Copyright (C) 2011 Luka Perkov <freecwmp@lukaperkov.net>
> + *   Copyright (C) 2012 Mohamed Kallel <mohamed.kallel@pivasoftware.com>
>   */
>  
>  #ifndef _FREECWMP_EXTERNAL_H__
> @@ -27,12 +28,14 @@ struct external_parameter {
>       char *fault_code;
>  };
>  
> +void external_set_param_val_resp_status (char *status);
> +void external_fetch_set_param_val_resp_status (char **status);
>  int external_get_action(char *action, char *name, char *arg);
>  int external_get_action_data(char *action, char *name, char **value);
>  int external_get_action_write(char *action, char *name, char *arg);
>  int external_get_action_execute();
>  int external_set_action_write(char *action, char *name, char *value);
> -int external_set_action_execute();
> +int external_set_action_execute(char *action);
>  int external_simple(char *arg);
>  int external_download(char *url, char *size);
>  void external_add_list_paramameter(char *param_name, char *param_data, char 
> *param_type, char *fault_code);
> diff --git a/src/ubus.c b/src/ubus.c
> index 072b2a1..e852ed4 100644
> --- a/src/ubus.c
> +++ b/src/ubus.c
> @@ -281,11 +281,84 @@ freecwmpd_handle_get_param_attributes(struct 
> ubus_context *ctx, struct ubus_obje
>       return 0;
>  }
>  
> +static enum set_param_values_fault {
> +     SET_PARAM_VALUES_FAULT_PARAM,
> +     SET_PARAM_VALUES_FAULT__FAULT,
> +     __SET_PARAM_VALUES_FAULT_MAX
> +};
> +
> +static const struct blobmsg_policy set_param_values_fault_policy[] = {
> +     [SET_PARAM_VALUES_FAULT_PARAM] = { .name = "parameter", .type = 
> BLOBMSG_TYPE_STRING },
> +     [SET_PARAM_VALUES_FAULT__FAULT] = { .name = "fault_code", .type = 
> BLOBMSG_TYPE_STRING },
> +};
> +
> +static int
> +freecwmpd_handle_set_param_values_fault(struct ubus_context *ctx, struct 
> ubus_object *obj,
> +                     struct ubus_request_data *req, const char *method,
> +                     struct blob_attr *msg)
> +{
> +     struct blob_attr *tb[__SET_PARAM_VALUES_FAULT_MAX];
> +
> +     blobmsg_parse(set_param_values_fault_policy, 
> ARRAY_SIZE(set_param_values_fault_policy), tb,
> +                   blob_data(msg), blob_len(msg));
> +
> +     if (!tb[SET_PARAM_VALUES_FAULT_PARAM])
> +             return UBUS_STATUS_INVALID_ARGUMENT;
> +
> +     if (!tb[SET_PARAM_VALUES_FAULT__FAULT])
> +             return UBUS_STATUS_INVALID_ARGUMENT;
> +
> +
> +     freecwmp_log_message(NAME, L_NOTICE,
> +                          "triggered ubus set_parameter_values_fault for the 
> parameter %s\n",
> +                          blobmsg_data(tb[SET_PARAM_VALUES_FAULT_PARAM]));
> +
> +
> +     
> external_add_list_paramameter(blobmsg_data(tb[SET_PARAM_VALUES_FAULT_PARAM]),
> +                                             NULL,
> +                                             NULL,
> +                                             
> blobmsg_data(tb[SET_PARAM_VALUES_FAULT__FAULT]));
> +
> +     return 0;
> +}
> +
> +static enum set_param_values_status {
> +     SET_PARAM_VALUES_STATUS_STATUS,
> +     __SET_PARAM_VALUES_STATUS_MAX
> +};
> +
> +static const struct blobmsg_policy set_param_values_status_policy[] = {
> +     [SET_PARAM_VALUES_STATUS_STATUS] = { .name = "status", .type = 
> BLOBMSG_TYPE_STRING },
> +};
> +
> +static int
> +freecwmpd_handle_set_param_values_status(struct ubus_context *ctx, struct 
> ubus_object *obj,
> +                     struct ubus_request_data *req, const char *method,
> +                     struct blob_attr *msg)
> +{
> +     struct blob_attr *tb[__SET_PARAM_VALUES_STATUS_MAX];
> +
> +     blobmsg_parse(set_param_values_status_policy, 
> ARRAY_SIZE(set_param_values_status_policy), tb,
> +                   blob_data(msg), blob_len(msg));
> +
> +     if (!tb[SET_PARAM_VALUES_STATUS_STATUS])
> +             return UBUS_STATUS_INVALID_ARGUMENT;
> +
> +
> +     freecwmp_log_message(NAME, L_NOTICE,
> +                          "triggered ubus set_parameter_values_status\n");
> +
> +     
> external_set_param_val_resp_status(blobmsg_data(tb[SET_PARAM_VALUES_STATUS_STATUS]));
> +
> +     return 0;
> +}
>  static const struct ubus_method freecwmp_methods[] = {
>       UBUS_METHOD("notify", freecwmpd_handle_notify, notify_policy),
>       UBUS_METHOD("inform", freecwmpd_handle_inform, inform_policy),
>       UBUS_METHOD("command", freecwmpd_handle_command, command_policy),
>       UBUS_METHOD("get_parameter_values", freecwmpd_handle_get_param_values, 
> get_param_values_policy),
> +     UBUS_METHOD("set_parameter_values_fault", 
> freecwmpd_handle_set_param_values_fault, set_param_values_fault_policy),
> +     UBUS_METHOD("set_parameter_values_status", 
> freecwmpd_handle_set_param_values_status, set_param_values_status_policy),
>       UBUS_METHOD("get_parameter_names", freecwmpd_handle_get_param_names, 
> get_param_names_policy),
>       UBUS_METHOD("get_parameter_attributes", 
> freecwmpd_handle_get_param_attributes, get_param_attributes_policy),
>  };
> diff --git a/src/xml.c b/src/xml.c
> index c735244..a8f82c7 100644
> --- a/src/xml.c
> +++ b/src/xml.c
> @@ -578,6 +578,7 @@ int xml_handle_set_parameter_values(mxml_node_t *body_in,
>       mxml_node_t *b = body_in;
>       char *parameter_name = NULL;
>       char *parameter_value = NULL;
> +     char *status=NULL;
>  
>       while (b) {
>               if (b && b->type == MXML_TEXT &&
> @@ -602,25 +603,35 @@ int xml_handle_set_parameter_values(mxml_node_t 
> *body_in,
>               b = mxmlWalkNext(b, body_in, MXML_DESCEND);
>       }
>  
> -     if (external_set_action_execute())
> +     if (external_set_action_execute("value"))
>               return -1;
>  
> +     // TODO KMD add here check for fault return from ubus

What does KMD mean?

> +
> +     external_fetch_set_param_val_resp_status(&status);
> +     if(!status) return -1; //TODO KMD return a fault message here

Fix comment.

> +
>       config_load();
>  
>       b = mxmlFindElement(tree_out, tree_out, "soap_env:Body",
>                           NULL, NULL, MXML_DESCEND);
> -     if (!b) return -1;
> +     if (!b) goto error;
>  
>       b = mxmlNewElement(b, "cwmp:SetParameterValuesResponse");
> -     if (!b) return -1;
> +     if (!b) goto error;
>  
>       b = mxmlNewElement(b, "Status");
> -     if (!b) return -1;
> +     if (!b) goto error;
>  
> -     b = mxmlNewText(b, 0, "1");
> -     if (!b) return -1;
> +     b = mxmlNewText(b, 0, status);
> +     if (!b) goto error;
>       
> +     free(status);
>       return 0;
> +
> +error:
> +     free(status);
> +     return-1;
>  }
>  
>  int xml_handle_get_parameter_values(mxml_node_t *body_in,
> @@ -899,7 +910,7 @@ static int 
> xml_handle_set_parameter_attributes(mxml_node_t *body_in,
>               b = mxmlWalkNext(b, n, MXML_DESCEND);
>       }
>  
> -     if (external_set_action_execute())
> +     if (external_set_action_execute("notification"))
>               return -1;
>  
>       b = mxmlFindElement(tree_out, tree_out, "soap_env:Body", NULL, NULL, 
> MXML_DESCEND);
> -- 
> 1.7.4.1

Luka 

<Prev in Thread] Current Thread [Next in Thread>