Skip to content

Commit 37743f6

Browse files
committed
refactor(posix/supervisor): move zcbor helpers to its own file
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 5f97579 commit 37743f6

5 files changed

Lines changed: 174 additions & 150 deletions

File tree

src/samples/supervisor/posix/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_library(OcreClient)
2222
target_sources(OcreClient
2323
PRIVATE
2424
client/client.c
25+
zcbor_helpers.c
2526
zcbor/src/zcbor_encode.c
2627
zcbor/src/zcbor_decode.c
2728
zcbor/src/zcbor_common.c
@@ -31,6 +32,7 @@ target_include_directories(OcreClient
3132
PUBLIC ../../../ocre/include
3233
PUBLIC ../../../runtime/include
3334
PRIVATE zcbor/include
35+
PRIVATE .
3436
)
3537

3638

@@ -94,6 +96,7 @@ target_link_libraries(ocre_cli PRIVATE Threads::Threads)
9496

9597
add_executable(ocred
9698
ocred.c
99+
zcbor_helpers.c
97100
zcbor/src/zcbor_encode.c
98101
zcbor/src/zcbor_decode.c
99102
zcbor/src/zcbor_common.c

src/samples/supervisor/posix/client/client.c

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <ocre/ocre.h>
2727

2828
#include "../ipc.h"
29+
#include "zcbor_helpers.h"
2930

3031
#define SOCK_PATH "ocre.sock"
3132

@@ -106,59 +107,6 @@ int ocre_is_valid_id(const char *id)
106107
return 1;
107108
}
108109

109-
/* Helper to encode a string or nil if NULL */
110-
static bool encode_string_or_nil(zcbor_state_t *state, const char *str)
111-
{
112-
if (str == NULL) {
113-
return zcbor_nil_put(state, NULL);
114-
}
115-
return zcbor_tstr_put_term(state, str, STRING_BUFFER_SIZE);
116-
}
117-
118-
/* Helper to encode a string array (NULL-terminated) */
119-
static bool encode_string_array(zcbor_state_t *state, const char **arr)
120-
{
121-
if (arr == NULL) {
122-
return zcbor_nil_put(state, NULL);
123-
}
124-
125-
/* Count the strings */
126-
size_t count = 0;
127-
while (arr[count] != NULL) {
128-
count++;
129-
}
130-
131-
/* Encode as a CBOR array */
132-
if (!zcbor_list_start_encode(state, count)) {
133-
return false;
134-
}
135-
136-
for (size_t i = 0; i < count; i++) {
137-
if (!zcbor_tstr_put_term(state, arr[i], STRING_BUFFER_SIZE)) {
138-
return false;
139-
}
140-
}
141-
142-
return zcbor_list_end_encode(state, count);
143-
}
144-
145-
static bool encode_request_and_container_id(zcbor_state_t *state, uint32_t req, const char *container_id)
146-
{
147-
bool success = zcbor_uint32_put(state, req);
148-
if (!success) {
149-
printf("Encoding req failed: %d\n", zcbor_peek_error(state));
150-
return false;
151-
}
152-
153-
success = encode_string_or_nil(state, container_id);
154-
if (!success) {
155-
printf("Encoding container id failed: %d\n", zcbor_peek_error(state));
156-
return false;
157-
}
158-
159-
return true;
160-
}
161-
162110
/* Stub implementation of ocre_build_configuration */
163111
const struct ocre_config ocre_build_configuration = {
164112
.version = "stub",

src/samples/supervisor/posix/ocred.c

Lines changed: 12 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <ocre/ocre.h>
2424

2525
#include "ipc.h"
26+
#include "zcbor_helpers.h"
2627

2728
#define SOCK_PATH "ocre.sock"
2829
#define RX_BUFFER_SIZE 2048
@@ -34,100 +35,14 @@
3435
/* Global ocre context for the server */
3536
static struct ocre_context *ctx = NULL;
3637

37-
static void print_hex(const char *label, const uint8_t *data, size_t len)
38-
{
39-
printf("HEX %s: ", label);
40-
for (size_t i = 0; i < len; i++) {
41-
printf("%02x ", data[i]);
42-
}
43-
printf("\n");
44-
}
45-
46-
/* Helper to decode a string or nil, returns true if string was decoded, false if nil */
47-
static bool decode_string_or_nil(zcbor_state_t *state, char *buffer, size_t buffer_size, bool *is_nil)
48-
{
49-
*is_nil = false;
50-
51-
/* Try to decode nil first */
52-
if (zcbor_nil_expect(state, NULL)) {
53-
*is_nil = true;
54-
buffer[0] = '\0';
55-
return true;
56-
}
57-
58-
zcbor_pop_error(state);
59-
60-
/* Decode as string */
61-
struct zcbor_string str;
62-
if (!zcbor_tstr_decode(state, &str)) {
63-
return false;
64-
}
65-
66-
size_t copy_len = str.len < buffer_size - 1 ? str.len : buffer_size - 1;
67-
memcpy(buffer, str.value, copy_len);
68-
buffer[copy_len] = '\0';
69-
70-
return true;
71-
}
72-
73-
/* Helper to decode a string array (CBOR list of strings) */
74-
static bool decode_string_array(zcbor_state_t *state, char **arr, size_t max_count, size_t *out_count, bool *is_nil)
75-
{
76-
*is_nil = false;
77-
*out_count = 0;
78-
79-
/* Try to decode nil first */
80-
if (zcbor_nil_expect(state, NULL)) {
81-
*is_nil = true;
82-
return true;
83-
}
84-
85-
zcbor_pop_error(state);
86-
87-
/* Decode as list */
88-
if (!zcbor_list_start_decode(state)) {
89-
return false;
90-
}
91-
92-
size_t count = 0;
93-
while (!zcbor_list_end_decode(state) && count < max_count) {
94-
struct zcbor_string str;
95-
if (!zcbor_tstr_decode(state, &str)) {
96-
break;
97-
}
98-
99-
/* Allocate and copy string */
100-
arr[count] = malloc(str.len + 1);
101-
if (arr[count] == NULL) {
102-
return false;
103-
}
104-
memcpy(arr[count], str.value, str.len);
105-
arr[count][str.len] = '\0';
106-
count++;
107-
}
108-
109-
arr[count] = NULL; /* NULL-terminate the array */
110-
*out_count = count;
111-
112-
return true;
113-
}
114-
115-
/* Helper to free a string array */
116-
static void free_string_array(char **arr, size_t count)
117-
{
118-
for (size_t i = 0; i < count; i++) {
119-
free(arr[i]);
120-
}
121-
}
122-
123-
/* Helper to encode a string or nil */
124-
static bool encode_string_or_nil(zcbor_state_t *state, const char *str)
125-
{
126-
if (str == NULL) {
127-
return zcbor_nil_put(state, NULL);
128-
}
129-
return zcbor_tstr_put_term(state, str, STRING_BUFFER_SIZE);
130-
}
38+
// static void print_hex(const char *label, const uint8_t *data, size_t len)
39+
// {
40+
// printf("HEX %s: ", label);
41+
// for (size_t i = 0; i < len; i++) {
42+
// printf("%02x ", data[i]);
43+
// }
44+
// printf("\n");
45+
// }
13146

13247
static int handle_context_create_container(zcbor_state_t *dec_state, uint8_t *tx_buf, size_t tx_buf_size)
13348
{
@@ -881,8 +796,8 @@ int main(void)
881796
break;
882797
}
883798

884-
printf("Received %d bytes\n", n);
885-
print_hex("received", rx_buf, n);
799+
// printf("Received %d bytes\n", n);
800+
// print_hex("received", rx_buf, n);
886801

887802
/* Process the request and generate response */
888803
int response_len = process_request(rx_buf, n, tx_buf, sizeof(tx_buf));
@@ -892,7 +807,7 @@ int main(void)
892807
break;
893808
}
894809

895-
print_hex("sending", tx_buf, response_len);
810+
// print_hex("sending", tx_buf, response_len);
896811

897812
if (send(s2, tx_buf, response_len, 0) < 0) {
898813
perror("send");
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
#include <stdbool.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
#include <zcbor_encode.h>
8+
#include <zcbor_decode.h>
9+
#include <zcbor_common.h>
10+
11+
#define STRING_BUFFER_SIZE 256
12+
13+
/* Helper to encode a string or nil if NULL */
14+
bool encode_string_or_nil(zcbor_state_t *state, const char *str)
15+
{
16+
if (str == NULL) {
17+
return zcbor_nil_put(state, NULL);
18+
}
19+
return zcbor_tstr_put_term(state, str, STRING_BUFFER_SIZE);
20+
}
21+
22+
/* Helper to encode a string array (NULL-terminated) */
23+
bool encode_string_array(zcbor_state_t *state, const char **arr)
24+
{
25+
if (arr == NULL) {
26+
return zcbor_nil_put(state, NULL);
27+
}
28+
29+
/* Count the strings */
30+
size_t count = 0;
31+
while (arr[count] != NULL) {
32+
count++;
33+
}
34+
35+
/* Encode as a CBOR array */
36+
if (!zcbor_list_start_encode(state, count)) {
37+
return false;
38+
}
39+
40+
for (size_t i = 0; i < count; i++) {
41+
if (!zcbor_tstr_put_term(state, arr[i], STRING_BUFFER_SIZE)) {
42+
return false;
43+
}
44+
}
45+
46+
return zcbor_list_end_encode(state, count);
47+
}
48+
49+
bool encode_request_and_container_id(zcbor_state_t *state, uint32_t req, const char *container_id)
50+
{
51+
bool success = zcbor_uint32_put(state, req);
52+
if (!success) {
53+
fprintf(stderr, "Encoding req failed: %d\n", zcbor_peek_error(state));
54+
return false;
55+
}
56+
57+
success = encode_string_or_nil(state, container_id);
58+
if (!success) {
59+
fprintf(stderr, "Encoding container id failed: %d\n", zcbor_peek_error(state));
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
66+
/* Helper to decode a string or nil, returns true if string was decoded, false if nil */
67+
bool decode_string_or_nil(zcbor_state_t *state, char *buffer, size_t buffer_size, bool *is_nil)
68+
{
69+
*is_nil = false;
70+
71+
/* Try to decode nil first */
72+
if (zcbor_nil_expect(state, NULL)) {
73+
*is_nil = true;
74+
buffer[0] = '\0';
75+
return true;
76+
}
77+
78+
zcbor_pop_error(state);
79+
80+
/* Decode as string */
81+
struct zcbor_string str;
82+
if (!zcbor_tstr_decode(state, &str)) {
83+
return false;
84+
}
85+
86+
size_t copy_len = str.len < buffer_size - 1 ? str.len : buffer_size - 1;
87+
memcpy(buffer, str.value, copy_len);
88+
buffer[copy_len] = '\0';
89+
90+
return true;
91+
}
92+
93+
/* Helper to decode a string array (CBOR list of strings) */
94+
bool decode_string_array(zcbor_state_t *state, char **arr, size_t max_count, size_t *out_count, bool *is_nil)
95+
{
96+
*is_nil = false;
97+
*out_count = 0;
98+
99+
/* Try to decode nil first */
100+
if (zcbor_nil_expect(state, NULL)) {
101+
*is_nil = true;
102+
return true;
103+
}
104+
105+
zcbor_pop_error(state);
106+
107+
/* Decode as list */
108+
if (!zcbor_list_start_decode(state)) {
109+
return false;
110+
}
111+
112+
size_t count = 0;
113+
while (!zcbor_list_end_decode(state) && count < max_count) {
114+
struct zcbor_string str;
115+
if (!zcbor_tstr_decode(state, &str)) {
116+
break;
117+
}
118+
119+
/* Allocate and copy string */
120+
arr[count] = malloc(str.len + 1);
121+
if (arr[count] == NULL) {
122+
return false;
123+
}
124+
memcpy(arr[count], str.value, str.len);
125+
arr[count][str.len] = '\0';
126+
count++;
127+
}
128+
129+
arr[count] = NULL; /* NULL-terminate the array */
130+
*out_count = count;
131+
132+
return true;
133+
}
134+
135+
/* Helper to free a string array */
136+
void free_string_array(char **arr, size_t count)
137+
{
138+
for (size_t i = 0; i < count; i++) {
139+
free(arr[i]);
140+
}
141+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
#include <stdbool.h>
4+
#include <stdio.h>
5+
6+
#include <zcbor_encode.h>
7+
#include <zcbor_common.h>
8+
9+
bool encode_string_or_nil(zcbor_state_t *state, const char *str);
10+
bool encode_string_array(zcbor_state_t *state, const char **arr);
11+
bool encode_string_or_nil(zcbor_state_t *state, const char *str);
12+
bool encode_string_array(zcbor_state_t *state, const char **arr);
13+
bool encode_request_and_container_id(zcbor_state_t *state, uint32_t req, const char *container_id);
14+
15+
bool decode_string_or_nil(zcbor_state_t *state, char *buffer, size_t buffer_size, bool *is_nil);
16+
bool decode_string_array(zcbor_state_t *state, char **arr, size_t max_count, size_t *out_count, bool *is_nil);
17+
void free_string_array(char **arr, size_t count);

0 commit comments

Comments
 (0)