connected

This commit is contained in:
Forsworns 2022-02-15 11:28:30 +08:00
parent 4f59e4ea24
commit f0b2886a03
9 changed files with 41 additions and 94 deletions

View File

@ -1,10 +1,11 @@
#include "snap7.h" #include "snap7.h"
#include <transform.h> #include <transform.h>
#include "lwip/sys.h"
static S7Object Client; static S7Object Client;
static unsigned char Buffer[65536]; // 64 K buffer static unsigned char Buffer[65536]; // 64 K buffer
static char Address[20]="192.168.250.252"; // PLC IP Address 192.168.250.8 static char* Address="192.168.250.252"; // plc 250.8, pc 250.252
static int Rack = 0, Slot = 1; // Default Rack and Slot static int Rack = 0, Slot = 1; // Default Rack and Slot
static int Area = S7AreaDB, DBNumber = 13, Start = 0, Amount = 4, WordLen = S7WLByte; // Default Rack and Slot static int Area = S7AreaDB, DBNumber = 13, Start = 0, Amount = 4, WordLen = S7WLByte; // Default Rack and Slot
static int bRead = 1; // By default read static int bRead = 1; // By default read
@ -18,9 +19,9 @@ static int CliConnect()
if (Check(Client, res, "UNIT Connection")) if (Check(Client, res, "UNIT Connection"))
{ {
Cli_GetPduLength(Client, Requested, Negotiated); Cli_GetPduLength(Client, Requested, Negotiated);
KPrintf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot); printf(" Connected to : %s (Rack=%d, Slot=%d)\n", Address, Rack, Slot);
KPrintf(" PDU Requested : %d bytes\n", Requested); printf(" PDU Requested : %d bytes\n", Requested);
KPrintf(" PDU Negotiated : %d bytes\n", Negotiated); printf(" PDU Negotiated : %d bytes\n", Negotiated);
}; };
return !res; return !res;
} }
@ -56,11 +57,19 @@ void PerformTasks()
} }
} }
void s7_demo() void s7_demo(int argc, char *argv[])
{ {
if(argc >= 2)
{
Address = argv[1];
}
printf(" Connected to : %s", Address);
// Client Creation // Client Creation
Client = Cli_Create(); Client = Cli_Create();
ETH_BSP_Config();
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
// Connection // Connection
if (CliConnect()) if (CliConnect())
{ {

View File

@ -43,28 +43,6 @@ void libdone(void)
// in future expansions here can be inserted some destruction code // in future expansions here can be inserted some destruction code
} }
#ifdef OS_WINDOWS
BOOL APIENTRY DllMain (HINSTANCE hInst,
DWORD reason,
LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
libinit();
break;
case DLL_PROCESS_DETACH:
libdone();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return libresult;
}
#endif
//*************************************************************************** //***************************************************************************
// CLIENT // CLIENT
//*************************************************************************** //***************************************************************************

View File

@ -57,21 +57,13 @@ longword TSnapBase::SwapDWord(longword Value)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void Msg_CloseSocket(socket_t FSocket) void Msg_CloseSocket(socket_t FSocket)
{ {
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket); close(FSocket);
#endif
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
longword Msg_GetSockAddr(socket_t FSocket) longword Msg_GetSockAddr(socket_t FSocket)
{ {
sockaddr_in RemoteSin; sockaddr_in RemoteSin;
#ifdef OS_WINDOWS
int namelen = sizeof(RemoteSin);
#else
uint32_t namelen = sizeof(RemoteSin); uint32_t namelen = sizeof(RemoteSin);
#endif
namelen=sizeof(sockaddr_in); namelen=sizeof(sockaddr_in);
if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0) if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
return RemoteSin.sin_addr.s_addr; return RemoteSin.sin_addr.s_addr;
@ -128,33 +120,21 @@ void TMsgSocket::GetSin(sockaddr_in sin, char *Address, u_short &Port)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void TMsgSocket::GetLocal() void TMsgSocket::GetLocal()
{ {
#ifdef OS_WINDOWS
int namelen = sizeof(LocalSin);
#else
uint32_t namelen = sizeof(LocalSin); uint32_t namelen = sizeof(LocalSin);
#endif
if (getsockname(FSocket, (struct sockaddr*)&LocalSin, &namelen)==0) if (getsockname(FSocket, (struct sockaddr*)&LocalSin, &namelen)==0)
GetSin(LocalSin, LocalAddress, LocalPort); GetSin(LocalSin, LocalAddress, LocalPort);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void TMsgSocket::GetRemote() void TMsgSocket::GetRemote()
{ {
#ifdef OS_WINDOWS
int namelen = sizeof(RemoteSin);
#else
uint32_t namelen = sizeof(RemoteSin); uint32_t namelen = sizeof(RemoteSin);
#endif
if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0) if (getpeername(FSocket,(struct sockaddr*)&RemoteSin, &namelen)==0)
GetSin(RemoteSin, RemoteAddress, RemotePort); GetSin(RemoteSin, RemoteAddress, RemotePort);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
int TMsgSocket::GetLastSocketError() int TMsgSocket::GetLastSocketError()
{ {
#ifdef OS_WINDOWS
return WSAGetLastError();
#else
return errno; return errno;
#endif
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void TMsgSocket::Purge() void TMsgSocket::Purge()
@ -178,8 +158,9 @@ void TMsgSocket::CreateSocket()
DestroySocket(); DestroySocket();
LastTcpError=0; LastTcpError=0;
FSocket =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ); FSocket =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (FSocket!=INVALID_SOCKET) if (FSocket!=INVALID_SOCKET){
SetSocketOptions(); SetSocketOptions();
}
else else
LastTcpError =GetLastSocketError(); LastTcpError =GetLastSocketError();
} }
@ -209,11 +190,7 @@ void TMsgSocket::DestroySocket()
{ {
if (shutdown(FSocket, SD_SEND)==0) if (shutdown(FSocket, SD_SEND)==0)
Purge(); Purge();
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket); close(FSocket);
#endif
FSocket=INVALID_SOCKET; FSocket=INVALID_SOCKET;
} }
LastTcpError=0; LastTcpError=0;
@ -223,13 +200,8 @@ int TMsgSocket::WaitingData()
{ {
int result = 0; int result = 0;
u_long x = 0; u_long x = 0;
#ifdef OS_WINDOWS
if (ioctlsocket(FSocket, FIONREAD, &x) == 0)
result = x;
#else
if (ioctl(FSocket, FIONREAD, &x) == 0) if (ioctl(FSocket, FIONREAD, &x) == 0)
result = x; result = x;
#endif
if (result>MaxPacketSize) if (result>MaxPacketSize)
result = MaxPacketSize; result = MaxPacketSize;
return result; return result;
@ -270,9 +242,9 @@ void TMsgSocket::SetSocketOptions()
int KeepAlive = 1; int KeepAlive = 1;
LastTcpError=0; LastTcpError=0;
SockCheck(setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY,(char*)&NoDelay, sizeof(NoDelay))); SockCheck(setsockopt(FSocket, IPPROTO_TCP, TCP_NODELAY,(char*)&NoDelay, sizeof(NoDelay)));
if (LastTcpError==0){
if (LastTcpError==0)
SockCheck(setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&KeepAlive, sizeof(KeepAlive))); SockCheck(setsockopt(FSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&KeepAlive, sizeof(KeepAlive)));
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
int TMsgSocket::SockCheck(int SockResult) int TMsgSocket::SockCheck(int SockResult)
@ -321,7 +293,6 @@ bool TMsgSocket::CanRead(int Timeout)
FD_ZERO(&FDset); FD_ZERO(&FDset);
FD_SET(FSocket, &FDset); FD_SET(FSocket, &FDset);
x = select(FSocket + 1, &FDset, NULL, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio x = select(FSocket + 1, &FDset, NULL, NULL, &TimeV); //<-Ignore this warning in 64bit Visual Studio
if (x==(int)SOCKET_ERROR) if (x==(int)SOCKET_ERROR)
{ {
@ -349,7 +320,10 @@ int TMsgSocket::SckConnect()
if (LastTcpError == 0) { if (LastTcpError == 0) {
flags = fcntl(FSocket, F_GETFL, 0); flags = fcntl(FSocket, F_GETFL, 0);
if (flags >= 0) { if (flags >= 0) {
if (fcntl(FSocket, F_SETFL, flags | O_NONBLOCK) != -1) { // lwip defines O_NONBLOCK as 1
#define LWIP_O_NONBLOCK 1
int setfl_res = fcntl(FSocket, F_SETFL, flags | LWIP_O_NONBLOCK);
if (setfl_res != -1) {
n = connect(FSocket, (struct sockaddr*)&RemoteSin, sizeof(RemoteSin)); n = connect(FSocket, (struct sockaddr*)&RemoteSin, sizeof(RemoteSin));
if (n < 0) { if (n < 0) {
if (errno != EINPROGRESS) { if (errno != EINPROGRESS) {
@ -429,11 +403,7 @@ void TMsgSocket::ForceClose()
if(FSocket != INVALID_SOCKET) if(FSocket != INVALID_SOCKET)
{ {
try { try {
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket); close(FSocket);
#endif
} catch (...) { } catch (...) {
} }
FSocket=INVALID_SOCKET; FSocket=INVALID_SOCKET;
@ -617,11 +587,7 @@ TRawSocketPinger::~TRawSocketPinger()
{ {
if (FSocket!=INVALID_SOCKET) if (FSocket!=INVALID_SOCKET)
{ {
#ifdef OS_WINDOWS
closesocket(FSocket);
#else
close(FSocket); close(FSocket);
#endif
FSocket=INVALID_SOCKET; FSocket=INVALID_SOCKET;
}; };
} }
@ -759,11 +725,7 @@ bool RawSocketsCheck()
Result=RawSocket != INVALID_SOCKET; Result=RawSocket != INVALID_SOCKET;
if (Result) if (Result)
#ifdef OS_WINDOWS
closesocket(RawSocket);
#else
close(RawSocket); close(RawSocket);
#endif
return Result; return Result;
} }

View File

@ -29,14 +29,7 @@
#include "snap_platform.h" #include "snap_platform.h"
#include "snap_sysutils.h" #include "snap_sysutils.h"
#include "errno.h" #include "errno.h"
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Non blocking connection to avoid root priviledges under UNIX
// i.e. raw socket pinger is not more used.
// Thanks to Rolf Stalder that made it ;)
//----------------------------------------------------------------------------
#include <fcntl.h>
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
typedef int socket_t; typedef int socket_t;
@ -183,9 +176,6 @@ class TPinger
private: private:
PRawSocketPinger RawPinger; PRawSocketPinger RawPinger;
bool RawAvail; bool RawAvail;
#ifdef OS_WINDOWS
bool WinPing(longword ip_addr, int Timeout);
#endif
bool RawPing(longword ip_addr, int Timeout); bool RawPing(longword ip_addr, int Timeout);
public: public:
TPinger(); TPinger();

View File

@ -30,6 +30,7 @@
#include <lwip/tcp.h> #include <lwip/tcp.h>
#include <lwip/errno.h> #include <lwip/errno.h>
#include <lwip/sockets.h> #include <lwip/sockets.h>
#include <lwip/sockets.h> // fcntl related status definition is different from linux
#include <lwip/inet.h> #include <lwip/inet.h>
#include <unistd.h> #include <unistd.h>

View File

@ -95,6 +95,12 @@ int PrivTaskDelay(int32_t ms)
return usleep(ms); return usleep(ms);
} }
uint32_t PrivGetTickTime(){
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
/*********************fs**************************/ /*********************fs**************************/
/************************Driver Posix Transform***********************/ /************************Driver Posix Transform***********************/

View File

@ -190,6 +190,7 @@ int PrivTaskStartup(pthread_t *thread);
int PrivTaskDelete(pthread_t thread, int sig); int PrivTaskDelete(pthread_t thread, int sig);
void PrivTaskQuit(void *value_ptr); void PrivTaskQuit(void *value_ptr);
int PrivTaskDelay(int32_t ms); int PrivTaskDelay(int32_t ms);
uint32_t PrivGetTickTime();
/*********************driver*************************/ /*********************driver*************************/

View File

@ -7,8 +7,8 @@ MAKEFLAGS += --no-print-directory
support :=kd233 stm32f407-st-discovery maix-go stm32f407zgt6 aiit-riscv64-board aiit-arm32-board hifive1-rev-B hifive1-emulator k210-emulator cortex-m3-emulator cortex-m4-emulator ok1052-c gapuino stm32f103-nano gd32vf103_rvstar cortex-m0-emulator support :=kd233 stm32f407-st-discovery maix-go stm32f407zgt6 aiit-riscv64-board aiit-arm32-board hifive1-rev-B hifive1-emulator k210-emulator cortex-m3-emulator cortex-m4-emulator ok1052-c gapuino stm32f103-nano gd32vf103_rvstar cortex-m0-emulator
SRC_DIR:= SRC_DIR:=
# kd233
export BOARD ?=kd233 export BOARD ?=ok1052-c
ifeq ($(filter $(BOARD),$(support)),) ifeq ($(filter $(BOARD),$(support)),)
$(warning "You should choose board like this:make BOARD=kd233") $(warning "You should choose board like this:make BOARD=kd233")

View File

@ -2009,7 +2009,7 @@
* LWIP_SO_RCVBUF==1: Enable SO_RCVBUF processing. * LWIP_SO_RCVBUF==1: Enable SO_RCVBUF processing.
*/ */
#if !defined LWIP_SO_RCVBUF || defined __DOXYGEN__ #if !defined LWIP_SO_RCVBUF || defined __DOXYGEN__
#define LWIP_SO_RCVBUF 0 #define LWIP_SO_RCVBUF 1
#endif #endif
/** /**
@ -3292,7 +3292,7 @@
* SOCKETS_DEBUG: Enable debugging in sockets.c. * SOCKETS_DEBUG: Enable debugging in sockets.c.
*/ */
#if !defined SOCKETS_DEBUG || defined __DOXYGEN__ #if !defined SOCKETS_DEBUG || defined __DOXYGEN__
#define SOCKETS_DEBUG LWIP_DBG_OFF #define SOCKETS_DEBUG LWIP_DBG_ON
#endif #endif
/** /**