blob: eb1efeeff5f28661a0bbcf477e445f359092128e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*******************************************************************************
2* °æÈ¨ËùÓÐ (C)2023, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£
3*
4* ÎļþÃû³Æ: nvserver_rpc.c
5* Îļþ±êʶ: nvserver_rpc.c
6* ÄÚÈÝÕªÒª: nvserverת½ÓcapµÄnvserver
7*
8* ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
9* ------------------------------------------------------------------------------
10* 2016/06/13 V1.0 Create ÁõÑÇÄÏ ´´½¨
11*
12*******************************************************************************/
13
14/*******************************************************************************
15* Í·Îļþ *
16*******************************************************************************/
17#include <unistd.h>
18#include <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
xf.li742dd022023-06-08 01:43:32 -070021#include <assert.h>
lh9ed821d2023-04-07 01:36:19 -070022#include <dirent.h>
23#include <string.h>
24#include <sys/file.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ipc.h>
28#include <sys/msg.h>
29
30#include "nvserver.h"
31#include "nv_typedef.h"
32#include <message.h>
33#include "sc_rpc.h"
34
35#ifdef FOTA_AB
36#include "zxic_fota_ab_upgrade.h"
37#endif
38
39#ifdef __cplusplus
40extern "C"
41{
42#endif
43
44/*******************************************************************************
45* ³£Á¿¶¨Òå *
46*******************************************************************************/
47
48/*******************************************************************************
49* ºê¶¨Òå *
50*******************************************************************************/
51#define RPC_RPMSG_DEV "/dev/rpmsg8"
52
53/*******************************************************************************
54* Êý¾ÝÀàÐͶ¨Òå *
55*******************************************************************************/
56
57/*******************************************************************************
58* ¾Ö²¿º¯ÊýÉùÃ÷ *
59*******************************************************************************/
60
61/*******************************************************************************
62* ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå *
63*******************************************************************************/
64
65/*******************************************************************************
66* È«¾Ö±äÁ¿¶¨Òå *
67*******************************************************************************/
68static int g_rpc_fd = -1;
xf.li742dd022023-06-08 01:43:32 -070069static unsigned int g_msgRcvCnt = 0;
70static unsigned int g_msgRcvErrCnt = 0;
71static unsigned int g_msgSndCnt = 0;
72static unsigned int g_msgSndErrCnt = 0;
lh9ed821d2023-04-07 01:36:19 -070073
74/*******************************************************************************
75* ¾Ö²¿º¯ÊýʵÏÖ *
76*******************************************************************************/
77/*******************************************************************************
78* ¹¦ÄÜÃèÊö: analyMsg
79* ²ÎÊý˵Ã÷:
80* (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢
81* (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢
82* ·µ »Ø Öµ: 0±íʾ³É¹¦
83* ÆäËü˵Ã÷: void
84*******************************************************************************/
xf.libdd93d52023-05-12 07:10:14 -070085static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd, T_sc_rpc_header *rpc_data, T_sc_rpc_header *rpc_data2)
lh9ed821d2023-04-07 01:36:19 -070086{
xf.libdd93d52023-05-12 07:10:14 -070087 unsigned int timeout = 10;
88 int try_cnt = 0;
lh9ed821d2023-04-07 01:36:19 -070089
xf.libdd93d52023-05-12 07:10:14 -070090 memset(rpc_data, 0, sizeof(T_sc_rpc_header));
91 memset(rpc_data2, 0, sizeof(T_sc_rpc_header));
lh9ed821d2023-04-07 01:36:19 -070092
xf.libdd93d52023-05-12 07:10:14 -070093 rpc_data->msg_type = RPC_MSG_TYPE_REQUEST;
94 rpc_data->func_id = RPC_FUNC_ID_NV;
95 rpc_data->data_len = sizeof(T_NV_MSG_INFO);
96 memcpy(rpc_data->data, msgrecv, sizeof(T_NV_MSG_INFO));
lh9ed821d2023-04-07 01:36:19 -070097
xf.libdd93d52023-05-12 07:10:14 -070098 do
lh9ed821d2023-04-07 01:36:19 -070099 {
xf.libdd93d52023-05-12 07:10:14 -0700100 if (try_cnt > 0)
101 {
102 sc_rpc_clear(g_rpc_fd);
103 }
104 sc_rpc_send(g_rpc_fd, rpc_data, 0);
105 try_cnt++;
106 //sleep(1);
107 //continue;
xf.li742dd022023-06-08 01:43:32 -0700108 } while (0 != sc_rpc_recv(g_rpc_fd, rpc_data2, timeout)); //µÈ´ýÍê³É
109 if (rpc_data2->msg_type != RPC_MSG_TYPE_REPLY)
110 assert(0);
lh9ed821d2023-04-07 01:36:19 -0700111
xf.libdd93d52023-05-12 07:10:14 -0700112 memcpy(msgsnd, rpc_data2->data, sizeof(T_NV_MSG_RESULT));
lh9ed821d2023-04-07 01:36:19 -0700113}
114
xf.li742dd022023-06-08 01:43:32 -0700115static int nv_rpc_wait_ready(int fd)
116{
117 T_sc_rpc_header *rpc_data;
118 unsigned int timeout = 200; // ms
119 int write_flag = 1;
120
121 rpc_data = malloc(sizeof(T_sc_rpc_header));
122 if (rpc_data == NULL)
123 return -1;
124 while(1)
125 {
126 rpc_data->msg_type = RPC_MSG_TYPE_READY;
127 if (write_flag)
128 {
129 if (write(fd, rpc_data, sizeof(T_sc_rpc_header)) != 0)
130 write_flag = 0; // write ok and write sonly once
131 }
132 if (read(fd, rpc_data, sizeof(T_sc_rpc_header)) == 0)
133 {
134 usleep(timeout * 1000);
135 continue;
136 }
137 break; /* read some data mean channel ok */
138 }
139
140 free(rpc_data);
141 return 0;
142}
143
lh9ed821d2023-04-07 01:36:19 -0700144/*******************************************************************************
145* È«¾Öº¯ÊýʵÏÖ *
146*******************************************************************************/
147
148/*******************************************************************************
149* ¹¦ÄÜÃèÊö: main
150* ²ÎÊý˵Ã÷:
151* (´«Èë²ÎÊý) void
152* (´«³ö²ÎÊý) void
153* ·µ »Ø Öµ: void
154* ÆäËü˵Ã÷: void
155*******************************************************************************/
156int nvserver_main(int argc, char *argv[])
157{
158 int msgId = 0;
159 T_NV_MSG_INFO rcvBuf;
160 T_NV_MSG_RESULT sndBuf;
161 struct msqid_ds msgInfo;
xf.libdd93d52023-05-12 07:10:14 -0700162 T_sc_rpc_header *rpc_data;
163 T_sc_rpc_header *rpc_data2;
xf.li742dd022023-06-08 01:43:32 -0700164 long pid_backup; /* app-libnvram pid backup */
lh9ed821d2023-04-07 01:36:19 -0700165
166 prctl(PR_SET_NAME, "nvserver", 0, 0, 0);
167
168 memset(&msgInfo, 0, sizeof(msgInfo));
169
170 g_rpc_fd = sc_rpc_open(RPC_RPMSG_DEV);
171
172 msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600);
173 if (-1 == msgId)
174 {
175 printf("nvserver error: msgget msgId fail, errno = %d\n", errno);
176 return -1;
177 }
178
179 if (-1 != msgctl(msgId, IPC_STAT, &msgInfo))
180 {
181 msgInfo.msg_qbytes = 262144; // 256k
182 if (-1 == msgctl(msgId, IPC_SET, &msgInfo))
183 {
184 printf("nvserver error: msgctl msgId fail, errno = %d\n", errno);
185 }
186 }
187
xf.libdd93d52023-05-12 07:10:14 -0700188 rpc_data = malloc(sizeof(T_sc_rpc_header));
189 if (rpc_data == NULL)
190 {
191 perror("[error]nvrpc malloc error\n");
192 return -2;
193 }
194 rpc_data2 = malloc(sizeof(T_sc_rpc_header));
195 if (rpc_data2 == NULL)
196 {
197 perror("[error]nvrpc malloc2 error\n");
198 free(rpc_data);
199 return -3;
200 }
201
xf.li742dd022023-06-08 01:43:32 -0700202 if (nv_rpc_wait_ready(g_rpc_fd) < 0)
203 {
204 assert(0);
205 }
206 sc_rpc_clear(g_rpc_fd);
207 printf("apnv and nv-rpc-daemon are ready both.");
208
lh9ed821d2023-04-07 01:36:19 -0700209 // ÏûÏ¢½»»¥
210 while (1)
211 {
212 memset(&rcvBuf, 0, sizeof(rcvBuf));
213 memset(&sndBuf, 0, sizeof(sndBuf));
xf.li742dd022023-06-08 01:43:32 -0700214 if (g_msgRcvCnt != g_msgSndCnt)
215 {
216 printf("[nvserver_rpc]Rcv:%u Snd:%u RcvErr:%u SndErr:%u\n", g_msgRcvCnt, g_msgSndCnt, g_msgRcvErrCnt, g_msgSndErrCnt);
217 assert(0);
218 }
lh9ed821d2023-04-07 01:36:19 -0700219 if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0))
220 {
xf.li742dd022023-06-08 01:43:32 -0700221 g_msgRcvErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700222 printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno);
223 continue;
224 }
xf.li742dd022023-06-08 01:43:32 -0700225 //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid);
226 pid_backup = rcvBuf.pid;
227 g_msgRcvCnt++; /* msgrcv success count */
lh9ed821d2023-04-07 01:36:19 -0700228
xf.libdd93d52023-05-12 07:10:14 -0700229 analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700230
xf.li742dd022023-06-08 01:43:32 -0700231 //printf("apnv nvserver_rpc msgsnd pid:%d, backup:%d\n", sndBuf.msgType, pid_backup);
lh9ed821d2023-04-07 01:36:19 -0700232 if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0))
233 {
xf.li742dd022023-06-08 01:43:32 -0700234 g_msgSndErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700235 printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno);
236 continue;
237 }
xf.li742dd022023-06-08 01:43:32 -0700238 g_msgSndCnt++; /* msgsnd success count */
239 if (sndBuf.msgType != pid_backup)
240 {
241 assert(0);
242 }
lh9ed821d2023-04-07 01:36:19 -0700243 }
244
xf.libdd93d52023-05-12 07:10:14 -0700245 free(rpc_data);
246 free(rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700247 return (0);
248}
249
250#ifdef __cplusplus
251}
252#endif