diff options
author | Romeo Disca <romeo.disca@gmail.com> | 2020-08-06 22:41:02 +0200 |
---|---|---|
committer | Romeo Disca <romeo.disca@gmail.com> | 2020-08-06 22:41:02 +0200 |
commit | 43664e4174d89a57f3679cdfdbebe2f706b56790 (patch) | |
tree | 5608bee290f4c7b6b401f7b855565c56de7ca166 /src | |
parent | e7595ef9caf602e5076c24cc5d01f285b6988b26 (diff) |
chore: create simple event handler
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 24 | ||||
-rwxr-xr-x | src/enums.rs | 99 | ||||
-rw-r--r-- | src/events.rs | 119 | ||||
-rw-r--r-- | src/main.rs | 16 |
4 files changed, 257 insertions, 1 deletions
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) + ; } |