Skip to content

Commit

Permalink
fixup! Don't mix up coap_status_t and oc_status_t enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 18, 2023
1 parent b7fcd10 commit a966fc1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
7 changes: 3 additions & 4 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,9 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
oc_send_response_internal(request, code, APPLICATION_VND_OCF_CBOR, size,
true);

int status_code = oc_status_code(code);
if ((method == OC_PUT || method == OC_POST) && status_code >= 0 &&
(coap_status_t)status_code <
oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
coap_status_t status_code = oc_status_code_unsafe(code);
if ((method == OC_PUT || method == OC_POST) &&
status_code < oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
if (iface_mask == OC_IF_CREATE) {
coap_notify_collection_observers(
collection, request->response->response_buffer, iface_mask);
Expand Down
11 changes: 6 additions & 5 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,10 +1163,10 @@ ri_invoke_coap_entity_set_response(coap_packet_t *response,

const oc_response_buffer_t *response_buffer =
ctx.response_obj->response_buffer;
if (response_buffer->code == (coap_status_t)oc_status_code(OC_IGNORE)) {
if (response_buffer->code == CLEAR_TRANSACTION) {
/* If the server-side logic chooses to reject a request, it sends
* below a response code of IGNORE, which results in the messaging
* layer freeing the CoAP transaction associated with the request.
* below a response code of CLEAR_TRANSACTION, which results in the
* messaging layer freeing the CoAP transaction associated with the request.
*/
coap_set_global_status_code(CLEAR_TRANSACTION);
return;
Expand Down Expand Up @@ -1638,9 +1638,10 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
}
#endif /* OC_SERVER */

if (request_obj.origin && (request_obj.origin->flags & MULTICAST) &&
if (request_obj.origin != NULL &&
(request_obj.origin->flags & MULTICAST) != 0 &&
response_buffer.code >= oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
response_buffer.code = (coap_status_t)oc_status_code(OC_IGNORE);
response_buffer.code = CLEAR_TRANSACTION;
}

ri_invoke_coap_entity_set_response_ctx_t resp_ctx = {
Expand Down
27 changes: 19 additions & 8 deletions api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ oc_send_response_internal(oc_request_t *request, oc_status_t response_code,
oc_content_format_t content_format,
size_t response_length, bool trigger_cb)
{
if (!request) {
int status_code = oc_status_code(response_code);
if (status_code < 0) {
assert(false && "invalid response code");
OC_ERR("invalid response code(%d)", (int)response_code);
return;
}
request->response->response_buffer->content_format = content_format;
request->response->response_buffer->response_length = response_length;
request->response->response_buffer->code =
(coap_status_t)oc_status_code(response_code);
request->response->response_buffer->code = (coap_status_t)status_code;
if (trigger_cb) {
oc_trigger_send_response_callback(request, response_code);
}
Expand Down Expand Up @@ -182,8 +184,8 @@ oc_send_response(oc_request_t *request, oc_status_t response_code)
void
oc_ignore_request(oc_request_t *request)
{
request->response->response_buffer->code =
(coap_status_t)oc_status_code(OC_IGNORE);
// oc_status_code(OC_IGNORE) equals CLEAR_TRANSACTION
request->response->response_buffer->code = CLEAR_TRANSACTION;
}

void
Expand Down Expand Up @@ -418,11 +420,15 @@ oc_send_response_raw(oc_request_t *request, const uint8_t *payload, size_t size,
oc_content_format_t content_format,
oc_status_t response_code)
{
int status_code = oc_status_code(response_code);
if (status_code < 0) {
OC_ERR("invalid response code(%d)", (int)response_code);
return;
}
request->response->response_buffer->content_format = content_format;
memcpy(request->response->response_buffer->buffer, payload, size);
request->response->response_buffer->response_length = size;
request->response->response_buffer->code =
(coap_status_t)oc_status_code(response_code);
request->response->response_buffer->code = (coap_status_t)status_code;
}

void
Expand Down Expand Up @@ -780,7 +786,12 @@ oc_send_separate_response(oc_separate_response_t *handle,
response_buffer.response_length = response_length();
}

response_buffer.code = (coap_status_t)oc_status_code(response_code);
int code = oc_status_code(response_code);
if (code < 0) {
OC_ERR("invalid response code(%d)", (int)response_code);
return;
}
response_buffer.code = (coap_status_t)code;
response_buffer.content_format = APPLICATION_VND_OCF_CBOR;

coap_separate_t *cur = oc_list_head(handle->requests);
Expand Down
4 changes: 3 additions & 1 deletion api/oc_server_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cbor.h>
#endif /* OC_RES_BATCH_SUPPORT */

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -88,7 +89,8 @@ void oc_process_baseline_interface_with_filter(
/** Setup response for the request */
void oc_send_response_internal(oc_request_t *request, oc_status_t response_code,
oc_content_format_t content_format,
size_t response_length, bool trigger_cb);
size_t response_length, bool trigger_cb)
OC_NONNULL();

#ifdef __cplusplus
}
Expand Down
5 changes: 2 additions & 3 deletions messaging/coap/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,13 +1009,12 @@ coap_fill_response(oc_response_t *response, oc_resource_t *resource,
resource->get_handler.cb(&request, iface_mask,
resource->get_handler.user_data);
} else {
response->response_buffer->code = (coap_status_t)oc_status_code(OC_IGNORE);
response->response_buffer->code = CLEAR_TRANSACTION;
}
#ifdef OC_DYNAMIC_ALLOCATION
response->response_buffer->buffer_size = oc_rep_get_encoded_payload_size();
#endif /* OC_DYNAMIC_ALLOCATION */
if (response->response_buffer->code ==
(coap_status_t)oc_status_code(OC_IGNORE)) {
if (response->response_buffer->code == CLEAR_TRANSACTION) {
COAP_DBG("resource request ignored");
return false;
}
Expand Down

0 comments on commit a966fc1

Please sign in to comment.