DESERT 3.5.1
Loading...
Searching...
No Matches
uwmodem.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2018 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
29#include <chrono>
30#include <iomanip>
31#include <uwmodem.h>
32
33bool
34UwModem::string2log(const std::string &ll_string, LogLevel &ll)
35{
36 if (ll_string == "ERR") {
37 ll = LogLevel::ERROR;
38 return true;
39 }
40 if (ll_string == "INFO") {
41 ll = LogLevel::INFO;
42 return true;
43 }
44 if (ll_string == "DBG") {
45 ll = LogLevel::DEBUG;
46 return true;
47 }
48 return false;
49}
50
51bool
52UwModem::log2string(LogLevel ll, std::string &ll_string)
53{
54 if (ll == LogLevel::ERROR) {
55 ll_string = "ERR";
56 return true;
57 }
58 if (ll == LogLevel::INFO) {
59 ll_string = "INFO";
60 return true;
61 }
62 if (ll == LogLevel::DEBUG) {
63 ll_string = "DBG";
64 return true;
65 }
66 return false;
67}
68
70 : MPhy()
71 , modemID(0)
72 , data_buffer()
73 , tx_queue()
74 , rx_queue()
75 , DATA_BUFFER_LEN(0)
76 , MAX_READ_BYTES(0)
77 , modem_address("")
78 , debug_(0)
79 , outLog()
80 , logFile("modem_")
81 , log_suffix("_log")
82 , loglevel_(LogLevel::ERROR)
83 , log_is_open(false)
84 , checkTimer(NULL)
85 , period(0.01)
86 , event_q()
87{
88 bind("debug_", (int *) &debug_);
89 bind("period_", (double *) &period);
90 bind("buffer_size", (unsigned int *) &DATA_BUFFER_LEN);
91 bind("max_read_size", (int *) &MAX_READ_BYTES);
92 bind("ID_", (int *) &modemID);
93}
94
96{
97 outLog.flush();
98 outLog.close();
99}
100
101void
102UwModem::printOnLog(LogLevel log_level, std::string module, std::string message)
103{
104
105 LogLevel actual_log_level = getLogLevel();
106 if (actual_log_level >= log_level) {
107 double timestamp =
108 (double) (std::chrono::duration_cast<std::chrono::milliseconds>(
109 std::chrono::system_clock::now().time_since_epoch())
110 .count()) /
111 1000.0;
112 if (!log_is_open) {
113 outLog.open((getLogFile()).c_str(), ios::app);
114 log_is_open = true;
115 }
116 std::string ll_descriptor = "";
117 log2string(log_level, ll_descriptor);
118
119 outLog << std::setprecision(15) << left << ll_descriptor << "::["
120 << timestamp << "]::[" << NOW << "]::" << module << "("
121 << modemID << ")::" << message << endl;
122 outLog.flush();
123 }
124}
125
126int
127UwModem::command(int argc, const char *const *argv)
128{
129 Tcl &tcl = Tcl::instance();
130
131 if (argc == 2) {
132 if (!strcmp(argv[1], "start")) {
133 start();
134 return TCL_OK;
135 }
136 if (!strcmp(argv[1], "stop")) {
137 stop();
138 return TCL_OK;
139 }
140 } else if (argc == 3) {
141 if (!strcmp(argv[1], "setModemAddress")) {
142 modem_address = argv[2];
143 return TCL_OK;
144 }
145 if (!strcmp(argv[1], "setLogSuffix")) {
146 log_suffix = argv[2];
147 return TCL_OK;
148 }
149 if (!strcmp(argv[1], "setLogLevel")) {
150 std::string log_lv = argv[2];
151 if (string2log(log_lv, loglevel_)) {
152 return TCL_OK;
153 }
154 std::cout << "setLogLevel::INVALID_LOGLEVEL" << std::endl;
155 return TCL_ERROR;
156 }
157 }
158
159 return MPhy::command(argc, argv);
160}
161
162void
164{
165 Phy2MacEndTx(p);
166 Packet::free(p);
167}
168
169void
171{
172 while (event_q.size() > 0) {
173 ModemEvent e = event_q.front();
174 e.f(*this, e.p);
175 event_q.pop();
176 }
177 checkTimer->resched(period);
178}
179
180void
182{
184}
UwModem * pmModem
Pointer to an UwModem object.
Definition uwmodem.h:353
virtual void expire(Event *e)
Method to handle the expiration of a given event.
Definition uwmodem.cpp:181
LogLevel
Enum representing the amount of logs being generated ERROR: only errors will be generated INFO : gene...
Definition uwmodem.h:72
std::string log_suffix
Possibility to insert a log suffix.
Definition uwmodem.h:261
static bool string2log(const std::string &ll_string, LogLevel &ll)
Method that converts a string representing the loglevel into the enum type of loglevel.
Definition uwmodem.cpp:34
std::string modem_address
String containing the address needed to connect to the device In case of socket, it may be expressed ...
Definition uwmodem.h:252
UwModem()
UwModem constructor.
Definition uwmodem.cpp:69
virtual void stop()=0
Method that stops the driver operations.
std::queue< ModemEvent > event_q
Queue of events that are scheduled for NS2 to execute (callbacks)
Definition uwmodem.h:269
static bool log2string(LogLevel ll, std::string &ll_string)
Method that converts an enum type of the loglevel into the string representing it.
Definition uwmodem.cpp:52
bool log_is_open
Flag to check if log file has already be opened.
Definition uwmodem.h:264
virtual void start()=0
Method that starts the driver operations.
void checkEvent()
Method to check if any event from real world has to go to ns.
Definition uwmodem.cpp:170
int MAX_READ_BYTES
Maximum number of bytes to be read by a single dump of data.
Definition uwmodem.h:245
int debug_
Usual debug value that chooses the debug level through Tcl interface.
Definition uwmodem.h:255
CheckTimer * checkTimer
Pointer to an object to schedule the "check-modem" events.
Definition uwmodem.h:265
LogLevel loglevel_
Log level on file, from ERROR (0) to DEBUG (2) in UwEvoLogicsS2CModem::logFile.
Definition uwmodem.h:262
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...
Definition uwmodem.cpp:102
virtual ~UwModem()
UwModem destructor.
Definition uwmodem.cpp:95
unsigned int DATA_BUFFER_LEN
Size of the buffer that holds data.
Definition uwmodem.h:242
virtual void endTx(Packet *p)
Method that ends a packet transmission.
Definition uwmodem.cpp:163
std::ofstream outLog
output strem to print into a disk-file log messages.
Definition uwmodem.h:257
virtual int command(int argc, const char *const *argv)
Tcl command interpreter: Method that maps Tcl commands into C++ methods.
Definition uwmodem.cpp:127
int modemID
Number used for identification purposes: not specified.
Definition uwmodem.h:225
double period
Checking period of the modem's buffer.
Definition uwmodem.h:267
LogLevel getLogLevel()
Method to return the flag used to enable the printing of log messages in UwEvoLogicsS2CModem::logFile...
Definition uwmodem.h:189
std::string getLogFile()
Method to return the name of the file where to log messages.
Definition uwmodem.h:209
std::function< void(UwModem &, Packet *p)> f
Definition uwmodem.h:359
Packet * p
Definition uwmodem.h:360
log_level
Definition uw-csma-ca.h:86
Header of the main class that implements the general interface between DESERT and real acoustic modem...