Advertisement
cyter

ConfigureSimpleLinkToDefaultState

Dec 3rd, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1. static long ConfigureSimpleLinkToDefaultState()
  2. {
  3.     SlVersionFull   ver = {0};
  4.     _WlanRxFilterOperationCommandBuff_t  RxFilterIdMask = {0};
  5.  
  6.     unsigned char ucVal = 1;
  7.     unsigned char ucConfigOpt = 0;
  8.     unsigned char ucConfigLen = 0;
  9.     unsigned char ucPower = 0;
  10.  
  11.     long lRetVal = -1;
  12.     long lMode = -1;
  13.  
  14.     lMode = sl_Start(0, 0, 0);
  15.     ASSERT_ON_ERROR(lMode);
  16.  
  17.     if(mode==1){//broadcaster
  18.  
  19.         // If the device is not in station-mode, try configuring it in station-mode
  20.         if (ROLE_AP != lMode)
  21.         {
  22.             if (ROLE_STA == lMode)
  23.             {
  24.                 // If the device is in STA mode, we need to wait for this event
  25.                 // before doing anything
  26.                 while(!IS_IP_ACQUIRED(g_ulStatus))
  27.                 {
  28.     #ifndef SL_PLATFORM_MULTI_THREADED
  29.                   _SlNonOsMainLoopTask();
  30.     #endif
  31.                 }
  32.             }
  33.  
  34.             // Switch to AP role and restart
  35.             lRetVal = sl_WlanSetMode(ROLE_AP);
  36.             ASSERT_ON_ERROR(lRetVal);
  37.  
  38.             lRetVal = sl_Stop(0xFF);
  39.             ASSERT_ON_ERROR(lRetVal);
  40.  
  41.             lRetVal = sl_Start(0, 0, 0);
  42.             ASSERT_ON_ERROR(lRetVal);
  43.  
  44.             // Check if the device is in station again
  45.             if (ROLE_AP != lRetVal)
  46.             {
  47.                 // We don't want to proceed if the device is not coming up in AP-mode
  48.                 //return DEVICE_NOT_IN_STATION_MODE;
  49.                 return DEVICE_NOT_IN_AP_MODE;
  50.             }
  51.         }
  52.  
  53.     }else if(mode==2){// receiver
  54.         // If the device is not in station-mode, try configuring it in station-mode
  55.         if (ROLE_STA != lMode)
  56.         {
  57.             if (ROLE_AP == lMode)
  58.             {
  59.                 // If the device is in AP mode, we need to wait for this event
  60.                 // before doing anything
  61.                 while(!IS_IP_ACQUIRED(g_ulStatus))
  62.                 {
  63.     #ifndef SL_PLATFORM_MULTI_THREADED
  64.                   _SlNonOsMainLoopTask();
  65.     #endif
  66.                 }
  67.             }
  68.  
  69.             // Switch to STA role and restart
  70.             lRetVal = sl_WlanSetMode(ROLE_STA);
  71.             ASSERT_ON_ERROR(lRetVal);
  72.  
  73.             lRetVal = sl_Stop(0xFF);
  74.             ASSERT_ON_ERROR(lRetVal);
  75.  
  76.             lRetVal = sl_Start(0, 0, 0);
  77.             ASSERT_ON_ERROR(lRetVal);
  78.  
  79.             // Check if the device is in station again
  80.             if (ROLE_STA != lRetVal)
  81.             {
  82.                 // We don't want to proceed if the device is not coming up in STA-mode
  83.                 return DEVICE_NOT_IN_STATION_MODE;
  84.             }
  85.         }
  86.     }
  87.    
  88.     // Get the device's version-information
  89.     ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
  90.     ucConfigLen = sizeof(ver);
  91.     lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,&ucConfigLen, (unsigned char *)(&ver));
  92.     ASSERT_ON_ERROR(lRetVal);
  93.    
  94.     UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION);
  95.     UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
  96.     ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3],
  97.     ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1],
  98.     ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3],
  99.     ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1],
  100.     ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]);
  101.  
  102.     // Set connection policy to Auto + SmartConfig
  103.     //      (Device's default connection policy)
  104.     lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
  105.     ASSERT_ON_ERROR(lRetVal);
  106.  
  107.     // Remove all profiles
  108.     lRetVal = sl_WlanProfileDel(0xFF);
  109.     ASSERT_ON_ERROR(lRetVal);
  110.  
  111.    
  112.  
  113.     //
  114.     // Device in station-mode. Disconnect previous connection if any
  115.     // The function returns 0 if 'Disconnected done', negative number if already
  116.     // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
  117.     // other return-codes
  118.     //
  119.     if(mode==2){
  120.         lRetVal = sl_WlanDisconnect();
  121.         if(0 == lRetVal)
  122.         {
  123.             // Wait
  124.             while(IS_CONNECTED(g_ulStatus))
  125.             {
  126.     #ifndef SL_PLATFORM_MULTI_THREADED
  127.                   _SlNonOsMainLoopTask();
  128.     #endif
  129.             }
  130.         }
  131.     }
  132.  
  133.     // Enable DHCP client
  134.     lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
  135.     ASSERT_ON_ERROR(lRetVal);
  136.  
  137.     // Disable scan
  138.     ucConfigOpt = SL_SCAN_POLICY(0);
  139.     lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
  140.     ASSERT_ON_ERROR(lRetVal);
  141.  
  142.     // Set Tx power level for station mode
  143.     // Number between 0-15, as dB offset from max power - 0 will set max power
  144.     ucPower = 0;
  145.     lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
  146.             WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
  147.     ASSERT_ON_ERROR(lRetVal);
  148.  
  149.     // Set PM policy to normal
  150.     lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
  151.     ASSERT_ON_ERROR(lRetVal);
  152.  
  153.     // Unregister mDNS services
  154.     lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
  155.     ASSERT_ON_ERROR(lRetVal);
  156.  
  157.     // Remove  all 64 filters (8*8)
  158.     memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
  159.     lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask,
  160.                        sizeof(_WlanRxFilterOperationCommandBuff_t));
  161.     ASSERT_ON_ERROR(lRetVal);
  162.  
  163.     lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  164.     ASSERT_ON_ERROR(lRetVal);
  165.  
  166.     InitializeAppVariables();
  167.    
  168.     return lRetVal; // Success
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement