// Data = AuthData#FTCServer(localIp/netMask/gateWay/dnsServer) void EasyLinkNotify_EasyLinkGetExtraDataHandler(int datalen, char* data, mico_Context_t * const inContext) { OSStatus err; int index ; char address[16]; easylink_log_trace(); uint32_t *ipInfo, ipInfoCount; require_action(inContext, exit, err = kParamErr); char *debugString; debugString = DataToHexStringWithSpaces( (const uint8_t *)data, datalen ); easylink_log("Get user info: %s", debugString); free(debugString); for(index = datalen - 1; index>=0; index-- ){ if(data[index] == '#' &&( (datalen - index) == 5 || (datalen - index) == 25 ) ) break; } require_action(index >= 0, exit, err = kParamErr); data[index++] = 0x0; ipInfo = (uint32_t *)&data[index]; ipInfoCount = (datalen - index)/sizeof(uint32_t); require_action(ipInfoCount >= 1, exit, err = kParamErr); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); inContext->flashContentInRam.micoSystemConfig.easylinkServerIP = *(uint32_t *)(ipInfo); if(ipInfoCount == 1){ inContext->flashContentInRam.micoSystemConfig.dhcpEnable = true; inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP); easylink_log("Get auth info: %s, EasyLink server ip address: %s", data, address); }else{ inContext->flashContentInRam.micoSystemConfig.dhcpEnable = false; ipInfo = (uint32_t *)&data[index]; inet_ntoa(inContext->flashContentInRam.micoSystemConfig.localIp, *(uint32_t *)(ipInfo+1)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.netMask, *(uint32_t *)(ipInfo+2)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.gateWay, *(uint32_t *)(ipInfo+3)); inet_ntoa(inContext->flashContentInRam.micoSystemConfig.dnsServer, *(uint32_t *)(ipInfo+4)); strcpy((char *)inContext->micoStatus.localIp, inContext->flashContentInRam.micoSystemConfig.localIp); strcpy((char *)inContext->micoStatus.netMask, inContext->flashContentInRam.micoSystemConfig.netMask); strcpy((char *)inContext->micoStatus.gateWay, inContext->flashContentInRam.micoSystemConfig.gateWay); strcpy((char *)inContext->micoStatus.dnsServer, inContext->flashContentInRam.micoSystemConfig.dnsServer); inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP); easylink_log("Get auth info: %s, EasyLink server ip address: %s, local IP info:%s %s %s %s ", data, address, inContext->flashContentInRam.micoSystemConfig.localIp,\ inContext->flashContentInRam.micoSystemConfig.netMask, inContext->flashContentInRam.micoSystemConfig.gateWay,inContext->flashContentInRam.micoSystemConfig.dnsServer); } mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); require_noerr(ConfigELRecvAuthData(data, inContext), exit); EasylinkFailed = false; mico_rtos_set_semaphore(&easylink_sem); ConfigEasyLinkIsSuccess(inContext); return; exit: easylink_log("ERROR, err: %d", err); ConfigWillStop(inContext); MicoSystemReboot(); }
void EasyLinkNotify_EasyLinkCompleteHandler(network_InitTypeDef_st *nwkpara, mico_Context_t * const inContext) { OSStatus err; easylink_log_trace(); easylink_log("EasyLink return @ %d", mico_get_time()); require_action(inContext, exit, err = kParamErr); require_action(nwkpara, exit, err = kTimeoutErr); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); memcpy(inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen); memcpy(inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen); inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); easylink_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key); return; /*EasyLink is not start*/ exit: easylink_log("ERROR, err: %d", err); #if defined (CONFIG_MODE_EASYLINK_WITH_SOFTAP) EasylinkFailed = true; mico_rtos_set_semaphore(&inContext->micoStatus.easylink_sem); #else ConfigWillStop(inContext); /*so roll back to previous settings (if it has) and reboot*/ mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); if(inContext->flashContentInRam.micoSystemConfig.configured != unConfigured){ inContext->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration(inContext); PlatformSoftReboot(); } mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); /*module should powd down in default setting*/ wifi_power_down(); mico_stop_timer(&_Led_EL_timer); Platform_LED_SYS_Set_Status(OFF); #endif return; }
void EasyLinkNotify_EasyLinkCompleteHandler(network_InitTypeDef_st *nwkpara, mico_Context_t * const inContext) { OSStatus err; easylink_log_trace(); easylink_log("EasyLink return"); require_action(inContext, exit, err = kParamErr); require_action(nwkpara, exit, err = kTimeoutErr); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); memcpy(inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen); memset(inContext->flashContentInRam.micoSystemConfig.bssid, 0x0, 6); memcpy(inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen); inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); easylink_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key); return; /*EasyLink timeout or error*/ exit: easylink_log("ERROR, err: %d", err); #if ( MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) EasylinkFailed = true; mico_rtos_set_semaphore(&easylink_sem); #else ConfigWillStop(inContext); /*so roll back to previous settings (if it has) and reboot*/ mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); if(inContext->flashContentInRam.micoSystemConfig.configured != unConfigured){ inContext->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration(inContext); MicoSystemReboot(); } mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); /*module should powd down in default setting*/ micoWlanPowerOff(); #endif return; }
void easylink_thread(void *inContext) { OSStatus err = kNoErr; mico_Context_t *Context = inContext; fd_set readfds; struct timeval_t t; int reConnCount = 0; easylink_log_trace(); require_action(easylink_sem, threadexit, err = kNotPreparedErr); if(Context->flashContentInRam.micoSystemConfig.easyLinkEnable != false){ OpenEasylink2_withdata(EasyLink_TimeOut); easylink_log("Start easylink"); mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER); if(EasylinkFailed == false) _easylinkConnectWiFi(Context); else{ _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; } }else{ mico_rtos_lock_mutex(&Context->flashContentInRam_mutex); Context->flashContentInRam.micoSystemConfig.easyLinkEnable = true; MICOUpdateConfiguration(Context); mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex); _easylinkConnectWiFi_fast(Context); } err = mico_rtos_get_semaphore(&easylink_sem, ConnectFTC_Timeout); require_noerr(err, reboot); httpHeader = HTTPHeaderCreate(); require_action( httpHeader, threadexit, err = kNoMemoryErr ); HTTPHeaderClear( httpHeader ); t.tv_sec = 100; t.tv_usec = 0; while(1){ if(easylinkClient_fd == -1){ err = _connectFTCServer(inContext, &easylinkClient_fd); require_noerr(err, Reconn); }else{ FD_ZERO(&readfds); FD_SET(easylinkClient_fd, &readfds); err = select(1, &readfds, NULL, NULL, &t); if(FD_ISSET(easylinkClient_fd, &readfds)){ err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader ); switch ( err ) { case kNoErr: // Read the rest of the HTTP body if necessary err = SocketReadHTTPBody( easylinkClient_fd, httpHeader ); require_noerr(err, Reconn); PrintHTTPHeader(httpHeader); // Call the HTTPServer owner back with the acquired HTTP header err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context ); require_noerr( err, Reconn ); // Reuse HTTPHeader HTTPHeaderClear( httpHeader ); break; case EWOULDBLOCK: // NO-OP, keep reading break; case kNoSpaceErr: easylink_log("ERROR: Cannot fit HTTPHeader."); goto Reconn; break; case kConnectionErr: // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed easylink_log("ERROR: Connection closed."); goto threadexit; //goto Reconn; break; default: easylink_log("ERROR: HTTP Header parse internal error: %d", err); goto Reconn; } } } continue; Reconn: HTTPHeaderClear( httpHeader ); SocketClose(&easylinkClient_fd); easylinkClient_fd = -1; require(reConnCount < 6, threadexit); reConnCount++; sleep(5); } /*Module is ignored by FTC server, */ threadexit: ConfigWillStop( Context ); _cleanEasyLinkResource( Context ); /*Roll back to previous settings (if it has) and reboot*/ mico_rtos_lock_mutex(&Context->flashContentInRam_mutex); if(Context->flashContentInRam.micoSystemConfig.configured != unConfigured){ Context->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration( Context ); PlatformSoftReboot(); } mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex); wifi_power_down(); mico_rtos_delete_thread( NULL ); return; /*SSID or Password is not correct, module cannot connect to wlan, so reboot and enter EasyLink again*/ reboot: ConfigWillStop( Context ); PlatformSoftReboot(); return; }
void easylink_thread(void *inContext) { OSStatus err = kNoErr; mico_Context_t *Context = inContext; fd_set readfds; int reConnCount = 0; int clientFdIsSet; easylink_log_trace(); require_action(easylink_sem, threadexit, err = kNotPreparedErr); if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_BYPASS){ Context->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO; MICOUpdateConfiguration(Context); _easylinkConnectWiFi_fast(Context); }else if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_SOFT_AP_BYPASS){ ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; }else{ #ifdef EasyLink_Plus easylink_log("Start easylink plus mode"); micoWlanStartEasyLinkPlus(EasyLink_TimeOut/1000); #else easylink_log("Start easylink V2"); micoWlanStartEasyLink(EasyLink_TimeOut/1000); #endif mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER); if(EasylinkFailed == false) _easylinkConnectWiFi(Context); else{ msleep(20); _cleanEasyLinkResource( Context ); ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; } } err = mico_rtos_get_semaphore(&easylink_sem, EasyLink_ConnectWlan_Timeout); require_noerr(err, reboot); httpHeader = HTTPHeaderCreate(); require_action( httpHeader, threadexit, err = kNoMemoryErr ); HTTPHeaderClear( httpHeader ); while(1){ if(easylinkClient_fd == -1){ err = _connectFTCServer(inContext, &easylinkClient_fd); require_noerr(err, Reconn); }else{ FD_ZERO(&readfds); FD_SET(easylinkClient_fd, &readfds); if(httpHeader->len == 0){ err = select(1, &readfds, NULL, NULL, NULL); require(err>=1, threadexit); clientFdIsSet = FD_ISSET(easylinkClient_fd, &readfds); } if(clientFdIsSet||httpHeader->len){ err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader ); switch ( err ) { case kNoErr: // Read the rest of the HTTP body if necessary do{ err = SocketReadHTTPBody( easylinkClient_fd, httpHeader ); require_noerr(err, Reconn); PrintHTTPHeader(httpHeader); // Call the HTTPServer owner back with the acquired HTTP header err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context ); require_noerr( err, Reconn ); if(httpHeader->contentLength == 0) break; } while( httpHeader->chunkedData == true || httpHeader->dataEndedbyClose == true); // Reuse HTTPHeader HTTPHeaderClear( httpHeader ); break; case EWOULDBLOCK: // NO-OP, keep reading break; case kNoSpaceErr: easylink_log("ERROR: Cannot fit HTTPHeader."); goto Reconn; case kConnectionErr: // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed easylink_log("ERROR: Connection closed."); /*Roll back to previous settings (if it has) and reboot*/ if(Context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured ){ Context->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration( Context ); MicoSystemReboot(); }else{ micoWlanPowerOff(); msleep(20); } goto threadexit; default: easylink_log("ERROR: HTTP Header parse internal error: %d", err); goto Reconn; } } } continue; Reconn: HTTPHeaderClear( httpHeader ); SocketClose(&easylinkClient_fd); easylinkClient_fd = -1; require(reConnCount < 6, reboot); reConnCount++; sleep(5); } /*Module is ignored by FTC server, */ threadexit: _cleanEasyLinkResource( Context ); ConfigWillStop( Context ); mico_rtos_delete_thread( NULL ); return; /*SSID or Password is not correct, module cannot connect to wlan, so reboot and enter EasyLink again*/ reboot: MicoSystemReboot(); return; }