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 <string>
24
25#include "deviceendpoint.h"
26
27#include "device.h"
28#include "deviceconfiguration.h"
29#include "rclip.h"
30#include "rfsv.h"
31#include "rpcs.h"
32#include "uuid.h"
33
34std::unique_ptr<DeviceEndpoint> DeviceEndpoint::connect(const std::string host,
35 int port,
37 Enum<ConnectionError> internalError = ConnectionError::FACERR_NONE;
38 auto rfsv = std::unique_ptr<RFSV>(RFSV::connect(host, port, &internalError));
39 if (!rfsv) {
40 if (error) {
41 *error = internalError;
42 }
43 return nullptr;
44 }
45
46 // Get the device configuration.
47 Enum<RFSV::errs> result;
48 auto deviceConfiguration = device::read_configuration(*rfsv, result);
49 std::string id = deviceConfiguration ? deviceConfiguration->id() : uuid::uuid4();
50 bool hasPersistentConfiguration = static_cast<bool>(deviceConfiguration);
51
52 auto rpcs = std::unique_ptr<RPCS>(RPCS::connect(host, port, &internalError));
53 if (!rpcs) {
54 if (error) {
55 *error = internalError;
56 }
57 return nullptr;
58 }
59
60 auto clip = std::unique_ptr<rclip>(rclip::connect(host, port, &internalError));
61 if (!clip) {
62 if (error) {
63 *error = internalError;
64 }
65 return nullptr;
66 }
67
68 return std::unique_ptr<DeviceEndpoint>(
69 new DeviceEndpoint(id, hasPersistentConfiguration, std::move(rfsv), std::move(rpcs), std::move(clip)));
70}
71
72DeviceEndpoint::DeviceEndpoint(const std::string &id,
73 bool hasPersistentConfiguration,
74 std::unique_ptr<RFSV> rfsv,
75 std::unique_ptr<RPCS> rpcs,
76 std::unique_ptr<rclip> clip)
77: rfsv_(std::move(rfsv))
78, rpcs_(std::move(rpcs))
79, clip_(std::move(clip))
80, id_(id)
81, hasPersistentConfiguration_(hasPersistentConfiguration) {}
82
83std::string DeviceEndpoint::id() const {
84 return id_;
85}
86
89}
90
91Enum<RFSV::errs> DeviceEndpoint::getName(std::string &name) const {
92
93 // Don't bother to fetch the configuration if we know it does't exist.
96 }
97
98 // Read the configuration.
100 auto deviceConfiguration = device::read_configuration(*rfsv_, error);
101
102 // Check for E_PSI_FILE_NXIST and return it as E_PSI_FILE_RECORD which seems more logical.
105 }
106
107 // Return any errors.
109 return error;
110 }
111
112 // Update the name and indicate success.
113 name = deviceConfiguration->name();
115}
116
118 auto deviceConfiguration = std::make_unique<DeviceConfiguration>(id_, name);
119 auto result = device::write_configuration(*rfsv_, *deviceConfiguration);
120 if (result == RFSV::E_PSI_GEN_NONE) {
122 }
123 return result;
124}
const std::unique_ptr< RFSV > rfsv_
DeviceEndpoint(const std::string &id, bool hasPersistentConfiguration, std::unique_ptr< RFSV > rfsv, std::unique_ptr< RPCS > rpcs, std::unique_ptr< rclip > clip)
bool hasPersistentConfiguration_
std::string id() const
Device identifier.
const std::string id_
bool hasPersistentId() const
Enum< RFSV::errs > setName(const std::string &name)
Set the device name.
static std::unique_ptr< DeviceEndpoint > connect(const std::string host, int port, Enum< ConnectionError > *error)
Enum< RFSV::errs > getName(std::string &name) const
Get the 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_FILE_NXIST
Definition: rfsv.h:139
@ E_PSI_FILE_RECORD
Definition: rfsv.h:149
@ 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
Definition: doctest.h:522
std::string uuid4()
Definition: uuid.cc:30
static void error(int line)
Definition: sismain.cpp:44