iCub-main
theNVmanager.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: Marco Accame
6  * email: 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 _THENVMANAGER_H_
24 #define _THENVMANAGER_H_
25 
26 
27 #include <vector>
28 #include <cstdint>
29 
30 #include "EoProtocol.h"
31 #include <hostTransceiver.hpp>
32 
33 namespace eth {
34 
36  {
37  public:
38  static theNVmanager& getInstance();
39 
40  public:
41 
42  enum class ropCode { sig = eo_ropcode_sig, say = eo_ropcode_say };
43 
44  // value and values[i] must point to memory with enough space to host the reply. the ask() functions will just copy the reply
45  // into these memory locations, hence the user must pre-allocate enough memory before calling ask()
46  // but what must be the size of the memory? well, it depends on {ipv4, id32}. in any case, it is upper bounded.
47 
48  // tells if a given ip address is supported
49  bool supported(const eOprotIP_t ipv4);
50 
51  // tells if a given ip address + network variable is supported
52  bool supported(const eOprotIP_t ipv4, const eOprotID32_t id32);
53 
54  // tells the size in bytes of a given network variable. its size is independent from the ip address
55  size_t sizeOfNV(const eOprotID32_t id32);
56 
57  // it returns true only when the remote board replies to the request of a given network variable.
58  // the varible has id32 = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_comm, 0, eoprot_tag_mn_comm_status_managementprotocolversion)
59  bool ping(const eOprotIP_t ipv4, eoprot_version_t &mnprotversion, const double timeout = 0.5, const unsigned int retries = 20);
60 
61 
62  // management of a single variable at a time: ask(), set(), check(), setcheck()
63 
64  // it sends a ask<> ROP to a single network variable and waits the say<> reply ROP until timeout.
65  // result is in value, which must be a buffer with at least sizeOfNV(id32) bytes
66  bool ask(const eOprotIP_t ipv4, const eOprotID32_t id32, void *value, const double timeout = 0.5);
67  bool ask(eth::HostTransceiver *t, const eOprotID32_t id32, void *value, const double timeout = 0.5);
68  // imposes a value to a given network variable. it does not wait nor verify
69  bool set(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value);
70  bool set(eth::HostTransceiver *t, const eOprotID32_t id32, const void *value);
71  // it asks the value of a single network variable and checks vs a given value which points to a buffer of at least sizeOfNV(id32) bytes
72  bool check(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value, const double timeout = 0.5, const unsigned int retries = 0);
73  bool check(eth::HostTransceiver *t, const eOprotID32_t id32, const void *value, const double timeout = 0.5, const unsigned int retries = 0);
74  // it sends set<> ROP to a given varaible and it checks that the value is really written. it repeats this cycle until done, at most retries + 1 times.
75  bool setcheck(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value, const unsigned int retries = 10, double waitbeforecheck = 0.001, double timeout = 0.5);
76  bool setcheck(eth::HostTransceiver *t, const eOprotID32_t id32, const void *value, const unsigned int retries = 10, double waitbeforecheck = 0.001, double timeout = 0.5);
77 
78  // function which must be placed in the reception handlers to unblock the waiting of replies from a given board
79  bool onarrival(const ropCode ropcode, const eOprotIP_t ipv4, const eOprotID32_t id32, const std::uint32_t signature);
80 
81  // function used to wait for a given ROP: ropcode<ipv4, id32, value>
82  // its main use is: set(id32command); wait(sig); read(value)
83  // we could use a compact function such as; command(ipv4, setid32, setvalue, sigid32, sigvalue, timeout);
84  // maybe we also use signature? with: bool wait(const ropCode ropcode, const std::uint32_t signature, const double timeout = 0.5);
85 // bool wait(const ropCode ropcode, const eOprotIP_t ipv4, const eOprotID32_t id32, const double timeout);
86  // function used to simply read the locally cached network variable.
87 // bool read(const eOprotIP_t ipv4, const eOprotID32_t id32, void *value);
88 
89  // it sends a set<id32com, command>, waits for a sig<id32rep, reply> with a timeout, reads reply.
90  bool command(const eOprotIP_t ipv4, const eOprotID32_t id32cmd, const void *cmd, const eOprotID32_t id32rep, void *rep, double timeout = 0.5);
91 
92 
93  // for future use: ask of multiple values on the same ipv4 board.
94 
95 
96  // sends read parallel requests for many network variables to the same ip address and waits
97  bool ask(const eOprotIP_t ipv4, const std::vector<eOprotID32_t> &id32s, const std::vector<void*> &values, const double timeout = 0.5);
98  bool ask(eth::HostTransceiver *t, const std::vector<eOprotID32_t> &id32s, const std::vector<void*> &values, const double timeout = 0.5);
99 
100 
101  // tobedone: i want to group several requests before i start to wait.
102  // i need:
103  // - group_start() which tells the nvmanager to use a given signature for all successive group_add_ask() until group_add_stop()
104  // - group_ask() which adds to the nvmanager a vector of ask rops identified by the same signature.
105  // this function can be repeated as many times one want. typically by different devices but by the same thread.
106  // - group_stop() which starts the wait for the replies. when this function returns, the various values vector will contain
107  // the replies.
108  // some more explanation:
109  // we shall use the same signature if the calling thread is the same as the one which called group_start().
110  // in this way, we can target the operation to a given thread without blocking other threads using normal ask() requests. .
111  // i would say however, that if another thread starts a group_start() during an active session .... it musts wait.
112  // also let's give some limitations:
113  // start of proper ask sending is done by group_stop() and not directly by group_ask().
114  // we can have at most one active session at any time.
115  // we can call group_ask() at most ... 8 times (boh, maybe it is not necessary to give a limit).
116  // the calling thread will be retrieved inside.
117  // i use a ACE_Recursive_Thread_Mutex to perform synch amongst different threads.
118 
119  bool group_start();
120  bool group_ask(eth::HostTransceiver *t, const std::vector<eOprotID32_t> &id32s, const std::vector<void*> &values);
121  bool group_stop(const double timeout = 0.5);
122 
123  private:
124  theNVmanager();
125 
126  public:
127  // remove copy constructors and copy assignment operators
128  theNVmanager(const theNVmanager&) = delete;
130  void operator=(const theNVmanager&) = delete;
131  void operator=(theNVmanager&) = delete;
132 
133  private:
134  struct Impl;
135  Impl *pImpl;
136  };
137 
138 
139 } // namespace eth
140 
141 
142 #endif // include-guard
143 
144 
145 // - end-of-file (leave a blank line after)----------------------------------------------------------------------------
146 
147 
theNVmanager(theNVmanager &)=delete
bool supported(const eOprotIP_t ipv4)
bool check(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value, const double timeout=0.5, const unsigned int retries=0)
bool onarrival(const ropCode ropcode, const eOprotIP_t ipv4, const eOprotID32_t id32, const std::uint32_t signature)
bool group_stop(const double timeout=0.5)
static theNVmanager & getInstance()
theNVmanager(const theNVmanager &)=delete
size_t sizeOfNV(const eOprotID32_t id32)
bool setcheck(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value, const unsigned int retries=10, double waitbeforecheck=0.001, double timeout=0.5)
void operator=(const theNVmanager &)=delete
bool ask(const eOprotIP_t ipv4, const eOprotID32_t id32, void *value, const double timeout=0.5)
bool command(const eOprotIP_t ipv4, const eOprotID32_t id32cmd, const void *cmd, const eOprotID32_t id32rep, void *rep, double timeout=0.5)
bool ping(const eOprotIP_t ipv4, eoprot_version_t &mnprotversion, const double timeout=0.5, const unsigned int retries=20)
bool set(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value)
bool group_ask(eth::HostTransceiver *t, const std::vector< eOprotID32_t > &id32s, const std::vector< void * > &values)
void operator=(theNVmanager &)=delete
cmd
Definition: dataTypes.h:30