Skip to content

Commit

Permalink
Added "session" handle report to the callbacks in gateway "C" interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Apr 30, 2024
1 parent 0a3bcce commit 09cc0bf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 67 deletions.
19 changes: 1 addition & 18 deletions client/include/cc_mqttsn_client/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,8 @@

#ifdef __cplusplus
extern "C" {
#else

#ifdef WIN32
#ifndef bool
#define bool char
#endif

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

#else // #ifdef WIN32
#else // #ifdef __cplusplus
#include <stdbool.h>
#endif // #ifdef WIN32

#endif // #ifdef __cplusplus

/// @brief Major verion of the library
Expand Down
19 changes: 10 additions & 9 deletions gateway/doc/session.dox
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// When using @b C++ interface, just instantiate object of cc_mqttsn_gateway::Session
/// class. The destruction of the object will clean up all acquired resources.
/// @code
/// cc_mqttsn_gateway::Session* session = new cc_mqttsn_gateway::Session(); // make sure to use smart pointer in production code
/// std::unique_ptr<cc_mqttsn_gateway::Session> session(new cc_mqttsn_gateway::Session());
/// @endcode
///
/// When using @b C interface, the allocation is performed using cc_mqttsn_gw_session_alloc()
Expand Down Expand Up @@ -94,7 +94,7 @@
///
/// @b C interface:
/// @code
/// void my_send_to_client(void* userData, const unsigned char* buf, unsigned bufLen)
/// void my_send_to_client(void* userData, CC_MqttsnSessionHandle session, const unsigned char* buf, unsigned bufLen)
/// {
/// ...
/// }
Expand All @@ -114,7 +114,7 @@
///
/// @b C interface:
/// @code
/// void my_send_to_broker(void* userData, const unsigned char* buf, unsigned bufLen)
/// void my_send_to_broker(void* userData, CC_MqttsnSessionHandle session, const unsigned char* buf, unsigned bufLen)
/// {
/// ...
/// }
Expand All @@ -134,12 +134,12 @@
/// {
/// ... // Set timer to expire after duration milliseconds
/// // After expiry call session.tick()
/// };
/// });
/// @endcode
///
/// @b C interface:
/// @code
/// void my_tick_req(void* userData, unsigned duration)
/// void my_tick_req(void* userData, CC_MqttsnSessionHandle session, unsigned duration)
/// {
/// ... /* Set timer to expire after duration milliseconds */
/// ... /* After expiry call cc_mqttsn_gw_session_tick() */
Expand Down Expand Up @@ -178,7 +178,7 @@
///
/// @b C interface:
/// @code
/// unsigned my_cancel_timer_req(void* userData)
/// unsigned my_cancel_timer_req(void* userData, CC_MqttsnSessionHandle session)
/// {
/// ... /* cancel timer */
/// return ...; /* return number of elapsed milliseconds */
Expand All @@ -205,7 +205,7 @@
///
/// @b C interface:
/// @code
/// void my_session_term(void* userData)
/// void my_session_term(void* userData, CC_MqttsnSessionHandle session)
/// {
/// ... /* Remove reference to session object from internal data structures */
/// cc_mqttsn_gw_session_free(handle);
Expand Down Expand Up @@ -244,7 +244,7 @@
///
/// @b C interface
/// @code
/// void my_broker_reconnect(void* userData)
/// void my_broker_reconnect(void* userData, CC_MqttsnSessionHandle session)
/// {
/// ... /* Close existing TCP/IP connection to broker and open a new one */
/// }
Expand Down Expand Up @@ -432,6 +432,7 @@
/// @code
/// void my_auth_callback(
/// void* userData,
/// CC_MqttsnSessionHandle session,
/// const char* clientId,
/// const char** username,
/// const unsigned char** password,
Expand Down Expand Up @@ -465,7 +466,7 @@
///
/// @b C interface:
/// @code
/// void my_client_connect_report(void* userData, const char* clientId)
/// void my_client_connect_report(void* userData, CC_MqttsnSessionHandle session, const char* clientId)
/// {
/// if (!cc_mqttsn_gw_session_add_predefined_topic(handle, "client/specific/predefined/topic", 2222)) {
/// ... /* report error */
Expand Down
39 changes: 15 additions & 24 deletions gateway/include/cc_mqttsn_gateway/gateway_all.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,8 @@

#ifdef __cplusplus
extern "C" {
#else

#ifdef WIN32
#ifndef bool
#define bool char
#endif

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

#else // #ifdef WIN32
#else // #ifdef __cplusplus
#include <stdbool.h>
#endif // #ifdef WIN32

#endif // #ifdef __cplusplus

/*===================== Gateway Object ======================*/
Expand Down Expand Up @@ -135,55 +118,63 @@ typedef struct
/// @details When the requested time is due, the driving code is expected
/// to call cc_mqttsn_gw_session_tick() member function.
/// @param[in] userData User data passed as the last parameter to the setting function.
/// @param[in] session Handle of session performing the request
/// @param[in] duration Number of @b milliseconds to measure.
typedef void (*CC_MqttsnSessionTickReqCb)(void* userData, unsigned duration);
typedef void (*CC_MqttsnSessionTickReqCb)(void* userData, CC_MqttsnSessionHandle session, unsigned duration);

/// @brief Type of callback, used to cancel existing time measurement.
/// @details When invoked the existing time measurement needs to be cancelled.
/// The function also needs to return amount of @b milliseconds elapsed
/// since last timer programming request.
/// @param[in] userData User data passed as the last parameter to the setting function.
/// @param[in] session Handle of session performing the request
/// @return Number of elapsed @b milliseconds since last timer programming
/// request.
typedef unsigned (*CC_MqttsnSessionCancelTickReqCb)(void* userData);
typedef unsigned (*CC_MqttsnSessionCancelTickReqCb)(void* userData, CC_MqttsnSessionHandle session);

/// @brief Type of callback, used to request delivery of serialised message
/// to the client or broker.
/// @param[in] userData User data passed as the last parameter to the setting function.
/// @param[in] session Handle of session performing the request
/// @param[in] buf Buffer containing serialised message.
/// @param[in] bufLen Number of bytes in the buffer
typedef void (*CC_MqttsnSessionSendDataReqCb)(void* userData, const unsigned char* buf, unsigned bufLen);
typedef void (*CC_MqttsnSessionSendDataReqCb)(void* userData, CC_MqttsnSessionHandle session, const unsigned char* buf, unsigned bufLen);

/// @brief Type of callback, used to request session termination.
/// @details When the callback is invoked, the driving code must flush
/// all the previously sent messages to appropriate I/O links and
/// delete this session object.
/// @param[in] userData User data passed as the last parameter to the setting function.
typedef void (*CC_MqttsnSessionTermReqCb)(void* userData);
/// @param[in] session Handle of session performing the request
typedef void (*CC_MqttsnSessionTermReqCb)(void* userData, CC_MqttsnSessionHandle session);

/// @brief Type of callback used to request reconnection to the broker.
/// @details When the callback is invoked, the driving code must close
/// existing TCP/IP connection to the broker and create a new one.
/// @param[in] userData User data passed as the last parameter to the setting function.
typedef void (*CC_MqttsnSessionBrokerReconnectReqCb)(void* userData);
/// @param[in] session Handle of session performing the request
typedef void (*CC_MqttsnSessionBrokerReconnectReqCb)(void* userData, CC_MqttsnSessionHandle session);

/// @brief Type of callback used to report client ID of the newly connected
/// MQTT-SN client.
/// @details The callback can be used to provide additional client specific
/// information, such as predefined topic IDs.
/// @param[in] userData User data passed as the last parameter to the setting function.
/// @param[in] session Handle of session performing the report
/// @param[in] clientId Client ID
typedef void (*CC_MqttsnSessionClientConnectReportCb)(void* userData, const char* clientId);
typedef void (*CC_MqttsnSessionClientConnectReportCb)(void* userData, CC_MqttsnSessionHandle session, const char* clientId);

/// @brief Type of callback used to request authentication information of
/// the client that is trying to connect.
/// @param[in] userData User data passed as the last parameter to the setting function.
/// @param[in] session Handle of session performing the request
/// @param[in] clientId Client ID
/// @param[out] username Username string
/// @param[out] password Binary password buffer
/// @param[out] passwordLen Length of the binary password
typedef void (*CC_MqttsnSessionAuthInfoReqCb)(
void* userData,
CC_MqttsnSessionHandle session,
const char* clientId,
const char** username,
const unsigned char** password,
Expand Down
32 changes: 16 additions & 16 deletions gateway/src/lib/gateway_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ void cc_mqttsn_gw_session_set_tick_req_cb(
}

reinterpret_cast<Session*>(session.obj)->setNextTickProgramReqCb(
[cb, data](unsigned duration)
[cb, data, session](unsigned duration)
{
cb(data, duration);
cb(data, session, duration);
});
}

Expand All @@ -154,9 +154,9 @@ void cc_mqttsn_gw_session_set_cancel_tick_cb(
}

reinterpret_cast<Session*>(session.obj)->setCancelTickWaitReqCb(
[cb, data]() -> unsigned
[cb, data, session]() -> unsigned
{
return cb(data);
return cb(data, session);
});
}

Expand All @@ -170,9 +170,9 @@ void cc_mqttsn_gw_session_set_send_data_to_client_cb(
}

reinterpret_cast<Session*>(session.obj)->setSendDataClientReqCb(
[cb, data](const std::uint8_t* buf, std::size_t bufLen)
[cb, data, session](const std::uint8_t* buf, std::size_t bufLen)
{
cb(data, buf, static_cast<unsigned>(bufLen));
cb(data, session, buf, static_cast<unsigned>(bufLen));
});
}

Expand All @@ -187,9 +187,9 @@ void cc_mqttsn_gw_session_set_send_data_to_broker_cb(
}

reinterpret_cast<Session*>(session.obj)->setSendDataBrokerReqCb(
[cb, data](const std::uint8_t* buf, std::size_t bufLen)
[cb, data, session](const std::uint8_t* buf, std::size_t bufLen)
{
cb(data, buf, static_cast<unsigned>(bufLen));
cb(data, session, buf, static_cast<unsigned>(bufLen));
});
}

Expand All @@ -203,9 +203,9 @@ void cc_mqttsn_gw_session_set_term_req_cb(
}

reinterpret_cast<Session*>(session.obj)->setTerminationReqCb(
[cb, data]()
[cb, data, session]()
{
cb(data);
cb(data, session);
});
}

Expand All @@ -219,9 +219,9 @@ void cc_mqttsn_gw_session_set_broker_reconnect_req_cb(
}

reinterpret_cast<Session*>(session.obj)->setBrokerReconnectReqCb(
[cb, data]()
[cb, data, session]()
{
cb(data);
cb(data, session);
});
}

Expand All @@ -240,9 +240,9 @@ void cc_mqttsn_gw_session_set_client_connect_report_cb(
}

reinterpret_cast<Session*>(session.obj)->setClientConnectedReportCb(
[cb, data](const std::string& clientId)
[cb, data, session](const std::string& clientId)
{
cb(data, clientId.c_str());
cb(data, session, clientId.c_str());
});
}

Expand All @@ -261,12 +261,12 @@ void cc_mqttsn_gw_session_set_auth_info_req_cb(
}

reinterpret_cast<Session*>(session.obj)->setAuthInfoReqCb(
[cb, data](const std::string& clientId) -> Session::AuthInfo
[cb, data, session](const std::string& clientId) -> Session::AuthInfo
{
const char* username = nullptr;
const std::uint8_t* password = nullptr;
unsigned passLen = 0U;
cb(data, clientId.c_str(), &username, &password, &passLen);
cb(data, session, clientId.c_str(), &username, &password, &passLen);

Session::AuthInfo info;
if (username != nullptr) {
Expand Down

0 comments on commit 09cc0bf

Please sign in to comment.