1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use ffi::utils::res_to_result;
use libc::c_void;
use std::ffi::CString;
use notification::{Notification, ExternNotification};
use options::Options;
use ffi::manager as extern_manager;
use value_classes::value_id::ValueID;
use error::{ Error, Result };

pub struct Manager {
    pub ptr: *mut extern_manager::Manager,
    options: Options,
    watchers: Vec<Option<Box<WatcherWrapper>>>
}

unsafe impl Send for Manager {}
unsafe impl Sync for Manager {}

pub trait NotificationWatcher: Sync {
    fn on_notification(&self, &Notification);
}

struct WatcherWrapper {
    watcher: Box<NotificationWatcher>
}

// watcher is actually a Box<WatcherWrapper>
extern "C" fn watcher_cb(notification: *const ExternNotification, watcher: *const c_void) {
    let watcher_wrapper: &WatcherWrapper = unsafe { &*(watcher as *const WatcherWrapper) };
    let notification = Notification::new(notification);
    watcher_wrapper.watcher.on_notification(&notification);
}

impl Manager {
    pub fn create(mut options: Options) -> Result<Manager> {
        try!(options.lock());
        let external_manager = unsafe { extern_manager::manager_create() };
        if external_manager.is_null() {
            Err(Error::OptionsAreNotLocked("Manager::create"))
        } else {
            Ok(Manager {
                ptr: external_manager,
                options: options,
                watchers: Vec::with_capacity(1)
            })
        }
    }

    /* disable for now, we don't need this anywhere yet
    pub fn get() -> Option<Manager> {
        let external_manager = unsafe { extern_manager::manager_get() };
        if external_manager.is_null() {
            None
        } else {
            Some(Manager { ptr: external_manager, options: Options::get().unwrap() })
        }
    }
    */

    pub fn add_node(&self, home_id:u32, secure: bool) -> Result<()> {
        res_to_result(unsafe {
            extern_manager::manager_add_node(self.ptr, home_id, secure)
        }).or(Err(Error::InvalidParameter("home_id", "Manager::add_node")))
    }

    pub fn remove_node(&self, home_id:u32) -> Result<()> {
        res_to_result(unsafe {
            extern_manager::manager_remove_node(self.ptr, home_id)
        }).or(Err(Error::InvalidParameter("home_id", "Manager::remove_node")))
    }

    pub fn test_network(&self, home_id:u32, count:u32) {
        unsafe {
            extern_manager::test_network(self.ptr, home_id, count);
        }
    }

    pub fn test_network_node(&self, home_id:u32, node_id:u8, count:u32) {
        unsafe {
            extern_manager::test_network_node(self.ptr, home_id, node_id, count);
        }
    }

    pub fn heal_network(&self, home_id:u32, do_rr:bool) {
        unsafe {
            extern_manager::heal_network(self.ptr, home_id, do_rr);
        }
    }

    pub fn heal_network_node(&self, home_id:u32, node_id:u8, do_rr:bool) {
        unsafe {
            extern_manager::heal_network_node(self.ptr, home_id, node_id, do_rr);
        }
    }

    pub fn add_watcher<T: 'static + NotificationWatcher>(&mut self, watcher: T) -> Result<usize> {
        let watcher_wrapper = Box::new(WatcherWrapper { watcher: Box::new(watcher) });

        let watcher_ptr: *const c_void = &*watcher_wrapper as *const _ as *const c_void;
        let api_res = unsafe {
            extern_manager::manager_add_watcher(self.ptr, watcher_cb, watcher_ptr)
        };

        if api_res {
            let position = self.watchers.len();
            self.watchers.push(Some(watcher_wrapper));
            Ok(position)
        } else {
            Err(Error::APIError("Could not add a watcher: it's already added"))
        }
    }

    pub fn remove_watcher(&mut self, position: usize) -> Result<()> {
        let wrapper = self.watchers[position].take();

        if let Some(mut wrapper) = wrapper {
            let result = self.remove_watcher_impl(&mut wrapper);
            if result.is_err() {
                // put the watcher back to the vec
                self.watchers[position] = Some(wrapper);
            }
            result
        } else {
            Err(Error::APIError("Could not find the watcher to remove"))
        }
    }

    fn remove_watcher_impl(&self, wrapper: &mut WatcherWrapper) -> Result<()> {
        let watcher_ptr: *mut c_void = wrapper as *mut _ as *mut c_void;
        res_to_result(unsafe {
            extern_manager::manager_remove_watcher(self.ptr, watcher_cb, watcher_ptr)
        }).or(Err(Error::APIError("Could not remove a watcher as it was not added or already removed")))
    }

    pub fn add_driver(&mut self, device: &str) -> Result<()> {
        let device = CString::new(device).unwrap();
        res_to_result(unsafe {
            extern_manager::manager_add_driver(self.ptr, device.as_ptr(), &extern_manager::ControllerInterface::ControllerInterface_Serial)
        }).or(Err(Error::APIError("Could not add the driver as it is already added")))
    }

    pub fn add_usb_driver(&mut self) -> Result<()> {
        let device = CString::new("HID Controller").unwrap();
        res_to_result(unsafe {
            extern_manager::manager_add_driver(self.ptr, device.as_ptr(), &extern_manager::ControllerInterface::ControllerInterface_Hid)
        }).or(Err(Error::APIError("Could not add the driver as it is already added")))
    }

    pub fn remove_driver(&mut self, device: &str) -> Result<()> {
        let device = CString::new(device).unwrap();
        res_to_result(unsafe {
            extern_manager::manager_remove_driver(self.ptr, device.as_ptr())
        }).or(Err(Error::APIError("Could not remove the driver as it was not added or already removed")))
    }

    pub fn remove_usb_driver(&mut self) -> Result<()> {
        let device = CString::new("HID Controller").unwrap();
        res_to_result(unsafe {
            extern_manager::manager_remove_driver(self.ptr, device.as_ptr())
        }).or(Err(Error::APIError("Could not remove the driver as it was not added or already removed")))
    }

    pub fn get_poll_interval(&self) -> i32 {
        unsafe {
            extern_manager::manager_get_poll_interval(self.ptr)
        }
    }

    pub fn set_poll_interval(&self, interval_ms: i32, is_between_each_poll: bool) {
        unsafe {
            extern_manager::manager_set_poll_interval(self.ptr, interval_ms, is_between_each_poll)
        }
    }

    pub fn enable_poll_with_intensity(&self, vid: &ValueID, intensity: u8) -> bool {
        unsafe {
            extern_manager::manager_enable_poll_with_intensity(self.ptr, &vid.as_ozw_vid(), intensity)
        }
    }

    pub fn enable_poll(&self, vid: &ValueID) -> bool {
        unsafe {
            extern_manager::manager_enable_poll(self.ptr, &vid.as_ozw_vid())
        }
    }

    pub fn disable_poll(&self, vid: &ValueID) -> bool {
        unsafe {
            extern_manager::manager_disable_poll(self.ptr, &vid.as_ozw_vid())
        }
    }

    pub fn is_polled(&self, vid: &ValueID) -> bool {
        unsafe {
            extern_manager::manager_is_polled(self.ptr, &vid.as_ozw_vid())
        }
    }

    pub fn set_poll_intensity(&self, vid: &ValueID, intensity: u8) {
        unsafe {
            extern_manager::manager_set_poll_intensity(self.ptr, &vid.as_ozw_vid(), intensity)
        }
    }

    pub fn get_poll_intensity(&self, vid: &ValueID) -> u8 {
        unsafe {
            extern_manager::manager_get_poll_intensity(self.ptr, &vid.as_ozw_vid())
        }
    }
}

impl Drop for Manager {
    fn drop(&mut self) {
        let watchers: Vec<_> = self.watchers.drain(..).collect();
        for watcher in watchers {
            if let Some(mut watcher) = watcher {
                self.remove_watcher_impl(&mut watcher).unwrap();
            }
        }

        unsafe { extern_manager::manager_destroy() }
    }
}