wifi_hotspot.h接口简介
- 启用AP热点模式
WifiErrorCode EnableHotspot(void); - 禁用AP热点模式
WifiErrorCode DisableHotspot(void); - 设置指定的热点配置
WifiErrorCode SetHotspotConfig(const HotspotConfig* config); - 获取指定的热点配置
WifiErrorCode GetHotspotConfig(HotspotConfig* result); - 检查AP热点模式是否启用
int IsHotspotActive(void); - 获取连接到该热点的一系列STA
WifiErrorCode GetStationList(StationInfo* result, unsigned int* size); - 获取接收信号强度和频
int GetSignalLevel(int rssi, int band);
wifi热点代码
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include <string.h>
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_errno.h"
#include "wifiiot_uart.h"
#include "lwip/netifapi.h"
#include "wifi_hotspot.h"
#include "wifi_device.h"
#include "wifi_error_code.h"
#define AP_SSID "BearPi"
#define AP_PSK "12345678"
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static struct netif *pNetif = NULL;
static int g_ap_enabled = 0;
static WifiEvent wifi_event = {0};
static WifiErrorCode error_code = 0;
static BOOL WifiAPTask(void)
{
osDelay(200);
wifi_event.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
wifi_event.OnHotspotStateChanged = OnHotspotStateChangedHandler;
wifi_event.OnHotspotStaJoin = OnHotspotStaJoinHandler;
error_code = RegisterWifiEvent(&wifi_event);
if(error_code != WIFI_SUCCESS)
{
printf("RegisterWifiEvent failed, error = %d.\r\n",error_code);
return -1;
}
HotspotConfig config = {0};
strcpy(config.ssid,AP_SSID);
strcpy(config.preSharedKey,AP_PSK);
config.band = HOTSPOT_BAND_TYPE_2G;
config.channelNum = 7;
config.securityType = WIFI_SEC_TYPE_PSK;
error_code = SetHotspotConfig(&config);
if (error_code != WIFI_SUCCESS)
{
printf("SetHotspotConfig failed, error = %d.\r\n", error_code);
return -1;
}
error_code = EnableHotspot();
if (error_code != WIFI_SUCCESS)
{
printf("EnableHotspot failed, error = %d.\r\n", error_code);
return -1;
}
if(IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE)
{
printf("Wifi station is not actived.\r\n");
return -1;
}
pNetif = netifapi_netif_find("ap0");
if(pNetif != NULL)
{
ip4_addr_t bp_gw;
ip4_addr_t bp_ipaddr;
ip4_addr_t bp_netmask;
IP4_ADDR(&bp_gw, 192, 168, 1, 1);
IP4_ADDR(&bp_ipaddr, 192, 168, 1, 1);
IP4_ADDR(&bp_netmask, 255, 255, 255, 0);
err_t ret = netifapi_netif_set_addr(pNetif,&bp_ipaddr,&bp_netmask,&bp_gw);
if(ret != ERR_OK)
{
printf("netifapi_netif_set_addr failed, error = %d.\r\n", ret);
return -1;
}
ret = netifapi_dhcps_start(pNetif,0,0);
if(ret != ERR_OK)
{
printf("netifapi_dhcp_start failed, error = %d.\r\n", ret);
return -1;
}
}
while (1)
{
osDelay(1);
}
}
static void HotspotStaJoinTask(void)
{
static char macAddress[32] = {0};
StationInfo stainfo[WIFI_MAX_STA_NUM] = {0};
StationInfo *sta_list_node = NULL;
unsigned int size = WIFI_MAX_STA_NUM;
error_code = GetStationList(stainfo,&size);
if (error_code != WIFI_SUCCESS) {
printf("HotspotStaJoin:get list fail, error is %d.\r\n", error_code);
return;
}
sta_list_node = stainfo;
for(uint32_t i=0; i<size; i++,sta_list_node++)
{
uint8_t *mac = sta_list_node->macAddress;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("HotspotSta[%d]: macAddress=%s.\r\n",i, macAddress);
}
g_ap_enabled++;
}
static void OnHotspotStaJoinHandler(StationInfo *info)
{
if (info == NULL)
{
printf("HotspotStaJoin:info is null.\r\n");
}
else
{
printf("New Sta Join\n");
osThreadAttr_t attr;
attr.name = "HotspotStaJoinTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 2048;
attr.priority = 24;
if (osThreadNew((osThreadFunc_t)HotspotStaJoinTask, NULL, &attr) == NULL) {
printf("HotspotStaJoin:create task fail!\r\n");
}
}
}
static void OnHotspotStateChangedHandler(int state)
{
printf("HotspotStateChanged:state is %d.\r\n", state);
if (state == WIFI_HOTSPOT_ACTIVE)
{
printf("wifi hotspot active.\r\n");
}
else
{
printf("wifi hotspot noactive.\r\n");
}
}
static void OnHotspotStaLeaveHandler(StationInfo *info)
{
if (info == NULL)
{
printf("HotspotStaLeave:info is null.\r\n");
}
else
{
static char macAddress[32] = {0};
unsigned char* mac = info->macAddress;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("HotspotStaLeave: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
g_ap_enabled--;
}
}
static void my_led_example(void)
{
osThreadAttr_t attr;
attr.attr_bits = 0;
attr.name = "wifi_ap";
attr.cb_mem = NULL;
attr.cb_size = 0;
attr.priority = 24;
attr.stack_size = 10240;
if(osThreadNew((osThreadFunc_t)WifiAPTask,NULL,&attr) == NULL)
{
printf("Falied to create adc_func!\n");
}
}
SYS_RUN(my_led_example);
运行效果