Skip to content

Commit

Permalink
Added user-defined callback for finding oscore context
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk Jarosz committed Feb 21, 2024
1 parent 7660069 commit 1ac0938
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/coap3/coap_net_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@
* @{
*/

/**
* Optional user callback to be used, when oscore_find_context can't find any context.
* Could be used to check external storage (e.g. FLASH).
*
* @param c_context The CoAP COntext to search.
* @param rcpkey_id The Recipient kid.
* @param ctxkey_id The ID Context to match (or NULL if no check).
* @param oscore_r2 Partial id_context to match against or NULL.
* @param recipient_ctx The recipient context to update.
*
* return The OSCORE context and @p recipient_ctx updated, or NULL is error.
*/
typedef oscore_ctx_t * (*external_oscore_find_context_handler_t)(const coap_context_t *c_context,
const coap_bin_const_t rcpkey_id,
const coap_bin_const_t *ctxkey_id,
uint8_t *oscore_r2,
oscore_recipient_ctx_t **recipient_ctx);

/**
* Queue entry
*/
Expand Down Expand Up @@ -93,6 +111,8 @@ struct coap_context_t {
#endif /* WITH_LWIP */
#if COAP_OSCORE_SUPPORT
struct oscore_ctx_t *p_osc_ctx; /**< primary oscore context */
external_oscore_find_context_handler_t external_oscore_find_context_handler;
/**< Called when oscore_find_context didn't find any context*/
#endif /* COAP_OSCORE_SUPPORT */

#if COAP_CLIENT_SUPPORT
Expand Down Expand Up @@ -430,6 +450,17 @@ coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu);
*/
int coap_client_delay_first(coap_session_t *session);

#if COAP_OSCORE_SUPPORT
/**
* Register optional user callback to be used, when oscore_find_context can't find any context.
* Callback could be used to check external storage (e.g. FLASH).
*
* @param ctx The current coap context to use.
* @param handler User callback.
*/
void coap_register_find_context_handler(coap_context_t *ctx, external_oscore_find_context_handler_t handler);
#endif

/** @} */

extern int coap_started;
Expand Down
9 changes: 9 additions & 0 deletions src/coap_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ coap_new_context(const coap_address_t *listen_addr) {
#endif /* COAP_EPOLL_SUPPORT || COAP_SERVER_SUPPORT */
}

#if COAP_OSCORE_SUPPORT
void
coap_register_find_context_handler(coap_context_t *ctx, external_oscore_find_context_handler_t handler)
{
assert(ctx);
ctx->external_oscore_find_context_handler = handler;
}
#endif

void
coap_set_app_data(coap_context_t *ctx, void *app_data) {
assert(ctx);
Expand Down
7 changes: 7 additions & 0 deletions src/oscore/oscore_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ oscore_find_context(const coap_context_t *c_context,
} /* while rpt */
pt = pt->next;
} /* end while */

/* no context was found in libcoap RAM - call user function to also check external storage (e.g. FLASH) */
if (c_context->external_oscore_find_context_handler)
{
return c_context->external_oscore_find_context_handler(c_context, rcpkey_id, ctxkey_id, oscore_r2, recipient_ctx);
}

return NULL;
}

Expand Down

0 comments on commit 1ac0938

Please sign in to comment.