Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit a27a766

Browse files
Arpad TigyiDellaBitta
Arpad Tigyi
authored andcommitted
Zephyr port (#15)
Zephyr RTOS support of the Embedded C Client: * Zephyr BSP implemented * Zephyr sample application added to examples The Zephyr git repo automatically cloned. Few builds steps executed in the Zephyr example since the Zephyr BSP requires some auto-generated header files. Then the Embedded C Client lib build with the BSP. Then example can be built. QA: no Travic CI build support.
1 parent 5a2e20f commit a27a766

20 files changed

+1123
-15
lines changed

.gitignore

-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,3 @@ obj/
33
res/trusted_RootCA_certs/roots.pem
44
test_coverage/
55
.vscode/
6-
third_party/tls/mbedtls
7-
third_party/tls/wolfssl
8-
third_party/protobuf-c/library/*.o
9-
third_party/freertos-addons
10-
third_party/FreeRTOSv10.1.1*
11-
examples/freertos_linux/Linux_gcc_gcp_iot/*.o
12-
examples/freertos_linux/Linux_gcc_gcp_iot/Linux_gcc_gcp_iot

.travis.yml

+16-3
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ env:
2727
- PRESET=FREERTOS_POSIX_REL
2828
BEFORE_SCRIPT_ACTION="make PRESET=FREERTOS_POSIX_REL prereqs"
2929

30-
# WOLFSSL Tests
30+
# WOLFSSL Tests
3131
- PRESET=POSIX_DEV
3232
IOTC_BSP_TLS=wolfssl
3333

3434
- PRESET=POSIX_REL
3535
IOTC_BSP_TLS=wolfssl
3636

3737
- PRESET=POSIX_THREADING_REL
38-
# - PRESET=POSIX_UNSECURE_REL
39-
# IOTC_BSP_TLS=""
38+
# - PRESET=POSIX_UNSECURE_REL
39+
# IOTC_BSP_TLS=""
4040

4141
- TESTS=fuzztests
4242
PRESET=FUZZ_TESTS
4343
IOTC_BSP_TLS=""
4444
IOTC_MAKETARGET="fuzz_tests"
4545
CXX=clang++
4646

47+
# - PRESET=ZEPHYR
48+
# IOTC_MAKETARGET=""
49+
4750
cache:
4851
apt
4952
addons:
@@ -55,7 +58,15 @@ addons:
5558
- clang-3.8
5659
- qemu-system
5760
- libc6:i386
61+
- libc6-dev-i386
62+
- gcc-multilib
63+
- linux-libc-dev:i386
5864
- expect
65+
- device-tree-compiler
66+
- gperf
67+
- dfu-util
68+
- xz-utils
69+
- python3-pip
5970
matrix:
6071
exclude:
6172
- compiler: clang
@@ -90,6 +101,8 @@ script:
90101
- "if [[ ! $CC == 'gcc' && ! $TESTS == 'fuzztests' ]]; then
91102
if [[ $PRESET == 'FREERTOS_POSIX_REL' || $PRESET == 'FREERTOS_POSIX_DEV' ]]; then
92103
make -C examples/freertos_linux/Linux_gcc_gcp_iot IOTC_CLIENT_LIB_PATH=../../../bin/freertos-linux IOTC_BSP_TLS=$IOTC_BSP_TLS;
104+
elif [[ $PRESET == 'ZEPHYR' ]]; then
105+
make -C examples/zephyr_native_posix/build VERBOSE=1;
93106
else
94107
make -C examples/mqtt_logic_producer IOTC_CLIENT_LIB_PATH=../../bin/linux IOTC_BSP_TLS=$IOTC_BSP_TLS;
95108
fi;

examples/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
freertos_linux/Linux_gcc_gcp_iot/*.o
2+
freertos_linux/Linux_gcc_gcp_iot/Linux_gcc_gcp_iot
3+
4+
zephyr_native_posix/build/

examples/common/src/example_utils.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem, size_t b
9191
printf(
9292
"private key file size of %lu bytes is larger that certificate buffer "
9393
"size of %lu bytes\n",
94-
file_size, buf_len);
94+
file_size, (long)buf_len);
9595
fclose(fp);
9696
return -1;
9797
}
@@ -111,11 +111,15 @@ void on_connection_state_changed(iotc_context_handle_t in_context_handle,
111111
void* data, iotc_state_t state) {
112112
iotc_connection_data_t* conn_data = (iotc_connection_data_t*)data;
113113

114+
if (NULL == conn_data) {
115+
return;
116+
}
117+
114118
switch (conn_data->connection_state) {
115119
/* IOTC_CONNECTION_STATE_OPENED means that the connection has been
116120
established and the IoTC Client is ready to send/recv messages */
117121
case IOTC_CONNECTION_STATE_OPENED:
118-
printf("connected!\n");
122+
printf("connected to %s:%d\n", conn_data->host, conn_data->port);
119123

120124
/* Publish immediately upon connect. 'publish_function' is defined
121125
in this example file and invokes the IoTC API to publish a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# This is part of the Google Cloud IoT Edge Embedded C Client,
4+
# it is licensed under the BSD 3-Clause license; you may not use this file
5+
# except in compliance with the License.
6+
#
7+
# You may obtain a copy of the License at:
8+
# https://opensource.org/licenses/BSD-3-Clause
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
cmake_minimum_required(VERSION 3.8.2)
17+
18+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
19+
project(zephyr_native_posix)
20+
21+
set(IOTC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../..)
22+
23+
target_include_directories(app PRIVATE ${IOTC_PATH}/examples/common/src)
24+
target_include_directories(app PRIVATE ${IOTC_PATH}/include)
25+
26+
target_sources(app PRIVATE
27+
src/main.c
28+
../common/src/commandline.c
29+
../common/src/example_utils.c)
30+
31+
# libiotc
32+
target_link_libraries(app PUBLIC ${IOTC_PATH}/bin/zephyr/libiotc.a)
33+
34+
# mbedTLS
35+
set(IOTC_MBEDTLS_LIB_PATH ${IOTC_PATH}/third_party/tls/mbedtls/library)
36+
target_link_libraries(app PUBLIC ${IOTC_MBEDTLS_LIB_PATH}/libmbedtls.a)
37+
target_link_libraries(app PUBLIC ${IOTC_MBEDTLS_LIB_PATH}/libmbedx509.a)
38+
target_link_libraries(app PUBLIC ${IOTC_MBEDTLS_LIB_PATH}/libmbedcrypto.a)
39+
40+
# wolfssl, disabled since wolfssl fails to build with -m32 flag (--enable-32bit)
41+
# target_link_libraries(app PUBLIC ${IOTC_PATH}/third_party/tls/wolfssl/src/.libs/libwolfssl.a)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# This is part of the Google Cloud IoT Edge Embedded C Client,
4+
# it is licensed under the BSD 3-Clause license; you may not use this file
5+
# except in compliance with the License.
6+
#
7+
# You may obtain a copy of the License at:
8+
# https://opensource.org/licenses/BSD-3-Clause
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
pip3 install --user pyelftools
17+
18+
mkdir -p build; cd build;
19+
20+
cmake -DBOARD=native_posix ..
21+
22+
make zephyr/include/generated/autoconf.h
23+
make syscall_list_h_target
24+
make syscall_macros_h_target
25+
make kobj_types_h_target

examples/zephyr_native_posix/prj.conf

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# This is part of the Google Cloud IoT Edge Embedded C Client,
4+
# it is licensed under the BSD 3-Clause license; you may not use this file
5+
# except in compliance with the License.
6+
#
7+
# You may obtain a copy of the License at:
8+
# https://opensource.org/licenses/BSD-3-Clause
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Networking config
17+
CONFIG_NETWORKING=y
18+
CONFIG_NET_IPV4=y
19+
CONFIG_NET_IPV6=n
20+
CONFIG_NET_ARP=y
21+
CONFIG_NET_UDP=y
22+
CONFIG_NET_TCP=y
23+
CONFIG_NET_SOCKETS=y
24+
CONFIG_NET_SOCKETS_POSIX_NAMES=y
25+
26+
# Network address config
27+
CONFIG_NET_CONFIG_SETTINGS=y
28+
CONFIG_NET_CONFIG_NEED_IPV6=n
29+
CONFIG_NET_CONFIG_NEED_IPV4=y
30+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
31+
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
32+
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
33+
34+
# CONFIG_ETH_NATIVE_POSIX_STARTUP_AUTOMATIC=y
35+
36+
# Enable the DNS resolver
37+
CONFIG_DNS_RESOLVER=y
38+
CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=5
39+
CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=2
40+
CONFIG_DNS_RESOLVER_MAX_SERVERS=1
41+
CONFIG_DNS_SERVER_IP_ADDRESSES=y
42+
CONFIG_DNS_NUM_CONCUR_QUERIES=5
43+
44+
# Google DNS IPv4 and IPv6 servers
45+
CONFIG_DNS_SERVER1="8.8.8.8"
46+
47+
# Kernel options
48+
CONFIG_HEAP_MEM_POOL_SIZE=16384
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* This is part of the Google Cloud IoT Edge Embedded C Client,
4+
* it is licensed under the BSD 3-Clause license; you may not use this file
5+
* except in compliance with the License.
6+
*
7+
* You may obtain a copy of the License at:
8+
* https://opensource.org/licenses/BSD-3-Clause
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <cmdline.h>
18+
#include <string.h>
19+
#include <zephyr.h>
20+
21+
#include <commandline.h>
22+
#include <example_utils.h>
23+
#include <iotc.h>
24+
25+
char ec_private_key_pem[PRIVATE_KEY_BUFFER_SIZE] = {0};
26+
27+
void main(void) {
28+
printk("Example for Zephyr port.\n");
29+
30+
/* commandline sample:
31+
zephyr.exe -testargs -p <GCP IoT Core Project ID> -d projects/<GCP IoT Core
32+
Project ID>/locations/<Region>/registries/<GCP IoT Core Registry
33+
ID>/devices/<GCP IoT Core Device ID> -t /devices/<GCP IoT Core
34+
DeviceID>/state
35+
*/
36+
37+
int argc = 0;
38+
char** argv = NULL;
39+
40+
native_get_cmd_line_args(&argc, &argv);
41+
42+
/* Zephyr passes the "-testargs" internal command line argument too. This code
43+
* skips it to be compatible to the native command line argument handlin. */
44+
if (1 < argc && 0 == strcmp(argv[1], "-testargs")) {
45+
--argc;
46+
++argv;
47+
}
48+
49+
/* parsing GCP IoT related command line arguments */
50+
if (0 != iotc_example_handle_command_line_args(argc, argv)) {
51+
return;
52+
}
53+
54+
if (0 != load_ec_private_key_pem_from_posix_fs(ec_private_key_pem,
55+
PRIVATE_KEY_BUFFER_SIZE)) {
56+
printk("\nApplication exiting due to private key load error.\n\n");
57+
return;
58+
}
59+
60+
printk("Starting GCP IoT Embedded C Client...\n");
61+
62+
iotc_initialize();
63+
64+
iotc_context_handle_t context_handle = iotc_create_context();
65+
66+
const uint16_t connection_timeout = 10;
67+
const uint16_t keepalive_timeout = 10;
68+
69+
iotc_crypto_private_key_data_t key_data;
70+
key_data.private_key_signature_algorithm =
71+
IOTC_JWT_PRIVATE_KEY_SIGNATURE_ALGORITHM_ES256;
72+
key_data.private_key_union_type = IOTC_CRYPTO_KEY_UNION_TYPE_PEM;
73+
key_data.private_key_union.key_pem.key = ec_private_key_pem;
74+
75+
iotc_connect(context_handle, iotc_project_id, iotc_device_path, &key_data,
76+
/*{jwt_expiration_period_sec=*/3600, connection_timeout,
77+
keepalive_timeout, on_connection_state_changed);
78+
79+
iotc_events_process_blocking();
80+
81+
/* Cleanup the default context, releasing its memory */
82+
iotc_delete_context(context_handle);
83+
84+
/* Cleanup internal allocations that were created by iotc_initialize. */
85+
iotc_shutdown();
86+
}

make/mt-config/mt-presets.mk

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# This is part of the Google Cloud IoT Edge Embedded C Client,
44
# it is licensed under the BSD 3-Clause license; you may not use this file
@@ -21,6 +21,9 @@ CONFIG_POSIX_MAX_THREADING =posix_fs-posix_platform-tls_bsp-threading-memory_lim
2121
CONFIG_POSIX_MIN =posix_fs-posix_platform-tls_bsp
2222
CONFIG_POSIX_MIN_UNSECURE =posix_fs-posix_platform
2323

24+
# CONFIG for ZEPHYR presets
25+
CONFIG_ZEPHYR_MAX =memory_fs-posix_platform-tls_bsp-memory_limiter
26+
2427
# CONFIG for ARM
2528
CONFIG_DUMMY_MAX =memory_fs-memory_limiter
2629
CONFIG_DUMMY_MIN =memory_fs
@@ -80,6 +83,14 @@ else ifeq ($(PRESET), FREERTOS_POSIX_DEV)
8083
IOTC_BSP_PLATFORM = freertos-posix
8184
IOTC_TARGET_PLATFORM = freertos-linux
8285

86+
# -------------------------------------------------------
87+
# ZEPHYR
88+
else ifeq ($(PRESET), ZEPHYR)
89+
CONFIG = $(CONFIG_ZEPHYR_MAX)
90+
TARGET = $(TARGET_STATIC_DEV)
91+
IOTC_BSP_PLATFORM = zephyr
92+
IOTC_TARGET_PLATFORM = zephyr
93+
8394
# -------------------------------------------------------
8495
# ARM
8596
else ifeq ($(PRESET), ARM_REL_MIN)

make/mt-config/mt-tls.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ifneq (,$(findstring Windows,$(IOTC_HOST_PLATFORM)))
3434
else ifeq ($(IOTC_USE_EXTERNAL_TLS_LIB),1)
3535
TLS_LIB_PREPARE_CMD :=
3636
else
37-
TLS_LIB_PREPARE_CMD := (cd $(LIBIOTC)/res/tls && ./build_$(IOTC_BSP_TLS).sh)
37+
TLS_LIB_PREPARE_CMD = (cd $(LIBIOTC)/res/tls && ./build_$(IOTC_BSP_TLS).sh $(IOTC_BSP_TLS_BUILD_ARGS))
3838
endif
3939

4040
$(IOTC_TLS_LIB_DEP):

make/mt-os/mt-os.mk

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ IOTC_CONST_PLATFORM_LINUX := linux
1717
IOTC_CONST_PLATFORM_OSX := osx
1818
IOTC_CONST_PLATFORM_ARM := arm-linux
1919
IOTC_CONST_PLATFORM_FREERTOS_LINUX := freertos-linux
20+
IOTC_CONST_PLATFORM_ZEPHYR := zephyr
2021

2122
ifneq (,$(findstring $(IOTC_CONST_PLATFORM_ARM),$(TARGET)))
2223
IOTC_CONST_PLATFORM_CURRENT := $(IOTC_CONST_PLATFORM_ARM)
@@ -38,5 +39,9 @@ ifneq (,$(findstring $(IOTC_CONST_PLATFORM_FREERTOS_LINUX),$(TARGET)))
3839
IOTC_PLATFORM_IS_FREERTOS=1
3940
endif
4041

42+
ifneq (,$(findstring $(IOTC_CONST_PLATFORM_ZEPHYR),$(TARGET)))
43+
IOTC_CONST_PLATFORM_CURRENT := $(IOTC_CONST_PLATFORM_ZEPHYR)
44+
endif
45+
4146
IOTC_BINDIR := $(IOTC_BINDIR_BASE)/$(IOTC_CONST_PLATFORM_CURRENT)
4247
IOTC_OBJDIR := $(IOTC_OBJDIR_BASE)/$(IOTC_CONST_PLATFORM_CURRENT)

0 commit comments

Comments
 (0)