Skip to content

Commit

Permalink
Merge branch 'master' into fujinet-pc-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
a8jan committed Feb 6, 2024
2 parents 8172e4d + 6e3ad08 commit 795fb7a
Show file tree
Hide file tree
Showing 19 changed files with 574 additions and 386 deletions.
30 changes: 30 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BasedOnStyle: LLVM
ColumnLimit: 200
AllowShortBlocksOnASingleLine: true
AlwaysBreakAfterReturnType: None
IncludeBlocks: Preserve
SortIncludes: false
UseTab: Always
IndentWidth: 4
TabWidth: 4
AccessModifierOffset: -4
AlwaysBreakTemplateDeclarations: Yes
BreakInheritanceList: AfterColon
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: false
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentPPDirectives: BeforeHash
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sdkconfig.fujinet-v1
include/version.h
build/CMakeCache.txt
build/*
!build/.gitkeep
out/*

# ignore generated WebUI files
Expand Down
2 changes: 1 addition & 1 deletion build-platforms/platformio-COCO.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ board = fujinet-v1
build_type = debug
build_flags =
${env.build_flags}
-D PINMAP_IEC_D32PRO
-D PINMAP_COCO_DEVKITC
5 changes: 1 addition & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ fi
# PC BUILD using cmake
if [ ! -z "$PC_TARGET" ] ; then
echo "PC Build Mode"
if [ ! -d "$SCRIPT_DIR/build" ] ; then
echo "ERROR: Could not find build dir to run cmake in"
exit 1
fi
mkdir -p "$SCRIPT_DIR/build"
if [ $DO_CLEAN -eq 1 ] ; then
echo "Removing old build artifacts"
rm -rf $SCRIPT_DIR/build/*
Expand Down
Empty file added build/.gitkeep
Empty file.
43 changes: 34 additions & 9 deletions distfiles/run-fujinet
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
#!/bin/sh
#!/usr/bin/env bash

echo "Starting FujiNet"
./fujinet "$@"
rc=$?
fn_pid=0
rc=75

function start_fujinet {
./fujinet "$@" &
fn_pid=$!
}

function kill_fujinet {
kill -HUP $fn_pid
}

function check_restart {
echo ""
echo "Received signal to restart FujiNet"
kill -HUP $fn_pid
sleep 1
start_fujinet "$@"
}

trap check_restart SIGHUP SIGUSR1 SIGCONT
trap kill_fujinet SIGINT SIGTERM SIGQUIT

# from sysexits.h
# #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
while [ $rc -eq 75 ]; do
echo "Restarting FujiNet"
./fujinet "$@"
rc=$?
echo "Starting FujiNet"
start_fujinet "$@"
running=1
while [ ! -z "$running" ]; do
sleep 1
running=$(ps -ef | grep " $fn_pid " | grep -Ev "grep")
done

echo "Fujinet Not running, waiting on process"
wait $fn_pid
rc=$?
done

echo "FujiNet ended with exit code $rc"
12 changes: 12 additions & 0 deletions distfiles/run-fujinet.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# PowerShell Runner run-fujinet.ps1
Write-Host "Starting FujiNet"

do {
.\fujinet.exe $args[0] $args[1] $args[2] $args[3] $args[4] $args[5] $args[6] $args[7]

if ($LASTEXITCODE -eq 75) {
Write-Host "Restarting FujiNet"
}
} while ($LASTEXITCODE -eq 75)

Write-Host "FujiNet ended with exit code $LASTEXITCODE"
1 change: 1 addition & 0 deletions fujinet_pc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ else()
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:fujinet> dist
COMMAND ${CMAKE_COMMAND} -E copy_directory ${BUILD_DATA_DIR} dist/data
COMMAND ${CMAKE_COMMAND} -E remove dist/run-fujinet.bat
COMMAND ${CMAKE_COMMAND} -E remove dist/run-fujinet.ps1
)
endif()
add_dependencies(dist fujinet)
Expand Down
18 changes: 18 additions & 0 deletions include/Log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef LOG_H_COMPATIBILITY
#define LOG_H_COMPATIBILITY

#include <cstdarg>
#include <cstdio>

// This is to provide a compatibility with SP over SLIP code copied over from AppleWin project
// and simply converts to vprintf

inline void LogFileOutput(const char *format, ...)
{
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}

#endif
90 changes: 55 additions & 35 deletions lib/bus/iwm/Connection.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
#include <chrono>
#ifdef BUILD_APPLE

#ifdef SP_OVER_SLIP

#include "Connection.h"
#include <iostream>

std::vector<uint8_t> Connection::wait_for_request()
{
// Use a timeout so we can stop waiting for responses
while (is_connected_) {
std::unique_lock<std::mutex> lock(data_mutex_);
if (data_cv_.wait_for(lock, std::chrono::milliseconds(100), [this]() { return !data_map_.empty(); })) {
const auto it = data_map_.begin();
std::vector<uint8_t> request_data = it->second;
data_map_.erase(it);

return request_data;
}
}
return std::vector<uint8_t>();
}

void Connection::join()
{
if (reading_thread_.joinable())
{
reading_thread_.join();
}
}

#endif // SP_OVER_SLIP

#endif // BUILD_APPLE
#ifdef BUILD_APPLE
#ifdef SP_OVER_SLIP

#include "Connection.h"
#include <condition_variable>
#include <cstdint>
#include <mutex>
#include <stdexcept>
#include <vector>

// This is called after AppleWin sends a request to a device, and is waiting for the response
std::vector<uint8_t> Connection::wait_for_response(uint8_t request_id, std::chrono::seconds timeout)
{
std::unique_lock<std::mutex> lock(data_mutex_);
// mutex is unlocked as it goes into a wait, so then the inserting thread can
// add to map, and this can then pick it up when notified, or timeout.
if (!data_cv_.wait_for(lock, timeout, [this, request_id]() { return data_map_.count(request_id) > 0; }))
{
throw std::runtime_error("Timeout waiting for response");
}
std::vector<uint8_t> response_data = data_map_[request_id];
data_map_.erase(request_id);
return response_data;
}

// This is used by devices that are waiting for requests from AppleWin.
// The codebase is used both sides of the connection.
std::vector<uint8_t> Connection::wait_for_request()
{
// Use a timeout so we can stop waiting for responses
while (is_connected_)
{
std::unique_lock<std::mutex> lock(data_mutex_);
if (data_cv_.wait_for(lock, std::chrono::milliseconds(100), [this]() { return !data_map_.empty(); }))
{
const auto it = data_map_.begin();
std::vector<uint8_t> request_data = it->second;
data_map_.erase(it);

return request_data;
}
}
return std::vector<uint8_t>();
}

void Connection::join()
{
if (reading_thread_.joinable())
{
reading_thread_.join();
}
}

#endif
#endif
33 changes: 19 additions & 14 deletions lib/bus/iwm/Connection.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#ifdef BUILD_APPLE
#ifdef SP_OVER_SLIP

#include <array>
#include <atomic>
Expand All @@ -13,27 +15,30 @@
class Connection
{
public:
virtual ~Connection() = default;
virtual void send_data(const std::vector<uint8_t> &data) = 0;
virtual ~Connection() = default;
virtual void send_data(const std::vector<uint8_t> &data) = 0;

virtual void create_read_channel() = 0;
virtual void close_connection() = 0;
virtual void create_read_channel() = 0;
virtual void close_connection() = 0;

bool is_connected() const { return is_connected_; }
void set_is_connected(const bool is_connected) { is_connected_ = is_connected; }
bool is_connected() const { return is_connected_; }
void set_is_connected(const bool is_connected) { is_connected_ = is_connected; }

std::vector<uint8_t> wait_for_response(uint8_t request_id, std::chrono::seconds timeout);
std::vector<uint8_t> wait_for_request();
std::vector<uint8_t> wait_for_response(uint8_t request_id, std::chrono::seconds timeout);
std::vector<uint8_t> wait_for_request();

void join();
void join();

private:
std::atomic<bool> is_connected_{false};
std::atomic<bool> is_connected_{false};

protected:
std::map<uint8_t, std::vector<uint8_t>> data_map_;
std::thread reading_thread_;
std::map<uint8_t, std::vector<uint8_t>> data_map_;
std::thread reading_thread_;

std::mutex data_mutex_;
std::condition_variable data_cv_;
std::mutex data_mutex_;
std::condition_variable data_cv_;
};

#endif
#endif
Loading

0 comments on commit 795fb7a

Please sign in to comment.