Skip to content

Commit

Permalink
TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jul 24, 2023
1 parent 95d52c2 commit c7a53e8
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 75 deletions.
34 changes: 27 additions & 7 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,46 @@ collection_free_resource_types(oc_list_t list)
}
}

void
oc_collection_free(oc_collection_t *collection)
static void
collection_free(oc_collection_t *collection, bool notify)
{
if (collection == NULL) {
return;
}
oc_list_remove(g_collections, collection);
oc_ri_free_resource_properties((oc_resource_t *)collection);
bool removed = oc_list_remove2(g_collections, collection) != NULL;

oc_link_t *link;
while ((link = (oc_link_t *)oc_list_pop(collection->links)) != NULL) {
oc_delete_link(link);
}

if (notify && removed) {
oc_notify_resource_removed(&collection->res);
}

oc_ri_free_resource_properties(&collection->res);
collection_free_resource_types(collection->supported_rts);
collection_free_resource_types(collection->mandatory_rts);

oc_memb_free(&g_collections_s, collection);
}

void
oc_collection_free(oc_collection_t *collection)
{
if (collection == NULL) {
return;
}
collection_free(collection, true);
}

void
oc_collections_free_all(void)
{
oc_collection_t *collection = (oc_collection_t *)oc_list_pop(g_collections);
while (collection != NULL) {
collection_free(collection, false);
collection = (oc_collection_t *)oc_list_pop(g_collections);
}
}

static oc_event_callback_retval_t
batch_notify_collection(void *data)
{
Expand Down
3 changes: 3 additions & 0 deletions api/oc_collection_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ bool oc_collection_add(oc_collection_t *collection) OC_NONNULL();
/** @brief Get head of the global list of collections */
oc_collection_t *oc_collection_get_all(void);

/** @brief Free all collections from the global list */
void oc_collections_free_all(void);

/** @brief Iterate the global list of colletions and return the next collection
* linked with the given resource */
oc_collection_t *oc_get_next_collection_with_link(const oc_resource_t *resource,
Expand Down
1 change: 1 addition & 0 deletions api/oc_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ process_batch_response(CborEncoder *links_array, oc_resource_t *resource,
href[6 + OC_UUID_LEN - 1 + oc_string_len(resource->uri)] = '\0';

oc_rep_set_text_string(links, href, href);
// TODO: add etag prop here
oc_rep_set_key(oc_rep_object(links), "rep");
memcpy(oc_rep_get_encoder(), &links_map, sizeof(CborEncoder));

Expand Down
75 changes: 32 additions & 43 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,9 @@ ri_delete_resource(oc_resource_t *resource, bool notify)
{
OC_DBG("delete resource(%p)", (void *)resource);

oc_list_remove(g_app_resources, resource);
oc_list_remove(g_app_resources_to_be_deleted, resource);
bool removed = oc_list_remove2(g_app_resources, resource) != NULL;
removed =
oc_list_remove2(g_app_resources_to_be_deleted, resource) != NULL || removed;

oc_remove_delayed_callback(resource, oc_delayed_delete_resource_cb);
oc_notify_clear(resource);
Expand Down Expand Up @@ -704,18 +705,8 @@ ri_delete_resource(oc_resource_t *resource, bool notify)
#endif /* !OC_DBG_IS_ENABLED */
}

if (notify) {
#if defined(OC_RES_BATCH_SUPPORT) && defined(OC_DISCOVERY_RESOURCE_OBSERVABLE)
coap_remove_discovery_batch_observers_by_resource(resource);
#endif

#ifdef OC_DISCOVERY_RESOURCE_OBSERVABLE
oc_resource_t *discovery =
oc_core_get_resource_by_index(OCF_RES, resource->device);
if (discovery != NULL) {
oc_notify_resource_changed_delayed_ms(discovery, 0);
}
#endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE */
if (removed && notify) {
oc_notify_resource_removed(resource);
}

oc_ri_free_resource_properties(resource);
Expand Down Expand Up @@ -767,6 +758,7 @@ oc_ri_add_resource(oc_resource_t *resource)
}

oc_list_add(g_app_resources, resource);
oc_notify_resource_added(resource);
return true;
}

Expand Down Expand Up @@ -1207,34 +1199,36 @@ ri_invoke_coap_entity_set_response(coap_packet_t *response,
}

#ifdef OC_SERVER
if (ctx.resource != NULL) {
#ifdef OC_HAS_FEATURE_ETAG
if (ctx.resource != NULL && ctx.method == OC_GET) {
coap_options_set_etag(response, (uint8_t *)&ctx.resource->etag,
sizeof(ctx.resource->etag));
}
if (ctx.method == OC_GET) {
coap_options_set_etag(response, (uint8_t *)&ctx.resource->etag,
sizeof(ctx.resource->etag));
}
#endif /* OC_HAS_FEATURE_ETAG */

/* If the recently handled request was a PUT/POST, it conceivably
* altered the resource state, so attempt to notify all observers
* of that resource with the change.
*/
if (
/* If the recently handled request was a PUT/POST, it conceivably
* altered the resource state, so attempt to notify all observers
* of that resource with the change.
*/
if (
#ifdef OC_COLLECTIONS
!ctx.resource_is_collection &&
!ctx.resource_is_collection &&
#endif /* OC_COLLECTIONS */
ctx.resource && (ctx.method == OC_PUT || ctx.method == OC_POST) &&
response_buffer->code < oc_status_code(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 =
oc_ri_alloc_resource_defaults();
resource_defaults_data->resource = ctx.resource;
resource_defaults_data->iface_mask = ctx.iface_mask;
oc_ri_add_timed_event_callback_ticks(
resource_defaults_data,
&oc_observe_notification_resource_defaults_delayed, 0);
} else {
oc_notify_resource_changed_delayed_ms(ctx.resource, 0);
(ctx.method == OC_PUT || ctx.method == OC_POST) &&
response_buffer->code < oc_status_code(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 =
oc_ri_alloc_resource_defaults();
resource_defaults_data->resource = ctx.resource;
resource_defaults_data->iface_mask = ctx.iface_mask;
oc_ri_add_timed_event_callback_ticks(
resource_defaults_data,
&oc_observe_notification_resource_defaults_delayed, 0);
} else {
oc_notify_resource_changed_delayed_ms(ctx.resource, 0);
}
}
}

Expand Down Expand Up @@ -1703,12 +1697,7 @@ oc_ri_shutdown(void)
oc_ri_on_delete_resource_remove_all();

#ifdef OC_COLLECTIONS
oc_collection_t *collection = oc_collection_get_all();
while (collection != NULL) {
oc_collection_t *next = (oc_collection_t *)collection->res.next;
oc_collection_free(collection);
collection = next;
}
oc_collections_free_all();
#endif /* OC_COLLECTIONS */

ri_delete_all_app_resources();
Expand Down
79 changes: 60 additions & 19 deletions api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "api/oc_buffer_internal.h"
#include "api/oc_etag_internal.h"
#include "api/oc_main_internal.h"
#include "api/oc_ri_internal.h"
#include "messaging/coap/coap_options.h"
#include "messaging/coap/engine.h"
Expand Down Expand Up @@ -483,16 +484,6 @@ oc_populate_resource_object(oc_resource_t *resource, const char *name,
#ifdef OC_HAS_FEATURE_ETAG
resource->etag = oc_etag_get();
#endif /* OC_HAS_FEATURE_ETAG */

#if defined(OC_RES_BATCH_SUPPORT) && defined(OC_DISCOVERY_RESOURCE_OBSERVABLE)
coap_notify_discovery_batch_observers(resource);
#endif /* OC_RES_BATCH_SUPPORT && OC_DISCOVERY_RESOURCE_OBSERVABLE */
#ifdef OC_DISCOVERY_RESOURCE_OBSERVABLE
oc_resource_t *discovery = oc_core_get_resource_by_index(OCF_RES, device);
if (discovery != NULL) {
oc_notify_resource_changed_delayed_ms(discovery, 0);
}
#endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE */
}

oc_resource_t *
Expand Down Expand Up @@ -543,6 +534,7 @@ oc_add_collection_v1(oc_resource_t *collection)
return false;
}
oc_resource_set_observable(collection, true);
oc_notify_resource_added(collection);
return true;
}

Expand Down Expand Up @@ -834,6 +826,9 @@ int
oc_notify_observers(oc_resource_t *resource)
{
assert(resource != NULL);
if (!oc_main_initialized()) {
return 0;
}
return coap_notify_observers(resource, NULL, NULL);
}

Expand Down Expand Up @@ -872,16 +867,12 @@ oc_notify_observers_delayed_ms(oc_resource_t *resource, uint16_t milliseconds)
oc_notify_observers_delayed_ticks(resource, ticks);
}

void
oc_notify_clear(const oc_resource_t *resource)
{
assert(resource != NULL);
oc_remove_delayed_callback(resource, &notify_observers_async);
}

void
oc_notify_resource_changed(oc_resource_t *resource)
{
if (!oc_main_initialized()) {
return;
}
assert(resource != NULL);
oc_notify_observers(resource);
#ifdef OC_HAS_FEATURE_ETAG
Expand All @@ -890,7 +881,7 @@ oc_notify_resource_changed(oc_resource_t *resource)
}

static oc_event_callback_retval_t
oc_notify_resource_async(void *data)
notify_resource_changed_async(void *data)
{
oc_notify_resource_changed((oc_resource_t *)data);
return OC_EVENT_DONE;
Expand All @@ -901,8 +892,58 @@ oc_notify_resource_changed_delayed_ms(oc_resource_t *resource,
uint64_t milliseconds)
{
assert(resource != NULL);
oc_reset_delayed_callback_ms(resource, &oc_notify_resource_async,
oc_reset_delayed_callback_ms(resource, &notify_resource_changed_async,
milliseconds);
}

void
oc_notify_resource_added(oc_resource_t *resource)
{
#ifdef OC_DISCOVERY_RESOURCE_OBSERVABLE
if (!oc_main_initialized()) {
return;
}
#ifdef OC_RES_BATCH_SUPPORT
coap_notify_discovery_batch_observers(resource);
#endif /* OC_RES_BATCH_SUPPORT */
oc_resource_t *discovery =
oc_core_get_resource_by_index(OCF_RES, resource->device);
if (discovery != NULL) {
oc_notify_resource_changed_delayed_ms(discovery, 0);
}
#else /* !OC_DISCOVERY_RESOURCE_OBSERVABLE */
(void)resource;
#endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE */
}

void
oc_notify_resource_removed(oc_resource_t *resource)
{
#ifdef OC_DISCOVERY_RESOURCE_OBSERVABLE
if (!oc_main_initialized()) {
return;
}

#ifdef OC_RES_BATCH_SUPPORT
coap_remove_discovery_batch_observers_by_resource(resource);
#endif /* OC_RES_BATCH_SUPPORT */

oc_resource_t *discovery =
oc_core_get_resource_by_index(OCF_RES, resource->device);
if (discovery != NULL) {
oc_notify_resource_changed_delayed_ms(discovery, 0);
}
#else /* !OC_DISCOVERY_RESOURCE_OBSERVABLE */
(void)resource;
#endif /* OC_DISCOVERY_RESOURCE_OBSERVABLE */
}

void
oc_notify_clear(const oc_resource_t *resource)
{
assert(resource != NULL);
oc_remove_delayed_callback(resource, &notify_resource_changed_async);
oc_remove_delayed_callback(resource, &notify_observers_async);
}

#endif /* OC_SERVER */
9 changes: 8 additions & 1 deletion api/oc_server_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ void oc_reset_delayed_callback_ms(void *cb_data, oc_trigger_t callback,
/// Remove scheduled notifications
void oc_notify_clear(const oc_resource_t *resource) OC_NONNULL();

/// Resource has been added, notify relevant modules
void oc_notify_resource_added(oc_resource_t *resource) OC_NONNULL();

/// Resource has been removed, notify relevant modules
void oc_notify_resource_removed(oc_resource_t *resource) OC_NONNULL();

/// Resource has been changed, notify relevant modules
void oc_notify_resource_changed_delayed_ms(oc_resource_t *resource,
uint64_t milliseconds);
uint64_t milliseconds) OC_NONNULL();

#ifdef OC_RES_BATCH_SUPPORT

Expand Down
5 changes: 0 additions & 5 deletions messaging/coap/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,6 @@ coap_notify_observers_internal(oc_resource_t *resource,
oc_response_buffer_t *response_buf,
const oc_endpoint_t *endpoint)
{
if (!resource) {
OC_WRN("coap_notify_observers_internal: no resource passed; returning");
return 0;
}

#ifdef OC_SECURITY
const oc_sec_pstat_t *ps = oc_sec_get_pstat(resource->device);
if (ps->s != OC_DOS_RFNOP) {
Expand Down

0 comments on commit c7a53e8

Please sign in to comment.