blob: 58a9dce7e126ee6419676ef667690eccba39370d [file] [log] [blame]
xf.li771eb062025-02-09 23:05:11 -08001/**************************************************************************
2*
3* Copyright (c) 2013 ZTE Corporation.
4*
5***************************************************************************
6* Ä£ ¿é Ãû : pb
7* ÎÄ ¼þ Ãû : pb_main.c
8* Ïà¹ØÎļþ :
9* ʵÏÖ¹¦ÄÜ : PBÏûÏ¢Á÷³Ì¿ØÖÆ´¦Àí
10* ×÷ Õß :
11* °æ ±¾ : V1.0
12* Íê³ÉÈÕÆÚ : 2017.3.13
13* ÆäËü˵Ã÷ :
14**************************************************************************/
15
16/**************************************************************************
17* #includeÇø
18**************************************************************************/
xf.libfc6e712025-02-07 01:54:34 -080019#include <errno.h>
20#include <sys/msg.h>
21#include <semaphore.h>
xf.li771eb062025-02-09 23:05:11 -080022
xf.libfc6e712025-02-07 01:54:34 -080023#include "pb_com.h"
xf.li771eb062025-02-09 23:05:11 -080024/**************************************************************************
25* ³£Á¿¶¨ÒåÇø
26**************************************************************************/
27//#define MODULE_ID_PB_LOCAL 0x2002
28/**************************************************************************
29* Êý¾Ý½á¹¹¶¨ÒåÇø
30**************************************************************************/
31typedef VOID (*pAtWeb_PbMsgProc)(UINT8 *pDatabuf);
32
33typedef struct
34{
35 UINT32 msg_id;
36 VOID (*func_ptr)(UINT8 *pDatabuf);
37 BOOL need_block;//ÊÇ·ñÐèÒª×èÈû£¬Èç¹û×èÈû£¬ÐèÒª½«ÏûϢתµ½ÄÚ²¿Ï̴߳¦Àí
38}T_zPbHandleTable;
39
40
41/**************************************************************************
42* staticº¯ÊýÉùÃ÷Çø
43**************************************************************************/
44
45VOID atWeb_AddOneContact(UINT8 *pDatabuf);
46VOID atWeb_DelOneContact(UINT8 *pDatabuf);
47VOID atWeb_DelMultiContact(UINT8 *pDatabuf);
48VOID atWeb_DelAllContact(UINT8 *pDatabuf);
49
50/**************************************************************************
51* È«¾Ö±äÁ¿¶¨ÒåÇø
52**************************************************************************/
53T_zPb_optRsp g_PbOptRsp = {0};
54sem_t g_Pb_sem_id = {0};//ÓÃÓÚÄÚ²¿µÈ´ý
55int g_zPb_MsqId = -1;
56int g_zPb_LocalMsqId = -1;
57
58/**************************************************************************
59* º¯ÊýʵÏÖÇø
60**************************************************************************/
61
62// pDatabufΪpstMsg->aucDataBuf
63VOID atWeb_AddOneContact(UINT8 *pDatabuf)
64{
65 T_zPb_WebContact* webPbContact = NULL;
66
67 if(pDatabuf == NULL)
68 {
69 printf("[pb], atWeb_AddOneContact para is null\n");
70 return;
71 }
72 webPbContact = (T_zPb_WebContact*)pDatabuf;
73 atWeb_AddOnePb(webPbContact, g_Pb_sem_id);
74}
75
76VOID atWeb_DelOneContact(UINT8 *pDatabuf)
77{
78 T_zPb_DelInfo *delRecord = NULL;
79
80 if(pDatabuf == NULL)
81 {
82 printf("[pb], atWeb_DelOneContact para is null\n");
83 return;
84 }
85 delRecord = (T_zPb_DelInfo*)pDatabuf;
86 atWeb_DelOnepb(delRecord, g_Pb_sem_id);
87}
88
89VOID atWeb_DelMultiContact(UINT8 *pDatabuf)
90{
91 T_zPb_DelInfo *delRecord = NULL;
92
93 if(pDatabuf == NULL)
94 {
95 printf("[pb], atWeb_DelOneContact para is null\n");
96 return;
97 }
98 delRecord = (T_zPb_DelInfo*)pDatabuf;
99 atWeb_DelMultPb(delRecord, g_Pb_sem_id);
100}
101VOID atWeb_DelAllContact(UINT8 *pDatabuf)
102{
103 T_zPb_DelInfo *delRecord = NULL;
104
105 if(pDatabuf == NULL)
106 {
107 printf("[pb], atWeb_DelOneContact para is null\n");
108 return;
109 }
110 delRecord = (T_zPb_DelInfo*)pDatabuf;
111 atWeb_DelAllpb(delRecord, g_Pb_sem_id);
112}
113
114/*VOID zPb_RecvPbInitRst(UINT8 *pDatabuf)
115{
116 memcpy(&g_PbOptRsp, pDatabuf, sizeof(T_zPb_optRsp));
117
118 if(g_PbOptRsp.result == 1)
119 {
120 zPb_initAtOk();
121 }
122 else
123 {
124 zPb_initAtErr();
125 }
126}*/
127
128VOID zPb_RecvScpbrInd(UINT8 *pDatabuf)
129{
130 T_zPb_AtScpbrTestRes* scpbsInd= NULL;
131
132 if(pDatabuf == NULL)
133 {
134 return;
135 }
136
137 scpbsInd = (T_zPb_AtScpbrTestRes*)pDatabuf;
138 atPb_ScpbrTestRsp(scpbsInd);
139}
140
141VOID zPb_RecvScpbrReadInd(UINT8 *pDatabuf)
142{
143 T_zPb_ScpbrSetRes* scpbsReadInd= NULL;
144
145 if(pDatabuf == NULL)
146 {
147 return;
148 }
149
150 scpbsReadInd = (T_zPb_ScpbrSetRes*)pDatabuf;
151 atPb_ScpbrSetRsp(scpbsReadInd);
152}
153
154VOID zPb_RecvCpmsInd(UINT8 *pDatabuf)
155{
156 T_zPb_AtCpbsReadRes* cpbsInd = NULL;
157
158 if(pDatabuf == NULL)
159 {
160 return;
161 }
162
163 cpbsInd = (T_zPb_AtCpbsReadRes*)pDatabuf;
164 atPb_RecvCpbsReadRsp(cpbsInd);
165}
166
167VOID zPb_RecvZpbicInd(UINT8 *pDatabuf)
168{
169 T_zAt_ZpbicRes *ptPara = ZUFI_NULL;
170 if(pDatabuf == NULL)
171 {
172 return;
173 }
174 ptPara = (T_zAt_ZpbicRes*)(pDatabuf);
175 printf( "zPb_RecvZpbicInd para,result:%d, type:%d\n", ptPara->result, ptPara->opertype);
176 if((1 == ptPara->result) && (1 == ptPara->opertype))
177 {
178 //ÅжÏNV,added by wenqin 2016-5-18
179 CHAR needPb[50]={0};
180 sc_cfg_get(NV_NEED_SUPPORT_PB,needPb,sizeof(needPb));
181 if(0 != strcmp(needPb,"no"))
182 {
183 ipc_send_message(MODULE_ID_PB, MODULE_ID_AT_CTL, MSG_CMD_PBINIT_REQ, 0, NULL, 0);
184 }
185 }
186}
187
188VOID zPb_RecvZuslotInd(UINT8 *pDatabuf)
189{
190 T_zAt_ZuslotRes *ptPara = ZUFI_NULL;
191 if(pDatabuf == NULL)
192 {
193 return;
194 }
195 ptPara = (T_zAt_ZuslotRes*)(pDatabuf);
196 printf( "zPb_RecvZpbicInd para,result:%d, type:%d\n", ptPara->slot, ptPara->slot_state);
197 if(ptPara->slot_state == 0)
198 {
199 //ÅжÏNV,added by wenqin 2016-5-18
200 CHAR needPb[50]={0};
201 sc_cfg_get(NV_NEED_SUPPORT_PB,needPb,sizeof(needPb));
202 if(0 != strcmp(needPb,"no"))
203 {
204 atPb_CfgPbNvInit();
205 atPb_DelAllRecsSimDb();
206 }
207 }
208}
209
210VOID zPb_RecvPbInitRst(UINT8 *pDatabuf)
211{
212 int pbReadRet = -1;
213 memcpy(&g_PbOptRsp, pDatabuf, sizeof(T_zPb_optRsp));
214
215 if(g_PbOptRsp.result == -1)
216 {
217 atPb_IintPbErr(NULL);
218 return;
219 }
220 //Íê³ÉÃûƬ¼ÐÊý¾Ý¶ÁÈ¡
221 pbReadRet = atPb_SendScpbrSet_repeat(g_Pb_sem_id);
222 printf("zPb_RecvPbInitRst result:%d\n", pbReadRet);
223 sc_cfg_set(ZPB_NV_INIT, ZPB_OPERATE_SUC);
224}
225
226void zPbLocalHandleWebMsg(MSG_BUF *ptMsgBuf)
227{
228 assert(ptMsgBuf != NULL);
229 printf( "Pb recv web msg cmd:%d\n", ptMsgBuf->usMsgCmd);
230 switch(ptMsgBuf->usMsgCmd)
231 {
232 case MSG_CMD_WRITE_PB:
233 atWeb_AddOneContact(ptMsgBuf->aucDataBuf);
234 break;
235 case MSG_CMD_DEL_A_PB:
236 atWeb_DelOneContact(ptMsgBuf->aucDataBuf);
237 break;
238 case MSG_CMD_DEL_MUTI_PB:
239 atWeb_DelMultiContact(ptMsgBuf->aucDataBuf);
240 break;
241 case MSG_CMD_DEL_ALL_PB:
242 atWeb_DelAllContact(ptMsgBuf->aucDataBuf);
243 break;
244 default:
245 break;
246
247 }
248}
249
250void zPbHandleWebMsg(MSG_BUF *ptMsgBuf)
251{
252 assert(ptMsgBuf != NULL);
253 printf( "Pb recv web msg cmd:%d\n", ptMsgBuf->usMsgCmd);
254 switch(ptMsgBuf->usMsgCmd)
255 {
256 case MSG_CMD_WRITE_PB:
257 case MSG_CMD_DEL_A_PB:
258 case MSG_CMD_DEL_MUTI_PB:
259 case MSG_CMD_DEL_ALL_PB:
260 ipc_send_message(MODULE_ID_PB, MODULE_ID_PB_LOCAL, ptMsgBuf->usMsgCmd, ptMsgBuf->usDataLen, (unsigned char *)ptMsgBuf->aucDataBuf, 0);
261 break;
262 default:
263 break;
264
265 }
266}
267
268UINT8 zPbMsgCreat(VOID)
269{
270 g_zPb_MsqId = msgget(MODULE_ID_PB, IPC_CREAT | 0600);
271 if(g_zPb_MsqId == -1)
272 {
273 return ZUFI_FAIL;
274 }
275 g_zPb_LocalMsqId = msgget(MODULE_ID_PB_LOCAL, IPC_CREAT | 0600);
276 if(g_zPb_LocalMsqId == -1)
277 {
278 return ZUFI_FAIL;
279 }
280 sem_init(&g_Pb_sem_id, 0 , 0);
281
282 return ZUFI_SUCC;
283}
284
285/*³õʼ»¯pbmµÄAP ²àµç»°±¾×¼±¸³É¹¦£¬·ñÔòÎÞ¿¨Ê±webuiתȦ£¬Ê±ÐòÉϲ»±£ÏÕ£¬ËùÒÔÔÚat_ctlÒ²ÔÚÉèÁËÒ»±é£¬×îºÃÓÃÏûÏ¢*/
286void detect_modem_state(void)
287{
288 CHAR state[50] = {0};
289
290 sc_cfg_get("modem_main_state", state, sizeof(state));
291 if(0 == strcmp(state, "modem_sim_undetected") || 0 == strcmp(state, "modem_sim_destroy"))
292 {
293 sc_cfg_set("pbm_init_flag","0");
294 }
295}
296
297void zPbLocalHandleAtctlMsg(MSG_BUF *ptMsgBuf)
298{
299 assert(ptMsgBuf != NULL);
300 printf( "Pb local recv msg cmd:%d\n", ptMsgBuf->usMsgCmd);
301 switch(ptMsgBuf->usMsgCmd)
302 {
303 case MSG_CMD_PBINIT_RSP:
304 zPb_RecvPbInitRst(ptMsgBuf->aucDataBuf);
305 break;
306 default:
307 break;
308
309 }
310}
311
312VOID zPbHandleAtctlMsg(MSG_BUF *ptMsgBuf)
313{
314 assert(ptMsgBuf != NULL);
315 printf( "Pb recv msg cmd:%d\n", ptMsgBuf->usMsgCmd);
316 switch(ptMsgBuf->usMsgCmd)
317 {
318 case MSG_CMD_DELETE_PB_RSP:
319 case MSG_CMD_ADD_MODIFY_PB_RSP:
320 case MSG_CMD_READ_PB_RSP:
321 {
322 memcpy(&g_PbOptRsp, ptMsgBuf->aucDataBuf, sizeof(T_zPb_optRsp));
323 sem_post(&g_Pb_sem_id);
324 }
325 break;
326 case MSG_CMD_CPBS_IND:
327 zPb_RecvCpmsInd(ptMsgBuf->aucDataBuf);
328 break;
329 case MSG_CMD_SCPBR_IND:
330 zPb_RecvScpbrInd(ptMsgBuf->aucDataBuf);
331 break;
332 case MSG_CMD_SCPBR_READ_IND:
333 zPb_RecvScpbrReadInd(ptMsgBuf->aucDataBuf);
334 break;
335 case MSG_CMD_ZPBIC_IND:
336 zPb_RecvZpbicInd(ptMsgBuf->aucDataBuf);
337 break;
338 case MSG_CMD_ZUSLOT_IND:
339 zPb_RecvZuslotInd(ptMsgBuf->aucDataBuf);
340 break;
341 case MSG_CMD_PBINIT_RSP:
342 ipc_send_message(MODULE_ID_PB, MODULE_ID_PB_LOCAL, ptMsgBuf->usMsgCmd, ptMsgBuf->usDataLen, (unsigned char *)ptMsgBuf->aucDataBuf,0);
343 break;
344
345 default:
346 break;
347
348 }
349}
350
351VOID zPbHandleResetToFactory()
352{
353 CHAR clearPb[50] = {0};
354
355 sc_cfg_get(NV_CLEAR_PB_WHEN_RESTORE,clearPb,sizeof(clearPb));
356
357 printf("atWeb_RestoreFactorySetting entered! \n");
358 printf("clear_pb_when_restore=%s \n", clearPb);
359 if(strcmp(clearPb, "yes") == 0)
360 {
361 atPb_DropDb();
362 }
363 ipc_send_message(MODULE_ID_PB, MODULE_ID_MAIN_CTRL, MSG_CMD_RESET_RSP, 0, NULL,0);
364}
365
366void zPbHandleMainCtrlMsg(MSG_BUF *ptMsgBuf)
367{
368 assert(ptMsgBuf != NULL);
369 printf( "Pb recv main ctrl msg cmd:%d\n", ptMsgBuf->usMsgCmd);
370 switch(ptMsgBuf->usMsgCmd)
371 {
372 case MSG_CMD_RESET_NOTIFY:
373 zPbHandleResetToFactory(ptMsgBuf->aucDataBuf);
374 break;
375 default:
376 break;
377
378 }
379}
380
381
382void pb_msg_thread_proc(void* arg)
383{
384 int iRet = 0;
385 MSG_BUF stMsg = {0};
386 int msgSize = sizeof(MSG_BUF) - sizeof(SINT32);
387 int queueId = *((int*)arg);
388 prctl(PR_SET_NAME, "pb_local", 0, 0, 0);
389 while(1)
390 {
391 iRet = 0;
392 memset(&stMsg, 0x00, sizeof(MSG_BUF));
393 iRet = msgrcv(queueId, &stMsg, msgSize, 0, 0);
394printf("pb_msg_thread_proc:%x,%x MODULE_ID_AT_CTL=%x\n",stMsg.src_id,stMsg.usMsgCmd, MODULE_ID_AT_CTL);
395 if (iRet >= 0)
396 {
397 switch (stMsg.src_id)
398 {
399 case MODULE_ID_WEB_CGI:
400 {
401 zPbHandleWebMsg(&stMsg);
402 break;
403 }
404 case MODULE_ID_AT_CTL:
405 {
406 zPbHandleAtctlMsg(&stMsg);
407 break;
408 }
409 case MODULE_ID_PB://½ö±¾µØ¶ÓÁд¦Àíº¯Êý´¦Àí
410 {
411 zPbLocalHandleWebMsg(&stMsg);
412 zPbLocalHandleAtctlMsg(&stMsg);
413 break;
414 }
415 case MODULE_ID_MAIN_CTRL:
416 {
417 zPbHandleMainCtrlMsg(&stMsg);
418 break;
419 }
420 default:
421 {
422 break;
423 }
424 }
425 }
426 else
427 {
428 printf("[pb] errno = %d, errmsg = %s\n", errno,strerror(errno));
429 }
430 }
431}
432
433
434int phonebook_main(int argc, char* argv[])
435{
436 pthread_t recv_thread_tid = 0;
437 MSG_BUF msgBuf = {0};
438 CHAR needPb[50] = {0};
439 prctl(PR_SET_NAME, "pb_main", 0, 0, 0);
440
441 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
442 loglevel_init();
443
444 //ÅжÏNV,added by wenqin 2016-5-18
445 sc_cfg_get(NV_NEED_SUPPORT_PB, needPb, sizeof(needPb));
446 if(0 != strcmp(needPb, "no"))
447 {
xf.libfc6e712025-02-07 01:54:34 -0800448#ifdef WEBS_SECURITY
xf.li771eb062025-02-09 23:05:11 -0800449 if(access(ZPB_DB_PATH, F_OK) != 0)
450 {
451 if(access(ZPB_TMP_PATH, F_OK) == 0)
452 {
453 if(remove(ZPB_SEC_PATH) != 0)
454 {
455 slog(PB_PRINT,SLOG_ERR,"remove ZPB_SEC_PATH fail");
456 }
457 if(rename(ZPB_TMP_PATH, ZPB_SEC_PATH) != 0)
458 {
459 slog(PB_PRINT,SLOG_ERR,"rename ZPB_TMP_PATH fail");
460 }
461 }
462 if(access(ZPB_SEC_PATH, F_OK) == 0)
463 {
464 char rnum_buf[24] = {0};
465 char cmd[128] = {0};
466
467 sc_cfg_get("rnum_at", rnum_buf, sizeof(rnum_buf));
468 snprintf(cmd, sizeof(cmd), "/bin/openssl enc -d -aes256 -salt -in %s -out %s -pass pass:%s", ZPB_SEC_PATH, ZPB_DB_PATH, rnum_buf);
469 zxic_system(cmd);
470 }
471 }
xf.libfc6e712025-02-07 01:54:34 -0800472#endif
xf.li771eb062025-02-09 23:05:11 -0800473 zPbMsgCreat();
474 atPb_Init();
475 }
476 else
477 {
478 return -1;
479 }
480
481 printf("Pb app init finished, will to receive msg, msgid:%d\n", g_zPb_MsqId);
482 //¸ÃÏß³ÌÓÃÓÚ´¦ÀíÄÚ²¿×èÈûµÄÏûÏ¢£¬¶ÔÓ¦ÏûÏ¢µÄsourceIdÊÇMODULE_ID_SMS
483 if(pthread_create(&recv_thread_tid, NULL, pb_msg_thread_proc, (void*)(&g_zPb_LocalMsqId)) == -1)
484 {
485 assert(0);
486 }
487
488 detect_modem_state();
489
490 //Ö÷Ïß³ÌÓÃÓÚ´¦ÀíÍⲿ½ÓÊÕÏûÏ¢£¬¶ÔÓ¦ÏûÏ¢µÄsourceId·ÇMODULE_ID_SMS
491 pb_msg_thread_proc(&g_zPb_MsqId);
492 return 0;
493}
494