Skip to content

Commit 742ce5b

Browse files
committed
Improve sync process logic
1 parent 6d41a13 commit 742ce5b

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

command_interface.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,39 @@ void command_interface_process_byte(char incoming)
7878
{
7979
NRF_LOG_INFO("ACK handler >%s<", command_input_buffer);
8080
NRF_LOG_FLUSH();
81-
command_interface_continue_transfer( command_input_buffer );
81+
if (sync_in_progress)
82+
{
83+
command_interface_continue_transfer( command_input_buffer );
84+
}
85+
else
86+
{
87+
NRF_LOG_WARNING("ACK received while !sync_in_progress");
88+
NRF_LOG_FLUSH();
89+
}
8290
}
8391
else if( strncmp(&command_input_buffer[strlen(command_input_buffer)-4], "nack", 4) == 0)
8492
{
8593
NRF_LOG_INFO("NACK handler >%s<", command_input_buffer);
8694
NRF_LOG_FLUSH();
8795
if (sync_in_progress)
8896
{
89-
// For a cat command we want to seek to the byte the client last received
90-
if(strncmp(command_input_buffer, "cat", 3) == 0)
97+
if (cat_in_progress)
9198
{
92-
// Set the file position to what we've received on the client side
93-
lfs_file_seek(m_lfs, &file_command_interface, atoi(command_input_buffer+4), LFS_SEEK_SET);
99+
// For a cat command we want to seek to the byte the client last received
100+
if(strncmp(command_input_buffer, "cat", 3) == 0)
101+
{
102+
// Set the file position to what we've received on the client side
103+
lfs_file_seek(m_lfs, &file_command_interface, atoi(command_input_buffer+4), LFS_SEEK_SET);
104+
}
94105
}
95106
// Continue to send data to the client
96107
command_interface_continue_transfer(command_input_buffer);
97108
}
109+
else
110+
{
111+
NRF_LOG_WARNING("NACK received while !sync_in_progress");
112+
NRF_LOG_FLUSH();
113+
}
98114
}
99115
else if(strncmp(command_input_buffer, "log", 3) == 0)
100116
{
@@ -191,6 +207,7 @@ void command_interface_process_byte(char incoming)
191207
}
192208
if(lfs_file_opencfg(m_lfs, &file_command_interface, filename, LFS_O_RDONLY, &lfs_file_config) >= 0)
193209
{
210+
lfs_file_seek(m_lfs, &file_command_interface, 0, LFS_SEEK_SET); //TODO: Testing rewind on open, necessary?
194211
sprintf((char *)command_response_buffer, "cat,%s", filename);
195212
m_ble_tx_logbuffer(command_response_buffer, (size_t)strlen((const char *)command_response_buffer));
196213
bytes_sent = 0;
@@ -448,22 +465,30 @@ void command_interface_continue_transfer(char* command)
448465
}
449466
else if(strncmp(command_input_buffer, "cat", 3) == 0)
450467
{
468+
if (!cat_in_progress)
469+
{
470+
NRF_LOG_WARNING("CAT ACK/NACK received while !cat_in_progress");
471+
NRF_LOG_FLUSH();
472+
return;
473+
}
451474
NRF_LOG_INFO("command_interface_continue_transfer(TRANSFER_MODE_CAT)");
452475
NRF_LOG_FLUSH();
453476

454477
if(bytes_sent >= file_command_interface.ctz.size)
455478
{
456479
m_ble_tx_logbuffer((unsigned char *)"cat,complete", strlen("cat,complete"));
457480

458-
NRF_LOG_INFO("finished cat");
481+
NRF_LOG_INFO("sending cat,complete");
459482
NRF_LOG_FLUSH();
460483

484+
//NOTE: Leaving file open in case the client needs to ACK/NACK
485+
//NOTE: File will be closed upon request of the next file or syncstop
486+
/*
461487
int close_result = lfs_file_close(m_lfs, &file_command_interface);
462-
463488
NRF_LOG_INFO("cat close result: %d", close_result);
464489
NRF_LOG_FLUSH();
465-
466490
cat_in_progress = false;
491+
*/
467492
}
468493
else if(bytes_sent < file_command_interface.ctz.size)
469494
{

0 commit comments

Comments
 (0)