/* Funzione: set_time(sys_time _time) * Parametri: * sys_time _time : tempo di riferimento a cui settare il tempo di sistema * * @brief : Settaggio tempo di sistema * * */ void set_time(sys_time time){ suspend_time(); _sys_time.year = time.year; _sys_time.month = time.month; _sys_time.day = time.day; _sys_time.hour = time.hour; _sys_time.minutes = time.minutes; _sys_time.seconds = time.seconds; _sys_time.milliseconds = time.milliseconds; start_time(); }
void pre_suspend(void) { #ifdef CONFIG_NETFRONT suspend_netfront(); #endif suspend_xenbus(); local_irq_disable(); suspend_gnttab(); suspend_time(); suspend_console(); suspend_events(); }
/* @fun: client_sync_time(sys_time temp) * @par: sys_time temp: struttura contenente il tempo del nodo centrale * @brief : In seguito alla ricezione della risposta del server, * il client setta il tempo a quello del nodo centrale * * */ void client_sync_time(sys_time temp){ suspend_time(); sync_end = HAL_GetTick(); uint16_t delay = (sync_end - sync_init) / 2; uint16_t delay_millis; uint8_t delay_sec; _sys_time.hour = temp.hour; _sys_time.minutes = temp.minutes; _sys_time.seconds = temp.seconds; _sys_time.milliseconds = temp.milliseconds; if(delay >= 1000){ delay_millis = delay%1000; delay_sec = (int) (delay/1000); } else{ delay_millis = delay; delay_sec = 0; } if((temp.milliseconds+=delay_millis) >= 1000) { _sys_time.milliseconds = temp.milliseconds - 1000; if (++_sys_time.seconds % 60 == 0) { /*Incrementa secondi. Se sono arrivati a 60 ==> ho contato 1 minuto*/ _sys_time.seconds = 0; if (++_sys_time.minutes % 60 == 0) { /*Incrementa minuti. Se sono arrivati a 60 ==> ho contato 1 ora*/ _sys_time.minutes = 0; if (++_sys_time.hour % 24 == 0) { /*Incrementa ore. Se sono arrivate a 24 ==> ho contato 1 giorno*/ _sys_time.hour = 0; _sys_time.day++; /*Incrementa giorni.*/ /*Controlla valore del giorno a seconda del mese per capire se si deve incrementare il mese*/ if (((_sys_time.month & 1) && (_sys_time.month < 8)) || //gennaio, marzo, maggio ,luglio, (!(_sys_time.month & 1) && (_sys_time.month >= 8))) { //agosto, ottobre, dicembre if (_sys_time.day % 32 == 0) { _sys_time.day = 1; _sys_time.month++; } } else if (_sys_time.month == 2) { //febbraio if (_sys_time.day % 29 == 0) { _sys_time.day = 1; _sys_time.month++; } } else { if (_sys_time.day % 31 == 0) { //restanti mesi _sys_time.day = 1; _sys_time.month++; } } if (_sys_time.month % 13 == 0) {//Se i mesi sono arrivati a 12 ==> ho contato un anno _sys_time.month = 1; _sys_time.year++; } } } } } if ((temp.seconds += delay_sec) >= 60) { _sys_time.seconds = temp.seconds - 59; if (++_sys_time.minutes % 60 == 0) { /*Incrementa minuti. Se sono arrivati a 60 ==> ho contato 1 ora*/ _sys_time.minutes = 0; if (++_sys_time.hour % 24 == 0) { /*Incrementa ore. Se sono arrivate a 24 ==> ho contato 1 giorno*/ _sys_time.hour = 0; _sys_time.day++; /*Incrementa giorni.*/ /*Controlla valore del giorno a seconda del mese per capire se si deve incrementare il mese*/ if (((_sys_time.month & 1) && (_sys_time.month < 8)) || //gennaio, marzo, maggio ,luglio, (!(_sys_time.month & 1) && (_sys_time.month >= 8))) { //agosto, ottobre, dicembre if (_sys_time.day % 32 == 0) { _sys_time.day = 1; _sys_time.month++; } } else if (_sys_time.month == 2) { //febbraio if (_sys_time.day % 29 == 0) { _sys_time.day = 1; _sys_time.month++; } } else { if (_sys_time.day % 31 == 0) { //restanti mesi _sys_time.day = 1; _sys_time.month++; } } if (_sys_time.month % 13 == 0) {//Se i mesi sono arrivati a 12 ==> ho contato un anno _sys_time.month = 1; _sys_time.year++; } } } } start_time(); }
/* Funzione: client_request_time(uint8_t client_id) * Parametri: client_id: id del nodo che effettua la richiesta * @brief : chiede all'host di sincronizzare gli orologi * * */ void client_request_time(uint8_t client_id){ sys_time temp; suspend_time(); uint32_t send_tick = HAL_GetTick(); //network_level_request_time(client_id, &temp); uint32_t receive_tick = HAL_GetTick(); uint16_t delay = (receive_tick - send_tick) / 2; uint16_t old_millis = temp.milliseconds; temp.milliseconds += delay; if(old_millis < temp.milliseconds){ _sys_time.milliseconds = old_millis-temp.milliseconds; if (++_sys_time.seconds % 60 == 0) { /*Incrementa secondi. Se sono arrivati a 60 ==> ho contato 1 minuto*/ BSP_LED_Toggle(LED5); _sys_time.seconds = 0; if (++_sys_time.minutes % 60 == 0) { /*Incrementa minuti. Se sono arrivati a 60 ==> ho contato 1 ora*/ _sys_time.minutes = 0; if (++_sys_time.hour % 24 == 0) { /*Incrementa ore. Se sono arrivate a 24 ==> ho contato 1 giorno*/ BSP_LED_Toggle(LED3); _sys_time.hour = 0; _sys_time.day++; /*Incrementa giorni.*/ /*Controlla valore del giorno a seconda del mese per capire se si deve incrementare il mese*/ if (((_sys_time.month & 1) && (_sys_time.month < 8)) || //gennaio, marzo, maggio ,luglio, (!(_sys_time.month & 1) && (_sys_time.month >= 8))) { //agosto, ottobre, dicembre if (_sys_time.day % 32 == 0) { _sys_time.day = 1; _sys_time.month++; } } else if (_sys_time.month == 2) { //febbraio if (_sys_time.day % 29 == 0) { _sys_time.day = 1; _sys_time.month++; } } else { if (_sys_time.day % 31 == 0) { //restanti mesi _sys_time.day = 1; _sys_time.month++; } } if (_sys_time.month % 13 == 0) {//Se i mesi sono arrivati a 12 ==> ho contato un anno _sys_time.month = 1; _sys_time.year++; } } } } } start_time(); }