Skip to content

Commit

Permalink
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 2442c7b commit 6d72bf4
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 69 deletions.
47 changes: 24 additions & 23 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ oc_get_next_collection_with_link(const oc_resource_t *resource,
typedef struct oc_handle_collection_request_result_t
{
bool ok;
int ecode;
int pcode;
coap_status_t ecode;
coap_status_t pcode;
} oc_handle_collection_request_result_t;

static bool
Expand Down Expand Up @@ -752,8 +752,8 @@ static oc_handle_collection_request_result_t
oc_handle_collection_baseline_request(oc_method_t method, oc_request_t *request)
{
oc_collection_t *collection = (oc_collection_t *)request->resource;
int ecode = oc_status_code(OC_STATUS_OK);
int pcode = oc_status_code(OC_STATUS_BAD_REQUEST);
coap_status_t ecode = oc_status_code_unsafe(OC_STATUS_OK);
coap_status_t pcode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
if (method == OC_PUT || method == OC_POST) {
if (collection->res.set_properties.cb.set_props == NULL) {
OC_ERR("internal collection error: set properties callback not set");
Expand Down Expand Up @@ -791,7 +791,7 @@ oc_handle_collection_baseline_request(oc_method_t method, oc_request_t *request)
collection->res.get_properties.user_data);
}
oc_rep_end_root_object();
pcode = ecode = oc_status_code(OC_STATUS_OK);
pcode = ecode = oc_status_code_unsafe(OC_STATUS_OK);
}

oc_handle_collection_request_result_t result = {
Expand Down Expand Up @@ -894,8 +894,8 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
const oc_resource_t *notify_resource)
{
assert(request != NULL);
int ecode = oc_status_code(OC_STATUS_OK);
int pcode = oc_status_code(OC_STATUS_BAD_REQUEST);
coap_status_t ecode = oc_status_code_unsafe(OC_STATUS_OK);
coap_status_t pcode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
CborEncoder encoder;
CborEncoder prev_link;
oc_request_t rest_request;
Expand Down Expand Up @@ -945,7 +945,7 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
pay = pay->next;
}
if (href == NULL || oc_string_len(*href) == 0) {
ecode = oc_status_code(OC_STATUS_BAD_REQUEST);
ecode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
goto processed_request;
}
process_request:
Expand Down Expand Up @@ -980,7 +980,7 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
#ifdef OC_SECURITY
if (request->origin != NULL &&
!oc_sec_check_acl(method, link->resource, request->origin)) {
response_buffer.code = oc_status_code(OC_STATUS_FORBIDDEN);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_FORBIDDEN);
} else
#endif /* OC_SECURITY */
{
Expand All @@ -992,8 +992,8 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
NULL)) {
oc_handle_collection_request_result_t res = {
.ok = false,
.ecode = oc_status_code(OC_STATUS_OK),
.pcode = oc_status_code(OC_STATUS_BAD_REQUEST),
.ecode = oc_status_code_unsafe(OC_STATUS_OK),
.pcode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST),
};
return res;
}
Expand Down Expand Up @@ -1043,16 +1043,16 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
}
}
if (method_not_found) {
ecode = oc_status_code(OC_STATUS_METHOD_NOT_ALLOWED);
ecode = oc_status_code_unsafe(OC_STATUS_METHOD_NOT_ALLOWED);
memcpy(&links_array, &prev_link, sizeof(CborEncoder));
goto next;
} else {
if ((method == OC_PUT || method == OC_POST) &&
response_buffer.code <
oc_status_code(OC_STATUS_BAD_REQUEST)) {
oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
}
if (response_buffer.code <
oc_status_code(OC_STATUS_BAD_REQUEST)) {
oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
pcode = response_buffer.code;
} else {
ecode = response_buffer.code;
Expand All @@ -1076,7 +1076,7 @@ oc_handle_collection_batch_request(oc_method_t method, oc_request_t *request,
}
} break;
default:
ecode = oc_status_code(OC_STATUS_BAD_REQUEST);
ecode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
goto processed_request;
}
rep = rep->next;
Expand All @@ -1098,15 +1098,15 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
oc_interface_mask_t iface_mask,
const oc_resource_t *notify_resource)
{
int ecode = oc_status_code(OC_STATUS_OK);
int pcode = oc_status_code(OC_STATUS_BAD_REQUEST);
coap_status_t ecode = oc_status_code_unsafe(OC_STATUS_OK);
coap_status_t pcode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
switch (iface_mask) {
#ifdef OC_COLLECTIONS_IF_CREATE
case OC_IF_CREATE:
if (oc_handle_collection_create_request(method, request)) {
pcode = ecode = oc_status_code(OC_STATUS_OK);
pcode = ecode = oc_status_code_unsafe(OC_STATUS_OK);
} else {
pcode = ecode = oc_status_code(OC_STATUS_BAD_REQUEST);
pcode = ecode = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
}
break;
#endif /* OC_COLLECTIONS_IF_CREATE */
Expand All @@ -1122,7 +1122,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
}
case OC_IF_LL:
oc_handle_collection_linked_list_request(request);
pcode = ecode = oc_status_code(OC_STATUS_OK);
pcode = ecode = oc_status_code_unsafe(OC_STATUS_OK);
break;
case OC_IF_B: {
oc_handle_collection_request_result_t res =
Expand All @@ -1149,8 +1149,8 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
}

oc_status_t code = OC_STATUS_BAD_REQUEST;
if (ecode < oc_status_code(OC_STATUS_BAD_REQUEST) &&
pcode < oc_status_code(OC_STATUS_BAD_REQUEST)) {
if (ecode < oc_status_code_unsafe(OC_STATUS_BAD_REQUEST) &&
pcode < oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
switch (method) {
case OC_GET:
code = OC_STATUS_OK;
Expand All @@ -1173,8 +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);

coap_status_t status_code = oc_status_code_unsafe(code);
if ((method == OC_PUT || method == OC_POST) &&
oc_status_code(code) < oc_status_code(OC_STATUS_BAD_REQUEST)) {
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
68 changes: 44 additions & 24 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "api/oc_network_events_internal.h"
#include "api/oc_rep_encode_internal.h"
#include "api/oc_rep_decode_internal.h"
#include "api/oc_ri_internal.h"
#include "messaging/coap/coap_internal.h"
#include "messaging/coap/coap_options.h"
#include "messaging/coap/constants.h"
Expand Down Expand Up @@ -119,7 +120,7 @@ OC_MEMB(g_resource_default_s, oc_resource_defaults_data_t,
OC_MAX_APP_RESOURCES);
#endif /* OC_SERVER */

static unsigned int oc_coap_status_codes[__NUM_OC_STATUS_CODES__];
static coap_status_t oc_coap_status_codes[__NUM_OC_STATUS_CODES__] = { 0 };

static const char *oc_status_strs[] = {
"OC_STATUS_OK", /* 0 */
Expand All @@ -145,7 +146,7 @@ static const char *oc_status_strs[] = {
};

static void
set_mpro_status_codes(void)
ri_set_status_codes(void)
{
/* OK_200 */
oc_coap_status_codes[OC_STATUS_OK] = CONTENT_2_05;
Expand Down Expand Up @@ -299,11 +300,26 @@ oc_delayed_delete_resource(oc_resource_t *resource)

#endif /* OC_SERVER */

coap_status_t
oc_status_code_unsafe(oc_status_t key)
{
return oc_coap_status_codes[key];
}

int
oc_status_code(oc_status_t key)
{
// safe: no status code is larger than INT_MAX
return (int)oc_coap_status_codes[key];
assert(key >= 0);
if (key >= __NUM_OC_STATUS_CODES__) {
if (OC_IGNORE == key) {
return CLEAR_TRANSACTION;
}
OC_WRN("oc_status_code: invalid status code %d", (int)key);
return -1;
}

// int cast is safe: no status code is larger than INT_MAX
return oc_status_code_unsafe(key);
}

int
Expand All @@ -314,6 +330,7 @@ oc_coap_status_to_status(coap_status_t status)
return i;
}
}
OC_WRN("oc_coap_status_to_status: invalid coap status code %d", (int)status);
return -1;
}

Expand Down Expand Up @@ -446,7 +463,7 @@ oc_ri_get_app_resource_by_uri(const char *uri, size_t uri_len, size_t device)
void
oc_ri_init(void)
{
set_mpro_status_codes();
ri_set_status_codes();

#ifdef OC_SERVER
oc_list_init(g_app_resources);
Expand Down Expand Up @@ -1146,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 == 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 All @@ -1159,8 +1176,9 @@ ri_invoke_coap_entity_set_response(coap_packet_t *response,
if (ctx.resource != NULL) {
#ifdef OC_HAS_FEATURE_ETAG
if (ctx.method == OC_GET &&
(response_buffer->code == oc_status_code(OC_STATUS_OK) ||
response_buffer->code == oc_status_code(OC_STATUS_NOT_MODIFIED))) {
(response_buffer->code == oc_status_code_unsafe(OC_STATUS_OK) ||
response_buffer->code ==
oc_status_code_unsafe(OC_STATUS_NOT_MODIFIED))) {
ri_invoke_coap_entity_set_response_etag(response, &ctx);
}
#endif /* OC_HAS_FEATURE_ETAG */
Expand All @@ -1174,7 +1192,7 @@ ri_invoke_coap_entity_set_response(coap_packet_t *response,
!ctx.resource_is_collection &&
#endif /* OC_COLLECTIONS */
(ctx.method == OC_PUT || ctx.method == OC_POST) &&
response_buffer->code < oc_status_code(OC_STATUS_BAD_REQUEST)) {
response_buffer->code < oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
if ((ctx.iface_mask == OC_IF_STARTUP) ||
(ctx.iface_mask == OC_IF_STARTUP_REVERT)) {
oc_resource_defaults_data_t *resource_defaults_data =
Expand Down Expand Up @@ -1210,7 +1228,7 @@ ri_invoke_coap_entity_set_response(coap_packet_t *response,
}

if (response_buffer->code ==
oc_status_code(OC_STATUS_REQUEST_ENTITY_TOO_LARGE)) {
oc_status_code_unsafe(OC_STATUS_REQUEST_ENTITY_TOO_LARGE)) {
coap_options_set_size1(response, (uint32_t)OC_BLOCK_SIZE);
}

Expand Down Expand Up @@ -1275,7 +1293,7 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
!oc_ri_filter_request_by_device_id(endpoint->device, uri_query,
uri_query_len)) {
coap_set_global_status_code(CLEAR_TRANSACTION);
coap_set_status_code(ctx->response, OC_IGNORE);
coap_set_status_code(ctx->response, CLEAR_TRANSACTION);
return false;
}

Expand Down Expand Up @@ -1562,28 +1580,29 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
if (forbidden) {
OC_WRN("ocri: Forbidden request");
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_FORBIDDEN);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_FORBIDDEN);
} else if (entity_too_large) {
OC_WRN("ocri: Request payload too large (hence incomplete)");
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_REQUEST_ENTITY_TOO_LARGE);
response_buffer.code =
oc_status_code_unsafe(OC_STATUS_REQUEST_ENTITY_TOO_LARGE);
} else if (bad_request) {
OC_WRN("ocri: Bad request");
/* Return a 4.00 response */
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_BAD_REQUEST);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_BAD_REQUEST);
} else if (!cur_resource) {
OC_WRN("ocri: Could not find resource");
/* Return a 4.04 response if the requested resource was not found */
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_NOT_FOUND);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_NOT_FOUND);
} else if (method_not_allowed) {
OC_WRN("ocri: Could not find method");
/* Return a 4.05 response if the resource does not implement the
* request method.
*/
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_METHOD_NOT_ALLOWED);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_METHOD_NOT_ALLOWED);
}
#ifdef OC_SECURITY
else if (not_authorized) {
Expand All @@ -1593,13 +1612,13 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
* access the resource. A 4.01 response is sent.
*/
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_UNAUTHORIZED);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_UNAUTHORIZED);
}
#endif /* OC_SECURITY */
else {
if (not_modified) {
response_buffer.response_length = 0;
response_buffer.code = oc_status_code(OC_STATUS_NOT_MODIFIED);
response_buffer.code = oc_status_code_unsafe(OC_STATUS_NOT_MODIFIED);
}
success = true;
}
Expand All @@ -1612,16 +1631,17 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
uint16_t block2_size = 0;
#endif /* OC_BLOCK_WISE */
if (success && method == OC_GET &&
response_buffer.code < oc_status_code(OC_STATUS_BAD_REQUEST)) {
response_buffer.code < oc_status_code_unsafe(OC_STATUS_BAD_REQUEST)) {
observe = ri_handle_observation(ctx->request, ctx->response, cur_resource,
resource_is_collection, block2_size,
endpoint, iface_query);
}
#endif /* OC_SERVER */

if (request_obj.origin && (request_obj.origin->flags & MULTICAST) &&
response_buffer.code >= oc_status_code(OC_STATUS_BAD_REQUEST)) {
response_buffer.code = OC_IGNORE;
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 = CLEAR_TRANSACTION;
}

ri_invoke_coap_entity_set_response_ctx_t resp_ctx = {
Expand Down
5 changes: 4 additions & 1 deletion api/oc_ri_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef struct oc_response_buffer_s
uint8_t *buffer;
size_t buffer_size;
size_t response_length;
int code;
coap_status_t code;
oc_content_format_t content_format;
#ifdef OC_HAS_FEATURE_ETAG
oc_coap_etag_t etag;
Expand All @@ -89,6 +89,9 @@ void oc_ri_init(void);
*/
void oc_ri_shutdown(void);

/** Unchecked conversion from a non-error oc_status_t to coap_status_t */
coap_status_t oc_status_code_unsafe(oc_status_t key);

#ifdef OC_HAS_FEATURE_ETAG

/** @brief Get ETag for given resource */
Expand Down
Loading

0 comments on commit 6d72bf4

Please sign in to comment.