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 #include "its_mode1.h"
00038 #include "its_common.h"
00039 #include "wpan.h"
00040 #include "mrf24j40.h"
00041
00042 #include "config.h"
00043 #include "pic_serial.h"
00044 #include "pic_tick.h"
00045 #include "pic_timer.h"
00046
00047 its1_state state = STATE_STARTUP;
00048
00049 uns8 controller_handle;
00050 uns16 tick_marker;
00051 uns8 channel;
00052
00053
00054
00055
00056
00057 void its1_setup_io() {
00058 wpan_setup_io();
00059 }
00060
00061
00062 its1_result its1_controller_handle_association(uns16 pan_id, uns16 its_device_id) {
00063
00064 its_device_handle device_handle;
00065
00066 device_handle = its_get_device(its_device_id);
00067
00068 if (device_handle == ITS_DEVICE_NONE) {
00069
00070 device_handle = its_add_device( its_device_id, pan_id, its_device_id);
00071 if (device_handle == ITS_DEVICE_NONE) {
00072
00073 serial_print_str("Too many devices! association failed");
00074 return RESULT_FAILED;
00075 } else {
00076
00077 its_transmit_to_handle(device_handle, ITS_ASSOC_RES, 0, 0);
00078 return RESULT_SUCCESSFUL;
00079
00080 }
00081
00082 }
00083 }
00084
00085
00086
00087 #ifdef ITS_MODE1_CONTROLLER
00088
00089 void wpan_data_received_callback(wpan_address *addr, uns8 *data, uns8 data_size) {
00090
00091 serial_print_str("Frame received\n");
00092
00093 uns8 count;
00094 wpan_print_address(addr);
00095
00096 serial_print_str(" Data: ");
00097 for (count = 0; count < data_size; count++) {
00098 serial_print_int_hex(data[count]);
00099 serial_putc(' ');
00100 if ((data[count] > 31) && (data[count] < 127)) {
00101 serial_putc(data[count]);
00102 } else {
00103 serial_putc('?');
00104 }
00105 serial_putc(' ');
00106 }
00107 serial_print_nl();
00108
00109
00110
00111
00112 if ((data_size > 2) && (data[0] == 'I') && (data[1] == 'T')) {
00113 serial_print_str("ITS Packet received\n");
00114 uns8 length_header = data[2];
00115 uns8 length_data = data[3+length_header+1];
00116 uns8 data_start = 3+length_header+2;
00117
00118 if (data[3] == ITS_ASSOC_REQ) {
00119 serial_print_str("ITS Association request\n");
00120
00121
00122 uns16 network_id_req = data[6];
00123 network_id_req <<= 8;
00124 network_id_req += data[5];
00125
00126 if (network_id_req == its_get_network_id()) {
00127
00128 uns16 device_id_req = data[8];
00129 device_id_req <<= 8;
00130 device_id_req += data[7];
00131 its1_controller_handle_association( network_id_req, device_id_req);
00132 } else {
00133 serial_print_str("Not for us, for network: ");
00134 serial_print_int_hex_16bit(network_id_req);
00135 }
00136 }
00137 else if (data[3] == ITS_GENERIC_DATA) {
00138
00139
00140
00141 serial_print_str("Generic data pkt\n");
00142
00143 uns16 device_id = data[8];
00144 device_id <<= 8;
00145 device_id += data[7];
00146 its1_controller_receive_callback(device_id, &data[data_start], data_size - data_start);
00147
00148 }
00149 }
00150 }
00151
00152 #else
00153
00154
00155 void wpan_data_received_callback(wpan_address *addr, uns8 *data, uns8 data_size) {
00156
00157
00158
00159 uns8 count;
00160 wpan_print_address(addr);
00161 wpan_print_frame_type(frame_type);
00162 serial_print_str(" Data: ");
00163 for (count = 0; count < data_size; count++) {
00164 serial_print_int_hex(data[count]);
00165 serial_putc(' ');
00166 if ((data[count] > 31) && (data[count] < 127)) {
00167 serial_putc(data[count]);
00168 } else {
00169 serial_putc('?');
00170 }
00171 serial_putc(' ');
00172 }
00173 serial_print_nl();
00174
00175
00176
00177
00178
00179
00180 if ((data_size > 2) && (data[0] == 'I') && (data[1] == 'T')) {
00181 serial_print_str("ITS Packet received\n");
00182 uns8 length_header = data[2];
00183 uns8 length_data = data[3+length_header+1];
00184 uns8 data_start = 3+length_header+2;
00185
00186 if (data[3] == ITS_ASSOC_RES) {
00187 serial_print_str("Received association response\n");
00188 if (state == STATE_SEARCHING) {
00189 serial_print_str("Changing state to associated\n");
00190 uns16 controller_device_id = data[8];
00191 controller_device_id <<= 8;
00192 controller_device_id += data[7];
00193
00194 uns8 device_handle = its_get_device(controller_device_id);
00195
00196 if (device_handle == ITS_DEVICE_NONE) {
00197
00198 device_handle = its_add_device( controller_device_id, addr->source_pan_id, controller_device_id);
00199 if (device_handle == ITS_DEVICE_NONE) {
00200
00201 serial_print_str("Too many devices! association failed");
00202 state = STATE_UNASSOCIATED;
00203 } else {
00204 controller_handle = device_handle;
00205 state = STATE_ASSOCIATED;
00206 }
00207 } else {
00208 state = STATE_ASSOCIATED;
00209 }
00210
00211 }
00212 }
00213 else if (data[3] == ITS_GENERIC_DATA) {
00214
00215
00216
00217 serial_print_str("Generic data pkt\n");
00218
00219 uns16 device_id = data[8];
00220 device_id <<= 8;
00221 device_id += data[7];
00222 its1_device_receive_callback(&data[data_start], data_size - data_start);
00223
00224 }
00225 }
00226 }
00227 #endif
00228
00229
00230
00231
00232
00233 void its1_controller_receive_callback(uns16 device_id, uns8 *data, uns8 data_length);
00234
00235 uns8 its1_controller_init(uns16 my_device_id, uns16 network_id) {
00236
00237 uns8 lowest_channel_ed;
00238 uns8 my_ed[8] = ITS_EA;
00239
00240 its_init();
00241 wpan_init();
00242
00243 its_set_device_id(my_device_id);
00244 its_set_network_id(network_id);
00245
00246 mrf24j40_set_pan_id(network_id);
00247 mrf24j40_set_short_address(my_device_id);
00248 mrf24j40_set_extended_address(&my_ed);
00249
00250 serial_print_str("Controller startup\n");
00251
00252
00253 serial_print_str("Choosing channel\n");
00254
00255 lowest_channel_ed = mrf24j40_scan_for_lowest_channel_ed();
00256
00257 serial_print_var("Chosing channel", lowest_channel_ed);
00258 serial_print_nl();
00259
00260
00261 mrf24j40_set_channel(lowest_channel_ed);
00262
00263 }
00264
00265 its1_result its1_controller_transmit(uns16 device_id, uns8 *data, uns8 data_length) {
00266
00267 its_device_handle device_handle;
00268
00269 device_handle = its_get_device(device_id);
00270
00271 its_transmit_to_handle(device_handle, ITS_GENERIC_DATA, data, data_length);
00272
00273
00274 }
00275
00276
00277
00278
00279 void its1_controller_process() {
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289 its1_result its1_device_init(uns16 my_device_id, uns16 network_id) {
00290
00291
00292
00293 uns8 my_ea[8] = ITS_EA;
00294
00295 serial_print_str("Device startup\n");
00296
00297 its_init();
00298 wpan_init();
00299
00300 its_set_device_id(my_device_id);
00301 its_set_network_id(network_id);
00302
00303 mrf24j40_set_pan_id(network_id);
00304 mrf24j40_set_short_address(my_device_id);
00305 mrf24j40_set_extended_address(&my_ea);
00306
00307 state = STATE_UNASSOCIATED;
00308 }
00309
00310 its1_result its1_find_controller() {
00311 state = STATE_SEARCHING;
00312 tick_marker = tick_get_count();
00313 channel = 0;
00314 }
00315
00316 void its1_device_process() {
00317
00318 uns16 test_tick;
00319 uns8 controller_ea[8] = CONTROLLER_EA;
00320
00321
00322
00323
00324
00325 if (state == STATE_SEARCHING) {
00326 test_tick = tick_get_count();
00327 if (tick_calc_diff(tick_marker, test_tick) >= 250) {
00328 tick_marker = test_tick;
00329 if (channel == 0) {
00330 channel = MRF_FIRST_CHANNEL;
00331 } else {
00332 if (channel == MRF_LAST_CHANNEL) {
00333
00334 state = STATE_UNASSOCIATED;
00335 serial_print_str("Didn't find controller\n");
00336 } else {
00337 channel++;
00338 }
00339 }
00340
00341 serial_print_var("Trying channel ", channel);
00342 serial_print_nl();
00343 mrf24j40_set_channel(channel);
00344
00345 its_transmit_to_ea(&controller_ea, 0xffff, ITS_ASSOC_REQ, 0, 0);
00346 }
00347 }
00348
00349 }
00350
00351
00352 its1_result its1_device_transmit(uns8 *data, uns8 data_length) {
00353
00354
00355 its_transmit_to_handle(controller_handle, ITS_GENERIC_DATA, data, data_length);
00356 }
00357