root / proj / libs / uart / src / hltp.c @ 313
History | View | Annotate | Download (5.91 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "hltp.h" |
4 |
|
5 |
#include "uart.h" |
6 |
|
7 |
static void* hltp_interpret_string(const uint8_t *p, const size_t sz){ |
8 |
void *ret = malloc((sz+1)*sizeof(char)); |
9 |
for(size_t i = 0; i < sz; ++i) ((char*)ret)[i] = p[i]; |
10 |
((char*)ret)[sz] = '\0'; |
11 |
return ret;
|
12 |
} |
13 |
int hltp_send_string(const char *p){ |
14 |
uint8_t type = hltp_type_string; |
15 |
uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = (uint8_t*)p; |
16 |
size_t sz[2]; sz [0] = 1; sz [1] = strlen(p); |
17 |
return nctp_send(2, ptr, sz); |
18 |
} |
19 |
|
20 |
static host_info_t* hltp_interpret_host_info(const uint8_t *p) { |
21 |
host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
|
22 |
size_t pos = 0;
|
23 |
// players information
|
24 |
memcpy(ret, p + pos, sizeof(double)*8); |
25 |
pos += sizeof(double)*8; |
26 |
// size of arrays
|
27 |
memcpy(&(ret->no_bullets), p + pos, sizeof(size_t));
|
28 |
pos += sizeof(size_t);
|
29 |
size_t sz = ret->no_bullets; |
30 |
// array containing the X positions of the bullets
|
31 |
(ret->bullets_x) = (double*)malloc(sizeof(double)*sz); |
32 |
memcpy((ret->bullets_x), p + pos, sizeof(double)*sz); |
33 |
pos += sizeof(double)*sz; |
34 |
// array containing the Y positions of the bullets
|
35 |
(ret->bullets_y) = (double*)malloc(sizeof(double)*(ret->no_bullets)); |
36 |
memcpy((ret->bullets_y), p + pos, sizeof(double)*sz); |
37 |
pos += sizeof(double)*sz; |
38 |
// array containing the X velocity of the bullets
|
39 |
(ret->bullets_vx) = (double*)malloc(sizeof(double)*(ret->no_bullets)); |
40 |
memcpy((ret->bullets_vx), p + pos, sizeof(double)*sz); |
41 |
pos += sizeof(double)*sz; |
42 |
// array containing the Y velocity of the bullets
|
43 |
(ret->bullets_vy) = (double*)malloc(sizeof(double)*(ret->no_bullets)); |
44 |
memcpy((ret->bullets_vy), p + pos, sizeof(double)*sz); |
45 |
pos += sizeof(double)*sz; |
46 |
// array containing the shooter id of the bullets
|
47 |
(ret->bullets_shooter) = (bool*)malloc(sizeof(bool)*(ret->no_bullets)); |
48 |
memcpy((ret->bullets_shooter), p + pos, sizeof(bool)*sz); |
49 |
|
50 |
return ret;
|
51 |
} |
52 |
int hltp_send_host_info(const host_info_t *p) { |
53 |
// struct size
|
54 |
size_t struct_size = sizeof(double) * (8 + 4*p->no_bullets) + p->no_bullets * sizeof(bool) + sizeof(size_t); |
55 |
uint8_t *base = (uint8_t*)malloc(struct_size); |
56 |
uint8_t *aux = base; |
57 |
// players information
|
58 |
memcpy(aux, p, sizeof(double) * 8); |
59 |
aux = aux + sizeof(double) * 8; |
60 |
// size of arrays
|
61 |
memcpy(aux, &(p->no_bullets), sizeof(size_t));
|
62 |
aux = aux + sizeof(size_t);
|
63 |
// array containing the X positions of the bullets
|
64 |
memcpy(aux, p->bullets_x, sizeof(double) * (p->no_bullets)); |
65 |
aux = aux + sizeof(double) * (p->no_bullets); |
66 |
// array containing the Y positions of the bullets
|
67 |
memcpy(aux, p->bullets_y, sizeof(double) * (p->no_bullets)); |
68 |
aux = aux + sizeof(double) * (p->no_bullets); |
69 |
// array containing the X velocity of the bullets
|
70 |
memcpy(aux, p->bullets_vx, sizeof(double) * (p->no_bullets)); |
71 |
aux = aux + sizeof(double) * (p->no_bullets); |
72 |
// array containing the Y velocity of the bullets
|
73 |
memcpy(aux, p->bullets_vy, sizeof(double) * (p->no_bullets)); |
74 |
aux = aux + sizeof(double) * (p->no_bullets); |
75 |
// array containing the shooter id of the bullets
|
76 |
memcpy(aux, p->bullets_shooter, sizeof(bool) * (p->no_bullets)); |
77 |
|
78 |
uint8_t type = hltp_type_host; |
79 |
uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = base; |
80 |
size_t sz[2]; sz [0] = 1; sz [1] = struct_size; |
81 |
return nctp_send(2, ptr, sz); |
82 |
} |
83 |
|
84 |
static remote_info_t* hltp_interpret_remote_info(const uint8_t *p) { |
85 |
remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
|
86 |
size_t pos = 0;
|
87 |
// keys pressed
|
88 |
memcpy(&(ret->remote_keys_pressed), p + pos, sizeof(keys_t));
|
89 |
pos += sizeof(keys_t);
|
90 |
// mouse positions
|
91 |
memcpy(&(ret->remote_mouse_x), p + pos, sizeof(int32_t));
|
92 |
pos += sizeof(int32_t);
|
93 |
memcpy(&(ret->remote_mouse_y), p + pos, sizeof(int32_t));
|
94 |
pos += sizeof(int32_t);
|
95 |
// bullets shot
|
96 |
memcpy(&(ret->bullets_shot), p + pos, sizeof(size_t));
|
97 |
pos += sizeof(size_t);
|
98 |
size_t sz = ret->bullets_shot; |
99 |
// array containing the X positions of the bullets
|
100 |
(ret->bullets_x) = (double*)malloc(sizeof(double)*sz); |
101 |
memcpy((ret->bullets_x), p + pos, sizeof(double)*sz); |
102 |
pos += sizeof(double)*sz; |
103 |
// array containing the Y positions of the bullets
|
104 |
(ret->bullets_y) = (double*)malloc(sizeof(double)*sz); |
105 |
memcpy((ret->bullets_y), p + pos, sizeof(double)*sz); |
106 |
pos += sizeof(double)*sz; |
107 |
// array containing the angle of the bullets
|
108 |
(ret->bullets_angle) = (double*)malloc(sizeof(double)*sz); |
109 |
memcpy((ret->bullets_angle), p + pos, sizeof(double)*sz); |
110 |
pos += sizeof(double)*sz; |
111 |
|
112 |
return ret;
|
113 |
} |
114 |
int hltp_send_remote_info(const remote_info_t *p) { |
115 |
// struct size
|
116 |
size_t struct_size = sizeof(keys_t) + sizeof(int32_t) * 2 + sizeof(size_t) + sizeof(double) * (3 *p->bullets_shot); |
117 |
uint8_t *base = (uint8_t*)malloc(struct_size); |
118 |
uint8_t *aux = base; |
119 |
memcpy(aux, p, sizeof(keys_t));
|
120 |
aux = aux + sizeof(keys_t);
|
121 |
memcpy(aux, p, sizeof(int32_t)*2); |
122 |
aux = aux + sizeof(int32_t)*2; |
123 |
memcpy(aux, &(p->bullets_shot), sizeof(size_t));
|
124 |
aux = aux + sizeof(size_t);
|
125 |
memcpy(aux, p->bullets_x, sizeof(double)*(p->bullets_shot)); |
126 |
aux = aux + sizeof(double)*(p->bullets_shot); |
127 |
memcpy(aux, p->bullets_y, sizeof(double)*(p->bullets_shot)); |
128 |
aux = aux + sizeof(double)*(p->bullets_shot); |
129 |
memcpy(aux, p->bullets_angle, sizeof(double)*(p->bullets_shot)); |
130 |
|
131 |
uint8_t type = hltp_type_remote; |
132 |
uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = base; |
133 |
size_t sz[2]; sz [0] = 1; sz [1] = struct_size; |
134 |
return nctp_send(2, ptr, sz); |
135 |
} |
136 |
|
137 |
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){ |
138 |
uint8_t ret = p[0];
|
139 |
switch(ret){
|
140 |
case hltp_type_string: *dest = hltp_interpret_string(p+1, sz-1); break; |
141 |
case hltp_type_host : *dest = hltp_interpret_host_info(p+1); break; |
142 |
case hltp_type_remote: *dest = hltp_interpret_remote_info(p+1); break; |
143 |
default: *dest = NULL; break; |
144 |
} |
145 |
return ret;
|
146 |
} |