Skip to content

Commit

Permalink
Use nnvg generated logic for serialisation/deserialisation for ID
Browse files Browse the repository at this point in the history
  • Loading branch information
aentinger committed Sep 15, 2020
1 parent 5d32028 commit 8107b51
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 33 deletions.
11 changes: 6 additions & 5 deletions extras/test/src/test_ID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static CanardPortID const ID_PORT_ID = 1337;
**************************************************************************************/

static util::CanFrame can_frame;
static uint16_t id_val = 0;
static CanardNodeID id_node_id = 0;
static uavcan_node_ID_1_0 id;
static CanardNodeID id_node_id = 0;

/**************************************************************************************
* PRIVATE FUNCTION DEFINITION
Expand All @@ -44,10 +44,10 @@ static bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t co

void onID_1_0_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
{
ID_1_0<ID_PORT_ID> const id = ID_1_0<ID_PORT_ID>::create(transfer);
ID_1_0<ID_PORT_ID> const received_id = ID_1_0<ID_PORT_ID>::create(transfer);

id_node_id = transfer.remote_node_id;
id_val = id.get_id();
id.value = received_id.data.value;
}

/**************************************************************************************
Expand All @@ -70,6 +70,7 @@ TEST_CASE("A 'ID.1.0.uavcan' message is sent", "[id-01]")

TEST_CASE("A 'ID.1.0.uavcan' message is received", "[id-02]")
{
uavcan_node_ID_1_0_init(&id);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, util::micros, transmitCanFrame);

REQUIRE(uavcan.subscribe<ID_1_0<ID_PORT_ID>>(onID_1_0_Received));
Expand All @@ -80,5 +81,5 @@ TEST_CASE("A 'ID.1.0.uavcan' message is received", "[id-02]")
uavcan.onCanFrameReceived(0x1005391B, data, sizeof(data));

REQUIRE(id_node_id == 27);
REQUIRE(id_val == 13);
REQUIRE(id.value == 13);
}
2 changes: 0 additions & 2 deletions src/types/uavcan/node/Heartbeat.1.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include "Heartbeat.1.0.h"

#include <libcanard/canard_dsdl.h>

#include "../../../utility/convert.hpp"

/**************************************************************************************
Expand Down
21 changes: 6 additions & 15 deletions src/types/uavcan/node/ID.1.0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* INCLUDE
**************************************************************************************/

#include <stdlib.h>

#include <libcanard/canard.h>

#include "ID.1.0.nnvg.h"

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
Expand All @@ -26,25 +26,16 @@ class ID_1_0

public:

uavcan_node_ID_1_0 data;

static constexpr CanardPortID PORT_ID = ID;
static constexpr size_t MAX_PAYLOAD_SIZE = 2;
static constexpr size_t MAX_PAYLOAD_SIZE = uavcan_node_ID_1_0_MAX_SERIALIZED_REPRESENTATION_SIZE_BYTES;
static constexpr CanardTransferKind TRANSFER_KIND = CanardTransferKindMessage;


ID_1_0(uint16_t const id);


static ID_1_0 create(CanardTransfer const & transfer);
size_t encode(uint8_t * payload) const;


inline uint16_t get_id() const { return _id; }
inline void set_id(uint16_t const id) { _id = id; }


private:

uint16_t _id;
size_t encode(uint8_t * payload) const;

};

Expand Down
18 changes: 7 additions & 11 deletions src/types/uavcan/node/ID.1.0.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* Contributors: https://github.com/107-systems/107-Arduino-UAVCAN/graphs/contributors.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <libcanard/canard_dsdl.h>

/**************************************************************************************
* STATIC CONSTEXPR DEFINITION
**************************************************************************************/
Expand All @@ -25,7 +19,7 @@ template <CanardPortID ID> constexpr CanardTransferKind ID_1_0<ID>::TRANSFER_KIN

template <CanardPortID ID>
ID_1_0<ID>::ID_1_0(uint16_t const id)
: _id{id}
: data{id}
{

}
Expand All @@ -37,13 +31,15 @@ ID_1_0<ID>::ID_1_0(uint16_t const id)
template <CanardPortID ID>
ID_1_0<ID> ID_1_0<ID>::create(CanardTransfer const & transfer)
{
uint16_t const id = canardDSDLGetU16(reinterpret_cast<uint8_t const *>(transfer.payload), transfer.payload_size, 0, 16);
return ID_1_0<ID>(id);
uavcan_node_ID_1_0 d;
uavcan_node_ID_1_0_init(&d);
uavcan_node_ID_1_0_deserialize(&d, 0, (uint8_t *)(transfer.payload), transfer.payload_size);
return ID_1_0<ID>(d.value);
}

template <CanardPortID ID>
size_t ID_1_0<ID>::encode(uint8_t * payload) const
{
canardDSDLSetUxx(payload, 0, _id, 16);
return MAX_PAYLOAD_SIZE;
size_t const offset = uavcan_node_ID_1_0_serialize(&data, 0, payload);
return (offset / 8);
}
111 changes: 111 additions & 0 deletions src/types/uavcan/node/ID.1.0.nnvg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// UAVCAN data structure definition. +-+ +-+
// | | | |
// AUTOGENERATED, DO NOT EDIT. \ - /
// ---
// o
// +------------------------------------------------------------------------------------------------------------------+
//
// Generator:
// nunavut-0.3.9 (serialization was enabled)
//
//
// Source File:
// /home/alex/projects/107-systems/public_regulated_data_types/uavcan/node/ID.1.0.uavcan
//
// Template:
// StructureType.j2
//
// Generated at: 2020-09-15 13:51:27.934955 UTC
// Is deprecated: no
// Fixed port ID: None
// Full name: uavcan.node.ID
// Version: 1.0

#ifndef UAVCAN_NODE_ID_1_0_INCLUDED
#define UAVCAN_NODE_ID_1_0_INCLUDED

#include <nunavut/support/serialization.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


typedef struct
{
uint16_t value;
} uavcan_node_ID_1_0;

/// Initialize an uavcan_node_ID_1_0 instance to default values.
/// No memory is allocated within this method.
/// Does nothing if @ref out_instance is NULL.
///
/// @param out_instance A structure instance to Initialize.
static inline void uavcan_node_ID_1_0_init(uavcan_node_ID_1_0* const out_instance)
{
if (out_instance != NULL)
{
out_instance->value = 0;
}
}

/// Contains the maximum number of bytes needed to serialize a(n) uavcan_node_ID_1_0
/// instance.
#define uavcan_node_ID_1_0_MAX_SERIALIZED_REPRESENTATION_SIZE_BYTES \
(2U)


/// Serialization of uavcan_node_ID_1_0 instance into a byte array.
///
/// @param in_instance A structure instance to serialize.
/// @param offset Offset (in bits) of data type in message.
/// @param out_buffer Output message buffer.
///
/// @returns Size of encoded data (in bits)
static inline int32_t uavcan_node_ID_1_0_serialize(const uavcan_node_ID_1_0* in_instance, uint32_t offset, uint8_t* const out_buffer)
{

if (out_buffer == NULL)
{
return -NUNAVUT_ERR_INVALID_BUF;
}

// Begin Structure: saturated uint16
nunavutSetUxx(out_buffer, offset, in_instance->value, 16);
offset += 16;
// End Structure: saturated uint16

return offset;
}

/// Deserialization of byte-array encoding into uavcan_node_ID_1_0 instance.
///
/// @param out_instance Structure instance to write data to.
/// @param offset Bit offset of structure in data buffer.
/// @param in_buffer Message buffer to deserialize.
/// @param buf_size_bytes Length of input buffer (in bytes) to avoid overflow.
///
/// @returns Size of decoded data (in bits)
static inline int32_t uavcan_node_ID_1_0_deserialize(uavcan_node_ID_1_0* const out_instance, uint32_t offset, uint8_t* const in_buffer, const size_t buf_size_bytes)
{

if (in_buffer == NULL)
{
return -NUNAVUT_ERR_INVALID_BUF;
}

// Begin Structure: saturated uint16
out_instance->value = nunavutGetU16(in_buffer, buf_size_bytes, offset, 16);
offset += 16;
// End Structure: saturated uint16

return offset;
}


#ifdef __cplusplus
} /* End extern "C" */
#endif /* __cplusplus */
#endif /* UAVCAN_NODE_ID_1_0_INCLUDED */

0 comments on commit 8107b51

Please sign in to comment.