120 lines
4.7 KiB
C
120 lines
4.7 KiB
C
/*
|
|
* Copyright (c) 2011, 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
|
|
* CoAP module for separate responses
|
|
* \author
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "er-coap-07-separate.h"
|
|
#include "er-coap-07-transactions.h"
|
|
|
|
#define DEBUG 0
|
|
#if DEBUG
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
|
|
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
|
|
#else
|
|
#define PRINTF(...)
|
|
#define PRINT6ADDR(addr)
|
|
#define PRINTLLADDR(addr)
|
|
#endif
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
int coap_separate_handler(resource_t *resource, void *request, void *response)
|
|
{
|
|
coap_packet_t *const coap_req = (coap_packet_t *) request;
|
|
coap_packet_t *const coap_res = (coap_packet_t *) response;
|
|
|
|
PRINTF("Separate response for /%s MID %u\n", resource->url, coap_res->mid);
|
|
|
|
/* Only ack CON requests. */
|
|
if (coap_req->type==COAP_TYPE_CON)
|
|
{
|
|
coap_transaction_t *const t = coap_get_transaction_by_mid(coap_res->mid);
|
|
|
|
/* send separate ACK. */
|
|
coap_packet_t ack[1];
|
|
/* ACK with empty code (0) */
|
|
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
|
|
/* Serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct. */
|
|
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata), coap_serialize_message(ack, uip_appdata));
|
|
|
|
/* Change response to separate response. */
|
|
coap_res->type = COAP_TYPE_CON;
|
|
coap_res->mid = coap_get_mid();
|
|
|
|
/* Update MID in transaction for identification. */
|
|
t->mid = coap_res->mid;
|
|
}
|
|
|
|
/* Pre-handlers could skip the handling by returning 0. */
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
coap_separate_response(void *response, coap_separate_t *separate_store)
|
|
{
|
|
coap_packet_t *const coap_res = (coap_packet_t *) response;
|
|
coap_transaction_t *const t = coap_get_transaction_by_mid(coap_res->mid);
|
|
|
|
if (t)
|
|
{
|
|
uip_ipaddr_copy(&separate_store->addr, &t->addr);
|
|
separate_store->port = t->port;
|
|
|
|
separate_store->mid = coap_res->mid;
|
|
separate_store->type = coap_res->type;
|
|
|
|
memcpy(separate_store->token, coap_res->token, coap_res->token_len);
|
|
separate_store->token_len = coap_res->token_len;
|
|
|
|
separate_store->block2_num = coap_res->block2_num;
|
|
separate_store->block2_more = coap_res->block2_more;
|
|
separate_store->block2_size = coap_res->block2_size;
|
|
separate_store->block2_offset = coap_res->block2_offset;
|
|
|
|
/* Signal the engine to skip automatic response and clear transaction by engine. */
|
|
coap_error_code = MANUAL_RESPONSE;
|
|
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|