@@ -2315,8 +2315,19 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest)
2315
2315
char *strBegin;
2316
2316
int readIndex = 0 ;
2317
2317
SARA_R5_error_t err;
2318
+ int scanNum;
2319
+ int readLength = 0 ;
2320
+ int socketStore = 0 ;
2318
2321
2319
- command = sara_r5_calloc_char (strlen (SARA_R5_READ_SOCKET) + 8 );
2322
+ // Check if length is zero
2323
+ if (length == 0 )
2324
+ {
2325
+ if (_printDebug == true )
2326
+ _debugPort->print (F (" socketRead: length is 0! Call socketReadAvailable?" ));
2327
+ return SARA_R5_ERROR_UNEXPECTED_PARAM;
2328
+ }
2329
+
2330
+ command = sara_r5_calloc_char (strlen (SARA_R5_READ_SOCKET) + 32 );
2320
2331
if (command == NULL )
2321
2332
return SARA_R5_ERROR_OUT_OF_MEMORY;
2322
2333
sprintf (command, " %s=%d,%d" , SARA_R5_READ_SOCKET, socket, length);
@@ -2333,6 +2344,36 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest)
2333
2344
2334
2345
if (err == SARA_R5_ERROR_SUCCESS)
2335
2346
{
2347
+ // Extract the data - and check the quote is present
2348
+ scanNum = sscanf (response, " +USORD: %d,%d,\" " ,
2349
+ &socketStore, &readLength);
2350
+ if (scanNum != 2 )
2351
+ {
2352
+ if (_printDebug == true )
2353
+ {
2354
+ _debugPort->print (F (" socketRead: error: scanNum is " ));
2355
+ _debugPort->println (scanNum);
2356
+ }
2357
+ free (command);
2358
+ free (response);
2359
+ return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2360
+ }
2361
+
2362
+ // Check that readLength == length.
2363
+ if (readLength != length)
2364
+ {
2365
+ if (_printDebug == true )
2366
+ {
2367
+ _debugPort->print (F (" socketRead: error: length=" ));
2368
+ _debugPort->print (length);
2369
+ _debugPort->print (F (" readLength=" ));
2370
+ _debugPort->println (readLength);
2371
+ }
2372
+ free (command);
2373
+ free (response);
2374
+ return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2375
+ }
2376
+
2336
2377
// Find the first double-quote:
2337
2378
strBegin = strchr (response, ' \" ' );
2338
2379
@@ -2343,11 +2384,15 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest)
2343
2384
return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2344
2385
}
2345
2386
2346
- while ((readIndex < length) && (readIndex < (int )strlen (strBegin)))
2387
+ // Now copy the data into readDest
2388
+ while (readIndex < length)
2347
2389
{
2348
2390
readDest[readIndex] = strBegin[1 + readIndex];
2349
- readIndex += 1 ;
2391
+ readIndex++ ;
2350
2392
}
2393
+
2394
+ if (_printDebug == true )
2395
+ _debugPort->println (F (" socketRead: success" ));
2351
2396
}
2352
2397
2353
2398
free (command);
@@ -2356,15 +2401,28 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest)
2356
2401
return err;
2357
2402
}
2358
2403
2359
- SARA_R5_error_t SARA_R5::socketReadUDP (int socket, int length, char *readDest)
2404
+ SARA_R5_error_t SARA_R5::socketReadUDP (int socket, int length, char *readDest, IPAddress *remoteIPAddress, int *remotePort )
2360
2405
{
2361
2406
char *command;
2362
2407
char *response;
2363
2408
char *strBegin;
2364
2409
int readIndex = 0 ;
2365
2410
SARA_R5_error_t err;
2411
+ int scanNum;
2412
+ int remoteIPstore[4 ] = { 0 , 0 , 0 , 0 };
2413
+ int portStore = 0 ;
2414
+ int readLength = 0 ;
2415
+ int socketStore = 0 ;
2416
+
2417
+ // Check if length is zero
2418
+ if (length == 0 )
2419
+ {
2420
+ if (_printDebug == true )
2421
+ _debugPort->print (F (" socketReadUDP: length is 0! Call socketReadAvailableUDP?" ));
2422
+ return SARA_R5_ERROR_UNEXPECTED_PARAM;
2423
+ }
2366
2424
2367
- command = sara_r5_calloc_char (strlen (SARA_R5_READ_UDP_SOCKET) + 16 );
2425
+ command = sara_r5_calloc_char (strlen (SARA_R5_READ_UDP_SOCKET) + 32 );
2368
2426
if (command == NULL )
2369
2427
return SARA_R5_ERROR_OUT_OF_MEMORY;
2370
2428
sprintf (command, " %s=%d,%d" , SARA_R5_READ_UDP_SOCKET, socket, length);
@@ -2381,14 +2439,38 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest)
2381
2439
2382
2440
if (err == SARA_R5_ERROR_SUCCESS)
2383
2441
{
2384
- // Find the third double-quote. This needs to be improved to collect other data.
2385
- if (_printDebug == true )
2386
- _debugPort->print (F (" socketReadUDP: {" ));
2387
- if (_printDebug == true )
2388
- _debugPort->print (response);
2389
- if (_printDebug == true )
2390
- _debugPort->println (F (" }" ));
2442
+ // Extract the data - and check the third quote is present
2443
+ scanNum = sscanf (response, " +USORF: %d,\" %d.%d.%d.%d\" ,%d,%d,\" " ,
2444
+ &socketStore, &remoteIPstore[0 ], &remoteIPstore[1 ], &remoteIPstore[2 ], &remoteIPstore[3 ],
2445
+ &portStore, &readLength);
2446
+ if (scanNum != 7 )
2447
+ {
2448
+ if (_printDebug == true )
2449
+ {
2450
+ _debugPort->print (F (" socketReadUDP: error: scanNum is " ));
2451
+ _debugPort->println (scanNum);
2452
+ }
2453
+ free (command);
2454
+ free (response);
2455
+ return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2456
+ }
2391
2457
2458
+ // Check that readLength == length.
2459
+ if (readLength != length)
2460
+ {
2461
+ if (_printDebug == true )
2462
+ {
2463
+ _debugPort->print (F (" socketReadUDP: error: length=" ));
2464
+ _debugPort->print (length);
2465
+ _debugPort->print (F (" readLength=" ));
2466
+ _debugPort->println (readLength);
2467
+ }
2468
+ free (command);
2469
+ free (response);
2470
+ return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2471
+ }
2472
+
2473
+ // Find the third double-quote
2392
2474
strBegin = strchr (response, ' \" ' );
2393
2475
strBegin = strchr (strBegin + 1 , ' \" ' );
2394
2476
strBegin = strchr (strBegin + 1 , ' \" ' );
@@ -2400,11 +2482,32 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest)
2400
2482
return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
2401
2483
}
2402
2484
2403
- while ((readIndex < length) && (readIndex < (int )strlen (strBegin)))
2485
+ // Now copy the data into readDest
2486
+ while (readIndex < length)
2404
2487
{
2405
2488
readDest[readIndex] = strBegin[1 + readIndex];
2406
- readIndex += 1 ;
2489
+ readIndex++ ;
2407
2490
}
2491
+
2492
+ // If remoteIPaddress is not NULL, copy the remote IP address
2493
+ if (remoteIPAddress != NULL )
2494
+ {
2495
+ IPAddress tempAddress;
2496
+ for (int i = 0 ; i <= 3 ; i++)
2497
+ {
2498
+ tempAddress[i] = (uint8_t )remoteIPstore[i];
2499
+ }
2500
+ *remoteIPAddress = tempAddress;
2501
+ }
2502
+
2503
+ // If remotePort is not NULL, copy the remote port
2504
+ if (remotePort != NULL )
2505
+ {
2506
+ *remotePort = portStore;
2507
+ }
2508
+
2509
+ if (_printDebug == true )
2510
+ _debugPort->println (F (" socketReadUDP: success" ));
2408
2511
}
2409
2512
2410
2513
free (command);
0 commit comments