PX4 Firmware
PX4 Autopilot Software http://px4.io
cdev_platform.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 Mark Charlebois. All rights reserved.
4  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 #include "cdev_platform.hpp"
36 
37 #include <string>
38 #include <map>
39 
40 #include "vfile.h"
41 #include "../CDev.hpp"
42 
43 #include <px4_platform_common/log.h>
44 #include <px4_platform_common/posix.h>
45 #include <px4_platform_common/time.h>
46 
47 #include "DevMgr.hpp"
48 
49 using namespace std;
50 using namespace DriverFramework;
51 
53 
54 pthread_mutex_t devmutex = PTHREAD_MUTEX_INITIALIZER;
55 pthread_mutex_t filemutex = PTHREAD_MUTEX_INITIALIZER;
56 
57 #define PX4_MAX_FD 350
58 static map<string, void *> devmap;
60 
61 extern "C" {
62 
63  int px4_errno;
64 
65  static cdev::CDev *getDev(const char *path)
66  {
67  PX4_DEBUG("CDev::getDev");
68 
69  pthread_mutex_lock(&devmutex);
70 
71  auto item = devmap.find(path);
72 
73  if (item != devmap.end()) {
74  pthread_mutex_unlock(&devmutex);
75  return (cdev::CDev *)item->second;
76  }
77 
78  pthread_mutex_unlock(&devmutex);
79 
80  return nullptr;
81  }
82 
83  static cdev::CDev *get_vdev(int fd)
84  {
85  pthread_mutex_lock(&filemutex);
86  bool valid = (fd < PX4_MAX_FD && fd >= 0 && filemap[fd].vdev);
87  cdev::CDev *dev;
88 
89  if (valid) {
90  dev = (cdev::CDev *)(filemap[fd].vdev);
91 
92  } else {
93  dev = nullptr;
94  }
95 
96  pthread_mutex_unlock(&filemutex);
97  return dev;
98  }
99 
101  {
102  PX4_DEBUG("CDev::register_driver %s", name);
103  int ret = 0;
104 
105  if (name == nullptr || data == nullptr) {
106  return -EINVAL;
107  }
108 
109  pthread_mutex_lock(&devmutex);
110 
111  // Make sure the device does not already exist
112  auto item = devmap.find(name);
113 
114  if (item != devmap.end()) {
115  pthread_mutex_unlock(&devmutex);
116  return -EEXIST;
117  }
118 
119  devmap[name] = (void *)data;
120  PX4_DEBUG("Registered DEV %s", name);
121 
122  pthread_mutex_unlock(&devmutex);
123 
124  return ret;
125  }
126 
127  int unregister_driver(const char *name)
128  {
129  PX4_DEBUG("CDev::unregister_driver %s", name);
130  int ret = -EINVAL;
131 
132  if (name == nullptr) {
133  return -EINVAL;
134  }
135 
136  pthread_mutex_lock(&devmutex);
137 
138  if (devmap.erase(name) > 0) {
139  PX4_DEBUG("Unregistered DEV %s", name);
140  ret = 0;
141  }
142 
143  pthread_mutex_unlock(&devmutex);
144 
145  return ret;
146  }
147 
148  int px4_open(const char *path, int flags, ...)
149  {
150  PX4_DEBUG("px4_open");
151  cdev::CDev *dev = getDev(path);
152  int ret = 0;
153  int i;
154  mode_t mode;
155 
156  if (!dev && (flags & PX4_F_WRONLY) != 0 &&
157  strncmp(path, "/obj/", 5) != 0 &&
158  strncmp(path, "/dev/", 5) != 0) {
159  va_list p;
160  va_start(p, flags);
161  mode = va_arg(p, int);
162  va_end(p);
163 
164  // Create the file
165  PX4_DEBUG("Creating virtual file %s", path);
166  dev = cdev::VFile::createFile(path, mode);
167  }
168 
169  if (dev) {
170 
171  pthread_mutex_lock(&filemutex);
172 
173  for (i = 0; i < PX4_MAX_FD; ++i) {
174  if (filemap[i].vdev == nullptr) {
175  filemap[i] = cdev::file_t(flags, dev);
176  break;
177  }
178  }
179 
180  pthread_mutex_unlock(&filemutex);
181 
182  if (i < PX4_MAX_FD) {
183  ret = dev->open(&filemap[i]);
184 
185  } else {
186 
187  const unsigned NAMELEN = 32;
188  char thread_name[NAMELEN] = {};
189 
190  PX4_WARN("%s: exceeded maximum number of file descriptors, accessing %s",
191  thread_name, path);
192 #ifndef __PX4_QURT
193  int nret = pthread_getname_np(pthread_self(), thread_name, NAMELEN);
194 
195  if (nret || thread_name[0] == 0) {
196  PX4_WARN("failed getting thread name");
197  }
198 
199 #endif
200 
201  ret = -ENOENT;
202  }
203 
204  } else {
205  ret = -EINVAL;
206  }
207 
208  if (ret < 0) {
209  return -1;
210  }
211 
212  PX4_DEBUG("px4_open fd = %d", i);
213  return i;
214  }
215 
216  int px4_close(int fd)
217  {
218  int ret;
219 
220  cdev::CDev *dev = get_vdev(fd);
221 
222  if (dev) {
223  pthread_mutex_lock(&filemutex);
224  ret = dev->close(&filemap[fd]);
225 
226  filemap[fd].vdev = nullptr;
227 
228  pthread_mutex_unlock(&filemutex);
229  PX4_DEBUG("px4_close fd = %d", fd);
230 
231  } else {
232  ret = -EINVAL;
233  }
234 
235  if (ret < 0) {
236  px4_errno = -ret;
237  ret = PX4_ERROR;
238  }
239 
240  return ret;
241  }
242 
243  ssize_t px4_read(int fd, void *buffer, size_t buflen)
244  {
245  int ret;
246 
247  cdev::CDev *dev = get_vdev(fd);
248 
249  if (dev) {
250  PX4_DEBUG("px4_read fd = %d", fd);
251  ret = dev->read(&filemap[fd], (char *)buffer, buflen);
252 
253  } else {
254  ret = -EINVAL;
255  }
256 
257  if (ret < 0) {
258  px4_errno = -ret;
259  ret = PX4_ERROR;
260  }
261 
262  return ret;
263  }
264 
265  ssize_t px4_write(int fd, const void *buffer, size_t buflen)
266  {
267  int ret;
268 
269  cdev::CDev *dev = get_vdev(fd);
270 
271  if (dev) {
272  PX4_DEBUG("px4_write fd = %d", fd);
273  ret = dev->write(&filemap[fd], (const char *)buffer, buflen);
274 
275  } else {
276  ret = -EINVAL;
277  }
278 
279  if (ret < 0) {
280  px4_errno = -ret;
281  ret = PX4_ERROR;
282  }
283 
284  return ret;
285  }
286 
287  int px4_ioctl(int fd, int cmd, unsigned long arg)
288  {
289  PX4_DEBUG("px4_ioctl fd = %d", fd);
290  int ret = 0;
291 
292  cdev::CDev *dev = get_vdev(fd);
293 
294  if (dev) {
295  ret = dev->ioctl(&filemap[fd], cmd, arg);
296 
297  } else {
298  ret = -EINVAL;
299  }
300 
301  if (ret < 0) {
302  px4_errno = -ret;
303  }
304 
305  return ret;
306  }
307 
308  int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
309  {
310  if (nfds == 0) {
311  PX4_WARN("px4_poll with no fds");
312  return -1;
313  }
314 
315  px4_sem_t sem;
316  int count = 0;
317  int ret = -1;
318  unsigned int i;
319 
320  const unsigned NAMELEN = 32;
321  char thread_name[NAMELEN] = {};
322 
323 #ifndef __PX4_QURT
324  int nret = pthread_getname_np(pthread_self(), thread_name, NAMELEN);
325 
326  if (nret || thread_name[0] == 0) {
327  PX4_WARN("failed getting thread name");
328  }
329 
330 #endif
331 
332  PX4_DEBUG("Called px4_poll timeout = %d", timeout);
333 
334  px4_sem_init(&sem, 0, 0);
335 
336  // sem use case is a signal
337  px4_sem_setprotocol(&sem, SEM_PRIO_NONE);
338 
339  // Go through all fds and check them for a pollable state
340  bool fd_pollable = false;
341 
342  for (i = 0; i < nfds; ++i) {
343  fds[i].sem = &sem;
344  fds[i].revents = 0;
345  fds[i].priv = nullptr;
346 
347  cdev::CDev *dev = get_vdev(fds[i].fd);
348 
349  // If fd is valid
350  if (dev) {
351  PX4_DEBUG("%s: px4_poll: CDev->poll(setup) %d", thread_name, fds[i].fd);
352  ret = dev->poll(&filemap[fds[i].fd], &fds[i], true);
353 
354  if (ret < 0) {
355  PX4_WARN("%s: px4_poll() error: %s",
356  thread_name, strerror(errno));
357  break;
358  }
359 
360  if (ret >= 0) {
361  fd_pollable = true;
362  }
363  }
364  }
365 
366  // If any FD can be polled, lock the semaphore and
367  // check for new data
368  if (fd_pollable) {
369  if (timeout > 0) {
370 
371  // Get the current time
372  struct timespec ts;
373  // Note, we can't actually use CLOCK_MONOTONIC on macOS
374  // but that's hidden and implemented in px4_clock_gettime.
375  px4_clock_gettime(CLOCK_MONOTONIC, &ts);
376 
377  // Calculate an absolute time in the future
378  const unsigned billion = (1000 * 1000 * 1000);
379  uint64_t nsecs = ts.tv_nsec + ((uint64_t)timeout * 1000 * 1000);
380  ts.tv_sec += nsecs / billion;
381  nsecs -= (nsecs / billion) * billion;
382  ts.tv_nsec = nsecs;
383 
384  ret = px4_sem_timedwait(&sem, &ts);
385 
386  if (ret && errno != ETIMEDOUT) {
387  PX4_WARN("%s: px4_poll() sem error: %s", thread_name, strerror(errno));
388  }
389 
390  } else if (timeout < 0) {
391  px4_sem_wait(&sem);
392  }
393 
394  // We have waited now (or not, depending on timeout),
395  // go through all fds and count how many have data
396  for (i = 0; i < nfds; ++i) {
397 
398  cdev::CDev *dev = get_vdev(fds[i].fd);
399 
400  // If fd is valid
401  if (dev) {
402  PX4_DEBUG("%s: px4_poll: CDev->poll(teardown) %d", thread_name, fds[i].fd);
403  ret = dev->poll(&filemap[fds[i].fd], &fds[i], false);
404 
405  if (ret < 0) {
406  PX4_WARN("%s: px4_poll() 2nd poll fail", thread_name);
407  break;
408  }
409 
410  if (fds[i].revents) {
411  count += 1;
412  }
413  }
414  }
415  }
416 
417  px4_sem_destroy(&sem);
418 
419  // Return the positive count if present,
420  // return the negative error number if failed
421  return (count) ? count : ret;
422  }
423 
424  int px4_fsync(int fd)
425  {
426  return 0;
427  }
428 
429  int px4_access(const char *pathname, int mode)
430  {
431  if (mode != F_OK) {
432  errno = EINVAL;
433  return -1;
434  }
435 
436  cdev::CDev *dev = getDev(pathname);
437  return (dev != nullptr) ? 0 : -1;
438  }
439 
441  {
442  int i = 0;
443  PX4_INFO("PX4 Devices:");
444 
445  pthread_mutex_lock(&devmutex);
446 
447  for (const auto &dev : devmap) {
448  if (strncmp(dev.first.c_str(), "/dev/", 5) == 0) {
449  PX4_INFO(" %s", dev.first.c_str());
450  }
451  }
452 
453  pthread_mutex_unlock(&devmutex);
454 
455  PX4_INFO("DF Devices:");
456  const char *dev_path;
457  unsigned int index = 0;
458  i = 0;
459 
460  do {
461  // Each look increments index and returns -1 if end reached
462  i = DevMgr::getNextDeviceName(index, &dev_path);
463 
464  if (i == 0) {
465  PX4_INFO(" %s", dev_path);
466  }
467  } while (i == 0);
468  }
469 
471  {
472  PX4_INFO("Devices:");
473 
474  pthread_mutex_lock(&devmutex);
475 
476  for (const auto &dev : devmap) {
477  if (strncmp(dev.first.c_str(), "/obj/", 5) == 0) {
478  PX4_INFO(" %s", dev.first.c_str());
479  }
480  }
481 
482  pthread_mutex_unlock(&devmutex);
483  }
484 
486  {
487  PX4_INFO("Files:");
488 
489  pthread_mutex_lock(&devmutex);
490 
491  for (const auto &dev : devmap) {
492  if (strncmp(dev.first.c_str(), "/obj/", 5) != 0 &&
493  strncmp(dev.first.c_str(), "/dev/", 5) != 0) {
494  PX4_INFO(" %s", dev.first.c_str());
495  }
496  }
497 
498  pthread_mutex_unlock(&devmutex);
499  }
500 
501 } // extern "C"
virtual int open(file_t *filep)
Handle an open of the device.
Definition: CDev.cpp:141
int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
STL namespace.
static const px4_file_operations_t fops
Pointer to the default cdev file operations table; useful for registering clone devices etc...
Definition: CDev.hpp:176
virtual ssize_t read(file_t *filep, char *buffer, size_t buflen)
Perform a read from the device.
Definition: CDev.hpp:111
int px4_fsync(int fd)
virtual int close(file_t *filep)
Handle a close of the device.
Definition: CDev.cpp:166
pthread_mutex_t filemutex
Abstract class for any character device.
Definition: CDev.hpp:58
ssize_t px4_read(int fd, void *buffer, size_t buflen)
ssize_t px4_write(int fd, const void *buffer, size_t buflen)
static cdev::file_t filemap[PX4_MAX_FD]
int unregister_driver(const char *name)
static VFile * createFile(const char *fname, mode_t mode)
Definition: vfile.cpp:51
#define PX4_MAX_FD
uint8_t * data
Definition: dataman.cpp:149
static cdev::CDev * get_vdev(int fd)
int px4_errno
int register_driver(const char *name, const cdev::px4_file_operations_t *fops, cdev::mode_t mode, void *data)
static map< string, void * > devmap
virtual ssize_t write(file_t *filep, const char *buffer, size_t buflen)
Perform a write to the device.
Definition: CDev.hpp:123
int fd
Definition: dataman.cpp:146
const char * name
Definition: tests_main.c:58
static cdev::CDev * getDev(const char *path)
struct file file_t
int px4_open(const char *path, int flags,...)
uint32_t mode_t
void px4_show_files()
pthread_mutex_t devmutex
void px4_show_topics()
void px4_show_devices()
virtual int ioctl(file_t *filep, int cmd, unsigned long arg)
Perform an ioctl operation on the device.
Definition: CDev.cpp:192
mode
Definition: vtol_type.h:76
int px4_access(const char *pathname, int mode)
virtual int poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup)
Perform a poll setup/teardown operation.
Definition: CDev.cpp:213
int px4_close(int fd)
int px4_ioctl(int fd, int cmd, unsigned long arg)