From 86b10a323ff2db4c49aef4052b51f8ac63c23615 Mon Sep 17 00:00:00 2001 From: Kishen Maloor Date: Wed, 7 Nov 2018 13:45:19 -0800 Subject: [PATCH] oc_rep:API to get encoder buffer and payload size Change-Id: Ie2055874ea80bc777e3b84a87fb54366447b3d55 Signed-off-by: Kishen Maloor Reviewed-on: https://gerrit.iotivity.org/gerrit/27419 Tested-by: IoTivity Jenkins --- api/oc_client_api.c | 2 +- api/oc_collection.c | 6 +++--- api/oc_discovery.c | 4 ++-- api/oc_rep.c | 8 +++++++- api/oc_server_api.c | 2 +- api/unittest/reptest.cpp | 29 ++++++++++++++++------------- include/oc_rep.h | 3 ++- security/oc_obt.c | 2 +- security/oc_store.c | 10 +++++----- 9 files changed, 38 insertions(+), 28 deletions(-) diff --git a/api/oc_client_api.c b/api/oc_client_api.c index bc52d5e7..a448160a 100644 --- a/api/oc_client_api.c +++ b/api/oc_client_api.c @@ -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) { diff --git a/api/oc_collection.c b/api/oc_collection.c index 4f473a14..f5d4fb7f 100644 --- a/api/oc_collection.c +++ b/api/oc_collection.c @@ -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; @@ -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(); @@ -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; diff --git a/api/oc_discovery.c b/api/oc_discovery.c index 4c9793fe..92a0c4ba 100644 --- a/api/oc_discovery.c +++ b/api/oc_discovery.c @@ -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 = @@ -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; diff --git a/api/oc_rep.c b/api/oc_rep.c index 04ffb3ef..fafdc88d 100644 --- a/api/oc_rep.c +++ b/api/oc_rep.c @@ -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) { diff --git a/api/oc_server_api.c b/api/oc_server_api.c index 62041af1..f9ebf45d 100644 --- a/api/oc_server_api.c +++ b/api/oc_server_api.c @@ -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; } diff --git a/api/unittest/reptest.cpp b/api/unittest/reptest.cpp index feb3a30e..4829f876 100644 --- a/api/unittest/reptest.cpp +++ b/api/unittest/reptest.cpp @@ -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); } @@ -45,9 +45,6 @@ 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(); @@ -55,7 +52,11 @@ TEST(TestRep, OCRepSetGetInt) 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); @@ -78,9 +79,6 @@ 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(); @@ -88,7 +86,11 @@ TEST(TestRep, OCRepSetGetTextString) 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); @@ -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 }; @@ -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); diff --git a/include/oc_rep.h b/include/oc_rep.h index e9a80770..03ef5ec7 100644 --- a/include/oc_rep.h +++ b/include/oc_rep.h @@ -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 diff --git a/security/oc_obt.c b/security/oc_obt.c index 902b3772..e86706ff 100644 --- a/security/oc_obt.c +++ b/security/oc_obt.c @@ -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); diff --git a/security/oc_store.c b/security/oc_store.c index ec49492d..fba87dbb 100644 --- a/security/oc_store.c +++ b/security/oc_store.c @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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];