微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI WIFI设计交流 > 请问CC3200的SmartConfig如何保存连接profile呢?

请问CC3200的SmartConfig如何保存连接profile呢?

时间:10-02 整理:3721RD 点击:

我根据httpserver的这个例程写了个用smartconfig连接外部路由器的程序在cc3200 launchpad上运行,但是麻烦的是每次断电再插上都要重新使用smartconfig配置连接。

我对httpserver的修改主要是下边这块:

static int g_iWlanAccess = -1; //初始化

void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)

...

case SL_WLAN_CONNECT_EVENT:
{
SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);

//
// Information about the connected AP (like name, MAC etc) will be
// available in 'slWlanConnectAsyncResponse_t'-Applications
// can use it if required
//
// slWlanConnectAsyncResponse_t *pEventData = NULL;
// pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
//

// Copy new connection SSID and BSSID to global parameters
memcpy(g_ucConnectionSSID,pWlanEvent->EventData.
STAandP2PModeWlanConnected.ssid_name,
pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);
memcpy(g_ucConnectionBSSID,
pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,
SL_BSSID_LENGTH);

UART_PRINT("[WLAN EVENT] STA Connected to the AP: %s ,"
"BSSID: %x:%x:%x:%x:%x:%x\n\r",
g_ucConnectionSSID,g_ucConnectionBSSID[0],
g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
g_ucConnectionBSSID[5]);

g_iWlanAccess = 0; // 在这里将g_iWlanAccess设置为0
}

....

在static long ConfigureSimpleLinkToDefaultState()中,我期待的是在一开始调用sl_Start的时候就会将g_iWlanAccess设置为0,所以在下边添加了一个判断:


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]);

if (g_iWlanAccess == 0) // 如果已经连上了路由器AP,不在继续设置smartconfig
{
return lRetVal;
}

// 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);
ASSERT_ON_ERROR(lRetVal);

但是上面的代码好像没有起作用,当我断电重插板子或者按reset键后,都重新进入smartconfig模式了。

因为smartconfig是不能读出密码的,只能读到ssd,所以貌似也没上面办法把密码保存起来。那么请问应如何修改代码才能记住这个连接呢?

非常感谢!

因为你在static long ConfigureSimpleLinkToDefaultState()函数里调用了lRetVal = sl_WlanProfileDel(0xFF);,删除了所有的profile,所以系统不会保存用过的profile的SSID和密码。你可以试着不删除profile,然后调用添加profile的函数试试

Thanks Ken. 

但是我sl_WlanProfileDel这块只是在之前没有连上Wlan的时候才会调用啊,请看下边的红色部分,如果把sl_WlanProfileDel去掉会不会在ssid变化的时候不好用呢?

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]);

if (g_iWlanAccess == 0) 
{
return lRetVal;
}

// (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);
ASSERT_ON_ERROR(lRetVal);

另外在连接的时候我直接调用的smartconfig.c的函数:

Report("Use Smart Config Application to configure the device.\n\r");
//Connect Using Smart Config
lRetVal = SmartConfigConnect();
ASSERT_ON_ERROR(lRetVal);

//Waiting for the device to Auto Connect
while(!IS_IP_ACQUIRED(g_ulStatus))
{
MAP_UtilsDelay(500);
}

请问这里还需要加上其他addprofile的代码吗?

非常感谢!

同问,调用SmartConfig.c中的SmartConfigConnect()函数之后,是否就已经将AP添加到了profile里面?

配置完一次之后该如何连接已经保存了的AP

已解决,按键进入Smartconfig配置模式,配置之后会自动保存到profile table,无需再手动添加。不进入配置模式时则自动连接即可。断电可用

//
//
// Reset The state of the machine
//
Network_IF_ResetMCUStateMachine();

// Reset CC3200 Network State Machine XQ 2015-05-03 10:07:59
InitializeAppVariables();
//
// Assumption is that the device is configured in station mode already
// and it is in its default state
//
lRetVal = sl_Start(NULL,NULL,NULL);

if (lRetVal < 0 || lRetVal != ROLE_STA)
{
UART_PRINT("Failed to start the device \n\r");
LOOP_FOREVER();
}

UART_PRINT("Started SimpleLink Device: STA Mode\n\r");//XQ END

if(GPIO_IF_Get(4,GPIOA1_BASE, 0x20)!=0)//SW3
{
DBG_PRINT("HIB: Connect to our AP using SmartConfig method \n\r");
/* Connect to our AP using SmartConfig method */
lRetVal = SmartConfigConnect();
if(lRetVal < 0)
{
ERR_PRINT(lRetVal);
}
//Waiting for the device to Auto Connect
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
MAP_UtilsDelay(500);
}


}
else
{
//Waiting for the device to Auto Connect
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
MAP_UtilsDelay(500);
}
}

你好,我想问下its default state具体是什么呢?我就把sl_WlanProfileDel(0xFF)l注释掉了,其他的还是ConfigureSimpleLinkToDefaultState()函数里面。但是还是连不上。

1、那个函数就是恢复到最初CC3200的状态,将所有的连接的Profile均删除

2、处理把sl_WlanProfileDel(0xFF)l注释掉,还需要注意Connectify连接的方式配置,如下代码进行了备注和测试


//****************************************************************************
//
//! \brief Configuring the Connection Policy
//!
//! This function configures different Connection Policy such as FAST,AUTO,OPEN
//!
//! \param[in] None
//!
//! \return 0 on success else error code
//!
//! \note
//
//****************************************************************************
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) ---//===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;

}


int main(void)
{
long lRetVal = -1;
//
// Initialize Board configurations
//
BoardInit();

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
//
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 \n\r");

//
// Assumption is that the device is configured in station mode already
// and it is in its default state
//
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 \n\r");

/* Connect to our AP using SmartConfig method */
lRetVal = SetConnectionPolicy(); //测试三种方式使CC3200连接AP网络---均测试OK
if(lRetVal < 0)
{
ERR_PRINT(lRetVal);
LOOP_FOREVER();
}

// revert all settings
lRetVal = sl_WlanProfileDel(0xFF);
if(lRetVal < 0)
{
ERR_PRINT(lRetVal);
LOOP_FOREVER();
}

lRetVal = sl_Stop(SL_STOP_TIMEOUT);

LOOP_FOREVER();
}

好的,我现在问题解决了

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top