32#include <mphy_timers.h>
44 std::chrono::milliseconds(3600);
61 : TclClass(
"Module/UW/UwModem/AHOI")
66 create(
int args,
const char *
const *argv)
90 , virtual_time_ref(0.0)
91 , WAIT_DELIVERY(std::chrono::milliseconds(3000))
108 bind(
"stop_bit", (
int *) &
stop_bit);
111 bind(
"modem_id", (
int *) &
id);
112 bind(
"max_n_retx", (
int *) &
MAX_RETX);
125 hdr_cmn *ch = HDR_CMN(p);
126 hdr_MPhy *ph = HDR_MPHY(p);
128 if (ch->direction() == hdr_cmn::UP) {
143 ph->rxtime = ph->txtime;
145 ph->worth_tracing =
false;
147 ph->srcSpectralMask = getTxSpectralMask(p);
148 ph->srcAntenna = getTxAntenna(p);
149 ph->srcPosition = getPosition();
150 ph->dstSpectralMask = 0;
155 std::unique_lock<std::mutex> tx_lock(
tx_queue_m);
178 ahoi::packet_t ahoiPkt = {0};
184 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m);
187 { return tx_status == TransmissionState::TX_IDLE; })) {
191 "startTx::FORCING_TX_STATUS_IDLE");
200 "startTx::FAIL_TO_WRITE_TO_DEVICE::[" + cmd_s +
"]");
209 return tx_status == TransmissionState::TX_IDLE;
214 "startTx::SENDING_PACKET[" + std::to_string(n_retx) +
"]");
221 "startTx::FAIL_TO_WRITE_TO_DEVICE::[" + cmd_s +
"]");
232 hdr_mac* mach = HDR_MAC(p);
234 ahoi::packet_t packet = {0};
238 ahoi::header_t header;
239 header.src = (
unsigned int)
modemID;
240 header.dst = mach->macDA();
244 header.len =
static_cast<uint8_t
>(payload.length());
246 packet.header = header;
247 for (uint i = 0; i < payload.size(); i++) {
248 packet.payload[i] = (uint8_t) payload[i];
346 std::unique_lock<std::mutex> tx_q_lock(
tx_queue_m);
359 std::unique_lock<std::mutex> state_lock(
status_m);
361 return status == ModemState::AVAILABLE;
367 "transmittingData::FORCING_MODEM_AVAILABILITY");
376 "transmittingData::INVALID_PACKET_RETRIEVED");
382 std::function<void(
UwModem &, Packet * p)> callback =
388 "transmittingData::BLOCKING_ON_NEXT_PACKET");
398 std::vector<char>::iterator beg_it =
data_buffer.begin();
399 std::vector<char>::iterator end_it =
data_buffer.begin();
401 std::vector<char>::iterator cmd_b =
data_buffer.begin();
402 std::vector<char>::iterator cmd_e =
data_buffer.begin();
406 std::shared_ptr<ahoi::packet_t> pck = std::make_shared<ahoi::packet_t>();
425 beg_it, end_it, cmd_b, cmd_e) !=
"") {
427 offset = std::distance(cmd_e, end_it);
431 if ((pck =
p_interpreter->parseResponse(cmd_b, cmd_e)) !=
nullptr) {
436 "receivingData::RX_MSG=" + std::string(cmd_b, cmd_e));
439 offset = std::distance(cmd_e, end_it);
440 std::copy(cmd_e, end_it, beg_it);
441 std::fill(beg_it + offset, end_it,
'\0');
442 end_it = beg_it + offset;
456 HDR_CMN(p)->direction() = hdr_cmn::UP;
463 ahoi::header_t header = packet->header;
464 ahoi::footer_t footer = packet->footer;
467 uwalh->
srcID() = header.src;
469 if (header.type == 0x00) {
472 for (
int i = 0; i < header.len; i++) {
475 power = footer.power;
483 "AHOIMODEM",
"storePacketInfo::PAYLOAD[" +
rx_payload +
"]");
492 power = footer->power;
506 uint8_t packet_type = (packet->header).type;
509 [&packet_type](
const std::pair<ahoi::Command, uint8_t> &p) {
510 return p.second == packet_type;
516 "updateStatus::UNKNOWN_COMMAND_RECEIVED");
519 std::string dbg_str = std::string(
"updateStatus::HEADER::") +
520 "[SRC::" + std::to_string((packet->header).src) +
521 "][DST::" + std::to_string((packet->header).dst) +
522 "][TYP::" + std::to_string((packet->header).type) +
523 "][DSN::" + std::to_string((packet->header).dsn) +
524 "][LEN::" + std::to_string((packet->header).len) +
"]";
528 std::unique_lock<std::mutex> state_lock(
status_m, std::adopt_lock);
529 std::unique_lock<std::mutex> tx_state_lock(
tx_status_m, std::adopt_lock);
536 switch (cmd->first) {
539 Packet *p = Packet::alloc();
541 if (packet->header.dst ==
modemID || packet->header.dst == 0xFF) {
546 std::function<void(
UwModem &, Packet * p)> callback =
557 if ((packet->header).src ==
modemID && (packet->header).dsn ==
sn) {
561 tx_state_lock.unlock();
563 std::string err_str =
"WRONG_RESPONSE::[" +
571 "updateStatus::UNMANAGED_CMD_TYPE");
581 "updateSN::CURRENT_SEQ_NUM::[" + std::to_string(
sn) +
"]");
Class to create the Otcl shadow object for an object of the class UwAhoiModem.
TclObject * create(int args, const char *const *argv)
The ahoi! modems are an underwater acoustic modems developed by TUUH University, Hamburg by the Smart...
void updateSN()
Method for Sequence Number (SN) update.
int stop_bit
flag for stop bit
virtual void endRx(Packet *p)
Method that ends a packet reception.
UwAhoiModem()
Constructor of the UwAhoiModem class.
TransmissionState
Enum type for the trasnmission state.
ModemState
Enum type for the modem general state.
std::condition_variable tx_status_cv
Condition variable to wait for TransmissionState::TX_IDLE.
virtual void startTx(Packet *p)
Method that triggers the transmission of a packet through a specified modem.
std::mutex tx_queue_m
Mutex associated with the transmission queue.
ahoi::packet_t fillAhoiPkt(Packet *p)
Method that fills the fields of an ahoi! packet with the needed values, so that later it can be seria...
std::unique_ptr< UwConnector > p_connector
Pointer to Connector object that interfaces with the device.
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
uint rssi
RSSI vs an "ideal packet".
virtual void stop()
Method that stops the driver operations.
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
uint power
Received power (RMSE) ratio and "ideal packet".
virtual void start()
Method that starts the driver operations.
uint agc_max
Maximum gain set by the AGC (if used) in reception of the last packet.
std::mutex tx_status_m
Mutex associated with the state machine of the transmission process.
uint bit_errors
Number of repaired bit errors during reception in the last packet.
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...
bool storePacketInfo(std::shared_ptr< ahoi::packet_t > header, Packet *p)
Method that extarcts fields from a provided ahoi! packet and save the info values to class field for ...
std::mutex status_m
Mutex associated with the state machine of the modem.
uint agc_mean
Mean gain set by the AGC (if used) in reception of the last packet.
std::atomic< bool > receiving
Atomic boolean variable that controls the receiving looping thread.
static uint MAX_RETX
Maximum number of retransmissions for the same packet.
std::chrono::milliseconds WAIT_DELIVERY
Time interval to wait for the modem notifying the response of a given packet.
ModemState status
Variable holding the current status of the modem.
static uint WAIT_DELIVERY_INT
Time interval matching the WAIT_DELIVERY variable: version of type int to match the chrono one,...
int flow_control
flag for flow control
virtual void transmittingData()
Method that detach a thread devoted to sending packets found in tx_queue.
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.
virtual ~UwAhoiModem()
Destructor of the UwAhoiModem class.
uint agc_min
Minimum gain set by the AGC (if used) in reception of the last packet.
std::unique_ptr< UwInterpreterAhoi > p_interpreter
Pointer to Interpreter object to parse device syntax.
virtual void startRx(Packet *p)
Method that starts a packet reception.
std::atomic< bool > transmitting
Atomic boolean variable that controls the transmitting looping thread.
std::thread rx_thread
Object with the rx thread.
std::thread tx_thread
Object with the tx thread.
int parity_bit
flag for parity bit
std::condition_variable status_cv
Condition variable to wait for ModemState::AVAILABLE.
void updateStatus(std::shared_ptr< ahoi::packet_t > packet)
Method that updates the status of the modem State Machine: state change is triggered by recepting the...
std::string rx_payload
String that is updated witn each new received messsage.
virtual void receivingData()
Mehod that detach a thread devoted to receiving data from the connector.
TransmissionState tx_status
Variable holding the current transmission status of the modem.
int baud_rate
Integer for port baud rate.
static uint8_t sn
Sequence number, which follows all the packets transmitted to the modem.
bool parseFooter(std::shared_ptr< ahoi::footer_t > footer)
Method that extarcts fields from a provided ahoi! packet and save the info values to class field for ...
virtual void recv(Packet *p)
Method that handles the reception of packets arriving from upper layers of the network simulator.
Class used for building syntactically compliant commands to send to the ahoi! modems and interpreting...
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.
Header of the class providing the Uwal header's description.
std::map< ahoi::Command, uint8_t > commands_id
hdr_uwal describes the packet header used by Uwal objects.
char * binPkt()
Return to the binPkt_ array pointer.
uint8_t & srcID()
Reference to the srcID_ variable.
uint32_t & binPktLength()
Reference to the binPktLength_ variable.
UwAhoiModem_TclClass class_ahoimodem
Driver for ahoi! acoustic UW modems: developed by TUUH, Hamburg.
Header of the main class that implements the adaptation layer between ns2/NS-Miracle and binary data ...
Definition of ClMsgUwMmac class.
This implements a generic serial connector .