iCub-main
ethBoards.cpp
Go to the documentation of this file.
1 // -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 
4 /*
5  * Copyright (C) 2017 iCub Facility - Istituto Italiano di Tecnologia
6  * Author: Marco Accame
7  * email: marco.accame@iit.it
8  * website: www.robotcub.org
9  * Permission is granted to copy, distribute, and/or modify this program
10  * under the terms of the GNU General Public License, version 2 or any
11  * later version published by the Free Software Foundation.
12  *
13  * A copy of the license can be found at
14  * http://www.robotcub.org/icub/license/gpl.txt
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19  * Public License for more details
20 */
21 
22 
23 // --------------------------------------------------------------------------------------------------------------------
24 // - public interface
25 // --------------------------------------------------------------------------------------------------------------------
26 
27 #include "ethBoards.h"
28 
29 
30 
31 // --------------------------------------------------------------------------------------------------------------------
32 // - external dependencies
33 // --------------------------------------------------------------------------------------------------------------------
34 
35 
36 //#include <yarp/os/SystemClock.h>
37 //#include <yarp/os/Log.h>
38 //#include <yarp/os/LogStream.h>
39 //using yarp::os::Log;
40 
41 
42 // --------------------------------------------------------------------------------------------------------------------
43 // - pimpl: private implementation (see scott meyers: item 22 of effective modern c++, item 31 of effective c++
44 // --------------------------------------------------------------------------------------------------------------------
45 
46 
47 
48 // --------------------------------------------------------------------------------------------------------------------
49 // - the class
50 // --------------------------------------------------------------------------------------------------------------------
51 
52 
53 // - class eth::EthBoards
54 
56 {
57  // memset(LUT, 0, sizeof(LUT));
58  // Changed memset with for loop initialization due to problem on Windows
59  // which was failing in updating correctly values to LUT vars
60  for (auto& lut : LUT)
61  {
62  lut.clear();
63  }
64 
65  sizeofLUT = 0;
66 }
67 
68 
70 {
78  for (auto& l : LUT)
79  {
80  l.clear();
81  }
82  sizeofLUT = 0;
83 }
84 
85 
87 {
88  return(sizeofLUT);
89 }
90 
92 {
93  if(NULL == res)
94  {
95  return(0);
96  }
97 
98  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
99  uint8_t index = 0;
100  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
101  index --;
102 
103 
104  if(index>=maxEthBoards)
105  {
106  return 0;
107  }
108 
109  if(NULL == LUT[index].resource)
110  {
111  return 0;
112  }
113 
114  return(LUT[index].numberofinterfaces);
115 }
116 
117 
119 {
120  if(NULL == res)
121  {
122  return false;
123  }
124 
125  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
126  uint8_t index = 0;
127  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
128  index --;
129 
130  if(index>=maxEthBoards)
131  {
132  //yError() << "eth::EthBoards::add() fails because index =" << index << "is >= maxEthBoards" << maxEthBoards;
133  return false;
134  }
135 
136  if(NULL != LUT[index].resource)
137  {
138  return false;
139  }
140 
141  LUT[index].resource = res;
142  LUT[index].ipv4 = ipv4;
143  LUT[index].boardnumber = index;
144  LUT[index].name = res->getProperties().boardnameString;
145  LUT[index].numberofinterfaces = 0;
146  for(int i=0; i<iethresType_numberof; i++)
147  {
148  LUT[index].interfaces[i] = NULL;
149  }
150 
151  sizeofLUT++;
152 
153  return true;
154 }
155 
156 
158 {
159  if((NULL == res) || (NULL == interface))
160  {
161  return false;
162  }
163 
164  iethresType_t type = interface->type();
165 
166  if(iethres_none == type)
167  {
168  return false;
169  }
170 
171  // now i compute the index
172  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
173  uint8_t index = 0;
174  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
175  index --;
176  if(index>=maxEthBoards)
177  {
178  return false;
179  }
180 
181  if(res != LUT[index].resource)
182  {
183  return false;
184  }
185 
186  if(NULL != LUT[index].interfaces[type])
187  {
188  return false;
189  }
190 
191  // ok, i add it
192  LUT[index].interfaces[type] = interface;
193  LUT[index].numberofinterfaces ++;
194 
195 
196  return true;
197 }
198 
199 
201 {
202  if(NULL == res)
203  {
204  return false;
205  }
206 
207  // now i compute the index
208  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
209  uint8_t index = 0;
210  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
211  index --;
212  if(index>=maxEthBoards)
213  {
214  return false;
215  }
216 
217  if(res != LUT[index].resource)
218  {
219  return false;
220  }
221 
222  auto& l = LUT[index];
223  l.clear();
224 
225  sizeofLUT--;
226 
227  return true;
228 }
229 
230 
232 {
233  if((NULL == res) || (iethres_none == type))
234  {
235  return false;
236  }
237 
238  // now i compute the index
239  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
240  uint8_t index = 0;
241  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
242  index --;
243  if(index>=maxEthBoards)
244  {
245  return false;
246  }
247 
248  if(res != LUT[index].resource)
249  {
250  return false;
251  }
252 
253  if(NULL != LUT[index].interfaces[type])
254  {
255  LUT[index].interfaces[type] = NULL;
256  LUT[index].numberofinterfaces --;
257  }
258 
259  return true;
260 }
261 
262 
264 {
265  eth::AbstractEthResource * ret = NULL;
266 
267  uint8_t index = 0;
268  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
269  index --;
270  if(index<maxEthBoards)
271  {
272  ret = LUT[index].resource;
273  }
274 
275  return(ret);
276 }
277 
278 bool eth::EthBoards::get_LUTindex(eOipv4addr_t ipv4, uint8_t &index)
279 {
280  index = 0;
281  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
282  index --;
283 
284  if(index>=maxEthBoards)
285  {
286  return false;
287  }
288 
289  if(NULL == LUT[index].resource)
290  {
291  return false;
292  }
293  return true;
294 }
295 
297 {
298  eth::IethResource *dev = NULL;
299  uint8_t index;
300 
301  if(!get_LUTindex(ipv4, index))
302  {
303  return NULL;
304  }
305 
306  if(iethres_none == type)
307  {
308  return NULL;
309  }
310 
311  dev = LUT[index].interfaces[type];
312 
313  return dev;
314 
315 }
316 
317 
318 eth::IethResource* eth::EthBoards::get_interface(eOipv4addr_t ipv4, eOprotID32_t id32)
319 {
320  eth::IethResource *dev = NULL;
321 
322  uint8_t index = 0;
323  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
324  index --;
325  if(index>=maxEthBoards)
326  {
327  return NULL;
328  }
329 
330  if(NULL == LUT[index].resource)
331  {
332  return dev;
333  }
334 
336  eOprotEndpoint_t ep = eoprot_ID2endpoint(id32);
337  switch(ep)
338  {
339  case eoprot_endpoint_management:
340  {
341  type = iethres_management;
342  } break;
343 
344  case eoprot_endpoint_motioncontrol:
345  {
346  type = iethres_motioncontrol;
347  } break;
348 
349  case eoprot_endpoint_skin:
350  {
351  type = iethres_skin;
352  } break;
353 
354  case eoprot_endpoint_analogsensors:
355  {
356  eOprotEntity_t en = eoprot_ID2entity(id32);
357  if(eoprot_entity_as_strain == en)
358  type = iethres_analogstrain;
359  else if(eoprot_entity_as_mais == en)
360  type = iethres_analogmais;
361  else if(eoprot_entity_as_inertial3 == en)
363  else if(eoprot_entity_as_temperature == en)
364  type = iethres_analogstrain;
365  else if(eoprot_entity_as_psc == en)
366  type = iethres_analogpsc;
367  else if(eoprot_entity_as_pos == en)
368  type = iethres_analogpos;
369  else if(eoprot_entity_as_ft == en)
370  type = iethres_analogft;
371  else if(eoprot_entity_as_battery == en)
372  type = iethres_analogbattery;
373  else
374  type = iethres_none;
375  } break;
376 
377  default:
378  {
379  type = iethres_none;
380  } break;
381  }
382 
383  if(iethres_none != type)
384  {
385  dev = LUT[index].interfaces[type];
386  }
387 
388 
389 
390  return dev;
391 }
392 
393 
394 const string & eth::EthBoards::name(eOipv4addr_t ipv4)
395 {
396  const string & ret = "none";
397 
398  uint8_t index = 0;
399  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
400  index --;
401  if(index<maxEthBoards)
402  {
403  return LUT[index].name;
404  }
405  else
406  {
407  return errorname[0]; // the last one contains an error string
408  }
409 
410  return(ret);
411 }
412 
413 
414 
415 bool eth::EthBoards::execute(void (*action)(eth::AbstractEthResource* res, void* p), void* par)
416 {
417  if(NULL == action)
418  {
419  return(false);
420  }
421 
422  for(int i=0; i<maxEthBoards; i++)
423  {
424  eth::AbstractEthResource* res = LUT[i].resource;
425  if(NULL != res)
426  {
427  action(res, par);
428  }
429 
430  }
431 
432  return(true);
433 }
434 
435 
436 bool eth::EthBoards::execute(eOipv4addr_t ipv4, void (*action)(eth::AbstractEthResource* res, void* p), void* par)
437 {
438  if(NULL == action)
439  {
440  return(false);
441  }
442 
443  eth::AbstractEthResource* res = get_resource(ipv4);
444 
445  if(NULL == res)
446  {
447  return(false);
448  }
449 
450  action(res, par);
451 
452  return(true);
453 }
454 
455 
456 
457 // - end-of-file (leave a blank line after)----------------------------------------------------------------------------
458 
459 
460 
461 
462 
virtual const Properties & getProperties()=0
size_t number_of_interfaces(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:91
const string & name(eOipv4addr_t ipv4)
Definition: ethBoards.cpp:394
eth::IethResource * get_interface(eOipv4addr_t ipv4, eOprotID32_t id32)
Definition: ethBoards.cpp:318
bool execute(void(*action)(eth::AbstractEthResource *res, void *p), void *par)
Definition: ethBoards.cpp:415
bool add(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:118
bool rem(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:200
size_t number_of_resources(void)
Definition: ethBoards.cpp:86
eth::AbstractEthResource * get_resource(eOipv4addr_t ipv4)
Definition: ethBoards.cpp:263
virtual iethresType_t type()=0
iethresType_t
Definition: IethResource.h:62
@ iethres_motioncontrol
Definition: IethResource.h:67
@ iethres_analogmais
Definition: IethResource.h:65
@ iethres_analoginertial3
Definition: IethResource.h:71
@ iethres_analogpsc
Definition: IethResource.h:73
@ iethres_analogft
Definition: IethResource.h:75
@ iethres_management
Definition: IethResource.h:64
@ iethres_skin
Definition: IethResource.h:68
@ iethres_analogbattery
Definition: IethResource.h:76
@ iethres_none
Definition: IethResource.h:63
@ iethres_analogpos
Definition: IethResource.h:74
@ iethres_analogstrain
Definition: IethResource.h:66
@ iethresType_numberof
Definition: IethResource.h:79