/******************************************************************************
版权所有: 深圳市**科技有限公司
文件名: bsp_bt.c
作者: wangdy
创建日期: 2021/06/2
描述: led初始化
其它:
修改历史: //修改历史记录列表,每条修改记录应包含修改日期、修改者及修改内容简述
序号 修改时间 修改人 修改内容
???? ????/??/?? ?????? 参考样式
******************************************************************************/
/************************************头文件************************************/
#include "bsp_bt.h"
#include <string.h>
#include "yc_gpio.h"
#include "yc_bt.h"
#include "tools_function.h"
#include "api_para_set.h"
#include "var_global.h"
#include "api_para_set.h"
#include "tools_fifo.h"
#include "ymodem.h"
#include "printerConfig.h"
#include "api_data_analysis.h"
/*************************************变量*************************************/
//在yc_bt里面
extern BT_BufTypeDef BluetoothBuf;
uint8_t bt2m0buf_full_flag = 0;
uint8_t ble_connect_flag = 0; //ble 建立连接标志 为0 未连接 为1 已连接 为2 已设置为连接参数
// byte0: uuid length
//byte1-16: uuid
// 49535343-FE78-4AE5-8FA9-9FAFD205E455
const uint8_t ble_service_uuid_lsps[] = {0x10,0x55,0xe4,0x05,0xd2,0xaf,0x9f,0xa9,0x8f,0xe5,0x4a,0x7d,0xfe,0x43,0x53,0x53,0x49};
/*
byte0: characterisitic
byte1:characterisitic uuid length
byte2-17:characterisitic uuid
byte18:write/read payload length
byte19-20:write/read payload
*/
//49535343-1E4D-4BD9-BA61-23C647249616
const uint8_t ble_Characteristic_uuid_lsps_tx[] = {0x10,0x10,0x16,0x96,0x24,0x47,0xc6,0x23,0x61,0xba,0xd9,0x4b,0x4d,0x1e,0x43,0x53,0x53,0x49,0x01,0x00};
//49535343-8841-43F4-A8D4-ECBE34729BB3
const uint8_t ble_Characteristic_uuid_lsps_rx[] = {0x0c,0x10,0xb3,0x9b,0x72,0x34,0xbe,0xec,0xd4,0xa8,0xf4,0x43,0x41,0x88,0x43,0x53,0x53,0x49,0x01,0x00};
//49535343-aca3-481c-91ec-d85e28a60318
const uint8_t ble_Characteristic_uuid_flow_ctrl[] = {0x18,0x10,0x18,0x03,0xa6,0x28,0x5e,0xd8,0xec,0x91,0x1c,0x48,0xa3,0xac,0x43,0x53,0x53,0x49,0x01,0x00};
uint8_t bt_cod[3] = {0x24,0x04,0x04};
BtRxTxVarTypeDef BtRxTxVar = {0};
/*************************************函数*************************************/
/*******************************************************************************
* 名称: Get_BtAddr
* 描述: 获取蓝牙地址
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
static void Get_BtAddr(uint8_t *addr)
{
uint8_t chip_id[6];
//读取芯片ID 用于生成序列号
read_chipid(chip_id);
//芯片ID处理
addr[0] = chip_id[0];
addr[1] = chip_id[1];
addr[2] = chip_id[2];
addr[3] = chip_id[3];
addr[4] = chip_id[4];
addr[5] = chip_id[5];
}
/*******************************************************************************
* 名称: BT_CharacterRegist
* 描述: 获取蓝牙地址
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
static void BT_CharacterRegist(void)
{
//增加服务
BtRxTxVar.BleServiceHandle = BT_AddBleService((uint8_t *)ble_service_uuid_lsps,sizeof(ble_service_uuid_lsps));
if(BtRxTxVar.BleServiceHandle!= 0) //增加服务 返回handle无需保存
{
MyPrintf("add service sucess,handle=%04x\n",BtRxTxVar.BleServiceHandle);
}
else
{
MyPrintf("add service failed,return=%04x\n",BtRxTxVar.BleServiceHandle);
}
//ble发送UUID
BtRxTxVar.BleSendHandle = BT_AddBleCharacteristic((uint8_t *)ble_Characteristic_uuid_lsps_tx,sizeof(ble_Characteristic_uuid_lsps_tx));
if(BtRxTxVar.BleSendHandle != 0) //增加服务特征 write返回的handle需要保存,发数据使用
{
MyPrintf("add Characteristic tx sucess,handle=%04x\n",BtRxTxVar.BleSendHandle);
}
else
{
MyPrintf("add Characteristic tx failed,return=%04x\n",BtRxTxVar.BleSendHandle);
}
//ble接收UUID
BtRxTxVar.BleRecvHandle = BT_AddBleCharacteristic((uint8_t *)ble_Characteristic_uuid_lsps_rx,sizeof(ble_Characteristic_uuid_lsps_rx));
if( BtRxTxVar.BleRecvHandle!= 0)
{
MyPrintf("add Characteristic rx sucess;handle=%04x\n",BtRxTxVar.BleRecvHandle);
}
else
{
MyPrintf("add Characteristic rx failed,return=%04x\n",BtRxTxVar.BleRecvHandle);
}
//ble流控UUID
#if 0
BtRxTxVar.BleFlowctlHandle = BT_AddBleCharacteristic((uint8_t *)ble_Characteristic_uuid_flow_ctrl,sizeof(ble_Characteristic_uuid_flow_ctrl));
if(BtRxTxVar.BleFlowctlHandle!= 0)
{
MyPrintf("add Characteristic flow_ctrl sucess;handle=%04x\n",BtRxTxVar.BleFlowctlHandle);
}
else
{
MyPrintf("add Characteristic flow_ctrl failed,return=%04x\n",BtRxTxVar.BleFlowctlHandle);
}
#endif
}
/*******************************************************************************
* 名称: BT_User_Init
* 描述: 蓝牙初始化
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
void BT_User_Init(void)
{
uint8_t bt_name[32] = {0};
int i = 0;
//读蓝牙部分参数信息
BtParaReadFromFlash();
//蓝牙地址生成
Get_BtAddr(BtRxTxVar.BtMacAddr);
//机器名称及蓝牙名称
memcpy(bt_name,Store_Para.BT_Name_Para.Bt_Name,strlen((char *)Store_Para.BT_Name_Para.Bt_Name));
//蓝牙接口初始化
BT_Init();
//蓝牙中断初始化
enable_intr(INTR_BT);
//设置蓝牙名称 - ble
if(BT_SetBleName(bt_name,strlen((char *)bt_name))==TRUE)
{
MyPrintf("SetBleName_suc ble name:%s\n",bt_name);
}
else
{
MyPrintf("SetBleName_failed\n");
}
//bt与ble名字地址可以设置成一样 蓝牙名称 经典蓝牙
if(BT_SetBtName(bt_name,strlen((char *)bt_name)) == TRUE)
{
MyPrintf("SetbtName_suc\n");
}
else
{
MyPrintf("SetbtName_fail\n");
}
//bt与ble名字地址可以设置成一样 蓝牙名称 经典蓝牙
if(BT_SetBleAddr(BtRxTxVar.BtMacAddr) == TRUE)
{
MyPrintf("SetBleAddr_suc\n");
}
else
{
MyPrintf("SetBleAddr_fail\n");
}
//设置蓝牙地址
if(BT_SetBtAddr(BtRxTxVar.BtMacAddr) == TRUE)
{
MyPrintf("SetBtAddr_suc\n");
}
else
{
MyPrintf("SetBtAddr_fail\n");
}
//设置配对模式为confirmkey
if(BT_SetParingMode(0x03) == TRUE)
{
MyPrintf("set confirmkey mode success\n");
}
else
MyPrintf("set confirmkey mode failed\n");
//设置COD
if(BT_SetCOD(bt_cod) == TRUE)
{
MyPrintf("set COD sucess\n");
}
else
{
MyPrintf("set COD failed\n");
}
//删除用户自定义服务
if(BT_DeleteService() == TRUE)
{
MyPrintf("delete service sucess\n");
}
else
{
MyPrintf("delete service failed\n");
}
//注册UUID
BT_CharacterRegist();
if(BT_SetVisibility(0x01,0x01,0x01) == TRUE) //设置可发现
{
MyPrintf("SetVisibility sucess\n");
}
else
{
MyPrintf("SetVisibility failed\n");
}
MyPrintf("bt version=%x\n",BT_GetVersion());
//nvram
if(Bt_Para.Bt_NvRam[34*0] > 0x05||Bt_Para.Bt_NvRam[34*1] > 0x05||Bt_Para.Bt_NvRam[34*2] > 0x05||Bt_Para.Bt_NvRam[34*3] > 0x05||Bt_Para.Bt_NvRam[34*4] > 0x05)
{
memset(Bt_Para.Bt_NvRam,0x00,NVRAM_LEN);
}
i=0;
if(BT_SetNVRAM(Bt_Para.Bt_NvRam,NVRAM_LEN) == TRUE)
{
MyPrintf("set nvram success:\n");
while(i<NVRAM_LEN)
{
MyPrintf("0x%02X ",Bt_Para.Bt_NvRam[i++]);
}
MyPrintf("\n");
}
else
{
MyPrintf("set nvram failed\n");
}
}
/*******************************************************************************
* 名称: ReserveNV
* 描述:
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
static uint8_t ReserveNV(uint8_t* nvram)
{
MyPrintf("new nvram data,updata to flash\r\n");
BtParaReadFromFlash();
memcpy(Bt_Para.Bt_NvRam,nvram,NVRAM_LEN);
BtParaWritetoFlash();
return 0;
}
/*******************************************************************************
* 名称: BT_Progress
* 描述: 蓝牙通信流程
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
void BT_Progress(void)
{
uint16_t i = 0;
static bool BefourFilled = false; //之前满掉了 则再下次进来则读7*127
int eventCmd= BT_GetEventOpcode();
switch(eventCmd)
{
case BT_DATA: //经典蓝牙数据
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen > 0)
{
BtRxTxVar.BtDataSource = BT_SPP; //SPP经典蓝牙
//升级 直接走Ymodem协议
if(MACHINE_UPDATE == Machine_State)
{
for(int i = 0; i< BtRxTxVar.BtRxLen; i++)
{
Ymodem_Recv_Byte(BtRxTxVar.BtRxData[i]);
}
}
else
{
for(int i = 0; i< BtRxTxVar.BtRxLen; i++)
{
FIFO_DataAnalysis_AddByte(FROM_BT,BtRxTxVar.BtRxData[i]);
}
}
#if 0
if(BefourFilled)
{
if(GetPrintBytesRemain() > 1016) //大于8*127
{
BefourFilled = false;
BT_SetSppFlowcontrol(7);
}
}
else
{
if(1016 < GetPrintBytesRemain()) //大于8*127
{
BT_SetSppFlowcontrol(1);
}
else
{
BefourFilled = true;
}
}
#else
BT_SetSppFlowcontrol(1);
#endif
#if BT_DEBUG
//MyPrintf("received %d bytes bt data:\n",BtRxTxVar.BtRxLen);
MyPrintf("R:");
for(int i=0; i < BtRxTxVar.BtRxLen; i++) MyPrintf("%02x ",BtRxTxVar.BtRxData[i]);
MyPrintf("\n");
#endif
}
break;
}
case BLE_DATA: //BLE蓝牙数据
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen > 2)
{
BtRxTxVar.BtDataSource = BT_BLE; //BLE低功耗蓝牙
//过滤非法数据
if(BtRxTxVar.BtRxLen == 4)
{
if(//(BtRxTxVar.BtRxData[0] == 0x13)&&(BtRxTxVar.BtRxData[1] == 0x00)&&
(BtRxTxVar.BtRxData[2] == 0x01)&&(BtRxTxVar.BtRxData[3] == 0x00))
{
break;
}
}
//升级 直接走Ymodem协议
if(MACHINE_UPDATE == Machine_State)
{
for(int i = 2; i< BtRxTxVar.BtRxLen; i++)
{
Ymodem_Recv_Byte(BtRxTxVar.BtRxData[i]);
}
}
else
{
//数据传输至解析接口
for(int i = 2; i< BtRxTxVar.BtRxLen; i++)
{
FIFO_DataAnalysis_AddByte(FROM_BT,BtRxTxVar.BtRxData[i]);
}
}
#if BT_DEBUG
MyPrintf("received %d bytes ble data:\n",BtRxTxVar.BtRxLen);
for(int i=2; i < BtRxTxVar.BtRxLen; i++) MyPrintf("%02x ",BtRxTxVar.BtRxData[i]);
MyPrintf("\n");
#endif
}
break;
}
case BT_CONNECTED: //经典蓝牙连接
{
MyPrintf("bt connected\n");
BtRxTxVar.BtBleConnectStateFlag = true;
BtRxTxVar.BtBleConnectType = BT_SPP;
//清除掉现有所有缓存
FIFO_PrintLineList_Clear(); //打印数据清除
FIFO_DataAnalysis_Clear(); //解析数据清除
break;
}
case BLE_CONNECTED: //BLE蓝牙连接
{
MyPrintf("ble connected\n");
BtRxTxVar.BtBleConnectStateFlag = true;
BtRxTxVar.BtBleConnectType = BT_BLE;
//清除掉现有所有缓存
FIFO_PrintLineList_Clear(); //打印数据清除
FIFO_DataAnalysis_Clear(); //解析数据清除
break;
}
case BT_DISCONNECTED: //经典蓝牙断连
{
MyPrintf("bt disconnected\n");
BtRxTxVar.BtBleConnectStateFlag = false;
BtRxTxVar.BtBleConnectType = BT_NONE;
//清除掉现有所有缓存
FIFO_PrintLineList_Clear(); //打印数据清除
FIFO_DataAnalysis_Clear(); //解析数据清除
break;
}
case BLE_DISCONNECTED: //BLE蓝牙断连
{
MyPrintf("ble disconnected\n");
BtRxTxVar.BtBleConnectStateFlag = false;
BtRxTxVar.BtBleConnectType = BT_NONE;
//清除掉现有所有缓存
FIFO_PrintLineList_Clear(); //打印数据清除
FIFO_DataAnalysis_Clear(); //解析数据清除
break;
}
case NVRAM_DATA: //更新配对信息
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen>0)
{
MyPrintf("receive %dbytes Nvramdata:",BtRxTxVar.BtRxLen);
for(int i=0;i<BtRxTxVar.BtRxLen;i++)
{
MyPrintf("%02X ",BtRxTxVar.BtRxData[i]);
}
MyPrintf("\n");
ReserveNV(BtRxTxVar.BtRxData);//将最新的5个设备信息(NVRAM) 写入flash ,
}
else
{
MyPrintf("read Nvram data error\r\n");
}
break;
}
case CONFIRM_GKEY: //密码
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen > 0)
{
#if BT_DEBUG
MyPrintf("receive %dbytes CONFIRM_GKEY:",BtRxTxVar.BtRxLen);
for(int i=0;i<BtRxTxVar.BtRxLen;i++)
{
MyPrintf("%02X ",BtRxTxVar.BtRxData[i]);
}
MyPrintf("\n");
#endif
}
//BT_ConfirmGkey参数为0则继续配对,1则取消配对。
if(BT_ConfirmGkey(0) == TRUE)
{
MyPrintf("set confirmkey success");
}
else
{
MyPrintf("set confirmkey failed\n");
}
break;
}
case PASSKEY:
{
// MyPrintf("request Passkey");
break;
}
case PAIRING_COMPLETED: //配对完成
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen > 0)
{
// MyPrintf("receive %dbytes paring data:",BtRxTxVar.BtRxLen);
// for(int i=0; i < BtRxTxVar.BtRxLen; i++)
// {
// MyPrintf("%02X ",BtRxTxVar.BtRxData[i]);
// }
// MyPrintf("\n");
}
else
{
// MyPrintf("read PAIRING_COMPLETED data error\r\n");
}
break;
}
case PAIRING_STATE: //配对状态
{
uint16_t pairing_status = 0;
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen == 2)
{
pairing_status |= BtRxTxVar.BtRxData[0];
pairing_status |= (BtRxTxVar.BtRxData[1]<<8);
switch(pairing_status)
{
case 0x0001:
MyPrintf("BT Pairing Success\r\n");
break;
case 0x0101:
MyPrintf("BT Pairing fail\r\n");
break;
case 0x0080:
MyPrintf("BLE Pairing Success\r\n");
break;
case 0x0180:
MyPrintf("BLE Pairing fail\r\n");
break;
default:
MyPrintf("unknown pairing_status[%04x]\r\n",pairing_status);
break;
}
}
else
{
// MyPrintf("read PAIRING_STATE data error\r\n");
}
break;
}
case -1: //无效码
{
//no event
break;
}
default: //默认
{
BtRxTxVar.BtRxLen = BT_ReadBTData(BtRxTxVar.BtRxData);
if(BtRxTxVar.BtRxLen > 0)
{
#if BT_DEBUG
MyPrintf("datalen:%d\r\n",BtRxTxVar.BtRxLen);
for(int i=0;i<BtRxTxVar.BtRxLen;i++)
{
MyPrintf("%02X ",BtRxTxVar.BtRxData[i]);
}
MyPrintf("\n");
#endif
}
}
break;
}
}
/*******************************************************************************
* 名称: BT_DataSend
* 描述: 蓝牙发送数据
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
void BT_DataSend(uint8_t *data,uint16_t len)
{
#if BT_DEBUG
MyPrintf("T:");
for(int i=0; i < len; i++) MyPrintf("%02x ",data[i]);
MyPrintf("\n");
#endif
if(BtRxTxVar.BtDataSource == BT_SPP)
{
BT_SendSppData(data,len);
//流控
#ifdef SPP_FLOWCONTROL
if(BT_SetSppFlowcontrol(1) != TRUE) MyPrintf("set Flowcontrol failed\n");
#endif
}
else if(BtRxTxVar.BtDataSource == BT_BLE)
{
BtRxTxVar.BtTxData[0]=(BtRxTxVar.BleSendHandle & 0xff);
BtRxTxVar.BtTxData[1]=((BtRxTxVar.BleSendHandle >> 8) & 0xff);
memcpy(&BtRxTxVar.BtTxData[2],data,len);
BT_SendBleData(BtRxTxVar.BtTxData,len+2);
}
}
/*******************************************************************************
* 名称: BT_FlowCtrl
* 描述: 蓝牙流控
* 输入参数: 无
* 输出参数: 无
* 其它: 无
*******************************************************************************/
#if 0
void BT_FlowCtrl(bool state)
{
BtRxTxVar.BtTxData[0]= (BtRxTxVar.BleFlowctlHandle & 0xff);
BtRxTxVar.BtTxData[1]=((BtRxTxVar.BleFlowctlHandle >> 8) & 0xff);
if(state)
{
BtRxTxVar.BtTxData[2] = 0;
}
else
{
BtRxTxVar.BtTxData[2] = 0;
}
BT_SendBleData(BtRxTxVar.BtTxData,3);
}
#endif
以下为.h文件
/******************************************************************************
版权所有: 深圳**科技有限公司
文件名: bsp_bt.h
作者: wangdy
创建日期: 2021/6/3
描述: 蓝牙接口
其它:
修改历史: //修改历史记录列表,每条修改记录应包含修改日期、修改者及修改内容简述
序号 修改时间 修改人 修改内容
???? ????/??/?? ?????? 参考样式
******************************************************************************/
/*********************************防止多次编译*********************************/
#ifndef _BSP_BT_H
#define _BSP_BT_H
/************************************头文件************************************/
#include <stdint.h>
#include <stdbool.h>
#include "yc_bt.h"
/************************************宏定义************************************/
//Nvram 长度170bytes,每组Nvram长度为34bytes,最多可存储5组配对信息
#define NVRAM_LEN 170
//#define SPP_FLOWCONTROL
#define BT_DEBUG 0
/************************************结构体************************************/
//数据来源
typedef enum
{
BT_NONE = 0,
BT_SPP,
BT_BLE,
}BtDataSourceTypeDef;
//蓝牙收发的数据
typedef struct
{
BtDataSourceTypeDef BtDataSource; //蓝牙数据来源
uint8_t BtRxData[MAX_BLUETOOTH_DATA_LEN]; //接收缓存
uint16_t BtRxLen; //接收长度
uint8_t BtTxData[MAX_BLUETOOTH_DATA_LEN]; //接收缓存
uint16_t BtTxLen; //接收长度
uint16_t BleSendHandle; //BLE发送句柄
uint16_t BleFlowctlHandle; //BLE流控句柄
uint16_t BleRecvHandle; //BLE接收句柄
uint16_t BleServiceHandle; //BLE UUID句柄
//蓝牙连接状态
bool BtBleConnectStateFlag; //蓝牙连接状态
BtDataSourceTypeDef BtBleConnectType; //连接类型 是BT还是BLE
//蓝牙地址信息
uint8_t BtMacAddr[6]; //蓝牙MAC地址
}BtRxTxVarTypeDef;
/**********************************可导出变量**********************************/
extern BtRxTxVarTypeDef BtRxTxVar;
/***********************************函数实现***********************************/
void BT_User_Init(void);
void BT_Progress(void); //蓝牙周期性调用流程
void BT_DataSend(uint8_t *data,uint16_t len); //发送数据
void BT_FlowCtrl(bool state); //蓝牙流控
#endif