示例#1
0
void
separate_finalize_handler()
{
  if(separate_active) {
    coap_transaction_t *transaction = NULL;

    if((transaction =
        coap_new_transaction(separate_store->request_metadata.mid,
                             &separate_store->request_metadata.addr,
                             separate_store->request_metadata.port))) {
      /* This way the packet can be treated as pointer as usual. */
      coap_packet_t response[1];

      /* Restore the request information for the response. */
      coap_separate_resume(response, &separate_store->request_metadata,
                           CONTENT_2_05);

      coap_set_payload(response, separate_store->buffer,
                       strlen(separate_store->buffer));

      /*
       * Be aware to respect the Block2 option, which is also stored
       * in the coap_separate_t.  As it is a critical option, this
       * example resource pretends to handle it for compliance.
       */
      coap_set_header_block2(response,
                             separate_store->request_metadata.block2_num, 0,
                             separate_store->request_metadata.block2_size);

      /* Warning: No check for serialization error. */
      transaction->packet_len =
        coap_serialize_message(response, transaction->packet);
      coap_send_transaction(transaction);
      /* The engine will clear the transaction (right after send for
         NON, after acked for CON). */

      separate_active = 0;
    } else {
      /*
       * Set timer for retry, send error message, ...
       * The example simply waits for another button press.
       */
    }
  }                             /* if (separate_active) */
}
void
oc_send_separate_response(oc_separate_response_t *handle,
                          oc_status_t response_code)
{
  oc_response_buffer_t response_buffer;
  response_buffer.buffer = handle->buffer;
  response_buffer.response_length = response_length();
  response_buffer.code = oc_status_code(response_code);

  coap_separate_t *cur = oc_list_head(handle->requests), *next = NULL;
  coap_packet_t response[1];

  while (cur != NULL) {
    next = cur->next;
    if (cur->observe > 0) {
      coap_transaction_t *t =
        coap_new_transaction(coap_get_mid(), &cur->endpoint);
      if (t) {
        coap_separate_resume(response, cur, oc_status_code(response_code),
                             t->mid);
        coap_set_header_content_format(response, APPLICATION_CBOR);
        if (cur->observe == 1) {
          coap_set_header_observe(response, 1);
        }
        if (response_buffer.response_length > 0) {
          coap_set_payload(response, handle->buffer,
                           response_buffer.response_length);
        }
        t->message->length = coap_serialize_message(response, t->message->data);
        coap_send_transaction(t);
      }
      coap_separate_clear(handle, cur);
    } else {
      if (coap_notify_observers(NULL, &response_buffer, &cur->endpoint) == 0) {
        coap_separate_clear(handle, cur);
      }
    }
    cur = next;
  }
  if (oc_list_length(handle->requests) == 0) {
    handle->active = 0;
  }
}
示例#3
0
static void
res_resume_handler()
{
  if(separate_active) {
    PRINTF("/separate       ");
    coap_transaction_t *transaction = NULL;
    if((transaction = coap_new_transaction(separate_store->request_metadata.mid,
					   NULL,
                                           &separate_store->request_metadata.addr,
                                           separate_store->request_metadata.port))) {
      PRINTF(
        "RESPONSE (%s %u)\n", separate_store->request_metadata.type == COAP_TYPE_CON ? "CON" : "NON", separate_store->request_metadata.mid);

      coap_packet_t response[1]; /* This way the packet can be treated as pointer as usual. */

      /* Restore the request information for the response. */
      coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05);

      REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
      coap_set_payload(response, separate_store->buffer,
                       strlen(separate_store->buffer));

      /*
       * Be aware to respect the Block2 option, which is also stored in the coap_separate_t.
       * As it is a critical option, this example resource pretends to handle it for compliance.
       */
      coap_set_header_block2(response,
                             separate_store->request_metadata.block2_num, 0,
                             separate_store->request_metadata.block2_size);

      /* Warning: No check for serialization error. */
      transaction->packet_len = coap_serialize_message(response,
                                                       transaction->packet);
      coap_send_transaction(transaction);
      /* The engine will clear the transaction (right after send for NON, after acked for CON). */

      separate_active = 0;
    } else {
      PRINTF("ERROR (transaction)\n");
    }
  } /* if (separate_active) */
}