-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CIOServer.cpp
184 lines (153 loc) · 3.13 KB
/
I2CIOServer.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* (c)2017 CDP Technologies AS
*/
#include "I2CIOServer.h"
#include "I2CAdapter.h"
#include "I2CDevice.h"
#include "I2CException.h"
#include "I2CHelpers.h"
#include <IO/ServerIO/ChannelManager.h>
#include <OSAPI/Process/OSAPIThread.h>
using namespace CDP::StudioAPI;
using namespace std;
// The Thread provides std::thread like interface for OSAPIThread
class Thread : public OSAPIThread
{
public:
Thread(const function<void()>& function)
: m_function(function)
{
}
protected:
void Main() override
{
m_function();
}
private:
function<void()> m_function;
};
class I2CIOServer::Private
{
public:
Private(I2CIOServer* qq)
: channelManager(new ServerIO::ChannelManager(qq))
, worker(bind(&Private::Process, this))
{
}
void Initialize()
{
TryOpenAdapter();
}
void TryOpenAdapter()
{
try
{
i2cAdapter.Open(adapter);
}
catch (const I2CException& e)
{
MessageLine("I2CIOServer: Cannot open adapter %s: %s", adapter.c_str(), e.what());
}
}
void Process()
{
while (!worker.Stopped())
{
semaphore.Wait();
semaphore.Reset();
if (i2cAdapter.IsOpen())
{
channelManager->SynchronizeValuesIn();
for (auto device : devices)
device->Process(i2cAdapter);
channelManager->SynchronizeValuesOut();
}
}
}
void Dispose()
{
TryCloseAdapter();
}
void TryCloseAdapter()
{
try
{
i2cAdapter.Close();
}
catch (const I2CException& e)
{
MessageLine("I2CIOServer: Cannot close adapter %s: %s", adapter.c_str(), e.what());
}
}
unique_ptr<ServerIO::ChannelManager> channelManager;
vector<I2CDevice*> devices;
CDPProperty<string> adapter;
I2CAdapter i2cAdapter;
Thread worker;
OSAPIEvent semaphore;
};
I2CIOServer::I2CIOServer()
: d(new Private(this))
{
}
I2CIOServer::~I2CIOServer()
{
for (auto device : d->devices)
delete device;
}
void I2CIOServer::Create(const char* fullName)
{
IOServer::Create(fullName);
d->adapter.Create("Adapter", this, CDPPropertyBase::e_Attribute);
}
void I2CIOServer::ProcessNull()
{
IOServer::ProcessNull();
d->semaphore.Set();
}
void I2CIOServer::ProcessOffline()
{
IOServer::ProcessOffline();
d->semaphore.Set();
}
void I2CIOServer::ProcessOnline()
{
IOServer::ProcessOnline();
d->semaphore.Set();
}
bool I2CIOServer::IsCommProblem()
{
return !d->i2cAdapter.IsOpen();
}
void I2CIOServer::Activate()
{
d->Initialize();
d->worker.Start(CDPTHREAD_PRIORITY_HIGH, ShortName());
IOServer::Activate();
}
void I2CIOServer::Suspend()
{
IOServer::Suspend();
d->worker.Stop();
d->semaphore.Set();
}
void I2CIOServer::FillNodeChildren(NodeStream& serializer) const
{
IOServer::FillNodeChildren(serializer);
serializer.StdContainerStreamer(d->devices);
}
string I2CIOServer::GetNodeTypeName() const
{
return "I2CIOServer";
}
bool I2CIOServer::HandleXMLElement(XMLElementEx* pEx)
{
if (pEx->GetName() == "Device")
{
auto device = new I2CDevice;
device->Configure(pEx, this, d->channelManager.get());
d->devices.push_back(device);
return true;
}
return IOServer::HandleXMLElement(pEx);
}