• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 
16 #include "lwip/netifapi.h"
17 
18 #include <hi_io.h>
19 #include <hi_gpio.h>
20 #include <hi_task.h>
21 #include <hi_watchdog.h>
22 #include "ohos_init.h"
23 #include "cmsis_os2.h"
24 #include "udp_config.h"
25 
26 #include <string.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 
31 #define INVAILD_SOCKET          (-1)
32 #define FREE_CPU_TIME_20MS      (20)
33 #define INVALID_VALUE           "202.202.202.202"
34 
35 #define NATIVE_IP_ADDRESS       "XXX.XXX.XX.XXX" // 用户查找本地IP后需要进行修改
36 #define WECHAT_MSG_LIGHT_ON     "_light_on"
37 #define WECHAT_MSG_LIGHT_OFF    "_light_off"
38 #define DEVICE_MSG_LIGHT_ON     "device_light_on"
39 #define DEVICE_MSG_LIGHT_OFF    "device_light_off"
40 #define WECHAT_MSG_UNLOAD_PAGE  "UnoladPage"
41 #define RECV_DATA_FLAG_OTHER    (2)
42 #define HOST_PORT               (5566)
43 #define DEVICE_PORT             (6655)
44 
45 #define UDP_RECV_LEN (255)
46 
47 typedef void (*FnMsgCallBack)(hi_gpio_value val);
48 
49 typedef struct FunctionCallback {
50     hi_bool stop;
51     hi_u32 conLost;
52     hi_u32 queueID;
53     hi_u32 iotTaskID;
54     FnMsgCallBack msgCallBack;
55 }FunctionCallback;
56 FunctionCallback g_gfnCallback;
57 
DeviceConfigInit(hi_gpio_value val)58 void DeviceConfigInit(hi_gpio_value val)
59 {
60     hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_GPIO);
61     hi_gpio_set_dir(HI_GPIO_IDX_9, HI_GPIO_DIR_OUT);
62     hi_gpio_set_ouput_val(HI_GPIO_IDX_9, val);
63 }
64 
DeviceMsgCallback(FnMsgCallBack msgCallBack)65 int  DeviceMsgCallback(FnMsgCallBack msgCallBack)
66 {
67     g_gfnCallback.msgCallBack = msgCallBack;
68     return 0;
69 }
70 
WeChatControlDeviceMsg(hi_gpio_value val)71 void WeChatControlDeviceMsg(hi_gpio_value val)
72 {
73     DeviceConfigInit(val);
74 }
75 
UdpTransportInit(struct sockaddr_in serAddr,struct sockaddr_in remoteAddr)76 int UdpTransportInit(struct sockaddr_in serAddr, struct sockaddr_in remoteAddr)
77 {
78     int sServer = socket(AF_INET, SOCK_DGRAM, 0);
79     if (sServer == INVAILD_SOCKET) {
80         printf("create server socket failed\r\n");
81         close(sServer);
82     }
83     // 本地主机ip和端口号
84     serAddr.sin_family = AF_INET;
85     serAddr.sin_port = htons(HOST_PORT);
86     serAddr.sin_addr.s_addr = inet_addr(NATIVE_IP_ADDRESS);
87     if (bind(sServer, (struct sockaddr*)&serAddr, sizeof(serAddr)) == -1) {
88         printf("bind socket failed\r\n");
89         close(sServer);
90     }
91     // 对方ip和端口号
92     remoteAddr.sin_family = AF_INET;
93     remoteAddr.sin_port = htons(DEVICE_PORT);
94     serAddr.sin_addr.s_addr = htons(INADDR_ANY);
95 
96     return sServer;
97 }
98 
UdpServerDemo(const char * param)99 void *UdpServerDemo(const char *param)
100 {
101     struct sockaddr_in serAddr = {0};
102     struct sockaddr_in remoteAddr = {0};
103     static int recvDataFlag = -1;
104     char *sendData = NULL;
105     int sServer = 0;
106 
107     (char*)(param);
108     printf(" This Pegasus udp server demo\r\n");
109     sServer = UdpTransportInit(serAddr, remoteAddr);
110 
111     int addrLen = sizeof(remoteAddr);
112     char recvData[UDP_RECV_LEN] = {0};
113 
114     while (1) {
115         /* 255长度 */
116         int recvLen = recvfrom(sServer, recvData, UDP_RECV_LEN, 0, (struct sockaddr*)&remoteAddr, (socklen_t*)&addrLen);
117         if (recvLen) {
118             if (strstr(inet_ntoa(remoteAddr.sin_addr), INVALID_VALUE) == NULL) {
119                 printf("A connection was received:%s\r\n", inet_ntoa(remoteAddr.sin_addr));
120                 printf("Received data:%s\r\n", recvData);
121             }
122             if (strstr(recvData, WECHAT_MSG_LIGHT_OFF) != NULL) {
123                 printf("Control equipment information received:%s\r\n", recvData);
124                 recvDataFlag = HI_FALSE;
125                 WeChatControlDeviceMsg(HI_GPIO_VALUE1);
126             } else if (strstr(recvData, WECHAT_MSG_LIGHT_ON) != NULL) {
127                 printf("Control equipment information received:%s\r\n", recvData);
128                 recvDataFlag = HI_TRUE;
129                 WeChatControlDeviceMsg(HI_GPIO_VALUE0);
130             } else if (strstr(recvData, WECHAT_MSG_UNLOAD_PAGE) != NULL) {
131                 printf("The applet exits the current interface\r\n");
132                 WeChatControlDeviceMsg(HI_GPIO_VALUE1);
133             } else {
134                 recvDataFlag = RECV_DATA_FLAG_OTHER;
135             }
136         }
137         if (recvDataFlag == HI_TRUE) {
138             sendData = DEVICE_MSG_LIGHT_ON;
139             sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
140         } else if (recvDataFlag == HI_FALSE) {
141             sendData = DEVICE_MSG_LIGHT_OFF;
142             sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
143         } else if (recvDataFlag == RECV_DATA_FLAG_OTHER) {
144             sendData = "Received a message from the server";
145             sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
146         }
147         hi_sleep(FREE_CPU_TIME_20MS);
148     }
149     close(sServer);
150     return NULL;
151 }
152 
153 #define UDP_TASK_STACKSIZE  0x1000
154 #define UDP_TASK_PRIOR 27
155 #define UDP_TASK_NAME "UDP_demo"
156 
UDPTransport(void)157 static void UDPTransport(void)
158 {
159     osThreadAttr_t attr;
160 #ifdef CONFIG_WIFI_AP_MODULE
161     if (hi_wifi_start_softap() != 0) {
162         printf("open softap failure\n");
163         return;
164     }
165     printf("open softap ok\n");
166 #elif defined(CONFIG_WIFI_STA_MODULE)
167     /* start wifi sta module */
168     WifiStaModule();
169 #endif
170     attr.name = "udp demo";
171     attr.attr_bits = 0U;
172     attr.cb_mem = NULL;
173     attr.cb_size = 0U;
174     attr.stack_mem = NULL;
175     attr.stack_size = UDP_TASK_STACKSIZE;
176     attr.priority = UDP_TASK_PRIOR;
177 
178     if (osThreadNew((osThreadFunc_t)UdpServerDemo, NULL, &attr) == NULL) {
179         printf("[UDP] Failed to create udp demo!\n");
180     }
181 }
182 
183 SYS_RUN(UDPTransport);