Revision 344
changed protocol
proj/libs/uart/src/hltp.c | ||
---|---|---|
18 | 18 |
} |
19 | 19 |
|
20 | 20 |
static host_info_t* hltp_interpret_host_info(const uint8_t *p, const size_t sz) { |
21 |
printf("%s\n", __func__); |
|
22 |
printf("sizeof(host_info_t)=%d, sz=%d\n", sizeof(host_info_t), sz); |
|
21 | 23 |
if (sz != sizeof(host_info_t)) { printf("%d should equal %d\n", sz, sizeof(host_info_t)); return NULL;} |
22 | 24 |
host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t)); |
23 | 25 |
//size_t pos = 0; |
... | ... | |
51 | 53 |
*/ |
52 | 54 |
return ret; |
53 | 55 |
} |
54 |
int hltp_send_host_info(const host_info_t *p) { |
|
56 |
int hltp_send_host_info(const host_info_t *p) { printf("%s, %d\n",__func__, rand());
|
|
55 | 57 |
|
58 |
printf(" sending angle: 0x%04X\n", p->host_angle); |
|
59 |
|
|
56 | 60 |
uint8_t type = hltp_type_host; |
57 | 61 |
const uint8_t* ptr[11]; size_t sz[11]; |
58 | 62 |
ptr[0] = (uint8_t*)& type ; sz[0] = 1; |
... | ... | |
113 | 117 |
return nctp_send(2, ptr, sz); |
114 | 118 |
} |
115 | 119 |
|
116 |
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){ |
|
120 |
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){ printf("%s %d\n",__func__, rand());
|
|
117 | 121 |
uint8_t ret = p[0]; |
122 |
printf(" ret=%d\n", ret); |
|
118 | 123 |
switch(ret){ |
119 | 124 |
case hltp_type_string: *dest = hltp_interpret_string (p+1, sz-1); break; |
120 | 125 |
case hltp_type_host : *dest = hltp_interpret_host_info (p+1, sz-1); break; |
proj/libs/uart/src/uart.c | ||
---|---|---|
278 | 278 |
|
279 | 279 |
/// NCTP |
280 | 280 |
|
281 |
#define NCTP_START 0x80 |
|
282 |
#define NCTP_END 0xFF |
|
281 |
//#define NCTP_START 0x80
|
|
282 |
//#define NCTP_END 0xFF
|
|
283 | 283 |
#define NCTP_OK 0xFF |
284 | 284 |
#define NCTP_NOK 0x00 |
285 | 285 |
|
... | ... | |
315 | 315 |
|
316 | 316 |
int nctp_send(size_t num, const uint8_t *const *ptr, const size_t *const sz){ |
317 | 317 |
int ret; |
318 |
uint16_t sz_total = 0;{ |
|
319 |
for(size_t i = 0; i < num; ++i) |
|
320 |
sz_total += sz[i]; |
|
321 |
} |
|
318 | 322 |
uint8_t *tmp; |
319 |
tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_START; queue_push(out, tmp); |
|
323 |
tmp = malloc(sizeof(uint8_t)); *tmp = *((uint8_t*)(&sz_total)+0); queue_push(out, tmp); |
|
324 |
tmp = malloc(sizeof(uint8_t)); *tmp = *((uint8_t*)(&sz_total)+1); queue_push(out, tmp); |
|
320 | 325 |
for(size_t i = 0; i < num; ++i){ |
321 | 326 |
const uint8_t *p = ptr[i]; const size_t s = sz[i]; |
322 | 327 |
for(size_t j = 0; j < s; ++j, ++p){ |
323 | 328 |
tmp = malloc(sizeof(uint8_t)); *tmp = *p; queue_push(out, tmp); |
324 | 329 |
} |
325 | 330 |
} |
326 |
tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_END; queue_push(out, tmp); |
|
331 |
|
|
327 | 332 |
if(uart_transmitter_empty(COM1_ADDR)){ |
328 | 333 |
if((ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)))) return ret; |
329 | 334 |
queue_pop(out); |
... | ... | |
339 | 344 |
} |
340 | 345 |
|
341 | 346 |
static void nctp_process_received(){ |
342 |
free(queue_top(in)); queue_pop(in); |
|
343 |
size_t sz = 1024; uint8_t *p = malloc(sz*sizeof(uint8_t)); |
|
344 |
size_t i = 0; |
|
345 |
printf("reach\n"); |
|
346 |
while(*(uint8_t*)queue_top(in) != NCTP_END){ |
|
347 |
//printf("%c\n", *(uint8_t*)queue_top(in)); |
|
348 |
p[i++] = *(uint8_t*)queue_top(in); |
|
347 |
uint16_t sz = 0;{ |
|
348 |
uint8_t sz0 = *(uint8_t*)queue_top(in); free(queue_top(in)); queue_pop(in); |
|
349 |
uint8_t sz1 = *(uint8_t*)queue_top(in); free(queue_top(in)); queue_pop(in); |
|
350 |
*((uint8_t*)(&sz)+0) = sz0; |
|
351 |
*((uint8_t*)(&sz)+1) = sz1; |
|
352 |
} |
|
353 |
uint8_t *p = malloc(sz*sizeof(uint8_t)); |
|
354 |
for(uint16_t i = 0; i < sz; ++i){ |
|
355 |
p[i] = *(uint8_t*)queue_top(in); |
|
349 | 356 |
free(queue_top(in)); queue_pop(in); |
350 |
if(i >= sz) p = realloc(p, sz=2*sz); |
|
351 |
}printf("reach2\n"); |
|
352 |
free(queue_top(in)); queue_pop(in); |
|
353 |
if(process != NULL) process(p, i); |
|
357 |
} |
|
358 |
if(process != NULL) process(p, sz); |
|
354 | 359 |
free(p); |
355 | 360 |
} |
361 |
|
|
362 |
static int num_bytes_to_receive = 0; |
|
363 |
static uint16_t szbytes_to_receive = 0; |
|
364 |
static uint8_t size0 = 0; |
|
356 | 365 |
static int nctp_receive(void){ |
357 | 366 |
int ret; |
358 | 367 |
uint8_t c; |
359 |
int num_ends = 0; |
|
368 |
int counter_to_process = 0; |
|
369 |
|
|
360 | 370 |
while(uart_receiver_ready(COM1_ADDR)){ |
361 | 371 |
if((ret = uart_get_char(COM1_ADDR, &c))) return ret; |
362 | 372 |
uint8_t *tmp = malloc(sizeof(uint8_t)); *tmp = c; |
373 |
|
|
363 | 374 |
queue_push(in, tmp); |
364 |
if(c == NCTP_END) ++num_ends; |
|
375 |
|
|
376 |
if (szbytes_to_receive){ // gotta receive 2nd size byte and update num_bytes |
|
377 |
*((uint8_t*)(&num_bytes_to_receive)+0) = size0; |
|
378 |
*((uint8_t*)(&num_bytes_to_receive)+1) = c; |
|
379 |
szbytes_to_receive = 0; |
|
380 |
} else if(num_bytes_to_receive > 0){ |
|
381 |
/* Now I know there are no more size bytes to receive. |
|
382 |
* If there are normal bytes to receive*/ |
|
383 |
--num_bytes_to_receive; |
|
384 |
if(num_bytes_to_receive == 0) ++counter_to_process; |
|
385 |
} else { |
|
386 |
/* Now I know I am not expecting anything. |
|
387 |
* The fact I received something means it is the 1st size byte */ |
|
388 |
size0 = c; |
|
389 |
szbytes_to_receive = 1; |
|
390 |
} |
|
365 | 391 |
} |
366 |
while(num_ends-- > 0) nctp_process_received();
|
|
392 |
while(counter_to_process-- > 0) nctp_process_received();
|
|
367 | 393 |
return SUCCESS; |
368 | 394 |
} |
369 | 395 |
|
Also available in: Unified diff