29#include "phymac-clmsg.h"
46 std::chrono::milliseconds(210);
49 std::chrono::seconds(5);
52 std::chrono::milliseconds(1000);
65 : TclClass(
"Module/UW/UwModem/EvoLogicsS2C")
70 create(
int args,
const char *
const *argv)
91 , im_status_updated(false)
96 , curr_source_level(3)
98 , pend_source_level(3)
99 , source_level_change(false)
101 , txdur_file_name_(
"")
102 , txdur_token_separator_(
';')
108 bind(
"max_n_status_queries",
121 hdr_cmn *ch = HDR_CMN(p);
122 hdr_MPhy *ph = HDR_MPHY(p);
123 if (ch->direction() == hdr_cmn::UP) {
138 ph->rxtime = ph->txtime;
140 ph->worth_tracing =
false;
142 ph->srcSpectralMask = getTxSpectralMask(p);
143 ph->srcAntenna = getTxAntenna(p);
144 ph->srcPosition = getPosition();
145 ph->dstSpectralMask = 0;
151 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
156 "recv::PUSHING_IN_TX_QUEUE");
167 if (!strcmp(argv[1],
"start")) {
171 if (!strcmp(argv[1],
"stop")) {
175 if (!strcmp(argv[1],
"setBurstMode")) {
179 if (!strcmp(argv[1],
"setIMMode")) {
183 if (!strcmp(argv[1],
"enableIMAck")) {
187 if (!strcmp(argv[1],
"disableIMAck")) {
191 if (!strcmp(argv[1],
"enableExtProtoMode")) {
195 if (!strcmp(argv[1],
"disableExtProtoMode")) {
199 if (!strcmp(argv[1],
"initLUT")) {
203 }
else if (argc == 3) {
204 if (!strcmp(argv[1],
"setConnector")) {
205 if(!strcmp(argv[2],
"SOCKET")) {
209 if(!strcmp(argv[2],
"SERIAL")) {
213 fprintf(stderr,
"Invalid connector type, choose between SOCKET and SERIAL");
216 if (!strcmp(argv[1],
"setSourceLevel")) {
217 std::stringstream sl_ss(argv[2]);
224 if (!strcmp(argv[1],
"setTXDurationFileName")) {
225 std::string tmp_ = ((
char *) argv[2]);
226 if (tmp_.size() == 0) {
227 fprintf(stderr,
"Empty string for the file name");
233 if (!strcmp(argv[1],
"setLUTSeparator")) {
234 string tmp_ = ((
char *) argv[2]);
235 if (tmp_.size() == 0) {
236 fprintf(stderr,
"Empty char for the file separator");
250 ifstream input_file_;
255 if (input_file_.is_open()) {
256 while (std::getline(input_file_, line_)) {
260 std::istringstream line_stream(line_);
264 int size = std::atoi(token.c_str());
268 "initializeLUT::INVALID_PKT_SIZE " + token);
275 if (token.length() > 0) {
276 duration = std::strtod(token.c_str(), &end);
281 "initializeLUT::INVALID_DURATION_FOR_PKT_SIZE "
282 + std::to_string(size));
286 double proc_delay = 0;
287 if (token.length() > 0) {
288 proc_delay = std::strtod(token.c_str(), &end);
289 duration += proc_delay;
291 if (!(proc_delay > 0))
294 "initializeLUT::INVALID_PROC_DELAY_FOR_PKT_SIZE "
295 + std::to_string(size));
316 "UWEVOLOGICSS2CMODEM",
317 "RECEIVED_CLMSG_FOR_POWER_CHANGE");
355 }
else if (m->type() == CLMSG_MAC2PHY_GETTXDURATION) {
356 Packet *p = ((ClMsgMac2PhyGetTxDuration *) m)->pkt;
357 hdr_cmn *ch = HDR_CMN(p);
359 if (ch->direction() == hdr_cmn::DOWN) {
365 "recvSyncClMsg::SET_TXDURATION " +
366 std::to_string(duration));
368 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(duration);
374 ((ClMsgMac2PhyGetTxDuration *) m)->setDuration(-1);
379 return MPhy::recvSyncClMsg(m);
386 double tx_duration = -1;
393 "getTxDuration::FAIL_TO_GET_TXDURATION");
401 std::string config_cmd{
""};
432 std::unique_lock<std::mutex> state_lock(
status_m);
435 return status == ModemState::AVAILABLE;
440 std::lock_guard<std::mutex> tx_state_lock(
tx_status_m);
445 "configure::FAIL_TO_WRITE_TO_DEVICE=" + config_cmd);
466 hdr_mac *mach = HDR_MAC(p);
481 "startTx::COMMAND_TX::" + cmd_s);
484 std::unique_lock<std::mutex> state_lock(
status_m);
486 return status == ModemState::AVAILABLE;
491 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
496 "startTx::FAIL_TO_WRITE_TO_DEVICE=" + cmd_s);
508 size_t status_polling_counter = 0;
517 "startTx::SENDING=" + cmd_s);
522 "startTx::FAIL_TO_WRITE_TO_DEVICE=" + cmd_s);
538 "startTx::TX_PENDING");
541 status_polling_counter++;
550 "startTx::MAX_N_STATUS_QUERIES_REACHED");
559 std::function<void(
UwModem &, Packet * p)> callback =
567 "startTx::TIMEOUT_EXPIRED::FORCING_MODEM_AVAILABILITY");
577 "startRx::CALL_PHY2MACSTARTRX");
592 std::cout <<
"ERROR: Modem address not set!" << std::endl;
599 std::cout <<
"ERROR: connection to modem failed to open: "
603 "start::CONNECTION_OPEN_FAILED");
624 std::cout <<
"UwEvoLogicsS2CModem::stop() close conn " << std::endl;
635 "stop::CONNECTION_CLOSE_FAIL");
649 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
665 "transmittingData::BLOCKING_ON_NEXT_PACKET");
675 std::vector<char>::iterator beg_it =
data_buffer.begin();
676 std::vector<char>::iterator end_it =
data_buffer.begin();
678 std::vector<char>::iterator cmd_b =
data_buffer.begin();
679 std::vector<char>::iterator cmd_e =
data_buffer.begin();
689 end_it = beg_it + r_bytes + offset;
691 while (
receiving.load() && r_bytes > 0 &&
692 (cmd =
p_interpreter->findResponse(beg_it, end_it, cmd_b)) !=
702 r_bytes += new_r_bytes;
703 end_it = beg_it + r_bytes;
708 "receivingData::RX_MSG=" + std::string(cmd_b, cmd_e));
711 std::fill(cmd_b, cmd_e,
'\0');
716 if ((
int) (cmd_e - beg_it) < r_bytes) {
717 offset = r_bytes - (int) (cmd_e - beg_it);
718 std::copy(cmd_e, cmd_e + offset,
data_buffer.begin());
728 std::unique_lock<std::mutex> state_lock(
status_m);
737 Packet *p = Packet::alloc();
739 std::function<void(
UwModem &, Packet * p)> callback =
750 Packet *p = Packet::alloc();
752 std::function<void(
UwModem &, Packet * p)> callback =
768 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
781 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
792 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
802 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
822 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
832 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
867 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
902 "updateStatus::FAILED_RX");
916 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
934 std::shared_ptr<USBLInfo> pos =
p_interpreter->getUSBLInfo();
936 std::string log_msg =
"updateStatus::USBLLONG::curr_time=" +
937 std::to_string(pos->curr_time) +
938 ",meas_time=" + std::to_string(pos->meas_time) +
939 ",remote_address=" + std::to_string(pos->r_address) +
940 ",X=" + std::to_string(pos->X) +
941 ",Y=" + std::to_string(pos->Y) +
942 ",Z=" + std::to_string(pos->Z) +
943 ",E=" + std::to_string(pos->E) +
944 ",N=" + std::to_string(pos->N) +
945 ",U=" + std::to_string(pos->U) +
946 ",accuracy=" + std::to_string(pos->accuracy);
969 hdr_cmn *ch = HDR_CMN(p);
980 HDR_CMN(p)->direction() = hdr_cmn::UP;
ClMessage_t CLMSG_S2C_POWER_LEVEL
ClMessage_t CLMSG_S2C_RX_FAILED
ClMessage_t CLMSG_S2C_TX_MODE
Class representing the message for changing or retrieving the power level (source level)
void set_power_level(int level)
Set the poer level in the selected Cl message.
int get_power_level() const
Retrieve the power level specified in the Cl message.
Class representing the Cl message type used for retrieving the failed receptions counter of S2C devic...
void set_n_rx_failed(int n_failed)
Method used to set the number of reception failures in the message.
Cl Message type for setting the Tx Mode: Instant Message, Burst or Piggyback.
tx_mode_t get_tx_mode() const
Method used to retrieve the TX mode value in the message.
void set_tx_mode(tx_mode_t mode)
Method that sets the TX mode in the message to the specified value.
void setReqType(ReqType type)
method to set the request type
ReqType getReqType()
method to return the request type
std::mutex status_m
Mutex associated with the state machine of the modem.
int n_rx_failed
Number of failed receptions up to now.
static uint MAX_N_STATUS_QUERIES
Maximum number of time to query the modem transmission status before to discard the transmitted packe...
bool initLUT_
Flag that tells whether the TX duration LUT is loaded or not.
TransmissionMode tx_mode
Either burst or im.
TransmissionState tx_status
Variable holding the current transmission status of the modem.
void setFailedTx(Packet *p)
Method that allows to set the error flag on the packet to notify upper layers about un-tranmsitted pa...
virtual void recv(Packet *p)
Method that handles the reception of packets arriving from upper layers of the network simulator.
std::atomic< bool > transmitting
Atomic boolean variable that controls the transmitting looping thread.
std::condition_variable status_cv
Condition variable to wait for ModemState::AVAILABLE.
std::atomic< bool > im_status_updated
Atomic boolean variable controlling if the modem had responded to ATDI.
int pend_source_level
Pending source level, requested but not set.
std::condition_variable tx_status_cv
Condition variable to wait for TransmissionState::TX_IDLE.
void updateStatus(UwInterpreterS2C::Response cmd)
Method that updates the status of the modem State Machine: state change is triggered by reception of ...
void createRxPacket(Packet *p)
Method that fills up a packet with the needed header and payload and makes it ready to be sent to the...
std::thread rx_thread
Object with the rx thread.
UwEvoLogicsS2CModem()
Constructor of the UwEvoLogicsS2CModem class.
std::mutex tx_queue_m
Mutex associated with the transmission queue.
virtual void transmittingData()
Method that detach a thread devoted to sending packets found in tx_queue.
virtual void initializeLUT()
Method that loads the TX LUT into a map.
static const std::chrono::seconds WAIT_DELIVERY_BURST
Time interval tu wait for a burst message tobe confirmed through a DELIVERED response.
bool source_level_change
Flag that tells a new SL value to be applied.
virtual void stop()
Method that stops the driver operations.
static const std::chrono::milliseconds MODEM_TIMEOUT
Maximum time to wait for modem to become ModemState::AVAILABLE.
bool ack_mode
Set to true to enable IM ack.
std::string rx_payload
String that is updated witn each new received messsage.
std::atomic< bool > receiving
Atomic boolean variable that controls the receiving looping thread.
Config
Enum listing the availbale configuration settings.
TransmissionMode
Transmission mode: either IM or BURST See the EvoLogics S2C manuals or reach for www....
TransmissionState
Transmission state: controls the flow of execution for sending commands to the S2C device.
virtual void startRx(Packet *p)
Method that starts a packet reception.
std::condition_variable tx_queue_cv
Condition variable that is linked with the transmitting queue.
virtual void receivingData()
Mehod that detach a thread devoted to receiving data from the connector.
virtual void endRx(Packet *p)
Method that ends a packet reception.
char txdur_token_separator_
TX duration LUT separator.
virtual void start()
Method that starts the driver operations.
ModemState status
Variable holding the current status of the modem.
virtual ~UwEvoLogicsS2CModem()
Destructor of the UwEvoLogicsS2CModem class.
TransmissionDurationLUT size2dur_
Map from size [byte] to TX duration [s].
int curr_source_level
Current source level already set in device.
std::string txdur_file_name_
TX duration LUT file name.
virtual bool configure(Config cmd)
Method that sends a written configuration the the EvoLgoics modem.
static const std::chrono::milliseconds WAIT_DELIVERY_IM
Time interval to wait for the modem notifying that there no more IM in its queue and a new IM can be ...
virtual double getTxDuration(Packet *p)
Method that return the duration of a given transmitted packet.
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
std::unique_ptr< UwInterpreterS2C > p_interpreter
Pointer to Interpreter object to parse device syntax.
std::mutex tx_status_m
Mutex associated with the transmission state machine of the modem.
std::thread tx_thread
Object with the tx thread.
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
std::unique_ptr< UwConnector > p_connector
Pointer to Connector object that interfaces with the device.
virtual void startTx(Packet *p)
Method that triggers the transmission of a packet through a specified modem.
Class to create the Otcl shadow object for an object of the class UwEvoLogicsS2CModem.
UwEvoLogicsS2C_TclClass()
TclObject * create(int args, const char *const *argv)
Response
Enum listing the types of commands that could be received or sent by a S2C device; See the EvoLogics ...
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...
int modemID
Number used for identification purposes: not specified.
double period
Checking period of the modem's buffer.
Class that implements serial port connection.
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 ...
UwEvoLogicsS2C_TclClass class_evologicss2c
Header of the main class that implements the drivers to manage the EvoLogics S2C line of devices....
ModemState
Enum containing the possible statuses of the driver.
Definition of ClMsgUwMmac class.
This implements a generic serial connector .
Class that implements a connector and, specifically, the socket connector. BSD sockets are used,...