forked from TeamAntumbra/glow-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.c
117 lines (97 loc) · 2.89 KB
/
proto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (c) 2015 Antumbra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "proto.h"
#include "rawusb.h"
#include <stdlib.h>
#include <string.h>
static uint8_t globuf[64];
static uint8_t buildptr;
const void *proto_recv(uint32_t *api, uint16_t *cmd)
{
if (!rawusb_recv_bulk(0x01, globuf, sizeof globuf))
return NULL;
const void *buf = globuf;
*api = proto_get_u32(&buf);
*cmd = proto_get_u16(&buf);
proto_skip_pad(&buf, 2);
return buf;
}
uint32_t proto_get_u32(const void **buf)
{
const uint8_t *bs = *buf;
*buf = bs + 4;
return ((uint32_t)bs[0] << 24 | (uint32_t)bs[1] << 16 |
(uint32_t)bs[2] << 8 | (uint32_t)bs[3]);
}
uint16_t proto_get_u16(const void **buf)
{
const uint8_t *bs = *buf;
*buf = bs + 2;
return (uint16_t)bs[0] << 8 | (uint16_t)bs[1];
}
uint8_t proto_get_u8(const void **buf)
{
const uint8_t *bs = *buf;
*buf = bs + 1;
return bs[0];
}
void proto_skip_pad(const void **buf, uint8_t sz)
{
*buf = (uint8_t *)*buf + sz;
}
bool proto_send(uint8_t status, const void *out, uint8_t sz)
{
proto_send_start(status);
proto_send_add(out, sz);
return proto_send_end();
}
void proto_send_start(uint8_t status)
{
buildptr = 0;
proto_send_u8(status);
proto_send_pad(7);
}
void proto_send_add(const void *out, uint8_t sz)
{
if (sz > sizeof globuf - buildptr)
sz = sizeof globuf - buildptr;
memcpy(globuf + buildptr, out, sz);
buildptr += sz;
}
void proto_send_pad(uint8_t sz)
{
if (sz > sizeof globuf - buildptr)
sz = sizeof globuf - buildptr;
memset(globuf + buildptr, 0, sz);
buildptr += sz;
}
void proto_send_u32(uint32_t n)
{
if (buildptr < sizeof globuf) globuf[buildptr++] = n >> 24 & 0xff;
if (buildptr < sizeof globuf) globuf[buildptr++] = n >> 16 & 0xff;
if (buildptr < sizeof globuf) globuf[buildptr++] = n >> 8 & 0xff;
if (buildptr < sizeof globuf) globuf[buildptr++] = n & 0xff;
}
void proto_send_u16(uint16_t n)
{
if (buildptr < sizeof globuf) globuf[buildptr++] = n >> 8 & 0xff;
if (buildptr < sizeof globuf) globuf[buildptr++] = n & 0xff;
}
void proto_send_u8(uint8_t n)
{
if (buildptr < sizeof globuf) globuf[buildptr++] = n;
}
bool proto_send_end(void)
{
proto_send_pad(56);
return rawusb_send_bulk(0x82, globuf, sizeof globuf);
}