//***************************************************************************** //! setDeviceName //! //! Sets the name of the Device //! //! Returns: On success, zero is returned. On error, -1 is returned //! //**************************************************************************** int setDeviceName() { int iretVal; unsigned char strDeviceName[32] = DEVICE_NAME; iretVal = sl_NetAppSet (SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen((const char *)strDeviceName), (unsigned char *) strDeviceName); return iretVal; }
//***************************************************************************** //! setApDomainName //! //! Sets the name of the Access Point's Domain Name //! //! Returns: On success, zero is returned. On error, -1 is returned //! //**************************************************************************** int setApDomainName() { int iretVal; unsigned char strDomain[32] = DEVICE_AP_DOMAIN_NAME; unsigned char lenDomain = strlen((const char *)strDomain); iretVal = sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DOMAIN_NAME, lenDomain, (unsigned char*)strDomain); return iretVal; }
bool wifi_setup_ap(const char *ssid, const char *pass, int channel) { uint8_t v; if (sl_WlanSetMode(ROLE_AP) != 0) { return false; } if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (const uint8_t *) ssid) != 0) { return false; } v = strlen(pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v) != 0) { return false; } if (v == SL_SEC_TYPE_WPA && sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(pass), (const uint8_t *) pass) != 0) { return false; } v = channel; if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, &v) != 0) { return false; } sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); { SlNetCfgIpV4Args_t ipcfg; memset(&ipcfg, 0, sizeof(ipcfg)); if (!inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4) || !inet_pton(AF_INET, "255.255.255.0", &ipcfg.ipV4Mask) || /* This means "disable". 0.0.0.0 won't do. */ !inet_pton(AF_INET, "255.255.255.255", &ipcfg.ipV4DnsServer) || /* We'd like to disable gateway too, but DHCP server refuses to start. */ !inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4Gateway) || sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(ipcfg), (uint8_t *) &ipcfg) != 0) { return false; } } { SlNetAppDhcpServerBasicOpt_t dhcpcfg; memset(&dhcpcfg, 0, sizeof(dhcpcfg)); dhcpcfg.lease_time = 900; if (!inet_pton(AF_INET, "192.168.4.20", &dhcpcfg.ipv4_addr_start) || !inet_pton(AF_INET, "192.168.4.200", &dhcpcfg.ipv4_addr_last) || sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(dhcpcfg), (uint8_t *) &dhcpcfg) != 0) { return false; } } sl_Stop(0); sl_Start(NULL, NULL, NULL); if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) { LOG(LL_ERROR, ("DHCP server failed to start")); return false; } LOG(LL_INFO, ("WiFi: AP %s configured", ssid)); return true; }
//***************************************************************************** // //! Set all P2P Configuration to device //! //! \param None //! //! \return status code - Success:0, Failure:-ve // //***************************************************************************** long P2PConfiguration() { unsigned char ucP2PParam[4]; long lRetVal = -1; // Set any p2p option (SL_CONNECTION_POLICY(0,0,0,any_p2p,0)) to connect to // first available p2p device lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),NULL,0); ASSERT_ON_ERROR(lRetVal); // Set the negotiation role (SL_P2P_ROLE_NEGOTIATE). // CC3200 will negotiate with remote device GO/client mode. // Other valid options are: // - SL_P2P_ROLE_GROUP_OWNER // - SL_P2P_ROLE_CLIENT lRetVal = sl_WlanPolicySet(SL_POLICY_P2P, SL_P2P_POLICY(SL_P2P_ROLE_NEGOTIATE, SL_P2P_NEG_INITIATOR_ACTIVE),NULL,0); ASSERT_ON_ERROR(lRetVal); // Set P2P Device name lRetVal = sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, \ NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen(P2P_DEVICE_NAME), (unsigned char *)P2P_DEVICE_NAME); ASSERT_ON_ERROR(lRetVal); // Set P2P device type lRetVal = sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_DEV_TYPE, strlen(P2P_CONFIG_VALUE), (unsigned char*)P2P_CONFIG_VALUE); ASSERT_ON_ERROR(lRetVal); // setting P2P channel parameters ucP2PParam[0] = LISENING_CHANNEL; ucP2PParam[1] = REGULATORY_CLASS; ucP2PParam[2] = OPERATING_CHANNEL; ucP2PParam[3] = REGULATORY_CLASS; // Set P2P Device listen and open channel valid channels are 1/6/11 lRetVal = sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_CHANNEL_N_REGS, sizeof(ucP2PParam), ucP2PParam); ASSERT_ON_ERROR(lRetVal); // Restart as P2P device lRetVal = sl_Stop(SL_STOP_TIMEOUT); lRetVal = sl_Start(NULL,NULL,NULL); if(lRetVal < 0 || lRetVal != ROLE_P2P) { ASSERT_ON_ERROR(P2P_CONFIG_FAILED); } else { UART_PRINT("Connect to %s \n\r",P2P_DEVICE_NAME); } return SUCCESS; }
/*! \brief Enable/Disable the authentication check for http server, By default authentication is disabled. \param[in] enable - false to disable and true to enable the authentication \return None \note \warning */ static _i32 set_authentication_check (_u8 enable) { _NetAppHttpServerGetSet_auth_enable_t auth_enable; _i32 status = -1; auth_enable.auth_enable = enable; status = sl_NetAppSet(SL_NET_APP_HTTP_SERVER_ID, NETAPP_SET_GET_HTTP_OPT_AUTH_CHECK, sizeof(_NetAppHttpServerGetSet_auth_enable_t), (_u8 *)&auth_enable); ASSERT_ON_ERROR(status); return SUCCESS; }
/*! \brief Set the HTTP port This function can be used to change the default port (80) for HTTP request \param[in] num- contains the port number to be set \return None \note \warning */ _i32 set_port_number(_u16 num) { _NetAppHttpServerGetSet_port_num_t port_num; _i32 status = -1; port_num.port_number = num; /*Need to restart the server in order for the new port number configuration *to take place */ status = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR(status); status = sl_NetAppSet (SL_NET_APP_HTTP_SERVER_ID, NETAPP_SET_GET_HTTP_OPT_PORT_NUMBER, sizeof(_NetAppHttpServerGetSet_port_num_t), (_u8 *)&port_num); ASSERT_ON_ERROR(status); status = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR(status); return SUCCESS; }
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { int ret; uint8_t v; SlNetCfgIpV4Args_t ipcfg; SlNetAppDhcpServerBasicOpt_t dhcpcfg; if ((ret = sl_WlanSetMode(ROLE_AP)) != 0) { fprintf(stderr, "sl_WlanSetMode: %d\n", ret); return 0; } if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(cfg->ssid), (const uint8_t *) cfg->ssid)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_SSID): %d\n", ret); return 0; } v = strlen(cfg->pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_SECURITY_TYPE): %d\n", ret); return 0; } if (v == SL_SEC_TYPE_WPA && (ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(cfg->pass), (const uint8_t *) cfg->pass)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_PASSWORD): %d\n", ret); return 0; } v = cfg->channel; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (uint8_t *) &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_CHANNEL): %d\n", ret); return 0; } v = cfg->hidden; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_HIDDEN_SSID, 1, (uint8_t *) &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_HIDDEN_SSID): %d\n", ret); return 0; } memset(&ipcfg, 0, sizeof(ipcfg)); if (!inet_pton(AF_INET, cfg->ip, &ipcfg.ipV4) || !inet_pton(AF_INET, cfg->netmask, &ipcfg.ipV4Mask) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4Gateway) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4DnsServer) || (ret = sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(ipcfg), (uint8_t *) &ipcfg)) != 0) { fprintf(stderr, "sl_NetCfgSet(IPCONFIG_MODE_ENABLE_IPV4): %d\n", ret); return 0; } memset(&dhcpcfg, 0, sizeof(dhcpcfg)); dhcpcfg.lease_time = 900; if (!inet_pton(AF_INET, cfg->dhcp_start, &dhcpcfg.ipv4_addr_start) || !inet_pton(AF_INET, cfg->dhcp_end, &dhcpcfg.ipv4_addr_last) || (ret = sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(dhcpcfg), (uint8_t *) &dhcpcfg)) != 0) { fprintf(stderr, "sl_NetCfgSet(NETAPP_SET_DHCP_SRV_BASIC_OPT): %d\n", ret); return 0; } /* We don't need TI's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); /* Turning the device off and on for the change to take effect. */ sl_Stop(0); sl_Start(NULL, NULL, NULL); osi_Sleep(100); fprintf(stderr, "AP %s configured\n", cfg->ssid); return 1; }
int main() { unsigned char ucP2PParam[4]; long lRetVal = -1; // // Initialize Board configurations // BoardInit(); // // Pinmuxing for GPIO, UART // PinMuxConfig(); // // configure LEDs // GPIO_IF_LedConfigure(LED1|LED2|LED3); // off all LEDs GPIO_IF_LedOff(MCU_ALL_LED_IND); #ifndef NOTERM // // Configuring UART // InitTerm(); #endif // // Display the Application Banner // DisplayBanner(APP_NAME); UART_PRINT("Scan Wi-FI direct device in your handheld device\n\r"); // Initializing the CC3200 device lRetVal = StartDeviceInP2P(); if(lRetVal < 0) { LOOP_FOREVER(__LINE__); } // Set any p2p option (SL_CONNECTION_POLICY(0,0,0,any_p2p,0)) to connect to // first available p2p device sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),NULL,0); // Set the negotiation role (SL_P2P_ROLE_NEGOTIATE). // CC3200 will negotiate with remote device GO/client mode. // Other valid options are: // - SL_P2P_ROLE_GROUP_OWNER // - SL_P2P_ROLE_CLIENT sl_WlanPolicySet(SL_POLICY_P2P, SL_P2P_POLICY(SL_P2P_ROLE_NEGOTIATE, SL_P2P_NEG_INITIATOR_ACTIVE),NULL,0); // Set P2P Device name sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen(P2P_DEVICE_NAME), (unsigned char *)P2P_DEVICE_NAME); // Set P2P device type sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_DEV_TYPE, strlen(P2P_CONFIG_VALUE), (unsigned char*)P2P_CONFIG_VALUE); // setting P2P channel parameters ucP2PParam[0] = LISENING_CHANNEL; ucP2PParam[1] = REGULATORY_CLASS; ucP2PParam[2] = OPERATING_CHANNEL; ucP2PParam[3] = REGULATORY_CLASS; // Set P2P Device listen and open channel valid channels are 1/6/11 sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_CHANNEL_N_REGS, sizeof(ucP2PParam), ucP2PParam); // Restart as P2P device sl_Stop(SL_STOP_TIMEOUT); lRetVal = sl_Start(NULL,NULL,NULL); if(lRetVal < 0 || lRetVal != ROLE_P2P) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(__LINE__); } else { UART_PRINT("Connect to %s \n\r",P2P_DEVICE_NAME); } /* Connect to configure P2P device */ lRetVal = WlanConnect(); if(lRetVal == 0) { GPIO_IF_LedOn(MCU_IP_ALLOC_IND); } else { UART_PRINT("Reset the device and try again\n\r"); LOOP_FOREVER(__LINE__); } DisplayIP(); UART_PRINT("Send TCP packets from your handheld device to CC3200's IP\n\r"); /*After calling this function, you can start sending data to CC3200 IP * address on PORT_NUM */ if(!(IS_CONNECT_FAILED(g_ulStatus))) BsdTcpServer(PORT_NUM); UART_PRINT("Received TCP packets successfully \n\r"); // revert Device into STA mode and power off Network Processor sl_WlanSetMode(ROLE_STA); sl_Stop(SL_STOP_TIMEOUT); UART_PRINT("Test passed, exiting application... \n\r"); while(1) { _SlNonOsMainLoopTask(); } }
static int WifiDefaultSettings(void) { long retval = -1; unsigned char val = 1; unsigned char config, config_len; //filters _WlanRxFilterOperationCommandBuff_t filter_mask = { .Padding = {0} }; //initialize the SimpleLink API retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } //set device in station mode if(retval != ROLE_STA) { if(retval == ROLE_AP) { //we need to wait for an event before doing anything while(!IS_IP_ACQUIRED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //change mode to Station retval = sl_WlanSetMode(ROLE_STA); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } //restart retval = sl_Stop(0xFF); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } if(retval != ROLE_STA) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } } //get SimpleLink version config = SL_DEVICE_GENERAL_VERSION; config_len = sizeof(SlVersionFull); retval = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &config, &config_len, (unsigned char*)&wifi_state.version); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //default connection policy retval = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 0), NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //disconnect retval = sl_WlanDisconnect(); if(retval == 0) { //not yet disconnected while(IS_CONNECTED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //Enable DHCP client retval = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //Disable scan policy config = SL_SCAN_POLICY(0); retval = sl_WlanPolicySet(SL_POLICY_SCAN, config, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //Set Tx power level for station mode //Number between 0-15, as dB offset from max power - 0 will set max power val = 0; retval = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&val); if(retval<0) { RETURN_ERROR(retval, "WIFI set fail"); } // Set PM policy to normal retval = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } // Unregister mDNS services retval = sl_NetAppMDNSUnRegisterService(0, 0); if(retval<0) { RETURN_ERROR(retval, "mDNS fail"); } //Set mDNS device hostname char hostname[64]; char macstring[20]; unsigned char maclen = SL_MAC_ADDR_LEN; retval = sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &maclen, wifi_state.mac); if(retval < 0) { RETURN_ERROR(retval, "WIFI conf fail"); } snprintf(macstring, 20, "%02X%02X%02X%02X%02X%02X", wifi_state.mac[0], wifi_state.mac[1], wifi_state.mac[2], wifi_state.mac[3], wifi_state.mac[4], wifi_state.mac[5]); snprintf(hostname, 64, "LeashDebugger%s", macstring); retval = sl_NetAppSet (SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen((const char *)hostname), (unsigned char *) hostname); // Remove all 64 filters (8*8) memset(filter_mask.FilterIdMask, 0xFF, 8); retval = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (uint8_t*)&filter_mask, sizeof(_WlanRxFilterOperationCommandBuff_t)); if(retval<0) { RETURN_ERROR(retval, "WIFI filter fail"); } retval = sl_Stop(SL_STOP_TIMEOUT); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } wifi_state.status = 0; return RET_SUCCESS; }
int main(void) { /* Init board */ BoardInit(); /* Init GPIO */ InitGPIO(); /* Init UART */ MAP_PRCMPeripheralClkEnable(PRCM_UARTA0, PRCM_RUN_MODE_CLK); InitTerm(); /* Init I2C */ I2C_IF_Open(I2C_MASTER_MODE_FST); /* Init the internet! */ // http://azug.minpet.unibas.ch/~lukas/bricol/ti_simplelink/resources/swru368.pdf SECTION 10 dhcpParams.lease_time = 1000; dhcpParams.ipv4_addr_start = 0xc0a80102; dhcpParams.ipv4_addr_last = 0xc0a801fe; sl_Start(NULL,NULL,NULL); MAP_UtilsDelay(8000000); // config IP etc ipV4.ipV4 = 0xc0a80101; ipV4.ipV4Mask = 0xFFFFFF00; ipV4.ipV4Gateway = 0xc0a80101; ipV4.ipV4DnsServer = 0xc0a80101; sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, 1 ,sizeof(SlNetCfgIpV4Args_t), (unsigned char*) &ipV4); sl_WlanSetMode(ROLE_AP); // config SSID sl_WlanSet(SL_WLAN_CFG_AP_ID,WLAN_AP_OPT_SSID, strlen(myssid), (unsigned char*) myssid); sl_Stop(100); sl_Start(NULL,NULL,NULL); // start DHCP server sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, outLen,(unsigned char*) &dhcpParams); sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID); //Stop Internal HTTP Serve long lRetVal = -1; lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); /* Finished init the internet! */ setRLED(); /* Wait for operator */ while(!readDIP1()); clearRLED(); /* Init IMU (alongside timers and interrupt */ imu_setup(); /* Init odometers */ odometer_setup(); set_controller_parameters(kp, ki, kd); set__odo_controller_parameters(kp_odo, ki_odo, kd_odo); /* Init motors (alongside motor GPIO, timers and interrupt */ motorSetup(); controller_setup(); odometer_controller_setup(); // MUST be the last init called! while(1) { _SlNonOsMainLoopTask(); } }
void wlan_sl_init (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t auth, const char *key, uint8_t key_len, uint8_t channel, uint8_t antenna, bool add_mac) { // stop the servers wlan_servers_stop(); // do a basic start wlan_first_start(); // close any active connections wlan_sl_disconnect(); // Remove all profiles ASSERT_ON_ERROR(sl_WlanProfileDel(0xFF)); // Enable the DHCP client uint8_t value = 1; ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, 1, 1, &value)); // Set PM policy to normal ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0)); // Unregister mDNS services ASSERT_ON_ERROR(sl_NetAppMDNSUnRegisterService(0, 0)); // Stop the internal HTTP server sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); // Remove all 64 filters (8 * 8) _WlanRxFilterOperationCommandBuff_t RxFilterIdMask; memset ((void *)&RxFilterIdMask, 0 ,sizeof(RxFilterIdMask)); memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); ASSERT_ON_ERROR(sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t))); #if MICROPY_HW_ANTENNA_DIVERSITY // set the antenna type wlan_set_antenna (antenna); #endif // switch to the requested mode wlan_set_mode(mode); // stop and start again (we need to in the propper mode from now on) wlan_reenable(mode); // Set Tx power level for station or AP mode // Number between 0-15, as dB offset from max power - 0 will set max power uint8_t ucPower = 0; if (mode == ROLE_AP) { ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_AP_TX_POWER, sizeof(ucPower), (unsigned char *)&ucPower)); // configure all parameters wlan_set_ssid (ssid, ssid_len, add_mac); wlan_set_security (auth, key, key_len); wlan_set_channel (channel); // set the country _u8* country = (_u8*)"EU"; ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country)); SlNetCfgIpV4Args_t ipV4; ipV4.ipV4 = (_u32)SL_IPV4_VAL(192,168,1,1); // _u32 IP address ipV4.ipV4Mask = (_u32)SL_IPV4_VAL(255,255,255,0); // _u32 Subnet mask for this AP ipV4.ipV4Gateway = (_u32)SL_IPV4_VAL(192,168,1,1); // _u32 Default gateway address ipV4.ipV4DnsServer = (_u32)SL_IPV4_VAL(192,168,1,1); // _u32 DNS server address ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4)); SlNetAppDhcpServerBasicOpt_t dhcpParams; dhcpParams.lease_time = 4096; // lease time (in seconds) of the IP Address dhcpParams.ipv4_addr_start = SL_IPV4_VAL(192,168,1,2); // first IP Address for allocation. dhcpParams.ipv4_addr_last = SL_IPV4_VAL(192,168,1,254); // last IP Address for allocation. ASSERT_ON_ERROR(sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID)); // Stop DHCP server before settings ASSERT_ON_ERROR(sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams)); // set parameters ASSERT_ON_ERROR(sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID)); // Start DHCP server with new settings // stop and start again wlan_reenable(mode); } else { // STA and P2P modes ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, sizeof(ucPower), (unsigned char *)&ucPower)); // set connection policy to Auto + Fast (tries to connect to the last connected AP) ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 1, 0, 0, 0), NULL, 0)); } // set current time and date (needed to validate certificates) wlan_set_current_time (pyb_rtc_get_seconds()); // start the servers before returning wlan_servers_start(); }
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { int ret; uint8_t v; SlNetCfgIpV4Args_t ipcfg; SlNetAppDhcpServerBasicOpt_t dhcpcfg; if ((ret = sl_WlanSetMode(ROLE_AP)) != 0) { return 0; } if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(cfg->ssid), (const uint8_t *) cfg->ssid)) != 0) { return 0; } v = strlen(cfg->pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v)) != 0) { return 0; } if (v == SL_SEC_TYPE_WPA && (ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(cfg->pass), (const uint8_t *) cfg->pass)) != 0) { return 0; } v = cfg->channel; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (uint8_t *) &v)) != 0) { return 0; } v = cfg->hidden; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_HIDDEN_SSID, 1, (uint8_t *) &v)) != 0) { return 0; } sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); memset(&ipcfg, 0, sizeof(ipcfg)); if (!inet_pton(AF_INET, cfg->ip, &ipcfg.ipV4) || !inet_pton(AF_INET, cfg->netmask, &ipcfg.ipV4Mask) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4Gateway) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4DnsServer) || (ret = sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(ipcfg), (uint8_t *) &ipcfg)) != 0) { return 0; } memset(&dhcpcfg, 0, sizeof(dhcpcfg)); dhcpcfg.lease_time = 900; if (!inet_pton(AF_INET, cfg->dhcp_start, &dhcpcfg.ipv4_addr_start) || !inet_pton(AF_INET, cfg->dhcp_end, &dhcpcfg.ipv4_addr_last) || (ret = sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(dhcpcfg), (uint8_t *) &dhcpcfg)) != 0) { return 0; } /* Turning the device off and on for the change to take effect. */ if (!restart_nwp()) return 0; if ((ret = sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID)) != 0) { LOG(LL_ERROR, ("DHCP server failed to start: %d", ret)); } LOG(LL_INFO, ("AP %s configured", cfg->ssid)); return 1; }