lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 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.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 21 | #include <assert.h> |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 22 | #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 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "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 | *******************************************************************************/ |
| 64 | static int g_rpc_fd = -1; |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 65 | static unsigned int g_msgRcvCnt = 0; |
| 66 | static unsigned int g_msgRcvErrCnt = 0; |
| 67 | static unsigned int g_msgSndCnt = 0; |
| 68 | static unsigned int g_msgSndErrCnt = 0; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 69 | |
| 70 | /******************************************************************************* |
| 71 | * ¾Ö²¿º¯ÊýʵÏÖ * |
| 72 | *******************************************************************************/ |
| 73 | /******************************************************************************* |
| 74 | * ¹¦ÄÜÃèÊö: analyMsg |
| 75 | * ²ÎÊý˵Ã÷: |
| 76 | * (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢ |
| 77 | * (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢ |
| 78 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 79 | * ÆäËü˵Ã÷: void |
| 80 | *******************************************************************************/ |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 81 | static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd, T_sc_rpc_msg *rpc_data, T_sc_rpc_msg *rpc_data2) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 82 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 83 | unsigned int timeout = 10; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 84 | int send_once_flag = 0; |
| 85 | int ret; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 86 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 87 | 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 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 103 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 104 | do |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 105 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 106 | if (send_once_flag == 0) |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 107 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 108 | 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.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 117 | } |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 118 | |
| 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.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 128 | assert(0); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 129 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 130 | memcpy(msgsnd, rpc_data2->data, rpc_data2->header.data_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 131 | } |
| 132 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 133 | static int nv_rpc_wait_ready(int fd) |
| 134 | { |
| 135 | T_sc_rpc_header *rpc_data; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 136 | unsigned int timeout = 100; // ms |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 137 | |
| 138 | rpc_data = malloc(sizeof(T_sc_rpc_header)); |
| 139 | if (rpc_data == NULL) |
| 140 | return -1; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 141 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 142 | while(1) |
| 143 | { |
| 144 | rpc_data->msg_type = RPC_MSG_TYPE_READY; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 145 | if (write(fd, rpc_data, sizeof(T_sc_rpc_header)) == sizeof(T_sc_rpc_header)) |
| 146 | break; |
| 147 | else |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 148 | usleep(timeout * 1000); |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | free(rpc_data); |
| 152 | return 0; |
| 153 | } |
| 154 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 155 | /******************************************************************************* |
| 156 | * È«¾Öº¯ÊýʵÏÖ * |
| 157 | *******************************************************************************/ |
| 158 | |
| 159 | /******************************************************************************* |
| 160 | * ¹¦ÄÜÃèÊö: main |
| 161 | * ²ÎÊý˵Ã÷: |
| 162 | * (´«Èë²ÎÊý) void |
| 163 | * (´«³ö²ÎÊý) void |
| 164 | * ·µ »Ø Öµ: void |
| 165 | * ÆäËü˵Ã÷: void |
| 166 | *******************************************************************************/ |
| 167 | int 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; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 173 | T_sc_rpc_msg *rpc_data; |
| 174 | T_sc_rpc_msg *rpc_data2; |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 175 | long pid_backup; /* app-libnvram pid backup */ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 176 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 177 | prctl(PR_SET_NAME, "nvserver_rpc", 0, 0, 0); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 178 | |
| 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 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 199 | rpc_data = malloc(sizeof(T_sc_rpc_msg)); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 200 | if (rpc_data == NULL) |
| 201 | { |
| 202 | perror("[error]nvrpc malloc error\n"); |
| 203 | return -2; |
| 204 | } |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 205 | rpc_data2 = malloc(sizeof(T_sc_rpc_msg)); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 206 | if (rpc_data2 == NULL) |
| 207 | { |
| 208 | perror("[error]nvrpc malloc2 error\n"); |
| 209 | free(rpc_data); |
| 210 | return -3; |
| 211 | } |
| 212 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 213 | if (nv_rpc_wait_ready(g_rpc_fd) < 0) |
| 214 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 215 | assert(0); |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 216 | } |
| 217 | sc_rpc_clear(g_rpc_fd); |
| 218 | printf("apnv and nv-rpc-daemon are ready both."); |
| 219 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 220 | // ÏûÏ¢½»»¥ |
| 221 | while (1) |
| 222 | { |
| 223 | memset(&rcvBuf, 0, sizeof(rcvBuf)); |
| 224 | memset(&sndBuf, 0, sizeof(sndBuf)); |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 225 | 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 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 230 | if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0)) |
| 231 | { |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 232 | g_msgRcvErrCnt++; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 233 | printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno); |
| 234 | continue; |
| 235 | } |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 236 | //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid); |
| 237 | pid_backup = rcvBuf.pid; |
| 238 | g_msgRcvCnt++; /* msgrcv success count */ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 239 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 240 | analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 241 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 242 | //printf("apnv nvserver_rpc msgsnd pid:%d, backup:%d\n", sndBuf.msgType, pid_backup); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 243 | if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0)) |
| 244 | { |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 245 | g_msgSndErrCnt++; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 246 | printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno); |
| 247 | continue; |
| 248 | } |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 249 | g_msgSndCnt++; /* msgsnd success count */ |
| 250 | if (sndBuf.msgType != pid_backup) |
| 251 | { |
| 252 | assert(0); |
| 253 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 254 | } |
| 255 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 256 | free(rpc_data); |
| 257 | free(rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 258 | return (0); |
| 259 | } |
| 260 | |
| 261 | #ifdef __cplusplus |
| 262 | } |
| 263 | #endif |