1 /*
2 * Copyright (c) 2022 Talkweb Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include "app_ethernet.h"
17 #include "cmsis_os2.h"
18 #include "ohos_run.h"
19
20
21 #define TCP_SERVER_PRIORITY 25
22 #define TCP_SERVER_STACK_SIZE 4096
23 #define TCP_SERVER_DELAY 200
24 #define TCP_SERVER_RCV_DATA_SIZE 512
25
26 #define SERVER_LISTEN_PORT 8080
27 #define TCP_BACKLOG 5
28 #define RECV_BUFFER_MAX 511
29
30 osThreadId_t tcp_server_id = NULL;
31
tcp_server(void * argument)32 void tcp_server(void *argument)
33 {
34 (void *)argument;
35 int sock, connected;
36 char recv_data[TCP_SERVER_RCV_DATA_SIZE] = {0};
37 struct sockaddr_in server_addr, client_addr;
38 socklen_t sin_size;
39 int recv_data_len;
40
41 printf("start tcp_server test\r\n");
42 sock = socket(AF_INET, SOCK_STREAM, 0);
43 if (sock < 0) {
44 printf("Socket error\n");
45 goto __exit;
46 }
47 server_addr.sin_family = AF_INET;
48 server_addr.sin_addr.s_addr = INADDR_ANY;
49 server_addr.sin_port = htons(SERVER_LISTEN_PORT);
50 memset_s(&(server_addr.sin_zero), sizeof(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
51
52 if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
53 printf("Unable to bind\n");
54 goto __exit;
55 }
56
57 if (listen(sock, TCP_BACKLOG) == -1) {
58 printf("Listen error\n");
59 goto __exit;
60 }
61
62 while (1) {
63 sin_size = sizeof(struct sockaddr_in);
64 connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
65
66 printf("new client connected from (%s, %d)\n",
67 inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
68
69 int flag = 1;
70 setsockopt(connected, IPPROTO_TCP, TCP_NODELAY, (void *)&flag, sizeof(int));
71 while (1) {
72 recv_data_len = recv(connected, recv_data, RECV_BUFFER_MAX, 0);
73 if (recv_data_len <= 0) {
74 break;
75 } else {
76 recv_data[recv_data_len] = '\0';
77 }
78
79 printf("recv %s\n", recv_data);
80 write(connected, recv_data, recv_data_len);
81
82 osDelay(TCP_SERVER_DELAY);
83 }
84 if (connected >= 0) {
85 closesocket(connected);
86 }
87
88 connected = -1;
89 }
90 __exit:
91 if (sock >= 0) {
92 closesocket(sock);
93 }
94 }
95
eth_enable_state_callBack(EthLinkState state)96 static void eth_enable_state_callBack(EthLinkState state)
97 {
98 static int net_init_finish = 0;
99 if (state == STATE_UPDATE_LINK_DOWN) {
100 printf("ETH LINK STATE: DisConnected!\r\n");
101 } else if (state == STATE_UPDATE_LINK_UP) {
102 printf("ETH LINK STATE: Connected!\r\n");
103 if (net_init_finish == 0) {
104 osThreadAttr_t attr;
105 attr.name = "tcp_server";
106 attr.attr_bits = 0U;
107 attr.cb_mem = NULL;
108 attr.cb_size = 0U;
109 attr.stack_mem = NULL;
110 attr.stack_size = TCP_SERVER_STACK_SIZE;
111 attr.priority = TCP_SERVER_PRIORITY;
112 tcp_server_id = osThreadNew((osThreadFunc_t)tcp_server, NULL, &attr);
113 if (tcp_server_id == NULL) {
114 printf("Failed to create tcp_server thread!\n");
115 }
116 net_init_finish = 1;
117 }
118 }
119 }
120
tcp_server_example(void)121 void tcp_server_example(void)
122 {
123 ethernet_enable(eth_enable_state_callBack);
124 }
125
126 OHOS_APP_RUN(tcp_server_example);