void clean_easylink_resource( ) { MICORemoveNotification( mico_notify_EASYLINK_WPS_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); MICORemoveNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); mico_rtos_deinit_semaphore(&easylink_sem); easylink_sem = NULL; }
void _cleanEasyLinkResource( mico_Context_t * const inContext ) { (void)inContext; if(easylinkClient_fd != -1) SocketClose(&easylinkClient_fd); /*module should power down under default setting*/ MICORemoveNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); MICORemoveNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); MICORemoveNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); MICORemoveNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); mico_rtos_deinit_semaphore(&easylink_sem); easylink_sem = NULL; if(httpHeader) free(httpHeader); }
void NTPClient_thread(void *inContext) { ntp_log_trace(); OSStatus err = kUnknownErr; mico_Context_t *Context = inContext; int Ntp_fd = -1; fd_set readfds; struct timeval_t t ; struct sockaddr_t addr; socklen_t addrLen; char ipstr[16]; unsigned int trans_sec, current; struct NtpPacket outpacket ,inpacket; socklen_t sockLen; char timeString[40]; /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler ); require_noerr( err, exit ); memset(&outpacket,0x0,sizeof(outpacket)); memset(&inpacket,0x0,sizeof(inpacket)); outpacket.flags = NTP_Flags; outpacket.stratum = NTP_Stratum; outpacket.poll = NTP_Poll; outpacket.precision = NTP_Precision; outpacket.root_delay = NTP_Root_Delay; outpacket.root_dispersion = NTP_Root_Dispersion; if(_wifiConnected == false){ mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER); mico_thread_msleep(50); } Ntp_fd = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP); require_action(IsValidSocket( Ntp_fd ), exit, err = kNoResourcesErr ); addr.s_ip = INADDR_ANY; addr.s_port = 45000; err = bind(Ntp_fd, &addr, sizeof(addr)); err = kNoErr; require_noerr(err, exit); while(1) { err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16); require_noerr(err, ReConnWithDelay); ntp_log("NTP server address: %s",ipstr); break; ReConnWithDelay: mico_thread_sleep(5); } addr.s_ip = inet_addr(ipstr); addr.s_port = NTP_Port; t.tv_sec = 5; t.tv_usec = 0; while(1) { require_action(sendto(Ntp_fd, &outpacket,sizeof(outpacket), 0, &addr, sizeof(addr)), exit, err = kNotWritableErr); FD_ZERO(&readfds); FD_SET(Ntp_fd, &readfds); select(1, &readfds, NULL, NULL, &t); if(FD_ISSET(Ntp_fd, &readfds)) { require_action(recvfrom(Ntp_fd, &inpacket, sizeof(struct NtpPacket), 0, &addr, &addrLen)>=0, exit, err = kNotReadableErr); trans_sec = inpacket.trans_ts_sec; trans_sec = ntohl(trans_sec); current = trans_sec - UNIX_OFFSET; ntp_log("Time Synchronoused, %s",asctime(localtime(¤t))); PlatformRTCWrite( localtime(¤t) ); goto exit; } } exit: if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err); MICORemoveNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler ); if(_wifiConnected_sem) mico_rtos_deinit_semaphore(&_wifiConnected_sem); SocketClose(&Ntp_fd); mico_rtos_delete_thread(NULL); return; }