doc: Update all docs to 1.9. Note this in doc/RELNOTES
[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         while (file->in_read == file->in_used) {
53                 ret = altos_fill(file, timeout);
54                 if (ret)
55                         return ret;
56         }
57         return file->in_data[file->in_read++];
58 }
59
60 PUBLIC int
61 altos_putchar(struct altos_file *file, char c)
62 {
63         int     ret;
64
65         if (file->out_used == USB_BUF_SIZE) {
66                 ret = altos_flush(file);
67                 if (ret) {
68                         return ret;
69                 }
70         }
71         file->out_data[file->out_used++] = c;
72         ret = 0;
73         if (file->out_used == USB_BUF_SIZE)
74                 ret = altos_flush(file);
75         return ret;
76 }
77
78 struct bt_vendor_map {
79         const char      vendor[10];
80         int             port;
81 };
82
83 static const struct bt_vendor_map altos_bt_vendor_map[] = {
84         { .vendor = "00:12:6f:", 1 },   /* Rayson */
85         { .vendor = "8c:de:52:", 6 },   /* ISSC */
86         { .vendor = "d8:80:39:", 6 },   /* Microchip */
87 };
88
89 #define NUM_BT_VENDOR_MAP       (sizeof altos_bt_vendor_map / sizeof altos_bt_vendor_map[0])
90 #define BT_PORT_DEFAULT         1
91
92 static inline int
93 ao_tolower(int c) {
94         if ('A' <= c && c <= 'Z')
95                 return c + 'a' - 'A';
96         return c;
97 }
98
99 int altos_bt_port(struct altos_bt_device *device) {
100         unsigned i, j;
101         for (i = 0; i < NUM_BT_VENDOR_MAP; i++) {
102                 const char *vendor = altos_bt_vendor_map[i].vendor;
103                 for (j = 0; ; j++) {
104                         if (vendor[j] == '\0')
105                                 return altos_bt_vendor_map[i].port;
106                         if (device->addr[j] == '\0')
107                                 break;
108                         if (ao_tolower(device->addr[j]) != vendor[j])
109                                 break;
110                 }
111         }
112         return BT_PORT_DEFAULT;
113 }
114
115 PUBLIC void
116 altos_free(struct altos_file *file)
117 {
118         altos_close(file);
119         free(file);
120 }