iCub-main
EthMaintainer.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) 2016 iCub Facility - Istituto Italiano di Tecnologia
4  * Author: Marco Accame, Alessandro Scalzo
5  * email: marco.accame@iit.it, alessandro.scalzo@iit.it
6  * website: www.robotcub.org
7  * Permission is granted to copy, distribute, and/or modify this program
8  * under the terms of the GNU General Public License, version 2 or any
9  * later version published by the Free Software Foundation.
10  *
11  * A copy of the license can be found at
12  * http://www.robotcub.org/icub/license/gpl.txt
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details
18 */
19 
20 
21 #ifndef __ETHMAINTAINER_H__
22 #define __ETHMAINTAINER_H__
23 
24 
25 #include "EoUpdaterProtocol.h"
26 
27 
28 #include "DSocket.h"
29 #include "EthBoard.h"
30 
31 #include <vector>
32 using namespace std;
33 
34 
35 string ipv4tostring(eOipv4addr_t ipv4);
36 bool string2ipv4(const string &ipv4string, eOipv4addr_t &ipv4);
37 
38 // eOipv4addr_t is in network order, ACE in host order
39 eOipv4addr_t acetoipv4(ACE_UINT32 address);
40 ACE_UINT32 ipv4toace(eOipv4addr_t ipv4);
41 
42 
44 {
45 public:
46 
47 #if defined(WIN32)
48 
49 enum { hostIPaddress = EO_COMMON_IPV4ADDR(10, 0, 1, 104),
50  mainIPport = 3333,
51  ipv4OfAllSelected = EO_COMMON_IPV4ADDR(0, 0, 0, 0),
52  ipv4Broadcast = EO_COMMON_IPV4ADDR(255, 255, 255, 255)
53 };
54 
55 #else
56  static const eOipv4addr_t hostIPaddress; // 10.0.1.104
57  static const eOipv4port_t mainIPport; // 3333
58 
59  static const eOipv4addr_t ipv4OfAllSelected; // 0
60  static const eOipv4addr_t ipv4Broadcast; // 255.255.255.255 ... but it broadcast only int its subnet
61 #endif
62 
63 public:
64 
65  EthMaintainer();
66 
67  ~EthMaintainer();
68 
69  bool open(eOipv4addr_t ipv4 = hostIPaddress, eOipv4port_t port = mainIPport);
70 
71  bool close();
72 
73  // verbose is false by default
74  void verbose(bool on);
75 
76  // debug print is false by default
77  void debugprint(bool on);
78 
79 
80  // by default it is true.
81  // if true: for most complex operations, the results of the queries which go to teh boards are internally stored in permanent
82  // dat structure. As such, if we use an ipv4 address equal to ipv4OfAllSelected, the used IP address are those selected internally.
83  // IN SHORT: this is the method currently used by teh GUI which performs a discovery etc.
84  // if false: the results are not internally stored but where appropriate are returned by the function.
85  // for instance, discover() just returns the found boards. then one must retrieve the relevant ipv4 addresses and use them one by one.
86  // as such one cannot use ipv4 = ipv4OfAllSelected.
87  bool boards_useinternal(bool on);
88 
89 
90  // loads a complete set of boards to the class.
91  bool boards_set(EthBoardList &boards);
92 
93  // retrieves the complet set of boards
94  EthBoardList& boards_get(void);
95 
96  // clear the complete set of boards.
97  bool boards_clr(void);
98 
99  // select / deselect some board(s). ipv4 can be a single value or ipv4Broadcast. it cannot be ipv4OfAllSelected
100  bool boards_select(eOipv4addr_t ipv4, bool on);
101 
102  // it adds one board info at a goven address. if force is true it adds it without checking for mac or ip.
103  // ipv4 cannot be ipv4OfAllSelected or ipv4Broadcast
104  int boards_add(eOipv4addr_t ipv4, boardInfo2_t &info2, bool force);
105 
106  // it removes the specified board(s). it can be a single value, ipv4Broadcast or ipv4OfAllSelected
107  int boards_rem(eOipv4addr_t ipv4);
108 
109 
110  // in here there are complex operations which can group many methods such as discover() or the likes.
111  // all these methods are self-organised and can be used as single command without the need to perform any discovery or else before.
112  // the only requirement is to have an EthMaintainer object and to have it opened with the open() method.
113 
114 
115  // it simply returns a EthBoardList object.
116  // it does NOT attempt to send in maintenance, but old versions of eApplication jump to eUpddater when they receive the uprot_OPC_DISCOVER command.
117  // if clearbeforediscovery is true, it clears the used boardlist.
118  // then the uprot_OPC_DISCOVER command is sent numberofdiscoveries times (1 is enough) with a wait timeout of waittimeout seconds each.
119  // the returned EthBoardList is guaranteed to contain unique MAC addresses. if two boards have the same IP address but different MAC they are counted twice.
120  EthBoardList discover(bool clearbeforediscovery = true, int numberofdiscoveries = 1, double waittimeout = 1.0);
121 
122 
123  // it retrieves information about the board of a given ip address.
124  // warning: the same information can be retrieved by discover
125  // ipv4 can be a single non zero value (e.g., 10.0.1.3) or the ipv4Broadcast value which means that the request is done in broadcast or ipv4OfAllSelected
126  // which means to all the selected boards.
127  // the information is returned in object EthBoardList, where there can be more boards with the same ip address.
128  // we surely guarantee that the mac is unique.
129  EthBoardList information(eOipv4addr_t ipv4, bool ask2board = true, bool forcemaintenance = true, int numberofrequests = 1, double waittimeout = 1.0);
130 
131 
132  // it sends the command eOuprot_cmd_MOREINFO_t to query the boards. if forcemaintenance is true it first send the boards in maintenance.
133  std::string moreinformation(eOipv4addr_t ipv4, bool forcemaintenance = false);
134 
135 
136  // send to maintenance an ipv4 address.
137  // if verify is true, then it checks that the IP address(es) are in maintenance with a max number of retries each interleaved by an interval
138  // as specified by the other parameters.
139  bool go2maintenance(eOipv4addr_t ipv4, bool verify = true, int retries = 6, double timegap = 1.0);
140 
141 
142  // send to application an ipv4 address.
143  // if checkdef2runapplication is true: it checks if def2run is eApplication.
144  // - if it is, then it returns true if boards are already running application
145  // - if it is false, it sends the boards in maintenance, it sets def2run = eApplication and then:
146  // a. it restarts teh boards,
147  // b. it waits for bootstraptime seconds,
148  // c. if verify is true it also checks that the eApplication is running
149  bool go2application(eOipv4addr_t ipv4, bool checkdef2runapplication = true, double bootstraptime = 10.0, bool verify = true);
150 
151 
152  // it programs board(s) w/ ipv4 address which must have:
153  // - same type as argument (unless type is eobrd_ethtype_none or eobrd_ethtype_unknown)
154  // - same process as argument
155  // - same target version as argument (unless targetversion is (0, 0)).
156  // if forceaintenance is true, the boards are forced in maintenance (eUpdater or eApplPROGupdater).
157  // then some checks are done if the board(s) run a process suitable to program what in argument.
158  // if progress is not NULL then it is called to express activity progress
159  // finally, if restart2application is true and process is NOT eUpdater, the board(s) are restarted with 10 seconds
160  // of bootstrap time and check: go2application(ipv4, true, 10, true)
161  bool program(eOipv4addr_t ipv4, eObrd_ethtype_t type, eOuprot_process_t process, eOversion_t targetversion, FILE *fp, bool forcemaintenance = true, void progress(float) = NULL, bool restart2application = true);
162 
163 
164 
165  // raw commands.
166  bool command_supported(eOipv4addr_t ipv4, eOuprot_proc_capabilities_t capability, bool ask2board = false);
167 
168  bool command_def2run(eOipv4addr_t ipv4, eOuprot_process_t process, bool forcemaintenance = true, bool verify = true);
169 
170  bool command_restart(eOipv4addr_t ipv4);
171 
172  bool command_changeaddress(eOipv4addr_t ipv4, eOipv4addr_t ipv4new, bool checkifnewispresent = true, bool forcemaintenance = true, bool restart = false, bool verify = false);
173 
174  bool command_info32_clr(eOipv4addr_t ipv4);
175  bool command_info32_set(eOipv4addr_t ipv4, const string &info32);
176  vector<string> command_info32_get(eOipv4addr_t ipv4);
177 
178  bool command_jump2updater(eOipv4addr_t ipv4);
179 
180  bool command_jump2address(eOipv4addr_t ipv4, uint32_t romaddress);
181 
182  bool command_blink(eOipv4addr_t ipv4);
183 
184  bool command_eeprom_erase(eOipv4addr_t ipv4);
185 
186  bool command_eeprom_read(eOipv4addr_t ipv4, uint16_t from, uint16_t size, uint8_t **value);
187 
188  // used to program a given partition of a given board
189  // capability to be checked is:
190  // - uprot_canDO_PROG_loader: for programming into the loader partition (the first)
191  // - uprot_canDO_PROG_updater: for programming into the updater partition (the second)
192  // - uprot_canDO_PROG_application: for programming into the application partition (the third)
193  bool command_program(eOipv4addr_t ipv4, FILE *programFile, eOuprot_partition2prog_t partition, void (*updateProgressBar)(float), EthBoardList *pboardlist, string &stringresult);
194 
195 
196 
197 protected:
198 
199  typedef struct
200  {
201  vector<EthBoard *> selected;
202  vector<int> steps;
203  int mN2Prog;
205  int mNChunks;
206  void * data;
207  int size;
208  int answers;
209  int retries;
210  } progData_t;
211 
212 
213  bool sendCommand(eOipv4addr_t ipv4, void * cmd, uint16_t len, EthBoardList *boardlist = NULL);
214 
215  int sendPROG2(const uint8_t opc, progData_t &progdata);
216 
217  bool isInMaintenance(eOipv4addr_t ipv4, EthBoardList &boardlist);
218  bool isInApplication(eOipv4addr_t ipv4, EthBoardList &boardlist);
219 
220  string processDiscoveryReplies2(EthBoardList &boardlist, double waittimeout = 1.0);
221 
222  std::string processMoreInfoReplies(EthBoardList &boardlist);
223 
224  // it forms the string returned by cmdGetMoreInfo() by formatting the field
225  // eOuprot_cmd_MOREINFO_REPLY_t::discover sent by the remote board. the formatting can hence be defined in here.
226  std::string prepareMoreInfoText(eOuprot_cmd_DISCOVER_REPLY_t * disc, const char *ipv4string);
227  std::string prepareMoreInfoText(eOuprot_cmd_DISCOVER_REPLY2_t * disc, const char *ipv4string);
228 
229  // it forms the string returned by cmdGetMoreInfo() just copying the field
230  // eOuprot_cmd_MOREINFO_REPLY_t::description[] inside the UDP packet as filled by the remote board.
231  std::string getMoreInfoText(eOuprot_cmd_MOREINFO_REPLY_t *moreinfo, char *ipv4string);
232 
233 private:
234 
235  // in here are methods which we prefer not to be used, but which may be useful in special cases
236 
237  // changes the IP mask of a given board, despite default address 0, it does not support it
238  // capability to be checked is:
239  // - uprot_canDO_LEGACY_IPmask_set
240  // NOTE: it is very dangerous to change the IP mask. it is also very improbable that we want to do it. we keep the method however.
241 // bool cmdChangeMask(eOipv4addr_t newMask, eOipv4addr_t ipv4 = ipv4OfAllSelected);
242 
243  // changes the MAC of a given board
244  // capability to be checked is:
245  // - uprot_canDO_LEGACY_MAC_set
246  // NOTE: we may change the MAC only if ... there are two equal mac addresses on two boards ...
247 // bool cmdChangeMAC(uint64_t newMAC48, eOipv4addr_t ipv4);
248 
249 // bool cmdPageClr(eOuprot_pagesize_t pagesize, eOipv4addr_t ipv4 = ipv4OfAllSelected);
250 // bool cmdPageSet(eOuprot_pagesize_t pagesize, uint8_t *data, eOipv4addr_t ipv4 = ipv4OfAllSelected);
251 // bool cmdPageGet(eOuprot_pagesize_t pagesize, uint8_t **data, eOipv4addr_t ipv4);
252 
253 
254 protected:
255 
256  bool _opened;
257  bool _verbose;
259 
262 
263 
264  unsigned char mRxBuffer[uprot_UDPmaxsize];
265  unsigned char mTxBuffer[uprot_UDPmaxsize];
266 
267  eOipv4addr_t myIPV4addr;
268  eOipv4port_t myIPV4port;
269 
271 };
272 
273 #endif
274 
275 // eof
276 
string ipv4tostring(eOipv4addr_t ipv4)
eOipv4addr_t acetoipv4(ACE_UINT32 address)
bool string2ipv4(const string &ipv4string, eOipv4addr_t &ipv4)
ACE_UINT32 ipv4toace(eOipv4addr_t ipv4)
static const eOipv4addr_t ipv4Broadcast
Definition: EthMaintainer.h:60
static const eOipv4addr_t ipv4OfAllSelected
Definition: EthMaintainer.h:59
eOipv4port_t myIPV4port
static const eOipv4port_t mainIPport
Definition: EthMaintainer.h:57
EthBoardList _internalboardlist
static const eOipv4addr_t hostIPaddress
Definition: EthMaintainer.h:56
eOipv4addr_t myIPV4addr
bool _useofinternalboardlist
cmd
Definition: dataTypes.h:30
static struct bpf_program fp
grid on
Definition: show_eyes_axes.m:5
vector< EthBoard * > selected