iCub-main
Loading...
Searching...
No Matches
serviceParserCanBattery.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT)
3 * All rights reserved.
4 * Author: Luca Tricerri
5 * This software may be modified and distributed under the terms of the
6 * BSD-3-Clause license. See the accompanying LICENSE file for details.
7 */
9
10#include <yarp/os/Log.h>
11#include <yarp/os/LogStream.h>
12
16
18{
19 Bottle propertyCanBoard = Bottle(property.findGroup("CANBOARDS"));
20 if (propertyCanBoard.isNull())
21 {
22 yError() << "ServiceParserCanBattery::check() cannot find PROPERTIES.CANBOARDS";
23 return false;
24 }
25
26 Bottle propertyCanBoardType = propertyCanBoard.findGroup("type");
27 if (propertyCanBoardType.isNull())
28 {
29 yError() << "ServiceParserCanBattery::check() cannot find "
30 "PROPERTIES.CANBOARDS.type";
31 return false;
32 }
33
34 Bottle propertyCanBoardProtocol = Bottle(propertyCanBoard.findGroup("PROTOCOL"));
35 if (propertyCanBoardProtocol.isNull())
36 {
37 yError() << "ServiceParserCanBattery::check() cannot find "
38 "PROPERTIES.CANBOARDS.PROTOCOL";
39 return false;
40 }
41
42 Bottle propertyCanBoardProtocolMajor = Bottle(propertyCanBoardProtocol.findGroup("major"));
43 if (propertyCanBoardProtocolMajor.isNull())
44 {
45 yError() << "ServiceParserCanBattery::check() cannot find "
46 "PROPERTIES.CANBOARDS.PROTOCOL.major";
47 return false;
48 }
49
50 Bottle propertyCanBoardProtocolMinor = Bottle(propertyCanBoardProtocol.findGroup("minor"));
51 if (propertyCanBoardProtocolMinor.isNull())
52 {
53 yError() << "ServiceParserCanBattery::check() cannot find "
54 "PROPERTIES.CANBOARDS.PROTOCOL.minor";
55 return false;
56 }
57
58 Bottle propertyCanBoardFirmware = Bottle(propertyCanBoard.findGroup("FIRMWARE"));
59 if (propertyCanBoardFirmware.isNull())
60 {
61 yError() << "ServiceParserCanBattery::check() cannot find "
62 "PROPERTIES.CANBOARDS.FIRMWARE";
63 return false;
64 }
65
66 Bottle propertyCanBoardFirmwareMajor = Bottle(propertyCanBoardFirmware.findGroup("major"));
67 if (propertyCanBoardFirmwareMajor.isNull())
68 {
69 yError() << "ServiceParserCanBattery::check() cannot find "
70 "PROPERTIES.CANBOARDS.FIRMWARE.major";
71 return false;
72 }
73
74 Bottle propertyCanBoardFirmwareMinor = Bottle(propertyCanBoardFirmware.findGroup("minor"));
75 if (propertyCanBoardFirmwareMinor.isNull())
76 {
77 yError() << "ServiceParserCanBattery::check() cannot find "
78 "PROPERTIES.CANBOARDS.FIRMWARE.minor";
79 return false;
80 }
81
82 Bottle propertyCanBoardFirmwareBuild = Bottle(propertyCanBoardFirmware.findGroup("build"));
83 if (propertyCanBoardFirmwareBuild.isNull())
84 {
85 yError() << "ServiceParserCanBattery::check() cannot find "
86 "PROPERTIES.CANBOARDS.FIRMWARE.build";
87 return false;
88 }
89
90 // Check size
91 if (propertyCanBoardType.size() != 2)
92 {
93 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> protocol type size";
94 return false;
95 }
96 if (propertyCanBoardProtocolMajor.size() != 2)
97 {
98 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> protocol major size";
99 return false;
100 }
101 if (propertyCanBoardProtocolMinor.size() != 2)
102 {
103 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> firmware minor size";
104 return false;
105 }
106 if (propertyCanBoardFirmwareMajor.size() != 2)
107 {
108 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> firmware major size";
109 return false;
110 }
111 if (propertyCanBoardFirmwareMinor.size() != 2)
112 {
113 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> firmware minor size";
114 return false;
115 }
116 if (propertyCanBoardFirmwareBuild.size() != 2)
117 {
118 yError() << "ServiceParserCanBattery::checkPropertyCanBoards --> firmware build size";
119 return false;
120 }
121
122 std::string boardType = propertyCanBoardType.get(1).asString();
123
124 eObrd_type_t currentBoard = checkBoardType(boardType);
125 if (currentBoard == eobrd_unknown)
126 return false;
127
128 batteryInfo_.majorProtocol = propertyCanBoardProtocolMajor.get(1).asInt32();
129 batteryInfo_.minorProtocol = propertyCanBoardProtocolMinor.get(1).asInt32();
130 batteryInfo_.majorFirmware = propertyCanBoardFirmwareMajor.get(1).asInt32();
131 batteryInfo_.minorFirmware = propertyCanBoardFirmwareMinor.get(1).asInt32();
132 batteryInfo_.buildFirmware = propertyCanBoardFirmwareBuild.get(1).asInt32();
133
134 return true;
135}
136
138{
139 Bottle propertySensors = Bottle(property.findGroup("SENSORS"));
140 if (propertySensors.isNull())
141 {
142 yError() << "ServiceParserCanBattery::checkPropertySensors() cannot find "
143 "PROPERTIES.SENSORS";
144 return false;
145 }
146
147 Bottle propertySensorsId = Bottle(propertySensors.findGroup("id"));
148 if (propertySensorsId.isNull())
149 {
150 yError() << "ServiceParserCanBattery::checkPropertySensors() cannot find "
151 "PROPERTIES.SENSORS.id";
152 return false;
153 }
154
155 Bottle propertySensorsBoard = Bottle(propertySensors.findGroup("board"));
156 if (propertySensorsBoard.isNull())
157 {
158 yError() << "ServiceParserCanBattery::checkPropertySensors() cannot find "
159 "PROPERTIES.SENSORS.type";
160 return false;
161 }
162
163 Bottle propertySensorsLocation = Bottle(propertySensors.findGroup("location"));
164 if (propertySensorsLocation.isNull())
165 {
166 yError() << "ServiceParserCanBattery::checkPropertySensors() cannot find "
167 "PROPERTIES.SENSORS.location";
168 return false;
169 }
170
171 // CHECK size
172 if (propertySensorsId.size() != 2)
173 {
174 yError() << "ServiceParserCanBattery::checkPropertySensors --> id size";
175 return false;
176 }
177 if (propertySensorsBoard.size() != 2)
178 {
179 yError() << "ServiceParserCanBattery::checkPropertySensors --> board size";
180 return false;
181 }
182 if (propertySensorsLocation.size() != 2)
183 {
184 yError() << "ServiceParserCanBattery::checkPropertySensors --> location size";
185 return false;
186 }
187
188 std::string id = propertySensorsId.get(1).asString();
189 std::string board = propertySensorsBoard.get(1).asString();
190 eObrd_type_t currentBoard = checkBoardType(board);
191 if (currentBoard == eobrd_unknown)
192 return false;
193 std::string location = propertySensorsLocation.get(1).asString();
194
195 try
196 {
197 batteryInfo_.port = std::stoi(location.substr(3, 1));
198 }
199 catch (const std::exception &)
200 {
201 yError() << "ServiceParser::checkPropertySensors() invalid can port";
202 return false;
203 }
204 try
205 {
206 batteryInfo_.address = std::stoi(location.substr(5, location.size() - 5));
207 }
208 catch (const std::exception &)
209 {
210 yError() << "ServiceParser::checkPropertySensors() invalid can address";
211 return false;
212 }
213
214 batteryInfo_.board = currentBoard;
215
216 return true;
217}
218
219bool ServiceParserCanBattery::checkSettings(const Bottle &service)
220{
221 Bottle settings = Bottle(service.findGroup("SETTINGS"));
222 if (settings.isNull())
223 {
224 yError() << "ServiceParserCanBattery::checkSettings() cannot find SETTINGS";
225 return false;
226 }
227
228 Bottle settingsBatteryPeriod = Bottle(settings.findGroup("acquisitionRate"));
229 if (settingsBatteryPeriod.isNull())
230 {
231 yError() << "ServiceParserCanBattery::checkSettings() cannot find "
232 "SETTINGS.acquisitionRate";
233 return false;
234 }
235
236 Bottle settingsEnabledSensors = Bottle(settings.findGroup("enabledSensors"));
237 if (settingsEnabledSensors.isNull())
238 {
239 yError() << "ServiceParserCanBattery::checkSettings() cannot find "
240 "SETTINGS.enabledSensors";
241 return false;
242 }
243
244 // Check size
245 int tmp = settingsBatteryPeriod.size();
246 std::string s = settingsBatteryPeriod.toString();
247 if (settingsEnabledSensors.size() != 2)
248 {
249 yError() << "ServiceParserCanBattery::checkSettings --> enabledSensors";
250 return false;
251 }
252 if (settingsBatteryPeriod.size() != 2)
253 {
254 yError() << "ServiceParserCanBattery::checkSettings --> acquisitionRate"
255 "temperaturePeriod size";
256 return false;
257 }
258
259 uint8_t acquisitionRate = (uint8_t)settingsBatteryPeriod.get(1).asInt8();
260 batteryInfo_.acquisitionRate = acquisitionRate;
261 return true;
262}
263
265{
266 if (false == service.check("type"))
267 {
268 yError() << "ServiceParserCanBattery::check() cannot find SERVICE.type";
269 return false;
270 }
271 std::string serviceType = service.find("type").asString();
272 eOmn_serv_type_t serviceTypeEomn = eomn_string2servicetype(serviceType.c_str());
273
274 if (eomn_serv_AS_battery != serviceTypeEomn)
275 {
276 yError() << "ServiceParserCanBattery::check() has found wrong SERVICE.type = " << serviceType << "it must be eomn_serv_AS_battery";
277 return false;
278 }
279 return true;
280}
281
282bool ServiceParserCanBattery::parse(const yarp::os::Searchable &config)
283{
284 Bottle service(config.findGroup("SERVICE"));
285 if (service.isNull())
286 {
287 yError() << "ServiceParser::check() cannot find SERVICE group";
288 return false;
289 }
290
291 Bottle properties = Bottle(service.findGroup("PROPERTIES"));
292 if (properties.isNull())
293 {
294 yError() << "ServiceParser::check() cannot find PROPERTIES";
295 return false;
296 }
297
298 if (!checkServiceType(service))
299 return false;
300
301 if (!checkSettings(service))
302 return false;
303
304 if (!checkPropertySensors(properties))
305 return false;
306
307 if (!checkPropertyCanBoards(properties))
308 return false;
309
310 return true;
311}
312
313bool ServiceParserCanBattery::toEomn(eOmn_serv_config_data_as_battery_t &out) const
314{
315 EOarray *ar = eo_array_New(eOas_battery_sensors_maxnumber, sizeof(eOas_battery_sensordescriptor_t), (void *)(&(out.arrayofsensors)));
316
317 eOas_battery_sensordescriptor_t item;
318 if (!batteryInfo_.toEomn(item))
319 {
320 yError() << "ServiceParserCanBattery::toEomn() wrong data for sensor";
321 return false;
322 }
323 eo_array_PushBack(ar, &item);
324
325 return true;
326}
327
332
333eObrd_type_t ServiceParserCanBattery::checkBoardType(const std::string &boardType)
334{
335 eObrd_type_t type = eoboards_string2type2(boardType.c_str(), eobool_true);
336 if (!eoas_battery_isboardvalid(eoboards_type2cantype(type)))
337 {
338 type = eoboards_string2type2(boardType.c_str(), eobool_false);
339 if (!eoas_battery_isboardvalid(eoboards_type2cantype(type)))
340 {
341 yError() << "checkBoardType --> unsupported board type:" << boardType;
342 return type;
343 }
344 }
345 return type;
346}
int buildFirmware
Definition batteryInfo.h:27
int minorProtocol
Definition batteryInfo.h:24
bool toEomn(eOas_battery_sensordescriptor_t &out) const
eObrd_type_t board
Definition batteryInfo.h:20
int minorFirmware
Definition batteryInfo.h:26
uint8_t acquisitionRate
Definition batteryInfo.h:19
int majorProtocol
Definition batteryInfo.h:23
int majorFirmware
Definition batteryInfo.h:25
virtual bool checkSettings(const Bottle &settings)
bool parse(const yarp::os::Searchable &config)
virtual bool checkPropertySensors(const Bottle &property)
bool toEomn(eOmn_serv_config_data_as_battery_t &out) const
virtual eObrd_type_t checkBoardType(const std::string &boardType)
virtual bool checkServiceType(const Bottle &service)
virtual bool checkPropertyCanBoards(const Bottle &bPropertiesCanBoards)
uint8_t board
out
Definition sine.m:8