blob: 93d1a6a9c6ae95b55635c89f9a7958a9fe4a016e [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6#include <unistd.h>
7#include <dlfcn.h>
8#include <stdint.h>
9
10#include <errno.h>
11#include <stdbool.h>
12
13#include "gsw_ota.h"
14
15
16
17#define LYNQ_OTA_INPROCESS 3
18#define LYNQ_OTA_SUCCESS 4
19#define LYNQ_OTA_FAILED 5
20
21#ifndef LOG_ERR_LEVEL
22#define LOG_ERR_LEVEL 3 /* error conditions */
23#endif
24#ifndef LOG_WARN_LEVEL
25#define LOG_WARN_LEVEL 4 /* warning conditions */
26#endif
27#ifndef LOG_INFO_LEVEL
28#define LOG_INFO_LEVEL 6 /* informational */
29#endif
30#ifndef LOG_DEBUG_LEVEL
31#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
32#endif
33#ifndef LOG_VERBOSE_LEVEL
34#define LOG_VERBOSE_LEVEL 8
35#endif
36
37
38#define LIB_PATH "/lib/libmbtk_lib.so"
39
40
41#define StatFunc(x,y) stat(x,y)
42
43#define LOGV(fmt, args ...) \
44 do{ \
45 char *file_ptr_1001 = __FILE__; \
46 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
47 char line_1001[10] = {0}; \
48 sprintf(line_1001, "%d", __LINE__); \
49 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
50 if(*ptr_1001 == '/') \
51 break; \
52 ptr_1001--; \
53 } \
54 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
55 } while(0)
56
57#define LOGI(fmt, args...) \
58 do{ \
59 char *file_ptr_1001 = __FILE__; \
60 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
61 char line_1001[10] = {0}; \
62 sprintf(line_1001, "%d", __LINE__); \
63 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
64 if(*ptr_1001 == '/') \
65 break; \
66 ptr_1001--; \
67 } \
68 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
69 } while(0)
70
71#define LOGD(fmt, args...) \
72 do{ \
73 char *file_ptr_1001 = __FILE__; \
74 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
75 char line_1001[10] = {0}; \
76 sprintf(line_1001, "%d", __LINE__); \
77 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
78 if(*ptr_1001 == '/') \
79 break; \
80 ptr_1001--; \
81 } \
82 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
83 } while(0)
84
85#define LOGW(fmt, args...) \
86 do{ \
87 char *file_ptr_1001 = __FILE__; \
88 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
89 char line_1001[10] = {0}; \
90 sprintf(line_1001, "%d", __LINE__); \
91 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
92 if(*ptr_1001 == '/') \
93 break; \
94 ptr_1001--; \
95 } \
96 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
97 } while(0)
98
99#define LOGE(fmt, args...) \
100 do{ \
101 char *file_ptr_1001 = __FILE__; \
102 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
103 char line_1001[10] = {0}; \
104 sprintf(line_1001, "%d", __LINE__); \
105 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
106 if(*ptr_1001 == '/') \
107 break; \
108 ptr_1001--; \
109 } \
110 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
111 } while(0)
112
113
114
115typedef int (*mbtk_fota_get_active_absys_type)(void);
116typedef int (*mbtk_fota_fw_write)(char* fname, int segment_size);
117typedef int (*mbtk_fota_fw_write_by_url)(char* url, int segment_size,
118 int conn_timeout, int download_timeout);
119
120typedef int(*fota_callback)(int state, int percent);
121typedef int (*mbtk_fota_init)(fota_callback cb);
122typedef int (*mbtk_fota_status)(void);
123
124typedef void (*mbtk_log)(int level, const char *format,...);
125
126static int segment_size =0;
127static int Process_flag = 0;
128char addr_buf[256] = {0};
129static void *handle = NULL;
130static mbtk_log fun_ptr_log = NULL;
131
132static int s_ota_flag = -1;
133
134int fota_cb(int status, int percent)
135{
136 Process_flag = percent;
137 return 0;
138}
139
140static int funstat( char *filename)
141{
142 int ret = 0;
143 struct stat tmep_s;
144
145 memset(&tmep_s, 0, sizeof(stat));
146 ret = StatFunc(filename, &tmep_s);
147
148 if (ret)
149 {
150 LOGE("stat %s failed! error_code: %s", filename ,strerror(errno));
151 return -1;
152 }
153
154 if (tmep_s.st_size > 0)
155 {
156 segment_size = tmep_s.st_size;
157 }
158
159 return 0;
160}
161
162int32_t init_handle()
163{
164 if(handle == NULL)
165 {
166 handle = dlopen(LIB_PATH, RTLD_NOW );
167 if(handle == NULL)
168 {
169 return GSW_HAL_FAIL;
170 }
171 if(fun_ptr_log == NULL)
172 {
173 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
174 if(fun_ptr_log == NULL)
175 {
176 return GSW_HAL_FAIL;
177 }
178 }
179
180 }
181
182
183
184 return GSW_HAL_SUCCESS;
185}
186
187void deinit_handle()
188{
189 dlclose(handle);
190 handle = NULL;
191 fun_ptr_log = NULL;
192
193}
194
195/**
196* @brief Start install modem software
197* @param [in] char* file_path
198* @param [out] NULL
199* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
200*/
201int32_t gsw_update_modem_start_autobackup(char* file_path)
202{
203
204 if(file_path == NULL)
205 {
206 LOGE("invalid file_path \n");
207 return GSW_HAL_FAIL;
208 }
209
210
211 mbtk_fota_fw_write func_ptr_update = NULL;
212 mbtk_fota_init func_ptr_init= NULL;
213 mbtk_fota_fw_write_by_url func_ptr_update_url = NULL;
214
215
216
217 int ret = -1;
218 ret = init_handle();
219 if(ret < 0)
220 {
221 LOGE("init_handle fail");
222 return GSW_HAL_FAIL;
223 }
224
225 func_ptr_update_url = (mbtk_fota_fw_write_by_url)dlsym(handle, "mbtk_fota_fw_write_by_url");
226 if(func_ptr_update_url == NULL)
227 {
228 LOGE("Error: %s", dlerror());
229 return GSW_HAL_FAIL;
230 }
231
232
233 // Get the function pointer
234 func_ptr_update = (mbtk_fota_fw_write)dlsym(handle, "mbtk_fota_fw_write");
235 if (func_ptr_update == NULL)
236 {
237 LOGE("Error: %s", dlerror());
238 deinit_handle();
239 return GSW_HAL_FAIL;
240 }
241
242 func_ptr_init = (mbtk_fota_init)dlsym(handle, "mbtk_fota_init");
243 if(func_ptr_init == NULL)
244 {
245 LOGE("Error: %s", dlerror());
246 deinit_handle();
247 return GSW_HAL_FAIL;
248 }
249
250
251 if(s_ota_flag == -1)
252 {
253 ret = func_ptr_init(fota_cb);
254 if(ret < 0)
255 {
256 LOGE("Error: mbtk_fota_init failed");
257
258 deinit_handle();
259 return GSW_HAL_FAIL;
260 }
261 else
262 {
263 s_ota_flag = 0;
264 }
265 }
266
267 if(strncmp(file_path, "http", 4) == 0)
268 {
269 segment_size = 62914560;
270 ret = func_ptr_update_url(file_path, segment_size,10, 600);
271 }
272 else
273 {
274 if(strstr(file_path,"updata.bin") == NULL)
275 {
276 LOGE("Bad file path ");
277 deinit_handle();
278 return GSW_HAL_FAIL;
279 }
280 else
281 {
282 if(access(file_path,F_OK) !=0)
283 {
284 LOGE("update file no exist");
285 deinit_handle();
286 return GSW_HAL_FAIL;
287 }
288 }
289
290 ret = funstat(file_path);
291 if (ret)
292 {
293 LOGE("get segment_size fail");
294 deinit_handle();
295 return GSW_HAL_FAIL;
296 }
297 ret = func_ptr_update(file_path, segment_size);
298 if(ret < 0)
299 {
300 LOGE("Error: mbtk_fota_fw_write failed\n");
301 deinit_handle();
302 return GSW_HAL_FAIL;
303 }
304 }
305 return GSW_HAL_SUCCESS;
306
307}
308
309/**
310* @brief check the modem update condition
311* @param [in] NULL
312* @param [out] NULL
313* @retval TRUE/FALSE
314*/
315bool gsw_update_modem_check_condition(void)
316{
317 char command[32] = {0};
318 char buffer[64] = {0};
319 const char *process_ota = "{otad}";
320
321 snprintf(command,sizeof(command), "ps | grep %s | grep -v grep", process_ota);
322 FILE *fp = popen(command, "r");
323 if (fp == NULL)
324 {
325 return false;
326 }
327
328 if(fgets(buffer, sizeof(buffer), fp)!= NULL)
329 {
330 pclose(fp);
331 return true;
332 }
333 pclose(fp);
334 return false;
335
336}
337
338/**
339* @brief get update modem result
340* @param [in] NULL
341* @param [out] NULL
342* @retval E_GSW_OTA_RET
343*/
344E_GSW_OTA_RET gsw_update_modem_result_query(void)
345{
346
347 mbtk_fota_status func_ptr_get_result = NULL;
348 int ret = -1;
349
350 ret = init_handle();
351 if(ret < 0)
352 {
353 LOGE("init_handle fail");
354 return GSW_HAL_FAIL;
355 }
356
357 // Get the function pointer
358 func_ptr_get_result = (mbtk_fota_status)dlsym(handle, "mbtk_fota_status");
359 if (func_ptr_get_result == NULL)
360 {
361 LOGE("Error: %s\n", dlerror());
362 return GSW_HAL_FAIL;
363 }
364 ret = func_ptr_get_result();
365 if(ret < 0 && ret !=-1)
366 {
367 LOGE("Error: mbtk_fota_status failed\n");
368 deinit_handle();
369 return GSW_HAL_FAIL;
370 }
371
372 deinit_handle();
373
374 if(ret == LYNQ_OTA_INPROCESS)
375 {
376 return GSW_OTA_INPROCESS;
377
378 }
379 else if(ret == LYNQ_OTA_SUCCESS)
380 {
381 return GSW_OTA_SUCCESS;
382 }
383 else if(ret == -1)
384 {
385 return GSW_OTA_NO_TASK;
386 }
387
388 return GSW_OTA_FAILURE;
389
390
391}
392
393/**
394* @brief Start install modem software
395* @param [in] char* file_path
396* @param [out] NULL
397* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
398*/
399int32_t gsw_update_modem_start_nobackup(char* file_path)
400{
401
402 return GSW_HAL_SUCCESS;
403}
404
405
406/**
407* @brief get current system
408* @param [in] NULL
409* @param [out] NULL
410* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
411*/
412E_GSW_OTA_SYSTEM gsw_update_modem_get_system(void)
413{
414 int ret = -1;
415
416 mbtk_fota_get_active_absys_type func_ptr;
417 ret = init_handle();
418 if(ret < 0)
419 {
420 LOGE("init_handle fail");
421 return GSW_HAL_FAIL;
422 }
423
424 // Get the function pointer
425 func_ptr = (mbtk_fota_get_active_absys_type)dlsym(handle, "mbtk_fota_get_active_absys_type");
426 if (func_ptr == NULL)
427 {
428 LOGE("Error: %s", dlerror());
429 return GSW_HAL_FAIL;
430 }
431 ret = func_ptr();
432 if(ret < 0)
433 {
434 LOGE("Error: mbtk_fota_get_active_absys_type failed");
435 deinit_handle();
436 return GSW_HAL_FAIL;
437
438 }
439
440 deinit_handle();
441
442 return ret;
443
444}
445
446/**
447* @brief cancel update
448* @param [in] NULL
449* @param [out] NULL
450* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
451*/
452int32_t gsw_update_modem_cancel(void)
453{
454
455 return GSW_HAL_SUCCESS;
456}
457
458/**
459* @brief get modem update info
460* @param [in] NULL
461* @param [out] gsw_update_info_s
462* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
463*/
464int32_t gsw_update_modem_get_info(gsw_update_info_s *update_info)
465{
466 int ret = -1;
467
468 mbtk_fota_status func_ptr_get_info = NULL;
469
470 ret = init_handle();
471 if(ret < 0)
472 {
473 LOGE("init_handle fail");
474 return GSW_HAL_FAIL;
475 }
476
477 // Get the function pointer
478 func_ptr_get_info = (mbtk_fota_status)dlsym(handle, "mbtk_fota_status");
479 if (func_ptr_get_info == NULL)
480 {
481 LOGE("Error: %s\n", dlerror());
482 return GSW_HAL_FAIL;
483 }
484 ret = func_ptr_get_info();
485 if(ret < 0 && ret != -1)
486 {
487 LOGE("Error: mbtk_fota_status failed\n");
488 deinit_handle();
489 return GSW_HAL_FAIL;
490 }
491
492 //升级成功之后,未重启,不可重复升级
493 if(ret == LYNQ_OTA_INPROCESS)
494 {
495 update_info->exit_code = GSW_UPDATE_NOGOING;
496 update_info->percentage = Process_flag;
497 update_info->update_state = GSW_UPDATE_INPROGRESS;
498 }
499 if(ret == LYNQ_OTA_SUCCESS)
500 {
501 update_info->exit_code = GSW_UPDATE_NOERROR;
502 update_info->percentage = Process_flag;
503 update_info->update_state = GSW_UPDATE_WAITEDONE;
504 }
505
506
507 if(ret == LYNQ_OTA_FAILED || ret == -1)
508 {
509 update_info->exit_code = GSW_UPDATE_GETOLDSOFTWAREFAILED;
510 update_info->percentage = Process_flag;
511 update_info->update_state = GSW_UPDATE_FAILED;
512 }
513
514 deinit_handle();
515 return GSW_HAL_SUCCESS;
516
517}
518
519
520/**
521* @brief get modem system status
522* @param [in] NULL
523* @param [out] gsw_system_status_s
524* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
525*/
526int32_t gsw_update_modem_get_status(gsw_system_status_s *system_status)
527{
528 return GSW_HAL_SUCCESS;
529}
530
531/**
532* @brief A/B system sync
533* @param [in] NULL
534* @param [out] NULL
535* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
536*/
537int32_t gsw_update_modem_sync(void)
538{
539 return GSW_HAL_SUCCESS;
540
541}
542
543/**
544* @brief A/B system switch
545* @param [in] NULL
546* @param [out] NULL
547* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
548*/
549int32_t gsw_update_modem_switch(void)
550{
551 return GSW_HAL_SUCCESS;
552}