libaltos: Add Windows BT support. Split into separate source files.
[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; 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 "libaltos_private.h"
19
20 PUBLIC int
21 altos_init(void)
22 {
23         return LIBALTOS_SUCCESS;
24 }
25
26 PUBLIC void
27 altos_fini(void)
28 {
29 }
30
31 struct altos_error altos_last_error;
32
33 void
34 altos_set_last_error(int code, char *string)
35 {
36         altos_last_error.code = code;
37         strncpy(altos_last_error.string, string, sizeof (altos_last_error.string) -1);
38         altos_last_error.string[sizeof(altos_last_error.string)-1] = '\0';
39 }
40
41 PUBLIC void
42 altos_get_last_error(struct altos_error *error)
43 {
44         *error = altos_last_error;
45 }
46
47 PUBLIC int
48 altos_getchar(struct altos_file *file, int timeout)
49 {
50         int     ret;
51         while (file->in_read == file->in_used) {
52                 ret = altos_fill(file, timeout);
53                 if (ret)
54                         return ret;
55         }
56         return file->in_data[file->in_read++];
57 }
58
59 PUBLIC int
60 altos_putchar(struct altos_file *file, char c)
61 {
62         int     ret;
63
64         if (file->out_used == USB_BUF_SIZE) {
65                 ret = altos_flush(file);
66                 if (ret) {
67                         return ret;
68                 }
69         }
70         file->out_data[file->out_used++] = c;
71         ret = 0;
72         if (file->out_used == USB_BUF_SIZE)
73                 ret = altos_flush(file);
74         return ret;
75 }
76
77
78 PUBLIC void
79 altos_free(struct altos_file *file)
80 {
81         altos_close(file);
82         free(file);
83 }