Commit 4357629c authored by Olaf Bergmann's avatar Olaf Bergmann

add handler for application data delivery

parent d2b443db
......@@ -211,6 +211,16 @@ rnd(uint8_t *out, size_t len) {
#endif /* HAVE_GETRANDOM */
}
static void
response_handler(struct dcaf_context_t *dcaf_context,
dcaf_transaction_t *transaction,
coap_pdu_t *received) {
(void)dcaf_context;
(void)transaction;
dcaf_log(DCAF_LOG_INFO, "got response: \n");
coap_show_pdu(LOG_INFO, received);
}
int
main(int argc, char **argv) {
dcaf_context_t *dcaf = NULL;
......@@ -261,9 +271,9 @@ main(int argc, char **argv) {
}
coap_startup();
coap_dtls_set_log_level(log_level.dtls);
coap_set_log_level(log_level.coap);
dcaf_set_log_level(log_level.dcaf);
coap_set_log_level(log_level.coap);
coap_dtls_set_log_level(log_level.dtls);
if ((optind < argc) && ((method = cmdline_method(argv[optind])) > 0)) {
optind++;
......@@ -311,6 +321,7 @@ main(int argc, char **argv) {
}
if (!dcaf_send_request(dcaf, method, uri, strlen(uri),
optlist, payload.s, payload.length,
response_handler,
DCAF_TRANSACTION_BLOCK)) {
dcaf_log(DCAF_LOG_EMERG, "cannot send request\n");
exit(EXIT_FAILURE);
......
......@@ -38,6 +38,7 @@ struct dcaf_transaction_t {
coap_block_t block;
dcaf_response_handler_t response_handler;
dcaf_error_handler_t error_handler;
dcaf_application_handler_t application_handler;
int flags;
dcaf_transaction_id_t tid;
coap_pdu_t *pdu;
......
......@@ -42,6 +42,10 @@ typedef void (*dcaf_error_handler_t)(dcaf_context_t *,
dcaf_transaction_t *,
int error);
typedef void (*dcaf_application_handler_t)(dcaf_context_t *,
dcaf_transaction_t *,
coap_pdu_t *);
/**
* Creates a new transaction object. When finished, the storage
* allocated for this object must be released with
......@@ -137,6 +141,7 @@ dcaf_transaction_t *dcaf_send_request_uri(dcaf_context_t *dcaf_context,
dcaf_optlist_t options,
const uint8_t *data,
size_t data_len,
dcaf_application_handler_t app_hnd,
int flags);
/**
* Sends a request with method @p code to the endpoint denoted
......@@ -171,6 +176,7 @@ dcaf_transaction_t *dcaf_send_request(dcaf_context_t *dcaf_context,
dcaf_optlist_t options,
const uint8_t *data,
size_t data_len,
dcaf_application_handler_t app_hnd,
int flags);
#endif /* _DCAF_TRANSACTION_H_ */
......@@ -254,12 +254,13 @@ dcaf_transaction_start(dcaf_context_t *dcaf_context,
dcaf_transaction_t *
dcaf_send_request_uri(dcaf_context_t *dcaf_context,
int code,
const coap_uri_t *uri,
dcaf_optlist_t options,
const uint8_t *data,
size_t data_len,
int flags) {
int code,
const coap_uri_t *uri,
dcaf_optlist_t options,
const uint8_t *data,
size_t data_len,
dcaf_application_handler_t app_hnd,
int flags) {
coap_context_t *ctx;
int result;
coap_address_t dst;
......@@ -343,6 +344,7 @@ dcaf_send_request_uri(dcaf_context_t *dcaf_context,
dcaf_log(DCAF_LOG_WARNING, "cannot create new transaction\n");
goto error;
}
t->application_handler = app_hnd;
dcaf_log(DCAF_LOG_DEBUG, "added transaction %02x%02x%02x%02x\n",
t->tid[0], t->tid[1], t->tid[2], t->tid[3]);
......@@ -367,7 +369,7 @@ dcaf_send_request_uri(dcaf_context_t *dcaf_context,
unsigned int wait_ms = 0; /* TODO: set to global timeout option */
unsigned int time_spent = 0;
while (!(done && coap_can_exit(ctx))) {
unsigned int timeout = wait_ms == 0 ? 1000 : min(wait_ms, 1000);
unsigned int timeout = wait_ms == 0 ? 5000 : min(wait_ms, 5000);
/* coap_io_process() returns the time in milliseconds it has
* spent. We use this value to determine if we have run out of
......@@ -411,6 +413,7 @@ dcaf_send_request(dcaf_context_t *dcaf_context,
dcaf_optlist_t options,
const uint8_t *data,
size_t data_len,
dcaf_application_handler_t app_hnd,
int flags) {
unsigned char buf[uri_len];
coap_uri_t uri;
......@@ -424,6 +427,6 @@ dcaf_send_request(dcaf_context_t *dcaf_context,
}
return dcaf_send_request_uri(dcaf_context, code, &uri, options,
data, data_len, flags);
data, data_len, app_hnd, flags);
}
#undef min
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment