Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "user_log.h"
12 : #include "hccp_common.h"
13 : #include "hccp_async.h"
14 : #include "ra.h"
15 : #include "ra_hdc_async_socket.h"
16 : #include "ra_client_host.h"
17 :
18 5 : HCCP_ATTRI_VISI_DEF int RaSocketBatchConnectAsync(struct SocketConnectInfoT conn[], unsigned int num, void **reqHandle)
19 : {
20 5 : struct RaSocketHandle *socketHandle = NULL;
21 5 : char remoteIp[MAX_IP_LEN] = {0};
22 5 : char localIp[MAX_IP_LEN] = {0};
23 5 : unsigned int phyId = 0;
24 5 : unsigned int i = 0;
25 5 : int ret = 0;
26 :
27 5 : CHK_PRT_RETURN(conn == NULL || reqHandle == NULL,
28 : hccp_err("[batch_connect][ra_socket]conn or "
29 : "req_handle is NULL"),
30 : ConverReturnCode(SOCKET_OP, -EINVAL));
31 :
32 3 : CHK_PRT_RETURN(num == 0 || num > MAX_SOCKET_NUM,
33 : hccp_err("[batch_connect][ra_socket]num[%u] invalid, "
34 : "must in range of (0, %u]",
35 : num, MAX_SOCKET_NUM),
36 : ConverReturnCode(SOCKET_OP, -EINVAL));
37 :
38 3 : for (i = 0; i < num; i++) {
39 2 : socketHandle = (struct RaSocketHandle *)(conn[i].socketHandle);
40 2 : CHK_PRT_RETURN(socketHandle == NULL, hccp_err("[batch_connect][ra_socket]socket_handle is NULL"),
41 : ConverReturnCode(SOCKET_OP, -EINVAL));
42 :
43 1 : phyId = socketHandle->rdevInfo.phyId;
44 1 : CHK_PRT_RETURN(phyId >= RA_MAX_PHY_ID_NUM,
45 : hccp_err("[batch_connect][ra_socket]phyId[%u]invalid, "
46 : "must in range of [0, %u)",
47 : phyId, RA_MAX_PHY_ID_NUM),
48 : ConverReturnCode(SOCKET_OP, -EINVAL));
49 :
50 1 : CHK_PRT_RETURN(strlen(conn[i].tag) >= SOCK_CONN_TAG_SIZE,
51 : hccp_err("[batch_connect][ra_socket]conn tag len(%d) more than max len(%u)", strlen(conn[i].tag),
52 : SOCK_CONN_TAG_SIZE),
53 : ConverReturnCode(SOCKET_OP, -EINVAL));
54 :
55 1 : ret = RaInetPton(socketHandle->rdevInfo.family, socketHandle->rdevInfo.localIp, localIp, MAX_IP_LEN);
56 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[batch_connect][ra_socket]ra_inet_pton for local_ip failed, ret(%d)", ret),
57 : ConverReturnCode(SOCKET_OP, ret));
58 :
59 1 : ret = RaInetPton(socketHandle->rdevInfo.family, conn[i].remoteIp, remoteIp, MAX_IP_LEN);
60 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[batch_connect][ra_socket]ra_inet_pton for remote_ip failed, ret(%d)", ret),
61 : ConverReturnCode(SOCKET_OP, ret));
62 :
63 1 : hccp_run_info("Input parameters: [%u]th, phyId[%u], localIp[%s], remoteIp[%s], port[%u], tag[%s], cnt[%u]", i,
64 : socketHandle->rdevInfo.phyId, localIp, remoteIp, conn[i].port, conn[i].tag, socketHandle->connectCnt);
65 : }
66 :
67 1 : socketHandle->connectCnt++;
68 1 : ret = RaHdcSocketBatchConnectAsync(phyId, conn, num, reqHandle);
69 1 : return ConverReturnCode(SOCKET_OP, ret);
70 : }
71 :
72 5 : HCCP_ATTRI_VISI_DEF int RaSocketListenStartAsync(struct SocketListenInfoT conn[], unsigned int num, void **reqHandle)
73 : {
74 5 : struct RaSocketHandle *socketHandle = NULL;
75 5 : char localIp[MAX_IP_LEN] = {0};
76 5 : unsigned int phyId = 0;
77 5 : unsigned int i = 0;
78 5 : int ret = 0;
79 :
80 5 : CHK_PRT_RETURN(conn == NULL || reqHandle == NULL, hccp_err("[listen_start][ra_socket]conn or req_handle is NULL"),
81 : ConverReturnCode(SOCKET_OP, -EINVAL));
82 :
83 3 : CHK_PRT_RETURN(num == 0 || num > MAX_SOCKET_NUM,
84 : hccp_err("[listen_start][ra_socket]num[%u] invalid, "
85 : "must in range of (0, %u]",
86 : num, MAX_SOCKET_NUM),
87 : ConverReturnCode(SOCKET_OP, -EINVAL));
88 :
89 3 : for (i = 0; i < num; i++) {
90 2 : socketHandle = (struct RaSocketHandle *)(conn[i].socketHandle);
91 2 : CHK_PRT_RETURN(socketHandle == NULL, hccp_err("[listen_start][ra_socket]socket_handle is NULL"),
92 : ConverReturnCode(SOCKET_OP, -EINVAL));
93 :
94 1 : phyId = socketHandle->rdevInfo.phyId;
95 1 : CHK_PRT_RETURN(phyId >= RA_MAX_PHY_ID_NUM,
96 : hccp_err("[listen_start][ra_socket]phyId[%u]invalid, "
97 : "must in range of [0, %u)",
98 : phyId, RA_MAX_PHY_ID_NUM),
99 : ConverReturnCode(SOCKET_OP, -EINVAL));
100 :
101 1 : ret = RaInetPton(socketHandle->rdevInfo.family, socketHandle->rdevInfo.localIp, localIp, MAX_IP_LEN);
102 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[listen_start][ra_socket]ra_inet_pton for server_ip failed, ret(%d)", ret),
103 : ConverReturnCode(SOCKET_OP, ret));
104 :
105 1 : hccp_run_info("Input parameters: [%u]th, phyId[%u], localIp[%s], port[%u]", i, phyId, localIp, conn[i].port);
106 : }
107 :
108 1 : ret = RaHdcSocketListenStartAsync(phyId, conn, num, reqHandle);
109 1 : return ConverReturnCode(SOCKET_OP, ret);
110 : }
111 :
112 5 : HCCP_ATTRI_VISI_DEF int RaSocketListenStopAsync(struct SocketListenInfoT conn[], unsigned int num, void **reqHandle)
113 : {
114 5 : struct RaSocketHandle *socketHandle = NULL;
115 5 : char localIp[MAX_IP_LEN] = {0};
116 5 : unsigned int phyId = 0;
117 5 : unsigned int i = 0;
118 5 : int ret = 0;
119 :
120 5 : CHK_PRT_RETURN(conn == NULL || reqHandle == NULL, hccp_err("[listen_stop][ra_socket]conn or req_handle is NULL"),
121 : ConverReturnCode(SOCKET_OP, -EINVAL));
122 :
123 3 : CHK_PRT_RETURN(num == 0 || num > MAX_SOCKET_NUM,
124 : hccp_err("[listen_stop][ra_socket]num[%u] invalid, "
125 : "must in range of (0, %u]",
126 : num, MAX_SOCKET_NUM),
127 : ConverReturnCode(SOCKET_OP, -EINVAL));
128 :
129 3 : for (i = 0; i < num; i++) {
130 2 : socketHandle = (struct RaSocketHandle *)(conn[i].socketHandle);
131 2 : CHK_PRT_RETURN(socketHandle == NULL, hccp_err("[listen_stop][ra_socket]socket_handle is NULL"),
132 : ConverReturnCode(SOCKET_OP, -EINVAL));
133 :
134 1 : phyId = socketHandle->rdevInfo.phyId;
135 1 : CHK_PRT_RETURN(phyId >= RA_MAX_PHY_ID_NUM,
136 : hccp_err("[listen_stop][ra_socket]phyId[%u]invalid, "
137 : "must in range of [0, %u)",
138 : phyId, RA_MAX_PHY_ID_NUM),
139 : ConverReturnCode(SOCKET_OP, -EINVAL));
140 :
141 1 : ret = RaInetPton(socketHandle->rdevInfo.family, socketHandle->rdevInfo.localIp, localIp, MAX_IP_LEN);
142 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[listen_stop][ra_socket]ra_inet_pton for server_ip failed, ret(%d)", ret),
143 : ConverReturnCode(SOCKET_OP, ret));
144 :
145 1 : hccp_run_info("Input parameters: [%u]th, phyId[%u], localIp[%s]", i, phyId, localIp);
146 : }
147 :
148 1 : ret = RaHdcSocketListenStopAsync(phyId, conn, num, reqHandle);
149 1 : return ConverReturnCode(SOCKET_OP, ret);
150 : }
151 :
152 5 : HCCP_ATTRI_VISI_DEF int RaSocketBatchCloseAsync(struct SocketCloseInfoT conn[], unsigned int num, void **reqHandle)
153 : {
154 5 : struct RaSocketHandle *socketHandle = NULL;
155 5 : char localIp[MAX_IP_LEN] = {0};
156 5 : unsigned int phyId = 0;
157 5 : unsigned int i = 0;
158 5 : int ret = 0;
159 :
160 5 : CHK_PRT_RETURN(conn == NULL || reqHandle == NULL, hccp_err("[batch_close][ra_socket]conn or req_handle is NULL"),
161 : ConverReturnCode(SOCKET_OP, -EINVAL));
162 :
163 3 : CHK_PRT_RETURN(num == 0 || num > MAX_SOCKET_NUM,
164 : hccp_err("[batch_close][ra_socket]num[%u] invalid, "
165 : "must in range of (0, %u]",
166 : num, MAX_SOCKET_NUM),
167 : ConverReturnCode(SOCKET_OP, -EINVAL));
168 :
169 3 : for (i = 0; i < num; i++) {
170 2 : socketHandle = (struct RaSocketHandle *)(conn[i].socketHandle);
171 2 : CHK_PRT_RETURN(socketHandle == NULL, hccp_err("[batch_close][ra_socket]socket_handle is NULL"),
172 : ConverReturnCode(SOCKET_OP, -EINVAL));
173 :
174 1 : phyId = socketHandle->rdevInfo.phyId;
175 1 : CHK_PRT_RETURN(phyId >= RA_MAX_PHY_ID_NUM,
176 : hccp_err("[batch_close][ra_socket]phyId[%u]invalid, "
177 : "must in range of [0, %u)",
178 : phyId, RA_MAX_PHY_ID_NUM),
179 : ConverReturnCode(SOCKET_OP, -EINVAL));
180 :
181 1 : ret = RaInetPton(socketHandle->rdevInfo.family, socketHandle->rdevInfo.localIp, localIp, MAX_IP_LEN);
182 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[batch_close][ra_socket]ra_inet_pton for local_ip failed, ret(%d)", ret),
183 : ConverReturnCode(SOCKET_OP, ret));
184 :
185 1 : hccp_run_info("Input parameters: [%u]th, phyId[%u], localIp[%s], cnt[%u]", i, phyId, localIp,
186 : socketHandle->closeCnt);
187 : }
188 :
189 1 : socketHandle->closeCnt++;
190 1 : ret = RaHdcSocketBatchCloseAsync(phyId, conn, num, reqHandle);
191 1 : return ConverReturnCode(SOCKET_OP, ret);
192 : }
193 :
194 1 : HCCP_ATTRI_VISI_DEF int RaSocketSendAsync(const void *fdHandle, const void *data, unsigned long long size,
195 : unsigned long long *sentSize, void **reqHandle)
196 : {
197 1 : int ret = 0;
198 :
199 1 : CHK_PRT_RETURN(fdHandle == NULL || data == NULL || sentSize == NULL || size == 0 || reqHandle == NULL,
200 : hccp_err("[send][ra_socket]fd_handle or data or sent_size or req_handle is NULL or size[%llu] is 0", size),
201 : ConverReturnCode(SOCKET_OP, -EINVAL));
202 :
203 1 : ret = RaHdcSocketSendAsync((const struct SocketHdcInfo *)fdHandle, data, size, sentSize, reqHandle);
204 1 : return ConverReturnCode(SOCKET_OP, ret);
205 : }
206 :
207 1 : HCCP_ATTRI_VISI_DEF int RaSocketRecvAsync(const void *fdHandle, void *data, unsigned long long size,
208 : unsigned long long *receivedSize, void **reqHandle)
209 : {
210 1 : int ret = 0;
211 :
212 1 : CHK_PRT_RETURN(fdHandle == NULL || data == NULL || receivedSize == NULL || size == 0 || reqHandle == NULL,
213 : hccp_err("[recv][ra_socket]fd_handle or data or received_size or req_handle is NULL or size[%llu] is 0", size),
214 : ConverReturnCode(SOCKET_OP, -EINVAL));
215 :
216 1 : ret = RaHdcSocketRecvAsync((const struct SocketHdcInfo *)fdHandle, data, size, receivedSize, reqHandle);
217 1 : return ConverReturnCode(SOCKET_OP, ret);
218 : }
|