hid_io_core/module/daemonnode.rs
1/* Copyright (C) 2020 by Jacob Alexander
2 *
3 * This file is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This file is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this file. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17use crate::api::common_capnp;
18/// HID-IO Core Daemon Node
19/// Handles API queries directly to HID-IO Core rather than to a specific device
20/// This is the standard way to interact with HID-IO Core modules from the capnp API
21///
22/// For the most part, this is a dummy node used mainly for node accounting with the mailbox.
23/// The capnproto API should call the internal functions directly if possible.
24use crate::api::Endpoint;
25use crate::mailbox;
26
27pub struct DaemonNode {
28 mailbox: mailbox::Mailbox,
29 uid: u64,
30 _endpoint: Endpoint,
31}
32
33impl DaemonNode {
34 pub fn new(mailbox: mailbox::Mailbox) -> std::io::Result<DaemonNode> {
35 // Assign a uid
36 let uid = match mailbox.clone().assign_uid("".to_string(), "".to_string()) {
37 Ok(uid) => uid,
38 Err(e) => {
39 panic!("Only 1 daemon node may be allocated: {e}");
40 }
41 };
42
43 // Setup Endpoint
44 let mut endpoint = Endpoint::new(common_capnp::NodeType::HidioDaemon, uid);
45 endpoint.set_daemonnode_params();
46
47 // Register node
48 mailbox.clone().register_node(endpoint.clone());
49
50 Ok(DaemonNode {
51 mailbox,
52 uid,
53 _endpoint: endpoint,
54 })
55 }
56}
57
58impl Drop for DaemonNode {
59 fn drop(&mut self) {
60 warn!("DaemonNode deallocated");
61
62 // Unregister node
63 self.mailbox.unregister_node(self.uid);
64 }
65}
66
67pub async fn initialize(mailbox: mailbox::Mailbox) {
68 // Event loop for Daemon Node (typically not used)
69 tokio::spawn(async {
70 let node = DaemonNode::new(mailbox).unwrap();
71 info!("Initializing daemon node... uid:{}", node.uid);
72 loop {
73 tokio::time::sleep(std::time::Duration::from_millis(100)).await;
74 }
75 });
76}