Skip to content

Commit

Permalink
test for request-response
Browse files Browse the repository at this point in the history
  • Loading branch information
garethpotter committed Nov 13, 2023
1 parent 9810263 commit 97bafea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/can/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/can/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdlib.h>

#include <zephyr/ztest.h>

#include <thingset.h>
Expand All @@ -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)
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 97bafea

Please sign in to comment.