Skip to content

Commit

Permalink
fixup! Do not send empty notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Nov 24, 2023
1 parent 3a4d91d commit 6cd4e54
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 19 deletions.
13 changes: 5 additions & 8 deletions api/cloud/oc_cloud_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void gen_cloud_tag(const char *name, size_t device, char *cloud_tag);
int
cloud_store_load(oc_cloud_store_t *store)
{
char cloud_tag[CLOUD_TAG_MAX];
char cloud_tag[CLOUD_TAG_MAX] = { 0 };
gen_cloud_tag(CLOUD_STORE_NAME, store->device, cloud_tag);
return cloud_store_load_internal(cloud_tag, store);
}
Expand Down Expand Up @@ -124,17 +124,14 @@ cloud_store_encode(const oc_cloud_store_t *store)
static long
cloud_store_dump_internal(const char *store_name, const oc_cloud_store_t *store)
{
if (!store_name || !store) {
return -1;
}

#ifdef OC_DYNAMIC_ALLOCATION
uint8_t *buf = malloc(OC_MIN_APP_DATA_SIZE);
if (!buf)
if (buf == NULL) {
return -1;
}
oc_rep_new_realloc_v1(&buf, OC_MIN_APP_DATA_SIZE, OC_MAX_APP_DATA_SIZE);
#else /* OC_DYNAMIC_ALLOCATION */
uint8_t buf[OC_MIN_APP_DATA_SIZE];
uint8_t buf[OC_MIN_APP_DATA_SIZE] = { 0 };
oc_rep_new_v1(buf, sizeof(buf));
#endif /* !OC_DYNAMIC_ALLOCATION */

Expand All @@ -158,7 +155,7 @@ cloud_store_dump_internal(const char *store_name, const oc_cloud_store_t *store)
long
cloud_store_dump(const oc_cloud_store_t *store)
{
char cloud_tag[CLOUD_TAG_MAX];
char cloud_tag[CLOUD_TAG_MAX] = { 0 };
gen_cloud_tag(CLOUD_STORE_NAME, store->device, cloud_tag);
// Calling dump for cloud and access point info
return cloud_store_dump_internal(cloud_tag, store);
Expand Down
10 changes: 5 additions & 5 deletions api/cloud/oc_cloud_store_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {
* @return 0 on success
* @return <0 on failure
*/
int cloud_store_load(oc_cloud_store_t *store) OC_NONNULL(1);
int cloud_store_load(oc_cloud_store_t *store) OC_NONNULL();

/**
* @brief Save store data to storage
Expand All @@ -44,7 +44,7 @@ int cloud_store_load(oc_cloud_store_t *store) OC_NONNULL(1);
* @return >=0 amount of bytes written to storage
* @return <0 on failure
*/
long cloud_store_dump(const oc_cloud_store_t *store);
long cloud_store_dump(const oc_cloud_store_t *store) OC_NONNULL();

/**
* @brief Schedule delayed saving of store data to storage
Expand All @@ -54,21 +54,21 @@ long cloud_store_dump(const oc_cloud_store_t *store);
* @warning You must ensure that the store pointer is still valid in the delayed
* execution
*/
void cloud_store_dump_async(const oc_cloud_store_t *store);
void cloud_store_dump_async(const oc_cloud_store_t *store) OC_NONNULL();

/**
* @brief Set store data to default values
*
* @param store store
*/
void cloud_store_initialize(oc_cloud_store_t *store);
void cloud_store_initialize(oc_cloud_store_t *store) OC_NONNULL();

/**
* @brief Deallocate allocated data
*
* @param store store
*/
void cloud_store_deinitialize(oc_cloud_store_t *store);
void cloud_store_deinitialize(oc_cloud_store_t *store) OC_NONNULL();

#ifdef __cplusplus
}
Expand Down
7 changes: 7 additions & 0 deletions api/unittest/repencodecrctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ calculateCrc(uint64_t crc, CborType type, const uint8_t *buffer, size_t size)
return oc_crc64(crc, buffer, size);
}

TEST_F(TestCrcRepEncodeWithRealloc, PayloadIsEmptyObject_F)
{
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
OC_REP_CRC_ENCODER, oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));
}

TEST_F(TestCrcRepEncodeWithRealloc, EncodeNull)
{
SetRepBuffer(1, 0);
Expand Down
40 changes: 39 additions & 1 deletion api/unittest/repjsontest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ TEST_F(TestJsonRepWithPool, OCRepSetGetEmpty)
oc_rep_begin_root_object();
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());

auto rep = ParsePayload();
EXPECT_EQ(nullptr, rep.get());

Expand All @@ -144,6 +143,45 @@ TEST_F(TestJsonRepWithPool, OCRepSetGetEmpty)
EXPECT_EQ(OC_REP_ARRAY, rep->type);
}

TEST_F(TestJsonRepWithPool, OCRepIsEmptyObject)
{
oc_rep_start_root_object();
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_TRUE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));
}

TEST_F(TestJsonRepWithPool, OCRepIsEmptyObject_F)
{
// "[]"
oc_rep_begin_links_array();
oc_rep_end_links_array();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));

ClearPool();
// "{a: 123}"
oc_rep_start_root_object();
oc_rep_set_int(root, a, 123);
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));

ClearPool();
// "{"
oc_rep_start_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));
}

TEST_F(TestJsonRepWithPool, OCRepSetGetNull)
{
/* add null value to root object */
Expand Down
40 changes: 39 additions & 1 deletion api/unittest/reptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ TEST_F(TestRepWithPool, OCRepSetGetEmpty)
oc_rep_begin_root_object();
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());

auto rep = ParsePayload();
EXPECT_EQ(nullptr, rep.get());

Expand All @@ -210,6 +209,45 @@ TEST_F(TestRepWithPool, OCRepSetGetEmpty)
EXPECT_EQ(OC_REP_ARRAY, rep->type);
}

TEST_F(TestRepWithPool, OCRepIsEmptyObject)
{
oc_rep_start_root_object();
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_TRUE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));
}

TEST_F(TestRepWithPool, OCRepIsEmptyObject_F)
{
// "[]"
oc_rep_begin_links_array();
oc_rep_end_links_array();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));

ClearPool();
// "{a: 123}"
oc_rep_start_root_object();
oc_rep_set_int(root, a, 123);
oc_rep_end_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));

ClearPool();
// "{"
oc_rep_start_root_object();
ASSERT_EQ(CborNoError, oc_rep_get_cbor_errno());
EXPECT_FALSE(oc_rep_encoded_payload_is_empty_object(
oc_rep_encoder_get_type(), oc_rep_get_encoder_buf(),
oc_rep_get_encoded_payload_size()));
}

TEST_F(TestRepWithPool, OCRepSetGetNull)
{
/* add null value to root object */
Expand Down
6 changes: 2 additions & 4 deletions security/oc_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,11 @@ sec_load_unique_ids_from_rep(oc_rep_t *rep, size_t device)
oc_device_info_t *device_info = oc_core_get_device_info(device);
for (; rep != NULL; rep = rep->next) {
if (rep->type == OC_REP_STRING) {
if (oc_string_len(rep->name) == 2 &&
memcmp(oc_string(rep->name), "pi", 2) == 0) {
if (oc_rep_is_property(rep, "pi", OC_CHAR_ARRAY_LEN("pi"))) {
oc_str_to_uuid(oc_string(rep->value.string), &platform_info->pi);
continue;
}
if (oc_string_len(rep->name) == 4 &&
memcmp(oc_string(rep->name), "piid", 4) == 0) {
if (oc_rep_is_property(rep, "piid", OC_CHAR_ARRAY_LEN("piid"))) {
oc_str_to_uuid(oc_string(rep->value.string), &device_info->piid);
continue;
}
Expand Down

0 comments on commit 6cd4e54

Please sign in to comment.