From 2f7174407949b8a45d7295d68bdee9231a28a5a3 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Tue, 24 Jul 2018 06:31:39 +0200 Subject: [PATCH] - added serial port hal - moved hal to separate directory - added new hal cmake project --- CMakeLists.txt | 12 +- hal/CMakeLists.txt | 107 +++++++ {src/hal => hal}/ethernet/bsd/ethernet_bsd.c | 0 .../ethernet/linux/ethernet_linux.c | 0 .../ethernet/win32/ethernet_win32.c | 0 .../filesystem/linux/file_provider_linux.c | 0 .../filesystem/win32/file_provider_win32.c | 0 {src/hal => hal}/inc/hal_ethernet.h | 0 {src/hal => hal}/inc/hal_filesystem.h | 0 {src/hal => hal}/inc/hal_socket.h | 0 {src/hal => hal}/inc/hal_thread.h | 0 {src/hal => hal}/inc/hal_time.h | 0 {src/hal => hal}/inc/platform_endian.h | 0 hal/serial/linux/serial_port_linux.c | 277 +++++++++++++++++ hal/serial/win32/serial_port_win32.c | 283 ++++++++++++++++++ {src/hal => hal}/socket/bsd/socket_bsd.c | 0 {src/hal => hal}/socket/linux/socket_linux.c | 0 {src/hal => hal}/socket/win32/socket_win32.c | 0 {src/hal => hal}/thread/bsd/thread_bsd.c | 0 {src/hal => hal}/thread/linux/thread_linux.c | 0 {src/hal => hal}/thread/win32/thread_win32.c | 0 {src/hal => hal}/time/unix/time.c | 0 {src/hal => hal}/time/win32/time.c | 0 src/CMakeLists.txt | 22 +- 24 files changed, 681 insertions(+), 20 deletions(-) create mode 100644 hal/CMakeLists.txt rename {src/hal => hal}/ethernet/bsd/ethernet_bsd.c (100%) rename {src/hal => hal}/ethernet/linux/ethernet_linux.c (100%) rename {src/hal => hal}/ethernet/win32/ethernet_win32.c (100%) rename {src/hal => hal}/filesystem/linux/file_provider_linux.c (100%) rename {src/hal => hal}/filesystem/win32/file_provider_win32.c (100%) rename {src/hal => hal}/inc/hal_ethernet.h (100%) rename {src/hal => hal}/inc/hal_filesystem.h (100%) rename {src/hal => hal}/inc/hal_socket.h (100%) rename {src/hal => hal}/inc/hal_thread.h (100%) rename {src/hal => hal}/inc/hal_time.h (100%) rename {src/hal => hal}/inc/platform_endian.h (100%) create mode 100644 hal/serial/linux/serial_port_linux.c create mode 100644 hal/serial/win32/serial_port_win32.c rename {src/hal => hal}/socket/bsd/socket_bsd.c (100%) rename {src/hal => hal}/socket/linux/socket_linux.c (100%) rename {src/hal => hal}/socket/win32/socket_win32.c (100%) rename {src/hal => hal}/thread/bsd/thread_bsd.c (100%) rename {src/hal => hal}/thread/linux/thread_linux.c (100%) rename {src/hal => hal}/thread/win32/thread_win32.c (100%) rename {src/hal => hal}/time/unix/time.c (100%) rename {src/hal => hal}/time/win32/time.c (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 55b76d0d..07fbcdb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,11 +92,11 @@ include_directories( ) set(API_HEADERS - src/hal/inc/hal_time.h - src/hal/inc/hal_thread.h - src/hal/inc/hal_filesystem.h - src/hal/inc/hal_ethernet.h - src/hal/inc/platform_endian.h + hal/inc/hal_time.h + hal/inc/hal_thread.h + hal/inc/hal_filesystem.h + hal/inc/hal_ethernet.h + hal/inc/platform_endian.h src/common/inc/libiec61850_common_api.h src/common/inc/libiec61850_platform_includes.h src/common/inc/linked_list.h @@ -161,6 +161,8 @@ configure_file( ${CMAKE_CURRENT_BINARY_DIR}/config/stack_config.h ) +include("${CMAKE_CURRENT_LIST_DIR}/hal/CMakeLists.txt") + if(BUILD_EXAMPLES) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/examples) endif(BUILD_EXAMPLES) diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt new file mode 100644 index 00000000..b2803309 --- /dev/null +++ b/hal/CMakeLists.txt @@ -0,0 +1,107 @@ +cmake_minimum_required(VERSION 2.8) + +# automagically detect if we should cross-compile +if(DEFINED ENV{TOOLCHAIN}) + set(CMAKE_C_COMPILER $ENV{TOOLCHAIN}gcc) + set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN}g++) + set(CMAKE_AR "$ENV{TOOLCHAIN}ar" CACHE FILEPATH "CW archiver" FORCE) +endif() + +project(hal) + +set(LIBHAL_VERSION_MAJOR "2") +set(LIBHAL_VERSION_MINOR "0") +set(LIBHAL_VERSION_PATCH "0") + +include_directories( + ${CMAKE_CURRENT_LIST_DIR}/inc +) + +set (libhal_linux_SRCS + ${CMAKE_CURRENT_LIST_DIR}/socket/linux/socket_linux.c + ${CMAKE_CURRENT_LIST_DIR}/ethernet/linux/ethernet_linux.c + ${CMAKE_CURRENT_LIST_DIR}/thread/linux/thread_linux.c + ${CMAKE_CURRENT_LIST_DIR}/filesystem/linux/file_provider_linux.c + ${CMAKE_CURRENT_LIST_DIR}/time/unix/time.c + ${CMAKE_CURRENT_LIST_DIR}/serial/linux/serial_port_linux.c +) + +set (libhal_windows_SRCS + ${CMAKE_CURRENT_LIST_DIR}/socket/win32/socket_win32.c + ${CMAKE_CURRENT_LIST_DIR}/ethernet/win32/ethernet_win32.c + ${CMAKE_CURRENT_LIST_DIR}/thread/win32/thread_win32.c + ${CMAKE_CURRENT_LIST_DIR}/filesystem/win32/file_provider_win32.c + ${CMAKE_CURRENT_LIST_DIR}/time/win32/time.c + ${CMAKE_CURRENT_LIST_DIR}/serial/win32/serial_port_win32.c +) + +set (libhal_bsd_SRCS + ${CMAKE_CURRENT_LIST_DIR}/socket/bsd/socket_bsd.c + ${CMAKE_CURRENT_LIST_DIR}/ethernet/bsd/ethernet_bsd.c + ${CMAKE_CURRENT_LIST_DIR}/thread/bsd/thread_bsd.c + ${CMAKE_CURRENT_LIST_DIR}/filesystem/linux/file_provider_linux.c + ${CMAKE_CURRENT_LIST_DIR}/time/unix/time.c +) + +IF(WIN32) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../third_party/winpcap/Lib/wpcap.lib") +message("Found winpcap -> can compile with GOOSE support") +set(WITH_WPCAP 1) +endif() + + + +set (libhal_SRCS + ${libhal_windows_SRCS} +) + +ELSEIF(UNIX) +IF(APPLE) +set (libhal_SRCS + ${libhal_bsd_SRCS} +) +ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") +set (libhal_SRCS + ${libhal_bsd_SRCS}} +) +ELSE() +set (libhal_SRCS + ${libhal_linux_SRCS} +) +ENDIF(APPLE) +ENDIF(WIN32) + +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" ) +#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC" ) + +add_library (hal STATIC ${libhal_SRCS}) + +add_library (hal-shared STATIC ${libhal_SRCS}) + +SET_TARGET_PROPERTIES(hal-shared PROPERTIES + COMPILE_FLAGS "-fPIC" +) + +IF(UNIX) + IF (CONFIG_SYSTEM_HAS_CLOCK_GETTIME) + target_link_libraries (hal + -lpthread + -lrt + ) + ELSE () + target_link_libraries (hal + -lpthread + ) + ENDIF (CONFIG_SYSTEM_HAS_CLOCK_GETTIME) +ENDIF(UNIX) +IF(MINGW) + target_link_libraries(hal ws2_32 iphlpapi) +ENDIF(MINGW) + +iF(WITH_WPCAP) +target_link_libraries(hal + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/winpcap/lib/wpcap.lib + ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/winpcap/lib/packet.lib +) +ENDIF(WITH_WPCAP) diff --git a/src/hal/ethernet/bsd/ethernet_bsd.c b/hal/ethernet/bsd/ethernet_bsd.c similarity index 100% rename from src/hal/ethernet/bsd/ethernet_bsd.c rename to hal/ethernet/bsd/ethernet_bsd.c diff --git a/src/hal/ethernet/linux/ethernet_linux.c b/hal/ethernet/linux/ethernet_linux.c similarity index 100% rename from src/hal/ethernet/linux/ethernet_linux.c rename to hal/ethernet/linux/ethernet_linux.c diff --git a/src/hal/ethernet/win32/ethernet_win32.c b/hal/ethernet/win32/ethernet_win32.c similarity index 100% rename from src/hal/ethernet/win32/ethernet_win32.c rename to hal/ethernet/win32/ethernet_win32.c diff --git a/src/hal/filesystem/linux/file_provider_linux.c b/hal/filesystem/linux/file_provider_linux.c similarity index 100% rename from src/hal/filesystem/linux/file_provider_linux.c rename to hal/filesystem/linux/file_provider_linux.c diff --git a/src/hal/filesystem/win32/file_provider_win32.c b/hal/filesystem/win32/file_provider_win32.c similarity index 100% rename from src/hal/filesystem/win32/file_provider_win32.c rename to hal/filesystem/win32/file_provider_win32.c diff --git a/src/hal/inc/hal_ethernet.h b/hal/inc/hal_ethernet.h similarity index 100% rename from src/hal/inc/hal_ethernet.h rename to hal/inc/hal_ethernet.h diff --git a/src/hal/inc/hal_filesystem.h b/hal/inc/hal_filesystem.h similarity index 100% rename from src/hal/inc/hal_filesystem.h rename to hal/inc/hal_filesystem.h diff --git a/src/hal/inc/hal_socket.h b/hal/inc/hal_socket.h similarity index 100% rename from src/hal/inc/hal_socket.h rename to hal/inc/hal_socket.h diff --git a/src/hal/inc/hal_thread.h b/hal/inc/hal_thread.h similarity index 100% rename from src/hal/inc/hal_thread.h rename to hal/inc/hal_thread.h diff --git a/src/hal/inc/hal_time.h b/hal/inc/hal_time.h similarity index 100% rename from src/hal/inc/hal_time.h rename to hal/inc/hal_time.h diff --git a/src/hal/inc/platform_endian.h b/hal/inc/platform_endian.h similarity index 100% rename from src/hal/inc/platform_endian.h rename to hal/inc/platform_endian.h diff --git a/hal/serial/linux/serial_port_linux.c b/hal/serial/linux/serial_port_linux.c new file mode 100644 index 00000000..e6308c05 --- /dev/null +++ b/hal/serial/linux/serial_port_linux.c @@ -0,0 +1,277 @@ +/* + * serial_port_linux.c + * + * Copyright 2017 MZ Automation GmbH + * + * This file is part of lib60870-C + * + * lib60870-C is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * lib60870-C is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with lib60870-C. If not, see . + * + * See COPYING file for the complete license text. + */ + +#include "lib_memory.h" + +#include +#include +#include +#include +#include +#include + +#include "hal_serial.h" +#include "hal_time.h" + +struct sSerialPort { + char interfaceName[100]; + int fd; + int baudRate; + uint8_t dataBits; + char parity; + uint8_t stopBits; + uint64_t lastSentTime; + struct timeval timeout; + SerialPortError lastError; +}; + + +SerialPort +SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits) +{ + SerialPort self = (SerialPort) GLOBAL_MALLOC(sizeof(struct sSerialPort)); + + if (self != NULL) { + self->fd = -1; + self->baudRate = baudRate; + self->dataBits = dataBits; + self->stopBits = stopBits; + self->parity = parity; + self->lastSentTime = 0; + self->timeout.tv_sec = 0; + self->timeout.tv_usec = 100000; /* 100 ms */ + strncpy(self->interfaceName, interfaceName, 100); + self->lastError = SERIAL_PORT_ERROR_NONE; + } + + return self; +} + +void +SerialPort_destroy(SerialPort self) +{ + if (self != NULL) { + GLOBAL_FREEMEM(self); + } +} + +bool +SerialPort_open(SerialPort self) +{ + self->fd = open(self->interfaceName, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL); + + if (self->fd == -1) { + self->lastError = SERIAL_PORT_ERROR_OPEN_FAILED; + return false; + } + + struct termios tios; + speed_t baudrate; + + tcgetattr(self->fd, &tios); + + switch (self->baudRate) { + case 110: + baudrate = B110; + break; + case 300: + baudrate = B300; + break; + case 600: + baudrate = B600; + break; + case 1200: + baudrate = B1200; + break; + case 2400: + baudrate = B2400; + break; + case 4800: + baudrate = B4800; + break; + case 9600: + baudrate = B9600; + break; + case 19200: + baudrate = B19200; + break; + case 38400: + baudrate = B38400; + break; + case 57600: + baudrate = B57600; + break; + case 115200: + baudrate = B115200; + break; + default: + baudrate = B9600; + self->lastError = SERIAL_PORT_ERROR_INVALID_BAUDRATE; + } + + /* Set baud rate */ + if ((cfsetispeed(&tios, baudrate) < 0) || (cfsetospeed(&tios, baudrate) < 0)) { + close(self->fd); + self->fd = -1; + self->lastError = SERIAL_PORT_ERROR_INVALID_BAUDRATE; + return false; + } + + tios.c_cflag |= (CREAD | CLOCAL); + + /* Set data bits (5/6/7/8) */ + tios.c_cflag &= ~CSIZE; + switch (self->dataBits) { + case 5: + tios.c_cflag |= CS5; + break; + case 6: + tios.c_cflag |= CS6; + break; + case 7: + tios.c_cflag |= CS7; + break; + case 8: + default: + tios.c_cflag |= CS8; + break; + } + + /* Set stop bits (1/2) */ + if (self->stopBits == 1) + tios.c_cflag &=~ CSTOPB; + else /* 2 */ + tios.c_cflag |= CSTOPB; + + if (self->parity == 'N') { + tios.c_cflag &=~ PARENB; + } else if (self->parity == 'E') { + tios.c_cflag |= PARENB; + tios.c_cflag &=~ PARODD; + } else { /* 'O' */ + tios.c_cflag |= PARENB; + tios.c_cflag |= PARODD; + } + + tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + + if (self->parity == 'N') { + tios.c_iflag &= ~INPCK; + } else { + tios.c_iflag |= INPCK; + } + + tios.c_iflag &= ~(IXON | IXOFF | IXANY); + tios.c_iflag |= IGNBRK; /* Set ignore break to allow 0xff characters */ + tios.c_iflag |= IGNPAR; + tios.c_oflag &=~ OPOST; + + tios.c_cc[VMIN] = 0; + tios.c_cc[VTIME] = 0; + + if (tcsetattr(self->fd, TCSANOW, &tios) < 0) { + close(self->fd); + self->fd = -1; + self->lastError = SERIAL_PORT_ERROR_INVALID_ARGUMENT; + + return false; + } + + return true; +} + +void +SerialPort_close(SerialPort self) +{ + if (self->fd != -1) { + close(self->fd); + self->fd = 0; + } +} + +int +SerialPort_getBaudRate(SerialPort self) +{ + return self->baudRate; +} + +void +SerialPort_discardInBuffer(SerialPort self) +{ + tcflush(self->fd, TCIOFLUSH); +} + +void +SerialPort_setTimeout(SerialPort self, int timeout) +{ + self->timeout.tv_sec = timeout / 1000; + self->timeout.tv_usec = (timeout % 1000) * 1000; +} + +SerialPortError +SerialPort_getLastError(SerialPort self) +{ + return self->lastError; +} + +int +SerialPort_readByte(SerialPort self) +{ + uint8_t buf[1]; + fd_set set; + + self->lastError = SERIAL_PORT_ERROR_NONE; + + FD_ZERO(&set); + FD_SET(self->fd, &set); + + int ret = select(self->fd + 1, &set, NULL, NULL, &(self->timeout)); + + if (ret == -1) { + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + return -1; + } + else if (ret == 0) + return -1; + else { + read(self->fd, (char*) buf, 1); + + return (int) buf[0]; + } +} + +int +SerialPort_write(SerialPort self, uint8_t* buffer, int startPos, int bufSize) +{ + //TODO assure minimum line idle time + + self->lastError = SERIAL_PORT_ERROR_NONE; + + ssize_t result = write(self->fd, buffer + startPos, bufSize); + + tcdrain(self->fd); + + self->lastSentTime = Hal_getTimeInMs(); + + return result; +} diff --git a/hal/serial/win32/serial_port_win32.c b/hal/serial/win32/serial_port_win32.c new file mode 100644 index 00000000..19eb30bf --- /dev/null +++ b/hal/serial/win32/serial_port_win32.c @@ -0,0 +1,283 @@ +/* +* serial_port_win32.c +* +* Copyright 2017 MZ Automation GmbH +* +* This file is part of lib60870-C +* +* lib60870-C is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* lib60870-C is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with lib60870-C. If not, see . +* +* See COPYING file for the complete license text. +*/ + +#include +#include + +#include + +#include "lib_memory.h" + +#include "hal_serial.h" +#include "hal_time.h" + +struct sSerialPort { + char interfaceName[100]; + HANDLE comPort; + int baudRate; + uint8_t dataBits; + char parity; + uint8_t stopBits; + uint64_t lastSentTime; + int timeout; + SerialPortError lastError; +}; + +SerialPort +SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits) +{ + SerialPort self = (SerialPort)GLOBAL_MALLOC(sizeof(struct sSerialPort)); + + if (self != NULL) { + self->comPort = NULL; + self->baudRate = baudRate; + self->dataBits = dataBits; + self->stopBits = stopBits; + self->parity = parity; + self->lastSentTime = 0; + self->timeout = 100; /* 100 ms */ + strncpy(self->interfaceName, interfaceName, 100); + self->lastError = SERIAL_PORT_ERROR_NONE; + } + + return self; +} + +void +SerialPort_destroy(SerialPort self) +{ + if (self != NULL) { + + if (self->comPort != INVALID_HANDLE_VALUE) + SerialPort_close(self); + + GLOBAL_FREEMEM(self); + } +} + +bool +SerialPort_open(SerialPort self) +{ + self->comPort = CreateFile(self->interfaceName, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, 0, NULL); + + + if (self->comPort == INVALID_HANDLE_VALUE) { + self->lastError = SERIAL_PORT_ERROR_OPEN_FAILED; + return false; + } + + DCB _serialParams = { 0 }; + _serialParams.DCBlength = sizeof(DCB); + + LPDCB serialParams = &_serialParams; + + BOOL status = GetCommState(self->comPort, serialParams); + + if (status == false) { + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + goto exit_error; + } + + /* set baud rate */ + switch (self->baudRate) { + case 110: + serialParams->BaudRate = CBR_110; + break; + case 300: + serialParams->BaudRate = CBR_300; + break; + case 600: + serialParams->BaudRate = CBR_600; + break; + case 1200: + serialParams->BaudRate = CBR_1200; + break; + case 2400: + serialParams->BaudRate = CBR_2400; + break; + case 4800: + serialParams->BaudRate = CBR_4800; + break; + case 9600: + serialParams->BaudRate = CBR_9600; + break; + case 19200: + serialParams->BaudRate = CBR_19200; + break; + case 38400: + serialParams->BaudRate = CBR_38400; + break; + case 57600: + serialParams->BaudRate = CBR_57600; + break; + case 115200: + serialParams->BaudRate = CBR_115200; + break; + default: + serialParams->BaudRate = CBR_9600; + self->lastError = SERIAL_PORT_ERROR_INVALID_BAUDRATE; + } + + /* Set data bits (5/6/7/8) */ + serialParams->ByteSize = self->dataBits; + + /* Set stop bits (1/2) */ + if (self->stopBits == 1) + serialParams->StopBits = ONESTOPBIT; + else /* 2 */ + serialParams->StopBits = TWOSTOPBITS; + + if (self->parity == 'N') + serialParams->Parity = NOPARITY; + else if (self->parity == 'E') + serialParams->Parity = EVENPARITY; + else /* 'O' */ + serialParams->Parity = ODDPARITY; + + status = SetCommState(self->comPort, serialParams); + + if (status == false) { + self->lastError = SERIAL_PORT_ERROR_INVALID_ARGUMENT; + goto exit_error; + } + + COMMTIMEOUTS timeouts = { 0 }; + + timeouts.ReadIntervalTimeout = 100; + timeouts.ReadTotalTimeoutConstant = 50; + timeouts.ReadTotalTimeoutMultiplier = 10; + timeouts.WriteTotalTimeoutConstant = 100; + timeouts.WriteTotalTimeoutMultiplier = 10; + + status = SetCommTimeouts(self->comPort, &timeouts); + + if (status == false) { + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + goto exit_error; + } + + status = SetCommMask(self->comPort, EV_RXCHAR); + + if (status == false) { + printf("SetCommMask failed!\n"); + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + goto exit_error; + } + + self->lastError = SERIAL_PORT_ERROR_NONE; + + return true; + +exit_error: + + if (self->comPort != INVALID_HANDLE_VALUE) { + CloseHandle(self->comPort); + self->comPort = INVALID_HANDLE_VALUE; + } + + return false; +} + + +void +SerialPort_close(SerialPort self) +{ + if (self->comPort != INVALID_HANDLE_VALUE) { + CloseHandle(self->comPort); + self->comPort = INVALID_HANDLE_VALUE; + } +} + +int +SerialPort_getBaudRate(SerialPort self) +{ + return self->baudRate; +} + +void +SerialPort_discardInBuffer(SerialPort self) +{ + PurgeComm(self->comPort, PURGE_RXCLEAR | PURGE_TXCLEAR); +} + +void +SerialPort_setTimeout(SerialPort self, int timeout) +{ + self->timeout = timeout; +} + +SerialPortError +SerialPort_getLastError(SerialPort self) +{ + return self->lastError; +} + +int +SerialPort_readByte(SerialPort self) +{ + uint8_t buf[1]; + + DWORD bytesRead = 0; + + BOOL status = ReadFile(self->comPort, buf, 1, &bytesRead, NULL); + + if (status == false) { + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + return -1; + } + + self->lastError = SERIAL_PORT_ERROR_NONE; + + if (bytesRead == 0) + return -1; + else + return (int) buf[0]; +} + +int +SerialPort_write(SerialPort self, uint8_t* buffer, int startPos, int bufSize) +{ + //TODO assure minimum line idle time + + self->lastError = SERIAL_PORT_ERROR_NONE; + + DWORD numberOfBytesWritten; + + BOOL status = WriteFile(self->comPort, buffer + startPos, bufSize, &numberOfBytesWritten, NULL); + + if (status == false) { + self->lastError = SERIAL_PORT_ERROR_UNKNOWN; + return -1; + } + + status = FlushFileBuffers(self->comPort); + + if (status == false) { + printf("FlushFileBuffers failed!\n"); + } + + self->lastSentTime = Hal_getTimeInMs(); + + return (int) numberOfBytesWritten; +} diff --git a/src/hal/socket/bsd/socket_bsd.c b/hal/socket/bsd/socket_bsd.c similarity index 100% rename from src/hal/socket/bsd/socket_bsd.c rename to hal/socket/bsd/socket_bsd.c diff --git a/src/hal/socket/linux/socket_linux.c b/hal/socket/linux/socket_linux.c similarity index 100% rename from src/hal/socket/linux/socket_linux.c rename to hal/socket/linux/socket_linux.c diff --git a/src/hal/socket/win32/socket_win32.c b/hal/socket/win32/socket_win32.c similarity index 100% rename from src/hal/socket/win32/socket_win32.c rename to hal/socket/win32/socket_win32.c diff --git a/src/hal/thread/bsd/thread_bsd.c b/hal/thread/bsd/thread_bsd.c similarity index 100% rename from src/hal/thread/bsd/thread_bsd.c rename to hal/thread/bsd/thread_bsd.c diff --git a/src/hal/thread/linux/thread_linux.c b/hal/thread/linux/thread_linux.c similarity index 100% rename from src/hal/thread/linux/thread_linux.c rename to hal/thread/linux/thread_linux.c diff --git a/src/hal/thread/win32/thread_win32.c b/hal/thread/win32/thread_win32.c similarity index 100% rename from src/hal/thread/win32/thread_win32.c rename to hal/thread/win32/thread_win32.c diff --git a/src/hal/time/unix/time.c b/hal/time/unix/time.c similarity index 100% rename from src/hal/time/unix/time.c rename to hal/time/unix/time.c diff --git a/src/hal/time/win32/time.c b/hal/time/win32/time.c similarity index 100% rename from src/hal/time/win32/time.c rename to hal/time/win32/time.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b5424e1..f8cad77b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,27 +195,12 @@ set (lib_sv_SRCS ) set (lib_linux_SRCS -./hal/socket/linux/socket_linux.c -./hal/ethernet/linux/ethernet_linux.c -./hal/thread/linux/thread_linux.c -./hal/filesystem/linux/file_provider_linux.c -./hal/time/unix/time.c ) set (lib_windows_SRCS -./hal/socket/win32/socket_win32.c -./hal/ethernet/win32/ethernet_win32.c -./hal/thread/win32/thread_win32.c -./hal/filesystem/win32/file_provider_win32.c -./hal/time/win32/time.c ) set (lib_bsd_SRCS -./hal/socket/bsd/socket_bsd.c -./hal/ethernet/bsd/ethernet_bsd.c -./hal/thread/bsd/thread_bsd.c -./hal/filesystem/linux/file_provider_linux.c -./hal/time/unix/time.c ) IF(WIN32) @@ -324,6 +309,9 @@ set_target_properties(iec61850-shared PROPERTIES SOVERSION "${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}.${LIB_VERSION_PATCH}" ) +target_link_libraries(iec61850-shared + hal-shared +) GENERATE_EXPORT_HEADER(iec61850-shared BASE_NAME iec61850-shared @@ -334,6 +322,10 @@ GENERATE_EXPORT_HEADER(iec61850-shared add_library (iec61850 STATIC ${library_SRCS}) +target_link_libraries(iec61850 + hal +) + IF(UNIX) IF (CONFIG_SYSTEM_HAS_CLOCK_GETTIME) target_link_libraries (iec61850