PX4 Firmware
PX4 Autopilot Software http://px4.io
test_dataman.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2018-2019 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 /**
35  * @file test_dataman.c
36  * Tests for the data manager.
37  */
38 
39 #include <px4_platform_common/px4_config.h>
40 #include <px4_platform_common/posix.h>
41 
42 #include <sys/types.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 
50 #include <arch/board/board.h>
51 
52 #include <drivers/drv_board_led.h>
53 #include <drivers/drv_hrt.h>
54 #include <semaphore.h>
55 
56 #include "tests_main.h"
57 
58 #include "dataman/dataman.h"
59 
60 static px4_sem_t *sems;
61 static bool *task_returned_error;
62 int test_dataman(int argc, char *argv[]);
63 
64 #define NUM_MISSIONS_TEST 50
65 
66 #define DM_MAX_DATA_SIZE sizeof(struct mission_s)
67 
68 static int
69 task_main(int argc, char *argv[])
70 {
71  char buffer[DM_MAX_DATA_SIZE];
72 
73  PX4_INFO("Starting dataman test task %s", argv[1]);
74  /* try to read an invalid item */
75  int my_id = atoi(argv[1]);
76 
77  /* try to read an invalid item */
78  if (dm_read(DM_KEY_NUM_KEYS, 0, buffer, sizeof(buffer)) >= 0) {
79  PX4_ERR("%d read an invalid item failed", my_id);
80  goto fail;
81  }
82 
83  /* try to read an invalid index */
84  if (dm_read(DM_KEY_SAFE_POINTS, DM_KEY_SAFE_POINTS_MAX, buffer, sizeof(buffer)) >= 0) {
85  PX4_ERR("%d read an invalid index failed", my_id);
86  goto fail;
87  }
88 
89  srand(hrt_absolute_time() ^ my_id);
90  unsigned hit = 0, miss = 0;
91  hrt_abstime wstart = hrt_absolute_time();
92 
93  for (unsigned i = 0; i < NUM_MISSIONS_TEST; i++) {
94  memset(buffer, my_id, sizeof(buffer));
95  buffer[1] = i;
96  unsigned hash = i ^ my_id;
97  unsigned len = (hash % (DM_MAX_DATA_SIZE / 2)) + 2;
98 
100  //PX4_INFO("ret: %d", ret);
101 
102  if (ret != len) {
103  PX4_WARN("task %d: write failed, index %d, length %d", my_id, hash, len);
104  goto fail;
105  }
106 
107  if (i % (NUM_MISSIONS_TEST / 10) == 0) {
108  PX4_INFO("task %d: %.0f%%", my_id, (double)i * 100.0f / NUM_MISSIONS_TEST);
109  }
110 
111  px4_usleep(rand() & ((64 * 1024) - 1));
112  }
113 
114  hrt_abstime rstart = hrt_absolute_time();
115  hrt_abstime wend = rstart;
116 
117  for (unsigned i = 0; i < NUM_MISSIONS_TEST; i++) {
118  unsigned hash = i ^ my_id;
119  unsigned len2;
120  unsigned len = (hash % (DM_MAX_DATA_SIZE / 2)) + 2;
121 
122  if ((len2 = dm_read(DM_KEY_WAYPOINTS_OFFBOARD_1, hash, buffer, sizeof(buffer))) < 2) {
123  PX4_WARN("task %d: read failed length test, index %d", my_id, hash);
124  goto fail;
125  }
126 
127  if (buffer[0] == my_id) {
128  hit++;
129 
130  if (len2 != len) {
131  PX4_WARN("task %d: read failed length test, index %d, wanted %d, got %d", my_id, hash, len, len2);
132  goto fail;
133  }
134 
135  if (buffer[1] != i) {
136  PX4_WARN("task %d: data verification failed, index %d, wanted %d, got %d", my_id, hash, my_id, buffer[1]);
137  goto fail;
138  }
139 
140  } else {
141  miss++;
142  }
143  }
144 
146  PX4_INFO("task %d pass, hit %d, miss %d, io time read %" PRIu64 "ms. write %" PRIu64 "ms.",
147  my_id, hit, miss, (rend - rstart) / NUM_MISSIONS_TEST / 1000, (wend - wstart) / NUM_MISSIONS_TEST / 1000);
148  px4_sem_post(sems + my_id);
149  return 0;
150 
151 fail:
152  PX4_ERR("test_dataman FAILED: task %d, buffer %02x %02x %02x %02x %02x %02x",
153  my_id, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
154  px4_sem_post(sems + my_id);
155  task_returned_error[my_id] = true;
156  return -1;
157 }
158 
159 int test_dataman(int argc, char *argv[])
160 {
161  int i = 0;
162  unsigned num_tasks = 4;
163  char buffer[DM_MAX_DATA_SIZE];
164 
165  if (argc > 1) {
166  num_tasks = atoi(argv[1]);
167  }
168 
169  sems = (px4_sem_t *)malloc(num_tasks * sizeof(px4_sem_t));
170  task_returned_error = (bool *)calloc(num_tasks, sizeof(bool));
171  PX4_INFO("Running %d tasks", num_tasks);
172 
173  for (i = 0; i < num_tasks; i++) {
174  int task;
175 
176  char a[16];
177  snprintf(a, 16, "%d", i);
178 
179  char *av[] = {"tests_dataman", a, NULL};
180 
181  px4_sem_init(sems + i, 1, 0);
182  /* sems use case is a signal */
183  px4_sem_setprotocol(sems, SEM_PRIO_NONE);
184 
185  /* start the task */
186  if ((task = px4_task_spawn_cmd("dataman", SCHED_DEFAULT, SCHED_PRIORITY_MAX - 5, 2048, task_main, av)) <= 0) {
187  PX4_ERR("task start failed");
188  }
189  }
190 
191  for (i = 0; i < num_tasks; i++) {
192  px4_sem_wait(sems + i);
193  px4_sem_destroy(sems + i);
194  }
195 
196  free(sems);
197 
198  bool got_error = false;
199 
200  for (i = 0; i < num_tasks; i++) {
201  if (task_returned_error[i]) {
202  got_error = true;
203  break;
204  }
205  }
206 
207  free(task_returned_error);
208 
209  if (got_error) {
210  return -1;
211  }
212 
214 
215  for (i = 0; i < NUM_MISSIONS_TEST; i++) {
216  if (dm_read(DM_KEY_WAYPOINTS_OFFBOARD_1, i, buffer, sizeof(buffer)) != 0) {
217  break;
218  }
219  }
220 
221  if (i >= NUM_MISSIONS_TEST) {
222  PX4_ERR("Restart in-flight failed");
223  return -1;
224 
225  }
226 
228 
229  for (i = 0; i < NUM_MISSIONS_TEST; i++) {
230  if (dm_read(DM_KEY_WAYPOINTS_OFFBOARD_1, i, buffer, sizeof(buffer)) != 0) {
231  PX4_ERR("Restart power-on failed");
232  return -1;
233  }
234  }
235 
236  return 0;
237 }
#define NUM_MISSIONS_TEST
Definition: test_dataman.c:64
int test_dataman(int argc, char *argv[])
Definition: test_dataman.c:159
LED driver API to control the onboard LED(s) via ioctl() interface.
static int task_main(int argc, char *argv[])
Definition: test_dataman.c:69
High-resolution timer with callouts and timekeeping.
__EXPORT ssize_t dm_read(dm_item_t item, unsigned index, void *buf, size_t count)
Retrieve from the data manager file.
Definition: dataman.cpp:1104
__EXPORT int dm_restart(dm_reset_reason reason)
Tell the data manager about the type of the last reset.
Definition: dataman.cpp:1220
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
DATAMANAGER driver.
Tests declaration file.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
#define DM_MAX_DATA_SIZE
Definition: test_dataman.c:66
static px4_sem_t * sems
Definition: test_dataman.c:60
__EXPORT ssize_t dm_write(dm_item_t item, unsigned index, dm_persitence_t persistence, const void *buf, size_t count)
Write to the data manager file.
Definition: dataman.cpp:1072
static bool * task_returned_error
Definition: test_dataman.c:61
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).