void ntpNotify_WifiStatusHandler(int event, mico_Context_t * const inContext) { ntp_log_trace(); (void)inContext; switch (event) { case NOTIFY_STATION_UP: _wifiConnected = true; if(_wifiConnected_sem) mico_rtos_set_semaphore(&_wifiConnected_sem); break; case NOTIFY_STATION_DOWN: break; default: break; } return; }
void ntpNotify_WifiStatusHandler(int event, void *arg) { ntp_log_trace(); UNUSED_PARAMETER(arg); switch (event) { case NOTIFY_STATION_UP: _wifiConnected = true; if(_wifiConnected_sem) mico_rtos_set_semaphore(&_wifiConnected_sem); break; case NOTIFY_STATION_DOWN: break; default: break; } return; }
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; }
void NTPClient_thread(void *arg) { ntp_log_trace(); OSStatus err = kUnknownErr; UNUSED_PARAMETER( arg ); 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; struct tm *currentTime; mico_rtc_time_t time; LinkStatusTypeDef wifi_link; int contry = 0; /* Regisist notifications */ err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler, NULL ); 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; err = micoWlanGetLinkStatus( &wifi_link ); require_noerr( err, exit ); if( wifi_link.is_connected == true ) _wifiConnected = true; if(_wifiConnected == false) mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER); 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); contry=0; while(1) { //err = gethostbyname((char *)NTP_Server, (uint8_t *)ipstr, 16); err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16); contry+=1; if (contry > 4) { require_noerr(err, exit); } else { 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_time_zone*3600); ntp_log("Time Synchronised, %s, tz=%d, from %s\n\r",asctime(gmtime(¤t)),ntp_time_zone, NTP_Server); //currentTime = localtime(¤t); currentTime = gmtime(¤t); time.sec = currentTime->tm_sec; time.min = currentTime->tm_min ; time.hr = currentTime->tm_hour; time.date = currentTime->tm_mday; time.weekday = currentTime->tm_wday; time.month = currentTime->tm_mon + 1; time.year = (currentTime->tm_year + 1900)%100; MicoRtcSetTime( &time ); goto exit; } } exit: if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err); mico_system_notify_remove( 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; }