29#include "phymac-clmsg.h"
44const std::string
sep = {
"::"};
49 std::chrono::milliseconds(3000);
51std::vector<std::pair<std::string, UwMODAModem::ModemResponse> >
53 std::make_pair(
"RX_STARTED", ModemResponse::RX_BEG),
54 std::make_pair(
"TX_ENDED", ModemResponse::TX_END),
55 std::make_pair(
"CFG_ENDED", ModemResponse::CFG_END)
58std::map<UwMODAModem::ModemState, std::string>
61 std::make_pair(ModemState::TRANSMITTING,
"TRANSMITTING"),
62 std::make_pair(ModemState::RECEIVING,
"RECEIVING"),
63 std::make_pair(ModemState::CONFIGURING,
"CONFIGURING")
75 : TclClass(
"Module/UW/UwModem/MODA")
80 create(
int args,
const char *
const *argv)
102 , signal_tag(
"DRIVER")
121 hdr_cmn *ch = HDR_CMN(p);
122 hdr_MPhy *ph = HDR_MPHY(p);
124 if (ch->direction() == hdr_cmn::UP) {
139 ph->rxtime = ph->txtime;
141 ph->worth_tracing =
false;
143 ph->srcSpectralMask = getTxSpectralMask(p);
144 ph->srcAntenna = getTxAntenna(p);
145 ph->srcPosition = getPosition();
146 ph->dstSpectralMask = 0;
152 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
167 if (!strcmp(argv[1],
"setSignalAddress")) {
185 double tx_duration = -1;
195 tx_duration = 0.560833333 + 0.013333333 * (uwalh->
binPktLength());
206 if (m->type() == CLMSG_MAC2PHY_GETTXDURATION) {
207 Packet *p = ((ClMsgMac2PhyGetTxDuration *) m)->pkt;
208 hdr_cmn *ch = HDR_CMN(p);
210 if (ch->direction() == hdr_cmn::DOWN) {
215 "MODAMODEM",
"recvSyncClMsg::GET_TXDURATION "
216 + std::to_string(duration));
218 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(duration);
224 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(-1);
229 return MPhy::recvSyncClMsg(m);
236 "startRx::CALL_PHY2MACSTARTRX");
253 std::string err_msg =
"SIGNALING_CHANNEL_FAILED_TO_OPEN_AT_PORT:"
260 std::string err_msg =
"DATA_CHANNEL_FAILED_TO_OPEN_AT_PORT:"
293 "MODAMODEM",
"SIGNAL_CONNECTION_UNABLE_TO_CLOSE");
296 "MODAMODEM",
"DATA_CONNECTION_UNABLE_TO_CLOSE");
314 std::unique_lock<std::mutex> state_lock(
status_m);
316 return status == ModemState::RECEIVING;
327 r_bytes +=
data_conn->readFromDevice(&(*beg_it) + r_bytes,
331 rx_payload = std::string(beg_it, beg_it + r_bytes);
335 Packet *p = Packet::alloc();
360 HDR_CMN(p)->direction() = hdr_cmn::UP;
372 auto end_it = beg_it;
376 int r_bytes =
signal_conn->readFromDevice(&(*end_it),
378 end_it = end_it + r_bytes;
399 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
416 "transmittingData::BLOCKING_ON_NEXT_PACKET");
430 if (tag_it != end_it) {
434 auto comm_it = std::search(beg_it,
438 if (comm_it != end_it) {
444 auto size_beg = std::search(comm_it, end_it,
sep.begin(),
sep.end());
445 auto size_end = std::search(comm_it, end_it,
447 std::string size_s = std::string(size_beg+
sep.size(), size_end);
450 offset = end_it - (size_end +
end_delim.size());
454 end_it = beg_it + offset;
469 std::lock_guard<std::mutex> status_lock(
status_m);
501 std::unique_lock<std::mutex> state_lock(
status_m);
503 return status == ModemState::AVAILABLE;
509 if ((
data_conn->writeToDevice(payload)) < 0) {
512 "startTx::FAIL_TO_WRITE_DATA_TO_DEVICE");
516 std::function<void(
UwModem &, Packet * p)> callback =
Class to create the Otcl shadow object for an object of the class UwMODAModem.
TclObject * create(int args, const char *const *argv)
std::thread rx_thread
Thread managing the data reception process.
virtual int getModulationType(Packet *p)
Method that returns the modulation type used for the packet being transmitted.
int premodulation
True if premodulation is on, false otherwise.
void createRxPacket(Packet *p)
Method that creates a packet from the received stream of bytes.
const std::string sep
String separator used in reception signaling.
virtual void endRx(Packet *p)
Method that ends a packet reception.
ModemState status
Variable holding the current status of the modem.
void receivingSignaling()
Method that dispacthes a thread dedicated to receiving signaling from the signaling connector Allows ...
ModemResponse parseSignaling(std::vector< char >::iterator &end_it)
Method that parses the content of the signaling data buffer to retrieve signaling messages.
void receivingData()
Method that dispatch a thread dedicated to receiving data from the data connector.
std::mutex status_m
Mutex associated with the state machine of the modem.
static const int SIGNALING_ADDRESS
Port of the signaling channel.
virtual void recv(Packet *p)
Method that handles the reception of packets arriving from upper layers of the network simulator.
std::thread tx_thread
Thread managing the data transmission process.
virtual double getTxDuration(Packet *p)
Method that returns the duration of a given transmitted packet.
void updateStatus(ModemResponse response)
Method that, based on the received signaling, updates the state machine of the driver.
std::string rx_payload
String that is updated with each new received message.
void start()
Method that starts the driver operations.
std::thread sig_thread
Thread managing the signaling reception process.
std::atomic< bool > receiving
Atomic boolean variable that controls the receiving looping thread.
virtual void startTx(Packet *p)
Method that triggers the transmission of a packet through a specified modem.
std::condition_variable status_cv
Condition variable that is linked with the status variable.
std::mutex tx_queue_m
Mutex associated with the transmission queue.
ModemResponse
Enum type representing modem responses.
static const std::chrono::milliseconds MODEM_TIMEOUT
Maximum time to wait for modem to become ModemState::AVAILABLE.
std::condition_variable tx_queue_cv
Condition variable that is linked with the transmitting queue.
static std::vector< std::pair< std::string, ModemResponse > > signaling_dict
Dictionary of accepted signaling states.
void stop()
Method that stops the driver operations.
void transmittingData()
Method that dispatches a thread dedicated to transmitting data through the data connector.
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
static std::map< ModemState, std::string > stateToString
Dictionary for converting a state to a printable string.
std::string signal_tag
Signaling tag to recognize signaling from the modem.
UwMODAModem()
Constructor of the UwMODAModem class.
const std::string end_delim
String end delimiter used in reception signaling.
std::vector< char > signal_buffer
Bytes buffer for the signaling channel (unparsed data)
ModemState
Enum type for the modem general state.
virtual void startRx(Packet *p)
Method that starts a packet reception.
virtual ~UwMODAModem()
Destructor of the UwMODAModem class.
int rx_size
Size of each new received message, coming from signaling.
std::unique_ptr< UwConnector > signal_conn
Signaling connector: used ot retrieve signaling coming from the modem signaling's channel.
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
static const int DATA_ADDRESS
Port of the data channel.
std::atomic< bool > transmitting
Atomic boolean variable that controls the transmitting looping thread.
std::string signal_address
std::unique_ptr< UwConnector > data_conn
Data connector: used ot retrieve data coming from the modem data socket.
Class that implements the interface to DESERT, as used through Tcl scripts.
void realTxEnded(Packet *p)
Method to call endTx from end of real packet transmission.
std::string modem_address
String containing the address needed to connect to the device In case of socket, it may be expressed ...
virtual void recv(Packet *p)=0
Method that handles the reception of packets arriving from the upper layers of the network simulator.
std::queue< ModemEvent > event_q
Queue of events that are scheduled for NS2 to execute (callbacks)
int MAX_READ_BYTES
Maximum number of bytes to be read by a single dump of data.
CheckTimer * checkTimer
Pointer to an object to schedule the "check-modem" events.
std::queue< Packet * > tx_queue
Modem's transmission queue: holds packets that are to be transmitted.
void printOnLog(LogLevel log_level, string module, string message)
Function that, given the appropriate level of log, prints to the set log file the provided log messag...
unsigned int DATA_BUFFER_LEN
Size of the buffer that holds data.
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
std::vector< char > data_buffer
Char buffer (vector) that holds data read from the modem (unparsed data) Main container for data rece...
double period
Checking period of the modem's buffer.
Class that implements a TCP or UDP socket.
hdr_uwal describes the packet header used by Uwal objects.
char * binPkt()
Return to the binPkt_ array pointer.
uint32_t & binPktLength()
Reference to the binPktLength_ variable.
const std::string end_delim
UwMODAModem_TclClass class_modamodem
Driver for the MODA SDM acoustic UW modem.
Class that implements a connector and, specifically, the socket connector. BSD sockets are used,...