Skip to content

Commit

Permalink
Merge pull request #165 from 107-systems/unique-id
Browse files Browse the repository at this point in the history
Provide a UniqueId service to provide a unique ID for the NodeInfo service.
  • Loading branch information
aentinger authored Oct 12, 2022
2 parents c578df0 + c1c52c4 commit d27f4e9
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#undef min
#include <algorithm>

#include "NodeInfo.h"

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand Down Expand Up @@ -161,6 +159,24 @@ static RegisterNatural16 reg_ro_uavcan_pub_distance_id ("uavcan.pub.distance.id
static RegisterString reg_ro_uavcan_pub_distance_type("uavcan.pub.distance.type", Register::Access::ReadOnly, Register::Persistent::No, "uavcan.primitive.scalar.Real32.1.0");
static RegisterList reg_list;

/* NODE INFO **************************************************************************/

static NodeInfo node_info
(
/* uavcan.node.Version.1.0 protocol_version */
1, 0,
/* uavcan.node.Version.1.0 hardware_version */
1, 0,
/* uavcan.node.Version.1.0 software_version */
0, 1,
/* saturated uint64 software_vcs_revision_id */
NULL,
/* saturated uint8[16] unique_id */
OpenCyphalUniqueId(),
/* saturated uint8[<=50] name */
"107-systems.tof-sensor-node"
);

/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/
Expand Down Expand Up @@ -209,7 +225,7 @@ void setup()

/* Register callbacks for node info and register api.
*/
node_hdl.subscribe<GetInfo_1_0::Request<>>(onGetInfo_1_0_Request_Received);
node_info.subscribe(node_hdl);

reg_list.add(reg_rw_uavcan_node_id);
reg_list.add(reg_ro_uavcan_node_description);
Expand Down Expand Up @@ -261,14 +277,6 @@ void mcp2515_onReceiveBufferFull(CanardFrame const & frame)
node_hdl.onCanFrameReceived(frame, micros());
}

void onGetInfo_1_0_Request_Received(CanardRxTransfer const &transfer, Node & node_hdl)
{
DBG_INFO("onGetInfo_1_0_Request_Received");
GetInfo_1_0::Response<> rsp = GetInfo_1_0::Response<>();
memcpy(&rsp.data, &NODE_INFO, sizeof(uavcan_node_GetInfo_Response_1_0));
node_hdl.respond(rsp, transfer.metadata.remote_node_id, transfer.metadata.transfer_id);
}

void publish_heartbeat(Node & u, uint32_t const uptime, Heartbeat_1_0<>::Mode const mode)
{
Heartbeat_1_0<> hb;
Expand Down
29 changes: 29 additions & 0 deletions examples/tools/PrintUniqueId/PrintUniqueId.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
*/

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

#include <107-Arduino-Cyphal.h>

/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/

void setup()
{
Serial.begin(115200);
while (!Serial) { }

Serial.println(OpenCyphalUniqueId);
}

void loop()
{

}
2 changes: 2 additions & 0 deletions src/107-Arduino-Cyphal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "Node.h"
#include "Types.h"
#include "NodeInfo.h"
#include "register/RegisterList.h"
#include "utility/UniqueId16.h"

#endif /* _107_ARDUINO_CYPHAL_H_ */
61 changes: 61 additions & 0 deletions src/NodeInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
*/

/**************************************************************************************
* INCLUDES
**************************************************************************************/

#include "NodeInfo.h"

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

NodeInfo::NodeInfo(uint8_t const protocol_major, uint8_t const protocol_minor,
uint8_t const hardware_major, uint8_t const hardware_minor,
uint8_t const software_major, uint8_t const software_minor,
uint64_t const software_vcs_revision_id,
UniqueId16Array const unique_id,
std::string const & name)
{
_node_info.protocol_version.major = protocol_major;
_node_info.protocol_version.minor = protocol_minor;

_node_info.hardware_version.major = hardware_major;
_node_info.hardware_version.minor = hardware_minor;

_node_info.software_version.major = software_major;
_node_info.software_version.minor = software_minor;

_node_info.software_vcs_revision_id = software_vcs_revision_id;

memcpy(_node_info.unique_id, unique_id.data(), sizeof(_node_info.unique_id));

_node_info.name.count = std::min(name.length(), uavcan_node_GetInfo_Response_1_0_name_ARRAY_CAPACITY_);
memcpy(_node_info.name.elements, name.c_str(), _node_info.name.count);
}

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

void NodeInfo::subscribe(Node & node_hdl)
{
node_hdl.subscribe<uavcan::node::GetInfo_1_0::Request<>>
([this](CanardRxTransfer const & transfer, Node & node_hdl) { this->onGetInfo_1_0_Request_Received(transfer, node_hdl); });
}

/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/

void NodeInfo::onGetInfo_1_0_Request_Received(CanardRxTransfer const & transfer, Node & node_hdl)
{
uavcan::node::GetInfo_1_0::Response<> rsp = uavcan::node::GetInfo_1_0::Response<>();
memcpy(&rsp.data, &_node_info, sizeof(uavcan_node_GetInfo_Response_1_0));
node_hdl.respond(rsp, transfer.metadata.remote_node_id, transfer.metadata.transfer_id);
}
45 changes: 45 additions & 0 deletions src/NodeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
*/

#ifndef ARDUINO_OPENCYPHAL_NODE_INFO_H_
#define ARDUINO_OPENCYPHAL_NODE_INFO_H_

/**************************************************************************************
* INCLUDES
**************************************************************************************/

#include <string>

#include "Node.h"
#include "utility/UniqueId16.h"

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/

class NodeInfo
{
public:

NodeInfo(uint8_t const protocol_major, uint8_t const protocol_minor,
uint8_t const hardware_major, uint8_t const hardware_minor,
uint8_t const software_major, uint8_t const software_minor,
uint64_t const software_vcs_revision_id,
UniqueId16Array const unique_id,
std::string const & name);


void subscribe(Node & node_hdl);


private:
uavcan_node_GetInfo_Response_1_0 _node_info;

void onGetInfo_1_0_Request_Received(CanardRxTransfer const & transfer, Node & node_hdl);
};

#endif /* ARDUINO_OPENCYPHAL_NODE_INFO_H_ */
4 changes: 2 additions & 2 deletions src/utility/CritSec-rp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "CritSec.h"

#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_NANO_RP2040_CONNECT)
#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)
#include <Arduino.h>

/**************************************************************************************
Expand All @@ -28,4 +28,4 @@ extern "C" void crit_sec_leave()
interrupts();
}

#endif /* ARDUINO_ARCH_RP2040 */
#endif /* defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) */
43 changes: 43 additions & 0 deletions src/utility/UniqueId16-esp32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
*/

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

#include "UniqueId16.h"

#if defined(ARDUINO_ARCH_ESP32)

#include <Arduino.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace impl
{

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

UniqueId16::UniqueId16()
: _unique_id{0}
{
size_t constexpr CHIP_ID_SIZE = 6;
uint64_t const chipid = ESP.getEfuseMac();
memcpy(_unique_id, &chipid, CHIP_ID_SIZE);
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* impl */

#endif /* defined(ARDUINO_ARCH_ESP32) */
45 changes: 45 additions & 0 deletions src/utility/UniqueId16-rp2040.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
*/

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

#include "UniqueId16.h"

#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)

#include <cstring>
#include <pico/unique_id.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace impl
{

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

UniqueId16::UniqueId16()
: _unique_id{0}
{
pico_unique_board_id_t pico_id;
pico_get_unique_board_id(&pico_id);

memcpy(_unique_id, pico_id.id, PICO_UNIQUE_BOARD_ID_SIZE_BYTES);
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* impl */

#endif /* defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARDUINO_NANO_RP2040_CONNECT) */
Loading

0 comments on commit d27f4e9

Please sign in to comment.