Revision 344
changed protocol
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