Skip to content

Commit f9f6366

Browse files
committed
feat(ble_services): Support Current Time Service
1 parent dfc210d commit f9f6366

File tree

22 files changed

+757
-10
lines changed

22 files changed

+757
-10
lines changed

.gitlab/ci/build.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ build_example_bluetooth_ble_services_ble_bas:
185185
variables:
186186
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_bas
187187

188-
build_example_bluetooth_ble_services_ble_dis:
188+
build_example_bluetooth_ble_services_ble_bcs:
189189
extends:
190190
- .build_examples_template
191191
- .rules:build:example_bluetooth_ble_services
@@ -196,9 +196,9 @@ build_example_bluetooth_ble_services_ble_dis:
196196
- IMAGE: espressif/idf:release-v5.1
197197
- IMAGE: espressif/idf:release-v5.2
198198
variables:
199-
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_dis
199+
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_bcs
200200

201-
build_example_bluetooth_ble_services_ble_bcs:
201+
build_example_bluetooth_ble_services_ble_cts:
202202
extends:
203203
- .build_examples_template
204204
- .rules:build:example_bluetooth_ble_services
@@ -209,7 +209,20 @@ build_example_bluetooth_ble_services_ble_bcs:
209209
- IMAGE: espressif/idf:release-v5.1
210210
- IMAGE: espressif/idf:release-v5.2
211211
variables:
212-
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_bcs
212+
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_cts
213+
214+
build_example_bluetooth_ble_services_ble_dis:
215+
extends:
216+
- .build_examples_template
217+
- .rules:build:example_bluetooth_ble_services
218+
parallel:
219+
matrix:
220+
- IMAGE: espressif/idf:release-v4.4
221+
- IMAGE: espressif/idf:release-v5.0
222+
- IMAGE: espressif/idf:release-v5.1
223+
- IMAGE: espressif/idf:release-v5.2
224+
variables:
225+
EXAMPLE_DIR: examples/bluetooth/ble_services/ble_dis
213226

214227
build_example_bluetooth_ble_services_ble_hrs:
215228
extends:

components/bluetooth/ble_services/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ list(APPEND srcs "uds/src/esp_uds.c")
4444
list(APPEND include "uds/include")
4545
endif()
4646

47+
if(CONFIG_BLE_CTS)
48+
list(APPEND srcs "cts/src/esp_cts.c")
49+
list(APPEND include "cts/include")
50+
endif()
51+
4752
list(APPEND req "ble_conn_mgr")
4853

4954
idf_component_register(SRCS "${srcs}"

components/bluetooth/ble_services/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ menu "BLE Standard Services"
33
orsource "./ans/Kconfig.in"
44
orsource "./bas/Kconfig.in"
55
orsource "./bcs/Kconfig.in"
6+
orsource "./cts/Kconfig.in"
67
orsource "./dis/Kconfig.in"
78
orsource "./hrs/Kconfig.in"
89
orsource "./hts/Kconfig.in"

components/bluetooth/ble_services/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ The example will be downloaded to the current folder. You can navigate into it f
2929
1. [ble_ans](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_ans)
3030
2. [ble_bas](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_bas)
3131
3. [ble_bcs](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_bcs)
32-
4. [ble_dis](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_dis)
33-
5. [ble_hrs](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_hrs)
34-
6. [ble_hts](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_hts)
35-
7. [ble_tps](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_tps)
36-
8. [ble_uds](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_uds)
32+
4. [ble_cts](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_cts)
33+
5. [ble_dis](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_dis)
34+
6. [ble_hrs](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_hrs)
35+
7. [ble_hts](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_hts)
36+
8. [ble_tps](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_tps)
37+
9. [ble_uds](https://github.com/espressif/esp-iot-solution/tree/master/examples/bluetooth/ble_services/ble_uds)
3738

3839
### Q&A
3940

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Current Time Service
2+
3+
menuconfig BLE_CTS
4+
bool "GATT Current Time Service"
5+
6+
if BLE_CTS
7+
8+
config BLE_CTS_CURRENT_TIME_WRITE_ENABLE
9+
bool "Current time write enable"
10+
default y
11+
help
12+
Set y to enable write or n to disable it.
13+
14+
config BLE_CTS_LOCAL_TIME_CHAR_ENABLE
15+
bool "Local Time Information Characteristic"
16+
default y
17+
help
18+
Set y to support CTS Local time characteristic or n to disable it.
19+
20+
config BLE_CTS_LOCAL_TIME_WRITE_ENABLE
21+
bool "Local time write enable"
22+
default y
23+
depends on BLE_CTS_LOCAL_TIME_CHAR_ENABLE
24+
help
25+
Set y to enable write or n to disable it.
26+
27+
config BLE_CTS_REF_TIME_CHAR_ENABLE
28+
bool "Reference Time Information Characteristic"
29+
default y
30+
help
31+
Set y to support CTS Reference time characteristic or n to disable it.
32+
33+
endif # BLE_CTS
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)