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

📄 rfm_model.c

📁 传感器网络中的嵌入式操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  model->init = static_init;  model->transmit = static_transmit;  model->stop_transmit = static_stops_transmit;  model->hears = static_hears;  model->connected = static_connected;  model->neighbors = simple_neighbors;  return model;}bool lossy_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];  dbg(DBG_TEMP, "connections for %i\n", moteID1);  while (current_link) {    if ((current_link->mote == moteID2) &&	(current_link->data < 1.0)) {      dbg(DBG_TEMP, "connected to %i\n", moteID2);      pthread_mutex_unlock(&radioConnectivityLock);      return TRUE;    }    current_link = current_link->next_link;  }  pthread_mutex_unlock(&radioConnectivityLock);  return FALSE;}void lossy_transmit(int moteID, char bit) {  link_t* current_link;     pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID];  transmitting[moteID] = bit;  while (current_link) {    int r = rand() % 100000;    double prob = ((double)r) / 100000.0;    int tmp_bit = bit;    if (prob < current_link->data) { // bit error, reverse the bit      tmp_bit = (tmp_bit)? 0:1;    }    radio_active[current_link->mote] += tmp_bit;    radio_idle_state[current_link->mote] = 0;    current_link->bit = tmp_bit;    current_link = current_link->next_link;  }  pthread_mutex_unlock(&radioConnectivityLock);}void lossy_stop_transmit(int moteID) {  link_t* current_link;    pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID];  transmitting[moteID] = 0;  while (current_link) {    radio_active[current_link->mote] -= current_link->bit;    current_link->bit = 0;    current_link = current_link->next_link;  }        pthread_mutex_unlock(&radioConnectivityLock);}char lossy_hears(int moteID) {  char bit_heard = (radio_active[moteID] > 0)? 1:0;  if (radio_idle_state[moteID]) {      int r = rand() % 100000;      double prob = ((double)r) / 100000.0;      if (prob < noise_prob) { // noise has caused this bit to be reversed	bit_heard = (bit_heard)? 0:1;      }  }  else {      short temp_heard = radio_heard[moteID];      temp_heard <<= 1;      temp_heard |= bit_heard;      radio_heard[moteID] = temp_heard;      if ((radio_heard[moteID] & IDLE_STATE_MASK) == 0) {	  radio_idle_state[moteID] = 1;      }  }  return bit_heard;}int read_lossy_entry(FILE* file, int* mote_one, int* mote_two, double* loss) {  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 == ':') {      buf[findex] = 0;      break;    }    else if (ch == '\n' || ch == ' ' || ch == '\t') {      if (findex == 0) {return 0;}      else {	buf[findex] = 0;	break;      }    }    else {      return 0;    }  }  *mote_two = atoi(buf);  findex = 0;  // Read in loss rate number  while(1) {    ch = getc(file);    if (ch == EOF) {return 0;}    else if ((ch >= '0' && ch <= '9') || ch == '.') {      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;    }  }  *loss = atof(buf);  return 1;}void lossy_init() {  int sfd = open(lossyFileName, O_RDONLY);  int i;  FILE* file = fdopen(sfd, "r");  link_t* new_link;  dbg_clear(DBG_SIM, "Initializing lossy model from %s.\n", lossyFileName);  pthread_mutex_init(&radioConnectivityLock, NULL);  adjacency_list_init();  if (sfd < 0) {    dbg(DBG_SIM, "Cannot open %s - assuming single radio cell\n", lossyFileName);    static_one_cell_init();    return;  }  for (i = 0; i < TOSNODES; i++) {    radio_connectivity[i] = NULL;    radio_idle_state[i] = 0;    radio_heard[i] = 0;  }  while(1) {    int mote_one;    int mote_two;    double loss;    if (read_lossy_entry(file, &mote_one, &mote_two, &loss)) {      if (mote_one != mote_two) {	new_link = allocate_link(mote_two);	new_link->data = loss;	new_link->next_link = radio_connectivity[mote_one];	radio_connectivity[mote_one] = new_link;      }    }    else {      break;    }  }  dbg(DBG_BOOT, ("RFM connectivity graph constructed.\n"));}link_t* lossy_neighbors(int moteID) {  link_t *thelink;  pthread_mutex_lock(&radioConnectivityLock);  thelink = radio_connectivity[moteID];  pthread_mutex_unlock(&radioConnectivityLock);  return thelink;}rfm_model* create_lossy_model(char* file) {  rfm_model* model = (rfm_model*)malloc(sizeof(rfm_model));  if (file != NULL) {    lossyFileName = file;  }  model->init = lossy_init;  model->transmit = lossy_transmit;  model->stop_transmit = lossy_stop_transmit;  model->hears = lossy_hears;  model->connected = lossy_connected;  model->neighbors = lossy_neighbors;  return model;}double get_link_prob_value(uint16_t moteID1, uint16_t moteID2) {  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 current_link->data;    }    current_link = current_link->next_link;  }  pthread_mutex_unlock(&radioConnectivityLock);  return 1;  }void set_link_prob_value(uint16_t moteID1, uint16_t moteID2, double prob) {  link_t* current_link;  link_t* new_link;  pthread_mutex_lock(&radioConnectivityLock);  current_link = radio_connectivity[moteID1];  dbg(DBG_SIM, "RFM: MDW: Setting loss prob %d->%d to %0.3f\n", moteID1, moteID2, prob);  while (current_link) {    if (current_link->mote == moteID2) {      current_link->data = prob;      pthread_mutex_unlock(&radioConnectivityLock);      return;          }    current_link = current_link->next_link;  }  new_link = allocate_link(moteID2);  new_link->next_link = radio_connectivity[moteID1];  new_link->data = prob;  radio_connectivity[moteID1] = new_link;  pthread_mutex_unlock(&radioConnectivityLock);}double get_noise_prob_value() {  return noise_prob;  }void set_noise_prob_value(double prob) {  noise_prob = prob;}char get_wait_length_before_idle() {  short temp = IDLE_STATE_MASK;  char count = 0;  while (temp) {    count++;    temp = temp >> 1;    temp &= 0x7fff;  }  return count;}void set_wait_length_before_idle(int count) {  short temp = 0;  while (count) {    temp = temp << 1;    temp |= 1;    count--;  }  IDLE_STATE_MASK = temp;}

⌨️ 快捷键说明

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