Beispiel #1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(otau_client_process, ev, data)
{
  static struct etimer server_timer;
  uip_ip6addr_t *srv_addr;
  
  PROCESS_BEGIN();
  servreg_hack_init();
  set_global_address();
  
  ota_mgr_init();
  
  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);
  etimer_set(&server_timer, SEND_INTERVAL);					  

  while(1) {
	PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&server_timer));
	srv_addr = servreg_hack_lookup(SERVICE_ID);
	if(srv_addr != NULL) {
		printf("Server address configured\r\n");
		server_configured =1;
		configure_server_details(srv_addr);
		memcpy((uint8_t *)&(global_server_addr.u16[4]),&(srv_addr->u16[4]),NATIVE_ADDR_SIZE);
        //uip_create_linklocal_allnodes_mcast(srv_addr);
		etimer_stop(&server_timer);
	}
	else
	{
		etimer_reset(&server_timer);
		printf("server not found\r\n");
	}
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udpstream_process, ev, data)
{
  static uint16_t streamno;
  static struct etimer et;
  static uip_ipaddr_t *ipaddr;

  PROCESS_BEGIN();
  PROCESS_PAUSE();

  printf("Formatting Coffee FS...\n");
  cfs_coffee_format();
  printf("done.\n");
  /* We need re-initialize queuebuf after formatting */
  queuebuf_init();

  /* Start service registration */
  servreg_hack_init();
  ipaddr = set_global_address();

  if(node_id == SINK_ID) {
    /* The sink creates a dag and waits for UDP datagrams */
    create_rpl_dag(ipaddr);
    servreg_hack_register(SERVICE_ID, ipaddr);
    simple_udp_register(&unicast_connection, UDP_PORT,
                        NULL, UDP_PORT, receiver);
    while(1) {
      PROCESS_WAIT_EVENT();
    }
  } else if(node_id == SENDER_ID) {
    /* The sender looks for the sink and sends UDP streams */
    ipaddr = NULL;
    simple_udp_register(&unicast_connection, UDP_PORT,
                          NULL, UDP_PORT, receiver);

    etimer_set(&et, 10*CLOCK_SECOND);
    etimer_restart(&et);

    while(1) {
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
      if(ipaddr != NULL) {
        streamno++;
        send_stream(ipaddr, streamno);
      } else {
        ipaddr = servreg_hack_lookup(SERVICE_ID);
        if(ipaddr != NULL) {
          etimer_set(&et, 2*CLOCK_SECOND);
          printf("Streaming to ");
          uip_debug_ipaddr_print(ipaddr);
          printf("\n");
        } else {
          printf("Service %d not found\n", SERVICE_ID);
        }
      }
      etimer_restart(&et);
    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;

  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  SENSORS_ACTIVATE(button_sensor);
 // SENSORS_ACTIVATE(light_sensor);

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  //etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

   // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
   // etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    addr = servreg_hack_lookup(SERVICE_ID);

    if(addr != NULL) {
      static unsigned int button_number;
      char buf[50];

      if(button_number == 0) {
	button_number = 1;
	leds_toggle(LEDS_ALL);
      } else {
	button_number = 0;
	leds_toggle(LEDS_ALL);
      }

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf("\n");

      //sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, LUMINAIRE_ILLUMINANCE);
      sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, (int)(410 ));      

      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
uip_ipaddr_t *
servreg_hack_lookup(servreg_hack_id_t id)
{
  servreg_hack_item_t *t;

  servreg_hack_init();

  purge_registrations();

  for(t = servreg_hack_list_head(); t != NULL; t = list_item_next(t)) {
    if(servreg_hack_item_id(t) == id) {
      return servreg_hack_item_address(t);
    }
  }
  return NULL;
}
Beispiel #5
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
	PRINTF("SIMPLE_UNICAST_SENDER: Unicast attempt!\n");
    addr = servreg_hack_lookup(SERVICE_ID);
	
    if(addr != NULL) {
      static unsigned int message_number;
      char buf[20];

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf(" \n");
      sprintf(buf, "Message %d", message_number);
      message_number++;
      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_receiver_process, ev, data)
{
  uip_ipaddr_t *ipaddr;

  PROCESS_BEGIN();

  servreg_hack_init();

  ipaddr = set_global_address();

  create_rpl_dag(ipaddr);

  servreg_hack_register(SERVICE_ID, ipaddr);

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  while(1) {
    PROCESS_WAIT_EVENT();
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
void
servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
{
  static servreg_hack_item_t *t;
  static struct servreg_hack_registration *r;
  /* Walk through list, see if we already have a service ID
     registered. If not, allocate a new registration and put it on our
     list. If we cannot allocate a service registration, we reuse one
     from the service registrations made by others. */

  servreg_hack_init();

  for(t = list_head(own_services);
      t != NULL;
      t = list_item_next(t)) {
    if(servreg_hack_item_id(t) == id) {
      return;
    }
  }

  r = memb_alloc(&registrations);
  if(r == NULL) {
    printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
    return;
  }
  r->id = id;
  r->seqno = 1;
  uip_ipaddr_copy(&r->addr, addr);
  timer_set(&r->timer, LIFETIME / 2);
  list_push(own_services, r);


  PROCESS_CONTEXT_BEGIN(&servreg_hack_process);
  etimer_set(&sendtimer, random_rand() % (NEW_REG_TIME));
  PROCESS_CONTEXT_END(&servreg_hack_process);

}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;

  uip_ipaddr_t *addr;

  PROCESS_BEGIN();

  SENSORS_ACTIVATE(button_sensor);
 // SENSORS_ACTIVATE(light_sensor);

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);
  rx_start_duration = energest_type_time(ENERGEST_TYPE_LISTEN);
  //etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {

   // PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
   // etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    addr = servreg_hack_lookup(SERVICE_ID);

    if(addr != NULL) {
      static unsigned int button_number;
      char buf[50];

      if(button_number == 0) {
	button_number = 1;
	leds_toggle(LEDS_ALL);
      } else {
	button_number = 0;
	leds_toggle(LEDS_ALL);
      }

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf("\n");

      //sprintf(buf, "Occupancy Detector: %d, Luminaire Illuminance: %d", button_number, LUMINAIRE_ILLUMINANCE);
      sprintf(buf, "Occupancy Detector: %d, Luminous Intensity: %d", button_number, /*(int)(random() % 180 + 320 )*/ 1500 );      

      simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);
	    //  printf("energy rx: %lu\n", energest_type_time(ENERGEST_TYPE_LISTEN) - rx_start_duration);
		//printf("energy tx: %lu\n", energest_type_time(ENERGEST_TYPE_TRANSMIT) - rx_start_duration);
		//printf("cpu: %lu\n", energest_type_time(ENERGEST_TYPE_CPU) - rx_start_duration);
		//printf("lpm: %lu\n", energest_type_time(ENERGEST_TYPE_LPM) - rx_start_duration);
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(unicast_sender_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t *addr;
  static int  sensor_value = 0;

  PROCESS_BEGIN();

  SENSORS_ACTIVATE(button_sensor);
  SENSORS_ACTIVATE(radio_sensor);
  
#ifdef X_NUCLEO_IKS01A1
  SENSORS_ACTIVATE(temperature_sensor);
  SENSORS_ACTIVATE(humidity_sensor);
  SENSORS_ACTIVATE(pressure_sensor);
  SENSORS_ACTIVATE(magneto_sensor);
  SENSORS_ACTIVATE(acceleration_sensor);
  SENSORS_ACTIVATE(gyroscope_sensor);
#endif /*X_NUCLEO_IKS01A1*/

  servreg_hack_init();

  set_global_address();

  simple_udp_register(&unicast_connection, UDP_PORT,
                      NULL, UDP_PORT, receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) 
  {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    addr = servreg_hack_lookup(SERVICE_ID);
    if(addr != NULL) {
      static unsigned int message_number = 0;
      char buf[40];

      printf("Sending unicast to ");
      uip_debug_ipaddr_print(addr);
      printf("\n");
	  
#ifdef X_NUCLEO_IKS01A1
    switch(message_number) {
	case 0:
          sensor_value = temperature_sensor.value(0);
	  sprintf(buf, "Temperature:\t%d.%d C", sensor_value/10, ABS_VALUE(sensor_value)%10);
	  break;
	case 1:
	  sprintf(buf, "Humidity:\t%d.%d rH", humidity_sensor.value(0)/10, humidity_sensor.value(0)%10);
	  break;
	case 2:
	  sprintf(buf, "Pressure:\t%d.%d mbar", pressure_sensor.value(0)/10, pressure_sensor.value(0)%10);
	  break;
	case 3:
	  sprintf(buf, "Magneto:\t\t%d/%d/%d (X/Y/Z) mgauss", magneto_sensor.value(X_AXIS),
                                                              magneto_sensor.value(Y_AXIS),
                                                              magneto_sensor.value(Z_AXIS));
	  break;
	case 4:
	  sprintf(buf, "Acceleration:\t%d/%d/%d (X/Y/Z) mg", acceleration_sensor.value(X_AXIS),
                                                               acceleration_sensor.value(Y_AXIS),
                                                               acceleration_sensor.value(Z_AXIS));
	  break;
	case 5:
	  sprintf(buf, "Gyroscope:\t%d/%d/%d (X/Y/Z) mdps", gyroscope_sensor.value(X_AXIS),
                                                              gyroscope_sensor.value(Y_AXIS),
                                                              gyroscope_sensor.value(Z_AXIS));
	  break;
	case 6:
          sensor_value = radio_sensor.value(RADIO_SENSOR_LAST_PACKET);
          sprintf(buf, "Radio (RSSI):\t%d.%d dBm", sensor_value/10, ABS_VALUE(sensor_value)%10);
	  break;
	case 7:
          sprintf(buf, "Radio (LQI):\t%d", radio_sensor.value(RADIO_SENSOR_LAST_VALUE));
	  break;
	default:
	  break;
     }
     simple_udp_sendto(&unicast_connection, buf, strlen(buf) + 1, addr);

     message_number++;
     if(message_number > 7) {
       message_number = 0;
     }
#endif /*X_NUCLEO_IKS01A1*/
    } else {
      printf("Service %d not found\n", SERVICE_ID);
    }
  }

  PROCESS_END();
}