Skip to content

Commit

Permalink
Add OC_API annotations to oc_rep.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 29, 2024
1 parent 65d0f64 commit b28a73a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 47 deletions.
1 change: 1 addition & 0 deletions api/oc_mnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "api/oc_core_res_internal.h"
#include "api/oc_mnt_internal.h"
#include "api/oc_rep_internal.h"
#include "api/oc_resource_internal.h"
#include "api/oc_ri_internal.h"
#include "oc_core_res.h"
Expand Down
19 changes: 19 additions & 0 deletions api/oc_rep_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ int oc_rep_parse_payload(const uint8_t *payload, size_t payload_size,
/** Convenience wrapper over oc_rep_parse_payload */
oc_rep_t *oc_parse_rep(const uint8_t *payload, size_t payload_size);

/**
* Get the buffer pointer at the start of the encoded cbor data.
*
* @return pointer to the start of the cbor encoded buffer
*
* @see oc_parse_rep
*/
const uint8_t *oc_rep_get_encoder_buf(void);

/**
* Shrink the buffer pointer to length of encoded cbor data.
*
* @param[in] buf pointer to cbor encoded buffer
* @return pointer to the start of the shrinked cbor encoded buffer
*
* @see oc_parse_rep
*/
uint8_t *oc_rep_shrink_encoder_buf(uint8_t *buf);

/**
* @brief Get the size of the cbor encoded data.
*
Expand Down
19 changes: 10 additions & 9 deletions api/unittest/helperstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,25 @@ TEST(Helpers, RandomBuffer)
TEST(Helpers, HexStringTooSmallError)
{
std::array<uint8_t, 4> array{ 0x1f, 0xa0, 0x5b, 0xff };
char hex_str[5]; // Too small to fit the full hex representation (8 chars +
// null terminator)
size_t hex_str_len = sizeof(hex_str);
std::array<char, 5> hex_str; // Too small to fit the full hex representation
// (8 chars + null terminator)
size_t hex_str_len = hex_str.size();
int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(),
hex_str, &hex_str_len);
&hex_str[0], &hex_str_len);
ASSERT_EQ(result, -1);
}

// Test successful conversion of a byte array to hex string
TEST(Helpers, ConvertByteArrayToHexStringSuccess)
{
std::array<uint8_t, 4> array{ 0x1f, 0xa0, 0x5b, 0xff };
char hex_str[9]; // Must hold 8 characters (2 per byte) + null terminator
size_t hex_str_len = sizeof(hex_str);
std::array<char, 9>
hex_str; // Must hold 8 characters (2 per byte) + null terminator
size_t hex_str_len = hex_str.size();
int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(),
hex_str, &hex_str_len);
&hex_str[0], &hex_str_len);
ASSERT_EQ(result, 0);
EXPECT_STREQ(hex_str, "1fa05bff");
EXPECT_STREQ(hex_str.data(), "1fa05bff");
EXPECT_EQ(hex_str_len, 8);
}

Expand Down Expand Up @@ -334,7 +335,7 @@ TEST(Helpers, OddLengthHexStringSuccess)
hex_str.c_str(), hex_str.length(), &array[0], &array_len);

ASSERT_EQ(result, 0);
EXPECT_EQ(array_len, 3);
std::array<uint8_t, 3> expected_array = { 0xfa, 0x05, 0xbf };
EXPECT_EQ(array_len, expected_array.size());
EXPECT_EQ(expected_array, array);
}
71 changes: 34 additions & 37 deletions include/oc_rep.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,6 @@ int oc_rep_get_encoder_buffer_size(void);
OC_API
int oc_rep_get_encoded_payload_size(void);

/**
* Get the buffer pointer at the start of the encoded cbor data.
*
* This is used when parsing the encoded cbor data to an oc_rep_t. It is
* unlikely to be used outside the IoTivity-lite library.
*
* @return pointer to the start of the cbor encoded buffer
*
* @see oc_parse_rep
*/
const uint8_t *oc_rep_get_encoder_buf(void);

/**
* Shrink the buffer pointer to length of encoded cbor data.
*
* This is used when parsing the encoded cbor data to an oc_rep_t. It is
* unlikely to be used outside the IoTivity-lite library.
*
* @param[in] buf pointer to cbor encoded buffer
* @return pointer to the start of the shrinked cbor encoded buffer
*
* @see oc_parse_rep
*/
uint8_t *oc_rep_shrink_encoder_buf(uint8_t *buf);

/**
* @brief Encode raw data to the global encoder, as if it was already encoded.
*
Expand All @@ -154,15 +129,16 @@ uint8_t *oc_rep_shrink_encoder_buf(uint8_t *buf);
* @param len Length of data.
*/
OC_API
void oc_rep_encode_raw(const uint8_t *data, size_t len);
void oc_rep_encode_raw(const uint8_t *data, size_t len) OC_NONNULL();

/**
* @brief Encode a NULL value.
*
* @param encoder Internal Iotivity-lite encoder to store the value
* @return CborError encoding error
*/
CborError oc_rep_encode_null(CborEncoder *encoder);
OC_API
CborError oc_rep_encode_null(CborEncoder *encoder) OC_NONNULL();

/**
* @brief Encode a boolean value.
Expand All @@ -171,7 +147,8 @@ CborError oc_rep_encode_null(CborEncoder *encoder);
* @param value value to encode
* @return CborError encoding error
*/
CborError oc_rep_encode_boolean(CborEncoder *encoder, bool value);
OC_API
CborError oc_rep_encode_boolean(CborEncoder *encoder, bool value) OC_NONNULL();

/**
* @brief Encode a signed integer value.
Expand All @@ -180,7 +157,8 @@ CborError oc_rep_encode_boolean(CborEncoder *encoder, bool value);
* @param value value to encode
* @return CborError encoding error
*/
CborError oc_rep_encode_int(CborEncoder *encoder, int64_t value);
OC_API
CborError oc_rep_encode_int(CborEncoder *encoder, int64_t value) OC_NONNULL();

/**
* @brief Encode an unsigned integer value.
Expand All @@ -189,7 +167,8 @@ CborError oc_rep_encode_int(CborEncoder *encoder, int64_t value);
* @param value value to encode
* @return CborError encoding error
*/
CborError oc_rep_encode_uint(CborEncoder *encoder, uint64_t value);
OC_API
CborError oc_rep_encode_uint(CborEncoder *encoder, uint64_t value) OC_NONNULL();

/**
* @brief Encode floating point value.
Expand All @@ -200,8 +179,9 @@ CborError oc_rep_encode_uint(CborEncoder *encoder, uint64_t value);
* @param value value to encode
* @return CborError encoding error
*/
OC_API
CborError oc_rep_encode_floating_point(CborEncoder *encoder, CborType fpType,
const void *value);
const void *value) OC_NONNULL();

/**
* @brief Encode a double value.
Expand All @@ -210,7 +190,8 @@ CborError oc_rep_encode_floating_point(CborEncoder *encoder, CborType fpType,
* @param value value to encode
* @return CborError encoding error
*/
CborError oc_rep_encode_double(CborEncoder *encoder, double value);
OC_API
CborError oc_rep_encode_double(CborEncoder *encoder, double value) OC_NONNULL();

/**
* @brief Encode a C-string.
Expand All @@ -220,8 +201,9 @@ CborError oc_rep_encode_double(CborEncoder *encoder, double value);
* @param length length of the C-string
* @return CborError encoding error
*/
OC_API
CborError oc_rep_encode_text_string(CborEncoder *encoder, const char *string,
size_t length);
size_t length) OC_NONNULL();

/**
* @brief Encode a byte string.
Expand All @@ -231,8 +213,9 @@ CborError oc_rep_encode_text_string(CborEncoder *encoder, const char *string,
* @param length length of the byte string
* @return CborError encoding error
*/
OC_API
CborError oc_rep_encode_byte_string(CborEncoder *encoder, const uint8_t *string,
size_t length);
size_t length) OC_NONNULL();

/**
* @brief Encode the beginning of an array.
Expand All @@ -245,8 +228,10 @@ CborError oc_rep_encode_byte_string(CborEncoder *encoder, const uint8_t *string,
*
* @note Must be closed by oc_rep_encoder_close_container
*/
OC_API
CborError oc_rep_encoder_create_array(CborEncoder *encoder,
CborEncoder *arrayEncoder, size_t length);
CborEncoder *arrayEncoder, size_t length)
OC_NONNULL();
/**
* @brief Encode the beginning of a map.
*
Expand All @@ -258,8 +243,10 @@ CborError oc_rep_encoder_create_array(CborEncoder *encoder,
*
* @note Must be closed by oc_rep_encoder_close_container
*/
OC_API
CborError oc_rep_encoder_create_map(CborEncoder *encoder,
CborEncoder *mapEncoder, size_t length);
CborEncoder *mapEncoder, size_t length)
OC_NONNULL();

/**
* @brief Encode the ending of a container (an array or a map).
Expand All @@ -273,8 +260,10 @@ CborError oc_rep_encoder_create_map(CborEncoder *encoder,
* @see oc_rep_encoder_create_array
* @see oc_rep_encoder_create_map
*/
OC_API
CborError oc_rep_encoder_close_container(CborEncoder *encoder,
CborEncoder *containerEncoder);
CborEncoder *containerEncoder)
OC_NONNULL();

/**
* Get a pointer to the cbor object with the given `name`
Expand All @@ -291,37 +280,45 @@ CborError oc_rep_encoder_close_container(CborEncoder *encoder,
#define oc_rep_array(name) &name##_array

/** Add an integer `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_null(CborEncoder *object, const char *key,
size_t key_len) OC_NONNULL();

/** Add an boolean `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_boolean(CborEncoder *object, const char *key,
size_t key_len, bool value) OC_NONNULL();

/** Add an integer `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_int(CborEncoder *object, const char *key,
size_t key_len, int64_t value) OC_NONNULL();

/** Add an unsigned integer `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_uint(CborEncoder *object, const char *key,
size_t key_len, uint64_t value) OC_NONNULL();

/** Add a double `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_double(CborEncoder *object, const char *key,
size_t key_len, double value) OC_NONNULL();

/** Add an string `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_text_string(CborEncoder *object, const char *key,
size_t key_len, const char *value,
size_t length) OC_NONNULL(1, 2);

/** Add an byte array `value` to the cbor `object` under the `key` name */
OC_API
CborError oc_rep_object_set_byte_string(CborEncoder *object, const char *key,
size_t key_len, const uint8_t *value,
size_t length) OC_NONNULL();

/** Add a string array using an oc_string_array_t as `values` to the cbor
* `object` under the `key` name. */
OC_API
CborError oc_rep_object_set_string_array(CborEncoder *object, const char *key,
size_t key_len,
const oc_string_array_t *array)
Expand Down
4 changes: 3 additions & 1 deletion include/oc_signal_event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
#ifndef OC_SIGNAL_EVENT_LOOP_H
#define OC_SIGNAL_EVENT_LOOP_H

#include "oc_export.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief signal event loop
*
*/
OC_API
void _oc_signal_event_loop(void);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions messaging/coap/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "api/oc_helpers_internal.h"
#include "api/oc_message_internal.h"
#include "api/oc_query_internal.h"
#include "api/oc_rep_internal.h"
#include "api/oc_ri_internal.h"
#include "api/oc_server_api_internal.h"
#include "messaging/coap/log_internal.h"
Expand Down

0 comments on commit b28a73a

Please sign in to comment.