PX4 Firmware
PX4 Autopilot Software http://px4.io
uORBMain.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include <string.h>
35 
36 #include "uORBManager.hpp"
37 #include "uORB.h"
38 #include "uORBCommon.hpp"
39 
40 #include <px4_platform_common/log.h>
41 #include <px4_platform_common/module.h>
42 
43 extern "C" { __EXPORT int uorb_main(int argc, char *argv[]); }
44 
45 static uORB::DeviceMaster *g_dev = nullptr;
46 static void usage()
47 {
48  PRINT_MODULE_DESCRIPTION(
49  R"DESCR_STR(
50 ### Description
51 uORB is the internal pub-sub messaging system, used for communication between modules.
52 
53 It is typically started as one of the very first modules and most other modules depend on it.
54 
55 ### Implementation
56 No thread or work queue is needed, the module start only makes sure to initialize the shared global state.
57 Communication is done via shared memory.
58 The implementation is asynchronous and lock-free, ie. a publisher does not wait for a subscriber and vice versa.
59 This is achieved by having a separate buffer between a publisher and a subscriber.
60 
61 The code is optimized to minimize the memory footprint and the latency to exchange messages.
62 
63 The interface is based on file descriptors: internally it uses `read`, `write` and `ioctl`. Except for the
64 publications, which use `orb_advert_t` handles, so that they can be used from interrupts as well (on NuttX).
65 
66 Messages are defined in the `/msg` directory. They are converted into C/C++ code at build-time.
67 
68 If compiled with ORB_USE_PUBLISHER_RULES, a file with uORB publication rules can be used to configure which
69 modules are allowed to publish which topics. This is used for system-wide replay.
70 
71 ### Examples
72 Monitor topic publication rates. Besides `top`, this is an important command for general system inspection:
73 $ uorb top
74 )DESCR_STR");
75 
76  PRINT_MODULE_USAGE_NAME("uorb", "communication");
77  PRINT_MODULE_USAGE_COMMAND("start");
78  PRINT_MODULE_USAGE_COMMAND_DESCR("status", "Print topic statistics");
79  PRINT_MODULE_USAGE_COMMAND_DESCR("top", "Monitor topic publication rates");
80  PRINT_MODULE_USAGE_PARAM_FLAG('a', "print all instead of only currently publishing topics", true);
81  PRINT_MODULE_USAGE_PARAM_FLAG('1', "run only once, then exit", true);
82  PRINT_MODULE_USAGE_ARG("<filter1> [<filter2>]", "topic(s) to match (implies -a)", true);
83 }
84 
85 int
86 uorb_main(int argc, char *argv[])
87 {
88  if (argc < 2) {
89  usage();
90  return -EINVAL;
91  }
92 
93  /*
94  * Start/load the driver.
95  */
96  if (!strcmp(argv[1], "start")) {
97 
98  if (g_dev != nullptr) {
99  PX4_WARN("already loaded");
100  /* user wanted to start uorb, its already running, no error */
101  return 0;
102  }
103 
104  if (!uORB::Manager::initialize()) {
105  PX4_ERR("uorb manager alloc failed");
106  return -ENOMEM;
107  }
108 
109  /* create the driver */
111 
112  if (g_dev == nullptr) {
113  return -errno;
114  }
115 
116 #if !defined(__PX4_QURT) && !defined(__PX4_POSIX_EAGLE) && !defined(__PX4_POSIX_EXCELSIOR)
117  /* FIXME: this fails on Snapdragon (see https://github.com/PX4/Firmware/issues/5406),
118  * so we disable logging messages to the ulog for now. This needs further investigations.
119  */
120  px4_log_initialize();
121 #endif
122 
123  return OK;
124  }
125 
126  /*
127  * Print driver information.
128  */
129  if (!strcmp(argv[1], "status")) {
130  if (g_dev != nullptr) {
131  g_dev->printStatistics(true);
132 
133  } else {
134  PX4_INFO("uorb is not running");
135  }
136 
137  return OK;
138  }
139 
140  if (!strcmp(argv[1], "top")) {
141  if (g_dev != nullptr) {
142  g_dev->showTop(argv + 2, argc - 2);
143 
144  } else {
145  PX4_INFO("uorb is not running");
146  }
147 
148  return OK;
149  }
150 
151  usage();
152  return -EINVAL;
153 }
static uORB::Manager * get_instance()
Method to get the singleton instance for the uORB::Manager.
Definition: uORBManager.hpp:89
API for the uORB lightweight object broker.
uORB::DeviceMaster * get_device_master()
Get the DeviceMaster.
Definition: uORBManager.cpp:92
static uORB::DeviceMaster * g_dev
Definition: uORBMain.cpp:45
Definition: I2C.hpp:51
static bool initialize()
Initialize the singleton.
Definition: uORBManager.cpp:49
__EXPORT int uorb_main(int argc, char *argv[])
Definition: uORBMain.cpp:86
static void usage()
Definition: uORBMain.cpp:46
void showTop(char **topic_filter, int num_filters)
Continuously print statistics, like the unix top command for processes.
#define OK
Definition: uavcan_main.cpp:71
Master control device for ObjDev.
void printStatistics(bool reset)
Print statistics for each existing topic.