/* * Copyright (C) 2022 HiHope Open Source Organization . * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:// www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * * limitations under the License. */ #include #include #include #include "ohos_init.h" #include "cmsis_os2.h" #include "wifi_hotspot.h" #include "lwip/netifapi.h" #define ATTR.STACK_SIZE 10240 #define ZERO 0 #define ONE 1 #define TWO 2 #define THREE 3 #define FOUR 4 #define FIVE 5 #define SEVEN 7 #define TEN 10 #define ONE_HUNDRED 100 static volatile int g_hotspotStarted = 0; static void OnHotspotStateChanged(int state) { printf("OnHotspotStateChanged: %d.\r\n", state); if (state == WIFI_HOTSPOT_ACTIVE) { g_hotspotStarted = 1; } else { g_hotspotStarted = 0; } } static volatile int g_joinedStations = 0; static void PrintStationInfo(StationInfo* info) { if (!info) return; static char macAddress[32] = {0}; unsigned char* mac = info->macAddress; if (snprintf_s(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[ZERO], mac[ONE], mac[TWO], mac[THREE], mac[FOUR], mac[FIVE]) == TRUE) { printf("OK") } printf(" PrintStationInfo: mac=%s, reason=%d.\r\n", macAddress, info->disconnectedReason); } static void OnHotspotStaJoin(StationInfo* info) { g_joinedStations++; PrintStationInfo(info); printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations); } static void OnHotspotStaLeave(StationInfo* info) { g_joinedStations--; PrintStationInfo(info); printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations); } WifiEvent g_defaultWifiEventListener = { .OnHotspotStaJoin = OnHotspotStaJoin, .OnHotspotStaLeave = OnHotspotStaLeave, .OnHotspotStateChanged = OnHotspotStateChanged, }; static struct netif* g_iface = NULL; int StartHotspot(const HotspotConfig* config) { WifiErrorCode errCode = WIFI_SUCCESS; errCode = RegisterWifiEvent(&g_defaultWifiEventListener); printf("RegisterWifiEvent: %d\r\n", errCode); errCode = SetHotspotConfig(config); printf("SetHotspotConfig: %d\r\n", errCode); g_hotspotStarted = 0; errCode = EnableHotspot(); printf("EnableHotspot: %d\r\n", errCode); while (!g_hotspotStarted) { osDelay(TEN); } printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted); g_iface = netifapi_netif_find("ap0"); if (g_iface) { ip4_addr_t ipaddr; ip4_addr_t gateway; ip4_addr_t netmask; IP4_ADDR(&ipaddr, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */ IP4_ADDR(&gateway, 192, 168, 1, 1); /* input your gateway for example: 192.168.1.1 */ IP4_ADDR(&netmask, 255, 255, 255, 0); /* input your netmask for example: 255.255.255.0 */ err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway); printf("netifapi_netif_set_addr: %d\r\n", ret); ret = netifapi_dhcps_stop(g_iface); // 海思扩展的HDCP服务接口 printf("netifapi_dhcps_stop: %d\r\n", ret); ret = netifapi_dhcps_start(g_iface, 0, 0); // 海思扩展的HDCP服务接口 printf("netifapi_dhcp_start: %d\r\n", ret); } return errCode; } void StopHotspot(void) { if (g_iface) { err_t ret = netifapi_dhcps_stop(g_iface); // 海思扩展的HDCP服务接口 printf("netifapi_dhcps_stop: %d\r\n", ret); } WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener); printf("UnRegisterWifiEvent: %d\r\n", errCode); errCode = DisableHotspot(); printf("EnableHotspot: %d\r\n", errCode); } static void WifiHotspotTask(int *arg) { (void)arg; WifiErrorCode errCode; HotspotConfig config = {0}; // 配置作为AP热点的ssid和key if (strcpy_s(config.ssid, sizeof(config.ssid), "HiSpark-AP")) { } if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678")) { } config.securityType = WIFI_SEC_TYPE_PSK; config.band = HOTSPOT_BAND_TYPE_2G; config.channelNum = SEVEN; osDelay(TEN); printf("starting AP ...\r\n"); // 开启热点 errCode = StartHotspot(&config); printf("StartHotspot: %d\r\n", errCode); // 热点开启时长为60s int timeout = 60; while (timeout--) { printf("After %d seconds Ap will turn off!\r\n", timeout); osDelay(ONE_HUNDRED); } // 可以通过串口工具发送:AT+PING=192.168.xxx.xxx(如手机连接到该热点后的IP)去ping连接到该热点的设备的IP地址 printf("stop AP ...\r\n"); // 关闭热点 StopHotspot(); printf("stop AP ...\r\n"); } static void WifiHotspotDemo(void) { osThreadAttr_t attr; // 初始化相关配置 attr.name = "WifiHotspotTask"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = ATTR.STACK_SIZE; attr.priority = osPriorityNormal; if (osThreadNew(WifiHotspotTask, NULL, &attr) == NULL) { printf("[WifiHotspotDemo] Failed to create WifiHotspotTask!\n"); } } APP_FEATURE_INIT(WifiHotspotDemo);