31#include <phymac-clmsg.h>
43 std::chrono::milliseconds(210);
58 : TclClass(
"Module/UW/UwModem/ModemCSA")
63 create(
int args,
const char *
const *argv)
100 hdr_cmn *ch = HDR_CMN(p);
101 hdr_MPhy *ph = HDR_MPHY(p);
102 if (ch->direction() == hdr_cmn::UP) {
122 ph->rxtime = ph->txtime;
124 ph->worth_tracing =
false;
126 ph->srcSpectralMask = getTxSpectralMask(p);
127 ph->srcAntenna = getTxAntenna(p);
128 ph->srcPosition = getPosition();
129 ph->dstSpectralMask = 0;
135 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
149 if (!strcmp(argv[1],
"setServer")) {
154 if (!strcmp(argv[1],
"setTCP")) {
159 if (!strcmp(argv[1],
"setUDP")) {
170 if (m->type() == CLMSG_MAC2PHY_GETTXDURATION) {
171 Packet *p = ((ClMsgMac2PhyGetTxDuration *) m)->pkt;
172 hdr_cmn *ch = HDR_CMN(p);
174 if (ch->direction() == hdr_cmn::DOWN) {
180 "recvSyncClMsg::GET_TXDURATION " +
181 std::to_string(duration));
183 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(duration);
188 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(-1);
192 return MPhy::recvSyncClMsg(m);
199 double tx_duration = 0.002404;
210 hdr_mac *mach = HDR_MAC(p);
217 cmd_s =
buildSend(payload, mach->macDA());
222 std::unique_lock<std::mutex> state_lock(
status_m);
224 return status == ModemState::AVAILABLE;
232 "startTx::FAIL_TO_WRITE_TO_DEVICE=" + cmd_s);
238 std::function<void(
UwModem &, Packet * p)> callback =
250 std::string cmd_s(
"");
251 cmd_s.append(
"PACKET,");
252 cmd_s += std::to_string(payload.size());
255 cmd_s.append(
",EPCK");
280 std::cout <<
"ERROR: Modem address not set!" << std::endl;
286 std::cout <<
"ERROR: connection to modem failed to open: "
309 std::cout <<
"UwModemCSA::stop() close conn " << std::endl;
332 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
348 "transmittingData::BLOCKING_ON_NEXT_PACKET");
358 std::vector<char>::iterator beg_it =
data_buffer.begin();
359 std::vector<char>::iterator end_it =
data_buffer.begin();
361 std::vector<char>::iterator cmd_b =
data_buffer.begin();
362 std::vector<char>::iterator cmd_e =
data_buffer.begin();
375 (cmd =
findCommand(beg_it, end_it, cmd_b, cmd_e)) !=
"") {
377 offset = std::distance(cmd_e, end_it);
383 offset = std::distance(cmd_e, end_it);
384 std::copy(cmd_e, end_it, beg_it);
385 std::fill(beg_it + offset, end_it,
'\0');
386 end_it = beg_it + offset;
395 std::vector<char>::iterator end_it, std::vector<char>::iterator &cmd_b,
396 std::vector<char>::iterator &cmd_e)
398 std::string cmd =
"";
405 cmd_b = std::search(beg_it, end_it,
del_b.begin(),
del_b.end());
407 if ((cmd_e = std::search(cmd_b, end_it,
del_e.begin(),
del_e.end())) !=
409 cmd_e +=
del_e.size();
410 return std::string(cmd_b, cmd_e);
418 std::vector<char>::iterator cmd_e, std::string &rx_payload)
421 auto curs_b = find(cmd_b, cmd_e,
',');
422 auto curs_e = find(cmd_b, cmd_e,
',') + 1;
424 if (std::string(cmd_b, curs_b).compare(
"PACKET") != 0)
429 curs_e = find(curs_b, cmd_e,
',');
433 int len = std::stoi(std::string(curs_b, curs_e));
439 auto payload_beg = curs_b;
440 if (payload_beg >= cmd_e)
442 auto payload_end = payload_beg + len;
443 rx_payload = std::string(payload_beg, payload_end);
451 std::unique_lock<std::mutex> state_lock(
status_m);
457 Packet *p = Packet::alloc();
472 HDR_CMN(p)->direction() = hdr_cmn::UP;
Class to create the Otcl shadow object for an object of the class UwModemCSA.
TclObject * create(int args, const char *const *argv)
virtual void endRx(Packet *p)
Method that ends a packet reception.
std::string rx_payload
String that is updated witn each new received messsage.
virtual void startRx(Packet *p)
Method that starts a packet reception.
ModemState status
Variable holding the current status of the modem.
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
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.
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.
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.
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.
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.
std::atomic< bool > receiving
Atomic boolean variable that controls the receiving looping thread.
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.
std::thread rx_thread
Object with the rx thread.
std::unique_ptr< UwConnector > p_connector
Pointer to Connector object that interfaces with the device.
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.
static const double EPSILON_S
minimum time to wait before to schedule a new event in seconds
UwModemCSA()
Constructor of the UwModemCSA class.
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 getModulationType(Packet *P)
Method that should return the modulation type used for the packet being transmitted.
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.
Header of the class providing the Uwal header's description.
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.
Header of the main class that implements the adaptation layer between ns2/NS-Miracle and binary data ...
UwModemCSA_TclClass class_modemcsa
Class that implements a generic modem driver for end-to-end communications.
Definition of ClMsgUwMmac class.
Class that implements a connector and, specifically, the socket connector. BSD sockets are used,...