Skip to main content
RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/softdevice/ble/
server.rs

1use nrf_softdevice::{
2    Softdevice,
3    ble::{
4        Connection,
5        gatt_server::{self, RegisterError, WriteOp},
6    },
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 { _dis: dis, bas, hid })
43    }
44}
45
46impl gatt_server::Server for Server {
47    type Event = ();
48
49    fn on_write(
50        &self,
51        conn: &Connection,
52        handle: u16,
53        _op: WriteOp,
54        _offset: usize,
55        data: &[u8],
56    ) -> Option<Self::Event> {
57        self.hid.on_write(conn, handle, data);
58        self.bas.on_write(handle, data);
59        None
60    }
61}