6 #include "contiki-conf.h"
8 #include "dev/protobuf-handler.h"
12 #ifdef PROTOBUF_HANDLER_DEBUG
13 #define PRINTF(...) printf(__VA_ARGS__)
17 static void (*writebyte)(
unsigned char c);
19 static process_event_t callback_event;
20 static struct process *callback_process;
21 static uint8_t processed_data[PROTBUF_MAX_MESSAGE_LENGTH -4];
22 static protobuf_data_t callback_data;
25 static uint16_t crc16_up(uint16_t crc, uint8_t a);
33 crc16_up(uint16_t crc, uint8_t a)
40 for (i = 0; i < 8; ++i){
42 crc = (crc >> 1) ^ 0xA001;
56 callback_event = (int)
NULL;
57 callback_process =
NULL;
64 protobuf_process_message(uint8_t *buf, uint8_t bytes)
66 uint16_t rec_crc, cal_crc;
67 uint8_t processed_data_length;
72 PRINTF(
"Spurious interrupt, ignoring\n");
75 PRINTF(
"TOO small for valid protocol buffer: %d\n", bytes);
79 #ifdef PROTOBUF_HANDLER_DEBUG
80 printf(
"Bytes recieved: %i\n", bytes);
82 printf(
"%i,", (
int)buf[i++]);
87 if(buf[1] != PROTBUF_OPCODE_RESPONSE){
88 PRINTF(
"not a response packet so ignoring\n");
89 }
else if(buf[0] != PROTBUF_MASTER_ADDR){
90 printf(
"not for me: ignoring");
92 rec_crc = ((uint16_t)buf[bytes - 1] << 8) | buf[bytes-2];
93 PRINTF(
"Recieved CRC: %d\n", rec_crc);
94 for(i=0; i < bytes-2; i++){
95 cal_crc = crc16_up(cal_crc, buf[i]);
97 PRINTF(
"Calculated CRC: %d\n", cal_crc);
98 if (rec_crc == cal_crc){
99 PRINTF(
"CRCs match\n");
100 processed_data_length = bytes -4;
101 PRINTF(
"Raw data: %d processed data: %d\n", bytes, processed_data_length);
102 memcpy(&processed_data, buf+2, processed_data_length);
104 #ifdef PROTOBUF_HANDLER_DEBUG
105 printf(
"Callback_data\n");
106 for(i=0; i<processed_data_length; i++){
107 printf(
"%d:",processed_data[i]);
111 callback_data.length = processed_data_length;
112 callback_data.data = processed_data;
113 if(callback_process !=
NULL){
114 process_post(callback_process, callback_event, &callback_data);
115 PRINTF(
"Process posted\n");
117 printf(
"No callback registered\n");
120 printf(
"CRCs do not match: Ignoring\n");
131 void protobuf_send_message(uint8_t
addr, uint8_t opcode, uint8_t *payload,
132 int8_t payload_length){
133 PRINTF(
"protobuf_send_message\n");
134 uint8_t buf[PROTBUF_MAX_MESSAGE_LENGTH];
135 uint8_t buf_length = 0;
136 uint16_t crc = 0xFFFF;
138 #ifdef PROTOBUF_HANDLER_DEBUG
139 printf(
"Dest: %02x\n", addr);
140 printf(
"Optcode: %02x\n", opcode);
141 printf(
"Payload length: %i\n", payload_length);
145 if (payload_length +4 > PROTBUF_MAX_MESSAGE_LENGTH){
148 printf(
"Message too long to be sent\n");
151 buf[buf_length++] =
addr;
152 buf[buf_length++] = opcode;
154 if (payload_length == 0){
158 for(i=0; i < payload_length; i++){
159 PRINTF(
"payload:%i,", payload[i]);
160 buf[buf_length++] = payload[i];
161 PRINTF(
"buf:%i\n", buf[buf_length-1]);
164 for(i=0; i < buf_length; i++){
165 crc = crc16_up(crc, buf[i]);
167 buf[buf_length++] = crc & 0xFF;
168 buf[buf_length++] = (crc >> 8) & 0xFF;
169 PRINTF(
"CRC: %04x\n", crc);
170 PRINTF(
"CRC low: %02x\n", crc & 0xFF);
171 PRINTF(
"CRC high: %02x\n", (crc >>8) & 0xFF);
174 if(writebyte !=
NULL){
176 for(i=0; i < buf_length; i++){
180 PRINTF(
"Finsihed\n");
182 printf(
"No writebyte specified\n");
186 void protobuf_handler_set_writeb(
void (*wb)(
unsigned char c)){
191 void protobuf_register_process_callback(
struct process *p, process_event_t ev){
192 PRINTF(
"Protobuf callback registered\n");
static uip_ds6_addr_t * addr
Pointer to a router list entry.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
#define NULL
The null pointer.