blob: 7e4d01251a7073a3a834e2acd7c642fc75a255aa [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
lh9ed821d2023-04-07 01:36:19 -070035#ifdef __cplusplus
36extern "C"
37{
38#endif
39
40/*******************************************************************************
41* ³£Á¿¶¨Òå *
42*******************************************************************************/
43
44/*******************************************************************************
45* ºê¶¨Òå *
46*******************************************************************************/
47#define RPC_RPMSG_DEV "/dev/rpmsg8"
48
49/*******************************************************************************
50* Êý¾ÝÀàÐͶ¨Òå *
51*******************************************************************************/
52
53/*******************************************************************************
54* ¾Ö²¿º¯ÊýÉùÃ÷ *
55*******************************************************************************/
56
57/*******************************************************************************
58* ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå *
59*******************************************************************************/
60
61/*******************************************************************************
62* È«¾Ö±äÁ¿¶¨Òå *
63*******************************************************************************/
64static int g_rpc_fd = -1;
xf.li742dd022023-06-08 01:43:32 -070065static unsigned int g_msgRcvCnt = 0;
66static unsigned int g_msgRcvErrCnt = 0;
67static unsigned int g_msgSndCnt = 0;
68static unsigned int g_msgSndErrCnt = 0;
lh9ed821d2023-04-07 01:36:19 -070069
70/*******************************************************************************
71* ¾Ö²¿º¯ÊýʵÏÖ *
72*******************************************************************************/
73/*******************************************************************************
74* ¹¦ÄÜÃèÊö: analyMsg
75* ²ÎÊý˵Ã÷:
76* (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢
77* (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢
78* ·µ »Ø Öµ: 0±íʾ³É¹¦
79* ÆäËü˵Ã÷: void
80*******************************************************************************/
lh758261d2023-07-13 05:52:04 -070081static 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 -070082{
xf.libdd93d52023-05-12 07:10:14 -070083 unsigned int timeout = 10;
lh758261d2023-07-13 05:52:04 -070084 int send_once_flag = 0;
85 int ret;
lh9ed821d2023-04-07 01:36:19 -070086
lh758261d2023-07-13 05:52:04 -070087 rpc_data->header.msg_type = RPC_MSG_TYPE_REQUEST;
88 rpc_data->header.func_id = RPC_FUNC_ID_NV;
89 rpc_data->header.data_len = sizeof(T_NV_MSG_INFO) - NV_MAX_FILE_LEN - NV_MAX_KEY_LEN - NV_MAX_VAL_LEN;
90 rpc_data->header.file_len = strlen(msgrecv->file) + 1;
91 rpc_data->header.data_len += rpc_data->header.file_len;
92 memcpy(rpc_data->data, msgrecv, rpc_data->header.data_len);
93 rpc_data->header.key_len = strlen(msgrecv->key) + 1;
94 memcpy(rpc_data->data+rpc_data->header.data_len, msgrecv->key, rpc_data->header.key_len);
95 rpc_data->header.data_len += rpc_data->header.key_len;
96 rpc_data->header.value_len = strlen(msgrecv->value);
97 if (rpc_data->header.value_len > 0)
98 {
99 rpc_data->header.value_len += 1;
100 memcpy(rpc_data->data+rpc_data->header.data_len, msgrecv->value, rpc_data->header.value_len);
101 rpc_data->header.data_len += rpc_data->header.value_len;
102 }
lh9ed821d2023-04-07 01:36:19 -0700103
xf.libdd93d52023-05-12 07:10:14 -0700104 do
lh9ed821d2023-04-07 01:36:19 -0700105 {
lh758261d2023-07-13 05:52:04 -0700106 if (send_once_flag == 0)
xf.libdd93d52023-05-12 07:10:14 -0700107 {
lh758261d2023-07-13 05:52:04 -0700108 ret = sc_rpc_send(g_rpc_fd, rpc_data, 0);
109 if (ret == 0)
110 {
111 send_once_flag = 1; // send success
112 }
113 else
114 {
115 assert(0); // send fail
116 }
xf.libdd93d52023-05-12 07:10:14 -0700117 }
lh758261d2023-07-13 05:52:04 -0700118
119 ret = sc_rpc_recv(g_rpc_fd, rpc_data2, timeout);
120 if (ret == 0)
121 {
122 break; // recv success and break
123 }
124
125 } while (1); //µÈ´ýÍê³É
126
127 if (rpc_data2->header.msg_type != RPC_MSG_TYPE_REPLY)
xf.li742dd022023-06-08 01:43:32 -0700128 assert(0);
lh9ed821d2023-04-07 01:36:19 -0700129
lh758261d2023-07-13 05:52:04 -0700130 memcpy(msgsnd, rpc_data2->data, rpc_data2->header.data_len);
lh9ed821d2023-04-07 01:36:19 -0700131}
132
xf.li742dd022023-06-08 01:43:32 -0700133static int nv_rpc_wait_ready(int fd)
134{
135 T_sc_rpc_header *rpc_data;
lh758261d2023-07-13 05:52:04 -0700136 unsigned int timeout = 100; // ms
xf.li742dd022023-06-08 01:43:32 -0700137
138 rpc_data = malloc(sizeof(T_sc_rpc_header));
139 if (rpc_data == NULL)
140 return -1;
lh758261d2023-07-13 05:52:04 -0700141
xf.li742dd022023-06-08 01:43:32 -0700142 while(1)
143 {
144 rpc_data->msg_type = RPC_MSG_TYPE_READY;
lh758261d2023-07-13 05:52:04 -0700145 if (write(fd, rpc_data, sizeof(T_sc_rpc_header)) == sizeof(T_sc_rpc_header))
146 break;
147 else
xf.li742dd022023-06-08 01:43:32 -0700148 usleep(timeout * 1000);
xf.li742dd022023-06-08 01:43:32 -0700149 }
150
151 free(rpc_data);
152 return 0;
153}
154
lh9ed821d2023-04-07 01:36:19 -0700155/*******************************************************************************
156* È«¾Öº¯ÊýʵÏÖ *
157*******************************************************************************/
158
159/*******************************************************************************
160* ¹¦ÄÜÃèÊö: main
161* ²ÎÊý˵Ã÷:
162* (´«Èë²ÎÊý) void
163* (´«³ö²ÎÊý) void
164* ·µ »Ø Öµ: void
165* ÆäËü˵Ã÷: void
166*******************************************************************************/
167int nvserver_main(int argc, char *argv[])
168{
169 int msgId = 0;
170 T_NV_MSG_INFO rcvBuf;
171 T_NV_MSG_RESULT sndBuf;
172 struct msqid_ds msgInfo;
lh758261d2023-07-13 05:52:04 -0700173 T_sc_rpc_msg *rpc_data;
174 T_sc_rpc_msg *rpc_data2;
xf.li742dd022023-06-08 01:43:32 -0700175 long pid_backup; /* app-libnvram pid backup */
lh9ed821d2023-04-07 01:36:19 -0700176
lh758261d2023-07-13 05:52:04 -0700177 prctl(PR_SET_NAME, "nvserver_rpc", 0, 0, 0);
lh9ed821d2023-04-07 01:36:19 -0700178
179 memset(&msgInfo, 0, sizeof(msgInfo));
180
181 g_rpc_fd = sc_rpc_open(RPC_RPMSG_DEV);
182
183 msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600);
184 if (-1 == msgId)
185 {
186 printf("nvserver error: msgget msgId fail, errno = %d\n", errno);
187 return -1;
188 }
189
190 if (-1 != msgctl(msgId, IPC_STAT, &msgInfo))
191 {
192 msgInfo.msg_qbytes = 262144; // 256k
193 if (-1 == msgctl(msgId, IPC_SET, &msgInfo))
194 {
195 printf("nvserver error: msgctl msgId fail, errno = %d\n", errno);
196 }
197 }
198
lh758261d2023-07-13 05:52:04 -0700199 rpc_data = malloc(sizeof(T_sc_rpc_msg));
xf.libdd93d52023-05-12 07:10:14 -0700200 if (rpc_data == NULL)
201 {
202 perror("[error]nvrpc malloc error\n");
203 return -2;
204 }
lh758261d2023-07-13 05:52:04 -0700205 rpc_data2 = malloc(sizeof(T_sc_rpc_msg));
xf.libdd93d52023-05-12 07:10:14 -0700206 if (rpc_data2 == NULL)
207 {
208 perror("[error]nvrpc malloc2 error\n");
209 free(rpc_data);
210 return -3;
211 }
212
xf.li742dd022023-06-08 01:43:32 -0700213 if (nv_rpc_wait_ready(g_rpc_fd) < 0)
214 {
lh758261d2023-07-13 05:52:04 -0700215 assert(0);
xf.li742dd022023-06-08 01:43:32 -0700216 }
217 sc_rpc_clear(g_rpc_fd);
218 printf("apnv and nv-rpc-daemon are ready both.");
219
lh9ed821d2023-04-07 01:36:19 -0700220 // ÏûÏ¢½»»¥
221 while (1)
222 {
223 memset(&rcvBuf, 0, sizeof(rcvBuf));
224 memset(&sndBuf, 0, sizeof(sndBuf));
xf.li742dd022023-06-08 01:43:32 -0700225 if (g_msgRcvCnt != g_msgSndCnt)
226 {
227 printf("[nvserver_rpc]Rcv:%u Snd:%u RcvErr:%u SndErr:%u\n", g_msgRcvCnt, g_msgSndCnt, g_msgRcvErrCnt, g_msgSndErrCnt);
228 assert(0);
229 }
lh9ed821d2023-04-07 01:36:19 -0700230 if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0))
231 {
xf.li742dd022023-06-08 01:43:32 -0700232 g_msgRcvErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700233 printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno);
234 continue;
235 }
xf.li742dd022023-06-08 01:43:32 -0700236 //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid);
237 pid_backup = rcvBuf.pid;
238 g_msgRcvCnt++; /* msgrcv success count */
lh9ed821d2023-04-07 01:36:19 -0700239
xf.libdd93d52023-05-12 07:10:14 -0700240 analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700241
xf.li742dd022023-06-08 01:43:32 -0700242 //printf("apnv nvserver_rpc msgsnd pid:%d, backup:%d\n", sndBuf.msgType, pid_backup);
lh9ed821d2023-04-07 01:36:19 -0700243 if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0))
244 {
xf.li742dd022023-06-08 01:43:32 -0700245 g_msgSndErrCnt++;
lh9ed821d2023-04-07 01:36:19 -0700246 printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno);
247 continue;
248 }
xf.li742dd022023-06-08 01:43:32 -0700249 g_msgSndCnt++; /* msgsnd success count */
250 if (sndBuf.msgType != pid_backup)
251 {
252 assert(0);
253 }
lh9ed821d2023-04-07 01:36:19 -0700254 }
255
xf.libdd93d52023-05-12 07:10:14 -0700256 free(rpc_data);
257 free(rpc_data2);
lh9ed821d2023-04-07 01:36:19 -0700258 return (0);
259}
260
261#ifdef __cplusplus
262}
263#endif