Skip to content

Commit a31e870

Browse files
authored
Refactor common nocli parts (#154)
1 parent 0f9fe35 commit a31e870

14 files changed

+462
-246
lines changed

linux/tools/nocli/Main.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ int
1111
main(int argc, char* argv[])
1212
{
1313
auto cliData = std::make_shared<nearobject::cli::NearObjectCliDataLinux>();
14-
nearobject::cli::NearObjectCli cli{ cliData };
14+
auto cliHandler = std::make_shared<nearobject::cli::NearObjectCliHandler>();
15+
nearobject::cli::NearObjectCli cli{ cliData, cliHandler };
1516

1617
// Parse the arguments.
1718
int result = cli.Parse(argc, argv);

tools/cli/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ set(NO_CLI_DIR_PUBLIC_INCLUDE_PREFIX ${NO_CLI_DIR_PUBLIC_INCLUDE}/nearobject/cli
77
target_sources(nocli-core
88
PRIVATE
99
${CMAKE_CURRENT_LIST_DIR}/NearObjectCli.cxx
10-
${CMAKE_CURRENT_LIST_DIR}/NearObjectCliData.cxx
10+
${CMAKE_CURRENT_LIST_DIR}/NearObjectCliHandler.cxx
1111
${CMAKE_CURRENT_LIST_DIR}/NearObjectCliUwbSessionEventCallbacks.cxx
1212
PUBLIC
1313
${NO_CLI_DIR_PUBLIC_INCLUDE_PREFIX}/NearObjectCli.hxx
14+
${NO_CLI_DIR_PUBLIC_INCLUDE_PREFIX}/NearObjectCliHandler.hxx
1415
${NO_CLI_DIR_PUBLIC_INCLUDE_PREFIX}/NearObjectCliData.hxx
1516
${NO_CLI_DIR_PUBLIC_INCLUDE_PREFIX}/NearObjectCliUwbSessionEventCallbacks.hxx
1617
)

tools/cli/Main.cxx

Lines changed: 0 additions & 7 deletions
This file was deleted.

tools/cli/NearObjectCli.cxx

Lines changed: 167 additions & 92 deletions
Large diffs are not rendered by default.

tools/cli/NearObjectCliData.cxx

Lines changed: 0 additions & 6 deletions
This file was deleted.

tools/cli/NearObjectCliHandler.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include <nearobject/cli/NearObjectCliHandler.hxx>
3+
#include <nearobject/cli/NearObjectCliUwbSessionEventCallbacks.hxx>
4+
#include <uwb/UwbDevice.hxx>
5+
#include <uwb/UwbSession.hxx>
6+
7+
using namespace nearobject::cli;
8+
9+
std::shared_ptr<uwb::UwbDevice>
10+
NearObjectCliHandler::ResolveUwbDevice(const nearobject::cli::NearObjectCliData& /*cliData */) noexcept
11+
{
12+
// Default implementation does not know how to resolve a device. OS-specific
13+
// implementations are expected to sub-class and override this function.
14+
return nullptr;
15+
}
16+
17+
void
18+
NearObjectCliHandler::HandleStartRanging(std::shared_ptr<uwb::UwbDevice> uwbDevice, uwb::protocol::fira::UwbSessionData& sessionData) noexcept
19+
{
20+
auto callbacks = std::make_shared<nearobject::cli::NearObjectCliUwbSessionEventCallbacks>();
21+
auto session = uwbDevice->CreateSession(callbacks);
22+
session->Configure(sessionData);
23+
session->StartRanging();
24+
}
25+
26+
void
27+
NearObjectCliHandler::HandleStopRanging() noexcept
28+
{
29+
// TODO
30+
}

tools/cli/NearObjectCliUwbSessionEventCallbacks.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66
using namespace nearobject::cli;
77

88
void
9-
NearObjectCliUwbSessionEventCallbacks::OnSessionEnded(::uwb::UwbSession * /* session */, ::uwb::UwbSessionEndReason /* reason */)
9+
NearObjectCliUwbSessionEventCallbacks::OnSessionEnded(::uwb::UwbSession* /* session */, ::uwb::UwbSessionEndReason /* reason */)
1010
{
1111
std::cout << "Session Ended" << std::endl;
1212
}
1313

1414
void
15-
NearObjectCliUwbSessionEventCallbacks::OnRangingStarted(::uwb::UwbSession * /* session */)
15+
NearObjectCliUwbSessionEventCallbacks::OnRangingStarted(::uwb::UwbSession* /* session */)
1616
{
1717
std::cout << "Ranging Started" << std::endl;
1818
}
1919

2020
void
21-
NearObjectCliUwbSessionEventCallbacks::OnRangingStopped(::uwb::UwbSession * /* session */)
21+
NearObjectCliUwbSessionEventCallbacks::OnRangingStopped(::uwb::UwbSession* /* session */)
2222
{
2323
std::cout << "Ranging Stopped" << std::endl;
2424
}
2525

2626
void
27-
NearObjectCliUwbSessionEventCallbacks::OnPeerPropertiesChanged(::uwb::UwbSession * /* session */, const std::vector<::uwb::UwbPeer> /* peersChanged */)
27+
NearObjectCliUwbSessionEventCallbacks::OnPeerPropertiesChanged(::uwb::UwbSession* /* session */, const std::vector<::uwb::UwbPeer> /* peersChanged */)
2828
{
2929
std::cout << "Peer Properties Changed" << std::endl;
3030
}
3131

3232
void
33-
NearObjectCliUwbSessionEventCallbacks::OnSessionMembershipChanged(::uwb::UwbSession * /* session */, const std::vector<::uwb::UwbPeer> /* peersAdded */, const std::vector<::uwb::UwbPeer> /* peersRemoved */)
33+
NearObjectCliUwbSessionEventCallbacks::OnSessionMembershipChanged(::uwb::UwbSession* /* session */, const std::vector<::uwb::UwbPeer> /* peersAdded */, const std::vector<::uwb::UwbPeer> /* peersRemoved */)
3434
{
3535
std::cout << "Session Membership Changed" << std::endl;
3636
}

tools/cli/include/nearobject/cli/NearObjectCli.hxx

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
#ifndef NEAR_OBJECT_CLI_HXX
33
#define NEAR_OBJECT_CLI_HXX
44

5+
#include <functional>
56
#include <memory>
67

78
#include <CLI/CLI.hpp>
8-
9-
#include "NearObjectCliData.hxx"
9+
#include <nearobject/cli/NearObjectCliHandler.hxx>
10+
#include <nearobject/cli/NearObjectCliData.hxx>
11+
#include <uwb/UwbDevice.hxx>
1012

1113
namespace nearobject::cli
1214
{
13-
struct NearObjectCliData;
14-
1515
/**
1616
* @brief Near object command-line (nocli) top-level object.
1717
*
@@ -25,8 +25,9 @@ public:
2525
* @brief Construct a new NearObjectCli object.
2626
*
2727
* @param cliData An instance to store the parsed data.
28+
* @param cliHandler An instance to handle parsed command requests.
2829
*/
29-
explicit NearObjectCli(std::shared_ptr<NearObjectCliData> cliData);
30+
NearObjectCli(std::shared_ptr<NearObjectCliData> cliData, std::shared_ptr<NearObjectCliHandler> cliHandler);
3031

3132
/**
3233
* @brief Get a reference to the parser. The parser will be configured with
@@ -89,6 +90,21 @@ public:
8990
GetRangeStopApp() noexcept;
9091

9192
private:
93+
/**
94+
* @brief Obtain a reference to the resolved uwb device.
95+
*
96+
* This is only valid if the selected command requires a uwb device. The
97+
* device is resolved using the names in the following order:
98+
*
99+
* 1. Device name as selected from a device probe request via command line.
100+
* 2. Device name as specified on the command line.
101+
* 3. Default device name (first device found).
102+
*
103+
* @return std::shared_ptr<uwb::UwbDevice>
104+
*/
105+
std::shared_ptr<uwb::UwbDevice>
106+
GetUwbDevice() noexcept;
107+
92108
/**
93109
* @brief Create an initialize the command line parser. This will create a
94110
* parser and configure it with all common options and flags.
@@ -98,11 +114,51 @@ private:
98114
std::unique_ptr<CLI::App>
99115
CreateParser() noexcept;
100116

117+
/**
118+
* @brief Add the 'uwb' sub-command.
119+
*
120+
* @param parent The parent app to add the command to.
121+
* @return CLI::App*
122+
*/
123+
CLI::App*
124+
AddSubcommandUwb(CLI::App* parent);
125+
126+
/**
127+
* @brief Add the 'uwb range' sub-command.
128+
*
129+
* @param parent The parent app to add the command to.
130+
* @return CLI::App*
131+
*/
132+
CLI::App*
133+
AddSubcommandUwbRange(CLI::App* parent);
134+
135+
/**
136+
* @brief Add the 'uwb range start' sub-command.
137+
*
138+
* @param parent
139+
* @return CLI::App*
140+
*/
141+
CLI::App*
142+
AddSubcommandUwbRangeStart(CLI::App* parent);
143+
144+
/**
145+
* @brief Add the 'uwb range stop' sub-command.
146+
*
147+
* @param parent The parent app to add the command to.
148+
* @return CLI::App*
149+
*/
150+
CLI::App*
151+
AddSubcommandUwbRangeStop(CLI::App* parent);
152+
101153
private:
102-
std::unique_ptr<CLI::App> m_cliApp;
103154
std::shared_ptr<NearObjectCliData> m_cliData;
155+
std::shared_ptr<NearObjectCliHandler> m_cliHandler;
156+
157+
// Resolved device instance, if applicable to the selected command.
158+
std::shared_ptr<uwb::UwbDevice> m_uwbDevice;
104159

105-
// the following are helper references to the subcommands of m_cliApp, the memory is managed by CLI11
160+
std::unique_ptr<CLI::App> m_cliApp;
161+
// The following are helper references to the subcommands of m_cliApp, the memory is managed by CLI11.
106162
CLI::App* m_uwbApp;
107163
CLI::App* m_rangeApp;
108164
CLI::App* m_rangeStartApp;

tools/cli/include/nearobject/cli/NearObjectCliData.hxx

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#ifndef NEAR_OBJECT_CLI_DATA_HXX
33
#define NEAR_OBJECT_CLI_DATA_HXX
44

5-
#include <map>
5+
#include <string>
6+
#include <unordered_map>
7+
68
#include <uwb/protocols/fira/UwbSessionData.hxx>
79

810
namespace nearobject::cli
@@ -12,20 +14,7 @@ namespace nearobject::cli
1214
*/
1315
struct NearObjectCliData
1416
{
15-
std::unordered_map<std::string, uwb::protocol::fira::DeviceRole> DeviceRoleMap;
16-
std::unordered_map<std::string, uwb::protocol::fira::RangingMethod> RangingMethodMap;
17-
std::unordered_map<std::string, uwb::protocol::fira::MeasurementReportMode> MeasurementReportModeMap;
18-
std::unordered_map<std::string, uwb::protocol::fira::RangingConfiguration> RangingConfigurationMap;
19-
std::unordered_map<std::string, uwb::protocol::fira::StsConfiguration> StsConfigurationMap;
20-
std::unordered_map<std::string, uwb::protocol::fira::MultiNodeMode> MultiNodeModeMap;
21-
std::unordered_map<std::string, uwb::protocol::fira::RangingMode> RangingModeMap;
22-
std::unordered_map<std::string, uwb::protocol::fira::SchedulingMode> SchedulingModeMap;
23-
std::unordered_map<std::string, uwb::protocol::fira::Channel> ChannelMap;
24-
std::unordered_map<std::string, uwb::protocol::fira::StsPacketConfiguration> StsPacketConfigurationMap;
25-
std::unordered_map<std::string, uwb::protocol::fira::ConvolutionalCodeConstraintLength> ConvolutionalCodeConstraintLengthMap;
26-
std::unordered_map<std::string, uwb::protocol::fira::PrfMode> PrfModeMap;
27-
std::unordered_map<std::string, uwb::UwbMacAddressFcsType> UwbMacAddressFcsTypeMap;
28-
std::unordered_map<std::string, uwb::protocol::fira::ResultReportConfiguration> ResultReportConfigurationMap;
17+
virtual ~NearObjectCliData() = default;
2918

3019
std::string MacVersionString;
3120
std::string PhyVersionString;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
#ifndef NEAR_OBJECT_CLI_HANDLER_HXX
3+
#define NEAR_OBJECT_CLI_HANDLER_HXX
4+
5+
#include <memory>
6+
7+
#include <nearobject/cli/NearObjectCliData.hxx>
8+
#include <uwb/protocols/fira/UwbSessionData.hxx>
9+
#include <uwb/UwbDevice.hxx>
10+
11+
namespace nearobject::cli
12+
{
13+
/**
14+
* @brief Class which handles and executes resolved command-line requests. The
15+
* command line driver will invoke the function associated with the command line
16+
* request. Multiple distinct actions may be invoked simultaneously.
17+
*/
18+
struct NearObjectCliHandler
19+
{
20+
NearObjectCliHandler() = default;
21+
22+
virtual ~NearObjectCliHandler() = default;
23+
24+
/**
25+
* @brief Invoked by the command line driver when a UWB device is required
26+
* for the selected nocli operation. This function must resolve and return
27+
* an instance of the appropriate UWB device to use for the operation.
28+
*
29+
* @param cliData The parsed command-line arguments.
30+
* @return std::shared_ptr<uwb::UwbDevice>
31+
*/
32+
virtual std::shared_ptr<uwb::UwbDevice>
33+
ResolveUwbDevice(const nearobject::cli::NearObjectCliData& cliData) noexcept;
34+
35+
/**
36+
* @brief Invoked by command-line driver when the request is to start a
37+
* ranging session.
38+
*
39+
* @param uwbDevice The resolved uwb device to start the ranging session on.
40+
* @param sessionData The data to configure the ranging session.
41+
*/
42+
virtual void
43+
HandleStartRanging(std::shared_ptr<uwb::UwbDevice> uwbDevice, uwb::protocol::fira::UwbSessionData& sessionData) noexcept;
44+
45+
/**
46+
* @brief Invoked by the command-line driver when the request is to stop an ongoing ranging session.
47+
*
48+
* TODO: this will need to change to accept a session id. Or, prior state
49+
* saved may allow this to remain argumentless.
50+
*/
51+
virtual void
52+
HandleStopRanging() noexcept;
53+
};
54+
55+
} // namespace nearobject::cli
56+
57+
#endif // NEAR_OBJECT_CLI_HANDLER_HXX

0 commit comments

Comments
 (0)