Commit 86b10a32 authored by Kishen Maloor's avatar Kishen Maloor

oc_rep:API to get encoder buffer and payload size

Change-Id: Ie2055874ea80bc777e3b84a87fb54366447b3d55
Signed-off-by: Kishen Maloor's avatarKishen Maloor <kishen.maloor@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/27419Tested-by: default avatarIoTivity Jenkins <jenkins-daemon@iotivity.org>
parent 25bd9d05
...@@ -35,7 +35,7 @@ oc_event_callback_retval_t oc_ri_remove_client_cb(void *data); ...@@ -35,7 +35,7 @@ oc_event_callback_retval_t oc_ri_remove_client_cb(void *data);
static bool static bool
dispatch_coap_request(void) 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) && if ((client_cb->method == OC_PUT || client_cb->method == OC_POST) &&
payload_size > 0) { payload_size > 0) {
......
...@@ -339,7 +339,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request, ...@@ -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"); oc_rep_set_key(*oc_rep_object(links), "rep");
memcpy(&g_encoder, &links_map, sizeof(CborEncoder)); 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; rest_request.resource = link->resource;
response_buffer.code = 0; response_buffer.code = 0;
response_buffer.response_length = 0; response_buffer.response_length = 0;
...@@ -389,7 +389,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request, ...@@ -389,7 +389,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
} else { } else {
if (code < oc_status_code(OC_STATUS_BAD_REQUEST)) if (code < oc_status_code(OC_STATUS_BAD_REQUEST))
code = response_buffer.code; code = response_buffer.code;
int size_after = oc_rep_finalize(); int size_after = oc_rep_get_encoded_payload_size();
if (size_before == size_after) { if (size_before == size_after) {
oc_rep_start_root_object(); oc_rep_start_root_object();
oc_rep_end_root_object(); oc_rep_end_root_object();
...@@ -423,7 +423,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request, ...@@ -423,7 +423,7 @@ oc_handle_collection_request(oc_method_t method, oc_request_t *request,
break; break;
} }
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
size = (size <= 2) ? 0 : size; size = (size <= 2) ? 0 : size;
request->response->response_buffer->response_length = (uint16_t)size; request->response->response_buffer->response_length = (uint16_t)size;
......
...@@ -393,7 +393,7 @@ oc_core_1_1_discovery_handler(oc_request_t *request, ...@@ -393,7 +393,7 @@ oc_core_1_1_discovery_handler(oc_request_t *request,
break; break;
} }
int response_length = oc_rep_finalize(); int response_length = oc_rep_get_encoded_payload_size();
if (matches && response_length) { if (matches && response_length) {
request->response->response_buffer->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, ...@@ -443,7 +443,7 @@ oc_core_discovery_handler(oc_request_t *request, oc_interface_mask_t interface,
default: default:
break; break;
} }
int response_length = oc_rep_finalize(); int response_length = oc_rep_get_encoded_payload_size();
if (matches && response_length > 0) { if (matches && response_length > 0) {
request->response->response_buffer->response_length = request->response->response_buffer->response_length =
(uint16_t)response_length; (uint16_t)response_length;
......
...@@ -45,8 +45,14 @@ oc_rep_get_cbor_errno(void) ...@@ -45,8 +45,14 @@ oc_rep_get_cbor_errno(void)
return g_err; return g_err;
} }
const uint8_t *
oc_rep_get_encoder_buf(void)
{
return g_buf;
}
int int
oc_rep_finalize(void) oc_rep_get_encoded_payload_size(void)
{ {
size_t size = cbor_encoder_get_buffer_size(&g_encoder, g_buf); size_t size = cbor_encoder_get_buffer_size(&g_encoder, g_buf);
if (g_err == CborErrorOutOfMemory) { if (g_err == CborErrorOutOfMemory) {
......
...@@ -62,7 +62,7 @@ oc_get_query_value(oc_request_t *request, const char *key, char **value) ...@@ -62,7 +62,7 @@ oc_get_query_value(oc_request_t *request, const char *key, char **value)
static int static int
response_length(void) response_length(void)
{ {
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
return (size <= 2) ? 0 : size; return (size <= 2) ? 0 : size;
} }
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
TEST(TestRep, OCRepFinalizeTest_P) TEST(TestRep, OCRepFinalizeTest_P)
{ {
int repSize = oc_rep_finalize(); int repSize = oc_rep_get_encoded_payload_size();
EXPECT_NE(repSize, -1); EXPECT_NE(repSize, -1);
} }
...@@ -45,9 +45,6 @@ TEST(TestRep, OCRepSetGetInt) ...@@ -45,9 +45,6 @@ TEST(TestRep, OCRepSetGetInt)
*/ */
uint8_t buf[1024]; uint8_t buf[1024];
oc_rep_new(&buf[0], 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 */ /* add int value "ultimate_answer":42 to root object */
oc_rep_start_root_object(); oc_rep_start_root_object();
...@@ -55,7 +52,11 @@ TEST(TestRep, OCRepSetGetInt) ...@@ -55,7 +52,11 @@ TEST(TestRep, OCRepSetGetInt)
oc_rep_end_root_object(); oc_rep_end_root_object();
/* convert CborEncoder to oc_rep_t */ /* 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); EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL; oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep); oc_parse_rep(payload, payload_len, &rep);
...@@ -78,9 +79,6 @@ TEST(TestRep, OCRepSetGetTextString) ...@@ -78,9 +79,6 @@ TEST(TestRep, OCRepSetGetTextString)
*/ */
uint8_t buf[1024]; uint8_t buf[1024];
oc_rep_new(&buf[0], 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 */ /* add text string value "hal9000":"Dave" to root object */
oc_rep_start_root_object(); oc_rep_start_root_object();
...@@ -88,7 +86,11 @@ TEST(TestRep, OCRepSetGetTextString) ...@@ -88,7 +86,11 @@ TEST(TestRep, OCRepSetGetTextString)
oc_rep_end_root_object(); oc_rep_end_root_object();
/* convert CborEncoder to oc_rep_t */ /* 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); EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL; oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep); oc_parse_rep(payload, payload_len, &rep);
...@@ -113,9 +115,6 @@ TEST(TestRep, OCRepSetGetIntArray) ...@@ -113,9 +115,6 @@ TEST(TestRep, OCRepSetGetIntArray)
*/ */
uint8_t buf[1024]; uint8_t buf[1024];
oc_rep_new(&buf[0], 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 */ /* add int array to root object */
int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 }; int fib[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
...@@ -124,7 +123,11 @@ TEST(TestRep, OCRepSetGetIntArray) ...@@ -124,7 +123,11 @@ TEST(TestRep, OCRepSetGetIntArray)
oc_rep_end_root_object(); oc_rep_end_root_object();
/* convert CborEncoder to oc_rep_t */ /* 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); EXPECT_NE(payload_len, -1);
oc_rep_t *rep = NULL; oc_rep_t *rep = NULL;
oc_parse_rep(payload, payload_len, &rep); oc_parse_rep(payload, payload_len, &rep);
......
...@@ -33,7 +33,8 @@ extern CborEncoder g_encoder, root_map, links_array; ...@@ -33,7 +33,8 @@ extern CborEncoder g_encoder, root_map, links_array;
extern int g_err; extern int g_err;
void oc_rep_new(uint8_t *payload, int size); 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_object(name) &name##_map
#define oc_rep_array(name) &name##_array #define oc_rep_array(name) &name##_array
......
...@@ -230,7 +230,7 @@ oc_obt_dump_state(void) ...@@ -230,7 +230,7 @@ oc_obt_dump_state(void)
oc_rep_set_int(root, id, id); oc_rep_set_int(root, id, id);
oc_rep_end_root_object(); oc_rep_end_root_object();
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_obt: dumped current state: size %d", size); OC_DBG("oc_obt: dumped current state: size %d", size);
oc_storage_write("obt_state", buf, size); oc_storage_write("obt_state", buf, size);
......
...@@ -223,7 +223,7 @@ oc_sec_dump_pstat(size_t device) ...@@ -223,7 +223,7 @@ oc_sec_dump_pstat(size_t device)
oc_rep_new(buf, OC_MAX_APP_DATA_SIZE); oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_pstat(device); oc_sec_encode_pstat(device);
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_store: encoded pstat size %d", size); OC_DBG("oc_store: encoded pstat size %d", size);
char svr_tag[SVR_TAG_MAX]; char svr_tag[SVR_TAG_MAX];
...@@ -249,7 +249,7 @@ oc_sec_dump_cred(size_t device) ...@@ -249,7 +249,7 @@ oc_sec_dump_cred(size_t device)
oc_rep_new(buf, OC_MAX_APP_DATA_SIZE); oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_cred(true, device); oc_sec_encode_cred(true, device);
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_store: encoded cred size %d", size); OC_DBG("oc_store: encoded cred size %d", size);
char svr_tag[SVR_TAG_MAX]; char svr_tag[SVR_TAG_MAX];
...@@ -276,7 +276,7 @@ oc_sec_dump_doxm(size_t device) ...@@ -276,7 +276,7 @@ oc_sec_dump_doxm(size_t device)
/* doxm */ /* doxm */
oc_rep_new(buf, OC_MAX_APP_DATA_SIZE); oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_doxm(device); oc_sec_encode_doxm(device);
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_store: encoded doxm size %d", size); OC_DBG("oc_store: encoded doxm size %d", size);
char svr_tag[SVR_TAG_MAX]; char svr_tag[SVR_TAG_MAX];
...@@ -302,7 +302,7 @@ oc_sec_dump_acl(size_t device) ...@@ -302,7 +302,7 @@ oc_sec_dump_acl(size_t device)
oc_rep_new(buf, OC_MAX_APP_DATA_SIZE); oc_rep_new(buf, OC_MAX_APP_DATA_SIZE);
oc_sec_encode_acl(device); oc_sec_encode_acl(device);
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_store: encoded ACL size %d", size); OC_DBG("oc_store: encoded ACL size %d", size);
char svr_tag[SVR_TAG_MAX]; char svr_tag[SVR_TAG_MAX];
...@@ -398,7 +398,7 @@ oc_sec_dump_unique_ids(size_t device) ...@@ -398,7 +398,7 @@ oc_sec_dump_unique_ids(size_t device)
oc_rep_set_text_string(root, piid, piid); oc_rep_set_text_string(root, piid, piid);
oc_rep_end_root_object(); oc_rep_end_root_object();
int size = oc_rep_finalize(); int size = oc_rep_get_encoded_payload_size();
if (size > 0) { if (size > 0) {
OC_DBG("oc_store: encoded unique identifiers: size %d", size); OC_DBG("oc_store: encoded unique identifiers: size %d", size);
char svr_tag[SVR_TAG_MAX]; char svr_tag[SVR_TAG_MAX];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment