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