Skip to content

Commit

Permalink
fixup! Calcuate crc64 for a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 20, 2023
1 parent e276ae2 commit ec7bf9c
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 61 deletions.
3 changes: 2 additions & 1 deletion api/cloud/rd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ _add_resource_payload(CborEncoder *parent, oc_resource_t *resource,
oc_rep_start_object(parent, links);
oc_rep_set_text_string(links, href, oc_string(resource->uri));
oc_rep_set_string_array(links, rt, resource->types);
oc_core_encode_interfaces_mask(oc_rep_object(links), resource->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(links), resource->interfaces,
false);
if (rel)
oc_rep_set_text_string(links, rel, rel);
oc_rep_set_int(links, ins, ins);
Expand Down
8 changes: 5 additions & 3 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ oc_handle_collection_create_request(oc_method_t method, oc_request_t *request)
oc_string_len(new_res->resource->uri));
oc_rep_set_string_array(root, rt, new_res->resource->types);
oc_core_encode_interfaces_mask(oc_rep_object(root),
new_res->resource->interfaces);
new_res->resource->interfaces, false);
oc_rep_set_object(root, p);
oc_rep_set_uint(p, bm, (uint8_t)(bm & ~(OC_PERIODIC | OC_SECURE)));
oc_rep_close_object(root, p);
Expand Down Expand Up @@ -676,7 +676,8 @@ collection_encode_links(const oc_collection_t *collection,
oc_rep_set_text_string_v1(links, href, oc_string(link->resource->uri),
oc_string_len(link->resource->uri));
oc_rep_set_string_array(links, rt, link->resource->types);
oc_core_encode_interfaces_mask(oc_rep_object(links), link->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(links), link->interfaces,
false);
oc_rep_set_string_array(links, rel, link->rel);
oc_rep_set_int(links, ins, link->ins);
oc_link_params_t *p = (oc_link_params_t *)oc_list_head(link->params);
Expand Down Expand Up @@ -814,7 +815,8 @@ oc_handle_collection_linked_list_request(oc_request_t *request)
oc_rep_set_text_string_v1(links, href, oc_string(link->resource->uri),
oc_string_len(link->resource->uri));
oc_rep_set_string_array(links, rt, link->resource->types);
oc_core_encode_interfaces_mask(oc_rep_object(links), link->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(links), link->interfaces,
false);
oc_rep_set_string_array(links, rel, link->rel);
oc_rep_set_int(links, ins, link->ins);
oc_link_params_t *p = (oc_link_params_t *)oc_list_head(link->params);
Expand Down
7 changes: 5 additions & 2 deletions api/oc_core_res.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ oc_core_shutdown(void)
}

void
oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask)
oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask,
bool include_private)
{
oc_rep_set_key((parent), "if");
oc_rep_start_array((parent), if);
Expand Down Expand Up @@ -202,9 +203,11 @@ oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask)
oc_rep_add_text_string(if, OC_IF_STARTUP_REVERT_STR);
}
#ifdef OC_HAS_FEATURE_ETAG_INTERFACE
if ((iface_mask & PLGD_IF_ETAG) != 0) {
if (include_private && (iface_mask & PLGD_IF_ETAG) != 0) {
oc_rep_add_text_string(if, PLGD_IF_ETAG_STR);
}
#else /* OC_HAS_FEATURE_ETAG_INTERFACE */
(void)include_private;
#endif /* OC_HAS_FEATURE_ETAG_INTERFACE */
oc_rep_end_array((parent), if);
}
Expand Down
5 changes: 3 additions & 2 deletions api/oc_core_res_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ oc_device_info_t *oc_core_add_new_device(oc_add_new_device_t cfg);
*
* @param parent the cbor encoder (cannot be NULL)
* @param iface_mask the interfaces (as bit mask)
* @param include_private include private interfaces
*/
void oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask)
OC_NONNULL();
void oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask,
bool include_private) OC_NONNULL();

/**
* @brief store the uri as a string
Expand Down
35 changes: 17 additions & 18 deletions api/oc_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ process_resource(const oc_resource_t *resource, const oc_request_t *request,
oc_rep_close_array(link, rt);

// if
oc_core_encode_interfaces_mask(oc_rep_object(link), resource->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(link), resource->interfaces,
false);

// p
oc_rep_set_object(link, p);
Expand Down Expand Up @@ -496,7 +497,8 @@ process_oic_1_1_resource(oc_resource_t *resource, oc_request_t *request,
oc_rep_close_array(res, rt);

// if
oc_core_encode_interfaces_mask(oc_rep_object(res), resource->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(res), resource->interfaces,
false);

// p
oc_rep_set_object(res, p);
Expand Down Expand Up @@ -582,7 +584,8 @@ process_oic_1_1_device_object(CborEncoder *device, oc_request_t *request,
if (baseline) {
oc_resource_t *ocf_res = oc_core_get_resource_by_index(OCF_RES, device_num);
oc_rep_set_string_array(links, rt, ocf_res->types);
oc_core_encode_interfaces_mask(oc_rep_object(links), ocf_res->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(links), ocf_res->interfaces,
false);
}

oc_rep_set_array(links, links);
Expand Down Expand Up @@ -1115,31 +1118,27 @@ discovery_encode(oc_request_t *request, oc_interface_mask_t iface)
{
int matches;
size_t device = request->resource->device;
// oc_rep_begin_array(oc_rep_get_encoder(), root);
oc_rep_start_links_array();
oc_rep_start_object(oc_rep_array(links), props);
memcpy(&root_map, &props_map, sizeof(CborEncoder));
oc_process_baseline_interface(
oc_core_get_resource_by_index(OCF_RES, device));
oc_rep_begin_array(oc_rep_get_encoder(), root);
oc_rep_begin_object(oc_rep_array(root), props);
oc_process_baseline_interface_with_filter(
oc_rep_object(props), oc_core_get_resource_by_index(OCF_RES, device),
NULL, NULL);
#ifdef OC_SECURITY
const oc_sec_sdi_t *s = oc_sec_sdi_get(device);
if (!s->priv) {
char uuid[OC_UUID_LEN];
oc_uuid_to_str(&s->uuid, uuid, OC_UUID_LEN);
oc_rep_set_text_string_v1(root, sduuid, uuid,
oc_rep_set_text_string_v1(props, sduuid, uuid,
oc_strnlen(uuid, OC_ARRAY_SIZE(uuid)));
oc_rep_set_text_string_v1(root, sdname, oc_string(s->name),
oc_rep_set_text_string_v1(props, sdname, oc_string(s->name),
oc_string_len(s->name));
}
#endif
oc_rep_set_array(root, links);
oc_rep_open_array(props, links);
matches = process_device_resources(oc_rep_array(links), request);
oc_rep_close_array(root, links);
memcpy(&props_map, &root_map, sizeof(CborEncoder));
oc_rep_end_object(oc_rep_array(links), props);
oc_rep_end_links_array();

// oc_rep_end_array(oc_rep_get_encoder(), root);
oc_rep_close_array(props, links);
oc_rep_end_object(oc_rep_array(root), props);
oc_rep_end_array(oc_rep_get_encoder(), root);
return matches > 0 ? OC_STATUS_OK : OC_STATUS_NOT_MODIFIED;
} break;
default:
Expand Down
7 changes: 4 additions & 3 deletions api/oc_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,8 @@ push_update(oc_ns_t *ns_instance)
oc_rep_close_array(root, rt);

/* if */
oc_core_encode_interfaces_mask(oc_rep_object(root), src_rsc->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(root), src_rsc->interfaces,
false);

/* build rep object */
src_rsc->payload_builder();
Expand Down Expand Up @@ -2518,8 +2519,8 @@ OC_PROCESS_THREAD(oc_push_process, ev, data)
oc_rep_close_array(root, rt);

/* if */
oc_core_encode_interfaces_mask(oc_rep_object(root),
src_rsc->interfaces);
oc_core_encode_interfaces_mask(oc_rep_object(root), src_rsc->interfaces,
false);

src_rsc->payload_builder();

Expand Down
49 changes: 25 additions & 24 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,43 +729,44 @@ oc_ri_get_interface_mask(const char *iface, size_t iface_len)
strncmp(iface, OC_IF_STARTUP_REVERT_STR, iface_len) == 0) {
return OC_IF_STARTUP_REVERT;
}
#ifdef OC_HAS_FEATURE_ETAG_INTERFACE
if (OC_CHAR_ARRAY_LEN(PLGD_IF_ETAG_STR) == iface_len &&
strncmp(iface, PLGD_IF_ETAG_STR, iface_len) == 0) {
return PLGD_IF_ETAG;
}
#endif /* OC_HAS_FEATURE_ETAG_INTERFACE */
return 0;
}

static bool
does_interface_support_method(oc_interface_mask_t iface_mask,
oc_method_t method)
bool
oc_ri_interface_support_method(oc_interface_mask_t iface, oc_method_t method)
{
bool supported = true;
switch (iface_mask) {
/* Per section 7.5.3 of the OCF Core spec, the following three interfaces
* are RETRIEVE-only.
*/
/* Supported operations are defined in section 7.5.3 of the OCF Core spec */
switch (iface) {
/* The following interfaces are RETRIEVE-only: */
case OC_IF_LL:
case OC_IF_S:
case OC_IF_R:
if (method != OC_GET)
supported = false;
break;
/* Per section 7.5.3 of the OCF Core spec, the following three interfaces
* support RETRIEVE, UPDATE.
* TODO: Refine logic below after adding logic that identifies
* and handles CREATE requests using PUT/POST.
*/
#ifdef OC_HAS_FEATURE_ETAG_INTERFACE
case PLGD_IF_ETAG:
#endif
return method == OC_GET;
/* The following interafaces are UPDATE(WRITE)-only: */
case OC_IF_W:
return method == OC_PUT || method == OC_POST || method == OC_DELETE;
/* The CREATE interface is PUT/POST-only: */
case OC_IF_CREATE:
return method == OC_PUT || method == OC_POST;
/* The following interfaces support RETRIEVE and UPDATE: */
case OC_IF_RW:
case OC_IF_B:
case OC_IF_BASELINE:
case OC_IF_CREATE:
/* Per section 7.5.3 of the OCF Core spec, the following interface
* supports CREATE, RETRIEVE and UPDATE.
*/
case OC_IF_A:
case OC_IF_STARTUP:
case OC_IF_STARTUP_REVERT:
case OC_IF_W:
break;
return true;
}
return supported;
return false;
}

#ifdef OC_SECURITY
Expand Down Expand Up @@ -1413,7 +1414,7 @@ oc_ri_invoke_coap_entity_handler(coap_make_response_ctx_t *ctx,
* If not, return a 4.00 response.
*/
if (((iface_mask & ~cur_resource->interfaces) != 0) ||
!does_interface_support_method(iface_mask, method)) {
!oc_ri_interface_support_method(iface_mask, method)) {
forbidden = true;
bad_request = true;
#ifdef OC_SECURITY
Expand Down
6 changes: 5 additions & 1 deletion api/oc_ri_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ void oc_ri_init(void);
*/
void oc_ri_shutdown(void);

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

/** @brief Check if given methodi s supported by the interface */
bool oc_ri_interface_support_method(oc_interface_mask_t iface,
oc_method_t method);

#ifdef OC_HAS_FEATURE_ETAG

/** @brief Get ETag for given resource */
Expand Down
2 changes: 1 addition & 1 deletion api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ oc_process_baseline_interface_with_filter(
&resource->types);
}
if (filter == NULL || filter(OC_BASELINE_PROP_IF, filter_data)) {
oc_core_encode_interfaces_mask(object, resource->interfaces);
oc_core_encode_interfaces_mask(object, resource->interfaces, false);
}
if (filter == NULL || filter(OC_BASELINE_PROP_TAG_LOCN, filter_data)) {
resource_encode_tag_locn(object, resource->tag_locn);
Expand Down
17 changes: 12 additions & 5 deletions api/unittest/coreresourcetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ TEST_F(TestCoreResource, CoreGetResourceV1_P)
}

static void
encodeInterfaces(unsigned iface_mask, std::vector<std::string> iface_strs = {})
encodeInterfaces(unsigned iface_mask, std::vector<std::string> iface_strs = {},
bool includePrivate = false)
{
oc::RepPool pool{};

oc_rep_start_root_object();
EXPECT_EQ(CborNoError, oc_rep_get_cbor_errno());
oc_core_encode_interfaces_mask(oc_rep_object(root), iface_mask);
oc_core_encode_interfaces_mask(oc_rep_object(root), iface_mask,
includePrivate);
EXPECT_EQ(CborNoError, oc_rep_get_cbor_errno());
oc_rep_end_root_object();
EXPECT_EQ(CborNoError, oc_rep_get_cbor_errno());
Expand Down Expand Up @@ -158,7 +160,12 @@ encodeInterfaces(unsigned iface_mask, std::vector<std::string> iface_strs = {})

TEST_F(TestCoreResource, EncodeInterfaces_P)
{
encodeInterfaces(0);
bool includePrivateInterfaces = false;
#ifdef OC_HAS_FEATURE_ETAG_INTERFACE
includePrivateInterfaces = true;
#endif /* OC_HAS_FEATURE_ETAG_INTERFACE */

encodeInterfaces(0, {}, includePrivateInterfaces);

std::vector<unsigned> all_ifs{
OC_IF_BASELINE,
Expand Down Expand Up @@ -197,11 +204,11 @@ TEST_F(TestCoreResource, EncodeInterfaces_P)

unsigned all_ifs_mask = 0;
for (size_t i = 0; i < all_ifs.size(); ++i) {
encodeInterfaces(all_ifs[i], { all_ifstrs[i] });
encodeInterfaces(all_ifs[i], { all_ifstrs[i] }, includePrivateInterfaces);
all_ifs_mask |= all_ifs[i];
}

encodeInterfaces(all_ifs_mask, all_ifstrs);
encodeInterfaces(all_ifs_mask, all_ifstrs, includePrivateInterfaces);
}

class TestCoreResourceWithDevice : public testing::Test {
Expand Down
2 changes: 1 addition & 1 deletion util/oc_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

#if defined(OC_HAS_FEATURE_ETAG) && defined(OC_STORAGE)
#define OC_HAS_FEATURE_CRC_ENCODER
// #define OC_HAS_FEATURE_ETAG_INTERFACE
#define OC_HAS_FEATURE_ETAG_INTERFACE
#endif /* OC_HAS_FEATURE_ETAG && OC_STORAGE */

#endif /* OC_FEATURES_H */

0 comments on commit ec7bf9c

Please sign in to comment.