Skip to content

Commit 3f03734

Browse files
committed
Support Trakce protocol v1.1.
Add OnOpenError callback.
1 parent 176ef47 commit 3f03734

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

lib-api.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ int connect() {
6969
const QString errMsg = "XN connect error while opening serial port '" +
7070
lib.s["XN"]["port"].toString() + "': " + e;
7171
lib.log(errMsg, LogLevel::Error);
72+
lib.events.call(lib.events.onOpenError, errMsg);
7273
lib.events.call(lib.events.afterClose);
7374
lib.guiOnClose();
7475
return TRK_CANNOT_OPEN_PORT;
@@ -353,6 +354,10 @@ void bindOnLocoStolen(TrkLocoEv f, void *data) {
353354
lib.events.bind(lib.events.onLocoStolen, f, data);
354355
}
355356

357+
void bindOnOpenError(TrkMsgEv f, void *data) {
358+
lib.events.bind(lib.events.onOpenError, f, data);
359+
}
360+
356361
///////////////////////////////////////////////////////////////////////////////
357362

358363
void showConfigDialog() {

lib-api.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace Xn {
99

10-
constexpr std::array<unsigned int, 1> API_SUPPORTED_VERSIONS {
11-
0x0001, // v1.0
10+
constexpr std::array<unsigned int, 3> API_SUPPORTED_VERSIONS {
11+
0x0001, 0x0100, 0x0101 // v0.1, v1.0, v1.1
1212
};
1313

1414
extern "C" {
@@ -67,6 +67,7 @@ XN_SHARED_EXPORT void CALL_CONV bindAfterClose(TrkStdNotifyEvent f, void *data);
6767
XN_SHARED_EXPORT void CALL_CONV bindOnTrackStatusChange(TrkStatusChangedEv f, void *data);
6868
XN_SHARED_EXPORT void CALL_CONV bindOnLog(TrkLogEv f, void *data);
6969
XN_SHARED_EXPORT void CALL_CONV bindOnLocoStolen(TrkLocoEv f, void *data);
70+
XN_SHARED_EXPORT void CALL_CONV bindOnOpenError(TrkMsgEv f, void *data);
7071

7172
XN_SHARED_EXPORT void CALL_CONV showConfigDialog();
7273

lib-events.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using TrkStdNotifyEvent = void CALL_CONV (*)(const void *sender, void *data);
1717
using TrkStatusChangedEv = void CALL_CONV (*)(const void *sender, void *data, int trkStatus);
1818
using TrkLogEv = void CALL_CONV (*)(const void *sender, void *data, int loglevel, const uint16_t *msg);
1919
using TrkLocoEv = void CALL_CONV (*)(const void *sender, void *data, uint16_t addr);
20+
using TrkMsgEv = void CALL_CONV (*)(const void *sender, void *data, const uint16_t *msg);
2021

2122
template <typename F>
2223
struct EventData {
@@ -34,6 +35,7 @@ struct XnEvents {
3435
EventData<TrkLogEv> onLog;
3536
EventData<TrkStatusChangedEv> onTrkStatusChanged;
3637
EventData<TrkLocoEv> onLocoStolen;
38+
EventData<TrkMsgEv> onOpenError;
3739

3840
void call(const EventData<TrkStdNotifyEvent> &e) const {
3941
if (e.defined())
@@ -51,7 +53,10 @@ struct XnEvents {
5153
if (e.defined())
5254
e.func(this, e.data, addr.addr);
5355
}
54-
56+
void call(const EventData<TrkMsgEv> &e, const QString &msg) const {
57+
if (e.defined())
58+
e.func(this, e.data, msg.utf16());
59+
}
5560
template <typename F>
5661
static void bind(EventData<F> &event, const F &func, void *const data) {
5762
event.func = func;

lib-main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void LibMain::getLIVersion() {
9292
);
9393
} catch (const QStrException &e) {
9494
log("Get LI Version: " + e.str(), LogLevel::Error);
95+
events.call(events.onOpenError, "Get LI Version: " + e.str());
9596
xn.disconnect();
9697
}
9798
}
@@ -111,12 +112,14 @@ void LibMain::xnGotLIVersion(void *, unsigned hw, unsigned sw) {
111112
);
112113
} catch (const QStrException &e) {
113114
log("Get LI Address: " + e.str(), LogLevel::Error);
115+
events.call(events.onOpenError, "Get LI Address: " + e.str());
114116
xn.disconnect();
115117
}
116118
}
117119

118120
void LibMain::xnOnLIVersionError(void *, void *) {
119121
log("Get LI Version: no response!", LogLevel::Error);
122+
events.call(events.onOpenError, "Get LI Version: no response!");
120123
xn.disconnect();
121124
}
122125

@@ -142,6 +145,7 @@ void LibMain::getCSVersion() {
142145
);
143146
} catch (const QStrException &e) {
144147
log("Get CS Version: " + e.str(), LogLevel::Error);
148+
events.call(events.onOpenError, "Get CS Version: " + e.str());
145149
xn.disconnect();
146150
}
147151
}
@@ -170,12 +174,14 @@ void LibMain::getCSStatus() {
170174
);
171175
} catch (const QStrException &e) {
172176
log("Get CS Status: " + e.str(), LogLevel::Error);
177+
events.call(events.onOpenError, "Get CS Status: " + e.str());
173178
xn.disconnect();
174179
}
175180
}
176181

177182
void LibMain::xnOnCSStatusError(void *, void *) {
178183
log("Get CS Status: no response!", LogLevel::Error);
184+
events.call(events.onOpenError, "Get CS Status: no response!");
179185
xn.disconnect();
180186
}
181187

xn.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ QT += core gui serialport
5151
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5252

5353
VERSION_MAJOR = 2
54-
VERSION_MINOR = 4
54+
VERSION_MINOR = 5
5555

5656
DEFINES += "VERSION_MAJOR=$$VERSION_MAJOR" \
5757
"VERSION_MINOR=$$VERSION_MINOR"

0 commit comments

Comments
 (0)