Skip to content

Commit

Permalink
oc_rep:API to get encoder buffer and payload size
Browse files Browse the repository at this point in the history
Change-Id: Ie2055874ea80bc777e3b84a87fb54366447b3d55
Signed-off-by: Kishen Maloor <[email protected]>
Reviewed-on: https://gerrit.iotivity.org/gerrit/27419
Tested-by: IoTivity Jenkins <[email protected]>
  • Loading branch information
kmaloor committed Nov 7, 2018
1 parent 25bd9d0 commit 86b10a3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ oc_event_callback_retval_t oc_ri_remove_client_cb(void *data);
static bool
dispatch_coap_request(void)
{
int payload_size = oc_rep_finalize();
int payload_size = oc_rep_get_encoded_payload_size();

if ((client_cb->method == OC_PUT || client_cb->method == OC_POST) &&
payload_size > 0) {
Expand Down
6 changes: 3 additions & 3 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
oc_rep_set_key(*oc_rep_object(links), "rep");
memcpy(&g_encoder, &links_map, sizeof(CborEncoder));

int size_before = oc_rep_finalize();
int size_before = oc_rep_get_encoded_payload_size();
rest_request.resource = link->resource;
response_buffer.code = 0;
response_buffer.response_length = 0;
Expand Down Expand Up @@ -389,7 +389,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
} else {
if (code < oc_status_code(OC_STATUS_BAD_REQUEST))
code = response_buffer.code;
int size_after = oc_rep_finalize();
int size_after = oc_rep_get_encoded_payload_size();
if (size_before == size_after) {
oc_rep_start_root_object();
oc_rep_end_root_object();
Expand Down Expand Up @@ -423,7 +423,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
break;
}

int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
size = (size <= 2) ? 0 : size;

request->response->response_buffer->response_length = (uint16_t)size;
Expand Down
4 changes: 2 additions & 2 deletions api/oc_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ oc_core_1_1_discovery_handler(oc_request_t *request,
break;
}

int response_length = oc_rep_finalize();
int response_length = oc_rep_get_encoded_payload_size();

if (matches && response_length) {
request->response->response_buffer->response_length =
Expand Down Expand Up @@ -443,7 +443,7 @@ oc_core_discovery_handler(oc_request_t *request, oc_interface_mask_t interface,
default:
break;
}
int response_length = oc_rep_finalize();
int response_length = oc_rep_get_encoded_payload_size();
if (matches && response_length > 0) {
request->response->response_buffer->response_length =
(uint16_t)response_length;
Expand Down
8 changes: 7 additions & 1 deletion api/oc_rep.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ oc_rep_get_cbor_errno(void)
return g_err;
}

const uint8_t *
oc_rep_get_encoder_buf(void)
{
return g_buf;
}

int
oc_rep_finalize(void)
oc_rep_get_encoded_payload_size(void)
{
size_t size = cbor_encoder_get_buffer_size(&g_encoder, g_buf);
if (g_err == CborErrorOutOfMemory) {
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 @@ -62,7 +62,7 @@ oc_get_query_value(oc_request_t *request, const char *key, char **value)
static int
response_length(void)
{
int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
return (size <= 2) ? 0 : size;
}

Expand Down
29 changes: 16 additions & 13 deletions api/unittest/reptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

TEST(TestRep, OCRepFinalizeTest_P)
{
int repSize = oc_rep_finalize();
int repSize = oc_rep_get_encoded_payload_size();
EXPECT_NE(repSize, -1);
}

Expand All @@ -45,17 +45,18 @@ TEST(TestRep, OCRepSetGetInt)
*/
uint8_t buf[1024];
oc_rep_new(&buf[0], 1024);
uint8_t *payload = g_encoder.data.ptr;
struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);

/* add int value "ultimate_answer":42 to root object */
oc_rep_start_root_object();
oc_rep_set_int(root, ultimate_answer, 42);
oc_rep_end_root_object();

/* convert CborEncoder to oc_rep_t */
int payload_len = oc_rep_finalize();
const uint8_t *payload = oc_rep_get_encoder_buf();

struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);
int payload_len = oc_rep_get_encoded_payload_size();
EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep);
Expand All @@ -78,17 +79,18 @@ TEST(TestRep, OCRepSetGetTextString)
*/
uint8_t buf[1024];
oc_rep_new(&buf[0], 1024);
uint8_t *payload = g_encoder.data.ptr;
struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);

/* add text string value "hal9000":"Dave" to root object */
oc_rep_start_root_object();
oc_rep_set_text_string(root, hal9000, "Dave");
oc_rep_end_root_object();

/* convert CborEncoder to oc_rep_t */
int payload_len = oc_rep_finalize();
const uint8_t *payload = oc_rep_get_encoder_buf();

struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);
int payload_len = oc_rep_get_encoded_payload_size();
EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep);
Expand All @@ -113,9 +115,6 @@ TEST(TestRep, OCRepSetGetIntArray)
*/
uint8_t buf[1024];
oc_rep_new(&buf[0], 1024);
uint8_t *payload = g_encoder.data.ptr;
struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);

/* add int array to root object */
int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
Expand All @@ -124,7 +123,11 @@ TEST(TestRep, OCRepSetGetIntArray)
oc_rep_end_root_object();

/* convert CborEncoder to oc_rep_t */
int payload_len = oc_rep_finalize();
const uint8_t *payload = oc_rep_get_encoder_buf();

struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0 ,0 };
oc_rep_set_pool(&rep_objects);
int payload_len = oc_rep_get_encoded_payload_size();
EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep);
Expand Down
3 changes: 2 additions & 1 deletion include/oc_rep.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extern CborEncoder g_encoder, root_map, links_array;
extern int g_err;

void oc_rep_new(uint8_t *payload, int size);
int oc_rep_finalize(void);
int oc_rep_get_encoded_payload_size(void);
const uint8_t *oc_rep_get_encoder_buf(void);

#define oc_rep_object(name) &name##_map
#define oc_rep_array(name) &name##_array
Expand Down
2 changes: 1 addition & 1 deletion security/oc_obt.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ oc_obt_dump_state(void)
oc_rep_set_int(root, id, id);
oc_rep_end_root_object();

int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_obt: dumped current state: size %d", size);
oc_storage_write("obt_state", buf, size);
Expand Down
10 changes: 5 additions & 5 deletions security/oc_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ oc_sec_dump_pstat(size_t device)

oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_pstat(device);
int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_store: encoded pstat size %d", size);
char svr_tag[SVR_TAG_MAX];
Expand All @@ -249,7 +249,7 @@ oc_sec_dump_cred(size_t device)

oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_cred(true, device);
int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_store: encoded cred size %d", size);
char svr_tag[SVR_TAG_MAX];
Expand All @@ -276,7 +276,7 @@ oc_sec_dump_doxm(size_t device)
/* doxm */
oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_doxm(device);
int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_store: encoded doxm size %d", size);
char svr_tag[SVR_TAG_MAX];
Expand All @@ -302,7 +302,7 @@ oc_sec_dump_acl(size_t device)

oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_acl(device);
int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_store: encoded ACL size %d", size);
char svr_tag[SVR_TAG_MAX];
Expand Down Expand Up @@ -398,7 +398,7 @@ oc_sec_dump_unique_ids(size_t device)
oc_rep_set_text_string(root, piid, piid);
oc_rep_end_root_object();

int size = oc_rep_finalize();
int size = oc_rep_get_encoded_payload_size();
if (size > 0) {
OC_DBG("oc_store: encoded unique identifiers: size %d", size);
char svr_tag[SVR_TAG_MAX];
Expand Down

0 comments on commit 86b10a3

Please sign in to comment.