(1)LwlP (Light weight lP)简介
- LwlP是轻量化的TCP/IP,是一个小型开源的 TCP/IP协议栈
- LwIP的设计目标是用较少的资源实现较完整的TCP/IP协议栈
- LwlP能在操作系统中运行,也能在无操作系统的情况下独立运行
- LwIP提供了Socket API和 Netconn API
(2)LwlP的主要特性
- 支持ARP,ICMP,IGMP
- 支持UDP,TCP,IP(可移植网络通信框架)
- 支持 DNS,PPP,SNMP
- 支持 DHCP,可动态分配IP地址(WIFl支持)
- 。。
(3)BearPi-Nano联网能力
- BearPi-Nano基于Hi3861芯片构建,而 Hi3861自生具备Wifi能力
- AP模式
- 工作于Wifi热点模式,可被其它设备以Wifi方式连接
- STA模式
- 工作于Wifi连接模式,可连接到指定Wifi热点
- 工作于Wifi连接模式,可连接到指定Wifi热点
- AP模式
(4)Wifi模块接口设计
- int Wifi_Init(void);
- int Wifi_Connect( const char*id, const char* pwd );
- int Wifi_Start(void);
- int Wifi_lsOk(void);
- void Wifi_Stop(void);
- char*Wifi_lpAddr(void);
(5)涉及的OH系统接口
(6)Wifi热点连接流程
(7)Wifi联网关键函数实现
编程实验:WIFI模块实现
wifi_connect.h
#ifndef WIFI_CONNECT_H
#define WIFI_CONNECT_H
int Wifi_Init(void);
int Wifi_Connect(const char* id, const char* pwd);
int Wifi_Start(void);
int Wifi_IsOk(void);
void Wifi_Stop(void);
char* Wifi_IpAddr(void);
#endif
wifi_connect.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "wifi_connect.h"
#include "wifi_device.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"
#include "lwip/dhcp.h"
#define WIFI_TIMEOUT 20
#define WLAN_PORT "wlan0"
static int g_WaitResult = 0;
static WifiEvent g_WifiEventHandler = {0};
static struct netif* g_LwipNetif = NULL;
static void ClearWaitResult(void)
{
g_WaitResult = 0;
}
static void SetWaitResult(int result)
{
g_WaitResult = result;
}
static int GetWaitResult(void)
{
return g_WaitResult;
}
static void ToWait(unsigned int sec)
{
while( sec-- && !GetWaitResult() )
{
sleep(1);
}
}
static void OnWifiScanStateChanged(int state, int size)
{
(void)state;
(void)size;
}
static void OnWifiConnectionChanged(int state, WifiLinkedInfo *info)
{
(void)info;
SetWaitResult(state);
}
static void OnHotspotStaJoin(StationInfo *info)
{
(void)info;
}
static void OnHotspotStateChanged(int state)
{
(void)state;
}
static void OnHotspotStaLeave(StationInfo *info)
{
(void)info;
}
int Wifi_Init(void)
{
g_WifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChanged;
g_WifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChanged;
g_WifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoin;
g_WifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeave;
g_WifiEventHandler.OnHotspotStateChanged = OnHotspotStateChanged;
return RegisterWifiEvent(&g_WifiEventHandler);
}
int Wifi_Connect(const char* id, const char* pwd)
{
int ret = WIFI_SUCCESS;
if( !Wifi_IsOk() )
{
ret = id && pwd ? EnableWifi() : ERROR_WIFI_UNKNOWN;
// ret = (ret == WIFI_SUCCESS) ? && IsWifiActive() ? WIFI_SUCCESS : ERROR_WIFI_NOT_AVAILABLE;
if( ret == WIFI_SUCCESS )
{
WifiDeviceConfig config = {0};
int result = 0;
config.securityType = WIFI_SEC_TYPE_PSK;
strcpy(config.ssid, id);
strcpy(config.preSharedKey, pwd);
ret += AddDeviceConfig(&config, &result);
ret += ConnectTo(result);
if( ret == WIFI_SUCCESS )
{
ClearWaitResult();
ToWait(WIFI_TIMEOUT);
ret = (GetWaitResult() > 0) ? WIFI_SUCCESS : ERROR_WIFI_UNKNOWN;
}
}
}
return ret;
}
int Wifi_Start(void)
{
int ret = WIFI_SUCCESS;
if( !Wifi_IsOk() )
{
g_LwipNetif = netifapi_netif_find(WLAN_PORT);
if( g_LwipNetif )
{
int i = WIFI_TIMEOUT;
if( dhcp_start(g_LwipNetif) == ERR_OK )
{
while( ((ret = dhcp_is_bound(g_LwipNetif)) != ERR_OK) && i-- )
{
usleep(200 * 1000);
}
}
if( ret != WIFI_SUCCESS )
{
Wifi_Stop();
}
}
else
{
ret = ERROR_WIFI_UNKNOWN;
}
}
return ret;
}
int Wifi_IsOk(void)
{
return !!g_LwipNetif;
}
void Wifi_Stop(void)
{
dhcp_stop(g_LwipNetif);
g_LwipNetif = NULL;
}
char* Wifi_IpAddr(void)
{
char* ret = NULL;
if( Wifi_IsOk() )
{
ip4_addr_t addr = {0};
ip4_addr_t mask = {0};
ip4_addr_t gw = {0};
netif_get_addr(g_LwipNetif, &addr, &mask, &gw);
if( (addr.addr != 0) && (addr.addr != -1) )
{
ret = ip4addr_ntoa(&addr);
}
else
{
Wifi_Stop();
}
}
return ret;
}