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 <stdlib.h>
00037
00038 #define MOTE
00039 #define IMPL
00040 #include "sensix.h"
00041 #include "sense_impl.h"
00042 #include "mote_sensix.h"
00043 #include "mote_networking.h"
00044 #include "mote_impl.h"
00045 #include "mote_types.h"
00046 #include "mote_limits.h"
00047 #include "mote_state.h"
00048
00049
00050 mote_state _node_state[MAX_NODES];
00051
00052
00053
00054
00055
00056
00057
00058 static int32_t _square_root(int32_t x)
00059 {
00060 static const unsigned char sqq_table[] = {
00061 0, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57,
00062 59, 61, 64, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83,
00063 84, 86, 87, 89, 90, 91, 93, 94, 96, 97, 98, 99, 101, 102,
00064 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118,
00065 119, 120, 121, 122, 123, 124, 125, 126, 128, 128, 129, 130, 131, 132,
00066 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145,
00067 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155, 156, 157,
00068 158, 159, 160, 160, 161, 162, 163, 163, 164, 165, 166, 167, 167, 168,
00069 169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 178,
00070 179, 180, 181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188,
00071 189, 189, 190, 191, 192, 192, 193, 193, 194, 195, 195, 196, 197, 197,
00072 198, 199, 199, 200, 201, 201, 202, 203, 203, 204, 204, 205, 206, 206,
00073 207, 208, 208, 209, 209, 210, 211, 211, 212, 212, 213, 214, 214, 215,
00074 215, 216, 217, 217, 218, 218, 219, 219, 220, 221, 221, 222, 222, 223,
00075 224, 224, 225, 225, 226, 226, 227, 227, 228, 229, 229, 230, 230, 231,
00076 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,
00077 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246,
00078 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253,
00079 253, 254, 254, 255
00080 };
00081 int xn;
00082
00083 if (x < 1) {
00084 if (x < 0)
00085 return -1;
00086 if (x == 0)
00087 return 0;
00088 }
00089
00090 if (x >= 0x10000) {
00091 if (x >= 0x1000000) {
00092 if (x >= 0x10000000) {
00093 if (x >= 0x40000000)
00094 xn = sqq_table[x>>24] << 8;
00095 else
00096 xn = sqq_table[x>>22] << 7;
00097 } else {
00098 if (x >= 0x4000000)
00099 xn = sqq_table[x>>20] << 6;
00100 else
00101 xn = sqq_table[x>>18] << 5;
00102 }
00103 } else {
00104 if (x >= 0x100000) {
00105 if (x >= 0x400000)
00106 xn = sqq_table[x>>16] << 4;
00107 else
00108 xn = sqq_table[x>>14] << 3;
00109 } else {
00110 if (x >= 0x40000)
00111 xn = sqq_table[x>>12] << 2;
00112 else
00113 xn = sqq_table[x>>10] << 1;
00114 }
00115 goto nr1;
00116 }
00117 } else {
00118 if (x >= 0x100) {
00119 if (x >= 0x1000) {
00120 if (x >= 0x4000)
00121 xn = (sqq_table[x>>8] >> 0) + 1;
00122 else
00123 xn = (sqq_table[x>>6] >> 1) + 1;
00124 } else {
00125 if (x >= 0x400)
00126 xn = (sqq_table[x>>4] >> 2) + 1;
00127 else
00128 xn = (sqq_table[x>>2] >> 3) + 1;
00129 }
00130 goto adj;
00131 } else
00132 return sqq_table[x] >> 4;
00133 }
00134 xn = (xn + 1 + x / xn) / 2;
00135
00136 nr1:
00137 xn = (xn + 1 + x / xn) / 2;
00138
00139 adj:
00140 if (xn * xn > x)
00141 xn--;
00142
00143 return xn;
00144 }
00145
00146
00147
00148
00149 static byte_t _incr(byte_t var)
00150 {
00151 byte_t pre = var++;
00152 if (var >= 0xfe)
00153 var = 1;
00154 return pre;
00155 }
00156
00157
00158 static int_t _summation(functor *f)
00159 {
00160 uint_t i;
00161 int_t sum = 0;
00162
00163 for (i = 0; i < f->size; i++)
00164 sum += f->results[i].data;
00165 return sum;
00166 }
00167
00168
00169 static int_t _difference(functor *f)
00170 {
00171 uint_t i;
00172 int_t max = 0;
00173
00174 for (i = 0; i < f->size; i++) {
00175 int_t delta = f->results[i].data - max;
00176 if (delta < 0)
00177 delta = 0 - delta;
00178 if (delta > max)
00179 max = delta;
00180 }
00181 return max;
00182 }
00183
00184
00185 static int_t _average(functor *f)
00186 {
00187 int_t sum = _summation(f);
00188
00189 if (f->size == 0)
00190 return -1;
00191
00192 return sum / f->size;
00193 }
00194
00195
00196 static int_t _variance(functor *f)
00197 {
00198 uint_t i;
00199 int_t var = 0;
00200 int_t mean = _average(f);
00201
00202 if (f->size == 0 || f->size == 1)
00203 return -1;
00204
00205 for (i = 0; i < f->size; i++) {
00206 int_t x = f->results[i].data - mean;
00207 var += x * x;
00208 }
00209
00210 return _square_root(var / (f->size - 1));
00211 }
00212
00213
00214 mote_state *get_state(uint_t node) {
00215 return &(_node_state[node]);
00216 }
00217
00218
00219 functor *get_functor(uint_t idx, uint_t node) {
00220 if (node > MAX_NODES) {
00221 log_error("Pointing beyond valid state\n");
00222 return NULL;
00223 }
00224 return &(_node_state[node].functor_pool[idx]);
00225 }
00226
00227
00228 void start_discovery(uint_t node) {
00229 discover_siblings(node);
00230 discover_ancestors(node);
00231 }
00232
00233
00234 void discover_ancestors(uint_t node) {
00235 if (node > MAX_NODES) {
00236 log_error("Pointing beyond valid state\n");
00237 return;
00238 }
00239 xmit_metadata(DESCENDANTS, node);
00240 }
00241
00242
00243 void discover_siblings(uint_t node) {
00244 if (node > MAX_NODES) {
00245 log_error("Pointing beyond valid state\n");
00246 return;
00247 }
00248 xmit_metadata(SIBLINGS, node);
00249 }
00250
00251
00252 void add_capability(byte_t cap, uint_t node)
00253 {
00254 uint_t idx;
00255 if (node > MAX_NODES) {
00256 log_error("Pointing beyond valid state\n");
00257 return;
00258 }
00259 idx = _node_state[node].cap_size;
00260 _node_state[node].capabilities[idx] = cap;
00261 _node_state[node].cap_size++;
00262 }
00263
00264
00265 int_t initialize(uint_t id, uint_t node)
00266 {
00267 unsigned int i;
00268 if (node > MAX_NODES) {
00269 log_error("Pointing beyond valid state\n");
00270 return -1;
00271 }
00272 memset(&(_node_state[node]), 0, sizeof(mote_state));
00273
00274 _node_state[node].ident = id;
00275
00276 for (i = 0; i < MAX_FUNCTORS; i++)
00277 init_functor(&(_node_state[node].functor_pool[i]), node);
00278
00279 _node_state[node].max_energy = check_energy();
00280 init_functor(&(_node_state[node].ancestors), node);
00281 _node_state[node].ancestors.f_type = ANCESTORS;
00282 init_functor(&(_node_state[node].siblings), node);
00283 _node_state[node].siblings.f_type = SIBLINGS;
00284
00285 init_self();
00286 listen_radio();
00287 return 0;
00288 }
00289
00290
00291 int_t f_apply(functor *f)
00292 {
00293 log_info("apply\n");
00294 if (f == NULL || f->function == NULL)
00295 return -1;
00296
00297 f->f_e = check_energy();
00298 f->f_t = check_time();
00299 return (*(f->function))(f);
00300 }
00301
00302
00303 int_t f_dataready(functor *f)
00304 {
00305 log_info("dataready\n");
00306 if (f == NULL || f->function_done == NULL)
00307 return -1;
00308
00309 if (!(f->bits & functor_data_ready))
00310 return 1;
00311
00312 if (f->collector == NULL) {
00313 f->f_e = energy_diff(f->f_e, check_energy());
00314 f->f_t = time_diff(f->f_t, check_time());
00315 }
00316 else {
00317 f->f_e = f->collector->f_e;
00318 f->f_t = f->collector->f_t;
00319 }
00320
00321 return (*(f->function_done))(f);
00322 }
00323
00324
00325 int_t f_cancel(functor *f)
00326 {
00327 log_info("cancel\n");
00328 f->bits &= ~functor_running;
00329 free_functor(f);
00330 return 0;
00331 }
00332
00333
00334 functor *allocate_functor(uint_t node)
00335 {
00336 uint_t idx = 0;
00337 if (node > MAX_NODES) {
00338 log_error("Pointing beyond valid state\n");
00339 return NULL;
00340 }
00341
00342 while (_node_state[node].functor_pool[idx].bits & functor_used)
00343 ++idx;
00344 if (idx >= MAX_FUNCTORS)
00345 return NULL;
00346
00347 init_functor(&_node_state[node].functor_pool[idx], node);
00348 _node_state[node].functor_pool[idx].f_id = idx;
00349 _node_state[node].functor_pool[idx].f_e = check_energy();
00350 _node_state[node].functor_pool[idx].f_t = check_time();
00351 _node_state[node].functor_pool[idx].bits |= functor_used;
00352 return &(_node_state[node].functor_pool[idx]);
00353 }
00354
00355
00356 void init_functor(functor *f, uint_t node)
00357 {
00358 if (node > MAX_NODES) {
00359 log_error("Pointing beyond valid state\n");
00360 return;
00361 }
00362
00363 memset(f, 0, sizeof(functor));
00364
00365 f->f_id = 0xff;
00366 f->req_octet = WORDSIZE;
00367
00368 f->function = NULL;
00369 f->function_done = NULL;
00370 f->collector = NULL;
00371
00372
00373
00374
00375 f->node_idx = node;
00376 }
00377
00378
00379 void free_functor(functor *f)
00380 {
00381 f->bits &= ~functor_used;
00382 if (f->collector != NULL)
00383 free_functor(f->collector);
00384 }
00385
00386
00387
00388
00389 static int_t f_sense_done(functor *f)
00390 {
00391 log_info("sense done\n");
00392 f->bits &= ~functor_running;
00393
00394 if (!f->bits & functor_pass_thru)
00395 xmit_datum(f);
00396 else {
00397 if (f_dataready(f->parent) > 0)
00398 return 1;
00399 }
00400 f_cancel(f);
00401 return 0;
00402 }
00403
00404 int_t f_sense(functor *f)
00405 {
00406 log_info("sense\n");
00407 if (f == NULL)
00408 return -1;
00409 f->f_type = ALPHA;
00410 f->function = &f_sense;
00411 f->function_done = &f_sense_done;
00412
00413 listen_sense(f);
00414 return 0;
00415 }
00416
00417
00418 static int_t f_peak_sense_done(functor *f)
00419 {
00420 log_info("peak sense done\n");
00421 f->bits &= ~functor_running;
00422
00423 if (!f->bits & functor_pass_thru)
00424 xmit_datum(f);
00425 else {
00426 if (f_dataready(f->parent) > 0)
00427 return 1;
00428 }
00429 f_cancel(f);
00430 return 0;
00431 }
00432
00433 int_t f_peak_sense(functor *f)
00434 {
00435 log_info("peak sense\n");
00436 if (f == NULL)
00437 return -1;
00438 f->f_type = BETA;
00439 f->function = &f_peak_sense;
00440 f->function_done = &f_peak_sense_done;
00441
00442 listen_sense(f);
00443 return 0;
00444 }
00445
00446
00447
00448 static int_t f_time_series_done(functor *f)
00449 {
00450 int i;
00451 log_info("time series done\n");
00452
00453 f->bits &= ~functor_running;
00454 if (f->collector->size * f->collector->rate < f->duration) {
00455 f_apply(f->collector);
00456 return 1;
00457 }
00458
00459 for(i = 0; i < f->collector->size; i++)
00460 f->results[f->size++].data = f->collector->results[i].data;
00461
00462 if (!f->bits & functor_pass_thru)
00463 xmit_data(f);
00464 else
00465 f_dataready(f->parent);
00466
00467 f_cancel(f);
00468 return 0;
00469 }
00470
00471 int_t f_time_series(functor *f)
00472 {
00473 log_info("time series\n");
00474 if (f == NULL || f->collector == NULL)
00475 return -1;
00476 f->f_type = THETA;
00477 f->function = &f_time_series;
00478 f->function_done = &f_time_series_done;
00479 f->collector->bits |= functor_pass_thru;
00480
00481 f->size = 0;
00482 return f_apply(f->collector);
00483 }
00484
00485
00486 static int_t f_spatial_series_done(functor *f)
00487 {
00488 int i;
00489 log_info("spatial series done\n");
00490
00491 f->bits &= ~functor_running;
00492 if (f->collector->size < _node_state[f->node_idx].siblings.size &&
00493 (check_time() - f->duration) < SPATIAL_SERIES_TIMEOUT)
00494 return 1;
00495
00496 for(i = 0; i < f->collector->size; i++) {
00497 f->results[f->size].data = f->collector->results[i].data;
00498 memcpy(&(f->results[f->size].id), &(f->collector->results[f->size].id),
00499 ID_BYTE_LEN);
00500 f->size++;
00501 }
00502
00503 if (!f->bits & functor_pass_thru)
00504 xmit_data(f);
00505 else
00506 f_dataready(f->parent);
00507
00508 f_cancel(f);
00509 return 0;
00510 }
00511
00512 int_t f_spatial_series(functor *f)
00513 {
00514 log_info("spatial series\n");
00515 if (f == NULL || f->collector == NULL)
00516 return -1;
00517 f->f_type = PSI;
00518 f->f_e = check_energy();
00519 f->f_t = check_time();
00520 f->function = &f_spatial_series;
00521 f->function_done = &f_spatial_series_done;
00522 f->collector->bits |= functor_pass_thru;
00523 f->duration = check_time();
00524
00525 f->size = 0;
00526 xmit_command(f->collector);
00527 return f_apply(f->collector);
00528 }
00529
00530
00531
00532 static int_t f_recite_done(functor *f)
00533 {
00534 log_info("recite done\n");
00535 f->bits &= ~functor_running;
00536 xmit_data(f);
00537
00538 f_cancel(f);
00539 return 0;
00540 }
00541
00542 int_t f_recite(functor *f)
00543 {
00544 log_info("recite\n");
00545 if (f == NULL || f->collector == NULL)
00546 return -1;
00547 f->f_type = IOTA;
00548 f->function = &f_recite;
00549 f->function_done = &f_recite_done;
00550 f->collector->bits |= functor_pass_thru;
00551
00552 return f_apply(f->collector);
00553 }
00554
00555
00556 static int_t f_sum_done(functor *f)
00557 {
00558 log_info("sum done\n");
00559 f->bits &= ~functor_running;
00560 f->result = _summation(f->collector);
00561 xmit_datum(f);
00562
00563 f_cancel(f);
00564 return 0;
00565 }
00566
00567 int_t f_sum(functor *f)
00568 {
00569 log_info("sum\n");
00570 if (f == NULL || f->collector == NULL)
00571 return -1;
00572 f->f_type = SUMMA;
00573 f->function = &f_sum;
00574 f->function_done = &f_sum_done;
00575 f->collector->bits |= functor_pass_thru;
00576
00577 return f_apply(f->collector);
00578 }
00579
00580
00581 static int_t f_delta_done(functor *f)
00582 {
00583 log_info("delta done\n");
00584 f->bits &= ~functor_running;
00585 f->result = _difference(f->collector);
00586 xmit_datum(f);
00587
00588 f_cancel(f);
00589 return 0;
00590 }
00591
00592 int_t f_delta(functor *f)
00593 {
00594 log_info("delta\n");
00595 if (f == NULL || f->collector == NULL)
00596 return -1;
00597 f->f_type = DELTA;
00598 f->function = &f_delta;
00599 f->function_done = &f_delta_done;
00600 f->collector->bits |= functor_pass_thru;
00601
00602 return f_apply(f->collector);
00603 }
00604
00605
00606 static int_t f_mean_done(functor *f)
00607 {
00608 log_info("mean done\n");
00609 f->bits &= ~functor_running;
00610 f->result = _average(f->collector);
00611 xmit_datum(f);
00612
00613 f_cancel(f);
00614 return 0;
00615 }
00616
00617 int_t f_mean(functor *f)
00618 {
00619 log_info("mean\n");
00620 if (f == NULL || f->collector == NULL)
00621 return -1;
00622 f->f_type = BARX;
00623 f->function = &f_mean;
00624 f->function_done = &f_mean_done;
00625 f->collector->bits |= functor_pass_thru;
00626
00627 return f_apply(f->collector);
00628 }
00629
00630
00631 static int_t f_sigma_done(functor *f)
00632 {
00633 log_info("sigma done\n");
00634 f->bits &= ~functor_running;
00635 f->result = _variance(f->collector);
00636 xmit_datum(f);
00637
00638 f_cancel(f);
00639 return 0;
00640 }
00641
00642 int_t f_sigma(functor *f)
00643 {
00644 log_info("sigma\n");
00645 if (f == NULL || f->collector == NULL)
00646 return -1;
00647 f->f_type = SIGMA;
00648 f->function = &f_sigma;
00649 f->function_done = &f_sigma_done;
00650 f->collector->bits |= functor_pass_thru;
00651
00652 return f_apply(f->collector);
00653 }
00654
00655
00656
00657 static byte_t lambda_seq = 0;
00658
00659 static int_t f_lambda_done(functor *f)
00660 {
00661 log_info("lambda done\n");
00662 f->bits &= ~functor_running;
00663 #if 1
00664 f_cancel(f);
00665 return -1;
00666 #else
00668 xmit_data(f);
00669
00670 f_cancel(f);
00671 return 0;
00672 #endif
00673 }
00674
00675 int_t f_lambda(functor *f)
00676 {
00677 log_info("lambda\n");
00678 if (f == NULL)
00679 return -1;
00680 f->f_type = LAMBDA;
00681 f->task_id = _incr(lambda_seq);
00682 f->function = &f_lambda;
00683 f->function_done = &f_lambda_done;
00684
00685 return f_apply(f->collector);
00686 }
00687
00688
00689
00690 uint_t energy_diff(uint_t prev, uint_t cur)
00691 {
00692 return prev - cur;
00693 }
00694
00695
00696 uint_t time_diff(uint_t prev, uint_t cur)
00697 {
00698 return cur - prev;
00699 }
00700
00701
00702 static void __attribute((constructor)) init(void) {
00703 memset(_node_state, 0, sizeof(mote_state) * MAX_NODES);
00704 }