blob: 2ecef9df10a254f5e5c4b6f9d8da1ed71b19c5a9 [file] [log] [blame]
b.liu9fc00602023-09-24 13:16:50 +08001#include <stdio.h>
2#include <stdlib.h>
b.liu9fc00602023-09-24 13:16:50 +08003#include <unistd.h>
b.liueb040652023-09-25 18:50:56 +08004#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <netinet/in.h>
8#include <pthread.h>
9#include <sys/epoll.h>
10#include <string.h>
11#include <fcntl.h>
12#include <signal.h>
13#include <netinet/tcp.h>
b.liu9fc00602023-09-24 13:16:50 +080014
15#include "mbtk_log.h"
16#include "mbtk_tcpip.h"
b.liueb040652023-09-25 18:50:56 +080017#include "mbtk_net_control.h"
18
19#define MBTK_TCPIP_CID_DEFAULT 0
20#define MBTK_TCPIP_READ_BUFF_SIZE 2048
21#define TCPIP_DEBUG 0
b.liu9fc00602023-09-24 13:16:50 +080022
23typedef struct {
b.liueb040652023-09-25 18:50:56 +080024 mbtk_sock_session sock_fd;
b.liu9fc00602023-09-24 13:16:50 +080025 char ser_addr[256];
26 int ser_port;
27 char local_addr[256];
28 int local_port;
29 bool ack_support;
30 bool ssl_support;
31 bool ignore_cert;
32 uint32 heartbeat_time;
33 uint32 delay_time;
34
35 mbtk_tcpip_read_callback_func read_cb;
36
37 uint32 data_traffic_send;
38 uint32 data_traffic_recv;
39
40} mbtk_tcpip_cli_info_t;
41
42typedef struct {
b.liueb040652023-09-25 18:50:56 +080043 mbtk_sock_session sock_fd;
b.liu9fc00602023-09-24 13:16:50 +080044 char ser_addr[256];
45 int ser_port;
46
47 int cli_num;
48 mbtk_tcpip_cli_info_t *cli_list;
49} mbtk_tcpip_ser_info_t;
50
51typedef struct {
52 int link_id;
53 int link_cid;
b.liueb040652023-09-25 18:50:56 +080054 bool link_connected;
55 mbtk_sock_type prot_type; // TCP/UDP
b.liu9fc00602023-09-24 13:16:50 +080056
57 mbtk_tcpip_type_enum type;
58 union
59 {
60 mbtk_tcpip_cli_info_t cli_info;
61 mbtk_tcpip_ser_info_t ser_info;
62 } tcpip_info;
63
64} mbtk_tcpip_link_t;
65
66static mbtk_tcpip_link_t tcpip_link[MBTK_TCPIP_LINK_MAX];
b.liueb040652023-09-25 18:50:56 +080067static bool tcpip_inited = FALSE;
68static mbtk_sock_handle tcpip_handle;
69static mbtk_tcpip_net_callback_func tcpip_net_cb = NULL;
70static mbtk_tcpip_sock_callback_func tcpip_sock_cb = NULL;
b.liu9fc00602023-09-24 13:16:50 +080071
b.liueb040652023-09-25 18:50:56 +080072/*
73struct tcp_info
b.liu9fc00602023-09-24 13:16:50 +080074{
b.liueb040652023-09-25 18:50:56 +080075 u_int8_t tcpi_state;
76 u_int8_t tcpi_ca_state;
77 u_int8_t tcpi_retransmits;
78 u_int8_t tcpi_probes;
79 u_int8_t tcpi_backoff;
80 u_int8_t tcpi_options;
81 u_int8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
82
83 u_int32_t tcpi_rto;
84 u_int32_t tcpi_ato;
85 u_int32_t tcpi_snd_mss;
86 u_int32_t tcpi_rcv_mss;
87
88 u_int32_t tcpi_unacked;
89 u_int32_t tcpi_sacked;
90 u_int32_t tcpi_lost;
91 u_int32_t tcpi_retrans;
92 u_int32_t tcpi_fackets;
93
94 u_int32_t tcpi_last_data_sent;
95 u_int32_t tcpi_last_ack_sent;
96 u_int32_t tcpi_last_data_recv;
97 u_int32_t tcpi_last_ack_recv;
98
99 u_int32_t tcpi_pmtu;
100 u_int32_t tcpi_rcv_ssthresh;
101 u_int32_t tcpi_rtt;
102 u_int32_t tcpi_rttvar;
103 u_int32_t tcpi_snd_ssthresh;
104 u_int32_t tcpi_snd_cwnd;
105 u_int32_t tcpi_advmss;
106 u_int32_t tcpi_reordering;
107
108 u_int32_t tcpi_rcv_rtt;
109 u_int32_t tcpi_rcv_space;
110
111 u_int32_t tcpi_total_retrans;
112};
113
114*/
115void tcp_info_print(struct tcp_info *tcp)
116{
117 if(tcp) {
118 LOGD("tcpi_state = %d", tcp->tcpi_state);
119 LOGD("tcpi_ca_state = %d", tcp->tcpi_ca_state);
120 LOGD("tcpi_retransmits = %d", tcp->tcpi_retransmits);
121 LOGD("tcpi_probes = %d", tcp->tcpi_probes);
122 LOGD("tcpi_backoff = %d", tcp->tcpi_backoff);
123 LOGD("tcpi_options = %d", tcp->tcpi_options);
124 LOGD("tcpi_snd_wscale = %d", 0x0F & tcp->tcpi_snd_wscale);
125 LOGD("tcpi_rcv_wscale = %d", 0xF0 & tcp->tcpi_rcv_wscale);
126 LOGD("tcpi_rto = %d", tcp->tcpi_rto);
127 LOGD("tcpi_ato = %d", tcp->tcpi_ato);
128 LOGD("tcpi_snd_mss = %d", tcp->tcpi_snd_mss);
129 LOGD("tcpi_rcv_mss = %d", tcp->tcpi_rcv_mss);
130 LOGD("tcpi_unacked = %d", tcp->tcpi_unacked);
131 LOGD("tcpi_sacked = %d", tcp->tcpi_sacked);
132 LOGD("tcpi_lost = %d", tcp->tcpi_lost);
133 LOGD("tcpi_retrans = %d", tcp->tcpi_retrans);
134 LOGD("tcpi_fackets = %d", tcp->tcpi_fackets);
135 LOGD("tcpi_last_data_sent = %d", tcp->tcpi_last_data_sent);
136 LOGD("tcpi_last_ack_sent = %d", tcp->tcpi_last_ack_sent);
137 LOGD("tcpi_last_data_recv = %d", tcp->tcpi_last_data_recv);
138 LOGD("tcpi_last_ack_recv = %d", tcp->tcpi_last_ack_recv);
139 LOGD("tcpi_pmtu = %d", tcp->tcpi_pmtu);
140 LOGD("tcpi_rcv_ssthresh = %d", tcp->tcpi_rcv_ssthresh);
141 LOGD("tcpi_rtt = %d", tcp->tcpi_rtt);
142 LOGD("tcpi_rttvar = %d", tcp->tcpi_rttvar);
143 LOGD("tcpi_snd_ssthresh = %d", tcp->tcpi_snd_ssthresh);
144 LOGD("tcpi_snd_cwnd = %d", tcp->tcpi_snd_cwnd);
145 LOGD("tcpi_advmss = %d", tcp->tcpi_advmss);
146 LOGD("tcpi_reordering = %d", tcp->tcpi_reordering);
147 LOGD("tcpi_rcv_rtt = %d", tcp->tcpi_rcv_rtt);
148 LOGD("tcpi_rcv_space = %d", tcp->tcpi_rcv_space);
149 LOGD("tcpi_total_retrans = %d", tcp->tcpi_total_retrans);
150 }
151}
152
153static int tcpip_fd_2_link(int fd)
154{
155 int link_id = 0;
156 mbtk_tcpip_link_t *link = NULL;
157 for(; link_id < MBTK_TCPIP_LINK_MAX; link_id++) {
158 link = tcpip_link + link_id;
159 if(link->link_connected) {
160 if(link->type == MBTK_TCPIP_TYPE_CLIENT) {
161 if(link->tcpip_info.cli_info.sock_fd > 0 && link->tcpip_info.cli_info.sock_fd == fd) {
162 break;
163 }
164 } else {
165 // Not support.
166
167 }
168 }
169 }
170
171 if(link_id == MBTK_TCPIP_LINK_MAX) {
172 return -1;
173 } else {
174 return link_id;
175 }
176}
177
b.liu8181e142023-09-26 10:31:10 +0800178static void tcpip_sock_cb_func(int handle, mbtk_sock_cb_info_s *sock_info)
b.liueb040652023-09-25 18:50:56 +0800179{
180 if(tcpip_inited && tcpip_handle == handle/* && http_fd == fd*/) {
b.liu8181e142023-09-26 10:31:10 +0800181 if(sock_info->event & (EPOLLIN | EPOLLOUT)) { // Cand read or write.
b.liueb040652023-09-25 18:50:56 +0800182 int sock_error = 0;
183 socklen_t socklen = sizeof(sock_error);
b.liu8181e142023-09-26 10:31:10 +0800184 if(getsockopt(sock_info->sock_fd, SOL_SOCKET, SO_ERROR, &sock_error, (socklen_t*)&socklen) == 0) {
b.liueb040652023-09-25 18:50:56 +0800185 LOGD("Socket error:%d", sock_error);
186 }
187
188 if(sock_error) {
189 LOGW("errno = %d", errno);
190 return;
191 }
192
b.liu8181e142023-09-26 10:31:10 +0800193 if(sock_info->sock_type == MBTK_SOCK_TCP) {
194 struct tcp_info info;
195 int len = sizeof(info);
196 if(getsockopt(sock_info->sock_fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len) == 0) {
197 LOGD("State : %d", info.tcpi_state);
b.liueb040652023-09-25 18:50:56 +0800198#if TCPIP_DEBUG
b.liu8181e142023-09-26 10:31:10 +0800199 tcp_info_print(&info);
b.liueb040652023-09-25 18:50:56 +0800200#endif
b.liueb040652023-09-25 18:50:56 +0800201 }
b.liu8181e142023-09-26 10:31:10 +0800202
203 if(TCP_ESTABLISHED != info.tcpi_state) {
204 LOGW("errno = %d", errno);
205 int link_id = tcpip_fd_2_link(sock_info->sock_fd);
206 if(link_id >= 0) {
207 // Socket disconnected?
208 mbtk_tcpip_sock_close(link_id);
209
210 if(tcpip_sock_cb) {
211 tcpip_sock_cb(link_id, 0);
212 }
213 }
214 return;
215 }
b.liueb040652023-09-25 18:50:56 +0800216 }
217 }
218
b.liu8181e142023-09-26 10:31:10 +0800219 if(sock_info->event & EPOLLIN) { // READ
220 LOGD("fd[%d] can read.", sock_info->sock_fd);
221 int link_id = tcpip_fd_2_link(sock_info->sock_fd);
b.liueb040652023-09-25 18:50:56 +0800222 if(link_id >= 0) {
223 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
224 if(tcpip_link[link_id].tcpip_info.cli_info.read_cb) {
225 char buff[MBTK_TCPIP_READ_BUFF_SIZE];
226 memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
b.liu8181e142023-09-26 10:31:10 +0800227 int read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
b.liueb040652023-09-25 18:50:56 +0800228 if(read_len > 0) {
229 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
230 tcpip_link[link_id].tcpip_info.cli_info.read_cb(link_id, (const char*)buff, read_len);
231 }
232#if 0
233 else { // Socket error(Such as server disconnected.).
234 LOGW("errno = %d", errno);
235 // Socket disconnected?
236 mbtk_tcpip_sock_close(link_id);
237
238 if(tcpip_sock_cb) {
239 tcpip_sock_cb(link_id, 0);
240 }
241 }
242#endif
243 while(read_len > 0) {
244 memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
b.liu8181e142023-09-26 10:31:10 +0800245 read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
b.liueb040652023-09-25 18:50:56 +0800246 // LOGD("read_len = %d", read_len);
247 if(read_len > 0) {
248 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
249 tcpip_link[link_id].tcpip_info.cli_info.read_cb(link_id, (const char*)buff, read_len);
250 }
251 }
252 }
253 } else {
254 // Not support.
255
256 }
257 }
b.liu8181e142023-09-26 10:31:10 +0800258 } else if(sock_info->event & EPOLLRDHUP) { // Close
259 LOGD("fd[%d] Closed?", sock_info->sock_fd);
b.liueb040652023-09-25 18:50:56 +0800260 } else {
b.liu8181e142023-09-26 10:31:10 +0800261 LOGW("Unknown event:%x",sock_info->event);
b.liueb040652023-09-25 18:50:56 +0800262 }
263 }
264}
265
b.liu8181e142023-09-26 10:31:10 +0800266static void tcpip_net_cb_func(mbtk_sock_handle handle, mbtk_net_cb_info_s *info)
b.liueb040652023-09-25 18:50:56 +0800267{
268 if(tcpip_inited && tcpip_handle == handle) {
b.liu8181e142023-09-26 10:31:10 +0800269 LOGD("Net state : %d, %s, %s", info->state, info->if_name, info->addr);
270 if(info->state == 0) {
b.liueb040652023-09-25 18:50:56 +0800271 mbtk_tcpip_net_close();
272 }
273
274 if(tcpip_net_cb) {
b.liu8181e142023-09-26 10:31:10 +0800275 tcpip_net_cb(info->state, info->addr);
b.liueb040652023-09-25 18:50:56 +0800276 }
277 }
278}
279
280static bool tcpip_link_check(int link_id)
281{
282 if(tcpip_inited && link_id >= 0 && link_id < MBTK_TCPIP_LINK_MAX) {
283 return TRUE;
284 }
285
286 return FALSE;
287}
288
289static bool tcpip_link_connected(int link_id)
290{
291 if(!tcpip_link_check(link_id)) {
292 return FALSE;
293 }
294
295 if(!tcpip_link[link_id].link_connected) {
296 return FALSE;
297 }
298
299 return TRUE;
300}
301
302static int tcpip_link_reset(mbtk_tcpip_link_t *link, int link_id)
303{
304 if(link) {
305 // Close socket if necessary.
306 if(link->link_connected) {
307 if(link->type == MBTK_TCPIP_TYPE_CLIENT) {
308 if(link->tcpip_info.cli_info.sock_fd > 0) {
309 int err;
310 if(mbtk_sock_close(tcpip_handle, link->tcpip_info.cli_info.sock_fd, 3000, &err) == 0
311 && err == MBTK_SOCK_SUCCESS) {
312 LOGD("Close socket[%d] success.", link->tcpip_info.cli_info.sock_fd);
313 } else {
314 LOGE("Close socket[%d] fail.", link->tcpip_info.cli_info.sock_fd);
315 return -1;
316 }
317 }
318 } else {
319 // Not support.
320
321 return -1;
322 }
323 }
324
325 memset(link, 0x0, sizeof(mbtk_tcpip_link_t));
326 link->link_id = link_id;
327 link->link_cid = MBTK_TCPIP_CID_DEFAULT;
328 return 0;
329 }
330
331 return -1;
332}
333
334mbtk_tcpip_err_enum mbtk_tcpip_net_open(mbtk_tcpip_net_callback_func net_cb, mbtk_tcpip_sock_callback_func sock_cb)
335{
336 if(tcpip_inited) {
337 LOGW("TCP/IP has inited.");
338 return MBTK_TCPIP_ERR_SUCCESS;
339 } else {
340 //mbtk_log_init("radio", "MBTK_TCPIP");
341 mbtk_net_state_t net_state = mbtk_net_state_get();
342 if(net_state == MBTK_NET_STATE_OFF) {
343 LOGE("Network unavailable.");
344 return MBTK_TCPIP_ERR_NET_UNAVAILABLE;
345 }
346
347 memset(&tcpip_link, 0x0, sizeof(mbtk_tcpip_link_t) * MBTK_TCPIP_LINK_MAX);
348 int i = 0;
349 for(; i < MBTK_TCPIP_LINK_MAX; i++) {
350 tcpip_link_reset(tcpip_link + i, i);
351 }
352
353 mbtk_init_info init_info;
354 init_info.net_type = MBTK_NET_LINUX;
355 init_info.net_cb = tcpip_net_cb_func;
356 init_info.sock_cb = tcpip_sock_cb_func;
357 sprintf(init_info.if_name, "ccinet%d", MBTK_TCPIP_CID_DEFAULT);
358 tcpip_handle = mbtk_sock_init(&init_info);
359 if(tcpip_handle < 0) {
360 LOGE("mbtk_sock_init() fail.");
361 return MBTK_TCPIP_ERR_NET_HANDLE;
362 }
363
364 tcpip_net_cb = net_cb;
365 tcpip_sock_cb = sock_cb;
366 tcpip_inited = TRUE;
367 return MBTK_TCPIP_ERR_SUCCESS;
368 }
b.liu9fc00602023-09-24 13:16:50 +0800369}
370
371mbtk_tcpip_err_enum mbtk_tcpip_net_close()
372{
b.liueb040652023-09-25 18:50:56 +0800373 if(tcpip_inited) {
374 int i = 0;
375 mbtk_tcpip_err_enum tcpip_err = MBTK_TCPIP_ERR_SUCCESS;
376
377 // Close all socket.
378 for(; i < MBTK_TCPIP_LINK_MAX; i++) {
379 if(tcpip_link_reset(tcpip_link + i, i)) {
380 tcpip_err = MBTK_TCPIP_ERR_UNKNOWN;
381 }
382 }
383
384 if(tcpip_err == MBTK_TCPIP_ERR_SUCCESS) {
385 tcpip_inited = FALSE;
386 }
387 return tcpip_err;
388 } else {
389 LOGW("TCP/IP not inited.");
390 return MBTK_TCPIP_ERR_SUCCESS;
391 }
b.liu9fc00602023-09-24 13:16:50 +0800392}
393
394mbtk_tcpip_err_enum mbtk_tcpip_sock_open(const mbtk_tcpip_info_t *tcpip_info)
395{
b.liueb040652023-09-25 18:50:56 +0800396 if(tcpip_info == NULL || strlen(tcpip_info->ser_addr) == 0 || tcpip_info->ser_port <= 0) {
397 LOGE("ARG error.");
398 return MBTK_TCPIP_ERR_ARG;
399 }
400
401 if(!tcpip_link_check(tcpip_info->link_id)) {
402 LOGE("Link[%d] error.", tcpip_info->link_id);
403 return MBTK_TCPIP_ERR_LINK_UNAVAILABLE;
404 }
405
406 if(tcpip_link_connected(tcpip_info->link_id)) {
407 LOGE("Link[%d] has connected.", tcpip_info->link_id);
408 return MBTK_TCPIP_ERR_LINK_UNAVAILABLE;
409 }
410
411 if(tcpip_info->tcpip_type == MBTK_TCPIP_TYPE_CLIENT) {
412 mbtk_tcpip_link_t *link = tcpip_link + tcpip_info->link_id;
413
414 int err;
415 mbtk_sock_info sock_info;
416 memset(&sock_info, 0x0, sizeof(mbtk_sock_info));
417 sock_info.type = tcpip_info->prot_type;
418 sock_info.is_support_ssl = tcpip_info->ssl_support;
419 sock_info.ingnore_cert = tcpip_info->ignore_cert;
420 memcpy(sock_info.address, tcpip_info->ser_addr, strlen(tcpip_info->ser_addr));
421 sock_info.port = tcpip_info->ser_port;
422 sock_info.local_port = tcpip_info->local_port;
423
424 link->tcpip_info.cli_info.sock_fd = mbtk_sock_open(tcpip_handle, &sock_info, 3000, &err);
425 if(link->tcpip_info.cli_info.sock_fd > 0) {
426 link->prot_type = tcpip_info->prot_type;
427 link->type = MBTK_TCPIP_TYPE_CLIENT;
428 memcpy(link->tcpip_info.cli_info.ser_addr, tcpip_info->ser_addr, strlen(tcpip_info->ser_addr));
429 link->tcpip_info.cli_info.ser_port = tcpip_info->ser_port;
430 link->tcpip_info.cli_info.local_port = tcpip_info->local_port;
431 link->tcpip_info.cli_info.ack_support = tcpip_info->ack_support;
432 link->tcpip_info.cli_info.ssl_support = tcpip_info->ssl_support;
433 link->tcpip_info.cli_info.ignore_cert = tcpip_info->ignore_cert;
434 link->tcpip_info.cli_info.heartbeat_time = tcpip_info->heartbeat_time;
435 link->tcpip_info.cli_info.delay_time = tcpip_info->delay_time;
436 link->tcpip_info.cli_info.read_cb = tcpip_info->read_cb;
437 link->link_connected = TRUE;
438 LOGD("Open socket[%d] success.");
439 return MBTK_TCPIP_ERR_SUCCESS;
440 } else {
441 LOGE("Open socket[%d] fail.");
442 return MBTK_TCPIP_ERR_UNKNOWN;
443 }
444 } else {
445 LOGE("Only support CLIENT now!");
446 return MBTK_TCPIP_ERR_UNKNOWN;
447 }
b.liu9fc00602023-09-24 13:16:50 +0800448}
449
450mbtk_tcpip_err_enum mbtk_tcpip_sock_close(int link_id)
451{
b.liueb040652023-09-25 18:50:56 +0800452 if(!tcpip_link_connected(link_id)) {
453 LOGE("Link[%d] not connected.", link_id);
454 return MBTK_TCPIP_ERR_LINK_NOT_CONNECT;
455 }
456
457 if(tcpip_link_reset(tcpip_link + link_id, link_id)) {
458 LOGE("Close link[%d] fail.", link_id);
459 return MBTK_TCPIP_ERR_UNKNOWN;
460 }
461
b.liu9fc00602023-09-24 13:16:50 +0800462 return MBTK_TCPIP_ERR_SUCCESS;
463}
464
465int mbtk_tcpip_send(int link_id, const char* data, int data_len, const char* ser_addr, int ser_port)
466{
b.liueb040652023-09-25 18:50:56 +0800467 if(!tcpip_link_connected(link_id)) {
468 LOGE("Link[%d] not connected.", link_id);
469 return -1;
470 }
471
472 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
473 int err;
474 int len = mbtk_sock_write(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
475 data, data_len, 3000, &err);
476 if(len > 0) {
477 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send += len;
478 }
479 return len;
480 } else {
481 // Not support.
482 return -1;
483 }
b.liu9fc00602023-09-24 13:16:50 +0800484
485 return 0;
486}
487
488int mbtk_tcpip_read(int link_id, char* buff, int buff_size)
489{
b.liueb040652023-09-25 18:50:56 +0800490 if(!tcpip_link_connected(link_id)) {
491 LOGE("Link[%d] not connected.", link_id);
492 return -1;
493 }
494
495 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
496 if(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.read_cb) {
497 LOGE("Set read_cb function,can not manual read.");
498 return -1;
499 }
500 int err;
501 int len = mbtk_sock_read(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
502 buff, buff_size, 3000, &err);
503 if(len > 0) {
504 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += len;
505 }
506 return len;
507 } else {
508 // Not support.
509 return -1;
510 }
b.liu9fc00602023-09-24 13:16:50 +0800511}
512
513/*
b.liueb040652023-09-25 18:50:56 +0800514* Get the data traffic of the specified link. Return -1 if fail.
b.liu9fc00602023-09-24 13:16:50 +0800515*/
516int mbtk_tcpip_data_traffic_get(int link_id)
517{
b.liueb040652023-09-25 18:50:56 +0800518 if(!tcpip_link_connected(link_id)) {
519 LOGE("Link[%d] not connected.", link_id);
520 return -1;
521 }
522
523 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
524 return tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv
525 + tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send;
526 } else {
527 // Not support.
528 return -1;
529 }
b.liu9fc00602023-09-24 13:16:50 +0800530}
531
532/*
533* Reset the data traffic of the specified link.
534*/
535mbtk_tcpip_err_enum mbtk_tcpip_data_traffic_reset(int link_id)
536{
b.liueb040652023-09-25 18:50:56 +0800537 if(!tcpip_link_connected(link_id)) {
538 LOGE("Link[%d] not connected.", link_id);
539 return MBTK_TCPIP_ERR_LINK_NOT_CONNECT;
540 }
541
542 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
543 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv = 0;
544 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send = 0;
545 return MBTK_TCPIP_ERR_SUCCESS;
546 } else {
547 // Not support.
548
549 }
550
551 return MBTK_TCPIP_ERR_UNKNOWN;
b.liu9fc00602023-09-24 13:16:50 +0800552}
553
554/*
b.liueb040652023-09-25 18:50:56 +0800555* Return 0 if disconnected, 1 for connected, other for fail.
b.liu9fc00602023-09-24 13:16:50 +0800556*/
557int mbtk_tcpip_link_state_get(int link_id)
558{
b.liueb040652023-09-25 18:50:56 +0800559 if(!tcpip_link_check(link_id)) {
560 LOGE("Link error.");
561 return -1;
562 }
563
564 return tcpip_link[link_id].link_connected ? 1 : 0;
b.liu9fc00602023-09-24 13:16:50 +0800565}
566