Skip to content

Commit

Permalink
fixup! api: refactor coap_receive
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Aug 6, 2023
1 parent 50117e4 commit 8e21f5d
Show file tree
Hide file tree
Showing 11 changed files with 899 additions and 469 deletions.
23 changes: 18 additions & 5 deletions api/oc_blockwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#ifdef OC_BLOCK_WISE

#include "api/oc_blockwise_internal.h"
#include "api/oc_helpers_internal.h"
#include "messaging/coap/coap.h"
#include "oc_blockwise.h"
Expand Down Expand Up @@ -246,7 +247,7 @@ oc_blockwise_scrub_buffers_for_client_cb(const void *cb)
#endif /* OC_CLIENT */

void
oc_blockwise_scrub_buffers(bool all)
oc_blockwise_free_all_request_buffers(bool all)
{
oc_blockwise_state_t *buffer =
(oc_blockwise_state_t *)oc_list_head(oc_blockwise_requests);
Expand All @@ -257,8 +258,13 @@ oc_blockwise_scrub_buffers(bool all)
}
buffer = next;
}
}

buffer = (oc_blockwise_state_t *)oc_list_head(oc_blockwise_responses);
void
oc_blockwise_free_all_response_buffers(bool all)
{
oc_blockwise_state_t *buffer =
(oc_blockwise_state_t *)oc_list_head(oc_blockwise_responses);
while (buffer != NULL) {
oc_blockwise_state_t *next = buffer->next;
if (buffer->ref_count == 0 || all) {
Expand All @@ -268,6 +274,13 @@ oc_blockwise_scrub_buffers(bool all)
}
}

void
oc_blockwise_scrub_buffers(bool all)
{
oc_blockwise_free_all_request_buffers(all);
oc_blockwise_free_all_response_buffers(all);
}

#ifdef OC_CLIENT
static oc_blockwise_state_t *
oc_blockwise_find_buffer_by_token(oc_list_t list, const uint8_t *token,
Expand Down Expand Up @@ -403,9 +416,9 @@ oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, uint32_t block_offset,
uint32_t *payload_size)
{
if (block_offset < buffer->payload_size) {
if (buffer->payload_size < requested_block_size)
*payload_size = (uint32_t)buffer->payload_size;
else {
if (buffer->payload_size < requested_block_size) {
*payload_size = buffer->payload_size;
} else {
*payload_size = MIN(requested_block_size,
(uint32_t)(buffer->payload_size - block_offset));
}
Expand Down
52 changes: 52 additions & 0 deletions api/oc_blockwise_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/****************************************************************************
*
* Copyright (c) 2023 plgd.dev s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/

#ifndef OC_BLOCKWISE_INTERNAL_H
#define OC_BLOCKWISE_INTERNAL_H

#include "oc_config.h"

#ifdef OC_BLOCK_WISE

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief free all request blocks that are handled (refcount = 0)
*
* @param all including ref count != 0
*/
void oc_blockwise_free_all_request_buffers(bool all);

/**
* @brief free all response blocks that are handled (refcount = 0)
*
* @param all including ref count != 0
*/
void oc_blockwise_free_all_response_buffers(bool all);

#ifdef __cplusplus
}
#endif

#endif /* OC_BLOCK_WISE */

#endif /* OC_BLOCKWISE_INTERNAL_H */
15 changes: 8 additions & 7 deletions api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dispatch_coap_request(void)
g_request_buffer->payload_size = (uint32_t)payload_size;
uint32_t block_size;
#ifdef OC_TCP
if (!(g_dispatch.transaction->message->endpoint.flags & TCP) &&
if ((g_dispatch.transaction->message->endpoint.flags & TCP) == 0 &&
payload_size > OC_BLOCK_SIZE) {
#else /* OC_TCP */
if ((long)payload_size > OC_BLOCK_SIZE) {
Expand Down Expand Up @@ -445,12 +445,13 @@ oc_do_get_with_timeout(const char *uri, const oc_endpoint_t *endpoint,
qos, user_data, NULL, NULL);
}

// preparation step for sending coap request using async methods (POST or PUT)
static bool
bool
oc_init_async_request(oc_method_t method, const char *uri,
const oc_endpoint_t *endpoint, const char *query,
oc_response_handler_t handler, oc_qos_t qos,
void *user_data)
void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data)
{
oc_client_handler_t client_handler = {
.response = handler,
Expand All @@ -464,7 +465,7 @@ oc_init_async_request(oc_method_t method, const char *uri,
return false;
}

if (!prepare_coap_request(cb, NULL, NULL)) {
if (!prepare_coap_request(cb, configure_request, configure_request_data)) {
oc_client_cb_free(cb);
return false;
}
Expand Down Expand Up @@ -497,15 +498,15 @@ oc_init_put(const char *uri, const oc_endpoint_t *endpoint, const char *query,
oc_response_handler_t handler, oc_qos_t qos, void *user_data)
{
return oc_init_async_request(OC_PUT, uri, endpoint, query, handler, qos,
user_data);
user_data, NULL, NULL);
}

bool
oc_init_post(const char *uri, const oc_endpoint_t *endpoint, const char *query,
oc_response_handler_t handler, oc_qos_t qos, void *user_data)
{
return oc_init_async_request(OC_POST, uri, endpoint, query, handler, qos,
user_data);
user_data, NULL, NULL);
}

bool
Expand Down
10 changes: 10 additions & 0 deletions api/oc_client_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "oc_client_state.h"
#include "oc_ri.h"

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -39,6 +41,14 @@ oc_client_cb_t *oc_do_request(oc_method_t method, const char *uri,
coap_configure_request_fn_t configure_request,
void *configure_request_data);

// preparation step for sending coap request using async methods (POST or PUT)
bool oc_init_async_request(oc_method_t method, const char *uri,
const oc_endpoint_t *endpoint, const char *query,
oc_response_handler_t handler, oc_qos_t qos,
void *user_data,
coap_configure_request_fn_t configure_request,
void *configure_request_data);

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 7 additions & 1 deletion api/oc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ oc_main_poll(void)
void
oc_main_shutdown(void)
{
if (g_initialized == false)
if (g_initialized == false) {
return;
}

g_initialized = false;

Expand Down Expand Up @@ -501,5 +502,10 @@ oc_set_drop_commands(size_t device, bool drop)
bool
oc_drop_command(size_t device)
{
#ifdef OC_DYNAMIC_ALLOCATION
if (g_drop_commands == NULL) {
return false;
}
#endif /* OC_DYNAMIC_ALLOCATION */
return g_drop_commands[device];
}
Loading

0 comments on commit 8e21f5d

Please sign in to comment.