Skip to content

Commit 0b58944

Browse files
authored
Add files via upload
registration callbacks and EPS status
1 parent 883beab commit 0b58944

File tree

2 files changed

+86
-11
lines changed

2 files changed

+86
-11
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

+75-8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitTries)
3737
_pingRequestCallback = NULL;
3838
_httpCommandRequestCallback = NULL;
3939
_mqttCommandRequestCallback = NULL;
40+
_registrationCallback = NULL;
41+
_epsRegistrationCallback = NULL;
4042
_debugAtPort = NULL;
4143
_debugPort = NULL;
4244
_printDebug = false;
@@ -566,6 +568,41 @@ bool SARA_R5::processURCEvent(const char *event)
566568
return true;
567569
}
568570
}
571+
{ // URC: +A
572+
int status = 0;
573+
unsigned int lac = 0, ci = 0, Act = 0;
574+
int scanNum = sscanf(event, "+CREG: %d,\"%4x\",\"%4x\",%d", &status, &lac, &ci, &Act);
575+
if (scanNum == 4)
576+
{
577+
if (_printDebug == true)
578+
_debugPort->println(F("processReadEvent: CREG"));
579+
580+
if (_registrationCallback != NULL)
581+
{
582+
_registrationCallback((SARA_R5_registration_status_t)status, lac, ci, Act);
583+
}
584+
585+
return true;
586+
}
587+
}
588+
{ // URC: +CEREG
589+
int status = 0;
590+
unsigned int tac = 0, ci = 0, Act = 0;
591+
int scanNum = sscanf(event, "+CEREG: %d,\"%4x\",\"%4x\",%d", &status, &tac, &ci, &Act);
592+
if (scanNum == 4)
593+
{
594+
if (_printDebug == true)
595+
_debugPort->println(F("processReadEvent: CEREG"));
596+
597+
if (_epsRegistrationCallback != NULL)
598+
{
599+
_epsRegistrationCallback((SARA_R5_registration_status_t)status, tac, ci, Act);
600+
}
601+
602+
return true;
603+
}
604+
}
605+
569606
return false;
570607
}
571608

@@ -670,6 +707,33 @@ void SARA_R5::setMQTTCommandCallback(void (*mqttCommandRequestCallback)(int comm
670707
_mqttCommandRequestCallback = mqttCommandRequestCallback;
671708
}
672709

710+
SARA_R5_error_t SARA_R5::setRegistrationCallback(void (*registrationCallback)(SARA_R5_registration_status_t status, unsigned int lac, unsigned int ci, int Act))
711+
{
712+
_registrationCallback = registrationCallback;
713+
714+
char *command = sara_r5_calloc_char(strlen(SARA_R5_REGISTRATION_STATUS) + 3);
715+
if (command == NULL)
716+
return SARA_R5_ERROR_OUT_OF_MEMORY;
717+
sprintf(command, "%s=%d", SARA_R5_REGISTRATION_STATUS, 2/*enable URC with location*/);
718+
SARA_R5_error_t err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR,
719+
NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT);
720+
free(command);
721+
return err;
722+
}
723+
724+
SARA_R5_error_t SARA_R5::setEpsRegistrationCallback(void (*registrationCallback)(SARA_R5_registration_status_t status, unsigned int tac, unsigned int ci, int Act))
725+
{
726+
_epsRegistrationCallback = registrationCallback;
727+
728+
char *command = sara_r5_calloc_char(strlen(SARA_R5_EPSREGISTRATION_STATUS) + 3);
729+
if (command == NULL)
730+
return SARA_R5_ERROR_OUT_OF_MEMORY;
731+
sprintf(command, "%s=%d", SARA_R5_EPSREGISTRATION_STATUS, 2/*enable URC with location*/);
732+
SARA_R5_error_t err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR,
733+
NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT);
734+
free(command);
735+
return err;
736+
}
673737

674738
size_t SARA_R5::write(uint8_t c)
675739
{
@@ -1353,17 +1417,17 @@ int8_t SARA_R5::rssi(void)
13531417
return rssi;
13541418
}
13551419

1356-
SARA_R5_registration_status_t SARA_R5::registration(void)
1420+
SARA_R5_registration_status_t SARA_R5::registration(bool eps)
13571421
{
13581422
char *command;
13591423
char *response;
13601424
SARA_R5_error_t err;
13611425
int status;
1362-
1363-
command = sara_r5_calloc_char(strlen(SARA_R5_REGISTRATION_STATUS) + 2);
1426+
const char* tag = eps ? SARA_R5_EPSREGISTRATION_STATUS : SARA_R5_REGISTRATION_STATUS;
1427+
command = sara_r5_calloc_char(strlen(tag) + 3);
13641428
if (command == NULL)
13651429
return SARA_R5_REGISTRATION_INVALID;
1366-
sprintf(command, "%s?", SARA_R5_REGISTRATION_STATUS);
1430+
sprintf(command, "%s?", tag);
13671431

13681432
response = sara_r5_calloc_char(minimumResponseAllocation);
13691433
if (response == NULL)
@@ -1381,11 +1445,14 @@ SARA_R5_registration_status_t SARA_R5::registration(void)
13811445
free(response);
13821446
return SARA_R5_REGISTRATION_INVALID;
13831447
}
1384-
1448+
13851449
int scanned = 0;
1386-
char *searchPtr = strstr(response, "+CREG: ");
1387-
if (searchPtr != NULL)
1388-
scanned = sscanf(searchPtr, "+CREG: %*d,%d", &status);
1450+
const char *startTag = eps ? "+CEREG: " : "+CREG: ";
1451+
char *searchPtr = strstr(response, startTag);
1452+
if (searchPtr != NULL) {
1453+
const char *format = eps ? "+CEREG: %*d,%d" : "+CREG: %*d,%d";
1454+
scanned = sscanf(searchPtr, format, &status);
1455+
}
13891456
if (scanned != 1)
13901457
status = SARA_R5_REGISTRATION_INVALID;
13911458

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const char SARA_R5_COMMAND_CNUM[] = "+CNUM"; // Subscriber number
113113
const char SARA_R5_SIGNAL_QUALITY[] = "+CSQ";
114114
const char SARA_R5_OPERATOR_SELECTION[] = "+COPS";
115115
const char SARA_R5_REGISTRATION_STATUS[] = "+CREG";
116+
const char SARA_R5_EPSREGISTRATION_STATUS[] = "+CEREG";
116117
const char SARA_R5_READ_OPERATOR_NAMES[] = "+COPN";
117118
const char SARA_R5_COMMAND_MNO[] = "+UMNOPROF"; // MNO (mobile network operator) Profile
118119
// ### SIM
@@ -185,8 +186,8 @@ const char SARA_R5_SEC_PROFILE[] = "+USECPRF";
185186
const char SARA_R5_SEC_MANAGER[] = "+USECMNG";
186187

187188
// ### Response
188-
const char SARA_R5_RESPONSE_OK[] = "\r\nOK\r\n";
189-
const char SARA_R5_RESPONSE_ERROR[] = "\r\nERROR\r\n";
189+
const char SARA_R5_RESPONSE_OK[] = "\nOK\r\n";
190+
const char SARA_R5_RESPONSE_ERROR[] = "\nERROR\r\n";
190191
const char SARA_R5_RESPONSE_CONNECT[] = "\r\nCONNECT\r\n";
191192
#define SARA_R5_RESPONSE_OK_OR_ERROR NULL
192193

@@ -624,6 +625,10 @@ class SARA_R5 : public Print
624625
void setPingCallback(void (*pingRequestCallback)(int retry, int p_size, String remote_hostname, IPAddress ip, int ttl, long rtt));
625626
void setHTTPCommandCallback(void (*httpCommandRequestCallback)(int profile, int command, int result));
626627
void setMQTTCommandCallback(void (*mqttCommandRequestCallback)(int command, int result));
628+
SARA_R5_error_t setRegistrationCallback(void (*registrationCallback)(SARA_R5_registration_status_t status,
629+
unsigned int lac, unsigned int ci, int Act));
630+
SARA_R5_error_t setEpsRegistrationCallback(void (*epsRegistrationCallback)(SARA_R5_registration_status_t status,
631+
unsigned int tac, unsigned int ci, int Act));
627632

628633
// Direct write/print to cell serial port
629634
virtual size_t write(uint8_t c);
@@ -663,7 +668,7 @@ class SARA_R5 : public Print
663668

664669
// Network service AT commands
665670
int8_t rssi(void); // Receive signal strength
666-
SARA_R5_registration_status_t registration(void);
671+
SARA_R5_registration_status_t registration(bool eps = true);
667672
bool setNetworkProfile(mobile_network_operator_t mno, bool autoReset = false, bool urcNotification = false);
668673
mobile_network_operator_t getNetworkProfile(void);
669674
typedef enum
@@ -973,6 +978,9 @@ class SARA_R5 : public Print
973978
void (*_pingRequestCallback)(int, int, String, IPAddress, int, long);
974979
void (*_httpCommandRequestCallback)(int, int, int);
975980
void (*_mqttCommandRequestCallback)(int, int);
981+
void (*_registrationCallback)(SARA_R5_registration_status_t status, unsigned int lac, unsigned int ci, int Act);
982+
void (*_epsRegistrationCallback)(SARA_R5_registration_status_t status, unsigned int tac, unsigned int ci, int Act);
983+
976984

977985
int _lastSocketProtocol[SARA_R5_NUM_SOCKETS]; // Record the protocol for each socket to avoid having to call querySocketType in parseSocketReadIndication
978986

0 commit comments

Comments
 (0)