Skip to content

Commit d27f4e9

Browse files
authored
Merge pull request #165 from 107-systems/unique-id
Provide a UniqueId service to provide a unique ID for the NodeInfo service.
2 parents c578df0 + c1c52c4 commit d27f4e9

File tree

11 files changed

+433
-56
lines changed

11 files changed

+433
-56
lines changed

examples/OpenCyphal-ToF-Distance-Sensor-Node/OpenCyphal-ToF-Distance-Sensor-Node.ino

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#undef min
3131
#include <algorithm>
3232

33-
#include "NodeInfo.h"
34-
3533
/**************************************************************************************
3634
* NAMESPACE
3735
**************************************************************************************/
@@ -161,6 +159,24 @@ static RegisterNatural16 reg_ro_uavcan_pub_distance_id ("uavcan.pub.distance.id
161159
static RegisterString reg_ro_uavcan_pub_distance_type("uavcan.pub.distance.type", Register::Access::ReadOnly, Register::Persistent::No, "uavcan.primitive.scalar.Real32.1.0");
162160
static RegisterList reg_list;
163161

162+
/* NODE INFO **************************************************************************/
163+
164+
static NodeInfo node_info
165+
(
166+
/* uavcan.node.Version.1.0 protocol_version */
167+
1, 0,
168+
/* uavcan.node.Version.1.0 hardware_version */
169+
1, 0,
170+
/* uavcan.node.Version.1.0 software_version */
171+
0, 1,
172+
/* saturated uint64 software_vcs_revision_id */
173+
NULL,
174+
/* saturated uint8[16] unique_id */
175+
OpenCyphalUniqueId(),
176+
/* saturated uint8[<=50] name */
177+
"107-systems.tof-sensor-node"
178+
);
179+
164180
/**************************************************************************************
165181
* SETUP/LOOP
166182
**************************************************************************************/
@@ -209,7 +225,7 @@ void setup()
209225

210226
/* Register callbacks for node info and register api.
211227
*/
212-
node_hdl.subscribe<GetInfo_1_0::Request<>>(onGetInfo_1_0_Request_Received);
228+
node_info.subscribe(node_hdl);
213229

214230
reg_list.add(reg_rw_uavcan_node_id);
215231
reg_list.add(reg_ro_uavcan_node_description);
@@ -261,14 +277,6 @@ void mcp2515_onReceiveBufferFull(CanardFrame const & frame)
261277
node_hdl.onCanFrameReceived(frame, micros());
262278
}
263279

264-
void onGetInfo_1_0_Request_Received(CanardRxTransfer const &transfer, Node & node_hdl)
265-
{
266-
DBG_INFO("onGetInfo_1_0_Request_Received");
267-
GetInfo_1_0::Response<> rsp = GetInfo_1_0::Response<>();
268-
memcpy(&rsp.data, &NODE_INFO, sizeof(uavcan_node_GetInfo_Response_1_0));
269-
node_hdl.respond(rsp, transfer.metadata.remote_node_id, transfer.metadata.transfer_id);
270-
}
271-
272280
void publish_heartbeat(Node & u, uint32_t const uptime, Heartbeat_1_0<>::Mode const mode)
273281
{
274282
Heartbeat_1_0<> hb;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2020 LXRobotics.
4+
* Author: Alexander Entinger <[email protected]>
5+
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
6+
*/
7+
8+
/**************************************************************************************
9+
* INCLUDE
10+
**************************************************************************************/
11+
12+
#include <107-Arduino-Cyphal.h>
13+
14+
/**************************************************************************************
15+
* SETUP/LOOP
16+
**************************************************************************************/
17+
18+
void setup()
19+
{
20+
Serial.begin(115200);
21+
while (!Serial) { }
22+
23+
Serial.println(OpenCyphalUniqueId);
24+
}
25+
26+
void loop()
27+
{
28+
29+
}

src/107-Arduino-Cyphal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "Node.h"
1616
#include "Types.h"
17+
#include "NodeInfo.h"
1718
#include "register/RegisterList.h"
19+
#include "utility/UniqueId16.h"
1820

1921
#endif /* _107_ARDUINO_CYPHAL_H_ */

src/NodeInfo.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2020 LXRobotics.
4+
* Author: Alexander Entinger <[email protected]>
5+
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
6+
*/
7+
8+
/**************************************************************************************
9+
* INCLUDES
10+
**************************************************************************************/
11+
12+
#include "NodeInfo.h"
13+
14+
/**************************************************************************************
15+
* CTOR/DTOR
16+
**************************************************************************************/
17+
18+
NodeInfo::NodeInfo(uint8_t const protocol_major, uint8_t const protocol_minor,
19+
uint8_t const hardware_major, uint8_t const hardware_minor,
20+
uint8_t const software_major, uint8_t const software_minor,
21+
uint64_t const software_vcs_revision_id,
22+
UniqueId16Array const unique_id,
23+
std::string const & name)
24+
{
25+
_node_info.protocol_version.major = protocol_major;
26+
_node_info.protocol_version.minor = protocol_minor;
27+
28+
_node_info.hardware_version.major = hardware_major;
29+
_node_info.hardware_version.minor = hardware_minor;
30+
31+
_node_info.software_version.major = software_major;
32+
_node_info.software_version.minor = software_minor;
33+
34+
_node_info.software_vcs_revision_id = software_vcs_revision_id;
35+
36+
memcpy(_node_info.unique_id, unique_id.data(), sizeof(_node_info.unique_id));
37+
38+
_node_info.name.count = std::min(name.length(), uavcan_node_GetInfo_Response_1_0_name_ARRAY_CAPACITY_);
39+
memcpy(_node_info.name.elements, name.c_str(), _node_info.name.count);
40+
}
41+
42+
/**************************************************************************************
43+
* PUBLIC MEMBER FUNCTIONS
44+
**************************************************************************************/
45+
46+
void NodeInfo::subscribe(Node & node_hdl)
47+
{
48+
node_hdl.subscribe<uavcan::node::GetInfo_1_0::Request<>>
49+
([this](CanardRxTransfer const & transfer, Node & node_hdl) { this->onGetInfo_1_0_Request_Received(transfer, node_hdl); });
50+
}
51+
52+
/**************************************************************************************
53+
* PRIVATE MEMBER FUNCTIONS
54+
**************************************************************************************/
55+
56+
void NodeInfo::onGetInfo_1_0_Request_Received(CanardRxTransfer const & transfer, Node & node_hdl)
57+
{
58+
uavcan::node::GetInfo_1_0::Response<> rsp = uavcan::node::GetInfo_1_0::Response<>();
59+
memcpy(&rsp.data, &_node_info, sizeof(uavcan_node_GetInfo_Response_1_0));
60+
node_hdl.respond(rsp, transfer.metadata.remote_node_id, transfer.metadata.transfer_id);
61+
}

src/NodeInfo.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2020 LXRobotics.
4+
* Author: Alexander Entinger <[email protected]>
5+
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
6+
*/
7+
8+
#ifndef ARDUINO_OPENCYPHAL_NODE_INFO_H_
9+
#define ARDUINO_OPENCYPHAL_NODE_INFO_H_
10+
11+
/**************************************************************************************
12+
* INCLUDES
13+
**************************************************************************************/
14+
15+
#include <string>
16+
17+
#include "Node.h"
18+
#include "utility/UniqueId16.h"
19+
20+
/**************************************************************************************
21+
* CLASS DECLARATION
22+
**************************************************************************************/
23+
24+
class NodeInfo
25+
{
26+
public:
27+
28+
NodeInfo(uint8_t const protocol_major, uint8_t const protocol_minor,
29+
uint8_t const hardware_major, uint8_t const hardware_minor,
30+
uint8_t const software_major, uint8_t const software_minor,
31+
uint64_t const software_vcs_revision_id,
32+
UniqueId16Array const unique_id,
33+
std::string const & name);
34+
35+
36+
void subscribe(Node & node_hdl);
37+
38+
39+
private:
40+
uavcan_node_GetInfo_Response_1_0 _node_info;
41+
42+
void onGetInfo_1_0_Request_Received(CanardRxTransfer const & transfer, Node & node_hdl);
43+
};
44+
45+
#endif /* ARDUINO_OPENCYPHAL_NODE_INFO_H_ */

src/utility/CritSec-rp2040.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "CritSec.h"
1313

14-
#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_NANO_RP2040_CONNECT)
14+
#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)
1515
#include <Arduino.h>
1616

1717
/**************************************************************************************
@@ -28,4 +28,4 @@ extern "C" void crit_sec_leave()
2828
interrupts();
2929
}
3030

31-
#endif /* ARDUINO_ARCH_RP2040 */
31+
#endif /* defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) */

src/utility/UniqueId16-esp32.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2020 LXRobotics.
4+
* Author: Alexander Entinger <[email protected]>
5+
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
6+
*/
7+
8+
/**************************************************************************************
9+
* INCLUDE
10+
**************************************************************************************/
11+
12+
#include "UniqueId16.h"
13+
14+
#if defined(ARDUINO_ARCH_ESP32)
15+
16+
#include <Arduino.h>
17+
18+
/**************************************************************************************
19+
* NAMESPACE
20+
**************************************************************************************/
21+
22+
namespace impl
23+
{
24+
25+
/**************************************************************************************
26+
* CTOR/DTOR
27+
**************************************************************************************/
28+
29+
UniqueId16::UniqueId16()
30+
: _unique_id{0}
31+
{
32+
size_t constexpr CHIP_ID_SIZE = 6;
33+
uint64_t const chipid = ESP.getEfuseMac();
34+
memcpy(_unique_id, &chipid, CHIP_ID_SIZE);
35+
}
36+
37+
/**************************************************************************************
38+
* NAMESPACE
39+
**************************************************************************************/
40+
41+
} /* impl */
42+
43+
#endif /* defined(ARDUINO_ARCH_ESP32) */

src/utility/UniqueId16-rp2040.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This software is distributed under the terms of the MIT License.
3+
* Copyright (c) 2020 LXRobotics.
4+
* Author: Alexander Entinger <[email protected]>
5+
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors.
6+
*/
7+
8+
/**************************************************************************************
9+
* INCLUDE
10+
**************************************************************************************/
11+
12+
#include "UniqueId16.h"
13+
14+
#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)
15+
16+
#include <cstring>
17+
#include <pico/unique_id.h>
18+
19+
/**************************************************************************************
20+
* NAMESPACE
21+
**************************************************************************************/
22+
23+
namespace impl
24+
{
25+
26+
/**************************************************************************************
27+
* CTOR/DTOR
28+
**************************************************************************************/
29+
30+
UniqueId16::UniqueId16()
31+
: _unique_id{0}
32+
{
33+
pico_unique_board_id_t pico_id;
34+
pico_get_unique_board_id(&pico_id);
35+
36+
memcpy(_unique_id, pico_id.id, PICO_UNIQUE_BOARD_ID_SIZE_BYTES);
37+
}
38+
39+
/**************************************************************************************
40+
* NAMESPACE
41+
**************************************************************************************/
42+
43+
} /* impl */
44+
45+
#endif /* defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARDUINO_NANO_RP2040_CONNECT) */

0 commit comments

Comments
 (0)