/* * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This file is part of the Contiki operating system. */ /** * \file * Erbium (Er) CoAP client example. * \author * Matthias Kovatsch * Gaƫtan Harter */ #include #include #include #include "contiki.h" #include "contiki-net.h" #include "er-coap-engine.h" #define DEBUG 0 #include "uip-debug.h" #ifndef ER_COAP_SERVER_ADDR /* cooja2 */ #define ER_COAP_SERVER_ADDR "fe80::0212:7402:0002:0x0202" #endif /* ER_COAP_SERVER_ADDR */ #define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT) #define TOGGLE_INTERVAL 10 PROCESS(er_example_client, "Erbium Example Client"); AUTOSTART_PROCESSES(&er_example_client); /* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */ void client_chunk_handler(void *response) { const uint8_t *chunk; int len = coap_get_payload(response, &chunk); printf("|%.*s", len, (char *)chunk); } PROCESS_THREAD(er_example_client, ev, data) { PROCESS_BEGIN(); static struct etimer et; static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ static uip_ipaddr_t server_ipaddr; uiplib_ipaddrconv(ER_COAP_SERVER_ADDR, &server_ipaddr); /* receives all CoAP messages */ coap_init_engine(); etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); 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_GET, 0); coap_set_header_uri_path(request, "test/hello"); 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); } } PROCESS_END(); }