|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <stdbool.h> |
| 10 | +#include <stdint.h> |
| 11 | + |
| 12 | +#include "esp_err.h" |
| 13 | +#include "esp_event_base.h" |
| 14 | + |
| 15 | +#ifdef __cplusplus |
| 16 | +extern "C" |
| 17 | +{ |
| 18 | +#endif |
| 19 | + |
| 20 | +/** @cond **/ |
| 21 | +/* BLE CTS EVENTS BASE */ |
| 22 | +ESP_EVENT_DECLARE_BASE(BLE_CTS_EVENTS); |
| 23 | +/** @endcond **/ |
| 24 | + |
| 25 | +/* 16 Bit Current Time Service UUID */ |
| 26 | +#define BLE_CTS_UUID16 0x1805 |
| 27 | + |
| 28 | +/* 16 Bit Current Time Characteristics UUID */ |
| 29 | +#define BLE_CTS_CHR_UUID16_CURRENT_TIME 0x2A2B |
| 30 | +#define BLE_CTS_CHR_UUID16_LOCAL_TIME 0x2A0F |
| 31 | +#define BLE_CTS_CHR_UUID16_REFERENCE_TIME 0x2A14 |
| 32 | + |
| 33 | +/* CTS Adjust Reason Masks */ |
| 34 | +#define BLE_CTS_MANUAL_TIME_UPDATE_MASK (1 << 0) |
| 35 | +#define BLE_CTS_EXTERNAL_REFERENCE_TIME_UPDATE_MASK (1 << 1) |
| 36 | +#define BLE_CTS_CHANGE_OF_TIME_ZONE_MASK (1 << 2) |
| 37 | +#define BLE_CTS_CHANGE_OF_DST_MASK (1 << 3) |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Current Time |
| 41 | + */ |
| 42 | +typedef struct { |
| 43 | + struct { |
| 44 | + uint16_t year; /*!< Year as defined by the Gregorian calendar, Valid range 1582 to 9999 */ |
| 45 | + uint8_t month; /*!< Month of the year as defined by the Gregorian calendar, Valid range 1 (January) to 12 (December) */ |
| 46 | + uint8_t day; /*!< Day of the month as defined by the Gregorian calendar, Valid range 1 to 31 */ |
| 47 | + uint8_t hours; /*!< Number of hours past midnight, Valid range 0 to 23 */ |
| 48 | + uint8_t minutes; /*!< Number of minutes since the start of the hour. Valid range 0 to 59 */ |
| 49 | + uint8_t seconds; /*!< Number of seconds since the start of the minute. Valid range 0 to 59 */ |
| 50 | + } __attribute__((packed)) timestamp; /*!< The date and time */ |
| 51 | + |
| 52 | + uint8_t day_of_week; /*!< Day_of_week. valid range : 1(Monday) - 7(Sunday), 0 means day of week not known */ |
| 53 | + uint8_t fractions_256; /*!< Fractions_256. the number of 1 / 256 fractions of second, valid range : 0 - 255 */ |
| 54 | + uint8_t adjust_reason; /*!< This field represents reason(s) for adjusting time */ |
| 55 | +} __attribute__((packed)) esp_ble_cts_cur_time_t; |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Local Time Information |
| 59 | + */ |
| 60 | +typedef struct { |
| 61 | + /* This field represents the offset from UTC in number of 15-minute increments. |
| 62 | + * Valid range from -48 to +56. |
| 63 | + * A value of -128 means that the time zone offset is not known. |
| 64 | + * All other values are Reserved for Future Use. |
| 65 | + * The offset defined in this characteristic is constant regardless of whether daylight savings is in effect. |
| 66 | + */ |
| 67 | + int8_t timezone; /*!< Time zone */ |
| 68 | + /** dst_offset. |
| 69 | + * allowed values : 0, 2, 4, 8, 255 |
| 70 | + */ |
| 71 | + uint8_t dst_offset; /*!< Dst offset */ |
| 72 | +} __attribute__((packed)) esp_ble_cts_local_time_t; |
| 73 | + |
| 74 | +/** |
| 75 | + * @brief reference time information |
| 76 | + */ |
| 77 | +typedef struct { |
| 78 | + /** time_source. |
| 79 | + * valid range : 0 - 253 |
| 80 | + * 255 means not known |
| 81 | + */ |
| 82 | + uint8_t time_source; /*!< Time source */ |
| 83 | + /* |
| 84 | + * This field represents accuracy (drift) of time information in steps of 1/8 of a second (125ms) compared to a reference time source. |
| 85 | + * Valid range from 0 to 253 (0s to 31.625s). |
| 86 | + * A value of 254 means drift is larger than 31.625s. |
| 87 | + * A value of 255 means drift is unknown. |
| 88 | + */ |
| 89 | + uint8_t time_accuracy; /*!< Time accuracy */ |
| 90 | + /** days_since_update. |
| 91 | + * valid range : 0 - 254 |
| 92 | + * A value of 255 is used when the time span is greater than or equal to 255 days |
| 93 | + */ |
| 94 | + uint8_t days_since_update; /*!< Days since update */ |
| 95 | + /** hours_since_update. |
| 96 | + * valid range : 0 - 23 |
| 97 | + * A value of 255 is used when the time span is greater than or equal to 255 days |
| 98 | + */ |
| 99 | + uint8_t hours_since_update; /*!< Hours since update */ |
| 100 | +} __attribute__((packed)) esp_ble_cts_ref_time_t; |
| 101 | + |
| 102 | +/** |
| 103 | + * @brief Read the value of Current Time characteristic. |
| 104 | + * |
| 105 | + * @param[in] out_val The pointer to store the Current Time Increment. |
| 106 | + * |
| 107 | + * @return |
| 108 | + * - ESP_OK on successful |
| 109 | + * - ESP_ERR_INVALID_ARG on wrong parameter |
| 110 | + */ |
| 111 | +esp_err_t esp_ble_cts_get_current_time(esp_ble_cts_cur_time_t *out_val); |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Set the Current Time characteristic value. |
| 115 | + * |
| 116 | + * @param[in] in_val The pointer to store the Current Time. |
| 117 | + * |
| 118 | + * @param[in] need_send send the current time to remote client. |
| 119 | + * |
| 120 | + * @return |
| 121 | + * - ESP_OK on successful |
| 122 | + * - ESP_ERR_INVALID_ARG on wrong initialization |
| 123 | + * - ESP_FAIL on error |
| 124 | + */ |
| 125 | +esp_err_t esp_ble_cts_set_current_time(esp_ble_cts_cur_time_t *in_val, bool need_send); |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief Read the value of Local Time characteristic. |
| 129 | + * |
| 130 | + * @param[in] out_val The pointer to store the Local Time Increment. |
| 131 | + * |
| 132 | + * @return |
| 133 | + * - ESP_OK on successful |
| 134 | + * - ESP_ERR_INVALID_ARG on wrong parameter |
| 135 | + */ |
| 136 | +esp_err_t esp_ble_cts_get_local_time(esp_ble_cts_local_time_t *out_val); |
| 137 | + |
| 138 | +/** |
| 139 | + * @brief Set the Local Time characteristic value. |
| 140 | + * |
| 141 | + * @param[in] in_val The pointer to store the Local Time. |
| 142 | + * |
| 143 | + * @return |
| 144 | + * - ESP_OK on successful |
| 145 | + */ |
| 146 | +esp_err_t esp_ble_cts_set_local_time(esp_ble_cts_local_time_t *in_val); |
| 147 | + |
| 148 | +/** |
| 149 | + * @brief Read the value of Reference Time characteristic. |
| 150 | + * |
| 151 | + * @param[in] out_val The pointer to store the Reference Time Increment. |
| 152 | + * |
| 153 | + * @return |
| 154 | + * - ESP_OK on successful |
| 155 | + * - ESP_ERR_INVALID_ARG on wrong parameter |
| 156 | + */ |
| 157 | +esp_err_t esp_ble_cts_get_reference_time(esp_ble_cts_ref_time_t *out_val); |
| 158 | + |
| 159 | +/** |
| 160 | + * @brief Set the Reference Time characteristic value. |
| 161 | + * |
| 162 | + * @param[in] in_val The pointer to store the Reference Time. |
| 163 | + * |
| 164 | + * @return |
| 165 | + * - ESP_OK on successful |
| 166 | + */ |
| 167 | +esp_err_t esp_ble_cts_set_reference_time(esp_ble_cts_ref_time_t *in_val); |
| 168 | + |
| 169 | +/** |
| 170 | + * @brief Initialization Current Time Service |
| 171 | + * |
| 172 | + * @return |
| 173 | + * - ESP_OK on successful |
| 174 | + * - ESP_ERR_INVALID_ARG on wrong initialization |
| 175 | + * - ESP_FAIL on error |
| 176 | + */ |
| 177 | +esp_err_t esp_ble_cts_init(void); |
| 178 | + |
| 179 | +#ifdef __cplusplus |
| 180 | +} |
| 181 | +#endif |
0 commit comments