PIDUINO
Loading...
Searching...
No Matches
system.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <string>
20#include <vector>
21#include <fstream>
22
30namespace Piduino {
31
32 class System {
33 public:
34
35 /*
36 processor : 0
37 model name : ARMv6-compatible processor rev 7 (v6l)
38 BogoMIPS : 2.00
39 Features : half thumb fastmult vfp edsp java tls
40 CPU implementer : 0x41
41 CPU architecture: 7
42 CPU variant : 0x0
43 CPU part : 0xb76
44 CPU revision : 7
45
46 CPU implementer: Your code means ARM;
47 CPU architecture: AArch64 means 64 bit ARM board:
48 CPU variant : Indicates the variant number of the processor, or "major revision". Yours is zero.
49 CPU part: Part number. 0xd03 indicates Cortex-A53 processor.
50 CPU revision: Indicates patch release or "minor revision". 3, in your case
51 */
52 // -----------------------------------------------------------------------
53 //
54 // System::ArmCore Class
55 //
56 // -----------------------------------------------------------------------
57 class ArmCore {
58 public:
59 ArmCore() : bogomips (0), implementer (0), archno (0), variant (0), revision (0), partno (0) {}
60 std::string model;
61 std::vector<std::string> features;
62 double bogomips;
64 int archno;
67 int partno;
68 };
69
70 // -----------------------------------------------------------------------
71 //
72 // System::ArmbianInfo Class
73 //
74 // -----------------------------------------------------------------------
76 public:
78
79 inline bool found() const {
80 return _valid;
81 }
82 inline const std::string &board() const {
83 return _board;
84 }
85 inline const std::string &boardName() const {
86 return _boardName;
87 }
88 inline const std::string &version() const {
89 return _version;
90 }
91 inline const std::string &family() const {
92 return _family;
93 }
94 inline const std::string &branch() const {
95 return _branch;
96 }
97 inline const std::string &arch() const {
98 return _arch;
99 }
100
101 private:
102 bool _valid;
103 std::string _board;
104 std::string _boardName;
105 std::string _version;
106 std::string _family;
107 std::string _branch;
108 std::string _arch;
109 };
110
111 // -----------------------------------------------------------------------
112 //
113 // System::RaspianInfo Class
114 //
115 // -----------------------------------------------------------------------
116 // This class is used to read the new revision codes found in /proc/cpuinfo
117 // and /proc/device-tree/system/linux,revision
118 // https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
120 public:
122
123 // Describe the board revision codes found in /proc/cpuinfo or /proc/device-tree/system/linux,revision
124 // RRRR (bits 0-3): Revision
126 // TTTTTTTT (bits 4-11)
127 RpiA = 0x00,
128 RpiB = 0x01,
129 RpiAPlus = 0x02,
130 RpiBPlus = 0x03,
131 Rpi2B = 0x04,
132 RpiAlpha = 0x05,
133 RpiCM1 = 0x06,
134 Rpi3B = 0x08,
135 RpiZero = 0x09,
136 RpiCM3 = 0x0a,
137 RpiZeroW = 0x0c,
138 Rpi3BPlus = 0x0d,
139 Rpi3APlus = 0x0e,
142 Rpi4B = 0x11,
143 RpiZero2W = 0x12,
144 Rpi400 = 0x13,
145 RpiCM4 = 0x14,
146 RpiCM4S = 0x15,
148 Rpi5 = 0x17,
149 RpiCM5 = 0x18,
150 Rpi500 = 0x19,
152 RpiUnknown = -1
153 };
154
156 // PPPP (bits 12-15)
157 BCM2835 = 0x00,
158 BCM2836 = 0x01,
159 BCM2837 = 0x02,
160 BCM2711 = 0x03,
161 BCM2712 = 0x04
162 };
163
165 // CCCC (bits 16-19)
166 SonyUK = 0x00,
167 Egoman = 0x01,
168 Embest = 0x02,
169 SonyJapan = 0x03,
170 Embest2 = 0x04,
171 Stadium = 0x05
172 };
173
175 // MMM (bits 20-22)
178 Memory1GB = 0x02,
179 Memory2GB = 0x03,
180 Memory4GB = 0x04,
181 Memory8GB = 0x05,
182 Memory16GB = 0x06
183 };
184
185 inline bool newFlag() const {
186 return _info.field.newrev;
187 }
188 inline bool found() const {
189 return newFlag();
190 }
191 inline uint8_t revision() const {
192 return _info.field.revision;
193 }
194 inline BoardType board() const {
195 return _info.field.board;
196 }
197 inline Processor processor() const {
198 return _info.field.processor;
199 }
200 inline Manufacturer manufacturer() const {
201 return _info.field.manufacturer;
202 }
203 inline MemorySize memory() const {
204 return _info.field.memory;
205 }
206 inline bool warranty() const {
207 return _info.field.warranty;
208 }
209 inline bool otpReady() const {
210 return _info.field.otpready;
211 }
212 inline bool otpProgram() const {
213 return _info.field.otpprogram;
214 }
215 inline bool overVoltage() const {
216 return _info.field.overvoltage;
217 }
218 inline uint32_t value() const {
219 return _info.value;
220 }
221
222 private:
237
238 union BoardInfo {
239 uint32_t value;
241 BoardInfo() : value (0) {}
242 BoardInfo (uint32_t v) : value (v) {}
243 BoardInfo (const BoardInfo &b) : value (b.value) {}
245 value = b.value;
246 return *this;
247 }
249 value = static_cast<uint32_t> (v);
250 return *this;
251 }
252 BoardInfo &operator= (unsigned long v) {
253 value = static_cast<uint32_t> (v);
254 return *this;
255 }
256 };
258 };
259
260 // -----------------------------------------------------------------------
261 //
262 // System Class
263 //
264 // -----------------------------------------------------------------------
267
268 // -----------------------------------------------------------------------
269 static bool fileExists (const char *path);
270 static inline bool fileExists (const std::string &p) {
271 return fileExists (p.c_str());
272 }
273 static bool charFileExists (const char *path);
274 static inline bool charFileExists (const std::string &p) {
275 return charFileExists (p.c_str());
276 }
277 static bool directoryExists (const char *path);
278 static inline bool directoryExists (const std::string &p) {
279 return fileExists (p.c_str());
280 }
281
282 static unsigned long readLinuxRevision();
283
284 static std::string progName();
285
286 void createPidFile (const char *path = nullptr);
287 inline void createPidFile (const std::string &path) {
288 createPidFile (path.c_str());
289 }
291
292 void close();
293
294 inline const std::string &hardware() const {
295 return _hardware;
296 }
297
298 inline unsigned long long serialNumber() const {
299 return _sn;
300 }
301
302 inline unsigned long revision() const {
303 return _revision;
304 }
305
306 inline unsigned long totalRam() const {
307 return _ram;
308 }
309
310 inline const ArmCore &core() const {
311 return _core;
312 }
313
314 inline const ArmbianInfo &armbianInfo() const {
315 return _armbian;
316 }
317
318 inline const RaspianInfo &raspianInfo() const {
319 return _raspian;
320 }
321 protected:
324
325 private:
326 std::string _hardware;
327 unsigned long _revision;
328 unsigned long long _sn;
329 long _ram; // RAM en Mo
333 std::string _pidfn;
335 };
336
337 // ---------------------------------------------------------------------------
338 //
339 // Piduino System Global Object
340 //
341 // ---------------------------------------------------------------------------
342 extern System system;
346}
351/* ========================================================================== */
std::vector< std::string > features
Definition system.h:61
std::string model
Definition system.h:60
const std::string & arch() const
Definition system.h:97
const std::string & branch() const
Definition system.h:94
const std::string & version() const
Definition system.h:88
const std::string & family() const
Definition system.h:91
const std::string & boardName() const
Definition system.h:85
const std::string & board() const
Definition system.h:82
MemorySize memory() const
Definition system.h:203
Manufacturer manufacturer() const
Definition system.h:200
BoardType board() const
Definition system.h:194
Processor processor() const
Definition system.h:197
uint32_t value() const
Definition system.h:218
uint8_t revision() const
Definition system.h:191
std::string _hardware
Definition system.h:326
unsigned long _revision
Definition system.h:327
void deletePidFile()
static bool charFileExists(const char *path)
const ArmbianInfo & armbianInfo() const
Definition system.h:314
ArmCore _core
Definition system.h:331
unsigned long revision() const
Definition system.h:302
RaspianInfo _raspian
Definition system.h:334
ArmbianInfo _armbian
Definition system.h:332
void createPidFile(const std::string &path)
Definition system.h:287
static bool fileExists(const char *path)
static bool fileExists(const std::string &p)
Definition system.h:270
static bool charFileExists(const std::string &p)
Definition system.h:274
const RaspianInfo & raspianInfo() const
Definition system.h:318
std::string _pidfn
Definition system.h:333
unsigned long long serialNumber() const
Definition system.h:298
const ArmCore & core() const
Definition system.h:310
const std::string & hardware() const
Definition system.h:294
unsigned long long _sn
Definition system.h:328
void createPidFile(const char *path=nullptr)
unsigned long totalRam() const
Definition system.h:306
static unsigned long readLinuxRevision()
static bool directoryExists(const char *path)
static bool directoryExists(const std::string &p)
Definition system.h:278
static std::string progName()
Global namespace for Piduino.
Definition board.h:28
System system
Piduino System Global Object.
BoardInfo & operator=(const BoardInfo &b)
Definition system.h:244