blob: 595f6a746eeec823c3a8d6b6c7702f8f004a247b [file] [log] [blame]
xf.li742dd022023-06-08 01:43:32 -07001/**
2 * @file sc_absvr.c
3 * @brief Implementation of sc_absvr.
4 *
5 * Copyright (C) 2023 Sanechips Technology Co., Ltd.
6 * @author
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©
11 *
12 */
13
14
15/*******************************************************************************
16 * Include header files *
17 ******************************************************************************/
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <assert.h>
22#include <syslog.h>
23#include <sys/klog.h>
24#include <sys/msg.h>
25#include <sys/un.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29
30#include "absvr_msg.h"
31#include "zxic_fota_ab_upgrade.h"
32#include "softap_log.h"
33#include "at_reg.h"
34#include "softap_api.h"
35
36
37/*******************************************************************************
38 * Macro definitions *
39 ******************************************************************************/
40#define MSG_DATABUF_MAX_LEN (128)
41
42#define DEBUG 1
43
44//#define ABSVR_PRINT "SC_AB_SVR"
45
46/*******************************************************************************
47 * Type definitions *
48 ******************************************************************************/
49typedef struct
50{
51 int result; // 0: ³É¹¦ ·Ç0: ʧ°Ü
52 unsigned char dataBuf[MSG_DATABUF_MAX_LEN];
53} absvr_common_result_t;
54
55
56typedef struct
57{
58 int boot_to_system;
59 int reboot_flag;
60
61 int system;
62 int status;
63
64 int status_for_nv;
65
66 int sync_status;
67} absvr_common_req_msg_t;
68
69
70/*******************************************************************************
71 * Local variable definitions *
72 ******************************************************************************/
73
74
75/*******************************************************************************
76 * Global variable definitions *
77 ******************************************************************************/
78
79
80/*******************************************************************************
81 * Local function declarations *
82 ******************************************************************************/
83static void flush_upgrade_status(z_upgrade_status_info_t *p_status);
84
85static int absvr_get_upgrade_type(MSG_BUF *msg);
86static int absvr_verify(MSG_BUF *msg);
87static int absvr_update(MSG_BUF *msg);
88static int absvr_sync(MSG_BUF *msg);
89static int absvr_get_upgrade_status(MSG_BUF *msg);
90static int absvr_get_current_system(MSG_BUF *msg);
91static int absvr_get_boot_to_system(MSG_BUF *msg);
92static int absvr_set_boot_to_system(MSG_BUF *msg);
93static int absvr_get_system_status(MSG_BUF *msg);
94static int absvr_set_system_status(MSG_BUF *msg);
95static int absvr_get_fota_status_for_nv(MSG_BUF *msg);
96static int absvr_set_fota_status_for_nv(MSG_BUF *msg);
97static int absvr_get_sync_status(MSG_BUF *msg);
98static int absvr_set_sync_status(MSG_BUF *msg);
99
100static int absvr_msg_proc(MSG_BUF *msg);
101
102
103/*******************************************************************************
104 * Local function implementations *
105 ******************************************************************************/
106static void flush_upgrade_status(z_upgrade_status_info_t *p_status)
107{
108 absvr_common_result_t absvr_common_result = {0};
109
110 memcpy(absvr_common_result.dataBuf, p_status, sizeof(z_upgrade_status_info_t));
111
112 if (0 != send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_UPDATING_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result)))
113 {
114 slog(ABSVR_PRINT, SLOG_ERR, "Err: flush_upgrade_status send_soc_msg fail \n");
115 }
116
117 return;
118}
119
120
121static int absvr_get_upgrade_type(MSG_BUF *msg)
122{
123 absvr_common_result_t absvr_common_result = {0};
124 int ret = -1;
125
126 ret = zxic_dual_get_upgrade_type();
127
128 absvr_common_result.result = ret;
129
130 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_UPGRADE_TYPE_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
131}
132
133
134static int absvr_verify(MSG_BUF *msg)
135{
136 absvr_common_result_t absvr_common_result = {0};
137
138 absvr_common_result.result = zxic_dual_verify();
139
140 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_VRITFY_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
141}
142
143
144static int absvr_update(MSG_BUF *msg)
145{
146 absvr_common_result_t absvr_common_result = {0};
147
148 z_upgrade_status_info_t status = {0};
149 z_upgrade_flush_status_t flush_status = {0};
150
151 flush_status.status = &status;
152 flush_status.status_cb = &flush_upgrade_status;
153
154 absvr_common_result.result = zxic_dual_upgrade(&flush_status);
155
156 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_UPDATE_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
157}
158
159
160static int absvr_sync(MSG_BUF *msg)
161{
162 absvr_common_result_t absvr_common_result = {0};
163
164 absvr_common_result.result = zxic_dual_sync_system();
165
166 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SYNC_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
167}
168
169
170static int absvr_get_upgrade_status(MSG_BUF *msg)
171{
172 absvr_common_result_t absvr_common_result = {0};
173 int ret = -1;
174
175 z_upgrade_status_info_t upgrade_info = {0};
176
177 ret = zxic_dual_get_upgrade_status(&upgrade_info);
178
179 absvr_common_result.result = ret;
180 memcpy(absvr_common_result.dataBuf, &upgrade_info, sizeof(z_upgrade_status_info_t));
181
182 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_UPGRADE_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
183}
184
185
186static int absvr_get_current_system(MSG_BUF *msg)
187{
188 absvr_common_result_t absvr_common_result = {0};
189 int ret = -1;
190
191 ret = zxic_dual_get_current_system();
192
193 absvr_common_result.result = ret;
194
195 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_CURRENT_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
196}
197
198
199static int absvr_get_boot_to_system(MSG_BUF *msg)
200{
201 absvr_common_result_t absvr_common_result = {0};
202 int ret = -1;
203
204 ret = zxic_dual_get_boot_to_system();
205
206 absvr_common_result.result = ret;
207
208 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_BOOT_TO_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
209}
210
211
212static int absvr_set_boot_to_system(MSG_BUF *msg)
213{
214 absvr_common_req_msg_t *common_req_msg = NULL;
215
216 absvr_common_result_t absvr_common_result = {0};
217 int ret = -1;
218
219 common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);
220
221 ret = zxic_dual_set_boot_to_system(common_req_msg->boot_to_system, common_req_msg->reboot_flag);
222
223 absvr_common_result.result = ret;
224
225 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_BOOT_TO_SYSTEM_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
226}
227
228
229static int absvr_get_system_status(MSG_BUF *msg)
230{
231 absvr_common_result_t absvr_common_result = {0};
232 int ret = -1;
233
234 z_upgrade_system_info_t system_info = {0};
235
236 ret = zxic_dual_get_system_status(&system_info);
237
238 absvr_common_result.result = ret;
239 memcpy(absvr_common_result.dataBuf, &system_info, sizeof(z_upgrade_system_info_t));
240
241 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_SYSTEM_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
242}
243
244
245static int absvr_set_system_status(MSG_BUF *msg)
246{
247 absvr_common_req_msg_t *common_req_msg = NULL;
248
249 absvr_common_result_t absvr_common_result = {0};
250 int ret = -1;
251
252 common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);
253
254 ret = zxic_dual_set_system_status(common_req_msg->system, common_req_msg->status);
255
256 absvr_common_result.result = ret;
257
258 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_SYSTEM_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
259}
260
261
262static int absvr_get_fota_status_for_nv(MSG_BUF *msg)
263{
264 absvr_common_result_t absvr_common_result = {0};
265 int ret = -1;
266
267 ret = zxic_dual_get_fota_status_for_nv();
268
269 absvr_common_result.result = ret;
270
271 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_FOTA_STATUS_FOR_NV_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
272}
273
274
275static int absvr_set_fota_status_for_nv(MSG_BUF *msg)
276{
277 absvr_common_req_msg_t *common_req_msg = NULL;
278
279 absvr_common_result_t absvr_common_result = {0};
280 int ret = -1;
281
282 common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);
283
284 ret = zxic_dual_set_fota_status_for_nv(common_req_msg->status_for_nv);
285
286 absvr_common_result.result = ret;
287
288 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_FOTA_STATUS_FOR_NV_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
289}
290
291
292static int absvr_get_sync_status(MSG_BUF *msg)
293{
294 absvr_common_result_t absvr_common_result = {0};
295
296 zxic_dual_get_sync_status(&(absvr_common_result.result));
297
298 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_GET_SYNC_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
299}
300
301
302static int absvr_set_sync_status(MSG_BUF *msg)
303{
304 absvr_common_req_msg_t *common_req_msg = NULL;
305
306 absvr_common_result_t absvr_common_result = {0};
307 int ret = -1;
308
309 common_req_msg = (absvr_common_req_msg_t *)(msg->aucDataBuf);
310
311 ret = zxic_dual_set_sync_status(common_req_msg->sync_status);
312
313 absvr_common_result.result = ret;
314
315 return send_soc_msg(FAR_PS, MODULE_ID_ABCLIENT, (unsigned short)ABSVR_SET_SYNC_STATUS_RSP, sizeof(absvr_common_result), (unsigned char *)(&absvr_common_result));
316}
317
318
319static int absvr_msg_proc(MSG_BUF *msg)
320{
321 int ret = 0;
322
323 if( NULL == msg)
324 {
325 slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc msg is NULL \n");
326 return -1;
327 }
328
329 slog(ABSVR_PRINT, SLOG_NORMAL, "absvr_msg_proc start process msg [%hu] \n", msg->usMsgCmd);
330
331 switch(msg->usMsgCmd)
332 {
333 case ABSVR_GET_UPGRADE_TYPE_REQ:
334 ret = absvr_get_upgrade_type(msg);
335 break;
336 case ABSVR_VRITFY_REQ:
337 ret = absvr_verify(msg);
338 break;
339 case ABSVR_UPDATE_REQ:
340 ret = absvr_update(msg);
341 break;
342 case ABSVR_SYNC_REQ:
343 ret = absvr_sync(msg);
344 break;
345 case ABSVR_GET_UPGRADE_STATUS_REQ:
346 ret = absvr_get_upgrade_status(msg);
347 break;
348 case ABSVR_GET_CURRENT_SYSTEM_REQ:
349 ret = absvr_get_current_system(msg);
350 break;
351 case ABSVR_GET_BOOT_TO_SYSTEM_REQ:
352 ret = absvr_get_boot_to_system(msg);
353 break;
354 case ABSVR_SET_BOOT_TO_SYSTEM_REQ:
355 ret = absvr_set_boot_to_system(msg);
356 break;
357 case ABSVR_GET_SYSTEM_STATUS_REQ:
358 ret = absvr_get_system_status(msg);
359 break;
360 case ABSVR_SET_SYSTEM_STATUS_REQ:
361 ret = absvr_set_system_status(msg);
362 break;
363 case ABSVR_GET_FOTA_STATUS_FOR_NV_REQ:
364 ret = absvr_get_fota_status_for_nv(msg);
365 break;
366 case ABSVR_SET_FOTA_STATUS_FOR_NV_REQ:
367 ret = absvr_set_fota_status_for_nv(msg);
368 break;
369 case ABSVR_GET_SYNC_STATUS_REQ:
370 ret = absvr_get_sync_status(msg);
371 break;
372 case ABSVR_SET_SYNC_STATUS_REQ:
373 ret = absvr_set_sync_status(msg);
374 break;
375 default:
376 slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc unknow usMsgCmd [%hu] \n", msg->usMsgCmd);
377 break;
378 }
379
380 return ret;
381}
382
383
384/*******************************************************************************
385 * Global function implementations *
386 ******************************************************************************/
387
388int main(void)
389{
390 int iRet = 0;
391
392 int iMsgHandle = 0;
393 MSG_BUF stMsg = {0};
394 LONG msgSize = sizeof(MSG_BUF) - sizeof(LONG);
395
396 /*¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿*/
397 loglevel_init();
398 slog(ABSVR_PRINT, SLOG_NORMAL, "sc_absvr begin ======= build date: %s %s \n", __DATE__, __TIME__);
399
400 iMsgHandle = msgget(MODULE_ID_ABSVR, IPC_CREAT | 0600);
401 if (-1 == iMsgHandle)
402 {
403 slog(ABSVR_PRINT, SLOG_ERR, "Err: can not create msg queue for sc_absvr \n");
404 return -1;
405 }
406
407 for (;;)
408 {
409 memset(&stMsg, 0, sizeof(stMsg));
410 iRet = 0;
411 iRet = msgrcv(iMsgHandle, &stMsg, msgSize, 0, 0);
412 if (-1 == iRet)
413 {
414 slog(ABSVR_PRINT, SLOG_ERR, "Err: recv msg fail errno = %d \n", errno);
415 continue;
416 }
417
418 if (0 != absvr_msg_proc(&stMsg))
419 {
420 slog(ABSVR_PRINT, SLOG_ERR, "Err: absvr_msg_proc src_id[%d] usMsgCmd[%hu] fail \n", stMsg.src_id, stMsg.usMsgCmd);
421 }
422 }
423
424 return 0;
425}
426