Skip to content

Commit ce15337

Browse files
committed
Zephyr: More QoL changes for zephyr integration
Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 52abc71 commit ce15337

5 files changed

Lines changed: 95 additions & 21 deletions

File tree

src/crypto/mbedtls.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,56 @@
99
#include <assert.h>
1010

1111
#include <mbedtls/aes.h>
12+
13+
#ifdef MBEDTLS_PSA_CRYPTO_C
14+
#include <psa/crypto.h>
15+
#else
1216
#include <mbedtls/entropy.h>
1317
#include <mbedtls/ctr_drbg.h>
18+
#endif
1419

1520
#include <osdp.h>
1621

17-
mbedtls_aes_context aes_ctx;
18-
mbedtls_entropy_context entropy_ctx;
19-
mbedtls_ctr_drbg_context ctr_drbg_ctx;
22+
static mbedtls_aes_context aes_ctx;
23+
24+
#ifndef MBEDTLS_PSA_CRYPTO_C
25+
static mbedtls_entropy_context entropy_ctx;
26+
static mbedtls_ctr_drbg_context ctr_drbg_ctx;
27+
#endif
2028

2129
void osdp_crypt_setup()
2230
{
31+
mbedtls_aes_init(&aes_ctx);
32+
33+
#ifdef MBEDTLS_PSA_CRYPTO_C
34+
psa_status_t status = psa_crypto_init();
35+
assert(status == PSA_SUCCESS);
36+
#else
2337
int rc;
24-
const char *version;
38+
const char *version = osdp_get_version();
2539

26-
version = osdp_get_version();
27-
mbedtls_aes_init(&aes_ctx);
2840
mbedtls_entropy_init(&entropy_ctx);
2941
mbedtls_ctr_drbg_init(&ctr_drbg_ctx);
30-
3142
rc = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx,
3243
mbedtls_entropy_func,
3344
&entropy_ctx,
3445
(const unsigned char *)version,
3546
strlen(version));
3647
assert(rc == 0);
48+
#endif
3749
}
3850

3951
void osdp_encrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len)
4052
{
4153
int rc;
4254

4355
if (iv != NULL) {
44-
/* encrypt multiple block with AES in CBC mode */
4556
rc = mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
4657
assert(rc == 0);
4758
rc = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT,
4859
len, iv, data, data);
4960
assert(rc == 0);
5061
} else {
51-
/* encrypt one block with AES in ECB mode */
5262
assert(len <= 16);
5363
rc = mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
5464
assert(rc == 0);
@@ -63,14 +73,12 @@ void osdp_decrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len)
6373
int rc;
6474

6575
if (iv != NULL) {
66-
/* decrypt multiple block with AES in CBC mode */
6776
rc = mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
6877
assert(rc == 0);
6978
rc = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT,
7079
len, iv, data, data);
7180
assert(rc == 0);
7281
} else {
73-
/* decrypt one block with AES in ECB mode */
7482
assert(len <= 16);
7583
rc = mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
7684
assert(rc == 0);
@@ -82,15 +90,20 @@ void osdp_decrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len)
8290

8391
void osdp_fill_random(uint8_t *buf, int len)
8492
{
85-
int rc;
86-
87-
rc = mbedtls_ctr_drbg_random(&ctr_drbg_ctx, buf, len);
93+
#ifdef MBEDTLS_PSA_CRYPTO_C
94+
psa_status_t status = psa_generate_random(buf, len);
95+
assert(status == PSA_SUCCESS);
96+
#else
97+
int rc = mbedtls_ctr_drbg_random(&ctr_drbg_ctx, buf, len);
8898
assert(rc == 0);
99+
#endif
89100
}
90101

91102
void osdp_crypt_teardown()
92103
{
104+
#ifndef MBEDTLS_PSA_CRYPTO_C
93105
mbedtls_ctr_drbg_free(&ctr_drbg_ctx);
94106
mbedtls_entropy_free(&entropy_ctx);
107+
#endif
95108
mbedtls_aes_free(&aes_ctx);
96109
}

utils

zephyr/CMakeLists.txt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ if (CONFIG_LIBOSDP)
2525
target_include_directories(osdp INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/include)
2626

2727
zephyr_library_named(modules_osdp)
28-
zephyr_library_compile_definitions(__BARE_METAL__)
2928

3029
# Pass all OSDP Kconfig values as compile definitions
3130
zephyr_library_compile_definitions(OSDP_PACKET_BUF_SIZE=${CONFIG_OSDP_PACKET_BUF_SIZE})
@@ -45,10 +44,12 @@ if (CONFIG_LIBOSDP)
4544
zephyr_library_compile_definitions(OSDP_CMD_ID_OFFSET=${CONFIG_OSDP_CMD_ID_OFFSET})
4645
zephyr_library_compile_definitions(OSDP_PCAP_LINK_TYPE=${CONFIG_OSDP_PCAP_LINK_TYPE})
4746
zephyr_library_compile_definitions(OSDP_PD_NAME_MAXLEN=${CONFIG_OSDP_PD_NAME_MAXLEN})
47+
zephyr_library_compile_definitions(OSDP_CP_MAX_PDS=${CONFIG_OSDP_PD_MAX})
4848

4949
zephyr_library_link_libraries(osdp)
5050
target_include_directories(modules_osdp PRIVATE ${OSDP_ROOT}/utils/include)
5151

52+
# Feature toggles
5253
if (CONFIG_LIBOSDP_SKIP_MARK_BYTE)
5354
zephyr_library_compile_definitions(OPT_OSDP_SKIP_MARK_BYTE=1)
5455
endif()
@@ -61,6 +62,23 @@ if (CONFIG_LIBOSDP)
6162
zephyr_library_compile_definitions(OPT_DISABLE_PRETTY_LOGGING=1)
6263
endif()
6364

65+
if (CONFIG_LIBOSDP_STATIC)
66+
zephyr_library_compile_definitions(OPT_OSDP_STATIC=1)
67+
endif()
68+
69+
if (CONFIG_LIBOSDP_LOG_MINIMAL)
70+
zephyr_library_compile_definitions(OPT_OSDP_LOG_MINIMAL=1)
71+
endif()
72+
73+
if (CONFIG_LIBOSDP_PACKET_TRACE)
74+
zephyr_library_compile_definitions(OPT_OSDP_PACKET_TRACE=1)
75+
endif()
76+
77+
if (CONFIG_LIBOSDP_DATA_TRACE)
78+
zephyr_library_compile_definitions(OPT_OSDP_DATA_TRACE=1)
79+
endif()
80+
81+
# Core sources
6482
zephyr_library_sources(
6583
## LibOSDP
6684
${OSDP_ROOT}/src/osdp_cp.c
@@ -72,17 +90,29 @@ if (CONFIG_LIBOSDP)
7290
${OSDP_ROOT}/src/osdp_metrics.c
7391

7492
## Utils
75-
## TODO: Migrate these to use (or wrap around) zephyr API
7693
${OSDP_ROOT}/utils/src/list.c
7794
${OSDP_ROOT}/utils/src/queue.c
7895
${OSDP_ROOT}/utils/src/utils.c
79-
${OSDP_ROOT}/utils/src/logger.c
8096
${OSDP_ROOT}/utils/src/disjoint_set.c
8197

8298
## Zephyr glue code
8399
src/osdp.c
84100
)
85101

102+
# Logger: full or minimal
103+
if (NOT CONFIG_LIBOSDP_LOG_MINIMAL)
104+
zephyr_library_sources(${OSDP_ROOT}/utils/src/logger.c)
105+
endif()
106+
107+
# Diagnostics (packet/data trace → PCAP capture)
108+
if (CONFIG_LIBOSDP_PACKET_TRACE OR CONFIG_LIBOSDP_DATA_TRACE)
109+
zephyr_library_sources(
110+
${OSDP_ROOT}/src/osdp_diag.c
111+
${OSDP_ROOT}/utils/src/pcap_gen.c
112+
)
113+
endif()
114+
115+
# Crypto backend
86116
if (CONFIG_LIBOSDP_CRYPTO_MBEDTLS)
87117
zephyr_library_compile_definitions(OPT_OSDP_USE_MBEDTLS)
88118
zephyr_library_sources(${OSDP_ROOT}/src/crypto/mbedtls.c)

zephyr/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ config LIBOSDP_CRYPTO_TINYAES
2626

2727
endchoice
2828

29+
config LIBOSDP_STATIC
30+
bool "Static allocation (no malloc/free)"
31+
default y
32+
help
33+
Allocate all LibOSDP internal structures statically.
34+
PD arrays are sized to OSDP_PD_MAX. This eliminates
35+
all dynamic memory allocation inside LibOSDP.
36+
2937
config LIBOSDP_RX_ZERO_COPY
3038
bool "Enable zero-copy RX buffers"
3139
default y
@@ -43,10 +51,32 @@ config LIBOSDP_SKIP_MARK_BYTE
4351
bool "Don't send the leading mark byte (0xFF)"
4452
default n
4553

54+
config LIBOSDP_LOG_MINIMAL
55+
bool "Minimal logging (reduced RAM/stack)"
56+
default n
57+
help
58+
Use a lightweight logger that omits timestamps and
59+
file/line information. Saves RAM and stack space
60+
on constrained targets.
61+
4662
config LIBOSDP_DISABLE_PRETTY_LOGGING
4763
bool "Don't colorize log outputs"
4864
default n
4965

66+
config LIBOSDP_PACKET_TRACE
67+
bool "Enable OSDP packet trace (PCAP)"
68+
default n
69+
help
70+
Record raw OSDP packets for diagnostics. Enables
71+
PCAP capture support via osdp_diag.
72+
73+
config LIBOSDP_DATA_TRACE
74+
bool "Enable OSDP data buffer trace"
75+
default n
76+
help
77+
Trace command/reply data buffers for diagnostics.
78+
Enables PCAP capture support via osdp_diag.
79+
5080
config OSDP_UART_BAUD_RATE
5181
int "OSDP UART baud rate"
5282
default 115200

zephyr/src/osdp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <stdint.h>
87
#include <zephyr/kernel.h>
98

10-
int64_t osdp_millis_now(void)
9+
#include <utils/utils.h>
10+
11+
tick_t osdp_millis_now(void)
1112
{
12-
return (int64_t) k_uptime_get();
13+
return (tick_t)k_uptime_get();
1314
}

0 commit comments

Comments
 (0)