Exemplo n.º 1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(rd_client_process, ev, data)
{
  static struct etimer et;
  static coap_packet_t request[1];      /* This way the packet can be treated as pointer as usual. */
  static char query_buffer[200];
  static char rd_client_name[64];

  PROCESS_BEGIN();
  PROCESS_PAUSE();
  PRINTF("RD client started\n");
  sprintf(rd_client_name, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
      uip_lladdr.addr[0], uip_lladdr.addr[1], uip_lladdr.addr[2], uip_lladdr.addr[3],
      uip_lladdr.addr[4], uip_lladdr.addr[5], uip_lladdr.addr[6], uip_lladdr.addr[7]);
  while(1) {
    new_address = 0;
    while(!registered) {
      while(uip_is_addr_unspecified(&rd_server_ipaddr)) {
        status = RD_CLIENT_UNCONFIGURED;
        PROCESS_YIELD();
      }
      status = RD_CLIENT_REGISTERING;
      etimer_set(&et, CLOCK_SECOND);
      PROCESS_YIELD_UNTIL(etimer_expired(&et));
      PRINTF("Registering to ");
      PRINT6ADDR(&rd_server_ipaddr);
      PRINTF(" %d with %s\n", rd_server_port, resources_list);
      coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0);
      coap_set_header_uri_path(request, "rd");
      sprintf(query_buffer, "ep=%s&b=U&lt=%d", rd_client_name, RD_CLIENT_LIFETIME);
      coap_set_header_uri_query(request, query_buffer);
      coap_set_payload(request, (uint8_t *) resources_list, resources_list_size);

      COAP_BLOCKING_REQUEST_BLOCK_RESPONSE(coap_default_context, &rd_server_ipaddr, UIP_HTONS(rd_server_port), request, client_registration_request_handler, client_registration_response_handler);
    }
    status = RD_CLIENT_REGISTERED;
    etimer_set(&et, RD_CLIENT_LIFETIME * CLOCK_SECOND / 10 * 9);
    PROCESS_YIELD_UNTIL(etimer_expired(&et) || new_address);
    registered = 0;

    if(!new_address) {
      PRINTF("Update endpoint %s\n", registration_name);
      coap_init_message(request, COAP_TYPE_CON, COAP_PUT, 0);
      coap_set_header_uri_path(request, registration_name);
      sprintf(query_buffer, "b=U&lt=%d", RD_CLIENT_LIFETIME);
      coap_set_header_uri_query(request, query_buffer);

      COAP_BLOCKING_REQUEST(coap_default_context, &rd_server_ipaddr, UIP_HTONS(rd_server_port), request, client_update_response_handler);
    }
  }

  PROCESS_END();
}
Exemplo n.º 2
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_client_process, ev, data)
{
/*
   #if WITH_COMPOWER
   static int print = 0;
   #endif
 */
  PROCESS_BEGIN();

  PROCESS_PAUSE();

  set_global_address();

  PRINTF("CoAP client process started\n");

  print_local_addresses();

/**************************************************************************/
  static coap_packet_t request[1];      /* This way the packet can be treated as pointer as usual. */

  /* receives all CoAP messages */
  coap_init_engine();

  etimer_set(&et, GET_INTERVAL * CLOCK_SECOND);

  while(1) {
    PROCESS_YIELD();

    if(ev == tcpip_event) {
      printf("TCPIP_HANDLER\n");
      tcpip_handler();
    }

    if(etimer_expired(&et)) {
      count_get++;

/*---------------------------------------------------------------------------*/
      if(count_get < 10) { /* we do normal GET 3 times for each resource */
        /* TEST GET: looping through the 3 resources: status, vdc, hwcfg */
        /* Also CoAP GET doesn't need to have a payload */
        coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
        if(count_get % 3 == 1) {
          coap_set_header_uri_path(request, service_urls[1]);
          PRINTF("GET %d: %s\n", count_get, service_urls[1]);
        } else if(count_get % 3 == 2) {
          coap_set_header_uri_path(request, service_urls[2]);
          PRINTF("GET %d: %s\n", count_get, service_urls[2]);
        } else {
          coap_set_header_uri_path(request, service_urls[3]);
          PRINTF("GET %d: %s\n", count_get, service_urls[3]);
        }

#if PLATFORM_HAS_LEDS
        /* set yellow led when sending packet */
        leds_on(LEDS_YELLOW);
#endif
        COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
                              client_chunk_handler);
      } /* if (count_get < 10) */
/*---------------------------------------------------------------------------*/
      /* test PUT: vdc and hwcfg on odd and even packet */
      /* every 10th timer we PUT a resource: vdc and hwcfg alternately */
      if(count_get % 10 == 0) {
        /* static char msg[64] = ""; */
        char msg[64] = "";

	/*---------------------------------------------------------------------------*/
	/* We read CO2 sensor if it is enable */
	#ifdef CO2
       	  coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
          coap_set_header_uri_path(request, "/dcdc/co2");
          PRINTF("GET %d: %s\n", count_get, "/dcdc/co2");
	  #if PLATFORM_HAS_LEDS
            leds_on(LEDS_YELLOW);
	  #endif
            COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
                              client_chunk_handler);
	#endif
	/*---------------------------------------------------------------------------*/

        count_put++;
        coap_init_message(request, COAP_TYPE_CON, COAP_PUT, 0);

        if(count_put % 2 == 0) {
          coap_set_header_uri_path(request, service_urls[3]);
          generate_random_payload(3, msg);
          coap_set_payload(request, (uint8_t *)msg, sizeof(msg) - 1);
          PRINTF("PUT %d: %s PAYLOAD: %s\n", count_get, service_urls[3], msg);
        } else {
          coap_set_header_uri_path(request, service_urls[2]);
          generate_random_payload(2, msg);
          coap_set_payload(request, (uint8_t *)msg, sizeof(msg) - 1);
          PRINTF("PUT %d: %s PAYLOAD: %s\n", count_get, service_urls[2], msg);
        }

#if PLATFORM_HAS_LEDS
        /* set yellow led when sending packet */
        leds_on(LEDS_YELLOW);
#endif
        COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
                              client_chunk_handler);


      }

      /* after 2 more timeout we do GET to check the resource new value */
      if((count_get > 10) && ((count_get - 2) % 10 == 0)) {
        coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);

        if(count_put % 2 == 0) {
          coap_set_header_uri_path(request, service_urls[3]);
          PRINTF("GET %d: %s\n", count_get, service_urls[3]);
        } else {
          coap_set_header_uri_path(request, service_urls[2]);
          PRINTF("GET %d: %s\n", count_get, service_urls[2]);
        }

#if PLATFORM_HAS_LEDS
        /* set yellow led when sending packet */
        leds_on(LEDS_YELLOW);
#endif
        COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request,
                              client_chunk_handler);
      }

/*---------------------------------------------------------------------------*/

      /* test GET with observe option when timer expires the 15th time */
      if(count_get == 15) {
        /* PRINTF("GET %d: OBSERVE: %s\n", count_get, service_urls[2]); */
        toggle_observation();
      }

      etimer_reset(&et);
    }
  } /* END_WHILE(1) */

  PROCESS_END();
}
PROCESS_THREAD(coap_client_example, ev, data)
{
  PROCESS_BEGIN();

  static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */
  SERVER_NODE(&server_ipaddr);

  /* receives all CoAP messages */
  coap_receiver_init();

  etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);

#if PLATFORM_HAS_BUTTON
  SENSORS_ACTIVATE(button_sensor);
  printf("Press a button to request %s\n", service_urls[uri_switch]);
#endif

  while(1) {
    PROCESS_YIELD();

    if (etimer_expired(&et)) {
      printf("--Toggle timer--\n");

      /* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
      coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0 );
      coap_set_header_uri_path(request, service_urls[1]);

      const char msg[] = "Toggle!";
      coap_set_payload(request, (uint8_t *)msg, sizeof(msg)-1);


      PRINT6ADDR(&server_ipaddr);
      PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));

      COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request, client_chunk_handler);

      printf("\n--Done--\n");

      etimer_reset(&et);

#if PLATFORM_HAS_BUTTON
    } else if (ev == sensors_event && data == &button_sensor) {

      /* send a request to notify the end of the process */

      coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
      coap_set_header_uri_path(request, service_urls[uri_switch]);

      printf("--Requesting %s--\n", service_urls[uri_switch]);

      PRINT6ADDR(&server_ipaddr);
      PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));

      COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request, client_chunk_handler);

      printf("\n--Done--\n");

      uri_switch = (uri_switch+1) % NUMBER_OF_URLS;
#endif

    }
  }

  PROCESS_END();
}