#include "aud_manage.h"
#include "crc_8.h"
#include "aud_config.h"
/*桢字段接受处理*/
static BOOL protocol_receive_frame(E_PROTOCOL_TYPE* pp_type, struct t_aud_frame* pp_aud_frame, u8 vp_c, BOOL* pp_excframe_finish)
{
static BOOL fl_comple = TRUE;
static u16 readcnt = 0;
BOOL ret = FALSE;
static u8 tmp_data[1024] = { 0 };
switch (*pp_type)
{
case PROTOCOL_HEAD:
fl_comple = TRUE; readcnt = 0;
if (vp_c == PROTOCOL_HEAD_CODE) //获取起始码
{
pp_aud_frame->m_Head = PROTOCOL_HEAD_CODE;
(*pp_type) = PROTOCOL_CRC;
}
break;
case PROTOCOL_CRC:
pp_aud_frame->m_Crc = vp_c; //获取CRC校验码
(*pp_type) = PROTOCOL_ID;
break;
case PROTOCOL_ID:
pp_aud_frame->m_CmdId = vp_c; //获取命令ID
(*pp_type) = PROTOCOL_DLEN;
break;
case PROTOCOL_DLEN: //获取数据长度 LSB - MSB
if (fl_comple == TRUE)
{
pp_aud_frame->m_DataLen = (vp_c & 0xff);
fl_comple = FALSE;
}
else
{
pp_aud_frame->m_DataLen |= (vp_c << 8);
fl_comple = TRUE;
(*pp_type) = (pp_aud_frame->m_DataLen == 0) ? PROTOCOL_HEAD : PROTOCOL_DATA;
}
break;
case PROTOCOL_DATA: //获取数据
if (pp_aud_frame->m_Data == NULL) /*动态分配数据缓冲区,请务必释放 */
{
AUD_TRACE("get mem size: %d Byte \r\n", pp_aud_frame->m_DataLen);
pp_aud_frame->m_Data = tmp_data;
}
pp_aud_frame->m_Data[readcnt++] = vp_c;
(*pp_type) = (readcnt == pp_aud_frame->m_DataLen) ? PROTOCOL_HEAD : PROTOCOL_DATA;
readcnt = ((*pp_type) == PROTOCOL_HEAD) ? 0 : readcnt;
if ((*pp_type) == PROTOCOL_HEAD)
{
(*pp_excframe_finish) = TRUE;
ret = TRUE;
}
break;
default:
break;
}
return ret;
}
/*帧应答*/
BOOL aud_protocol_response(struct t_aud_frame* pp_aud_frame, E_ERRCODE vp_errcode, struct t_rsp_code* ext_usr_rsp_code)
{
struct t_rsp_code rsp_code;
u32 ret = 0;
/*传输模式不应答*/
if (pp_aud_frame == NULL || ((pp_aud_frame->m_CmdId == AUDIO_DATA_IND) && (ext_usr_rsp_code == NULL)))
{
return TRUE;
}
/*自定义应答*/
if (ext_usr_rsp_code != NULL) /*the param 3 only support aud data ind status*/
{
rsp_code.m_ContextId = ext_usr_rsp_code->m_ContextId;
rsp_code.m_PlayResult = ext_usr_rsp_code->m_PlayResult;
ret = protocol_send_frame(AUDIO_DATA_OVER_RSP, &rsp_code, AUD_TYPE_RSP);
return ret;
}
switch (pp_aud_frame->m_CmdId)
{
case START_RECORDER_REQ:
rsp_code.m_ContextId = 0;
rsp_code.m_PlayResult = vp_errcode;
ret = protocol_send_frame(START_RECORDER_RSP, &rsp_code, RECORDER_TYPE_RSP);
break;
case STOP_RECORDER_REQ:
rsp_code.m_ContextId = 0;
rsp_code.m_PlayResult = vp_errcode;
ret = protocol_send_frame(STOP_RECORDER_RSP, &rsp_code, STOP_REC_TYPE_RSP);
break;
case START_PLAY_AUDIO_REQ:
rsp_code.m_ContextId = (pp_aud_frame->m_Data[7]) | (pp_aud_frame->m_Data[8] << 8) | (pp_aud_frame->m_Data[9] << 16) | (pp_aud_frame->m_Data[10] << 24);
rsp_code.m_PlayResult = vp_errcode;
ret = protocol_send_frame(START_PLAY_AUDIO_RSP, &rsp_code, AUD_TYPE_RSP);
break;
case STOP_PLAY_AUDIO_REQ:
rsp_code.m_ContextId = (pp_aud_frame->m_Data[7]) | (pp_aud_frame->m_Data[8] << 8) | (pp_aud_frame->m_Data[9] << 16) | (pp_aud_frame->m_Data[10] << 24);
rsp_code.m_PlayResult = vp_errcode;
ret = protocol_send_frame(STOP_PLAY_AUDIO_RSP, &rsp_code, AUD_TYPE_RSP);
break;
case AUDIO_DATA_IND:
rsp_code.m_ContextId = (pp_aud_frame->m_Data[7]) | (pp_aud_frame->m_Data[8] << 8) | (pp_aud_frame->m_Data[9] << 16) | (pp_aud_frame->m_Data[10] << 24);
rsp_code.m_PlayResult = vp_errcode;
ret = protocol_send_frame(AUDIO_DATA_OVER_RSP, &rsp_code, AUD_TYPE_RSP);
break;
default:
break;
}
return (ret > 0) ? TRUE : FALSE;
}
/*audio协议帧解析*/
BOOL aud_protocol_parse(const void* pp_ptr, u32 vp_ulen, struct t_aud_frame** pp_aud_frame_out, E_PROTOCOL_TYPE** vp_status_out, BOOL* pp_excframe_finish)
{
u32 i = 0;
BOOL ret = FALSE;
/*遍历buf 合成音频app协议帧*/
static struct t_aud_frame aud_frame;
/*当前接收数据类型*/
static E_PROTOCOL_TYPE _protocol_type = PROTOCOL_HEAD;
*vp_status_out = &_protocol_type;
for (i = 0; i < vp_ulen; i++)
{
ret = protocol_receive_frame(&_protocol_type, &aud_frame, ((u8*)pp_ptr)[i], pp_excframe_finish); //读取一帧
//if ( (*pp_excframe_finish)== TRUE) //成功读取一帧,判断CRC校验
if (ret == TRUE)
{
if (aud_get_crc8(&aud_frame) == aud_frame.m_Crc)
{
AUD_TRACE("crc match success.\r\n");
aud_frame.m_Valid = TRUE;
}
else
{
AUD_TRACE("crc match failed: getcrc = 0x%x, calcrc = 0x%x .\r\n", aud_frame.m_Crc, aud_get_crc8(&aud_frame));
aud_frame.m_Valid = FALSE;
}
/*导出解析后的协议帧*/
*pp_aud_frame_out = &aud_frame;
}
}
return ret;
}
/*释放帧资源*/
void aud_destory_frame(struct t_aud_frame* pp_aud_frame)
{
if (pp_aud_frame == NULL)
{
return;
}
if (pp_aud_frame->m_Data) // 执行完成后,释放内存
{
memset(pp_aud_frame->m_Data,0,1024);
pp_aud_frame->m_Data = NULL;
AUD_TRACE("clr mem size: %d Byte \r\n", pp_aud_frame->m_DataLen);
}
memset(pp_aud_frame, 0, sizeof(struct t_aud_frame));
}
/*audio协议帧执行*/
BOOL aud_protocol_excute(struct t_aud_frame* pp_aud_frame, struct t_cmd_hander* vp_cmd_handler_tbl, u8 vp_tab_len, E_ERRCODE* vp_errcode)
{
u8 index = 0;
/*执行命令*/
for (index; index < vp_tab_len; index++)
{
if (pp_aud_frame->m_CmdId == (vp_cmd_handler_tbl[index]).m_CmdId)
{
return ((vp_cmd_handler_tbl[index]).m_handler)(pp_aud_frame, vp_errcode);
}
}
AUD_TRACE("cmd no support \r\n");
return FALSE;
}
/*发送帧*/
u32 protocol_send_frame(u8 vp_CmdId, struct t_rsp_code* pp_rspcode, E_TYPE_RSP vp_type_rsp)
{
/*应答请求*/
struct t_aud_frame rsp_aud_frame;
u8 rsq_buf[64] = { 0 };
u8 rsq_size = 0;
rsp_aud_frame.m_Head = PROTOCOL_HEAD_CODE;
rsp_aud_frame.m_CmdId = vp_CmdId;
rsp_aud_frame.m_DataLen = (vp_type_rsp == AUD_TYPE_RSP) ? sizeof(struct t_rsp_code) : (sizeof(pp_rspcode->m_PlayResult));
rsp_aud_frame.m_Data = (u8*)pp_rspcode;
rsp_aud_frame.m_Crc = aud_get_crc8(&rsp_aud_frame);
memcpy(rsq_buf, (u8*)&rsp_aud_frame, (u32)&(((struct t_aud_frame*)0)->m_DataLen)); //拷贝应答头
switch (vp_type_rsp)
{
case AUD_TYPE_RSP:
//拷贝数据长度 LSb-MSB
rsq_buf[3] = (rsp_aud_frame.m_DataLen) & 0xff; rsq_buf[4] = (rsp_aud_frame.m_DataLen >> 8) & 0xff;
//拷贝应答码
rsq_buf[5] = pp_rspcode->m_PlayResult & 0xff; rsq_buf[6] = (pp_rspcode->m_PlayResult >> 8) & 0xff;
rsq_buf[7] = (pp_rspcode->m_PlayResult >> 16) & 0xff; rsq_buf[8] = (pp_rspcode->m_PlayResult >> 24) & 0xff;
//拷贝上下文ID
rsq_buf[9] = pp_rspcode->m_ContextId & 0xff; rsq_buf[10] = (pp_rspcode->m_ContextId >> 8) & 0xff;
rsq_buf[11] = (pp_rspcode->m_ContextId >> 16) & 0xff; rsq_buf[12] = (pp_rspcode->m_ContextId >> 24) & 0xff;
rsq_size = 13;
break;
case RECORDER_TYPE_RSP:
//拷贝数据长度 LSb-MSB
rsq_buf[3] = (rsp_aud_frame.m_DataLen) & 0xff; rsq_buf[4] = (rsp_aud_frame.m_DataLen >> 8) & 0xff;
//拷贝应答码
rsq_buf[5] = pp_rspcode->m_PlayResult & 0xff; rsq_buf[6] = (pp_rspcode->m_PlayResult >> 8) & 0xff;
rsq_buf[7] = (pp_rspcode->m_PlayResult >> 16) & 0xff; rsq_buf[8] = (pp_rspcode->m_PlayResult >> 24) & 0xff;
rsq_size = 9;
break;
case STOP_REC_TYPE_RSP:
rsq_buf[3] = 0; rsq_buf[4] = 0;
rsq_size = 5;
break;
default:
break;
}
return pfv_sci_SendData(USE_AUDIO_SCI, &rsq_buf, rsq_size);
}
/*帧校验*/
u8 aud_get_crc8(struct t_aud_frame* pp_aud_frame)
{
/*校验buf数据*/
u8 p_data[1024] = {0};
memset(p_data, 0, pp_aud_frame->m_DataLen + 3);
p_data[0] = pp_aud_frame->m_CmdId;
memcpy((u8*)&p_data[1], (u8*)&(pp_aud_frame->m_DataLen), sizeof(u16));
memcpy((u8*)&p_data[3], pp_aud_frame->m_Data, pp_aud_frame->m_DataLen);
/*计算校验*/
u8 crc = get_crc8(p_data, pp_aud_frame->m_DataLen + 3);
return crc;
}