-
Notifications
You must be signed in to change notification settings - Fork 17
/
proto.c
69 lines (61 loc) · 1.86 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
#include "proto.h"
#include <stdlib.h>
extern process_event_t kb_sendpkt_message;
void proto_send_packet(packet_t *p_pkt)
{
kb_event_t *p_kb_evt;
int i;
uint8_t checksum;
/* Allocate memory for kb_event_t structure. */
p_kb_evt = (kb_event_t *)malloc(sizeof(kb_event_t));
if (p_kb_evt != NULL)
{
/* Allocate memory for our payload. */
p_kb_evt->payload = (uint8_t *)malloc(p_pkt->size + 3);
if (p_kb_evt->payload != NULL)
{
/* Build our event. */
p_kb_evt->payload[0] = (uint8_t)(p_pkt->size + 3);
p_kb_evt->payload[1] = CMD_GOT_PKT;
for (i=0; i<p_pkt->size; i++)
{
p_kb_evt->payload[2+i] = p_pkt->payload[i];
}
checksum = packet_compute_checksum(p_kb_evt->payload, p_pkt->size+2);
p_kb_evt->payload[2+i] = checksum;
p_kb_evt->payload_size = p_pkt->size + 3;
/* Send packet to USB process. */
process_post(PROCESS_BROADCAST, kb_sendpkt_message, (void *)p_kb_evt);
/* Free packet. */
free(p_pkt);
}
}
}
void proto_send(command_t command, uint8_t *payload, int len)
{
kb_event_t *p_kb_evt;
int i;
uint8_t checksum;
/* Allocate memory for kb_event_t structure. */
p_kb_evt = (kb_event_t *)malloc(sizeof(kb_event_t));
if (p_kb_evt != NULL)
{
/* Allocate memory for our payload. */
p_kb_evt->payload = (uint8_t *)malloc(len + 3);
if (p_kb_evt->payload != NULL)
{
/* Build our event. */
p_kb_evt->payload[0] = (uint8_t)(len + 3);
p_kb_evt->payload[1] = command;
for (i=0; i<len; i++)
{
p_kb_evt->payload[2+i] = payload[i];
}
checksum = packet_compute_checksum(p_kb_evt->payload, len+2);
p_kb_evt->payload[2+i] = checksum;
p_kb_evt->payload_size = len + 3;
/* Send packet to USB process. */
process_post(PROCESS_BROADCAST, kb_sendpkt_message, (void *)p_kb_evt);
}
}
}