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 | #include <sys/ioctl.h> |
| 29 | #include <unistd.h> |
| 30 | #include "sc_rpc.h" |
| 31 | #include "zxicbasic_api.h" |
| 32 | #include "rpmsg_zx29.h" |
| 33 | |
| 34 | /******************************************************************************* |
| 35 | * 常量定义 * |
| 36 | *******************************************************************************/ |
| 37 | |
| 38 | /******************************************************************************* |
| 39 | * 宏定义 * |
| 40 | *******************************************************************************/ |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 41 | #define RPC_CHN_MAX_BUF_LEN 18432 |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 42 | |
| 43 | /******************************************************************************* |
| 44 | * 数据类型定义 * |
| 45 | *******************************************************************************/ |
| 46 | |
| 47 | /******************************************************************************* |
| 48 | * 局部函数声明 * |
| 49 | *******************************************************************************/ |
| 50 | |
| 51 | /******************************************************************************* |
| 52 | * 局部静态变量定义 * |
| 53 | *******************************************************************************/ |
| 54 | |
| 55 | /******************************************************************************* |
| 56 | * 全局变量定义 * |
| 57 | *******************************************************************************/ |
| 58 | |
| 59 | /******************************************************************************* |
| 60 | * 局部函数实现 * |
| 61 | *******************************************************************************/ |
| 62 | |
| 63 | /******************************************************************************* |
| 64 | * 全局函数实现 * |
| 65 | *******************************************************************************/ |
| 66 | |
| 67 | int sc_rpc_open(const char *rpmsg_dev) |
| 68 | { |
| 69 | int fd; |
| 70 | fd = open(rpmsg_dev, O_RDWR); |
| 71 | |
| 72 | if (fd < 0) |
| 73 | { |
| 74 | perror("[rpc] open device fail\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | if (ioctl(fd, RPMSG_CREATE_CHANNEL, RPC_CHN_MAX_BUF_LEN) < 0) |
| 79 | { |
| 80 | perror("[rpc] RPMSG_CREATE_CHANNEL error\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | // 发送消息时,触发中断 |
| 85 | if (ioctl(fd, RPMSG_SET_INT_FLAG, NULL) < 0) |
| 86 | { |
| 87 | perror("[rpc] RPMSG_SET_INT_FLAG error\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | // 阻塞的方式读 |
| 92 | if (ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL) < 0) |
| 93 | { |
| 94 | perror("[rpc] RPMSG_CLEAR_POLL_FLAG error\n"); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return fd; |
| 99 | } |
| 100 | |
| 101 | int sc_rpc_clear(int fd) |
| 102 | { |
| 103 | char tmp_buf[1024]; |
| 104 | int ret = -1; |
| 105 | |
| 106 | if (ioctl(fd, RPMSG_SET_POLL_FLAG, NULL) < 0) |
| 107 | { |
| 108 | perror("[rpc] RPMSG_SET_POLL_FLAG error\n"); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | while (1) |
| 113 | { |
| 114 | ret = safe_read(fd, tmp_buf, sizeof(tmp_buf)); |
| 115 | if (ret == 0) |
| 116 | { |
| 117 | break; /* channel clear */ |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | printf("[rpc] UNBLOCK read len = %d!\n", ret); |
| 122 | } |
| 123 | } |
| 124 | if (ioctl(fd, RPMSG_CLEAR_POLL_FLAG, NULL) < 0) |
| 125 | { |
| 126 | perror("[rpc] RPMSG_CLEAR_POLL_FLAG error\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 133 | int sc_rpc_send(int fd, T_sc_rpc_msg *data, unsigned int flag) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 134 | { |
| 135 | ssize_t ret; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 136 | T_sc_rpc_header *header = (T_sc_rpc_header *)data; |
| 137 | |
| 138 | ret = full_write(fd, data, sizeof(T_sc_rpc_header) + header->data_len); |
| 139 | if (ret != sizeof(T_sc_rpc_header) + header->data_len) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 140 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 141 | printf("[rpc] write error, len=%d != %d!\n", ret, (int)sizeof(T_sc_rpc_header)+ header->data_len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 148 | int sc_rpc_recv(int fd, T_sc_rpc_msg *data, unsigned int timeout) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 149 | { |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 150 | #if 1 |
| 151 | fd_set readfds; |
| 152 | ssize_t len; |
| 153 | int ret; |
| 154 | struct timeval timeout_t = {timeout, 0}; |
| 155 | int errNo = 0; |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 156 | T_sc_rpc_header *header = 0; |
| 157 | |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 158 | FD_ZERO(&readfds); |
| 159 | FD_SET(fd, &readfds); |
| 160 | |
| 161 | //ret = select(fd + 1, &readfds, NULL, NULL, &timeout_t); |
| 162 | do { |
| 163 | ret = select(fd + 1, &readfds, NULL, NULL, &timeout_t); |
| 164 | } while (ret < 0 && errno == EINTR); |
| 165 | |
| 166 | if (ret == 0) |
| 167 | { |
| 168 | printf("[error] nvrpc select timeout!\n"); |
| 169 | return -1; |
| 170 | } |
| 171 | else if(ret < 0) |
| 172 | { |
| 173 | printf("[error] nvrpc ret:%d\n", ret); |
| 174 | return -2; |
| 175 | } |
| 176 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 177 | len = safe_read(fd, data, sizeof(T_sc_rpc_msg)); |
| 178 | header = (T_sc_rpc_header *)data; |
| 179 | if (len != sizeof(T_sc_rpc_header)+ header->data_len) |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 180 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 181 | printf("[nvrpc] read error, len=%d != %d!\n", len, (int)sizeof(T_sc_rpc_header)+ header->data_len); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 182 | return -3; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | #else |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 187 | ssize_t ret; |
| 188 | |
| 189 | ret = full_read(fd, data, sizeof(T_sc_rpc_header)); |
| 190 | if (ret != sizeof(T_sc_rpc_header)) |
| 191 | { |
| 192 | printf("[rpc] read error, len=%d != %d!\n", ret, (int)sizeof(T_sc_rpc_header)); |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | return 0; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 197 | #endif |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | int sc_rpc_close(int fd) |
| 201 | { |
| 202 | return close(fd); |
| 203 | } |