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 | *******************************************************************************/ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 85 | static void analyMsg(T_NV_MSG_INFO *msgrecv, T_NV_MSG_RESULT *msgsnd, T_sc_rpc_header *rpc_data, T_sc_rpc_header *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; |
| 88 | int try_cnt = 0; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 89 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 90 | memset(rpc_data, 0, sizeof(T_sc_rpc_header)); |
| 91 | memset(rpc_data2, 0, sizeof(T_sc_rpc_header)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 92 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 93 | 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)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 97 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 98 | do |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 99 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 100 | 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.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 108 | } while (0 != sc_rpc_recv(g_rpc_fd, rpc_data2, timeout)); //µÈ´ýÍê³É |
| 109 | if (rpc_data2->msg_type != RPC_MSG_TYPE_REPLY) |
| 110 | assert(0); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 111 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 112 | memcpy(msgsnd, rpc_data2->data, sizeof(T_NV_MSG_RESULT)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 113 | } |
| 114 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 115 | static 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 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 144 | /******************************************************************************* |
| 145 | * È«¾Öº¯ÊýʵÏÖ * |
| 146 | *******************************************************************************/ |
| 147 | |
| 148 | /******************************************************************************* |
| 149 | * ¹¦ÄÜÃèÊö: main |
| 150 | * ²ÎÊý˵Ã÷: |
| 151 | * (´«Èë²ÎÊý) void |
| 152 | * (´«³ö²ÎÊý) void |
| 153 | * ·µ »Ø Öµ: void |
| 154 | * ÆäËü˵Ã÷: void |
| 155 | *******************************************************************************/ |
| 156 | int 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.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 162 | T_sc_rpc_header *rpc_data; |
| 163 | T_sc_rpc_header *rpc_data2; |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 164 | long pid_backup; /* app-libnvram pid backup */ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 165 | |
| 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.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 188 | 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.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 202 | 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 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 209 | // ÏûÏ¢½»»¥ |
| 210 | while (1) |
| 211 | { |
| 212 | memset(&rcvBuf, 0, sizeof(rcvBuf)); |
| 213 | memset(&sndBuf, 0, sizeof(sndBuf)); |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 214 | 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 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 219 | if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0)) |
| 220 | { |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 221 | g_msgRcvErrCnt++; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 222 | printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno); |
| 223 | continue; |
| 224 | } |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 225 | //printf("apnv nvserver_rpc msgrcv pid:%d\n", rcvBuf.pid); |
| 226 | pid_backup = rcvBuf.pid; |
| 227 | g_msgRcvCnt++; /* msgrcv success count */ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 228 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 229 | analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 230 | |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 231 | //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] | 232 | if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0)) |
| 233 | { |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 234 | g_msgSndErrCnt++; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 235 | printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno); |
| 236 | continue; |
| 237 | } |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame] | 238 | g_msgSndCnt++; /* msgsnd success count */ |
| 239 | if (sndBuf.msgType != pid_backup) |
| 240 | { |
| 241 | assert(0); |
| 242 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 243 | } |
| 244 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 245 | free(rpc_data); |
| 246 | free(rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 247 | return (0); |
| 248 | } |
| 249 | |
| 250 | #ifdef __cplusplus |
| 251 | } |
| 252 | #endif |