-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZephyrHxM.pm
320 lines (233 loc) · 8.37 KB
/
ZephyrHxM.pm
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
package ZephyrHxM;
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
use 5.008;
use strict;
use warnings;
use Carp;
use Digest::CRC qw(crc8);
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
use POSIX;
our $VERSION = '0.01';
$VERSION = eval $VERSION; # see L<perlmodstyle>
=head1 NAME
ZephyrHxM - Perl extension for receiving data from Zephyr HxM
=head1 SYNOPSIS
use ZephyrHxM;
my $device = new ZephyrHxM;
# Wait for and fetch latest packet
my $packet = $device->fetch_latest();
print "Battery status: " . $packet->{battery_charge} . "%\n";
# FIXME: How do I then close the TTY? unset($device) or what?
=head1 DESCRIPTION
This module provides a simple interface to listen to and decode Zephyr HxM
packets from the first available unit on the first available Bluetooth
interface. It is up to the caller to invoke bluetooth-agent as necessary.
For example:
$ sudo bluetooth-agent 1234 & # Needs to run in the background.
$ hrvmonitor
...progress here
$ sudo killall bluetooth-agent
This module strictly supports standard "heart rate, speed and distance"
packets.
Note that this module invokes "hcitool" and "sudo rfcomm" at start-up and
shutdown so you may need to configure sudo accordingly.
=head1 CONSTRUCTOR
=over 4
=item new([I<verbose>])
Creates and returns a new C<ZephyrHxM> object, connecting to the
first-available Zephyr HxM device available via Bluetooth. If I<verbose> is
supplied and true, then a few status and error messages will be printed out as
appropriate. Fatal error messages are always printed.
=cut
use IO::Tty 'B115200';
sub new {
my ($class, $verbose) = @_;
my $self = bless {}, $class;
## Bluetooth level connection
# Tried Net::Bluetooth, but its sdp_search() fails. Too bad.
my $localdevices_raw = qx/hcitool dev/;
my ($hci_device, $hci_addr) = $localdevices_raw =~ /\s*([a-zA-Z0-9]+)\s+([a-fA-F0-9:]+)\s*/;
croak "Cannot find Bluetooth interface." unless defined($hci_device);
print "Scanning for Bluetooth devices...\n" if $verbose;
my $remotedevices_raw = qx/hcitool scan/;
my ($zephyr_addr, $zephyr_id) = $remotedevices_raw =~ /\s*([a-fA-F0-9:]+)\s+(HXM[0-9a-zA-Z]+)\s*/;
croak "Cannot find a Zephyr HxM unit." unless defined($zephyr_addr);
print "Connecting to $zephyr_id...\n" if $verbose;
system("sudo rfcomm release $zephyr_addr");
system("sudo rfcomm bind $hci_device $zephyr_addr");
## RS-232 level connection
my $tty;
my $rfcomms = qx/rfcomm |grep $zephyr_addr/;
my ($rfcomm_device) = $rfcomms =~ /^(rfcomm[^:]+):/;
unless (defined($rfcomm_device)) {
system("sudo rfcomm release $zephyr_addr");
croak "Unable to figure out RFCOMM device.";
}
sysopen($tty, '/dev/'.$rfcomm_device, O_RDONLY) or croak "Cannot open /dev/$rfcomm_device: $!";
binmode $tty;
my $termios = new POSIX::Termios;
# 115,200 8N1, ignore modem status lines, enable receiver
$termios->setcflag(CS8 | CLOCAL | CREAD);
$termios->setiflag(0);
$termios->setoflag(0);
$termios->setlflag(0);
$termios->setospeed(B115200);
$termios->setispeed(B115200);
$termios->setattr(fileno($tty), TCSANOW)
or croak "tcsetattr() failed: $!";
$self->{_tty} = $tty;
$self->{_device_name} = $zephyr_id;
$self->{_device} = $rfcomm_device;
$self->{_last_beatnum} = -1;
return $self;
}
sub _tty { shift->{_tty} }
=back
=head1 METHODS
=over 4
=item close()
Cleanly close communication channel.
=cut
sub close {
my ($self) = @_;
close($self->{_tty});
system("sudo rfcomm release ". $self->{_device});
}
=item fetch_latest()
Reads any pending input from the device and parses the last packet available.
If no data is waiting for us or if the last packet was invalid, wait for the
very next valid packet. The parsed packet returned is in the form of a hash
with the following keys:
=over 4
=item B<error>: Error message if we couldn't get a valid packet and timed out.
=item B<device_name>: Name of the Bluetooth device.
=item B<firmware_id>
=item B<firmware_version>
=item B<hardware_id>
=item B<hardware_version>
=item B<battery_charge>: Percentage of battery charge remaining.
=item B<heart_rate>: As computed by the device.
=item B<new_beats>: Chronological ordered list of new R-R durations, in msec.
=item B<distance>: I<Not yet implemented>
=item B<speed>: I<Not yet implemented>
=item B<strides>: I<Not yet implemented>
=back
=cut
sub fetch_latest {
my ($self) = @_;
my $packet = {
error => 0,
device_name => $self->{_device_name},
firmware_id => 0,
firmware_version => 0,
hardware_id => 0,
hardware_version => 0,
battery_charge => 0,
heart_rate => 0,
new_beats => [],
distance => 0,
speed => 0,
strides => 0,
};
my $input = $self->_fetch_packet();
if (length($input) != 60) {
$packet->{error} = 'TTY read yielded a '. length($input) .'-byte packet.';
return $packet;
}
my @struct = unpack("CCCvCCvvCCCvvvvvvvvvvvvvvvCCCCCCvvCCCCCC", $input);
# FIXME: Wasn't I supposed to try again if it's invalid?
my $stx = shift @struct;
my $msgid = shift @struct;
my $dlc = shift @struct;
my $etx = pop @struct;
my $crc = pop @struct;
unless ($stx == 0x02 && $msgid == 0x26 && $dlc == 0x37 && $etx == 0x03) {
$packet->{error} = 'Packet not valid HxM format.';
return $packet;
}
# FIXME: I always get CRC mismatches; disabling for now.
#my $mycrc = crc8(substr($input, 3, 55));
#unless ($crc == $mycrc) {
# $packet->{error} = 'Packet CRC mismatch. Got '. $crc .', expected '. $mycrc .'.';
# return $packet;
#}
# FIXME: I don't know how to possibly implement the CRC in Perl. I don't
# even know how I could extract the payload from $input to feed to it!
$packet->{firmware_id} = shift @struct;
my $fv_major = shift @struct;
my $fv_minor = shift @struct;
$packet->{firmware_version} = $fv_major .'.'. $fv_minor;
$packet->{hardware_id} = shift @struct;
$packet->{hardware_version} = shift @struct;
$packet->{battery_charge} = shift @struct;
$packet->{heart_rate} = shift @struct;
## Handle beat timestamps
# Fetch from packet
my $beatnumber = shift @struct;
my @beattimes;
for (my $i = 0; $i < 15; $i++) {
# Remove the 15 timestamps from @struct, store in same
# reverse-chronological order as source.
push @beattimes, shift @struct;
}
# Beat number: ABSOLUTE 0..255, rolls over continuously
# $self->{_last_beatnum} = -1 # at startup
my $new_count = $beatnumber - $self->{_last_beatnum};
$new_count = $new_count + 256 if $new_count < 0;
$self->{_last_beatnum} = $beatnumber;
# Beat timestamps: ABSOLUTE 0..65535, roll over continuously
for (my $i = 0; $i < $new_count; $i++) {
my $new_time = shift @beattimes
or last;
last if scalar(@beattimes) <= 0; # We need to look one earlier to calculate R-R.
my $new_rr = $new_time - $beattimes[0];
$new_rr = $new_rr + 65536 if $new_rr < 0;
unshift @{ $packet->{new_beats} }, $new_rr;
}
# Dump 6 reserved characters.
shift @struct;
shift @struct;
shift @struct;
shift @struct;
shift @struct;
shift @struct;
$packet->{distance} = shift @struct;
$packet->{speed} = shift @struct;
$packet->{strides} = shift @struct;
return $packet;
}
# Just try getting 60 bytes out of the TTY, wait for them a second.
sub _fetch_packet {
my ($self) = @_;
# I can't think of an intelligent way to split packets properly...
tcflush(fileno($self->_tty), TCIFLUSH);
# fd_set to be used with select()
my $readfds = '';
vec($readfds, fileno($self->_tty), 1) = 1;
# We should receive packets every second, so 1200ms should be plenty.
my $timeout = 1.2;
my $last_time = time;
# It's not even funny how little I know what I'm doing in Perl I/O. :(
my $input = '';
while (($timeout > 0) && select($readfds, undef, undef, $timeout)) {
if (sysread($self->_tty, $input, 60 - length($input), length($input))) {
last if length($input) >= 60;
}
}
# FIXME: How do I know if there was a fatal I/O error?
return $input;
}
=back
=cut
1;