2014-04-22 08:31:43 +00:00
|
|
|
/*
|
|
|
|
* 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 <kovatsch@inf.ethz.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-net.h"
|
2017-09-01 14:57:50 +00:00
|
|
|
#include "coap-engine.h"
|
2017-10-26 22:20:08 +00:00
|
|
|
#include "coap-blocking-api.h"
|
2017-11-05 22:38:45 +00:00
|
|
|
#if PLATFORM_SUPPORTS_BUTTON_HAL
|
|
|
|
#include "dev/button-hal.h"
|
|
|
|
#else
|
2014-04-22 08:31:43 +00:00
|
|
|
#include "dev/button-sensor.h"
|
2017-11-05 22:38:45 +00:00
|
|
|
#endif
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2018-01-12 15:08:42 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-04-13 16:12:49 +00:00
|
|
|
#define LOG_MODULE "App"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_APP
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* FIXME: This server address is hard-coded for Cooja and link-local for unconnected border router. */
|
2017-10-26 22:20:08 +00:00
|
|
|
#define SERVER_EP "coap://[fe80::212:7402:0002:0202]"
|
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
#define TOGGLE_INTERVAL 10
|
|
|
|
|
|
|
|
PROCESS(er_example_client, "Erbium Example Client");
|
|
|
|
AUTOSTART_PROCESSES(&er_example_client);
|
|
|
|
|
|
|
|
static struct etimer et;
|
|
|
|
|
|
|
|
/* Example URIs that can be queried. */
|
|
|
|
#define NUMBER_OF_URLS 4
|
|
|
|
/* leading and ending slashes only for demo purposes, get cropped automatically when setting the Uri-Path */
|
|
|
|
char *service_urls[NUMBER_OF_URLS] =
|
|
|
|
{ ".well-known/core", "/actuators/toggle", "battery/", "error/in//path" };
|
|
|
|
#if PLATFORM_HAS_BUTTON
|
|
|
|
static int uri_switch = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */
|
|
|
|
void
|
2017-11-14 20:32:43 +00:00
|
|
|
client_chunk_handler(coap_message_t *response)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
const uint8_t *chunk;
|
|
|
|
|
|
|
|
int len = coap_get_payload(response, &chunk);
|
|
|
|
|
|
|
|
printf("|%.*s", len, (char *)chunk);
|
|
|
|
}
|
|
|
|
PROCESS_THREAD(er_example_client, ev, data)
|
|
|
|
{
|
2018-03-05 15:35:45 +00:00
|
|
|
static coap_endpoint_t server_ep;
|
2014-04-22 08:31:43 +00:00
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
static coap_message_t request[1]; /* This way the packet can be treated as pointer as usual. */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2018-01-12 15:08:42 +00:00
|
|
|
coap_endpoint_parse(SERVER_EP, strlen(SERVER_EP), &server_ep);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND);
|
|
|
|
|
|
|
|
#if PLATFORM_HAS_BUTTON
|
2017-11-05 22:38:45 +00:00
|
|
|
#if !PLATFORM_SUPPORTS_BUTTON_HAL
|
2014-04-22 08:31:43 +00:00
|
|
|
SENSORS_ACTIVATE(button_sensor);
|
|
|
|
#endif
|
2017-11-05 22:38:45 +00:00
|
|
|
printf("Press a button to request %s\n", service_urls[uri_switch]);
|
|
|
|
#endif /* PLATFORM_HAS_BUTTON */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-01-12 15:08:42 +00:00
|
|
|
LOG_INFO_COAP_EP(&server_ep);
|
|
|
|
LOG_INFO_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-26 22:20:08 +00:00
|
|
|
COAP_BLOCKING_REQUEST(&server_ep, request, client_chunk_handler);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
printf("\n--Done--\n");
|
|
|
|
|
|
|
|
etimer_reset(&et);
|
|
|
|
|
|
|
|
#if PLATFORM_HAS_BUTTON
|
2017-11-05 22:38:45 +00:00
|
|
|
#if PLATFORM_SUPPORTS_BUTTON_HAL
|
|
|
|
} else if(ev == button_hal_release_event) {
|
|
|
|
#else
|
2014-04-22 08:31:43 +00:00
|
|
|
} else if(ev == sensors_event && data == &button_sensor) {
|
2017-11-05 22:38:45 +00:00
|
|
|
#endif
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* 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]);
|
|
|
|
|
2018-01-12 15:08:42 +00:00
|
|
|
LOG_INFO_COAP_EP(&server_ep);
|
|
|
|
LOG_INFO_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-26 22:20:08 +00:00
|
|
|
COAP_BLOCKING_REQUEST(&server_ep, request,
|
2014-04-22 08:31:43 +00:00
|
|
|
client_chunk_handler);
|
|
|
|
|
|
|
|
printf("\n--Done--\n");
|
|
|
|
|
|
|
|
uri_switch = (uri_switch + 1) % NUMBER_OF_URLS;
|
2017-11-05 22:38:45 +00:00
|
|
|
#endif /* PLATFORM_HAS_BUTTON */
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|