Skip to content

Commit

Permalink
Handle oc_message_t buffer size
Browse files Browse the repository at this point in the history
Depending on compilation options the buffer of oc_message_t might
be static or dynamic, and of various sizes. Functions that work
with the buffer must receive correct size of the buffer.
  • Loading branch information
Danielius1922 committed Jun 22, 2023
1 parent eac6f2a commit 61bfade
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 202 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/cmake-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ jobs:
- args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF"
# ipv4 on, tcp on, pki off
- args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_PKI_ENABLED=OFF"
# cloud on (ipv4+tcp on), collections create on, maintenance resource on
# cloud on (ipv4+tcp on), collections create on
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON"
# cloud on (ipv4+tcp on), collections create on, custom message buffer size
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=1024"
# debug on
- args: "-DOC_DEBUG_ENABLED=ON"
# debug on, cloud on (ipv4+tcp on)
Expand All @@ -66,7 +68,7 @@ jobs:
- args: "-DOC_SECURITY_ENABLED=OFF -DOC_TCP_ENABLED=ON -DOC_IPV4_ENABLED=ON"
# /oic/res observable on, rep realloc on
- args: "-DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON"
# everything off (dynamic allocation off, secure off, pki off, idd off, oscore off, well-known core resource off, software update off, push notifications off, plgd-time off, introspection off)
# everything off (dynamic allocation off, secure off, pki off, idd off, oscore off, well-known core resource off, software update off, , maintenance resource off, push notifications off, plgd-time off, introspection off)
- args: "-DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_SECURITY_ENABLED=OFF -DOC_PKI_ENABLED=OFF -DOC_IDD_API_ENABLED=OFF -DOC_OSCORE_ENABLED=OFF -DOC_WKCORE_ENABLED=OFF -DOC_SOFTWARE_UPDATE_ENABLED=OFF -DOC_MNT_ENABLED=OFF -DOC_PUSH_ENABLED=OFF -DPLGD_DEV_TIME_ENABLED=OFF -DOC_INTROSPECTION_ENABLED=OFF"
uses: ./.github/workflows/unit-test-with-cfg.yml
with:
Expand Down
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ if (OC_DEBUG_ENABLED)
else()
set(OC_LOG_MAXIMUM_LOG_LEVEL "DISABLED" CACHE STRING "Maximum supported log level in compile time.")
endif()
set(OC_INOUT_BUFFER_SIZE "" CACHE STRING "Custom buffer size for network messages.")
set(OC_INOUT_BUFFER_POOL_SIZE "" CACHE STRING "Custom pool size of network messages.")
set(PLGD_DEV_TIME_ENABLED OFF CACHE BOOL "Enable plgd time feature.")

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand Down Expand Up @@ -363,6 +365,20 @@ if(OC_MEMORY_TRACE_ENABLED)
list(APPEND TEST_COMPILE_DEFINITIONS "OC_MEMORY_TRACE")
endif()

if (NOT("${OC_INOUT_BUFFER_SIZE}" STREQUAL ""))
if(NOT OC_DYNAMIC_ALLOCATION_ENABLED)
message(FATAL_ERROR "Cannot set custom buffer size for network messages without dynamic allocation")
endif()
list(APPEND PUBLIC_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})")
list(APPEND MBEDTLS_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_SIZE=(${OC_INOUT_BUFFER_SIZE})")
endif()
if (NOT("${OC_INOUT_BUFFER_POOL_SIZE}" STREQUAL ""))
if(NOT OC_DYNAMIC_ALLOCATION_ENABLED)
message(FATAL_ERROR "Cannot set custom pool size for network messages without dynamic allocation")
endif()
list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_INOUT_BUFFER_POOL_SIZE=(${OC_INOUT_BUFFER_POOL_SIZE})")
endif()

if(PLGD_DEV_TIME_ENABLED)
list(APPEND PUBLIC_COMPILE_DEFINITIONS "PLGD_DEV_TIME")
if(BUILD_MBEDTLS)
Expand Down
10 changes: 5 additions & 5 deletions api/oc_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
#endif /* OC_DYNAMIC_ALLOCATION */

OC_PROCESS(oc_message_buffer_handler, "OC Message Buffer Handler");
#ifdef OC_INOUT_BUFFER_POOL
OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL);
OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL);
#else /* OC_INOUT_BUFFER_POOL */
#ifdef OC_INOUT_BUFFER_POOL_SIZE
OC_MEMB_STATIC(oc_incoming_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE);
OC_MEMB_STATIC(oc_outgoing_buffers, oc_message_t, OC_INOUT_BUFFER_POOL_SIZE);
#else /* OC_INOUT_BUFFER_POOL_SIZE */
OC_MEMB(oc_incoming_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS);
OC_MEMB(oc_outgoing_buffers, oc_message_t, OC_MAX_NUM_CONCURRENT_REQUESTS);
#endif /* !OC_INOUT_BUFFER_POOL */
#endif /* !OC_INOUT_BUFFER_POOL_SIZE */

static void
message_deallocate(oc_message_t *message, struct oc_memb *pool)
Expand Down
8 changes: 4 additions & 4 deletions api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ dispatch_coap_request(void)
}

bool success = false;
g_dispatch.transaction->message->length =
coap_serialize_message(g_request, g_dispatch.transaction->message->data);
g_dispatch.transaction->message->length = coap_serialize_message(
g_request, g_dispatch.transaction->message->data, oc_message_buffer_size());
if (g_dispatch.transaction->message->length > 0) {
coap_send_transaction(g_dispatch.transaction);

Expand Down Expand Up @@ -265,8 +265,8 @@ oc_do_multicast_update(void)
coap_set_header_content_format(g_request, APPLICATION_VND_OCF_CBOR);
}

g_multicast_update->length =
coap_serialize_message(g_request, g_multicast_update->data);
g_multicast_update->length = coap_serialize_message(
g_request, g_multicast_update->data, oc_message_buffer_size());
if (g_multicast_update->length > 0) {
oc_send_message(g_multicast_update);
} else {
Expand Down
6 changes: 4 additions & 2 deletions api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
****************************************************************************/

#include "oc_server_api_internal.h"
#include "api/oc_buffer_internal.h"
#include "api/oc_ri_internal.h"
#include "messaging/coap/engine.h"
#include "messaging/coap/oc_coap.h"
Expand All @@ -25,6 +25,7 @@
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_core_res_internal.h"
#include "oc_server_api_internal.h"
#include "port/oc_log_internal.h"
#include "util/oc_features.h"
#include "util/oc_macros_internal.h"
Expand Down Expand Up @@ -697,7 +698,8 @@ handle_separate_response_transaction(coap_transaction_t *t,
uint8_t response_code)
{
coap_set_status_code(response, response_code);
t->message->length = coap_serialize_message(response, t->message->data);
t->message->length = coap_serialize_message(response, t->message->data,
oc_message_buffer_size());
if (t->message->length <= 0) {
coap_clear_transaction(t);
return;
Expand Down
Loading

0 comments on commit 61bfade

Please sign in to comment.