From 97bafea192544f5698db65e565c109789666e208 Mon Sep 17 00:00:00 2001 From: Gareth Potter Date: Tue, 24 Oct 2023 20:22:22 +0100 Subject: [PATCH] test for request-response --- tests/can/prj.conf | 2 ++ tests/can/src/main.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/tests/can/prj.conf b/tests/can/prj.conf index 0b39679..5c5b399 100644 --- a/tests/can/prj.conf +++ b/tests/can/prj.conf @@ -8,6 +8,8 @@ CONFIG_ENTROPY_GENERATOR=y CONFIG_THINGSET=y CONFIG_THINGSET_SDK=y CONFIG_THINGSET_CAN=y +CONFIG_ISOTP_FAST=y +CONFIG_ISOTP_USE_TX_BUF=y CONFIG_ZTEST=y CONFIG_ZTEST_NEW_API=y diff --git a/tests/can/src/main.c b/tests/can/src/main.c index f83274c..023ff73 100644 --- a/tests/can/src/main.c +++ b/tests/can/src/main.c @@ -4,6 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ + #include + #include #include @@ -17,6 +19,27 @@ 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 struct k_sem response_rx_sem; + +uint8_t *response; +size_t response_len; +int response_code; + +static void isotp_fast_recv_cb(struct net_buf *buffer, int rem_len, + isotp_fast_msg_id sender_addr, void *arg) +{ + response = malloc(buffer->len); + memcpy(response, buffer->data, buffer->len); + response_code = 0; + response_len = buffer->len; + k_sem_give(&response_rx_sem); +} + +static void isotp_fast_sent_cb(int result, void *arg) +{ + response_code = result; + k_sem_give(&request_tx_sem); +} static void report_rx_callback(uint16_t data_id, const uint8_t *value, size_t value_len, uint8_t source_addr) @@ -73,12 +96,43 @@ ZTEST(thingset_can, test_send_request_to_node) zassert_equal(err, 0, "receive timeout"); } +ZTEST(thingset_can, test_request_response) +{ + k_sem_reset(&request_tx_sem); + k_sem_reset(&response_rx_sem); + + struct isotp_fast_ctx client_ctx; + struct isotp_fast_opts opts = { + .bs = 0, + .stmin = 0, + .flags = 0, + }; + int err = isotp_fast_bind(&client_ctx, can_dev, 0x1800cc00, &opts, isotp_fast_recv_cb, + NULL, NULL, isotp_fast_sent_cb); + zassert_equal(err, 0, "bind fail"); + + uint8_t msg[] = { 0x01, 0x1e }; + err = isotp_fast_send(&client_ctx, msg, sizeof(msg), 0x01, NULL); + zassert_equal(err, 0, "send fail"); + k_sem_take(&request_tx_sem, TEST_RECEIVE_TIMEOUT); + + k_sem_take(&response_rx_sem, TEST_RECEIVE_TIMEOUT); + zassert_equal(response_code, 0, "receive fail"); + + zassert_equal(response_len, 3, "unexpected response length %d", response_len); + // not found; can't do more than that for now + zassert_equal(response[0], 0xa4, "unexpected response"); + free(response); + isotp_fast_unbind(&client_ctx); +} + static void *thingset_can_setup(void) { int err; k_sem_init(&report_rx_sem, 0, 1); k_sem_init(&request_tx_sem, 0, 1); + k_sem_init(&response_rx_sem, 0, 1); thingset_init_global(&ts);