Skip to content

Commit

Permalink
Start of updating CAN ID header format to match v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aasmune committed Mar 26, 2019
1 parent 2b53674 commit 0d3d002
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
# CMake build directory
build/
cmake-build-*/
CMakeFiles/
cmake_install.cmake
CMakeCache.txt
Makefile
demo
run_tests

# IDE and tools
.idea
Expand All @@ -49,3 +55,6 @@ dsdlc_generated/

# Pycache
__pycache__/

# Ignore IDE folders
.vscode/
40 changes: 12 additions & 28 deletions canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@
#define TRANSFER_TIMEOUT_USEC 2000000

#define TRANSFER_ID_BIT_LEN 5U
#define ANON_MSG_DATA_TYPE_ID_BIT_LEN 2U

#define SOURCE_ID_FROM_ID(x) ((uint8_t) (((x) >> 0U) & 0x7FU))
#define SERVICE_NOT_MSG_FROM_ID(x) ((bool) (((x) >> 7U) & 0x1U))
#define REQUEST_NOT_RESPONSE_FROM_ID(x) ((bool) (((x) >> 15U) & 0x1U))
#define SOURCE_ID_FROM_ID(x) ((uint8_t) (((x) >> 1U) & 0x7FU))
#define SERVICE_NOT_MSG_FROM_ID(x) ((bool) (((x) >> 25U) & 0x1U))
#define REQUEST_NOT_RESPONSE_FROM_ID(x) ((bool) (((x) >> 26U) & 0x1U))
#define DEST_ID_FROM_ID(x) ((uint8_t) (((x) >> 8U) & 0x7FU))
#define PRIORITY_FROM_ID(x) ((uint8_t) (((x) >> 24U) & 0x1FU))
#define PRIORITY_FROM_ID(x) ((uint8_t) (((x) >> 26U) & 0x1FU))
#define MSG_TYPE_FROM_ID(x) ((uint16_t)(((x) >> 8U) & 0xFFFFU))
#define SRV_TYPE_FROM_ID(x) ((uint8_t) (((x) >> 16U) & 0xFFU))
#define SRV_TYPE_FROM_ID(x) ((uint8_t) (((x) >> 15U) & 0x1FFU))

#define MAKE_TRANSFER_DESCRIPTOR(data_type_id, transfer_type, src_node_id, dst_node_id) \
(((uint32_t)(data_type_id)) | (((uint32_t)(transfer_type)) << 16U) | \
Expand Down Expand Up @@ -151,29 +150,19 @@ int16_t canardBroadcast(CanardInstance* ins,
uint32_t can_id = 0;
uint16_t crc = 0xFFFFU;

can_id = ((uint32_t) priority << 26U) | ((uint32_t) data_type_id << 8U) | (uint32_t) canardGetLocalNodeID(ins);

if (canardGetLocalNodeID(ins) == 0)
{
if (payload_len > 7)
{
return -CANARD_ERROR_NODE_ID_NOT_SET;
}

static const uint16_t DTIDMask = (1U << ANON_MSG_DATA_TYPE_ID_BIT_LEN) - 1U;

if ((data_type_id & DTIDMask) != data_type_id)
{
return -CANARD_ERROR_INVALID_ARGUMENT;
}

// anonymous transfer, random discriminator
const uint16_t discriminator = (uint16_t)((crcAdd(0xFFFFU, payload, payload_len)) & 0x7FFEU);
can_id = ((uint32_t) priority << 24U) | ((uint32_t) discriminator << 9U) |
((uint32_t) (data_type_id & DTIDMask) << 8U) | (uint32_t) canardGetLocalNodeID(ins);
can_id |= (1U << 24U);
}
else
{
can_id = ((uint32_t) priority << 24U) | ((uint32_t) data_type_id << 8U) | (uint32_t) canardGetLocalNodeID(ins);

if (payload_len > 7)
{
crc = crcAddSignature(crc, data_type_signature);
Expand Down Expand Up @@ -211,9 +200,9 @@ int16_t canardRequestOrRespond(CanardInstance* ins,
return -CANARD_ERROR_NODE_ID_NOT_SET;
}

const uint32_t can_id = ((uint32_t) priority << 24U) | ((uint32_t) data_type_id << 16U) |
((uint32_t) kind << 15U) | ((uint32_t) destination_node_id << 8U) |
(1U << 7U) | (uint32_t) canardGetLocalNodeID(ins);
const uint32_t can_id = ((uint32_t) priority << 26U) | ((uint32_t) data_type_id << 15U) |
((uint32_t) kind << 24U) | ((uint32_t) destination_node_id << 8U) |
(1U << 25U) | (uint32_t) canardGetLocalNodeID(ins);
uint16_t crc = 0xFFFFU;

if (payload_len > 7)
Expand Down Expand Up @@ -1081,12 +1070,7 @@ CANARD_INTERNAL uint16_t extractDataType(uint32_t id)
{
if (extractTransferType(id) == CanardTransferTypeBroadcast)
{
uint16_t dtid = MSG_TYPE_FROM_ID(id);
if (SOURCE_ID_FROM_ID(id) == CANARD_BROADCAST_NODE_ID)
{
dtid &= (1U << ANON_MSG_DATA_TYPE_ID_BIT_LEN) - 1U;
}
return dtid;
return (uint16_t) MSG_TYPE_FROM_ID(id);
}
else
{
Expand Down
53 changes: 26 additions & 27 deletions tests/test_rxerr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,34 @@
#include <canard.h>

//Independent implementation of ID layout from https://uavcan.org/Specification/4._CAN_bus_transport_layer/
#define CONSTRUCT_PRIO(prio) (((prio) & 0x1F) << 24)
#define CONSTRUCT_MTID(mtid) (((mtid) & 0xFFFF) << 8)
#define CONSTRUCT_SID(sid) (((sid) & 0x7F))
#define CONSTRUCT_DISC(disc) (((disc) & 0x3FFF) << 10)
#define CONSTRUCT_DISC_MTID(mtid) (((mtid) & 0x3) << 8)
#define CONSTRUCT_STID(stid) (((stid) & 0xFF) << 16)
#define CONSTRUCT_DID(did) (((did) & 0x7F) << 8)
#define CONSTRUCT_REQUEST(request) (((request) & 0x1) << 15)

#define CONSTRUCT_SNM_BIT (1 << 7)

#define CONSTRUCT_ANON_MSG_ID(prio, disc, mtid, sid) (CONSTRUCT_PRIO(prio) | \
CONSTRUCT_DISC_MTID(mtid) | \
CONSTRUCT_DISC(disc) | \
CONSTRUCT_SID(sid) \
#define CONSTRUCT_PRIO(prio) (((prio) & 0x7) << 26)
#define CONSTRUCT_SUBJECT_ID(subject_id) (((subject_id) & 0xFFFF) << 8)
#define CONSTRUCT_SOURCE_ID(source_id) (((source_id) & 0x7F) << 1)
#define CONSTRUCT_SERVICE_ID(service_tid) (((service_tid) & 0x1FF) << 15)
#define CONSTRUCT_DEST_ID(dest_id) (((dest_id) & 0x7F) << 8)
#define CONSTRUCT_REQUEST(request) (((request) & 0x1) << 24)

#define CONSTRUCT_ANON_BIT (1 << 24)
#define CONSTRUCT_SNM_BIT (1 << 25)
#define CONSTRUCT_PV_BIT (1)

#define CONSTRUCT_MSG_ID(prio, subject_id, source_id) (CONSTRUCT_PRIO(prio) | \
CONSTRUCT_SUBJECT_ID(subject_id) | \
CONSTRUCT_SOURCE_ID(source_id) | \
CONSTRUCT_PV_BIT |\
CANARD_CAN_FRAME_EFF)

#define CONSTRUCT_MSG_ID(prio, mtid, sid) (CONSTRUCT_PRIO(prio) | \
CONSTRUCT_MTID(mtid) | \
CONSTRUCT_SID(sid) | \
CANARD_CAN_FRAME_EFF)

#define CONSTRUCT_SVC_ID(prio, stid, request, did, sid) (CONSTRUCT_PRIO(prio) | \
CONSTRUCT_STID(stid) | \
CONSTRUCT_REQUEST(request) | \
CONSTRUCT_DID(did) | \
CONSTRUCT_SID(sid) | \
CONSTRUCT_SNM_BIT | \
CANARD_CAN_FRAME_EFF)
#define CONSTRUCT_ANON_MSG_ID(prio, subject_id, source_id) (CONSTRUCT_ANON_BIT | \
CONSTRUCT_MSG_ID(prio, subject_id, source_id))

#define CONSTRUCT_SVC_ID(prio, service_id, request, dest_id, source_id) (CONSTRUCT_PRIO(prio) | \
CONSTRUCT_SNM_BIT | \
CONSTRUCT_REQUEST(request) | \
CONSTRUCT_SERVICE_ID(service_id) | \
CONSTRUCT_DEST_ID(dest_id) | \
CONSTRUCT_SOURCE_ID(source_id) | \
CONSTRUCT_PV_BIT |\
CANARD_CAN_FRAME_EFF)

#define CONSTRUCT_START(start) (((start) & 0x1) << 7)
#define CONSTRUCT_END(end) (((end) & 0x1) << 6)
Expand Down

0 comments on commit 0d3d002

Please sign in to comment.