1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CHPP_CLIENT_TIMESYNC_H_ 18 #define CHPP_CLIENT_TIMESYNC_H_ 19 20 #include <stdbool.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 24 #include "chpp/app.h" 25 #include "chpp/clients.h" 26 #include "chpp/common/timesync.h" 27 #include "chpp/macros.h" 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 /** 34 * Maximum timesync step change at each measurement 35 */ 36 #ifndef CHPP_CLIENT_TIMESYNC_MAX_CHANGE_NS 37 #define CHPP_CLIENT_TIMESYNC_MAX_CHANGE_NS (100 * CHPP_NSEC_PER_MSEC) 38 #endif 39 40 /** 41 * Default maximum age of a time offset measurement 42 */ 43 #ifndef CHPP_TIMESYNC_DEFAULT_MAX_AGE_NS 44 #define CHPP_TIMESYNC_DEFAULT_MAX_AGE_NS (24 * CHPP_NSEC_PER_HOUR) 45 #endif 46 47 /** 48 * Timesync Results. 49 */ 50 struct ChppTimesyncResult { 51 enum ChppAppErrorCode error; // Indicates success or error type 52 int64_t offsetNs; // Time offset of service (service - client) 53 uint64_t rttNs; // RTT 54 uint64_t measurementTimeNs; // Time of last measurement 55 }; 56 57 /************************************************ 58 * Public functions 59 ***********************************************/ 60 61 /** 62 * Initializes the client. 63 * 64 * @param context Maintains status for each app layer instance. 65 */ 66 void chppTimesyncClientInit(struct ChppAppState *context); 67 68 /** 69 * Deinitializes the client. 70 * 71 * @param context Maintains status for each app layer instance. 72 */ 73 void chppTimesyncClientDeinit(struct ChppAppState *context); 74 75 /** 76 * Resets the client. 77 * 78 * @param context Maintains status for each app layer instance. 79 */ 80 void chppTimesyncClientReset(struct ChppAppState *context); 81 82 /** 83 * Dispatches an Rx Datagram from the transport layer that is determined to 84 * be for the CHPP Timesync Client. 85 * 86 * @param context Maintains status for each app layer instance. 87 * @param buf Input (response) datagram. Cannot be null. 88 * @param len Length of input data in bytes. 89 * 90 * @return Indicates success or failure. 91 */ 92 bool chppDispatchTimesyncServiceResponse(struct ChppAppState *context, 93 const uint8_t *buf, size_t len); 94 95 /** 96 * Initiates a CHPP timesync to measure time offset of the service. 97 * 98 * @param context Maintains status for each app layer instance. 99 * 100 * @return Indicates success or failure. 101 */ 102 bool chppTimesyncMeasureOffset(struct ChppAppState *context); 103 104 /** 105 * Provides the time offset of the service. If the latest measurement is within 106 * maxTimesyncAgeNs, this function reuses the last measurement. Otherwise, it 107 * will initiate a new measurement. 108 * 109 * @param context Maintains status for each app layer instance. 110 * @param maxTimesyncAgeNs Maximum acceptable age of measuement. 111 * 112 * @return Time offset of service vs client (service - client) 113 */ 114 int64_t chppTimesyncGetOffset(struct ChppAppState *context, 115 uint64_t maxTimesyncAgeNs); 116 117 /** 118 * Provides the raw results of the latest timesync measurement. 119 * 120 * @param context Maintains status for each app layer instance. 121 * 122 * @return Latest result. 123 */ 124 const struct ChppTimesyncResult *chppTimesyncGetResult( 125 struct ChppAppState *context); 126 127 #ifdef __cplusplus 128 } 129 #endif 130 131 #endif // CHPP_CLIENT_TIMESYNC_H_ 132