CC3200的SmartConfig 断开重连
由于产品需要,用CC3200这颗芯片做smarrconfig (用的是TI的例程)目前没有配置上路由器,不过设备断开后,或者断电后不能重新链接之前的路由器,有什么方法可以让CC3200断线重连功能呢?能否给出个具体的步骤和方法,接口等,由于第一次使用CC3200,目前学的很吃力。希望有大神指教。谢谢
建议参考一下SDK中的例子cc3200-sdk\example\connection_policy
关于详细的测试备注如下:
static long SetConnectionPolicy()
{
unsigned char policyVal;
long lRetVal = -1;
/* Clear all stored profiles and reset the policies */
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0);
ASSERT_ON_ERROR(lRetVal);
//----------------------------------------------(1)通过增加profile方式连接AP-OK-----------------------------------------------
//Add Profile
/* user needs to change SSID_NAME = "<Secured AP>"
SECURITY_TYPE = SL_SEC_TYPE_WPA
SECURITY_KEY = "<password>"
and set the priority as per requirement to connect with a secured AP */
SlSecParams_t secParams;
secParams.Key = SECURITY_KEY;
secParams.KeyLen = strlen(SECURITY_KEY);
secParams.Type = SECURITY_TYPE;
lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&secParams,0,1,0);
ASSERT_ON_ERROR(lRetVal);
//set AUTO policy automatically reconnect to one of its stored profiles 自动模式下CC3200会从SPI_Flash中读取存储的profile配置AP信息并连接AP
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),&policyVal, 1 /*PolicyValLen*/);
ASSERT_ON_ERROR(lRetVal);
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in auto mode
//
//注意并未采用主动连接的方式:lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
//-------------------------------------------------------------------------------------------------------------------------
// Disconnect from AP
lRetVal = sl_WlanDisconnect();
if(0 == lRetVal)
{
// Wait
while(IS_CONNECTED(g_ulStatus))
{
#ifndef SL_PLATFORM_MULTI_THREADED
_SlNonOsMainLoopTask();
#endif
}
}
//----------------------------------------------(2)通过SmartConfig方式连接AP-OK-----------------------------------------------
//delete profiles
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);
//set Auto SmartConfig policy
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,1),&policyVal,0);
ASSERT_ON_ERROR(lRetVal);
// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉
ASSERT_ON_ERROR(lRetVal);
// wait for smart config to complete and device to connect to an AP
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask(); //查看NWP返回的消息!---在这处理消息来处理 SimpleLinkGeneralEventHandler()/SimpleLinkWlanEventHandler/..等回调函数!
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in smartconfig mode
//
//备注:配置完sl_WlanPolicySet()后并未调用lRetVal = sl_WlanSmartConfigStart()但是通过手机Smart Starter进行SmartConfig也能配置CC3200连接AP!---而在provisioning_smartconfig例程中是调用了的!
//-------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------(3)Fast方式连接AP-OK-----------------------------------------------
/* Delete all profiles (0xFF) stored */
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);
// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉
ASSERT_ON_ERROR(lRetVal);
/* Set connection policy to Fast, Device will connect to last connected AP.
* This feature can be used to reconnect to AP */
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); //Fast模式特点可以用在重连AP--YES 测试OK
ASSERT_ON_ERROR(lRetVal);
/* Connect to the open AP */
// lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, 0, 0);
//------------------------增加-----------------------------
secParams.Key = (signed char*)SECURITY_KEY;
secParams.KeyLen = strlen(SECURITY_KEY);
secParams.Type = SECURITY_TYPE;
lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
//---------------------------------------------------------
ASSERT_ON_ERROR(lRetVal);
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
// // Add unsecured AP profile with priority 6 (7 is highest) ---之前的SDK不支持这种模式,必须要增加Profile形式?现在可以删除这个?===Test-OK,可以删除!
// // Because of a limitation in SDK-0.5 which prevents the automatic addition
// // of the profile (fast), the application is required to explicitly add it.
// // The limitation shall be addressed in subsequent SDK release, and the below
// // lines for adding the profile can be removed then...!
// secParams.Key = "";
// secParams.KeyLen = 0;
// secParams.Type = SL_SEC_TYPE_OPEN;
// lRetVal = sl_WlanProfileAdd((signed char*)SSID_NAME,strlen(SSID_NAME), 0, &secParams, 0, 6, 0);
// ASSERT_ON_ERROR(lRetVal);
// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉,注意此时并未保存profile,CC3200选择最近联网的AP中进行连接!
ASSERT_ON_ERROR(lRetVal);
/* Wait for the connection to be established */
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in FAST mode
//
//-------------------------------------------------------------------------------------------------------------------------
//remove all policies
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(0,0,0,0,0),&policyVal,0);
ASSERT_ON_ERROR(lRetVal);
return SUCCESS;
}
补充一个UDP测试的AP断开重连的程序
if(Flag_SL_WLAN_DISCONNECT_EVENT==1) //每次AP断开后都会进入到SimpleLinkWlanEventHandler()函数中,产生SL_WLAN_DISCONNECT_EVENT事件---测试OK
{
unsigned char policyVal;
UART_PRINT("AP Disconnect Try Reconnect\n\r");
// lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); //通过Fast模式无法重连AP,
// (1)
// ResetNwp(); //可能是在通信过程中突然将AP关闭后,CC3200并未保存AP的信息,导致即使重启NWP也无法连接AP现象,即使重启好了AP也不行
// 使能profile重启NWP也无法连接AP,而例程中的方法是在AP连接的状态下将NWP重启,是可以连接AP的,可能在NWP重启sl_stop时保存了AP信息!
// WlanConnect(); //通过AP断开后启动OK,自己重连的方式测试OK---每次AP断开后都可以进入到SimpleLinkWlanEventHandler()中断函数中,
// (2) 可以自动重连
// lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),&policyVal, 1 /*PolicyValLen*/);
// ASSERT_ON_ERROR(lRetVal);
// ResetNwp();
// (3) 在(2)基础上什么也不用做,直接等待,CC3200会自动重连的!然后连接建立UDP开始发送数据
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
CC3200在连接到AP后保存Profile,在CC3200再次启动后,注意不要将所有的Profile删除,否则就无法连接到你之前记录的路由器AP上了,在程序中查找
//*****************************************************************************
//! \brief This function puts the device in its default state. It:
//! - Set the mode to STATION
//! - Configures connection policy to Auto and AutoSmartConfig
//! - Deletes all the stored profiles -----------------------------------------------------------这个地方不要打开,否则会删除所有的Profile
//! - Enables DHCP
//! - Disables Scan policy
//! - Sets Tx power to maximum
//! - Sets power policy to normal
//! - Unregister mDNS services
//! - Remove all filters
//!
//! \param none
//! \return On success, zero is returned. On error, negative is returned
//*****************************************************************************
static long ConfigureSimpleLinkToDefaultState()
SlVersionFull ver = {0};
_WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0};
unsigned char ucVal = 1;
unsigned char ucConfigOpt = 0;
unsigned char ucConfigLen = 0;
unsigned char ucPower = 0;
long lRetVal = -1;
long lMode = -1;
lMode = sl_Start(0, 0, 0);
ASSERT_ON_ERROR(lMode);
// If the device is not in station-mode, try configuring it in station-mode
if (ROLE_STA != lMode)
{
if (ROLE_AP == lMode)
{
// If the device is in AP mode, we need to wait for this event
// before doing anything
while(!IS_IP_ACQUIRED(g_ulStatus))
{
#ifndef SL_PLATFORM_MULTI_THREADED
_SlNonOsMainLoopTask();
#endif
}
}
// Switch to STA role and restart
lRetVal = sl_WlanSetMode(ROLE_STA);
ASSERT_ON_ERROR(lRetVal);
lRetVal = sl_Stop(0xFF);
ASSERT_ON_ERROR(lRetVal);
lRetVal = sl_Start(0, 0, 0);
ASSERT_ON_ERROR(lRetVal);
// Check if the device is in station again
if (ROLE_STA != lRetVal)
{
// We don't want to proceed if the device is not coming up in STA-mode
return DEVICE_NOT_IN_STATION_MODE;
}
}
// Get the device's version-information
ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
ucConfigLen = sizeof(ver);
lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,
&ucConfigLen, (unsigned char *)(&ver));
ASSERT_ON_ERROR(lRetVal);
UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION);
UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3],
ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1],
ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3],
ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1],
ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]);
// Set connection policy to Auto + SmartConfig
// (Device's default connection policy)
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
ASSERT_ON_ERROR(lRetVal);
// Remove all profiles
lRetVal = sl_WlanProfileDel(0xFF);------------------------------------------------------------------不要删除所有的Profile
ASSERT_ON_ERROR(lRetVal);
//
// Device in station-mode. Disconnect previous connection if any
// The function returns 0 if 'Disconnected done', negative number if already
// disconnected Wait for 'disconnection' event if 0 is returned, Ignore
// other return-codes
//
lRetVal = sl_WlanDisconnect(); //会把标志位置位,中断是在后面发生的!在后面清除标志位
if(0 == lRetVal)
{
// Wait
while(IS_CONNECTED(g_ulStatus))
{
#ifndef SL_PLATFORM_MULTI_THREADED
_SlNonOsMainLoopTask();
#endif
}
}
// Enable DHCP client
lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
ASSERT_ON_ERROR(lRetVal);
// Disable scan
ucConfigOpt = SL_SCAN_POLICY(0);
lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
ASSERT_ON_ERROR(lRetVal);
// Set Tx power level for station mode
// Number between 0-15, as dB offset from max power - 0 will set max power
ucPower = 0;
lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
ASSERT_ON_ERROR(lRetVal);
//add
//lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0);
// Set PM policy to normal
lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
ASSERT_ON_ERROR(lRetVal);
// Unregister mDNS services
lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
ASSERT_ON_ERROR(lRetVal);
// Remove all 64 filters (8*8)
memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask,
sizeof(_WlanRxFilterOperationCommandBuff_t));
ASSERT_ON_ERROR(lRetVal);
lRetVal = sl_Stop(SL_STOP_TIMEOUT);
ASSERT_ON_ERROR(lRetVal);
InitializeAppVariables();
return lRetVal; // Success
}
这个回答我看过了,多谢你的回复
你的回答我看过,不过我试了试还是不行
我试了也不行 请问你搞定了吗?