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_common.h"
00038 #include "mrf24j40.h"
00039 #include "pic_serial.h"
00040 #include "wpan.h"
00041
00042 #include "debug.h"
00043
00044 its_device_info its_devices[ITS_MAX_KNOWN_DEVICES];
00045
00046 uns16 its_network_id;
00047 uns16 its_device_id;
00048
00049 uns8 its_sequence = 0;
00050
00051 void its_print_devices() {
00052 debug_nl();
00053 for (uns8 count=0; count < ITS_MAX_KNOWN_DEVICES; count++) {
00054 if (its_devices[count].its_device_id != 0xffff) {
00055 debug_str(" dev= ");
00056 debug_int_hex_16bit(its_devices[count].its_device_id);
00057 if (its_devices[count].addr.remote.remote_indicator != 0xffff) {
00058 debug_str(" pan= ");
00059 debug_int_hex_16bit(its_devices[count].addr.local.pan_id);
00060 debug_str(" sa= ");
00061 debug_int_hex_16bit(its_devices[count].addr.local.short_address);
00062 } else {
00063 debug_str(" via= ");
00064 debug_int_hex_16bit(its_devices[count].addr.remote.prior_device_id);
00065 }
00066 debug_nl();
00067
00068 }
00069 }
00070 }
00071
00072 uns8 its_get_next_sequence() {
00073 return its_sequence++;
00074 }
00075
00076 void its_set_network_id(uns16 network_id) {
00077 its_network_id = network_id;
00078 }
00079
00080
00081 uns16 its_get_network_id() {
00082 return its_network_id;
00083 }
00084
00085 void its_set_device_id(uns16 device_id) {
00086 its_device_id = device_id;
00087 }
00088
00089 uns16 its_get_device_id() {
00090 return its_device_id;
00091 }
00092
00093 void its_init() {
00094 for (uns8 count=0; count < ITS_MAX_KNOWN_DEVICES; count++) {
00095 its_devices[count].its_device_id = 0xffff;
00096 }
00097 wpan_init();
00098 }
00099
00100 its_device_info *its_get_device_info(uns8 handle) {
00101 if (handle < ITS_MAX_KNOWN_DEVICES) {
00102 return &its_devices[handle];
00103 } else {
00104 return NULL;
00105 }
00106 }
00107
00108 its_device_handle its_get_device_handle(uns16 device_id) {
00109
00110 bit found = 0;
00111 if (device_id == 0xffff) {
00112 return ITS_DEVICE_NONE;
00113 }
00114 for (uns8 count=0; count < ITS_MAX_KNOWN_DEVICES; count++) {
00115 if (its_devices[count].its_device_id == device_id) {
00116 found = 1;
00117 break;
00118 }
00119 }
00120 if (found) {
00121 return count;
00122 } else {
00123 return ITS_DEVICE_NONE;
00124 }
00125 }
00126
00127 its_device_handle its_add_local_device(uns16 device_id, uns16 pan_id, uns16 short_address) {
00128
00129 bit found_slot = 0;
00130 debug_str("Adding device_id (");
00131 debug_int_hex_16bit(device_id);
00132 debug_str(" (pan_id ");
00133 debug_int_hex_16bit(pan_id);
00134 debug_str(" short_addr ");
00135 debug_int_hex_16bit(short_address);
00136 debug_str(")\n");
00137
00138 for (uns8 count=0; count < ITS_MAX_KNOWN_DEVICES; count++) {
00139 if (its_devices[count].its_device_id == 0xffff) {
00140 found_slot = 1;
00141 break;
00142 }
00143 }
00144 if (found_slot) {
00145 its_devices[count].its_device_id = device_id;
00146 its_devices[count].addr.local.pan_id = pan_id;
00147 its_devices[count].addr.local.short_address = short_address;
00148 } else {
00149 count = ITS_DEVICE_NONE;
00150 }
00151
00152 its_print_devices();
00153
00154 return count;
00155
00156 }
00157
00158 its_device_handle its_add_net_device(uns16 device_id, uns16 previous_hop) {
00159
00160 bit found_slot = 0;
00161 debug_str(" Add ");
00162 debug_int_hex_16bit(device_id);
00163 debug_str(" (via ");
00164 debug_int_hex_16bit(previous_hop);
00165 debug_str(")\n");
00166
00167 for (uns8 count=0; count < ITS_MAX_KNOWN_DEVICES; count++) {
00168 if (its_devices[count].its_device_id == 0xffff) {
00169 found_slot = 1;
00170 break;
00171 }
00172 }
00173 if (found_slot) {
00174 its_devices[count].its_device_id = device_id;
00175 its_devices[count].addr.remote.remote_indicator = 0xffff;
00176 its_devices[count].addr.remote.prior_device_id = previous_hop;
00177 } else {
00178 count = ITS_DEVICE_NONE;
00179 }
00180 its_print_devices();
00181 return count;
00182 }
00183
00184
00185 void its_transmit_to_handle(its_device_handle handle, uns8 packet_type, uns8 *data, uns8 data_length) {
00186
00187
00188
00189 its_transmit_to_sa(its_devices[handle].addr.local.pan_id,
00190 its_devices[handle].addr.local.short_address,
00191 its_devices[handle].its_device_id,
00192 packet_type, data, data_length);
00193 }
00194
00195 void its_transmit_to_sa(uns16 dest_pan_id, uns16 dest_sa, uns16 dest_device_id, uns8 packet_type, uns8 *data, uns8 data_length) {
00196
00197 uns8 buffer[20];
00198 uns16 temp;
00199 uns8 count;
00200
00201 buffer[0] = 'I';
00202 buffer[1] = 'T';
00203 buffer[2] = 11;
00204 buffer[3] = packet_type;
00205 buffer[4] = its_sequence++;
00206
00207 buffer[5] = its_network_id & 0xff;
00208 buffer[6] = its_network_id >> 8;
00209
00210 buffer[7] = its_device_id & 0xff;
00211 buffer[8] = its_device_id >> 8;
00212
00213 temp = dest_device_id;
00214 buffer[9] = temp & 0xff;
00215 buffer[10] = temp >> 8;
00216
00217
00218 buffer[11] = 1;
00219 buffer[12] = 0;
00220 buffer[13] = 0;
00221
00222 buffer[14] = data_length;
00223 for (count = 0; count < data_length; count++) {
00224 buffer[15+count] = data[count];
00225 }
00226 mrf24j40_transmit_to_short_address(FRAME_TYPE_DATA, dest_pan_id,
00227 dest_sa, &buffer,
00228 data_length + 15, MRF_ACK);
00229 }
00230
00231
00232 void its_transmit_to_ea(uns8 *dest_ea, uns16 dest_its_device_id, uns8 packet_type, uns8 *data, uns8 data_length) {
00233
00234
00235
00236 uns8 buffer[20];
00237
00238 uns8 count;
00239
00240
00241
00242 buffer[0] = 'I';
00243 buffer[1] = 'T';
00244 buffer[2] = 11;
00245 buffer[3] = packet_type;
00246 buffer[4] = its_sequence++;
00247
00248 buffer[5] = its_network_id & 0xff;
00249 buffer[6] = its_network_id >> 8;
00250
00251 buffer[7] = its_device_id & 0xff;
00252 buffer[8] = its_device_id >> 8;
00253
00254
00255 buffer[9] = dest_its_device_id & 0xff;
00256 buffer[10] = dest_its_device_id >> 8;
00257
00258
00259 buffer[11] = 1;
00260 buffer[12] = 0;
00261 buffer[13] = 0;
00262
00263 buffer[14] = data_length;
00264 for (count = 0; count < data_length; count++) {
00265 buffer[15+count] = data[count];
00266 }
00267 mrf24j40_transmit_to_extended_address(FRAME_TYPE_DATA, 0xffff,
00268 dest_ea, &buffer,
00269 data_length + 15, MRF_NO_ACK);
00270 }
00271