现在想有静态IP改为动态获取IP?请问如何修改??
long ConfigureSimpleLinkToDefaultState() {SlVersionFullver = {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-modeif (ROLE_STA != lMode){if (ROLE_AP == lMode){// If the device is in AP mode, we need to wait for this event// before doing anythingwhile(!IS_IP_ACQUIRED(g_ulStatus)){ #ifndef SL_PLATFORM_MULTI_THREADED_SlNonOsMainLoopTask(); #endif}}// Switch to STA role and restartlRetVal = 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 againif (ROLE_STA != lRetVal){// We don't want to proceed if the device is not coming up in STA-modereturn DEVICE_NOT_IN_STATION_MODE;}}// Get the device's version-informationucConfigOpt = 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 profileslRetVal = sl_WlanProfileDel(0xFF);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){// Waitwhile(IS_CONNECTED(g_ulStatus)){ #ifndef SL_PLATFORM_MULTI_THREADED_SlNonOsMainLoopTask(); #endif}}printf("Enable DHCP!\r\n");// Enable DHCP client //lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);/* 关闭了DHCP ,使用自定义的IP地址 */SlNetCfgIpV4Args_t ipV4;/**/ipV4.ipV4= (_u32)SL_IPV4_VAL(ipv4_loc.ip[0],ipv4_loc.ip[1],ipv4_loc.ip[2],ipv4_loc.ip[3]);// _u32 IP addressipV4.ipV4Mask= (_u32)SL_IPV4_VAL(ipv4_loc.mask[0],ipv4_loc.mask[1],ipv4_loc.mask[2],ipv4_loc.mask[3]);// _u32 Subnet mask for this STA/P2PipV4.ipV4Gateway= (_u32)SL_IPV4_VAL(ipv4_loc.gw[0],ipv4_loc.gw[1],ipv4_loc.gw[2],ipv4_loc.gw[3]);// _u32 Default gateway addressipV4.ipV4DnsServer = (_u32)SL_IPV4_VAL(ipv4_loc.dns[0],ipv4_loc.dns[1],ipv4_loc.dns[2],ipv4_loc.dns[3]);// _u32 DNS server addresslRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE,IPCONFIG_MODE_ENABLE_IPV4,sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipV4);ASSERT_ON_ERROR(lRetVal);// Disable scanucConfigOpt = 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 powerucPower = 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);// Set PM policy to normallRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);ASSERT_ON_ERROR(lRetVal);// Unregister mDNS serviceslRetVal = 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 }
Viki Shi:
Setting a static IP address to the device working in STA mode or P2P client.
The IP address will be stored in the FileSystem.
In order to disable the static IP and get the address assigned from DHCP one should use SL_STA_P2P_CL_IPV4_DHCP_SETSlNetCfgIpV4Args_t ipV4;
ipV4.ipV4 = (_u32)SL_IPV4_VAL(10,1,1,201); // _u32 IP address
ipV4.ipV4Mask = (_u32)SL_IPV4_VAL(255,255,255,0); // _u32 Subnet mask for this STA/P2P
ipV4.ipV4Gateway = (_u32)SL_IPV4_VAL(10,1,1,1); // _u32 Default gateway address
ipV4.ipV4DnsServer = (_u32)SL_IPV4_VAL(8,16,32,64); // _u32 DNS server addresssl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE,IPCONFIG_MODE_ENABLE_IPV4,sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipV4);
sl_Stop(0);
sl_Start(NULL,NULL,NULL);
user5496770:
回复 Viki Shi:
获取的只有IP与网关,如何同时得到子网掩码???
Viki Shi:
回复 user5496770:
SL_IPV4_STA_P2P_CL_GET_INFO:23 Get IP address from WLAN station or P2P client. A DHCP flag is returned to indicate if the IP address is static or from DHCP.45 _u8 len = sizeof(SlNetCfgIpV4Args_t);6 _u8 dhcpIsOn = 0;7 SlNetCfgIpV4Args_t ipV4 = {0};8 sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&dhcpIsOn,&len,(_u8 *)&ipV4);910 printf("DHCP is %s IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d DNS %d.%d.%d.%d\n",11(dhcpIsOn > 0) ? "ON" : "OFF",12SL_IPV4_BYTE(ipV4.ipV4,3),SL_IPV4_BYTE(ipV4.ipV4,2),SL_IPV4_BYTE(ipV4.ipV4,1),SL_IPV4_BYTE(ipV4.ipV4,0),13SL_IPV4_BYTE(ipV4.ipV4Mask,3),SL_IPV4_BYTE(ipV4.ipV4Mask,2),SL_IPV4_BYTE(ipV4.ipV4Mask,1),SL_IPV4_BYTE(ipV4.ipV4Mask,0),14SL_IPV4_BYTE(ipV4.ipV4Gateway,3),SL_IPV4_BYTE(ipV4.ipV4Gateway,2),SL_IPV4_BYTE(ipV4.ipV4Gateway,1),SL_IPV4_BYTE(ipV4.ipV4Gateway,0),15SL_IPV4_BYTE(ipV4.ipV4DnsServer,3),SL_IPV4_BYTE(ipV4.ipV4DnsServer,2),SL_IPV4_BYTE(ipV4.ipV4DnsServer,1),SL_IPV4_BYTE(ipV4.ipV4DnsServer,0));
user5496770:
回复 Viki Shi:
请问这个自动获取函数,与你给我的自动获取函数有什么区别吗?
lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);