DESERT 3.5.1
Loading...
Searching...
No Matches
DESERT_Framework/DESERT/physical/uwmphy_modem/msocket.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 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
37#include "msocket.h"
38#include <uwmdriver.h>
39
40#include <string>
41#include <cctype>
42
43void
44Msocket::error(const char *msg)
45{
46 perror(msg);
47 exit(-1);
48}
49
50Msocket::Msocket(UWMdriver *pmDriver_, std::string portno_)
51 : UWMconnector(pmDriver_, portno_)
52{
53 sockfd = 0;
54 std::string tokenizer(":");
55 size_t p_tokenizer = pathToDevice.find(tokenizer);
56
57 if (p_tokenizer == std::string::npos) {
58
59 server_host = "localhost";
60 portno = atoi(pathToDevice.c_str());
61
62 } else {
63
64 server_host = pathToDevice.substr(0, p_tokenizer);
65 portno = atoi((pathToDevice.substr(p_tokenizer + 1)).c_str());
66 }
67}
68
72
73int
75{
76 sockfd = socket(AF_INET, SOCK_STREAM, 0);
77 if (sockfd < 0) {
78 perror("MSOCKET::ERROR::OPEN_SOCKET");
79 }
80 server = gethostbyname(server_host.c_str());
81 if (server == NULL) {
82 // fprintf(stderr,"ERROR, no such host\n");
83 perror("MSOCKET::ERROR::NO_SUCH_HOST");
84 exit(1);
85 }
86 bzero((char *) &serv_addr, sizeof(serv_addr));
87 serv_addr.sin_family = AF_INET;
88 bcopy((char *) server->h_addr,
89 (char *) &serv_addr.sin_addr.s_addr,
90 server->h_length);
91 serv_addr.sin_port = htons(portno);
92
93 if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <
94 0) {
95 perror("MSOCKET::OPEN_CONNECTION::CONNECT_ERROR");
96 exit(1);
97 }
98 rc = pthread_create(&thread_id, NULL, read_process_msocket, (void *) this);
99 if (rc) {
100 printf("ERROR; return code from pthread_create() is %d\n", rc);
101 exit(-1);
102 }
103
104 return _MODEM_OK;
105}
106
107void
109{
110 // Close the connection with the server
111 close(sockfd);
112}
113
114int
115Msocket::writeToModem(std::string str)
116{
117 bzero(msg_tx, _MAX_MSG_LENGTH);
118 memcpy(msg_tx, str.c_str(), str.size());
119 int msg_ssz = str.size();
120
121 {
122 std::string s;
123 s.assign(msg_tx, msg_ssz);
124 }
125
126 // Append to the message that we want to send the string terminator '\n'
127 // (S2C
128 // modem requirements)
129 msg_tx[msg_ssz++] = '\n';
130
131 // Send message host to modem via TCP-IP socket
132 int w_bytes = write(sockfd, msg_tx, msg_ssz);
133
134 // Return the number of written bytes
135 return w_bytes;
136}
137
138void *
139read_process_msocket(void *pMsocket_me_)
140{
141 // Array to store the received message
142 char msg_rx[_MAX_MSG_LENGTH + 1];
143 // Structure to queue the received message
144 msgModem tmp_;
145 std::ofstream out;
146
147 Msocket *pMsocket_me = (Msocket *) pMsocket_me_;
148
149 while (1) {
150 // Read from the socket
151 tmp_.msg_length =
152 read(pMsocket_me->getSocket(), msg_rx, _MAX_MSG_LENGTH);
153
154 if (tmp_.msg_length < 0) {
155 perror("SOCKET::READ::ERROR_READ_FROM_SOCKET");
156 }
157
158 // Set end of string
159 msg_rx[tmp_.msg_length] = '\0';
160
161 // Check the queue length
162 if (pMsocket_me->queueMsg.size() >
163 pMsocket_me->getDriverQueueLength()) {
164 std::cout << "MSOCKET::READ::ERROR::BUFFER_FULL ---> drop the oldest "
165 "packet"
166 << std::endl;
167 pMsocket_me->queueMsg.pop();
168 }
169
170 tmp_.msg_rx.assign(msg_rx, tmp_.msg_length);
171 pMsocket_me->queueMsg.push(tmp_);
172
173 usleep(1000);
174 }
175 pthread_exit(NULL);
176}
void * read_process_msocket(void *)
Function to read from the modem via a TCP/IP connection (it must be called as a pure C function).
void * read_process_msocket(void *pMsocket_me_)
Function to read from the modem via a TCP/IP connection (it must be called as a pure C function).
Header of the class derived from UWMconnector to handle the TCP/IP socket connection of a client betw...
#define _MODEM_OK
Variable to test the right opening of the modem's connection.
#define _MAX_MSG_LENGTH
Variable defining the maximum length of the messages \ exchanged between host and modem.
Header of the class needed by UWMPhy_modem to handle the different transmissions cases and correspond...
Class used to manage host/modem string exchange via TCP/IP connection (this class is a derived class ...
pthread_t thread_id
Id of a parallel thread.
char msg_tx[_MAX_MSG_LENGTH]
Message to be transmitted host to modem.
virtual int writeToModem(std::string)
Method for writing to the modem.
int rc
Indicator of the thread creation.
int getSocket()
Method to read the pointer to the socket where to write (it is needed by the reading function invoked...
struct sockaddr_in serv_addr
Structure to contain the Internet address to be used.
struct hostent * server
Structure to define the server host.
virtual int openConnection()
Method to open the connection with the modem.
virtual void closeConnection()
Method to close the connection with the modem.
Msocket(UWMdriver *, std::string)
Class constructor.
std::string server_host
Name of the server host on the Internet to which we want to be connected as client.
void error(const char *)
Method report errors.
The class needed by UWMPhy_modem to manage string exchange with the modem.
std::string pathToDevice
The path to be connected with the modem device.
std::queue< msgModem > queueMsg
Queue used to buffer incoming strings from the modem.
The class needed by UWMPhy_modem to handle the different transmissions cases and corresponding protoc...