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 3, 2023
1 parent 3c4597c commit 50117e4
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 525 deletions.
2 changes: 2 additions & 0 deletions api/oc_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "oc_config.h"
#include "oc_events_internal.h"
#include "oc_signal_event_loop.h"
#include "port/oc_log_internal.h"
#include "port/oc_network_event_handler_internal.h"
#include "util/oc_features.h"
#include "util/oc_macros_internal.h"
Expand All @@ -34,6 +35,7 @@
#include "security/oc_tls_internal.h"
#endif /* OC_SECURITY */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#ifdef OC_DYNAMIC_ALLOCATION
Expand Down
3 changes: 3 additions & 0 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "messaging/coap/constants.h"
#include "messaging/coap/engine.h"
#include "messaging/coap/oc_coap.h"
#include "messaging/coap/transactions.h"
#include "oc_api.h"
#include "oc_buffer.h"
#include "oc_core_res.h"
Expand All @@ -51,6 +52,8 @@
#ifdef OC_SERVER
#include "api/oc_ri_server_internal.h"
#include "api/oc_server_api_internal.h"
#include "messaging/coap/observe.h"
#include "messaging/coap/separate.h"
#ifdef OC_COLLECTIONS
#include "api/oc_collection_internal.h"
#include "api/oc_link_internal.h"
Expand Down
253 changes: 114 additions & 139 deletions messaging/coap/coap_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,26 @@ coap_send_ping_message(const oc_endpoint_t *endpoint, uint8_t custody_option,
}

int
coap_send_pong_message(const oc_endpoint_t *endpoint, void *packet)
coap_send_pong_message(const oc_endpoint_t *endpoint,
const coap_packet_t *packet)
{
if (!endpoint || !packet)
return 0;

coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
coap_packet_t pong_pkt[1];
coap_tcp_init_message(pong_pkt, PONG_7_03);
coap_packet_t pong_pkt;
coap_tcp_init_message(&pong_pkt, PONG_7_03);

coap_set_token(pong_pkt, coap_pkt->token, coap_pkt->token_len);
coap_set_token(&pong_pkt, packet->token, packet->token_len);

if (coap_pkt->custody) {
if (!coap_signal_set_custody(pong_pkt, coap_pkt->custody)) {
if (packet->custody) {
if (!coap_signal_set_custody(&pong_pkt, packet->custody)) {
OC_ERR("coap_signal_set_custody failed");
return 0;
}
}

OC_DBG("send pong signal message.");
return coap_send_signal_message(endpoint, pong_pkt);
return coap_send_signal_message(endpoint, &pong_pkt);
}

int
Expand Down Expand Up @@ -198,28 +198,24 @@ coap_send_abort_message(const oc_endpoint_t *endpoint, uint16_t opt,
return coap_send_signal_message(endpoint, abort_pkt);
}

int
coap_check_signal_message(void *packet)
bool
coap_check_signal_message(const coap_packet_t *packet)
{
if (!packet)
return 0;

coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
if (coap_pkt->code == CSM_7_01 || coap_pkt->code == PING_7_02 ||
coap_pkt->code == PONG_7_03 || coap_pkt->code == RELEASE_7_04 ||
coap_pkt->code == ABORT_7_05)
return 1;
if (!packet) {
return false;
}

return 0;
return packet->code == CSM_7_01 || packet->code == PING_7_02 ||
packet->code == PONG_7_03 || packet->code == RELEASE_7_04 ||
packet->code == ABORT_7_05;
}

int
handle_coap_signal_message(void *packet, const oc_endpoint_t *endpoint)
handle_coap_signal_message(const coap_packet_t *packet,
const oc_endpoint_t *endpoint)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

OC_DBG("Coap signal message received.(code: %d)", coap_pkt->code);
if (coap_pkt->code == CSM_7_01) {
OC_DBG("Coap signal message received.(code: %d)", packet->code);
if (packet->code == CSM_7_01) {
tcp_csm_state_t state = oc_tcp_get_csm_state(endpoint);
if (state == CSM_DONE) {
// TODO: max-message-size, blockwise_transfer handling
Expand All @@ -228,182 +224,161 @@ handle_coap_signal_message(void *packet, const oc_endpoint_t *endpoint)
coap_send_csm_message(endpoint, OC_PDU_SIZE, 0);
}
oc_tcp_update_csm_state(endpoint, CSM_DONE);
} else if (coap_pkt->code == PING_7_02) {
} else if (packet->code == PING_7_02) {
coap_send_pong_message(endpoint, packet);
} else if (coap_pkt->code == PONG_7_03) {
} else if (packet->code == PONG_7_03) {
OC_DBG("Find client cb using token :");
OC_DBG(" [%02X%02X%02X%02X%02X%02X%02X%02X]", coap_pkt->token[0],
coap_pkt->token[1], coap_pkt->token[2], coap_pkt->token[3],
coap_pkt->token[4], coap_pkt->token[5], coap_pkt->token[6],
coap_pkt->token[7]);
} else if (coap_pkt->code == RELEASE_7_04) {
OC_DBG(" [%02X%02X%02X%02X%02X%02X%02X%02X]", packet->token[0],
packet->token[1], packet->token[2], packet->token[3],
packet->token[4], packet->token[5], packet->token[6],
packet->token[7]);
} else if (packet->code == RELEASE_7_04) {
// alternative address
// hold off
oc_connectivity_end_session(endpoint);
} else if (coap_pkt->code == ABORT_7_05) {
OC_WRN("Peer aborted! [code: %d(diagnostic: %*.s)]", coap_pkt->bad_csm_opt,
(int)coap_pkt->payload_len, (char *)coap_pkt->payload);
} else if (packet->code == ABORT_7_05) {
OC_WRN("Peer aborted! [code: %d(diagnostic: %*.s)]", packet->bad_csm_opt,
(int)packet->payload_len, (char *)packet->payload);
}

return COAP_NO_ERROR;
}

int
coap_signal_get_max_msg_size(void *packet, uint32_t *size)
bool
coap_signal_get_max_msg_size(const coap_packet_t *packet, uint32_t *size)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != CSM_7_01 ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_MAX_MSG_SIZE)) {
return 0;
if (packet->code != CSM_7_01 ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_MAX_MSG_SIZE)) {
return false;
}
*size = coap_pkt->max_msg_size;
return 1;
*size = packet->max_msg_size;
return true;
}

int
coap_signal_set_max_msg_size(void *packet, uint32_t size)
bool
coap_signal_set_max_msg_size(coap_packet_t *packet, uint32_t size)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != CSM_7_01) {
return 0;
if (packet->code != CSM_7_01) {
return false;
}
coap_pkt->max_msg_size = size;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_MAX_MSG_SIZE);
return 1;
packet->max_msg_size = size;
SET_OPTION(packet, COAP_SIGNAL_OPTION_MAX_MSG_SIZE);
return true;
}

int
coap_signal_get_blockwise_transfer(void *packet, uint8_t *blockwise_transfer)
bool
coap_signal_get_blockwise_transfer(const coap_packet_t *packet,
uint8_t *blockwise_transfer)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != CSM_7_01 ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER)) {
return 0;
if (packet->code != CSM_7_01 ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER)) {
return false;
}
*blockwise_transfer = coap_pkt->blockwise_transfer;
return 1;
*blockwise_transfer = packet->blockwise_transfer;
return true;
}

int
coap_signal_set_blockwise_transfer(void *packet, uint8_t blockwise_transfer)
bool
coap_signal_set_blockwise_transfer(coap_packet_t *packet,
uint8_t blockwise_transfer)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != CSM_7_01 ||
if (packet->code != CSM_7_01 ||
(blockwise_transfer != 0 && blockwise_transfer != 1)) {
return 0;
return false;
}
coap_pkt->blockwise_transfer = blockwise_transfer;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER);
return 1;
packet->blockwise_transfer = blockwise_transfer;
SET_OPTION(packet, COAP_SIGNAL_OPTION_BLOCKWISE_TRANSFER);
return true;
}

int
coap_signal_get_custody(void *packet, uint8_t *custody)
bool
coap_signal_get_custody(const coap_packet_t *packet, uint8_t *custody)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if ((coap_pkt->code != PING_7_02 && coap_pkt->code != PONG_7_03) ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_CUSTODY)) {
return 0;
if ((packet->code != PING_7_02 && packet->code != PONG_7_03) ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_CUSTODY)) {
return false;
}
*custody = coap_pkt->custody;
return 1;
*custody = packet->custody;
return true;
}

int
coap_signal_set_custody(void *packet, uint8_t custody)
bool
coap_signal_set_custody(coap_packet_t *packet, uint8_t custody)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if ((coap_pkt->code != PING_7_02 && coap_pkt->code != PONG_7_03) ||
if ((packet->code != PING_7_02 && packet->code != PONG_7_03) ||
(custody != 0 && custody != 1)) {
return 0;
return false;
}
coap_pkt->custody = custody;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_CUSTODY);
return 1;
packet->custody = custody;
SET_OPTION(packet, COAP_SIGNAL_OPTION_CUSTODY);
return true;
}

size_t
coap_signal_get_alt_addr(void *packet, const char **addr)
coap_signal_get_alt_addr(const coap_packet_t *packet, const char **addr)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != RELEASE_7_04 ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_ALT_ADDR)) {
if (packet->code != RELEASE_7_04 ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_ALT_ADDR)) {
return 0;
}
*addr = coap_pkt->alt_addr;
return coap_pkt->alt_addr_len;
*addr = packet->alt_addr;
return packet->alt_addr_len;
}

size_t
coap_signal_set_alt_addr(void *packet, const char *addr, size_t addr_len)
coap_signal_set_alt_addr(coap_packet_t *packet, const char *addr,
size_t addr_len)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != RELEASE_7_04 || !addr || addr_len <= 0) {
if (packet->code != RELEASE_7_04 || !addr || addr_len <= 0) {
return 0;
}
coap_pkt->alt_addr = addr;
coap_pkt->alt_addr_len = addr_len;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_ALT_ADDR);
return coap_pkt->alt_addr_len;
packet->alt_addr = addr;
packet->alt_addr_len = addr_len;
SET_OPTION(packet, COAP_SIGNAL_OPTION_ALT_ADDR);
return packet->alt_addr_len;
}

int
coap_signal_get_hold_off(void *packet, uint32_t *time_seconds)
bool
coap_signal_get_hold_off(const coap_packet_t *packet, uint32_t *time_seconds)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != RELEASE_7_04 ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_HOLD_OFF)) {
return 0;
if (packet->code != RELEASE_7_04 ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_HOLD_OFF)) {
return false;
}
*time_seconds = coap_pkt->hold_off;
return 1;
*time_seconds = packet->hold_off;
return true;
}

int
coap_signal_set_hold_off(void *packet, uint32_t time_seconds)
bool
coap_signal_set_hold_off(coap_packet_t *packet, uint32_t time_seconds)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != RELEASE_7_04) {
return 0;
if (packet->code != RELEASE_7_04) {
return false;
}
coap_pkt->hold_off = time_seconds;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_HOLD_OFF);
return 1;
packet->hold_off = time_seconds;
SET_OPTION(packet, COAP_SIGNAL_OPTION_HOLD_OFF);
return true;
}

int
coap_signal_get_bad_csm(void *packet, uint16_t *opt)
bool
coap_signal_get_bad_csm(const coap_packet_t *packet, uint16_t *opt)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != ABORT_7_05 ||
!IS_OPTION(coap_pkt, COAP_SIGNAL_OPTION_BAD_CSM)) {
return 0;
if (packet->code != ABORT_7_05 ||
!IS_OPTION(packet, COAP_SIGNAL_OPTION_BAD_CSM)) {
return false;
}
*opt = coap_pkt->bad_csm_opt;
return 1;
*opt = packet->bad_csm_opt;
return true;
}

int
coap_signal_set_bad_csm(void *packet, uint16_t opt)
bool
coap_signal_set_bad_csm(coap_packet_t *packet, uint16_t opt)
{
coap_packet_t *const coap_pkt = (coap_packet_t *)packet;

if (coap_pkt->code != ABORT_7_05) {
return 0;
if (packet->code != ABORT_7_05) {
return false;
}
coap_pkt->bad_csm_opt = opt;
SET_OPTION(coap_pkt, COAP_SIGNAL_OPTION_BAD_CSM);
return 1;
packet->bad_csm_opt = opt;
SET_OPTION(packet, COAP_SIGNAL_OPTION_BAD_CSM);
return true;
}
#endif /* OC_TCP */
Loading

0 comments on commit 50117e4

Please sign in to comment.