plptools
Loading...
Searching...
No Matches
deviceendpoint.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (c) 2026 Jason Morley <hello@jbmorley.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20#include "config.h"
21
22#include <memory>
23#include <optional>
24#include <string>
25
26#include "deviceendpoint.h"
27
28#include "device.h"
29#include "deviceconfiguration.h"
30#include "rclip.h"
31#include "rfsv.h"
32#include "rpcs.h"
33#include "uuid.h"
34
35std::unique_ptr<DeviceEndpoint> DeviceEndpoint::connect(const std::string host,
36 int port,
39 auto rfsv = std::unique_ptr<RFSV>(RFSV::connect(host, port, &internalError));
40 if (!rfsv) {
41 if (error) {
42 *error = internalError;
43 }
44 return nullptr;
45 }
46
47 // Get the device configuration.
48 Enum<RFSV::errs> result;
49 auto deviceConfiguration = device::read_configuration(*rfsv, result);
50 std::string id = deviceConfiguration ? deviceConfiguration->id() : uuid::uuid4();
51 auto name = deviceConfiguration ? std::optional<std::string>(deviceConfiguration->name()) : std::nullopt;
52 bool hasPersistentConfiguration = static_cast<bool>(deviceConfiguration);
53
54 auto rpcs = std::unique_ptr<RPCS>(RPCS::connect(host, port, &internalError));
55 if (!rpcs) {
56 if (error) {
57 *error = internalError;
58 }
59 return nullptr;
60 }
61
62 auto clip = std::unique_ptr<rclip>(rclip::connect(host, port, &internalError));
63 if (!clip) {
64 if (error) {
65 *error = internalError;
66 }
67 return nullptr;
68 }
69
70 return std::unique_ptr<DeviceEndpoint>(
71 new DeviceEndpoint(id,
72 name,
73 hasPersistentConfiguration,
74 std::move(rfsv),
75 std::move(rpcs),
76 std::move(clip)));
77}
78
79DeviceEndpoint::DeviceEndpoint(const std::string &id,
80 const std::optional<std::string> &name,
81 bool hasPersistentConfiguration,
82 std::unique_ptr<RFSV> rfsv,
83 std::unique_ptr<RPCS> rpcs,
84 std::unique_ptr<rclip> clip)
85: rfsv_(std::move(rfsv))
86, rpcs_(std::move(rpcs))
87, clip_(std::move(clip))
88, id_(id)
89, name_(name)
90, hasPersistentConfiguration_(hasPersistentConfiguration) {
91}
92
93std::string DeviceEndpoint::id() const {
94 return id_;
95}
96
100
101std::optional<std::string> DeviceEndpoint::name() const {
102 return name_;
103}
104
106 auto deviceConfiguration = std::make_unique<DeviceConfiguration>(id_, name);
107 auto result = device::write_configuration(*rfsv_, *deviceConfiguration);
108 if (result == RFSV::E_PSI_GEN_NONE) {
109 name_ = name;
111 }
112 return result;
113}
const std::unique_ptr< RFSV > rfsv_
bool hasPersistentConfiguration_
std::string id() const
Device identifier.
std::optional< std::string > name_
const std::string id_
bool hasPersistentId() const
Enum< RFSV::errs > setName(const std::string &name)
Set the device name.
DeviceEndpoint(const std::string &id, const std::optional< std::string > &name, bool hasPersistentConfiguration, std::unique_ptr< RFSV > rfsv, std::unique_ptr< RPCS > rpcs, std::unique_ptr< rclip > clip)
static std::unique_ptr< DeviceEndpoint > connect(const std::string host, int port, Enum< ConnectionError > *error)
std::optional< std::string > name() const
Device name.
Wrapper class featuring range-checking and string representation of enumerated values.
Definition Enum.h:135
static RFSV * connect(const std::string &host, int port, Enum< ConnectionError > *error)
Definition rfsv.cc:115
@ E_PSI_GEN_NONE
Definition rfsv.h:114
static RPCS * connect(const std::string &host, int port, Enum< ConnectionError > *error)
Definition rpcs.cc:93
static rclip * connect(const std::string &host, int port, Enum< ConnectionError > *error=nullptr)
Definition rclip.cc:33
@ FACERR_NONE
Definition doctest.h:522
std::string uuid4()
Definition uuid.cc:30
static void error(int line)
Definition sismain.cpp:44