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> |
| 21 | #include <dirent.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/file.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/ipc.h> |
| 27 | #include <sys/msg.h> |
| 28 | |
| 29 | #include "nvserver.h" |
| 30 | #include "nv_typedef.h" |
| 31 | #include <message.h> |
| 32 | #include "sc_rpc.h" |
| 33 | |
| 34 | #ifdef FOTA_AB |
| 35 | #include "zxic_fota_ab_upgrade.h" |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" |
| 40 | { |
| 41 | #endif |
| 42 | |
| 43 | /******************************************************************************* |
| 44 | * ³£Á¿¶¨Òå * |
| 45 | *******************************************************************************/ |
| 46 | |
| 47 | /******************************************************************************* |
| 48 | * ºê¶¨Òå * |
| 49 | *******************************************************************************/ |
| 50 | #define RPC_RPMSG_DEV "/dev/rpmsg8" |
| 51 | |
| 52 | /******************************************************************************* |
| 53 | * Êý¾ÝÀàÐͶ¨Òå * |
| 54 | *******************************************************************************/ |
| 55 | |
| 56 | /******************************************************************************* |
| 57 | * ¾Ö²¿º¯ÊýÉùÃ÷ * |
| 58 | *******************************************************************************/ |
| 59 | |
| 60 | /******************************************************************************* |
| 61 | * ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå * |
| 62 | *******************************************************************************/ |
| 63 | |
| 64 | /******************************************************************************* |
| 65 | * È«¾Ö±äÁ¿¶¨Òå * |
| 66 | *******************************************************************************/ |
| 67 | static int g_rpc_fd = -1; |
| 68 | |
| 69 | /******************************************************************************* |
| 70 | * ¾Ö²¿º¯ÊýʵÏÖ * |
| 71 | *******************************************************************************/ |
| 72 | /******************************************************************************* |
| 73 | * ¹¦ÄÜÃèÊö: analyMsg |
| 74 | * ²ÎÊý˵Ã÷: |
| 75 | * (´«Èë²ÎÊý) msgrecv:½ÓÊÕÏûÏ¢ |
| 76 | * (´«³ö²ÎÊý) msgsnd:·¢ËÍÏûÏ¢ |
| 77 | * ·µ »Ø Öµ: 0±íʾ³É¹¦ |
| 78 | * ÆäËü˵Ã÷: void |
| 79 | *******************************************************************************/ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 80 | 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] | 81 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 82 | unsigned int timeout = 10; |
| 83 | int try_cnt = 0; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 84 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 85 | memset(rpc_data, 0, sizeof(T_sc_rpc_header)); |
| 86 | memset(rpc_data2, 0, sizeof(T_sc_rpc_header)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 87 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 88 | rpc_data->msg_type = RPC_MSG_TYPE_REQUEST; |
| 89 | rpc_data->func_id = RPC_FUNC_ID_NV; |
| 90 | rpc_data->data_len = sizeof(T_NV_MSG_INFO); |
| 91 | memcpy(rpc_data->data, msgrecv, sizeof(T_NV_MSG_INFO)); |
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 | do |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 94 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 95 | if (try_cnt > 0) |
| 96 | { |
| 97 | sc_rpc_clear(g_rpc_fd); |
| 98 | } |
| 99 | sc_rpc_send(g_rpc_fd, rpc_data, 0); |
| 100 | try_cnt++; |
| 101 | //sleep(1); |
| 102 | //continue; |
| 103 | } while (0 != sc_rpc_recv(g_rpc_fd, rpc_data2, timeout)); // µÈ´ýCAPºË´´½¨Í¨µÀ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 104 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 105 | memcpy(msgsnd, rpc_data2->data, sizeof(T_NV_MSG_RESULT)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /******************************************************************************* |
| 109 | * È«¾Öº¯ÊýʵÏÖ * |
| 110 | *******************************************************************************/ |
| 111 | |
| 112 | /******************************************************************************* |
| 113 | * ¹¦ÄÜÃèÊö: main |
| 114 | * ²ÎÊý˵Ã÷: |
| 115 | * (´«Èë²ÎÊý) void |
| 116 | * (´«³ö²ÎÊý) void |
| 117 | * ·µ »Ø Öµ: void |
| 118 | * ÆäËü˵Ã÷: void |
| 119 | *******************************************************************************/ |
| 120 | int nvserver_main(int argc, char *argv[]) |
| 121 | { |
| 122 | int msgId = 0; |
| 123 | T_NV_MSG_INFO rcvBuf; |
| 124 | T_NV_MSG_RESULT sndBuf; |
| 125 | struct msqid_ds msgInfo; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 126 | T_sc_rpc_header *rpc_data; |
| 127 | T_sc_rpc_header *rpc_data2; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 128 | |
| 129 | prctl(PR_SET_NAME, "nvserver", 0, 0, 0); |
| 130 | |
| 131 | memset(&msgInfo, 0, sizeof(msgInfo)); |
| 132 | |
| 133 | g_rpc_fd = sc_rpc_open(RPC_RPMSG_DEV); |
| 134 | |
| 135 | msgId = msgget(MODULE_ID_NV, IPC_CREAT | 0600); |
| 136 | if (-1 == msgId) |
| 137 | { |
| 138 | printf("nvserver error: msgget msgId fail, errno = %d\n", errno); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (-1 != msgctl(msgId, IPC_STAT, &msgInfo)) |
| 143 | { |
| 144 | msgInfo.msg_qbytes = 262144; // 256k |
| 145 | if (-1 == msgctl(msgId, IPC_SET, &msgInfo)) |
| 146 | { |
| 147 | printf("nvserver error: msgctl msgId fail, errno = %d\n", errno); |
| 148 | } |
| 149 | } |
| 150 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 151 | rpc_data = malloc(sizeof(T_sc_rpc_header)); |
| 152 | if (rpc_data == NULL) |
| 153 | { |
| 154 | perror("[error]nvrpc malloc error\n"); |
| 155 | return -2; |
| 156 | } |
| 157 | rpc_data2 = malloc(sizeof(T_sc_rpc_header)); |
| 158 | if (rpc_data2 == NULL) |
| 159 | { |
| 160 | perror("[error]nvrpc malloc2 error\n"); |
| 161 | free(rpc_data); |
| 162 | return -3; |
| 163 | } |
| 164 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 165 | // ÏûÏ¢½»»¥ |
| 166 | while (1) |
| 167 | { |
| 168 | memset(&rcvBuf, 0, sizeof(rcvBuf)); |
| 169 | memset(&sndBuf, 0, sizeof(sndBuf)); |
| 170 | if (-1 == msgrcv(msgId, &rcvBuf, sizeof(T_NV_MSG_INFO) - sizeof(long), MSG_TYPE_NV, 0)) |
| 171 | { |
| 172 | printf("nvserver error: nvserver msgrcv fail, errno = %d!\n", errno); |
| 173 | continue; |
| 174 | } |
| 175 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 176 | analyMsg(&rcvBuf, &sndBuf, rpc_data, rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 177 | |
| 178 | if (-1 == msgsnd(msgId, &sndBuf, sizeof(T_NV_MSG_RESULT) - sizeof(long), 0)) |
| 179 | { |
| 180 | printf("nvserver error: nvserver msgsnd fail, errno = %d!\n", errno); |
| 181 | continue; |
| 182 | } |
| 183 | } |
| 184 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 185 | free(rpc_data); |
| 186 | free(rpc_data2); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 187 | return (0); |
| 188 | } |
| 189 | |
| 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |