-
Notifications
You must be signed in to change notification settings - Fork 1
/
hidBlProtocol.c
65 lines (60 loc) · 2.37 KB
/
hidBlProtocol.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
/*
* This file is part of the m032HidBootloader distribution (https://github.com/nimo-labs/m032HidBootloader).
* Copyright (c) 2021 Nimolabs Ltd. www.nimo.uk
*
* 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, version 3.
*
* 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 <stdio.h>
#include "hidBlProtocol.h"
void hidBlProtocolDeSerialisePacket(struct hidBlProtocolPacket_s *packet, const unsigned char *rawData)
{
unsigned char i;
packet->packetType = rawData[0];
packet->dataLen = rawData[1];
packet->address = (rawData[2] << 24) | (rawData[3] << 16) | (rawData[4] << 8) | rawData[5];
for (i = 0; i < packet->dataLen; i++)
packet->data[i] = rawData[i + 6];
}
char hidBlProtocolSerialisePacket(struct hidBlProtocolPacket_s *packet, unsigned char *serialPacket, unsigned char buffLen)
{
unsigned char i;
if (buffLen < (HID_BL_PROTOCOL_PACKET_HEADER_SIZE + HID_BL_PROTOCOL_DATA_SIZE))
{
// printf("hidBlProtocolSerialisePacket: buffer too short!\r\n");
return 1;
}
serialPacket[0] = packet->packetType;
serialPacket[1] = packet->dataLen;
serialPacket[2] = (packet->address >> 24) & 0xFF;
serialPacket[3] = (packet->address >> 16) & 0xFF;
serialPacket[4] = (packet->address >> 8) & 0xFF;
serialPacket[5] = packet->address & 0xFF;
for (i = 0; i < packet->dataLen; i++)
serialPacket[i + 6] = packet->data[i];
return 0;
}
char hidBlProtocolEncodePacket(struct hidBlProtocolPacket_s *packet, unsigned long address, unsigned char packetType, const unsigned char *data, unsigned char dataLen)
{
unsigned char i;
if (dataLen > HID_BL_PROTOCOL_DATA_SIZE)
{
//printf("hidBlProtocolEncodePacket: data too long!\r\n");
return 1;
}
packet->packetType = packetType;
packet->dataLen = dataLen;
packet->address = address;
for (i = 0; i < dataLen; i++)
packet->data[i] = data[i];
return 0;
}