blob: a62929807e586b52458d179385bd2a7120ae737f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <cwmp/types.h>
2#include <cwmp/cwmp.h>
3#include <cwmp/event.h>
4#include <cwmp/log.h>
5
6
7typedef struct memorystruct {
8 char *data;
9 size_t size;
10}memorystruct_t;
11
12
13static inform_event_t inform_event_table[] =
14{
15 {INFORM_BOOTSTRAP, 0, CWMP_INFORM_EVENT_CODE_0 , "inform:event_bootstrap", "inform:command_bootstrap"},
16 {INFORM_BOOT, 0, CWMP_INFORM_EVENT_CODE_1 , "inform:event_boot", "inform:command_boot"},
17 {INFORM_PERIODIC, 0, CWMP_INFORM_EVENT_CODE_2 , "inform:event_periodic", "inform:command_periodic"},
18 {INFORM_SCHEDULED, 0, CWMP_INFORM_EVENT_CODE_3 , "inform:event_scheduled", "inform:command_scheduled"},
19 {INFORM_VALUECHANGE, 0, CWMP_INFORM_EVENT_CODE_4 , "inform:event_valuechange", "inform:command_valuechange"},
20 {INFORM_KICKED, 0, CWMP_INFORM_EVENT_CODE_5 , "inform:event_kicked", "inform:command_kicked"},
21 {INFORM_CONNECTIONREQUEST, 0, CWMP_INFORM_EVENT_CODE_6 , "inform:event_connreq", "inform:command_connreq"},
22 {INFORM_TRANSFERCOMPLETE, 0, CWMP_INFORM_EVENT_CODE_7 , "inform:event_transcomplt", "inform:command_transcomplt"},
23 {INFORM_DIAGNOSTICSCOMPLETE, 0, CWMP_INFORM_EVENT_CODE_8 , "inform:event_diagcomplt", "inform:command_diagcomplt"},
24 {INFORM_REQUESTDOWNLOAD, 0, CWMP_INFORM_EVENT_CODE_9 , "inform:event_reqdl", "inform:command_reqdl"},
25 {INFORM_AUTONOMOUSTRANSFERCOMPLETE, 0, CWMP_INFORM_EVENT_CODE_10, "inform:event_autotranscomplt", "inform:command_autotranscomplt"},
26 {INFORM_MREBOOT, 0, CWMP_INFORM_EVENT_CODE_11, "inform:event_mreboot", "inform:command_mreboot"},
27 {INFORM_MSCHEDULEINFORM, 0, CWMP_INFORM_EVENT_CODE_12, "inform:event_mscheinform", "inform:command_mscheinform"},
28 {INFORM_MDOWNLOAD, 0, CWMP_INFORM_EVENT_CODE_13, "inform:event_mdownload", "inform:command_mdownload"},
29 {INFORM_MUPLOAD, 0, CWMP_INFORM_EVENT_CODE_14, "inform:event_mupload", "inform:command_mupload"},
30 {INFORM_ACCOUNTCHANGE, 0, CWMP_INFORM_EVENT_CODE_15, "inform:event_accountchange", "inform:command_accountchange"},
31 {INFORM_MVENDORSPECRPC, 0, CWMP_INFORM_EVENT_CODE_16, "inform:event_mvendorspecrpc", "inform:command_mvendorspecrpc"},
32 {INFORM_XOUIEVENT, 0, CWMP_INFORM_EVENT_CODE_17, "inform:event_xouievent", "inform:command_xouievent"},
33 {INFORM_MAX, 0, "", "", ""}
34};
35
36
37
38event_list_t * cwmp_event_list_create(pool_t * pool, int size)
39{
40 //create event list
41 event_list_t * el;
42 el = PMALLOC(sizeof(event_list_t));
43 if (!el)
44 {
45 cwmp_log_error("el pmalloc return null");
46 return NULL;
47 }
48 el->events = PMALLOC(sizeof(event_code_t *) * size);
49 if(!el->events)
50 {
51 cwmp_log_error("el->event pmalloc return null");
52 return NULL;
53 }
54
55 el->count = 0;
56 el->size = size;
57 return el;
58}
59
60event_code_t * cwmp_event_code_create(pool_t * pool )
61{
62 event_code_t * ev;
63 ev = PMALLOC(sizeof(event_code_t));
64 //ev->event = 0;
65 //ev->code = NULL;
66 //memset(ev->command_key, 0 , COMMAND_KEY_LEN+1);
67 //ev->have_key = 0;
68 //ev->policy = 0;
69 //ev->ref = 0;
70 //ev->fault_code = 0;
71 //ev->start = 0;
72 //ev->end = 0;
73 return ev;
74}
75
76int cwmp_event_list_init(pool_t * pool, event_list_t * el)
77{
78 //create event list
79 int i, size;
80 size = el->size;
81 for(i=0; i<size; i++)
82 {
83 event_code_t * ec = cwmp_event_code_create(pool);
84 el->events[i] = ec;
85 }
86 el->count = 0;
87 return CWMP_OK;
88}
89
90
91int cwmp_event_global_init(cwmp_t * cwmp)
92{
93 FILE *fp = NULL;
94 int i = 0;
95
96 FUNCTION_TRACE();
97
98 if(!cwmp)
99 {
100 cwmp_log_error("param cwmp is NULL\n");
101 return CWMP_ERROR;
102 }
103 memset(&cwmp->event_global, 0, sizeof(event_global_t));
104
105 //ÅжÏÅäÖÃÎļþÊÇ·ñ´æÔÚ
106 if(access(cwmp->event_filename, F_OK) == -1)
107 {
108 cwmp_log_error("event_file (%s) no exist!\n", cwmp->event_filename);
109 return CWMP_ERROR;
110 }
111
112 fp = fopen(cwmp->event_filename, "rb");
113 if(!fp)
114 {
115 cwmp_log_error("could not open event file %s\n", cwmp->event_filename);
116 return CWMP_ERROR;
117 }
118
119 if(fread(&cwmp->event_global, sizeof(event_global_t), 1, fp) != 1)
120 {
121 cwmp_log_error("read event global file fail\n");
122 }
123
124 cwmp_log_info("event_flag:%d", cwmp->event_global.event_flag);
125#if 0
126 for(i=0; i<COMMAND_KEY_LEN+1; i++)
127 {
128 cwmp_log_info("event_key:%d", cwmp->event_global.event_key[i]);
129 }
130#endif
131 cwmp_log_info("fault_code:%d", cwmp->event_global.fault_code);
132 cwmp_log_info("start:%d", cwmp->event_global.start);
133 cwmp_log_info("end:%d", cwmp->event_global.end);
134
135 fclose(fp);
136
137 return CWMP_OK;
138}
139
140//°ÑʼþÐÅϢдÈëÎļþÖÐ
141int cwmp_event_file_save(cwmp_t * cwmp)
142{
143 FILE *fp = NULL;
144 int ret = CWMP_OK;
145 int i = 0;
146
147 FUNCTION_TRACE();
148 if(!cwmp)
149 {
150 return CWMP_ERROR;
151 }
152
153 if((fp = fopen(cwmp->event_filename, "wb+")) == NULL)
154 {
155 cwmp_log_error("can't open file: %s.\n", cwmp->event_filename);
156 return CWMP_ERROR;
157 }
158
159 if(fwrite(&cwmp->event_global, sizeof(event_global_t), 1, fp) != 1)
160 {
161 cwmp_log_error("can't write event global to %s\n", cwmp->event_filename);
162
163 ret = CWMP_ERROR;
164 }
165
166
167 cwmp_log_info("event_flag:%d", cwmp->event_global.event_flag);
168
169 for(i=0; i<COMMAND_KEY_LEN+1; i++)
170 {
171 cwmp_log_info("event_key:%d", cwmp->event_global.event_key[i]);
172 }
173
174 cwmp_log_info("fault_code:%d", cwmp->event_global.fault_code);
175 cwmp_log_info("start:%d", cwmp->event_global.start);
176 cwmp_log_info("end:%d", cwmp->event_global.end);
177
178
179 fclose(fp);
180 return ret;
181}
182
183
184
185//É豸Æô¶¯ÒÔºó£¬Ò»Ð©Ê¼þÐèÒªÉϱ¨
186int cwmp_event_init(cwmp_t *cwmp)
187{
188 int bootstrap_flag = 0;
189 int upgrade_flag = 0;
190 char val[64] = {0};
191
192 FUNCTION_TRACE();
193
194 if(!cwmp)
195 {
196 cwmp_log_error("param cwmp is NULL\n");
197 return CWMP_ERROR;
198 }
199
200 pthread_mutex_init(&cwmp->event_mutex, NULL);
201
202 cwmp->el = cwmp_event_list_create(cwmp->pool, INFORM_MAX);
203 if(!cwmp->el)
204 {
205 cwmp_log_error("cwmp->el is null");
206 return CWMP_ERROR;
207 }
208
209 cwmp_event_list_init(cwmp->pool, cwmp->el);
210
211 cwmp_event_global_init(cwmp);
212
213
214 if(cwmp->event_global.event_flag == EVENT_REBOOT_NONE_FLAG || cwmp->event_global.event_flag & EVENT_REBOOT_BOOTSTRAP_FLAG)
215 {
216
217 cwmp_log_info("1.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
218 cwmp->event_global.event_flag = EVENT_REBOOT_BOOTSTRAP_FLAG;
219 cwmp_event_set_value(cwmp, INFORM_BOOTSTRAP, 1, NULL, 0, 0, 0);
220
221 cwmp->boot_flag = 1;
222
223 }
224 else //reboot
225 {
226 cwmp_log_info("2.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
227
228 cwmp_event_set_value(cwmp, INFORM_BOOT, 1, NULL, 0, 0, 0);
229 if(cwmp->event_global.event_flag & EVENT_REBOOT_ACS_FLAG)
230 {
231 cwmp_event_set_value(cwmp, INFORM_MREBOOT, 1, NULL, 0, 0, 0);
232 }
233 }
234
235 //upgrade firmware
236 if(cwmp->event_global.event_flag & EVENT_REBOOT_TRANSFERCOMPLETE_FLAG)
237 {
238 cwmp_log_info("4.flag=%d, key=%s\n", cwmp->event_global.event_flag, cwmp->event_global.event_key);
239 cwmp_event_set_value(cwmp, INFORM_MDOWNLOAD, 1, NULL, 0, 0, 0);
240 cwmp_event_set_value(cwmp, INFORM_TRANSFERCOMPLETE, 1, cwmp->event_global.event_key, cwmp->event_global.fault_code, cwmp->event_global.start, cwmp->event_global.end);
241 }
242
243 return CWMP_OK;
244}
245
246/*
247//ÉèÖÃij¸öʼþµÄcommandkey
248int cwmp_event_set_commandkey(cwmp_t *cwmp, const int event, const char *cmd_key, time_t start)
249{
250 cwmp_log_debug( "cwmp_event_set_commandkey begin, event=%d\n", event);
251 if(event < 0 || event >= INFORM_MAX)
252 {
253 cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
254 return CWMP_ERROR;
255 }
256
257 pthread_mutex_lock(&cwmp->event_mutex);
258
259 int max = cwmp->el->size;
260 int i;
261 for(i=0; i<max; i++)
262 {
263 event_code_t * ec = cwmp->el->events[i];
264 if(ec->event == event)
265 {
266 strcpy(ec->command_key, cmd_key);
267 ec->start = start;
268 break;
269 }
270 }
271
272
273 pthread_mutex_unlock(&cwmp->event_mutex);
274
275 cwmp_log_debug( "cwmp_event_set_commandkey end\n");
276 return CWMP_OK;
277}
278*/
279
280int cwmp_event_set_value(cwmp_t *cwmp, int event, int value, const char * cmd_key, int fault_code, time_t start, time_t end)
281{
282 cwmp_log_debug( "cwmp_event_set_value begin, event=%d, value=%d\n", event, value);
283 if(event < 0 || event >= INFORM_MAX)
284 {
285 cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
286 return CWMP_ERROR;
287 }
288 int count = cwmp->el->count;
289
290 int max = cwmp->el->size;
291
292 if(count >= cwmp->el->size)
293 {
294 return CWMP_ERROR;
295 }
296
297 pthread_mutex_lock(&cwmp->event_mutex);
298
299
300 int i;
301 int k=count;
302
303 for(i=0; i<count; i++)
304 {
305 event_code_t * ec = cwmp->el->events[i];
306 if(ec->event == event)
307 {
308 k = i;
309 break;
310 }
311 }
312
313 event_code_t * ec = cwmp->el->events[k];
314 memset(ec, 0, sizeof(event_code_t));
315 ec->event = event;
316 ec->ref = value;
317 ec->code = inform_event_table[event].code;
318 if(cmd_key != NULL)
319 {
320 TRstrncpy(ec->command_key, cmd_key,COMMAND_KEY_LEN);
321 cwmp_log_info("command key:%s", ec->command_key);
322 }
323
324 ec->fault_code = fault_code;
325 ec->start = start;
326 ec->end = end;
327 cwmp->el->count ++;
328
329 pthread_mutex_unlock(&cwmp->event_mutex);
330
331 return CWMP_OK;
332
333}
334
335/*
336//ÉèÖÃij¸öʼþµÄÖµ
337int cwmp_event_set_value1(cwmp_t *cwmp, int event, int value)
338{
339 cwmp_log_debug( "cwmp_event_set_value begin, event=%d, value=%d\n", event, value);
340 if(event < 0 || event >= INFORM_MAX)
341 {
342 cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
343 return CWMP_ERROR;
344 }
345
346 pthread_mutex_lock(&cwmp->event_mutex);
347 if(cwmp->el->count < cwmp->el->size-1)
348 {
349 int count = cwmp->el->count;
350 event_code_t * ec = cwmp->el->events[count];
351 ec->event = event;
352 ec->ref = value;
353 ec->code = inform_event_table[event].code;
354 cwmp->el->count ++;
355 }
356
357 pthread_mutex_unlock(&cwmp->event_mutex);
358
359 cwmp_log_debug( "cwmp_event_set_value end\n");
360 return CWMP_OK;
361}
362
363int cwmp_event_inc_set_value(cwmp_t *cwmp, int event, int step)
364{
365 cwmp_log_debug( "cwmp_event_inc_set_value begin, event=%d, step=%d\n", event, step);
366 if(event < 0 || event >= INFORM_MAX)
367 {
368 cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
369 return CWMP_ERROR;
370 }
371
372 pthread_mutex_lock(&cwmp->event_mutex);
373 // cwmp->event_info[index].ref += step;
374 int max = cwmp->el->size;
375 int i;
376 for(i=0; i<max-1; i++)
377 {
378 event_code_t * ec = cwmp->el->events[i];
379 if(ec->event == event)
380 {
381 ec->ref += step;
382 break;
383 }
384 }
385 if(i == max)
386 {
387 event_code_t * newec = cwmp->el->events[cwmp->el->count];
388 newec->event = event;
389 newec->ref += 1;
390 newec->code = inform_event_table[event].code;
391 }
392
393
394 pthread_mutex_unlock(&cwmp->event_mutex);
395
396 cwmp_log_debug( "cwmp_event_inc_set_value end\n");
397 return CWMP_OK;
398}
399
400int cwmp_event_dec_set_value(cwmp_t *cwmp, const int event, const unsigned int step)
401{
402 cwmp_log_debug( "cwmp_event_dec_set_value begin, event=%d, step=%d\n", event, step);
403 if(event < 0 || event >= INFORM_MAX)
404 {
405 cwmp_log_error( "event=%d, max=%d\n", event, INFORM_MAX);
406 return CWMP_ERROR;
407 }
408
409 pthread_mutex_lock(&cwmp->event_mutex);
410 int max = cwmp->el->size;
411 int i;
412 for(i=0; i<max-1; i++)
413 {
414 event_code_t * ec = cwmp->el->events[i];
415 if(ec->event == event)
416 {
417 ec->ref -= step;
418 if(ec->ref < 0)
419 {
420 ec->ref = 0;
421 }
422 break;
423 }
424 }
425 pthread_mutex_unlock(&cwmp->event_mutex);
426
427 cwmp_log_debug( "cwmp_event_dec_set_value end\n");
428 return CWMP_OK;
429}
430
431*/
432
433int cwmp_event_get_index(cwmp_t *cwmp,const char *name)
434{
435 int index = -1;
436 int i;
437 int size = 0;
438
439 if(!name)
440 {
441 goto finish;
442 }
443
444 size = cwmp->el->size;
445
446 for(i=0; i<INFORM_MAX; i++)
447 {
448 if(TRstrcmp(cwmp->el->events[i]->code, name) == 0)
449 {
450 index = i;
451 break;
452 }
453 }
454
455finish:
456 return index;
457}
458
459#if 0
460size_t cwmp_write_callback(void *ptr, size_t size, size_t nmemb, void *data)
461{
462 size_t realsize = size * nmemb;
463 memorystruct_t *mem = (memorystruct_t*)data;
464
465 if(!mem || !ptr)
466 {
467 cwmp_log_error( "ptr or data is NULL\n");
468 return 0;
469 }
470
471 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
472 if (mem->data)
473 {
474 memcpy(&(mem->data[mem->size]), ptr, realsize);
475 mem->size += realsize;
476 mem->data[mem->size] = 0;
477 }
478
479 return realsize;
480}
481#endif
482
483
484//Çå³ýµôÏà¹ØµÄÐÅÏ¢
485int cwmp_event_clear_active(cwmp_t *cwmp)
486{
487 int i;
488 int j;
489 int notify_flag = 0;
490
491 assert(cwmp != NULL);
492
493 FUNCTION_TRACE();
494
495 pthread_mutex_lock(&cwmp->event_mutex);
496
497 event_code_t ** pec = cwmp->el->events;
498 int evsize = cwmp->el->count;
499
500 for(i=0; i<evsize; i++)
501 {
502 if(pec[i]->ref <= 0)
503 {
504 continue;
505 }
506
507 switch(pec[i]->event)
508 {
509 case INFORM_BOOTSTRAP:
510 {
511 cwmp->event_global.event_flag |= EVENT_REBOOT_ACS_FLAG;
512 cwmp_event_file_save(cwmp);
513
514 }
515 break;
516
517 case INFORM_MREBOOT:
518 cwmp->event_global.event_flag |= EVENT_REBOOT_ACS_FLAG;
519 cwmp_event_file_save(cwmp);
520 break;
521
522 case INFORM_VALUECHANGE:
523 notify_flag = 1;
524 break;
525
526 case INFORM_TRANSFERCOMPLETE:
527 {
528 cwmp->event_global.event_flag |= EVENT_REBOOT_TRANSFERCOMPLETE_FLAG;
529 memset(cwmp->event_global.event_key, 0, COMMAND_KEY_LEN+1);
530 strncpy(cwmp->event_global.event_key, pec[i]->command_key, COMMAND_KEY_LEN);
531 cwmp->event_global.fault_code = pec[i]->fault_code;
532 cwmp->event_global.start = pec[i]->start;
533 cwmp->event_global.end = pec[i]->end;
534 cwmp_event_file_save(cwmp);
535 }
536 break;
537
538 }
539
540 pec[i]->ref = 0; //clear
541 }
542
543 cwmp->el->count = 0;
544 pthread_mutex_unlock(&cwmp->event_mutex);
545
546 //Çå³ýValue Change
547 /*
548 if(notify_flag == 1)
549 {
550 hash_index_t *hi = NULL;
551 val_change_t *tmp = NULL;
552 char *key = NULL;
553
554 for (hi = hash_first(cwmp->ht_val_change); hi; hi = hash_next(hi))
555 {
556 hash_this(hi, (const void**)(&key), NULL, (void**)&tmp);
557 if(!key || !tmp)
558 {
559 continue;
560 }
561
562 //´Óvalue change hashÈ¥³ý
563 hash_set(cwmp->ht_val_change, (void*)tmp->name, strlen(tmp->name), NULL);
564
565 //ÊÍ·ÅÄÚ´æ
566 if(tmp->value)
567 {
568 free_check(tmp->value);
569 }
570 free_check(tmp);
571 }
572 }
573 */
574
575 return CWMP_OK;
576}
577
578
579//È¡µÃactive eventÒÔ¼°count
580/*
581static int get_active_event_list(cwmp_t *cwmp, event_list_t **pevent_list, int *pevt_count)
582{
583 int i;
584 event_list_t *evlist = NULL;
585 int count = 0;
586 int ret = CWMP_OK;
587
588 if(!cwmp || !pevent_list || !pevt_count)
589 {
590 cwmp_log_error( "param is NULL\n");
591 return CWMP_ERROR;
592 }
593
594 pthread_mutex_lock(&cwmp->event_mutex);
595 int max = cwmp->event_list->count;
596 for(i=0; max; i++)
597 {
598 if(cwmp->event_info[i].ref > 0)
599 {
600 evlist = realloc(evlist, sizeof(event_t)*(count+1));
601 if(!evlist)
602 {
603 ret = CWMP_ERROR;
604 cwmp_log_error( "realloc fail\n");
605 break;
606 }
607 memcpy(&evlist[count], &cwmp->event_info[i], sizeof(event_list_t));
608 count++;
609 }
610 }
611 pthread_mutex_unlock(&cwmp->event_mutex);
612
613 if(ret == CWMP_OK)
614 {
615 *pevent_list = evlist;
616 *pevt_count = count;
617 }
618 else
619 {
620 free(evlist);
621 }
622
623 return ret;
624}
625
626*/
627
628