1 /*
2  * Copyright (c) 2024 A Labs GmbH
3  * Copyright (c) 2024 tado GmbH
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/device.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/lorawan/emul.h>
11 #include <zephyr/lorawan/lorawan.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/ztest.h>
15 
16 #define CMD_PACKAGE_VERSION             (0x00)
17 #define CMD_APP_TIME                    (0x01)
18 #define CMD_DEVICE_APP_TIME_PERIODICITY (0x02)
19 #define CMD_FORCE_DEVICE_RESYNC         (0x03)
20 
21 #define CLOCK_SYNC_PORT (202)
22 #define CLOCK_SYNC_ID
23 
24 struct lorawan_msg {
25 	/* large enough buffer to fit maximum clock sync message length */
26 	uint8_t data[6];
27 	uint8_t len;
28 };
29 
30 K_MSGQ_DEFINE(uplink_msgq, sizeof(struct lorawan_msg), 10, 4);
31 
uplink_handler(uint8_t port,uint8_t len,const uint8_t * data)32 void uplink_handler(uint8_t port, uint8_t len, const uint8_t *data)
33 {
34 	struct lorawan_msg msg;
35 	int ret;
36 
37 	zassert_equal(port, CLOCK_SYNC_PORT);
38 
39 	zassert_true(len <= sizeof(msg.data));
40 	memcpy(msg.data, data, len);
41 	msg.len = len;
42 
43 	ret = k_msgq_put(&uplink_msgq, &msg, K_NO_WAIT);
44 	zassert_equal(ret, 0);
45 }
46 
ZTEST(clock_sync,test_package_version)47 ZTEST(clock_sync, test_package_version)
48 {
49 	struct lorawan_msg ans;
50 	uint8_t req_data[] = {CMD_PACKAGE_VERSION};
51 	int ret;
52 
53 	k_msgq_purge(&uplink_msgq);
54 
55 	lorawan_emul_send_downlink(CLOCK_SYNC_PORT, false, 0, 0, sizeof(req_data), req_data);
56 
57 	ret = k_msgq_get(&uplink_msgq, &ans, K_MSEC(100));
58 	zassert_equal(ret, 0, "receiving PackageVersionAns timed out");
59 	zassert_equal(ans.len, 3);
60 	zassert_equal(ans.data[0], CMD_PACKAGE_VERSION);
61 	zassert_equal(ans.data[1], 1); /* PackageIdentifier */
62 	zassert_equal(ans.data[2], 2); /* PackageVersion */
63 }
64 
ZTEST(clock_sync,test_app_time)65 ZTEST(clock_sync, test_app_time)
66 {
67 	uint8_t ans_data[6] = {CMD_APP_TIME};
68 	struct lorawan_msg req;
69 	uint32_t device_time;
70 	uint32_t gps_time;
71 	uint8_t token_req;
72 	int ret;
73 
74 	k_msgq_purge(&uplink_msgq);
75 
76 	/* wait for more than the default (=minimum) periodicity of 128s + 30s jitter */
77 	ret = k_msgq_get(&uplink_msgq, &req, K_SECONDS(128 + 30 + 1));
78 	zassert_equal(ret, 0, "receiving AppTimeReq timed out");
79 	zassert_equal(req.len, 6);
80 	zassert_equal(req.data[0], CMD_APP_TIME);
81 
82 	device_time = sys_get_le32(req.data + 1);
83 	token_req = req.data[5] & 0xF;
84 	zassert_within((int)device_time, k_uptime_seconds(), 1);
85 
86 	/* apply a time correction of 1000 seconds */
87 	sys_put_le32(1000, ans_data + 1);
88 	ans_data[5] = token_req;
89 
90 	lorawan_emul_send_downlink(CLOCK_SYNC_PORT, false, 0, 0, sizeof(ans_data), ans_data);
91 
92 	lorawan_clock_sync_get(&gps_time);
93 	zassert_within(gps_time, k_uptime_seconds() + 1000, 1);
94 }
95 
ZTEST(clock_sync,test_device_app_time_periodicity)96 ZTEST(clock_sync, test_device_app_time_periodicity)
97 {
98 	const uint8_t period = 1; /* actual periodicity in seconds: 128 * 2^period */
99 	uint8_t req_data[] = {
100 		CMD_DEVICE_APP_TIME_PERIODICITY,
101 		period & 0xF,
102 	};
103 	struct lorawan_msg app_time_req;
104 	struct lorawan_msg ans;
105 	uint32_t device_time;
106 	uint32_t gps_time;
107 	int ret;
108 
109 	k_msgq_purge(&uplink_msgq);
110 
111 	lorawan_emul_send_downlink(CLOCK_SYNC_PORT, false, 0, 0, sizeof(req_data), req_data);
112 
113 	ret = k_msgq_get(&uplink_msgq, &ans, K_MSEC(100));
114 	zassert_equal(ret, 0, "receiving DeviceAppTimePeriodicityAns timed out");
115 	zassert_equal(ans.len, 6);
116 	zassert_equal(ans.data[0], CMD_DEVICE_APP_TIME_PERIODICITY);
117 	zassert_equal(ans.data[1], 0);
118 
119 	device_time = sys_get_le32(ans.data + 2);
120 	lorawan_clock_sync_get(&gps_time);
121 	zassert_within(device_time, gps_time, 1);
122 
123 	/* wait for more than the old periodicity of 128s + 30s jitter */
124 	ret = k_msgq_get(&uplink_msgq, &app_time_req, K_SECONDS(128 + 30 + 1));
125 	zassert_equal(ret, -EAGAIN, "received AppTimeReq too early");
126 
127 	/* wait for another 128s to cover the new periodicity of 256s + 30s jitter */
128 	ret = k_msgq_get(&uplink_msgq, &app_time_req, K_SECONDS(128));
129 	zassert_equal(ret, 0, "receiving AppTimeReq timed out");
130 	zassert_equal(app_time_req.len, 6);
131 	zassert_equal(app_time_req.data[0], CMD_APP_TIME);
132 
133 	/* reset to minimum periodicity */
134 	req_data[1] = 0;
135 	lorawan_emul_send_downlink(CLOCK_SYNC_PORT, false, 0, 0, sizeof(req_data), req_data);
136 	ret = k_msgq_get(&uplink_msgq, &ans, K_MSEC(100));
137 	zassert_equal(ret, 0, "receiving DeviceAppTimePeriodicityAns timed out");
138 	zassert_equal(ans.len, 6);
139 	zassert_equal(ans.data[0], CMD_DEVICE_APP_TIME_PERIODICITY);
140 }
141 
ZTEST(clock_sync,test_force_device_resync)142 ZTEST(clock_sync, test_force_device_resync)
143 {
144 	const uint8_t nb_transmissions = 2;
145 	uint8_t resync_req_data[] = {
146 		CMD_FORCE_DEVICE_RESYNC,
147 		nb_transmissions,
148 	};
149 	struct lorawan_msg app_time_req;
150 	int ret;
151 
152 	k_msgq_purge(&uplink_msgq);
153 
154 	lorawan_emul_send_downlink(CLOCK_SYNC_PORT, false, 0, 0, sizeof(resync_req_data),
155 				   resync_req_data);
156 
157 	for (int i = 0; i < nb_transmissions; i++) {
158 		/* wait for more than CLOCK_RESYNC_DELAY of 10 secs */
159 		ret = k_msgq_get(&uplink_msgq, &app_time_req, K_SECONDS(11));
160 		zassert_equal(ret, 0, "receiving AppTimeReq #%d timed out", i + 1);
161 		zassert_equal(app_time_req.len, 6);
162 		zassert_equal(app_time_req.data[0], CMD_APP_TIME);
163 	}
164 }
165 
clock_sync_setup(void)166 static void *clock_sync_setup(void)
167 {
168 	const struct device *lora_dev = DEVICE_DT_GET(DT_ALIAS(lora0));
169 	struct lorawan_join_config join_cfg = {0};
170 	int ret;
171 
172 	zassert_true(device_is_ready(lora_dev), "LoRa device not ready");
173 
174 	ret = lorawan_start();
175 	zassert_equal(ret, 0, "lorawan_start failed: %d", ret);
176 
177 	ret = lorawan_join(&join_cfg);
178 	zassert_equal(ret, 0, "lorawan_join failed: %d", ret);
179 
180 	lorawan_emul_register_uplink_callback(uplink_handler);
181 
182 	ret = lorawan_clock_sync_run();
183 	zassert_equal(ret, 0, "clock_sync_run failed: %d", ret);
184 
185 	/* wait for first messages to be processed in the background */
186 	k_sleep(K_SECONDS(1));
187 
188 	return NULL;
189 }
190 
191 ZTEST_SUITE(clock_sync, NULL, clock_sync_setup, NULL, NULL, NULL);
192