示例#1
0
PROCESS_THREAD(collect_common_process, ev, data)
{
  static struct etimer period_timer, wait_timer;
  PROCESS_BEGIN();

  collect_common_net_init();
  /* Send a packet every 60-62 seconds. */
  etimer_set(&period_timer, CLOCK_SECOND * PERIOD);
  while(1) {
    PROCESS_WAIT_EVENT();
    if(ev == serial_line_event_message) {
      char *line;
      char query[10];
      char reg[2];
      line = (char *)data;
      if(strncmp(line, "collect", 7) == 0 ||
         strncmp(line, "gw", 2) == 0) {
        collect_common_set_sink();
      } else if(strncmp(line, "net", 3) == 0) {
        collect_common_net_print();
      } else if(strncmp(line, "time ", 5) == 0) {
        unsigned long tmp;
        line += 6;
        while(*line == ' ') {
          line++;
        }
        tmp = strtolong(line);
        time_offset = clock_seconds() - tmp;
      } else if(strncmp(line, "mac ", 4) == 0) {
        line +=4;
        while(*line == ' ') {
          line++;
        }
        if(*line == '0') {
          NETSTACK_RDC.off(1);
        } else {
          NETSTACK_RDC.on();
        }

      } else if(strncmp(line, "~K", 2) == 0 ||
                strncmp(line, "killall", 7) == 0) {
        /* Ignore stop commands */
      } else if(strncmp(line, "Query-",6) == 0){        
        /* To extract the region information of the requested query */
        strcpy(query, line+7);
        char *pch = NULL;

        pch = strtok (query,".");
        if(strncmp(pch, "x", 1) != 0) {
          b = atoi(pch);
          multicast_flag = 0;
        }
        else{
          b = 0;
          multicast_flag = 1;
#if SCENARIO
          goto BUILD;
#endif
        }

        pch = strtok (NULL, ".");
        if(strncmp(pch, "x", 1) != 0) {
          f = atoi(pch);
          multicast_flag = 0;
        }
        else{
          f = 0;
          multicast_flag = 1;
#if SCENARIO
          goto LEVL;
#endif
        }

        pch = strtok (NULL, ".");
        if(strncmp(pch, "x", 1) != 0) {
          r = atoi(pch);
          multicast_flag = 0;
        }
        else{
          r = 0;
          multicast_flag = 1;
#if SCENARIO
          goto ROM;
#endif
        }

        pch = strtok (NULL, ".");
        if(strncmp(pch, "x", 1) != 0) {
          sid = atoi(pch);
          multicast_flag = 0;
        }
        else{
          sid = 0;
          multicast_flag = 1;
#if SCENARIO
          goto SENSR;
#endif
        }
        goto SEND_QR;
#if SCENARIO
BUILD:  for(b=1;b<=3;b++)
         for(f=1;f<=3;f++)
          for(r=1;r<=3;r++)
            for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

LEVL:      for(f=1;f<=3;f++)
              for(r=1;r<=3;r++)
                for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

ROM:          for(r=1;r<=3;r++)
                for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;

SENSR:          for(sid=1;sid<=3;sid++)
                    query_send_sink(b,f,r,sid);
        goto JUMP;
        
#endif    
SEND_QR:                query_send_sink(b,f,r,sid);
JUMP:{}

      } else if(strncmp(line, "Region-",7) == 0){   //Region of the node
        strcpy(reg, line+8);

        if(strncmp(reg, "B", 1) == 0){
          region = BUILDING;
        }
        else if(strncmp(reg, "L", 1) == 0){
          region = LEVEL;
        }
        else if(strncmp(reg, "R", 1) == 0){
          region = ROOM;
        }
        else if(strncmp(reg, "S", 1) == 0){
          region = SENSOR;
        }
      }
      else {
        //printf("unhandled command: %s\n", line);
      }
    }
    if(ev == PROCESS_EVENT_TIMER) {
      if(data == &period_timer) {
        etimer_reset(&period_timer);
        etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT));
      } else if(data == &wait_timer) {
        if(send_active) {
          /* Time to send the data */
          /* Broadcast its own region rank */
          if( region == ROOM )          
            broadcast_identity(ROOM);
          else if ( region == BUILDING )
            broadcast_identity(BUILDING);
          else if ( region == LEVEL )
            broadcast_identity(LEVEL);
          else if ( region == SENSOR )
            broadcast_identity(SENSOR);
        }
      }
    }
  }

  PROCESS_END();
}
示例#2
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(collect_common_process, ev, data)
{
  static struct etimer period_timer, wait_timer;
  PROCESS_BEGIN();

  collect_common_net_init();

  /* Send a packet every 60-62 seconds. */
  etimer_set(&period_timer, CLOCK_SECOND * PERIOD);
  while(1) {
    PROCESS_WAIT_EVENT();
    if(ev == serial_line_event_message) {
    /*  char *line;
      line = (char *)data;
      if(strncmp(line, "collect", 7) == 0 ||
         strncmp(line, "gw", 2) == 0) {
        collect_common_set_sink();
      } else if(strncmp(line, "net", 3) == 0) {
        collect_common_net_print();
      } else if(strncmp(line, "time ", 5) == 0) {
        unsigned long tmp;
        line += 6;
        while(*line == ' ') {
          line++;
        }
        tmp = strtolong(line);
        time_offset = clock_seconds() - tmp;
        printf("Time offset set to %lu\n", time_offset);
      } else if(strncmp(line, "mac ", 4) == 0) {
        line +=4;
        while(*line == ' ') {
          line++;
        }
        if(*line == '0') {
          NETSTACK_RDC.off(1);
          printf("mac: turned MAC off (keeping radio on): %s\n",
                 NETSTACK_RDC.name);
        } else {
          NETSTACK_RDC.on();
          printf("mac: turned MAC on: %s\n", NETSTACK_RDC.name);
        }

      } else if(strncmp(line, "~K", 2) == 0 ||
                strncmp(line, "killall", 7) == 0) {
        /* Ignore stop commands */
    /*  } else {
        printf("unhandled command: %s\n", line);
      }*/
    }
    if(ev == PROCESS_EVENT_TIMER) {
      if(data == &period_timer) {
        etimer_reset(&period_timer);
        etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT));
      } else if(data == &wait_timer) {
        if(send_active) {
          /* Time to send the data */
          collect_common_send();
        }
      }
    }
  }

  PROCESS_END();
}
示例#3
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udp_server_process, ev, data)
{
  struct sensors_sensor *sensor;
  uip_ipaddr_t ipaddr;
  struct uip_ds6_addr *root_if;
  static struct etimer timer;
//  char buf[MAX_PAYLOAD_LEN];

  PROCESS_BEGIN();

  collect_common_net_init();
  
  PROCESS_PAUSE();

  SENSORS_ACTIVATE(button_sensor);

  PRINTF("UDP server started\r\n");

  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 1);
  uip_ip6addr(&clientipaddr,0xaaaa,0,0,0,0x0212,0x4B00,0x051d,0x0d30);
  
  /* uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); */
  uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL); //MANUAL 手动模式;说明书;
  root_if = uip_ds6_addr_lookup(&ipaddr);//look up 查找;
  if(root_if != NULL) {
    rpl_dag_t *dag;
    dag = rpl_set_root(RPL_DEFAULT_INSTANCE,(uip_ip6addr_t *)&ipaddr);
    uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
    rpl_set_prefix(dag, &ipaddr, 64); //prefix 前缀;
    PRINTF("created a new RPL dag.\r\n");
  } else {
    PRINTF("failed to create a new RPL DAG.\r\n");
  }

  print_local_addresses();

  /* The data sink runs with a 100% duty cycle in order to ensure high
     packet reception rates. */
  NETSTACK_RDC.off(1);

  server_conn = udp_new(NULL, UIP_HTONS(UDP_CLIENT_PORT), NULL);
  udp_bind(server_conn, UIP_HTONS(UDP_SERVER_PORT));

  PRINTF("Created a server connection with remote address ");
  PRINT6ADDR(&server_conn->ripaddr);
  PRINTF("\r\n");
  PRINTF(" local/remote port %u/%u\r\n", UIP_HTONS(server_conn->lport),
         UIP_HTONS(server_conn->rport));
  initlcm();
  set_wenzi();

  while(1) {
    PROCESS_YIELD();
        if(ev == sensors_event){
	sensor = (struct sensors_sensor *)data;
	if(sensor == &button_sensor){
            func_index=table[func_index].up;
	}
        
        if(sensor == &button_2_sensor){
            func_index=table[func_index].down;
	}
        if(sensor == &button_3_sensor){
            func_index=table[func_index].enter;
	}
        
        current_operation_index=table[func_index].current_operation;
        (*current_operation_index)();//执行当前操作函数
        }
    if(ev == tcpip_event) {
      tcpip_handler();
    } else if (ev == blink_event) {
      etimer_set(&timer, CLOCK_CONF_SECOND);
    }
    if(ev == PROCESS_EVENT_TIMER && sta == 1) {
      if(data == &timer){
        leds_toggle(LEDS_GREEN | LEDS_RED);
      }
       etimer_reset(&timer);
    }
    if(ev == serial_line_event_message) {
      char *line;
      line = (char *)data;
      if(strncmp(line, "GET", 3) == 0) {
        PRINTF("GET");
    uip_ipaddr_copy(&server_conn->ripaddr, &clientipaddr);
    uip_udp_packet_send(server_conn, "GET", sizeof("GET"));
    uip_create_unspecified(&server_conn->ripaddr);
      }else {
        printf("unhandled command: %s\n", line);
      }
          /* Store rv temporarily in dec so we can use it for the battery */
    }     
  }

  PROCESS_END();
}