Skip to content

Commit

Permalink
feat(blk): blinky sub demo now works
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Dec 3, 2023
1 parent e3f78de commit b39ac19
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 97 deletions.
127 changes: 102 additions & 25 deletions examples/ble-blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ static mut SCAN_RSP_DATA: &[u8] = &[
// complete name
0x12, // length of this data
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
b'S',
b'i',
b'm',
b'p',
b'B',
b'l',
b'e',
b'i',
b'n',
b'k',
b'y',
b' ',
b'P',
b'e',
Expand Down Expand Up @@ -105,21 +105,21 @@ static mut SYSTEM_ID: [u8; 8] = [0u8; 8];
static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
// Device Information Service
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::primaryServiceUUID.as_ptr() },
},
permissions: GATT_PERMIT_READ,
handle: 0,
// The first must be a Service attribute
value: &gattAttrType_t {
value: &GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::DEVINFO_SERV_UUID as *const _ as _,
} as *const _ as _,
},
// System ID Declaration
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -129,7 +129,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// System ID Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::SYSTEM_ID_UUID as *const _ as _,
},
Expand All @@ -139,7 +139,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number String Declaration
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -149,7 +149,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::SERIAL_NUMBER_UUID as *const _ as _,
},
Expand All @@ -159,7 +159,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Temperature
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -169,7 +169,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::TEMP_UUID as *const _ as _,
},
Expand Down Expand Up @@ -293,30 +293,29 @@ static mut BLINKY_CLIENT_CHARCFG: [gattCharCfg_t; 4] = unsafe { core::mem::zeroe
static mut BLINKY_ATTR_TABLE: [GattAttribute; 6] = [
// Blinky Service
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::primaryServiceUUID.as_ptr() },
},
permissions: GATT_PERMIT_READ,
handle: 0,
value: &gattAttrType_t {
value: &GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &BLINKY_SERV_UUID as *const _ as _,
} as *const _ as _,
},

// Blinky Data Declaration and Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
permissions: GATT_PERMIT_READ,
handle: 0,
value: &GATT_PROP_NOTIFY as *const _ as _,
value: &(GATT_PROP_NOTIFY | GATT_PROP_READ) as *const _ as _,
},
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &BLINKY_DATA_UUID as *const _ as _,
},
Expand All @@ -326,18 +325,17 @@ static mut BLINKY_ATTR_TABLE: [GattAttribute; 6] = [
},
// Blinky client config
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::clientCharCfgUUID.as_ptr() },
},
permissions: GATT_PERMIT_READ | GATT_PERMIT_WRITE,
handle: 0,
value: unsafe { BLINKY_CLIENT_CHARCFG.as_ptr() as _ },
},

// Command
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -346,7 +344,7 @@ static mut BLINKY_ATTR_TABLE: [GattAttribute; 6] = [
value: &GATT_PROP_WRITE as *const _ as _,
},
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &BLINKY_CMD_UUID as *const _ as _,
},
Expand Down Expand Up @@ -389,11 +387,11 @@ unsafe fn blinky_init() {
return 0;
}
unsafe extern "C" fn blinky_on_write_attr(
_conn_handle: u16,
conn_handle: u16,
attr: *mut GattAttribute,
value: *mut u8,
len: u16,
_offset: u16,
offset: u16,
method: u8,
) -> u8 {
let uuid = *((*attr).type_.uuid as *const u16);
Expand All @@ -407,6 +405,32 @@ unsafe fn blinky_init() {
} else if cmd == 0x00 {
BLINKY_ON.store(false, Ordering::Relaxed);
}
} else if uuid == BLINKY_CONF_UUID {
// sub to notrification
// let status = GATTServApp::process_ccc_write_req(conn_handle, attr, value, len, offset, GATT_CLIENT_CFG_NOTIFY);
// if status.is_ok() {
// println!("! on_write_attr sub");
// let val = slice::from_raw_parts(value, len as usize);
// println!("! on_write_attr sub value {:?}", val);
// }
//APP_CHANNEL.try_send(AppEvent::BlinkySubscribed(conn_handle));
//println!("! on_write_attr sub");
} else if uuid == gatt_uuid::GATT_CLIENT_CHAR_CFG_UUID {
// client char cfg
let status =
GATTServApp::process_ccc_write_req(conn_handle, attr, value, len, offset, GATT_CLIENT_CFG_NOTIFY);
if status.is_ok() {
println!("! on_write_attr sub");
let val = slice::from_raw_parts(value, len as usize);
println!("! on_write_attr sub value {:?}", val);
if val == &[0x01, 0x00] {
APP_CHANNEL.try_send(AppEvent::BlinkySubscribed(conn_handle));
} else {
APP_CHANNEL.try_send(AppEvent::BlinkyUnsubscribed(conn_handle));
}
}
} else {
return ATT_ERR_ATTR_NOT_FOUND;
}

return 0;
Expand Down Expand Up @@ -434,6 +458,8 @@ unsafe fn blinky_init() {
pub enum AppEvent {
Connected(u16),
Disconnected(u16),
BlinkySubscribed(u16),
BlinkyUnsubscribed(u16),
}

static APP_CHANNEL: Channel<CriticalSectionRawMutex, AppEvent, 3> = Channel::new();
Expand Down Expand Up @@ -564,7 +590,57 @@ async fn peripheral(spawner: Spawner, task_id: u8, mut subscriber: ble::EventSub
AppEvent::Disconnected(conn_handle) => unsafe {
GATTServApp::init_char_cfg(conn_handle, BLINKY_CLIENT_CHARCFG.as_mut_ptr());
},
AppEvent::BlinkySubscribed(conn_handle) => unsafe {
spawner.spawn(blinky_notification(conn_handle)).unwrap();
},
_ => {
// other event. just broadcast
}
},
}
}
}

#[embassy_executor::task]
async fn blinky_notification(conn_handle: u16) {
let mut ticker = Ticker::every(Duration::from_millis(1000));

static mut NOTIFY_MSG: gattMsg_t = gattMsg_t {
handleValueNoti: attHandleValueNoti_t {
handle: 0,
len: 2,
pValue: ptr::null_mut(),
},
};
loop {
match select(ticker.next(), APP_CHANNEL.receive()).await {
Either::First(_) => unsafe {
let val = GATTServApp::read_char_cfg(conn_handle, BLINKY_CLIENT_CHARCFG.as_ptr());
if val == 0x01 {
// notification is no
let on = BLINKY_ON.load(Ordering::Relaxed);
let val: u8 = if on { 0x01 } else { 0x00 };
// let mut msg = gattMsg_t::alloc_handle_value_notification(conn_handle, 2);

unsafe {
NOTIFY_MSG.handleValueNoti.pValue =
GATT_bm_alloc(0, ATT_HANDLE_VALUE_NOTI, 2, ptr::null_mut(), 0) as _;
NOTIFY_MSG.handleValueNoti.handle = BLINKY_ATTR_TABLE[2].handle;
NOTIFY_MSG.handleValueNoti.len = 2;

core::ptr::copy(&val as *const _ as _, NOTIFY_MSG.handleValueNoti.pValue, 2);
println!("!! handle {}", BLINKY_ATTR_TABLE[2].handle);

let rc = GATT_Notification(conn_handle, &NOTIFY_MSG.handleValueNoti, 0);
println!("!! notify rc {:?}", rc);
}
}
},
Either::Second(AppEvent::Disconnected(_)) | Either::Second(AppEvent::BlinkyUnsubscribed(_)) => {
println!("disconnect, stop notification");
return;
}
_ => (),
}
}
}
Expand Down Expand Up @@ -659,6 +735,7 @@ async fn main(spawner: Spawner) -> ! {

let mut ble_config = ble::Config::default();
ble_config.pa_config = None;
ble_config.mac_addr = [0xca, 0xfe, 0xba, 0xbe, 0x01, 0x01].into();
let (task_id, sub) = hal::ble::init(ble_config).unwrap();
println!("BLE hal task id: {}", task_id);

Expand Down
16 changes: 8 additions & 8 deletions examples/ble-peripheral-devinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ static mut SYSTEM_ID: [u8; 8] = [0u8; 8];
static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
// Device Information Service
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::primaryServiceUUID.as_ptr() },
},
permissions: GATT_PERMIT_READ,
handle: 0,
// The first must be a Service attribute
value: &gattAttrType_t {
value: &GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::DEVINFO_SERV_UUID as *const _ as _,
} as *const _ as _,
},
// System ID Declaration
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -130,7 +130,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// System ID Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::SYSTEM_ID_UUID as *const _ as _,
},
Expand All @@ -140,7 +140,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number String Declaration
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -150,7 +150,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::SERIAL_NUMBER_UUID as *const _ as _,
},
Expand All @@ -160,7 +160,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Temperature
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: unsafe { gatt_uuid::characterUUID.as_ptr() },
},
Expand All @@ -170,7 +170,7 @@ static mut DEVICE_INFO_TABLE: [GattAttribute; 7] = [
},
// Serial Number Value
GattAttribute {
type_: gattAttrType_t {
type_: GattAttrType {
len: ATT_BT_UUID_SIZE,
uuid: &gatt_uuid::TEMP_UUID as *const _ as _,
},
Expand Down
21 changes: 11 additions & 10 deletions src/ble/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]

use core::ffi::c_void;

Check warning on line 3 in src/ble/ffi.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `core::ffi::c_void`
use core::num::NonZeroU8;

use super::gatt::gattMsg_t;

Check warning on line 6 in src/ble/ffi.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `super::gatt::gattMsg_t`

// pub type bStatus_t = u8;
// SUCCESS(0x00):指令按预期执行。
// INVALIDPARAMETER(0x02):无效的连接句柄或请求字段。
Expand Down Expand Up @@ -547,15 +550,14 @@ pub const DEVDISC_MODE_LIMITED: u8 = 0x02;
pub const DEVDISC_MODE_ALL: u8 = 0x03;

pub type pfnEcc_key_t = Option<unsafe extern "C" fn(pub_: *mut u8, priv_: *mut u8) -> ::core::ffi::c_int>;
pub type pfnEcc_dhkey_t =
Option<
unsafe extern "C" fn(
peer_pub_key_x: *mut u8,
peer_pub_key_y: *mut u8,
our_priv_key: *mut u8,
out_dhkey: *mut u8,
) -> ::core::ffi::c_int,
>;
pub type pfnEcc_dhkey_t = Option<
unsafe extern "C" fn(
peer_pub_key_x: *mut u8,
peer_pub_key_y: *mut u8,
our_priv_key: *mut u8,
out_dhkey: *mut u8,
) -> ::core::ffi::c_int,
>;
pub type pfnEcc_alg_f4_t = Option<
unsafe extern "C" fn(u: *mut u8, v: *mut u8, x: *mut u8, z: u8, out_enc_data: *mut u8) -> ::core::ffi::c_int,
>;
Expand Down Expand Up @@ -720,4 +722,3 @@ extern "C" {
}

// GATT

Loading

0 comments on commit b39ac19

Please sign in to comment.