Skip to content

Commit bded22e

Browse files
Hasnain VirkAri Parkkila
authored andcommitted
Prevent assigning socket id until opened at modem
Local modem ip stacks vary in their implementations and the way of working. Some of the modems may not open a socket until an IP context is assigned. That's why we came up with a container that stores addresses of any CellularSocket instances created on-demand by the application. When the application requests opening a socket we store allocate and store the premitive in the container however actual socket creation at the modem may happen at a later stage, e.g., a call to send_to() may result in actual opening of a socket. That's why we must not assign socket ids in the CellularSocket object during construction. It must happen when actual socket is opened and is alive. Another implication of the previous model is that we may have multiple sockets created in our container but the actual socket ids are not assigned yet, so we cannot directly map the socket id to the container indices which has been happening previously. To solve this issue we have promoted the AT_CellularStac::find_socket_index(...) method to be a protected method rather than being private so that the children can use the method to determine if the given index in the container corrsponds to the assigned socket id or not. We have given up on the socket->created flag and the whole decision making to actually open a socket on the modem happens on the basis of a valid socket being assigned or not.
1 parent 1dbb478 commit bded22e

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ int AT_CellularStack::find_socket_index(nsapi_socket_t handle)
5252
return -1;
5353
}
5454

55-
5655
/** NetworkStack
5756
*/
5857

@@ -131,10 +130,12 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
131130

132131
tr_info("Socket %d open", index);
133132
// create local socket structure, socket on modem is created when app calls sendto/recvfrom
133+
// Do not assign a socket ID yet. Socket is not created at the Modem yet.
134+
// create_socket_impl(handle) will assign the correct socket ID.
134135
_socket[index] = new CellularSocket;
135136
CellularSocket *psock = _socket[index];
136137
SocketAddress addr(0, get_dynamic_ip_port());
137-
psock->id = index;
138+
138139
psock->localAddress = addr;
139140
psock->proto = proto;
140141
*handle = psock;
@@ -153,7 +154,6 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
153154
return err;
154155
}
155156
int sock_id = socket->id;
156-
bool sock_created = socket->created;
157157

158158
int index = find_socket_index(handle);
159159
if (index == -1) {
@@ -165,14 +165,14 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
165165

166166
// Close the socket on the modem if it was created
167167
_at.lock();
168-
if (sock_created) {
168+
if (sock_id > -1) {
169169
err = socket_close_impl(sock_id);
170170
}
171171

172172
if (!err) {
173173
tr_info("Socket %d closed", index);
174174
} else {
175-
tr_info("Socket %d close (id %d, created %d, started %d, error %d)", index, sock_id, socket->created, socket->started, err);
175+
tr_info("Socket %d close (id %d, started %d, error %d)", index, sock_id, socket->started, err);
176176
}
177177

178178
_socket[index] = NULL;
@@ -206,7 +206,7 @@ nsapi_error_t AT_CellularStack::socket_bind(nsapi_socket_t handle, const SocketA
206206
}
207207
}
208208

209-
if (!socket->created) {
209+
if (socket->id == -1) {
210210
create_socket_impl(socket);
211211
}
212212

@@ -266,7 +266,7 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con
266266

267267
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
268268

269-
if (!socket->created) {
269+
if (socket->id == -1) {
270270
_at.lock();
271271

272272
ret_val = create_socket_impl(socket);
@@ -317,7 +317,7 @@ nsapi_size_or_error_t AT_CellularStack::socket_recvfrom(nsapi_socket_t handle, S
317317

318318
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
319319

320-
if (!socket->created) {
320+
if (socket->id == -1) {
321321
_at.lock();
322322

323323
ret_val = create_socket_impl(socket);

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
9797
localAddress("", 0),
9898
_cb(NULL),
9999
_data(NULL),
100-
created(false),
101100
closed(false),
102101
started(false),
103102
tx_ready(false),
@@ -115,7 +114,6 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
115114
SocketAddress localAddress;
116115
void (*_cb)(void *);
117116
void *_data;
118-
bool created; // socket has been created on modem stack
119117
bool closed; // socket has been closed by a peer
120118
bool started; // socket has been opened on modem stack
121119
bool tx_ready; // socket is ready for sending on modem stack
@@ -176,12 +174,22 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
176174
void *buffer, nsapi_size_t size) = 0;
177175

178176
/**
179-
* Find the socket handle based on socket identifier
177+
* Find the socket handle based on the index of the socket construct
178+
* in the socket container. Please note that this index may or may not be
179+
* the socket id. The actual meaning of this index depends upon the modem
180+
* being used.
180181
*
181-
* @param sock_id Socket identifier
182+
* @param index Index of the socket construct in the container
182183
* @return Socket handle, NULL on error
183184
*/
184-
CellularSocket *find_socket(int sock_id);
185+
CellularSocket *find_socket(int index);
186+
187+
/**
188+
* Find the index of the given CellularSocket handle. This index may or may
189+
* not be the socket id. The actual meaning of this index depends upon the modem
190+
* being used.
191+
*/
192+
int find_socket_index(nsapi_socket_t handle);
185193

186194
// socket container
187195
CellularSocket **_socket;
@@ -199,7 +207,6 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
199207
nsapi_ip_stack_t _stack_type;
200208

201209
private:
202-
int find_socket_index(nsapi_socket_t handle);
203210

204211
int get_socket_index_by_port(uint16_t port);
205212

0 commit comments

Comments
 (0)