use std::cell::Cell;
use std::rc::Rc;
use bluetooth_traits::{BluetoothRequest, BluetoothResponse, GATTType};
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use crate::dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bluetooth::{get_gatt_children, response_async, AsyncBluetoothListener};
use crate::dom::bluetoothdevice::BluetoothDevice;
use crate::dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::realms::InRealm;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct BluetoothRemoteGATTServer {
reflector_: Reflector,
device: Dom<BluetoothDevice>,
connected: Cell<bool>,
}
impl BluetoothRemoteGATTServer {
pub fn new_inherited(device: &BluetoothDevice) -> BluetoothRemoteGATTServer {
BluetoothRemoteGATTServer {
reflector_: Reflector::new(),
device: Dom::from_ref(device),
connected: Cell::new(false),
}
}
pub fn new(
global: &GlobalScope,
device: &BluetoothDevice,
) -> DomRoot<BluetoothRemoteGATTServer> {
reflect_dom_object(
Box::new(BluetoothRemoteGATTServer::new_inherited(device)),
global,
)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
pub fn set_connected(&self, connected: bool) {
self.connected.set(connected);
}
}
impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
fn Device(&self) -> DomRoot<BluetoothDevice> {
DomRoot::from_ref(&self.device)
}
fn Connected(&self) -> bool {
self.connected.get()
}
#[allow(unsafe_code)]
fn Connect(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let p = Promise::new_in_current_realm(comp, can_gc);
let sender = response_async(&p, self);
self.get_bluetooth_thread()
.send(BluetoothRequest::GATTServerConnect(
String::from(self.Device().Id()),
sender,
))
.unwrap();
p
}
fn Disconnect(&self, can_gc: CanGc) -> ErrorResult {
if !self.Connected() {
return Ok(());
}
self.Device().clean_up_disconnected_device(can_gc);
self.Device().garbage_collect_the_connection()
}
fn GetPrimaryService(&self, service: BluetoothServiceUUID, can_gc: CanGc) -> Rc<Promise> {
get_gatt_children(
self,
true,
BluetoothUUID::service,
Some(service),
String::from(self.Device().Id()),
self.Device().get_gatt().Connected(),
GATTType::PrimaryService,
can_gc,
)
}
fn GetPrimaryServices(
&self,
service: Option<BluetoothServiceUUID>,
can_gc: CanGc,
) -> Rc<Promise> {
get_gatt_children(
self,
false,
BluetoothUUID::service,
service,
String::from(self.Device().Id()),
self.Connected(),
GATTType::PrimaryService,
can_gc,
)
}
}
impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
fn handle_response(&self, response: BluetoothResponse, promise: &Rc<Promise>, _can_gc: CanGc) {
match response {
BluetoothResponse::GATTServerConnect(connected) => {
if self.Device().is_represented_device_null() {
if let Err(e) = self.Device().garbage_collect_the_connection() {
return promise.reject_error(e);
}
return promise.reject_error(Error::Network);
}
self.connected.set(connected);
promise.resolve_native(self);
},
BluetoothResponse::GetPrimaryServices(services_vec, single) => {
let device = self.Device();
if single {
promise.resolve_native(&device.get_or_create_service(&services_vec[0], self));
return;
}
let mut services = vec![];
for service in services_vec {
let bt_service = device.get_or_create_service(&service, self);
services.push(bt_service);
}
promise.resolve_native(&services);
},
_ => promise.reject_error(Error::Type("Something went wrong...".to_owned())),
}
}
}