blob: c2335a9ee34db7db677b2f318132d0c1e5382b16 [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>
32#include <fota_common.h>
xf.lia7d2a2b2025-02-10 01:44:22 -080033
lh9ed821d2023-04-07 01:36:19 -070034#ifdef FOTA_AB
xf.li6c8fc1e2023-08-12 00:11:09 -070035#include "pub_flags.h"
36#include "flags_api.h"
lh9ed821d2023-04-07 01:36:19 -070037#endif
xf.lia7d2a2b2025-02-10 01:44:22 -080038
lh9ed821d2023-04-07 01:36:19 -070039#ifdef __cplusplus
xf.lia7d2a2b2025-02-10 01:44:22 -080040extern "C" {
lh9ed821d2023-04-07 01:36:19 -070041#endif
xf.lia7d2a2b2025-02-10 01:44:22 -080042
43/*******************************************************************************
44* ³£Á¿¶¨Òå *
45*******************************************************************************/
46
47/*******************************************************************************
48* ºê¶¨Òå *
49*******************************************************************************/
50
51/*******************************************************************************
52* Êý¾ÝÀàÐͶ¨Òå *
53*******************************************************************************/
54
55/*******************************************************************************
56* ¾Ö²¿º¯ÊýÉùÃ÷ *
57*******************************************************************************/
58
59/*******************************************************************************
60* ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå *
61*******************************************************************************/
62
63/*******************************************************************************
64* È«¾Ö±äÁ¿¶¨Òå *
65*******************************************************************************/
66extern T_NV_NODE *nv_list;
67
68/*******************************************************************************
69* ¾Ö²¿º¯ÊýʵÏÖ *
70*******************************************************************************/
71
72/*******************************************************************************
73* È«¾Öº¯ÊýʵÏÖ *
74*******************************************************************************/
75//#define FOTA_UPDATE_STATUS_FILE "/cache/zte_fota/update_status"
lh9ed821d2023-04-07 01:36:19 -070076#ifdef FOTA_AB
xf.lia7d2a2b2025-02-10 01:44:22 -080077int get_update_status (void)
78{
79 T_FLAGS_INFO flags_info = {0};
80 unsigned int status = 0;
81 int ret = 0;
82 ret = flags_get(&flags_info);
83 status = flags_info.boot_fota_flag.fota_status;
84
85 if (status == 1)
86 return 2;
87 else
88 return 0;
89}
lh9ed821d2023-04-07 01:36:19 -070090#else
xf.lia7d2a2b2025-02-10 01:44:22 -080091int get_update_status (void)
92{
93 int update_status;
94 FILE *fd = 0;
95 int ret;
96 char* filename = NULL;
97
98 if(access(FOTA_UPDATE_STATUS_FILE_OLD, R_OK) == 0){
99 filename = FOTA_UPDATE_STATUS_FILE_OLD;
100 }else{
101 filename = FOTA_UPDATE_STATUS_FILE;
102 }
103
104 printf("get_update_status, read_update_status from %s\n", filename);
105 fd = fopen (filename, "rb+");
106 if (fd == NULL) {
107 printf ("[nvserver]update_status open error:%s\n", strerror (errno));
108 goto error0;
109 }
110
111 ret = fscanf (fd, "%d", (int *) &update_status);
112 if (ret < 0) {
113 printf ("get info from file error:%s\n", strerror (errno));
114 fclose (fd);
115 goto error0;
116 }
117 printf ("update_status=%d\n", update_status);
118 fclose (fd);
119
120 return update_status;
121
122error0:
123 return -1;
124}
lh9ed821d2023-04-07 01:36:19 -0700125#endif
xf.lia7d2a2b2025-02-10 01:44:22 -0800126
127int nvupdate (char *nv_file, char *config_file, const char *key, const char *value, int saveFlag)
128{
129 int index = 0;
130 int key_buf_len = 0;
131 int value_buf_len = 0;
132 T_NV_NODE *list = NULL;
133 T_NV_ITEM *item = NULL;
134 T_NV_ITEM *newItem = NULL;
135
136 if (NULL == key || NULL == value)
137 return RESULT_FAIL;
138
139 printf("nvserver nvupdate nv_file:%s key:%s value:%s\n", nv_file, key, value);
140
141 key_buf_len = strlen(key) + 1;
142 value_buf_len = strlen(value) + 1;
143
144 for (list = nv_list; list; list = list->next) {
145 if (strcmp (list->nvFile, nv_file))
146 continue;
147
148 index = hash (key) % NV_HASH_LEN;
149 for (item = list->nvTable[index]; item; item = item->next) {
150 // change item
151 if (strcmp (item->key, key))
152 continue;
153
154 if (saveFlag)
155 item->saveFlag = saveFlag;
156 //if value is the same , or the key is in default_parameter_user , though the value change ,but do not update it
157 //just update it if the key is in default_parameter_sys
158 if (!strcmp (item->value, value))
159 {
160 /* value equal when sys or user */
161 item->update_flag = 1; // indicate the value is updated , or it is the newest
162 printf("nvserver nvupdate sameskip:item->key:%s item->value:%s value:%s config_file:%s\n", item->key,
163 item->value, value, config_file);
164 return RESULT_SUCCESS;
165 }
166 if (strstr(config_file, "user"))
167 {
168 if (1 == item->update_flag)
169 {
170 /* second time:key in sys_user or user_user */
171 printf("nvserver nvupdate second change:item->key:%s item->value:%s value:%s config_file:%s\n", item->key,
172 item->value, value, config_file);
173 }
174 else
175 {
176 item->update_flag = 1;
177 printf("nvserver nvupdate userskip:item->key:%s item->value1:%s value:%s config_file:%s\n", item->key,
178 item->value, value, config_file);
179 return RESULT_SUCCESS;
180 }
181 }
182
183 printf ("nvserver key=%s change value:%s to value=%s \n", item->key, item->value, value);
184 free (item->value);
185 item->value = (char *) malloc (value_buf_len);
186 if (!item->value)
187 return RESULT_MALLOC_FAIL;
188
189 strncpy (item->value, value, value_buf_len - 1);
190 item->value[value_buf_len - 1] = '\0';
191
192 item->update_flag = 1; // indicate the value is updated , or it is the newest
193 return RESULT_SUCCESS;
194 }
195
196 // add item
197 newItem = (T_NV_ITEM *) malloc (sizeof (T_NV_ITEM));
198 if (!newItem) {
199 printf ("nvserver RESULT_MALLOC_FAIL1 \n");
200 return RESULT_MALLOC_FAIL;
201 }
202 newItem->key = (char *) malloc (strlen (key) + 1);
203 if (!newItem->key) {
204 free (newItem);
205 printf ("nvserver RESULT_MALLOC_FAIL2\n");
206
207 return RESULT_MALLOC_FAIL;
208 }
209
210 newItem->value = (char *) malloc (value_buf_len);
211 if (!newItem->value) {
212 free (newItem->key);
213 free (newItem);
214 printf ("nvserver RESULT_MALLOC_FAIL3 \n");
215 return RESULT_MALLOC_FAIL;
216 }
217
218 strncpy (newItem->key, key, key_buf_len - 1);
219 newItem->key[key_buf_len - 1] = '\0';
220
221 strncpy (newItem->value, value, value_buf_len - 1);
222 newItem->value[value_buf_len - 1] = '\0';
223
224 newItem->next = NULL;
225 newItem->saveFlag = saveFlag;
226 newItem->update_flag = 1; // indicate the value is updated , or it is the newest
227 printf ("nvserver add key=%s, value=%s \n", newItem->key, newItem->value);
228
229
230 if (!list->nvTable[index])
231 list->nvTable[index] = newItem;
232 else {
233 newItem->next = list->nvTable[index]->next;
234 list->nvTable[index]->next = newItem;
235 }
236
237 return RESULT_SUCCESS;
238 }
239
240 return RESULT_FAIL;
241}
242
243int reloadFactroyParam (T_NV_NODE *list)
244{
245 char *val = NULL;
246 FILE *fp = NULL;
247 T_NV_CONFIG *config = NULL;
248 char buf[NV_MAX_ITEM_LEN] = {0};
249
250 printf("nvserver reloadFactroyParam nvFile:%s\n", list->nvFile);
251
252 for (config = list->fileList; config; config = config->next) {
253 printf ("nvserver reloadFactroyParam configFile start:%s!\n", config->configFile);
254 fp = fopen (config->configFile, "ro");
255 if (!fp) {
256 printf ("nvserver error:open %s file fail errno = %d!\n", config->configFile, errno);
257 return RESULT_FILE_OPEN_FAIL;
258 }
259
260 while (fgets (buf, NV_MAX_ITEM_LEN, fp)) {
261 if (buf[0] == '\n' || buf[0] == '#')
262 continue;
263
264 val = strchr (buf, '=');
265 if (!val) {
266 printf ("nvserver error:%s file format error:string = %s\n", config->configFile, buf);
267 continue;
268 }
269
270 buf[strlen (buf) - 1] = '\0';
271 *val++ = '\0';
272
273 nvupdate (list->nvFile, config->configFile, buf, val, 1);
274 }
275 printf ("nvserver reloadFactroyParam configFile end:%s!\n", config->configFile);
276 fclose (fp);
277 }
278
279 return RESULT_SUCCESS;
280}
281
282void dump_list(T_NV_ITEM *list)
283{
284 if(list == NULL){
285 printf("list is null\n");
286 return;
287 }
288
289 T_NV_ITEM *p = list->next;
290
291 while(p!= NULL){
292 printf ("nvserver dump key=%s, value=%s, p=0x%x\n", p->key, p->value, ((unsigned int)p));
293 p=p->next;
294 }
295}
296
297int delete_not_needed (T_NV_NODE *list)
298{
299 int index = 0;
300 T_NV_ITEM *item = NULL;
301 T_NV_ITEM head = {0};
302 T_NV_ITEM *prev = &head;
303
304 printf ("nvserver delete_not_needed enter ***\n");
305
306 for (index = 0; index < NV_HASH_LEN; index ++) {
307 head.next = list->nvTable[index];
308 prev = &head;
309// printf ("nvserver index=%d begin *****\n", index);
310// dump_list(&head);
311 for (item = prev->next; item;) {
312
313// printf("nvserver1 delete key=%s, value=%s \n", item->key, item->value);
314
315 if (1 == item->update_flag) {
316 prev = item;
317 item = item->next;
318 }
319 else
320 {
321 printf ("nvserver delete key=%s, value=%s \n", item->key, item->value);
322 prev->next = item->next;
323 free (item->key);
324 free (item->value);
325 free (item);
326 item = prev->next;
327
328 }
329 }
330 list->nvTable[index] = head.next;
331// dump_list(&head);
332// printf ("nvserver index=%d end *****\n", index);
333
334 }
335
336 printf ("nvserver delete_not_needed end ***\n");
337
338 return RESULT_SUCCESS;
339}
340
341
342
343
lh9ed821d2023-04-07 01:36:19 -0700344#ifdef __cplusplus
345}
346#endif
347