blob: 58038f36bc6aa29dcec41c712a1bc4b2f22dc1cc [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/************************************************************************
2 * *
3 * Netcwmp/Opencwmp Project *
4 * A software client for enabling TR-069 in embedded devices (CPE). *
5 * *
6 * Copyright (C) 2013-2014 netcwmp.netcwmp group *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU General Public License *
10 * as published by the Free Software Foundation; either version 2 *
11 * of the License, or (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the *
20 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, *
21 * Boston, MA 02111-1307 USA *
22 * *
23 * Copyright 2013-2014 Mr.x(Mr.x) <netcwmp@gmail.com> *
24 * *
25 ***********************************************************************/
26#include "cwmp/periodic.h"
27
28#include "cwmp_module.h"
29#include "cwmp_agent.h"
30#include <cwmp/session.h>
31//#include "modules/data_model.h"
32
33#include "soft_timer.h"
34
35
36#define CWMP_TRUE 1
37
38#define MAX_SESSION_RETRY 3
39
40enum
41{
42 CWMP_ST_START = 0,
43 CWMP_ST_INFORM,
44 CWMP_ST_SEND,
45 CWMP_ST_RESEMD,
46 CWMP_ST_RECV,
47 CWMP_ST_ANSLYSE,
48 CWMP_ST_RETRY,
49 CWMP_ST_END,
50 CWMP_ST_EXIT
51};
52
53#define SECHEDULE_INFORM_TIMER (31)
54
55schedule_inform_info_st g_si;
56
57int get_session_status()
58{
59 return g_si.cwmp->session_running;
60}
61
62void *schedule_inform_timeout(void *arg)
63{
64 FUNCTION_TRACE();
65 int i = 0;
66
67 schedule_inform_info_st *psi = (schedule_inform_info_st *)arg;
68
69 cwmp_log_info("push begin");
70
71 psi->timer_running = 2;
72
73// while(1)
74// {
75// if(SESSION_NO_RUNNING == get_session_status())
76// {
77 psi->cwmp->new_request = CWMP_YES;
78 cwmp_event_set_value(psi->cwmp, INFORM_SCHEDULED, 1, psi->commandKey, 0, 0, 0);
79 cwmp_log_info("commandkey is:%s", psi->commandKey);
80 psi->timer_running = 0;
81
82 cwmp_log_info("push ok");
83
84 return NULL;
85// }
86// else
87// {
88// cwmp_log_info("there is session running");
89// psi->timer_running = 0;
90// g_si.delaytime += 1;
91// if(CWMP_OK != schedule_inform_create_timer(&g_si))
92// {
93// ;
94// }
95// }
96// }
97
98 return NULL;
99}
100
101
102int schedule_inform_create_timer(schedule_inform_info_st* psi)
103{
104 FUNCTION_TRACE();
105
106 if(2 == g_si.timer_running){
107 cwmp_log_info("there is other schedule inform running!");
108 return CWMP_OK;
109 }
110 else if(1 == g_si.timer_running)
111 {
112 // if exist one schedule inform timer, update
113 cwmp_log_info("there is other schedule inform active!");
114 sc_timer_delete(SECHEDULE_INFORM_TIMER);
115 }
116 else
117 {
118 cwmp_log_info("update schedule inform info, create timer");
119 }
120
121 TRstrncpy(g_si.commandKey, psi->commandKey, COMMAND_KEY_LEN);
122 g_si.delaytime = psi->delaytime;
123 g_si.cwmp = psi->cwmp;
124
125
126 cwmp_log_info("begin to create timer!");
127
128 long result = sc_timer_create(SECHEDULE_INFORM_TIMER, TIMER_FLAG_ONCE, psi->delaytime*1000, schedule_inform_timeout, &g_si);
129 g_si.timer_running = 1;
130 cwmp_log_info("end to create timer!");
131
132 cwmp_log_info("commandkey is:%s, delaytime:%d", psi->commandKey, psi->delaytime);
133
134 return CWMP_OK;
135}
136
137
138
139int cwmp_agent_retry_session(cwmp_session_t * session)
140{
141
142 int sec = 0;
143 int count = 1;
144
145 srand(time(NULL));
146 switch (session->retry_count)
147 {
148 case 0:
149 {
150 count = 5; //5~10
151 break;
152 }
153 case 1:
154 {
155 count = 10; //5~15
156 break;
157 }
158 case 2:
159 {
160 count = 20; //5~25
161 break;
162 }
163 default:
164 {
165 count = 30; //5~35
166 break;
167 }
168 }
169
170 sec = 5 + rand()% count;
171
172 while (sec>0)
173 {
174 sleep(1);
175 sec--;
176 }
177
178 if (session->retry_count > MAX_SESSION_RETRY)
179 {
180 session->retry_count = 0;
181 return CWMP_TIMEOUT;
182 }
183 else
184 {
185 session->retry_count ++;
186 return CWMP_OK;
187 }
188
189}
190
191
192int cwmp_agent_create_datetimes(datatime_t *nowtime)
193{
194 struct tm *t;
195 time_t tn;
196
197
198 //FUNCTION_TRACE();
199 tn = time(NULL);
200#ifdef WIN32
201 cwmp_log_debug("inform datatime");
202 //localtime_s(&t, &tn);
203 t = MALLOC(sizeof(struct tm));
204 memset(t, 0, sizeof(struct tm));
205#else
206 t = localtime(&tn);
207#endif
208
209 if(NULL != t)
210 {
211 nowtime->year = t->tm_year + 1900;
212 nowtime->month = t->tm_mon + 1;
213 nowtime->day = t->tm_mday;
214 nowtime->hour = t->tm_hour;
215 nowtime->min = t->tm_min;
216 nowtime->sec = t->tm_sec;
217 }
218 else
219 {
220 cwmp_log_error("localtime return null");
221 }
222
223 return CWMP_OK;
224}
225
226//È¡µÃactive eventÒÔ¼°count
227int cwmp_agent_get_active_event(cwmp_t *cwmp, cwmp_session_t * session, event_list_t **pevent_list)
228{
229 event_list_t * el;
230 event_code_t * ev;
231 int i=0;
232 FUNCTION_TRACE();
233
234 el = cwmp_create_event_list(session->env, INFORM_MAX);
235 if(NULL == el)
236 {
237 cwmp_log_error("el cwmp_create_event_list return NULL");
238 return CWMP_ERROR;
239 }
240
241 event_code_t ** pec = cwmp->el->events;
242
243 int elsize = cwmp->el->count;
244 for(i=0; i<elsize; i++)
245 {
246
247 if(pec[i] && pec[i]->ref > 0)
248 {
249
250 event_code_t * ec = pec[i];
251 ev = cwmp_create_event_code(session->env);
252 if(NULL == ev)
253 {
254 cwmp_log_error("ev cwmp_create_event_code return null");
255 return CWMP_ERROR;
256 }
257
258 ev->event = ec->event;
259 ev->code = ec->code;
260
261 if (pec[i]->event == INFORM_MREBOOT || pec[i]->event == INFORM_BOOTSTRAP)
262 {
263 strncpy(ev->command_key , ec->command_key, COMMAND_KEY_LEN);
264 }
265
266
267 el->events[el->count++] = ev;
268 ev = NULL;
269
270 }
271 }
272 if (el->count == 0)
273 {
274 ev = cwmp_create_event_code(session->env);
275 if(NULL == ev)
276 {
277 cwmp_log_error("ev cwmp_create_event_code return null");
278 return CWMP_ERROR;
279 }
280
281 ev->event = INFORM_BOOT;
282 ev->code = CWMP_INFORM_EVENT_CODE_1;
283 el->events[el->count++] = ev;
284 }
285
286 *pevent_list = el;
287
288 return CWMP_OK;
289}
290
291int cwmp_agent_send_request(cwmp_session_t * session)
292{
293 FUNCTION_TRACE();
294 return cwmp_session_send_request(session);
295}
296
297int cwmp_agent_recv_response(cwmp_session_t * session)
298{
299 return cwmp_session_recv_response(session);
300}
301
302void cwmp_agent_start_session(cwmp_t * cwmp)
303{
304 int rv;
305 cwmp_session_t * session;
306 int session_close = CWMP_NO;
307 xmldoc_t * newdoc;
308 FUNCTION_TRACE();
309 event_list_t *evtlist;
310
311 while (TRUE)
312 {
313 if (cwmp->new_request == CWMP_NO)
314 {
315 cwmp_log_debug("No new request from ACS\n");
316 sleep(2);
317 //cwmp->new_request = CWMP_YES;
318 continue;
319 }
320
321 cwmp_log_debug("New request from ACS\n");
322
323 cwmp_clean_periodic_inform(cwmp);
324
325 cwmp->new_request = CWMP_NO;
326 session = cwmp_session_create(cwmp);
327 if(NULL == session)
328 {
329 cwmp_log_error("session cwmp_session_create return NULL");
330 cwmp->new_request = CWMP_YES;
331 continue;
332 }
333
334 session_close = CWMP_NO;
335 session->timeout = cwmp_conf_get_int("cwmpd:http_timeout");
336 //cwmp_session_set_timeout(cwmp_conf_get_int("cwmpd:http_timeout"));
337 cwmp_log_debug("session timeout is %d", session->timeout);
338
339 cwmp_session_open(session);
340
341 while (!session_close)
342 {
343 //cwmp_log_debug("session status: %d", session->status);
344 switch (session->status)
345 {
346 case CWMP_ST_START:
347 //create a new connection to acs
348 cwmp_log_debug("session stutus: New START\n");
349
350 if (cwmp_session_connect(session, cwmp->acs_url) != CWMP_OK)
351 {
352 cwmp_log_error("connect to acs: %s failed.\n", cwmp->acs_url);
353 session->status = CWMP_ST_RETRY;
354 }
355 else
356 {
357 session->status = CWMP_ST_INFORM;
358 }
359 break;
360 case CWMP_ST_INFORM:
361 evtlist = NULL;
362 cwmp_log_debug("session stutus: INFORM\n");
363 cwmp_agent_get_active_event(cwmp, session, & evtlist);
364 if(evtlist != NULL)
365 {
366 cwmp_event_clear_active(cwmp);
367 }
368 else
369 {
370 cwmp_log_debug("evtlist is null");
371 session->status = CWMP_ST_EXIT;
372 break;
373 }
374
375 cwmp_log_debug("session stutus: INFORM2\n");
376 if (cwmp->acs_auth)
377 {
378 cwmp_session_set_auth(session, cwmp->acs_user, cwmp->acs_pwd );
379 }
380
381 cwmp_log_debug("session stutus: INFORM3\n");
382 newdoc = cwmp_session_create_inform_message(session, evtlist, session->envpool);
383
384 if(NULL != newdoc)
385 {
386 cwmp_write_doc_to_chunk(newdoc, session->writers, session->envpool);
387 session->last_method = CWMP_INFORM_METHOD;
388 session->status = CWMP_ST_SEND;
389 }
390 else
391 {
392 cwmp_log_error("newdoc is null");
393 session->last_method = CWMP_INFORM_METHOD;
394 session->status = CWMP_ST_EXIT;
395 }
396
397 break;
398
399 case CWMP_ST_SEND:
400
401 cwmp_log_debug("session stutus: SEND");
402 cwmp_log_debug("session data request length: %d", cwmp_chunk_length(session->writers));
403 session->newdata = CWMP_NO;
404
405 rv = cwmp_agent_send_request(session);
406
407 if (rv == CWMP_OK)
408 {
409 cwmp_log_debug("session data sended OK, rv=%d", rv);
410 session->status = CWMP_ST_RECV;
411 }
412 else
413 {
414 cwmp_log_debug("session data sended faild! rv=%d", rv);
415 session->status = CWMP_ST_EXIT;
416 /*
417 if (rv == CWMP_COULDNOT_CONNECT)
418 {
419 session->status = CWMP_ST_RETRY;
420 }
421 else
422 {
423 session->status = CWMP_ST_EXIT;
424 }
425 */
426 }
427
428 break;
429 case CWMP_ST_RECV:
430 cwmp_log_debug("session stutus: RECV");
431 cwmp_chunk_clear(session->readers);
432
433 rv = cwmp_agent_recv_response(session);
434
435 if (rv == CWMP_OK)
436 {
437 session->status = CWMP_ST_ANSLYSE;
438 }
439 else
440 {
441 session->status = CWMP_ST_END;
442 }
443 break;
444
445 case CWMP_ST_ANSLYSE:
446 cwmp_log_debug("session stutus: ANSLYSE");
447 rv = cwmp_agent_analyse_session(session);
448 if (rv == CWMP_OK)
449 {
450 session->status = CWMP_ST_SEND;
451 }
452 else
453 {
454 session->status = CWMP_ST_END;
455 }
456 break;
457 case CWMP_ST_RETRY:
458 cwmp_log_debug("session stutus: RETRY");
459 if (cwmp_agent_retry_session(session) == CWMP_TIMEOUT)
460 {
461 cwmp_log_debug("session retry timeover, go out");
462 session->status = CWMP_ST_EXIT;
463 }
464 else
465 {
466 session->status = CWMP_ST_START;
467 }
468 break;
469 case CWMP_ST_END:
470 //close connection of ACS
471 cwmp_log_debug("session stutus: END");
472 //run task from queue
473
474 if (session->newdata == CWMP_YES)
475 {
476 session->status = CWMP_ST_SEND;
477 }
478 else
479 {
480 session->status = CWMP_ST_EXIT;
481 }
482 break;
483
484 case CWMP_ST_EXIT:
485 cwmp_log_debug("session stutus: EXIT");
486 cwmp_session_close(session);
487 if (session->reconnect == CWMP_YES)
488 {
489 session->reconnect = CWMP_NO;
490 session->status = CWMP_ST_START;
491 break;
492 }
493 session_close = CWMP_YES;
494 break;
495
496
497 default:
498 cwmp_log_debug("Unknown session stutus");
499 break;
500 }//end switch
501 }//end while(!session_close)
502
503 cwmp_log_debug("session stutus: EXIT");
504 cwmp_session_free(session);
505 session = NULL;
506
507 cwmp->session_running = SESSION_NO_RUNNING;
508
509 int newtaskres = cwmp_agent_run_tasks(cwmp);
510 if(newtaskres == CWMP_YES)
511 {
512 cwmp->new_request = CWMP_YES;
513 }
514
515 cwmp_reset_periodic_inform(cwmp);
516
517 }//end while(TRUE)
518}
519
520
521int cwmp_agent_analyse_session(cwmp_session_t * session)
522{
523 pool_t * doctmppool = NULL;
524 char * xmlbuf;
525 cwmp_uint32_t len;
526 xmldoc_t * doc;
527 char * method;
528 xmldoc_t * newdoc = NULL;
529 int rc = CWMP_OK;
530
531 static char * xml_fault = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:cwmp=\"urn:dslforum-org:cwmp-1-0\" xmlns=\"urn:dslforum-org:cwmp-1-0\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" id=\"_0\"><SOAP-ENV:Fault>Error Message</SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>";
532
533 cwmp_uint32_t msglength = 0;
534
535 FUNCTION_TRACE();
536 if(NULL == session)
537 {
538 cwmp_log_error("session is null");
539 return CWMP_ERROR;
540 }
541
542 cwmp_t * cwmp = session->cwmp;
543 if(NULL == cwmp)
544 {
545 cwmp_log_error("cwmp is null");
546 return CWMP_ERROR;
547 }
548
549 msglength = cwmp_chunk_length(session->readers);
550 if (msglength<= 0)
551 {
552 session->newdata = CWMP_NO;
553 cwmp_log_debug("analyse receive length is 0");
554 goto eventcheck;
555// return CWMP_ERROR;
556 }
557
558 doctmppool = pool_create(POOL_DEFAULT_SIZE);
559 if(NULL == doctmppool)
560 {
561 cwmp_log_error("doctmppool is null");
562 return CWMP_ERROR;
563 }
564
565 xmlbuf = pool_palloc(doctmppool, msglength+32);
566 if(NULL == xmlbuf)
567 {
568 cwmp_log_error("xmlbuf is null");
569 return CWMP_ERROR;
570 }
571
572 len = sprintf(xmlbuf,"<cwmp>");
573 cwmp_chunk_copy(xmlbuf + len, session->readers, msglength);
574 strncpy(xmlbuf+len+msglength, "</cwmp>", 32-len-1);
575
576 cwmp_log_debug("agent analyse xml: \n%s", xmlbuf);
577
578 doc = XmlParseBuffer(doctmppool, xmlbuf);
579 if (!doc)
580 {
581 cwmp_log_debug("analyse create doc null\n");
582 cwmp_chunk_write_string(session->writers, xml_fault, TRstrlen(xml_fault), session->envpool);
583 goto finished;
584
585 }
586
587 method = cwmp_get_rpc_method_name(doc);
588 cwmp_log_debug("analyse method is: %s\n", method);
589
590 cwmp_chunk_clear(session->writers);
591 pool_clear(session->envpool);
592
593 cwmp_log_info("----------------------------");
594
595 if (TRstrcmp(method, CWMP_RPC_GETRPCMETHODS) == 0)
596 {
597 newdoc = cwmp_session_create_getrpcmethods_response_message(session, doc, doctmppool);
598 }
599 else if (TRstrcmp(method, CWMP_RPC_INFORMRESPONSE) == 0)
600 {
601 newdoc = NULL;
602 }
603 else if (TRstrcmp(method, CWMP_RPC_GETPARAMETERNAMES) == 0)
604 {
605 newdoc = cwmp_session_create_getparameternames_response_message(session, doc, doctmppool);
606 }
607 else if (TRstrcmp(method, CWMP_RPC_GETPARAMETERVALUES) == 0)
608 {
609 newdoc = cwmp_session_create_getparametervalues_response_message(session, doc, doctmppool);
610 }
611 else if (TRstrcmp(method, CWMP_RPC_SETPARAMETERVALUES) == 0)
612 {
613 newdoc = cwmp_session_create_setparametervalues_response_message(session, doc, doctmppool);
614 }
615 else if (TRstrcmp(method, CWMP_RPC_DOWNLOAD) == 0)
616 {
617 newdoc = cwmp_session_create_download_response_message(session, doc, doctmppool);
618 }
619 else if (TRstrcmp(method, CWMP_RPC_UPLOAD) == 0)
620 {
621 newdoc = cwmp_session_create_upload_response_message(session, doc, doctmppool);
622 }
623 else if (TRstrcmp(method, CWMP_RPC_TRANSFERCOMPLETERESPONSE) == 0)
624 {
625 newdoc = NULL;
626 }
627 else if (TRstrcmp(method, CWMP_RPC_REBOOT) == 0)
628 {
629 newdoc = cwmp_session_create_reboot_response_message(session, doc, doctmppool);
630 }
631 else if (TRstrcmp(method, CWMP_RPC_ADDOBJECT) == 0)
632 {
633 newdoc = cwmp_session_create_addobject_response_message(session, doc, doctmppool);
634 }
635 else if (TRstrcmp(method, CWMP_RPC_DELETEOBJECT) == 0)
636 {
637 newdoc = cwmp_session_create_deleteobject_response_message(session, doc, doctmppool);
638 }
639
640 else if (TRstrcmp(method, CWMP_RPC_FACTORYRESET) == 0)
641 {
642 newdoc = cwmp_session_create_factoryreset_response_message(session, doc, doctmppool);
643 }
644 else if (TRstrcmp(method, CWMP_RPC_GETPARAMETERATTRIBUTES) == 0)
645 {
646 newdoc = cwmp_session_create_getparamterattributes_response_message(session, doc, doctmppool);
647 }
648 else if (TRstrcmp(method, CWMP_RPC_SETPARAMETERATTRIBUTES) == 0)
649 {
650 newdoc = cwmp_session_create_setparamterattributes_response_message(session, doc, doctmppool);
651 }
652 else if (TRstrcmp(method, CWMP_RPC_SCHEDULEINFORM) == 0)
653 {
654 schedule_inform_info_st si;
655 si.cwmp = session->cwmp;
656 newdoc = cwmp_session_create_scheduleinform_response_message(session, doc, doctmppool, &si);
657 int rc = schedule_inform_create_timer(&si);
658 }
659 else
660 {
661 //check event queue
662 //newdoc = cwmp_session_create_event_response_message(session, doc, doctmppool);
663 cwmp_log_info("unknown method type:%s", method);
664 }
665
666 cwmp_log_info("----------------------------");
667
668// cwmp_t * cwmp = session->cwmp;
669 if(newdoc == NULL)
670 {
671 cwmp_log_debug("agent analyse newdoc is null. ");
672
673eventcheck:
674 {
675
676 cwmp_log_debug("agent analyse begin check global event, %d", cwmp->event_global.event_flag);
677
678 //check global event for transfercomplete
679
680 if(cwmp->event_global.event_flag & EVENT_REBOOT_TRANSFERCOMPLETE_FLAG)
681 {
682 cwmp->event_global.event_flag &= ~EVENT_REBOOT_TRANSFERCOMPLETE_FLAG;
683 if(!doctmppool)
684 {
685 doctmppool = pool_create(POOL_DEFAULT_SIZE);
686 }
687
688 if(!doctmppool)
689 {
690 cwmp_log_error("doctmppool pool create return null");
691 return CWMP_ERROR;
692 }
693 event_code_t ec;
694 ec.event = INFORM_TRANSFERCOMPLETE;
695 TRstrncpy(ec.command_key, cwmp->event_global.event_key, COMMAND_KEY_LEN);
696 ec.fault_code = cwmp->event_global.fault_code;
697 ec.start = cwmp->event_global.start;
698 ec.end = cwmp->event_global.end;
699 newdoc = cwmp_session_create_transfercomplete_message(session, &ec, doctmppool);
700
701 }
702
703 }
704
705 }
706
707
708 cwmp_log_debug("newdoc %p, msglength: %d", newdoc, msglength );
709 if(newdoc != NULL) // || (newdoc == NULL && msglength == 0 && session->retry_count < 2))
710 {
711 session->newdata = CWMP_YES;
712 cwmp_write_doc_to_chunk(newdoc, session->writers, session->envpool);
713 rc = CWMP_OK;
714 }
715 else if(/*newdoc == NULL && */msglength != 0)
716 {
717 session->newdata = CWMP_YES;
718 cwmp_write_doc_to_chunk(newdoc, session->writers, session->envpool);
719 rc = CWMP_OK;
720 }
721 else
722 {
723 rc = CWMP_ERROR;
724 }
725
726finished:
727 if(doctmppool != NULL)
728 {
729 pool_destroy(doctmppool);
730 }
731
732 return rc;
733}
734
735
736
737static void print_param(parameter_node_t * param, int level)
738{
739 if(!param) return;
740
741 parameter_node_t * child;
742 char fmt[128];
743 //cwmp_log_debug("name: %s, type: %s, level: %d\n", param->name, cwmp_get_type_string(param->type), level);
744 int i=0;
745
746 snprintf(fmt, 127, "|%%-%ds%%s, get:%%p set:%%p refresh:%%p\n", level*4);
747
748 cwmp_log_debug_src(fmt, "----", param->name, param->get, param->set, param->refresh);
749
750 child = param->child;
751
752 if(!child)
753 return;
754 print_param(child, level+1);
755
756 parameter_node_t * next = child->next_sibling;
757
758 while(next)
759 {
760 print_param(next, level+1);
761 next = next->next_sibling;
762 }
763}
764
765
766
767void cwmp_agent_session(cwmp_t * cwmp)
768{
769 char name[1024] = {0};
770 char value[1024]= {0};
771 char local_ip[256];
772
773 char * envstr;
774 char * encstr;
775
776 envstr = "SOAP-ENV"; //cwmp_conf_get("cwmp:soap_env");
777 encstr = "SOAP-ENC"; // cwmp_conf_get("cwmp:soap_enc");
778
779 cwmp_set_envelope_ns(envstr, encstr);
780
781#if 0
782 if (cwmp_session_get_localip(local_ip) == -1)
783 {
784 cwmp_log_error("get local ip error. exited.\n");
785 exit(-1);
786 }
787#else
788 cwmp_session_get_localip(local_ip);
789#endif
790
791 print_param(cwmp->root, 0);
792
793 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, ManagementServerModule, URLModule);
794 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, cwmp->acs_url, TRstrlen(cwmp->acs_url), cwmp->pool);
795
796 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, ManagementServerModule, ConnectionRequestURLModule);
797 TRsnprintf(value, 1024, "http://%s:%d", local_ip, cwmp->httpd_port);
798 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, value, TRstrlen(value), cwmp->pool);
799 cwmp_log_debug("set request name:%s, value:%s", name, value);
800
801
802 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, DeviceInfoModule, ManufacturerModule);
803 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, cwmp->cpe_mf, TRstrlen(cwmp->cpe_mf), cwmp->pool);
804
805
806 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, DeviceInfoModule, ManufacturerOUIModule);
807 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, cwmp->cpe_oui, TRstrlen(cwmp->cpe_oui), cwmp->pool);
808
809 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, DeviceInfoModule, ProductClassModule);
810 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, cwmp->cpe_pc, TRstrlen(cwmp->cpe_pc), cwmp->pool);
811
812 CWMP_SPRINTF_PARAMETER_NAME(name, 3, InternetGatewayDeviceModule, DeviceInfoModule, SerialNumberModule);
813 cwmp_data_set_parameter_value(cwmp, cwmp->root, name, cwmp->cpe_sn, TRstrlen(cwmp->cpe_sn), cwmp->pool);
814
815
816 cwmp_agent_start_session(cwmp);
817}
818
819
820/*
821int cwmp_agent_download_file(download_arg_t * dlarg)
822{
823 int faultcode = 0;
824 char url[255];
825 if (TRstrncasecmp(dlarg->url, "ftp://", 6) == 0)
826 {
827 if (dlarg->username != NULL && strlen(dlarg->username) != 0)
828 {
829 TRsnprintf(url, 255, "ftp://%s:%s@%s", dlarg->username, dlarg->password, dlarg->url+6);
830 }
831 else
832 {
833 TRstrncpy(url, dlarg->url, 255);
834 }
835 }
836 else
837 {
838 TRstrncpy(url, dlarg->url, 255);
839 }
840
841 fetchIO * downfile = fetchGetURL(url, "");
842 if (!downfile)
843 {
844 cwmp_log_debug("download file fail: %s", url);
845 faultcode = 9001;
846 }
847 else
848 {
849 char readbuf[1024];
850 int readlen;
851 char targetfile[64];
852 if (dlarg->targetname != NULL && strlen(dlarg->targetname) != 0)
853 {
854 TRsnprintf(targetfile, 64, "/tmp/%s", dlarg->targetname);
855 }
856 else
857 {
858 TRsnprintf(targetfile, 64, "/tmp/%d.file", time(NULL));
859 }
860 FILE * df = fopen(targetfile, "wb+");
861 while (df != NULL)
862 {
863 readlen = fetchIO_read(downfile, readbuf, 1023);
864 if (readlen <= 0)
865 {
866 cwmp_log_debug("fetch io read zero. %s", readlen);
867 break;
868 }
869 readbuf[readlen] = 0;
870 fwrite(readbuf, readlen, 1, df);
871 }
872
873 if (df)
874 {
875 fclose(df);
876 }
877 else
878 {
879 faultcode = 9001;
880 }
881
882 cwmp_log_debug("download file ok: %s, %s", url, targetfile);
883
884
885 fetchIO_close(downfile);
886 }
887
888
889 return faultcode;
890
891}
892
893
894
895int cwmp_agent_upload_file(upload_arg_t * ularg)
896{
897 int faultcode = 0;
898 char url[255];
899
900 if (TRstrncasecmp(ularg->url, "ftp://", 6) == 0)
901 {
902 if (ularg->username != NULL && strlen(ularg->username) != 0)
903 {
904 TRsnprintf(url, 255, "ftp://%s:%s@%s", ularg->username, ularg->password, ularg->url+6);
905 }
906 else
907 {
908 TRstrncpy(url, ularg->url, 255);
909 }
910 }
911 else
912 {
913 TRstrncpy(url, ularg->url, 255);
914 }
915
916 fetchIO * uploadfile = fetchPutURL(url, "");
917 if (!uploadfile)
918 {
919 cwmp_log_debug("upload file fail: %s", url);
920 faultcode = 9001;
921 }
922 else
923 {
924 char readbuf[1024];
925 int readlen;
926 char targetfile[64];
927 FILE * uf;
928 int rc;
929 if(strcmp(ularg->filetype, "1 Vendor Configuration File") == 0)
930 {
931 //¸ù¾Ýʵ¼ÊÇé¿ö, ÐÞ¸ÄÕâÀïµÄÅäÖÃÎļþ·¾¶
932
933 uf = fopen("/tmp/mysystem.cfg", "rb");
934 }
935 else if(strcmp(ularg->filetype, "2 Vendor Log File") == 0)
936 {
937 //¸ù¾Ýʵ¼ÊÇé¿ö, ÐÞ¸ÄÕâÀïµÄÅäÖÃÎļþ·¾¶
938 uf = fopen("/tmp/mysystem.log", "rb");
939 }
940 else
941 {
942 uf = fopen("/tmp/mysystem.log", "rb");
943 }
944
945
946 while (uf != NULL)
947 {
948 readlen = fread(readbuf, 1024, 1, uf);
949 if (readlen <= 0)
950 {
951 cwmp_log_debug("fetch io read zero. %s", readlen);
952
953 break;
954 }
955 readbuf[readlen] = 0;
956
957 rc = fetchIO_write(uploadfile, readbuf, readlen);
958 if(rc <= 0)
959 {
960 faultcode = 9001;
961 break;
962 }
963
964 }
965
966 if(uf)
967 {
968 fclose(uf);
969 }
970 else
971 {
972 faultcode = 9001;
973 }
974
975
976
977 cwmp_log_debug("upload file finished: %s, file:%s", url, targetfile);
978
979
980 fetchIO_close(uploadfile);
981 }
982
983
984 return faultcode;
985
986}
987*/
988
989
990
991int cwmp_agent_download_file(download_arg_t * dlarg)
992{
993 int faultcode = 0;
994 char * fromurl = dlarg->url;
995
996 char tofile[256+1] = {0};
997 char downloadroot[256+1] = {0};
998
999 FUNCTION_TRACE();
1000
1001 if(!fromurl)
1002 {
1003 cwmp_log_error("download src file path is null!");
1004 return 9001;
1005 }
1006
1007 if(NULL == dlarg->targetname)
1008 {
1009 if(TRstrcmp(dlarg->filetype, "1 Firmware Upgrade Image") == 0)
1010 {
1011 cwmp_conf_get("file:download_img_path", tofile);
1012 }
1013 else if(TRstrcmp(dlarg->filetype, "3 Vendor Configuration File") == 0)
1014 {
1015 cwmp_conf_get("file:download_cfg_path", tofile);
1016 }
1017 else
1018 {
1019 cwmp_conf_get("file:download_unknown_path", tofile);
1020 }
1021 }
1022 else
1023 {
1024 cwmp_conf_get("file:download_root", downloadroot);
1025 TRsnprintf(tofile, 256, "%s/%s", downloadroot, dlarg->targetname);
1026 }
1027
1028 cwmp_log_info("file type:%s, save file(%s) to path:%s", dlarg->filetype, fromurl, tofile);
1029
1030 if(/*dlarg->url &&*/ TRstrncasecmp("ftp://", dlarg->url, 6) == 0)
1031 {
1032 cwmp_log_debug("Download url:%s", dlarg->url);
1033 return 9001;
1034 }
1035
1036 faultcode = http_receive_file(fromurl, tofile);
1037
1038 if(faultcode != CWMP_OK)
1039 {
1040 cwmp_log_debug("faultcode:%d", faultcode);
1041 faultcode = 9001;
1042 }
1043
1044 return faultcode;
1045}
1046
1047
1048
1049int cwmp_agent_upload_file(upload_arg_t * ularg)
1050{
1051 int faultcode = 0;
1052 FUNCTION_TRACE();
1053 char * fromfile;
1054
1055 char cfg_path[256] = {0};
1056 char log_path[256] = {0};
1057
1058 FUNCTION_TRACE();
1059
1060 cwmp_conf_get("file:upload_cfg_path", cfg_path);
1061 cwmp_conf_get("file:upload_log_path", log_path);
1062
1063 if(TRstrcmp(ularg->filetype, "1 Vendor Configuration File") == 0)
1064 {
1065 fromfile = cfg_path;
1066 }
1067 else if(TRstrcmp(ularg->filetype, "2 Vendor Log File") == 0)
1068 {
1069 fromfile = log_path;
1070 }
1071 else
1072 {
1073 fromfile = cfg_path;
1074 }
1075
1076 cwmp_log_info("upload type:%s, send file:%s, url:%s", ularg->filetype, fromfile, ularg->url);
1077
1078
1079 faultcode = http_send_file(fromfile, ularg->url);
1080
1081 if(faultcode != CWMP_OK)
1082 {
1083 cwmp_log_error("http send file return[%d], not CWMP_OK, set faultcode 9001", faultcode);
1084 faultcode = 9001;
1085 }
1086
1087 return faultcode;
1088}
1089
1090
1091
1092int cwmp_agent_run_tasks(cwmp_t * cwmp)
1093{
1094 void * data;
1095 int tasktype = 0;;
1096 int ok = CWMP_NO;
1097 int rc = 0;
1098
1099 FUNCTION_TRACE();
1100
1101 while(1)
1102 {
1103 tasktype = queue_pop(cwmp->queue, &data);
1104 if(tasktype == -1)
1105 {
1106 cwmp_log_debug("no more task to run");
1107 break;
1108 }
1109 ok = CWMP_YES;
1110 switch(tasktype)
1111 {
1112 case TASK_DOWNLOAD_TAG:
1113 {
1114 cwmp_log_debug("Download begin.....");
1115
1116 download_arg_t * dlarg = (download_arg_t*)data;
1117 //begin download file
1118 time_t starttime = time(NULL);
1119 int faultcode = 0;
1120
1121 faultcode = cwmp_agent_download_file(dlarg);
1122
1123 time_t endtime = time(NULL);
1124 cwmp_event_set_value(cwmp, INFORM_TRANSFERCOMPLETE, 1,dlarg->cmdkey, faultcode, starttime, endtime);
1125
1126
1127 FREE(dlarg);
1128
1129 cwmp_log_debug("Download OK.....");
1130 }
1131 break;
1132
1133 case TASK_UPLOAD_TAG:
1134 {
1135 cwmp_log_debug("Upload begin.....");
1136
1137 upload_arg_t * ularg = (upload_arg_t*)data;
1138 //begin download file
1139 time_t starttime = time(NULL);
1140 int faultcode = 0;
1141
1142 faultcode = cwmp_agent_upload_file(ularg);
1143
1144 time_t endtime = time(NULL);
1145 cwmp_event_set_value(cwmp, INFORM_TRANSFERCOMPLETE, 1,ularg->cmdkey, faultcode, starttime, endtime);
1146
1147
1148 FREE(ularg);
1149
1150 cwmp_log_debug("Upload OK.....");
1151 }
1152 break;
1153
1154 case TASK_REBOOT_TAG:
1155 {
1156 //begin reboot system
1157 cwmp_log_debug("reboot ...");
1158 cwmp_event_set_value(cwmp, INFORM_MREBOOT, 1, NULL, 0, 0, 0);
1159 cwmp_event_clear_active(cwmp);
1160
1161 // inorder to save file ok
1162
1163 sleep(20);
1164
1165 rc = restart_request(MODULE_ID_TR069);
1166
1167 if(0 != rc)
1168 {
1169 cwmp_log_info("restart request fail! rc:%d", rc);
1170 }
1171 }
1172 break;
1173
1174 case TASK_FACTORYRESET_TAG:
1175 {
1176 //begin factory reset system
1177 cwmp_log_debug("factory reset begin...");
1178
1179 cwmp_event_clear_active(cwmp);
1180
1181 rc = reset_request(MODULE_ID_TR069);
1182
1183 if(0 != rc)
1184 {
1185 cwmp_log_info("reset request fail! rc:%d", rc);
1186 }
1187
1188
1189 cwmp_log_debug("factory reset OK.....");
1190 }
1191 break;
1192
1193 default:
1194
1195 break;
1196
1197 }
1198 }
1199
1200 return ok;
1201}
1202
1203
1204
1205
1206
1207