• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cmsis_os2.h"
17 #include "ohos_run.h"
18 #include "app_ethernet.h"
19 
20 #define SERVER_IP "192.168.8.119"
21 #define SERVER_PORT 8080
22 
23 osThreadId_t tcp_client_id = NULL;
24 
tcp_client(void * thread_param)25 void tcp_client(void *thread_param)
26 {
27     struct sockaddr_in client_addr;
28     char recv_data[512] = {0};
29     int recv_data_len;
30 
31     while (1) {
32         int sock = socket(AF_INET, SOCK_STREAM, 0);
33         if (sock < 0) {
34             printf("Socket error\n");
35             osDelay(100);
36             continue;
37         }
38 
39         memset_s(&(client_addr), sizeof(client_addr), 0, sizeof(client_addr));
40 
41         client_addr.sin_family = AF_INET;
42         client_addr.sin_port = htons(SERVER_PORT);
43         client_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
44 
45         printf("try connect to server " SERVER_IP ":%d\n", SERVER_PORT);
46         if (connect(sock, (struct sockaddr *)&client_addr, sizeof(struct sockaddr)) == -1) {
47             closesocket(sock);
48             osDelay(1000);
49             continue;
50         }
51 
52         printf("Connect to tcp server successful!\n");
53         while (1) {
54             recv_data_len = recv(sock, recv_data, 511, 0);
55             if (recv_data_len <= 0)
56                 break;
57             else
58                 recv_data[recv_data_len] = '\0';
59 
60             printf("recv:%s\n", recv_data);
61             write(sock, recv_data, recv_data_len);
62         }
63     }
64 }
65 
eth_enable_state_callBack(EthLinkState state)66 static void eth_enable_state_callBack(EthLinkState state)
67 {
68     if (state == STATE_UPDATE_LINK_DOWN) {
69         printf("ETH LINK STATE: DisConnected!\r\n");
70         osThreadTerminate(tcp_client_id);
71         tcp_client_id = NULL;
72     } else if (state == STATE_UPDATE_LINK_UP) {
73         printf("ETH LINK STATE: Connected!\r\n");
74 
75         osThreadAttr_t attr;
76         attr.name = "tcp_client";
77         attr.attr_bits = 0U;
78         attr.cb_mem = NULL;
79         attr.cb_size = 0U;
80         attr.stack_mem = NULL;
81         attr.stack_size = 1024 * 4;
82         attr.priority = 25;
83         tcp_client_id = osThreadNew((osThreadFunc_t)tcp_client, NULL, &attr);
84         if (tcp_client_id == NULL) {
85             printf("Failed to create tcp_client thread!\n");
86         }
87     }
88 }
89 
tcp_client_example(void)90 void tcp_client_example(void)
91 {
92     EthLinkInfo info = {
93         .useStaticIp = 1,
94         .ipaddr.u8_addr = {192, 168, 8, 200},
95         .netmask.u8_addr = {255, 255, 255, 0},
96         .gw.u8_addr = {192, 168, 8, 1},
97 
98         .useStaticMac = 1,
99         .macAddr = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01}
100     };
101 
102     set_ethernet_link_info(&info);
103     ethernet_enable(eth_enable_state_callBack);
104 }
105 
106 OHOS_APP_RUN(tcp_client_example);