blob: fe5ba994f8406db2f1799b4b0f70e6a0ffac64fc [file] [log] [blame]
xf.lia7d2a2b2025-02-10 01:44:22 -08001/*******************************************************************************
2* °æÈ¨ËùÓÐ (C)2016, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£
3*
4* ÎļþÃû³Æ: nvserver.c
5* Îļþ±êʶ: nvserver.c
6* ÄÚÈÝÕªÒª: nvºǫ́ӦÓÃʵÏÖÎļþ
7*
8* ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
9* ------------------------------------------------------------------------------
10* 2016/06/13 V1.0 Create ÁõÑÇÄÏ ´´½¨
11*
12*******************************************************************************/
lh9ed821d2023-04-07 01:36:19 -070013
xf.lia7d2a2b2025-02-10 01:44:22 -080014/*******************************************************************************
15* Í·Îļþ *
16*******************************************************************************/
lh9ed821d2023-04-07 01:36:19 -070017#include <unistd.h>
18#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <dirent.h>
22#include <string.h>
23#include <sys/file.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/ipc.h>
27#include <sys/msg.h>
xf.lia7d2a2b2025-02-10 01:44:22 -080028
lh9ed821d2023-04-07 01:36:19 -070029#include "nvserver.h"
30#include "nv_typedef.h"
31#include <message.h>
xf.lia7d2a2b2025-02-10 01:44:22 -080032
lh9ed821d2023-04-07 01:36:19 -070033#ifdef FOTA_AB
xf.li6c8fc1e2023-08-12 00:11:09 -070034#include "pub_flags.h"
35#include "flags_api.h"
lh9ed821d2023-04-07 01:36:19 -070036#endif
xf.lia7d2a2b2025-02-10 01:44:22 -080037
lh9ed821d2023-04-07 01:36:19 -070038#ifdef __cplusplus
xf.lia7d2a2b2025-02-10 01:44:22 -080039extern "C" {
lh9ed821d2023-04-07 01:36:19 -070040#endif
xf.lia7d2a2b2025-02-10 01:44:22 -080041
42/*******************************************************************************
43* ³£Á¿¶¨Òå *
44*******************************************************************************/
45
46/*******************************************************************************
47* ºê¶¨Òå *
48*******************************************************************************/
49
50/*******************************************************************************
51* Êý¾ÝÀàÐͶ¨Òå *
52*******************************************************************************/
53
54/*******************************************************************************
55* ¾Ö²¿º¯ÊýÉùÃ÷ *
56*******************************************************************************/
57static void nvConfig();
58static void nvInit();
59static int nvDirInit();
60static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd);
61static bool checkNvFs(char *file);
62static bool isCfgConfiged(char *configFile);
63static bool isNvConfiged(char *configFile);
64static uint getSum(const char *s, int len);
65static int loadFactroyParam(T_NV_NODE *list);
66static int restoreNvFs(char *dstFile, char *srcFile);
67static int loadNvFs(char *file);
68static int addConfigFile(char *nvFile, char *configFile);
69static int saveNvFs(char *nvName, char *nvFile);
70static int nvset(char *file, const char *key, const char *value, int saveFlag);
71static int nvget(char *file, char *key, char *value);
72static int nvunset(char *file, char *key);
73static int nvclear(char *file);
74static int nvreset(char *file);
75static int nvcommit(char *file);
76
77/*******************************************************************************
78* ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå *
79*******************************************************************************/
80
81/*******************************************************************************
82* È«¾Ö±äÁ¿¶¨Òå *
83*******************************************************************************/
84T_NV_NODE *nv_list;
85
86/*******************************************************************************
87* ¾Ö²¿º¯ÊýʵÏÖ *
88*******************************************************************************/
89
90/*******************************************************************************
91* È«¾Öº¯ÊýʵÏÖ *
92*******************************************************************************/
93
94/*******************************************************************************
95* ¹¦ÄÜÃèÊö: main
96* ²ÎÊý˵Ã÷:
97* (´«Èë²ÎÊý) void
98* (´«³ö²ÎÊý) void
99* ·µ »Ø Öµ: void
100* ÆäËü˵Ã÷: void
101*******************************************************************************/
102int nvserver_main(int argc, char *argv[])
103{
104 int msgId = 0;
105 T_NV_MSG_INFO rcvBuf;
106 T_NV_MSG_RESULT sndBuf;
107 struct msqid_ds msgInfo;
108
109 prctl(PR_SET_NAME, "nvserver", 0, 0, 0);
110
111 memset( &rcvBuf, 0, sizeof(rcvBuf));
112 memset( &sndBuf, 0, sizeof(sndBuf));
113 memset( &msgInfo, 0, sizeof(msgInfo));
114
115 msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600);
116 if(-1 == msgId)
117 {
118 printf("nvserver error: msgget msgId fail, errno = %d\n", errno);
119 return -1;
120 }
121
122 if(-1 != msgctl(msgId, IPC_STAT, &msgInfo))
123 {
124 msgInfo.msg_qbytes = 262144; // 256k
125 if(-1 == msgctl(msgId, IPC_SET, &msgInfo))
126 printf("nvserver error: msgctl msgId fail, errno = %d\n", errno);
127 }
128
129 if(nvDirInit() != 0)
130 {
131 printf("nvDirInit faile!\n");
132 return -1;
133 }
134
135 nvConfig();
136
137 nvInit();
138
139 // ÏûÏ¢½»»¥
140 while(1)
141 {
142 if(-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0))
143 {
144 printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno);
145 continue;
146 }
147
148 analyMsg(&rcvBuf, &sndBuf);
149
150 if(-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0))
151 {
152 printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno);
153 continue;
154 }
155 }
156
157 return (0);
158}
159
160/*******************************************************************************
161* ¹¦ÄÜÃèÊö: configdir
162* ²ÎÊý˵Ã÷:
163* (´«Èë²ÎÊý) dir
164* (´«³ö²ÎÊý) void
165* ·µ »Ø Öµ: void
166* ÆäËü˵Ã÷: void
167*******************************************************************************/
168static void configdir(char *dir)
169{
170 DIR *dp;
171 int ret;
172
173 struct dirent *entry;
174 struct stat statbuf;
175 if((dp = opendir(dir)) == NULL)
176 {
177 fprintf(stderr,"cannot open directory: %s\n", dir);
178 return;
179 }
180
181 chdir(dir);
182 while((entry = readdir(dp)) != NULL)
183 {
184 ret = lstat(entry->d_name,&statbuf);
185 if (ret < 0)
186 {
187 fprintf(stderr,"lstat error: %s\n", strerror(errno));
188 chdir("..");
189 closedir(dp);
190 return;
191 }
192 if(!S_ISDIR(statbuf.st_mode))
193 {
194
195 if(strcmp(".",entry->d_name) == 0 ||strcmp("..",entry->d_name) == 0)
196 continue;
197
198 if(!isNvConfiged(entry->d_name))
199 {
200 if(addConfigFile(entry->d_name, NULL) != RESULT_SUCCESS)
201 printf("nvserver error:config %s error!\n", entry->d_name);
202 }
203 }
204 }
205 chdir("..");
206 closedir(dp);
207}
208
209/*******************************************************************************
210* ¹¦ÄÜÃèÊö: nvConfig
211* ²ÎÊý˵Ã÷:
212* (´«Èë²ÎÊý) void
213* (´«³ö²ÎÊý) void
214* ·µ »Ø Öµ: void
215* ÆäËü˵Ã÷: void
216*******************************************************************************/
217static void nvConfig()
218{
219 char *val = NULL;
220 FILE *fp = NULL;
221 char buf[NV_MAX_CONFIG_LEN] = {0};
222
223 fp = fopen(NV_CONFIG_FILE, "ro");
224 if(!fp)
225 {
226 printf("nvserver error:open %s file fail errno = %d!\n", NV_CONFIG_FILE, errno);
227 return;
228 }
229
230 while (fgets(buf, NV_MAX_CONFIG_LEN, fp))
231 {
232 if (buf[0] == '\n' || buf[0] == '#')
233 continue;
234
235 val = strchr(buf, '=');
236 if (!val)
237 {
238 printf("nvserver error:%s file format error: str = %s!\n", NV_CONFIG_FILE, buf);
239 continue;
240 }
241
242 // buf=nvconfig val=nvfile
243 buf[strlen(buf) - 1] = '\0';
244 *val++ = '\0';
245
246 if(!isCfgConfiged(buf))
247 {
248 if(addConfigFile(val, buf) != RESULT_SUCCESS)
249 printf("nvserver error:config %s error!\n", buf);
250 }
251 }
252
253 fclose(fp);
254
255 configdir(NV_FS_MAIN_PATH);
256}
257
258/*******************************************************************************
259* ¹¦ÄÜÃèÊö: nvDirInit
260* ²ÎÊý˵Ã÷:
261* (´«Èë²ÎÊý) void
262* (´«³ö²ÎÊý) void
263* ·µ »Ø Öµ: void
264* ÆäËü˵Ã÷: void
265*******************************************************************************/
266static int nvDirInit()
267{
268 if(access(NV_FS_PATH, F_OK) != 0)
269 {
270 if(mkdir(NV_FS_PATH, 0755) != 0)
271 {
272 printf("nerver mkdir %s fali,errno=%d\n", NV_FS_PATH, errno);
273 return -1;
274 }
275
276 if(mkdir(NV_FS_MAIN_PATH, 0755) != 0)
277 {
278 printf("nerver mkdir %s fali,errno=%d\n", NV_FS_MAIN_PATH, errno);
279 return -1;
280 }
281
282 if(mkdir(NV_FS_BACKUP_PATH, 0755) != 0)
283 {
284 printf("nerver mkdir %s fali,errno=%d\n", NV_FS_BACKUP_PATH, errno);
285 return -1;
286 }
287 }
288 else
289 {
290 if(access(NV_FS_MAIN_PATH, F_OK) != 0)
291 {
292 if(mkdir(NV_FS_MAIN_PATH, 0755) != 0)
293 {
294 printf("nerver mkdir %s fali,errno=%d\n", NV_FS_MAIN_PATH, errno);
295 return -1;
296 }
297 }
298
299 if(access(NV_FS_BACKUP_PATH, F_OK) != 0)
300 {
301 if(mkdir(NV_FS_BACKUP_PATH, 0755) != 0)
302 {
303 printf("nerver mkdir %s fali,errno=%d\n", NV_FS_BACKUP_PATH, errno);
304 return -1;
305 }
306 }
307 }
308
309 return 0;
310}
311
312
313/*******************************************************************************
314* ¹¦ÄÜÃèÊö: nvInit
315* ²ÎÊý˵Ã÷:
316* (´«Èë²ÎÊý) void
317* (´«³ö²ÎÊý) void
318* ·µ »Ø Öµ: void
319* ÆäËü˵Ã÷: void
320*******************************************************************************/
321static void nvInit()
322{
323 T_NV_NODE *list = NULL;
324 char nvMainFile[NV_PATH_LEN] = {0};
325 char nvBackupFile[NV_PATH_LEN] = {0};
lh9ed821d2023-04-07 01:36:19 -0700326#ifdef FOTA_AB
xf.lia7d2a2b2025-02-10 01:44:22 -0800327 T_FLAGS_INFO flags_info = {0};
328 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700329#endif
xf.lia7d2a2b2025-02-10 01:44:22 -0800330
331 for(list = nv_list; list; list = list->next)
332 {
333 snprintf(nvMainFile, NV_PATH_LEN, "%s/%s", NV_FS_MAIN_PATH, list->nvFile);
334 snprintf(nvBackupFile, NV_PATH_LEN, "%s/%s", NV_FS_BACKUP_PATH, list->nvFile);
335
336 if(checkNvFs(nvMainFile))
337 {
338 if(!checkNvFs(nvBackupFile))
339 restoreNvFs(nvBackupFile, nvMainFile);
340 }
341 else if(checkNvFs(nvBackupFile))
342 {
343 restoreNvFs(nvMainFile, nvBackupFile);
344 }
345 else
346 {
347 loadFactroyParam(list);
348 nvcommit(list->nvFile);
349 continue;
350 }
351
352 loadNvFs(list->nvFile);
353 if(!strcmp(list->nvFile, NV_CFG) && get_update_status() == 2 ){
354 reloadFactroyParam(list);
355 delete_not_needed(list);
356 nvcommit(list->nvFile);
xf.li6c8fc1e2023-08-12 00:11:09 -0700357#ifdef FOTA_AB
xf.lia7d2a2b2025-02-10 01:44:22 -0800358 ret = flags_get(&flags_info);
359 flags_info.boot_fota_flag.fota_status = 0;
360 ret = flags_set(&flags_info);
xf.li6c8fc1e2023-08-12 00:11:09 -0700361#endif
xf.lia7d2a2b2025-02-10 01:44:22 -0800362 }
363 }
364}
365
366/*******************************************************************************
367* ¹¦ÄÜÃèÊö: hash
368* ²ÎÊý˵Ã÷:
369* (´«Èë²ÎÊý) void
370* (´«³ö²ÎÊý) void
371* ·µ »Ø Öµ: hash value
372* ÆäËü˵Ã÷: void
373*******************************************************************************/
374uint hash(const char *s)
375{
376 uint hash = 0;
377
378 while (*s)
379 {
380 hash = NV_HASH_MUL * hash + *s++;
381 }
382
383 return hash;
384}
385
386/*******************************************************************************
387* ¹¦ÄÜÃèÊö: loadFactroyParam
388* ²ÎÊý˵Ã÷:
389* (´«Èë²ÎÊý) list
390* (´«³ö²ÎÊý) void
391* ·µ »Ø Öµ: 0±íʾ³É¹¦ -1±íʾʧ°Ü
392* ÆäËü˵Ã÷: void
393*******************************************************************************/
394static int loadFactroyParam(T_NV_NODE *list)
395{
396 char *val = NULL;
397 FILE *fp = NULL;
398 T_NV_CONFIG *config = NULL;
399 char buf[NV_MAX_ITEM_LEN] = {0};
400
401 for(config = list->fileList; config; config = config->next)
402 {
403 fp = fopen(config->configFile, "ro");
404 if(!fp)
405 {
406 printf("nvserver error:open %s file fail errno = %d!\n", config->configFile, errno);
407 return RESULT_FILE_OPEN_FAIL;
408 }
409
410 while (fgets(buf, NV_MAX_ITEM_LEN, fp))
411 {
412 if (buf[0] == '\n' || buf[0] == '#')
413 continue;
414
415 val = strchr(buf, '=');
416 if (!val)
417 {
418 printf("nvserver error:%s file format error:string = %s\n", config->configFile, buf);
419 continue;
420 }
421
422 /*strip line break,the last line may not have a line break*/
423 if (buf[strlen(buf) - 1] == '\n')
424 buf[strlen(buf) - 1] = '\0';
425
426 *val++ = '\0';
427
428 nvset(list->nvFile, buf, val, 1);
429 }
430 printf("nvserver loadFactroyParam %s!\n", config->configFile);
431 fclose(fp);
432 }
433
434 return RESULT_SUCCESS;
435}
436
437/*******************************************************************************
438* ¹¦ÄÜÃèÊö: checkNvFs
439* ²ÎÊý˵Ã÷:
440* (´«Èë²ÎÊý) void
441* (´«³ö²ÎÊý) void
442* ·µ »Ø Öµ: bool
443* ÆäËü˵Ã÷: void
444*******************************************************************************/
445static bool checkNvFs(char *file)
446{
447 int len = 0;
448 int cnt = 0;
449 FILE * fp = NULL;
450 char * buf = NULL;
451 struct stat statbuff = {0};
452
453 if(stat(file, &statbuff) < 0)
454 return false;
455
456 len = statbuff.st_size;
457 if(len < NV_CHECK_SIZE)
458 return false;
459
460 fp = fopen(file, "ro");
461 if (!fp)
462 return false;
463
464 buf = (char *)malloc(len);
465 if(!buf)
466 {
467 fclose(fp);
468 return false;
469 }
470
471 cnt = 0;
472 while (cnt < len)
473 {
474 cnt = cnt + fread(buf + cnt, 1, len - cnt, fp);
475 if (ferror(fp))
476 {
477 clearerr(fp);
478 free(buf);
479 fclose(fp);
480 return false;
481 }
482 }
483 if (len != cnt)
484 {
485 free(buf);
486 fclose(fp);
487 return false;
488 }
489
490 if(getSum(buf, len - NV_CHECK_SIZE) + NV_FILE_FLAG != *(uint *)(buf + len - NV_CHECK_SIZE))
491 {
492 free(buf);
493 fclose(fp);
494 return false;
495 }
496
497 free(buf);
498 fclose(fp);
499
500 return true;
501}
502
503/*******************************************************************************
504* ¹¦ÄÜÃèÊö: copyfile
505* ²ÎÊý˵Ã÷:
506* (´«Èë²ÎÊý) to:Ä¿±êÎļþ
507* (´«Èë²ÎÊý) from:Ô´Îļþ
508* ·µ »Ø Öµ: 0±íʾ³É¹¦,¸ºÖµÊ§°Ü
509* ÆäËü˵Ã÷:
510*******************************************************************************/
511static int copyfile(const char *from, const char *to)
512{
513 int fd_to;
514 int fd_from;
515 char buf[4096];
516 ssize_t nread;
517 int ret = -1;
518
519 fd_from = open(from, O_RDONLY);
520 if (fd_from < 0)
521 return -2;
522
523 fd_to = open(to, O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0640);
524 if (fd_to < 0) {
525 ret = -3;
526 goto out_error;
527 }
528
529 while (1)
530 {
531 char *out_ptr;
532 ssize_t nwritten;
533
534 nread = read(fd_from, buf, sizeof(buf));
535 if (nread == 0)
536 {
537 break; /* read file done*/
538 }
539 else
540 {
541 if (nread < 0 )
542 {
543 if (errno == EINTR || errno == EAGAIN)
544 {
545 continue;
546 }
547 else
548 {
549 ret = -4;
550 goto out_error;
551 }
552 }
553 }
554
555 out_ptr = buf;
556 do
557 {
558 nwritten = write(fd_to, out_ptr, nread);
559 if (nwritten > 0)
560 {
561 nread -= nwritten;
562 out_ptr += nwritten;
563 }
564 else
565 {
566 if (nwritten < 0)
567 {
568 if (errno == EINTR || errno == EAGAIN)
569 {
570 continue;
571 }
572 else
573 {
574 ret = -5;
575 goto out_error;
576 }
577 }
578 }
579 } while (nread > 0);
580 }
581
582 ret = fsync(fd_to);
583 if (ret < 0) {
584 printf("Sync Failed:%s, file path:%s", strerror(errno), to);
585 goto out_error;
586 }
587
588 if (close(fd_to) < 0)
589 {
590 fd_to = -1;
591 ret = -6;
592 goto out_error;
593 }
594 close(fd_from);
595
596 /* Success! */
597 return 0;
598
599out_error:
600 printf("copyfile %s to %s error:%d\n", from, to, ret);
601 close(fd_from);
602 if (fd_to >= 0)
603 close(fd_to);
604
605 return ret;
606}
607
608/*******************************************************************************
609* ¹¦ÄÜÃèÊö: restoreNvFs
610* ²ÎÊý˵Ã÷:
611* (´«Èë²ÎÊý) dstFile:Ä¿±êÎļþ
612* (´«Èë²ÎÊý) srcFile:Ô´Îļþ
613* ·µ »Ø Öµ: 0±íʾ³É¹¦
614* ÆäËü˵Ã÷: void
615*******************************************************************************/
616static int restoreNvFs(char *dstFile, char *srcFile)
617{
618 if (copyfile(srcFile, dstFile) != 0)
619 return RESULT_FAIL;
620
621 return RESULT_SUCCESS;
622}
623
624/*******************************************************************************
625* ¹¦ÄÜÃèÊö: loadNvFs
626* ²ÎÊý˵Ã÷:
627* (´«Èë²ÎÊý) file:nvÎļþ
628* (´«³ö²ÎÊý) void
629* ·µ »Ø Öµ: 0±íʾ³É¹¦
630* ÆäËü˵Ã÷: void
631*******************************************************************************/
632static int loadNvFs(char *file)
633{
634 int len = 0;
635 int cnt = 0;
636 FILE *fp = NULL;
637 char *buf = NULL;
638 char *name = NULL;
639 char *value = NULL;
640 char *eq = NULL;
641
642 struct stat statbuff = {0};
643 char nvFile[NV_PATH_LEN] = {0};
644
645 sprintf(nvFile, "%s/%s", NV_FS_MAIN_PATH, file);
646
647 if(stat(nvFile, &statbuff) < 0)
648 return RESULT_FAIL;
649
650 len = statbuff.st_size;
651 if(NV_CHECK_SIZE > len)
652 return RESULT_FAIL;
653
654 fp = fopen(nvFile, "ro");
655 if (!fp)
656 return RESULT_FILE_OPEN_FAIL;
657
658 len = len - NV_CHECK_SIZE;
659 buf = (char *)malloc(len + 1);
660 if(!buf)
661 {
662 fclose(fp);
663 return RESULT_MALLOC_FAIL;
664 }
665 memset(buf, 0, len + 1);
666
667 cnt = 0;
668 while (cnt < len)
669 {
670 cnt = cnt + fread(buf + cnt, 1, len - cnt, fp);
671 if (ferror(fp))
672 {
673 clearerr(fp);
674 fclose(fp);
675 free(buf);
676 return RESULT_FILE_READ_FAIL;
677 }
678 }
679 if (cnt != len)
680 {
681 fclose(fp);
682 free(buf);
683 return RESULT_FILE_READ_FAIL;
684 }
685
686 buf[len] = '\0'; /* fix warning for coverity */
687 name = buf;
688 while(*name)
689 {
690 if (!(eq = strchr(name, '=')))
691 {
692 break;
693 }
694 *eq = '\0';
695 value = eq + 1;
696
697 nvset(file, name, value, 1);
698
699 *eq = '=';
700 name = value + strlen(value) + 1;
701 }
702
703 free(buf);
704 fclose(fp);
705
706 return RESULT_SUCCESS;
707}
708
709/*******************************************************************************
710* ¹¦ÄÜÃèÊö: analyMsg
711* ²ÎÊý˵Ã÷:
712* (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢
713* (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢
714* ·µ »Ø Öµ: 0±íʾ³É¹¦
715* ÆäËü˵Ã÷: void
716*******************************************************************************/
717static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd)
718{
719 switch(msgrecv->nvType)
720 {
721 case MSG_GET:
722 msgsnd->result = nvget(msgrecv->file, msgrecv->key, msgsnd->value);
723 break;
724 case MSG_SET:
725 msgsnd->result = nvset(msgrecv->file, msgrecv->key, msgrecv->value, msgrecv->saveflag);
726 break;
727 case MSG_UNSET:
728 msgsnd->result = nvunset(msgrecv->file, msgrecv->key);
729 break;
730 case MSG_CLEAR:
731 msgsnd->result = nvclear(msgrecv->file);
732 break;
733 case MSG_RESET:
734 msgsnd->result = nvreset(msgrecv->file);
735 break;
736 case MSG_SHOW:
737 msgsnd->result = saveNvFs(msgrecv->file, NV_FS_SHOW);;
738 break;
739 case MSG_COMMIT:
740 msgsnd->result = nvcommit(msgrecv->file);
741 break;
742 default:
743 break;
744 }
745
746 msgsnd->msgType = msgrecv->pid;
747 msgsnd->msgIndex = msgrecv->msgIndex;
748}
749
750/*******************************************************************************
751* ¹¦ÄÜÃèÊö: addConfigFile
752* ²ÎÊý˵Ã÷:
753* (´«Èë²ÎÊý) nvFile:nvÎļþ
754* (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþ
755* ·µ »Ø Öµ: 0±íʾ³É¹¦
756* ÆäËü˵Ã÷: void
757*******************************************************************************/
758static int addConfigFile(char *nvFile, char *configFile)
759{
760 T_NV_NODE *list = NULL;
761 T_NV_NODE *newList = NULL;
762 T_NV_CONFIG *newConfig = NULL;
763
764 if(!nvFile)
765 {
766 printf("nvserver error: param illegal!\n");
767 return RESULT_INVAL;
768 }
769
770 if(configFile)// nv has no config file
771 {
772 newConfig = (T_NV_CONFIG * )malloc(sizeof(T_NV_CONFIG));
773 if(!newConfig)
774 return RESULT_MALLOC_FAIL;
775
776 strncpy(newConfig->configFile, configFile, NV_PATH_LEN - 1);
777 newConfig->configFile[NV_PATH_LEN - 1] = '\0';
778 newConfig->next = NULL;
779 }
780
781 for(list = nv_list; list; list =list->next)
782 {
783 if(strcmp(list->nvFile, nvFile) == 0)
784 break;
785 }
786
787 if(!list) //can't fine the nv
788 {
789 newList = (T_NV_NODE *)malloc(sizeof(T_NV_NODE));
790 if(!newList)
791 {
792 if(newConfig)
793 free(newConfig);
794 return RESULT_MALLOC_FAIL;
795 }
796
797 newList->next = NULL;
798 strncpy(newList->nvFile, nvFile, NV_PATH_LEN - 1);
799 newList->nvFile[NV_PATH_LEN - 1] = '\0';
800 memset(newList->nvTable, 0, NV_HASH_LEN * 4);
801
802 newList->fileList = newConfig;
803
804 if(!nv_list)
805 nv_list = newList;
806 else
807 {
808 newList->next = nv_list->next;
809 nv_list->next = newList;
810 }
811 }
812 else if(!list->fileList) //can't fine the filelist
813 list->fileList = newConfig;
814 else //can't fine the file
815 {
816 if(newConfig == NULL)
817 return RESULT_FAIL;
818 newConfig->next = list->fileList->next;
819 list->fileList->next = newConfig;
820 }
821
822 return RESULT_SUCCESS;
823}
824
825/*******************************************************************************
826* ¹¦ÄÜÃèÊö: isConfiged
827* ²ÎÊý˵Ã÷:
828* (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþÃû
829* ·µ »Ø Öµ: void
830* ÆäËü˵Ã÷: void
831*******************************************************************************/
832static bool isCfgConfiged(char *configFile)
833{
834 T_NV_NODE *list = NULL;
835 T_NV_CONFIG *config = NULL;
836
837 for(list = nv_list; list; list = list->next)
838 {
839 for(config = list->fileList; config; config = config->next)
840 {
841 if(!strcmp(config->configFile, configFile))
842 return true;
843 }
844 }
845 return false;
846}
847
848/*******************************************************************************
849* ¹¦ÄÜÃèÊö: isNvConfiged
850* ²ÎÊý˵Ã÷:
851* (´«Èë²ÎÊý) configFile:ÅäÖÃÎļþÃû
852* ·µ »Ø Öµ: void
853* ÆäËü˵Ã÷: void
854*******************************************************************************/
855static bool isNvConfiged(char *nvFile)
856{
857 T_NV_NODE *list = NULL;
858
859 for(list = nv_list; list; list = list->next)
860 {
861 if(!strcmp(list->nvFile, nvFile))
862 return true;
863 }
864 return false;
865}
866
867/*******************************************************************************
868* ¹¦ÄÜÃèÊö: getSum
869* ²ÎÊý˵Ã÷:
870* (´«Èë²ÎÊý) s:×Ö·û´®»º³å
871* ·µ »Ø Öµ: sumÊýÖµ
872* ÆäËü˵Ã÷: void
873*******************************************************************************/
874static uint getSum(const char *s, int len)
875{
876 uint sum = 0;
877 char *data = (char*)s;
878
879 while (len-- > 0)
880 {
881 sum += (*data++);
882 }
883
884 return sum;
885}
886
887/*******************************************************************************
888* ¹¦ÄÜÃèÊö: saveNvFs
889* ²ÎÊý˵Ã÷:
890* (´«Èë²ÎÊý) nvName:nvÎļþÃû
891* (´«Èë²ÎÊý) nvFile:nv±£´æÎļþ
892* ·µ »Ø Öµ: 0±íʾ³É¹¦
893* ÆäËü˵Ã÷: void
894*******************************************************************************/
895static int saveNvFs(char *nvName, char *nvFile)
896{
897 int i = 0;
898 int sum = 0;
899 int bufSize = 0;
900 int itemSize = 0;
901 int ret = 0;
902 int fp = 0;
903 char *buf = NULL;
904 T_NV_NODE * list = NULL;
905 T_NV_ITEM * item = NULL;
906
907 for(list = nv_list; list; list = list->next)
908 {
909 if(strcmp(list->nvFile, nvName))
910 continue;
911
912 fp= open(nvFile, O_SYNC | O_RDWR | O_CREAT | O_TRUNC, 0640);
913 if(fp == -1)
914 {
915 printf("open %s fail,errno = %d\n", nvFile, errno);
916 return RESULT_FILE_OPEN_FAIL;
917 }
918
919 buf = (char *)malloc(NV_BLOCK_SIZE);
920 if(!buf)
921 {
922 close(fp);
923 return RESULT_MALLOC_FAIL;
924 }
925
926 for(i = 0; i < NV_HASH_LEN; i ++)
927 {
928 for(item = list->nvTable[i]; item; item = item->next)
929 {
930 if(strcmp(nvFile, NV_FS_SHOW) && !item->saveFlag)
931 continue;
932
933 itemSize = strlen(item->key) + strlen(item->value) + 2;
934 if(bufSize + itemSize > NV_BLOCK_SIZE)
935 {
936 if(write(fp, buf, bufSize) < 0)
937 {
938 printf("error %s %d: write fail errno = %d\n", __FILE__, __LINE__, errno);
939 close(fp);
940 free(buf);
941 return RESULT_FILE_WRITE_FAIL;
942 }
943 sum += getSum(buf, bufSize);
944 bufSize = 0;
945 }
946
947 sprintf(buf + bufSize, "%s=%s", item->key, item->value);
948 bufSize += itemSize;
949 }
950 }
951
952 // last block
953 if(bufSize != 0)
954 {
955 if(write(fp, buf, bufSize) < 0)
956 {
957 printf("nvserver error: write fail errno = %d\n", errno);
958 close(fp);
959 free(buf);
960 return RESULT_FILE_WRITE_FAIL;
961 }
962 sum += getSum(buf, bufSize);
963 }
964 sum += NV_FILE_FLAG;
965
966 // write sum
967 if(write(fp, &sum, NV_CHECK_SIZE) < 0)
968 {
969 printf("nvserver error: write fail errno = %d\n", errno);
970 close(fp);
971 free(buf);
972 return RESULT_FILE_WRITE_FAIL;
973 }
974
975 ret = fsync(fp);
976
977 free(buf);
978 close(fp);
979
980 if (ret < 0) {
981 printf("Sync Failed:%s, file path:%s", strerror(errno), nvFile);
982 return ret;
983 }
984
985 return RESULT_SUCCESS;
986 }
987
988 return RESULT_NO_FILE;
989}
990
991
992/*******************************************************************************
993* ¹¦ÄÜÃèÊö: nvget
994* ²ÎÊý˵Ã÷:
995* (´«Èë²ÎÊý) file:nvÎļþ
996* (´«Èë²ÎÊý) key:nvÏîÃû
997* (´«³ö²ÎÊý) value:nvÖµ
998* ·µ »Ø Öµ: 0±íʾ³É¹¦
999* ÆäËü˵Ã÷: void
1000*******************************************************************************/
1001static int nvget(char *file, char *key, char *value)
1002{
1003 int index = 0;
1004 T_NV_NODE *list = NULL;
1005 T_NV_ITEM *item = NULL;
1006
1007 for(list = nv_list; list; list = list->next)
1008 {
1009 if(strcmp(list->nvFile, file))
1010 continue;
1011
1012 index = hash(key) % NV_HASH_LEN;
1013 for(item = list->nvTable[index]; item; item = item->next)
1014 {
1015 if(strcmp(item->key, key))
1016 continue;
1017
1018 strncpy(value, item->value, NV_MAX_VAL_LEN - 1);
1019 value[NV_MAX_VAL_LEN - 1] = '\0';
1020 return RESULT_SUCCESS;
1021 }
1022 }
1023
1024 return RESULT_NO_ITEM;
1025}
1026
1027/*******************************************************************************
1028* ¹¦ÄÜÃèÊö: nvset
1029* ²ÎÊý˵Ã÷:
1030* (´«Èë²ÎÊý) file:nvÎļþ
1031* (´«Èë²ÎÊý) key:nvÏîÃû
1032* (´«Èë²ÎÊý) value:nvÖµ
1033* (´«Èë²ÎÊý) saveFlag:±£´æ±êÖ¾
1034* ·µ »Ø Öµ: 0±íʾ³É¹¦
1035* ÆäËü˵Ã÷: void
1036*******************************************************************************/
1037static int nvset(char *file, const char *key, const char *value, int saveFlag)
1038{
1039 int index = 0;
1040 int ret = 0;
1041 int key_buf_len = 0;
1042 int value_buf_len = 0;
1043 T_NV_NODE *list = NULL;
1044 T_NV_ITEM *item = NULL;
1045 T_NV_ITEM *newItem = NULL;
1046
1047 if (NULL == key || NULL == value)
1048 return RESULT_FAIL;
1049
1050 key_buf_len = strlen(key) + 1;
1051 value_buf_len = strlen(value) + 1;
1052
1053 for(list = nv_list; list; list = list->next)
1054 {
1055 if(strcmp(list->nvFile, file))
1056 continue;
1057
1058 index = hash(key) % NV_HASH_LEN;
1059 for(item = list->nvTable[index]; item; item = item->next)
1060 {
1061 // change item
1062 if(strcmp(item->key, key))
1063 continue;
1064
1065 if(saveFlag)
1066 item->saveFlag = saveFlag;
1067
1068 if(!strcmp(item->value, value))
1069 return RESULT_SUCCESS;
1070
1071 free(item->value);
1072 item->value = (char *)malloc(value_buf_len);
1073 if(!item->value)
1074 return RESULT_MALLOC_FAIL;
1075
1076 strncpy(item->value, value, value_buf_len - 1);
1077 item->value[value_buf_len - 1] = '\0';
1078
1079 return RESULT_SUCCESS;
1080 }
1081
1082 // add item
1083 newItem = (T_NV_ITEM *)malloc(sizeof(T_NV_ITEM ));
1084 if(!newItem)
1085 return RESULT_MALLOC_FAIL;
1086
1087 newItem->key = (char *)malloc(key_buf_len);
1088 if(!newItem->key)
1089 {
1090 free(newItem);
1091 return RESULT_MALLOC_FAIL;
1092 }
1093
1094 newItem->value = (char *)malloc(value_buf_len);
1095 if(!newItem->value)
1096 {
1097 free(newItem->key);
1098 free(newItem);
1099 return RESULT_MALLOC_FAIL;
1100 }
1101
1102 strncpy(newItem->key, key, key_buf_len - 1);
1103 newItem->key[key_buf_len - 1] = '\0';
1104 strncpy(newItem->value, value, value_buf_len - 1);
1105 newItem->value[value_buf_len - 1] = '\0';
1106
1107 newItem->next = NULL;
1108 newItem->saveFlag = saveFlag;
1109 newItem->update_flag=0;// 0: not update , 1: updated
1110
1111 if(!list->nvTable[index])
1112 list->nvTable[index] = newItem;
1113 else
1114 {
1115 newItem->next = list->nvTable[index]->next;
1116 list->nvTable[index]->next = newItem;
1117 }
1118
1119 return RESULT_SUCCESS;
1120 }
1121
1122 // add nv
1123 ret = addConfigFile(file, NULL);
1124 if(ret == RESULT_SUCCESS)
1125 return nvset(file, key, value, saveFlag);
1126 else
1127 return ret;
1128}
1129
1130/*******************************************************************************
1131* ¹¦ÄÜÃèÊö: nvunset
1132* ²ÎÊý˵Ã÷:
1133* (´«Èë²ÎÊý) file:nvÎļþ
1134* (´«Èë²ÎÊý) key:nvÏîÃû
1135* ·µ »Ø Öµ: 0±íʾ³É¹¦
1136* ÆäËü˵Ã÷: void
1137*******************************************************************************/
1138static int nvunset(char * file, char *key)
1139{
1140 int index = 0;
1141 T_NV_NODE *list = NULL;
1142 T_NV_ITEM *item = NULL;
1143 T_NV_ITEM *prev = NULL;
1144
1145 for(list = nv_list; list; list = list->next)
1146 {
1147 if(strcmp(list->nvFile, file))
1148 continue;
1149
1150 index = hash(key) % NV_HASH_LEN;
1151 for(item = list->nvTable[index]; item; prev = item, item = item->next)
1152 {
1153 if(strcmp(item->key, key))
1154 continue;
1155
1156 if(!prev)
1157 list->nvTable[index] = item->next;
1158 else
1159 prev->next = item->next;
1160
1161 free(item->key);
1162 free(item->value);
1163 free(item);
1164
1165 return RESULT_SUCCESS;
1166 }
1167 }
1168
1169 return RESULT_NO_ITEM;
1170}
1171
1172/*******************************************************************************
1173* ¹¦ÄÜÃèÊö: nvreset
1174* ²ÎÊý˵Ã÷:
1175* (´«Èë²ÎÊý) file:nvÎļþ
1176* ·µ »Ø Öµ: 0±íʾ³É¹¦
1177* ÆäËü˵Ã÷: ɾ³ýnvÎļþ
1178*******************************************************************************/
1179static int nvreset(char *file)
1180{
1181 int ret = 0;
1182 T_NV_NODE *list = NULL;
1183
1184 for(list = nv_list; list; list = list->next)
1185 {
1186 if(strcmp(list->nvFile, file))
1187 continue;
1188
1189 ret = nvclear(file);
1190 if(ret != RESULT_SUCCESS)
1191 return ret;
1192
1193 if(loadFactroyParam(list) != RESULT_SUCCESS)
1194 return RESULT_FAIL;
1195
1196 return nvcommit(file);
1197 }
1198
1199 return RESULT_NO_FILE;
1200}
1201
1202/*******************************************************************************
1203* ¹¦ÄÜÃèÊö: nvclear
1204* ²ÎÊý˵Ã÷:
1205* (´«Èë²ÎÊý) file:nvÎļþ
1206* ·µ »Ø Öµ: 0±íʾ³É¹¦
1207* ÆäËü˵Ã÷: ²Á³ýÄÚ´ænv
1208*******************************************************************************/
1209static int nvclear(char *file)
1210{
1211 int i = 0;
1212 T_NV_NODE *list = NULL;
1213 T_NV_ITEM *cur = NULL;
1214 T_NV_ITEM *item = NULL;
1215
1216 for(list = nv_list; list; list = list->next)
1217 {
1218 if(strcmp(list->nvFile, file))
1219 continue;
1220
1221 for(i = 0; i < NV_HASH_LEN; i ++)
1222 {
1223 for(item = list->nvTable[i]; item; )
1224 {
1225 cur = item;
1226 item = item->next;
1227
1228 free(cur->key);
1229 free(cur->value);
1230 free(cur);
1231 }
1232 list->nvTable[i] = NULL;
1233 }
1234
1235 return RESULT_SUCCESS;
1236 }
1237
1238 return RESULT_NO_FILE;
1239}
1240
1241/*******************************************************************************
1242* ¹¦ÄÜÃèÊö: nvcommit
1243* ²ÎÊý˵Ã÷:
1244* (´«Èë²ÎÊý) file:nvÎļþ
1245* ·µ »Ø Öµ: RESULT_SUCCESS±íʾ³É¹¦
1246* ÆäËü˵Ã÷: void
1247*******************************************************************************/
1248static int nvcommit(char *file)
1249{
1250 int ret = 0;
1251 char nvMainFile[NV_PATH_LEN] = {0};
1252 char nvBackupFile[NV_PATH_LEN] = {0};
1253
1254 sprintf(nvMainFile, "%s/%s", NV_FS_MAIN_PATH, file);
1255 sprintf(nvBackupFile, "%s/%s", NV_FS_BACKUP_PATH, file);
1256
1257 ret = saveNvFs(file, nvMainFile);
1258 if(ret != RESULT_SUCCESS)
1259 return ret;
1260
1261 return restoreNvFs(nvBackupFile, nvMainFile);
1262}
1263
lh9ed821d2023-04-07 01:36:19 -07001264#ifdef __cplusplus
1265}
1266#endif
1267