Skip to content

Commit

Permalink
Bugfix: Gatts app needs registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Apr 30, 2024
1 parent fc0070f commit d1344fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Users are expected to provide implementations for various `rs-matter` abstractio

Furthermore, _operating_ the assembled Matter stack is also challenging, as various features might need to be switched on or off depending on whether Matter is running in commissioning or operating mode, and also depending on the current network connectivity (as in e.g. Wifi signal lost).

This crate provides an all-in-one [`MatterStack`](https://github.com/ivmarkov/esp-idf-matter/blob/master/src/lib.rs#L111) assembly that configures `rs-matter` for operating on top of the ESP IDF SDK.
This crate addresses these issues by providing an all-in-one [`MatterStack`](https://github.com/ivmarkov/esp-idf-matter/blob/master/src/stack.rs#L57) assembly that configures `rs-matter` for reliably operating on top of the ESP IDF SDK.

Instantiate it and then call `MatterStack::run(...)`.

Expand Down
34 changes: 26 additions & 8 deletions src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub struct BtpGattPeripheral<'a, 'd, M>
where
M: BleEnabled,
{
app_id: u16,
driver: &'a BtDriver<'d, M>,
context: &'a BtpGattContext,
}
Expand All @@ -118,8 +119,12 @@ where
M: BleEnabled,
{
/// Create a new instance.
pub fn new(driver: &'a BtDriver<'d, M>, context: &'a BtpGattContext) -> Self {
Self { driver, context }
pub fn new(app_id: u16, driver: &'a BtDriver<'d, M>, context: &'a BtpGattContext) -> Self {
Self {
app_id,
driver,
context,
}
}

pub async fn run<F>(
Expand All @@ -140,7 +145,7 @@ where

unsafe {
gap.subscribe_nonstatic(|event| {
let ctx = GattExecContext::new(&gap, &gatts, self.context);
let ctx = GattExecContext::new(self.app_id, &gap, &gatts, self.context);

ctx.check_esp_status(ctx.on_gap_event(event));
})?;
Expand All @@ -151,7 +156,7 @@ where

unsafe {
gatts.subscribe_nonstatic(|(gatt_if, event)| {
let ctx = GattExecContext::new(&gap, &gatts, self.context);
let ctx = GattExecContext::new(self.app_id, &gap, &gatts, self.context);

ctx.check_esp_status(ctx.on_gatts_event(
&service_name,
Expand All @@ -165,10 +170,14 @@ where

info!("BLE Gap and Gatts subscriptions initialized");

gatts.register_app(self.app_id)?;

info!("Gatts BTP app registered");

loop {
let mut ind = self.context.ind.lock_if(|ind| !ind.data.is_empty()).await;

let ctx = GattExecContext::new(&gap, &gatts, self.context);
let ctx = GattExecContext::new(self.app_id, &gap, &gatts, self.context);

// TODO: Is this asynchronous?
ctx.indicate(&ind.data, ind.addr)?;
Expand Down Expand Up @@ -231,6 +240,7 @@ where
T: Borrow<BtDriver<'d, M>>,
M: BleEnabled,
{
app_id: u16,
gap: &'a EspBleGap<'d, M, T>,
gatts: &'a EspGatts<'d, M, T>,
ctx: &'a BtpGattContext,
Expand All @@ -242,11 +252,17 @@ where
M: BleEnabled,
{
fn new(
app_id: u16,
gap: &'a EspBleGap<'d, M, T>,
gatts: &'a EspGatts<'d, M, T>,
ctx: &'a BtpGattContext,
) -> Self {
Self { gap, gatts, ctx }
Self {
app_id,
gap,
gatts,
ctx,
}
}

fn indicate(&self, data: &[u8], address: BtAddr) -> Result<bool, EspError> {
Expand Down Expand Up @@ -294,9 +310,11 @@ where
F: FnMut(GattPeripheralEvent),
{
match event {
GattsEvent::ServiceRegistered { status, .. } => {
GattsEvent::ServiceRegistered { status, app_id } => {
self.check_gatt_status(status)?;
self.create_service(gatt_if, service_name, service_adv_data)?;
if self.app_id == app_id {
self.create_service(gatt_if, service_name, service_adv_data)?;
}
}
GattsEvent::ServiceCreated {
status,
Expand Down
4 changes: 3 additions & 1 deletion src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ mod wifible {
use crate::{MatterStack, Network};

const MAX_WIFI_NETWORKS: usize = 2;
const GATTS_APP_ID: u16 = 0;

pub struct WifiBle {
btp_context: BtpContext<EspRawMutex>,
Expand Down Expand Up @@ -430,7 +431,8 @@ mod wifible {
{
info!("Running Matter in commissioning mode (BLE)");

let peripheral = BtpGattPeripheral::new(bt, &self.network.btp_gatt_context);
let peripheral =
BtpGattPeripheral::new(GATTS_APP_ID, bt, &self.network.btp_gatt_context);

let btp = Btp::new(peripheral, &self.network.btp_context);

Expand Down

0 comments on commit d1344fd

Please sign in to comment.