/* mp3_decode.c */

#include <string.h>

#include "mp3_decode.h"

#include "ff.h"

/* file system interface. */
static bool     file_open (char * file_name);
static uint32_t file_read (uint8_t * buf, uint32_t buf_size);
static void     file_close(void);

/* mp3 buffer for mp3_decode. */
static uint8_t mp3_buf[MP3_DECODE_BUF_MIN_SIZE];

/* file handler. */
static FIL     fi;

/* the interface of mp3_deocde. */
mp3_decode_if_t mp3_dec_if =
{
    .mp3_buf      = mp3_buf,
    .mp3_buf_size = sizeof(mp3_buf),
    .file_open    = file_open,
    .file_read    = file_read,
    .file_close   = file_close,
};

/* open file. */
static bool file_open(char * file_name)
{
    if (FR_OK != f_open(&fi, file_name, FA_READ) )
    {
        return false;
    }
    return true;
}

/* read file. */
static uint32_t file_read(uint8_t * buf, uint32_t buf_size)
{
    UINT read_cnt = 0;

    if ( (FR_OK != f_read(&fi, &buf[read_cnt], buf_size, &read_cnt) ) )
    {
        return 0;
    }

    return (uint32_t)read_cnt;
}

/* close file. */
static void file_close(void)
{
    f_close(&fi);
}

/* EOF. */
