Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incremental updates #536

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/oc_blockwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ oc_blockwise_state_t *
oc_blockwise_alloc_response_buffer(const char *href, size_t href_len,
const oc_endpoint_t *endpoint,
oc_method_t method, oc_blockwise_role_t role,
uint32_t buffer_size, bool generate_etag)
uint32_t buffer_size, coap_status_t code,
bool generate_etag)
{
oc_blockwise_response_state_t *buffer =
(oc_blockwise_response_state_t *)blockwise_init_buffer(
Expand All @@ -194,6 +195,7 @@ oc_blockwise_alloc_response_buffer(const char *href, size_t href_len,
OC_ERR("cannot allocate block-wise response buffer");
return NULL;
}
buffer->code = code;
if (generate_etag) {
oc_random_buffer(buffer->etag.value, sizeof(buffer->etag.value));
buffer->etag.length = sizeof(buffer->etag.value);
Expand Down
4 changes: 3 additions & 1 deletion api/oc_blockwise_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef struct oc_blockwise_request_state_s
typedef struct oc_blockwise_response_state_s
{
oc_blockwise_state_t base;
coap_status_t code;
oc_coap_etag_t etag;

#ifdef OC_CLIENT
Expand Down Expand Up @@ -119,13 +120,14 @@ oc_blockwise_state_t *oc_blockwise_alloc_request_buffer(
* @param method method
* @param role the role (client or server)
* @param buffer_size the buffer size for allocation
* @param code the response code
* @param generate_etag generate ETag
* @return oc_blockwise_state_t*
*/
oc_blockwise_state_t *oc_blockwise_alloc_response_buffer(
const char *href, size_t href_len, const oc_endpoint_t *endpoint,
oc_method_t method, oc_blockwise_role_t role, uint32_t buffer_size,
bool generate_etag) OC_NONNULL(3);
coap_status_t code, bool generate_etag) OC_NONNULL(3);

/**
* @brief free the request buffer
Expand Down
92 changes: 38 additions & 54 deletions api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ dispatch_coap_request(void)

static bool
prepare_coap_request(oc_client_cb_t *cb, coap_configure_request_fn_t configure,
void *configure_data)
const void *configure_data)
{
coap_message_type_t type = COAP_TYPE_NON;

Expand Down Expand Up @@ -378,7 +378,7 @@ oc_do_request(oc_method_t method, const char *uri,
uint16_t timeout_seconds, oc_response_handler_t handler,
oc_qos_t qos, void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data)
const void *configure_request_data)
{
assert(uri != NULL);
assert(handler != NULL);
Expand All @@ -394,15 +394,13 @@ oc_do_request(oc_method_t method, const char *uri,
return NULL;
}

bool status =
prepare_coap_request(cb, configure_request, configure_request_data);
if (status) {
status = dispatch_coap_request();
}
if (!status) {
if (!prepare_coap_request(cb, configure_request, configure_request_data)) {
oc_client_cb_free(cb);
return NULL;
}
if (!dispatch_coap_request()) {
return NULL;
}
if (timeout_seconds > 0) {
oc_set_delayed_callback(cb, oc_client_cb_remove_with_notify_timeout_async,
timeout_seconds);
Expand Down Expand Up @@ -452,7 +450,7 @@ oc_init_async_request(oc_method_t method, const char *uri,
oc_response_handler_t handler, oc_qos_t qos,
void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data)
const void *configure_request_data)
{
assert(uri != NULL);
assert(handler != NULL);
Expand Down Expand Up @@ -484,8 +482,7 @@ oc_do_async_request_with_timeout(uint16_t timeout_seconds, oc_method_t method)
return false;
}

bool dispatch = dispatch_coap_request();
if (!dispatch) {
if (!dispatch_coap_request()) {
return false;
}

Expand Down Expand Up @@ -639,9 +636,13 @@ oc_do_ipv4_discovery(const char *query, oc_client_handler_t handler,
if (cb == NULL) {
return NULL;
}
if (!prepare_coap_request(cb, NULL, NULL)) {
oc_client_cb_free(cb);
return NULL;
}
cb->discovery = true;
if (prepare_coap_request(cb, NULL, NULL)) {
dispatch_coap_request();
if (!dispatch_coap_request()) {
return NULL;
}
return cb;
}
Expand All @@ -665,19 +666,15 @@ oc_do_ipv4_multicast(const char *uri, const char *query,
return NULL;
}

cb->multicast = true;

bool status = prepare_coap_request(cb, NULL, NULL);

if (status) {
status = dispatch_coap_request();
if (!prepare_coap_request(cb, NULL, NULL)) {
oc_client_cb_free(cb);
return NULL;
}

if (status) {
return cb;
cb->multicast = true;
if (!dispatch_coap_request()) {
return NULL;
}

return NULL;
return cb;
}
#endif /* OC_IPV4 */

Expand Down Expand Up @@ -709,25 +706,20 @@ multi_scope_ipv6_multicast(const oc_client_cb_t *cb4, uint8_t scope,

oc_client_cb_t *cb = oc_ri_alloc_client_cb(
uri, &mcast, OC_GET, query, client_handler, LOW_QOS, user_data);
if (cb == NULL) {
return false;
}
if (cb4 != NULL) {
cb->mid = cb4->mid;
memcpy(cb->token, cb4->token, cb4->token_len);
}

if (cb) {
if (cb4) {
cb->mid = cb4->mid;
memcpy(cb->token, cb4->token, cb4->token_len);
}
cb->multicast = true;
if (prepare_coap_request(cb, NULL, NULL) && dispatch_coap_request()) {
return true;
}

if (g_dispatch.transaction) {
coap_clear_transaction(g_dispatch.transaction);
g_dispatch.transaction = NULL;
}
if (!prepare_coap_request(cb, NULL, NULL)) {
oc_client_cb_free(cb);
g_dispatch.client_cb = NULL;
return false;
}
return false;
cb->multicast = true;
return dispatch_coap_request();
}

bool
Expand Down Expand Up @@ -775,28 +767,20 @@ dispatch_ip_discovery(const oc_client_cb_t *cb4, const char *query,

oc_client_cb_t *cb = oc_ri_alloc_client_cb(OCF_RES_URI, endpoint, OC_GET,
query, handler, qos, user_data);

if (cb == NULL) {
return false;
}
cb->discovery = true;
if (cb4) {
if (cb4 != NULL) {
cb->mid = cb4->mid;
memcpy(cb->token, cb4->token, cb4->token_len);
}

if (prepare_coap_request(cb, NULL, NULL) && dispatch_coap_request()) {
return true;
}

if (g_dispatch.transaction) {
coap_clear_transaction(g_dispatch.transaction);
g_dispatch.transaction = NULL;
if (!prepare_coap_request(cb, NULL, NULL)) {
oc_client_cb_free(cb);
return false;
}

oc_client_cb_free(cb);
g_dispatch.client_cb = NULL;
return false;
cb->discovery = true;
return dispatch_coap_request();
}

static bool
Expand Down
16 changes: 7 additions & 9 deletions api/oc_client_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@ extern "C" {
#endif

/** @brief Callback function to customize the coap request */
typedef void (*coap_configure_request_fn_t)(coap_packet_t *, void *);
typedef void (*coap_configure_request_fn_t)(coap_packet_t *, const void *);

/** @brief Prepare and dispatch an OC_GET or an OC_DELETE request */
oc_client_cb_t *oc_do_request(oc_method_t method, const char *uri,
const oc_endpoint_t *endpoint, const char *query,
uint16_t timeout_seconds,
oc_response_handler_t handler, oc_qos_t qos,
void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data) OC_NONNULL(2, 3);
oc_client_cb_t *oc_do_request(
oc_method_t method, const char *uri, const oc_endpoint_t *endpoint,
const char *query, uint16_t timeout_seconds, oc_response_handler_t handler,
oc_qos_t qos, void *user_data, coap_configure_request_fn_t configure_request,
const void *configure_request_data) OC_NONNULL(2, 3);

/** @brief Prepare an OC_POST or an OC_PUT request */
bool oc_init_async_request(oc_method_t method, const char *uri,
const oc_endpoint_t *endpoint, const char *query,
oc_response_handler_t handler, oc_qos_t qos,
void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data) OC_NONNULL(2, 3);
const void *configure_request_data) OC_NONNULL(2, 3);

#ifdef __cplusplus
}
Expand Down
39 changes: 28 additions & 11 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,19 @@ oc_collections_free_all(void)
}

static void
collection_notify_resource_changed(oc_collection_t *collection)
collection_notify_resource_changed(oc_collection_t *collection,
bool batchDispatch)
{
#ifdef OC_HAS_FEATURE_ETAG
oc_resource_update_etag(&collection->res);
#endif /* OC_HAS_FEATURE_ETAG */
oc_reset_delayed_callback(collection, collection_notify_links_list_async, 0);
#if defined(OC_RES_BATCH_SUPPORT) && defined(OC_DISCOVERY_RESOURCE_OBSERVABLE)
coap_add_discovery_batch_observer(&collection->res, /*removed*/ false,
/*dispatch*/ true);
batchDispatch);
#else /* !OC_RES_BATCH_SUPPORT || !OC_DISCOVERY_RESOURCE_OBSERVABLE */
(void)batchDispatch;
#endif /* OC_RES_BATCH_SUPPORT && OC_DISCOVERY_RESOURCE_OBSERVABLE */
#ifdef OC_HAS_FEATURE_ETAG
oc_resource_update_etag(&collection->res);
#endif /* OC_HAS_FEATURE_ETAG */
}

void
Expand Down Expand Up @@ -210,18 +213,32 @@ oc_collection_add_link(oc_resource_t *collection, oc_link_t *link)
if (link->resource == collection) {
oc_string_array_add_item(link->rel, "self");
}
collection_notify_resource_changed(col);
collection_notify_resource_changed(col, true);
}

void
oc_collection_remove_link(oc_resource_t *collection, const oc_link_t *link)
bool
oc_collection_remove_link_and_notify(oc_resource_t *collection,
const oc_link_t *link, bool notify,
bool batchDispatch)
{
if (collection == NULL || link == NULL) {
return;
return false;
}
oc_collection_t *col = (oc_collection_t *)collection;
oc_list_remove(col->links, link);
collection_notify_resource_changed(col);
if (oc_list_remove2(col->links, link) == NULL) {
return false;
}
if (notify) {
collection_notify_resource_changed(col, batchDispatch);
}
return true;
}

void
oc_collection_remove_link(oc_resource_t *collection, const oc_link_t *link)
{
oc_collection_remove_link_and_notify(collection, link, /*notify*/ true,
/*batchDispatch*/ true);
}

oc_link_t *
Expand Down
14 changes: 14 additions & 0 deletions api/oc_collection_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ bool oc_handle_collection_request(oc_method_t method, oc_request_t *request,
const oc_resource_t *notify_resource)
OC_NONNULL(2);

/**
* @brief Remove link from a collection and notify observers.
*
* @param collection the collection to remove the link from
* @param link the link to remove
* @param notify whether to notify observers
* @param batchDispatch whether to schedule dispatch of batch notifications
* @return true link was removed
* @return false link was not removed
*/
bool oc_collection_remove_link_and_notify(oc_resource_t *collection,
const oc_link_t *link, bool notify,
bool batchDispatch);

#ifdef OC_COLLECTIONS_IF_CREATE

/** @brief Free all resource type factories and resources that have been created
Expand Down
Loading