iCub-main
ethManager.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2017 iCub Facility - Istituto Italiano di Tecnologia
5  * Author: Alberto Cardellino, Marco Accame
6  * email: alberto.cardellino@iit.it, marco.accame@iit.it
7  * website: www.robotcub.org
8  * Permission is granted to copy, distribute, and/or modify this program
9  * under the terms of the GNU General Public License, version 2 or any
10  * later version published by the Free Software Foundation.
11  *
12  * A copy of the license can be found at
13  * http://www.robotcub.org/icub/license/gpl.txt
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18  * Public License for more details
19 */
20 
21 // - include guard ----------------------------------------------------------------------------------------------------
22 
23 #ifndef _THEETHMANAGER_H_
24 #define _THEETHMANAGER_H_
25 
26 // Standard includes
27 #include <iostream>
28 #include <vector>
29 #include <list>
30 #include <string>
31 #include <stdio.h>
32 #include <mutex>
33 //#include <map>
34 
35 
36 // ACE includes
37 #include <ace/ACE.h>
38 #include <ace/config.h>
39 #include <ace/Thread.h>
40 #include <ace/SOCK_Dgram_Bcast.h>
41 #include "ace/OS_main.h"
42 #include "ace/OS_NS_string.h"
43 #include "ace/OS_NS_sys_socket.h"
44 
45 // YARP includes
46 #include <yarp/os/Bottle.h>
47 #include <yarp/os/Time.h>
48 #include <yarp/os/Port.h>
49 
50 // embobjlib includes
51 #include "hostTransceiver.hpp"
52 #include "FeatureInterface.h"
53 #include "IethResource.h"
54 #include "abstractEthResource.h"
55 
56 // embobj includes
57 #include "EoProtocol.h"
58 #include "EoBoards.h"
59 
60 // others
61 #include <yarp/os/LogStream.h> // iCub debug class include
62 
63 
64 using namespace yarp::os;
65 using namespace std;
66 
67 
68 
69 
70 #include <ethBoards.h>
71 #include <ethSender.h>
72 #include <ethReceiver.h>
73 
74 
75 // -- class TheEthManager
76 // -- it is the main singleton which delas with eth communication.
77 // -- it holds the two tx and rx threads (classes EthSender and EthReceiver)
78 // -- it holds class EthBoards which stores references to EthResource (what is used to form / parse UDP packets for the eth board)
79 // -- and to the interfaces which use the EthResource of a given board.
80 
81 namespace eth {
82 
84  {
85 
86  public:
87  // this is the maximum number of boards that the singleton can manage.
88  enum { maxBoards = eth::EthBoards::maxEthBoards };
89 
90  enum { maxRXpacketsize = 1496, maxTXpacketsize = 1496 };
91 
92  // these are the boards, their use is protected by txSem or rxSem or both of them.
94 
95  private:
96 
97  // singletons have private constructor / destructor
98  TheEthManager();
99  ~TheEthManager();
100 
101  public:
102 
103  // singletons use this method to retrieve an handle to the object
104  static TheEthManager* instance();
105 
106  // we use this method to for a destruction of the object.
107  static bool killYourself();
108 
109 
110  // useful for printing times relative to start of the object
111  double getLifeTime(void);
112 
113  bool verifyEthBoardInfo(yarp::os::Searchable &cfgtotal, eOipv4addr_t &boardipv4, string boardipv4string, string boardname);
114 
115 
116  eth::AbstractEthResource* requestResource2(IethResource *interface, yarp::os::Searchable &cfgtotal);
117 
118  int releaseResource2(eth::AbstractEthResource* ethresource, IethResource* interface);
119 
120  const eOipv4addressing_t& getLocalIPV4addressing(void);
121 
122  bool Transmission(void);
123 
124  bool CheckPresence(void);
125 
126  bool Reception(eOipv4addr_t from, uint64_t* data, ssize_t size);
127 
128  eth::AbstractEthResource* getEthResource(eOipv4addr_t ipv4);
129 
130  IethResource* getInterface(eOipv4addr_t ipv4, eOprotID32_t id32);
131 
132  IethResource* getInterface(eOipv4addr_t ipv4, iethresType_t type);
133 
134  int getNumberOfResources(void);
135 
136  const string & getName(eOipv4addr_t ipv4);
137 
138  int sendPacket(const void *udpframe, size_t len, const eOipv4addressing_t &toaddressing);
139 
140  eOipv4addr_t toipv4addr(const ACE_INET_Addr &aceinetaddr);
141 
142  ACE_INET_Addr toaceinet(const eOipv4addressing_t &ipv4addressing);
143 
144  private:
145 
146 
147  bool isCommunicationInitted(void);
148 
149  bool createCommunicationObjects(const eOipv4addressing_t &localaddress, int txrate, int rxrate);
150 
151  bool initCommunication(yarp::os::Searchable &cfgtotal);
152 
153  bool stopCommunicationThreads(void);
154 
155  bool lock(bool on);
156 
157  bool lockTX(bool on);
158  bool lockRX(bool on);
159  bool lockTXRX(bool on);
160 
161 
162  private:
163 
164  void initEOYsystem(void);
165 
166  // this semaphore is used to ....
167  static std::mutex managerSem;
168  // the following two semaphore are used separately or together to stop tx and rx if a change is done on ethboards (in startup and shutdown phases)
169  static std::mutex txSem;
170  static std::mutex rxSem;
171 
172  static eth::TheEthManager* handle;
173 
174  // contains the start-up time of the system so that time measures / prints can be done relative.
175  double startUpTime;
176 
177  eOipv4addressing_t ipv4local;
178 
179  bool communicationIsInitted;
180 
181  // periodic threads which use methods of class TheEthManager to transmit / receive + the udp socket
182  eth::EthSender* sender;
183  eth::EthReceiver* receiver;
184  ACE_SOCK_Dgram* UDP_socket;
185  bool embBoardsConnected;
186 
187  };
188 
189 } // namespace eth
190 
191 
192 #endif // include-guard
193 
194 
195 // - end-of-file (leave a blank line after)----------------------------------------------------------------------------
196 
197 
198 
199 
200 
201 
@ data
eth::EthBoards * ethBoards
Definition: ethManager.h:93
iethresType_t
Definition: IethResource.h:62
static pcap_t * handle
grid on
Definition: show_eyes_axes.m:5