static void mg_init(struct mg_mgr *mgr) { LOG(LL_INFO, ("MG task running")); stop_nwp(); /* See function description in wifi.c */ LOG(LL_INFO, ("Starting NWP...")); int role = sl_Start(0, 0, 0); if (role < 0) { LOG(LL_ERROR, ("Failed to start NWP")); return; } { SlVersionFull ver; unsigned char opt = SL_DEVICE_GENERAL_VERSION; unsigned char len = sizeof(ver); memset(&ver, 0, sizeof(ver)); sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &opt, &len, (unsigned char *) (&ver)); LOG(LL_INFO, ("NWP v%d.%d.%d.%d started, host v%d.%d.%d.%d", ver.NwpVersion[0], ver.NwpVersion[1], ver.NwpVersion[2], ver.NwpVersion[3], SL_MAJOR_VERSION_NUM, SL_MINOR_VERSION_NUM, SL_VERSION_NUM, SL_SUB_VERSION_NUM)); } GPIO_IF_LedToggle(MCU_RED_LED_GPIO); data_init_sensors(TMP006_ADDR, BM222_ADDR); sl_fs_init(); #if defined(WIFI_STA_SSID) if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) { LOG(LL_ERROR, ("Error setting up WiFi station")); } #elif defined(WIFI_AP_SSID) if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) { LOG(LL_ERROR, ("Error setting up WiFi AP")); } #else #error WiFi not configured #endif /* We don't need SimpleLink's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); const char *err = ""; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); opts.error_string = &err; struct mg_connection *nc = mg_bind_opt(mgr, "80", mg_ev_handler, opts); if (nc != NULL) { mg_set_protocol_http_websocket(nc); nc->ev_timer_time = mg_time(); /* Start data collection */ } else { LOG(LL_ERROR, ("Failed to create listener: %s", err)); } }
static int restart_nwp() { /* We don't need TI's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); sl_Stop(0); s_current_role = sl_Start(NULL, NULL, NULL); sl_restart_cb(&sj_mgr); return (s_current_role >= 0); }
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; }
//**************************************************************************** // //! \brief Handles HTTP Server Task //! //! \param[in] pvParameters is the data passed to the Task //! //! \return None // //**************************************************************************** static void HTTPServerTask(void *pvParameters) { long lRetVal = -1; 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"); memset(g_ucSSID,'\0',AP_SSID_LEN_MAX); //Read Device Mode Configuration ReadDeviceConfiguration(); //Connect to Network lRetVal = ConnectToNetwork(); //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); if(lRetVal < 0) { ERR_PRINT(lRetVal); LOOP_FOREVER(); } //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); if(lRetVal < 0) { ERR_PRINT(lRetVal); LOOP_FOREVER(); } //Handle Async Events while(1) { } }
static int restart_nwp(void) { /* Properly close FS container if it's open for writing. */ cc3200_fs_flush(); /* We don't need TI's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); /* Without a delay in sl_Stop subsequent sl_Start gets stuck sometimes. */ sl_Stop(10); s_current_role = sl_Start(NULL, NULL, NULL); sl_restart_cb(miot_get_mgr()); return (s_current_role >= 0); }
static void mg_task(void *arg) { LOG(LL_INFO, ("MG task running")); GPIO_IF_LedToggle(MCU_RED_LED_GPIO); osi_MsgQCreate(&s_v7_q, "MG", sizeof(struct event), 32 /* len */); sl_Start(NULL, NULL, NULL); data_init_sensors(TMP006_ADDR, BM222_ADDR); cc3200_fs_init(); #if defined(WIFI_STA_SSID) if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) { LOG(LL_ERROR, ("Error setting up WiFi station")); } #elif defined(WIFI_AP_SSID) if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) { LOG(LL_ERROR, ("Error setting up WiFi AP")); } #else #error WiFi not configured #endif /* We don't need SimpleLink's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); mg_mgr_init(&mg_mgr, NULL); const char *err = ""; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); opts.error_string = &err; struct mg_connection *nc = mg_bind(&mg_mgr, "80", mg_ev_handler); if (nc != NULL) { mg_set_protocol_http_websocket(nc); nc->ev_timer_time = mg_time(); /* Start data collection */ } else { LOG(LL_ERROR, ("Failed to create listener: %s", err)); } while (1) { struct event e; mg_mgr_poll(&mg_mgr, 0); if (osi_MsgQRead(&s_v7_q, &e, 1) != OSI_OK) continue; } }
static void mg_init(struct mg_mgr *mgr) { WiFi_Params wifiParams; WiFi_Handle handle; LOG(LL_INFO, ("MG task running")); /* Open WiFi driver */ WiFi_Params_init(&wifiParams); wifiParams.bitRate = 2000000; handle = WiFi_open(Board_WIFI, Board_WIFI_SPI, NULL, &wifiParams); if (handle == NULL) { System_abort("WiFi driver failed to open."); } sl_Start(0, 0, 0); sl_fs_init(); #if defined(WIFI_STA_SSID) if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) { LOG(LL_ERROR, ("Error setting up WiFi station")); } #elif defined(WIFI_AP_SSID) if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) { LOG(LL_ERROR, ("Error setting up WiFi AP")); } #else #error WiFi not configured #endif /* We don't need SimpleLink's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); const char *err = ""; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); opts.error_string = &err; struct mg_connection *nc = mg_bind(mgr, "80", mg_ev_handler); if (nc != NULL) { mg_set_protocol_http_websocket(nc); } else { LOG(LL_ERROR, ("Failed to create listener: %s", err)); } }
static void mg_init(struct mg_mgr *mgr) { LOG(LL_INFO, ("MG task running")); stop_nwp(); /* See function description in wifi.c */ int role = sl_Start(0, 0, 0); if (role < 0) { LOG(LL_ERROR, ("Failed to start NWP")); return; } LOG(LL_INFO, ("NWP started")); GPIO_IF_LedToggle(MCU_RED_LED_GPIO); data_init_sensors(TMP006_ADDR, BM222_ADDR); sl_fs_init(); #if defined(WIFI_STA_SSID) if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) { LOG(LL_ERROR, ("Error setting up WiFi station")); } #elif defined(WIFI_AP_SSID) if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) { LOG(LL_ERROR, ("Error setting up WiFi AP")); } #else #error WiFi not configured #endif /* We don't need SimpleLink's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); const char *err = ""; struct mg_bind_opts opts; memset(&opts, 0, sizeof(opts)); opts.error_string = &err; struct mg_connection *nc = mg_bind(mgr, "80", mg_ev_handler); if (nc != NULL) { mg_set_protocol_http_websocket(nc); nc->ev_timer_time = mg_time(); /* Start data collection */ } else { LOG(LL_ERROR, ("Failed to create listener: %s", err)); } }
/*! \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 main() { struct mg_mgr mgr; Serial pc(SERIAL_TX, SERIAL_RX, 115200); printf("Mongoose demo\n"); led_green = 1; /* off */ led_blue = 1; /* off */ SimpleLinkInterface wifi(PG_10, PG_11, SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); wifi.connect(WIFI_STA_SSID, WIFI_STA_PASS); const char *ip = wifi.get_ip_address(); const char *gw = wifi.get_gateway(); const char *mac = wifi.get_mac_address(); printf("IP address is: %s\n", ip ? ip : "No IP"); printf("GW address is: %s\n", gw ? gw : "No IP"); printf("MAC address is: %s\n", mac ? mac : "No MAC"); mg_mgr_init(&mgr, NULL); const char *err; struct mg_bind_opts opts = {}; opts.error_string = &err; mg_connection *nc = mg_bind_opt(&mgr, HTTP_SERVER_PORT, ev_handler, opts); if (nc == NULL) { printf("Failed to create listener: %s\r\n", err); led_red = 0; /* on */ return 1; } mg_set_protocol_http_websocket(nc); printf("Server address: http://%s:%s\n", ip, HTTP_SERVER_PORT); while (true) { mg_mgr_poll(&mgr, 1000); led_green = !led_green; } }
//--tested, working--// void WiFiServer::begin() { // //Stop the port 80 internal http server if running a server on port 80 // if (_port == 80) { sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); } // //get a socket from the WiFiClass (convoluted method from the arduino library) // int socketIndex = WiFiClass::getSocket(); if (socketIndex == NO_SOCKET_AVAIL) { return; } // //get a socket handle from the simplelink api and make sure it's valid // int socketHandle = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_IPPROTO_TCP); if (socketHandle < 0) { return; } // //bind the socket to the requested port and check for success //if failure, gracefully close the socket and return failure // SlSockAddrIn_t portAddress; portAddress.sin_family = SL_AF_INET; portAddress.sin_port = sl_Htons(_port); portAddress.sin_addr.s_addr = 0; int enableOption = 1; sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &enableOption, sizeof(enableOption)); sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_KEEPALIVE, &enableOption, sizeof(enableOption)); int iRet = sl_Bind(socketHandle, (SlSockAddr_t*)&portAddress, sizeof(SlSockAddrIn_t)); if (iRet < 0) { sl_Close(socketHandle); return; } // //Make the socket start listening for incoming tcp connections //(backlog of length 0) // iRet = sl_Listen(socketHandle, 0); if (iRet < 0) { sl_Close(socketHandle); return; } // //set socket operation to be non blocking // long NonBlocking = true; iRet = sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &NonBlocking, sizeof(NonBlocking)); if (iRet < 0) { sl_Close(socketHandle); return; } // //Simplelink api calls are done, so set the object's variables // _socketIndex = socketIndex; WiFiClass::_handleArray[socketIndex] = socketHandle; WiFiClass::_portArray[socketIndex] = _port; WiFiClass::_typeArray[socketIndex] = TYPE_TCP_SERVER; }
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; }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return 0 - Success //! -1 - Failure // //**************************************************************************** long ConnectToNetwork() { long lRetVal = -1; unsigned int uiConnectTimeoutCnt =0; // staring simplelink lRetVal = sl_Start(NULL,NULL,NULL); ASSERT_ON_ERROR( lRetVal); // Device is in AP Mode and Force AP Jumper is not Connected if(ROLE_STA != lRetVal && g_uiDeviceModeConfig == ROLE_STA ) { if (ROLE_AP == lRetVal) { // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //Switch to STA Mode lRetVal = ConfigureMode(ROLE_STA); ASSERT_ON_ERROR( lRetVal); } //Device is in STA Mode and Force AP Jumper is Connected if(ROLE_AP != lRetVal && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode lRetVal = ConfigureMode(ROLE_AP); ASSERT_ON_ERROR( lRetVal); } //No Mode Change Required if(lRetVal == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } //Stop Internal HTTP Server 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); char cCount=0; //Blink LED 3 times to Indicate AP Mode for(cCount=0;cCount<3;cCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } char ssid[32]; unsigned short len = 32; unsigned short config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char* )ssid); UART_PRINT("\n\r Connect to : \'%s\'\n\r\n\r",ssid); } else { //Stop Internal HTTP Server 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); //waiting for the device to Auto Connect while(uiConnectTimeoutCnt<AUTO_CONNECTION_TIMEOUT_COUNT && ((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); uiConnectTimeoutCnt++; } //Couldn't connect Using Auto Profile if(uiConnectTimeoutCnt == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); CLR_STATUS_BIT_ALL(g_ulStatus); //Connect Using Smart Config lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); //Waiting for the device to Auto Connect while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); } return 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(); } }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return 0 on success else error code // //**************************************************************************** long ConnectToNetwork() { char ucAPSSID[32]; unsigned short len, config_opt; long lRetVal = -1; // staring simplelink g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); // Device is not in STA mode and Force AP Jumper is not Connected //- Switch to STA mode if(g_uiSimplelinkRole != ROLE_STA && g_uiDeviceModeConfig == ROLE_STA ) { //Switch to STA Mode lRetVal = sl_WlanSetMode(ROLE_STA); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(SL_STOP_TIMEOUT); g_usMCNetworkUstate = 0; g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); UART_PRINT("started in STA mode\n\r"); } //Device is not in AP mode and Force AP Jumper is Connected - //Switch to AP mode if(g_uiSimplelinkRole != ROLE_AP && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode lRetVal = sl_WlanSetMode(ROLE_AP); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(SL_STOP_TIMEOUT); g_usMCNetworkUstate = 0; g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //No Mode Change Required if(g_uiSimplelinkRole == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server while(!IS_IP_ACQUIRED(g_ulStatus)) { } //Stop Internal HTTP Server 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); char iCount=0; //Read the AP SSID memset(ucAPSSID,'\0',AP_SSID_LEN_MAX); len = AP_SSID_LEN_MAX; config_opt = WLAN_AP_OPT_SSID; lRetVal = sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) ucAPSSID); ASSERT_ON_ERROR(lRetVal); Report("\n\rDevice is in AP Mode, Please Connect to AP [%s] and" "type [mysimplelink.net] in the browser \n\r",ucAPSSID); //Blink LED 3 times to Indicate AP Mode for(iCount=0;iCount<3;iCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } } else { //Stop Internal HTTP Server 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); /*//waiting for the device to Auto Connect while ( (!IS_IP_ACQUIRED(g_ulStatus))&& g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); g_ucConnectTimeout++; } //Couldn't connect Using Auto Profile if(g_ucConnectTimeout == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); CLR_STATUS_BIT_ALL(g_ulStatus); 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); }*/ SlSecParams_t secParams = {0}; secParams.Key = (signed char *)SECURITY_KEY; secParams.KeyLen = strlen(SECURITY_KEY); secParams.Type = SECURITY_TYPE; lRetVal = sl_WlanConnect((signed char *)SSID_NAME, strlen((const char *)SSID_NAME), 0, &secParams, 0); ASSERT_ON_ERROR(lRetVal); // Wait for WLAN Event while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { //Turn Green LED On GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); osi_Sleep(50); //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); osi_Sleep(50); } //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); UART_PRINT("connected\n\r"); } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); UART_PRINT("\n\rDevice is in STA Mode, Connect to the AP[%s] and type" "IP address [%d.%d.%d.%d] in the browser \n\r",g_ucConnectionSSID, SL_IPV4_BYTE(g_uiIpAddress,3),SL_IPV4_BYTE(g_uiIpAddress,2), SL_IPV4_BYTE(g_uiIpAddress,1),SL_IPV4_BYTE(g_uiIpAddress,0)); return SUCCESS; }
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(); }
void initNetwork(signed char *ssid, SlSecParams_t *keyParams) { memset(g_ConnectionSSID, 0, sizeof(g_ConnectionSSID)); memset(g_ConnectionBSSID, 0, sizeof(g_ConnectionBSSID)); short status = sl_Start(0, 0, 0); if (status >= 0) { printVersionInfo(); // disable scan unsigned char configOpt = SL_SCAN_POLICY(0); sl_WlanPolicySet(SL_POLICY_SCAN, configOpt, NULL, 0); // set tx power to maximum unsigned char txPower = 0; sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&txPower); // set power policy to normal sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0); // remove all rx filters _WlanRxFilterOperationCommandBuff_t rxFilterIdMask; memset(rxFilterIdMask.FilterIdMask, 0xFF, 8); sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (unsigned char *)&rxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)); status = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 0), NULL, 0); } if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } sl_WlanDisconnect(); status = sl_WlanSetMode(ROLE_STA); if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } UART_PRINT("\r\n"); UART_PRINT("[QuickStart] Network init\r\n"); status = sl_WlanConnect(ssid, strlen((char *)ssid), NULL, keyParams, NULL); if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } while (!IS_IP_ACQUIRED(g_Status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #else osi_Sleep(100); #endif } sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); }
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; }