altos: For telelco discovery packets, retry 5 times with shorter timeout
[fw/altos] / src / drivers / ao_lco_func.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_pad.h>
20 #include <ao_radio_cmac.h>
21 #include <ao_lco_func.h>
22
23 static __xdata struct ao_pad_command    command;
24 static __xdata uint8_t                  ao_lco_mutex;
25
26 int8_t
27 ao_lco_query(uint16_t box, struct ao_pad_query *query, uint16_t *tick_offset)
28 {
29         int8_t          r;
30         uint16_t        sent_time;
31
32         ao_mutex_get(&ao_lco_mutex);
33         command.tick = ao_time() - *tick_offset;
34         command.box = box;
35         command.cmd = AO_LAUNCH_QUERY;
36         command.channels = 0;
37         ao_radio_cmac_send(&command, sizeof (command));
38         sent_time = ao_time();
39         r = ao_radio_cmac_recv(query, sizeof (*query), AO_MS_TO_TICKS(10));
40         if (r == AO_RADIO_CMAC_OK)
41                 *tick_offset = sent_time - query->tick;
42         ao_mutex_put(&ao_lco_mutex);
43         return r;
44 }
45
46 void
47 ao_lco_arm(uint16_t box, uint8_t channels, uint16_t tick_offset)
48 {
49         ao_mutex_get(&ao_lco_mutex);
50         command.tick = ao_time() - tick_offset;
51         command.box = box;
52         command.cmd = AO_LAUNCH_ARM;
53         command.channels = channels;
54         ao_radio_cmac_send(&command, sizeof (command));
55         ao_mutex_put(&ao_lco_mutex);
56 }
57
58 void
59 ao_lco_ignite(uint16_t box, uint8_t channels, uint16_t tick_offset)
60 {
61         ao_mutex_get(&ao_lco_mutex);
62         command.tick = ao_time() - tick_offset;
63         command.box = box;
64         command.cmd = AO_LAUNCH_FIRE;
65         command.channels = channels;
66         ao_radio_cmac_send(&command, sizeof (command));
67         ao_mutex_put(&ao_lco_mutex);
68 }
69