Skip to content

Commit

Permalink
Fixes timeout handling, fixes forward open and close for POSIX and Win32
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMelikMerkumians committed Dec 27, 2016
1 parent 1c56464 commit 0bb238a
Show file tree
Hide file tree
Showing 28 changed files with 312 additions and 133 deletions.
22 changes: 11 additions & 11 deletions source/contrib/msinttypes/inttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
// 7.8 Format conversion of integer types

typedef struct {
intmax_t quot;
intmax_t rem;
intmax_t quot;
intmax_t rem;
} imaxdiv_t;

// 7.8.1 Macros for format specifiers
Expand Down Expand Up @@ -279,18 +279,18 @@ __inline
#endif // STATIC_IMAXDIV ]
imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
{
imaxdiv_t result;
imaxdiv_t result;

result.quot = numer / denom;
result.rem = numer % denom;
result.quot = numer / denom;
result.rem = numer % denom;

if (numer < 0 && result.rem > 0) {
// did division wrong; must fix up
++result.quot;
result.rem -= denom;
}
if (numer < 0 && result.rem > 0) {
// did division wrong; must fix up
++result.quot;
result.rem -= denom;
}

return result;
return result;
}

// 7.8.2.3 The strtoimax and strtoumax functions
Expand Down
4 changes: 2 additions & 2 deletions source/src/cip/cipconnectionmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,14 @@ EipStatus GetConnectionOwner(CipInstance *instance,
}

EipStatus ManageConnections(MilliSeconds elapsed_time) {

//OPENER_TRACE_INFO("Entering ManageConnections\n");
/*Inform application that it can execute */
HandleApplication();
ManageEncapsulationMessages(elapsed_time);

ConnectionObject *connection_object = g_active_connection_list;
while (NULL != connection_object) {
OPENER_TRACE_INFO("Entering Connection Object loop\n");
//OPENER_TRACE_INFO("Entering Connection Object loop\n");
if (kConnectionStateEstablished == connection_object->state) {
if ( (0 != connection_object->consuming_instance) || /* we have a consuming connection check inactivity watchdog timer */
(connection_object->transport_type_class_trigger & 0x80) ) /* all sever connections have to maintain an inactivity watchdog timer */
Expand Down
2 changes: 1 addition & 1 deletion source/src/cip/ciperror.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#ifndef OPENER_CIPERROR_H_
Expand Down
9 changes: 5 additions & 4 deletions source/src/cip/cipethernetlink.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#include <string.h>
Expand Down Expand Up @@ -28,8 +28,8 @@ CipEthernetLinkObject g_ethernet_link;
* @param mac_address The MAC address of the Ethernet Link
*/
void ConfigureMacAddress(const EipUint8 *const mac_address) {
memcpy(&g_ethernet_link.physical_address, mac_address,
sizeof(g_ethernet_link.physical_address));
memcpy( &g_ethernet_link.physical_address, mac_address,
sizeof(g_ethernet_link.physical_address) );

}

Expand All @@ -49,7 +49,8 @@ EipStatus CipEthernetLinkInit() {

if (ethernet_link_class != NULL) {

CipInstance *ethernet_link_instance = GetCipInstance(ethernet_link_class, 1);
CipInstance *ethernet_link_instance =
GetCipInstance(ethernet_link_class, 1);
InsertAttribute(ethernet_link_instance, 1, kCipUdint,
&g_ethernet_link.interface_speed, kGetableSingleAndAll); /* bind attributes to the instance*/
InsertAttribute(ethernet_link_instance, 2, kCipDword,
Expand Down
2 changes: 1 addition & 1 deletion source/src/cip/cipethernetlink.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#ifndef OPENER_CIPETHERNETLINK_H_
Expand Down
2 changes: 1 addition & 1 deletion source/src/cip/cipidentity.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static EipStatus Reset(CipInstance *instance,
CipMessageRouterRequest *message_router_request,
/* pointer to message router request*/
CipMessageRouterResponse *message_router_response,
struct sockaddr_in *originator_address) { /* pointer to message router response*/
struct sockaddr *originator_address) { /* pointer to message router response*/
(void) instance;

EipStatus eip_status = kEipStatusOkSend;
Expand Down
20 changes: 10 additions & 10 deletions source/src/cip/cipidentity.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#ifndef OPENER_CIPIDENTITY_H_
Expand All @@ -15,19 +15,19 @@ static const int kIdentityClassCode = 0x01;
typedef enum {
kOwned = 0x0001, /**< Indicates that the device has an owner */
kConfigured = 0x0004, /**< Indicates that the device is configured to do
something different, than the out-of-the-box default. */
something different, than the out-of-the-box default. */
kMinorRecoverableFault = 0x0100, /**< Indicates that the device detected a
fault with itself, which was thought to be recoverable. The device did not
switch to a faulted state. */
fault with itself, which was thought to be recoverable. The device did not
switch to a faulted state. */
kMinorUncoverableFault = 0x0200, /**< Indicates that the device detected a
fault with itself, which was thought to be recoverable. The device did not
switch to a faulted state. */
fault with itself, which was thought to be recoverable. The device did not
switch to a faulted state. */
kMajorRecoveralbeFault = 0x0400, /**< Indicates that the device detected a
fault with itself,which was thought to be recoverable. The device changed
to the "Major Recoverable Fault" state */
fault with itself,which was thought to be recoverable. The device changed
to the "Major Recoverable Fault" state */
kMajorUnrecoverableFault = 0x0800 /**< Indicates that the device detected a
fault with itself,which was thought to be recoverable. The device changed
to the "Major Unrecoverable Fault" state */
fault with itself,which was thought to be recoverable. The device changed
to the "Major Unrecoverable Fault" state */
} CipIdentityStatus;

typedef enum {
Expand Down
10 changes: 7 additions & 3 deletions source/src/enet_encap/encap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cipconnectionmanager.h"
#include "cipidentity.h"
#include "generic_networkhandler.h"
#include "trace.h"

/*Identity data from cipidentity.c*/
extern EipUint16 vendor_id_;
Expand Down Expand Up @@ -488,6 +489,8 @@ void HandleReceivedRegisterSessionCommand(int socket,
EipStatus HandleReceivedUnregisterSessionCommand(
EncapsulationData *receive_data) {

OPENER_TRACE_INFO("encap.c: Unregister Session Command\n");

if ( (0 < receive_data->session_handle)
&& (receive_data->session_handle <=
OPENER_NUMBER_OF_SUPPORTED_SESSIONS) ) {
Expand Down Expand Up @@ -642,8 +645,8 @@ SessionStatus CheckRegisteredSessions(EncapsulationData *receive_data) {
}

void CloseSession(int socket) {
int i;
for (i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; ++i) {
OPENER_TRACE_INFO("encap.c: Close session\n");
for (size_t i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; ++i) {
if (g_registered_sessions[i] == socket) {
IApp_CloseSocket_tcp(socket);
g_registered_sessions[i] = kEipInvalidSocket;
Expand All @@ -653,7 +656,8 @@ void CloseSession(int socket) {
}

void EncapsulationShutDown(void) {
for (int i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; ++i) {
OPENER_TRACE_INFO("encap.c: Encapsulation shutdown\n");
for (size_t i = 0; i < OPENER_NUMBER_OF_SUPPORTED_SESSIONS; ++i) {
if (kEipInvalidSocket != g_registered_sessions[i]) {
IApp_CloseSocket_tcp(g_registered_sessions[i]);
g_registered_sessions[i] = kEipInvalidSocket;
Expand Down
4 changes: 2 additions & 2 deletions source/src/enet_encap/encap.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#ifndef OPENER_ENCAP_H_
Expand All @@ -18,7 +18,7 @@

/*** defines ***/

#define ENCAPSULATION_HEADER_LENGTH 24
#define ENCAPSULATION_HEADER_LENGTH 24

/** @brief Ethernet/IP standard port */
static const int kOpenerEthernetPort = 0xAF12;
Expand Down
2 changes: 1 addition & 1 deletion source/src/ports/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ opener_common_includes()
#######################################
opener_platform_support("INCLUDES")

set( PLATFORM_GENERIC_SRC generic_networkhandler.c)
set( PLATFORM_GENERIC_SRC generic_networkhandler.c socket_timer.c )

add_library( PLATFORM_GENERIC ${PLATFORM_GENERIC_SRC})
2 changes: 2 additions & 0 deletions source/src/ports/POSIX/networkhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "typedefs.h"

#define OPENER_SOCKET_WOULD_BLOCK EWOULDBLOCK

/** @brief Executes platform dependent network handler initialization code
*
* @return EipStatusOk if initialization was successful, otherwise EipStatusError
Expand Down
10 changes: 5 additions & 5 deletions source/src/ports/WIN32/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#include <stdio.h>
Expand Down Expand Up @@ -36,9 +36,9 @@ int main(int argc, char *arg[]) {
printf("Wrong number of command line parameters!\n");
printf("The correct command line parameters are:\n");
printf(
"./OpENer ipaddress subnetmask gateway domainname hostaddress macaddress\n");
"./OpENer ipaddress subnetmask gateway domainname hostaddress macaddress\n");
printf(
" e.g. ./OpENer 192.168.0.2 255.255.255.0 192.168.0.1 test.com testdevice 00 15 C5 BF D0 87\n");
" e.g. ./OpENer 192.168.0.2 255.255.255.0 192.168.0.1 test.com testdevice 00 15 C5 BF D0 87\n");
exit(0);
} else {
/* fetch Internet address info from the platform */
Expand Down Expand Up @@ -67,7 +67,7 @@ int main(int argc, char *arg[]) {
CipStackInit(nUniqueConnectionID);

/* Setup Network Handles */
if (kEipStatusOk == NetworkHandlerInitialize()) {
if ( kEipStatusOk == NetworkHandlerInitialize() ) {
g_end_stack = 0;
#ifndef WIN32
/* register for closing signals so that we can trigger the stack to end */
Expand All @@ -76,7 +76,7 @@ int main(int argc, char *arg[]) {

/* The event loop. Put other processing you need done continually in here */
while (1 != g_end_stack) {
if (kEipStatusOk != NetworkHandlerProcessOnce()) {
if ( kEipStatusOk != NetworkHandlerProcessOnce() ) {
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/src/ports/WIN32/networkhandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ void CloseSocketPlatform(int socket_handle) {
}

int SetSocketToNonBlocking(int socket_handle) {
u_long iMode = 0;
ioctlsocket(m_socket, FIONBIO, &iMode);
u_long iMode = 1;
return ioctlsocket(socket_handle, FIONBIO, &iMode);
}
2 changes: 2 additions & 0 deletions source/src/ports/WIN32/networkhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "typedefs.h"

#define OPENER_SOCKET_WOULD_BLOCK WSAEWOULDBLOCK

typedef unsigned long socklen_t;

EipStatus NetworkHandlerInitializePlatform(void);
Expand Down
33 changes: 18 additions & 15 deletions source/src/ports/WIN32/sample_application/opener_user_conf.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (c) 2009, Rockwell Automation, Inc.
* All rights reserved.
* All rights reserved.
*
******************************************************************************/
#ifndef OPENER_USER_CONF_H_
#define OPENER_USER_CONF_H_

/** @file
* @brief OpENer configuration setup
*
*
* This file contains the general application specific configuration for OpENer.
*
*
* Furthermore you have to specific platform specific network include files.
* OpENer needs definitions for the following data-types
* and functions:
Expand Down Expand Up @@ -85,18 +85,18 @@ typedef unsigned short in_port_t;
*/
#define OPENER_NUMBER_OF_SUPPORTED_SESSIONS 20

/** @brief The time in ms of the timer used in this implementations
/** @brief The time in ms of the timer used in this implementations
*/
static const int kOpenerTimerTickInMilliSeconds = 10;

/** @brief Define if RUN IDLE data is sent with consumed data
*/
*/
static const int kOpenerConsumedDataHasRunIdleHeader = 1;

/** @brief Define if RUN IDLE data is to be sent with produced data
*
* Per default we don't send run idle headers with produced data
*/
*
* Per default we don't send run idle headers with produced data
*/
static const int kOpenerProducedDataHasRunIdleHeader = 0;

#ifdef OPENER_WITH_TRACES
Expand All @@ -111,12 +111,15 @@ static const int kOpenerProducedDataHasRunIdleHeader = 0;
* further execution in an while(1) loop.
*/
#define OPENER_ASSERT(assertion) \
do { \
if(!(assertion)) { \
LOG_TRACE("Assertion \"%s\" failed: file \"%s\", line %d\n", #assertion, __FILE__, __LINE__); \
while(1){;} \
} \
} while(0)
do { \
if( !(assertion) ) { \
LOG_TRACE("Assertion \"%s\" failed: file \"%s\", line %d\n", \
# assertion, \
__FILE__, \
__LINE__); \
while(1) {;} \
} \
} while(0)

/* else use standard assert() */
//#include <assert.h>
Expand All @@ -142,7 +145,7 @@ static const int kOpenerProducedDataHasRunIdleHeader = 0;
#endif

/** @brief The number of bytes used for the Ethernet message buffer on
* the pc port. For different platforms it may makes sense to
* the pc port. For different platforms it may makes sense to
* have more than one buffer.
*
* This buffer size will be used for any received message.
Expand Down
Loading

0 comments on commit 0bb238a

Please sign in to comment.