-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIoTransaction.h
125 lines (97 loc) · 3.13 KB
/
IoTransaction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef IO_TRANSACTION_H_
#define IO_TRANSACTION_H_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <Arduino.h>
#include <mbed.h>
#include <SharedPtr.h>
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
/**************************************************************************************
* IoRequest
**************************************************************************************/
class IoRequest
{
public:
IoRequest(byte * write_buf_, size_t const bytes_to_write_, byte * read_buf_, size_t const bytes_to_read_)
: write_buf{write_buf_}
, bytes_to_write{bytes_to_write_}
, read_buf{read_buf_}
, bytes_to_read{bytes_to_read_}
{ }
IoRequest(byte & write_buf_, byte & read_buf_)
: IoRequest{&write_buf_, 1, &read_buf_, 1}
{ }
byte * write_buf{nullptr};
size_t const bytes_to_write{0};
byte * read_buf{nullptr};
size_t const bytes_to_read{0};
};
/**************************************************************************************
* IoResponse
**************************************************************************************/
namespace impl
{
class IoResponse
{
public:
IoResponse()
: bytes_written{0}
, bytes_read{0}
, _cond{_mutex}
, _is_done{false}
{ }
size_t bytes_written{0};
size_t bytes_read{0};
void done()
{
_mutex.lock();
_is_done = true;
_cond.notify_all();
_mutex.unlock();
}
void wait()
{
_mutex.lock();
while (!_is_done) {
_cond.wait();
}
_mutex.unlock();
}
private:
rtos::Mutex _mutex;
rtos::ConditionVariable _cond;
bool _is_done{false};
};
} /* namespace impl */
typedef mbed::SharedPtr<impl::IoResponse> IoResponse;
/**************************************************************************************
* IoTransaction
**************************************************************************************/
class IoTransaction
{
public:
IoTransaction(IoRequest * q, IoResponse * p) : req{q}, rsp{p} { }
IoRequest * req{nullptr};
IoResponse * rsp{nullptr};
};
#endif /* IO_TRANSACTION_H_ */