OpENer - Open Source EtherNet/IP(TM) I/O Target Stack  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
networkhandler.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2009, Rockwell Automation, Inc.
3  * All rights reserved.
4  *
5  ******************************************************************************/
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <time.h>
9 #include <fcntl.h>
10 
11 #include "networkhandler.h"
12 
13 #include "opener_error.h"
14 #include "trace.h"
15 #include "encap.h"
16 #include "opener_user_conf.h"
17 
19  struct timespec now = { .tv_nsec = 0, .tv_sec = 0 };
20 
21  int error = clock_gettime( CLOCK_MONOTONIC, &now );
22  OPENER_ASSERT(-1 != error)
23  MicroSeconds micro_seconds = (MicroSeconds)now.tv_nsec / 1000ULL +
24  now.tv_sec * 1000000ULL;
25  return micro_seconds;
26 }
27 
29  return (MilliSeconds) (GetMicroSeconds() / 1000ULL);
30 }
31 
33  /* Add platform dependent code here if necessary */
34  return kEipStatusOk;
35 }
36 
37 void CloseSocketPlatform(int socket_handle) {
38  if(0 != shutdown(socket_handle, SHUT_RDWR) ) {
39  int error_code = GetSocketErrorNumber();
40  char *error_message = GetErrorMessage(error_code);
41  OPENER_TRACE_ERR("Could not close socket %d - Error Code: %d - %s\n",
42  socket_handle,
43  error_code,
44  error_message);
45  FreeErrorMessage(error_message);
46  }
47  close(socket_handle);
48 }
49 
50 int SetSocketToNonBlocking(int socket_handle) {
51  return fcntl(socket_handle, F_SETFL, fcntl(socket_handle,
52  F_GETFL,
53  0) | O_NONBLOCK);
54 }
55 
56 int SetQosOnSocket(const int socket,
57  CipUsint qos_value) {
58  int set_tos = qos_value;
59  return setsockopt(socket, IPPROTO_IP, IP_TOS, &set_tos, sizeof(set_tos) );
60 }
MilliSeconds GetMilliSeconds(void)
This function shall return the current time in milliseconds relative to epoch, and shall be implement...
void CloseSocketPlatform(int socket_handle)
Platform dependent code to close a socket.
Tracing infrastructure for OpENer.
char * GetErrorMessage(int error_number)
Returns a human readable message for the given error number.
Definition: opener_error.c:21
This file includes the prototypes for error resolution functions like strerror_r or WSAGetLastError...
#define OPENER_ASSERT(assertion)
unsigned long long MicroSeconds
Definition: typedefs.h:69
#define OPENER_TRACE_ERR(...)
Definition: trace.h:86
int SetSocketToNonBlocking(int socket_handle)
EipStatus
EIP stack status enum.
Definition: typedefs.h:93
uint8_t CipUsint
Definition: typedefs.h:46
MicroSeconds GetMicroSeconds(void)
This function shall return the current time in microseconds relative to epoch, and shall be implement...
int SetQosOnSocket(int socket, CipUsint qos_value)
EipStatus NetworkHandlerInitializePlatform(void)
Executes platform dependent network handler initialization code.
This file contains the public interface of the encapsulation layer.
unsigned long MilliSeconds
Definition: typedefs.h:68
void FreeErrorMessage(char *error_message)
Frees the space of the error message generated by GetErrorMessage(int)
Definition: opener_error.c:34
int GetSocketErrorNumber()
Gets the error number or equivalent.
Definition: opener_error.c:17