Skip to content

Commit

Permalink
Merge pull request #9 from janekbaraniewski/cleanup-and-logging
Browse files Browse the repository at this point in the history
Cleanup, logs improvements, refactoring
  • Loading branch information
janekbaraniewski committed May 5, 2024
2 parents 8f6e548 + a72a496 commit d0872f7
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 218 deletions.
44 changes: 9 additions & 35 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@ jobs:
with:
python-version: '3.x'

# - name: Install dependencies (Ubuntu)
# if: startsWith(matrix.config.os, 'ubuntu')
# run: |
# sudo apt-get update
# sudo apt-get install -y libboost-all-dev

# - name: Install dependencies (macOS)
# if: startsWith(matrix.config.os, 'macos')
# run: |
# brew install boost

- name: Install dependencies (Windows)
if: startsWith(matrix.config.os, 'windows')
run: |
choco install boost-msvc-14.2
- name: Set up CMake (All platforms)
uses: lukka/get-cmake@latest

Expand All @@ -60,15 +44,17 @@ jobs:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_ARCHITECTURE_ID=${{ matrix.config.arch }}
cmake --build build --config Release
- name: Rename binary
- name: Rename binary using sed and set env
run: |
mv build/ser2net2ser build/ser2net2ser-${{ env.VERSION }}-${{ matrix.config.os }}-${{ matrix.config.arch }}
OS_NAME=$(echo "${{ matrix.config.os }}" | sed 's/ubuntu-latest/linux/; s/macos-latest/darwin/')
echo "OS_NAME=$OS_NAME" >> $GITHUB_ENV
mv build/ser2net2ser build/ser2net2ser-${{ env.VERSION }}-$OS_NAME-${{ matrix.config.arch }}
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: ser2net2ser-${{ env.VERSION }}-${{ matrix.config.os }}-${{ matrix.config.arch }}
path: build/ser2net2ser-${{ env.VERSION }}-${{ matrix.config.os }}-${{ matrix.config.arch }}
name: ser2net2ser-${{ env.VERSION }}-${{ env.OS_NAME }}-${{ matrix.config.arch }}
path: build/ser2net2ser-${{ env.VERSION }}-${{ env.OS_NAME }}-${{ matrix.config.arch }}

create-release:
needs: build-and-release
Expand All @@ -85,27 +71,15 @@ jobs:
- name: List all files in artifacts directory
run: ls -R artifacts

# - name: Create Release
# id: create_release
# uses: actions/create-release@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# tag_name: ${{ github.ref }}
# release_name: Release ${{ github.ref_name }}
# draft: false
# prerelease: false

- name: debug
- name: Debug file locations
run: |
pwd
ls -la artifacts/
find artifacts/ -type f
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: artifacts/ser2net2ser*
file: artifacts/*/* # Changed this line to include subdirectories
tag: ${{ github.ref }}
release_name: Release ${{ github.ref_name }}
overwrite: true
Expand Down
55 changes: 38 additions & 17 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,44 @@ on:
branches: [ master ]

jobs:
build:
statick-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up CMake (All platforms)
uses: lukka/get-cmake@latest

- name: Set up Clang
uses: egor-tensin/setup-clang@v1
with:
version: latest

- name: Install dependencies
run: sudo apt-get install -y cppcheck clang-format

- name: Run Cppcheck
run: cppcheck --enable=all --std=c++17 --language=c++ --error-exitcode=1 --output-file=cppcheck_report.txt ./src
continue-on-error: true

- name: Check code format (Clang-Format)
run: clang-format -i -style=file $(find . -name '*.cpp' -or -name '*.h')

- name: Upload Cppcheck Results
uses: actions/upload-artifact@v2
if: always()
with:
name: cppcheck-results
path: cppcheck_report.txt

- name: Upload Clang-Format Results
uses: actions/upload-artifact@v2
if: always()
with:
name: format-results
path: $(find . -name '*.cpp' -or -name '*.h')

tests:
runs-on: ${{ matrix.config.os }}
strategy:
fail-fast: false
Expand All @@ -29,22 +66,6 @@ jobs:
with:
python-version: '3.x'

# - name: Install dependencies (Ubuntu)
# if: startsWith(matrix.config.os, 'ubuntu')
# run: |
# sudo apt-get update
# sudo apt-get install -y libboost-all-dev

# - name: Install dependencies (macOS)
# if: startsWith(matrix.config.os, 'macos')
# run: |
# brew install boost

# - name: Install dependencies (Windows)
# if: startsWith(matrix.config.os, 'windows')
# run: |
# choco install boost-msvc-14.2

- name: Set up CMake (All platforms)
uses: lukka/get-cmake@latest

Expand Down
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
# Create core library without Logger.cpp, which is now in the logging library
add_library(core_lib
src/Logger.cpp
src/SerialServer.cpp
src/SerialClient.cpp
src/VirtualSerialPort.cpp
src/SerialPort.cpp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Motivation

I've been using `socat` + `ser2net` for sharing serial devices over network for some time, mainly in my other project -> [kubeserial]. I've run into few limitations so I created this project to recreate the behaviour of `socat` + `ser2net` and then solve connection issues I've been having.
I've been using `socat` + `ser2net` for sharing serial devices over network for some time, mainly in my other project [kubeserial]. I've run into few limitations so I created this project to recreate the behaviour of `socat` + `ser2net` and then solve connection issues I've been having.

## Requirements

Expand Down
Binary file modified docs/connect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/serve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <sstream>
#include <mutex>
#include <string>
#include <iostream>
#include <chrono>
#include <iomanip>

class Logger {
public:
Expand Down
5 changes: 5 additions & 0 deletions include/SerialPort.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <cstring>
#include <stdexcept>
#include <thread>
#include <atomic>
#include <queue>
Expand Down
25 changes: 0 additions & 25 deletions include/SerialServer.h

This file was deleted.

8 changes: 4 additions & 4 deletions include/SocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ class SocketClient {
Logger(Logger::Level::Info) << "Start connecting to socet server:" << server_ip << ":" << server_port;
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
Logger(Logger::Level::Error) << "Error creating socket: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Error creating socket: " << strerror(errno);
return false;
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
if (inet_pton(AF_INET, server_ip, &server_addr.sin_addr) <= 0) {
Logger(Logger::Level::Error) << "Invalid address/ Address not supported" << std::endl;
Logger(Logger::Level::Error) << "Invalid address/ Address not supported";
return false;
}

if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
Logger(Logger::Level::Error) << "Connection Failed: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Connection Failed: " << strerror(errno);
exit(1);
return false;
}

Logger(Logger::Level::Info) << "Connected to server at " << server_ip << ":" << server_port << std::endl;
Logger(Logger::Level::Info) << "Connected to server at " << server_ip << ":" << server_port;
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions include/TCPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <thread>
#include <cstring>
#include <iostream>

#include "SerialPort.h"
#include "Logger.h"

Expand Down
3 changes: 0 additions & 3 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include "Logger.h"
#include <iostream>
#include <chrono>
#include <iomanip>

std::mutex Logger::mtx;

Expand Down
11 changes: 5 additions & 6 deletions src/SerialClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using std::string;
using std::cout;
using std::cerr;
using std::endl;

SerialClient::SerialClient(const std::string& server_ip, unsigned short server_port, const std::string& vsp_name)
: vsp_(vsp_name) {
Expand All @@ -25,7 +24,7 @@ void SerialClient::run() {
FD_SET(socketClient_.sock_fd, &read_fds);

if (select(max_fd, &read_fds, NULL, NULL, NULL) < 0 && errno != EINTR) {
Logger(Logger::Level::Error) << "Select error: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Select error: " << strerror(errno);
break;
}

Expand All @@ -36,10 +35,10 @@ void SerialClient::run() {
Logger(Logger::Level::Info) << "From PTY: " << buffer;
socketClient_.sendToServer(buffer, bytes_read);
} else if (bytes_read == 0) {
Logger(Logger::Level::Info) << "PTY closed." << std::endl;
Logger(Logger::Level::Error) << "PTY closed.";
break;
} else {
Logger(Logger::Level::Error) << "Error reading from PTY: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Error reading from PTY: " << strerror(errno);
}
}

Expand All @@ -50,10 +49,10 @@ void SerialClient::run() {
Logger(Logger::Level::Info) << "From Server: " << buffer;
vsp_.async_write(buffer, bytes_read);
} else if (bytes_read == 0) {
Logger(Logger::Level::Info) << "Server closed connection." << std::endl;
Logger(Logger::Level::Info) << "Server closed connection.";
break;
} else {
Logger(Logger::Level::Error) << "Error reading from socket: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Error reading from socket: " << strerror(errno);
}
}
}
Expand Down
23 changes: 8 additions & 15 deletions src/SerialPort.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#include "SerialPort.h"
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <cstring>
#include <stdexcept>

SerialPort::SerialPort(const std::string& device, int baud_rate) {
Logger(Logger::Level::Info) << "SerialPort init start - device " << device << " - baudRate - " << baud_rate << std::endl;
Logger(Logger::Level::Info) << "Initializing serial port connection to " << device << " baudRate " << baud_rate;
serial_fd = open(device.c_str(), O_RDWR | O_NOCTTY);
if (serial_fd < 0) {
Logger(Logger::Level::Error) << "Error opening serial port: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "Error opening serial port: " << strerror(errno);
throw std::runtime_error("Error opening serial port");
}

configurePort(baud_rate);
Logger(Logger::Level::Info) << "SerialPort init finish" << std::endl;
Logger(Logger::Level::Info) << "SerialPort init finish";
}

SerialPort::~SerialPort() {
Expand All @@ -26,7 +21,7 @@ void SerialPort::configurePort(int baud_rate) {
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(serial_fd, &tty) != 0) {
Logger(Logger::Level::Error) << "tcgetattr failed: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "tcgetattr failed: " << strerror(errno);
throw std::runtime_error("tcgetattr failed");
}

Expand All @@ -49,25 +44,25 @@ void SerialPort::configurePort(int baud_rate) {
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout

if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {
Logger(Logger::Level::Error) << "tcsetattr failed: " << strerror(errno) << std::endl;
Logger(Logger::Level::Error) << "tcsetattr failed: " << strerror(errno);
throw std::runtime_error("tcsetattr failed");
}
}

void SerialPort::writeData(const std::string& data) {
Logger(Logger::Level::Info) << "SerialPort write data - " << data;
Logger(Logger::Level::Info) << "SerialPort - write: " << data;
write(serial_fd, data.c_str(), data.size());
}


void SerialPort::readLoop() {
Logger(Logger::Level::Info) << "SerialPort start reading loop";
Logger(Logger::Level::Info) << "SerialPort - start reading loop.";
while (keep_reading) {
char buf[1024];
int n = read(serial_fd, buf, sizeof(buf) - 1);
if (n > 0) {
buf[n] = '\0'; // Ensure null-termination
Logger(Logger::Level::Info) << "SerialPort read - " << buf;
Logger(Logger::Level::Info) << "SerialPort - read:" << buf;
std::lock_guard<std::mutex> lock(mtx);
read_buffer.push(std::string(buf));
cv.notify_one();
Expand All @@ -87,11 +82,9 @@ void SerialPort::stopReading() {
}

std::string SerialPort::readData() {
Logger(Logger::Level::Info) << "SerialPort read data";
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this]{ return !read_buffer.empty(); });
std::string data = read_buffer.front();
read_buffer.pop();
Logger(Logger::Level::Info) << "SerialPort read data - got data - " << data;
return data;
}
22 changes: 0 additions & 22 deletions src/SerialServer.cpp

This file was deleted.

Loading

0 comments on commit d0872f7

Please sign in to comment.