Skip to content

Commit 5c5f080

Browse files
Grolleau-BenjaminGrom-
authored andcommitted
[refactor][core - platform] replace printf() calls with stse_platform_printf() for platform-specific logging
Signed-off-by: Benjamin Grolleau <benjamin.grolleau@outlook.com>
1 parent 0cd2992 commit 5c5f080

6 files changed

Lines changed: 49 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- **API change** — all platform-level SecureElement initialization functions now require an additional `void *pArg` parameter ([51aa3f8](https://github.com/STMicroelectronics/STSELib/commit/51aa3f8114d33adfe38da6fd261e4b9a71a1fa9b))
1919
- **Platform AES API change** — all platform AES cryptographic functions now reference keys by secure storage index (`PLAT_UI32 key_idx`) instead of raw key pointer and length (`PLAT_UI8 *pKey, PLAT_UI16 key_length`). Affected functions: `stse_platform_aes_cmac_init`, `stse_platform_aes_cmac_compute`, `stse_platform_aes_cmac_verify`, `stse_platform_aes_cbc_enc`, `stse_platform_aes_cbc_dec`, `stse_platform_aes_ecb_enc`
2020
- **Renamed** `stsafea_open_host_session` to `stsafea_open_host_session_from_idx` — signature updated to accept key indices (`PLAT_UI32 host_MAC_key_idx`, `PLAT_UI32 host_cypher_key_idx`) instead of raw key pointers
21+
- **printf() calls replaced** with `stse_platform_printf()` in all library code to abstract away standard I/O and allow platform-specific implementations for logging and output
2122

2223
### Added
2324

2425
- `stse_platform_store_aes_key()` — new platform function to store an AES key into platform secure storage and retrieve its index
2526
- `stse_platform_delete_aes_key()` — new platform function to delete an AES key from platform secure storage by index
27+
- `stse_platform_printf()` — new platform function for formatted output, replacing all direct calls to `printf()` in the library with this abstraction to allow platform-specific implementations and avoid direct use of standard I/O in library code
2628

2729
### Changed
2830

core/stse_frame.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,25 @@ void stse_frame_debug_print(stse_frame_t *pFrame) {
164164
PLAT_UI16 data_index;
165165

166166
if (pFrame->element_count == 0) {
167-
printf("\n\r (EMPTY)");
167+
stse_platform_printf("\n\r (EMPTY)");
168168
return;
169169
}
170170
pCurrent_element = pFrame->first_element;
171-
printf(" (%d-byte) :", pFrame->length);
171+
stse_platform_printf(" (%d-byte) :", pFrame->length);
172172
do {
173-
printf(" { ");
173+
stse_platform_printf(" { ");
174174
if (pCurrent_element->length == 0) {
175-
printf("S ");
175+
stse_platform_printf("S ");
176176
} else {
177177
for (data_index = 0; data_index < pCurrent_element->length; data_index++) {
178178
if (pCurrent_element->pData != NULL) {
179-
printf("0x%02X ", pCurrent_element->pData[data_index]);
179+
stse_platform_printf("0x%02X ", pCurrent_element->pData[data_index]);
180180
} else {
181-
printf("0x00 ");
181+
stse_platform_printf("0x00 ");
182182
}
183183
}
184184
}
185-
printf("}");
185+
stse_platform_printf("}");
186186
pCurrent_element = pCurrent_element->next;
187187
} while (pCurrent_element != NULL);
188188
}

core/stse_platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ stse_ReturnCode_t stse_platform_crypto_init(void *pArg);
7070
*/
7171
stse_ReturnCode_t stse_platform_generate_random_init(void *pArg);
7272

73+
/*!
74+
* \brief Platform log abstraction
75+
*/
76+
void stse_platform_printf(const char *format, ...);
77+
7378
/*!
7479
* \brief Platform generate random callback function
7580
* \return \ref STSE_OK on success; \ref stse_ReturnCode_t error code otherwise

doc/resources/Markdown/04_PORTING_GUIDE/03_PORTINNG_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Following platform files architecture are recommended to simplify the porting of
4141
- @subpage stse_platform_power
4242
- @subpage stse_platform_i2c
4343
- @subpage stse_platform_st1wire
44+
- @subpage stse_platform_log
4445

4546
# Reference Implementations
4647

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# stse_platform_log.c {#stse_platform_log}
2+
3+
The `stse_platform_log.c` file provides string formatting and logging functions for the STSE library, abstracting the platform-specific details of standard output and debugging streams in order to don't be `stdio`-dependent and allow platform-specific implementations for logging and output.
4+
5+
## stse_platform_printf:
6+
- **Purpose**: Provides a platform abstraction for formatted output, replacing all direct calls to `printf()` in the library with this function to allow platform-specific implementations and avoid direct use of standard I/O in library code.
7+
- **Parameters**: Accepts a format string and a variable number of arguments, similar to the standard `printf()` function.
8+
- **Return Value**: return void
9+
10+
## Implementation example:
11+
12+
```c
13+
#include "stselib.h"
14+
15+
#include <stdio.h>
16+
#include <stdarg.h>
17+
18+
void stse_platform_printf(const char *format, ...) {
19+
va_list args;
20+
21+
va_start(args, format);
22+
vprintf(format, args);
23+
va_end(args);
24+
}
25+
```

services/stsafea/stsafea_frame_transfer.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ stse_ReturnCode_t stsafea_frame_transmit(stse_Handle_t *pSTSE, stse_frame_t *pFr
7272
stse_frame_push_element(pFrame, &crc_element);
7373

7474
#ifdef STSE_FRAME_DEBUG_LOG
75-
printf("\n\r STSAFE Frame > ");
75+
stse_platform_printf("\n\r STSAFE Frame > ");
7676
stse_frame_debug_print(pFrame);
77-
printf("\n\r");
77+
stse_platform_printf("\n\r");
7878
#endif /* STSE_FRAME_DEBUG_LOG */
7979

8080
ret = STSE_PLATFORM_BUS_ACK_ERROR;
@@ -223,11 +223,11 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handle_t *pSTSE, stse_frame_t *pFra
223223
computed_crc = stse_platform_Crc16_Calculate(&received_header, STSE_RSP_FRAME_HEADER_SIZE);
224224

225225
#ifdef STSE_FRAME_DEBUG_LOG
226-
printf("\n\r STSAFE Frame < (%d-byte) : { 0x%02X } { 0x%02X 0x%02X }\n\r",
227-
received_length + STSE_FRAME_CRC_SIZE,
228-
received_header,
229-
received_crc[0],
230-
received_crc[1]);
226+
stse_platform_printf("\n\r STSAFE Frame < (%d-byte) : { 0x%02X } { 0x%02X 0x%02X }\n\r",
227+
received_length + STSE_FRAME_CRC_SIZE,
228+
received_header,
229+
received_crc[0],
230+
received_crc[1]);
231231
#endif /* STSE_FRAME_DEBUG_LOG */
232232

233233
/* - Verify CRC */
@@ -367,9 +367,9 @@ stse_ReturnCode_t stsafea_frame_receive(stse_Handle_t *pSTSE, stse_frame_t *pFra
367367
}
368368

369369
#ifdef STSE_FRAME_DEBUG_LOG
370-
printf("\n\r STSAFE Frame < ");
370+
stse_platform_printf("\n\r STSAFE Frame < ");
371371
stse_frame_debug_print(pFrame);
372-
printf("\n\r");
372+
stse_platform_printf("\n\r");
373373
#endif /* STSE_FRAME_DEBUG_LOG */
374374

375375
/* - Swap CRC */

0 commit comments

Comments
 (0)