10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci * limitations under the License.
140a7ce71fSopenharmony_ci */
150a7ce71fSopenharmony_ci
160a7ce71fSopenharmony_ci#include "lwip/netifapi.h"
170a7ce71fSopenharmony_ci
180a7ce71fSopenharmony_ci#include <hi_io.h>
190a7ce71fSopenharmony_ci#include <hi_gpio.h>
200a7ce71fSopenharmony_ci#include <hi_task.h>
210a7ce71fSopenharmony_ci#include <hi_watchdog.h>
220a7ce71fSopenharmony_ci#include "ohos_init.h"
230a7ce71fSopenharmony_ci#include "cmsis_os2.h"
240a7ce71fSopenharmony_ci#include "udp_config.h"
250a7ce71fSopenharmony_ci
260a7ce71fSopenharmony_ci#include <string.h>
270a7ce71fSopenharmony_ci#include <errno.h>
280a7ce71fSopenharmony_ci#include <stdlib.h>
290a7ce71fSopenharmony_ci#include <stdio.h>
300a7ce71fSopenharmony_ci
310a7ce71fSopenharmony_ci#define INVAILD_SOCKET          (-1)
320a7ce71fSopenharmony_ci#define FREE_CPU_TIME_20MS      (20)
330a7ce71fSopenharmony_ci#define INVALID_VALUE           "202.202.202.202"
340a7ce71fSopenharmony_ci
350a7ce71fSopenharmony_ci#define NATIVE_IP_ADDRESS       "XXX.XXX.XX.XXX" // 用户查找本地IP后需要进行修改
360a7ce71fSopenharmony_ci#define WECHAT_MSG_LIGHT_ON     "_light_on"
370a7ce71fSopenharmony_ci#define WECHAT_MSG_LIGHT_OFF    "_light_off"
380a7ce71fSopenharmony_ci#define DEVICE_MSG_LIGHT_ON     "device_light_on"
390a7ce71fSopenharmony_ci#define DEVICE_MSG_LIGHT_OFF    "device_light_off"
400a7ce71fSopenharmony_ci#define WECHAT_MSG_UNLOAD_PAGE  "UnoladPage"
410a7ce71fSopenharmony_ci#define RECV_DATA_FLAG_OTHER    (2)
420a7ce71fSopenharmony_ci#define HOST_PORT               (5566)
430a7ce71fSopenharmony_ci#define DEVICE_PORT             (6655)
440a7ce71fSopenharmony_ci
450a7ce71fSopenharmony_ci#define UDP_RECV_LEN (255)
460a7ce71fSopenharmony_ci
470a7ce71fSopenharmony_citypedef void (*FnMsgCallBack)(hi_gpio_value val);
480a7ce71fSopenharmony_ci
490a7ce71fSopenharmony_citypedef struct FunctionCallback {
500a7ce71fSopenharmony_ci    hi_bool stop;
510a7ce71fSopenharmony_ci    hi_u32 conLost;
520a7ce71fSopenharmony_ci    hi_u32 queueID;
530a7ce71fSopenharmony_ci    hi_u32 iotTaskID;
540a7ce71fSopenharmony_ci    FnMsgCallBack msgCallBack;
550a7ce71fSopenharmony_ci}FunctionCallback;
560a7ce71fSopenharmony_ciFunctionCallback g_gfnCallback;
570a7ce71fSopenharmony_ci
580a7ce71fSopenharmony_civoid DeviceConfigInit(hi_gpio_value val)
590a7ce71fSopenharmony_ci{
600a7ce71fSopenharmony_ci    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_GPIO);
610a7ce71fSopenharmony_ci    hi_gpio_set_dir(HI_GPIO_IDX_9, HI_GPIO_DIR_OUT);
620a7ce71fSopenharmony_ci    hi_gpio_set_ouput_val(HI_GPIO_IDX_9, val);
630a7ce71fSopenharmony_ci}
640a7ce71fSopenharmony_ci
650a7ce71fSopenharmony_ciint  DeviceMsgCallback(FnMsgCallBack msgCallBack)
660a7ce71fSopenharmony_ci{
670a7ce71fSopenharmony_ci    g_gfnCallback.msgCallBack = msgCallBack;
680a7ce71fSopenharmony_ci    return 0;
690a7ce71fSopenharmony_ci}
700a7ce71fSopenharmony_ci
710a7ce71fSopenharmony_civoid WeChatControlDeviceMsg(hi_gpio_value val)
720a7ce71fSopenharmony_ci{
730a7ce71fSopenharmony_ci    DeviceConfigInit(val);
740a7ce71fSopenharmony_ci}
750a7ce71fSopenharmony_ci
760a7ce71fSopenharmony_ciint UdpTransportInit(struct sockaddr_in serAddr, struct sockaddr_in remoteAddr)
770a7ce71fSopenharmony_ci{
780a7ce71fSopenharmony_ci    int sServer = socket(AF_INET, SOCK_DGRAM, 0);
790a7ce71fSopenharmony_ci    if (sServer == INVAILD_SOCKET) {
800a7ce71fSopenharmony_ci        printf("create server socket failed\r\n");
810a7ce71fSopenharmony_ci        close(sServer);
820a7ce71fSopenharmony_ci    }
830a7ce71fSopenharmony_ci    // 本地主机ip和端口号
840a7ce71fSopenharmony_ci    serAddr.sin_family = AF_INET;
850a7ce71fSopenharmony_ci    serAddr.sin_port = htons(HOST_PORT);
860a7ce71fSopenharmony_ci    serAddr.sin_addr.s_addr = inet_addr(NATIVE_IP_ADDRESS);
870a7ce71fSopenharmony_ci    if (bind(sServer, (struct sockaddr*)&serAddr, sizeof(serAddr)) == -1) {
880a7ce71fSopenharmony_ci        printf("bind socket failed\r\n");
890a7ce71fSopenharmony_ci        close(sServer);
900a7ce71fSopenharmony_ci    }
910a7ce71fSopenharmony_ci    // 对方ip和端口号
920a7ce71fSopenharmony_ci    remoteAddr.sin_family = AF_INET;
930a7ce71fSopenharmony_ci    remoteAddr.sin_port = htons(DEVICE_PORT);
940a7ce71fSopenharmony_ci    serAddr.sin_addr.s_addr = htons(INADDR_ANY);
950a7ce71fSopenharmony_ci
960a7ce71fSopenharmony_ci    return sServer;
970a7ce71fSopenharmony_ci}
980a7ce71fSopenharmony_ci
990a7ce71fSopenharmony_civoid *UdpServerDemo(const char *param)
1000a7ce71fSopenharmony_ci{
1010a7ce71fSopenharmony_ci    struct sockaddr_in serAddr = {0};
1020a7ce71fSopenharmony_ci    struct sockaddr_in remoteAddr = {0};
1030a7ce71fSopenharmony_ci    static int recvDataFlag = -1;
1040a7ce71fSopenharmony_ci    char *sendData = NULL;
1050a7ce71fSopenharmony_ci    int sServer = 0;
1060a7ce71fSopenharmony_ci
1070a7ce71fSopenharmony_ci    (char*)(param);
1080a7ce71fSopenharmony_ci    printf(" This Pegasus udp server demo\r\n");
1090a7ce71fSopenharmony_ci    sServer = UdpTransportInit(serAddr, remoteAddr);
1100a7ce71fSopenharmony_ci
1110a7ce71fSopenharmony_ci    int addrLen = sizeof(remoteAddr);
1120a7ce71fSopenharmony_ci    char recvData[UDP_RECV_LEN] = {0};
1130a7ce71fSopenharmony_ci
1140a7ce71fSopenharmony_ci    while (1) {
1150a7ce71fSopenharmony_ci        /* 255长度 */
1160a7ce71fSopenharmony_ci        int recvLen = recvfrom(sServer, recvData, UDP_RECV_LEN, 0, (struct sockaddr*)&remoteAddr, (socklen_t*)&addrLen);
1170a7ce71fSopenharmony_ci        if (recvLen) {
1180a7ce71fSopenharmony_ci            if (strstr(inet_ntoa(remoteAddr.sin_addr), INVALID_VALUE) == NULL) {
1190a7ce71fSopenharmony_ci                printf("A connection was received:%s\r\n", inet_ntoa(remoteAddr.sin_addr));
1200a7ce71fSopenharmony_ci                printf("Received data:%s\r\n", recvData);
1210a7ce71fSopenharmony_ci            }
1220a7ce71fSopenharmony_ci            if (strstr(recvData, WECHAT_MSG_LIGHT_OFF) != NULL) {
1230a7ce71fSopenharmony_ci                printf("Control equipment information received:%s\r\n", recvData);
1240a7ce71fSopenharmony_ci                recvDataFlag = HI_FALSE;
1250a7ce71fSopenharmony_ci                WeChatControlDeviceMsg(HI_GPIO_VALUE1);
1260a7ce71fSopenharmony_ci            } else if (strstr(recvData, WECHAT_MSG_LIGHT_ON) != NULL) {
1270a7ce71fSopenharmony_ci                printf("Control equipment information received:%s\r\n", recvData);
1280a7ce71fSopenharmony_ci                recvDataFlag = HI_TRUE;
1290a7ce71fSopenharmony_ci                WeChatControlDeviceMsg(HI_GPIO_VALUE0);
1300a7ce71fSopenharmony_ci            } else if (strstr(recvData, WECHAT_MSG_UNLOAD_PAGE) != NULL) {
1310a7ce71fSopenharmony_ci                printf("The applet exits the current interface\r\n");
1320a7ce71fSopenharmony_ci                WeChatControlDeviceMsg(HI_GPIO_VALUE1);
1330a7ce71fSopenharmony_ci            } else {
1340a7ce71fSopenharmony_ci                recvDataFlag = RECV_DATA_FLAG_OTHER;
1350a7ce71fSopenharmony_ci            }
1360a7ce71fSopenharmony_ci        }
1370a7ce71fSopenharmony_ci        if (recvDataFlag == HI_TRUE) {
1380a7ce71fSopenharmony_ci            sendData = DEVICE_MSG_LIGHT_ON;
1390a7ce71fSopenharmony_ci            sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
1400a7ce71fSopenharmony_ci        } else if (recvDataFlag == HI_FALSE) {
1410a7ce71fSopenharmony_ci            sendData = DEVICE_MSG_LIGHT_OFF;
1420a7ce71fSopenharmony_ci            sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
1430a7ce71fSopenharmony_ci        } else if (recvDataFlag == RECV_DATA_FLAG_OTHER) {
1440a7ce71fSopenharmony_ci            sendData = "Received a message from the server";
1450a7ce71fSopenharmony_ci            sendto(sServer, sendData, strlen(sendData), 0, (struct sockaddr*)&remoteAddr, addrLen);
1460a7ce71fSopenharmony_ci        }
1470a7ce71fSopenharmony_ci        hi_sleep(FREE_CPU_TIME_20MS);
1480a7ce71fSopenharmony_ci    }
1490a7ce71fSopenharmony_ci    close(sServer);
1500a7ce71fSopenharmony_ci    return NULL;
1510a7ce71fSopenharmony_ci}
1520a7ce71fSopenharmony_ci
1530a7ce71fSopenharmony_ci#define UDP_TASK_STACKSIZE  0x1000
1540a7ce71fSopenharmony_ci#define UDP_TASK_PRIOR 27
1550a7ce71fSopenharmony_ci#define UDP_TASK_NAME "UDP_demo"
1560a7ce71fSopenharmony_ci
1570a7ce71fSopenharmony_cistatic void UDPTransport(void)
1580a7ce71fSopenharmony_ci{
1590a7ce71fSopenharmony_ci    osThreadAttr_t attr;
1600a7ce71fSopenharmony_ci#ifdef CONFIG_WIFI_AP_MODULE
1610a7ce71fSopenharmony_ci    if (hi_wifi_start_softap() != 0) {
1620a7ce71fSopenharmony_ci        printf("open softap failure\n");
1630a7ce71fSopenharmony_ci        return;
1640a7ce71fSopenharmony_ci    }
1650a7ce71fSopenharmony_ci    printf("open softap ok\n");
1660a7ce71fSopenharmony_ci#elif defined(CONFIG_WIFI_STA_MODULE)
1670a7ce71fSopenharmony_ci    /* start wifi sta module */
1680a7ce71fSopenharmony_ci    WifiStaModule();
1690a7ce71fSopenharmony_ci#endif
1700a7ce71fSopenharmony_ci    attr.name = "udp demo";
1710a7ce71fSopenharmony_ci    attr.attr_bits = 0U;
1720a7ce71fSopenharmony_ci    attr.cb_mem = NULL;
1730a7ce71fSopenharmony_ci    attr.cb_size = 0U;
1740a7ce71fSopenharmony_ci    attr.stack_mem = NULL;
1750a7ce71fSopenharmony_ci    attr.stack_size = UDP_TASK_STACKSIZE;
1760a7ce71fSopenharmony_ci    attr.priority = UDP_TASK_PRIOR;
1770a7ce71fSopenharmony_ci
1780a7ce71fSopenharmony_ci    if (osThreadNew((osThreadFunc_t)UdpServerDemo, NULL, &attr) == NULL) {
1790a7ce71fSopenharmony_ci        printf("[UDP] Failed to create udp demo!\n");
1800a7ce71fSopenharmony_ci    }
1810a7ce71fSopenharmony_ci}
1820a7ce71fSopenharmony_ci
1830a7ce71fSopenharmony_ciSYS_RUN(UDPTransport);