示例#1
0
bool GS_API_JoinNetwork() {
     bool joined = false;
     uint8_t security = atoi(apiNetworkConfig.security);
     // Set all the configuration options

     // Check for DHCP Enabled
     if (atoi(apiNetworkConfig.dhcpEnabled)) {
          if (!gs_api_handle_cmd_resp(AtLibGs_DHCPSet(1)))
               return joined;
     } else {
          if (!gs_api_handle_cmd_resp(AtLibGs_DHCPSet(0)))
               return joined;

          if (!gs_api_handle_cmd_resp(
                   AtLibGs_IPSet((int8_t*)apiNetworkConfig.staticIp,
                                 (int8_t*)apiNetworkConfig.subnetMask,
                                 (int8_t*)apiNetworkConfig.gatewayIp)))
               return joined;
     }

     // Check security
     switch (security) {
     case 0:
          // Set all
          // Don't check these since some might not be valid anyway
          AtLibGs_SetPassPhrase((int8_t*)apiNetworkConfig.passphrase);

          AtLibGs_SetWepKey(atoi(apiNetworkConfig.wepId), apiNetworkConfig.wepKey);


          AtLibGs_SetAuthMode(0);
          break;

     case 1:
          // Set none
          break;

     case 2:
          // Set WEP
          if (!gs_api_handle_cmd_resp(
                   AtLibGs_SetWepKey(atoi(apiNetworkConfig.wepId),
                                     apiNetworkConfig.wepKey)))
               return joined;

          if (!gs_api_handle_cmd_resp(AtLibGs_SetAuthMode(0)))
               return joined;
          break;

     case 4:
     case 8:
     case 16:
     case 32:
     case 64:
          // Set WPA
          if (!gs_api_handle_cmd_resp(AtLibGs_SetPassPhrase((int8_t*)apiNetworkConfig.passphrase)))
               return joined;
          break;

     default:
          // nothing
          break;
     }

     // Set the security mode
     if(!gs_api_handle_cmd_resp(AtLibGs_SetSecurity(security)))
          return joined;

     // Set adhoc or infrastructure
     if(!gs_api_handle_cmd_resp(AtLibGs_Mode(atoi(apiNetworkConfig.connType))))
          return joined;


     // Associate
     if(gs_api_handle_cmd_resp(AtLibGs_Assoc((int8_t*)apiNetworkConfig.ssid,(int8_t*)"", (int8_t*)apiNetworkConfig.channel))){
          joined = true;
          // Clear all data handler functions
          memset(cidDataHandlers, 0, sizeof(cidDataHandlers));
     }

     return joined;
}
/**
*  @brief  Set up WiFi network
*
*  initialize and set up WiFi parameters for desired WiFi network
*
*  @param  Network Configuration struct
*
*  @return True if successful
*/
bool GS_API_SetupWifiNetwork(HOST_APP_NETWORK_CONFIG_T* apiNetCfg) {
     bool set = false;
     uint8_t security = atoi(apiNetCfg->security);
     // Set all the configuration options

     // Check for DHCP Enabled
     if (atoi(apiNetCfg->dhcpEnabled)) {
          if (!gs_api_handle_cmd_resp(AtLibGs_DHCPSet(1)))
          {
        	  return set;
          }
     } else {
          if (!gs_api_handle_cmd_resp(AtLibGs_DHCPSet(0)))
               return set;

          if (!gs_api_handle_cmd_resp(
                   AtLibGs_IPSet((int8_t *) apiNetCfg->staticIp,
                                 (int8_t *) apiNetCfg->subnetMask,
                                 (int8_t *) apiNetCfg->gatewayIp)))
               return set;
     }

     // Check security
     switch (security) {
     case 0:
          // Set all
          // Don't check these since some might not be valid anyway
          if (!gs_api_handle_cmd_resp(
        		  AtLibGs_SetPassPhrase((int8_t *) apiNetCfg->passphrase)))
        	  return set;

          if (!gs_api_handle_cmd_resp(AtLibGs_SetAuthMode(0)))
        	  return set;
          break;

     case 1:
          // Set none
          break;

     case 2:
          // Set WEP
          if (!gs_api_handle_cmd_resp(
                   AtLibGs_SetWepKey(atoi(apiNetCfg->wepId),
                		   	   	   	   	   apiNetCfg->wepKey)))
               return set;

          if (!gs_api_handle_cmd_resp(AtLibGs_SetAuthMode(0)))
               return set;
          break;

     case 4:
     case 8:
     case 16:
     case 32:
     case 64:
          // Set WPA
          if (!gs_api_handle_cmd_resp(AtLibGs_SetPassPhrase((int8_t *) apiNetCfg->passphrase)))
               return set;
          break;

     default:
          // nothing
          break;
     }

     // Set the security mode
     if(!gs_api_handle_cmd_resp(AtLibGs_SetSecurity(security)))
          return set;

     // Set adhoc or infrastructure
     if(!gs_api_handle_cmd_resp(AtLibGs_Mode(atoi(apiNetCfg->connType))))
          return set;

     return set;
}