forked from PX4/DriverFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevMgr.cpp
More file actions
354 lines (280 loc) · 8.2 KB
/
DevMgr.cpp
File metadata and controls
354 lines (280 loc) · 8.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**********************************************************************
* Copyright (c) 2015 Mark Charlebois
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the
* disclaimer below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Dronecode Project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
* GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#include <stdio.h>
#include <string.h>
#include "DriverFramework.hpp"
#include "DFList.hpp"
#include "SyncObj.hpp"
#include "DevObj.hpp"
#include "DevMgr.hpp"
#include <stdlib.h>
#include <errno.h>
using namespace DriverFramework;
static SyncObj *g_lock_dev_mgr = nullptr;
bool DevMgr::m_initialized = false;
#define NO_VERIFY 1 // Use fast method to get DevObj
// TODO add missing locking and reimplement with map
// This is meant just to work for now. It hs not been optimized yet.
static DFPointerList g_driver_list;
int DevMgr::initialize()
{
g_lock_dev_mgr = new SyncObj();
if (g_lock_dev_mgr == nullptr) {
return -3;
}
g_lock_dev_mgr->lock();
m_initialized = true;
g_lock_dev_mgr->unlock();
return 0;
}
void DevMgr::finalize()
{
g_lock_dev_mgr->lock();
m_initialized = false;
g_driver_list.clear();
g_lock_dev_mgr->unlock();
delete g_lock_dev_mgr;
g_lock_dev_mgr = nullptr;
}
#if DRIVER_MAX_INSTANCES > 9
#error must redo algorithm below
#endif
int DevMgr::registerDriver(DevObj *obj)
{
DF_LOG_DEBUG("DevMgr::registerDriver %s", obj->m_name);
if (obj->m_dev_path == nullptr) {
return -2;
}
bool registered = false;
g_lock_dev_mgr->lock();
int ret = 0;
if (obj->m_dev_class_path != nullptr) {
for (unsigned int i = 0; i < DRIVER_MAX_INSTANCES; i++) {
bool found = false;
char tmp_path[strlen(obj->m_dev_class_path) + 3];
snprintf(tmp_path, sizeof(tmp_path), "%s%d", obj->m_dev_class_path, i);
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
if ((list_obj->m_dev_instance_path) && (strcmp(tmp_path, list_obj->m_dev_instance_path) == 0)) {
found = true;
break;
}
idx = g_driver_list.next(idx);
}
if (!found) {
if (obj->m_dev_instance_path) {
free(obj->m_dev_instance_path);
obj->m_dev_instance_path = nullptr;
}
obj->m_dev_instance_path = strdup(tmp_path);
g_driver_list.pushBack(obj);
DF_LOG_DEBUG("Added driver %p %s", obj, obj->m_dev_instance_path);
registered = true;
break;
}
}
if (!registered) {
// Error - no available dev class instance
ret = -3;
}
} else {
// Some drivers do not specify a base class, or hardcode a specific instance
g_driver_list.pushBack(obj);
DF_LOG_DEBUG("Added driver %p %s", obj, obj->m_dev_path);
}
g_lock_dev_mgr->unlock();
return ret;
}
void DevMgr::unregisterDriver(DevObj *obj)
{
DF_LOG_DEBUG("DevMgr::unregisterDriver %s", obj->m_name);
if (!g_lock_dev_mgr) {
return;
}
g_lock_dev_mgr->lock();
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
if (list_obj == obj) {
g_driver_list.erase(idx);
break;
}
idx = g_driver_list.next(idx);
}
g_lock_dev_mgr->unlock();
}
DevObj *DevMgr::getDevObjByID(union DeviceId id)
{
g_lock_dev_mgr->lock();
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
if (list_obj->getId().dev_id == id.dev_id) {
break;
}
idx = g_driver_list.next(idx);
}
g_lock_dev_mgr->unlock();
return (idx != nullptr) ? reinterpret_cast<DevObj *>(g_driver_list.get(idx)) : nullptr;
}
DevObj *DevMgr::_getDevObjByHandle(DevHandle &h)
{
g_lock_dev_mgr->lock();
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
if (h.m_handle == list_obj) {
break;
}
idx = g_driver_list.next(idx);
}
g_lock_dev_mgr->unlock();
return (idx == nullptr) ? nullptr : reinterpret_cast<DevObj *>(g_driver_list.get(idx));
}
void DevMgr::getHandle(const char *dev_path, DevHandle &h)
{
if (dev_path == nullptr) {
h.m_errno = EINVAL;
return;
}
h.m_errno = EBADF;
g_lock_dev_mgr->lock();
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
// The dev path may be a class instance path or a dev name
if ((strcmp(dev_path, list_obj->m_dev_path) == 0) ||
(list_obj->m_dev_instance_path && (strcmp(dev_path, list_obj->m_dev_instance_path) == 0))) {
// Device is registered
g_lock_dev_mgr->unlock();
list_obj->addHandle(h);
g_lock_dev_mgr->lock();
h.m_handle = list_obj;
h.m_errno = 0;
break;
}
idx = g_driver_list.next(idx);
}
g_lock_dev_mgr->unlock();
}
void DevMgr::releaseHandle(DevHandle &h)
{
DevObj *driver = DevMgr::getDevObjByHandle<DevObj>(h);
if (driver) {
driver->removeHandle(h);
}
g_lock_dev_mgr->lock();
h.m_handle = nullptr;
h.m_errno = 0;
g_lock_dev_mgr->unlock();
}
void DevMgr::setDevHandleError(DevHandle &h, int error)
{
h.m_errno = error;
}
int DevMgr::getNextDeviceName(unsigned int &index, const char **instancename)
{
unsigned int i = 0;
int ret = -1;
g_lock_dev_mgr->lock();
// First go through actual dev names
DFPointerList::Index idx = nullptr;
idx = g_driver_list.next(idx);
while (idx != nullptr) {
if (i == index) {
DevObj *list_obj = reinterpret_cast<DevObj *>(g_driver_list.get(idx));
*instancename = list_obj->m_dev_instance_path;
index += 1;
ret = 0;
break;
}
idx = g_driver_list.next(idx);
++i;
}
g_lock_dev_mgr->unlock();
return ret;
}
//------------------------------------------------------------------------
// DevHandle
//------------------------------------------------------------------------
DevHandle::~DevHandle()
{
if (isValid()) {
DevMgr::releaseHandle(*this);
}
}
#ifndef __DF_NUTTX
int DevHandle::ioctl(unsigned long cmd, unsigned long arg)
{
if (m_handle) {
return reinterpret_cast<DevObj *>(m_handle)->devIOCTL(cmd, arg);
}
return -1;
}
ssize_t DevHandle::read(void *buf, size_t len)
{
if (m_handle) {
return reinterpret_cast<DevObj *>(m_handle)->devRead(buf, len);
}
return -1;
}
ssize_t DevHandle::write(const void *buf, size_t len)
{
if (m_handle) {
return reinterpret_cast<DevObj *>(m_handle)->devWrite(buf, len);
}
return -1;
}
#else
int DevHandle::ioctl(unsigned long cmd, unsigned long arg)
{
return ::ioctl(m_fd, cmd, (void *)arg);
}
ssize_t DevHandle::read(void *buf, size_t len)
{
return ::read(m_fd, buf, len);
}
ssize_t DevHandle::write(const void *buf, size_t len)
{
return ::write(m_fd, buf, len);
}
#endif