|
| 1 | +#include "sfe_sara_r5.h" |
| 2 | + |
| 3 | +UBX_CELL_error_t SARA_R5::setUtimeMode(UBX_CELL_utime_mode_t mode, UBX_CELL_utime_sensor_t sensor) |
| 4 | +{ |
| 5 | + UBX_CELL_error_t err; |
| 6 | + char *command; |
| 7 | + |
| 8 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_REQUEST_TIME) + 16); |
| 9 | + if (command == nullptr) |
| 10 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 11 | + if (mode == UBX_CELL_UTIME_MODE_STOP) // stop UTIME does not require a sensor |
| 12 | + sprintf(command, "%s=%d", SARA_R5_GNSS_REQUEST_TIME, mode); |
| 13 | + else |
| 14 | + sprintf(command, "%s=%d,%d", SARA_R5_GNSS_REQUEST_TIME, mode, sensor); |
| 15 | + |
| 16 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 17 | + nullptr, UBX_CELL_10_SEC_TIMEOUT); |
| 18 | + free(command); |
| 19 | + return err; |
| 20 | +} |
| 21 | + |
| 22 | +UBX_CELL_error_t SARA_R5::getUtimeMode(UBX_CELL_utime_mode_t *mode, UBX_CELL_utime_sensor_t *sensor) |
| 23 | +{ |
| 24 | + UBX_CELL_error_t err; |
| 25 | + char *command; |
| 26 | + char *response; |
| 27 | + |
| 28 | + UBX_CELL_utime_mode_t m; |
| 29 | + UBX_CELL_utime_sensor_t s; |
| 30 | + |
| 31 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_REQUEST_TIME) + 2); |
| 32 | + if (command == nullptr) |
| 33 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 34 | + sprintf(command, "%s?", SARA_R5_GNSS_REQUEST_TIME); |
| 35 | + |
| 36 | + response = ubx_cell_calloc_char(minimumResponseAllocation); |
| 37 | + if (response == nullptr) |
| 38 | + { |
| 39 | + free(command); |
| 40 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 41 | + } |
| 42 | + |
| 43 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 44 | + response, UBX_CELL_10_SEC_TIMEOUT); |
| 45 | + |
| 46 | + // Response format: \r\n+UTIME: <mode>[,<sensor>]\r\n\r\nOK\r\n |
| 47 | + if (err == UBX_CELL_ERROR_SUCCESS) |
| 48 | + { |
| 49 | + int mStore, sStore, scanned = 0; |
| 50 | + char *searchPtr = strstr(response, "+UTIME:"); |
| 51 | + if (searchPtr != nullptr) |
| 52 | + { |
| 53 | + searchPtr += strlen("+UTIME:"); // Move searchPtr to first character - probably a space |
| 54 | + while (*searchPtr == ' ') searchPtr++; // skip spaces |
| 55 | + scanned = sscanf(searchPtr, "%d,%d\r\n", &mStore, &sStore); |
| 56 | + } |
| 57 | + m = (UBX_CELL_utime_mode_t)mStore; |
| 58 | + s = (UBX_CELL_utime_sensor_t)sStore; |
| 59 | + if (scanned == 2) |
| 60 | + { |
| 61 | + *mode = m; |
| 62 | + *sensor = s; |
| 63 | + } |
| 64 | + else if (scanned == 1) |
| 65 | + { |
| 66 | + *mode = m; |
| 67 | + *sensor = UBX_CELL_UTIME_SENSOR_NONE; |
| 68 | + } |
| 69 | + else |
| 70 | + err = UBX_CELL_ERROR_UNEXPECTED_RESPONSE; |
| 71 | + } |
| 72 | + |
| 73 | + free(command); |
| 74 | + free(response); |
| 75 | + return err; |
| 76 | +} |
| 77 | + |
| 78 | +UBX_CELL_error_t SARA_R5::setUtimeIndication(UBX_CELL_utime_urc_configuration_t config) |
| 79 | +{ |
| 80 | + UBX_CELL_error_t err; |
| 81 | + char *command; |
| 82 | + |
| 83 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_TIME_INDICATION) + 16); |
| 84 | + if (command == nullptr) |
| 85 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 86 | + sprintf(command, "%s=%d", SARA_R5_GNSS_TIME_INDICATION, config); |
| 87 | + |
| 88 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 89 | + nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 90 | + free(command); |
| 91 | + return err; |
| 92 | +} |
| 93 | + |
| 94 | +UBX_CELL_error_t SARA_R5::getUtimeIndication(UBX_CELL_utime_urc_configuration_t *config) |
| 95 | +{ |
| 96 | + UBX_CELL_error_t err; |
| 97 | + char *command; |
| 98 | + char *response; |
| 99 | + |
| 100 | + UBX_CELL_utime_urc_configuration_t c; |
| 101 | + |
| 102 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_TIME_INDICATION) + 2); |
| 103 | + if (command == nullptr) |
| 104 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 105 | + sprintf(command, "%s?", SARA_R5_GNSS_TIME_INDICATION); |
| 106 | + |
| 107 | + response = ubx_cell_calloc_char(minimumResponseAllocation); |
| 108 | + if (response == nullptr) |
| 109 | + { |
| 110 | + free(command); |
| 111 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 112 | + } |
| 113 | + |
| 114 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 115 | + response, UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 116 | + |
| 117 | + // Response format: \r\n+UTIMEIND: <mode>\r\n\r\nOK\r\n |
| 118 | + if (err == UBX_CELL_ERROR_SUCCESS) |
| 119 | + { |
| 120 | + int cStore, scanned = 0; |
| 121 | + char *searchPtr = strstr(response, "+UTIMEIND:"); |
| 122 | + if (searchPtr != nullptr) |
| 123 | + { |
| 124 | + searchPtr += strlen("+UTIMEIND:"); // Move searchPtr to first char |
| 125 | + while (*searchPtr == ' ') searchPtr++; // skip spaces |
| 126 | + scanned = sscanf(searchPtr, "%d\r\n", &cStore); |
| 127 | + } |
| 128 | + c = (UBX_CELL_utime_urc_configuration_t)cStore; |
| 129 | + if (scanned == 1) |
| 130 | + { |
| 131 | + *config = c; |
| 132 | + } |
| 133 | + else |
| 134 | + err = UBX_CELL_ERROR_UNEXPECTED_RESPONSE; |
| 135 | + } |
| 136 | + |
| 137 | + free(command); |
| 138 | + free(response); |
| 139 | + return err; |
| 140 | +} |
| 141 | + |
| 142 | +UBX_CELL_error_t SARA_R5::setUtimeConfiguration(int32_t offsetNanoseconds, int32_t offsetSeconds) |
| 143 | +{ |
| 144 | + UBX_CELL_error_t err; |
| 145 | + char *command; |
| 146 | + |
| 147 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_TIME_CONFIGURATION) + 48); |
| 148 | + if (command == nullptr) |
| 149 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 150 | +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
| 151 | + sprintf(command, "%s=%d,%d", SARA_R5_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds); |
| 152 | +#else |
| 153 | + sprintf(command, "%s=%ld,%ld", SARA_R5_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds); |
| 154 | +#endif |
| 155 | + |
| 156 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 157 | + nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 158 | + free(command); |
| 159 | + return err; |
| 160 | +} |
| 161 | + |
| 162 | +UBX_CELL_error_t SARA_R5::getUtimeConfiguration(int32_t *offsetNanoseconds, int32_t *offsetSeconds) |
| 163 | +{ |
| 164 | + UBX_CELL_error_t err; |
| 165 | + char *command; |
| 166 | + char *response; |
| 167 | + |
| 168 | + int32_t ons; |
| 169 | + int32_t os; |
| 170 | + |
| 171 | + command = ubx_cell_calloc_char(strlen(SARA_R5_GNSS_TIME_CONFIGURATION) + 2); |
| 172 | + if (command == nullptr) |
| 173 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 174 | + sprintf(command, "%s?", SARA_R5_GNSS_TIME_CONFIGURATION); |
| 175 | + |
| 176 | + response = ubx_cell_calloc_char(minimumResponseAllocation); |
| 177 | + if (response == nullptr) |
| 178 | + { |
| 179 | + free(command); |
| 180 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 181 | + } |
| 182 | + |
| 183 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, |
| 184 | + response, UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 185 | + |
| 186 | + // Response format: \r\n+UTIMECFG: <offset_nano>,<offset_sec>\r\n\r\nOK\r\n |
| 187 | + if (err == UBX_CELL_ERROR_SUCCESS) |
| 188 | + { |
| 189 | + int scanned = 0; |
| 190 | + char *searchPtr = strstr(response, "+UTIMECFG:"); |
| 191 | + if (searchPtr != nullptr) |
| 192 | + { |
| 193 | + searchPtr += strlen("+UTIMECFG:"); // Move searchPtr to first char |
| 194 | + while (*searchPtr == ' ') searchPtr++; // skip spaces |
| 195 | +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
| 196 | + scanned = sscanf(searchPtr, "%d,%d\r\n", &ons, &os); |
| 197 | +#else |
| 198 | + scanned = sscanf(searchPtr, "%ld,%ld\r\n", &ons, &os); |
| 199 | +#endif |
| 200 | + } |
| 201 | + if (scanned == 2) |
| 202 | + { |
| 203 | + *offsetNanoseconds = ons; |
| 204 | + *offsetSeconds = os; |
| 205 | + } |
| 206 | + else |
| 207 | + err = UBX_CELL_ERROR_UNEXPECTED_RESPONSE; |
| 208 | + } |
| 209 | + |
| 210 | + free(command); |
| 211 | + free(response); |
| 212 | + return err; |
| 213 | +} |
| 214 | + |
| 215 | +UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configuration_parameter_t parameter, int value) |
| 216 | +{ |
| 217 | + UBX_CELL_error_t err; |
| 218 | + char *command; |
| 219 | + |
| 220 | + if (profile >= UBX_CELL_NUM_PSD_PROFILES) |
| 221 | + return UBX_CELL_ERROR_ERROR; |
| 222 | + |
| 223 | + command = ubx_cell_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 24); |
| 224 | + if (command == nullptr) |
| 225 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 226 | + sprintf(command, "%s=%d,%d,%d", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, |
| 227 | + value); |
| 228 | + |
| 229 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, |
| 230 | + UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 231 | + |
| 232 | + free(command); |
| 233 | + return err; |
| 234 | +} |
| 235 | + |
| 236 | +UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configuration_parameter_t parameter, UBX_CELL_pdp_protocol_type_t value) |
| 237 | +{ |
| 238 | + return (setPDPconfiguration(profile, parameter, (int)value)); |
| 239 | +} |
| 240 | + |
| 241 | +UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configuration_parameter_t parameter, String value) |
| 242 | +{ |
| 243 | + UBX_CELL_error_t err; |
| 244 | + char *command; |
| 245 | + |
| 246 | + if (profile >= UBX_CELL_NUM_PSD_PROFILES) |
| 247 | + return UBX_CELL_ERROR_ERROR; |
| 248 | + |
| 249 | + command = ubx_cell_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 64); |
| 250 | + if (command == nullptr) |
| 251 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 252 | + sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, |
| 253 | + value.c_str()); |
| 254 | + |
| 255 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, |
| 256 | + UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 257 | + |
| 258 | + free(command); |
| 259 | + return err; |
| 260 | +} |
| 261 | + |
| 262 | +UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configuration_parameter_t parameter, IPAddress value) |
| 263 | +{ |
| 264 | + UBX_CELL_error_t err; |
| 265 | + char *command; |
| 266 | + |
| 267 | + if (profile >= UBX_CELL_NUM_PSD_PROFILES) |
| 268 | + return UBX_CELL_ERROR_ERROR; |
| 269 | + |
| 270 | + command = ubx_cell_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 64); |
| 271 | + if (command == nullptr) |
| 272 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 273 | + sprintf(command, "%s=%d,%d,\"%d.%d.%d.%d\"", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, |
| 274 | + value[0], value[1], value[2], value[3]); |
| 275 | + |
| 276 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, |
| 277 | + UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 278 | + |
| 279 | + free(command); |
| 280 | + return err; |
| 281 | +} |
| 282 | + |
| 283 | +UBX_CELL_error_t SARA_R5::performPDPaction(int profile, UBX_CELL_pdp_actions_t action) |
| 284 | +{ |
| 285 | + UBX_CELL_error_t err; |
| 286 | + char *command; |
| 287 | + |
| 288 | + if (profile >= UBX_CELL_NUM_PSD_PROFILES) |
| 289 | + return UBX_CELL_ERROR_ERROR; |
| 290 | + |
| 291 | + command = ubx_cell_calloc_char(strlen(SARA_R5_MESSAGE_PDP_ACTION) + 32); |
| 292 | + if (command == nullptr) |
| 293 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 294 | + sprintf(command, "%s=%d,%d", SARA_R5_MESSAGE_PDP_ACTION, profile, action); |
| 295 | + |
| 296 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, |
| 297 | + UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 298 | + |
| 299 | + free(command); |
| 300 | + return err; |
| 301 | +} |
| 302 | + |
| 303 | +UBX_CELL_error_t SARA_R5::getNetworkAssignedIPAddress(int profile, IPAddress *address) |
| 304 | +{ |
| 305 | + char *command; |
| 306 | + char *response; |
| 307 | + UBX_CELL_error_t err; |
| 308 | + int scanNum = 0; |
| 309 | + int profileStore = 0; |
| 310 | + int paramTag = 0; // 0: IP address: dynamic IP address assigned during PDP context activation |
| 311 | + int paramVals[4]; |
| 312 | + |
| 313 | + command = ubx_cell_calloc_char(strlen(SARA_R5_NETWORK_ASSIGNED_DATA) + 16); |
| 314 | + if (command == nullptr) |
| 315 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 316 | + sprintf(command, "%s=%d,%d", SARA_R5_NETWORK_ASSIGNED_DATA, profile, paramTag); |
| 317 | + |
| 318 | + response = ubx_cell_calloc_char(minimumResponseAllocation); |
| 319 | + if (response == nullptr) |
| 320 | + { |
| 321 | + free(command); |
| 322 | + return UBX_CELL_ERROR_OUT_OF_MEMORY; |
| 323 | + } |
| 324 | + |
| 325 | + err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, response, |
| 326 | + UBX_CELL_STANDARD_RESPONSE_TIMEOUT); |
| 327 | + |
| 328 | + if (err == UBX_CELL_ERROR_SUCCESS) |
| 329 | + { |
| 330 | + char *searchPtr = strstr(response, "+UPSND:"); |
| 331 | + if (searchPtr != nullptr) |
| 332 | + { |
| 333 | + searchPtr += strlen("+UPSND:"); // Move searchPtr to first char |
| 334 | + while (*searchPtr == ' ') searchPtr++; // skip spaces |
| 335 | + scanNum = sscanf(searchPtr, "%d,%d,\"%d.%d.%d.%d\"", |
| 336 | + &profileStore, ¶mTag, |
| 337 | + ¶mVals[0], ¶mVals[1], ¶mVals[2], ¶mVals[3]); |
| 338 | + } |
| 339 | + if (scanNum != 6) |
| 340 | + { |
| 341 | + if (_printDebug == true) |
| 342 | + { |
| 343 | + _debugPort->print(F("getNetworkAssignedIPAddress: error: scanNum is ")); |
| 344 | + _debugPort->println(scanNum); |
| 345 | + } |
| 346 | + free(command); |
| 347 | + free(response); |
| 348 | + return UBX_CELL_ERROR_UNEXPECTED_RESPONSE; |
| 349 | + } |
| 350 | + |
| 351 | + IPAddress tempAddress = { (uint8_t)paramVals[0], (uint8_t)paramVals[1], |
| 352 | + (uint8_t)paramVals[2], (uint8_t)paramVals[3] }; |
| 353 | + *address = tempAddress; |
| 354 | + } |
| 355 | + |
| 356 | + free(command); |
| 357 | + free(response); |
| 358 | + |
| 359 | + return err; |
| 360 | +} |
0 commit comments