Git Product home page Git Product logo

Comments (3)

sureshjoshi avatar sureshjoshi commented on September 26, 2024 1

Okay, just in case anyone runs into this, there is a bit of a hack in the meantime to get some raw data out (I've never used Rust macros, so the "correct" solution will take me some time to wrap my head around before I can PR).

Given this gatt client:

#[nrf_softdevice::gatt_client(uuid = "00726f62-6f74-7061-6a61-6d61732e6361")]
struct MyClient {
    #[characteristic(uuid = "01726f62-6f74-7061-6a61-6d61732e6361", write)]
    tx: u8,
    #[characteristic(uuid = "02726f62-6f74-7061-6a61-6d61732e6361", read, notify)]
    rx: u8,
}

The existing macros will create what you need to set up notifications.

let mut config = central::ConnectConfig::default();
config.scan_config.whitelist = Some(addresses);
let connection = unwrap!(central::connect(sd, &config).await);
let my_client: MyClient = unwrap!(gatt_client::discover(&connection).await);

So, write to the characteristic descriptor with your notification/indication request:

// Write to the generated notify cccd handle with a 16-bit little-endian 0x0001 to setup notifications (0x0002 for indications)
unwrap!(gatt_client::write(&connection, my_client.rx_cccd_handle, &[0x01, 0x00]).await);

Then, use this new function to consume the raw ble_evt_t:

let result = gatt_client::run(&connection, &client, |e| {
    info!("Event: {:?}", e.header.evt_id);
}).await;

In gatt_client.rs, I've created a run function that currently just passes the BLE event straight down for manual consumption (setting this up with macro-generated events Ala gatt_server::run would be better).

pub async fn run<'m, F, C>(conn: &Connection, client: &C, mut f: F) -> DisconnectedError
where
    // HACK: Passing the event straight down
    F: FnMut(&ble_evt_t),
    C: Client,
{
    let conn_handle = match conn.with_state(|state| state.check_connected()) {
        Ok(handle) => handle,
        Err(DisconnectedError) => return DisconnectedError,
    };

    portal(conn_handle)
        .wait_many(|ble_evt| unsafe {
            info!("run: ble_evt={:?}", ble_evt);
            let ble_evt = &*ble_evt;
            if u32::from(ble_evt.header.evt_id) == raw::BLE_GAP_EVTS_BLE_GAP_EVT_DISCONNECTED {
                return Some(DisconnectedError);
            }

            // If evt_id is not BLE_GAP_EVTS_BLE_GAP_EVT_DISCONNECTED, then it must be a GATTC event
            let gattc_evt = get_union_field(ble_evt, &ble_evt.evt.gattc_evt);
            let conn = unwrap!(Connection::from_handle(gattc_evt.conn_handle));
            let evt: Option<ble_evt_t> = match ble_evt.header.evt_id as u32 {
                raw::BLE_GATTC_EVTS_BLE_GATTC_EVT_HVX => {
                    let params = get_union_field(ble_evt, &gattc_evt.params.hvx);
                    let v = get_flexarray(ble_evt, &params.data, params.len as usize);
                    trace!("GATT_HVX write handle={:?} data={:?}", params.handle, v);
                    None
                }

                _ => None,
            };

            // HACK: Just passing the event straight down
            f(ble_evt);
            

            None
        })
        .await
}

Hopefully this helps someone, and if anyone would like to provide me some suggestions on how this API should look and how I should consider setting up the macros - I'd appreciate it. I can then run with it and submit a PR to do this properly.

from nrf-softdevice.

alexmoon avatar alexmoon commented on September 26, 2024

It doesn't look like notifications are currently supported. You would need to add handling for the BLE_GATTC_EVT_HVX event to gatt_client.rs and add support for writing to the Client Configuration Characteristic Descriptor to enable/disable notifications. PRs are welcome.

from nrf-softdevice.

sureshjoshi avatar sureshjoshi commented on September 26, 2024

Is this still an open issue?

As in, if I use the NRF as a client and connect to a peripheral, I cannot receive notifications from that peripheral?

from nrf-softdevice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.