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
//! A combinator parser built with [nom](https://docs.rs/nom/latest/nom/) that
//! reads u-blox events.

use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{alphanumeric0, char, hex_digit1, i32, u32},
    combinator::{map, map_res},
    error::Error,
    sequence::{delimited, preceded, tuple},
    Finish, IResult,
};

use std::str::FromStr;

/// The various kinds of messages that can be sent from the u-blox antenna.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HardwareEvent {
    /// A real measurement from a specific anetnna/tag pair
    UUDFEvent(UUDFEvent),
    /// A "heartbeat" message with no measurement
    UUDFPEvent(UUDFPEvent),
}

impl FromStr for HardwareEvent {
    type Err = Error<String>;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match alt((
            map(parse_uudf_event, HardwareEvent::UUDFEvent),
            map(parse_uudfp_event, HardwareEvent::UUDFPEvent),
        ))(s)
        .finish()
        {
            Ok((_remaining, event)) => Ok(event),
            Err(Error { input, code }) => Err(Error {
                input: input.to_owned(),
                code,
            }),
        }
    }
}

/// Indicates that the tag is alive and communicating
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UUDFPEvent {
    /// The ID of teh tag whose data is being reported
    pub tag_id: u64,
}

impl FromStr for UUDFPEvent {
    type Err = Error<String>;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match parse_uudfp_event(s).finish() {
            Ok((_remaining, event)) => Ok(event),
            Err(Error { input, code }) => Err(Error {
                input: input.to_owned(),
                code,
            }),
        }
    }
}

/// All of the information about a specific measurement between a particular
/// tag/antenna pair.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UUDFEvent {
    /// The ID of the tag whose data is being reported
    pub tag_id: u64,
    /// Signal strength
    pub rssi: i32,
    /// Azimuth to tag
    pub angle_1: i32,
    /// Elevation to tag
    pub angle_2: i32,
    /// u-blox won't say what they use this for...maybe curing cancer.
    reserved: i32,
    /// TODO what does it mean "channel", is this which Bluetooth channel?
    /// Completely unclear from the u-blox documentation.
    pub channel: u32,
    /// The ID of the antenna
    pub anchor_id: u64,
    /// The user can configure this with `AT+UDFCFG` tag 2
    pub user_defined: String,
    /// A timestamp in milliseconds since the listener block was powered on
    pub timestamp: u32,
    /// What event this is. The first reading is 1, then the next one is 2
    /// and so on.
    pub sequence: u32,
}

impl FromStr for UUDFEvent {
    type Err = Error<String>;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match parse_uudf_event(s).finish() {
            Ok((_remaining, event)) => Ok(event),
            Err(Error { input, code }) => Err(Error {
                input: input.to_owned(),
                code,
            }),
        }
    }
}

fn parse_id(s: &str) -> IResult<&str, u64> {
    map_res(hex_digit1, |d: &str| {
        if d.len() == 12 {
            u64::from_str_radix(d, 16)
        } else {
            u64::from_str_radix("hey", 0)
        }
    })(s)
}

fn parse_quoted_id(s: &str) -> IResult<&str, u64> {
    delimited(char('\"'), parse_id, char('\"'))(s)
}

fn parse_quoted_string(s: &str) -> IResult<&str, String> {
    map(
        delimited(char('\"'), alphanumeric0, char('\"')),
        |cs: &str| cs.to_owned(),
    )(s)
}

fn parse_uudf_event(s: &str) -> IResult<&str, UUDFEvent> {
    map(
        tuple((
            preceded(tag("+UUDF:"), parse_id),
            preceded(char(','), i32),
            preceded(char(','), i32),
            preceded(char(','), i32),
            preceded(char(','), i32),
            preceded(char(','), u32),
            preceded(char(','), parse_quoted_id),
            preceded(char(','), parse_quoted_string),
            preceded(char(','), u32),
            preceded(char(','), u32),
        )),
        |(
            instance_id,
            rssi,
            angle_1,
            angle_2,
            reserved,
            channel,
            anchor_id,
            user_defined,
            timestamp,
            sequence,
        )| UUDFEvent {
            tag_id: instance_id,
            rssi,
            angle_1,
            angle_2,
            reserved,
            channel,
            anchor_id,
            user_defined,
            timestamp,
            sequence,
        },
    )(s)
}

fn parse_uudfp_event(s: &str) -> IResult<&str, UUDFPEvent> {
    map(
        tuple((
            preceded(tag("+UUDFP:"), parse_id),
            preceded(char(','), hex_digit1),
        )),
        |(instance_id, _other_hex)| UUDFPEvent {
            tag_id: instance_id,
        },
    )(s)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn uudf_test_1() {
        let s = "+UUDF:CCF9578E0D8A,-42,20,0,-43,37,\"CCF9578E0D89\",\"\",15869,23";

        let res = UUDFEvent::from_str(s).unwrap();

        assert_eq!(
            res,
            UUDFEvent {
                tag_id: 0xCCF9578E0D8A,
                rssi: -42,
                angle_1: 20,
                angle_2: 0,
                reserved: -43,
                channel: 37,
                anchor_id: 0xCCF9578E0D89,
                user_defined: "".to_owned(),
                timestamp: 15869,
                sequence: 23,
            }
        );
    }

    #[test]
    fn uudf_test_2() {
        let s = "+UUDF:CCF9578E0D8B,-41,10,4,-42,38,\"CCF9578E0D89\",\"\",15892,24";

        let res = UUDFEvent::from_str(s).unwrap();

        assert_eq!(
            res,
            UUDFEvent {
                tag_id: 0xCCF9578E0D8B,
                rssi: -41,
                angle_1: 10,
                angle_2: 4,
                reserved: -42,
                channel: 38,
                anchor_id: 0xCCF9578E0D89,
                user_defined: "".to_owned(),
                timestamp: 15892,
                sequence: 24,
            }
        );
    }

    #[test]
    fn uudf_test_3() {
        let s = "+UUDF:CCF9578E0D8A,-42,-10,2,-43,39,\"CCF9578E0D89\",\"\",15921,25";

        let res = UUDFEvent::from_str(s).unwrap();

        assert_eq!(
            res,
            UUDFEvent {
                tag_id: 0xCCF9578E0D8A,
                rssi: -42,
                angle_1: -10,
                angle_2: 2,
                reserved: -43,
                channel: 39,
                anchor_id: 0xCCF9578E0D89,
                user_defined: "".to_owned(),
                timestamp: 15921,
                sequence: 25,
            }
        );
    }

    #[test]
    fn uufdp_test() {
        let s = "+UUDFP:6C3DEBAFAEE4,19FF1500000050F80C0065000900052A0D001F000000D0030000";

        let res = UUDFPEvent::from_str(s).unwrap();
        assert_eq!(
            res,
            UUDFPEvent {
                tag_id: 0x6C3DEBAFAEE4,
            }
        );
    }

    #[test]
    fn hardware_event_uudf_test_1() {
        let s = "+UUDF:CCF9578E0D8A,-42,20,0,-43,37,\"CCF9578E0D89\",\"\",15869,23";

        let res = HardwareEvent::from_str(s).unwrap();
        assert_eq!(
            res,
            HardwareEvent::UUDFEvent(UUDFEvent {
                tag_id: 0xCCF9578E0D8A,
                rssi: -42,
                angle_1: 20,
                angle_2: 0,
                reserved: -43,
                channel: 37,
                anchor_id: 0xCCF9578E0D89,
                user_defined: "".to_owned(),
                timestamp: 15869,
                sequence: 23,
            })
        );
    }

    #[test]
    fn hardware_event_uufdp_test() {
        let s = "+UUDFP:6C3DEBAFAEE4,19FF1500000050F80C0065000900052A0D001F000000D0030000";

        let res = HardwareEvent::from_str(s).unwrap();
        assert_eq!(
            res,
            HardwareEvent::UUDFPEvent(UUDFPEvent {
                tag_id: 0x6C3DEBAFAEE4,
            })
        );
    }
}