我用wlan_station例程,连上小米路由器后,通信都没有问题。主动把路由器断电,3200会收到disconnect消息,但是路由器上电后3200不会自动重连。
请教下这种断线情况该怎么处理来重连?能够配制成自动重连么?谢谢!
Viki Shi:
请参考本帖: https://e2e.ti.com/support/wireless_connectivity/simplelink_wifi_cc31xx_cc32xx/f/968/p/449742/1623542
Terry Han:
建议参考一下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;
}
HappyTian:
回复 Terry Han:
信息很详细,非常感谢!