Skip to content

Commit

Permalink
Changed logic for VSCP client callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
grodansparadis committed May 8, 2024
1 parent 857c5fa commit 6e5c5f2
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 184 deletions.
190 changes: 95 additions & 95 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,103 +252,103 @@ endif()

if (WIN32)
add_executable(vscpd
src/vscp/daemon/vscpd.cpp
src/vscp/daemon/controlobject.h
src/vscp/daemon/controlobject.cpp
src/vscp/daemon/devicethread.h
src/vscp/daemon/devicethread.cpp
src/vscp/daemon/devicelist.h
src/vscp/daemon/devicelist.cpp
src/vscp/common/version.h
src/vscp/common/vscp.h
src/vscp/common/vscpremotetcpif.h
src/vscp/common/vscpremotetcpif.cpp
src/vscp/common/vscpdatetime.h
src/vscp/common/vscpdatetime.cpp
src/vscp/common/guid.h
src/vscp/common/guid.cpp
src/vscp/common/vscphelper.h
src/vscp/common/vscphelper.cpp
src/vscp/common/canal.h
src/vscp/common/canaldlldef.h
src/vscp/common/vscpcanaldeviceif.h
src/vscp/common/vscpcanaldeviceif.cpp
src/vscp/common/canal_xmlconfig.h
src/vscp/common/canal_xmlconfig.cpp
src/vscp/common/vscp_client_base.h
src/vscp/common/vscp_client_base.cpp
src/vscp/common/vscp_client_mqtt.h
src/vscp/common/vscp_client_mqtt.cpp
./third_party/mustache/mustache.hpp
./third_party/sqlite3/sqlite3.h
./third_party/sqlite3/sqlite3.c
src/common/windows/getopt.c
src/common/configfile.h
src/common/configfile.cpp
src/common/sockettcp.h
src/common/sockettcp.c
src/common/vscpbase64.h
src/common/vscpbase64.c
src/common/vscp_aes.h
src/common/vscp_aes.c
src/common/crc.h
src/common/crc.c
src/common/crc8.h
src/common/crc8.c
src/common/vscpmd5.h
src/common/vscpmd5.c
src/common/randpassword.h
src/common/randpassword.cpp
)
src/vscp/daemon/vscpd.cpp
src/vscp/daemon/controlobject.h
src/vscp/daemon/controlobject.cpp
src/vscp/daemon/devicethread.h
src/vscp/daemon/devicethread.cpp
src/vscp/daemon/devicelist.h
src/vscp/daemon/devicelist.cpp
src/vscp/common/version.h
src/vscp/common/vscp.h
src/vscp/common/vscpremotetcpif.h
src/vscp/common/vscpremotetcpif.cpp
src/vscp/common/vscpdatetime.h
src/vscp/common/vscpdatetime.cpp
src/vscp/common/guid.h
src/vscp/common/guid.cpp
src/vscp/common/vscphelper.h
src/vscp/common/vscphelper.cpp
src/vscp/common/canal.h
src/vscp/common/canaldlldef.h
src/vscp/common/vscpcanaldeviceif.h
src/vscp/common/vscpcanaldeviceif.cpp
src/vscp/common/canal_xmlconfig.h
src/vscp/common/canal_xmlconfig.cpp
src/vscp/common/vscp_client_base.h
src/vscp/common/vscp_client_base.cpp
src/vscp/common/vscp_client_mqtt.h
src/vscp/common/vscp_client_mqtt.cpp
./third_party/mustache/mustache.hpp
./third_party/sqlite3/sqlite3.h
./third_party/sqlite3/sqlite3.c
src/common/windows/getopt.c
src/common/configfile.h
src/common/configfile.cpp
src/common/sockettcp.h
src/common/sockettcp.c
src/common/vscpbase64.h
src/common/vscpbase64.c
src/common/vscp_aes.h
src/common/vscp_aes.c
src/common/crc.h
src/common/crc.c
src/common/crc8.h
src/common/crc8.c
src/common/vscpmd5.h
src/common/vscpmd5.c
src/common/randpassword.h
src/common/randpassword.cpp
)
else()
add_executable(vscpd
src/vscp/daemon/vscpd.cpp
src/vscp/daemon/controlobject.h
src/vscp/daemon/controlobject.cpp
src/vscp/daemon/devicethread.h
src/vscp/daemon/devicethread.cpp
src/vscp/daemon/devicelist.h
src/vscp/daemon/devicelist.cpp
src/vscp/common/version.h
src/vscp/common/vscp.h
src/vscp/common/vscpremotetcpif.h
src/vscp/common/vscpremotetcpif.cpp
src/vscp/common/vscpdatetime.h
src/vscp/common/vscpdatetime.cpp
src/vscp/common/guid.h
src/vscp/common/guid.cpp
src/vscp/common/vscphelper.h
src/vscp/common/vscphelper.cpp
src/vscp/common/canal.h
src/vscp/common/canaldlldef.h
src/vscp/common/vscpcanaldeviceif.h
src/vscp/common/vscpcanaldeviceif.cpp
src/vscp/common/canal_xmlconfig.h
src/vscp/common/canal_xmlconfig.cpp
src/vscp/common/vscp_client_base.h
src/vscp/common/vscp_client_base.cpp
src/vscp/common/vscp_client_mqtt.h
src/vscp/common/vscp_client_mqtt.cpp
./third_party/mustache/mustache.hpp
./third_party/sqlite3/sqlite3.h
./third_party/sqlite3/sqlite3.c
src/common/configfile.h
src/common/configfile.cpp
src/common/sockettcp.h
src/common/sockettcp.c
src/common/vscpbase64.h
src/common/vscpbase64.c
src/common/vscp_aes.h
src/common/vscp_aes.c
src/common/crc.h
src/common/crc.c
src/common/crc8.h
src/common/crc8.c
src/common/vscpmd5.h
src/common/vscpmd5.c
src/common/randpassword.h
src/common/randpassword.cpp
)
src/vscp/daemon/vscpd.cpp
src/vscp/daemon/controlobject.h
src/vscp/daemon/controlobject.cpp
src/vscp/daemon/devicethread.h
src/vscp/daemon/devicethread.cpp
src/vscp/daemon/devicelist.h
src/vscp/daemon/devicelist.cpp
src/vscp/common/version.h
src/vscp/common/vscp.h
src/vscp/common/vscpremotetcpif.h
src/vscp/common/vscpremotetcpif.cpp
src/vscp/common/vscpdatetime.h
src/vscp/common/vscpdatetime.cpp
src/vscp/common/guid.h
src/vscp/common/guid.cpp
src/vscp/common/vscphelper.h
src/vscp/common/vscphelper.cpp
src/vscp/common/canal.h
src/vscp/common/canaldlldef.h
src/vscp/common/vscpcanaldeviceif.h
src/vscp/common/vscpcanaldeviceif.cpp
src/vscp/common/canal_xmlconfig.h
src/vscp/common/canal_xmlconfig.cpp
src/vscp/common/vscp_client_base.h
src/vscp/common/vscp_client_base.cpp
src/vscp/common/vscp_client_mqtt.h
src/vscp/common/vscp_client_mqtt.cpp
./third_party/mustache/mustache.hpp
./third_party/sqlite3/sqlite3.h
./third_party/sqlite3/sqlite3.c
src/common/configfile.h
src/common/configfile.cpp
src/common/sockettcp.h
src/common/sockettcp.c
src/common/vscpbase64.h
src/common/vscpbase64.c
src/common/vscp_aes.h
src/common/vscp_aes.c
src/common/crc.h
src/common/crc.c
src/common/crc8.h
src/common/crc8.c
src/common/vscpmd5.h
src/common/vscpmd5.c
src/common/randpassword.h
src/common/randpassword.cpp
)
endif()

# * * * Includes * * *
Expand Down
17 changes: 10 additions & 7 deletions src/vscp/common/vscp_client_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@

CVscpClient::CVscpClient()
{
m_evcallback = nullptr;
m_excallback = nullptr;
m_bActiveCallbackEv = false;
m_bActiveCallbackEx = false;
m_callbackObject = nullptr;
m_bFullLevel2 = false; // This is not a full level II communication client
}
Expand All @@ -63,22 +63,25 @@ CVscpClient::~CVscpClient()
//

int
CVscpClient::setCallback(LPFNDLL_EV_CALLBACK evcallback, void *pData)
CVscpClient::setCallbackEv(std::function<void(vscpEvent &ev, void *pobj)> callback, void *pData)
{
m_evcallback = evcallback;
m_callbackev = callback;
m_callbackObject = pData;
m_bActiveCallbackEv = true;

return VSCP_ERROR_SUCCESS;
};

///////////////////////////////////////////////////////////////////////////////
// setCallback
// setCallbackEx
//

int
CVscpClient::setCallback(LPFNDLL_EX_CALLBACK excallback, void *pData)
CVscpClient::setCallbackEx(std::function<void(vscpEventEx &ex, void *pobj)> callback, void *pData)
{
m_excallback = excallback;
m_callbackex = callback;
m_callbackObject = pData;
m_bActiveCallbackEv = true;

return VSCP_ERROR_SUCCESS;
};
68 changes: 55 additions & 13 deletions src/vscp/common/vscp_client_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ using json = nlohmann::json;
// as it wants.

#ifdef WIN32
typedef void(__stdcall *LPFNDLL_EV_CALLBACK)(vscpEvent *pev, void *pobj); // Event callback
typedef void(__stdcall *LPFNDLL_EX_CALLBACK)(vscpEventEx *pex, void *pobj); // Event ex callbac
// std::function<void(vscpEvent &ev, void *pobj)>
// std::function<void(vscpEventEx &ex, void *pobj)>
// typedef void(__stdcall *CALLBACK_EV)(vscpEvent *pev, void *pobj); // Event callback
// typedef void(__stdcall *CALLBACK_EX)(vscpEventEx *pex, void *pobj); // Event ex callbac
#else
typedef void (*LPFNDLL_EV_CALLBACK)(vscpEvent *pev, void *pobj); // Event callback
typedef void (*LPFNDLL_EX_CALLBACK)(vscpEventEx *pex, void *pobj); // Event ex callback
// typedef void (*CALLBACK_EV)(vscpEvent *pev, void *pobj); // Event callback
// typedef void (*CALLBACK_EX)(vscpEventEx *pex, void *pobj); // Event ex callback
#endif

class CVscpClient {
Expand Down Expand Up @@ -192,15 +194,15 @@ class CVscpClient {
@param pData User defined data to pass in callback call
@return Return VSCP_ERROR_SUCCESS of OK and error code else.
*/
virtual int setCallback(LPFNDLL_EV_CALLBACK evcallback, void *pData = nullptr);
virtual int setCallbackEv(std::function<void(vscpEvent &ev, void *pobj)> callback, void *pData = nullptr);

/*!
Set (and enable) receive callback ex events
@param LPFNDLL_EX_CALLBACK Callback to call when an event is received
@param pData User defined data to pass in callback call
@return Return VSCP_ERROR_SUCCESS of OK and error code else.
*/
virtual int setCallback(LPFNDLL_EX_CALLBACK excallback, void *pData = nullptr);
virtual int setCallbackEx(std::function<void(vscpEventEx &ex, void *pobj)> callback, void *pData = nullptr);

/*!
Getter/setters for connection timeout
Expand All @@ -220,13 +222,25 @@ class CVscpClient {
Check if ev callback is defined
@return true if callback is defined
*/
bool isEvCallback(void) { return (nullptr != m_evcallback); }
bool isCallbackEvActive(void) { return m_bActiveCallbackEv; }

/*!
Check if ex callback is defined
@return true if callback is defined
*/
bool isExCallback(void) { return (nullptr != m_excallback); }
bool isCallbackExActive(void) { return m_bActiveCallbackEx; }

/*!
Activate or deactivate Ev callback
@param b Set to true (default) to activate.
*/
void setCallbackEvActive(bool b = true) { m_bActiveCallbackEv = b; };

/*!
Activate or deactivate Ex callback
@param b Set to true (default) to activate.
*/
void setCallbackExActive(bool b = true) { m_bActiveCallbackEx = b; };

/*!
Return a JSON representation of connection
Expand Down Expand Up @@ -263,33 +277,61 @@ class CVscpClient {
Mark as full level II or not
@param b Set to true to mark as full level II working client
*/
void setFullLevel2(bool b=true) { m_bFullLevel2 = b; };
void setFullLevel2(bool b = true) { m_bFullLevel2 = b; };

/*!
Check if client is afull level II client
@return true If the client is a full level II client
*/
bool isFullLevel2(void) { return m_bFullLevel2; };

/*!
Set callback object
@param Pointer to object to set
*/

void setCallbackObj(void *pobj) { m_callbackObject = pobj; };

/*!
Return Callback object
@return Pointer to object
*/
void *getCallbackObj(void) { return m_callbackObject; };

public:

/*!
Callback for events
*/
LPFNDLL_EV_CALLBACK m_evcallback;
std::function<void(vscpEvent &ev, void *pobj)> m_callbackev;

/*!
Callback for ex events
*/
LPFNDLL_EX_CALLBACK m_excallback;
std::function<void(vscpEventEx &ex, void *pobj)> m_callbackex;

protected:

/// Type of connection object
connType m_type = CVscpClient::connType::NONE;

/*!
Flag that is true when an ev callback is installed
*/
bool m_bActiveCallbackEv;

/*!
Flag that is true when an ex callback is installed
*/
bool m_bActiveCallbackEx;

/*!
This data pointer is set by the callback
setter and is sent with the callback call
*/
void *m_callbackObject;

/// Type of connection object
connType m_type = CVscpClient::connType::NONE;


/// Name for connection object
std::string m_name;
Expand Down
Loading

0 comments on commit 6e5c5f2

Please sign in to comment.