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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#![doc(html_root_url = "https://docs.rs/uhppote-rs/1/")]
//! uhppote-rs is a safe Rust library for access control systems based on the UHPPOTE UT0311-L0x
//! TCP/IP Wiegand access control boards. This library is based on the
//! [uhppoted](https://github.com/uhppoted/uhppoted) project.
//!
//! Most interactions with the system happen through the [`Device`] type.
//!
//! Example:
//! ```no_run
//! use uhppote_rs::Uhppote;
//! let uhppoted = Uhppoted::default();
//! let device = uhppoted.get_device(423196779).unwrap();
//! let status = device.get_status().unwrap();
//! ```
mod messages;
mod types;
use anyhow::bail;
use anyhow::Result;
use chrono::Datelike;
pub use chrono::NaiveDate;
pub use chrono::NaiveDateTime;
pub use chrono::NaiveTime;
use messages::types::DateBCD;
use messages::*;
use std::fmt::Debug;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::net::UdpSocket;
use std::time::Duration;
pub use types::*;

const UHPPOTE_PORT: u16 = 60000;
#[derive(Debug)]
pub struct Uhppoted {
    bind_address: SocketAddr,
    broadcast_address: Ipv4Addr,
    timeout: Duration,
}

impl Uhppoted {
    /// Create a new Uhppote struct
    ///
    /// Example:
    /// ```no_run
    /// use uhppote_rs::Uhppoted;
    /// let uhppoted = UUhppoted::new(
    ///     "0.0.0.0:0".parse().unwrap(),
    ///     "255.255.255.255".parse().unwrap(),
    ///     Duration::new(5, 0),
    ///     Vec::new(),
    ///     false,
    /// )
    pub fn new(bind: SocketAddr, broadcast: Ipv4Addr, timeout: Duration) -> Uhppoted {
        Uhppoted {
            bind_address: bind,
            broadcast_address: broadcast,
            timeout,
        }
    }

    /// Get all the available [`DeviceConfig`]s on the local network. This broadcasts a discovery message
    /// and waits [`Uhppoted::timeout`] for responses.
    pub fn get_device_configs(&self) -> Result<Vec<DeviceConfig>> {
        let request = GetConfigRequest::new(0);
        let response: Vec<GetConfigResponse> = broadcast_and_receive(request, self)?;
        let r = response
            .into_iter()
            .map(|r| r.try_into().unwrap())
            .collect();
        Ok(r)
    }

    /// Get all the available [`Device`]s on the local network. This broadcasts a discovery message
    /// and waits [`Uhppoted::timeout`] for responses.
    pub fn get_devices(&self) -> Result<Vec<Device>> {
        let request = GetConfigRequest::new(0);
        let response: Vec<GetConfigResponse> = broadcast_and_receive(request, self)?;
        let r = response
            .into_iter()
            .map(|r| Device::new(self, r.device_id, Some(r.ip_address)))
            .collect();
        Ok(r)
    }

    /// Get a [`Device`] by its device ID. This does not check if the device actually exists, but
    /// merely represents a device to interact with.
    ///
    /// When `ip_address` is specified, communication will happen directly with the device. Otherwise,
    /// communication to the device will happen via local network broadcast.
    ///
    /// Specify an `ip_address` when the device is not on the local network.
    pub fn get_device(&self, id: u32, ip_address: Option<Ipv4Addr>) -> Device {
        Device::new(self, id, ip_address)
    }

    /// Listen for incoming [`Status`] messages from the UHPPOTE system on a specific `address`.
    /// Example:
    /// ```no_run
    /// use uhppote_rs::Uhppoted;
    /// let uhppoted = Uhppoted::default();
    /// let device = uhppoted.get_device(423196779);
    ///
    /// let listener_address: SocketAddr = "192.168.0.10:12345".parse().unwrap();
    ///
    /// device.set_listener(listener_address).unwrap();
    /// uhppoted.listen(listener_address, |status| {
    ///     println!("{:?}", status);
    /// });
    /// ```
    pub fn listen(&self, address: SocketAddr, handler: fn(Status)) -> Result<()> {
        let socket = UdpSocket::bind(&address)?;
        socket.set_broadcast(true)?;
        socket.set_read_timeout(None)?;
        loop {
            let mut buf = [0u8; 64];
            socket.recv(&mut buf)?;
            match buf[1].try_into()? {
                RequestResponseType::Status => {
                    let response = GetStatusResponse::from_bytes(&buf)?;
                    handler(response.try_into()?);
                }
                response_type => bail!("Can't listen for {:?}", response_type),
            }
        }
    }
}

impl Default for Uhppoted {
    /// Creates a default instance of [`Uhppoted`].
    /// Defaults:
    ///   - `bind`: 0.0.0.0:0
    ///   - `broadcast`: 255.255.255.255
    ///   - `listen`: None
    ///   - `timeout`: Duration::new(5, 0)
    ///   - `controllers`: None
    fn default() -> Uhppoted {
        Uhppoted::new(
            "0.0.0.0:0".parse().unwrap(),
            "255.255.255.255".parse().unwrap(),
            Duration::new(5, 0),
        )
    }
}

#[derive(Debug)]
pub struct Device<'a> {
    u: &'a Uhppoted,
    id: u32,
    ip_address: Option<Ipv4Addr>,
}

impl<'a> Device<'a> {
    /// Create a new [`Device`] from an [`Uhppoted`] and a device ID.
    fn new(u: &'a Uhppoted, id: u32, ip_address: Option<Ipv4Addr>) -> Device<'a> {
        Device { u, id, ip_address }
    }

    /// Add a [`Card`] to the [`Device`].
    pub fn add_card(&self, card: Card) -> Result<()> {
        let request = PutCardRequest::new(
            self.id,
            card.number,
            card.from.try_into()?,
            card.to.try_into()?,
            card.doors[0],
            card.doors[1],
            card.doors[2],
            card.doors[3],
        );
        let response: PutCardResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("PutCard failed")
        }
    }

    /// Add a [`Task`] to the system.
    pub fn add_task(&self, task: Task) -> Result<()> {
        let request = AddTaskRequest::new(
            self.id,
            DateBCD::new(
                task.from.year() as u16,
                task.from.month() as u8,
                task.from.day() as u8,
            ),
            DateBCD::new(
                task.to.year() as u16,
                task.to.month() as u8,
                task.to.day() as u8,
            ),
            task.monday,
            task.tuesday,
            task.wednesday,
            task.thursday,
            task.friday,
            task.saturday,
            task.sunday,
            task.at.try_into()?,
            task.door,
            task.task as u8,
            task.more_cards,
        );

        let response: AddTaskResponse = send_and_receive(request, self)?;

        if response.success {
            Ok(())
        } else {
            bail!("AddTask failed")
        }
    }

    /// Remove all [`Card`]s from the [`Device`].
    pub fn clear_cards(&self) -> Result<()> {
        let magic_word = 0x55aaaa55;
        let request = DeleteCardsRequest::new(self.id, magic_word);
        let response: DeleteCardsResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("DeleteCard failed")
        }
    }

    /// Remove all [`Task`]s from the [`Device`].
    pub fn clear_tasks(&self) -> Result<()> {
        let request = ClearTaskListRequest::new(self.id, 0x55aaaa55);
        let response: ClearTaskListResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("ClearTaskList failed")
        }
    }

    /// Remove all [`TimeProfile`]s from the [`Device`].
    pub fn clear_time_profiles(&self) -> Result<()> {
        let magic_word = 0x55aaaa55;
        let request = ClearTimeProfilesRequest::new(self.id, magic_word);
        let response: ClearTimeProfilesResponse = send_and_receive(request, self)?;
        if response.magic_word == magic_word {
            Ok(())
        } else {
            bail!("ClearTimeProfiles failed")
        }
    }

    /// Remove a [`Card`] from the [`Device`].
    pub fn delete_card(&self, number: u32) -> Result<()> {
        let request = DeleteCardRequest::new(self.id, number);
        let response: DeleteCardResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("DeleteCard failed")
        }
    }

    /// Get a specific [`Card`] by its ID.
    pub fn get_card_by_id(&self, id: u32) -> Result<Card> {
        let request = GetCardByIDRequest::new(self.id, id);
        let response: GetCardByIDResponse = send_and_receive(request, self)?;
        response.try_into()
    }

    /// Get a specific [`Card`] by its index.
    pub fn get_card_by_index(&self, index: u32) -> Result<Card> {
        let request = GetCardByIndexRequest::new(self.id, index);
        let response: GetCardByIndexResponse = send_and_receive(request, self)?;
        response.try_into()
    }

    /// Get the number of [`Card`]s from the [`Device`].
    pub fn get_cards(&self) -> Result<u32> {
        let request = GetCardsRequest::new(self.id);
        let response: GetCardsResponse = send_and_receive(request, self)?;
        Ok(response.records)
    }

    /// Get a [`DeviceConfig`] for a the [`Device`].
    pub fn get_config(&self) -> Result<DeviceConfig> {
        let request = GetConfigRequest::new(self.id);
        let response: GetConfigResponse = send_and_receive(request, self)?;
        response.try_into()
    }

    /// Get a [`DoorControl`] for a specific door.
    /// Note that doors are addressed 1-4, not 0-3.
    pub fn get_door_control(&self, door: u8) -> Result<DoorControl> {
        let request = GetDoorControlStateRequest::new(self.id, door);
        let response: GetDoorControlStateResponse = send_and_receive(request, self)?;
        Ok(response.into())
    }

    /// Get an [`Event`] by its index.
    pub fn get_event(&self, index: u32) -> Result<Event> {
        let request = GetEventRequest::new(self.id, index);
        let response: GetEventResponse = send_and_receive(request, self)?;
        response.try_into()
    }

    /// Get the event index the [`Device`]
    pub fn get_event_index(&self) -> Result<u32> {
        let request = GetEventIndexRequest::new(self.id);
        let response: GetEventIndexResponse = send_and_receive(request, self)?;
        Ok(response.index)
    }

    /// Get what listener (IP:PORT) is set on the [`Device`]. This is where the the [`Device`]
    /// will send [`Status`] messages to over UDP.
    pub fn get_listener(&self) -> Result<SocketAddr> {
        let request = GetListenerRequest::new(self.id);
        let response: GetListenerResponse = send_and_receive(request, self)?;
        Ok(SocketAddr::from((response.ip_address, response.port)))
    }

    /// Get the [`Status`] of the [`Device`].
    pub fn get_status(&self) -> Result<Status> {
        let request = GetStatusRequest::new(self.id);
        let response: GetStatusResponse = send_and_receive(request, self)?;
        let status: Status = response.try_into()?;
        Ok(status)
    }

    /// Get the current time of the [`Device`].
    pub fn get_time(&self) -> Result<NaiveDateTime> {
        let request = GetTimeRequest::new(self.id);
        let response: GetTimeResponse = send_and_receive(request, self)?;
        response.datetime.try_into()
    }

    /// Get the [`TimeProfile`] by ID.
    pub fn get_time_profile(&self, profile_id: u8) -> Result<TimeProfile> {
        let request = GetTimeProfileRequest::new(self.id, profile_id);
        let response: GetTimeProfileResponse = send_and_receive(request, self)?;
        response.try_into()
    }

    /// Open a door.
    /// Note that doors are addressed 1-4, not 0-3.
    pub fn open_door(&self, door: u8) -> Result<()> {
        let request = OpenDoorRequest::new(self.id, door);
        let response: OpenDoorResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("OpenDoor failed")
        }
    }

    /// Refresh the task list of the [`Device`].
    pub fn refresh_task_list(&self) -> Result<()> {
        let request = RefreshTaskListRequest::new(self.id, 0x55aaaa55);
        let response: RefreshTaskListResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("RefreshTaskList failed")
        }
    }

    /// Set the [`DoorControl`] for a specific door.
    /// Note that the delay is in seconds and can maximally be 255.
    pub fn set_door_control_state(
        &self,
        door: u8,
        state: DoorControl,
        delay: Duration,
    ) -> Result<DoorControl> {
        let request =
            SetDoorControlStateRequest::new(self.id, door, state.mode as u8, delay.as_secs() as u8);
        let response: SetDoorControlStateResponse = send_and_receive(request, self)?;
        Ok(response.into())
    }

    /// Set the event index the [`Device`] will use.
    pub fn set_event_index(&self, index: u32) -> Result<()> {
        let request = SetEventIndexRequest::new(self.id, index, 0x55aaaa55);
        let response: SetEventIndexResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("SetEventIndex failed")
        }
    }

    /// Set the listener (IP:PORT) the [`Device`] will use to send [`Status`] messages to over UDP.
    pub fn set_listener(&self, address: Ipv4Addr, port: u16) -> Result<()> {
        let request = SetListenerRequest::new(self.id, address, port);
        let response: SetListenerResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("SetListener failed")
        }
    }

    /// Set IP address, subnet mask and gateway for the [`Device`].
    pub fn set_network_config(
        &self,
        address: Ipv4Addr,
        subnet: Ipv4Addr,
        gateway: Ipv4Addr,
    ) -> Result<()> {
        let request = SetAddressRequest::new(self.id, address, subnet, gateway, 0x55aaaa55);

        send(request, self)
    }

    /// Enable the recording of special events.
    pub fn enable_record_special_events(&self, enable: bool) -> Result<()> {
        let request = SetRecordSpecialEventsRequest::new(self.id, enable);
        let response: SetRecordSpecialEventsResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("SetRecordSpecialEvents failed")
        }
    }

    /// Set the local time of the [`Device`].
    pub fn set_time(&self, datetime: NaiveDateTime) -> Result<NaiveDateTime> {
        let request = SetTimeRequest::new(self.id, datetime.try_into()?);
        let response: SetTimeResponse = send_and_receive(request, self)?;
        response.datetime.try_into()
    }

    /// Add or update new [`TimeProfile`] to the [`Device`].
    pub fn add_or_update_time_profile(&self, profile: TimeProfile) -> Result<()> {
        let request = SetTimeProfileRequest::new(
            self.id,
            profile.id,
            profile.from.try_into()?,
            profile.to.try_into()?,
            profile.monday,
            profile.tuesday,
            profile.wednesday,
            profile.thursday,
            profile.friday,
            profile.saturday,
            profile.sunday,
            profile.segments[0].start.try_into()?,
            profile.segments[0].end.try_into()?,
            profile.segments[1].start.try_into()?,
            profile.segments[1].end.try_into()?,
            profile.segments[2].start.try_into()?,
            profile.segments[2].end.try_into()?,
            profile.linked_profile_id,
        );
        let response: SetTimeProfileResponse = send_and_receive(request, self)?;
        if response.success {
            Ok(())
        } else {
            bail!("SetTimeProfile failed")
        }
    }
}

/// Send a [`Request`] and receive a [`Response`].
fn send_and_receive<T: messages::Request, S: messages::Response + Debug>(
    request: T,
    d: &Device,
) -> Result<S> {
    let socket = setup_socket(d.u)?;
    let addr = get_address(d);
    socket.send_to(
        &request.to_bytes(),
        &SocketAddr::new(addr.into(), UHPPOTE_PORT),
    )?;

    // Receive the response
    let mut buf = [0u8; 64];
    socket.recv(&mut buf)?;

    S::from_bytes(&buf)
}

/// Send a [`Request`] to the [`Device`], but don't expect a response.
fn send<T: messages::Request>(request: T, d: &Device) -> Result<()> {
    let socket = setup_socket(d.u)?;
    let addr = get_address(d);
    socket.send_to(
        &request.to_bytes(),
        &SocketAddr::new(addr.into(), UHPPOTE_PORT),
    )?;
    Ok(())
}

/// Get the IP address of the [`Device`]. If None, use the broadcast address from [`Uhppoted`]
fn get_address(d: &Device) -> Ipv4Addr {
    match d.ip_address {
        Some(ip) => ip,
        None => d.u.broadcast_address,
    }
}

/// Setup a socket with correct timeouts.
fn setup_socket(u: &Uhppoted) -> Result<UdpSocket, anyhow::Error> {
    let socket = UdpSocket::bind(u.bind_address)?;
    socket.set_write_timeout(Some(Duration::new(1, 0)))?;
    socket.set_read_timeout(Some(u.timeout))?;
    socket.set_broadcast(true)?;
    Ok(socket)
}

/// Broadcast a [`Request`] to all [`Device`]s.
fn broadcast_and_receive<T: messages::Request, S: messages::Response + Debug>(
    request: T,
    u: &Uhppoted,
) -> Result<Vec<S>> {
    let socket = setup_socket(u)?;

    let to_addr = SocketAddr::new(u.broadcast_address.into(), UHPPOTE_PORT);

    socket.send_to(&request.to_bytes(), to_addr)?;
    let mut buf = [0u8; 64];

    let mut ret = Vec::new();

    while let Ok((_, _)) = socket.recv_from(&mut buf) {
        ret.push(S::from_bytes(&buf)?);
    }

    Ok(ret)
}