00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "pic_packet.h"
00048
00049
00050 typedef union _rf_packet {
00051 rf_packet_det d;
00052 char a[PKT_PACKET_SIZE];
00053 } rf_packet;
00054
00055
00056
00057 typedef struct _sending_item {
00058 rf_packet packet;
00059 uns16 tick_sent;
00060 uns8 sent_count;
00061 uns8 flag;
00062 } sending_item;
00063
00064
00065 typedef struct _seen_packet {
00066 uns16 pkt_id;
00067 uns16 source_addr;
00068 } seen_packet;
00069
00070 static seen_packet pkt_seen_list[PKT_SEEN_LIST_SIZE];
00071 static sending_item pkt_tx_queue[PKT_TX_QUEUE_SIZE];
00072
00073 uns8 pkt_seen_list_last = 0;
00074 uns16 pkt_my_addr = 0x66;
00075 uns16 pkt_my_next_pkt_id = 0;
00076
00077
00078 #define PKT_FLAG_DELETED 0xff
00079
00080 uns8 pkt_seen(uns16 pkt_id, uns16 source_addr) {
00081
00082 uns8 count;
00083
00084 for (count = 0; count < PKT_SEEN_LIST_SIZE; count++ ) {
00085 if ((pkt_seen_list[count].pkt_id == pkt_id) &&
00086 (pkt_seen_list[count].source_addr == source_addr)) {
00087 return 1;
00088 }
00089 }
00090 return 0;
00091 }
00092
00093 void pkt_calc_check_byte(rf_packet *packet) {
00094
00095 uns8 calc, count;
00096
00097 calc = 0;
00098 for (count = 0; count < PKT_PACKET_SIZE -1; count++) {
00099 calc ^= packet->a[count];
00100 }
00101 packet->d.check_byte = calc;
00102 }
00103
00104 uns8 pkt_check_check_byte(rf_packet *packet) {
00105
00106 uns8 calc, count;
00107
00108 calc = 0;
00109 for (count = 0; count < PKT_PACKET_SIZE -1; count++) {
00110 calc ^= packet->a[count];
00111 }
00112
00113 if (calc == packet->d.check_byte)
00114 return 1;
00115 else
00116 return 0;
00117
00118 }
00119
00120 uns8 pkt_print_packet(rf_packet *my_packet) {
00121
00122 serial_print_str("[Pkt: s:");
00123 serial_print_int_hex(my_packet->d.source_addr);
00124 serial_print_str(" i:");
00125 serial_print_int_hex(my_packet->d.pkt_id);
00126 serial_print_str(" d:");
00127 serial_print_int_hex(my_packet->d.dest_addr);
00128 serial_print_str(" r1:");
00129 serial_print_int_hex(my_packet->d.r1_addr);
00130 serial_print_str(" r2:");
00131 serial_print_int_hex(my_packet->d.r2_addr);
00132 serial_print_str(" r3:");
00133 serial_print_int_hex(my_packet->d.r3_addr);
00134 serial_print_str(" p:");
00135 for (uns8 count = 0; count < PKT_PAYLOAD_SIZE; count++) {
00136 serial_print_int_hex(my_packet->d.payload[count]);
00137 serial_print_spc();
00138 }
00139 serial_print_str("] ");
00140 }
00141
00142
00143
00144 uns8 pkt_queue_packet(rf_packet *packet, uns8 resend)
00145 {
00146 uns8 count, found;
00147
00148 found = 0;
00149 for (count = 0; count < PKT_TX_QUEUE_SIZE; count++) {
00150 if (pkt_tx_queue[count].flag == PKT_FLAG_DELETED) {
00151 found = 1;
00152 break;
00153 }
00154 }
00155 if (found) {
00156 #ifdef PKT_DEBUG_HIGH
00157 serial_print_str(" Qin ");
00158 serial_print_int(count);
00159 #endif
00160 memcpy( (void *)&pkt_tx_queue[count],
00161 (void *)packet,
00162 PKT_PACKET_SIZE);
00163 pkt_tx_queue[count].sent_count = 0;
00164
00165 pkt_tx_queue[count].flag = resend;
00166 return PKT_STATUS_QUEUED;
00167 } else {
00168 return PKT_STATUS_TX_QUEUE_FULL;
00169 }
00170
00171 }
00172
00173
00174 uns8 pkt_process_rf_data(uns8 *data_in) {
00175
00176 uns8 status = 0;
00177 uns8 count;
00178 uns16 orig_pkt_id;
00179 uns8 ack_payload[PKT_PAYLOAD_SIZE] = { 0xff, 0xff };
00180 sending_item *pitem;
00181 rf_packet packet;
00182 start_crit_sec();
00183
00184 memcpy( (void *)&packet,
00185 (void *)data_in,
00186 PKT_PACKET_SIZE);
00187 end_crit_sec();
00188
00189 #ifdef PKT_DEBUG
00190 serial_putc('r');
00191 pkt_print_packet(&packet);
00192 #endif
00193 if (!pkt_check_check_byte(&packet)) {
00194 #ifdef PKT_DEBUG_HIGH
00195 serial_print_str("CF! ");
00196 #endif
00197 return PKT_STATUS_CHECK_FAIL;
00198 }
00199
00200
00201 if ((packet.d.dest_addr == pkt_my_addr) ||
00202 (packet.d.dest_addr == PKT_BROADCAST_ADDR)) {
00203 status = PKT_STATUS_PKT_IS_FOR_ME;
00204
00205 if ((packet.d.payload[0] != 0xff) ||
00206 (packet.d.payload[1] != 0xff)) {
00207 ack_payload[2] = packet.d.pkt_id & 0xff;
00208 ack_payload[3] = packet.d.pkt_id >> 8;
00209 #ifndef PKT_DEBUG_NO_TRANSMIT
00210 uns8 send_status = pkt_send_payload(packet.d.source_addr, ack_payload, PKT_FLAG_NO_RESEND);
00211 #ifdef PKT_DEBUG
00212 serial_print_str("ACKst=");
00213 serial_print_int(send_status);
00214 #endif
00215 #endif
00216 } else {
00217 status = PKT_STATUS_PKT_IS_ACK_FOR_ME;
00218 orig_pkt_id = packet.d.payload[3];
00219 orig_pkt_id <<= 8;
00220 orig_pkt_id += packet.d.payload[2];
00221 for (count = 0; count < PKT_TX_QUEUE_SIZE; count++) {
00222 pitem = &pkt_tx_queue[count];
00223 if ((pitem->flag != PKT_FLAG_DELETED) &&
00224 (pitem->packet.d.pkt_id == orig_pkt_id)) {
00225 status = PKT_STATUS_PKT_IS_FACK_FOR_ME;
00226 #ifdef PKT_CALLBACK_ON_SEND_SUCCEEDED
00227 pkt_send_succeeded_callback(pitem->packet.d.dest_addr, pitem->packet.d.pkt_id);
00228 #endif
00229
00230 pitem->flag = PKT_FLAG_DELETED;
00231 }
00232 }
00233 }
00234
00235
00236
00237 if (!pkt_seen(packet.d.pkt_id, packet.d.source_addr)) {
00238
00239
00240 if ((status != PKT_STATUS_PKT_IS_FACK_FOR_ME) &&
00241 (status != PKT_STATUS_PKT_IS_ACK_FOR_ME)) {
00242 pkt_payload_rx_callback(packet.d.source_addr, packet.d.pkt_id, packet.d.payload);
00243 }
00244
00245 pkt_seen_list_last++;
00246 if (pkt_seen_list_last == PKT_SEEN_LIST_SIZE) {
00247 pkt_seen_list_last = 0;
00248 }
00249 pkt_seen_list[pkt_seen_list_last].pkt_id = packet.d.pkt_id;
00250 pkt_seen_list[pkt_seen_list_last].source_addr = packet.d.source_addr;
00251 } else {
00252 status = PKT_STATUS_PKT_FOR_ME_BUT_SEEN;
00253 #ifdef PKT_DEBUG_HIGH
00254 serial_print_str(" seen ");
00255 #endif
00256 }
00257 }
00258 else
00259
00260 if (pkt_seen(packet.d.pkt_id, packet.d.source_addr)) {
00261
00262 status = PKT_STATUS_SEEN_BEFORE;
00263 } else
00264
00265 if (packet.d.source_addr == pkt_my_addr) {
00266 status = PKT_STATUS_I_AM_SENDER;
00267 } else
00268
00269 if (packet.d.r1_addr == PKT_DIRECT_SEND_ADDR) {
00270 status = PKT_STATUS_DIRECT_SEND;
00271 } else
00272
00273 if ((packet.d.r1_addr == pkt_my_addr) ||
00274 (packet.d.r2_addr == pkt_my_addr) ||
00275 (packet.d.r3_addr == pkt_my_addr)) {
00276 status = PKT_STATUS_PREVIOUS_ROUTED_VIA_ME;
00277 } else
00278
00279 if (packet.d.r3_addr != 0) {
00280 status = PKT_STATUS_ROUTING_FULL;
00281 } else {
00282 status = PKT_STATUS_NEED_TO_REBROADCAST;
00283
00284 if (packet.d.r1_addr == 0) {
00285 packet.d.r1_addr = pkt_my_addr;
00286 } else if (packet.d.r2_addr == 0) {
00287 packet.d.r2_addr = pkt_my_addr;
00288 } else {
00289 packet.d.r3_addr = pkt_my_addr;
00290 }
00291
00292 pkt_calc_check_byte(&packet);
00293
00294
00295 #ifndef PKT_DEBUG_NO_TRANSMIT
00296 pkt_queue_packet(&packet, PKT_FLAG_NO_RESEND);
00297 #endif
00298
00299 }
00300
00301 return status;
00302 }
00303
00304 #ifdef PKT_USE_24L01
00305 inline void pkt_send_packet(rf_packet *packet) {
00306
00307
00308 uns8 count;
00309
00310 #ifdef PKT_DEBUG
00311 serial_print_str(" <S>");
00312 #endif
00313 #ifdef PKT_DEBUG_HIGH
00314 pkt_print_packet(packet);
00315 #endif
00316
00317
00318
00319
00320
00321
00322
00323
00324 pic_rf_transmit((uns8 *)packet, PKT_PACKET_SIZE);
00325 }
00326
00327 #else
00328 inline void pkt_send_packet(rf_packet *packet) {
00329
00330 uns8 tx_buffer[PKT_PACKET_SIZE + 3];
00331 uns8 count;
00332
00333 #ifdef PKT_DEBUG
00334 serial_print_str(" <S>");
00335 #endif
00336 #ifdef PKT_DEBUG_HIGH
00337 pkt_print_packet(packet);
00338 #endif
00339 tx_buffer[0] = 0b11100111;
00340 tx_buffer[1] = 0b11100111;
00341 tx_buffer[2] = 0b11100111;
00342 for (count = 0; count < PKT_PACKET_SIZE; count++) {
00343 tx_buffer[count+3] = packet->a[count];
00344 }
00345 pic_rf_transmit(tx_buffer, PKT_PACKET_SIZE + 3);
00346
00347 }
00348
00349
00350 #endif
00351
00352
00353
00354 void pkt_process_tx_queue() {
00355
00356 uns8 count;
00357 uns16 current_tick;
00358 uns8 sent_count;
00359 uns8 flag;
00360 sending_item *item;
00361
00362 current_tick = tick_get_count();
00363
00364 for (count = 0; count < PKT_TX_QUEUE_SIZE; count++) {
00365 item = &pkt_tx_queue[count];
00366 flag = item->flag;
00367 if (flag != PKT_FLAG_DELETED) {
00368 sent_count = item->sent_count;
00369 if (sent_count > PKT_SEND_MAX_TRIES) {
00370 item->flag = PKT_FLAG_DELETED;
00371 #ifdef PKT_CALLBACK_ON_SEND_FAILED
00372 pkt_send_failed_callback(item->packet.d.dest_addr, item->packet.d.pkt_id);
00373 #endif
00374 #ifdef PKT_DEBUG
00375 serial_print_str(" SF!\n ");
00376 #endif
00377 } else if ((sent_count == 0)
00378 || (tick_calc_diff(item->tick_sent, current_tick)
00379 > PKT_RESEND_TICK_DELAY*sent_count + count)) {
00380
00381 item->tick_sent = current_tick;
00382
00383 #ifdef PKT_DEBUG_PAYLOAD_SENT
00384 item->packet.d.payload[7] = sent_count;
00385 pkt_calc_check_byte((rf_packet*)item);
00386 #endif
00387 #ifdef PKT_DEBUG_HIGH
00388 serial_print_str(" t=");
00389 serial_print_int(current_tick);
00390 serial_print_spc();
00391 #endif
00392 pkt_send_packet((rf_packet*)item);
00393 #ifdef PKT_CALLBACK_ON_SEND
00394 pkt_send_callback(item->packet.d.dest_addr, item->packet.d.pkt_id);
00395 #endif
00396
00397 if (flag != PKT_FLAG_RESEND) {
00398 item->flag = PKT_FLAG_DELETED;
00399 #ifdef PKT_DEBUG_HIGH
00400 serial_print_str(" SNR ");
00401 pkt_print_packet((rf_packet*)item);
00402 #endif
00403
00404 } else {
00405 item->sent_count++;
00406 #ifdef PKT_DEBUG_HIGH
00407 serial_print_str("SC ");
00408 serial_print_int(pkt_tx_queue[count].sent_count);
00409 serial_print_spc();
00410 #endif
00411
00412
00413 if (item->packet.d.r1_addr == 0xffff) {
00414 item->packet.d.r1_addr = 0;
00415 }
00416 }
00417 }
00418 }
00419 }
00420 }
00421
00422
00423 void pkt_init(uns16 my_addr, uns16 last_sent_id) {
00424
00425 uns8 count;
00426 uns16 current_tick;
00427
00428 pkt_my_addr = my_addr;
00429 pkt_my_next_pkt_id = last_sent_id + 1;
00430 for (count = 0; count < PKT_TX_QUEUE_SIZE; count++) {
00431 pkt_tx_queue[count].flag = PKT_FLAG_DELETED;
00432 }
00433
00434 for (count = 0; count < PKT_SEEN_LIST_SIZE; count++ ) {
00435 pkt_seen_list[count].source_addr = 0xffff;
00436 }
00437 }
00438
00439
00440 uns8 pkt_send_payload(uns16 dest_addr, uns8 *payload, uns8 resend) {
00441
00442 rf_packet my_packet;
00443 uns8 count;
00444
00445 #ifdef PKT_DEBUG_HIGH
00446 serial_print_str("\npkt send d:");
00447 serial_print_int(dest_addr);
00448 serial_print_str("s: ");
00449 serial_print_int(pkt_my_addr);
00450 serial_print_str("\n");
00451 #endif
00452
00453
00454
00455 my_packet.d.source_addr = pkt_my_addr;
00456 my_packet.d.pkt_id = pkt_my_next_pkt_id++;
00457 my_packet.d.dest_addr = dest_addr;
00458 my_packet.d.r1_addr = 0;
00459 if ((dest_addr != PKT_BROADCAST_ADDR) &&
00460 ((payload[0] != 0xff) || (payload[1] != 0xff))) {
00461 my_packet.d.r1_addr = PKT_DIRECT_SEND_ADDR;
00462 }
00463 my_packet.d.r2_addr = 0;
00464 my_packet.d.r3_addr = 0;
00465 for (count = 0; count < PKT_PAYLOAD_SIZE; count++) {
00466 my_packet.d.payload[count] = payload[count];
00467 }
00468 pkt_calc_check_byte(&my_packet);
00469
00470 #ifdef PKT_DEBUG
00471 serial_print_str(" PS");
00472 pkt_print_packet(&my_packet);
00473 #endif
00474
00475
00476
00477 return pkt_queue_packet(&my_packet, resend);
00478 }
00479
00480
00481
00482