Skip to content

Commit c058483

Browse files
committed
feat: Add ability to add/delete serial port info via rust bridge
1 parent c36128d commit c058483

File tree

6 files changed

+961
-297
lines changed

6 files changed

+961
-297
lines changed

intiface-engine-flutter-bridge/src/api.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
mobile_init,
55
};
66
use anyhow::Result;
7-
use buttplug::server::device::configuration::DeviceConfigurationManagerBuilder;
7+
use buttplug::server::device::configuration::{DeviceConfigurationManagerBuilder, SerialSpecifier};
88
pub use buttplug::{
99
core::message::{ButtplugActuatorFeatureMessageType, ButtplugDeviceMessageType, ButtplugSensorFeatureMessageType, DeviceFeature, DeviceFeatureActuator, DeviceFeatureRaw, DeviceFeatureSensor, Endpoint, FeatureType},
1010
server::device::{
@@ -295,6 +295,34 @@ impl Into<UserDeviceIdentifier> for ExposedUserDeviceIdentifier {
295295
}
296296
}
297297

298+
299+
#[derive(Debug, Clone)]
300+
pub struct ExposedSerialSpecifier {
301+
pub baud_rate: u32,
302+
pub data_bits: u8,
303+
pub stop_bits: u8,
304+
pub parity: String,
305+
pub port: String,
306+
}
307+
308+
impl From<SerialSpecifier> for ExposedSerialSpecifier {
309+
fn from(value: SerialSpecifier) -> Self {
310+
Self {
311+
port: value.port().clone(),
312+
parity: value.parity().clone().into(),
313+
baud_rate: value.baud_rate().clone(),
314+
data_bits: value.data_bits().clone(),
315+
stop_bits: value.stop_bits().clone()
316+
}
317+
}
318+
}
319+
320+
impl Into<SerialSpecifier> for ExposedSerialSpecifier {
321+
fn into(self) -> SerialSpecifier {
322+
SerialSpecifier::new(&self.port, self.baud_rate, self.data_bits, self.stop_bits, self.parity.chars().next().unwrap())
323+
}
324+
}
325+
298326
#[derive(Debug, Clone)]
299327
pub struct ExposedWebsocketSpecifier {
300328
pub name: String,
@@ -570,7 +598,7 @@ pub fn setup_device_configuration_manager(base_config: Option<String>, user_conf
570598
}
571599
}
572600

573-
pub fn get_user_communication_specifiers() -> Vec<(String, ExposedWebsocketSpecifier)> {
601+
pub fn get_user_websocket_communication_specifiers() -> Vec<(String, ExposedWebsocketSpecifier)> {
574602
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
575603
let mut ws_specs = vec!();
576604
for kv in dcm.user_communication_specifiers() {
@@ -583,6 +611,19 @@ pub fn get_user_communication_specifiers() -> Vec<(String, ExposedWebsocketSpeci
583611
ws_specs
584612
}
585613

614+
pub fn get_user_serial_communication_specifiers() -> Vec<(String, ExposedSerialSpecifier)> {
615+
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
616+
let mut port_specs = vec!();
617+
for kv in dcm.user_communication_specifiers() {
618+
for comm_spec in kv.value() {
619+
if let ProtocolCommunicationSpecifier::Serial(port) = comm_spec {
620+
port_specs.push((kv.key().to_owned(), ExposedSerialSpecifier::from(port.clone())))
621+
}
622+
}
623+
}
624+
port_specs
625+
}
626+
586627
pub fn get_user_device_definitions() -> Vec<(ExposedUserDeviceIdentifier, ExposedUserDeviceDefinition)> {
587628
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
588629
dcm
@@ -610,6 +651,16 @@ pub fn remove_websocket_specifier(protocol: String, name: String) {
610651
dcm.remove_user_communication_specifier(&protocol, &ProtocolCommunicationSpecifier::Websocket(WebsocketSpecifier::new(&name)));
611652
}
612653

654+
pub fn add_serial_specifier(protocol: String, port: String, baud_rate: u32, data_bits: u8, stop_bits: u8, parity: String) {
655+
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
656+
dcm.add_user_communication_specifier(&protocol, &ProtocolCommunicationSpecifier::Serial(SerialSpecifier::new(&port, baud_rate, data_bits, stop_bits, parity.chars().next().unwrap())));
657+
}
658+
659+
pub fn remove_serial_specifier(protocol: String, port: String) {
660+
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
661+
dcm.remove_user_communication_specifier(&protocol, &ProtocolCommunicationSpecifier::Serial(SerialSpecifier::new_from_name(&port)));
662+
}
663+
613664
pub fn update_user_config(identifier: ExposedUserDeviceIdentifier, config: ExposedUserDeviceDefinition) {
614665
let dcm = DEVICE_CONFIG_MANAGER.try_read().expect("We should have a reader at this point");
615666
dcm.add_user_device_definition(&identifier.into(), &config.into());

intiface-engine-flutter-bridge/src/bridge_generated.io.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ pub extern "C" fn wire_setup_device_configuration_manager(
3636
}
3737

3838
#[no_mangle]
39-
pub extern "C" fn wire_get_user_communication_specifiers(port_: i64) {
40-
wire_get_user_communication_specifiers_impl(port_)
39+
pub extern "C" fn wire_get_user_websocket_communication_specifiers(port_: i64) {
40+
wire_get_user_websocket_communication_specifiers_impl(port_)
41+
}
42+
43+
#[no_mangle]
44+
pub extern "C" fn wire_get_user_serial_communication_specifiers(port_: i64) {
45+
wire_get_user_serial_communication_specifiers_impl(port_)
4146
}
4247

4348
#[no_mangle]
@@ -68,6 +73,30 @@ pub extern "C" fn wire_remove_websocket_specifier(
6873
wire_remove_websocket_specifier_impl(port_, protocol, name)
6974
}
7075

76+
#[no_mangle]
77+
pub extern "C" fn wire_add_serial_specifier(
78+
port_: i64,
79+
protocol: *mut wire_uint_8_list,
80+
port: *mut wire_uint_8_list,
81+
baud_rate: u32,
82+
data_bits: u8,
83+
stop_bits: u8,
84+
parity: *mut wire_uint_8_list,
85+
) {
86+
wire_add_serial_specifier_impl(
87+
port_, protocol, port, baud_rate, data_bits, stop_bits, parity,
88+
)
89+
}
90+
91+
#[no_mangle]
92+
pub extern "C" fn wire_remove_serial_specifier(
93+
port_: i64,
94+
protocol: *mut wire_uint_8_list,
95+
port: *mut wire_uint_8_list,
96+
) {
97+
wire_remove_serial_specifier_impl(port_, protocol, port)
98+
}
99+
71100
#[no_mangle]
72101
pub extern "C" fn wire_update_user_config(
73102
port_: i64,

intiface-engine-flutter-bridge/src/bridge_generated.rs

+85-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,24 @@ fn wire_setup_device_configuration_manager_impl(
110110
},
111111
)
112112
}
113-
fn wire_get_user_communication_specifiers_impl(port_: MessagePort) {
113+
fn wire_get_user_websocket_communication_specifiers_impl(port_: MessagePort) {
114114
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, Vec<(String, ExposedWebsocketSpecifier)>, _>(
115115
WrapInfo {
116-
debug_name: "get_user_communication_specifiers",
116+
debug_name: "get_user_websocket_communication_specifiers",
117117
port: Some(port_),
118118
mode: FfiCallMode::Normal,
119119
},
120-
move || move |task_callback| Result::<_, ()>::Ok(get_user_communication_specifiers()),
120+
move || move |task_callback| Result::<_, ()>::Ok(get_user_websocket_communication_specifiers()),
121+
)
122+
}
123+
fn wire_get_user_serial_communication_specifiers_impl(port_: MessagePort) {
124+
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, Vec<(String, ExposedSerialSpecifier)>, _>(
125+
WrapInfo {
126+
debug_name: "get_user_serial_communication_specifiers",
127+
port: Some(port_),
128+
mode: FfiCallMode::Normal,
129+
},
130+
move || move |task_callback| Result::<_, ()>::Ok(get_user_serial_communication_specifiers()),
121131
)
122132
}
123133
fn wire_get_user_device_definitions_impl(port_: MessagePort) {
@@ -177,6 +187,59 @@ fn wire_remove_websocket_specifier_impl(
177187
},
178188
)
179189
}
190+
fn wire_add_serial_specifier_impl(
191+
port_: MessagePort,
192+
protocol: impl Wire2Api<String> + UnwindSafe,
193+
port: impl Wire2Api<String> + UnwindSafe,
194+
baud_rate: impl Wire2Api<u32> + UnwindSafe,
195+
data_bits: impl Wire2Api<u8> + UnwindSafe,
196+
stop_bits: impl Wire2Api<u8> + UnwindSafe,
197+
parity: impl Wire2Api<String> + UnwindSafe,
198+
) {
199+
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, (), _>(
200+
WrapInfo {
201+
debug_name: "add_serial_specifier",
202+
port: Some(port_),
203+
mode: FfiCallMode::Normal,
204+
},
205+
move || {
206+
let api_protocol = protocol.wire2api();
207+
let api_port = port.wire2api();
208+
let api_baud_rate = baud_rate.wire2api();
209+
let api_data_bits = data_bits.wire2api();
210+
let api_stop_bits = stop_bits.wire2api();
211+
let api_parity = parity.wire2api();
212+
move |task_callback| {
213+
Result::<_, ()>::Ok(add_serial_specifier(
214+
api_protocol,
215+
api_port,
216+
api_baud_rate,
217+
api_data_bits,
218+
api_stop_bits,
219+
api_parity,
220+
))
221+
}
222+
},
223+
)
224+
}
225+
fn wire_remove_serial_specifier_impl(
226+
port_: MessagePort,
227+
protocol: impl Wire2Api<String> + UnwindSafe,
228+
port: impl Wire2Api<String> + UnwindSafe,
229+
) {
230+
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, (), _>(
231+
WrapInfo {
232+
debug_name: "remove_serial_specifier",
233+
port: Some(port_),
234+
mode: FfiCallMode::Normal,
235+
},
236+
move || {
237+
let api_protocol = protocol.wire2api();
238+
let api_port = port.wire2api();
239+
move |task_callback| Result::<_, ()>::Ok(remove_serial_specifier(api_protocol, api_port))
240+
},
241+
)
242+
}
180243
fn wire_update_user_config_impl(
181244
port_: MessagePort,
182245
identifier: impl Wire2Api<ExposedUserDeviceIdentifier> + UnwindSafe,
@@ -481,6 +544,25 @@ impl rust2dart::IntoIntoDart<ExposedDeviceFeatureSensor> for ExposedDeviceFeatur
481544
}
482545
}
483546

547+
impl support::IntoDart for ExposedSerialSpecifier {
548+
fn into_dart(self) -> support::DartAbi {
549+
vec![
550+
self.baud_rate.into_into_dart().into_dart(),
551+
self.data_bits.into_into_dart().into_dart(),
552+
self.stop_bits.into_into_dart().into_dart(),
553+
self.parity.into_into_dart().into_dart(),
554+
self.port.into_into_dart().into_dart(),
555+
]
556+
.into_dart()
557+
}
558+
}
559+
impl support::IntoDartExceptPrimitive for ExposedSerialSpecifier {}
560+
impl rust2dart::IntoIntoDart<ExposedSerialSpecifier> for ExposedSerialSpecifier {
561+
fn into_into_dart(self) -> Self {
562+
self
563+
}
564+
}
565+
484566
impl support::IntoDart for ExposedUserDeviceCustomization {
485567
fn into_dart(self) -> support::DartAbi {
486568
vec![

ios/Runner/bridge_generated.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ void wire_setup_device_configuration_manager(int64_t port_,
137137
struct wire_uint_8_list *base_config,
138138
struct wire_uint_8_list *user_config);
139139

140-
void wire_get_user_communication_specifiers(int64_t port_);
140+
void wire_get_user_websocket_communication_specifiers(int64_t port_);
141+
142+
void wire_get_user_serial_communication_specifiers(int64_t port_);
141143

142144
void wire_get_user_device_definitions(int64_t port_);
143145

@@ -151,6 +153,18 @@ void wire_remove_websocket_specifier(int64_t port_,
151153
struct wire_uint_8_list *protocol,
152154
struct wire_uint_8_list *name);
153155

156+
void wire_add_serial_specifier(int64_t port_,
157+
struct wire_uint_8_list *protocol,
158+
struct wire_uint_8_list *port,
159+
uint32_t baud_rate,
160+
uint8_t data_bits,
161+
uint8_t stop_bits,
162+
struct wire_uint_8_list *parity);
163+
164+
void wire_remove_serial_specifier(int64_t port_,
165+
struct wire_uint_8_list *protocol,
166+
struct wire_uint_8_list *port);
167+
154168
void wire_update_user_config(int64_t port_,
155169
struct wire_ExposedUserDeviceIdentifier *identifier,
156170
struct wire_ExposedUserDeviceDefinition *config);
@@ -197,11 +211,14 @@ static int64_t dummy_method_to_enforce_bundling(void) {
197211
dummy_var ^= ((int64_t) (void*) wire_stop_engine);
198212
dummy_var ^= ((int64_t) (void*) wire_send_backend_server_message);
199213
dummy_var ^= ((int64_t) (void*) wire_setup_device_configuration_manager);
200-
dummy_var ^= ((int64_t) (void*) wire_get_user_communication_specifiers);
214+
dummy_var ^= ((int64_t) (void*) wire_get_user_websocket_communication_specifiers);
215+
dummy_var ^= ((int64_t) (void*) wire_get_user_serial_communication_specifiers);
201216
dummy_var ^= ((int64_t) (void*) wire_get_user_device_definitions);
202217
dummy_var ^= ((int64_t) (void*) wire_get_protocol_names);
203218
dummy_var ^= ((int64_t) (void*) wire_add_websocket_specifier);
204219
dummy_var ^= ((int64_t) (void*) wire_remove_websocket_specifier);
220+
dummy_var ^= ((int64_t) (void*) wire_add_serial_specifier);
221+
dummy_var ^= ((int64_t) (void*) wire_remove_serial_specifier);
205222
dummy_var ^= ((int64_t) (void*) wire_update_user_config);
206223
dummy_var ^= ((int64_t) (void*) wire_remove_user_config);
207224
dummy_var ^= ((int64_t) (void*) wire_get_user_config_str);

0 commit comments

Comments
 (0)