altos/test: Fix AES key format in test code
[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         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 int altos_bt_port(struct altos_bt_device *device) {
93         unsigned i;
94         for (i = 0; i < NUM_BT_VENDOR_MAP; i++)
95                 if (strncmp (device->addr, altos_bt_vendor_map[i].vendor, strlen(altos_bt_vendor_map[i].vendor)) == 0)
96                         return altos_bt_vendor_map[i].port;
97         return BT_PORT_DEFAULT;
98 }
99
100 PUBLIC void
101 altos_free(struct altos_file *file)
102 {
103         altos_close(file);
104         free(file);
105 }