Skip to content

Commit

Permalink
Add Serde Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Norred committed Nov 15, 2023
1 parent 9e73020 commit cb3c06a
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 13 deletions.
1 change: 1 addition & 0 deletions nrf-softdevice-macro/src/security_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use quote::{quote, ToTokens};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SecurityMode {
NoAccess,
Open,
Expand Down
83 changes: 70 additions & 13 deletions nrf-softdevice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ evt-max-size-512 = []

[dependencies]
defmt = { version = "0.3", optional = true }
serde = { version = "1.0", optional = true, default-features = false, features = [
"derive",
] }
log = { version = "0.4.11", optional = true }
critical-section = { version = "1.0", optional = true }

Expand All @@ -60,13 +63,13 @@ futures = { version = "0.3.17", default-features = false }
embedded-storage = "0.3.0"
embedded-storage-async = { version = "0.4.0", optional = true }

nrf52805-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52810-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52811-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52820-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52832-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52833-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52840-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52805-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52810-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52811-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52820-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52832-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52833-pac = { version = "0.12.0", features = ["rt"], optional = true }
nrf52840-pac = { version = "0.12.0", features = ["rt"], optional = true }

nrf-softdevice-s112 = { version = "0.1.1", path = "../nrf-softdevice-s112", optional = true }
nrf-softdevice-s113 = { version = "0.1.1", path = "../nrf-softdevice-s113", optional = true }
Expand All @@ -78,16 +81,70 @@ nrf-softdevice-macro = { version = "0.1.0", path = "../nrf-softdevice-macro" }

[package.metadata.docs.rs]
targets = ["thumbv7em-none-eabi"]
features = ["nrf52840", "s140", "ble-central", "ble-peripheral", "ble-l2cap", "ble-gatt-server", "ble-gatt-client", "ble-rssi", "ble-sec"]
features = [
"nrf52840",
"s140",
"ble-central",
"ble-peripheral",
"ble-l2cap",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
"ble-sec",
]

[package.metadata.embassy_docs]
src_base = "https://github.com/embassy-rs/nrf-softdevice/blob/nrf-softdevice-mbr-v$VERSION/nrf-softdevice-mbr/src/"
src_base_git = "https://github.com/embassy-rs/nrf-softdevice/blob/$COMMIT/nrf-softdevice-mbr/src/"
target = "thumbv7em-none-eabi"
flavors = [
{ name = "s112", features = ["nrf52832", "s112", "ble-peripheral", "ble-gatt-server", "ble-gatt-client", "ble-rssi", "ble-sec"] },
{ name = "s113", features = ["nrf52840", "s113", "ble-peripheral", "ble-l2cap", "ble-gatt-server", "ble-gatt-client", "ble-rssi", "ble-sec"] },
{ name = "s122", features = ["nrf52833", "s122", "ble-central", "ble-gatt-server", "ble-gatt-client", "ble-rssi"] },
{ name = "s132", features = ["nrf52832", "s132", "ble-central", "ble-peripheral", "ble-l2cap", "ble-gatt-server", "ble-gatt-client", "ble-rssi", "ble-sec"] },
{ name = "s140", features = ["nrf52840", "s140", "ble-central", "ble-peripheral", "ble-l2cap", "ble-gatt-server", "ble-gatt-client", "ble-rssi", "ble-sec"] },
{ name = "s112", features = [
"nrf52832",
"s112",
"ble-peripheral",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
"ble-sec",
] },
{ name = "s113", features = [
"nrf52840",
"s113",
"ble-peripheral",
"ble-l2cap",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
"ble-sec",
] },
{ name = "s122", features = [
"nrf52833",
"s122",
"ble-central",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
] },
{ name = "s132", features = [
"nrf52832",
"s132",
"ble-central",
"ble-peripheral",
"ble-l2cap",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
"ble-sec",
] },
{ name = "s140", features = [
"nrf52840",
"s140",
"ble-central",
"ble-peripheral",
"ble-l2cap",
"ble-gatt-server",
"ble-gatt-client",
"ble-rssi",
"ble-sec",
] },
]
2 changes: 2 additions & 0 deletions nrf-softdevice/src/ble/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{raw, RawError, Softdevice};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ConnectError {
Timeout,
NoAddresses,
Expand Down Expand Up @@ -127,6 +128,7 @@ impl<'a> Default for ConnectConfig<'a> {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ScanError {
Timeout,
Raw(RawError),
Expand Down
4 changes: 4 additions & 0 deletions nrf-softdevice/src/ble/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ const BLE_GAP_DATA_LENGTH_DEFAULT: u8 = 27; // The stack's default data length.

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub(crate) struct OutOfConnsError;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DisconnectedError;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SetConnParamsError {
Disconnected,
Raw(RawError),
Expand All @@ -41,6 +44,7 @@ impl From<RawError> for SetConnParamsError {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg(feature = "ble-peripheral")]
pub enum IgnoreSlaveLatencyError {
Disconnected,
Expand Down
6 changes: 6 additions & 0 deletions nrf-softdevice/src/ble/gatt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Descriptor {
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub enum HvxType {
Invalid = 0,
Expand Down Expand Up @@ -77,6 +78,7 @@ pub trait Client {
/// Error type for [`discover`]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum DiscoverError {
/// Connection is disconnected.
Disconnected,
Expand Down Expand Up @@ -314,6 +316,7 @@ pub async fn discover<T: Client>(conn: &Connection) -> Result<T, DiscoverError>

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ReadError {
Disconnected,
Truncated,
Expand Down Expand Up @@ -375,6 +378,7 @@ pub async fn read(conn: &Connection, handle: u16, buf: &mut [u8]) -> Result<usiz

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum WriteError {
Disconnected,
Timeout,
Expand Down Expand Up @@ -474,6 +478,7 @@ pub async fn write_without_response(conn: &Connection, handle: u16, buf: &[u8])

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TryWriteError {
Disconnected,
BufferFull,
Expand Down Expand Up @@ -532,6 +537,7 @@ pub(crate) unsafe fn on_evt(ble_evt: *const raw::ble_evt_t) {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum MtuExchangeError {
/// Connection is disconnected.
Disconnected,
Expand Down
11 changes: 11 additions & 0 deletions nrf-softdevice/src/ble/gatt_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct CharacteristicHandles {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ServiceHandle(u16);

impl ServiceHandle {
Expand All @@ -42,6 +43,7 @@ impl ServiceHandle {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct IncludedServiceHandle(u16);

impl IncludedServiceHandle {
Expand All @@ -52,6 +54,7 @@ impl IncludedServiceHandle {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DescriptorHandle(u16);

impl DescriptorHandle {
Expand All @@ -63,6 +66,7 @@ impl DescriptorHandle {
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub enum WriteOp {
Request = 1,
Expand Down Expand Up @@ -153,6 +157,7 @@ pub trait Service: Sized {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum RegisterError {
Raw(RawError),
}
Expand Down Expand Up @@ -269,6 +274,7 @@ where

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum GetValueError {
Truncated,
Raw(RawError),
Expand Down Expand Up @@ -298,6 +304,7 @@ pub fn get_value(_sd: &Softdevice, handle: u16, buf: &mut [u8]) -> Result<usize,

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SetValueError {
Raw(RawError),
}
Expand All @@ -322,6 +329,7 @@ pub fn set_value(_sd: &Softdevice, handle: u16, val: &[u8]) -> Result<(), SetVal

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum NotifyValueError {
Disconnected,
Raw(RawError),
Expand Down Expand Up @@ -359,6 +367,7 @@ pub fn notify_value(conn: &Connection, handle: u16, val: &[u8]) -> Result<(), No

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum IndicateValueError {
Disconnected,
Raw(RawError),
Expand Down Expand Up @@ -396,6 +405,7 @@ pub fn indicate_value(conn: &Connection, handle: u16, val: &[u8]) -> Result<(),

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum GetSysAttrsError {
DataSize(usize),
Disconnected,
Expand All @@ -422,6 +432,7 @@ pub fn get_sys_attrs(conn: &Connection, buf: &mut [u8]) -> Result<usize, GetSysA

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SetSysAttrsError {
Disconnected,
Raw(RawError),
Expand Down
6 changes: 6 additions & 0 deletions nrf-softdevice/src/ble/gatt_server/characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::raw;

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct AttributeMetadata {
pub read: SecurityMode,
pub write: SecurityMode,
Expand Down Expand Up @@ -58,6 +59,7 @@ impl AttributeMetadata {

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Attribute<T: AsRef<[u8]>> {
pub metadata: AttributeMetadata,
pub value: T,
Expand Down Expand Up @@ -115,6 +117,7 @@ impl<T: AsRef<[u8]>> Attribute<T> {

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct UserDescription {
pub metadata: Option<AttributeMetadata>,
pub value: &'static [u8],
Expand All @@ -123,6 +126,7 @@ pub struct UserDescription {

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Properties {
pub broadcast: bool,
pub read: bool,
Expand Down Expand Up @@ -220,6 +224,8 @@ impl Properties {

#[derive(Default, Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(bound(deserialize = "'de: 'static")))]
pub struct Metadata {
pub properties: Properties,
pub user_description: Option<UserDescription>,
Expand Down
1 change: 1 addition & 0 deletions nrf-softdevice/src/ble/gatt_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use heapless::{String, Vec};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum FromGattError {
InvalidLength,
InvalidCharacter,
Expand Down
3 changes: 3 additions & 0 deletions nrf-softdevice/src/ble/l2cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) unsafe fn on_evt(ble_evt: *const raw::ble_evt_t) {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TxError<P: Packet> {
Disconnected,
TxQueueFull(P),
Expand All @@ -92,6 +93,7 @@ impl<P: Packet> From<RawError> for TxError<P> {
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum RxError {
Disconnected,
AllocateFailed,
Expand All @@ -112,6 +114,7 @@ impl From<RawError> for RxError {

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SetupError {
Disconnected,
Refused,
Expand Down
4 changes: 4 additions & 0 deletions nrf-softdevice/src/ble/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct RawAdvertisement<'a> {
/// Connectable advertisement types, which can accept connections from interested Central devices.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ConnectableAdvertisement<'a> {
ScannableUndirected {
adv_data: &'a [u8],
Expand Down Expand Up @@ -95,6 +96,7 @@ impl<'a> From<ConnectableAdvertisement<'a>> for RawAdvertisement<'a> {
/// only used to broadcast information in the air.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum NonconnectableAdvertisement<'a> {
ScannableUndirected {
adv_data: &'a [u8],
Expand Down Expand Up @@ -204,6 +206,7 @@ impl<'a> From<NonconnectableAdvertisement<'a>> for RawAdvertisement<'a> {
/// Error for [`advertise_start`]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum AdvertiseError {
Timeout,
NoFreeConn,
Expand Down Expand Up @@ -399,6 +402,7 @@ where
#[repr(u8)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum FilterPolicy {
#[default]
Any = raw::BLE_GAP_ADV_FP_ANY as u8,
Expand Down
Loading

0 comments on commit cb3c06a

Please sign in to comment.