void _sys_state_thread(void *arg) { UNUSED_PARAMETER( arg ); /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ if(needsUpdate == true) MICOUpdateConfiguration(context); switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); MicoSystemReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); micoWlanPowerOff(); break; case eState_Standby: sendNotifySYSWillPowerOff(); mico_thread_msleep(200); micoWlanPowerOff(); MicoSystemStandBy(MICO_WAIT_FOREVER); break; default: break; } } }
void ResetF411(void) { MicoGpioOutputLow(F411_RESET_PIN); mico_thread_msleep(100); MicoGpioOutputHigh(F411_RESET_PIN); mico_thread_msleep(100); AaSysLogPrint(LOGLEVEL_INF, "f411 rest done"); }
teZcbStatus eZBZLL_OnOffCheck(tsZCB_Node *psZCBNode, uint8_t u8Mode) { teZcbStatus eStatus; uint8_t u8OnOffStatus; if ((eStatus = eZBZLL_OnOff(psZCBNode, 0,0, u8Mode)) != E_ZCB_OK) { return eStatus; } if (u8Mode < 2) { /* Can't check the staus of a toggle command */ /* Wait 100ms */ mico_thread_msleep(100); if ((eStatus = eZCB_ReadAttributeRequest(psZCBNode, E_ZB_CLUSTERID_ONOFF, 0, 0, 0, E_ZB_ATTRIBUTEID_ONOFF_ONOFF, &u8OnOffStatus)) != E_ZCB_OK) { return eStatus; } user_ZigbeeZLL_log("On Off attribute read as: 0x%02X\n", u8OnOffStatus); if (u8OnOffStatus != u8Mode) { return E_ZCB_REQUEST_NOT_ACTIONED; } } return E_ZCB_OK; }
//按键处理函数 //返回按键值 //mode:0,不支持连续按;1,支持连续按; //返回值: //0,没有任何按键按下 //KEY0_PRES,KEY0按下 //KEY1_PRES,KEY1按下 //WKUP_PRES,WK_UP按下 //注意此函数有响应优先级,KEY0>KEY1>WK_UP!! u8 KEY_Scan(u8 mode) { static u8 key_up=1;//按键按松开标志 if(mode) { key_up=1; //支持连按 } if(key_up&&KEY ==0) { mico_thread_msleep(10);//去抖动 key_up=0; if(KEY==0) { return KEY_UP; } } else { if(KEY ==1) { key_up=1; } } return 0;// 无按键按下 }
void mico_system_monitor_thread_main( void* arg ) { (void)arg; while (1) { int a; uint32_t current_time = mico_get_time(); for (a = 0; a < MAXIMUM_NUMBER_OF_SYSTEM_MONITORS; ++a) { if (system_monitors[a] != NULL) { if ((current_time - system_monitors[a]->last_update) > system_monitors[a]->longest_permitted_delay) { /* A system monitor update period has been missed */ while(1); } } } MicoWdgReload(); mico_thread_msleep(DEFAULT_SYSTEM_MONITOR_PERIOD); } }
OSStatus platform_adc_take_sample( const platform_adc_t* adc, uint16_t* output ) { OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( adc != NULL, exit, err = kParamErr); channel_num = adc->channel; adc_channel_enable(ADC, adc->channel); adc_set_callback(ADC, adc->interrupt, adc_end_conversion, 1); /* Start conversion */ adc_start_software_conversion(ADC); adc_start_calibration(ADC); while (adc_get_interrupt_status(ADC) & (1 << adc->channel)); *output = adc_channel_get_value(ADC, adc->channel); mico_thread_msleep(1); adc_channel_disable(ADC, adc->channel); exit: platform_mcu_powersave_enable(); return err; }
//ftp.chdir([dir]) //=================================== static int lftp_chdir( lua_State* L ) { if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) { ftp_log("[FTP usr] Login first\r\n" ); lua_pushinteger(L, -1); return 1; } _getFName(L, 1); free(ftpresponse); ftpresponse = NULL; cmd_done = 0; uint32_t tmo = mico_get_time(); ftpCmdSocket->clientFlag = REQ_ACTION_CHDIR; while (cmd_done == 0) { if ((mico_get_time() - tmo) > 4000) break; mico_thread_msleep(60); luaWdgReload(); } if (cmd_done == 0) { ftp_log("[FTP usr] Timeout\r\n" ); lua_pushinteger(L, -2); return 1; } lua_pushinteger(L, 0); if (ftpresponse != NULL) { lua_pushstring(L, ftpresponse); free(ftpresponse); ftpresponse = NULL; } else lua_pushstring(L, "?"); return 2; }
static int httpd_signal_and_wait_for_halt() { const int total_wait_time_ms = 1000 * 20; /* 20 seconds */ const int check_interval_ms = 100; /* 100 ms */ int num_iterations = total_wait_time_ms / check_interval_ms; httpd_d("Sent stop request"); httpd_stop_req = TRUE; /* Do a dummy local connect to wakeup the httpd thread */ int sockfd; int rv = tcp_local_connect(&sockfd); if (rv != kNoErr) return rv; while (httpd_state != HTTPD_THREAD_SUSPENDED && num_iterations--) { mico_thread_msleep(check_interval_ms); } close(sockfd); if (httpd_state == HTTPD_THREAD_SUSPENDED) return kNoErr; httpd_d("Timed out waiting for httpd to stop. " "Force closed temporary socket"); httpd_stop_req = FALSE; return -kInProgressErr; }
static void lua_usr_usart_thread(void *data) { uint16_t len=0,index=0; uint32_t lastTick=0; while(1) { if((len=MicoUartGetLengthInBuffer(LUA_USR_UART))==0) { if(index>0 && mico_get_time() - lastTick>=100) goto doUartData; mico_thread_msleep(10); continue; } if(index ==0) lastTick = mico_get_time(); if(index+len>=USR_UART_LENGTH) len = USR_UART_LENGTH - index; MicoUartRecv(LUA_USR_UART, pinbuf+index, len, 10); index = index+len;pinbuf[index]=0x00; doUartData: if(index>=USR_UART_LENGTH || mico_get_time() - lastTick>=100) { index = 0; if(usr_uart_cb_ref == LUA_NOREF) continue; lua_rawgeti(gL, LUA_REGISTRYINDEX, usr_uart_cb_ref); lua_pushstring(gL,(char const*)pinbuf); lua_call(gL, 1, 0); } } //mico_rtos_delete_thread(NULL); }
OSStatus host_platform_sdio_enumerate( void ) { OSStatus result; uint32_t loop_count; uint32_t data = 0; loop_count = 0; do { /* Send CMD0 to set it to idle state */ host_platform_sdio_transfer( BUS_WRITE, SDIO_CMD_0, SDIO_BYTE_MODE, SDIO_1B_BLOCK, 0, 0, 0, NO_RESPONSE, NULL ); /* CMD5. */ host_platform_sdio_transfer( BUS_READ, SDIO_CMD_5, SDIO_BYTE_MODE, SDIO_1B_BLOCK, 0, 0, 0, NO_RESPONSE, NULL ); /* Send CMD3 to get RCA. */ result = host_platform_sdio_transfer( BUS_READ, SDIO_CMD_3, SDIO_BYTE_MODE, SDIO_1B_BLOCK, 0, 0, 0, RESPONSE_NEEDED, &data ); loop_count++; if ( loop_count >= (uint32_t) SDIO_ENUMERATION_TIMEOUT_MS ) { return kTimeoutErr; } } while ( ( result != kNoErr ) && ( mico_thread_msleep( (uint32_t) 1 ), ( 1 == 1 ) ) ); /* If you're stuck here, check the platform matches your hardware */ /* Send CMD7 with the returned RCA to select the card */ host_platform_sdio_transfer( BUS_WRITE, SDIO_CMD_7, SDIO_BYTE_MODE, SDIO_1B_BLOCK, data, 0, 0, RESPONSE_NEEDED, NULL ); return kNoErr; }
//ftp.stop() //=================================== static int lftp_stop( lua_State* L ) { if ( (ftp_thread_is_started) && (ftpCmdSocket != NULL) ) { ftpCmdSocket->clientFlag = REQ_ACTION_QUIT; if (ftpCmdSocket->disconnect_cb != LUA_NOREF) { lua_pushinteger(L, 0); return 1; } // wait max 10 sec for disconnect uint32_t tmo = mico_get_time(); while (ftp_thread_is_started) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(100); luaWdgReload(); } if (!ftp_thread_is_started) { _ftp_deinit(0); lua_pushinteger(L, 0); } else lua_pushinteger(L, -1); } else lua_pushinteger(L, 0); return 1; }
// properties notify task void notify_thread(void* arg) { OSStatus err = kUnknownErr; mico_notify_thread_data_t *p_notify_thread_data; p_notify_thread_data = (mico_notify_thread_data_t*)arg; require_action(p_notify_thread_data, exit, err = kParamErr); // wait semaphore for cloud connection mico_fogcloud_waitfor_connect(p_notify_thread_data->context, MICO_WAIT_FOREVER); // block to wait fogcloud connect msg_dispatch_log("Cloud connected, do notify task."); while(1){ if(p_notify_thread_data->context->appStatus.fogcloudStatus.isCloudConnected){ err = _properties_notify(p_notify_thread_data->context, p_notify_thread_data->p_service_table); if(kNoErr != err){ msg_dispatch_log("ERROR: properties notify failed! err = %d", err); } } mico_thread_msleep(p_notify_thread_data->notify_interval); } exit: // never get here only if notify work err && exit. msg_dispatch_log("ERROR: notify thread exit err=%d.", err); mico_rtos_delete_thread(NULL); return; }
//tmr.delayms() static int ltmr_delayms( lua_State* L ) { uint32_t ms = luaL_checkinteger( L, 1 ); if ( ms <= 0 ) return luaL_error( L, "wrong arg range" ); mico_thread_msleep(ms); return 0; }
//======================================= static int lwifi_poweroff( lua_State* L ) { micoWlanSuspend(); wifi_sta_started = 0; mico_thread_msleep(100); micoWlanPowerOff(); return 0; }
/* 电量检测线程 */ void BAT_handleThread(void *inContext) { printf( "This is BAT_handleThread.\r\n"); while(1) { mico_thread_msleep(5000); /* 低电量时,主动上传平台 */ } }
//ftp.sendstring(file, str [,append]) //======================================== static int lftp_sendstring( lua_State* L ) { if ((gL != NULL) && (ftpCmdSocket != NULL)) { if ((status & FTP_LOGGED)) { if (lua_gettop(L) >= 3) { send_type = (uint8_t)luaL_checkinteger(L, 3); if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE; } else send_type = SEND_OVERWRITTE; send_type |= SEND_STRING; if (_getFName(L, 1) < 0) { ftp_log("[FTP fil] File name missing\r\n" ); lua_pushinteger(L, -13); return 1; } size_t len; sendDataBuf = (char*)luaL_checklstring( L, 2, &len ); if (sendDataBuf == NULL) { ftp_log("[FTP fil] Bad string\r\n"); lua_pushinteger(L, -14); } else { file_size = len; data_done = 0; ftpCmdSocket->clientFlag = REQ_ACTION_SEND; uint32_t tmo = mico_get_time(); while (!data_done) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(60); luaWdgReload(); } if (!data_done) { ftp_log("[FTP usr] Timeout: string not sent\r\n" ); lua_pushinteger(L, -15); } else { ftp_log("[FTP usr] String sent\r\n" ); lua_pushinteger(L, file_status); } sendDataBuf = NULL; } } else { ftp_log("[FTP usr] Not logged\r\n" ); lua_pushinteger(L, -12); } } else { ftp_log("[FTP usr] Login first\r\n" ); lua_pushinteger(L, -11); } return 1; }
/** * @brief Provides a blocking delay in millisecond. * @note Care must be taken when using HAL_Delay(), this function provides accurate delay * (in milliseconds) based on variable incremented in SysTick ISR. This implies that * if HAL_Delay() is called from a peripheral ISR process, then the SysTick interrupt * must have higher priority (numerically lower) than the peripheral interrupt. * Otherwise the caller ISR process will be blocked. To change the SysTick interrupt * priority you have to use HAL_NVIC_SetPriority() function. * @param Delay : specifies the delay time length, in milliseconds. * @retval None */ void HAL_Delay(__IO uint32_t Delay) { // uint32_t timingdelay; // // timingdelay = HAL_GetTick() + Delay; // while(HAL_GetTick() < timingdelay) // { // } mico_thread_msleep(Delay); }
void except_handle(void *inContext) { char str[20]; while(1) { if(dht11_temp_data >= 32) { hsb2rgb_led_open(0, 100, 50); mico_thread_msleep(500); hsb2rgb_led_open(0, 0, 0); mico_thread_msleep(500); } sprintf(str, "T:%3.1fC H:%3.1f%%",(float)dht11_temp_data, (float)dht11_hum_data); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, (uint8_t*)str); memset(str,0,20); sprintf(str, "infrared: %d",infrared_reflective_data); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, (uint8_t*)str); memset(str,0,20); sprintf(str, "light: %d",light_sensor_data); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, (uint8_t*)str); } }
//ftp.send(file [,append]) //================================== static int lftp_send( lua_State* L ) { if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) { ftp_log("[FTP usr] Login first\r\n" ); lua_pushinteger(L, -11); return 1; } if (lua_gettop(L) >= 2) { send_type = (uint8_t)luaL_checkinteger(L, 2); if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE; } else send_type = SEND_OVERWRITTE; if (_getFName(L, 1) < 0) { ftp_log("[FTP fil] File name missing\r\n" ); lua_pushinteger(L, -12); return 1; } if (_openFile("r") < 0) { lua_pushinteger(L, -13); return 1; } spiffs_stat s; // Get file size SPIFFS_fstat(&fs, file_fd, &s); file_size = s.size; data_done = 0; ftpCmdSocket->clientFlag = REQ_ACTION_SEND; if (ftpCmdSocket->sent_cb == LUA_NOREF) { // no cb function, wait until file received (max 10 sec) uint32_t tmo = mico_get_time(); while (!data_done) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(60); luaWdgReload(); } if (!data_done) { ftp_log("[FTP usr] Timeout: file not sent\r\n" ); lua_pushinteger(L, -14); } else { ftp_log("[FTP usr] File sent\r\n" ); lua_pushinteger(L, file_status); } } else lua_pushinteger(L, 0); return 1; }
//stat = ftp.start() //=================================== static int lftp_start( lua_State* L ) { LinkStatusTypeDef wifi_link; int err = micoWlanGetLinkStatus( &wifi_link ); if ( wifi_link.is_connected == false ) { ftp_log("[FTP usr] WiFi NOT CONNECTED!\r\n" ); lua_pushinteger(L, -1); return 1; } if ( (gL == NULL) || (ftpCmdSocket == NULL) ) { ftp_log("[FTP usr] Execute ftp.new first!\r\n" ); lua_pushinteger(L, -2); return 1; } if (ftp_thread_is_started) { ftp_log("[FTP usr] Already started!\r\n" ); lua_pushinteger(L, -3); return 1; } mico_system_notify_register( mico_notify_TCP_CLIENT_CONNECTED, (void *)_micoNotify_FTPClientConnectedHandler, NULL ); // all setup, start the ftp thread if (!ftp_thread_is_started) { if (mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY-1, "Ftp_Thread", _thread_ftp, 1024, NULL) != kNoErr) { _ftp_deinit(0); ftp_log("[FTP usr] Create thread failed\r\n" ); lua_pushinteger(L, -4); return 1; } else ftp_thread_is_started = true; } if (ftpCmdSocket->logon_cb != LUA_NOREF) { lua_pushinteger(L, 0); return 1; } // wait max 10 sec for login uint32_t tmo = mico_get_time(); while ( (ftp_thread_is_started) && !(status & FTP_LOGGED) ) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(100); luaWdgReload(); } if (!(status & FTP_LOGGED)) lua_pushinteger(L, -4); else lua_pushinteger(L, 0); return 1; }
//ftp.recv(file [,tostr]) //=================================== static int lftp_recv( lua_State* L ) { if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) { ftp_log("[FTP usr] Login first\r\n" ); lua_pushinteger(L, -11); return 1; } if (_getFName(L, 1) < 0) { ftp_log("[FTP fil] File name missing\r\n" ); lua_pushinteger(L, -12); return 1; } if (_openFile("w") < 0) { lua_pushinteger(L, -13); return 1; } recv_type = RECV_TOFILE; if (lua_gettop(L) >= 2) { int tos = luaL_checkinteger(L, 2); if (tos == 1) recv_type = RECV_TOSTRING; } data_done = 0; ftpCmdSocket->clientFlag = REQ_ACTION_RECV; if (ftpCmdSocket->received_cb == LUA_NOREF) { // no cb function, wait until file received (max 10 sec) uint32_t tmo = mico_get_time(); while (!data_done) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(60); luaWdgReload(); } if (!data_done) { ftp_log("[FTP usr] Timeout: file not received\r\n" ); lua_pushinteger(L, -14); } else { ftp_log("[FTP usr] File received\r\n" ); lua_pushinteger(L, file_status); if (recv_type == RECV_TOSTRING) { lua_pushstring(L, recvDataBuf); return 2; } } } else lua_pushinteger(L, 0); return 1; }
int application_start( void ) { network_InitTypeDef_adv_st wNetConfig; int udp_fd = -1; struct sockaddr_t addr; socklen_t addrLen; uint8_t *buf = NULL; #if MCU_POWERSAVE_ENABLED MicoMcuPowerSaveConfig(true); #endif power_log( "Power measure program: RTOS and wlan initialized and connect wlan, wait station up to measure" ); MicoInit( ); #if IEEE_POWERSAVE_ENABLED micoWlanEnablePowerSave(); #endif memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_adv_st)); strncpy((char*)wNetConfig.ap_info.ssid, "William Xu", 32); wNetConfig.ap_info.security = SECURITY_TYPE_AUTO; strncpy((char*)wNetConfig.key, "mx099555", 64); wNetConfig.key_len = 8; wNetConfig.dhcpMode = true; wNetConfig.wifi_retry_interval = 100; micoWlanStartAdv(&wNetConfig); power_log("connect to %s.....", wNetConfig.ap_info.ssid); buf = malloc(1024); udp_fd = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);; addr.s_port = 2000; addr.s_ip = INADDR_ANY; bind(udp_fd, &addr, sizeof(addr)); addr.s_port = 2001; addr.s_ip = inet_addr( "192.168.2.1" ); connect( udp_fd, &addr, sizeof(addr) ); while(1) { send( udp_fd, buf, 1024, 0 ); mico_thread_msleep( 10 ); } mico_rtos_delete_thread( NULL ); return 0; }
void apds9930_enable() { //Disable and Powerdown APDS9930_Write_RegData(ENABLE_ADDR, APDS9930_DISABLE); APDS9930_Write_RegData(ATIME_ADDR, ATIME_256C); APDS9930_Write_RegData(PTIME_ADDR, PTIME_10C); APDS9930_Write_RegData(WTIME_ADDR, WTIME_74C); APDS9930_Write_RegData(CONFIG_ADDR, RECONFIG); APDS9930_Write_RegData(PPULSE_ADDR, PPULSE_MIN); //Config APDS9930_Write_RegData(CONTROL_ADDR,PDRIVE_100|PDIODE_CH1|PGAIN_1x|AGAIN_1x); //Enable APDS9930 APDS9930_Write_RegData(ENABLE_ADDR, WEN|PEN|AEN|PON); //must delay > 12ms mico_thread_msleep(12); }
/* 灯光处理线程 */ void LED_handleThread(void *inContext) { u8 ledValue = 0; u8 playLedTimes = 0; //无播放音乐时,演示LED次数 printf( "This is LED_handleThread.\r\n"); LED_openRGB(0, 0, 0); //初始化关闭所有灯 LED_closeRGB(); while(mico_rtos_get_semaphore(&cupTimeObj.playLed_sem, MICO_WAIT_FOREVER) == kNoErr) { while(mico_rtos_get_semaphore(&cupTimeObj.stopLed_sem, MICO_NO_WAIT) != kNoErr \ && cupTimeObj.playMode != PLAY_ONLY_MP3) { printf("Playing led ........\r\n"); #if 0 for (ledValue = 0; ledValue < 100; ledValue++) { LED_openRGB(100 - ledValue, ledValue, ledValue); mico_thread_msleep(15); } mico_thread_msleep(100); for (ledValue = 100; ledValue > 0; ledValue--) { LED_openRGB(100 - ledValue, ledValue, ledValue); mico_thread_msleep(15); } mico_thread_msleep(100); #else /* 秒闪 */ LED_openRGB(50, 50, 50); mico_thread_msleep(500); LED_openRGB(0, 0, 0); mico_thread_msleep(500); #endif /* 只有LED模式时,LED只演示三次 */ if (cupTimeObj.playMode == PLAY_ONLY_LED) { playLedTimes++; if (playLedTimes == 3 || KEY_getValue() == KEY_UP) { playLedTimes = 0; break; } } } LED_openRGB(0, 0, 0); LED_closeRGB(); } }
void easylink_thread(void *inContext) { micoWlanStartEasyLink( 60 ); wifi_easylink_log("Start Easylink configuration"); mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER); if ( is_easylink_success == 1 ) { mico_thread_msleep(10); connect_ap( ); } else { wifi_easylink_log("Easylink configuration fail"); } clean_easylink_resource(); mico_rtos_delete_thread(NULL); }
teZcbStatus eZBZLL_MoveToLevelCheck(tsZCB_Node *psZCBNode, uint8_t u8OnOff, uint8_t u8Level, uint16_t u16TransitionTime) { teZcbStatus eStatus; uint8_t u8CurrentLevel; if ((eStatus = eZCB_ReadAttributeRequest(psZCBNode, E_ZB_CLUSTERID_LEVEL_CONTROL, 0, 0, 0, E_ZB_ATTRIBUTEID_LEVEL_CURRENTLEVEL, &u8CurrentLevel)) != E_ZCB_OK) { return eStatus; } user_ZigbeeZLL_log("Current Level attribute read as: 0x%02X\n", u8CurrentLevel); if (u8CurrentLevel == u8Level) { /* Level is already set */ /* This is a guard for transition times that are outside of the JIP 300ms retry window */ return E_ZCB_OK; } if (u8Level > 254) { u8Level = 254; } if ((eStatus = eZBZLL_MoveToLevel(psZCBNode, 0, u8OnOff, u8Level, u16TransitionTime)) != E_ZCB_OK) { return eStatus; } /* Wait the transition time */ mico_thread_msleep(u16TransitionTime * 100); if ((eStatus = eZCB_ReadAttributeRequest(psZCBNode, E_ZB_CLUSTERID_LEVEL_CONTROL, 0, 0, 0, E_ZB_ATTRIBUTEID_LEVEL_CURRENTLEVEL, &u8CurrentLevel)) != E_ZCB_OK) { return eStatus; } user_ZigbeeZLL_log("Current Level attribute read as: 0x%02X\n", u8CurrentLevel); if (u8CurrentLevel != u8Level) { return E_ZCB_REQUEST_NOT_ACTIONED; } return E_ZCB_OK; }
static inline int tcp_local_connect(int *sockfd) { uint16_t port; int retry_cnt = 3; httpd_d("Doing local connect for shutting down server\n\r"); *sockfd = -1; while (retry_cnt--) { *sockfd = socket(AF_INET, SOCK_STREAM, 0); if (*sockfd >= 0) break; /* Wait some time to allow some sockets to get released */ mico_thread_msleep(1000); } if (*sockfd < 0) { httpd_d("Unable to create socket to stop server"); return -kInProgressErr; } port = HTTP_PORT; char *host = "127.0.0.1"; struct sockaddr_t addr; memset(&addr, 0, sizeof(struct sockaddr_t)); addr.s_port = port; addr.s_ip = inet_addr(host); httpd_d("local connecting ..."); if (connect(*sockfd, &addr, sizeof(addr)) != 0) { httpd_d("Server close error. tcp connect failed %s:%d", host, port); close(*sockfd); *sockfd = 0; return -kInProgressErr; } /* * We do not wish to do anything with this connection. Its sole * purpose was to wake the main httpd thread out of sleep. */ return kNoErr; }
/**************************************************************************** * Function : psZCB_FindNodeShortAddress * Description : Find Node * Input Para : * Output Para : * Return Value: ****************************************************************************/ tsZCB_Node *psZCB_FindNodeShortAddress(uint16_t u16ShortAddress) { tsZCB_Node *psZCBNode = &sZCB_Network.sNodes; mico_rtos_lock_mutex(&sZCB_Network.sLock); while (psZCBNode) { if (psZCBNode->u16ShortAddress == u16ShortAddress) { int iLockAttempts = 0; user_ZBNetwork_log("Short address 0x%04X found in network", u16ShortAddress); DBG_PrintNode(psZCBNode); while (++iLockAttempts < 5) { if (mico_rtos_lock_mutex(&psZCBNode->sLock) == kNoErr) { break; } else { mico_rtos_unlock_mutex(&sZCB_Network.sLock); if (iLockAttempts == 5) { user_ZBNetwork_log("\n\nError: Could not get lock on node!!"); return NULL; } mico_thread_msleep(1000); mico_rtos_lock_mutex(&sZCB_Network.sLock); } } break; } psZCBNode = psZCBNode->psNext; } mico_rtos_unlock_mutex(&sZCB_Network.sLock); return psZCBNode; }
//=================================== static int lwifi_scan( lua_State* L ) { OSStatus err = 0; int tmo = mico_get_time(); wifi_scanned_print = 0; if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 1); // copy argument (func) to the top of stack if (wifi_scan_succeed != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = luaL_ref(L, LUA_REGISTRYINDEX); } else { if (wifi_scan_succeed != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = LUA_NOREF; if (lua_type(L, 1) == LUA_TNUMBER) { int prn = luaL_checkinteger( L, 1 ); if (prn == 1) wifi_scanned_print = 1; } } err = mico_system_notify_register( mico_notify_WIFI_SCAN_ADV_COMPLETED, (void *)_micoNotify_WiFi_Scan_OK, NULL ); require_noerr( err, exit ); gL = L; wifi_scanned = 0; micoWlanStartScanAdv(); tmo = mico_get_time(); if (wifi_scan_succeed == LUA_NOREF) { while (wifi_scanned == 0) { if ((mico_get_time() - tmo) > 8000) break; mico_thread_msleep(100); luaWdgReload(); } if ((wifi_scanned == 1) && (wifi_scanned_print == 0)) { return 2; } } exit: return 0; }
//------------------------------ static void _stopWifiSta(void) { network_InitTypeDef_st wNetConfig; memset(&wNetConfig, 0x00, sizeof(network_InitTypeDef_st)); wNetConfig.wifi_mode = Station; wNetConfig.wifi_retry_interval = 0x7FFFFFFF; sprintf((char*)&wNetConfig.dnsServer_ip_addr, "208.67.222.222"); sprintf((char*)&wNetConfig.gateway_ip_addr, "0.0.0.0"); sprintf((char*)&wNetConfig.local_ip_addr, "0.0.0.0"); sprintf((char*)&wNetConfig.net_mask, "0.0.0.0"); micoWlanSuspendStation(); //mico_thread_msleep(10); micoWlanStart(&wNetConfig); mico_thread_msleep(10); micoWlanSuspendStation(); wifi_sta_started = 0; }