aboutsummaryrefslogtreecommitdiffstats
path: root/src/framework/ipc/ps/nsfw_soft_param.c
blob: 743276fa3814018394d76ef846526d9b2c12dc28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*==============================================*
 *      include header files                    *
 *----------------------------------------------*/

#include <stdlib.h>
#include "types.h"
#include "nstack_securec.h"
#include "nsfw_init.h"

#include "nstack_log.h"
#include "nsfw_maintain_api.h"

#include "nsfw_mem_api.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C"{
/* *INDENT-ON* */
#endif /* __cplusplus */

typedef struct _nsfw_set_soft_item
{
  void *data;
  u32 size;
  u64 max;
  u64 min;
} nsfw_set_soft_item;

/* *INDENT-OFF* */
nsfw_set_soft_fun g_soft_fun[NSFW_MAX_SOFT_PARAM] = { 0 };
nsfw_set_soft_item g_soft_int_cfg[NSFW_MAX_SOFT_PARAM];
/* *INDENT-ON* */

int
nsfw_isdigitstr (const char *str)
{
  if (NULL == str || '\0' == str[0])
    return 0;

  while (*str)
    {
      if (*str < '0' || *str > '9')
        return 0;
      str++;
    }
  return 1;
}

u8
nsfw_soft_param_reg_fun (u32 param_name, nsfw_set_soft_fun fun)
{
  if (NULL == fun || param_name >= NSFW_MAX_SOFT_PARAM)
    {
      NSFW_LOGERR ("argv err]fun=%p,type=%u", fun, param_name);
      return FALSE;
    }

  g_soft_fun[param_name] = fun;
  NSFW_LOGINF ("reg]fun=%d,type=%u", fun, param_name);
  return TRUE;
}

int
nsfw_soft_set_int (u32 param, char *buf, u32 buf_len)
{
  u64 buf_value = 0;
  if (NULL == buf || param >= NSFW_MAX_SOFT_PARAM)
    {
      NSFW_LOGERR ("argv err]buf=%p,param=%u", buf, param);
      return FALSE;
    }

  if (!nsfw_isdigitstr (buf))
    {
      NSFW_LOGERR ("argv err]buf=%s,param=%u", buf, param);
      return FALSE;
    }

  char *parsing_end;
  buf_value = (u64) strtol (buf, &parsing_end, 10);
  nsfw_set_soft_item *int_item = &g_soft_int_cfg[param];
  if (NULL == int_item->data)
    {
      NSFW_LOGERR ("data err]buf=%s,param=%u,min=%llu,max=%llu", buf, param,
                   int_item->min, int_item->max);
      return FALSE;
    }

  if (buf_value < int_item->min || buf_value > int_item->max)
    {
      NSFW_LOGERR ("argv err]buf=%s,param=%u,min=%llu,max=%llu", buf, param,
                   int_item->min, int_item->max);
      return FALSE;
    }

  u32 size = int_item->size;
  if (size >= sizeof (u64))
    {
      size = sizeof (u64);
    }

  if (EOK != MEMCPY_S (int_item->data, size, &buf_value, size))
    {
      NSFW_LOGERR ("MEMCPY_S failed");
      return FALSE;
    }

  NSFW_LOGINF ("set soft param ok]param=%u,value=%llu,size=%u", param,
               buf_value, size);
  return TRUE;
}

/*****************************************************************************
*   Prototype    : nsfw_soft_param_reg_int
*   Description  : reg int param set
*   Input        : u32 param_name
*                  u32 size
*                  u32 min
*                  u32 max
*                  u64 *data
*   Output       : None
*   Return Value : u8
*   Calls        :
*   Called By    :
*****************************************************************************/
u8
nsfw_soft_param_reg_int (u32 param_name, u32 size, u32 min, u32 max,
                         u64 * data)
{
  if (NULL == data || param_name >= NSFW_MAX_SOFT_PARAM)
    {
      NSFW_LOGERR ("argv err]data=%p,type=%u", data, param_name);
      return FALSE;
    }

  g_soft_int_cfg[param_name].data = data;
  g_soft_int_cfg[param_name].size = size;
  g_soft_int_cfg[param_name].max = max;
  g_soft_int_cfg[param_name].min = min;
  return nsfw_soft_param_reg_fun (param_name, nsfw_soft_set_int);
}

void
nsfw_set_soft_para (fw_poc_type proc_type, u32 para_name, void *value,
                    u32 size)
{
  nsfw_mgr_msg *msg =
    (nsfw_mgr_msg *) nsfw_mgr_msg_alloc (MGR_MSG_SOF_PAR_REQ, proc_type);
  if (NULL == msg)
    {
      NSFW_LOGERR
        ("nsfw_mgr_msg_alloc failed] msg type=%d, proc type=%d, para_name=%u",
         MGR_MSG_SOF_PAR_REQ, proc_type, para_name);
      return;
    }

  nsfw_soft_param_msg *soft_msg = GET_USER_MSG (nsfw_soft_param_msg, msg);

  soft_msg->param_name = para_name;
  soft_msg->rsp_code = 0;

  if (EOK !=
      MEMCPY_S (soft_msg->param_value, sizeof (soft_msg->param_value),
                value, size))
    {
      NSFW_LOGERR
        ("MEMCPY_S failed] msg type=%d, proc type=%d, para_name=%u",
         MGR_MSG_SOF_PAR_REQ, proc_type, para_name);
      nsfw_mgr_msg_free (msg);
      return;
    }

  nsfw_mgr_msg *rsp = nsfw_mgr_null_rspmsg_alloc ();
  if (NULL == rsp)
    {
      NSFW_LOGERR
        ("nsfw_mgr_null_rspmsg_alloc failed] msg type=%d, proc type=%d, para_name=%u",
         MGR_MSG_SOF_PAR_REQ, proc_type, para_name);
      nsfw_mgr_msg_free (msg);
      return;
    }

  if (!nsfw_mgr_send_req_wait_rsp (msg, rsp))
    {
      NSFW_LOGERR
        ("can not get response] msg type=%d, proc type=%d, para_name=%u",
         MGR_MSG_SOF_PAR_REQ, proc_type, para_name);
      nsfw_mgr_msg_free (msg);
      nsfw_mgr_msg_free (rsp);
      return;
    }

  nsfw_soft_param_msg *soft_rsp_msg = GET_USER_MSG (nsfw_soft_param_msg, rsp);
  if (soft_rsp_msg->rsp_code != NSFW_EXIT_SUCCESS)
    {
      NSFW_LOGERR
        ("set soft param failed] msg type=%d, proc type=%d, para_name=%u, rsp code=%u",
         MGR_MSG_SOF_PAR_REQ, proc_type, para_name, soft_rsp_msg->rsp_code);
      nsfw_mgr_msg_free (msg);
      nsfw_mgr_msg_free (rsp);
      return;
    }

  NSFW_LOGINF
    ("set soft param success] msg type=%d, proc type=%d, para_name=%u",
     MGR_MSG_SOF_PAR_REQ, proc_type, para_name);

  nsfw_mgr_msg_free (msg);
  nsfw_mgr_msg_free (rsp);
  return;
}

int
nsfw_softparam_msg_proc (nsfw_mgr_msg * msg)
{
  nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg);
  if (NULL == rsp_msg)
    {
      NSFW_LOGERR ("alloc rsp failed,drop msg!" MSGINFO, PRTMSG (msg));
      return FALSE;
    }

  nsfw_soft_param_msg *soft_msg = GET_USER_MSG (nsfw_soft_param_msg, msg);
  nsfw_soft_param_msg *soft_rsp_msg =
    GET_USER_MSG (nsfw_soft_param_msg, rsp_msg);
  if ((soft_msg->param_name < NSFW_MAX_SOFT_PARAM)
      && (NULL != g_soft_fun[soft_msg->param_name]))
    {
      soft_msg->param_value[sizeof (soft_msg->param_value) - 1] = 0;
      (void) g_soft_fun[soft_msg->param_name] (soft_msg->param_name,
                                               (char *)
                                               soft_msg->param_value,
                                               sizeof
                                               (soft_msg->param_value));
      soft_rsp_msg->rsp_code = NSFW_EXIT_SUCCESS;
    }
  else
    {
      NSFW_LOGERR ("set soft failed!]soft=%u", soft_msg->param_name);
      soft_rsp_msg->rsp_code = NSFW_EXIT_FAILED;
    }

  (void) nsfw_mgr_send_msg (rsp_msg);
  nsfw_mgr_msg_free (rsp_msg);
  return TRUE;
}

int nsfw_softparam_module_init (void *param);
int
nsfw_softparam_module_init (void *param)
{
  u8 proc_type = (u8) ((long long) param);
  NSFW_LOGINF ("softparam module init]type=%u", proc_type);
  switch (proc_type)
    {
    case NSFW_PROC_MAIN:
      (void) nsfw_mgr_reg_msg_fun (MGR_MSG_SOF_PAR_REQ,
                                   nsfw_softparam_msg_proc);
      return 0;
    default:
      if (proc_type < NSFW_PROC_MAX)
        {
          break;
        }
      return -1;
    }

  return 0;
}

/* *INDENT-OFF* */
NSFW_MODULE_NAME (NSFW_SOFT_PARAM_MODULE)
NSFW_MODULE_PRIORITY (99)
NSFW_MODULE_INIT (nsfw_softparam_module_init)
/* *INDENT-ON* */

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif /* __cplusplus */