Skip to content

Commit c37095d

Browse files
BuR Driver update
1 parent 5f0e726 commit c37095d

9 files changed

+268
-18
lines changed

Drivers/BuR/ACT/LibMCDriver_BuR.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This base Driver interface MUST be in Sync with LibMCDriver.xml
6262

6363
<class name="Base">
6464
</class>
65+
6566

6667
<class name="Driver" parent="Base" description="An abstract Machine Control Driver">
6768

@@ -91,9 +92,6 @@ This base Driver interface MUST be in Sync with LibMCDriver.xml
9192

9293
<method name="QueryParameters" description = "Stores the driver parameters in the driver environment.">
9394
</method>
94-
95-
<method name="Connect" description= "Connects to .">
96-
</method>
9795

9896
</class>
9997

@@ -114,6 +112,17 @@ Custom implementation
114112

115113
<class name="Driver_BuR" parent="Driver" description="A Driver for a BR Automation PLC.">
116114

115+
<method name="Connect" description= "Connects to a BuR PLC Controller.">
116+
<param name="IPAddress" type="string" pass="in" description="IP Address of PLC Service." />
117+
<param name="Port" type="uint32" pass="in" description="Port of PLC Service." />
118+
<param name="Timeout" type="uint32" pass="in" description="Timeout in milliseconds." />
119+
</method>
120+
121+
122+
<method name="Disconnect" description= "Disconnects from the BuR PLC Controller.">
123+
</method>
124+
125+
117126
</class>
118127

119128

Drivers/BuR/Headers/CppDynamic/libmcdriver_bur_abi.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_queryparam
136136
Class definition for Driver_BuR
137137
**************************************************************************************************************************/
138138

139+
/**
140+
* Connects to a BuR PLC Controller.
141+
*
142+
* @param[in] pDriver_BuR - Driver_BuR instance.
143+
* @param[in] pIPAddress - IP Address of PLC Service.
144+
* @param[in] nPort - Port of PLC Service.
145+
* @param[in] nTimeout - Timeout in milliseconds.
146+
* @return error code or 0 (success)
147+
*/
148+
LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_bur_connect(LibMCDriver_BuR_Driver_BuR pDriver_BuR, const char * pIPAddress, LibMCDriver_BuR_uint32 nPort, LibMCDriver_BuR_uint32 nTimeout);
149+
150+
/**
151+
* Disconnects from the BuR PLC Controller.
152+
*
153+
* @param[in] pDriver_BuR - Driver_BuR instance.
154+
* @return error code or 0 (success)
155+
*/
156+
LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_bur_disconnect(LibMCDriver_BuR_Driver_BuR pDriver_BuR);
157+
139158
/*************************************************************************************************************************
140159
Global functions
141160
**************************************************************************************************************************/

Drivers/BuR/Headers/CppDynamic/libmcdriver_bur_dynamic.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ typedef LibMCDriver_BuRResult (*PLibMCDriver_BuRDriver_QueryParametersPtr) (LibM
123123
Class definition for Driver_BuR
124124
**************************************************************************************************************************/
125125

126+
/**
127+
* Connects to a BuR PLC Controller.
128+
*
129+
* @param[in] pDriver_BuR - Driver_BuR instance.
130+
* @param[in] pIPAddress - IP Address of PLC Service.
131+
* @param[in] nPort - Port of PLC Service.
132+
* @param[in] nTimeout - Timeout in milliseconds.
133+
* @return error code or 0 (success)
134+
*/
135+
typedef LibMCDriver_BuRResult (*PLibMCDriver_BuRDriver_BuR_ConnectPtr) (LibMCDriver_BuR_Driver_BuR pDriver_BuR, const char * pIPAddress, LibMCDriver_BuR_uint32 nPort, LibMCDriver_BuR_uint32 nTimeout);
136+
137+
/**
138+
* Disconnects from the BuR PLC Controller.
139+
*
140+
* @param[in] pDriver_BuR - Driver_BuR instance.
141+
* @return error code or 0 (success)
142+
*/
143+
typedef LibMCDriver_BuRResult (*PLibMCDriver_BuRDriver_BuR_DisconnectPtr) (LibMCDriver_BuR_Driver_BuR pDriver_BuR);
144+
126145
/*************************************************************************************************************************
127146
Global functions
128147
**************************************************************************************************************************/
@@ -205,6 +224,8 @@ typedef struct {
205224
PLibMCDriver_BuRDriver_GetVersionPtr m_Driver_GetVersion;
206225
PLibMCDriver_BuRDriver_GetHeaderInformationPtr m_Driver_GetHeaderInformation;
207226
PLibMCDriver_BuRDriver_QueryParametersPtr m_Driver_QueryParameters;
227+
PLibMCDriver_BuRDriver_BuR_ConnectPtr m_Driver_BuR_Connect;
228+
PLibMCDriver_BuRDriver_BuR_DisconnectPtr m_Driver_BuR_Disconnect;
208229
PLibMCDriver_BuRGetVersionPtr m_GetVersion;
209230
PLibMCDriver_BuRGetLastErrorPtr m_GetLastError;
210231
PLibMCDriver_BuRReleaseInstancePtr m_ReleaseInstance;

Drivers/BuR/Headers/CppDynamic/libmcdriver_bur_dynamic.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ class CDriver_BuR : public CDriver {
362362
{
363363
}
364364

365+
inline void Connect(const std::string & sIPAddress, const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout);
366+
inline void Disconnect();
365367
};
366368

367369
/**
@@ -491,6 +493,8 @@ class CDriver_BuR : public CDriver {
491493
pWrapperTable->m_Driver_GetVersion = nullptr;
492494
pWrapperTable->m_Driver_GetHeaderInformation = nullptr;
493495
pWrapperTable->m_Driver_QueryParameters = nullptr;
496+
pWrapperTable->m_Driver_BuR_Connect = nullptr;
497+
pWrapperTable->m_Driver_BuR_Disconnect = nullptr;
494498
pWrapperTable->m_GetVersion = nullptr;
495499
pWrapperTable->m_GetLastError = nullptr;
496500
pWrapperTable->m_ReleaseInstance = nullptr;
@@ -600,6 +604,24 @@ class CDriver_BuR : public CDriver {
600604
if (pWrapperTable->m_Driver_QueryParameters == nullptr)
601605
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
602606

607+
#ifdef _WIN32
608+
pWrapperTable->m_Driver_BuR_Connect = (PLibMCDriver_BuRDriver_BuR_ConnectPtr) GetProcAddress(hLibrary, "libmcdriver_bur_driver_bur_connect");
609+
#else // _WIN32
610+
pWrapperTable->m_Driver_BuR_Connect = (PLibMCDriver_BuRDriver_BuR_ConnectPtr) dlsym(hLibrary, "libmcdriver_bur_driver_bur_connect");
611+
dlerror();
612+
#endif // _WIN32
613+
if (pWrapperTable->m_Driver_BuR_Connect == nullptr)
614+
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
615+
616+
#ifdef _WIN32
617+
pWrapperTable->m_Driver_BuR_Disconnect = (PLibMCDriver_BuRDriver_BuR_DisconnectPtr) GetProcAddress(hLibrary, "libmcdriver_bur_driver_bur_disconnect");
618+
#else // _WIN32
619+
pWrapperTable->m_Driver_BuR_Disconnect = (PLibMCDriver_BuRDriver_BuR_DisconnectPtr) dlsym(hLibrary, "libmcdriver_bur_driver_bur_disconnect");
620+
dlerror();
621+
#endif // _WIN32
622+
if (pWrapperTable->m_Driver_BuR_Disconnect == nullptr)
623+
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
624+
603625
#ifdef _WIN32
604626
pWrapperTable->m_GetVersion = (PLibMCDriver_BuRGetVersionPtr) GetProcAddress(hLibrary, "libmcdriver_bur_getversion");
605627
#else // _WIN32
@@ -703,6 +725,14 @@ class CDriver_BuR : public CDriver {
703725
if ( (eLookupError != 0) || (pWrapperTable->m_Driver_QueryParameters == nullptr) )
704726
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
705727

728+
eLookupError = (*pLookup)("libmcdriver_bur_driver_bur_connect", (void**)&(pWrapperTable->m_Driver_BuR_Connect));
729+
if ( (eLookupError != 0) || (pWrapperTable->m_Driver_BuR_Connect == nullptr) )
730+
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
731+
732+
eLookupError = (*pLookup)("libmcdriver_bur_driver_bur_disconnect", (void**)&(pWrapperTable->m_Driver_BuR_Disconnect));
733+
if ( (eLookupError != 0) || (pWrapperTable->m_Driver_BuR_Disconnect == nullptr) )
734+
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
735+
706736
eLookupError = (*pLookup)("libmcdriver_bur_getversion", (void**)&(pWrapperTable->m_GetVersion));
707737
if ( (eLookupError != 0) || (pWrapperTable->m_GetVersion == nullptr) )
708738
return LIBMCDRIVER_BUR_ERROR_COULDNOTFINDLIBRARYEXPORT;
@@ -830,6 +860,25 @@ class CDriver_BuR : public CDriver {
830860
/**
831861
* Method definitions for class CDriver_BuR
832862
*/
863+
864+
/**
865+
* CDriver_BuR::Connect - Connects to a BuR PLC Controller.
866+
* @param[in] sIPAddress - IP Address of PLC Service.
867+
* @param[in] nPort - Port of PLC Service.
868+
* @param[in] nTimeout - Timeout in milliseconds.
869+
*/
870+
void CDriver_BuR::Connect(const std::string & sIPAddress, const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout)
871+
{
872+
CheckError(m_pWrapper->m_WrapperTable.m_Driver_BuR_Connect(m_pHandle, sIPAddress.c_str(), nPort, nTimeout));
873+
}
874+
875+
/**
876+
* CDriver_BuR::Disconnect - Disconnects from the BuR PLC Controller.
877+
*/
878+
void CDriver_BuR::Disconnect()
879+
{
880+
CheckError(m_pWrapper->m_WrapperTable.m_Driver_BuR_Disconnect(m_pHandle));
881+
}
833882

834883
} // namespace LibMCDriver_BuR
835884

Drivers/BuR/Implementation/libmcdriver_bur_driver_bur.cpp

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,23 @@ using namespace LibMCDriver_BuR::Impl;
4848
**************************************************************************************************************************/
4949

5050
CDriver_BuR::CDriver_BuR(const std::string& sName, LibMCEnv::PDriverEnvironment pDriverEnvironment)
51-
: m_sName(sName), m_pDriverEnvironment (pDriverEnvironment)
51+
: m_sName(sName), m_pDriverEnvironment (pDriverEnvironment), m_nWorkerThreadCount (1), m_nMaxReceiveBufferSize (1024*1024)
5252
{
5353
if (pDriverEnvironment.get() == nullptr)
5454
throw ELibMCDriver_BuRInterfaceException(LIBMCDRIVER_BUR_ERROR_INVALIDPARAM);
5555
}
5656

57+
58+
5759
void CDriver_BuR::Configure(const std::string& sConfigurationString)
5860
{
59-
uint32_t workerThreadCount = 1;
60-
uint32_t maxReceiveBufferSize = 1024 * 1024;
6161

6262
m_pTcpService = brynet::net::TcpService::Create();
63-
m_pTcpService->startWorkerThread(workerThreadCount);
63+
m_pTcpService->startWorkerThread(m_nWorkerThreadCount);
6464

6565
m_pAsyncConnector = brynet::net::AsyncConnector::Create();
6666
m_pAsyncConnector->startWorkerThread();
6767

68-
brynet::net::wrapper::ConnectionBuilder connectionBuilder;
69-
70-
connectionBuilder.WithService(m_pTcpService)
71-
.WithConnector(m_pAsyncConnector)
72-
.WithMaxRecvBufferSize(maxReceiveBufferSize)
73-
.AddEnterCallback([this](const brynet::net::TcpConnection::Ptr& session) { enterCallback(session); });
74-
7568
}
7669

7770
std::string CDriver_BuR::GetName()
@@ -100,14 +93,75 @@ void CDriver_BuR::GetHeaderInformation(std::string& sNameSpace, std::string& sBa
10093
void CDriver_BuR::QueryParameters()
10194
{
10295
brynet::base::app_kbhit();
96+
97+
if (!m_pCurrentConnection.expired()) {
98+
auto pConnection = m_pCurrentConnection.lock();
99+
//pConnection->send ();
100+
}
101+
103102
}
104103

105-
void CDriver_BuR::enterCallback(const std::shared_ptr <brynet::net::TcpConnection> session)
104+
void CDriver_BuR::enterCallback(const std::shared_ptr <brynet::net::TcpConnection> session, const std::string& sIPAddress, const uint32_t nPort)
106105
{
106+
107+
std::lock_guard<std::mutex> lockGuard(m_ConnectionMutex);
108+
109+
if (!m_pCurrentConnection.expired()) {
110+
auto pConnection = m_pCurrentConnection.lock();
111+
pConnection->postDisConnect();
112+
}
113+
114+
115+
session->setDataCallback([session](brynet::base::BasePacketReader& reader)
116+
{
117+
118+
119+
});
120+
121+
m_pCurrentConnection = session->weak_from_this();
122+
std::cout << "Connected!" << std::endl;
107123

124+
108125
}
109126

110127
void CDriver_BuR::failedCallback()
111128
{
129+
if (!m_pCurrentConnection.expired()) {
130+
std::lock_guard<std::mutex> lockGuard(m_ConnectionMutex);
131+
auto pConnection = m_pCurrentConnection.lock();
132+
pConnection->postDisConnect();
133+
}
134+
m_pCurrentConnection.reset();
135+
136+
}
137+
138+
139+
void CDriver_BuR::Connect(const std::string& sIPAddress, const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout)
140+
{
141+
brynet::net::wrapper::ConnectionBuilder connectionBuilder;
112142

113-
}
143+
connectionBuilder.WithService(m_pTcpService)
144+
.WithConnector(m_pAsyncConnector)
145+
.WithMaxRecvBufferSize(m_nMaxReceiveBufferSize)
146+
.AddEnterCallback([this, sIPAddress, nPort](const brynet::net::TcpConnection::Ptr& session) { enterCallback(session, sIPAddress, nPort); });
147+
148+
connectionBuilder
149+
.WithAddr(sIPAddress, nPort)
150+
.WithTimeout(std::chrono::milliseconds(nTimeout))
151+
.WithFailedCallback([this]() { failedCallback(); })
152+
.AddSocketProcessCallback([](brynet::net::TcpSocket& socket) {
153+
socket.setNodelay();
154+
})
155+
.asyncConnect();
156+
157+
}
158+
159+
void CDriver_BuR::Disconnect()
160+
{
161+
if (!m_pCurrentConnection.expired()) {
162+
std::lock_guard<std::mutex> lockGuard(m_ConnectionMutex);
163+
auto pConnection = m_pCurrentConnection.lock();
164+
pConnection->postDisConnect();
165+
}
166+
m_pCurrentConnection.reset();
167+
}

Drivers/BuR/Implementation/libmcdriver_bur_driver_bur.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Abstract: This is the class declaration of CDriver_BuR
4545
#endif
4646

4747
// Include custom headers here.
48-
48+
#include <mutex>
4949

5050
namespace brynet {
5151
namespace net {
@@ -70,12 +70,19 @@ class CDriver_BuR : public virtual IDriver_BuR, public virtual CDriver {
7070
LibMCEnv::PDriverEnvironment m_pDriverEnvironment;
7171
std::string m_sName;
7272

73+
uint32_t m_nWorkerThreadCount;
74+
uint32_t m_nMaxReceiveBufferSize;
75+
7376
std::shared_ptr<brynet::net::TcpService> m_pTcpService;
7477
std::shared_ptr <brynet::net::AsyncConnector> m_pAsyncConnector;
7578

76-
void enterCallback(const std::shared_ptr <brynet::net::TcpConnection> session);
79+
std::mutex m_ConnectionMutex;
80+
81+
void enterCallback(const std::shared_ptr <brynet::net::TcpConnection> session, const std::string & sIPAddress, const uint32_t nPort);
7782
void failedCallback();
7883

84+
std::weak_ptr<brynet::net::TcpConnection> m_pCurrentConnection;
85+
7986
public:
8087

8188
CDriver_BuR (const std::string & sName, LibMCEnv::PDriverEnvironment pDriverEnvironment);
@@ -92,6 +99,10 @@ class CDriver_BuR : public virtual IDriver_BuR, public virtual CDriver {
9299

93100
void QueryParameters() override;
94101

102+
void Connect(const std::string& sIPAddress, const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout) override;
103+
104+
void Disconnect() override;
105+
95106
};
96107

97108
} // namespace Impl

Drivers/BuR/Interfaces/libmcdriver_bur_abi.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_queryparam
136136
Class definition for Driver_BuR
137137
**************************************************************************************************************************/
138138

139+
/**
140+
* Connects to a BuR PLC Controller.
141+
*
142+
* @param[in] pDriver_BuR - Driver_BuR instance.
143+
* @param[in] pIPAddress - IP Address of PLC Service.
144+
* @param[in] nPort - Port of PLC Service.
145+
* @param[in] nTimeout - Timeout in milliseconds.
146+
* @return error code or 0 (success)
147+
*/
148+
LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_bur_connect(LibMCDriver_BuR_Driver_BuR pDriver_BuR, const char * pIPAddress, LibMCDriver_BuR_uint32 nPort, LibMCDriver_BuR_uint32 nTimeout);
149+
150+
/**
151+
* Disconnects from the BuR PLC Controller.
152+
*
153+
* @param[in] pDriver_BuR - Driver_BuR instance.
154+
* @return error code or 0 (success)
155+
*/
156+
LIBMCDRIVER_BUR_DECLSPEC LibMCDriver_BuRResult libmcdriver_bur_driver_bur_disconnect(LibMCDriver_BuR_Driver_BuR pDriver_BuR);
157+
139158
/*************************************************************************************************************************
140159
Global functions
141160
**************************************************************************************************************************/

Drivers/BuR/Interfaces/libmcdriver_bur_interfaces.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ typedef IBaseSharedPtr<IDriver> PIDriver;
315315

316316
class IDriver_BuR : public virtual IDriver {
317317
public:
318+
/**
319+
* IDriver_BuR::Connect - Connects to a BuR PLC Controller.
320+
* @param[in] sIPAddress - IP Address of PLC Service.
321+
* @param[in] nPort - Port of PLC Service.
322+
* @param[in] nTimeout - Timeout in milliseconds.
323+
*/
324+
virtual void Connect(const std::string & sIPAddress, const LibMCDriver_BuR_uint32 nPort, const LibMCDriver_BuR_uint32 nTimeout) = 0;
325+
326+
/**
327+
* IDriver_BuR::Disconnect - Disconnects from the BuR PLC Controller.
328+
*/
329+
virtual void Disconnect() = 0;
330+
318331
};
319332

320333
typedef IBaseSharedPtr<IDriver_BuR> PIDriver_BuR;

0 commit comments

Comments
 (0)