54 #define LOG_MODULE "coap" 55 #define LOG_LEVEL LOG_LEVEL_COAP 60 static uint32_t res_block;
62 static uint8_t block_error;
64 static void coap_request_callback(
void *callback_data, coap_message_t *response);
69 progress_request(coap_request_state_t *state) {
70 coap_message_t *request = state->request;
71 request->mid = coap_get_mid();
72 if((state->transaction =
73 coap_new_transaction(request->mid, state->remote_endpoint))) {
74 state->transaction->callback = coap_request_callback;
75 state->transaction->callback_data = state;
77 if(state->block_num > 0) {
78 coap_set_header_block2(request, state->block_num, 0,
81 state->transaction->message_len =
82 coap_serialize_message(request, state->transaction->message);
84 coap_send_transaction(state->transaction);
85 LOG_DBG(
"Requested #%"PRIu32
" (MID %u)\n", state->block_num, request->mid);
94 coap_request_callback(
void *callback_data, coap_message_t *response)
96 coap_request_state_t *state = (coap_request_state_t *)callback_data;
99 state->response = response;
101 LOG_DBG(
"request callback\n");
103 if(!state->response) {
104 LOG_WARN(
"Server not responding giving up...\n");
105 state->callback(state);
110 coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
111 coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL);
113 LOG_DBG(
"Received #%lu%s B1:%lu (%u bytes)\n",
114 (
unsigned long)res_block, (
unsigned)more ?
"+" :
"",
115 (
unsigned long)res_block1,
116 state->response->payload_len);
118 if(res_block == state->block_num) {
120 state->callback(state);
122 ++(state->block_num);
124 LOG_WARN(
"WRONG BLOCK %"PRIu32
"/%"PRIu32
"\n", res_block, state->block_num);
128 if(more && block_error < COAP_MAX_ATTEMPTS) {
129 progress_request(state);
132 state->response = NULL;
133 state->callback(state);
141 coap_message_t *request,
142 void (*callback)(coap_request_state_t *state))
150 state->block_num = 0;
151 state->response = NULL;
152 state->request = request;
153 state->remote_endpoint = endpoint;
154 state->callback = callback;
156 return progress_request(state);
CoAP engine implementation.
int coap_send_request(coap_request_state_t *state, coap_endpoint_t *endpoint, coap_message_t *request, void(*callback)(coap_request_state_t *state))
Send a CoAP request to a remote endpoint.
Callback API for doing CoAP requests Adapted from the blocking API
CoAP module for reliable transport
Default definitions of C compiler quirk work-arounds.