45 #include "coap-blocking-api.h" 54 #define LOG_MODULE "coap" 55 #define LOG_LEVEL LOG_LEVEL_COAP 61 coap_blocking_request_callback(
void *callback_data, coap_message_t *response)
63 coap_blocking_request_state_t *blocking_state = (coap_blocking_request_state_t *)callback_data;
65 blocking_state->state.response = response;
70 (coap_blocking_request_state_t *blocking_state, process_event_t ev,
71 coap_endpoint_t *remote_ep,
72 coap_message_t *request,
73 coap_blocking_response_handler_t request_callback))
76 coap_request_state_t *state = &blocking_state->state;
81 state->response = NULL;
86 state->block_error = 0;
89 request->mid = coap_get_mid();
90 if((state->transaction = coap_new_transaction(request->mid, remote_ep))) {
91 state->transaction->callback = coap_blocking_request_callback;
92 state->transaction->callback_data = blocking_state;
94 if(state->block_num > 0) {
95 coap_set_header_block2(request, state->block_num, 0,
98 state->transaction->message_len = coap_serialize_message(request,
103 coap_send_transaction(state->transaction);
104 LOG_DBG(
"Requested #%"PRIu32
" (MID %u)\n", state->block_num, request->mid);
108 if(!state->response) {
109 LOG_WARN(
"Server not responding\n");
110 state->status = COAP_REQUEST_STATUS_TIMEOUT;
114 coap_get_header_block2(state->response, &state->res_block, &state->more, NULL, NULL);
116 LOG_DBG(
"Received #%"PRIu32
"%s (%u bytes)\n", state->res_block, state->more ?
"+" :
"",
117 state->response->payload_len);
119 state->status = COAP_REQUEST_STATUS_MORE;
121 state->status = COAP_REQUEST_STATUS_RESPONSE;
124 if(state->res_block == state->block_num) {
125 request_callback(state->response);
126 ++(state->block_num);
128 LOG_WARN(
"WRONG BLOCK %"PRIu32
"/%"PRIu32
"\n",
129 state->res_block, state->block_num);
130 ++(state->block_error);
133 LOG_WARN(
"Could not allocate transaction buffer");
136 }
while(state->more && (state->block_error) < COAP_MAX_ATTEMPTS);
138 if((state->block_error) >= COAP_MAX_ATTEMPTS) {
140 state->status = COAP_REQUEST_STATUS_BLOCK_ERROR;
143 state->status = COAP_REQUEST_STATUS_FINISHED;
145 PT_END(&blocking_state->pt);
CoAP engine implementation.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
#define PT_END(pt)
Declare the end of a protothread.
void process_poll(struct process *p)
Request a process to be polled.
#define PT_EXIT(pt)
Exit the protothread.
#define PT_THREAD(name_args)
Declaration of a protothread.
Default definitions of C compiler quirk work-arounds.