Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Basic functionality implementation. Need to handle events.
Browse files Browse the repository at this point in the history
  • Loading branch information
persello committed Aug 16, 2022
0 parents commit 6342faf
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[build]
target = "riscv32imc-esp-espidf"

[target.xtensa-esp32-espidf]
linker = "ldproxy"

[target.xtensa-esp32s2-espidf]
linker = "ldproxy"

[target.xtensa-esp32s3-espidf]
linker = "ldproxy"

[target.riscv32imc-esp-espidf]
linker = "ldproxy"
rustflags = ["-C", "default-linker-libraries"]

[env]
ESP_IDF_SDKCONFIG_DEFAULTS = "sdkconfig.defaults"
ESP_IDF_VERSION = { value = "branch:release/v4.4" }

ESP_IDF_GLOB_CONFIG_FILES_BASE = { value = ".", relative = true }
ESP_IDF_GLOB_CONFIG_FILES_1 = { value = "/partitions.csv" }

[unstable]
build-std = ["std", "panic_abort"]

[net]
git-fetch-with-cli = true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
Cargo.lock
.embuild/
**/.DS_Store
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/bluedroid.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(bluedroid)
22 changes: 22 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "bluedroid"
version = "0.1.0"
edition = "2021"

[dependencies]
esp-idf-sys = { version = "0.31.6", features = ["native"] }
log = { version = "0.4.17" }
lazy_static = { version = "1.4.0" }

[build-dependencies]
embuild = { version = "0.30.1" }
anyhow = { version = "1.0.58" }

[dev-dependencies]
anyhow = { version = "1.0.58" }
esp-idf-sys = { version = "0.31.4", features = ["native", "binstart"] }
esp-idf-svc = { version = "0.42.1" }

[[example]]
name = "server"
required-features = ["esp-idf-sys/binstart"]
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> anyhow::Result<()> {
embuild::build::CfgArgs::output_propagated("ESP_IDF")?;
embuild::build::LinkArgs::output_propagated("ESP_IDF")
}
31 changes: 31 additions & 0 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bluedroid::{
gatt_server::{GattServer, Application, Service},
utilities::ble_uuid::BleUuid,
};
use esp_idf_svc;
use log::info;
use bluedroid::gatt_server::{Characteristic, Descriptor};

fn main() {
esp_idf_sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();

info!("Logger initialised.");

let main_application = Application::new("Main Application", 0x01)
.add_service(
Service::new("Service 1", BleUuid::from_uuid16(0x0001), true)
.add_characteristic(
Characteristic::new("Characteristic 1", BleUuid::from_uuid16(0x0001))
.add_descriptor(
&mut Descriptor::new("Descriptor 1", BleUuid::from_uuid16(0x0001))
)
)
);

let applications = [main_application];

let mut s = GattServer::take().unwrap();
s.add_applications(&applications);
s.start();
}
5 changes: 5 additions & 0 deletions partitions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 3M,
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]

channel = "nightly-2022-07-23"
14 changes: 14 additions & 0 deletions sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BT_ENABLED=y
# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
# CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS is not set
# CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE is not set
CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y
# CONFIG_VFS_SUPPORT_IO is not set
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})
59 changes: 59 additions & 0 deletions src/gatt_server/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::gatt_server::service::Service;
use esp_idf_sys::*;
use log::info;

#[derive(Debug, Clone)]
pub struct Application {
name: Option<String>,
services: Vec<Service>,
identifier: u16,
pub(crate) interface: Option<u8>,
handle_counter: u16,
}

impl Application {
pub fn new(name: &str, identifier: u16) -> Self {
Application {
name: Some(String::from(name)),
services: Vec::new(),
identifier,
interface: None,
handle_counter: 0,
}
}

pub fn add_service(mut self, service: &Service) -> Self {
self.services.push(service.clone());
self
}

pub(crate) fn generate_handle(&mut self) -> u16 {
self.handle_counter += 1;
self.handle_counter
}

pub(crate) fn register_self(&self) {
info!("Registering {}.", self);
unsafe { esp_nofail!(esp_ble_gatts_app_register(self.identifier)) };
}

fn register_services(mut self) {
info!("Registering {}'s services.", &self);
let handle = self.generate_handle();
self.services.iter_mut().for_each(|service| {
service.register_self(self.interface.unwrap(), handle);
});
}
}

impl std::fmt::Display for Application {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let interface_string = if let Some(interface) = self.interface {
format!("{}", interface)
} else {
String::from("None")
};

write!(f, "{} (0x{:02x}, interface: {})", self.name.clone().unwrap_or_else(|| "Unnamed application".to_string()), self.identifier, interface_string)
}
}
37 changes: 37 additions & 0 deletions src/gatt_server/characteristic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::fmt::Formatter;
use crate::gatt_server::descriptor::Descriptor;
use crate::utilities::ble_uuid::BleUuid;

#[derive(Debug, Clone)]
pub struct Characteristic {
name: Option<String>,
uuid: BleUuid,
value: Vec<u8>,
descriptors: Vec<Descriptor>,
}

impl Characteristic {
pub fn new(name: &str, uuid: BleUuid) -> Characteristic {
Characteristic {
name: Some(String::from(name)),
uuid,
value: Vec::new(),
descriptors: Vec::new(),
}
}

pub fn add_descriptor(&mut self, descriptor: &mut Descriptor) -> &mut Self {
self.descriptors.push(descriptor.clone());
self
}

fn register_self(&mut self) {}

fn register_descriptors(&self) {}
}

impl std::fmt::Display for Characteristic {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.name.clone().unwrap_or_else(|| "Unnamed characteristic".to_string()), self.uuid)
}
}
24 changes: 24 additions & 0 deletions src/gatt_server/descriptor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::utilities::ble_uuid::BleUuid;

#[derive(Debug, Clone)]
pub struct Descriptor {
name: Option<String>,
uuid: BleUuid,
value: Vec<u8>,
}

impl Descriptor {
pub fn new(name: &str, uuid: BleUuid) -> Descriptor {
Descriptor {
name: Some(String::from(name)),
uuid,
value: Vec::new(),
}
}
}

impl std::fmt::Display for Descriptor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.name.clone().unwrap_or_else(|| "Unnamed descriptor".to_string()), self.uuid)
}
}
1 change: 1 addition & 0 deletions src/gatt_server/gap_event_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

16 changes: 16 additions & 0 deletions src/gatt_server/gatts_event_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::gatt_server::GattServer;
use esp_idf_sys::*;

impl GattServer {
fn gatts_event_handler(
&mut self,
event: esp_gatts_cb_event_t,
gatts_if: esp_gatt_if_t,
param: *mut esp_ble_gatts_cb_param_t,
) {
let params = unsafe { (*param).reg };
if event == esp_gatts_cb_event_t_ESP_GATTS_REG_EVT && params.status == esp_gatt_status_t_ESP_GATT_OK {
// self.applications
}
}
}
Loading

0 comments on commit 6342faf

Please sign in to comment.