Skip to content

Commit 7a0d715

Browse files
committed
Update getFileBlock
This adds more error checking and command formatting to make it similar to other methods. Also makes detecting comma_idx (offset of file size in the response) more reliable
1 parent 8916bc4 commit 7a0d715

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -5582,6 +5582,8 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, char *contents)
55825582

55835583
SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size_t offset, size_t requested_length, size_t& bytes_read)
55845584
{
5585+
SARA_R5_error_t err;
5586+
char *command;
55855587
bytes_read = 0;
55865588
if (filename.length() < 1 || buffer == nullptr || requested_length < 1)
55875589
{
@@ -5599,10 +5601,15 @@ SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size
55995601
return SARA_R5_ERROR_INVALID;
56005602
}
56015603

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

56075614
int ich;
56085615
char ch;
@@ -5617,24 +5624,24 @@ SARA_R5_error_t SARA_R5::getFileBlock(const String& filename, char* buffer, size
56175624
continue;
56185625
}
56195626
ch = (char)(ich & 0xFF);
5620-
cmd[bytes_read++] = ch;
5627+
command[bytes_read++] = ch;
56215628
if (ch == '"')
56225629
{
56235630
quote_count++;
56245631
}
5625-
else if (ch == ',' && comma_idx == 0)
5632+
else if (ch == ',' && quote_count == 2)
56265633
{
56275634
comma_idx = bytes_read;
56285635
}
56295636
}
56305637

5631-
cmd[bytes_read] = 0;
5632-
cmd[bytes_read - 2] = 0;
5638+
command[bytes_read] = 0;
5639+
command[bytes_read - 2] = 0;
56335640

56345641
// Example response:
56355642
// +URDBLOCK: "wombat.bin",64000,"<data starts here>... "<cr><lf>
5636-
size_t data_length = strtoul(&cmd[comma_idx], nullptr, 10);
5637-
free(cmd);
5643+
size_t data_length = strtoul(&command[comma_idx], nullptr, 10);
5644+
free(command);
56385645

56395646
bytes_read = 0;
56405647
size_t bytes_remaining = data_length;

0 commit comments

Comments
 (0)