Skip to content

Commit

Permalink
net: lib: coap: method_from_code() API change to return errors.
Browse files Browse the repository at this point in the history
method_from_code() signature has changed to return error and
the method. In case of an invalid code it returns -EINVAL and
causes coap_handle_request() to return -ENOTSUP.

Fixes: #49498

Signed-off-by: Sagar Shah <sagar.shah@legrand.us>
  • Loading branch information
sagarshah-leg authored and fabiobaltieri committed Sep 12, 2022
1 parent 1357be3 commit dfd8970
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
5 changes: 4 additions & 1 deletion include/zephyr/net/coap.h
Expand Up @@ -514,7 +514,10 @@ int coap_packet_append_payload(struct coap_packet *cpkt, const uint8_t *payload,
* @param addr Peer address
* @param addr_len Peer address length
*
* @return 0 in case of success or negative in case of error.
* @retval 0 in case of success.
* @retval -ENOTSUP in case of invalid request code.
* @retval -EPERM in case resource handler is not implemented.
* @retval -ENOENT in case the resource is not found.
*/
int coap_handle_request(struct coap_packet *cpkt,
struct coap_resource *resources,
Expand Down
32 changes: 21 additions & 11 deletions subsys/net/lib/coap/coap.c
Expand Up @@ -819,26 +819,33 @@ static bool uri_path_eq(const struct coap_packet *cpkt,
return true;
}

static coap_method_t method_from_code(const struct coap_resource *resource,
uint8_t code)
static int method_from_code(const struct coap_resource *resource,
uint8_t code, coap_method_t *method)
{
switch (code) {
case COAP_METHOD_GET:
return resource->get;
*method = resource->get;
return 0;
case COAP_METHOD_POST:
return resource->post;
*method = resource->post;
return 0;
case COAP_METHOD_PUT:
return resource->put;
*method = resource->put;
return 0;
case COAP_METHOD_DELETE:
return resource->del;
*method = resource->del;
return 0;
case COAP_METHOD_FETCH:
return resource->fetch;
*method = resource->fetch;
return 0;
case COAP_METHOD_PATCH:
return resource->patch;
*method = resource->patch;
return 0;
case COAP_METHOD_IPATCH:
return resource->ipatch;
*method = resource->ipatch;
return 0;
default:
return NULL;
return -EINVAL;
}
}

Expand Down Expand Up @@ -871,7 +878,10 @@ int coap_handle_request(struct coap_packet *cpkt,
}

code = coap_header_get_code(cpkt);
method = method_from_code(resource, code);
if (method_from_code(resource, code, &method) < 0) {
return -ENOTSUP;
}

if (!method) {
return -EPERM;
}
Expand Down

0 comments on commit dfd8970

Please sign in to comment.