Skip to content

Commit

Permalink
Support Trakce protocol v1.1.
Browse files Browse the repository at this point in the history
Add OnOpenError callback.
  • Loading branch information
horacekj committed Nov 20, 2022
1 parent 176ef47 commit 3f03734
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int connect() {
const QString errMsg = "XN connect error while opening serial port '" +
lib.s["XN"]["port"].toString() + "': " + e;
lib.log(errMsg, LogLevel::Error);
lib.events.call(lib.events.onOpenError, errMsg);
lib.events.call(lib.events.afterClose);
lib.guiOnClose();
return TRK_CANNOT_OPEN_PORT;
Expand Down Expand Up @@ -353,6 +354,10 @@ void bindOnLocoStolen(TrkLocoEv f, void *data) {
lib.events.bind(lib.events.onLocoStolen, f, data);
}

void bindOnOpenError(TrkMsgEv f, void *data) {
lib.events.bind(lib.events.onOpenError, f, data);
}

///////////////////////////////////////////////////////////////////////////////

void showConfigDialog() {
Expand Down
5 changes: 3 additions & 2 deletions lib-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace Xn {

constexpr std::array<unsigned int, 1> API_SUPPORTED_VERSIONS {
0x0001, // v1.0
constexpr std::array<unsigned int, 3> API_SUPPORTED_VERSIONS {
0x0001, 0x0100, 0x0101 // v0.1, v1.0, v1.1
};

extern "C" {
Expand Down Expand Up @@ -67,6 +67,7 @@ XN_SHARED_EXPORT void CALL_CONV bindAfterClose(TrkStdNotifyEvent f, void *data);
XN_SHARED_EXPORT void CALL_CONV bindOnTrackStatusChange(TrkStatusChangedEv f, void *data);
XN_SHARED_EXPORT void CALL_CONV bindOnLog(TrkLogEv f, void *data);
XN_SHARED_EXPORT void CALL_CONV bindOnLocoStolen(TrkLocoEv f, void *data);
XN_SHARED_EXPORT void CALL_CONV bindOnOpenError(TrkMsgEv f, void *data);

XN_SHARED_EXPORT void CALL_CONV showConfigDialog();

Expand Down
7 changes: 6 additions & 1 deletion lib-events.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using TrkStdNotifyEvent = void CALL_CONV (*)(const void *sender, void *data);
using TrkStatusChangedEv = void CALL_CONV (*)(const void *sender, void *data, int trkStatus);
using TrkLogEv = void CALL_CONV (*)(const void *sender, void *data, int loglevel, const uint16_t *msg);
using TrkLocoEv = void CALL_CONV (*)(const void *sender, void *data, uint16_t addr);
using TrkMsgEv = void CALL_CONV (*)(const void *sender, void *data, const uint16_t *msg);

template <typename F>
struct EventData {
Expand All @@ -34,6 +35,7 @@ struct XnEvents {
EventData<TrkLogEv> onLog;
EventData<TrkStatusChangedEv> onTrkStatusChanged;
EventData<TrkLocoEv> onLocoStolen;
EventData<TrkMsgEv> onOpenError;

void call(const EventData<TrkStdNotifyEvent> &e) const {
if (e.defined())
Expand All @@ -51,7 +53,10 @@ struct XnEvents {
if (e.defined())
e.func(this, e.data, addr.addr);
}

void call(const EventData<TrkMsgEv> &e, const QString &msg) const {
if (e.defined())
e.func(this, e.data, msg.utf16());
}
template <typename F>
static void bind(EventData<F> &event, const F &func, void *const data) {
event.func = func;
Expand Down
6 changes: 6 additions & 0 deletions lib-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void LibMain::getLIVersion() {
);
} catch (const QStrException &e) {
log("Get LI Version: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get LI Version: " + e.str());
xn.disconnect();
}
}
Expand All @@ -111,12 +112,14 @@ void LibMain::xnGotLIVersion(void *, unsigned hw, unsigned sw) {
);
} catch (const QStrException &e) {
log("Get LI Address: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get LI Address: " + e.str());
xn.disconnect();
}
}

void LibMain::xnOnLIVersionError(void *, void *) {
log("Get LI Version: no response!", LogLevel::Error);
events.call(events.onOpenError, "Get LI Version: no response!");
xn.disconnect();
}

Expand All @@ -142,6 +145,7 @@ void LibMain::getCSVersion() {
);
} catch (const QStrException &e) {
log("Get CS Version: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get CS Version: " + e.str());
xn.disconnect();
}
}
Expand Down Expand Up @@ -170,12 +174,14 @@ void LibMain::getCSStatus() {
);
} catch (const QStrException &e) {
log("Get CS Status: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get CS Status: " + e.str());
xn.disconnect();
}
}

void LibMain::xnOnCSStatusError(void *, void *) {
log("Get CS Status: no response!", LogLevel::Error);
events.call(events.onOpenError, "Get CS Status: no response!");
xn.disconnect();
}

Expand Down
2 changes: 1 addition & 1 deletion xn.pro
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

VERSION_MAJOR = 2
VERSION_MINOR = 4
VERSION_MINOR = 5

DEFINES += "VERSION_MAJOR=$$VERSION_MAJOR" \
"VERSION_MINOR=$$VERSION_MINOR"
Expand Down

0 comments on commit 3f03734

Please sign in to comment.