blob: 411da04f8dca179e55b603b65d72551e5b090055 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include "cwmp/model.h"
2#include "cwmp/xmlet.h"
3#include "cwmp/log.h"
4#include "cwmp_private.h"
5
6
7
8
9int cwmp_model_copy_parameter_object(parameter_node_t * object_param, parameter_node_t * new_node, int instance_number)
10{
11 parameter_node_t * tmp_node, *curr_node, *next_newnode;
12
13// FUNCTION_TRACE();
14
15 if(!object_param || !new_node)
16 {
17 return CWMP_ERROR;
18 }
19 if(instance_number > 0)
20 {
21 memcpy(new_node, object_param, sizeof(parameter_node_t));
22 new_node->name = TRstrdup(TRitoa(instance_number));
23
24 new_node->child = NULL;
25 new_node->next_sibling = NULL;
26 for(tmp_node=object_param; tmp_node; tmp_node = tmp_node->next_sibling)
27 {
28 if(!tmp_node->next_sibling)
29 {
30 tmp_node->next_sibling = new_node;
31 break;
32 }
33
34 }
35 }
36
37 for(tmp_node=object_param->child, curr_node=NULL; tmp_node; tmp_node=tmp_node->next_sibling)
38 {
39 next_newnode = MALLOC(sizeof(parameter_node_t));
40 if(NULL == next_newnode)
41 {
42 cwmp_log_error("next_newnode malloc return NULL");
43 goto fail;
44 }
45 memset(next_newnode, 0, sizeof(parameter_node_t));
46 memcpy(next_newnode, tmp_node, sizeof(parameter_node_t));
47 next_newnode->name = TRstrdup(tmp_node->name);
48 next_newnode->parent = new_node;
49 next_newnode->child = NULL;
50 next_newnode->next_sibling = NULL;
51
52 if(!curr_node)
53 {
54 new_node->child = next_newnode;
55 }
56 else
57 {
58 curr_node->next_sibling = next_newnode;
59 }
60
61 curr_node = next_newnode;
62
63 cwmp_model_copy_parameter_object(tmp_node, next_newnode, 0);
64
65
66 }
67
68 return CWMP_OK;
69
70fail:
71 //todo: if need to free other node ???
72 return CWMP_ERROR;
73}
74
75
76int cwmp_model_copy_parameter(parameter_node_t * param, parameter_node_t ** new_param, int instance_number)
77{
78 int rv;
79 if(!param)
80 return CWMP_OK;
81
82// FUNCTION_TRACE();
83
84 parameter_node_t * object_param = param->child; //object param like InternetGatewayDevice.WANDevice.{i}
85
86 parameter_node_t * new_node = MALLOC(sizeof(parameter_node_t));
87
88 if (!new_node)
89 {
90 cwmp_log_error("cwmp model copy parameter malloc new node failed ...\n");
91 return CWMP_ERROR;
92 }
93
94 new_node->name = NULL;
95 new_node->rw = 0;
96 new_node->type = 0;
97 new_node->value = NULL;
98 new_node->parent = param;
99
100 rv = cwmp_model_copy_parameter_object(object_param, new_node, instance_number);
101 *new_param = new_node;
102 if(rv != CWMP_OK)
103 {
104 cwmp_log_error("cwmp_model: copy parameter failed");
105 cwmp_model_free_parameter(new_node);
106 *new_param = NULL;
107 }
108 return rv;
109}
110
111int cwmp_model_free_parameter(parameter_node_t * param)
112{
113 parameter_node_t *tmp_param = NULL;
114 parameter_node_t *next_param = NULL;
115
116 if(!param)
117 {
118 return CWMP_OK;
119 }
120
121 for(tmp_param=param->child; tmp_param; tmp_param=next_param)
122 {
123 next_param = tmp_param->next_sibling;
124 cwmp_model_delete_parameter(tmp_param);
125 }
126
127 FREE(param->name);
128 FREE(param);
129
130 return CWMP_OK;
131}
132
133int cwmp_model_delete_parameter(parameter_node_t * param)
134{
135 parameter_node_t *tmp_param = NULL;
136 parameter_node_t *next_param = NULL;
137 parameter_node_t *parent = NULL;
138
139// FUNCTION_TRACE();
140
141 if(!param)
142 {
143 return CWMP_OK;
144 }
145 parent = param->parent;
146 for(tmp_param=parent->child; tmp_param; tmp_param=tmp_param->next_sibling)
147 {
148 if(tmp_param->next_sibling == param)
149 {
150 tmp_param->next_sibling = param->next_sibling;
151 break;
152 }
153 }
154
155 return cwmp_model_free_parameter(param);
156}
157
158void* cwmp_model_find_func(model_func_t * func_list, int func_count, const char * func_name)
159{
160// FUNCTION_TRACE();
161
162 if(!func_name || func_count <= 0)
163 return NULL;
164
165 model_func_t * f = func_list;
166 int i;
167
168// FUNCTION_TRACE();
169
170 for(i=0; i<func_count; i++, f++)
171 {
172 if(TRstrcasecmp(func_name, f->name) == 0)
173 {
174 return f->func;
175 }
176 }
177 return NULL;
178
179
180}
181
182int cwmp_model_create_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count, pool_t * pool);
183
184
185int cwmp_model_init_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count, pool_t * pool)
186{
187// FUNCTION_TRACE();
188
189 param->get = NULL;
190 param->set = NULL;
191 param->notify = NULL;
192 param->add = NULL;
193 param->del = NULL;
194 param->refresh = NULL;
195
196 if(!node)
197 {
198 return CWMP_OK;
199 }
200
201 char *nodename = node->nodeName;
202 char *name, *value, *attr_nc;
203 if(! TRstrcasecmp(nodename, DEVICE_MODEL_OBJECT_NODE))
204 {
205 param->type = TYPE_OBJECT;
206 }
207 else
208 {
209 char * type = cwmp_xml_get_node_attribute(node, "type");
210 if(!type)
211 {
212 param->type = TYPE_STRING;
213 }
214 else
215 {
216 param->type = cwmp_get_type_value(type);
217 }
218 }
219 name = cwmp_xml_get_node_attribute(node, "name");
220 if(!name)
221 {
222 return CWMP_ERROR;
223 }
224
225 param->name = TRstrdup(name);
226
227 value = cwmp_xml_get_node_attribute(node, "rw");
228 if(value)
229 {
230 param->rw = TRatoi(value);
231 }
232
233 memset(&(param->attr), 0, sizeof(parameter_node_attr_t));
234 attr_nc = cwmp_xml_get_node_attribute(node, "noti_rw");
235 if(attr_nc)
236 {
237 param->attr.nc = TRatoi(attr_nc);
238 }
239
240 //value = cwmp_xml_get_node_attribute(node, "value");
241 //if(value)
242 //{
243 // param->value = pool_pstrdup(pool, value);
244 //}
245
246 if(param->type == TYPE_OBJECT)
247 {
248 value = cwmp_xml_get_node_attribute(node, "add_func");
249 if(value)
250 {
251 param->add = (parameter_add_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
252
253
254 }
255
256 value = cwmp_xml_get_node_attribute(node, "del_func");
257 if(value)
258 {
259 param->del = (parameter_del_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
260 }
261
262 value = cwmp_xml_get_node_attribute(node, "refresh_func");
263 if(value)
264 {
265 param->refresh = (parameter_refresh_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
266 }
267 }
268 else
269 {
270 value = cwmp_xml_get_node_attribute(node, "get_func");
271 if(value)
272 {
273 param->get = (parameter_get_handler_pt) cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
274 }
275
276 value = cwmp_xml_get_node_attribute(node, "set_func");
277 if(value)
278 {
279 param->set = (parameter_set_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
280 }
281
282 value = cwmp_xml_get_node_attribute(node, "notify_func");
283 if(value)
284 {
285 param->notify = (parameter_notify_handler_pt)cwmp_model_find_func(func_list, func_count, value);//dlsym(cwmp->dev_lib, value);;
286 }
287 }
288
289 return CWMP_OK;
290
291}
292
293int cwmp_model_create_child_parameter(parameter_node_t * child_param, xmlnode_t * child_node, model_func_t * func_list, int func_count, pool_t * pool)
294{
295// FUNCTION_TRACE();
296 cwmp_model_create_parameter(child_param, child_node, func_list, func_count, pool);
297
298
299 xmlnode_t * next_node = XmlNodeGetNextSibling(child_node);
300 parameter_node_t * last_sibling = child_param;
301 while(next_node)
302 {
303 parameter_node_t * next_param = (parameter_node_t *)pool_pcalloc(pool, sizeof(parameter_node_t));
304 if(NULL == next_param)
305 {
306 cwmp_log_error("next_param pool pcalloc return null");
307 return CWMP_ERROR;
308 }
309 cwmp_model_create_parameter(next_param, next_node, func_list, func_count, pool);
310
311 next_node = XmlNodeGetNextSibling(next_node);
312
313 next_param->parent = child_param;
314 last_sibling->next_sibling = next_param;
315 last_sibling = next_param;
316 }
317
318 return CWMP_OK;
319}
320
321
322int cwmp_model_create_parameter(parameter_node_t * param, xmlnode_t * node, model_func_t * func_list, int func_count, pool_t * pool)
323{
324// FUNCTION_TRACE();
325 cwmp_model_init_parameter(param, node, func_list, func_count, pool);
326
327 cwmp_log_debug("name %s: %p,%p", param->name, param->get, param->set);
328
329 xmlnode_t * child_node = XmlNodeGetFirstChild(node);
330 if(!child_node)
331 {
332 return CWMP_OK;
333 }
334
335 parameter_node_t * child_param = (parameter_node_t *)pool_pcalloc(pool, sizeof(parameter_node_t));
336 if(NULL == child_param)
337 {
338 cwmp_log_error("child_param pool_pcalloc return NULL");
339 return CWMP_ERROR;
340 }
341
342 cwmp_model_create_child_parameter(child_param, child_node, func_list, func_count, pool);
343 param->child = child_param;
344 child_param->parent = param;
345
346 return CWMP_OK;
347}
348
349
350static int cwmp_model_init_object(cwmp_t * cwmp, parameter_node_t *param)
351{
352 parameter_node_t *node = NULL;
353
354// FUNCTION_TRACE();
355
356 if(!param)
357 {
358 cwmp_log_error("input param is null");
359 return CWMP_ERROR;
360 }
361
362 if(param->type == TYPE_OBJECT && param->refresh)
363 {
364 param->refresh(cwmp, param, callback_register_task);
365 }
366
367 for (node = param->child; node; node = node->next_sibling)
368 {
369 cwmp_model_init_object(cwmp, node);
370 }
371
372 return CWMP_OK;
373}
374
375
376int cwmp_model_refresh_object(cwmp_t * cwmp, parameter_node_t *param, int flag, callback_register_func_t callback_reg)
377{
378 parameter_node_t *node = NULL;
379
380// FUNCTION_TRACE();
381
382 if(!param)
383 {
384 return CWMP_ERROR;
385 }
386
387 if(param->refresh && flag == 1)
388 {
389 param->refresh(cwmp, param, callback_reg);
390 }
391
392 for (node = param->child; node; node = node->next_sibling)
393 {
394 if(TRstrcmp(param->name, "{i}") != 0)
395 {
396 cwmp_model_refresh_object(cwmp, node, 1, callback_reg);
397 }
398 }
399
400 return CWMP_OK;
401}
402
403
404
405
406int cwmp_model_load_parameter(cwmp_t * cwmp, xmldoc_t * doc, model_func_t * func_list, int func_count)
407{
408 pool_t * pool = cwmp->pool;
409
410
411 xmlnode_t * root_node;
412 xmlnode_t * model_node;
413 ASSERT(doc != NULL);
414 FUNCTION_TRACE();
415 root_node = XmlNodeGetFirstChild(& doc->node);
416 if (! root_node)
417 {
418 cwmp_log_error("xml document root is null!");
419 return CWMP_ERROR;
420 }
421
422 cwmp_log_debug("model load: xml node name is %s\n", root_node->nodeName);
423 model_node = cwmp_xml_get_child_with_name(root_node, DEVICE_MODEL_NODE);
424 if (model_node == NULL)
425 {
426 cwmp_log_error("device model node not found!");
427 return CWMP_ERROR;
428 }
429
430 parameter_node_t * root_param = pool_pcalloc(pool, sizeof(parameter_node_t));
431 if(NULL == root_param)
432 {
433 cwmp_log_error("root_param pool_pcalloc return NULL!");
434 return CWMP_ERROR;
435 }
436
437 cwmp_model_create_parameter(root_param, model_node, func_list, func_count, pool);
438
439 cwmp->root = root_param->child;
440
441 cwmp_log_info("init model object");
442
443 cwmp_model_init_object(cwmp, cwmp->root);
444
445 return CWMP_OK;
446}
447
448int cwmp_model_load_xml(cwmp_t * cwmp, const char * xmlfile, model_func_t * func_list, int func_count)
449{
450
451 xmldoc_t * doc;
452 size_t xmllen, nread ;
453
454 FUNCTION_TRACE();
455
456#if 0
457 FILE * fp = fopen(xmlfile, "rb");
458 if(!fp)
459 {
460 cwmp_log_error("xmlfile is NULL\n");
461 return CWMP_ERROR;
462 }
463
464 fseek(fp, 0, SEEK_END);
465 xmllen = ftell(fp);
466 if(xmllen <= 0)
467 {
468 cwmp_log_error("ftell return %d!\n", xmllen);
469 fclose(fp);
470 return CWMP_ERROR;
471 }
472
473 char * buf = (char*)MALLOC(sizeof(char)*(xmllen+1));
474 if(!buf)
475 {
476 cwmp_log_error("model load: malloc fail\n");
477 fclose(fp);
478 return CWMP_ERROR;
479 }
480
481 memset(buf, 0, sizeof(char)*(xmllen+1));
482
483 fseek(fp, 0, SEEK_SET);
484
485 nread = fread(buf, 1, xmllen, fp);
486 if(nread <= 0)
487 {
488 fclose(fp);
489 FREE(buf);
490 return CWMP_ERROR;
491 }
492#else
493 struct stat statbuf;
494 if(stat(xmlfile,&statbuf) < 0)
495 {
496 cwmp_log_info("Get file len fail! file path:%s", xmlfile);
497 return CWMP_ERROR;
498 }
499 int size=statbuf.st_size;
500
501 char * buf = (char*)MALLOC(sizeof(char)*(size+1));
502 if(!buf)
503 {
504 cwmp_log_error("model load: malloc fail\n");
505 return CWMP_ERROR;
506 }
507
508 memset(buf, 0, sizeof(char)*(size+1));
509
510 FILE * fp = fopen(xmlfile, "rb");
511 if(!fp)
512 {
513 cwmp_log_error("xmlfile is NULL\n");
514 FREE(buf);
515 return CWMP_ERROR;
516 }
517
518 fseek(fp, 0, SEEK_SET);
519
520 nread = fread(buf, 1, size, fp);
521 if(nread <= 0)
522 {
523 fclose(fp);
524 FREE(buf);
525 return CWMP_ERROR;
526 }
527
528
529#endif
530
531 buf[nread] = 0;
532
533 pool_t * pool = pool_create(POOL_DEFAULT_SIZE);
534 if(NULL == pool)
535 {
536 cwmp_log_error("pool create return null");
537 fclose(fp);
538 FREE(buf);
539 return CWMP_ERROR;
540 }
541
542 doc = XmlParseBuffer(pool, buf);
543 if (!doc)
544 {
545 cwmp_log_error("model load: create doc faild\n");
546 goto finish;
547 }
548
549 cwmp_model_load_parameter(cwmp, doc, func_list, func_count);
550 //cwmp_model_load_inform_info(cwmp, doc);
551 //cwmp_model_load_device_info(cwmp, doc);
552
553finish:
554
555 fclose(fp);
556
557// if(NULL != buf)
558 {
559 FREE(buf);
560 }
561
562// if(NULL != pool)
563 {
564 pool_destroy(pool);
565 }
566
567 return CWMP_OK;
568}
569
570
571
572
573