syncInterface.c (2172B)
1 /** 2 * @file syncInterface.c 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 #include "syncInterface.h" 25 26 #include <stdint.h> 27 #include <stdlib.h> 28 29 #include "xtotpConfig.h" 30 #include "xtotpUtil.h" 31 32 uint8_t sync_initInterface( 33 syncInterface *interfaceHandler, 34 syncPwrCB powerOnCB, 35 syncPwrCB powerOffCB, 36 synchronizeCB syncCB, 37 uint64_t lastSyncTime) { 38 if (NULL == interfaceHandler || NULL == powerOnCB || NULL == powerOffCB || NULL == syncCB) { 39 return EXIT_FAILURE; 40 } 41 // Register callbacks and initial values 42 interfaceHandler->powerOn = powerOnCB; 43 interfaceHandler->powerOff = powerOffCB; 44 interfaceHandler->synchronize = syncCB; 45 interfaceHandler->lastSyncTime = lastSyncTime; 46 interfaceHandler->expectedSyncTime = lastSyncTime + XTOTP_SYNC_UPDATE_PERIOD; 47 48 // Set all other data to 0 49 interfaceHandler->isSynchronizing = false; 50 interfaceHandler->syncDuration = 0; 51 interfaceHandler->syncDiff = 0; 52 return EXIT_SUCCESS; 53 } 54 55 void sync_enable(syncInterface *interfaceHandler) { 56 if (interfaceHandler->isSynchronizing) { 57 return; 58 } 59 interfaceHandler->powerOn(); 60 interfaceHandler->isSynchronizing = true; 61 interfaceHandler->syncDuration = 0; 62 } 63 64 void sync_abort(syncInterface *interfaceHandler) { 65 if (!interfaceHandler->isSynchronizing) { 66 return; 67 } 68 interfaceHandler->powerOff(); 69 interfaceHandler->isSynchronizing = false; 70 }