• 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 "app_ethernet.h"
17 #include "cmsis_os2.h"
18 #include "ohos_run.h"
19 
20 #define LOCAL_PORT 1234
21 #define DESTINATION_PORT 5678
22 #define DESTINATION_IP "192.168.8.119"
23 
24 #define TASK_DELAY 1000
25 #define STACK_SIZE 4096
26 #define IP_ADDR_3 2
27 #define IP_ADDR_4 3
28 
29 osThreadId_t udp_test_id = NULL;
30 static struct udp_pcb *g_upcb = NULL;
31 static unsigned char udp_rcv_flag = 0;
32 
udp_receive_callback(void * arg,struct udp_pcb * upcb,struct pbuf * p,const ip_addr_t * addr,u16_t port)33 void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
34 {
35     (void *)arg;
36     if (p != NULL) {
37         char *recdata = (char *)malloc(p->len * sizeof(char) + 1);
38         if (recdata != NULL) {
39             if (udp_rcv_flag != 1) {
40                 udp_rcv_flag = 1;
41             }
42             memcpy_s(recdata, p->len, p->payload, p->len);
43             recdata[p->len] = '\0';
44             printf("udp recv from[%d.%d.%d.%d:%d]: %s\n", *((uint8_t *)&addr->addr), *((uint8_t *)&addr->addr + 1),
45                    *((uint8_t *)&addr->addr + IP_ADDR_3), *((uint8_t *)&addr->addr + IP_ADDR_4), port, recdata);
46             free(recdata);
47             udp_send(upcb, p);
48         }
49         pbuf_free(p);
50     }
51 }
52 
udp_client_send(char * pData)53 void udp_client_send(char *pData)
54 {
55     struct pbuf *p;
56     p = pbuf_alloc(PBUF_TRANSPORT, strlen(pData), PBUF_POOL);
57     if (p != NULL) {
58         pbuf_take(p, pData, strlen(pData));
59         udp_send(g_upcb, p);
60         pbuf_free(p);
61     }
62 }
63 
udp_test(void * arg)64 void udp_test(void *arg)
65 {
66     (void)arg;
67     ip_addr_t serverIP;
68     err_t err;
69     ipaddr_aton(DESTINATION_IP, &serverIP);
70     g_upcb = udp_new();
71     if (g_upcb != NULL) {
72         g_upcb->local_port = LOCAL_PORT;
73         err = udp_connect(g_upcb, &serverIP, DESTINATION_PORT);
74         if (err == ERR_OK) {
75             udp_recv(g_upcb, udp_receive_callback, NULL);
76             printf("udp client connected\r\n");
77         } else {
78             udp_remove(g_upcb);
79             printf("can not connect udp pcb.\r\n");
80         }
81     }
82     while (!udp_rcv_flag) {
83         printf("wait recv udp data...\n");
84         osDelay(1000);
85     }
86 }
87 
eth_enable_state_callBack(EthLinkState state)88 static void eth_enable_state_callBack(EthLinkState state)
89 {
90     static int net_init_finish = 0;
91     if (state == STATE_UPDATE_LINK_DOWN) {
92         printf("ETH LINK STATE: DisConnected!\r\n");
93     } else if (state == STATE_UPDATE_LINK_UP) {
94         printf("ETH LINK STATE: Connected!\r\n");
95         if (net_init_finish == 0) {
96             osThreadAttr_t attr;
97             attr.name = "udp_test";
98             attr.attr_bits = 0U;
99             attr.cb_mem = NULL;
100             attr.cb_size = 0U;
101             attr.stack_mem = NULL;
102             attr.stack_size = STACK_SIZE;
103             attr.priority = 25;
104             udp_test_id = osThreadNew((osThreadFunc_t)udp_test, NULL, &attr);
105             if (udp_test_id == NULL) {
106                 printf("Failed to create udp_test thread!\n");
107             }
108             net_init_finish = 1;
109         }
110     }
111 }
112 
udp_test_example(void)113 void udp_test_example(void)
114 {
115     ethernet_enable(eth_enable_state_callBack);
116 }
117 
118 OHOS_APP_RUN(udp_test_example);