Firmware
param.h
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 
45 #ifndef _SYSTEMLIB_PARAM_PARAM_H
46 #define _SYSTEMLIB_PARAM_PARAM_H
47 
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include <sys/types.h>
51 
53 #define PARAM_FILE_MAXSIZE 4096
54 
55 __BEGIN_DECLS
56 
60 #define PARAM_TYPE_INT32 0
61 #define PARAM_TYPE_FLOAT 1
62 #define PARAM_TYPE_STRUCT 100
63 #define PARAM_TYPE_STRUCT_MAX (16384 + PARAM_TYPE_STRUCT)
64 #define PARAM_TYPE_UNKNOWN (0xffff)
65 
66 typedef uint16_t param_type_t;
67 
68 
69 #ifdef __PX4_NUTTX // on NuttX use 16 bits to save RAM
70 
77 typedef uint16_t param_t;
78 
82 #define PARAM_INVALID ((uint16_t)0xffff)
83 
87 #define PARAM_HASH ((uint16_t)INT16_MAX)
88 
89 #else // on other platforms use 32 bits for better performance
90 
98 typedef uint32_t param_t;
99 
103 #define PARAM_INVALID ((uint32_t)0xffffffff)
104 
108 #define PARAM_HASH ((uint32_t)INT32_MAX)
109 
110 #endif /* __PX4_NUTTX */
111 
112 
116 __EXPORT void param_init(void);
117 
126 __EXPORT param_t param_find(const char *name);
127 
135 
141 __EXPORT unsigned param_count(void);
142 
148 __EXPORT unsigned param_count_used(void);
149 
155 __EXPORT bool param_used(param_t param);
156 
163 __EXPORT param_t param_for_index(unsigned index);
164 
171 __EXPORT param_t param_for_used_index(unsigned index);
172 
179 __EXPORT int param_get_index(param_t param);
180 
188 
195 __EXPORT const char *param_name(param_t param);
196 
204 
211 
218 
225 __EXPORT param_type_t param_type(param_t param);
226 
233 __EXPORT size_t param_size(param_t param);
234 
243 __EXPORT int param_get(param_t param, void *val);
244 
253 __EXPORT int param_set(param_t param, const void *val);
254 
261 __EXPORT void param_set_used(param_t param);
262 
271 __EXPORT int param_set_no_notification(param_t param, const void *val);
272 
277 __EXPORT void param_notify_changes(void);
278 
288 __EXPORT int param_reset(param_t param);
289 
295 __EXPORT void param_reset_all(void);
296 
306 __EXPORT void param_reset_excludes(const char *excludes[], int num_excludes);
307 
316 __EXPORT int param_export(int fd, bool only_unsaved);
317 
327 __EXPORT int param_import(int fd);
328 
339 __EXPORT int param_load(int fd);
340 
355 __EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed, bool only_used);
356 
365 __EXPORT int param_set_default_file(const char *filename);
366 
374 __EXPORT const char *param_get_default_file(void);
375 
384 __EXPORT int param_save_default(void);
385 
391 __EXPORT int param_load_default(void);
392 
398 __EXPORT uint32_t param_hash_check(void);
399 
404 __EXPORT void param_print_status(void);
405 
411 __EXPORT void param_control_autosave(bool enable);
412 
413 /*
414  * Macros creating static parameter definitions.
415  *
416  * Note that these structures are not known by name; they are
417  * collected into a section that is iterated by the parameter
418  * code.
419  *
420  * Note that these macros cannot be used in C++ code due to
421  * their use of designated initializers. They should probably
422  * be refactored to avoid the use of a union for param_value_u.
423  */
424 
426 #define PARAM_DEFINE_INT32(_name, _default)
427 
429 #define PARAM_DEFINE_FLOAT(_name, _default)
430 
432 #define PARAM_DEFINE_STRUCT(_name, _default)
433 
438  void *p;
439  int32_t i;
440  float f;
441 };
442 
449 struct param_info_s {
450  const char *name
451 
452 // GCC 4.8 and higher don't implement proper alignment of static data on
453 // 64-bit. This means that the 24-byte param_info_s variables are
454 // 16 byte aligned by GCC and that messes up the assumption that
455 // sequential items in the __param segment can be addressed as an array.
456 // The assumption is that the address of the second parameter is at
457 // &param[0]+sizeof(param[0]). When compiled with clang it is
458 // true, with gcc is is not true.
459 // See https://llvm.org/bugs/show_bug.cgi?format=multiple&id=18006
460 // The following hack is for GCC >=4.8 only. Clang works fine without
461 // this.
462 #ifdef __PX4_POSIX
463  __attribute__((aligned(16)));
464 #else
465  ;
466 #endif
467  param_type_t type;
468  uint16_t volatile_param: 1;
469  union param_value_u val;
470 };
471 
472 __END_DECLS
473 
474 
475 
476 #ifdef __cplusplus
477 #if 0 // set to 1 to debug param type mismatches
478 #include <cstdio>
479 #define CHECK_PARAM_TYPE(param, type) \
480  if (param_type(param) != type) { \
481  /* use printf() to avoid having to use more includes */ \
482  printf("wrong type passed to param_get() for param %s\n", param_name(param)); \
483  }
484 #else
485 #define CHECK_PARAM_TYPE(param, type)
486 #endif
487 
488 // param is a C-interface. This means there is no overloading, and thus no type-safety for param_get().
489 // So for C++ code we redefine param_get() to inlined overloaded versions, which gives us type-safety
490 // w/o having to use a different interface
491 static inline int param_get(param_t param, float *val)
492 {
493  CHECK_PARAM_TYPE(param, PARAM_TYPE_FLOAT);
494  return param_get(param, (void *)val);
495 }
496 static inline int param_get(param_t param, int32_t *val)
497 {
498  CHECK_PARAM_TYPE(param, PARAM_TYPE_INT32);
499  return param_get(param, (void *)val);
500 }
501 #undef CHECK_PARAM_TYPE
502 
503 #endif /* __cplusplus */
504 
505 #endif
__EXPORT param_t param_find_no_notification(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:374
__EXPORT void param_init(void)
Initialize the param backend.
Definition: parameters.cpp:228
__EXPORT size_t param_size(param_t param)
Determine the size of a parameter.
Definition: parameters.cpp:523
__EXPORT bool param_value_unsaved(param_t param)
Test whether a parameter&#39;s value has been changed but not saved.
Definition: parameters.cpp:506
__EXPORT int param_import(int fd)
Import parameters from a file, discarding any unrecognized parameters.
Definition: parameters.cpp:1313
__EXPORT void param_foreach(void(*func)(void *arg, param_t param), void *arg, bool only_changed, bool only_used)
Apply a function to each parameter.
Definition: parameters.cpp:1334
#define PARAM_TYPE_INT32
Parameter types.
Definition: param.h:60
__EXPORT void param_print_status(void)
Print the status of the param system.
Definition: parameters.cpp:1376
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:587
__EXPORT int param_set_no_notification(param_t param, const void *val)
Set the value of a parameter, but do not notify the system about the change.
Definition: parameters.cpp:803
__EXPORT int param_get_index(param_t param)
Look up the index of a parameter.
Definition: parameters.cpp:447
__EXPORT const char * param_get_default_file(void)
Get the default parameter file name.
Definition: parameters.cpp:951
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:797
__EXPORT param_t param_for_index(unsigned index)
Look up a parameter by index.
Definition: parameters.cpp:406
__EXPORT void param_set_used(param_t param)
Mark a parameter as used.
Definition: parameters.cpp:821
__EXPORT bool param_value_is_default(param_t param)
Test whether a parameter&#39;s value has changed from the default.
Definition: parameters.cpp:496
__EXPORT int param_get_used_index(param_t param)
Look up the index of an used parameter.
Definition: parameters.cpp:457
__EXPORT unsigned param_count(void)
Return the total number of parameters.
Definition: parameters.cpp:380
Definition: crtp.h:57
Static parameter definition structure.
Definition: param.h:449
__EXPORT const char * param_name(param_t param)
Obtain the name of a parameter.
Definition: parameters.cpp:484
__EXPORT void param_reset_excludes(const char *excludes[], int num_excludes)
Reset all parameters to their default values except for excluded parameters.
Definition: parameters.cpp:899
Parameter value union.
Definition: param.h:437
__EXPORT int param_export(int fd, bool only_unsaved)
Export changed parameters to a file.
Definition: parameters.cpp:1036
Definition: I2C.hpp:51
__EXPORT unsigned param_count_used(void)
Return the actually used number of parameters.
Definition: parameters.cpp:386
__EXPORT int param_set_default_file(const char *filename)
Set the default parameter file name.
Definition: parameters.cpp:928
__EXPORT param_type_t param_type(param_t param)
Obtain the type of a parameter.
Definition: parameters.cpp:517
__EXPORT int param_save_default(void)
Save parameters to the default file.
Definition: parameters.cpp:957
__EXPORT uint32_t param_hash_check(void)
Generate the hash of all parameters and their values.
Definition: parameters.cpp:1353
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:368
__EXPORT bool param_is_volatile(param_t param)
Obtain the volatile state of a parameter.
Definition: parameters.cpp:490
__EXPORT void param_notify_changes(void)
Notify the system about parameter changes.
Definition: parameters.cpp:321
__EXPORT void param_control_autosave(bool enable)
Enable/disable the param autosaving.
Definition: parameters.cpp:670
__EXPORT void param_reset_all(void)
Reset all parameters to their default values.
Definition: parameters.cpp:893
__EXPORT int param_reset(param_t param)
Reset a parameter to its default value.
Definition: parameters.cpp:840
__EXPORT param_t param_for_used_index(unsigned index)
Look up an used parameter by index.
Definition: parameters.cpp:418
__EXPORT int param_load_default(void)
Load parameters from the default parameter file.
Definition: parameters.cpp:1003
__EXPORT int param_load(int fd)
Load parameters from a file.
Definition: parameters.cpp:1323
uint32_t param_t
Parameter handle.
Definition: param.h:98
__EXPORT bool param_used(param_t param)
Wether a parameter is in use in the system.
Definition: parameters.cpp:809