PX4 Firmware
PX4 Autopilot Software http://px4.io
BlockParam.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-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 
34 /**
35  * @file BlockParam.cpp
36  *
37  * Controller library code
38  */
39 
40 #include "BlockParam.hpp"
41 
42 #include <cstring>
43 
44 #include <containers/List.hpp>
45 
46 namespace control
47 {
48 
49 BlockParamBase::BlockParamBase(Block *parent, const char *name, bool parent_prefix)
50 {
51  char fullname[blockNameLengthMax];
52 
53  if (parent == nullptr) {
54  strncpy(fullname, name, blockNameLengthMax - 1);
55  fullname[sizeof(fullname) - 1] = '\0';
56 
57  } else {
58  char parentName[blockNameLengthMax];
59  parent->getName(parentName, blockNameLengthMax);
60 
61  if (strcmp(name, "") == 0) {
62  strncpy(fullname, parentName, blockNameLengthMax);
63  // ensure string is terminated
64  fullname[sizeof(fullname) - 1] = '\0';
65 
66  } else if (parent_prefix) {
67  if (snprintf(fullname, blockNameLengthMax, "%s_%s", parentName, name) >= blockNameLengthMax) {
68  PX4_ERR("param too long: %s", name);
69  }
70 
71  } else {
72  strncpy(fullname, name, blockNameLengthMax);
73  // ensure string is terminated
74  fullname[sizeof(fullname) - 1] = '\0';
75  }
76 
77  parent->getParams().add(this);
78  }
79 
80  _handle = param_find(fullname);
81 
82  if (_handle == PARAM_INVALID) {
83  PX4_ERR("error finding param: %s", fullname);
84  }
85 };
86 
87 template <>
88 BlockParam<bool>::BlockParam(Block *block, const char *name, bool parent_prefix) :
89  BlockParamBase(block, name, parent_prefix),
90  _val()
91 {
92  update();
93 }
94 
95 template <>
97 {
98  int32_t tmp = 0;
99  int ret = param_get(_handle, &tmp);
100 
101  if (tmp == 1) {
102  _val = true;
103 
104  } else {
105  _val = false;
106  }
107 
108  return (ret == PX4_OK);
109 }
110 
111 template <>
112 BlockParam<int32_t>::BlockParam(Block *block, const char *name, bool parent_prefix) :
113  BlockParamBase(block, name, parent_prefix),
114  _val()
115 {
116  update();
117 }
118 
119 template <>
120 BlockParam<float>::BlockParam(Block *block, const char *name, bool parent_prefix) :
121  BlockParamBase(block, name, parent_prefix),
122  _val()
123 {
124  update();
125 }
126 
127 template <>
128 BlockParam<int32_t &>::BlockParam(Block *block, const char *name, bool parent_prefix, int32_t &extern_val) :
129  BlockParamBase(block, name, parent_prefix),
130  _val(extern_val)
131 {
132  update();
133 }
134 
135 template <>
136 BlockParam<float &>::BlockParam(Block *block, const char *name, bool parent_prefix, float &extern_val) :
137  BlockParamBase(block, name, parent_prefix),
138  _val(extern_val)
139 {
140  update();
141 }
142 
143 } // namespace control
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
An intrusive linked list.
void getName(char *name, size_t n)
Definition: Block.cpp:57
BlockParamBase(Block *parent, const char *name, bool parent_prefix=true)
Instantiate a block param base.
Definition: BlockParam.cpp:49
static constexpr uint8_t blockNameLengthMax
Definition: Block.hpp:51
Controller library code.
Definition: reflect.c:56
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
const char * name
Definition: tests_main.c:58
BlockParam(Block *block, const char *name, bool parent_prefix=true)
List< BlockParamBase * > & getParams()
Definition: Block.hpp:85