RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/softdevice/ble/
server.rs

1use nrf_softdevice::{
2    ble::{
3        gatt_server::{self, RegisterError, WriteOp},
4        Connection,
5    },
6    Softdevice,
7};
8
9use super::services::{
10    battery::BatteryService,
11    device_information::{DeviceInformation, DeviceInformationService, PnPID, VidSource},
12    hid::HidService,
13};
14
15pub struct Server {
16    pub _dis: DeviceInformationService,
17    pub bas: BatteryService,
18    pub hid: HidService,
19}
20
21impl Server {
22    pub fn new(
23        sd: &mut Softdevice,
24        device_information: DeviceInformation,
25    ) -> Result<Self, RegisterError> {
26        let dis = DeviceInformationService::new(
27            sd,
28            &PnPID {
29                vid_source: VidSource::UsbIF,
30                vendor_id: 0xDEAD,
31                product_id: 0xBEEF,
32                product_version: 0x0000,
33            },
34            device_information,
35        )?;
36
37        let bas = BatteryService::new(sd)?;
38        let _ = bas.battery_level_set(sd, 100);
39
40        let hid = HidService::new(sd)?;
41
42        Ok(Self {
43            _dis: dis,
44            bas,
45            hid,
46        })
47    }
48}
49
50impl gatt_server::Server for Server {
51    type Event = ();
52
53    fn on_write(
54        &self,
55        conn: &Connection,
56        handle: u16,
57        _op: WriteOp,
58        _offset: usize,
59        data: &[u8],
60    ) -> Option<Self::Event> {
61        self.hid.on_write(conn, handle, data);
62        self.bas.on_write(handle, data);
63        None
64    }
65}