plptools
Loading...
Searching...
No Matches
device.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2002 Fritz Elfert <felfert@to.com>
6 * Copyright (C) 2006-2025 Reuben Thomas <rrt@sc3d.org>
7 * Copyright (C) 2026 Jason Morley <hello@jbmorley.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "config.h"
25
26#include "device.h"
27
28#include <cstdint>
29#include <memory>
30
31#include "deviceconfiguration.h"
32#include "pathutils.h"
33#include "rfsv.h"
34
36 std::string configurationPath = rfsv.deviceConfigurationPath();
37
39
40 // Ensure the directory exists by trying to create it and ignoring exists errors.
41 std::string directoryPath = pathutils::epoc_dirname(configurationPath.c_str());
42 error = rfsv.mkdir(directoryPath.c_str());
44 return error;
45 }
46
47 // Write the file.
48 uint32_t handle;
49 uint32_t mode = rfsv.opMode(RFSV::PSI_O_WRONLY);
50 error = rfsv.fcreatefile(mode, configurationPath.c_str(), handle);
53 return error;
54 }
55 error = rfsv.freplacefile(mode, configurationPath.c_str(), handle);
57 return error;
58 }
59 }
60
61 std::string contents = deviceConfiguration.serialize();
62 uint32_t count = 0;
63 error = rfsv.fwrite(handle, reinterpret_cast<const unsigned char *>(contents.data()), contents.length(), count);
65 rfsv.fclose(handle);
66 return error;
67 }
68
69 return rfsv.fclose(handle);
70}
71
72std::unique_ptr<DeviceConfiguration> device::read_configuration(RFSV &rfsv, Enum<RFSV::errs> &error) {
73 std::string configurationPath = rfsv.deviceConfigurationPath();
74 uint32_t handle;
75
76 // Get the file size.
77 PlpDirent dirent;
78 error = rfsv.fgeteattr(configurationPath.c_str(), dirent);
80 return nullptr;
81 }
82
83 // Open the file.
84 error = rfsv.fopen(RFSV::PSI_O_RDONLY, configurationPath.c_str(), handle);
86 return nullptr;
87 }
88
89 // Read the contents.
90 uint32_t size = dirent.getSize();
91 std::vector<unsigned char> buffer(size);
92 uint32_t count;
93 error = rfsv.fread(handle, buffer.data(), size, count);
95 rfsv.fclose(handle);
96 return nullptr;
97 }
98
99 // Close the file.
100 error = rfsv.fclose(handle);
102 return nullptr;
103 }
104
105 // Parse the contents.
106 std::string contents(buffer.begin(), buffer.end());
107 auto configuration = DeviceConfiguration::deserialize(contents);
108 if (!configuration) {
110 return nullptr;
111 }
112
113 return configuration;
114}
Class for managing and serializing device details.
static std::unique_ptr< DeviceConfiguration > deserialize(const std::string &contents)
std::string serialize() const
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:135
A class, representing a directory entry of the Psion.
Definition: plpdirent.h:79
uint32_t getSize() const
Retrieves the file size of a directory entry.
Definition: plpdirent.cc:71
Access remote file services of a Psion.
Definition: rfsv.h:79
@ PSI_O_WRONLY
Definition: rfsv.h:95
@ PSI_O_RDONLY
Definition: rfsv.h:94
virtual std::string deviceConfigurationPath()=0
virtual Enum< errs > fwrite(const uint32_t handle, const unsigned char *const buffer, const uint32_t len, uint32_t &count)=0
Write to a file on the Psion.
virtual uint32_t opMode(const uint32_t mode)=0
Converts an open-mode (A combination of the PSI_O_ constants.) from generic representation to the mac...
virtual Enum< errs > fopen(const uint32_t attr, const char *const name, uint32_t &handle)=0
Opens a file.
virtual Enum< errs > fread(const uint32_t handle, unsigned char *const buffer, const uint32_t len, uint32_t &count)=0
Reads from a file on the Psion.
virtual Enum< errs > fgeteattr(const char *const name, PlpDirent &e)=0
Retrieves attributes, size and modification time of a file on the Psion.
virtual Enum< errs > fcreatefile(const uint32_t attr, const char *const name, uint32_t &handle)=0
Creates a named file.
virtual Enum< errs > mkdir(const char *const name)=0
Creates a directory on the Psion.
virtual Enum< errs > fclose(const uint32_t handle)=0
Close a file on the Psion whih was previously opened/created by using fopen , fcreatefile ,...
virtual Enum< errs > freplacefile(const uint32_t attr, const char *const name, uint32_t &handle)=0
Creates an named file, overwriting an existing file.
@ E_PSI_FILE_EXIST
Definition: rfsv.h:138
@ E_PSI_FILE_CORRUPT
Definition: rfsv.h:172
@ E_PSI_GEN_NONE
Definition: rfsv.h:114
Enum< RFSV::errs > write_configuration(RFSV &rfsv, const DeviceConfiguration &deviceConfiguration)
Definition: device.cc:35
std::unique_ptr< DeviceConfiguration > read_configuration(RFSV &rfsv, Enum< RFSV::errs > &error)
Definition: device.cc:72
char * epoc_dirname(const char *path)
Compute parent directory of an EPOC directory.
Definition: pathutils.cc:91
static void error(int line)
Definition: sismain.cpp:44