mirror of
https://github.com/openharmony/device_bearpi_bearpi_hm_nano.git
synced 2026-07-01 22:14:04 -04:00
@@ -87,7 +87,7 @@ WifiErrorCode GetStationList(StationInfo* result, unsigned int* size)
|
||||
```c
|
||||
static BOOL WifiAPTask(void)
|
||||
{
|
||||
//延时2S便于查看日志
|
||||
//延时2S便于查看日志
|
||||
osDelay(200);
|
||||
|
||||
//注册wifi事件的回调函数
|
||||
@@ -95,11 +95,18 @@ static BOOL WifiAPTask(void)
|
||||
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
|
||||
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
|
||||
error = RegisterWifiEvent(&g_wifiEventHandler);
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("RegisterWifiEvent failed, error = %d.\r\n",error);
|
||||
return -1;
|
||||
}
|
||||
printf("RegisterWifiEvent succeed!\r\n");
|
||||
//检查热点模式是否使能
|
||||
if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE)
|
||||
{
|
||||
printf("Wifi station is actived.\r\n");
|
||||
return -1;
|
||||
}
|
||||
//设置指定的热点配置
|
||||
HotspotConfig config = {0};
|
||||
|
||||
@@ -110,7 +117,8 @@ static BOOL WifiAPTask(void)
|
||||
config.channelNum = 7;
|
||||
|
||||
error = SetHotspotConfig(&config);
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("SetHotspotConfig failed, error = %d.\r\n", error);
|
||||
return -1;
|
||||
}
|
||||
@@ -118,54 +126,22 @@ static BOOL WifiAPTask(void)
|
||||
|
||||
//启动wifi热点模式
|
||||
error = EnableHotspot();
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("EnableHotspot failed, error = %d.\r\n", error);
|
||||
return -1;
|
||||
}
|
||||
printf("EnableHotspot succeed!\r\n");
|
||||
|
||||
//检查热点模式是否使能
|
||||
if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) {
|
||||
printf("Wifi station is not actived.\r\n");
|
||||
return -1;
|
||||
}
|
||||
printf("Wifi station is actived!\r\n");
|
||||
|
||||
//启动dhcp
|
||||
g_lwip_netif = netifapi_netif_find("ap0");
|
||||
if (g_lwip_netif) {
|
||||
ip4_addr_t bp_gw;
|
||||
ip4_addr_t bp_ipaddr;
|
||||
ip4_addr_t bp_netmask;
|
||||
|
||||
IP4_ADDR(&bp_gw, 192, 168, 1, 1); /* input your gateway for example: 192.168.1.1 */
|
||||
IP4_ADDR(&bp_ipaddr, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */
|
||||
IP4_ADDR(&bp_netmask, 255, 255, 255, 0); /* input your netmask for example: 255.255.255.0 */
|
||||
|
||||
err_t ret = netifapi_netif_set_addr(g_lwip_netif, &bp_ipaddr, &bp_netmask, &bp_gw);
|
||||
if(ret != ERR_OK) {
|
||||
printf("netifapi_netif_set_addr failed, error = %d.\r\n", ret);
|
||||
return -1;
|
||||
}
|
||||
printf("netifapi_netif_set_addr succeed!\r\n");
|
||||
|
||||
ret = netifapi_dhcps_start(g_lwip_netif, 0, 0);
|
||||
if(ret != ERR_OK) {
|
||||
printf("netifapi_dhcp_start failed, error = %d.\r\n", ret);
|
||||
return -1;
|
||||
}
|
||||
printf("netifapi_dhcps_start succeed!\r\n");
|
||||
|
||||
}
|
||||
|
||||
/****************以下为UDP服务器代码***************/
|
||||
/****************以下为UDP服务器代码,默认IP:192.168.5.1***************/
|
||||
//在sock_fd 进行监听
|
||||
int sock_fd;
|
||||
//服务端地址信息
|
||||
struct sockaddr_in server_sock;
|
||||
|
||||
//创建socket
|
||||
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
||||
{
|
||||
perror("socket is error.\r\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -176,7 +152,8 @@ static BOOL WifiAPTask(void)
|
||||
server_sock.sin_port = htons(8888);
|
||||
|
||||
//调用bind函数绑定socket和地址
|
||||
if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1) {
|
||||
if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
perror("bind is error.\r\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -186,19 +163,22 @@ static BOOL WifiAPTask(void)
|
||||
//客户端地址信息
|
||||
struct sockaddr_in client_addr;
|
||||
int size_client_addr= sizeof(struct sockaddr_in);
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
|
||||
printf("Waiting to receive data...\r\n");
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
ret = recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&client_addr,(socklen_t*)&size_client_addr);
|
||||
if(ret < 0) {
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("UDP server receive failed!\r\n");
|
||||
return -1;
|
||||
}
|
||||
printf("receive %d bytes of data from ipaddr = %s, port = %d.\r\n", ret, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
printf("data is %s\r\n",recvBuf);
|
||||
ret = sendto(sock_fd, recvBuf, strlen(recvBuf), 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
|
||||
if (ret < 0) {
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("UDP server send failed!\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
|
||||
#define AP_SSID "BearPi"
|
||||
#define AP_PSK "BearPi"
|
||||
#define AP_PSK "0987654321"
|
||||
|
||||
#define ONE_SECOND 1
|
||||
#define DEF_TIMEOUT 15
|
||||
@@ -51,11 +51,18 @@ static BOOL WifiAPTask(void)
|
||||
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
|
||||
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
|
||||
error = RegisterWifiEvent(&g_wifiEventHandler);
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("RegisterWifiEvent failed, error = %d.\r\n",error);
|
||||
return -1;
|
||||
}
|
||||
printf("RegisterWifiEvent succeed!\r\n");
|
||||
//检查热点模式是否使能
|
||||
if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE)
|
||||
{
|
||||
printf("Wifi station is actived.\r\n");
|
||||
return -1;
|
||||
}
|
||||
//设置指定的热点配置
|
||||
HotspotConfig config = {0};
|
||||
|
||||
@@ -66,7 +73,8 @@ static BOOL WifiAPTask(void)
|
||||
config.channelNum = 7;
|
||||
|
||||
error = SetHotspotConfig(&config);
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("SetHotspotConfig failed, error = %d.\r\n", error);
|
||||
return -1;
|
||||
}
|
||||
@@ -74,54 +82,22 @@ static BOOL WifiAPTask(void)
|
||||
|
||||
//启动wifi热点模式
|
||||
error = EnableHotspot();
|
||||
if (error != WIFI_SUCCESS) {
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("EnableHotspot failed, error = %d.\r\n", error);
|
||||
return -1;
|
||||
}
|
||||
printf("EnableHotspot succeed!\r\n");
|
||||
|
||||
//检查热点模式是否使能
|
||||
if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) {
|
||||
printf("Wifi station is not actived.\r\n");
|
||||
return -1;
|
||||
}
|
||||
printf("Wifi station is actived!\r\n");
|
||||
|
||||
//启动dhcp
|
||||
g_lwip_netif = netifapi_netif_find("ap0");
|
||||
if (g_lwip_netif) {
|
||||
ip4_addr_t bp_gw;
|
||||
ip4_addr_t bp_ipaddr;
|
||||
ip4_addr_t bp_netmask;
|
||||
|
||||
IP4_ADDR(&bp_gw, 192, 168, 1, 1); /* input your gateway for example: 192.168.1.1 */
|
||||
IP4_ADDR(&bp_ipaddr, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */
|
||||
IP4_ADDR(&bp_netmask, 255, 255, 255, 0); /* input your netmask for example: 255.255.255.0 */
|
||||
|
||||
err_t ret = netifapi_netif_set_addr(g_lwip_netif, &bp_ipaddr, &bp_netmask, &bp_gw);
|
||||
if(ret != ERR_OK) {
|
||||
printf("netifapi_netif_set_addr failed, error = %d.\r\n", ret);
|
||||
return -1;
|
||||
}
|
||||
printf("netifapi_netif_set_addr succeed!\r\n");
|
||||
|
||||
ret = netifapi_dhcps_start(g_lwip_netif, 0, 0);
|
||||
if(ret != ERR_OK) {
|
||||
printf("netifapi_dhcp_start failed, error = %d.\r\n", ret);
|
||||
return -1;
|
||||
}
|
||||
printf("netifapi_dhcps_start succeed!\r\n");
|
||||
|
||||
}
|
||||
|
||||
/****************以下为UDP服务器代码***************/
|
||||
/****************以下为UDP服务器代码,默认IP:192.168.5.1***************/
|
||||
//在sock_fd 进行监听
|
||||
int sock_fd;
|
||||
//服务端地址信息
|
||||
struct sockaddr_in server_sock;
|
||||
|
||||
//创建socket
|
||||
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
||||
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
||||
{
|
||||
perror("socket is error.\r\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -132,7 +108,8 @@ static BOOL WifiAPTask(void)
|
||||
server_sock.sin_port = htons(8888);
|
||||
|
||||
//调用bind函数绑定socket和地址
|
||||
if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1) {
|
||||
if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
perror("bind is error.\r\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -142,19 +119,22 @@ static BOOL WifiAPTask(void)
|
||||
//客户端地址信息
|
||||
struct sockaddr_in client_addr;
|
||||
int size_client_addr= sizeof(struct sockaddr_in);
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
|
||||
printf("Waiting to receive data...\r\n");
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
ret = recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&client_addr,(socklen_t*)&size_client_addr);
|
||||
if(ret < 0) {
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("UDP server receive failed!\r\n");
|
||||
return -1;
|
||||
}
|
||||
printf("receive %d bytes of data from ipaddr = %s, port = %d.\r\n", ret, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
printf("data is %s\r\n",recvBuf);
|
||||
ret = sendto(sock_fd, recvBuf, strlen(recvBuf), 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
|
||||
if (ret < 0) {
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("UDP server send failed!\r\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -162,43 +142,17 @@ static BOOL WifiAPTask(void)
|
||||
/*********************END********************/
|
||||
}
|
||||
|
||||
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 = GetStationList(stainfo, &size);
|
||||
if (error != WIFI_SUCCESS) {
|
||||
printf("HotspotStaJoin:get list fail, error is %d.\r\n", error);
|
||||
return;
|
||||
}
|
||||
sta_list_node = stainfo;
|
||||
for (uint32_t i = 0; i < size; i++, sta_list_node++) {
|
||||
unsigned char* 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_apEnableSuccess++;
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
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("HotspotStaJoin: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
|
||||
g_apEnableSuccess++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -207,7 +161,8 @@ static void OnHotspotStaLeaveHandler(StationInfo *info)
|
||||
{
|
||||
if (info == NULL) {
|
||||
printf("HotspotStaLeave:info is null.\r\n");
|
||||
} else {
|
||||
}
|
||||
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]);
|
||||
@@ -227,7 +182,7 @@ static void OnHotspotStateChangedHandler(int state)
|
||||
}
|
||||
}
|
||||
|
||||
static void WifiApDemo(void)
|
||||
static void Wifi_AP_Demo(void)
|
||||
{
|
||||
osThreadAttr_t attr;
|
||||
|
||||
@@ -239,9 +194,10 @@ static void WifiApDemo(void)
|
||||
attr.stack_size = 10240;
|
||||
attr.priority = 25;
|
||||
|
||||
if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL) {
|
||||
printf("Failed to create WifiAPTask!\r\n");
|
||||
if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL)
|
||||
{
|
||||
printf("Falied to create WifiAPTask!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
APP_FEATURE_INIT(WifiApDemo);
|
||||
APP_FEATURE_INIT(Wifi_AP_Demo);
|
||||
|
||||
Reference in New Issue
Block a user