stmbl/shared/common.c

38 lines
889 B
C
Raw Permalink Normal View History

2015-10-10 22:00:26 +00:00
#include "common.h"
2017-02-03 23:48:20 +00:00
#include "crc16.h"
2015-10-10 22:00:26 +00:00
void buff_packet(packet_header_t* p, uint8_t size){
uint8_t nonkey_count = 0;
uint8_t* buf = ((uint8_t*)p) + sizeof(packet_header_t);
for(int i = size - 1; i >= 0; i--){
if(buf[i] == p->start){
buf[i] = nonkey_count;
nonkey_count = 0;
}
else{
nonkey_count++;
}
}
p->key = nonkey_count;
2017-02-03 23:48:20 +00:00
crc16_t crc = crc16_init();
crc16_update(crc, (void*)p, size);
p->crc = crc16_finalize(crc);
2015-10-10 22:00:26 +00:00
}
2017-02-03 23:48:20 +00:00
int unbuff_packet(packet_header_t* p, uint8_t size){
2015-10-10 22:00:26 +00:00
uint8_t temp;
uint8_t* buf = ((uint8_t*)p) + sizeof(packet_header_t);
for(int j = p->key; j < size;){
temp = buf[j];
buf[j] = p->start;
j += temp + 1;
}
2017-02-03 23:48:20 +00:00
crc16_t crc = crc16_init();
crc16_update(crc, (void*)p, size);
if(p->crc == crc16_finalize(crc)){
return 1;
}else{
return 0;
}
2015-10-10 22:00:26 +00:00
}