Skip to content

Commit

Permalink
Saving work on the new client application.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed May 23, 2024
1 parent 196593a commit 18bf971
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 103 deletions.
1 change: 1 addition & 0 deletions client/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function (gen_lib_mqttsn_client config_file)

message (STATUS "Defining library ${lib_name}")
set (src
src/op/Op.cpp
src/ClientImpl.cpp
src/TimerMgr.cpp
)
Expand Down
13 changes: 13 additions & 0 deletions client/lib/include/cc_mqttsn_client/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ typedef struct
{
unsigned char gwId; ///< Gateway ID
CC_MqttsnGwStatus status; ///< Gateway status
const unsigned char* m_addr; ///< Gateway address, NULL if not known
unsigned m_addrLen; ///< Length of the gateway address, 0 if not known.
} CC_MqttsnGatewayInfo;

/// @brief Callback used to request time measurement.
Expand Down Expand Up @@ -199,8 +201,19 @@ typedef void (*CC_MqttsnSubscribeCompleteReportCb)(void* data, CC_MqttsnAsyncOpS
/// @param[in] msgInfo Information about incoming message.
typedef void (*CC_MqttsnMessageReportCb)(void* data, const CC_MqttsnMessageInfo* msgInfo);

/// @brief Callback used to report discovered errors.
/// @param[in] data Pointer to user data object, passed as the last parameter to
/// the request call.
/// @param[in] msg Error log message.
/// @ingroup client
typedef void (*CC_MqttsnErrorLogCb)(void* data, const char* msg);

/// @brief Callback used to request delay (in ms) to wait before
/// responding with @b GWINFO message on behalf of a gateway.
/// @details In case function return 0U, the response on behalf of the gateway is disabled.
/// @return Number of milliseconds to wait for another @b GWINFO to cancel the intended send of @b GWINFO on behalf of the gateway.
typedef unsigned (*CC_MqttsnGwinfoDelayRequestCb)(void* data);

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion client/lib/script/DefineDefaultConfigVars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set_default_var_value(CC_MQTTSN_CLIENT_ALLOC_LIMIT 0)
#set_default_var_value(CC_MQTTSN_CLIENT_PASSWORD_FIELD_FIXED_LEN 0)
#set_default_var_value(CC_MQTTSN_CLIENT_TOPIC_FIELD_FIXED_LEN 0)
#set_default_var_value(CC_MQTTSN_CLIENT_BIN_DATA_FIELD_FIXED_LEN 0)
#set_default_var_value(CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE 0)
set_default_var_value(CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE 0)
set_default_var_value(CC_MQTTSN_CLIENT_RECEIVE_MAX_LIMIT 0)
set_default_var_value(CC_MQTTSN_CLIENT_SEND_MAX_LIMIT 0)
set_default_var_value(CC_MQTTSN_CLIENT_ASYNC_SUBS_LIMIT 0)
Expand Down
2 changes: 1 addition & 1 deletion client/lib/script/WriteConfigHeader.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ replace_in_text (CC_MQTTSN_CLIENT_HAS_DYN_MEM_ALLOC_CPP)
replace_in_text (CC_MQTTSN_CLIENT_ALLOC_LIMIT)
#replace_in_text (CC_MQTTSN_CLIENT_STRING_FIELD_FIXED_LEN)
#replace_in_text (CC_MQTTSN_CLIENT_BIN_DATA_FIELD_FIXED_LEN)
#replace_in_text (CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE)
replace_in_text (CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE)
replace_in_text (CC_MQTTSN_CLIENT_RECEIVE_MAX_LIMIT)
replace_in_text (CC_MQTTSN_CLIENT_SEND_MAX_LIMIT)
replace_in_text (CC_MQTTSN_CLIENT_ASYNC_SUBS_LIMIT)
Expand Down
8 changes: 4 additions & 4 deletions client/lib/script/WriteProtocolOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ endmacro()
#set_default_opt (MESSAGE_SUBSCRIBE_FIELDS_LIST)
#set_default_opt (MESSAGE_UNSUBSCRIBE_FIELDS_LIST)

#set_default_opt (MAX_PACKET_SIZE)
set_default_opt (MAX_PACKET_SIZE)
#set_default_opt (MSG_ALLOC_OPT)

#########################################
Expand All @@ -64,9 +64,9 @@ endif ()
# set (FIELD_STRING "comms::option::app::FixedSizeStorage<${CC_MQTTSN_CLIENT_STRING_FIELD_FIXED_LEN}>")
#endif ()

#if (NOT ${CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE} EQUAL 0)
# set (MAX_PACKET_SIZE "comms::option::app::FixedSizeStorage<${CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE}>")
#endif ()
if (NOT ${CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE} EQUAL 0)
set (MAX_PACKET_SIZE "comms::option::app::FixedSizeStorage<${CC_MQTTSN_CLIENT_MAX_OUTPUT_PACKET_SIZE}>")
endif ()

#if (NOT ${CC_MQTTSN_CLIENT_CLIENT_ID_FIELD_FIXED_LEN} EQUAL 0)
# set (MESSAGE_CONNECT_FIELDS_CLIENT_ID "comms::option::app::FixedSizeStorage<${CC_MQTTSN_CLIENT_CLIENT_ID_FIELD_FIXED_LEN}>")
Expand Down
106 changes: 53 additions & 53 deletions client/lib/src/ClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,67 +523,67 @@ void ClientImpl::processData(const std::uint8_t* iter, unsigned len)
// }
// }

// CC_MqttsnErrorCode ClientImpl::sendMessage(const ProtMessage& msg)
// {
// auto len = m_frame.length(msg);
CC_MqttsnErrorCode ClientImpl::sendMessage(const ProtMessage& msg, unsigned broadcastRadius)
{
auto len = m_frame.length(msg);

// if (m_buf.max_size() < len) {
// errorLog("Output buffer overflow.");
// return CC_MqttsnErrorCode_BufferOverflow;
// }
if (m_buf.max_size() < len) {
errorLog("Output buffer overflow.");
return CC_MqttsnErrorCode_BufferOverflow;
}

// m_buf.resize(len);
// auto writeIter = comms::writeIteratorFor<ProtMessage>(&m_buf[0]);
// auto es = m_frame.write(msg, writeIter, len);
// COMMS_ASSERT(es == comms::ErrorStatus::Success);
// if (es != comms::ErrorStatus::Success) {
// errorLog("Failed to serialize output message.");
// return CC_MqttsnErrorCode_InternalError;
// }
m_buf.resize(len);
auto writeIter = comms::writeIteratorFor<ProtMessage>(&m_buf[0]);
auto es = m_frame.write(msg, writeIter, len);
COMMS_ASSERT(es == comms::ErrorStatus::Success);
if (es != comms::ErrorStatus::Success) {
errorLog("Failed to serialize output message.");
return CC_MqttsnErrorCode_InternalError;
}

// COMMS_ASSERT(m_sendOutputDataCb != nullptr);
// m_sendOutputDataCb(m_sendOutputDataData, &m_buf[0], static_cast<unsigned>(len));
COMMS_ASSERT(m_sendOutputDataCb != nullptr);
m_sendOutputDataCb(m_sendOutputDataData, &m_buf[0], static_cast<unsigned>(len), broadcastRadius);

// for (auto& opPtr : m_keepAliveOps) {
// opPtr->messageSent();
// }
// for (auto& opPtr : m_keepAliveOps) {
// opPtr->messageSent();
// }

// return CC_MqttsnErrorCode_Success;
// }
return CC_MqttsnErrorCode_Success;
}

// void ClientImpl::opComplete(const op::Op* op)
// {
// auto iter = std::find(m_ops.begin(), m_ops.end(), op);
// COMMS_ASSERT(iter != m_ops.end());
// if (iter == m_ops.end()) {
// return;
// }
void ClientImpl::opComplete(const op::Op* op)
{
auto iter = std::find(m_ops.begin(), m_ops.end(), op);
COMMS_ASSERT(iter != m_ops.end());
if (iter == m_ops.end()) {
return;
}

// *iter = nullptr;
// m_opsDeleted = true;

// using ExtraCompleteFunc = void (ClientImpl::*)(const op::Op*);
// static const ExtraCompleteFunc Map[] = {
// /* Type_Connect */ &ClientImpl::opComplete_Connect,
// /* Type_KeepAlive */ &ClientImpl::opComplete_KeepAlive,
// /* Type_Disconnect */ &ClientImpl::opComplete_Disconnect,
// /* Type_Subscribe */ &ClientImpl::opComplete_Subscribe,
// /* Type_Unsubscribe */ &ClientImpl::opComplete_Unsubscribe,
// /* Type_Recv */ &ClientImpl::opComplete_Recv,
// /* Type_Send */ &ClientImpl::opComplete_Send,
// };
// static const std::size_t MapSize = std::extent<decltype(Map)>::value;
// static_assert(MapSize == op::Op::Type_NumOfValues);

// auto idx = static_cast<unsigned>(op->type());
// COMMS_ASSERT(idx < MapSize);
// if (MapSize <= idx) {
// return;
// }
*iter = nullptr;
m_opsDeleted = true;

using ExtraCompleteFunc = void (ClientImpl::*)(const op::Op*);
static const ExtraCompleteFunc Map[] = {
// /* Type_Connect */ &ClientImpl::opComplete_Connect,
// /* Type_KeepAlive */ &ClientImpl::opComplete_KeepAlive,
// /* Type_Disconnect */ &ClientImpl::opComplete_Disconnect,
// /* Type_Subscribe */ &ClientImpl::opComplete_Subscribe,
// /* Type_Unsubscribe */ &ClientImpl::opComplete_Unsubscribe,
// /* Type_Recv */ &ClientImpl::opComplete_Recv,
// /* Type_Send */ &ClientImpl::opComplete_Send,
};
static const std::size_t MapSize = std::extent<decltype(Map)>::value;
static_assert(MapSize == op::Op::Type_NumOfValues);

auto idx = static_cast<unsigned>(op->type());
COMMS_ASSERT(idx < MapSize);
if (MapSize <= idx) {
return;
}

// auto func = Map[idx];
// (this->*func)(op);
// }
auto func = Map[idx];
(this->*func)(op);
}

// void ClientImpl::brokerConnected(bool sessionPresent)
// {
Expand Down
25 changes: 17 additions & 8 deletions client/lib/src/ClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// #include "op/ConnectOp.h"
// #include "op/DisconnectOp.h"
// #include "op/KeepAliveOp.h"
// #include "op/Op.h"
#include "op/Op.h"
// #include "op/RecvOp.h"
// #include "op/SendOp.h"
// #include "op/SubscribeOp.h"
Expand Down Expand Up @@ -132,6 +132,12 @@ class ClientImpl final : public ProtMsgHandler
m_errorLogData = data;
}

void setGwinfoDelayReqCb(CC_MqttsnGwinfoDelayRequestCb cb, void* data)
{
m_gwinfoDelayReqCb = cb;
m_gwinfoDelayReqData = data;
}

// -------------------- Message Handling -----------------------------

// using Base::handle;
Expand All @@ -151,8 +157,8 @@ class ClientImpl final : public ProtMsgHandler

// -------------------- Ops Access API -----------------------------

// CC_MqttsnErrorCode sendMessage(const ProtMessage& msg);
// void opComplete(const op::Op* op);
CC_MqttsnErrorCode sendMessage(const ProtMessage& msg, unsigned broadcastRadius = 0);
void opComplete(const op::Op* op);
// void brokerConnected(bool sessionPresent);
// void brokerDisconnected(
// CC_MqttsnBrokerDisconnectReason reason = CC_MqttsnBrokerDisconnectReason_ValuesLimit,
Expand Down Expand Up @@ -236,9 +242,9 @@ class ClientImpl final : public ProtMsgHandler
// using SendOpAlloc = ObjAllocator<op::SendOp, ExtConfig::SendOpsLimit>;
// using SendOpsList = ObjListType<SendOpAlloc::Ptr, ExtConfig::SendOpsLimit>;

// using OpPtrsList = ObjListType<op::Op*, ExtConfig::OpsLimit>;
using OpPtrsList = ObjListType<op::Op*, ExtConfig::OpsLimit>;
// using OpToDeletePtrsList = ObjListType<const op::Op*, ExtConfig::OpsLimit>;
// using OutputBuf = ObjListType<std::uint8_t, ExtConfig::MaxOutputPacketSize>;
using OutputBuf = ObjListType<std::uint8_t, ExtConfig::MaxOutputPacketSize>;

// enum TerminateMode
// {
Expand Down Expand Up @@ -291,6 +297,9 @@ class ClientImpl final : public ProtMsgHandler
CC_MqttsnErrorLogCb m_errorLogCb = nullptr;
void* m_errorLogData = nullptr;

CC_MqttsnGwinfoDelayRequestCb m_gwinfoDelayReqCb = nullptr;
void* m_gwinfoDelayReqData = nullptr;

ConfigState m_configState;
// ClientState m_clientState;
// SessionState m_sessionState;
Expand All @@ -299,7 +308,7 @@ class ClientImpl final : public ProtMsgHandler
TimerMgr m_timerMgr;
unsigned m_apiEnterCount = 0U;

// OutputBuf m_buf;
OutputBuf m_buf;

ProtFrame m_frame;

Expand All @@ -324,8 +333,8 @@ class ClientImpl final : public ProtMsgHandler
// SendOpAlloc m_sendOpsAlloc;
// SendOpsList m_sendOps;

// OpPtrsList m_ops;
// bool m_opsDeleted = false;
OpPtrsList m_ops;
bool m_opsDeleted = false;
// bool m_preparationLocked = false;
};

Expand Down
4 changes: 4 additions & 0 deletions client/lib/src/ConfigState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ struct ConfigState
{
static constexpr unsigned DefaultResponseTimeoutMs = 10000;
static constexpr unsigned DefaultRetryCount = 3U;
static constexpr unsigned DefaultBroadcastRadius = 3U;
static constexpr unsigned MaxBroadcastRadius = 255U;

unsigned m_responseTimeoutMs = DefaultResponseTimeoutMs;
unsigned m_retryCount = DefaultRetryCount;
unsigned m_broadcastRadius = DefaultBroadcastRadius;
// CC_MqttsnPublishOrdering m_publishOrdering = CC_MqttsnPublishOrdering_SameQos;
// bool m_verifyOutgoingTopic = Config::HasTopicFormatVerification;
// bool m_verifyIncomingTopic = Config::HasTopicFormatVerification;
Expand Down
Loading

0 comments on commit 18bf971

Please sign in to comment.