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
00048
00049 #include "its_mode2.h"
00050 #include "its_common.h"
00051 #include "wpan.h"
00052 #include "mrf24j40.h"
00053
00054 #include "config.h"
00055 #include "pic_serial.h"
00056 #include "pic_tick.h"
00057 #include "pic_timer.h"
00058 #include "memory.h"
00059
00060 #include "debug.h"
00061
00062 its2_state state = STATE_STARTUP;
00063
00064 bit debug_module =0;
00065 bit queue_processing = 0;
00066
00067 uns8 controller_handle;
00068 uns16 tick_marker;
00069 uns16 state_timeout;
00070
00071 uns8 channel;
00072 bit its2_transmitting = 0;
00073 uns8 its2_seen_index;
00074
00075 static seen_packet its2_seen_list[ITS2_SEEN_LIST_SIZE];
00076 static queued_item its2_tx_queue[ITS2_TX_QUEUE_SIZE];
00077
00078
00079
00080
00081
00082 void turn_off_mrf_interrupts() {
00084 nop();
00085 nop();
00086 nop();
00087 nop();
00088 }
00089
00090 void turn_on_mrf_interrupts() {
00092 }
00093
00094
00095 void its2_setup_io() {
00096 wpan_setup_io();
00097 }
00098
00099 uns8 its2_find_free_queue_slot() {
00100
00101 bit found;
00102 uns8 count;
00103
00104 found = 0;
00105 for (count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00106 if (its2_tx_queue[count].flag == ITS2_FLAG_DELETED) {
00107 found = 1;
00108 break;
00109 }
00110 }
00111 if (found) {
00112 return count;
00113 } else {
00114 return 0xff;
00115 }
00116 }
00117
00118 its2_result its2_router_handle_association(uns16 pan_id, uns16 its_device_id) {
00119
00120 its_device_handle device_handle;
00121
00122 device_handle = its_get_device_handle(its_device_id);
00123
00124 if (device_handle == ITS_DEVICE_NONE) {
00125
00126 device_handle = its_add_local_device( its_device_id, pan_id, its_device_id);
00127 if (device_handle == ITS_DEVICE_NONE) {
00128
00129 debug_str("Too many devices! association failed");
00130 return RESULT_FAILED;
00131 } else {
00132
00133 its_transmit_to_handle(device_handle, ITS_ASSOC_RES, 0, 0);
00134 return RESULT_SUCCESSFUL;
00135
00136 }
00137
00138 }
00139 }
00140
00141
00142
00143 #ifdef ITS_MODE2_ROUTER
00144
00145
00146
00147
00148
00149 uns8 its2_update_route(queued_item *item) {
00150
00151 its_device_info *device_info;
00152 its_device_info *router_info;
00153 uns16 route_list[ITS2_MAX_HOP_COUNT];
00154 uns8 router_handle;
00155 uns8 count;
00156 uns8 count_hops;
00157 uns16 hop;
00158
00159 debug_str("\n Routing: ");
00160 debug_int_hex_16bit(item->dest_its_device_id);
00161 hop = item->dest_its_device_id;
00162 item->dest_device_handle = its_get_device_handle(hop);
00163 if (item->dest_device_handle != ITS_DEVICE_NONE) {
00164 debug_str(" known ");
00165 count_hops = 0;
00166 while (count_hops < ITS2_MAX_HOP_COUNT) {
00167 router_handle = its_get_device_handle(hop);
00168 router_info = its_get_device_info(router_handle);
00169 if (router_info->addr.remote.remote_indicator != 0xffff) {
00170 item->dest_device_handle = router_handle;
00171 debug_int_hex_16bit(hop);
00172 debug_str(" (local) ");
00173 break;
00174 } else {
00175 debug_str(" via ");
00176 debug_int_hex_16bit(router_info->addr.remote.prior_device_id);
00177 route_list[count_hops] = router_info->addr.remote.prior_device_id;
00178 hop = router_info->addr.remote.prior_device_id;
00179 count_hops++;
00180 }
00181 }
00182 if (count_hops == ITS2_MAX_HOP_COUNT) {
00183 debug_str(" MAX HOPS EXCEEDED! ");
00184 return ITS2_UPDATE_ROUTE_FAIL;
00185 }
00186
00187
00188 item->packet.num_routes = count_hops;
00189 count = 0;
00190 debug_str(" Route via [");
00191
00192 while (count_hops > 0) {
00193 debug_var(" ", route_list[count_hops-1]);
00194 item->packet.routers[count] = route_list[count_hops-1];
00195 count++;
00196 count_hops--;
00197 }
00198 debug_str("] ");
00199 item->status = QS_READY_TO_SEND;
00200 } else {
00201
00202 if (item->dest_its_device_id == 0xffff) {
00203 item->packet.num_routes = 0;
00204 item->status = QS_READY_TO_SEND;
00205 } else {
00206 debug_str("Unknown, trying local ");
00207 item->status = QS_WAITING_ON_LOCAL_ADDR;
00208 }
00209 }
00210 return ITS2_UPDATE_ROUTE_SUCCESS;
00211 }
00212
00213 void its2_respond_ack(uns16 its_device_id, uns8 sequence) {
00214 debug_str("Sending ACK");
00215 its2_router_queue_packet(its_device_id, ITS_ACK, &sequence, 1, ITS2_FLAG_NO_ACK);
00216 }
00217
00218 uns8 its2_seen_packet(uns16 its_source_id, uns8 sequence) {
00219
00220 for (uns8 count=0; count < ITS2_SEEN_LIST_SIZE; count++) {
00221 if ((its2_seen_list[count].its_source_id == its_source_id) &&
00222 (its2_seen_list[count].sequence == sequence)) {
00223 return 1;
00224 }
00225 }
00226 return 0;
00227 }
00228
00229 its2_add_to_seen_list(uns16 its_source_id, uns8 sequence) {
00230
00231 its2_seen_list[its2_seen_index].its_source_id = its_source_id;
00232 its2_seen_list[its2_seen_index].sequence = sequence;
00233 its2_seen_index++;
00234 if (its2_seen_index == ITS2_SEEN_LIST_SIZE) {
00235 its2_seen_index = 0;
00236 }
00237 }
00238
00239
00240
00241 void wpan_data_received_callback(wpan_address *addr, uns8 *data, uns8 data_size, uns8 lqi, uns8 rssi) {
00242
00243
00244 uns8 device_handle;
00245 uns8 count;
00246
00247 its2_packet pkt;
00248 uns16 source_pan_id;
00249 uns16 source_sa;
00250
00251 turn_off_mrf_interrupts();
00252
00253 #ifdef debug_on
00254 wpan_print_address(addr);
00255 #endif
00256
00257 debug_int(data_size);
00258 debug_str(" bytes (");
00259
00260
00261
00262 #ifdef debug_on
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 debug_var(") lqi=", lqi);
00277 debug_var(" rssi=", rssi);
00278
00279 #endif
00280
00281 if (debug_module) {
00282 if (addr->source_sa == 0x0001) {
00283 debug_str(" IGNORING ");
00284 return;
00285 }
00286 }
00287
00288
00289 if ((data_size > 2) && (data[POS_KEY1] == 'I') && (data[POS_KEY2] == 'T')) {
00290 debug_str("ITS Pkt!\n");
00291 uns8 length_header = data[POS_LENGTH_HEADER];
00292 uns8 length_data = data[3+length_header+1];
00293 uns8 data_start = 3+length_header;
00294 uns16 my_id = its_get_device_id();
00295 source_pan_id = addr->source_pan_id;
00296 source_sa = addr->source_sa;
00297 memcpy( (void *)&pkt,
00298 (void *)&data[POS_PKT_TYPE],
00299 data_size - POS_PKT_TYPE);
00300 its2_print_packet(&pkt);
00301
00302
00303
00304 bit i_am_in_route_list = 0;
00305 for (uns8 count = 0; count < pkt.hop_count; count++) {
00306 if (pkt.routers[count] == my_id) {
00307 i_am_in_route_list = 1;
00308 }
00309 }
00310
00311
00312
00313
00314 if ((pkt.its_source_id != my_id) && (!i_am_in_route_list)) {
00315
00316 if (pkt.hop_count == 0) {
00317 debug_str("\n Device ");
00318 debug_int_hex_16bit(pkt.its_source_id);
00319 device_handle = its_get_device_handle(pkt.its_source_id);
00320
00321 if (device_handle == ITS_DEVICE_NONE) {
00322
00323 debug_str(" (adding)");
00324 device_handle = its_add_local_device( pkt.its_source_id, source_pan_id, source_sa);
00325 if (device_handle == ITS_DEVICE_NONE) {
00326
00327 debug_str(" but table full\n");
00328 }
00329 } else {
00330 debug_str(" known device\n");
00331 }
00332 } else {
00333 uns16 last_route = pkt.routers[pkt.hop_count-1];
00334 debug_var("Last route: ", last_route);
00335 device_handle = its_get_device_handle(last_route);
00336 if (device_handle == ITS_DEVICE_NONE) {
00337 debug_str(" adding ");
00338 device_handle = its_add_local_device( last_route, source_pan_id, source_sa);
00339 }
00340
00341
00342 if (pkt.hop_count > 1) {
00343 for (uns8 count = pkt.hop_count - 2; count != 255; count--) {
00344 device_handle = its_get_device_handle(last_route);
00345 if (device_handle == ITS_DEVICE_NONE) {
00346 its_add_net_device( last_route, pkt.routers[count]);
00347 }
00348 last_route = pkt.routers[count];
00349 }
00350 }
00351
00352
00353 device_handle = its_get_device_handle(pkt.its_source_id);
00354 if (device_handle == ITS_DEVICE_NONE) {
00355 debug_str(" (add rte)");
00356 device_handle = its_add_net_device( pkt.its_source_id, last_route);
00357 }
00358
00359 }
00360 } else {
00361 debug_str("NOTADDING");
00362 }
00364 debug_nl();
00365
00366 if (pkt.packet_type == ITS_ASSOC_REQ) {
00367 debug_str("ITS Assoc req\n");
00368
00369
00370
00371
00372
00373
00374
00375 if (pkt.its_network_id == my_id) {
00376
00377
00378
00379
00380 its2_router_handle_association( pkt.its_network_id, pkt.its_source_id);
00381 } else {
00382 debug_str("Not for us, for network: ");
00383 debug_int_hex_16bit(pkt.its_network_id);
00384 }
00385 }
00386 else if (pkt.packet_type == ITS_LOCAL_DISCOVER_REQ) {
00387 debug_str(" Local disc");
00388 if (pkt.its_dest_id == my_id) {
00389 debug_str(" for me!");
00390 its2_respond_local_addr(pkt.its_source_id);
00391 } else {
00392 debug_str(" not for me");
00393 }
00394 }
00395 else if (pkt.packet_type == ITS_NET_DISCOVER_REQ) {
00396 debug_str(" Net disc");
00397 if (pkt.its_dest_id == my_id) {
00398 debug_str(" for me!");
00399 its2_respond_net_addr(pkt.its_source_id);
00400 } else {
00401 debug_str(" not for me");
00402
00403
00404
00405
00406
00407
00408 if ((pkt.its_source_id != my_id) &&
00409 (!i_am_in_route_list) &&
00410 (!its2_seen_packet(pkt.its_source_id, pkt.sequence))) {
00411 its2_rebroadcast_net_discover_req(&pkt);
00412 } else {
00413 debug_str(" NOT ");
00414 }
00415 debug_str(" rebroadcasting ");
00416 }
00417 }
00418 else if (pkt.packet_type == ITS_LOCAL_DISCOVER_RES) {
00419 debug_str(" Local disc resp");
00420
00421 for (uns8 count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00422 if ((its2_tx_queue[count].flag != ITS2_FLAG_DELETED) &&
00423 (its2_tx_queue[count].packet.its_dest_id == pkt.its_source_id)) {
00424 debug_str(" Found match, changing status\n");
00425 its2_tx_queue[count].dest_device_handle = device_handle;
00426 its2_tx_queue[count].status = QS_READY_TO_SEND;
00427 break;
00428 }
00429 }
00430
00431 }
00432 else if (pkt.packet_type == ITS_GENERIC_DATA) {
00433 debug_str(" Generic data pkt");
00434 if (pkt.its_dest_id == my_id) {
00435 debug_str(" for us");
00436
00437 its2_respond_ack(pkt.its_source_id, pkt.sequence);
00438 its2_router_receive_callback(pkt.its_source_id, &data[data_start], data_size - data_start);
00439 } else {
00440 debug_var(" route via ", pkt.routers[pkt.hop_count-1]);
00441 its2_forward_routed_packet(&pkt, &data[data_start], data_size - data_start);
00442 }
00443 }
00444 else if (pkt.packet_type == ITS_ACK) {
00445 debug_str("Got ACK! seq=");
00446 debug_int(data[data_start]);
00447 debug_spc();
00448 if (pkt.its_dest_id == my_id) {
00449 bit found = 0;
00450 for (uns8 count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00451 if ((its2_tx_queue[count].flag != ITS2_FLAG_DELETED) &&
00452 (its2_tx_queue[count].packet.its_source_id == my_id) &&
00453 (its2_tx_queue[count].packet.sequence == data[data_start])) {
00454 debug_str(" Match! ");
00455 its2_tx_queue[count].status = QS_ACK_RECEIVED;
00456 found = 1;
00457 break;
00458 }
00459 }
00460 if (!found) {
00461 debug_str(" <Didn't find. Odd.>");
00462 }
00463 } else {
00464 debug_str(" Not for me, routing ");
00465 its2_forward_routed_packet(&pkt, &data[data_start], data_size - data_start);
00466 }
00467 }
00468 else if (pkt.packet_type == ITS_NET_DISCOVER_RES) {
00469 debug_str(" Net disc RES");
00470 if (pkt.its_dest_id == my_id) {
00471 debug_str(" for us!");
00472 bit found = 0;
00473 for (uns8 count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00474 if ((its2_tx_queue[count].flag != ITS2_FLAG_DELETED) &&
00475 (its2_tx_queue[count].packet.its_dest_id == pkt.its_source_id)) {
00476 debug_str(" Found match, updating route\n");
00477 uns8 update_route_result = its2_update_route(&its2_tx_queue[count]);
00478 if (update_route_result == ITS2_UPDATE_ROUTE_FAIL) {
00479 debug_str(" Route failed\n");
00480 its2_tx_queue[count].status = QS_ROUTING_FAILED;
00481 }
00482 found = 1;
00483 break;
00484 }
00485 }
00486 if (!found) {
00487 debug_str(" <Didn't find. Odd.>");
00488 }
00489 } else {
00490
00491 debug_var(" route via ", pkt.routers[pkt.hop_count-1]);
00492 its2_print_packet(&pkt);
00493 its2_forward_routed_packet(&pkt, &data[data_start], data_size - data_start);
00494 }
00495 }
00496 else {
00497 debug_var(" Pkt unknown type ", pkt.packet_type);
00498 }
00499 }
00500
00501 turn_on_mrf_interrupts();
00502
00503 }
00504
00505 #else
00506
00507
00508 void wpan_data_received_callback(wpan_address *addr, uns8 *data, uns8 data_size) {
00509
00510 uns8 count;
00511 wpan_print_address(addr);
00512
00513 debug_str(" Data: ");
00514 for (count = 0; count < data_size; count++) {
00515 debug_int_hex(data[count]);
00516 debug_putc(' ');
00517 if ((data[count] > 31) && (data[count] < 127)) {
00518 debug_putc(data[count]);
00519 } else {
00520 debug_putc('?');
00521 }
00522 debug_putc(' ');
00523 }
00524 debug_nl();
00525
00526
00527 if ((data_size > 2) && (data[0] == 'I') && (data[1] == 'T')) {
00528 debug_str("ITS Packet received\n");
00529 uns8 length_header = data[2];
00530 uns8 length_data = data[3+length_header+1];
00531 uns8 data_start = 3+length_header+2;
00532
00533 if (data[3] == ITS_ASSOC_RES) {
00534 debug_str("Received association response\n");
00535 if (state == STATE_SEARCHING) {
00536 debug_str("Changing state to associated\n");
00537 uns16 controller_device_id = data[8];
00538 controller_device_id <<= 8;
00539 controller_device_id += data[7];
00540
00541 uns8 device_handle = its_get_device(controller_device_id);
00542
00543 if (device_handle == ITS_DEVICE_NONE) {
00544
00545 device_handle = its_add_device( controller_device_id, addr->source_pan_id, controller_device_id);
00546 if (device_handle == ITS_DEVICE_NONE) {
00547
00548 debug_str("Too many devices! association failed");
00549 state = STATE_UNASSOCIATED;
00550 } else {
00551 controller_handle = device_handle;
00552 state = STATE_ASSOCIATED;
00553 }
00554 } else {
00555 state = STATE_ASSOCIATED;
00556 }
00557
00558 }
00559 }
00560 else if (data[3] == ITS_GENERIC_DATA) {
00561
00562
00563
00564 debug_str("Generic data pkt\n");
00565
00566 uns16 device_id = data[8];
00567 device_id <<= 8;
00568 device_id += data[7];
00569 its2_device_receive_callback(&data[data_start], data_size - data_start);
00570
00571 }
00572 }
00573 }
00574 #endif
00575
00576
00577
00578
00579
00580 void its2_print_packet(its2_packet *pkt) {
00581
00582 debug_str("(pkt type=");
00583 debug_int(pkt->packet_type);
00584 debug_str(" seq=");
00585 debug_int(pkt->sequence);
00586 debug_str(" net=");
00587 debug_int_hex_16bit(pkt->its_network_id);
00588 debug_str(" src=");
00589 debug_int_hex_16bit(pkt->its_source_id);
00590 debug_str(" dst=");
00591 debug_int_hex_16bit(pkt->its_dest_id);
00592 debug_str(" max_hop=");
00593 debug_int(pkt->max_hop_count);
00594 debug_str(" num_rts=");
00595 debug_int(pkt->num_routes);
00596 debug_str(" hop_cnt=");
00597 debug_int(pkt->hop_count);
00598
00599 uns8 count;
00600 debug_str(" Rts=");
00601 for (count = 0; count < pkt->num_routes; count++) {
00602 if (count > ITS2_MAX_HOP_COUNT) {
00603 debug_str(" Too many routes! ");
00604 break;
00605 } else {
00606 debug_putc(' ');
00607 debug_int_hex_16bit(pkt->routers[count]);
00608 }
00609 }
00610 debug_str(") ");
00611 }
00612
00613 its2_result its2_rebroadcast_net_discover_req(its2_packet *pkt) {
00614
00615 uns8 queue_slot;
00616 queued_item *item;
00617
00618 if (pkt->hop_count == pkt->max_hop_count) {
00619 debug_str("<Can't re-b: max hop count exceeded!>");
00620 return ROUTING_TOO_MANY_HOPS;
00621 }
00622 queue_slot = its2_find_free_queue_slot();
00623 if (queue_slot != ITS2_NO_AVAILABLE_SLOTS) {
00624
00625 debug_var("<Qed>", queue_slot);
00626 item = &its2_tx_queue[queue_slot];
00627
00628 item->flag = ITS2_FLAG_NO_ACK;
00629 memcpy( (void *)&item->packet,
00630 (void *)pkt,
00631 11 + (pkt->num_routes * 2));
00632 item->dest_its_device_id = 0xffff;
00633 item->packet.routers[item->packet.hop_count] = its_get_device_id();
00634 item->packet.hop_count++;
00635 item->packet.num_routes++;
00636 item->sent_count = 0;
00637 item->dest_device_handle = ITS_DEVICE_NONE;
00638 item->data_length = 0;
00639 item->data = NULL;
00640
00641
00642 item->status = QS_READY_TO_SEND;
00643
00644 its2_add_to_seen_list(item->packet.its_source_id, item->packet.sequence);
00645
00646 return ITEM_QUEUED;
00647 } else {
00648 return QUEUE_FULL;
00649 }
00650
00651 }
00652
00653 its2_result its2_forward_routed_packet(its2_packet *pkt, uns8 *data, uns8 data_length) {
00654
00655 uns8 queue_slot;
00656 queued_item *item;
00657 void *p;
00658
00659 queue_slot = its2_find_free_queue_slot();
00660
00661 if (queue_slot != ITS2_NO_AVAILABLE_SLOTS) {
00662
00663 debug_var("<Qed>", queue_slot);
00664 debug_str(" data lengh=");
00665 debug_int(data_length);
00666 debug_spc();
00667
00668 item = &its2_tx_queue[queue_slot];
00669 item->flag = ITS2_FLAG_NO_ACK;
00670 memcpy( (void *)&item->packet,
00671 (void *)pkt,
00672 11 + (pkt->num_routes * 2));
00673
00674 its2_print_packet(&item->packet);
00675
00676 item->packet.hop_count++;
00677
00678
00679
00680
00681
00682 if (item->packet.hop_count == item->packet.num_routes) {
00683
00684 debug_str(" going to final=");
00685 item->dest_its_device_id = item->packet.its_dest_id;
00686 } else {
00687
00688 debug_str(" going to next hop=");
00689 item->dest_its_device_id = item->packet.routers[item->packet.hop_count];
00690 }
00691
00692 debug_int_hex_16bit(item->dest_its_device_id);
00693
00694 item->sent_count = 0;
00695 item->dest_device_handle = its_get_device_handle(item->dest_its_device_id);
00696
00697
00698 if (item->dest_device_handle == ITS_DEVICE_NONE) {
00699
00700 debug_str(" unknown. Looking for local. ");
00701 item->status = QS_WAITING_ON_LOCAL_ADDR;
00702 } else {
00703
00704 its_device_info *device_info = its_get_device_info(item->dest_device_handle);
00705
00706 if (device_info->addr.remote.remote_indicator == 0xffff) {
00707 debug_str(" Not local! Abandon ship ");
00708 item->flag = ITS2_FLAG_DELETED;
00709 return NEXT_HOP_NOT_LOCAL;
00710
00711
00712 } else {
00713 debug_str(" found locally. ");
00714 item->status = QS_READY_TO_SEND;
00715 }
00716 }
00717 item->data_length = 0;
00718 item->data = NULL;
00719
00720
00721
00722 if (data_length > 0) {
00723 p = alloc(data_length);
00724
00725 memcpy( p,
00726 (void *)data,
00727 data_length);
00728 item->data = (uns8*)p;
00729 }
00730
00731 item->data_length = data_length;
00732
00733 return ITEM_QUEUED;
00734 } else {
00735 return QUEUE_FULL;
00736 }
00737
00738 }
00739
00740
00741
00742
00743 uns8 its2_router_init(uns16 my_device_id, uns16 network_id) {
00744
00745 uns8 lowest_channel_ed;
00746 uns8 count;
00747
00748
00749
00750 its2_seen_index = 0;
00751
00752 for (count=0; count < ITS2_SEEN_LIST_SIZE; count++) {
00753 its2_seen_list[count].its_source_id = 0xffff;
00754 }
00755
00756 for (count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00757 its2_tx_queue[count].flag = ITS2_FLAG_DELETED;
00758 }
00759
00760 its_init();
00761
00762 its_set_device_id(my_device_id);
00763 its_set_network_id(network_id);
00764
00765 mrf24j40_set_pan_id(network_id);
00766 mrf24j40_set_short_address(my_device_id);
00767
00768
00769 debug_str("Router startup\n");
00770
00771 debug_var("Chosing channel", 15);
00772 debug_nl();
00773
00774
00775 mrf24j40_set_channel(15);
00776
00777 }
00778
00779 void its2_request_local_addr(uns16 device_id) {
00780 its2_router_queue_packet(device_id, ITS_LOCAL_DISCOVER_REQ, NULL, 0, ITS2_FLAG_NO_ACK);
00781 }
00782
00783 void its2_respond_local_addr(uns16 device_id) {
00784
00785 if ((device_id != 0x0001) || !debug_module) {
00786 its2_router_queue_packet(device_id, ITS_LOCAL_DISCOVER_RES, NULL, 0, ITS2_FLAG_NO_ACK);
00787 } else {
00788 debug_str("IGNORE");
00789 }
00790 }
00791
00792 void its2_request_net_addr(uns16 device_id) {
00793 its2_router_queue_packet(device_id, ITS_NET_DISCOVER_REQ, NULL, 0, ITS2_FLAG_NO_ACK);
00794 }
00795
00796 void its2_respond_net_addr(uns16 device_id) {
00797 its2_router_queue_packet(device_id, ITS_NET_DISCOVER_RES, NULL, 0, ITS2_FLAG_NO_ACK);
00798 }
00799
00800 void its2_rebroadcast_net_addr_req() {
00801 }
00802
00803
00804 void wpan_data_transmitted_callback(uns8 status, uns8 retries, uns8 channel_busy) {
00805 its2_transmitting = 0;
00806 }
00807
00808 void its2_router_process() {
00809
00810 turn_off_mrf_interrupts();
00811
00812 its2_process_tx_queue();
00813
00814 uns16 test_tick;
00815 test_tick = tick_get_count();
00816 if (tick_calc_diff(tick_marker, test_tick) >= state_timeout) {
00817
00818 }
00819 turn_on_mrf_interrupts();
00820
00821 }
00822
00823 its2_result its2_router_queue_packet(uns16 device_id, uns8 packet_type, uns8 *data, uns8 data_length, uns8 ack) {
00824
00825
00826
00827 uns8 found;
00828 void *p;
00829 queued_item *item;
00830 uns8 count;
00831
00832 turn_off_mrf_interrupts();
00833
00834 debug_str("\n[its2_router_queue_packet]\n");
00835
00836
00837 found = 0;
00838 for (count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00839 if (its2_tx_queue[count].flag == ITS2_FLAG_DELETED) {
00840 found = 1;
00841 break;
00842 }
00843 }
00844 if (found) {
00845 debug_var("Qed: ", count);
00846 item = &its2_tx_queue[count];
00847 item->flag = ack;
00848 item->sent_count = 0;
00849
00850
00851
00852
00853 item->packet.packet_type = packet_type;
00854 item->packet.sequence = its_get_next_sequence();
00855 item->packet.its_network_id = its_get_network_id();
00856 item->packet.its_source_id = its_get_device_id();
00857 item->packet.its_dest_id = device_id;
00858 if ((packet_type == ITS_LOCAL_DISCOVER_REQ) ||
00859 (packet_type == ITS_NET_DISCOVER_REQ)) {
00860 item->dest_its_device_id = 0xffff;
00861 } else {
00862 item->dest_its_device_id = device_id;
00863 }
00864 item->packet.max_hop_count = ITS2_MAX_HOP_COUNT;
00865 item->packet.hop_count = 0;
00866
00867
00868
00869
00870 if (data_length > 0) {
00871 p = alloc(data_length);
00872
00873 memcpy( p,
00874 (void *)data,
00875 data_length);
00876 item->data = (uns8*)p;
00877 }
00878
00879 item->data_length = data_length;
00880
00881
00882
00883
00884
00885 uns8 routing_result = its2_update_route(item);
00886
00887 turn_on_mrf_interrupts();
00888
00889 if (routing_result == ITS2_UPDATE_ROUTE_FAIL) {
00890 debug_str("RoUtE fail");
00891 return ROUTING_TOO_MANY_HOPS;
00892 } else {
00893 debug_str("qdone ");
00894 return ITEM_QUEUED;
00895 }
00896 } else {
00897 turn_on_mrf_interrupts();
00898 debug_str(" Full! ");
00899 return QUEUE_FULL;
00900 }
00901 }
00902
00903
00904 void its2_process_tx_queue() {
00905
00906 uns16 current_tick;
00907 uns8 count;
00908 queued_item *item;
00909 uns8 flag;
00910 uns8 status;
00911 uns16 my_id;
00912
00913
00914 current_tick = tick_get_count();
00915 my_id = its_get_device_id();
00916
00917
00918
00919
00920 for (count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
00921 item = &its2_tx_queue[count];
00922 flag = item->flag;
00923 status = item->status;
00924 if (flag == ITS2_FLAG_DELETED) {
00925 continue;
00926 }
00927
00928 if (status == QS_WAITING_ON_LOCAL_ADDR) {
00929 if (item->sent_count == ITS2_SEND_MAX_TRIES) {
00930 if (item->packet.its_source_id == my_id) {
00931 debug_str(" Tried local, now time for net search");
00932 item->sent_count = 0;
00933 item->status = QS_WAITING_ON_NETWORK_ADDR;
00934 } else {
00935
00936
00937 debug_str("Next hop not available locally. Giving up.");
00938 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_NEXT_HOP_UNKNOWN);
00939 its2_delete_item_from_queue(item);
00940 }
00941 } else if ((item->sent_count == 0) || (tick_calc_diff(item->tick_sent, current_tick)
00942 > ITS2_RESEND_TICK_DELAY)) {
00943
00944 debug_str(" REQ local address for q ");
00945 debug_int(count);
00946 debug_str(" dev ");
00947 debug_int_hex_16bit(item->dest_its_device_id);
00948
00949 item->tick_sent = current_tick;
00950 item->sent_count++;
00951 its2_request_local_addr(item->dest_its_device_id);
00952 item->tick_sent = current_tick;
00953 } else {
00954 debug_str(" Local waiting for ");
00955 debug_int(item->tick_sent);
00956 }
00957 } else
00958 if (status == QS_WAITING_ON_NETWORK_ADDR) {
00959 if (item->sent_count == ITS2_SEND_MAX_TRIES) {
00960
00961 debug_str("No addr. killing");
00962 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_NO_ROUTE);
00963 its2_delete_item_from_queue(item);
00964 } else {
00965
00966
00967
00968
00969 if (tick_calc_diff(item->tick_sent, current_tick)
00970 > ITS2_RESEND_TICK_DELAY) {
00971 item->tick_sent = current_tick;
00972 item->sent_count++;
00973 its2_request_net_addr(item->dest_its_device_id);
00974 }
00975 }
00976 } else
00977 if (status == QS_READY_TO_SEND) {
00978 if (!its2_transmitting) {
00979 debug_str("<TX!>");
00980 its2_transmit(item);
00981 debug_str("<Done>");
00982
00983 }
00984 } else
00985 if (status == QS_WAITING_ON_ACK) {
00986 if (item->sent_count > ITS2_SEND_MAX_TRIES) {
00987
00988
00989
00990 debug_str("Too many. killing.");
00991 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_NO_ACK);
00992 its2_delete_item_from_queue(item);
00993 }
00994 if (tick_calc_diff(item->tick_sent, current_tick)
00995 > ITS2_RESEND_TICK_DELAY) {
00996 if (!its2_transmitting) {
00997 debug_str("<RETRYTX!>");
00998 its2_transmit(item);
00999 debug_str("<Done>");
01000 }
01001 }
01002 }
01003 if (status == QS_ACK_RECEIVED) {
01004
01005 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_SUCCESS);
01006
01007 its2_delete_item_from_queue(item);
01008 } else
01009 if (status == QS_SENT) {
01010 debug_str(" <SENT del> ");
01011 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_SUCCESS);
01012 its2_delete_item_from_queue(item);
01013 } else
01014 if (status == QS_ROUTING_FAILED) {
01015 debug_str(" <RFAIL del> ");
01016 its2_transmit_status_callback(&item->packet, ITS2_TX_STATUS_NO_ROUTE);
01017 its2_delete_item_from_queue(item);
01018 }
01019 }
01020
01021
01022
01023 }
01024
01025 void its2_delete_item_from_queue(queued_item *item) {
01026 if (item->data_length > 0) {
01027 free((void *)item->data);
01028 }
01029 item->flag = ITS2_FLAG_DELETED;
01030 }
01031
01032
01033
01034
01035 void its2_transmit(queued_item *item) {
01036
01037 uns8 buffer[50];
01038 uns8 count;
01039 uns8 index;
01040 its_device_info *device_info;
01041
01042 its2_transmitting = 1;
01043
01044 debug_str(" X: ");
01045 its2_print_packet(&item->packet);
01046
01047 buffer[0] = 'I';
01048 buffer[1] = 'T';
01049 memcpy( (void *)&buffer[3], (void *)&item->packet, 11 );
01050
01051 index = 11 + 3;
01052 for (count = 0; count < item->packet.num_routes; count++) {
01053 buffer[index++] = item->packet.routers[count] & 0xff;
01054 buffer[index++] = item->packet.routers[count] >> 8;
01055 }
01056
01057 buffer[2] = index - 3;
01058
01059
01060 for (count = 0; count < item->data_length; count++) {
01061 buffer[index++] = item->data[count];
01062 }
01063 debug_str(" To=");
01064 debug_int_hex_16bit(item->dest_its_device_id);
01065 debug_str(" bytes=");
01066 debug_int(index);
01067
01068 item->sent_count++;
01069 item->tick_sent = tick_get_count();
01070 if (item->flag == ITS2_FLAG_ACK) {
01071 item->status = QS_WAITING_ON_ACK;
01072 } else {
01073 item->status = QS_SENT;
01074 }
01075 if (item->dest_device_handle != ITS_DEVICE_NONE) {
01076 debug_var(" Handle ", item->dest_device_handle);
01077 device_info = its_get_device_info(item->dest_device_handle);
01078 if (device_info != NULL) {
01079 debug_str(" Pan ");
01080 debug_int_hex_16bit(device_info->addr.local.pan_id);
01081 debug_str(" SA ");
01082 debug_int_hex_16bit(device_info->addr.local.short_address);
01083 } else {
01084 debug_str("!!Strange!! - NO DEVICE INFO!!\n");
01085 }
01086 mrf24j40_transmit_to_short_address(FRAME_TYPE_DATA, device_info->addr.local.pan_id,
01087 device_info->addr.local.short_address,
01088 &buffer, index, MRF_ACK);
01089 debug_str(" Sent bytes ");
01090 debug_int(index);
01091 debug_putc(' ');
01092 }
01093 else {
01094 debug_str(" It's a broadcast");
01095 mrf24j40_transmit_to_short_address(FRAME_TYPE_DATA, 0xffff,
01096 0xffff, &buffer, index, MRF_NO_ACK);
01097 }
01098
01099 }
01100
01101
01102 void its2_router_process(queued_item *item) {
01103
01104 }
01105
01106 void its2_print_queue() {
01107
01108 uns8 count;
01109 queued_item *item;
01110
01111 debug_nl();
01112 for (count = 0; count < ITS2_TX_QUEUE_SIZE; count++) {
01113 item = &its2_tx_queue[count];
01114 if (item->flag != ITS2_FLAG_DELETED) {
01115 debug_var("Q:",count);
01116 debug_var(" FL:", item->flag);
01117 debug_var(" SRC:", item->packet.its_source_id);
01118 debug_var(" DST:", item->packet.its_dest_id);
01119 debug_var(" STAT:", item->status);
01120 debug_var(" SENT:", item->sent_count);
01121 debug_var(" TICK:", item->tick_sent);
01122 debug_nl();
01123 }
01124 }
01125 debug_var("Current tick", tick_get_count());
01126 debug_nl();
01127 }
01128
01129
01130
01131
01132
01133
01134
01135
01136 its2_result its2_device_init(uns16 my_device_id, uns16 network_id) {
01137
01138
01139
01140
01141
01142 debug_str("Device startup\n");
01143
01144 its_init();
01145 wpan_init();
01146
01147 its_set_device_id(my_device_id);
01148 its_set_network_id(network_id);
01149
01150 mrf24j40_set_pan_id(network_id);
01151 mrf24j40_set_short_address(my_device_id);
01152
01153
01154 state = STATE_UNASSOCIATED;
01155 }
01156
01157 its2_result its2_find_controller() {
01158 state = STATE_SEARCHING;
01159 tick_marker = tick_get_count();
01160 channel = 0;
01161 }
01162
01163 void its2_device_process() {
01164
01165 uns16 test_tick;
01166
01167
01168
01169
01170
01171 if (state == STATE_SEARCHING) {
01172 test_tick = tick_get_count();
01173 if (tick_calc_diff(tick_marker, test_tick) >= 250) {
01174 tick_marker = test_tick;
01175 if (channel == 0) {
01176 channel = MRF_FIRST_CHANNEL;
01177 } else {
01178 if (channel == MRF_LAST_CHANNEL) {
01179
01180 state = STATE_UNASSOCIATED;
01181 debug_str("Didn't find controller\n");
01182 } else {
01183 channel++;
01184 }
01185 }
01186
01187 debug_var("Trying channel ", channel);
01188 debug_nl();
01189 mrf24j40_set_channel(channel);
01190
01191 its_transmit_to_sa( 0xffff, 0xffff, 0xffff,
01192 ITS_ASSOC_REQ, 0, 0);
01193 }
01194 }
01195
01196 }
01197
01198
01199 its2_result its2_device_transmit(uns8 *data, uns8 data_length) {
01200
01201
01202 its_transmit_to_handle(controller_handle, ITS_GENERIC_DATA, data, data_length);
01203 }
01204
01205
01206