Firmware
px4_module.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 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 
38 #pragma once
39 
40 #include <pthread.h>
41 #include <unistd.h>
42 #include <stdbool.h>
43 
44 #include <px4_atomic.h>
45 #include <px4_time.h>
46 #include <px4_log.h>
47 #include <px4_tasks.h>
48 #include <systemlib/px4_macros.h>
49 
50 #ifdef __cplusplus
51 
52 #include <cstring>
53 
60 extern pthread_mutex_t px4_modules_mutex;
61 
114 template<class T>
115 class ModuleBase
116 {
117 public:
118  ModuleBase() : _task_should_exit{false} {}
119  virtual ~ModuleBase() {}
120 
128  static int main(int argc, char *argv[])
129  {
130  if (argc <= 1 ||
131  strcmp(argv[1], "-h") == 0 ||
132  strcmp(argv[1], "help") == 0 ||
133  strcmp(argv[1], "info") == 0 ||
134  strcmp(argv[1], "usage") == 0) {
135  return T::print_usage();
136  }
137 
138  if (strcmp(argv[1], "start") == 0) {
139  // Pass the 'start' argument too, because later on px4_getopt() will ignore the first argument.
140  return start_command_base(argc - 1, argv + 1);
141  }
142 
143  if (strcmp(argv[1], "status") == 0) {
144  return status_command();
145  }
146 
147  if (strcmp(argv[1], "stop") == 0) {
148  return stop_command();
149  }
150 
151  lock_module(); // Lock here, as the method could access _object.
152  int ret = T::custom_command(argc - 1, argv + 1);
153  unlock_module();
154 
155  return ret;
156  }
157 
168  static int run_trampoline(int argc, char *argv[])
169  {
170  int ret = 0;
171 
172 #ifdef __PX4_NUTTX
173  // On NuttX task_create() adds the task name as first argument.
174  argc -= 1;
175  argv += 1;
176 #endif
177 
178  T *object = T::instantiate(argc, argv);
179  _object.store(object);
180 
181  if (object) {
182  object->run();
183 
184  } else {
185  PX4_ERR("failed to instantiate object");
186  ret = -1;
187  }
188 
189  exit_and_cleanup();
190 
191  return ret;
192  }
193 
201  static int start_command_base(int argc, char *argv[])
202  {
203  int ret = 0;
204  lock_module();
205 
206  if (is_running()) {
207  ret = -1;
208  PX4_ERR("Task already running");
209 
210  } else {
211  ret = T::task_spawn(argc, argv);
212 
213  if (ret < 0) {
214  PX4_ERR("Task start failed (%i)", ret);
215  }
216  }
217 
218  unlock_module();
219  return ret;
220  }
221 
227  static int stop_command()
228  {
229  int ret = 0;
230  lock_module();
231 
232  if (is_running()) {
233  T *object = _object.load();
234 
235  if (object) {
236  object->request_stop();
237 
238  unsigned int i = 0;
239 
240  do {
241  unlock_module();
242  px4_usleep(20000); // 20 ms
243  lock_module();
244 
245  if (++i > 100 && _task_id != -1) { // wait at most 2 sec
246  if (_task_id != task_id_is_work_queue) {
247  px4_task_delete(_task_id);
248  }
249 
250  _task_id = -1;
251 
252  delete _object.load();
253  _object.store(nullptr);
254 
255  ret = -1;
256  break;
257  }
258  } while (_task_id != -1);
259 
260  } else {
261  // In the very unlikely event that can only happen on work queues,
262  // if the starting thread does not wait for the work queue to initialize,
263  // and inside the work queue, the allocation of _object fails
264  // and exit_and_cleanup() is not called, set the _task_id as invalid.
265  _task_id = -1;
266  }
267  }
268 
269  unlock_module();
270  return ret;
271  }
272 
277  static int status_command()
278  {
279  int ret = -1;
280  lock_module();
281 
282  if (is_running() && _object.load()) {
283  T *object = _object.load();
284  ret = object->print_status();
285 
286  } else {
287  PX4_INFO("not running");
288  }
289 
290  unlock_module();
291  return ret;
292  }
293 
299  virtual int print_status()
300  {
301  PX4_INFO("running");
302  return 0;
303  }
304 
309  virtual void run()
310  {
311  }
312 
317  static bool is_running()
318  {
319  return _task_id != -1;
320  }
321 
322 protected:
323 
327  virtual void request_stop()
328  {
329  _task_should_exit.store(true);
330  }
331 
336  bool should_exit() const
337  {
338  return _task_should_exit.load();
339  }
340 
346  static void exit_and_cleanup()
347  {
348  // Take the lock here:
349  // - if startup fails and we're faster than the parent thread, it will set
350  // _task_id and subsequently it will look like the task is running.
351  // - deleting the object must take place inside the lock.
352  lock_module();
353 
354  delete _object.load();
355  _object.store(nullptr);
356 
357  _task_id = -1; // Signal a potentially waiting thread for the module to exit that it can continue.
358  unlock_module();
359  }
360 
365  static int wait_until_running()
366  {
367  int i = 0;
368 
369  do {
370  /* Wait up to 1s. */
371  px4_usleep(2500);
372 
373  } while (!_object.load() && ++i < 400);
374 
375  if (i == 400) {
376  PX4_ERR("Timed out while waiting for thread to start");
377  return -1;
378  }
379 
380  return 0;
381  }
382 
386  static T *get_instance()
387  {
388  return (T *)_object.load();
389  }
390 
395  static px4::atomic<T *> _object;
396 
398  static int _task_id;
399 
401  static constexpr const int task_id_is_work_queue = -2;
402 
403 private:
407  static void lock_module()
408  {
409  pthread_mutex_lock(&px4_modules_mutex);
410  }
411 
415  static void unlock_module()
416  {
417  pthread_mutex_unlock(&px4_modules_mutex);
418  }
419 
421  px4::atomic_bool _task_should_exit{false};
422 };
423 
424 template<class T>
425 px4::atomic<T *> ModuleBase<T>::_object{nullptr};
426 
427 template<class T>
428 int ModuleBase<T>::_task_id = -1;
429 
430 
431 #endif /* __cplusplus */
432 
433 
434 __BEGIN_DECLS
435 
442 #ifdef __PX4_NUTTX
443 
449 static inline void PRINT_MODULE_DESCRIPTION(const char *description) {}
450 #else
451 
465 __EXPORT void PRINT_MODULE_DESCRIPTION(const char *description);
466 #endif
467 
473 __EXPORT void PRINT_MODULE_USAGE_NAME(const char *executable_name, const char *category);
474 
479 __EXPORT void PRINT_MODULE_USAGE_SUBCATEGORY(const char *subcategory);
480 
484 __EXPORT void PRINT_MODULE_USAGE_NAME_SIMPLE(const char *executable_name, const char *category);
485 
486 
490 __EXPORT void PRINT_MODULE_USAGE_COMMAND_DESCR(const char *name, const char *description);
491 
492 #define PRINT_MODULE_USAGE_COMMAND(name) \
493  PRINT_MODULE_USAGE_COMMAND_DESCR(name, NULL);
494 
498 #define PRINT_MODULE_USAGE_DEFAULT_COMMANDS() \
499  PRINT_MODULE_USAGE_COMMAND("stop"); \
500  PRINT_MODULE_USAGE_COMMAND_DESCR("status", "print status info");
501 
502 
514 __EXPORT void PRINT_MODULE_USAGE_PARAM_INT(char option_char, int default_val, int min_val, int max_val,
515  const char *description, bool is_optional);
516 
526 __EXPORT void PRINT_MODULE_USAGE_PARAM_FLOAT(char option_char, float default_val, float min_val, float max_val,
527  const char *description, bool is_optional);
528 
536 __EXPORT void PRINT_MODULE_USAGE_PARAM_FLAG(char option_char, const char *description, bool is_optional);
537 
551 __EXPORT void PRINT_MODULE_USAGE_PARAM_STRING(char option_char, const char *default_val, const char *values,
552  const char *description, bool is_optional);
553 
559 __EXPORT void PRINT_MODULE_USAGE_PARAM_COMMENT(const char *comment);
560 
561 
569 __EXPORT void PRINT_MODULE_USAGE_ARG(const char *values, const char *description, bool is_optional);
570 
571 
572 __END_DECLS
__BEGIN_DECLS __EXPORT void PRINT_MODULE_DESCRIPTION(const char *description)
Module documentation and command usage help methods.
Definition: module.cpp:51
__EXPORT int px4_task_delete(px4_task_t pid)
Deletes a task - does not do resource cleanup.
Definition: px4_posix_tasks.cpp:283
Provides atomic integers and counters.
__EXPORT void PRINT_MODULE_USAGE_NAME(const char *executable_name, const char *category)
Prints the command name.
Definition: module.cpp:61
A set of useful macros for enhanced runtime and compile time error detection and warning suppression...
__EXPORT void PRINT_MODULE_USAGE_ARG(const char *values, const char *description, bool is_optional)
Prints the definition for an argument, which does not have the typical -p <val> form, but for example &#39;param set &#39;.
Definition: module.cpp:158
__EXPORT void PRINT_MODULE_USAGE_PARAM_STRING(char option_char, const char *default_val, const char *values, const char *description, bool is_optional)
Prints a string parameter.
Definition: module.cpp:132
__EXPORT void PRINT_MODULE_USAGE_PARAM_COMMENT(const char *comment)
Prints a comment, that applies to the next arguments or parameters.
Definition: module.cpp:87
Definition: I2C.hpp:51
Platform dependant logging/debug implementation.
__EXPORT void PRINT_MODULE_USAGE_PARAM_FLAG(char option_char, const char *description, bool is_optional)
Prints a flag parameter, without any value.
Definition: module.cpp:122
__EXPORT void PRINT_MODULE_USAGE_NAME_SIMPLE(const char *executable_name, const char *category)
Prints the name for a command without any sub-commands (.
Definition: module.cpp:72
__EXPORT void PRINT_MODULE_USAGE_SUBCATEGORY(const char *subcategory)
Specify a subcategory (optional).
Definition: module.cpp:67
__EXPORT void PRINT_MODULE_USAGE_COMMAND_DESCR(const char *name, const char *description)
Prints a command with a short description what it does.
Definition: module.cpp:77
__EXPORT void PRINT_MODULE_USAGE_PARAM_INT(char option_char, int default_val, int min_val, int max_val, const char *description, bool is_optional)
Prints an integer parameter.
Definition: module.cpp:92
__EXPORT void PRINT_MODULE_USAGE_PARAM_FLOAT(char option_char, float default_val, float min_val, float max_val, const char *description, bool is_optional)
Prints a float parameter.
Definition: module.cpp:107