µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2018 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_POSIX_IO_BLOCK_H_
29#define CMSIS_PLUS_POSIX_IO_BLOCK_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
37#if defined(OS_USE_OS_APP_CONFIG_H)
38#include <cmsis-plus/os-app-config.h>
39#endif
40
43#include <mutex>
44
45// ----------------------------------------------------------------------------
46
47#if ! defined(OS_STRING_POSIX_DEVICE_PREFIX)
48#define OS_STRING_POSIX_DEVICE_PREFIX "/dev/"
49#endif
50
51// ----------------------------------------------------------------------------
52
53#pragma GCC diagnostic push
54
55#if defined(__clang__)
56#pragma clang diagnostic ignored "-Wc++98-compat"
57#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
58#endif
59
60// ----------------------------------------------------------------------------
61
62namespace os
63{
64 namespace posix
65 {
66 // ------------------------------------------------------------------------
67
68 class device_impl;
69
70 // ========================================================================
71
77 class device : public io
78 {
79 // ----------------------------------------------------------------------
80
86 public:
87
88 device (device_impl& impl, type t, const char* name);
89
94 // The rule of five.
95 device (const device&) = delete;
96 device (device&&) = delete;
97 device&
98 operator= (const device&) = delete;
99 device&
100 operator= (device&&) = delete;
101
106 virtual
107 ~device () override;
108
113 // ----------------------------------------------------------------------
119 public:
120
126 int
127 open (const char* path = nullptr, int oflag = 0, ...);
128
135 int
136 vopen (const char* path, int oflag, std::va_list args);
137
138 virtual int
139 close (void) override;
140
141 int
142 ioctl (int request, ...);
143
144 virtual int
145 vioctl (int request, std::va_list args);
146
147 virtual void
148 sync (void);
149
150 // ----------------------------------------------------------------------
151
152 virtual bool
153 match_name (const char* name) const;
154
155 const char*
156 name (void) const;
157
158 static const char*
159 device_prefix (void);
160
161 // ----------------------------------------------------------------------
162 // Support functions.
163
165 impl (void) const;
166
171 // ----------------------------------------------------------------------
172 protected:
173
178 const char* name_ = nullptr;
179
184 // ----------------------------------------------------------------------
185 public:
186
191 // Intrusive node used to link this device to the registry list.
192 // Must be public.
193 utils::double_list_links registry_links_;
194
198 };
199
200 // ========================================================================
201
202#pragma GCC diagnostic push
203#pragma GCC diagnostic ignored "-Wpadded"
204
205 class device_impl : public io_impl
206 {
207 // ----------------------------------------------------------------------
208
213 friend class device;
214
219 // ----------------------------------------------------------------------
225 public:
226
227 device_impl (void);
228
233 // The rule of five.
234 device_impl (const device_impl&) = delete;
235 device_impl (device_impl&&) = delete;
237 operator= (const device_impl&) = delete;
239 operator= (device_impl&&) = delete;
240
245 virtual
246 ~device_impl () override;
247
252 // ----------------------------------------------------------------------
258 public:
259
260 virtual bool
261 do_is_opened (void) override;
262
263 virtual int
264 do_vopen (const char* path, int oflag, std::va_list args) = 0;
265
266 virtual int
267 do_vioctl (int request, std::va_list args) = 0;
268
269 virtual void
270 do_sync (void) = 0;
271
272 // ----------------------------------------------------------------------
273
274 int
275 open_count (void);
276
281 // ----------------------------------------------------------------------
282 protected:
283
288 int open_count_ = 0;
289
293 };
294
295#pragma GCC diagnostic pop
296
297 // ==========================================================================
298 } /* namespace posix */
299} /* namespace os */
300
301// ===== Inline & template implementations ====================================
302
303namespace os
304{
305 namespace posix
306 {
307 // ========================================================================
308
309 inline const char*
310 device::name (void) const
311 {
312 return name_;
313 }
314
315 inline device_impl&
316 device::impl (void) const
317 {
318 return static_cast<device_impl&> (impl_);
319 }
320
321 inline const char*
323 {
325 }
326
327 // ========================================================================
328
329 inline int
331 {
332 return open_count_;
333 }
334
335 // ==========================================================================
336 } /* namespace posix */
337} /* namespace os */
338
339#pragma GCC diagnostic pop
340
341// ----------------------------------------------------------------------------
342
343#endif /* __cplusplus */
344
345// ----------------------------------------------------------------------------
346
347#endif /* CMSIS_PLUS_POSIX_IO_BLOCK_H_ */
virtual int do_vioctl(int request, std::va_list args)=0
virtual ~device_impl() override
Definition device.cpp:214
virtual bool do_is_opened(void) override
Definition device.cpp:224
int open_count(void)
Definition device.h:330
virtual void do_sync(void)=0
virtual int do_vopen(const char *path, int oflag, std::va_list args)=0
Base device class.
Definition device.h:78
device_impl & impl(void) const
Definition device.h:316
static const char * device_prefix(void)
Definition device.h:322
virtual int close(void) override
Definition device.cpp:125
virtual bool match_name(const char *name) const
Definition device.cpp:197
int vopen(const char *path, int oflag, std::va_list args)
Definition device.cpp:87
virtual int vioctl(int request, std::va_list args)
Definition device.cpp:161
int ioctl(int request,...)
Definition device.cpp:149
virtual ~device() override
Definition device.cpp:61
int open(const char *path=nullptr, int oflag=0,...)
Definition device.cpp:75
virtual void sync(void)
Definition device.cpp:179
const char * name(void) const
Definition device.h:310
Base I/O class.
Definition io.h:98
#define OS_STRING_POSIX_DEVICE_PREFIX
Definition device.h:48
System namespace.