PX4 Firmware
PX4 Autopilot Software http://px4.io
uORBTest_UnitTest.hpp
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 #ifndef _uORBTest_UnitTest_hpp_
35 #define _uORBTest_UnitTest_hpp_
36 #include "../uORBCommon.hpp"
37 #include "../uORB.h"
38 #include <px4_platform_common/time.h>
39 #include <px4_platform_common/tasks.h>
40 #include <unistd.h>
41 
42 struct orb_test {
43  int val;
45 };
47 ORB_DECLARE(orb_multitest);
48 
49 
51  int val;
53  char junk[64];
54 };
56 ORB_DECLARE(orb_test_medium_multi);
57 ORB_DECLARE(orb_test_medium_queue);
58 ORB_DECLARE(orb_test_medium_queue_poll);
59 
61  int val;
63  char junk[512];
64 };
66 
67 
68 namespace uORBTest
69 {
70 class UnitTest;
71 }
72 
74 {
75 public:
76 
77  // Singleton pattern
78  static uORBTest::UnitTest &instance();
79  ~UnitTest() = default;
80  int test();
81  template<typename S> int latency_test(orb_id_t T, bool print);
82  int info();
83 
84  // Disallow copy
85  UnitTest(const uORBTest::UnitTest & /*unused*/) = delete;
86 
87 private:
88  UnitTest() : pubsubtest_passed(false), pubsubtest_print(false) {}
89 
90  static int pubsubtest_threadEntry(int argc, char *argv[]);
91  int pubsublatency_main();
92 
93  static int pub_test_multi2_entry(int argc, char *argv[]);
94  int pub_test_multi2_main();
95 
96  volatile bool _thread_should_exit;
97 
100  int pubsubtest_res = OK;
101 
102  orb_advert_t _pfd[4]; ///< used for test_multi and test_multi_reversed
103 
104  int test_single();
105 
106  /* These 3 depend on each other and must be called in this order */
107  int test_multi();
108  int test_multi_reversed();
109  int test_unadvertise();
110 
111  int test_multi2();
112 
113  /* queuing tests */
114  int test_queue();
115  static int pub_test_queue_entry(int argc, char *argv[]);
116  int pub_test_queue_main();
117  int test_queue_poll_notify();
118  volatile int _num_messages_sent = 0;
119 
120  int test_fail(const char *fmt, ...);
121  int test_note(const char *fmt, ...);
122 };
123 
124 template<typename S>
126 {
127  test_note("---------------- LATENCY TEST ------------------");
128  S t;
129  t.val = 308;
130  t.time = hrt_absolute_time();
131 
132  orb_advert_t pfd0 = orb_advertise(T, &t);
133 
134  if (pfd0 == nullptr) {
135  return test_fail("orb_advertise failed (%i)", errno);
136  }
137 
138  char *const args[1] = { nullptr };
139 
140  pubsubtest_print = print;
141  pubsubtest_passed = false;
142 
143  /* test pub / sub latency */
144 
145  // Can't pass a pointer in args, must be a null terminated
146  // array of strings because the strings are copied to
147  // prevent access if the caller data goes out of scope
148  int pubsub_task = px4_task_spawn_cmd("uorb_latency",
149  SCHED_DEFAULT,
150  SCHED_PRIORITY_MAX,
151  2000,
153  args);
154 
155  /* give the test task some data */
156  while (!pubsubtest_passed) {
157  ++t.val;
158  t.time = hrt_absolute_time();
159 
160  if (PX4_OK != orb_publish(T, pfd0, &t)) {
161  return test_fail("mult. pub0 timing fail");
162  }
163 
164  /* simulate >800 Hz system operation */
165  px4_usleep(1000);
166  }
167 
168  if (pubsub_task < 0) {
169  return test_fail("failed launching task");
170  }
171 
172  orb_unadvertise(pfd0);
173 
174  return pubsubtest_res;
175 }
176 
177 #endif // _uORBTest_UnitTest_hpp_
static int pubsubtest_threadEntry(int argc, char *argv[])
volatile bool _thread_should_exit
int latency_test(orb_id_t T, bool print)
int info()
Print a little info about the driver.
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
Definition: uORB.cpp:43
LidarLite * instance
Definition: ll40ls.cpp:65
ORB_DECLARE(orb_test)
void test(enum LPS25H_BUS busid)
Perform some basic functional tests on the driver; make sure we can collect data from the sensor in p...
Definition: lps25h.cpp:792
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
Definition: uORB.cpp:70
Object metadata.
Definition: uORB.h:50
int orb_unadvertise(orb_advert_t handle)
Definition: uORB.cpp:65
#define OK
Definition: uavcan_main.cpp:71
hrt_abstime time
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).