Skip to content

Commit

Permalink
CAN: Document and fix thingset_can_reqresp_callback_t
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaeger committed Feb 28, 2024
1 parent fe46f43 commit 60d01ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
26 changes: 18 additions & 8 deletions include/thingset/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,30 @@ typedef void (*thingset_can_report_rx_callback_t)(const uint8_t *report_buf, siz
* @param data_id ThingSet data object ID
* @param value Buffer containing the CBOR raw data of the value
* @param value_len Length of the value in the buffer
* @param source_addr Node address the control message was received from
* @param source_addr Node address the item was received from
*/
typedef void (*thingset_can_item_rx_callback_t)(uint16_t data_id, const uint8_t *value,
size_t value_len, uint8_t source_addr);

typedef void (*thingset_can_response_callback_t)(uint8_t *data, size_t len, int result,
uint8_t sender_id, void *arg);
/**
* Callback typedef for received responses via CAN ISO-TP
*
* @param data Buffer containing the ThingSet response or NULL in case of error.
* @param length Length of the data in the buffer
* @param send_err 0 for success or negative errno indicating a send error.
* @param recv_err 0 for success or negative errno indicating a receive error.
* @param source_addr Node address the response was received from
* @param arg User-data passed to the callback
*/
typedef void (*thingset_can_reqresp_callback_t)(uint8_t *data, size_t len, int send_err,
int recv_err, uint8_t source_addr, void *arg);

struct thingset_can_request_response
{
struct k_sem sem;
struct k_timer timer;
uint32_t can_id;
thingset_can_response_callback_t callback;
thingset_can_reqresp_callback_t callback;
void *cb_arg;
};

Expand Down Expand Up @@ -258,16 +268,16 @@ int thingset_can_send_report_inst(struct thingset_can *ts_can, const char *path,
* @param tx_len Length of the message.
* @param target_addr Target node address (8-bit value) to send the message to.
* @param target_bus Target bus number (4-bit value) to send the message to.
* @param rsp_callback If a response is expected, this callback will be invoked,
* either when it arrives or if a timeout or some other error occurs.
* @param callback This callback will be invoked when a response is received or an error during
* sending or receiving occurred. Set to NULL if no response is expected.
* @param callback_arg User data for the callback.
* @param timeout Timeout to wait for a response.
*
* @returns 0 for success or negative errno in case of error
*/
int thingset_can_send_inst(struct thingset_can *ts_can, uint8_t *tx_buf, size_t tx_len,
uint8_t target_addr, uint8_t target_bus,
thingset_can_response_callback_t rsp_callback, void *callback_arg,
thingset_can_reqresp_callback_t callback, void *callback_arg,
k_timeout_t timeout);

#ifdef CONFIG_THINGSET_CAN_REPORT_RX
Expand Down Expand Up @@ -337,7 +347,7 @@ int thingset_can_send_report(const char *path, enum thingset_data_format format)
* @returns 0 for success or negative errno in case of error
*/
int thingset_can_send(uint8_t *tx_buf, size_t tx_len, uint8_t target_addr, uint8_t target_bus,
thingset_can_response_callback_t rsp_callback, void *callback_arg,
thingset_can_reqresp_callback_t callback, void *callback_arg,
k_timeout_t timeout);

#ifdef CONFIG_THINGSET_CAN_REPORT_RX
Expand Down
18 changes: 9 additions & 9 deletions src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,13 @@ static void thingset_can_reqresp_timeout_handler(struct k_timer *timer)
{
struct thingset_can_request_response *rr =
CONTAINER_OF(timer, struct thingset_can_request_response, timer);
rr->callback(NULL, 0, -ETIMEDOUT, 0, rr->cb_arg);
rr->callback(NULL, 0, 0, -ETIMEDOUT, 0, rr->cb_arg);
thingset_can_reset_request_response(rr);
}

int thingset_can_send_inst(struct thingset_can *ts_can, uint8_t *tx_buf, size_t tx_len,
uint8_t target_addr, uint8_t target_bus,
thingset_can_response_callback_t rsp_callback, void *callback_arg,
thingset_can_reqresp_callback_t callback, void *callback_arg,
k_timeout_t timeout)
{
if (!device_is_ready(ts_can->dev)) {
Expand All @@ -434,12 +434,12 @@ int thingset_can_send_inst(struct thingset_can *ts_can, uint8_t *tx_buf, size_t
| THINGSET_CAN_TARGET_SET(target_addr),
};

if (rsp_callback != NULL) {
if (callback != NULL) {
if (k_sem_take(&ts_can->request_response.sem, timeout) != 0) {
return -ETIMEDOUT;
}

ts_can->request_response.callback = rsp_callback;
ts_can->request_response.callback = callback;
ts_can->request_response.cb_arg = callback_arg;
k_timer_init(&ts_can->request_response.timer, thingset_can_reqresp_timeout_handler, NULL);
k_timer_start(&ts_can->request_response.timer, timeout, timeout);
Expand Down Expand Up @@ -472,7 +472,7 @@ static void thingset_can_reqresp_recv_callback(struct net_buf *buffer, int rem_l
if (ts_can->request_response.callback != NULL
&& ts_can->request_response.can_id == addr.ext_id)
{
ts_can->request_response.callback(ts_can->rx_buffer, len, 0,
ts_can->request_response.callback(ts_can->rx_buffer, len, 0, 0,
(uint8_t)(addr.ext_id & 0xFF),
ts_can->request_response.cb_arg);
thingset_can_reset_request_response(&ts_can->request_response);
Expand Down Expand Up @@ -508,7 +508,7 @@ static void thingset_can_reqresp_sent_callback(int result, void *arg)
{
struct thingset_can *ts_can = arg;
if (ts_can->request_response.callback != NULL && result != 0) {
ts_can->request_response.callback(NULL, 0, result, 0, ts_can->request_response.cb_arg);
ts_can->request_response.callback(NULL, 0, 0, result, 0, ts_can->request_response.cb_arg);
thingset_can_reset_request_response(&ts_can->request_response);
}
else {
Expand Down Expand Up @@ -730,11 +730,11 @@ int thingset_can_send_report(const char *path, enum thingset_data_format format)
}

int thingset_can_send(uint8_t *tx_buf, size_t tx_len, uint8_t target_addr, uint8_t target_bus,
thingset_can_response_callback_t rsp_callback, void *callback_arg,
thingset_can_reqresp_callback_t callback, void *callback_arg,
k_timeout_t timeout)
{
return thingset_can_send_inst(&ts_can_single, tx_buf, tx_len, target_addr, target_bus,
rsp_callback, callback_arg, timeout);
return thingset_can_send_inst(&ts_can_single, tx_buf, tx_len, target_addr, target_bus, callback,
callback_arg, timeout);
}

#ifdef CONFIG_THINGSET_CAN_REPORT_RX
Expand Down

0 comments on commit 60d01ea

Please sign in to comment.