@@ -44,21 +44,6 @@ GEMALTO_CINTERION_CellularStack::~GEMALTO_CINTERION_CellularStack()
44
44
_at.set_urc_handler (" ^SISR:" , 0 );
45
45
}
46
46
47
- GEMALTO_CINTERION_CellularStack::CellularSocket *GEMALTO_CINTERION_CellularStack::find_socket (int sock_id)
48
- {
49
- CellularSocket *sock = NULL ;
50
- for (int i = 0 ; i < SOCKET_MAX; i++) {
51
- if (_socket[i] && _socket[i]->id == sock_id) {
52
- sock = _socket[i];
53
- break ;
54
- }
55
- }
56
- if (!sock) {
57
- tr_error (" Socket not found %d" , sock_id);
58
- }
59
- return sock;
60
- }
61
-
62
47
void GEMALTO_CINTERION_CellularStack::urc_sis ()
63
48
{
64
49
int sock_id = _at.read_int ();
@@ -175,56 +160,11 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_close_impl(int sock_id)
175
160
}
176
161
177
162
nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer (CellularSocket *socket, const SocketAddress *address)
178
- {
179
- // host address (IPv4) and local+remote port is needed only for BGS2 which does not support UDP server socket
180
- char sock_addr[sizeof (" sockudp://" ) - 1 + NSAPI_IPv6_SIZE + sizeof (" []:12345;port=12345" ) - 1 + 1 ];
181
-
182
- if (socket->proto == NSAPI_UDP) {
183
- if (GEMALTO_CINTERION::get_module () != GEMALTO_CINTERION::ModuleBGS2) {
184
- std::sprintf (sock_addr, " sockudp://%s:%u" , address ? address->get_ip_address () : " " , socket->localAddress .get_port ());
185
- } else {
186
- std::sprintf (sock_addr, " sockudp://%s:%u;port=%u" , address->get_ip_address (), address->get_port (), socket->localAddress .get_port ());
187
- }
188
- } else {
189
- if (address->get_ip_version () == NSAPI_IPv4) {
190
- std::sprintf (sock_addr, " socktcp://%s:%u" , address->get_ip_address (), address->get_port ());
191
- } else {
192
- std::sprintf (sock_addr, " socktcp://[%s]:%u" , address->get_ip_address (), address->get_port ());
193
- }
194
- }
195
-
196
- _at.cmd_start (" AT^SISS=" );
197
- _at.write_int (socket->id );
198
- _at.write_string (" address" , false );
199
- _at.write_string (sock_addr);
200
- _at.cmd_stop_read_resp ();
201
-
202
- _at.cmd_start (" AT^SISO=" );
203
- _at.write_int (socket->id );
204
- _at.cmd_stop_read_resp ();
205
-
206
- if (_at.get_last_error ()) {
207
- tr_error (" Socket %d open failed!" , socket->id );
208
- _at.clear_error ();
209
- socket_close_impl (socket->id ); // socket may already be open on modem if app and modem are not in sync, as a recovery, try to close the socket so open succeeds the next time
210
- return NSAPI_ERROR_NO_SOCKET;
211
- }
212
-
213
- socket->created = true ;
214
- tr_debug (" Cinterion open %d (err %d)" , socket->id , _at.get_last_error ());
215
-
216
- return _at.get_last_error ();
217
- }
218
-
219
- // To open socket:
220
- // 1. Select URC mode or polling mode with AT^SCFG
221
- // 2. create a GPRS connection profile with AT^SICS (must have PDP)
222
- // 3. create service profile with AT^SISS and map connectionID to serviceID
223
- // 4. open internet session with AT^SISO (ELS61 tries to attach to a packet domain)
224
- nsapi_error_t GEMALTO_CINTERION_CellularStack::create_socket_impl (CellularSocket *socket)
225
163
{
226
164
int connection_profile_id = CONNECTION_PROFILE_ID;
227
165
166
+ int retry_open = 1 ;
167
+ retry_open:
228
168
// setup internet session profile
229
169
int internet_service_id = socket->id ;
230
170
bool foundSrvType = false ;
@@ -285,6 +225,56 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_socket_impl(CellularSocket
285
225
_at.cmd_stop_read_resp ();
286
226
}
287
227
228
+ // host address (IPv4) and local+remote port is needed only for BGS2 which does not support UDP server socket
229
+ char sock_addr[sizeof (" sockudp://" ) - 1 + NSAPI_IPv6_SIZE + sizeof (" []:12345;port=12345" ) - 1 + 1 ];
230
+
231
+ if (socket->proto == NSAPI_UDP) {
232
+ if (GEMALTO_CINTERION::get_module () != GEMALTO_CINTERION::ModuleBGS2) {
233
+ std::sprintf (sock_addr, " sockudp://%s:%u" , address ? address->get_ip_address () : " " , socket->localAddress .get_port ());
234
+ } else {
235
+ std::sprintf (sock_addr, " sockudp://%s:%u;port=%u" , address->get_ip_address (), address->get_port (), socket->localAddress .get_port ());
236
+ }
237
+ } else {
238
+ if (address->get_ip_version () == NSAPI_IPv4) {
239
+ std::sprintf (sock_addr, " socktcp://%s:%u" , address->get_ip_address (), address->get_port ());
240
+ } else {
241
+ std::sprintf (sock_addr, " socktcp://[%s]:%u" , address->get_ip_address (), address->get_port ());
242
+ }
243
+ }
244
+
245
+ _at.cmd_start (" AT^SISS=" );
246
+ _at.write_int (socket->id );
247
+ _at.write_string (" address" , false );
248
+ _at.write_string (sock_addr);
249
+ _at.cmd_stop_read_resp ();
250
+
251
+ _at.cmd_start (" AT^SISO=" );
252
+ _at.write_int (socket->id );
253
+ _at.cmd_stop_read_resp ();
254
+
255
+ if (_at.get_last_error ()) {
256
+ tr_error (" Socket %d open failed!" , socket->id );
257
+ _at.clear_error ();
258
+ socket_close_impl (socket->id ); // socket may already be open on modem if app and modem are not in sync, as a recovery, try to close the socket so open succeeds the next time
259
+ if (retry_open--) {
260
+ goto retry_open;
261
+ }
262
+ return NSAPI_ERROR_NO_SOCKET;
263
+ }
264
+
265
+ socket->created = true ;
266
+ tr_debug (" Cinterion open %d (err %d)" , socket->id , _at.get_last_error ());
267
+
268
+ return _at.get_last_error ();
269
+ }
270
+
271
+ // To open socket:
272
+ // 1. Select URC mode or polling mode with AT^SCFG
273
+ // 2. create a GPRS connection profile with AT^SICS (must have PDP)
274
+ // 3. create service profile with AT^SISS and map connectionID to serviceID
275
+ // 4. open internet session with AT^SISO (ELS61 tries to attach to a packet domain)
276
+ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_socket_impl (CellularSocket *socket)
277
+ {
288
278
if (socket->proto == NSAPI_UDP) {
289
279
if (GEMALTO_CINTERION::get_module () != GEMALTO_CINTERION::ModuleBGS2) {
290
280
return socket_open_defer (socket);
@@ -335,13 +325,13 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_sendto_impl(Cellul
335
325
}
336
326
}
337
327
if (!socket->started || !socket->tx_ready ) {
338
- tr_debug (" Socket %d would block (started %d, tx %d)" , socket->id , socket->started , socket->tx_ready );
328
+ tr_debug (" Socket %d send would block (started %d, tx %d)" , socket->id , socket->started , socket->tx_ready );
339
329
return NSAPI_ERROR_WOULD_BLOCK;
340
330
}
341
331
342
332
if (size > UDP_PACKET_SIZE) {
343
- tr_warn ( " Sending UDP packet size %d (max %d)" , size, UDP_PACKET_SIZE);
344
- size = UDP_PACKET_SIZE ;
333
+ tr_error ( " sendto size %d (max %d)" , size, UDP_PACKET_SIZE);
334
+ return NSAPI_ERROR_PARAMETER ;
345
335
}
346
336
347
337
_at.set_at_timeout (FAILURE_TIMEOUT);
@@ -413,13 +403,12 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
413
403
if (!socket->rx_avail ) {
414
404
_at.process_oob (); // check for ^SISR URC
415
405
if (!socket->rx_avail ) {
416
- tr_debug (" Socket %d would block" , socket->id );
406
+ tr_debug (" Socekt %d recv would block" , socket->id );
417
407
return NSAPI_ERROR_WOULD_BLOCK;
418
408
}
419
409
}
420
410
421
411
if (size > UDP_PACKET_SIZE) {
422
- tr_debug (" Socket recvfrom size %d > %d" , size, UDP_PACKET_SIZE);
423
412
size = UDP_PACKET_SIZE;
424
413
}
425
414
@@ -459,6 +448,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
459
448
}
460
449
socket->rx_avail = false ;
461
450
if (len >= (nsapi_size_or_error_t )size) {
451
+ len = (nsapi_size_or_error_t )size;
462
452
int remain_len = _at.read_int ();
463
453
if (remain_len > 0 ) {
464
454
socket->rx_avail = true ;
@@ -470,7 +460,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
470
460
char ip_address[NSAPI_IPv6_SIZE + sizeof (" []:12345" ) - 1 + 1 ];
471
461
int ip_len = _at.read_string (ip_address, sizeof (ip_address));
472
462
if (ip_len <= 0 ) {
473
- tr_error (" Socket %d recvfrom addr! " , socket->id );
463
+ tr_error (" Socket %d recvfrom addr (len %d) " , socket->id , ip_len );
474
464
return NSAPI_ERROR_DEVICE_ERROR;
475
465
}
476
466
if (address) {
0 commit comments