File transfer with cmd code via BLE.

Hi All,

        I am using NRF52840 with USB MSC code. I have managed to create a file inside flash chip. I am also able to transfer this file via BLE to the app. Now while doing file transfer I am diving the file into 200bytes of chunks and then transmitting data. I am successfully able to transmit the file as well. I have to add a cmd code "0x44" in each 200 byte transfer. How can I do that? 

File transfer start

0x44  + 200 bytes

0x44 +200 bytes

0x44 + 200 bytes

...

end of transfer.

Can anyone help me modfiy the code below:-


void read_from_file(void)
{
    FRESULT ff_result;
    FIL file;
    uint32_t bytes_read;


    fatfs_ls();
    
    if(file_found_on_sdcard)
    {
          NRF_LOG_INFO("Reading from file %s",my_filename);
          ff_result = f_open(&file, my_filename, FA_READ);
          if (ff_result != FR_OK)
          {
              NRF_LOG_INFO("Unable to open or create file:%s",my_filename);
              return;
          }

          ff_result = f_read(&file, file_buffer, FILE_SIZE_MAX, (UINT *) &bytes_read);
          if (ff_result != FR_OK)
          {
              NRF_LOG_INFO("Read failed\r\n.");
          }
          else
          {
              NRF_LOG_INFO("%d bytes read.", bytes_read);
              file_actual_read_size = bytes_read;
          }

          (void) f_close(&file);
    }
    else
    {
          NRF_LOG_INFO("No file named %s found",my_filename);
          file_send_to_peripheral = false;
    }
    return;
}


void send_file_to_app(void)
{
        read_from_file();
        
      
            if(file_actual_read_size > 0)
            {
                uint16_t remaining_bytes = file_actual_read_size;
                uint16_t chunk_length = MAX_HRM_LEN;
                ret_code_t err_code;
                while(remaining_bytes > 0)
                {
                    //err_code = ble_nus_data_send(&m_nus, &file_buffer[file_actual_read_size - remaining_bytes], &chunk_length, m_conn_handle);
                    err_code = rcbr_service_send_data(&file_buffer[file_actual_read_size - remaining_bytes], chunk_length);
                    if(err_code != NRF_ERROR_RESOURCES)
                    {
                        APP_ERROR_CHECK(err_code);
                        remaining_bytes -= chunk_length;
                        if(remaining_bytes < chunk_length)
                        {
                            chunk_length = remaining_bytes;
                        }
                        NRF_LOG_INFO("Remaining bytes to send: %d", remaining_bytes);
                    }
                }

            }
            file_send_to_peripheral = false;

}

Thanks & Regards,

Snehal

  • Hi Snehal,

    Thank you for contacting DevZone at NordicSemi.

    As in the send_file_to_app() function, first you are calling read_from_file() function, which checks if the file is on the sdcard and then opens and reads the file. The function also writes the contents to the file_buffer and number of bytes into file_actual_read_size variable (you might have declared globally).


    As you want to send the command-character + the data of size chunk_length from the file_buffer

    There could be different ways to do this, but I think you must construct a new (temporary) buffer of size chunk_length+1 and put 0x44 at location 0 and then copy the data from file buffer and send.

    I show a cope snippet below, on as is basis, that is exactly doing the same:

    In this example, I have 10 byte data, and I want to send in chunk-size of 4

    I am constructing a new buffer of size 5 and fill it with command and then data

    int main()
    {
        int file_actual_read_size = 10;                                     //number of bytes 10
        char file_buffer[10] ={'A','B','C','D','E','F','G','H','I','J'};    //buffer of 10 size
        
        int chunksize=4;                                                    //transfer size=4
        char temp_buffer[5];                                                //temp buffer of size chunksize+1
        int remaining_bytes = file_actual_read_size;                        //initially all bytes are remaining
        
        if(file_actual_read_size > 0)
        {
            int itr = 0;
            while(remaining_bytes > 0)
            {
                //fill temp buffer
                temp_buffer[0] = 0x44;
                int sindex = file_actual_read_size - remaining_bytes;
                for(int i=0; i<chunksize; i++)
                {
                    temp_buffer[i+1] = file_buffer[sindex + i];
                }
                remaining_bytes -= chunksize;
                //call sending funtion with temp_buffer
                printf("\niteration#%d, transferring: ", ++itr);
                for(int i=0; i<=chunksize; i++)
                {
                    printf("%c", temp_buffer[i]);
                }
                
            }
        }
        return 0;
    }

    Below I show the sample output (I am just displaying the buffer, you will be sending the buffer)

    Where the initial character 'D' has hex-code of 0x44 as per ascii table.

    I hope it helps.

    Regards,

    Naeem

Related