PX4 Firmware
PX4 Autopilot Software http://px4.io
test_param.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-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_param.c
36  * Tests related to the parameter system.
37  */
38 
39 #include <px4_platform_common/defines.h>
40 #include <stdio.h>
41 #include "systemlib/err.h"
42 #include <parameters/param.h>
43 #include "tests_main.h"
44 
45 #define PARAM_MAGIC1 12345678
46 #define PARAM_MAGIC2 0xa5a5a5a5
47 
48 int
49 test_param(int argc, char *argv[])
50 {
51  param_t p;
52 
53  p = param_find("TEST_PARAMS");
54 
55  if (p == PARAM_INVALID) {
56  warnx("test parameter not found");
57  return 1;
58  }
59 
60  if (param_reset(p) != OK) {
61  warnx("failed param reset");
62  return 1;
63  }
64 
65  param_type_t t = param_type(p);
66 
67  if (t != PARAM_TYPE_INT32) {
68  warnx("test parameter type mismatch (got %u)", (unsigned)t);
69  return 1;
70  }
71 
72  int32_t val = -1;
73 
74  if (param_get(p, &val) != OK) {
75  warnx("failed to read test parameter");
76  return 1;
77  }
78 
79  if (val != PARAM_MAGIC1) {
80  warnx("parameter value mismatch");
81  return 1;
82  }
83 
84  val = PARAM_MAGIC2;
85 
86  if (param_set(p, &val) != OK) {
87  warnx("failed to write test parameter");
88  return 1;
89  }
90 
91  if (param_get(p, &val) != OK) {
92  warnx("failed to re-read test parameter");
93  return 1;
94  }
95 
96  if ((uint32_t)val != PARAM_MAGIC2) {
97  warnx("parameter value mismatch after write");
98  return 1;
99  }
100 
101  warnx("parameter test PASS");
102 
103  return 0;
104 }
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
#define PARAM_TYPE_INT32
Parameter types.
Definition: param.h:60
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
uint16_t param_type_t
Definition: param.h:66
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:814
#define PARAM_MAGIC1
Definition: test_param.c:45
Global flash based parameter store.
int test_param(int argc, char *argv[])
Definition: test_param.c:49
#define warnx(...)
Definition: err.h:95
__EXPORT param_type_t param_type(param_t param)
Obtain the type of a parameter.
Definition: parameters.cpp:519
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Tests declaration file.
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
#define PARAM_MAGIC2
Definition: test_param.c:46
#define OK
Definition: uavcan_main.cpp:71
__EXPORT int param_reset(param_t param)
Reset a parameter to its default value.
Definition: parameters.cpp:857
uint32_t param_t
Parameter handle.
Definition: param.h:98