2017-10-24 23:46:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, SICS Swedish ICT
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Callback API for doing CoAP requests
|
|
|
|
* Adapted from the blocking API
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson, joakime@sics.se
|
|
|
|
*/
|
|
|
|
|
2017-12-21 17:54:28 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup coap
|
|
|
|
* @{
|
|
|
|
*/
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
#include "coap-engine.h"
|
|
|
|
#include "coap-callback-api.h"
|
|
|
|
#include "coap-transactions.h"
|
|
|
|
#include "sys/cc.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-07-26 09:54:57 +00:00
|
|
|
#include <inttypes.h>
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-05-04 08:58:58 +00:00
|
|
|
#define LOG_MODULE "coap"
|
2017-11-30 23:41:58 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_COAP
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
static void coap_request_callback(void *callback_data, coap_message_t *response);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
2018-05-23 13:59:43 +00:00
|
|
|
static int
|
2018-05-31 14:22:01 +00:00
|
|
|
progress_request(coap_callback_request_state_t *callback_state) {
|
|
|
|
coap_request_state_t *state = &callback_state->state;
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_message_t *request = state->request;
|
2017-10-24 23:46:28 +00:00
|
|
|
request->mid = coap_get_mid();
|
|
|
|
if((state->transaction =
|
|
|
|
coap_new_transaction(request->mid, state->remote_endpoint))) {
|
|
|
|
state->transaction->callback = coap_request_callback;
|
|
|
|
state->transaction->callback_data = state;
|
|
|
|
|
|
|
|
if(state->block_num > 0) {
|
|
|
|
coap_set_header_block2(request, state->block_num, 0,
|
|
|
|
COAP_MAX_CHUNK_SIZE);
|
|
|
|
}
|
2017-11-14 20:32:43 +00:00
|
|
|
state->transaction->message_len =
|
|
|
|
coap_serialize_message(request, state->transaction->message);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
coap_send_transaction(state->transaction);
|
2018-02-23 12:47:18 +00:00
|
|
|
LOG_DBG("Requested #%"PRIu32" (MID %u)\n", state->block_num, request->mid);
|
2018-05-23 13:59:43 +00:00
|
|
|
return 1;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2018-05-23 13:59:43 +00:00
|
|
|
return 0;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_request_callback(void *callback_data, coap_message_t *response)
|
2017-10-24 23:46:28 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_callback_request_state_t *callback_state = (coap_callback_request_state_t*)callback_data;
|
|
|
|
coap_request_state_t *state = &callback_state->state;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
uint32_t res_block1;
|
|
|
|
|
|
|
|
state->response = response;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("request callback\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
if(!state->response) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_WARN("Server not responding giving up...\n");
|
2018-05-31 14:22:01 +00:00
|
|
|
state->status = COAP_REQUEST_STATUS_TIMEOUT;
|
|
|
|
callback_state->callback(callback_state);
|
2017-10-24 23:46:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Got a response */
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_get_header_block2(state->response, &state->res_block, &state->more, NULL, NULL);
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL);
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Received #%lu%s B1:%lu (%u bytes)\n",
|
2018-05-31 14:22:01 +00:00
|
|
|
(unsigned long)state->res_block, (unsigned)state->more ? "+" : "",
|
2017-11-30 23:41:58 +00:00
|
|
|
(unsigned long)res_block1,
|
|
|
|
state->response->payload_len);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->res_block == state->block_num) {
|
2017-10-24 23:46:28 +00:00
|
|
|
/* Call the callback function as we have more data */
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->more) {
|
|
|
|
state->status = COAP_REQUEST_STATUS_MORE;
|
|
|
|
} else {
|
|
|
|
state->status = COAP_REQUEST_STATUS_RESPONSE;
|
|
|
|
}
|
|
|
|
callback_state->callback(callback_state);
|
2017-10-24 23:46:28 +00:00
|
|
|
/* this is only for counting BLOCK2 blocks.*/
|
|
|
|
++(state->block_num);
|
|
|
|
} else {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_WARN("WRONG BLOCK %"PRIu32"/%"PRIu32"\n", state->res_block, state->block_num);
|
|
|
|
++(state->block_error);
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->more) {
|
|
|
|
if((state->block_error) < COAP_MAX_ATTEMPTS) {
|
|
|
|
progress_request(callback_state);
|
|
|
|
} else {
|
|
|
|
/* failure - now we give up and notify the callback */
|
|
|
|
state->status = COAP_REQUEST_STATUS_BLOCK_ERROR;
|
|
|
|
callback_state->callback(callback_state);
|
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
} else {
|
2018-05-31 14:22:01 +00:00
|
|
|
/* No more blocks, finish and notify the callback */
|
|
|
|
state->status = COAP_REQUEST_STATUS_FINISHED;
|
2017-10-24 23:46:28 +00:00
|
|
|
state->response = NULL;
|
2018-05-31 14:22:01 +00:00
|
|
|
callback_state->callback(callback_state);
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
2018-05-23 13:59:43 +00:00
|
|
|
int
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_send_request(coap_callback_request_state_t *callback_state, coap_endpoint_t *endpoint,
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_message_t *request,
|
2018-05-31 14:22:01 +00:00
|
|
|
void (*callback)(coap_callback_request_state_t *callback_state))
|
2017-10-24 23:46:28 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_request_state_t *state = &callback_state->state;
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
state->more = 0;
|
|
|
|
state->res_block = 0;
|
|
|
|
state->block_error = 0;
|
2017-10-24 23:46:28 +00:00
|
|
|
state->block_num = 0;
|
|
|
|
state->response = NULL;
|
|
|
|
state->request = request;
|
|
|
|
state->remote_endpoint = endpoint;
|
2018-05-31 14:22:01 +00:00
|
|
|
callback_state->callback = callback;
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
return progress_request(callback_state);
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2017-12-21 17:54:28 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|