Example #1
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_rename
 * DESCRIPTION
 *
 * PARAMETERS
 *  srcName         [IN]
 *  destName        [IN]
 * RETURNS
 *
 *****************************************************************************/
S32 btmtk_fs_rename(const U8 *srcName, const U8 *destName)
{
#ifdef BTMTK_ON_WISE
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U16 ucs2_buf[FS_MAX_PATH_LEN] = {0};

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)srcName, FS_MAX_PATH_LEN * sizeof(U16));
    bt_asc_str_n_to_ucs2_str((S8 *)ucs2_buf, (S8 *)destName, FS_MAX_PATH_LEN * sizeof(U16));
    result = FS_Rename(srcName, destName);
    return translateFileError(result);
#else /* BTMTK_ON_WISE */
    U8 pathBuf2[FS_MAX_PATH_LEN] = {0};
    translateFilePath(srcName, (U8 *)pathBuf);
    translateFilePath(destName, pathBuf2);
#if defined(BTMTK_ON_LINUX)
    if (rename((char *)pathBuf, (char*)pathBuf2) == 0)
#else
    if (FALSE != MoveFileA((char *)pathBuf, pathBuf2))
#endif
    {
        return BTMTK_FS_OK;
    }
    return BTMTK_FS_ERR;
#endif /* BTMTK_ON_WISE */
}
Example #2
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_create_dir
 * DESCRIPTION
 *
 * PARAMETERS
 *  dirName     [IN]
 * RETURNS
 *
 *****************************************************************************/
S32 btmtk_fs_create_dir(const U8 *dirName)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    bt_prompt_trace(MOD_BT, "[FS] +btmtk_fs_create_dir(%s)", dirName);
#ifdef BTMTK_ON_WISE
    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)dirName, FS_MAX_PATH_LEN * sizeof(U16));
    return translateFileError(FS_CreateDir(pathBuf));
#else
    translateFilePath(dirName, (U8 *)pathBuf);
#ifdef BTMTK_ON_LINUX
    if (mkdir((char *)pathBuf, 0777) != 0)
#else
    if (_mkdir((char *)pathBuf) != 0)
#endif
    {
        bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_create_dir failed : %s, errno=%d", strerror(errno), errno);
        return BTMTK_FS_ERR;
    }
    bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_create_dir");
    return BTMTK_FS_OK;
#endif /* BTMTK_ON_WISE */
}
Example #3
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_delete
 * DESCRIPTION
 *
 * PARAMETERS
 *  fileName        [IN]
 * RETURNS
 *
 *****************************************************************************/
S32 btmtk_fs_delete(const U8 *fileName)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    bt_prompt_trace(MOD_BT, "[FS] +btmtk_fs_delete(%s)", fileName);
#ifdef BTMTK_ON_WISE
    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)fileName, FS_MAX_PATH_LEN * sizeof(U16));
    return translateFileError(FS_Delete(pathBuf));
#else

    translateFilePath(fileName, (U8 *)pathBuf);
    if (remove((char *)pathBuf) != 0)
    {
        bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_delete failed : %s, errno=%d", strerror(errno), errno);
        return BTMTK_FS_ERR;
    }
    bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_delete");
    return BTMTK_FS_OK;
#endif /* BTMTK_ON_WISE */
}
Example #4
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_is_dir_exist
 * DESCRIPTION
 *
 * PARAMETERS
 *  dirName     [IN]
 * RETURNS
 *
 *****************************************************************************/
BT_BOOL btmtk_fs_is_dir_exist(const U8 *dirName)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef BTMTK_ON_WISE
    FS_HANDLE file_handle;

    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)dirName, FS_MAX_PATH_LEN * sizeof(U16));
    if ((file_handle = FS_Open(pathBuf, FS_OPEN_DIR | FS_READ_ONLY)) > 0)
    {
        FS_Close(file_handle);
        return TRUE;
    }
    return FALSE;
#else /* BTMTK_ON_WISE */
#ifdef BTMTK_ON_LINUX
    struct stat buf;
    BT_BOOL bRet;
#else
    struct _stat buf;
#endif
    translateFilePath(dirName, (U8 *)pathBuf);
#ifdef BTMTK_ON_LINUX
    bRet = (BT_BOOL)((stat((char *)pathBuf, &buf) != 0) ? FALSE : TRUE);
    if(!bRet)
    {
        bt_prompt_trace(MOD_BT, "[FS] btmtk_fs_is_dir_exist failed : %s, errno=%d", strerror(errno), errno);
    }
    return bRet;
#else
    return (BT_BOOL)((_stat((char *)pathBuf, &buf) != 0) ? FALSE : TRUE);
#endif
#endif /* BTMTK_ON_WISE */
    bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_is_dir_exist");
}
Example #5
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_get_filesize
 * DESCRIPTION
 *
 * PARAMETERS
 *  fileName        [IN]
 * RETURNS
 *
 *****************************************************************************/
S32 btmtk_fs_get_filesize(const U8 *fileName)
{
#ifdef BTMTK_ON_WISE
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int err, handle;
    U32 size;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)fileName, FS_MAX_PATH_LEN * sizeof(U16));
    handle = FS_Open(pathBuf, FS_READ_ONLY | FS_OPEN_SHARED);
    if (handle < 0)
    {
        return translateFileError(handle);
    }
    err = FS_GetFileSize(handle, &size);
    FS_Close(handle);
    return translateFileError(err);
#else /* BTMTK_ON_WISE */
    FILE *fp;
    int size;

    translateFilePath(fileName, (U8 *)pathBuf);
    fp = fopen((char *)pathBuf, translateFileModeC(BTMTK_FS_READ_ONLY));
    if (fp == NULL)
    {
        return BTMTK_FS_ERR;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fclose(fp);
    return size;
#endif /* BTMTK_ON_WISE */
}
Example #6
0
int encodec_sbc(
        U8 min_bit_pool,
        U8 block_len, // b0: 16, b1: 12, b2: 8, b3: 4
        U8 subband_num, // b0: 8, b1: 4
        U8 alloc_method, // b0: loudness, b1: SNR
        U8 sample_rate, // b0: 48000, b1: 44100, b2: 32000, b3: 16000
        U8 channel_mode // b0: joint stereo, b1: stereo, b2: dual channel, b3: mono
        )
{
    FILE       *wavIn, *sbcOut;
    SbcPcmData  pcmData;
    SbcEncoder  encoder;
    U16         bytesToRead;
    U16         bytesToEncode;
    U16         bytesEncoded;
    XaStatus    status;
    U8          *fNameIn;

    U8          *fNameOut;
    U32         sampleFreq = 0;
    U32         chunk2Size = 0;
    U16         sbcLen;
    U8 pathBuf[_MAX_PATH] = {0};
    int         encodedFrameLen;

    /* Initialize default encoder settings */
    SBC_InitEncoder(&encoder);
    fNameOut = BT_A2DP_SBC_FILE;
    switch(sample_rate)
    {
        case 0x01: /* 48.0K*/
            fNameIn = (U8 *)&BT_A2DP_SRC48_FILE;
            break;
        case 0x02:
            fNameIn = (U8 *)&BT_A2DP_SRC44_FILE;
            break;
        case 0x04:
            fNameIn = (U8 *)&BT_A2DP_SRC32_FILE;
            break;
        case 0x08:
            fNameIn = (U8 *)&BT_A2DP_SRC16_FILE;
            break;

    }
    switch(block_len)
    {
        case 0x01:
            encoder.streamInfo.numBlocks = 16;
            break;
        case 0x02:
            encoder.streamInfo.numBlocks = 12;
            break;
        case 0x04:
            encoder.streamInfo.numBlocks = 8;
            break;
        case 0x08:
            encoder.streamInfo.numBlocks = 4;
            break;
    }
    switch(subband_num)
    {
        case 0x01:
            encoder.streamInfo.numSubBands = 8;
            break;
        case 0x02:
            encoder.streamInfo.numSubBands = 4;
            break;
    }
    switch(alloc_method)
    {
        case 0x01:
            encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_LOUDNESS;
            break;
        case 0x02:
            encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_SNR;
            break;
    }
    switch(channel_mode)
    {
        case 0x01:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_JOINT_STEREO;
            break;
        case 0x02:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_STEREO;
            break;
        case 0x04:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_DUAL_CHNL;
            break;
        case 0x08:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_MONO;
            break;
    }

    encoder.streamInfo.bitPool = min_bit_pool;    
#if 0        
    encoder.streamInfo.numBlocks = 16;
    encoder.streamInfo.numSubBands = 8;
    encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_LOUDNESS;
    encoder.streamInfo.channelMode = SBC_CHNL_MODE_JOINT_STEREO;
    encoder.streamInfo.bitPool = 35;
#endif

    /* Open the WAV file */
    memset(pathBuf, 0, sizeof(pathBuf));
    translateFilePath(fNameIn, pathBuf);
    wavIn = fopen(pathBuf, "rb");
    if(wavIn == NULL)
        return -1;
    memset(pathBuf, 0, sizeof(pathBuf));
    OS_MemSet(pathBuf, 0, sizeof(pathBuf));
    translateFilePath(fNameOut, pathBuf);
    sbcOut = fopen(pathBuf, "wb");
    if(sbcOut == NULL)
        return -1;    

    /**** Parse the WAV header ****/
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "RIFF", 4) != 0) {
            printf("Invalid RIFF file format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Read format */
    fseek(wavIn, 8, 0);
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "WAVE", 4) != 0) {
            printf("Invalid WAV format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /**** Start subchunk 1 ****/
    fseek(wavIn, 12, 0);
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "fmt", 3) != 0) {
            printf("Invalid format chunk\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }
    
    /* PCM */
    fseek(wavIn, 20, 0);
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        if (LEtoHost16(pcmBuffer) != 1) {
            printf("Unsupported audio format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Channels */
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        encoder.streamInfo.numChannels = pcmBuffer[0];
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Sample frequency */
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {

        sampleFreq = LEtoHost32(pcmBuffer);

        switch (sampleFreq) {
        case 48000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_48;
            break;
        case 44100:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_44_1;
            break;
        case 32000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_32;
            break;
        case 16000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_16;
            break;
        default:
            printf("Unsupported sampling frequency %d\n", sampleFreq);
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }
    /* Bits per sample */
    fseek(wavIn, 34, 0);
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        if (LEtoHost32(pcmBuffer) != 16) {
            printf("Unsupported sample size\n");
        }
    }

    /**** Data chunk ****/
    fseek(wavIn, 44, 0);

    /* Print out SBC format */
    printf("Start\n");
    printf("Sampling Frequency = %d\n", sampleFreq);

    if (encoder.streamInfo.numChannels == 1) {
        if (encoder.streamInfo.channelMode != SBC_CHNL_MODE_MONO) {
            printf("Switching to mono mode...\n");
        }
        encoder.streamInfo.channelMode = SBC_CHNL_MODE_MONO;
        printf("Mode = Mono\n");
    } else {
        switch (encoder.streamInfo.channelMode) {
        case SBC_CHNL_MODE_JOINT_STEREO:
            printf("Mode = Joint Stereo\n");
            break;
        case SBC_CHNL_MODE_STEREO:
            printf("Mode = Stereo\n");
            break;
        case SBC_CHNL_MODE_DUAL_CHNL:
            printf("Mode = Dual Channel\n");
            break;
        default:
            printf("Mode = Unknown\n");
        }
    }

    printf("Blocks = %d\n", encoder.streamInfo.numBlocks);
    printf("Subbands = %d\n", encoder.streamInfo.numSubBands);

    if (encoder.streamInfo.allocMethod == SBC_ALLOC_METHOD_SNR) {
        printf("Allocation = SNR\n");
    } else if(encoder.streamInfo.allocMethod == SBC_ALLOC_METHOD_LOUDNESS) {
        printf("Allocation = LOUDNESS\n");
    } else {
        printf("Allocation = Unknown\n");
    }

    printf("Bitpool = %d\n", encoder.streamInfo.bitPool);

    /* Initialize PCM data structure */
    pcmData.data = pcmBuffer;
    bytesToRead = encoder.streamInfo.numChannels * 
                  encoder.streamInfo.numSubBands * 
                  encoder.streamInfo.numBlocks * 2;
    bytesToRead = (PCM_BUFF_SIZE / bytesToRead) * bytesToRead;

    /* Start reading the SBC data and encoding */
    while ((bytesToEncode = fread(pcmBuffer, 1, bytesToRead, wavIn))) {

        pcmData.data = pcmBuffer;
        pcmData.dataLen = bytesToEncode;

        while (bytesToEncode) {

            /* Encode the PCM buffer into the SBC buffer */
            status = SBC_EncodeFrames(&encoder, &pcmData, &bytesEncoded, 
                                      sbcBuffer, &sbcLen, SBC_BUFF_SIZE);

            bytesToEncode -= bytesEncoded;
            pcmData.data += bytesEncoded;
            pcmData.dataLen -= bytesEncoded;

            if (status == XA_STATUS_SUCCESS) {

                /* Block encoded */
                if (fwrite(sbcBuffer, 1, sbcLen, sbcOut) != sbcLen) {
                    printf("Error writing SBC data\n");
                    return -1;
                }

            } else if (status != XA_STATUS_CONTINUE) {
                /* Parsing error */
                printf("Error encoding SBC stream\n");
                return -1;
            } else {
                /* Not enough data left to encode a frame */
                break;
            }
        }
    }

    printf("Done\n");
    fclose(wavIn);
    fclose(sbcOut);
    encodedFrameLen = SBC_FrameLen(&encoder.streamInfo);
    return encodedFrameLen;
}
Example #7
0
/*****************************************************************************
 * FUNCTION
 *  btmtk_fs_open
 * DESCRIPTION
 *
 * PARAMETERS
 *  fileName        [IN]
 *  mode            [IN]
 * RETURNS
 *
 *****************************************************************************/
S32 btmtk_fs_open(const U8 *fileName, U32 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int i;

#ifdef BTMTK_ON_WISE
    int err, err1;
#else
    FILE *fp;
    U8 fileExist = 0;
#endif

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    for (i = 0; i < BT_MAX_NUM_FILES; i++)
    {
        if (bt_file_desc[i].used == KAL_FALSE)
        {
            break;
        }
    }

    if (i >= BT_MAX_NUM_FILES)
    {
        return BTMTK_FS_ERR_FULL;
    }

#ifdef BTMTK_ON_WISE
    bt_asc_str_n_to_ucs2_str((S8 *)pathBuf, (S8 *)fileName, FS_MAX_PATH_LEN * sizeof(U16));
    err = FS_Open(pathBuf, translateFileMode(mode));
    if ((mode & BTMTK_FS_APPEND) && err >= 0)
    {
        err1 = FS_Seek(err, 0, FS_FILE_END);
        err = (err1 < 0) ? err1 : err;
    }

    if (err >= 0)
    {
        bt_file_desc[i] = err;
        bt_file_desc[i].used = KAL_TRUE;
        return i;
    }
    else
    {
        return translateFileError(err);
    }
#else /* BTMTK_ON_WISE */
    bt_prompt_trace(MOD_BT, "[FS] +btmtk_fs_open(fileName=%s, mode=0x%X)", fileName, mode);
    translateFilePath(fileName, (U8 *)pathBuf);
    bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_open: %s", fileName);
    fileExist = IsFileExist((const char *)pathBuf);
    fp = fopen((char *)pathBuf, translateFileModeC(mode));
    if (fp == NULL)
    {
        bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_open failed : %s, errno=%d", strerror(errno), errno);
        return BTMTK_FS_ERR;
    }
    else
    {
        bt_file_desc[i].handle = (int)fp;
        bt_file_desc[i].used = KAL_TRUE;
        if( chmod((const char *)pathBuf, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0 )
            bt_prompt_trace(MOD_BT, "[ERR] chmod failed : errno=%u, %s", errno, strerror(errno));
        bt_prompt_trace(MOD_BT, "[FS] -btmtk_fs_open handle=0x%x", bt_file_desc[i]);
        return i;
    }
#endif /* BTMTK_ON_WISE */
}