Skip to content

Commit

Permalink
Add first CAN unit tests and build them in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaeger committed Oct 18, 2023
1 parent 793d628 commit 74b15b6
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
sudo apt install -y git make python3 python3-pip doxygen
pip3 install -r docs/requirements.txt
- name: Run build tests
- name: Run sample build tests
working-directory: thingset-zephyr-sdk
run: |
west build -p -b olimex_lora_stm32wl_devkit samples/counter -- -DOVERLAY_CONFIG=lorawan.conf
Expand All @@ -51,6 +51,11 @@ jobs:
west build -p -b native_posix samples/counter -- -DOVERLAY_CONFIG=native_websocket.conf
west build -p -b xiao_esp32c3 samples/serial_ble_gateway
- name: Run unit tests
working-directory: thingset-zephyr-sdk
run: |
../zephyr/scripts/twister -T ./tests --integration --inline-logs
- name: Build documentation
working-directory: thingset-zephyr-sdk
run: |
Expand Down
11 changes: 11 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ThingSet SDK unit tests

## Run unit tests

With twister:

../zephyr/scripts/twister -T ./tests --integration -v -n

Manually (`tests/can` used as an example):

west build -b native_posix tests/can -t run
9 changes: 9 additions & 0 deletions tests/can/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(thingset_sdk_can_test)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
17 changes: 17 additions & 0 deletions tests/can/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) The ThingSet Project Contributors
# SPDX-License-Identifier: Apache-2.0

CONFIG_CAN=y
CONFIG_ISOTP=y
CONFIG_ENTROPY_GENERATOR=y

CONFIG_THINGSET=y
CONFIG_THINGSET_SDK=y
CONFIG_THINGSET_CAN=y

CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ZTEST_SUMMARY=n

# enable click-able absolute paths in assert messages
#CONFIG_BUILD_OUTPUT_STRIP_PATHS=n
100 changes: 100 additions & 0 deletions tests/can/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) The ThingSet Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>

#include <thingset.h>
#include <thingset/can.h>

#define TEST_RECEIVE_TIMEOUT K_MSEC(100)

static struct thingset_context ts;

static const struct device *can_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_canbus));

static struct k_sem report_rx_sem;
static struct k_sem request_tx_sem;

static void report_rx_callback(uint16_t data_id, const uint8_t *value, size_t value_len,
uint8_t source_addr)
{
k_sem_give(&report_rx_sem);
}

ZTEST(thingset_can, test_receive_report_from_node)
{

struct can_frame report_frame = {
.id = 0x1E000002, /* node with address 0x02 */
.flags = CAN_FRAME_IDE,
.data = { 0xF6 },
.dlc = 1,
};
int err;

k_sem_reset(&report_rx_sem);

err = can_send(can_dev, &report_frame, K_MSEC(10), NULL, NULL);
zassert_equal(err, 0, "can_send failed: %d", err);

err = k_sem_take(&report_rx_sem, TEST_RECEIVE_TIMEOUT);
zassert_equal(err, 0, "receive timeout");
}

static void request_rx_cb(const struct device *dev, struct can_frame *frame, void *user_data)
{
k_sem_give(&request_tx_sem);
}

ZTEST(thingset_can, test_send_request_to_node)
{
struct can_filter other_node_filter = {
.id = 0x1800CC00,
.mask = 0x1F00FF00,
.flags = CAN_FILTER_DATA | CAN_FILTER_IDE,
};
uint8_t req_buf[] = { 0x01, 0x00 }; /* simple single-frame request via ISO-TP */
int err;

k_sem_reset(&request_tx_sem);

err = can_add_rx_filter(can_dev, &request_rx_cb, NULL, &other_node_filter);
zassert_false(err < 0, "adding rx filter failed: %d", err);

thingset_can_send(req_buf, sizeof(req_buf), 0xCC);

err = k_sem_take(&request_tx_sem, TEST_RECEIVE_TIMEOUT);
zassert_equal(err, 0, "receive timeout");
}

static void *thingset_can_setup(void)
{
int err;

k_sem_init(&report_rx_sem, 0, 1);
k_sem_init(&request_tx_sem, 0, 1);

thingset_init_global(&ts);

zassert_true(device_is_ready(can_dev), "CAN device not ready");

(void)can_stop(can_dev);

err = can_set_mode(can_dev, CAN_MODE_LOOPBACK);
zassert_equal(err, 0, "failed to set loopback mode (err %d)", err);

err = can_start(can_dev);
zassert_equal(err, 0, "failed to start CAN controller (err %d)", err);

/* wait for address claiming to finish */
k_sleep(K_MSEC(1000));

thingset_can_set_report_rx_callback(report_rx_callback);

return NULL;
}

ZTEST_SUITE(thingset_can, NULL, thingset_can_setup, NULL, NULL, NULL);
7 changes: 7 additions & 0 deletions tests/can/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

tests:
thingset_sdk.can:
integration_platforms:
- native_posix
extra_args: EXTRA_CFLAGS=-Wno-error

0 comments on commit 74b15b6

Please sign in to comment.