Sending custom BLE characteristic to SD card

I have developed my own custom BLE service. I need to send BLE characteristic to an SD card. I have implemented the FATFS example and it works.

Now I'm trying to merge my custom BLE application with FATFS. However, I've not been able to link a BLE characteristic WRITE event to write to writing to FATFS. 

Here is the code snippet. Here I'm just trying to link a BLE WRITE event to a FATFS WRITE function to write a generic string to the file (not sending characteristic data).

However, on debugging, the program execution never seems to enter the fatfs_write() function. 

What could be happening? Is there something wrong in the program flow logic? Please help. 

Thanks

static void on_cus_evt(ble_cus_t     * p_cus_service,
                       ble_cus_evt_t * p_evt,
                       ble_evt_t const * p_ble_evt_main)
{
    ret_code_t err_code;
    
    switch(p_evt->evt_type)
    {
        
        case BLE_CUS_EVT_WRITE:

            nrf_gpio_pin_toggle(LED_3);
            printf("%s"," write to sd card ");
            flag =1;
            break;
        case 2: ....
        case 3: ....
}

void fatfs_write()
{
      NRF_LOG_INFO("Writing to file " FILE_NAME "...");
                ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
                if (ff_result != FR_OK)
                {
                    NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
                    printf("%s"," unable to open ");
                    return;
                }

                ff_result = f_write(&file, TEST_STRING, sizeof(TEST_STRING) - 1, (UINT *) &bytes_written);
                if (ff_result != FR_OK)
                {
                    NRF_LOG_INFO("Write failed\r\n.");
                    printf("%s"," write failed ");

                }
                else
                {
                    NRF_LOG_INFO("%d bytes written.", bytes_written);
                    printf("%s"," byted written ");
                }

                (void) f_close(&file);
                flag =0;
                return;
}

void main()
{
//initialize BLE, FATFS
for (;;)
    {
        if(flag)
        {
           fatfs_write();
        }
}
}