PIDUINO
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 2.1 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 <cstdint>
20#include <string>
21#include <vector>
22#include <fstream>
23
31namespace Piduino {
32
33 class System {
34 public:
35
36 /*
37 processor : 0
38 model name : ARMv6-compatible processor rev 7 (v6l)
39 BogoMIPS : 2.00
40 Features : half thumb fastmult vfp edsp java tls
41 CPU implementer : 0x41
42 CPU architecture: 7
43 CPU variant : 0x0
44 CPU part : 0xb76
45 CPU revision : 7
46
47 CPU implementer: Your code means ARM;
48 CPU architecture: AArch64 means 64 bit ARM board:
49 CPU variant : Indicates the variant number of the processor, or "major revision". Yours is zero.
50 CPU part: Part number. 0xd03 indicates Cortex-A53 processor.
51 CPU revision: Indicates patch release or "minor revision". 3, in your case
52 */
53 // -----------------------------------------------------------------------
54 //
55 // System::ArmCore Class
56 //
57 // -----------------------------------------------------------------------
58 class ArmCore {
59 public:
60 ArmCore() : bogomips (0), implementer (0), archno (0), variant (0), revision (0), partno (0) {}
61 std::string model;
62 std::vector<std::string> features;
63 double bogomips;
65 int archno;
68 int partno;
69 };
70
71 // -----------------------------------------------------------------------
72 //
73 // System::ArmbianInfo Class
74 //
75 // -----------------------------------------------------------------------
77 public:
79
80 inline bool found() const {
81 return _valid;
82 }
83 inline const std::string &board() const {
84 return _board;
85 }
86 inline const std::string &boardName() const {
87 return _boardName;
88 }
89 inline const std::string &version() const {
90 return _version;
91 }
92 inline const std::string &family() const {
93 return _family;
94 }
95 inline const std::string &branch() const {
96 return _branch;
97 }
98 inline const std::string &arch() const {
99 return _arch;
100 }
101
102 private:
103 bool _valid;
104 std::string _board;
105 std::string _boardName;
106 std::string _version;
107 std::string _family;
108 std::string _branch;
109 std::string _arch;
110 };
111
112 // -----------------------------------------------------------------------
113 //
114 // System::RaspianInfo Class
115 //
116 // -----------------------------------------------------------------------
117 // This class is used to read the new revision codes found in /proc/cpuinfo
118 // and /proc/device-tree/system/linux,revision
119 // https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
121 public:
123
124 // Describe the board revision codes found in /proc/cpuinfo or /proc/device-tree/system/linux,revision
125 // RRRR (bits 0-3): Revision
127 // TTTTTTTT (bits 4-11)
128 RpiA = 0x00,
129 RpiB = 0x01,
130 RpiAPlus = 0x02,
131 RpiBPlus = 0x03,
132 Rpi2B = 0x04,
133 RpiAlpha = 0x05,
134 RpiCM1 = 0x06,
135 Rpi3B = 0x08,
136 RpiZero = 0x09,
137 RpiCM3 = 0x0a,
138 RpiZeroW = 0x0c,
139 Rpi3BPlus = 0x0d,
140 Rpi3APlus = 0x0e,
143 Rpi4B = 0x11,
144 RpiZero2W = 0x12,
145 Rpi400 = 0x13,
146 RpiCM4 = 0x14,
147 RpiCM4S = 0x15,
149 Rpi5 = 0x17,
150 RpiCM5 = 0x18,
151 Rpi500 = 0x19,
153 RpiUnknown = -1
154 };
155
157 // PPPP (bits 12-15)
158 BCM2835 = 0x00,
159 BCM2836 = 0x01,
160 BCM2837 = 0x02,
161 BCM2711 = 0x03,
162 BCM2712 = 0x04
163 };
164
166 // CCCC (bits 16-19)
167 SonyUK = 0x00,
168 Egoman = 0x01,
169 Embest = 0x02,
170 SonyJapan = 0x03,
171 Embest2 = 0x04,
172 Stadium = 0x05
173 };
174
176 // MMM (bits 20-22)
179 Memory1GB = 0x02,
180 Memory2GB = 0x03,
181 Memory4GB = 0x04,
182 Memory8GB = 0x05,
183 Memory16GB = 0x06
184 };
185
186 inline bool newFlag() const {
187 return _info.field.newrev;
188 }
189 inline bool found() const {
190 return newFlag();
191 }
192 inline uint8_t revision() const {
193 return _info.field.revision;
194 }
195 inline BoardType board() const {
196 return _info.field.board;
197 }
198 inline Processor processor() const {
199 return _info.field.processor;
200 }
201 inline Manufacturer manufacturer() const {
202 return _info.field.manufacturer;
203 }
204 inline MemorySize memory() const {
205 return _info.field.memory;
206 }
207 inline bool warranty() const {
208 return _info.field.warranty;
209 }
210 inline bool otpReady() const {
211 return _info.field.otpready;
212 }
213 inline bool otpProgram() const {
214 return _info.field.otpprogram;
215 }
216 inline bool overVoltage() const {
217 return _info.field.overvoltage;
218 }
219 inline uint32_t value() const {
220 return _info.value;
221 }
222
223 private:
225 uint8_t revision: 4;
230 bool newrev: 1;
231 bool unused: 1;
232 bool warranty: 1;
233 uint8_t unused2: 3;
234 bool otpready: 1;
235 bool otpprogram: 1;
236 bool overvoltage: 1;
237 };
238
239 union BoardInfo {
240 uint32_t value;
242 BoardInfo() : value (0) {}
243 BoardInfo (uint32_t v) : value (v) {}
244 BoardInfo (const BoardInfo &b) : value (b.value) {}
246 value = b.value;
247 return *this;
248 }
250 value = static_cast<uint32_t> (v);
251 return *this;
252 }
253 BoardInfo &operator= (unsigned long v) {
254 value = static_cast<uint32_t> (v);
255 return *this;
256 }
257 };
259 };
260
261 // -----------------------------------------------------------------------
262 //
263 // System Class
264 //
265 // -----------------------------------------------------------------------
268
269 // -----------------------------------------------------------------------
270 static bool fileExists (const char *path);
271 static inline bool fileExists (const std::string &p) {
272 return fileExists (p.c_str());
273 }
274 static bool charFileExists (const char *path);
275 static inline bool charFileExists (const std::string &p) {
276 return charFileExists (p.c_str());
277 }
278 static bool directoryExists (const char *path);
279 static inline bool directoryExists (const std::string &p) {
280 return fileExists (p.c_str());
281 }
282
283 static unsigned long readLinuxRevision();
284
285 static std::string progName();
286
287 void createPidFile (const char *path = nullptr);
288 inline void createPidFile (const std::string &path) {
289 createPidFile (path.c_str());
290 }
292
293 void close();
294
295 inline const std::string &hardware() const {
296 return _hardware;
297 }
298
299 inline unsigned long long serialNumber() const {
300 return _sn;
301 }
302
303 inline unsigned long revision() const {
304 return _revision;
305 }
306
307 inline unsigned long totalRam() const {
308 return _ram;
309 }
310
311 inline const ArmCore &core() const {
312 return _core;
313 }
314
315 inline const ArmbianInfo &armbianInfo() const {
316 return _armbian;
317 }
318
319 inline const RaspianInfo &raspianInfo() const {
320 return _raspian;
321 }
322 protected:
325
326 private:
327 std::string _hardware;
328 unsigned long _revision;
329 unsigned long long _sn;
330 long _ram; // RAM en Mo
334 std::string _pidfn;
336 };
337
338 // ---------------------------------------------------------------------------
339 //
340 // Piduino System Global Object
341 //
342 // ---------------------------------------------------------------------------
343 extern System system;
347}
352/* ========================================================================== */
std::vector< std::string > features
Definition: system.h:62
std::string model
Definition: system.h:61
const std::string & arch() const
Definition: system.h:98
const std::string & branch() const
Definition: system.h:95
const std::string & version() const
Definition: system.h:89
const std::string & family() const
Definition: system.h:92
const std::string & boardName() const
Definition: system.h:86
const std::string & board() const
Definition: system.h:83
MemorySize memory() const
Definition: system.h:204
Manufacturer manufacturer() const
Definition: system.h:201
BoardType board() const
Definition: system.h:195
bool otpProgram() const
Definition: system.h:213
Processor processor() const
Definition: system.h:198
bool overVoltage() const
Definition: system.h:216
uint32_t value() const
Definition: system.h:219
uint8_t revision() const
Definition: system.h:192
std::string _hardware
Definition: system.h:327
unsigned long _revision
Definition: system.h:328
void deletePidFile()
static bool charFileExists(const char *path)
const ArmbianInfo & armbianInfo() const
Definition: system.h:315
ArmCore _core
Definition: system.h:332
unsigned long revision() const
Definition: system.h:303
RaspianInfo _raspian
Definition: system.h:335
ArmbianInfo _armbian
Definition: system.h:333
void createPidFile(const std::string &path)
Definition: system.h:288
static bool fileExists(const char *path)
static bool fileExists(const std::string &p)
Definition: system.h:271
static bool charFileExists(const std::string &p)
Definition: system.h:275
const RaspianInfo & raspianInfo() const
Definition: system.h:319
std::string _pidfn
Definition: system.h:334
unsigned long long serialNumber() const
Definition: system.h:299
const ArmCore & core() const
Definition: system.h:311
const std::string & hardware() const
Definition: system.h:295
unsigned long long _sn
Definition: system.h:329
void createPidFile(const char *path=nullptr)
unsigned long totalRam() const
Definition: system.h:307
static unsigned long readLinuxRevision()
static bool directoryExists(const char *path)
static bool directoryExists(const std::string &p)
Definition: system.h:279
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:245