1use crate::util::adb_utils::ScrCpyArgs;
2
3use super::{supplier::SupplierStateMin, DeviceFilterVec, *};
4use consumer::ConsumerStateMin;
5use marketmaker::MarketMakerMinState;
6
7#[derive(Serialize, Deserialize)]
10pub enum Request {
11 System(SysStateRequest),
12 MarketMaker(MarketMakerRequest),
13 Supplier(SupplierRequest),
14 Consumer(ConsumerRequest),
15}
16
17#[derive(Serialize, Deserialize)]
21pub enum SysStateRequest {
22 GetState,
23 GetPeerId,
24 SystemCheck,
25 SetAdbPath {
26 adb_path: String,
27 },
28 SetScrcpyPath {
29 scrcpy_path: String,
30 },
31 StartMarketMaker,
32 StartSupplier {
33 mm_host: String,
34 mm_port: u16,
35 name: Option<String>,
36 secure_comms: bool,
37 },
38 StartConsumer {
39 mm_host: String,
40 mm_port: u16,
41 name: Option<String>,
42 },
43 GetMarketMakerConfig,
44 GetSupplierConfig,
45 GetConsumerConfig,
46 Shutdown,
47
48 StopMarketMaker,
49 StopSupplier,
50 StopConsumer,
51 GetAdbVersionInfo,
52 GetScrcpyInfo,
53
54 SupplierMarketMakerTerminating,
55 ConsumerMarketMakerTerminating,
56}
57
58#[derive(Serialize, Deserialize, Debug)]
60pub enum SysStateResponse {
61 CurrentSysState {
62 state: SysStateMin,
63 },
64 PeerId {
65 peer_id: String,
66 },
67 SystemCheck {
68 supplier_check: SupplierCheck,
69 consumer_check: ConsumerCheck,
70 },
71 SetAdbPathSuccess,
72 SetAdbPathFailure {
73 reason: String,
74 },
75 SetScrcpyPathSuccess,
76 SetScrcpyPathFailure {
77 reason: String,
78 },
79 GetPeerIdFailure,
80 ShutDownSuccess,
81 ShutDownFailure,
82 StartMarketMakerSuccess,
83 StartMarketMakerFailed {
84 reason: String,
85 },
86 StartSupplierSuccess,
87 StartSupplierFailed {
88 reason: String,
89 },
90 StartConsumerSuccess,
91 StartConsumerFailed {
92 reason: String,
93 },
94 StopMarketMakerSuccess,
95 StopMarketMakerFailed,
96 StopSupplierSuccess,
97 StopSupplierFailed,
98 StopConsumerSuccess,
99 StopConsumerFailed,
100
101 TerminationAcknowledged,
102
103 AdbVersionInfo {
104 info: AdbVersionInfo,
105 },
106 ScrcpyInfo {
107 info: String,
108 },
109
110 RequestNotAllowed,
111 InvalidRequest {
112 request: String,
113 },
114}
115
116impl Display for SysStateResponse {
117 fn fmt(&self, f: &mut Formatter) -> Result {
118 match self {
119 SysStateResponse::CurrentSysState { state } => write!(f, "{}", state),
120 SysStateResponse::PeerId { peer_id } => write!(f, "PeerId: {}", peer_id),
121 SysStateResponse::GetPeerIdFailure => write!(f, "Failed to retrieve peer id"),
122 SysStateResponse::SystemCheck {
123 supplier_check,
124 consumer_check,
125 } => {
126 write!(
127 f,
128 "Consumer: {}\n\nSupplier: {}",
129 consumer_check, supplier_check
130 )
131 }
132 SysStateResponse::SetAdbPathSuccess => {
133 write!(f, "ADB path set successfully")
134 }
135 SysStateResponse::SetAdbPathFailure { reason } => {
136 write!(f, "Failed to set ADB path\n{}", reason)
137 }
138 SysStateResponse::SetScrcpyPathSuccess => {
139 write!(f, "SCRCPY path set successfully")
140 }
141 SysStateResponse::SetScrcpyPathFailure { reason } => {
142 write!(f, "Failed to set SCRCPY path: {}", reason)
143 }
144 SysStateResponse::ShutDownSuccess => write!(f, "Shutdown successful"),
145 SysStateResponse::ShutDownFailure => write!(f, "Shutdown failed"),
146 SysStateResponse::StartMarketMakerSuccess => {
147 write!(f, "MarketMaker started successfully")
148 }
149 SysStateResponse::StartMarketMakerFailed { reason } => {
150 write!(f, "MarketMaker failed to start: {}", reason)
151 }
152 SysStateResponse::StartSupplierSuccess => write!(f, "Supplier started successfully"),
153 SysStateResponse::StartSupplierFailed { reason } => {
154 write!(f, "Supplier failed to start:\n{}", reason)
155 }
156 SysStateResponse::StartConsumerSuccess => write!(f, "Consumer started successfully"),
157 SysStateResponse::StartConsumerFailed { reason } => {
158 write!(f, "Consumer failed to start:\n{}", reason)
159 }
160 SysStateResponse::StopMarketMakerSuccess => {
161 write!(f, "MarketMaker stopped successfully")
162 }
163 SysStateResponse::StopMarketMakerFailed => write!(f, "MarketMaker failed to stop"),
164 SysStateResponse::StopSupplierSuccess => write!(f, "Supplier stopped successfully"),
165 SysStateResponse::StopSupplierFailed => write!(f, "Supplier failed to stop"),
166 SysStateResponse::StopConsumerSuccess => write!(f, "Consumer stopped successfully"),
167 SysStateResponse::StopConsumerFailed => write!(f, "Consumer failed to stop"),
168 SysStateResponse::TerminationAcknowledged => write!(f, "Termination acknowledged"),
169 SysStateResponse::AdbVersionInfo { info } => write!(f, "{}", info),
170 SysStateResponse::ScrcpyInfo { info } => write!(f, "{}", info),
171 SysStateResponse::RequestNotAllowed => write!(f, "Request not allowed"),
172 SysStateResponse::InvalidRequest { request } => {
173 write!(f, "Invalid request: {}", request)
174 }
175 }
176 }
177}
178
179#[derive(Serialize, Deserialize, Debug)]
181pub enum MarketMakerRequest {
182 Status,
184 Terminate,
185 UseWhitelist,
186 ResetWhitelist,
187 WhitelistSupplier {
188 key: String,
189 },
190 WhitelistConsumer {
191 key: String,
192 },
193 UnwhitelistSupplier {
194 key: String,
195 },
196 UnwhitelistConsumer {
197 key: String,
198 },
199
200 SupplierConnect {
202 supplier: SupplierSpec,
203 },
204 SupplyDevices {
205 devices: Vec<DeviceSpec>,
206 },
207 SupplierHeartBeat,
208 SupplierDisconnect,
209 ReclaimDevice {
210 device_id: String,
211 force: bool,
212 },
213
214 ConsumerConnect {
216 consumer: ConsumerSpec,
217 },
218 ReserveDevice {
219 device_id: String,
220 },
221 ReleaseDevice {
222 device_id: String,
223 },
224 ConsumerHeartBeat,
225 ConsumerDisconnect,
226 GetAvailableDevices,
227 GetDevicesByFilter {
228 filter_vec: DeviceFilterVec,
229 },
230 ReleaseAllDevices,
231 StartScrcpyTunnel {
232 device_id: String,
237 supplier_id: String,
238 port: u16,
239 scrcpy_port: u16,
240 },
241
242 Test,
244 UpdateDevices {
245 devices: Vec<DeviceSpec>,
246 }, }
248
249#[derive(Serialize, Deserialize, Debug)]
251pub enum MarketMakerResponse {
252 Test,
254 Status {
255 state: MarketMakerMinState,
256 },
257 ShutDownSuccess,
258 ShutDownFailure {
259 reason: String,
260 },
261 UseWhitelistSuccess,
262 ResetWhitelistSuccess,
263
264 WhitelistSupplierSuccess,
265 WhitelistSupplierFailure {
266 reason: String,
267 },
268 WhitelistConsumerSuccess,
269 WhitelistConsumerFailure {
270 reason: String,
271 },
272 UnwhitelistSupplierSuccess,
273 UnwhitelistSupplierFailure {
274 reason: String,
275 },
276 UnwhitelistConsumerSuccess,
277 UnwhitelistConsumerFailure {
278 reason: String,
279 },
280
281 SupplierConnected {
283 supplier: SupplierSpec,
284 pub_key: String,
285 },
286 SupplierNotConnected {
287 reason: String,
288 },
289 DevicesSupplied {
290 supplied_devices: Vec<DeviceSpec>,
291 failed_devices: Vec<DeviceSpec>,
292 },
293 DeviceReclaimed {
294 device_id: String,
295 },
296 SupplierDisconnected,
297 DeviceBeingUsed {
298 device_id: String,
299 },
300 DeviceNotReclaimed {
301 reason: String,
302 },
303
304 ConsumerConnected {
306 consumer: ConsumerSpec,
307 pub_key: String,
308 },
309 ConsumerNotConnected {
310 reason: String,
311 },
312 DeviceReserved {
313 device: DeviceSpec,
314 peer_id: Option<String>,
315 },
316 DeviceNotReserved {
317 reason: String,
318 },
319 DeviceReleased,
320 DeviceNotReleased {
321 reason: String,
322 },
323 ConsumerDisconnected,
324 AvailableDevices {
325 devices: Vec<DeviceSpec>,
326 },
327 DevicesByFilter {
328 devices: Vec<DeviceSpec>,
329 filter_vec: DeviceFilterVec,
330 },
331 ErrorGettingDevices {
332 reason: String,
333 },
334 AllDeviceReleaseSuccess,
335 AllDeviceReleaseFailure {
336 reason: String,
337 },
338 ScrcpyTunnelSuccess,
339 ScrcpyTunnelFailure {
340 reason: String,
341 },
342
343 HeartBeatResponse,
345 RequestNotAllowed,
346 InvalidRequest {
347 request: String,
348 },
349}
350
351impl Display for MarketMakerResponse {
352 fn fmt(&self, f: &mut Formatter) -> Result {
353 match self {
354 MarketMakerResponse::Test => write!(f, "Test"),
355 MarketMakerResponse::Status { state } => write!(f, "{}", state),
356 MarketMakerResponse::ShutDownSuccess => write!(f, "Termination success"),
357 MarketMakerResponse::ShutDownFailure { reason } => {
358 write!(f, "Termination failure: {}", reason)
359 }
360
361 MarketMakerResponse::UseWhitelistSuccess => write!(f, "Whitelist enabled"),
362 MarketMakerResponse::ResetWhitelistSuccess => write!(f, "Whitelist disabled"),
363 MarketMakerResponse::WhitelistSupplierSuccess => {
364 write!(f, "Whitelist supplier success")
365 }
366 MarketMakerResponse::WhitelistSupplierFailure { reason } => {
367 write!(f, "Whitelist supplier failure: {}", reason)
368 }
369 MarketMakerResponse::WhitelistConsumerSuccess => {
370 write!(f, "Whitelist consumer success")
371 }
372 MarketMakerResponse::WhitelistConsumerFailure { reason } => {
373 write!(f, "Whitelist consumer failure: {}", reason)
374 }
375 MarketMakerResponse::UnwhitelistSupplierSuccess => {
376 write!(f, "Unwhitelist supplier success")
377 }
378 MarketMakerResponse::UnwhitelistSupplierFailure { reason } => {
379 write!(f, "Unwhitelist supplier failure: {}", reason)
380 }
381 MarketMakerResponse::UnwhitelistConsumerSuccess => {
382 write!(f, "Unwhitelist consumer success")
383 }
384 MarketMakerResponse::UnwhitelistConsumerFailure { reason } => {
385 write!(f, "Unwhitelist consumer failure: {}", reason)
386 }
387
388 MarketMakerResponse::SupplierConnected { supplier, .. } => {
389 write!(f, "Supplier connected:\n{}", supplier)
390 }
391 MarketMakerResponse::SupplierNotConnected { reason } => {
392 write!(f, "Supplier not connected: {}", reason)
393 }
394 MarketMakerResponse::DevicesSupplied {
395 supplied_devices,
396 failed_devices: _,
397 } => {
398 writeln!(f, "Devices supplied:").unwrap();
399 for d in supplied_devices {
400 writeln!(f, "{}", d).unwrap();
401 }
402 write!(f, "")
403 }
404 MarketMakerResponse::DeviceReclaimed { device_id } => {
405 write!(f, "Device reclaimed: {}", device_id)
406 }
407 MarketMakerResponse::SupplierDisconnected => write!(f, "Supplier disconnected"),
408 MarketMakerResponse::DeviceBeingUsed { device_id } => {
409 write!(f, "Device being used: {}", device_id)
410 }
411 MarketMakerResponse::DeviceNotReclaimed { reason } => {
412 write!(f, "Device not reclaimed: {}", reason)
413 }
414
415 MarketMakerResponse::ConsumerConnected { consumer, .. } => {
416 write!(f, "Consumer connected:\n{}", consumer)
417 }
418 MarketMakerResponse::ConsumerNotConnected { reason } => {
419 write!(f, "Consumer not connected: {}", reason)
420 }
421 MarketMakerResponse::DeviceReserved { device, peer_id: _ } => {
422 write!(f, "Device reserved:\n{}", device)
423 }
424 MarketMakerResponse::DeviceNotReserved { reason } => {
425 write!(f, "Device not reserved: {}", reason)
426 }
427 MarketMakerResponse::DeviceReleased => write!(f, "Device released"),
428 MarketMakerResponse::DeviceNotReleased { reason } => {
429 write!(f, "Device not released: {}", reason)
430 }
431 MarketMakerResponse::ConsumerDisconnected => write!(f, "Consumer disconnected"),
432 MarketMakerResponse::AvailableDevices { devices } => {
433 writeln!(f, "Available devices:").unwrap();
434 for d in devices {
435 writeln!(f, "{}", d).unwrap();
436 }
437 write!(f, "")
438 }
439 MarketMakerResponse::DevicesByFilter {
440 devices,
441 filter_vec,
442 } => {
443 writeln!(f, "Devices by filter:").unwrap();
444 writeln!(f, "{}", filter_vec).unwrap();
445 for d in devices {
446 writeln!(f, "{}", d).unwrap();
447 }
448 write!(f, "")
449 }
450 MarketMakerResponse::ErrorGettingDevices { reason } => {
451 write!(f, "Error getting devices: {}", reason)
452 }
453 MarketMakerResponse::AllDeviceReleaseSuccess => {
454 write!(f, "All devices released successfully")
455 }
456 MarketMakerResponse::AllDeviceReleaseFailure { reason } => {
457 write!(f, "All devices not released: {}", reason)
458 }
459 MarketMakerResponse::ScrcpyTunnelSuccess => {
460 write!(f, "Scrcpy tunnel started successfully")
461 }
462 MarketMakerResponse::ScrcpyTunnelFailure { reason } => {
463 write!(f, "Scrcpy tunnel failure: {}", reason)
464 }
465
466 MarketMakerResponse::HeartBeatResponse => write!(f, "HeartBeatResponse"),
467 MarketMakerResponse::RequestNotAllowed => write!(f, "Request not allowed"),
468 MarketMakerResponse::InvalidRequest { request } => {
469 write!(f, "Invalid request: {}", request)
470 }
471 }
472 }
473}
474
475#[derive(Serialize, Deserialize, Debug)]
477pub enum SupplierRequest {
478 Test,
480 Status,
481 SupplyDevices {
482 devices: Option<Vec<String>>,
483 },
484 ReclaimDevice {
485 device_id: String,
486 force: bool,
487 },
488
489 MarketMakerTerminating,
491 StartSecureTunnel {
492 device_id: String,
493 port: u16,
494 pub_key: String,
495 },
496 StopSecureTunnel {
497 device_id: String,
498 },
499 StartScrcpyTunnel {
500 device_id: String,
511 peer_id: String,
512 consumer_host: String,
513 port: u16,
514 scrcpy_port: u16,
515 },
516}
517
518#[derive(Serialize, Deserialize, Debug)]
520pub enum SupplierResponse {
521 Test,
522 Status {
523 state: SupplierStateMin,
524 },
525 DevicesSupplied {
526 supplied_devices: Vec<DeviceSpec>,
527 failed_devices: Vec<DeviceSpec>,
528 },
529 DeviceSupplyFailure {
530 reason: String,
531 },
532 TerminationAcknowledged,
533 DeviceReclaimed {
534 device_id: String,
535 },
536 DeviceNotReclaimed {
537 reason: String,
538 },
539 SecureTunnelStarted {
540 port: u16,
541 },
542 SecureTunnelStartFailure {
543 reason: String,
544 },
545 SecureTunnelStopped,
546 ScrcpyTunnelSuccess,
547 ScrcpyTunnelFailure {
548 reason: String,
549 },
550
551 RequestNotAllowed,
552 InvalidRequest {
553 request: String,
554 },
555}
556
557impl Display for SupplierResponse {
558 fn fmt(&self, f: &mut Formatter) -> Result {
559 match self {
560 SupplierResponse::Test => write!(f, "Test"),
561 SupplierResponse::Status { state } => write!(f, "{}", state),
562 SupplierResponse::DevicesSupplied {
563 supplied_devices,
564 failed_devices,
565 } => {
566 if !supplied_devices.is_empty() {
567 writeln!(f, "Devices supplied:").unwrap();
568 for d in supplied_devices {
569 writeln!(f, "{}", d).unwrap();
570 }
571 }
572 if !failed_devices.is_empty() {
573 writeln!(
574 f,
575 "\nFailed to supply some devices because of duplicate device_id(s)"
576 )
577 .unwrap();
578 writeln!(f, "Devices failed to supply:").unwrap();
579 for d in failed_devices {
580 writeln!(f, "{}", d).unwrap();
581 }
582 }
583 write!(f, "")
584 }
585 SupplierResponse::DeviceSupplyFailure { reason } => {
586 write!(f, "Device supply failure: {}", reason)
587 }
588 SupplierResponse::TerminationAcknowledged => write!(f, "Termination acknowledged"),
589 SupplierResponse::DeviceReclaimed { device_id } => {
590 write!(f, "Device reclaimation success: {}", device_id)
591 }
592 SupplierResponse::DeviceNotReclaimed { reason } => {
593 write!(f, "Device reclaimation failure: {}", reason)
594 }
595 SupplierResponse::SecureTunnelStarted { port } => {
596 write!(f, "Secure tunnel started on: {}", port)
597 }
598 SupplierResponse::SecureTunnelStartFailure { reason } => {
599 write!(f, "Secure tunnel start failure: {}", reason)
600 }
601 SupplierResponse::SecureTunnelStopped => write!(f, "Secure tunnel stopped"),
602 SupplierResponse::ScrcpyTunnelSuccess => {
603 write!(f, "Scrcpy tunnel started successfully")
604 }
605 SupplierResponse::ScrcpyTunnelFailure { reason } => {
606 write!(f, "Scrcpy tunnel start failure: {}", reason)
607 }
608 SupplierResponse::RequestNotAllowed => write!(f, "Request not allowed"),
609 SupplierResponse::InvalidRequest { request } => {
610 write!(f, "Invalid request: {}", request)
611 }
612 }
613 }
614}
615
616#[derive(Serialize, Deserialize, Debug)]
618pub enum ConsumerRequest {
619 Test,
621 Status,
622 GetAvailableDevices,
623 GetDevicesByFilter {
624 filter_vec: DeviceFilterVec,
625 },
626 ReserveDevice {
627 device_id: String,
628 no_use: bool,
629 },
630 ReleaseDevice {
631 device_id: String,
632 },
633 ReleaseAllDevices,
634 UseDevice {
635 device_id: String,
636 },
637 StartScrCpy {
638 device_id: String,
639 max_size: Option<u16>,
640 bit_rate: Option<u32>,
641 max_fps: Option<u8>,
642 },
643 SetScrCpyDefaults {
644 max_fps: Option<u8>,
645 max_size: Option<u16>,
646 bit_rate: Option<u32>,
647 },
648
649 MarketMakerTerminating,
651 SupplierDisconnected {
652 device_id: String,
653 },
654}
655
656#[derive(Serialize, Deserialize, Debug)]
658pub enum ConsumerResponse {
659 Test,
660 Status {
661 state: ConsumerStateMin,
662 },
663 TerminationAcknowledged,
664 AvailableDevices {
665 devices: Vec<DeviceSpec>,
666 },
667 DevicesByFilter {
668 devices: Vec<DeviceSpec>,
669 filter_vec: DeviceFilterVec,
670 },
671 DeviceReserved {
672 device: DeviceSpec,
673 },
674 DeviceNotReserved {
675 reason: String,
676 },
677 DeviceReleased {
678 device_id: String,
679 },
680 DeviceNotReleased {
681 reason: String,
682 },
683 AllDeviceReleaseSuccess,
684 AllDeviceReleaseFailure {
685 reason: String,
686 },
687 UseDeviceSuccess {
688 device_id: String,
689 },
690 UseDeviceFailure {
691 reason: String,
692 },
693 StartScrCpySuccess {
694 device_id: String,
695 },
696 StartScrCpyFailure {
697 reason: String,
698 },
699 ScrCpyDefaultsSet {
700 args: ScrCpyArgs,
701 },
702
703 ErrorGettingDevices {
704 reason: String,
705 },
706 RequestNotAllowed,
707 InvalidRequest {
708 request: String,
709 },
710}
711
712impl Display for ConsumerResponse {
713 fn fmt(&self, f: &mut Formatter) -> Result {
714 match self {
715 ConsumerResponse::Test => write!(f, "Test"),
716 ConsumerResponse::Status { state } => write!(f, "{}", state),
717 ConsumerResponse::TerminationAcknowledged => write!(f, "Termination acknowledged"),
718 ConsumerResponse::AvailableDevices { devices } => {
719 writeln!(f, "Available devices:").unwrap();
720 for d in devices {
721 writeln!(f, "{}", d).unwrap();
722 }
723 write!(f, "")
724 }
725 ConsumerResponse::DevicesByFilter {
726 devices,
727 filter_vec,
728 } => {
729 writeln!(f, "Devices filtered by:").unwrap();
730 writeln!(f, "{}", filter_vec).unwrap();
731 if devices.is_empty() {
732 writeln!(f, "No devices found").unwrap();
733 } else {
734 writeln!(f, "Devices found:\n").unwrap();
735 for d in devices {
736 writeln!(f, "{}", d).unwrap();
737 }
738 }
739 write!(f, "")
740 }
741 ConsumerResponse::DeviceReserved { device } => {
742 write!(f, "Device reserved:\n{}", device)
743 }
744 ConsumerResponse::DeviceNotReserved { reason } => {
745 write!(f, "Device not reserved: {}", reason)
746 }
747 ConsumerResponse::DeviceReleased { device_id } => {
748 write!(f, "Device released: {}", device_id)
749 }
750 ConsumerResponse::DeviceNotReleased { reason } => {
751 write!(f, "Device not released: {}", reason)
752 }
753 ConsumerResponse::AllDeviceReleaseSuccess => {
754 write!(f, "All devices released successfully")
755 }
756 ConsumerResponse::AllDeviceReleaseFailure { reason } => {
757 write!(f, "Error releasing devices: {}", reason)
758 }
759 ConsumerResponse::UseDeviceSuccess { device_id } => {
760 write!(f, "Default device switched successfully to: {}", device_id)
761 }
762 ConsumerResponse::UseDeviceFailure { reason } => {
763 write!(f, "Error switching default device: {}", reason)
764 }
765 ConsumerResponse::StartScrCpySuccess { device_id } => {
766 write!(
767 f,
768 "Started screen mirroring successfully for device: {}",
769 device_id
770 )
771 }
772 ConsumerResponse::StartScrCpyFailure { reason } => {
773 write!(f, "Screen mirroring failed: {}", reason)
774 }
775 ConsumerResponse::ScrCpyDefaultsSet { args } => {
776 write!(f, "ScrCpy defaults set:\n{}", args)
777 }
778 ConsumerResponse::ErrorGettingDevices { reason } => {
779 write!(f, "Error getting devices: {}", reason)
780 }
781 ConsumerResponse::RequestNotAllowed => write!(f, "Request not allowed"),
782 ConsumerResponse::InvalidRequest { request } => {
783 write!(f, "Invalid request: {}", request)
784 }
785 }
786 }
787}