syncInterface.h (3762B)
1 /** 2 * @file syncInterface.h 3 * @author Adrian STEINER (steia19@bfh.ch) 4 * @brief synchronisation interface to get a valid reference time 5 * @version 0.1 6 * @date 12-02-2025 7 * 8 * @copyright (C) 2025 Adrian STEINER 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <https: //www.gnu.org/licenses/>. 21 * 22 */ 23 24 #ifndef SYNC_INTERFACE_H 25 #define SYNC_INTERFACE_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #include <stdbool.h> 32 #include <stdint.h> 33 #include <stdlib.h> 34 35 #include "tinytime.h" 36 37 /** 38 * @brief Sync status 39 * 40 */ 41 typedef enum { 42 SYNC_TIME_AND_DATE = 0, ///< Time received to synchronize 43 SYNC_TIME_ONLY, ///< Received time only 44 SYNC_WAITING, ///< Connection OK, but no time received 45 SYNC_ERROR ///< Error in connection 46 } syncStatus; 47 48 /** 49 * @brief Sync power callback prototype 50 * 51 */ 52 typedef void (*syncPwrCB)(void); 53 54 /** 55 * @brief Sync synchronise callback prototype 56 * @param tinyTimeType the crrent time if received 57 * 58 * @return the sync status 59 */ 60 typedef syncStatus (*synchronizeCB)(tinyTimeType *); 61 62 /** 63 * @brief Sync interface handler 64 * 65 */ 66 typedef struct { 67 /* Callbacks */ 68 syncPwrCB powerOn; 69 syncPwrCB powerOff; 70 synchronizeCB synchronize; 71 /* Used data */ 72 uint64_t lastSyncTime; ///< Last synchronize time 73 uint64_t expectedSyncTime; ///< The expected next update process 74 uint64_t syncDiff; ///< Last difference between clock and synchronization time 75 uint32_t syncDuration; ///< Current synchronisation duration in ms 76 bool isSynchronizing; ///< Flag that the sync process is running 77 } syncInterface; 78 79 /** 80 * @brief Initializes the synchronization interface. 81 * 82 * Sets up the synchronization interface with the provided callback functions 83 * and the last known synchronization time. This function prepares the interface 84 * for synchronization events. 85 * 86 * @param interfaceHandler Pointer to the synchronization interface structure to 87 * initialize. 88 * @param powerOnCB Callback function to be called when powering on. 89 * @param powerOffCB Callback function to be called when powering off. 90 * @param syncCB Callback function to be called during synchronization. 91 * @param lastSyncTime Timestamp of the last synchronization event (in 92 * milliseconds or ticks). 93 * @return uint8_t Returns EXIT_SUCCESS if initialization was successful, 94 * EXIT_FAILURE otherwise. 95 */ 96 uint8_t sync_initInterface(syncInterface *interfaceHandler, 97 syncPwrCB powerOnCB, 98 syncPwrCB powerOffCB, 99 synchronizeCB syncCB, 100 uint64_t lastSyncTime); 101 102 /** 103 * @brief Sync enable hardware 104 * 105 * @param interfaceHandler the sync interface handler reference 106 */ 107 void sync_enable(syncInterface *interfaceHandler); 108 109 /** 110 * @brief Stop synchronising 111 * 112 * @param interfaceHandler the sync interface handler reference 113 */ 114 void sync_abort(syncInterface *interfaceHandler); 115 116 /** 117 * @brief Check the synchronisung function to recieve the time 118 * 119 * @param interfaceHandler the sync interface handler reference 120 * @return the sync status 121 122 */ 123 syncStatus sync_synchronize(syncInterface *interfaceHandler); 124 125 #ifdef __cplusplus 126 } 127 #endif 128 129 #endif /* SYNC_INTERFACE_H*/