Skip to content

Commit

Permalink
fixup! use oc_endpoint_to_string64 to utilize stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 19, 2023
1 parent f93fdc6 commit 3a9a2aa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
42 changes: 22 additions & 20 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
*
****************************************************************************/

#include "api/oc_endpoint_internal.h"
#include "api/oc_helpers_internal.h"
#include "oc_endpoint.h"
#include "oc_endpoint_internal.h"
#include "oc_core_res.h"
#include "port/common/oc_ip.h"
#include "port/oc_connectivity.h"
Expand Down Expand Up @@ -66,33 +67,34 @@ oc_endpoint_set_di(oc_endpoint_t *endpoint, const oc_uuid_t *di)
memcpy(endpoint->di.id, di->id, sizeof(di->id));
}

int
oc_endpoint_flags_to_scheme(unsigned flags, char *buf, size_t size)
static oc_string_view_t
endpoint_flags_to_scheme(unsigned flags)
{
const char *scheme = OC_SCHEME_COAP;
size_t needed = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAP);
if ((flags & SECURED) != 0) {
needed = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAPS);
scheme = OC_SCHEME_COAPS;
}
#ifdef OC_TCP
if ((flags & TCP) != 0) {
if ((flags & SECURED) != 0) {
needed = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAPS_TCP);
scheme = OC_SCHEME_COAPS_TCP;
} else {
needed = OC_CHAR_ARRAY_LEN(OC_SCHEME_COAP_TCP);
scheme = OC_SCHEME_COAP_TCP;
return OC_STRING_VIEW(OC_SCHEME_COAPS_TCP);
}
return OC_STRING_VIEW(OC_SCHEME_COAP_TCP);
}
#endif /* OC_TCP */

if (buf == NULL) {
return (int)needed;
if ((flags & SECURED) != 0) {
return OC_STRING_VIEW(OC_SCHEME_COAPS);
}
if (needed <= size) {
memcpy(buf, scheme, needed);
return (int)needed;
return OC_STRING_VIEW(OC_SCHEME_COAP);
}

int
oc_endpoint_flags_to_scheme(unsigned flags, char *buffer, size_t buffer_size)
{
oc_string_view_t scheme = endpoint_flags_to_scheme(flags);
if (buffer == NULL) {
return (int)scheme.length;
}
if (scheme.length < buffer_size) {
memcpy(buffer, scheme.data, scheme.length);
buffer[scheme.length] = '\0';
return (int)scheme.length;
}
return -1;
}
Expand Down
22 changes: 12 additions & 10 deletions api/oc_endpoint_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ extern "C" {
#define OC_SCHEME_OCF "ocf://"

/**
* @brief Get scheme string for transport flags
* @brief Write the scheme string (including NUL terminator) for given transport
* flags to buffer
*
* @param flags type of endpoint
* @param buf to store the scheme
* @param buf_len length of the buffer and will be set to the length of the
* used/needed buffer.
* @return return number of written bytes. -1 for error
* @param flags transport flags of an endpoint
* @param buffer output buffer (if NULL the function returns the number of bytes
* that would have been written, excluding the NUL terminator)
* @param buffer_size size of output buffer
* @return return number of written bytes (excluding the NUL terminator)
* @return -1 for error
*/
int oc_endpoint_flags_to_scheme(unsigned flags, char *buf, size_t buf_len);
int oc_endpoint_flags_to_scheme(unsigned flags, char *buffer,
size_t buffer_size);

/**
* @brief Convert the endpoint to a human readable string (e.g.
* "[fe::22]:1234")
*
* @param endpoint the endpoint
* @param buffer output buffer
* @param endpoint the endpoint (cannot be NULL)
* @param buffer output buffer (cannot be NULL)
* @param buffer_size size of output buffer
* @return number of written bytes, -1 for error
*/
Expand Down Expand Up @@ -91,7 +94,6 @@ typedef struct oc_string64_s
* @param endpoint_str endpoint as human readable string
* @return true for success
*/
OC_API
bool oc_endpoint_to_string64(const oc_endpoint_t *endpoint,
oc_string64_t *endpoint_str);

Expand Down
2 changes: 1 addition & 1 deletion api/unittest/plgdtimetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ TEST_F(TestPlgdTimeWithServer, FetchTimeFail)

std::array<char, 16> scheme{};
size_t scheme_len = scheme.size();
oc_endpoint_flags_to_scheme(ep_flags, &scheme[0], &scheme_len);
ASSERT_NE(-1, oc_endpoint_flags_to_scheme(ep_flags, &scheme[0], &scheme_len));

std::string ep_str = std::string(scheme.data()) + "[ff02::158]:12345";
oc_endpoint_t ep = oc::endpoint::FromString(ep_str);
Expand Down
5 changes: 3 additions & 2 deletions include/oc_endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ int oc_endpoint_to_string(const oc_endpoint_t *endpoint,
* @brief convert the endpoint to a human readable string (e.g.
* "coaps://[fe::22]:1234")
*
* @param endpoint the endpoint
* @param buffer output buffer
* @param endpoint the endpoint (cannot be NULL)
* @param buffer output buffer (cannot be NULL)
* @param buffer_size size of output buffer
* @return number of written bytes, -1 for error
*/
OC_API
int oc_endpoint_to_cstring(const oc_endpoint_t *endpoint, char *buffer,
size_t buffer_size) OC_NONNULL();

Expand Down

0 comments on commit 3a9a2aa

Please sign in to comment.