summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomeo Disca <romeo.disca@gmail.com>2020-08-06 22:41:02 +0200
committerRomeo Disca <romeo.disca@gmail.com>2020-08-06 22:41:02 +0200
commit43664e4174d89a57f3679cdfdbebe2f706b56790 (patch)
tree5608bee290f4c7b6b401f7b855565c56de7ca166
parente7595ef9caf602e5076c24cc5d01f285b6988b26 (diff)
chore: create simple event handler
-rw-r--r--Cargo.lock100
-rw-r--r--Cargo.toml1
-rw-r--r--src/client.rs24
-rwxr-xr-xsrc/enums.rs99
-rw-r--r--src/events.rs119
-rw-r--r--src/main.rs16
6 files changed, 358 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4462c9b
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,100 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "derivative"
+version = "2.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cb582b60359da160a9477ee80f15c8d784c477e69c217ef2cdd4169c24ea380f"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "num_enum"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca565a7df06f3d4b485494f25ba05da1435950f4dc263440eda7a6fa9b8e36e4"
+dependencies = [
+ "derivative",
+ "num_enum_derive",
+]
+
+[[package]]
+name = "num_enum_derive"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffa5a33ddddfee04c0283a7653987d634e880347e96b5b2ed64de07efb59db9d"
+dependencies = [
+ "proc-macro-crate",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro-crate"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
+dependencies = [
+ "toml",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.114"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
+
+[[package]]
+name = "simpleclient"
+version = "0.1.0"
+dependencies = [
+ "num_enum",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.38"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "toml"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
diff --git a/Cargo.toml b/Cargo.toml
index 31e1929..bdd05e1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+num_enum = "0.4.2"
diff --git a/src/client.rs b/src/client.rs
new file mode 100644
index 0000000..3557df8
--- /dev/null
+++ b/src/client.rs
@@ -0,0 +1,24 @@
+
+
+use super::events::*;
+
+pub fn event_handler<F>(f: F) -> Box<dyn FnMut(Event)>
+ where F: FnMut(Event) + 'static
+ {
+ Box::new(f)
+ }
+
+pub struct FlicClient {
+ map: Vec<Box<dyn FnMut(Event)>>,
+}
+
+impl FlicClient {
+ pub fn new() -> FlicClient {
+ FlicClient{map: vec![]}
+ }
+ pub fn register_event_handler(mut self, event: Box<dyn FnMut(Event)>) -> Self {
+ self.map.push(event);
+ self
+ }
+}
+
diff --git a/src/enums.rs b/src/enums.rs
new file mode 100755
index 0000000..64284a2
--- /dev/null
+++ b/src/enums.rs
@@ -0,0 +1,99 @@
+
+#![allow(dead_code)]
+
+use num_enum::TryFromPrimitive;
+use num_enum::IntoPrimitive;
+
+// Enums
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum CreateConnectionChannelError {
+ NoError,
+ MaxPendingConnectionsReached,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum ConnectionStatus {
+ Disconnected,
+ Connected,
+ Ready,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum DisconnectReason {
+ Unspecified,
+ ConnectionEstablishmentFailed,
+ TimedOut,
+ BondingKeysMismatch,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum RemovedReason {
+ RemovedByThisClient,
+ ForceDisconnectedByThisClient,
+ ForceDisconnectedByOtherClient,
+
+ ButtonIsPrivate,
+ VerifyTimeout,
+ InternetBackendError,
+ InvalidData,
+
+ CouldntLoadDevice,
+
+ DeletedByThisClient,
+ DeletedByOtherClient,
+ ButtonBelongsToOtherPartner,
+ DeletedFromButton,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum ClickType {
+ ButtonDown,
+ ButtonUp,
+ ButtonClick,
+ ButtonSingleClick,
+ ButtonDoubleClick,
+ ButtonHold,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum BdAddrType {
+ PublicBdAddrType,
+ RandomBdAddrType,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum LatencyMode {
+ NormalLatency,
+ LowLatency,
+ HighLatency,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum ScanWizardResult {
+ WizardSuccess,
+ WizardCancelledByUser,
+ WizardFailedTimeout,
+ WizardButtonIsPrivate,
+ WizardBluetoothUnavailable,
+ WizardInternetBackendError,
+ WizardInvalidData,
+ WizardButtonBelongsToOtherPartner,
+ WizardButtonAlreadyConnectedToOtherDevice,
+}
+
+#[repr(u8)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, IntoPrimitive, TryFromPrimitive)]
+pub enum BluetoothControllerState {
+ Detached,
+ Resetting,
+ Attached,
+}
diff --git a/src/events.rs b/src/events.rs
new file mode 100644
index 0000000..aa53280
--- /dev/null
+++ b/src/events.rs
@@ -0,0 +1,119 @@
+
+use super::enums::*;
+
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub enum Event {
+ AdvertisementPacket {
+ opcode: u8,
+ scan_id: u32,
+ bd_addr: String,
+ name: String,
+ rssi: u8,
+ is_private: bool,
+ already_verified: bool,
+ already_connected_to_this_device: bool,
+ already_connected_to_other_device: bool,
+ },
+
+ CreateConnectionChannelResponse {
+ conn_id: u32,
+ error: CreateConnectionChannelError,
+ connection_status: ConnectionStatus,
+ },
+
+ ConnectionStatusChanged {
+ conn_id: u32,
+ connection_status: ConnectionStatus,
+ disconnect_reason: DisconnectReason,
+ },
+
+ ConnectionChannelRemoved {
+ conn_id: u32,
+ removed_reason: RemovedReason,
+ },
+
+ ButtonEvent {
+ conn_id: u32,
+ click_type: ClickType,
+ was_queued: bool,
+ time_diff: i32,
+ },
+
+ NewVerifiedButton {
+ opcode: u8,
+ bd_addr: String,
+ },
+
+ GetInfoResponse {
+ opcode: u8,
+ bluetooth_controller_state: BluetoothControllerState,
+ my_bd_addr: String,
+ my_bd_addr_type: BdAddrType,
+ max_pending_connections: u8,
+ max_concurrently_connected_buttons: i16,
+ current_pending_connections: u8,
+ currently_no_space_for_new_connection: bool,
+ bd_addr_of_verified_buttons: Vec<String>,
+ },
+
+ NoSpaceForNewConnection {
+ opcode: u8,
+ max_concurrently_connected_buttons: u8,
+ },
+
+ GotSpaceForNewConnection {
+ opcode: u8,
+ max_concurrently_connected_buttons: u8,
+ },
+
+ BluetoothControllerStateChange {
+ opcode: u8,
+ state: BluetoothControllerState,
+ },
+
+ PingResponse {
+ opcode: u8,
+ ping_id: u32,
+ },
+
+ GetButtonInfoResponse {
+ opcode: u8,
+ bd_addr: String,
+ uuid: String,
+ color: Option<String>,
+ serial_number: Option<String>,
+ },
+
+ ScanWizardFoundPrivateButton {
+ scan_wizard_id: u32,
+ },
+
+ ScanWizardFoundPublicButton {
+ scan_wizard_id: u32,
+ bd_addr: String,
+ name: String,
+ },
+
+ ScanWizardButtonConnected {
+ scan_wizard_id: u32,
+ },
+
+ ScanWizardCompleted {
+ scan_wizard_id: u32,
+ result: ScanWizardResult,
+ },
+
+ ButtonDeleted {
+ opcode: u8,
+ bd_addr: String,
+ deleted_by_this_client: bool,
+ },
+
+ BatteryStatus {
+ opcode: u8,
+ listener_id: u32,
+ battery_percentage: i8,
+ timestamp: u64,
+ },
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..fff75ce 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,17 @@
+
+mod enums;
+mod events;
+mod client;
+
+use client::*;
+
fn main() {
- println!("Hello, world!");
+
+ let event = event_handler(|event| { println!("ping response: {:?}", event); });
+ let event2 = event_handler(|event| { println!("ping response: {:?}", event); });
+
+ let _client = FlicClient::new()
+ .register_event_handler(event)
+ .register_event_handler(event2)
+ ;
}