libaltos: Create altos_pause_one_second API
[fw/altos] / libaltos / libaltos_common.c
1 /*
2  * Copyright © 2016 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "libaltos_private.h"
20
21 PUBLIC int
22 altos_init(void)
23 {
24         return LIBALTOS_SUCCESS;
25 }
26
27 PUBLIC void
28 altos_fini(void)
29 {
30 }
31
32 struct altos_error altos_last_error;
33
34 void
35 altos_set_last_error(int code, char *string)
36 {
37         altos_last_error.code = code;
38         strncpy(altos_last_error.string, string, sizeof (altos_last_error.string) -1);
39         altos_last_error.string[sizeof(altos_last_error.string)-1] = '\0';
40 }
41
42 PUBLIC void
43 altos_get_last_error(struct altos_error *error)
44 {
45         *error = altos_last_error;
46 }
47
48 PUBLIC int
49 altos_getchar(struct altos_file *file, int timeout)
50 {
51         int     ret;
52
53         file->busy = 1;
54         while (file->in_read == file->in_used) {
55                 ret = altos_fill(file, timeout);
56                 if (ret)
57                         goto done;
58         }
59         ret = file->in_data[file->in_read++];
60 done:
61         file->busy = 0;
62         return ret;
63 }
64
65 PUBLIC int
66 altos_putchar(struct altos_file *file, char c)
67 {
68         int     ret;
69
70         if (file->out_used == USB_BUF_SIZE) {
71                 ret = altos_flush(file);
72                 if (ret) {
73                         return ret;
74                 }
75         }
76         file->out_data[file->out_used++] = c;
77         ret = 0;
78         if (file->out_used == USB_BUF_SIZE)
79                 ret = altos_flush(file);
80         return ret;
81 }
82
83 struct bt_vendor_map {
84         const char      vendor[10];
85         int             port;
86 };
87
88 static const struct bt_vendor_map altos_bt_vendor_map[] = {
89         { .vendor = "00:12:6f:", 1 },   /* Rayson */
90         { .vendor = "8c:de:52:", 6 },   /* ISSC */
91         { .vendor = "d8:80:39:", 6 },   /* Microchip */
92 };
93
94 #define NUM_BT_VENDOR_MAP       (sizeof altos_bt_vendor_map / sizeof altos_bt_vendor_map[0])
95 #define BT_PORT_DEFAULT         1
96
97 static inline int
98 ao_tolower(int c) {
99         if ('A' <= c && c <= 'Z')
100                 return c + 'a' - 'A';
101         return c;
102 }
103
104 int altos_bt_port(struct altos_bt_device *device) {
105         unsigned i, j;
106         for (i = 0; i < NUM_BT_VENDOR_MAP; i++) {
107                 const char *vendor = altos_bt_vendor_map[i].vendor;
108                 for (j = 0; ; j++) {
109                         if (vendor[j] == '\0')
110                                 return altos_bt_vendor_map[i].port;
111                         if (device->addr[j] == '\0')
112                                 break;
113                         if (ao_tolower(device->addr[j]) != vendor[j])
114                                 break;
115                 }
116         }
117         return BT_PORT_DEFAULT;
118 }
119
120 PUBLIC void
121 altos_free(struct altos_file *file)
122 {
123         int i;
124         altos_close(file);
125         for (i = 0; i < 10 && file->busy; i++)
126                 altos_pause_one_second();
127         free(file);
128 }