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  sizeofLUT = 0;
59 }
60 
61 
63 {
64  memset(LUT, 0, sizeof(LUT));
65  sizeofLUT = 0;
66 }
67 
68 
70 {
71  return(sizeofLUT);
72 }
73 
75 {
76  if(NULL == res)
77  {
78  return(0);
79  }
80 
81  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
82  uint8_t index = 0;
83  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
84  index --;
85 
86 
87  if(index>=maxEthBoards)
88  {
89  return 0;
90  }
91 
92  if(NULL == LUT[index].resource)
93  {
94  return 0;
95  }
96 
97  return(LUT[index].numberofinterfaces);
98 }
99 
100 
102 {
103  if(NULL == res)
104  {
105  return false;
106  }
107 
108  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
109  uint8_t index = 0;
110  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
111  index --;
112 
113  if(index>=maxEthBoards)
114  {
115  //yError() << "eth::EthBoards::add() fails because index =" << index << "is >= maxEthBoards" << maxEthBoards;
116  return false;
117  }
118 
119  if(NULL != LUT[index].resource)
120  {
121  return false;
122  }
123 
124  LUT[index].resource = res;
125  LUT[index].ipv4 = ipv4;
126  LUT[index].boardnumber = index;
127  LUT[index].name = res->getProperties().boardnameString;
128  LUT[index].numberofinterfaces = 0;
129  for(int i=0; i<iethresType_numberof; i++)
130  {
131  LUT[index].interfaces[i] = NULL;
132  }
133 
134  sizeofLUT++;
135 
136  return true;
137 }
138 
139 
141 {
142  if((NULL == res) || (NULL == interface))
143  {
144  return false;
145  }
146 
147  iethresType_t type = interface->type();
148 
149  if(iethres_none == type)
150  {
151  return false;
152  }
153 
154  // now i compute the index
155  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
156  uint8_t index = 0;
157  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
158  index --;
159  if(index>=maxEthBoards)
160  {
161  return false;
162  }
163 
164  if(res != LUT[index].resource)
165  {
166  return false;
167  }
168 
169  if(NULL != LUT[index].interfaces[type])
170  {
171  return false;
172  }
173 
174  // ok, i add it
175  LUT[index].interfaces[type] = interface;
176  LUT[index].numberofinterfaces ++;
177 
178 
179  return true;
180 }
181 
182 
184 {
185  if(NULL == res)
186  {
187  return false;
188  }
189 
190  // now i compute the index
191  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
192  uint8_t index = 0;
193  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
194  index --;
195  if(index>=maxEthBoards)
196  {
197  return false;
198  }
199 
200  if(res != LUT[index].resource)
201  {
202  return false;
203  }
204 
205  LUT[index].resource = NULL;
206  LUT[index].ipv4 = 0;
207  LUT[index].boardnumber = 0;
208  LUT[index].name = "";
209  LUT[index].numberofinterfaces = 0;
210  for(int i=0; i<iethresType_numberof; i++)
211  {
212  LUT[index].interfaces[i] = NULL;
213  }
214 
215  sizeofLUT--;
216 
217  return true;
218 }
219 
220 
222 {
223  if((NULL == res) || (iethres_none == type))
224  {
225  return false;
226  }
227 
228  // now i compute the index
229  eOipv4addr_t ipv4 = res->getProperties().ipv4addr;
230  uint8_t index = 0;
231  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
232  index --;
233  if(index>=maxEthBoards)
234  {
235  return false;
236  }
237 
238  if(res != LUT[index].resource)
239  {
240  return false;
241  }
242 
243  if(NULL != LUT[index].interfaces[type])
244  {
245  LUT[index].interfaces[type] = NULL;
246  LUT[index].numberofinterfaces --;
247  }
248 
249  return true;
250 }
251 
252 
254 {
255  eth::AbstractEthResource * ret = NULL;
256 
257  uint8_t index = 0;
258  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
259  index --;
260  if(index<maxEthBoards)
261  {
262  ret = LUT[index].resource;
263  }
264 
265  return(ret);
266 }
267 
268 bool eth::EthBoards::get_LUTindex(eOipv4addr_t ipv4, uint8_t &index)
269 {
270  index = 0;
271  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
272  index --;
273 
274  if(index>=maxEthBoards)
275  {
276  return false;
277  }
278 
279  if(NULL == LUT[index].resource)
280  {
281  return false;
282  }
283  return true;
284 }
285 
287 {
288  eth::IethResource *dev = NULL;
289  uint8_t index;
290 
291  if(!get_LUTindex(ipv4, index))
292  {
293  return NULL;
294  }
295 
296  if(iethres_none == type)
297  {
298  return NULL;
299  }
300 
301  dev = LUT[index].interfaces[type];
302 
303  return dev;
304 
305 }
306 
307 
308 eth::IethResource* eth::EthBoards::get_interface(eOipv4addr_t ipv4, eOprotID32_t id32)
309 {
310  eth::IethResource *dev = NULL;
311 
312  uint8_t index = 0;
313  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
314  index --;
315  if(index>=maxEthBoards)
316  {
317  return NULL;
318  }
319 
320  if(NULL == LUT[index].resource)
321  {
322  return dev;
323  }
324 
326  eOprotEndpoint_t ep = eoprot_ID2endpoint(id32);
327  switch(ep)
328  {
329  case eoprot_endpoint_management:
330  {
331  type = iethres_management;
332  } break;
333 
334  case eoprot_endpoint_motioncontrol:
335  {
336  type = iethres_motioncontrol;
337  } break;
338 
339  case eoprot_endpoint_skin:
340  {
341  type = iethres_skin;
342  } break;
343 
344  case eoprot_endpoint_analogsensors:
345  {
346  eOprotEntity_t en = eoprot_ID2entity(id32);
347  if(eoprot_entity_as_strain == en)
348  type = iethres_analogstrain;
349  else if(eoprot_entity_as_mais == en)
350  type = iethres_analogmais;
351  else if(eoprot_entity_as_inertial3 == en)
353  else if(eoprot_entity_as_temperature == en)
354  type = iethres_analogstrain;
355  else if(eoprot_entity_as_psc == en)
356  type = iethres_analogpsc;
357  else if(eoprot_entity_as_pos == en)
358  type = iethres_analogpos;
359  else if(eoprot_entity_as_ft == en)
360  type = iethres_analogft;
361  else if(eoprot_entity_as_battery == en)
362  type = iethres_analogbattery;
363  else
364  type = iethres_none;
365  } break;
366 
367  default:
368  {
369  type = iethres_none;
370  } break;
371  }
372 
373  if(iethres_none != type)
374  {
375  dev = LUT[index].interfaces[type];
376  }
377 
378 
379 
380  return dev;
381 }
382 
383 
384 const string & eth::EthBoards::name(eOipv4addr_t ipv4)
385 {
386  const string & ret = "none";
387 
388  uint8_t index = 0;
389  eo_common_ipv4addr_to_decimal(ipv4, NULL, NULL, NULL, &index);
390  index --;
391  if(index<maxEthBoards)
392  {
393  return LUT[index].name;
394  }
395  else
396  {
397  return errorname[0]; // the last one contains an error string
398  }
399 
400  return(ret);
401 }
402 
403 
404 
405 bool eth::EthBoards::execute(void (*action)(eth::AbstractEthResource* res, void* p), void* par)
406 {
407  if(NULL == action)
408  {
409  return(false);
410  }
411 
412  for(int i=0; i<maxEthBoards; i++)
413  {
414  eth::AbstractEthResource* res = LUT[i].resource;
415  if(NULL != res)
416  {
417  action(res, par);
418  }
419 
420  }
421 
422  return(true);
423 }
424 
425 
426 bool eth::EthBoards::execute(eOipv4addr_t ipv4, void (*action)(eth::AbstractEthResource* res, void* p), void* par)
427 {
428  if(NULL == action)
429  {
430  return(false);
431  }
432 
433  eth::AbstractEthResource* res = get_resource(ipv4);
434 
435  if(NULL == res)
436  {
437  return(false);
438  }
439 
440  action(res, par);
441 
442  return(true);
443 }
444 
445 
446 
447 // - end-of-file (leave a blank line after)----------------------------------------------------------------------------
448 
449 
450 
451 
452 
virtual const Properties & getProperties()=0
size_t number_of_interfaces(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:74
const string & name(eOipv4addr_t ipv4)
Definition: ethBoards.cpp:384
eth::IethResource * get_interface(eOipv4addr_t ipv4, eOprotID32_t id32)
Definition: ethBoards.cpp:308
bool execute(void(*action)(eth::AbstractEthResource *res, void *p), void *par)
Definition: ethBoards.cpp:405
bool add(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:101
bool rem(eth::AbstractEthResource *res)
Definition: ethBoards.cpp:183
size_t number_of_resources(void)
Definition: ethBoards.cpp:69
eth::AbstractEthResource * get_resource(eOipv4addr_t ipv4)
Definition: ethBoards.cpp:253
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