466 lines
16 KiB
C
466 lines
16 KiB
C
/**************************************************************************************************
|
||
* Copyright (c) Fenda Technologies Co., Ltd. 2020-2021. All rights reserved.
|
||
* Description: 音乐接口函数
|
||
* Author: ball.caojianguang
|
||
* Create: 2020-5-21
|
||
************************************************************************************************/
|
||
|
||
#include "ble_music_server.h"
|
||
#include "string.h"
|
||
#include "task_ble.h"
|
||
#include "ble_data_transmission.h"
|
||
#include "ui_event.h"
|
||
#include "task_ui.h"
|
||
#include "ble_management_server.h"
|
||
#include "amota_api.h"
|
||
|
||
|
||
uint8_t app_playState = 0;
|
||
|
||
|
||
MusicPlayInfo_t musicPlayInfo = {0};
|
||
|
||
void MusicCtrlInit(void)
|
||
{
|
||
memset(&musicPlayInfo, 0, sizeof(musicPlayInfo));
|
||
musicPlayInfo.playState = MUSIC_STATE_DISABLE;
|
||
}
|
||
|
||
|
||
void GetMusicInfo(MusicPlayInfo_t *info)
|
||
{
|
||
memcpy(info, &musicPlayInfo, sizeof(MusicPlayInfo_t));
|
||
}
|
||
|
||
|
||
void SetMusicInfo(const MusicPlayInfo_t *info)
|
||
{
|
||
memcpy(&musicPlayInfo, info, sizeof(MusicPlayInfo_t));
|
||
}
|
||
|
||
|
||
/****************************************************************************
|
||
|
||
音乐播放开关-4.14.1
|
||
|
||
*****************************************************************************/
|
||
void BLE_DeviceMusicContrlSwitch(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
protocol_data_info_t data_info = {0};
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
|
||
data_info.error = FENDA_SUCCESS;
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case MUSIC_CTRL:
|
||
app_playState = *data_info.p_data;
|
||
ble_printf("MusicControlSwitch Value = 0x%x.\r\n", *data_info.p_data);
|
||
// ------TO BE ADDED 待添加音乐控制开关log打点 -------
|
||
if(*data_info.p_data) {
|
||
musicPlayInfo.playState = MUSIC_STATE_GETTING;
|
||
task_ble_notify(BLE_QUERY_MUSIC_INFO_MSG, 0); // to do
|
||
} else {
|
||
musicPlayInfo.playState = MUSIC_STATE_DISABLE;
|
||
// ------TO BE ADDED 待添加音乐播放状态通知UI -------
|
||
task_ui_notify(EVENT_INFO_CACHE_LOADING,0,&musicPlayInfo,sizeof(musicPlayInfo)); // to do
|
||
}
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
|
||
} while(in_len);
|
||
|
||
write_idx = user_set_protocol_error(out_data, MUSIC_SERVER, data_info.error);
|
||
*out_len = write_idx;
|
||
}
|
||
|
||
/***************************************************************************************************
|
||
|
||
设备请求音乐播放状态-4.14.2
|
||
|
||
***************************************************************************************************/
|
||
|
||
void BLE_DeviceRequestMusicPlayStatusUpload(void)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
uint8_t buf_temp[TEMP_NUM_MAX] = {0};
|
||
ble_printf("Device Request Music play status begin.\r\n");
|
||
buf_temp[write_idx++] = 0x02; //LTV L<>?
|
||
buf_temp[write_idx++] = MUISC_INFO_TYPE; //type
|
||
BLE_SendFrameById(MUSIC_SERVER, DEVICE_REQ_MUSIC_STATUS_ID, buf_temp, write_idx);
|
||
/*
|
||
MusicPlayInfo_t music_info;
|
||
memset(&music_info, 0, sizeof(music_info));
|
||
|
||
ADPTGetMusicStatus(&music_info);
|
||
|
||
if((music_info.playState != MUSIC_STATE_PAUSE)&&(music_info.playState != MUSIC_STATE_PLAYING))
|
||
{
|
||
music_info.playState = MUSIC_STATE_PAUSE;
|
||
music_info.maxVol = 15;
|
||
}
|
||
ADPTNotifyMusic(&music_info);
|
||
*/
|
||
}
|
||
|
||
|
||
void BLE_DeviceRequestPlayStatusACK(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
protocol_data_info_t data_info = {0};
|
||
// uint8_t MusicRequestPlayStatusACK[2];
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
|
||
data_info.error = FENDA_SUCCESS;
|
||
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case MUSIC_CONTROL_TYPE:
|
||
// MusicRequestPlayStatusACK[0] = *data_info.p_data++;
|
||
// MusicRequestPlayStatusACK[1] = *data_info.p_data;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
in_data = data_info.p_data;
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
} while(in_len);
|
||
|
||
*out_len = 0;
|
||
}
|
||
|
||
|
||
|
||
|
||
/***************************************************************************************************
|
||
|
||
(APP发送音乐信息)音乐播放状态通知--4.14.3
|
||
|
||
***************************************************************************************************/
|
||
|
||
void AppMusicPlayStatusNotify(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
uint8_t MusicPlayValue;
|
||
MusicPlayState MusicPlayStatus;
|
||
protocol_data_info_t data_info = {0};
|
||
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
memset(&musicPlayInfo.musicName, 0, sizeof(musicPlayInfo.musicName));
|
||
memset(&musicPlayInfo.artistName, 0, sizeof(musicPlayInfo.artistName));
|
||
memset(&musicPlayInfo.playState, 0, sizeof(musicPlayInfo.playState));
|
||
|
||
data_info.error = FENDA_SUCCESS;
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case SINGER_NAME_TYPE:
|
||
if(data_info.len < 128) {
|
||
memcpy(musicPlayInfo.artistName, data_info.p_data, data_info.len);
|
||
} else {
|
||
memcpy(musicPlayInfo.artistName, data_info.p_data, 127);
|
||
}
|
||
|
||
ble_printf("music artistName DataLen = %d.\r\n", data_info.len);
|
||
break;
|
||
|
||
case SONG_NAME_TYPE:
|
||
if(data_info.len < 128) {
|
||
memcpy(musicPlayInfo.musicName, data_info.p_data, data_info.len);
|
||
} else {
|
||
memcpy(musicPlayInfo.musicName, data_info.p_data, 127);
|
||
}
|
||
ble_printf("music musicName DataLen = %d.\r\n", data_info.len);
|
||
break;
|
||
|
||
case PLAY_STATE_TYPE:
|
||
MusicPlayValue = *data_info.p_data;
|
||
if((MusicPlayValue > 0) && (MusicPlayValue < 4)) {
|
||
if(MusicPlayValue == 1) {
|
||
MusicPlayStatus = MUSIC_STATE_EMPTY;
|
||
} else if(MusicPlayValue == 2) {
|
||
MusicPlayStatus = MUSIC_STATE_PAUSE;
|
||
} else if(MusicPlayValue == 3) {
|
||
MusicPlayStatus = MUSIC_STATE_PLAYING;
|
||
} else {
|
||
MusicPlayStatus = MUSIC_STATE_UNCONN;
|
||
}
|
||
} else {
|
||
MusicPlayStatus = MUSIC_STATE_UNCONN;
|
||
}
|
||
|
||
ble_printf("music MusicPlayStatus = %d.\r\n", MusicPlayStatus);
|
||
musicPlayInfo.playState = MusicPlayStatus;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
|
||
}
|
||
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
} while(in_len);
|
||
|
||
if(musicPlayInfo.playState == MUSIC_STATE_DISABLE) {
|
||
return;
|
||
}
|
||
// ------TO BE ADDED 待添加音乐信息更新通知UI -------
|
||
task_ui_notify(EVENT_INFO_CACHE_LOADING,0,&musicPlayInfo,sizeof(musicPlayInfo));
|
||
|
||
write_idx = user_set_protocol_error(out_data, MUSIC_SERVER, data_info.error);
|
||
*out_len = write_idx;
|
||
}
|
||
|
||
/***************************************************************************************************
|
||
|
||
音乐操作--控制音乐指令上报(设备-> APP)- 4.14.4
|
||
|
||
***************************************************************************************************/
|
||
|
||
void BLE_DeviceMusicContrlCmdUpload(uint8_t music_cmd)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
uint8_t buf_temp[TEMP_NUM_MAX] = {0};
|
||
uint16_t button;
|
||
// if(musicPlayInfo.playState == MUSIC_STATE_DISABLE)
|
||
// return;
|
||
|
||
ble_printf("Device Contrl Music Cmd Upload.\r\n");
|
||
buf_temp[write_idx++] = 0x03; // LTV Len
|
||
buf_temp[write_idx++] = 0x01; //type
|
||
buf_temp[write_idx++] = music_cmd;
|
||
/* Send play command */
|
||
switch(music_cmd)
|
||
{
|
||
case DVICE_MUSIC_PLAY:
|
||
ble_printf("Send music playcommand.\r\n");
|
||
musicPlayInfo.playState = MUSIC_STATE_PLAYING;
|
||
button = REMOTE_PLAY;
|
||
break;
|
||
|
||
case DEVICE_MUSIC_POUSE:
|
||
ble_printf("Send music pouse command.\r\n");
|
||
musicPlayInfo.playState = MUSIC_STATE_PAUSE;
|
||
button = REMOTE_PAUSE;
|
||
break;
|
||
|
||
case DEVICE_MUSIC_PRE:
|
||
ble_printf("Send music scan previous command.\r\n");
|
||
musicPlayInfo.playState = MUSIC_STATE_PLAYING;
|
||
button = REMOTE_PREVIOUS;
|
||
break;
|
||
|
||
case DEVICE_MUSIC_NEXT:
|
||
ble_printf("Send music scan next command.\r\n");
|
||
musicPlayInfo.playState = MUSIC_STATE_PLAYING;
|
||
button = REMOTE_NEXT;
|
||
break;
|
||
|
||
case DEVICE_MUSIC_DECREASE:
|
||
ble_printf("Send music volume down command.\r\n");
|
||
if(musicPlayInfo.volume > 0){
|
||
musicPlayInfo.volume--;
|
||
}
|
||
else{
|
||
musicPlayInfo.volume = 0;
|
||
}
|
||
button = REMOTE_VOLUME_DOWN;
|
||
break;
|
||
|
||
case DEVICE_MUSIC_INCREASE:
|
||
ble_printf("Send music volume up command.\r\n");
|
||
if(musicPlayInfo.volume > 0){
|
||
if (GetBandPhoneOS() == IOS_PHONE)
|
||
{
|
||
musicPlayInfo.volume--;
|
||
}
|
||
else
|
||
{
|
||
musicPlayInfo.volume++;
|
||
}
|
||
}
|
||
else{
|
||
musicPlayInfo.volume = 0;
|
||
}
|
||
button = REMOTE_VOLUME_UP;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
#ifdef __HID_SERVICE__
|
||
if(GetBandPhoneOS() == IOS_PHONE)
|
||
{
|
||
ble_printf(" &&&&&&& button = %d &&&&&&&&\r\n", button);
|
||
if(button != 0)
|
||
{
|
||
hidAppRemoteReportEvent(button);
|
||
button = 0;
|
||
hidAppRemoteReportEvent(button);
|
||
}
|
||
}
|
||
#endif //#ifdef __HID_SERVICE__
|
||
|
||
// ------TO BE ADDED 待添加音乐播放状态变化通知UI ----
|
||
//task_ui_notify(EVENT_INFO_CACHE_LOADING,0,&musicPlayInfo,sizeof(musicPlayInfo));
|
||
BLE_SendFrameById(MUSIC_SERVER, CONTROL_MUSIC_COMMAND_UPLOAD_ID, buf_temp, write_idx);
|
||
}
|
||
|
||
void AppDeviceMusicContrlACK(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
protocol_data_info_t data_info = {0};
|
||
// uint8_t MusicControlACK = 0;
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
|
||
data_info.error = FENDA_SUCCESS;
|
||
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case MUSIC_CONTROL_TYPE:
|
||
// MusicControlACK = *data_info.p_data;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
in_data = data_info.p_data;
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
} while(in_len);
|
||
|
||
*out_len = 0;
|
||
}
|
||
|
||
/***************************************************************************************************
|
||
|
||
音乐操作--APP 同步音乐控制指令到设备(APP->设备<E8AEBE>?-4.14.5
|
||
|
||
***************************************************************************************************/
|
||
|
||
void BLE_DeviceMusicContrlCmd(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
protocol_data_info_t data_info = {0};
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
|
||
data_info.error = FENDA_SUCCESS;
|
||
|
||
if(musicPlayInfo.playState == MUSIC_STATE_DISABLE)
|
||
return ;
|
||
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case MUSIC_CONTROL_TYPE:
|
||
ble_printf("Revice DeviceMusicContrlCmd = 0x%x.\r\n", *data_info.p_data);
|
||
if(*data_info.p_data == 1) {
|
||
musicPlayInfo.playState = MUSIC_STATE_PLAYING;
|
||
} else if(*data_info.p_data == 2) {
|
||
musicPlayInfo.playState = MUSIC_STATE_PAUSE;
|
||
}
|
||
// ------TO BE ADDED 待添加音乐控制开关log打点 -------
|
||
// ------TO BE ADDED 待添加音乐播放状态变化通知UI ----
|
||
task_ui_notify(EVENT_INFO_CACHE_LOADING,0,&musicPlayInfo,sizeof(musicPlayInfo));
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
} while(in_len);
|
||
|
||
write_idx = user_set_protocol_error(out_data, MUSIC_SERVER, data_info.error);
|
||
*out_len = write_idx;
|
||
}
|
||
|
||
/***************************************************************************************************
|
||
|
||
APP 下发手机音量信息--4.14.6
|
||
|
||
***************************************************************************************************/
|
||
|
||
void BLE_phoneVolumeStatusNotify(uint8_t *in_data, uint16_t in_len, uint8_t *out_data, uint16_t *out_len)
|
||
{
|
||
uint16_t write_idx = 0;
|
||
protocol_data_info_t data_info = {0};
|
||
memset(&data_info, 0, sizeof(protocol_data_info_t));
|
||
|
||
if(musicPlayInfo.playState == MUSIC_STATE_DISABLE)
|
||
return;
|
||
|
||
data_info.error = PARAM_ERROR;
|
||
do {
|
||
user_get_data_info(&data_info, &in_data, &in_len);
|
||
switch(data_info.type & 0x7f) { //无子节点
|
||
case MAX_VOLUME_TYPE:
|
||
musicPlayInfo.maxVol = *data_info.p_data;
|
||
ble_printf("music maxVol = 0x%x.\r\n", musicPlayInfo.maxVol);
|
||
break;
|
||
|
||
case CURRENT_VOLUME_TYPE:
|
||
musicPlayInfo.volume = *data_info.p_data;
|
||
ble_printf("music volume = 0x%x.\r\n", musicPlayInfo.volume);
|
||
data_info.error = FENDA_SUCCESS;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
in_data = in_data + (data_info.len);
|
||
in_len = in_len - (2 + data_info.len);
|
||
if(in_len > FRAM_MAX_SIZ) {
|
||
in_len = 0;
|
||
}
|
||
} while(in_len);
|
||
|
||
// ------TO BE ADDED 待添加音乐播放状态变化通知UI ----
|
||
task_ui_notify(EVENT_INFO_CACHE_LOADING,0,&musicPlayInfo,sizeof(musicPlayInfo));
|
||
write_idx = user_set_protocol_error(out_data, MUSIC_SERVER, data_info.error);
|
||
*out_len = write_idx;
|
||
}
|
||
|
||
|
||
|
||
// 所有涉及到音乐的函数指针,这里的顺序和common id 顺序相同
|
||
const p_func_t MusicServerFunc[MUSIC_SERVERE_MAX_ID] = {
|
||
user_null_func,
|
||
BLE_DeviceMusicContrlSwitch, //4.14.1
|
||
BLE_DeviceRequestPlayStatusACK, //4.14.2 上报APP响应处理
|
||
AppMusicPlayStatusNotify, //4.14.3
|
||
AppDeviceMusicContrlACK, //4.14.4 主动上报
|
||
BLE_DeviceMusicContrlCmd, //4.14.5
|
||
BLE_phoneVolumeStatusNotify, //4.14.6
|
||
};
|