Skip to content

Commit a0fdd65

Browse files
feat: change cts callback to get unix time
1 parent 1802d09 commit a0fdd65

File tree

2 files changed

+32
-84
lines changed

2 files changed

+32
-84
lines changed
Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
#include <zephyr/sys/byteorder.h>
22
#include <zephyr/logging/log.h>
33
#include <zephyr/bluetooth/gatt.h>
4+
#include <string.h>
45

5-
#include "current_time_service.h"
6+
#include "timeutils/timeutils.h"
7+
#include "devicetwin/devicetwin.h"
68

79
LOG_MODULE_REGISTER(ZephyrWatch_CurrentTimeService, LOG_LEVEL_INF);
810

9-
10-
/* Holds current time data. */
11-
static uint8_t m_current_time_data[10] = {
12-
232U, // Year (2024) last byte
13-
7U, // Year (2024) first byte
14-
5U, // Month (May)
15-
13U, // Day (13th)
16-
23U, // Hours (23)
17-
45U, // Minutes (45)
18-
30U, // Seconds (30)
19-
2U, // Day of week (Tuesday)
20-
0U, // Fractions 256 part of 'Exact Time 256'
21-
0U, // Adjust reason
22-
};
23-
24-
/* Current Time Service Declaration */
11+
/* Current Time Service Write Callback */
2512
static ssize_t m_time_write_callback(
2613
struct bt_conn *conn,
2714
const struct bt_gatt_attr *attr,
@@ -30,56 +17,45 @@ static ssize_t m_time_write_callback(
3017
uint16_t offset,
3118
uint8_t flags) {
3219

33-
// Get the value of the attribute.
34-
uint8_t *value = attr->user_data;
35-
36-
// Check if the value written is valid.
37-
if (offset + len > sizeof(m_current_time_data)) {
38-
LOG_ERR("Write request too long.");
20+
// Check if we received exactly 4 bytes for UNIX timestamp
21+
if (len != 4 || offset != 0) {
22+
LOG_ERR("Invalid write length or offset. Expected 4 bytes at offset 0, got %d bytes at offset %d", len, offset);
3923
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
4024
}
4125

42-
// Copy the new value into the m_current_time_data array (given as buffer).
43-
memcpy(value + offset, buf, len);
44-
LOG_INF("Current time updated from the Bluetooth client.");
26+
// Extract the UNIX timestamp from the buffer (assuming little-endian)
27+
uint32_t unix_timestamp = sys_le32_to_cpu(*(uint32_t *)buf);
28+
LOG_INF("Received UNIX timestamp: %u", unix_timestamp);
29+
30+
// Convert UNIX timestamp to UTC time
31+
utc_time_t utc_time = unix_to_utc(unix_timestamp);
32+
33+
// Get the device twin instance
34+
device_twin_t *device_twin = get_device_twin_instance();
35+
if (device_twin == NULL) {
36+
LOG_ERR("Failed to get device twin instance.");
37+
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
38+
}
39+
40+
// Update the device twin's current time
41+
device_twin->current_time = utc_time;
42+
43+
LOG_INF("Current time updated: %04d-%02d-%02d %02d:%02d:%02d",
44+
utc_time.year, utc_time.month, utc_time.day,
45+
utc_time.hour, utc_time.minute, utc_time.second);
46+
4547
return len;
4648
}
4749

50+
/* Dummy data for GATT characteristic - not used for actual data storage */
51+
static uint8_t dummy_data[4] = {0};
52+
4853
/* Current Time Service Declaration */
49-
/*
5054
BT_GATT_SERVICE_DEFINE(cts_cvs,
5155
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
5256
BT_GATT_CHARACTERISTIC(
5357
BT_UUID_CTS_CURRENT_TIME,
5458
BT_GATT_CHRC_WRITE,
5559
BT_GATT_PERM_WRITE,
56-
NULL, m_time_write_callback, m_current_time_data),
60+
NULL, m_time_write_callback, dummy_data),
5761
);
58-
*/
59-
60-
/* API for the Current Time Service application layer consumers.
61-
* - get_current_time: Get the current time. Returns a struct with
62-
* proper key-value pairs for year, month, hours, minutes, and seconds.
63-
* - set_current_time: Set the current time. Takes a struct with
64-
* proper key-value pairs for year, month, hours, minutes, and seconds.
65-
*/
66-
67-
struct current_time get_current_time(void) {
68-
struct current_time time;
69-
time.year = sys_le16_to_cpu(*(uint16_t *) &m_current_time_data[0]);
70-
time.month = m_current_time_data[2];
71-
time.day = m_current_time_data[3];
72-
time.hours = m_current_time_data[4];
73-
time.minutes = m_current_time_data[5];
74-
time.seconds = m_current_time_data[6];
75-
return time;
76-
}
77-
78-
void set_current_time(struct current_time time) {
79-
*(uint16_t *) &m_current_time_data[0] = sys_cpu_to_le16(time.year);
80-
m_current_time_data[2] = time.month;
81-
m_current_time_data[3] = time.day;
82-
m_current_time_data[4] = time.hours;
83-
m_current_time_data[5] = time.minutes;
84-
m_current_time_data[6] = time.seconds;
85-
}

src/bluetooth/services/current_time_service.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)