Files
player/Project/Src/MyApp/ble_demo.c

925 lines
16 KiB
C
Raw Normal View History

2025-06-27 00:32:57 +08:00
#include <stdio.h>
#include "at_host.h"
#include "string.h"
#include "stdlib.h"
#include "rtthread.h"
#include "ble_demo.h"
2025-07-05 19:47:28 +08:00
//<2F><>ͬһ<CDAC><D2BB><EFBFBD>߳<EFBFBD><DFB3>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD>
2025-06-27 00:32:57 +08:00
#if 1
#include "rthw.h"
#define IRQ_DISABLE() rt_enter_critical()
#define IRQ_ENABLE() rt_exit_critical()
#else
#define IRQ_DISABLE() { }
#define IRQ_ENABLE() { }
#endif
2025-07-05 19:47:28 +08:00
/*------------------------buff<66><66><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>---------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>buff
2025-06-27 00:32:57 +08:00
static int buff_clear(ble_buff *buff);
2025-07-05 19:47:28 +08:00
//<2F><>ʼ<EFBFBD><CABC>buff
2025-06-27 00:32:57 +08:00
static void buff_init(ble_buff *buff)
{
buff->buff_len=BLE_BUFF_LEN;
buff_clear(buff);
}
2025-07-05 19:47:28 +08:00
//д<><D0B4>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
2025-06-27 00:32:57 +08:00
static int buff_save_byte(ble_buff *buff,uint8_t data)
{
IRQ_DISABLE();
if (buff->buff_used<buff->buff_len)
{
buff->buff[buff->save_ptr]=data;
buff->buff_used++;
buff->save_ptr++;
if (buff->save_ptr>=buff->buff_len)
buff->save_ptr=0;
IRQ_ENABLE();
return 1;
}
IRQ_ENABLE();
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡһ<C8A1><D2BB><EFBFBD>ֽ<EFBFBD>
2025-06-27 00:32:57 +08:00
static int buff_read_byte(ble_buff *buff,uint8_t *data)
{
IRQ_DISABLE();
if (buff->buff_used)
{
*data=buff->buff[buff->read_ptr];
buff->buff_used--;
buff->read_ptr++;
if (buff->read_ptr>=buff->buff_len)
buff->read_ptr=0;
IRQ_ENABLE();
return 1;
}
IRQ_ENABLE();
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>buff<66><66><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ַ<EFBFBD>
2025-06-27 00:32:57 +08:00
static int buff_find_char(ble_buff *buff,uint8_t data)
{
int ptr=buff->read_ptr;
int len=buff->buff_used;
while(len--)
{
if(data==buff->buff[ptr])
return 1;
ptr++;
if (ptr>=buff->buff_len)
ptr=0;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>buff
2025-06-27 00:32:57 +08:00
static int buff_clear(ble_buff *buff)
{
IRQ_DISABLE();
buff->buff_used=0;
buff->read_ptr=0;
buff->save_ptr=0;
IRQ_ENABLE();
return 0;
}
2025-07-05 19:47:28 +08:00
/*------------------------buff<66><66><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>End---------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
/*------------------------device<63><65><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>---------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int device_init(ble_device *device)
{
device->conn_handle=0;
device->linked=0;
memset(device->mac,0,6);
buff_init(&device->buff_recv);
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><E8B1B8>mac<61><63>ַ
2025-06-27 00:32:57 +08:00
int device_set_mac(ble_device *device,uint8_t mac[6])
{
memcpy(device->mac,mac,6);
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><E8B1B8>conn_handle
2025-06-27 00:32:57 +08:00
int device_set_handle(ble_device *device,uint16_t handle)
{
device->conn_handle=handle;
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int device_save_buff(ble_device *device,uint8_t *buff,int size)
{
int ret=0;
while(size--)
{
if (buff_save_byte(&device->buff_recv,buff[ret])==0)
return ret;
ret++;
}
return ret;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD>壬ֱ<E5A3AC><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E7B6A8>
2025-06-27 00:32:57 +08:00
int device_read_buff(ble_device *device,uint8_t *buff,int buff_size,uint8_t end_par)
{
if(buff_find_char(&device->buff_recv,end_par)==0)
return 0;
int ret=0;
uint8_t t=0;
while(1)
{
if(buff_read_byte(&device->buff_recv,&t)==0)
return ret;
if(buff_size)
{
buff[ret]=t;
buff_size--;
ret++;
}
if(t==end_par)
return ret;
}
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD>壬ָ<E5A3AC><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int device_read_buff_by_len(ble_device *device,uint8_t *buff,int read_len)
{
int ret=0;
uint8_t t=0;
while(1)
{
if(buff_read_byte(&device->buff_recv,&t)==0)
return ret;
if(read_len)
{
buff[ret]=t;
read_len--;
ret++;
}
else
return ret;
}
}
2025-07-05 19:47:28 +08:00
/*------------------------device<63><65><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>End---------------------------*/
2025-06-27 00:32:57 +08:00
static void ble_print_data(uint8_t *data,int len)
{
for(int i=0;i<len;i++)
{
printf("%02X ",data[i]);
}
printf("\r\n");
}
2025-07-05 19:47:28 +08:00
/*--------------------------ble<6C><65><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>----------------------------*/
2025-06-27 00:32:57 +08:00
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>֪ͨ<CDA8><D6AA>Ϣ
2025-06-27 00:32:57 +08:00
int ble_add_notice(ble_struct *ble,uint8_t *obj,int obj_size,uint8_t op,uint8_t *par,int par_size);
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_linked_change(ble_struct *ble,uint16_t handle,uint16_t state);
2025-07-05 19:47:28 +08:00
//<2F>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_save_data(ble_struct *ble,uint16_t handle,uint8_t *data,int len);
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_device *ble_find_device(ble_struct *ble,uint16_t handle);
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_add_device(ble_struct *ble,uint16_t handle);
2025-07-05 19:47:28 +08:00
//ɾ<><C9BE><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_sub_device(ble_struct *ble,uint16_t handle);
static int ble_service_on(void *t);
static int ble_service_off(void);
2025-07-05 19:47:28 +08:00
//ble<6C><65>ʼ<EFBFBD><CABC>
2025-06-27 00:32:57 +08:00
void ble_init(ble_struct *ble)
{
if(ble==0) return;
memset(ble,0,sizeof(ble_struct));
at_init();
ble->mode='i';
ble->linked=0;
for(int i=0;i<BLE_MAX_LINKED;i++)
{
device_init(&ble->device[i]);
}
at_clear_buff();
ble->ret_event=rt_event_create("ble_ret", RT_IPC_FLAG_FIFO);
ble->kno_event=rt_event_create("ble_kno", RT_IPC_FLAG_FIFO);
rt_event_send(ble->kno_event,2);
ble_service_on(ble);
ble_reboot(ble);
}
2025-07-05 19:47:28 +08:00
//bleȥ<65><C8A5>ʼ<EFBFBD><CABC>
2025-06-27 00:32:57 +08:00
void ble_deinit(ble_struct *ble)
{
if(ble==0) return ;
ble_reboot(ble);
ble_service_off();
at_deinit();
if(ble->ret_event)
rt_event_delete(ble->ret_event);
if(ble->kno_event)
rt_event_delete(ble->kno_event);
}
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD>ûص<C3BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_set_callback_linked_change(ble_struct *ble,void (*linked_change)(uint16_t handle,uint16_t state))
{
ble->linked_change=linked_change;
return 1;
}
int ble_set_callback_recv_data(ble_struct *ble,void (*recv_data)(uint16_t handle,uint8_t *data,int len))
{
ble->recv_data=recv_data;
return 1;
}
int ble_set_callback_user_irq(ble_struct *ble,void (*user_irq)(uint8_t *data,int len))
{
ble->user_irq=user_irq;
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>֪ͨ<CDA8><D6AA>Ϣ
2025-06-27 00:32:57 +08:00
int ble_deal_notice(ble_struct *ble)
{
if(ble==0) return 0;
int len=0;
uint8_t *data=ble->return_line;
len=at_get_return_line(data);
if(len==-1) {
return 0;
}
uint8_t *par=0;
int par_size=0;
uint8_t *obj=0;
int obj_size=0;
uint8_t op=0;
if(at_decode_line(data,len,&obj,&obj_size,&op,&par,&par_size)==-1)
{
return 0;
}
if(op==CMDAT_CMD_RET)
{
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EEB7B5><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><E6B4A2>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_add_notice(ble,obj,obj_size,op,par,par_size);
}
else if(op==CMDAT_CMD_ACT)
{
2025-07-05 19:47:28 +08:00
//֪ͨ<CDA8><D6AA>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
if(obj[0]==0x01)
{
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2025-06-27 00:32:57 +08:00
ble_linked_change(ble,*(uint16_t *)par,*(uint16_t *)(par+2));
}
else if(obj[0]==0x02)
{
2025-07-05 19:47:28 +08:00
//<2F>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
if(ble->recv_data==0)
ble_save_data(ble,*(uint16_t *)par,par+2,par_size-2);
else
ble->recv_data(*(uint16_t *)par,par+2,par_size-2);
}
else if(obj[0]==0xff)
{
2025-07-05 19:47:28 +08:00
// <20>û<EFBFBD><C3BB>ж<EFBFBD>
2025-06-27 00:32:57 +08:00
if(ble->user_irq) ble->user_irq(par,par_size);
}
}
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>֪ͨ<CDA8><D6AA>Ϣ
2025-06-27 00:32:57 +08:00
int ble_add_notice(ble_struct *ble,uint8_t *obj,int obj_size,uint8_t op,uint8_t *par,int par_size)
{
notice_t *n=&ble->notice;
if(ble->kno_event)
{
2025-07-05 19:47:28 +08:00
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>շ<EFBFBD><D5B7>鿴֪ͨ
2025-06-27 00:32:57 +08:00
rt_uint32_t ev;
rt_event_recv(ble->kno_event,0xffffffff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,1000,&ev);
}
//ble_print_data(par,par_size);
IRQ_DISABLE();
memcpy(n->obj,obj,obj_size);
memcpy(n->par,par,par_size);
n->obj_size=obj_size;
n->par_size=par_size;
IRQ_ENABLE();
if(ble->ret_event)
{
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD><EFBFBD>֪ͨ<CDA8><D6AA><EFBFBD>ѽ<EFBFBD><D1BD>շ<EFBFBD><D5B7>
2025-06-27 00:32:57 +08:00
rt_event_send(ble->ret_event,1);
}
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ֪ͨ<CDA8><D6AA>Ϣ,<2C><><EFBFBD><EFBFBD><30><CAA7>
2025-06-27 00:32:57 +08:00
int ble_get_notice(ble_struct *ble,uint8_t *obj,int *obj_size,uint8_t *op,uint8_t *par,int *par_size)
{
rt_uint32_t ev;
if(ble->ret_event)
{
if(rt_event_recv(ble->ret_event,0xffffffff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,1000,&ev)==RT_EOK)
{
IRQ_DISABLE();
notice_t *n=&ble->notice;
if(obj) memcpy(obj,n->obj,n->obj_size);
if(obj_size) *obj_size=n->obj_size;
if(par) memcpy(par,n->par,n->par_size);
if(par_size) *par_size=n->par_size;
n->obj_size=0;
n->par_size=0;
IRQ_ENABLE();
if(ble->kno_event)
{
2025-07-05 19:47:28 +08:00
// ֪ͨ<CDA8>Ѳ鿴
2025-06-27 00:32:57 +08:00
rt_event_send(ble->kno_event,1);
}
return 1;
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_linked_change(ble_struct *ble,uint16_t handle,uint16_t state)
{
if(state==1)
{
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_add_device(ble,handle);
}
else if(state==0)
{
2025-07-05 19:47:28 +08:00
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_sub_device(ble,handle);
}
else
{
return 0;
}
2025-07-05 19:47:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
if(ble->linked_change) ble->linked_change(handle,state);
return 1;
}
2025-07-05 19:47:28 +08:00
//<2F>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_save_data(ble_struct *ble,uint16_t handle,uint8_t *data,int len)
{
ble_device *device=ble_find_device(ble,handle);
if(device)
{
//printf("%s:recved %d\r\n",__func__,len);
return device_save_buff(device,data,len);
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_device *ble_find_device(ble_struct *ble,uint16_t handle)
{
if(handle==0) return 0;
for(int i=0;i<BLE_MAX_LINKED;i++)
{
if(ble->device[i].conn_handle==handle)
return &ble->device[i];
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
ble_device *ble_find_device_by_index(ble_struct *ble,int index)
{
if(ble==0) return 0;
if(index<BLE_MAX_LINKED)
{
if(ble->device[index].conn_handle)
return &ble->device[index];
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_add_device(ble_struct *ble,uint16_t handle)
{
if(handle==0) return 0;
IRQ_DISABLE();
for(int i=0;i<BLE_MAX_LINKED;i++)
{
ble_device *device=&ble->device[i];
if(device->conn_handle==0)
{
memcpy(device->mac,ble->conn_mac,6);
device->conn_handle=handle;
device->linked=1;
ble->linked++;
IRQ_ENABLE();
return 1;
}
}
IRQ_ENABLE();
return 0;
}
2025-07-05 19:47:28 +08:00
//ɾ<><C9BE><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_sub_device(ble_struct *ble,uint16_t handle)
{
if(handle==0) return 0;
IRQ_DISABLE();
for(int i=0;i<BLE_MAX_LINKED;i++)
{
ble_device *device=&ble->device[i];
if(device->conn_handle==handle)
{
device->conn_handle=0;
device->linked=0;
memset(device->mac,0,6);
buff_clear(&device->buff_recv);
ble->linked--;
IRQ_ENABLE();
return 1;
}
}
IRQ_ENABLE();
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>ֽ綨<D6BD><E7B6A8>
2025-06-27 00:32:57 +08:00
int ble_get_recv_data_by_end(ble_struct *ble,uint16_t handle,uint8_t *buff,int buff_size,uint8_t end_par)
{
if(ble==0) return 0;
ble_device *device=ble_find_device(ble,handle);
if (device)
{
return device_read_buff(device,buff,buff_size,end_par);
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_get_recv_data(ble_struct *ble,uint16_t handle,uint8_t *buff,int read_len)
{
if(ble==0) return 0;
ble_device *device=ble_find_device(ble,handle);
if (device)
{
return device_read_buff_by_len(device,buff,read_len);
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EEB7B5>
2025-06-27 00:32:57 +08:00
int ble_get_cmd_return(ble_struct *ble,uint8_t *buff,int *buff_size)
{
int ret=0;
ret=ble_get_notice(ble,0,0,0,buff,buff_size);
return ret;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_set_name(ble_struct *ble,char *name)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x01,name,strlen(name));
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
return 1;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><>ȡ<EFBFBD>豸mac<61><63>ַ
2025-06-27 00:32:57 +08:00
int ble_get_mac(ble_struct *ble,uint8_t mac[6])
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_get_event(0x02,0,0);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(size==6)
{
memcpy(mac,data,6);
return 1;
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_reboot(ble_struct *ble)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
int time_out=100;
do{
at_send_set_event(0x03,0,0);
rt_thread_delay(2);
ble_get_notice(ble,0,0,0,data,&size);
if((size==1)&&(data[0]==1))
{
rt_thread_delay(100);
at_clear_buff();
return 1;
}
}
while(time_out--);
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>uuid
2025-06-27 00:32:57 +08:00
int ble_set_uuid(ble_struct *ble,uint8_t sub,uint8_t type,uint8_t *uuid)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
data[0]=sub;data[1]=type;
memcpy(&data[2],uuid,type/8);
at_send_set_event(0x05,data,2+type/8);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
return 1;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>/<2F>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_set_data_transport(ble_struct *ble,uint8_t power)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x06,&power,1);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
return 1;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_send_data(ble_struct *ble,uint16_t handle,uint8_t *buff,int len)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
int pack_len=0;
while(len)
{
data[0]=handle;
data[1]=handle>>8;
if(len>20) pack_len=20;
else pack_len=len;
memcpy(&data[2],buff,pack_len);
at_send_set_event(0x07,data,pack_len+2);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
{
if (len==0)
return 1;
}
else
{
return 0;
}
}
else
return 0;
len-=pack_len;
buff+=pack_len;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD>ӻ<EFBFBD>ģʽ
2025-06-27 00:32:57 +08:00
int ble_set_slave(ble_struct *ble)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x10,0,0);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
{
ble->mode='s';
return 1;
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ
2025-06-27 00:32:57 +08:00
int ble_set_host(ble_struct *ble)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x20,0,0);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
{
ble->mode='h';
return 1;
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>Ĵӻ<C4B4>
2025-06-27 00:32:57 +08:00
int ble_connect_by_mac(ble_struct *ble,uint8_t mac[6])
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x21,mac,6);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
{
memcpy(ble->conn_mac,mac,6);
return 1;
}
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F>Ͽ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>Ĵӻ<C4B4>
2025-06-27 00:32:57 +08:00
int ble_discon_by_mac(ble_struct *ble,uint8_t mac[6])
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0x22,mac,6);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(data[0]==1)
return 1;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//ɨ<><C9A8>
2025-06-27 00:32:57 +08:00
int ble_scan(ble_struct *ble,ble_mac_name mac[32],int *mac_num)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
*mac_num=0;
at_send_set_event(0x23,0,0);
rt_thread_delay(3500);
while(1)
{
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(size==1)
{
if(data[0]==1)
return 1;
else
return 0;
}
else if(size>=7)
{
memcpy(mac[*mac_num].mac,data,6);
mac[*mac_num].rssi=data[6];
if(size>7)
{
int len=size-7;
memcpy(mac[*mac_num].name,data+7,len);
mac[*mac_num].name[len]=0;
}
else
mac[*mac_num].name[0]=0;
(*mac_num)++;
}
}
else
return 0;
}
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
int ble_user_cmd_set(ble_struct *ble,uint8_t *tx,int tx_len)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
at_send_set_event(0xff,tx,tx_len);
if(ble_get_cmd_return(ble,data,&size)==1)
{
if(size==1)
return 1;
}
return 0;
}
2025-07-05 19:47:28 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ݣ<EFBFBD>rx_len<65><6E>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȣ<EFBFBD>ʧ<EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>0
2025-06-27 00:32:57 +08:00
int ble_user_cmd_get(ble_struct *ble,uint8_t *tx,int tx_len,uint8_t *rx,int *rx_len)
{
if(ble==0) return 0;
uint8_t *data=ble->data_temp;
int size=0;
int recv_len=0;
uint8_t obj;
int obj_size;
at_send_get_event(0xff,tx,tx_len);
while(1)
{
if(ble_get_notice(ble,&obj,&obj_size,0,data,&size)==1)
{
if(obj_size==0)
return data[0];
else
{
if(recv_len==0)
{
if(size>*rx_len) recv_len=*rx_len;
else recv_len=size;
memcpy(rx,data,recv_len);
//ble_print_data(rx,recv_len);
*rx_len=recv_len;
}
}
}else break;
}
return 0;
}
static int g_service_work;
static int g_service_done=1;
2025-07-05 19:47:28 +08:00
// ble <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
2025-06-27 00:32:57 +08:00
static void ble_service_thread(void *t)
{
ble_struct *ble=t;
g_service_done=0;
int ev;
while(g_service_work)
{
ev=at_get_event();
if(ev&CMDAT_EVENT_RECV)
{
ev&=~CMDAT_EVENT_RECV;
while(ble_deal_notice(ble)==1);
}
if(ev&CMDAT_EVENT_USER)
{
ev&=~CMDAT_EVENT_USER;
at_send_packet();
}
if(ev)
{
}
}
g_service_done=1;
}
static int ble_service_on(void *t)
{
rt_err_t result = RT_EOK;
rt_thread_t tid;
2025-07-05 19:47:28 +08:00
// <20><>֤<EFBFBD>߳<EFBFBD>û<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
if(g_service_done)
{
g_service_work=1;
tid = rt_thread_create("ble_service_thread", ble_service_thread, t, 2048, 11, 10);
if (tid != NULL && result == RT_EOK)
rt_thread_startup(tid);
return 0;
}
else
{
return -1;
}
}
static int ble_service_off(void)
{
2025-07-05 19:47:28 +08:00
// <20><>֤<EFBFBD>߳<EFBFBD><DFB3>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-06-27 00:32:57 +08:00
if(g_service_work)
{
g_service_work=0;
at_send_user_event(0x04);
while(g_service_done==0)
{
rt_thread_delay(10);
}
return 0;
}
else
{
return -1;
}
}