1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 * Copyright (c) 2023 Jamie M.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "smp_test_util.h"
9 #include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
10 #include <zephyr/net_buf.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zcbor_encode.h>
13
14 /* SMP header function for generating os_mgmt datetime command header with sequence number set
15 * to 1
16 */
smp_make_hdr(struct smp_hdr * rsp_hdr,size_t len,bool version2,bool write)17 void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len, bool version2, bool write)
18 {
19 *rsp_hdr = (struct smp_hdr) {
20 .nh_len = sys_cpu_to_be16(len),
21 .nh_flags = 0,
22 .nh_version = (version2 == true ? SMP_MCUMGR_VERSION_2 : SMP_MCUMGR_VERSION_1),
23 .nh_op = (write == true ? MGMT_OP_WRITE : MGMT_OP_READ),
24 .nh_group = sys_cpu_to_be16(MGMT_GROUP_ID_OS),
25 .nh_seq = 1,
26 .nh_id = OS_MGMT_ID_DATETIME_STR,
27 };
28 }
29
30 /* Function for creating an os_mgmt datetime get command */
create_mcumgr_datetime_get_packet(zcbor_state_t * zse,bool version2,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)31 bool create_mcumgr_datetime_get_packet(zcbor_state_t *zse, bool version2, uint8_t *buffer,
32 uint8_t *output_buffer, uint16_t *buffer_size)
33 {
34 bool ok;
35
36 ok = zcbor_map_start_encode(zse, 2) &&
37 zcbor_map_end_encode(zse, 2);
38
39 *buffer_size = (zse->payload_mut - buffer);
40 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, version2, false);
41 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
42 *buffer_size += sizeof(struct smp_hdr);
43
44 return ok;
45 }
46
47 /* Functions for creating an os_mgmt datetime set command */
create_mcumgr_datetime_set_packet_str(zcbor_state_t * zse,bool version2,const char * data,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)48 bool create_mcumgr_datetime_set_packet_str(zcbor_state_t *zse, bool version2, const char *data,
49 uint8_t *buffer, uint8_t *output_buffer,
50 uint16_t *buffer_size)
51 {
52 bool ok = zcbor_map_start_encode(zse, 2) &&
53 zcbor_tstr_put_lit(zse, "datetime") &&
54 zcbor_tstr_put_term(zse, data, CONFIG_ZCBOR_MAX_STR_LEN) &&
55 zcbor_map_end_encode(zse, 2);
56
57 *buffer_size = (zse->payload_mut - buffer);
58 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, version2, true);
59 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
60 *buffer_size += sizeof(struct smp_hdr);
61
62 return ok;
63 }
64
create_mcumgr_datetime_set_packet(zcbor_state_t * zse,bool version2,struct rtc_time * a_time,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)65 bool create_mcumgr_datetime_set_packet(zcbor_state_t *zse, bool version2, struct rtc_time *a_time,
66 uint8_t *buffer, uint8_t *output_buffer,
67 uint16_t *buffer_size)
68 {
69 char tmp_str[32];
70
71 sprintf(tmp_str, "%4d-%02d-%02dT%02d:%02d:%02d", (uint16_t)a_time->tm_year,
72 (uint8_t)a_time->tm_mon, (uint8_t)a_time->tm_mday, (uint8_t)a_time->tm_hour,
73 (uint8_t)a_time->tm_min, (uint8_t)a_time->tm_sec);
74
75 return create_mcumgr_datetime_set_packet_str(zse, version2, tmp_str, buffer,
76 output_buffer, buffer_size);
77 }
78