DESERT 3.5.1
Loading...
Searching...
No Matches
uwmodemcsa.h
Go to the documentation of this file.
1//
2// Copyright (c) 2019 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
37#ifndef UWMODEMCSA_H
38#define UWMODEMCSA_H
39
40#include <uwconnector.h>
41#include <uwmodem.h>
42
43#include <atomic>
44#include <condition_variable>
45#include <memory>
46#include <mutex>
47#include <thread>
48#include <vector>
49
50enum class ModemState { AVAILABLE, BUSY };
51
52class UwModemCSA : public UwModem
53{
54public:
61 UwModemCSA();
62
66 virtual ~UwModemCSA();
67
74 virtual void recv(Packet *p);
75
82 virtual int recvSyncClMsg(ClMessage *m);
83
91 virtual double getTxDuration(Packet *p);
92
102 virtual int command(int argc, const char *const *argv);
103
104private:
110 virtual void startTx(Packet *p);
111
118 virtual void startRx(Packet *p);
119
125 virtual void endRx(Packet *p);
126
131 virtual void start();
132
138 virtual void stop();
139
143 virtual void transmittingData();
144
148 virtual void receivingData();
149
153 virtual std::string findCommand(std::vector<char>::iterator beg_it,
154 std::vector<char>::iterator end_it,
155 std::vector<char>::iterator &cmd_b,
156 std::vector<char>::iterator &cmd_e);
157
161 virtual bool parseCommand(std::vector<char>::iterator cmd_b,
162 std::vector<char>::iterator cmd_e, std::string &rx_payload);
163
167 virtual std::string buildSend(const std::string &payload, int dest);
168
175 void startRealRx(const std::string &cmd);
176
177 void createRxPacket(Packet *p);
178
180 std::unique_ptr<UwConnector> p_connector;
181
185 std::mutex status_m;
187 std::mutex tx_queue_m;
189 std::condition_variable status_cv;
191 std::condition_variable tx_queue_cv;
193 std::atomic<bool> receiving;
195 std::atomic<bool> transmitting;
197 std::thread rx_thread;
199 std::thread tx_thread;
201 std::string rx_payload;
203 std::string del_b;
205 std::string del_e;
207 const static std::chrono::milliseconds MODEM_TIMEOUT;
208
210 static const double EPSILON_S;
213 static const size_t MAX_TX_STATUS_POLL;
214};
215
216#endif
virtual void endRx(Packet *p)
Method that ends a packet reception.
std::string rx_payload
String that is updated witn each new received messsage.
Definition uwmodemcsa.h:201
virtual void startRx(Packet *p)
Method that starts a packet reception.
ModemState status
Variable holding the current status of the modem.
Definition uwmodemcsa.h:182
virtual bool parseCommand(std::vector< char >::iterator cmd_b, std::vector< char >::iterator cmd_e, std::string &rx_payload)
Method that parses the command to obtain the recquired informations.
virtual std::string findCommand(std::vector< char >::iterator beg_it, std::vector< char >::iterator end_it, std::vector< char >::iterator &cmd_b, std::vector< char >::iterator &cmd_e)
Method that finds the position of a command in a buffer.
static const size_t MAX_TX_STATUS_POLL
maximum number of time to poll the modem transmission status before to discard the transmitted packet
Definition uwmodemcsa.h:213
virtual void recv(Packet *p)
Method that handles the reception of packets arriving from upper layers of the network simulator.
std::condition_variable status_cv
Condition variable to wait for ModemState::AVAILABLE.
Definition uwmodemcsa.h:189
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
virtual void receivingData()
Method that detach a thread devoted to receiving data from the connector.
std::atomic< bool > transmitting
Atomic boolean variable that controls the transmitting looping thread.
Definition uwmodemcsa.h:195
virtual void start()
Method that starts the driver operations.
virtual void stop()
Method that stops the driver operations.
void startRealRx(const std::string &cmd)
Method that updates the status of the modem State Machine: state change is triggered by reception of ...
virtual std::string buildSend(const std::string &payload, int dest)
static const std::chrono::milliseconds MODEM_TIMEOUT
Maximum time to wait for modem to become ModemState::AVAILABLE.
Definition uwmodemcsa.h:207
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
std::condition_variable tx_queue_cv
Condition variable that is linked with the transmitting queue.
Definition uwmodemcsa.h:191
virtual double getTxDuration(Packet *p)
Method that returns the duration of a given transmitted packet.
std::mutex tx_queue_m
Mutex associated with the transmission queue.
Definition uwmodemcsa.h:187
std::atomic< bool > receiving
Atomic boolean variable that controls the receiving looping thread.
Definition uwmodemcsa.h:193
virtual void transmittingData()
Method that detach a thread devoted to sending packets found in tx_queue.
void createRxPacket(Packet *p)
std::thread tx_thread
Object with the tx thread.
Definition uwmodemcsa.h:199
std::thread rx_thread
Object with the rx thread.
Definition uwmodemcsa.h:197
std::unique_ptr< UwConnector > p_connector
Pointer to Connector object that interfaces with the device.
Definition uwmodemcsa.h:180
std::string del_e
Definition uwmodemcsa.h:205
virtual ~UwModemCSA()
Destructor of the UwModemCSA class.
virtual void startTx(Packet *p)
Method that triggers the transmission of a packet through a specified modem.
std::mutex status_m
Mutex associated with the state machine of the modem.
Definition uwmodemcsa.h:185
static const double EPSILON_S
minimum time to wait before to schedule a new event in seconds
Definition uwmodemcsa.h:210
std::string del_b
Definition uwmodemcsa.h:203
UwModemCSA()
Constructor of the UwModemCSA class.
Class that implements the interface to DESERT, as used through Tcl scripts.
Definition uwmodem.h:62
Generic class that provides a method to interface with the devices. Will be specialized for,...
ModemState
Enum containing the possible statuses of the driver.
Header of the main class that implements the general interface between DESERT and real acoustic modem...
ModemState
Definition uwmodemcsa.h:50