Station、AP模式配置
一、实验名称
Station、AP模式配置
二、实验简介
1、Wifi
目前网络中最著名的是有线网络以太网(Ethernet),但是无线网络 WLAN 也是一个很有前景的发展领域,虽然可能不会完全取代以太网,但是它正拥有越来越多的用户,无线网络中最有前景的是 Wifi。
IEEE 802.11 定义了介质访问接入控制层和物理层。物理层定义了工作在 2.4GHz 的 ISM频段上的两种无线调频方式和一种红外传输的方式,总数据传输速率设计为 2Mbit/s。两个设备之间的通信可以自由直接(ad hoc)的方式进行,也可以在基站(Base Station,BS)或者访问点(Access Point,AP)的协调下进行。
wifi共有14个信道
工作原理
STA和AP的连接过程分为三个阶段
分为三个阶段:扫描、认证、关联
客户端首次连接AP
客户端在连接之后,,如果没有发现新的符合条件的AP站点,那么就需要经过漫游过程:
2、STA模式
每一个连接到无线网络中的终端(如笔记本电脑、PDA及其它可以联网的用户设备)都可称为一个站点。
3、AP模式
也就是无线接入点,是一个无线网络的创建者,是网络的中心节点。一般家庭或办公室使用的无线路由器就是一个AP。
三、实验器材
硬件:CC3200开发板
软件:LaunchPad开发环境
四、实验步骤
1、配置Station
Station模式的作用是为了作为终端设备,连接通信网中的AP
按照实验步骤,我们将文件设置为自己ap信息
配置成功后
打开串口调试助手
查看实验结果
2、配置AP模式
五、核心代码分析
1、Station
//远程服务器域名宏定义
#define HOST_NAME "www.ti.com"
void WlanStationMode( void *pvParameters )
{
long lRetVal = -1;
//初始化变量
InitializeAppVariables();
//
// Following function configure the device to default state by cleaning
// the persistent settings stored in NVMEM (viz. connection profiles &
// policies, power policy etc)
//
// Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton
//
// Note that all profiles and persistent settings that were done on the
// device will be lost
//将 SimpLink 配置为默认工作状态
lRetVal = ConfigureSimpleLinkToDefaultState();
if(lRetVal < 0)
{
if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
{
UART_PRINT("Failed to configure the device in its default state\n\r");
}
LOOP_FOREVER();
}
UART_PRINT("Device is configured in default state \r\n");
//启动 simplink
lRetVal = sl_Start(0, 0, 0);
if (lRetVal < 0 || ROLE_STA != lRetVal)
{
UART_PRINT("Failed to start the device \n\r");
LOOP_FOREVER();
}
UART_PRINT("Device started as STATION \r\n");
//
//CC3200 连接到相应 AP
lRetVal = WlanConnect();
if(lRetVal < 0)
{
UART_PRINT("Failed to establish connection w/ an AP \n\r");
LOOP_FOREVER();
}
UART_PRINT("Connection established w/ AP and IP is aquired \r\n");
UART_PRINT("Pinging...! \r\n");
//
// 通过 ping 网关来检测链路状态
lRetVal = CheckLanConnection();
if(lRetVal < 0)
{ UART_PRINT("Device couldn't ping the gateway \n\r");
LOOP_FOREVER();
}
// Turn on GREEN LED when device gets PING response from AP
GPIO_IF_LedOn(MCU_EXECUTE_SUCCESS_IND);
//
// 通过 ping 远程服务器检测因特网是否可用
lRetVal = CheckInternetConnection();
if(lRetVal < 0)
{
UART_PRINT("Device couldn't ping the external host \n\r");
LOOP_FOREVER();
}
// 点亮黄灯表示已经 ping 通远程服务器
GPIO_IF_LedOn(MCU_ORANGE_LED_GPIO);
UART_PRINT("Device pinged both the gateway and the external host \n\r");
UART_PRINT("WLAN STATION example executed successfully \n\r");
//
// power off the network processor
//
lRetVal = sl_Stop(SL_STOP_TIMEOUT);
LOOP_FOREVER();
}
2、AP
WlanAPMode(void *pvParameters)
void WlanAPMode( void *pvParameters )
{
int iTestResult = 0;
unsigned char ucDHCP;
long lRetVal = -1;
//初始化变量
InitializeAppVariables();
//配置 cc3200 在默认工作状态
lRetVal = ConfigureSimpleLinkToDefaultState();
if(lRetVal < 0)
{
if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
UART_PRINT("Failed to configure the device in its default state \n\r");
LOOP_FOREVER();
}
UART_PRINT("\rDevice is configured in default state \n ");
//启动 simplink
lRetVal = sl_Start(NULL,NULL,NULL);
if (lRetVal < 0)
{
UART_PRINT("Failed to start the device \n\r");
LOOP_FOREVER();
}
//由于默认为 station 模式,需要设置为 AP 模式
if(lRetVal != ROLE_AP)
{
if(ConfigureMode(ROLE_AP) != ROLE_AP)
{
UART_PRINT("Unable to set AP mode, exiting Application...\n\r");
sl_Stop(SL_STOP_TIMEOUT);
LOOP_FOREVER();
}
}
while(!IS_IP_ACQUIRED(g_ulStatus))
{
//looping till ip is acquired
}
unsigned char len = sizeof(SlNetCfgIpV4Args_t);
SlNetCfgIpV4Args_t ipV4 = {0};
//获取网络配置信息
lRetVal = sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&ucDHCP,&len,
(unsigned char *)&ipV4); if (lRetVal < 0)
{
UART_PRINT("Failed to get network configuration \n\r");
LOOP_FOREVER();
}
UART_PRINT("\r\n Connect a client to Device\r\n");
//等待客户端接入
while(!IS_IP_LEASED(g_ulStatus))
{
;
}
UART_PRINT("Client is connected to Device\r\n");
//向客户端发起 ping 测试
iTestResult = PingTest(g_ulStaIp);
if(iTestResult < 0)
{
UART_PRINT("Ping to client failed \n\r");
}
UNUSED(ucDHCP);
UNUSED(iTestResult);
// Switch off Network processor
// lRetVal = sl_Stop(SL_STOP_TIMEOUT);
UART_PRINT("\r\nWLAN AP example executed successfully");
while(1);
}
六、实验反思
这次实验提供一个让我们初步熟悉短距离通信中作为最核心功能的STATION模式和AP模式的基本功能,为之后的实验做好了准备工作。
这次实验,我非常感兴趣的短距离通信技术本身。老师在上课时讲了很多关于wifi技术的知识,wifi能过多点接入,同步传输的主要得益于频分复用。
通过阅读源码发现,大部分功能其实都是在调用FreeRTOS的api。通过查阅资料得知,在嵌入式领域中,嵌入式实时操作系统正得到越来越广泛的应用。采用嵌入式实时操作系统(RTOS)可以更合理、更有效地利用CPU的资源,简化应用软件的设计,缩短系统开发时间,更好地保证系统的实时性和可靠性。作为一个轻量级的操作系统,功能包括:任务管理、时间管理、信号量、消息队列、内存管理、记录功能、软件定时器、协程等,可基本满足较小系统的需要。
并且在学习的过程中,发现了一个嵌入式开发的工程师自己制作的教程,关于代码部分的分析还是有很多让人有收获的地方。
CC3200WLAN参数的设置与获取