Skip to content

Commit b0f9e3c

Browse files
authored
Merge pull request #40 from sparkfun/release_candidate
v1.1.10 - update getFileBlock
2 parents d35bce3 + 9ed0292 commit b0f9e3c

3 files changed

+78
-32
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun u-blox SARA-R5 Arduino Library
2-
version=1.1.9
2+
version=1.1.10
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud<br/><br/>

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

+76-30
Original file line numberDiff line numberDiff line change
@@ -5580,10 +5580,14 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, char *contents)
55805580
return err;
55815581
}
55825582

5583-
SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size_t offset, size_t requested_length, size_t& bytes_read)
5583+
SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size_t offset, size_t requestedLength, size_t& bytesRead)
55845584
{
5585-
bytes_read = 0;
5586-
if (filename.length() < 1 || buffer == nullptr || requested_length < 1)
5585+
SARA_R5_error_t err;
5586+
char *command;
5587+
char *response;
5588+
5589+
bytesRead = 0;
5590+
if (filename.length() < 1 || buffer == nullptr || requestedLength < 1)
55875591
{
55885592
return SARA_R5_ERROR_UNEXPECTED_PARAM;
55895593
}
@@ -5599,54 +5603,96 @@ SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size
55995603
return SARA_R5_ERROR_INVALID;
56005604
}
56015605

5602-
size_t cmd_len = filename.length() + 32;
5603-
char* cmd = sara_r5_calloc_char(cmd_len);
5604-
sprintf(cmd, "at+urdblock=\"%s\",%zu,%zu\r\n", filename.c_str(), offset, requested_length);
5605-
sendCommand(cmd, false);
5606+
command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_READ_BLOCK) + filename.length() + 28);
5607+
if (command == nullptr)
5608+
{
5609+
return SARA_R5_ERROR_OUT_OF_MEMORY;
5610+
}
5611+
sprintf(command, "%s=\"%s\",%lu,%lu", SARA_R5_FILE_SYSTEM_READ_BLOCK, filename.c_str(), (unsigned long) offset, (unsigned long) requestedLength);
56065612

5613+
response = sara_r5_calloc_char(minimumResponseAllocation);
5614+
if (response == nullptr)
5615+
{
5616+
if (_printDebug == true)
5617+
{
5618+
_debugPort->print(F("getFileBlock: response alloc failed: "));
5619+
_debugPort->println(minimumResponseAllocation);
5620+
}
5621+
free(command);
5622+
return SARA_R5_ERROR_OUT_OF_MEMORY;
5623+
}
5624+
5625+
// Send command and wait for some response
5626+
// Response format: \r\n+URDBLOCK: "filename",64000,"these bytes are the data of the file block"\r\n\r\nOK\r\n
5627+
sendCommand(command, true);
5628+
err = waitForResponse(SARA_R5_FILE_SYSTEM_READ_BLOCK, SARA_R5_RESPONSE_ERROR, 5 * SARA_R5_STANDARD_RESPONSE_TIMEOUT);
5629+
if (err != SARA_R5_ERROR_SUCCESS)
5630+
{
5631+
if (_printDebug == true)
5632+
{
5633+
_debugPort->print(F("getFileBlock: waitForResponse returned err "));
5634+
_debugPort->println(err);
5635+
}
5636+
free(command);
5637+
free(response);
5638+
return err;
5639+
}
5640+
5641+
// Skip the filename in quotes and get the data length index
56075642
int ich;
56085643
char ch;
5609-
int quote_count = 0;
5610-
size_t comma_idx = 0;
5611-
5612-
while (quote_count < 3)
5644+
int quoteCount = 0;
5645+
size_t lengthIndex = 0;
5646+
while (quoteCount < 3 && bytesRead < minimumResponseAllocation)
56135647
{
56145648
ich = _hardSerial->read();
56155649
if (ich < 0)
56165650
{
56175651
continue;
56185652
}
56195653
ch = (char)(ich & 0xFF);
5620-
cmd[bytes_read++] = ch;
5654+
response[bytesRead++] = ch;
56215655
if (ch == '"')
56225656
{
5623-
quote_count++;
5657+
quoteCount++;
56245658
}
5625-
else if (ch == ',' && comma_idx == 0)
5659+
else if (ch == ',' && lengthIndex == 0 && quoteCount == 2)
56265660
{
5627-
comma_idx = bytes_read;
5661+
lengthIndex = bytesRead;
56285662
}
56295663
}
5664+
response[bytesRead] = 0; // Make response a null-terminated string
5665+
response[bytesRead - 2] = 0; // Terminate response string right after block length
5666+
size_t data_length = strtoul(&response[lengthIndex], nullptr, 10);
56305667

5631-
cmd[bytes_read] = 0;
5632-
cmd[bytes_read - 2] = 0;
5633-
5634-
// Example response:
5635-
// +URDBLOCK: "wombat.bin",64000,"<data starts here>... "<cr><lf>
5636-
size_t data_length = strtoul(&cmd[comma_idx], nullptr, 10);
5637-
free(cmd);
5638-
5639-
bytes_read = 0;
5640-
size_t bytes_remaining = data_length;
5641-
while (bytes_read < data_length)
5668+
// Read file block data directly into supplied buffer
5669+
bytesRead = 0;
5670+
size_t bytesRemaining = data_length;
5671+
while (bytesRead < data_length)
56425672
{
56435673
// This method seems more reliable than reading a byte at a time.
5644-
size_t rc = _hardSerial->readBytes(&buffer[bytes_read], bytes_remaining);
5645-
bytes_read += rc;
5646-
bytes_remaining -= rc;
5674+
size_t rc = _hardSerial->readBytes(&buffer[bytesRead], bytesRemaining);
5675+
bytesRead += rc;
5676+
bytesRemaining -= rc;
56475677
}
56485678

5649-
return SARA_R5_ERROR_SUCCESS;
5679+
// Read rest of response until \r\nOK\r\n
5680+
err = waitForResponse(SARA_R5_RESPONSE_OK, SARA_R5_RESPONSE_ERROR, SARA_R5_STANDARD_RESPONSE_TIMEOUT);
5681+
if (err != SARA_R5_ERROR_SUCCESS)
5682+
{
5683+
if (_printDebug == true)
5684+
{
5685+
_debugPort->print(F("getFileBlock: waitForResponse returned err "));
5686+
_debugPort->println(err);
5687+
}
5688+
free(command);
5689+
free(response);
5690+
return err;
5691+
}
5692+
5693+
free(command);
5694+
free(response);
5695+
return err;
56505696
}
56515697

56525698
SARA_R5_error_t SARA_R5::getFileSize(String filename, int *size)

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ class SARA_R5 : public Print
10101010
// TO DO: add full support for file tags. Default tag to USER
10111011
SARA_R5_error_t getFileContents(String filename, String *contents); // OK for text files. But will fail with binary files (containing \0) on some platforms.
10121012
SARA_R5_error_t getFileContents(String filename, char *contents); // OK for binary files. Make sure contents can hold the entire file. Get the size first with getFileSize.
1013-
SARA_R5_error_t getFileBlock(const String& filename, char* buffer, size_t offset, size_t length, size_t& bytes_read); // OK for binary files. Make sure buffer can hold the requested block size.
1013+
SARA_R5_error_t getFileBlock(const String& filename, char* buffer, size_t offset, size_t requestedLength, size_t& bytesRead); // OK for binary files. Make sure buffer can hold the requested block size.
10141014

10151015
// Append data to a file, delete file first to not appends the data.
10161016
SARA_R5_error_t appendFileContents(String filename, String str);

0 commit comments

Comments
 (0)