Skip to content

Commit 5f40f61

Browse files
committed
platform: add support of zephyr
1 parent 1e82430 commit 5f40f61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1777
-33
lines changed

.clang-format-ignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,27 @@ tests/mocks/esp-idf/src/esp_http_client.c
1313
tests/mocks/esp-idf/src/esp_ota_ops.c
1414
tests/mocks/esp-idf/src/nvs.c
1515
tests/mocks/freertos/include/FreeRTOSConfig.h
16+
tests/mocks/zephyr/include/zephyr/device.h
17+
tests/mocks/zephyr/include/zephyr/dfu/flash_img.h
18+
tests/mocks/zephyr/include/zephyr/dfu/mcuboot.h
19+
tests/mocks/zephyr/include/zephyr/drivers/flash.h
20+
tests/mocks/zephyr/include/zephyr/fs/nvs.h
21+
tests/mocks/zephyr/include/zephyr/kernel.h
22+
tests/mocks/zephyr/include/zephyr/logging/log.h
23+
tests/mocks/zephyr/include/zephyr/net/http/client.h
24+
tests/mocks/zephyr/include/zephyr/net/net_ip.h
25+
tests/mocks/zephyr/include/zephyr/net/socket.h
26+
tests/mocks/zephyr/include/zephyr/net/tls_credentials.h
27+
tests/mocks/zephyr/include/zephyr/storage/flash_map.h
28+
tests/mocks/zephyr/include/zephyr/sys/reboot.h
29+
tests/mocks/zephyr/include/zephyr/sys/util_macro.h
30+
tests/mocks/zephyr/include/zephyr/sys_clock.h
31+
tests/mocks/zephyr/src/device.c
32+
tests/mocks/zephyr/src/flash.c
33+
tests/mocks/zephyr/src/flash_img.c
34+
tests/mocks/zephyr/src/http_client.c
35+
tests/mocks/zephyr/src/kernel.c
36+
tests/mocks/zephyr/src/mcuboot.c
37+
tests/mocks/zephyr/src/nvs.c
38+
tests/mocks/zephyr/src/reboot.c
39+
tests/mocks/zephyr/src/socket.c

.github/workflows/build_tests.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
# @file build_tests.sh
3+
# @brief Build all tests
4+
#
5+
# MIT License
6+
#
7+
# Copyright (c) 2022-2023 joelguittet and mender-mcu-client contributors
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
27+
cd tests
28+
mkdir build
29+
cd build
30+
31+
# Build ESP-IDF use case
32+
cmake .. -G "Unix Makefiles" -DCONFIG_MENDER_MCU_CLIENT_BOARD_TYPE="esp-idf" -DCONFIG_MENDER_MCU_CLIENT_HTTP_TYPE="esp-idf" -DCONFIG_MENDER_MCU_CLIENT_RTOS_TYPE="freertos" -DCONFIG_MENDER_MCU_CLIENT_TLS_TYPE="mbedtls"
33+
make -j$(nproc)
34+
35+
# Build Zephyr use case
36+
cmake .. -G "Unix Makefiles" -DCONFIG_MENDER_MCU_CLIENT_BOARD_TYPE="zephyr" -DCONFIG_MENDER_MCU_CLIENT_HTTP_TYPE="zephyr" -DCONFIG_MENDER_MCU_CLIENT_RTOS_TYPE="zephyr" -DCONFIG_MENDER_MCU_CLIENT_TLS_TYPE="mbedtls"
37+
make -j$(nproc)

.github/workflows/ci.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,10 @@ jobs:
6464
uses: SonarSource/sonarcloud-github-c-cpp@v1
6565
- name: Run build-wrapper
6666
run: |
67-
cd tests
68-
mkdir build
69-
cd build
70-
cmake .. -G "Unix Makefiles"
71-
build-wrapper-linux-x86-64 --out-dir bw_output make -j$(nproc)
67+
build-wrapper-linux-x86-64 --out-dir bw_output ./.github/workflows/build_tests.sh
7268
- name: Run sonar-scanner
7369
env:
7470
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7571
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
7672
run: |
77-
sonar-scanner -Dsonar.organization=joelguittet -Dsonar.projectKey=joelguittet_mender-mcu-client -Dsonar.cfamily.cache.enabled=false -Dsonar.inclusions=include/**/*,core/**/*,platform/**/* -Dsonar.projectVersion=$(cat VERSION) -Dsonar.cfamily.build-wrapper-output="tests/build/bw_output"
73+
sonar-scanner -Dsonar.organization=joelguittet -Dsonar.projectKey=joelguittet_mender-mcu-client -Dsonar.cfamily.cache.enabled=false -Dsonar.inclusions=include/**/*,core/**/*,platform/**/* -Dsonar.projectVersion=$(cat VERSION) -Dsonar.cfamily.build-wrapper-output=bw_output

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ To start using Mender, we recommend that you begin with the Getting started sect
3131

3232
To start using mender-mcu-client, we recommend that you begin with one of the example provided with the library:
3333

34-
* [mender-esp32-example](https://github.com/joelguittet/mender-esp32-example) demonstrates the usage of mender-mcu-client on ESP32 MCU to perform upgrade with dual OTA partitions and rollback capability.
34+
* [mender-esp32-example](https://github.com/joelguittet/mender-esp32-example) demonstrates the usage of mender-mcu-client on ESP32 MCU to perform upgrade with dual OTA partitions and rollback capability using ESP-IDF framework.
35+
* [mender-stm32l4a6-zephyr-example](https://github.com/joelguittet/mender-stm32l4a6-zephyr-example) demonstrates the usage of mender-mcu-client on STM32L4A6 MCU to perform upgrade with dual OTA partitions and rollback capability using Zephyr RTOS.
3536
* More examples will come, and particularly new platforms support will be added soon.
3637

3738

core/src/mender-untar.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* @brief tar block size
3232
*/
33-
#define UNTAR_STREAM_BLOCK_SIZE 512
33+
#define MENDER_UNTAR_STREAM_BLOCK_SIZE (512)
3434

3535
/**
3636
* @brief tar file header
@@ -109,12 +109,12 @@ mender_untar_parse(mender_untar_ctx_t *ctx, void *input_data, size_t input_lengt
109109
return -1;
110110
}
111111
ctx->data = tmp;
112-
memcpy(ctx->data + ctx->data_length, input_data, input_length);
112+
memcpy((void *)(((uint8_t *)ctx->data) + ctx->data_length), input_data, input_length);
113113
ctx->data_length += input_length;
114114
}
115115

116116
/* Check if enought data are received */
117-
if (ctx->data_length < UNTAR_STREAM_BLOCK_SIZE) {
117+
if (ctx->data_length < MENDER_UNTAR_STREAM_BLOCK_SIZE) {
118118
return 0;
119119
}
120120

@@ -206,8 +206,8 @@ mender_untar_parse_file(mender_untar_ctx_t *ctx, void **output_data, size_t *out
206206

207207
/* Compute length to be copied */
208208
if (0
209-
!= (*output_length = ((ctx->current_file.size - ctx->current_file.index) > UNTAR_STREAM_BLOCK_SIZE)
210-
? UNTAR_STREAM_BLOCK_SIZE
209+
!= (*output_length = ((ctx->current_file.size - ctx->current_file.index) > MENDER_UNTAR_STREAM_BLOCK_SIZE)
210+
? MENDER_UNTAR_STREAM_BLOCK_SIZE
211211
: (ctx->current_file.size - ctx->current_file.index))) {
212212

213213
/* Create new file data block */
@@ -238,19 +238,19 @@ mender_untar_shift_data(mender_untar_ctx_t *ctx) {
238238
char *tmp;
239239

240240
/* Shift remaining data */
241-
if (ctx->data_length > UNTAR_STREAM_BLOCK_SIZE) {
242-
memcpy(ctx->data, ctx->data + UNTAR_STREAM_BLOCK_SIZE, ctx->data_length - UNTAR_STREAM_BLOCK_SIZE);
243-
if (NULL == (tmp = realloc(ctx->data, ctx->data_length - UNTAR_STREAM_BLOCK_SIZE))) {
241+
if (ctx->data_length > MENDER_UNTAR_STREAM_BLOCK_SIZE) {
242+
memcpy(ctx->data, (void *)(((uint8_t *)ctx->data) + MENDER_UNTAR_STREAM_BLOCK_SIZE), ctx->data_length - MENDER_UNTAR_STREAM_BLOCK_SIZE);
243+
if (NULL == (tmp = realloc(ctx->data, ctx->data_length - MENDER_UNTAR_STREAM_BLOCK_SIZE))) {
244244
/* Unable to allocate memory */
245245
return -1;
246246
}
247247
ctx->data = tmp;
248-
ctx->data_length -= UNTAR_STREAM_BLOCK_SIZE;
248+
ctx->data_length -= MENDER_UNTAR_STREAM_BLOCK_SIZE;
249249
} else {
250250
free(ctx->data);
251251
ctx->data = NULL;
252252
ctx->data_length = 0;
253253
}
254254

255-
return (ctx->data_length >= UNTAR_STREAM_BLOCK_SIZE) ? 1 : 0;
255+
return (ctx->data_length >= MENDER_UNTAR_STREAM_BLOCK_SIZE) ? 1 : 0;
256256
}

include/mender-rtos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @param priority priority of the task
4040
* @param handle Task handle if the function succeeds, NULL otherwise
4141
*/
42-
void mender_rtos_task_create(void (*task_function)(void *), char *name, size_t stack_size, void *arg, uint32_t priority, void **handle);
42+
void mender_rtos_task_create(void (*task_function)(void *), char *name, size_t stack_size, void *arg, int priority, void **handle);
4343

4444
/**
4545
* @brief Fucntion used to delete a task

include/mender-storage.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ mender_err_t mender_storage_erase_authentication_keys(void);
4545
/**
4646
* @brief Get authentication keys
4747
* @param private_key Private key from storage, NULL if not found
48-
* @param private_key_lentgh Private key length from storage, 0 if not found
48+
* @param private_key_length Private key length from storage, 0 if not found
4949
* @param public_key Public key from storage, NULL if not found
50-
* @param public_key_lentgh Public key length from storage, 0 if not found
50+
* @param public_key_length Public key length from storage, 0 if not found
5151
* @return MENDER_OK if the function succeeds, error code otherwise
5252
*/
5353
mender_err_t mender_storage_get_authentication_keys(unsigned char **private_key,
@@ -58,19 +58,19 @@ mender_err_t mender_storage_get_authentication_keys(unsigned char **private_key,
5858
/**
5959
* @brief Set authentication keys
6060
* @param private_key Private key to store
61-
* @param private_key_lentgh Private key length
61+
* @param private_key_length Private key length
6262
* @param public_key Public key to store
63-
* @param public_key_lentgh Public key length
63+
* @param public_key_length Public key length
6464
* @return MENDER_OK if the function succeeds, error code otherwise
6565
*/
6666
mender_err_t mender_storage_set_authentication_keys(unsigned char *private_key, size_t private_key_length, unsigned char *public_key, size_t public_key_length);
6767

6868
/**
6969
* @brief Get OTA deployment
7070
* @param ota_id OTA ID from storage, NULL if not found
71-
* @param ota_id_lentgh OTA ID length from storage, 0 if not found
71+
* @param ota_id_length OTA ID length from storage, 0 if not found
7272
* @param ota_artifact_name Artifact name from storage, NULL if not found
73-
* @param ota_artifact_name_lentgh Artifact name length from storage, 0 if not found
73+
* @param ota_artifact_name_length Artifact name length from storage, 0 if not found
7474
* @return MENDER_OK if the function succeeds, error code otherwise
7575
*/
7676
mender_err_t mender_storage_get_ota_deployment(char **ota_id, size_t *ota_id_length, char **ota_artifact_name, size_t *ota_artifact_name_length);

platform/board/esp-idf/src/mender-storage.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,18 @@ mender_storage_get_ota_deployment(char **ota_id, size_t *ota_id_length, char **o
157157
if (NULL == (*ota_artifact_name = malloc(*ota_artifact_name_length + 1))) {
158158
mender_log_error("Unable to allocate memory");
159159
free(*ota_id);
160+
*ota_id = NULL;
160161
return MENDER_FAIL;
161162
}
162163

163164
/* Read ID and artifact name */
164165
if ((ESP_OK != nvs_get_str(mender_storage_nvs_handle, MENDER_STORAGE_NVS_OTA_ID, *ota_id, ota_id_length))
165166
|| (ESP_OK != nvs_get_str(mender_storage_nvs_handle, MENDER_STORAGE_NVS_OTA_ARTIFACT_NAME, *ota_artifact_name, ota_artifact_name_length))) {
166167
mender_log_error("Unable to read OTA ID or artifact name");
168+
free(*ota_id);
169+
*ota_id = NULL;
170+
free(*ota_artifact_name);
171+
*ota_artifact_name = NULL;
167172
return MENDER_FAIL;
168173
}
169174

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @file mender-log.c
3+
* @brief Mender logging interface for Zephyr platform
4+
*
5+
* MIT License
6+
*
7+
* Copyright (c) 2022-2023 joelguittet and mender-mcu-client contributors
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE.
26+
*/
27+
28+
#include <zephyr/logging/log.h>
29+
LOG_MODULE_REGISTER(mender, CONFIG_MENDER_LOG_LEVEL);
30+
31+
#include "mender-log.h"
32+
33+
mender_err_t
34+
mender_log_init(void) {
35+
36+
/* Nothing to do */
37+
return MENDER_OK;
38+
}
39+
40+
mender_err_t
41+
mender_log_print(mender_log_level_t level, const char *filename, const char *function, int line, char *format, ...) {
42+
43+
(void)function;
44+
char log[256] = { 0 };
45+
46+
/* Format message */
47+
va_list args;
48+
va_start(args, format);
49+
vsnprintf(log, sizeof(log), format, args);
50+
va_end(args);
51+
52+
/* Switch depending log level */
53+
switch (level) {
54+
case MENDER_LOG_ERROR:
55+
LOG_ERR("%s (%d): %s", filename, line, log);
56+
break;
57+
case MENDER_LOG_WARNING:
58+
LOG_WRN("%s (%d): %s", filename, line, log);
59+
break;
60+
case MENDER_LOG_INFO:
61+
LOG_INF("%s (%d): %s", filename, line, log);
62+
break;
63+
case MENDER_LOG_DEBUG:
64+
LOG_DBG("%s (%d): %s", filename, line, log);
65+
break;
66+
}
67+
68+
return MENDER_OK;
69+
}
70+
71+
mender_err_t
72+
mender_log_exit(void) {
73+
74+
/* Nothing to do */
75+
return MENDER_OK;
76+
}

0 commit comments

Comments
 (0)