⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfm_model.c

📁 传感器网络中的嵌入式操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
// $Id: rfm_model.c,v 1.13.2.2 2003/08/18 22:09:50 cssharp Exp $/*									tab:4 * "Copyright (c) 2000-2003 The Regents of the University  of California.   * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE      * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,  * 94704.  Attention:  Intel License Inquiry. *//* * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors: Philip Levis, Nelson Lee * */#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <pthread.h>/* * The simple RFM model simulates every mote being in a single cell * (they can all hear one another). Bit transmission is * error-free. Simulation is achieved by using a radio_active variable * for each mote, which starts at 0.  Every time a mote transmits, it * increments the radio_active value for every other mote. When a mote * listens, it hears a bit if the radio_active value is one or * greater. When a mote finishes transmitting, it decrements the * radio_active value of every other mote. Although very simple, this * simulation mechanism allows for extremeley accurate network timing * simulation. * *///char isTransmitting[TOSNODES];char transmitting[TOSNODES];int radio_active[TOSNODES];link_t* radio_connectivity[TOSNODES]; // adjacency listspthread_mutex_t radioConnectivityLock;char sendProb[TOSNODES];char receiveProb[TOSNODES];// buffer for idle detection over networkshort radio_heard[TOSNODES];// state indicating whether channel is idlebool radio_idle_state[TOSNODES];double noise_prob = 0;short IDLE_STATE_MASK = 0xffff;char* lossyFileName = "lossy.nss";bool simple_connected(int moteID1, int moteID2) {  return TRUE;}void simple_init() {  int i;  pthread_mutex_init(&radioConnectivityLock, NULL);  adjacency_list_init();  static_one_cell_init();  for (i = 0; i < tos_state.num_nodes; i++) {    radio_active[i] = 0;  }}void simple_transmit(int moteID, char bit) {  int i;    transmitting[moteID] = bit;  for (i = 0; i < tos_state.num_nodes; i++) {    radio_active[i] += bit;  }}void simple_stops_transmit(int moteID) {  int i;    if (transmitting[moteID]) {    transmitting[moteID] = 0;    for (i = 0; i < tos_state.num_nodes; i++) {      radio_active[i]--;    }  }}char simple_hears(int moteID) {  // Uncomment these lines to add erroneus 1s. The probability  // can be adjusted by changing the constants.  //int rand = random();  //if ((rand & (int)0xf) == 0xf) {  //  return 1;  //}  //else {  return (radio_active[moteID] > 0)? 1:0;  //}}link_t* simple_neighbors(int moteID) {  link_t *thelink;  pthread_mutex_lock(&radioConnectivityLock);  thelink = radio_connectivity[moteID];  pthread_mutex_unlock(&radioConnectivityLock);  return thelink;}rfm_model* create_simple_model() {  rfm_model* model = (rfm_model*)malloc(sizeof(rfm_model));  model->init = simple_init;  model->transmit = simple_transmit;  model->stop_transmit = simple_stops_transmit;  model->hears = simple_hears;  model->connected = simple_connected;  model->neighbors = simple_neighbors;  return model;}int read_entry(FILE* file, int* mote_one, int* mote_two) {  char buf[128];  int findex = 0;  int ch;  // Read in first number  while(1) {    ch = getc(file);    if (ch == EOF) {return 0;}    else if (ch >= '0' && ch <= '9') {      buf[findex] = (char)ch;      findex++;    }    else if (ch == ':') {      buf[findex] = 0;      break;    }    else if (ch == '\n' || ch == ' ' || ch == '\t') {      if (findex > 0) {return 0;}    }    else {      return 0;    }  }  *mote_one = atoi(buf);  findex = 0;  // Read in second number  while(1) {    ch = getc(file);    if (ch == EOF) {return 0;}    else if (ch >= '0' && ch <= '9') {      buf[findex] = (char)ch;      findex++;    }    else if (ch == '\n' || ch == ' ' || ch == '\t') {      if (findex == 0) {return 0;}      else {	buf[findex] = 0;	break;      }    }    else {      return 0;    }  }  *mote_two = atoi(buf);  return 1;}void static_one_cell_init() {  int i, j;  link_t* new_link;  pthread_mutex_init(&radioConnectivityLock, NULL);  radio_connectivity[0] = NULL;    for (i = 0; i < tos_state.num_nodes; i++) {    for (j = 0; j < tos_state.num_nodes; j++) {      if (i != j) {  	new_link = allocate_link(j);        new_link->data = 0.0; // bit error rate set to zero 	// so can be reused by lossy        new_link->next_link = radio_connectivity[i];  	radio_connectivity[i] = new_link;    }   }  }}void static_init() {  int sfd = open("cells.txt", O_RDONLY);  int i;  FILE* file = fdopen(sfd, "r");  link_t* new_link;    pthread_mutex_init(&radioConnectivityLock, NULL);  adjacency_list_init();  if (sfd < 0) {    dbg(DBG_ERROR, ("No cells.txt found for static rfm model. Defaulting to one cell.\n"));        static_one_cell_init();    return;  }    for (i = 0; i < TOSNODES; i++) {    radio_connectivity[i] = NULL;  }  while(1) {    int mote_one;    int mote_two;    if (read_entry(file, &mote_one, &mote_two)) {      new_link = allocate_link(mote_two);      new_link->next_link = radio_connectivity[mote_one];      radio_connectivity[mote_one] = new_link;      new_link = allocate_link(mote_one);      new_link->next_link = radio_connectivity[mote_two];      radio_connectivity[mote_two] = new_link;          }    else {      break;    }  }  dbg(DBG_BOOT, ("RFM connectivity graph constructed.\n"));}bool static_connected(int moteID1, int moteID2) {  // this method is rather slow, and runs on the order of the number  // of links attached moteID1 because it traverses moteID1's  // adjacency list to make this a constant time operation, add a  // hashtable to the adjacency list implementation  link_t* current_link;  pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID1];  while (current_link) {    if (current_link->mote == moteID2) {      pthread_mutex_unlock(&radioConnectivityLock);      return TRUE;    }    current_link = current_link->next_link;  }  pthread_mutex_unlock(&radioConnectivityLock);  return FALSE;}void static_transmit(int moteID, char bit) {  link_t* current_link;  pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID];  transmitting[moteID] = bit;  while (current_link) {    radio_active[current_link->mote] += bit;    current_link = current_link->next_link;  }  pthread_mutex_unlock(&radioConnectivityLock);}void static_stops_transmit(int moteID) {  link_t* current_link;  pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID];  if (transmitting[moteID]) {    transmitting[moteID] = 0;    while (current_link) {      radio_active[current_link->mote]--;      current_link = current_link->next_link;    }        }  pthread_mutex_unlock(&radioConnectivityLock);}char static_hears(int moteID) {  return (radio_active[moteID] > 0)? 1:0;}rfm_model* create_static_model() {  rfm_model* model = (rfm_model*)malloc(sizeof(rfm_model));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -