blob: c5e6c5e09ae463fa76633444b9a14ae31784f4bd [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*******************************************************************************/
lh758261d2023-07-13 05:52:04 -070085static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd, T_sc_rpc_msg *rpc_data, T_sc_rpc_msg *rpc_data2)
lh9ed821d2023-04-07 01:36:19 -070086{
xf.libdd93d52023-05-12 07:10:14 -070087 unsigned int timeout = 10;
lh758261d2023-07-13 05:52:04 -070088 int send_once_flag = 0;
89 int ret;
lh9ed821d2023-04-07 01:36:19 -070090
lh758261d2023-07-13 05:52:04 -070091 rpc_data->header.msg_type = RPC_MSG_TYPE_REQUEST;
92 rpc_data->header.func_id = RPC_FUNC_ID_NV;
93 rpc_data->header.data_len = sizeof(T_NV_MSG_INFO) - NV_MAX_FILE_LEN - NV_MAX_KEY_LEN - NV_MAX_VAL_LEN;
94 rpc_data->header.file_len = strlen(msgrecv->file) + 1;
95 rpc_data->header.data_len += rpc_data->header.file_len;
96 memcpy(rpc_data->data, msgrecv, rpc_data->header.data_len);
97 rpc_data->header.key_len = strlen(msgrecv->key) + 1;
98 memcpy(rpc_data->data+rpc_data->header.data_len, msgrecv->key, rpc_data->header.key_len);
99 rpc_data->header.data_len += rpc_data->header.key_len;
100 rpc_data->header.value_len = strlen(msgrecv->value);
101 if (rpc_data->header.value_len > 0)
102 {
103 rpc_data->header.value_len += 1;
104 memcpy(rpc_data->data+rpc_data->header.data_len, msgrecv->value, rpc_data->header.value_len);
105 rpc_data->header.data_len += rpc_data->header.value_len;
106 }
lh9ed821d2023-04-07 01:36:19 -0700107
xf.libdd93d52023-05-12 07:10:14 -0700108 do
lh9ed821d2023-04-07 01:36:19 -0700109 {
lh758261d2023-07-13 05:52:04 -0700110 if (send_once_flag == 0)
xf.libdd93d52023-05-12 07:10:14 -0700111 {
lh758261d2023-07-13 05:52:04 -0700112 ret = sc_rpc_send(g_rpc_fd, rpc_data, 0);
113 if (ret == 0)
114 {
115 send_once_flag = 1; // send success
116 }
117 else
118 {
119 assert(0); // send fail
120 }
xf.libdd93d52023-05-12 07:10:14 -0700121 }
lh758261d2023-07-13 05:52:04 -0700122
123 ret = sc_rpc_recv(g_rpc_fd, rpc_data2, timeout);
124 if (ret == 0)
125 {
126 break; // recv success and break
127 }
128
129 } while (1); //µÈ´ýÍê³É
130
131 if (rpc_data2->header.msg_type != RPC_MSG_TYPE_REPLY)
xf.li742dd022023-06-08 01:43:32 -0700132 assert(0);
lh9ed821d2023-04-07 01:36:19 -0700133
lh758261d2023-07-13 05:52:04 -0700134 memcpy(msgsnd, rpc_data2->data, rpc_data2->header.data_len);
lh9ed821d2023-04-07 01:36:19 -0700135}
136
xf.li742dd022023-06-08 01:43:32 -0700137static int nv_rpc_wait_ready(int fd)
138{
139 T_sc_rpc_header *rpc_data;
lh758261d2023-07-13 05:52:04 -0700140 unsigned int timeout = 100; // ms
xf.li742dd022023-06-08 01:43:32 -0700141
142 rpc_data = malloc(sizeof(T_sc_rpc_header));
143 if (rpc_data == NULL)
144 return -1;
lh758261d2023-07-13 05:52:04 -0700145
xf.li742dd022023-06-08 01:43:32 -0700146 while(1)
147 {
148 rpc_data->msg_type = RPC_MSG_TYPE_READY;
lh758261d2023-07-13 05:52:04 -0700149 if (write(fd, rpc_data, sizeof(T_sc_rpc_header)) == sizeof(T_sc_rpc_header))
150 break;
151 else
xf.li742dd022023-06-08 01:43:32 -0700152 usleep(timeout * 1000);
xf.li742dd022023-06-08 01:43:32 -0700153 }
154
155 free(rpc_data);
156 return 0;
157}
158
lh9ed821d2023-04-07 01:36:19 -0700159/*******************************************************************************
160* È«¾Öº¯ÊýʵÏÖ *
161*******************************************************************************/
162
163/*******************************************************************************
164* ¹¦ÄÜÃèÊö: main
165* ²ÎÊý˵Ã÷:
166* (´«Èë²ÎÊý) void
167* (´«³ö²ÎÊý) void
168* ·µ »Ø Öµ: void
169* ÆäËü˵Ã÷: void
170*******************************************************************************/
171int nvserver_main(int argc, char *argv[])
172{
173 int msgId = 0;
174 T_NV_MSG_INFO rcvBuf;
175 T_NV_MSG_RESULT sndBuf;
176 struct msqid_ds msgInfo;
lh758261d2023-07-13 05:52:04 -0700177 T_sc_rpc_msg *rpc_data;
178 T_sc_rpc_msg *rpc_data2;
xf.li742dd022023-06-08 01:43:32 -0700179 long pid_backup; /* app-libnvram pid backup */
lh9ed821d2023-04-07 01:36:19 -0700180
lh758261d2023-07-13 05:52:04 -0700181 prctl(PR_SET_NAME, "nvserver_rpc", 0, 0, 0);
lh9ed821d2023-04-07 01:36:19 -0700182
183 memset(&msgInfo, 0, sizeof(msgInfo));
184
185 g_rpc_fd = sc_rpc_open(RPC_RPMSG_DEV);
186
187 msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600);
188 if (-1 == msgId)
189 {
190 printf("nvserver error: msgget msgId fail, errno = %d\n", errno);
191 return -1;
192 }
193
194 if (-1 != msgctl(msgId, IPC_STAT, &msgInfo))
195 {
196 msgInfo.msg_qbytes = 262144; // 256k
197 if (-1 == msgctl(msgId, IPC_SET, &msgInfo))
198 {
199 printf("nvserver error: msgctl msgId fail, errno = %d\n", errno);
200 }
201 }
202
lh758261d2023-07-13 05:52:04 -0700203 rpc_data = malloc(sizeof(T_sc_rpc_msg));
xf.libdd93d52023-05-12 07:10:14 -0700204 if (rpc_data == NULL)
205 {
206 perror("[error]nvrpc malloc error\n");
207 return -2;
208 }
lh758261d2023-07-13 05:52:04 -0700209 rpc_data2 = malloc(sizeof(T_sc_rpc_msg));
xf.libdd93d52023-05-12 07:10:14 -0700210 if (rpc_data2 == NULL)
211 {
212 perror("[error]nvrpc malloc2 error\n");
213 free(rpc_data);
214 return -3;
215 }
216
xf.li742dd022023-06-08 01:43:32 -0700217 if (nv_rpc_wait_ready(g_rpc_fd) < 0)
218 {
lh758261d2023-07-13 05:52:04 -0700219 assert(0);
xf.li742dd022023-06-08 01:43:32 -0700220 }
221 sc_rpc_clear(g_rpc_fd);
222 printf("apnv and nv-rpc-daemon are ready both.");
223
lh9ed821d2023-04-07 01:36:19 -0700224 // ÏûÏ¢½»»¥
225 while (1)
226 {
227 memset(&rcvBuf, 0, sizeof(rcvBuf));
228 memset(&sndBuf, 0, sizeof(sndBuf));
xf.li742dd022023-06-08 01:43:32 -0700229 if (g_msgRcvCnt != g_msgSndCnt)
230 {
231 printf("[nvserver_rpc]Rcv:%u Snd:%u RcvErr:%u SndErr:%u\n", g_msgRcvCnt, g_msgSndCnt, g_msgRcvErrCnt, g_msgSndErrCnt);
232 assert(0);
233 }
lh9ed821d2023-04-07 01:36:19 -0700234 if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0))
235 {
xf.li742dd022023-06-08 01:43:32 -0700236 g_msgRcvErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700237 printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno);
238 continue;
239 }
xf.li742dd022023-06-08 01:43:32 -0700240 //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid);
241 pid_backup = rcvBuf.pid;
242 g_msgRcvCnt++; /* msgrcv success count */
lh9ed821d2023-04-07 01:36:19 -0700243
xf.libdd93d52023-05-12 07:10:14 -0700244 analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700245
xf.li742dd022023-06-08 01:43:32 -0700246 //printf("apnv nvserver_rpc msgsnd pid:%d, backup:%d\n", sndBuf.msgType, pid_backup);
lh9ed821d2023-04-07 01:36:19 -0700247 if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0))
248 {
xf.li742dd022023-06-08 01:43:32 -0700249 g_msgSndErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700250 printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno);
251 continue;
252 }
xf.li742dd022023-06-08 01:43:32 -0700253 g_msgSndCnt++; /* msgsnd success count */
254 if (sndBuf.msgType != pid_backup)
255 {
256 assert(0);
257 }
lh9ed821d2023-04-07 01:36:19 -0700258 }
259
xf.libdd93d52023-05-12 07:10:14 -0700260 free(rpc_data);
261 free(rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700262 return (0);
263}
264
265#ifdef __cplusplus
266}
267#endif