00001
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 #include <string.h>
00036 #include "mote_sensix.h"
00037 #include "mote_impl.h"
00038 #include "mote_networking.h"
00039 #include "mote_state.h"
00040
00041 #include "mos.h"
00042 #include "com.h"
00043 #include "msched.h"
00044 #include "clock.h"
00045 #include "dev.h"
00046
00047
00048 #if PLATFORM_TELOSB
00049 # include "msp-adc.h"
00050 # define ADC_READ adc_get_conversion16
00051 # define BATTERY_CHANNEL 11
00052 #endif
00053
00054
00055 extern state _node_state[];
00056
00057 static functor *latest;
00058
00059
00060 void init_self()
00061 {
00062 #if PLATFORM_TELOSB
00063 add_capability(TEMP, 0);
00064 add_capability(ACCEL, 0);
00065 add_capability(HUMID, 0);
00066 #else
00067 #if PLATFORM_MICA_ANY
00068 add_capability(LIGHT, 0);
00069 add_capability(TEMP, 0);
00070 add_capability(ACCEL, 0);
00071 add_capability(MAG, 0);
00072 add_capability(GPS, 0);
00073 #endif
00074 #endif
00075 }
00076
00077
00078 typedef enum { READ, WRITE, ON, OFF, } sense_f;
00079
00080 static void _sensor_function(byte_t type, sense_f what, int_t *result)
00081 {
00082 switch(type) {
00083 case (LIGHT):
00084 #if PLATFORM_TELOSB
00085 #else
00086 #if PLATFORM_MICA_ANY
00087 if (what == READ)
00088 dev_read(DEV_MICA2_LIGHT, result, WORDSIZE);
00089 else if (what == ON)
00090 dev_mode(DEV_MICA2_LIGHT, DEV_MODE_ON);
00091 else if (what == OFF)
00092 dev_mode(DEV_MICA2_LIGHT, DEV_MODE_OFF);
00093 #endif
00094 #endif
00095 break;
00096
00097 case (TEMP):
00098 #if PLATFORM_TELOSB
00099 if (what == READ)
00100 dev_read(DEV_MSP_TEMPERATURE, result, WORDSIZE);
00101 else if (what == ON)
00102 dev_mode(DEV_MSP_TEMPERTURE, DEV_MODE_ON);
00103 else if (what == OFF)
00104 dev_mode(DEV_MSP_TEMPERTURE, DEV_MODE_OFF);
00105 #else
00106 #if PLATFORM_MICA_ANY
00107 if (what == READ)
00108 dev_read(DEV_MICA2_TEMP, result, WORDSIZE);
00109 else if (what == ON)
00110 dev_mode(DEV_MICA2_TEMP, DEV_MODE_ON);
00111 else if (what == OFF)
00112 dev_mode(DEV_MICA2_TEMP, DEV_MODE_OFF);
00113 #endif
00114 #endif
00115 break;
00116
00117 case (ACCEL):
00118 #if PLATFORM_TELOSB
00119 if (what == READ)
00120 dev_read(DEV_MSP_ACCELEROMETER, result, WORDSIZE);
00121 else if (what == ON)
00122 dev_mode(DEV_MSP_ACCELEROMETER, DEV_MODE_ON);
00123 else if (what == OFF)
00124 dev_mode(DEV_MSP_ACCELEROMETER, DEV_MODE_OFF);
00125 #else
00126 #if PLATFORM_MICA_ANY
00127 if (what == READ) {
00128 int_t temp;
00129 dev_read(DEV_MICA2_ACCEL_X, result, WORDSIZE);
00130 dev_read(DEV_MICA2_ACCEL_Y, &temp, WORDSIZE);
00131 *result += temp;
00132 }
00133 else if (what == ON) {
00134 dev_mode(DEV_MICA2_ACCEL_X, DEV_MODE_ON);
00135 dev_mode(DEV_MICA2_ACCEL_Y, DEV_MODE_ON);
00136 }
00137 else if (what == OFF) {
00138 dev_mode(DEV_MICA2_ACCEL_X, DEV_MODE_OFF);
00139 dev_mode(DEV_MICA2_ACCEL_Y, DEV_MODE_OFF);
00140 }
00141 #endif
00142 #endif
00143 break;
00144
00145 case (MAG):
00146 #if PLATFORM_TELOSB
00147 #else
00148 #if PLATFORM_MICA_ANY
00149 if (what == READ) {
00150 int_t temp;
00151 dev_read(DEV_MICA2_MAGNET_X, result, WORDSIZE);
00152 dev_read(DEV_MICA2_MAGNET_Y, &temp, WORDSIZE);
00153 *result += temp;
00154 }
00155 else if (what == ON) {
00156 dev_mode(DEV_MICA2_MAGNET_X, DEV_MODE_ON);
00157 dev_mode(DEV_MICA2_MAGNET_Y, DEV_MODE_ON);
00158 }
00159 else if (what == OFF) {
00160 dev_mode(DEV_MICA2_MAGNET_X, DEV_MODE_OFF);
00161 dev_mode(DEV_MICA2_MAGNET_Y, DEV_MODE_OFF);
00162 }
00163 #endif
00164 #endif
00165 break;
00166
00167 case (HUMID):
00168 #if PLATFORM_TELOSB
00169 if (what == READ)
00170 dev_read(DEV_MSP_HUMIDITY, result, WORDSIZE);
00171 else if (what == ON)
00172 dev_mode(DEV_MSP_HUMIDITY, DEV_MODE_ON);
00173 else if (what == OFF)
00174 dev_mode(DEV_MSP_HUMIDITY, DEV_MODE_OFF);
00175 #else
00176 #if PLATFORM_MICA_ANY
00177 #endif
00178 #endif
00179 break;
00180
00181 case (GPS):
00182 #if PLATFORM_TELOSB
00183 #else
00184 #if PLATFORM_MICA_ANY
00185 if (what == READ)
00186 dev_read(DEV_MICA2_GPS, result, WORDSIZE);
00187 else if (what == ON)
00188 dev_mode(DEV_MICA2_GPS, DEV_MODE_ON);
00189 else if (what == OFF)
00190 dev_mode(DEV_MICA2_GPS, DEV_MODE_OFF);
00191 #endif
00192 #endif
00193 break;
00194 }
00195 }
00196
00197
00198 static void _do_sense()
00199 {
00200 functor *f = latest;
00201 unsigned int i, found = 0;
00202 for (i = 0; i < _node_state[0].cap_size; i++) {
00203 if (f->sensor == _node_state[0].capabilities[i]) {
00204 found = 1;
00205 break;
00206 }
00207 }
00208 if (!found) {
00209 mos_thread_exit();
00210 return;
00211 }
00212 _sensor_function(f->sensor, ON, NULL);
00213
00214 while (1) {
00215 int_t result = 0;
00216
00217 mos_thread_sleep(f->rate);
00218 if (!(f->bits & functor_running))
00219 break;
00220
00221 _sensor_function(f->sensor, READ, &result);
00222 if (f->f_type == BETA) {
00223 if (f->bits & functor_hi_thresh && f->bits & functor_lo_thresh &&
00224 (result <= f->threshold && result >= f->alt_threshold))
00225 goto snooze;
00226 else if (f->bits & functor_hi_thresh && result <= f->threshold)
00227 goto snooze;
00228 else if (f->bits & functor_lo_thresh && result >= f->threshold)
00229 goto snooze;
00230 }
00231 f->results[i].data = result;
00232 memcpy(&(f->results[i].id), &(_node_state[0].ident), ID_BYTE_LEN);
00233 i++;
00234 if (i >= MAX_RESULTS)
00235 i = 0;
00236
00237 f->bits |= functor_data_ready;
00238 snooze:
00239 if (f_dataready(f) <= 0)
00240 break;
00241 }
00242 _sensor_function(f->sensor, OFF, NULL);
00243 mos_thread_exit();
00244 }
00245
00246
00247 void listen_sense(functor *f)
00248 {
00249 f->bits |= functor_running;
00250 latest = f;
00251 if (mos_thread_new(_do_sense, 128, PRIORITY_NORMAL) != THREAD_OK)
00252 f_cancel(f);
00253 }
00254
00255
00256 uint_t check_energy()
00257 {
00258 uint_t energy
00259 #if PLATFORM_MICA_ANY
00260 dev_read(DEV_MICA2_BATTERY, &energy, sizeof(uint_t));
00261 #else
00262 energy = ADC_READ(BATTERY_CHANNEL);
00263 #endif
00264 return energy;
00265 }
00266
00267
00268 uint_t check_time()
00269 {
00270 return mos_get_realtime();
00271 }
00272
00273
00274
00275
00276 static void _receiver()
00277 {
00278 com_mode(IFACE_RADIO, IF_LISTEN);
00279 while (1) {
00280 comBuf *recv_pkt;
00281 msg_hdr_t *hdr;
00282 byte_t data[MAX_MSG_SIZE];
00283 uint_t offset = 0;
00284 uint_t remaining = 0;
00285
00286 memset(data, 0, MAX_MSG_SIZE);
00287
00288 recv_pkt = com_recv(IFACE_RADIO);
00289 if (recv_pkt->size != HEADER_SIZE) {
00290 com_free_buf(recv_pkt);
00291 continue;
00292 }
00293
00294 memcpy(data, recv_pkt->data, recv_pkt->size);
00295 hdr = (msg_hdr_t*)data;
00296 remaining = hdr->size;
00297 offset = recv_pkt->size;
00298 com_free_buf(recv_pkt);
00299
00300 while (remaining > 0) {
00301 recv_pkt = com_recv(IFACE_RADIO);
00302 memcpy(data + offset, recv_pkt->data, recv_pkt->size);
00303 remaining -= recv_pkt->size;
00304 offset += recv_pkt->size;
00305 com_free_buf(recv_pkt);
00306 }
00307
00308 parse_packet(data, offset, 0);
00309 }
00310 }
00311
00312
00313 void listen_radio()
00314 {
00315 mos_thread_new(_receiver, 128, PRIORITY_NORMAL);
00316 }
00317
00318
00319 int_t send_up(byte_t what, uint_t size, byte_t *array)
00320 {
00321
00322 return bcast(what, size, array);
00323 }
00324
00325
00326 int_t send_dn(byte_t what, uint_t size, byte_t *array)
00327 {
00328 return -1;
00329 }
00330
00331
00332 int_t bcast(byte_t what, uint_t data_size, byte_t *data)
00333 {
00334 comBuf send_pkt;
00335 uint_t offset = 0;
00336 uint_t remaining = data_size;
00337
00338 com_mode(IFACE_RADIO, IF_LISTEN);
00339 load_header(send_pkt.data, what, data_size);
00340 com_send(IFACE_RADIO, &send_pkt);
00341
00342 while (remaining > 0) {
00343 if (remaining > COM_DATA_SIZE)
00344 send_pkt.size = COM_DATA_SIZE;
00345 else
00346 send_pkt.size = remaining;
00347
00348 memset(send_pkt.data, 0, COM_DATA_SIZE);
00349 memcpy(send_pkt.data, data + offset, send_pkt.size);
00350 com_send(IFACE_RADIO, &send_pkt);
00351
00352 remaining -= send_pkt.size;
00353 offset += send_pkt.size;
00354 }
00355 return 0;
00356 }
00357
00358
00359
00360
00361 static void _main_thread()
00362 {
00363 uint_t id;
00364 dev_read(DEV_HARDWARE_ID, &id, sizeof(uint_t));
00365 initialize(id, 0);
00366
00367 while (1) {
00368 mos_thread_sleep(REDISCOVER_TIME);
00369 start_discovery(0);
00370 }
00371 mos_thread_exit();
00372 }
00373
00374
00375 void start(void)
00376 {
00377 mos_thread_new(_main_thread, 128, PRIORITY_NORMAL);
00378 }