@jason2866/serialport-bindings-cpp 0.0.0-development

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+ #ifndef PACKAGES_SERIALPORT_SRC_SERIALPORT_H_
2
+ #define PACKAGES_SERIALPORT_SRC_SERIALPORT_H_
3
+
4
+ // Workaround for electron 11 abi issue https://github.com/serialport/node-serialport/issues/2191
5
+ // TODO Replace with ABI stable runtime check (per https://github.com/serialport/node-serialport/pull/2305#discussion_r697542996)
6
+ #include <node_version.h>
7
+ #if CHECK_NODE_API_MODULE_VERSION && NODE_API_MODULE_VERSION == 85
8
+ #define V8_REVERSE_JSARGS
9
+ #endif
10
+
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+ #include <napi.h>
15
+ #include <string>
16
+
17
+ #define ERROR_STRING_SIZE 1088
18
+
19
+ Napi::Value Open(const Napi::CallbackInfo& info);
20
+
21
+ Napi::Value Update(const Napi::CallbackInfo& info);
22
+
23
+ Napi::Value Close(const Napi::CallbackInfo& info);
24
+
25
+ Napi::Value Flush(const Napi::CallbackInfo& info);
26
+
27
+ Napi::Value Set(const Napi::CallbackInfo& info);
28
+
29
+ Napi::Value Get(const Napi::CallbackInfo& info);
30
+
31
+ Napi::Value GetBaudRate(const Napi::CallbackInfo& info);
32
+
33
+ Napi::Value Drain(const Napi::CallbackInfo& info);
34
+
35
+ enum SerialPortParity {
36
+ SERIALPORT_PARITY_NONE = 1,
37
+ SERIALPORT_PARITY_MARK = 2,
38
+ SERIALPORT_PARITY_EVEN = 3,
39
+ SERIALPORT_PARITY_ODD = 4,
40
+ SERIALPORT_PARITY_SPACE = 5
41
+ };
42
+
43
+ enum SerialPortStopBits {
44
+ SERIALPORT_STOPBITS_ONE = 1,
45
+ SERIALPORT_STOPBITS_ONE_FIVE = 2,
46
+ SERIALPORT_STOPBITS_TWO = 3
47
+ };
48
+
49
+ enum SerialPortRtsMode {
50
+ SERIALPORT_RTSMODE_ENABLE = 1,
51
+ SERIALPORT_RTSMODE_HANDSHAKE = 2,
52
+ SERIALPORT_RTSMODE_TOGGLE = 3
53
+ };
54
+
55
+ SerialPortParity ToParityEnum(const Napi::String& str);
56
+ SerialPortStopBits ToStopBitEnum(double stopBits);
57
+ SerialPortRtsMode ToRtsModeEnum(const Napi::String& str);
58
+
59
+ struct OpenBaton : public Napi::AsyncWorker {
60
+ OpenBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:OpenBaton"),
61
+ errorString(), path() {}
62
+ char errorString[ERROR_STRING_SIZE];
63
+ char path[1024];
64
+ int fd = 0;
65
+ int result = 0;
66
+ int baudRate = 0;
67
+ int dataBits = 0;
68
+ bool rtscts = false;
69
+ bool xon = false;
70
+ bool xoff = false;
71
+ bool xany = false;
72
+ bool hupcl = false;
73
+ bool lock = false;
74
+ SerialPortParity parity;
75
+ SerialPortStopBits stopBits;
76
+ SerialPortRtsMode rtsMode;
77
+ #ifndef WIN32
78
+ uint8_t vmin = 0;
79
+ uint8_t vtime = 0;
80
+ #endif
81
+ void Execute() override;
82
+
83
+ void OnOK() override {
84
+ Napi::Env env = Env();
85
+ Napi::HandleScope scope(env);
86
+ Callback().Call({env.Null(), Napi::Number::New(env, result)});
87
+ }
88
+ };
89
+
90
+ struct ConnectionOptions {
91
+ ConnectionOptions() : errorString() {}
92
+ char errorString[ERROR_STRING_SIZE];
93
+ int fd = 0;
94
+ int baudRate = 0;
95
+ };
96
+ struct ConnectionOptionsBaton : ConnectionOptions , Napi::AsyncWorker {
97
+ ConnectionOptionsBaton(Napi::Function& callback) : ConnectionOptions() , Napi::AsyncWorker(callback, "node-serialport:ConnectionOptionsBaton") {}
98
+
99
+ void Execute() override;
100
+
101
+ void OnOK() override {
102
+ Napi::Env env = Env();
103
+ Napi::HandleScope scope(env);
104
+ Callback().Call({env.Null()});
105
+ }
106
+ };
107
+
108
+ struct SetBaton : public Napi::AsyncWorker {
109
+ SetBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:SetBaton"),
110
+ errorString() {}
111
+ int fd = 0;
112
+ int result = 0;
113
+ char errorString[ERROR_STRING_SIZE];
114
+ bool rts = false;
115
+ bool cts = false;
116
+ bool dtr = false;
117
+ bool dsr = false;
118
+ bool brk = false;
119
+ bool lowLatency = false;
120
+
121
+ void Execute() override;
122
+
123
+ void OnOK() override {
124
+ Napi::Env env = Env();
125
+ Napi::HandleScope scope(env);
126
+ Callback().Call({env.Null()});
127
+ }
128
+ };
129
+
130
+ struct GetBaton : public Napi::AsyncWorker {
131
+ GetBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:GetBaton"),
132
+ errorString() {}
133
+ int fd = 0;
134
+ char errorString[ERROR_STRING_SIZE];
135
+ bool cts = false;
136
+ bool dsr = false;
137
+ bool dcd = false;
138
+ bool lowLatency = false;
139
+
140
+ void Execute() override;
141
+
142
+ void OnOK() override {
143
+ Napi::Env env = Env();
144
+ Napi::HandleScope scope(env);
145
+ Napi::Object results = Napi::Object::New(env);
146
+ results.Set("cts", cts);
147
+ results.Set("dsr", dsr);
148
+ results.Set("dcd", dcd);
149
+ results.Set("lowLatency", lowLatency);
150
+ Callback().Call({env.Null(), results});
151
+ }
152
+ };
153
+
154
+ struct GetBaudRateBaton : public Napi::AsyncWorker {
155
+ GetBaudRateBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:GetBaudRateBaton"),
156
+ errorString() {}
157
+ int fd = 0;
158
+ char errorString[ERROR_STRING_SIZE];
159
+ int baudRate = 0;
160
+
161
+ void Execute() override;
162
+
163
+ void OnOK() override {
164
+ Napi::Env env = Env();
165
+ Napi::HandleScope scope(env);
166
+ Napi::Object results = Napi::Object::New(env);
167
+ (results).Set(Napi::String::New(env, "baudRate"), Napi::Number::New(env, baudRate));
168
+ Callback().Call({env.Null(),results});
169
+ }
170
+ };
171
+
172
+ struct VoidBaton : public Napi::AsyncWorker {
173
+ VoidBaton(Napi::Function& callback, const char *resource_name) : Napi::AsyncWorker(callback, resource_name),
174
+ errorString() {}
175
+ int fd = 0;
176
+ char errorString[ERROR_STRING_SIZE];
177
+
178
+ void OnOK() override {
179
+ Napi::Env env = Env();
180
+ Napi::HandleScope scope(env);
181
+ Callback().Call({env.Null()});
182
+ }
183
+ };
184
+
185
+ struct CloseBaton : VoidBaton {
186
+ CloseBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:CloseBaton") {}
187
+ void Execute() override;
188
+ };
189
+
190
+ struct DrainBaton : VoidBaton {
191
+ DrainBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:DrainBaton") {}
192
+ void Execute() override;
193
+ };
194
+
195
+ struct FlushBaton : VoidBaton {
196
+ FlushBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:FlushBaton") {}
197
+ void Execute() override;
198
+ };
199
+
200
+ int setup(int fd, OpenBaton *data);
201
+ int setBaudRate(ConnectionOptions *data);
202
+ #endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_H_
@@ -0,0 +1,76 @@
1
+ #if defined(__linux__)
2
+
3
+ #include <sys/ioctl.h>
4
+ #include <asm/ioctls.h>
5
+ #include <asm/termbits.h>
6
+ #include <linux/serial.h>
7
+
8
+ // Uses the termios2 interface to set nonstandard baud rates
9
+ int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
10
+ struct termios2 t;
11
+
12
+ if (ioctl(fd, TCGETS2, &t) == -1) {
13
+ return -1;
14
+ }
15
+
16
+ t.c_cflag &= ~CBAUD;
17
+ t.c_cflag |= BOTHER;
18
+ t.c_ospeed = t.c_ispeed = baudrate;
19
+
20
+ if (ioctl(fd, TCSETS2, &t) == -1) {
21
+ return -2;
22
+ }
23
+
24
+ return 0;
25
+ }
26
+
27
+ // Uses termios2 interface to retrieve system reported baud rate
28
+ int linuxGetSystemBaudRate(const int fd, int* const outbaud) {
29
+ struct termios2 t;
30
+
31
+ if (ioctl(fd, TCGETS2, &t) == -1) {
32
+ return -1;
33
+ }
34
+
35
+ *outbaud = static_cast<int>(t.c_ospeed);
36
+
37
+ return 0;
38
+ }
39
+
40
+ int linuxSetLowLatencyMode(const int fd, const bool enable) {
41
+ struct serial_struct ss;
42
+
43
+ if (ioctl(fd, TIOCGSERIAL, &ss) == -1) {
44
+ return -1;
45
+ }
46
+
47
+ if ((ss.flags & ASYNC_LOW_LATENCY) == enable) {
48
+ return 0;
49
+ }
50
+
51
+ if (enable) {
52
+ ss.flags |= ASYNC_LOW_LATENCY;
53
+ } else {
54
+ ss.flags &= ~ASYNC_LOW_LATENCY;
55
+ }
56
+
57
+ if (ioctl(fd, TIOCSSERIAL, &ss) == -1) {
58
+ return -2;
59
+ }
60
+
61
+ return 0;
62
+ }
63
+
64
+ int linuxGetLowLatencyMode(const int fd, bool* const enabled) {
65
+ struct serial_struct ss;
66
+
67
+ if (ioctl(fd, TIOCGSERIAL, &ss) == -1) {
68
+ return -1;
69
+ }
70
+
71
+ *enabled = ss.flags & ASYNC_LOW_LATENCY;
72
+
73
+ return 0;
74
+ }
75
+
76
+ #endif
@@ -0,0 +1,10 @@
1
+ #ifndef PACKAGES_SERIALPORT_SRC_SERIALPORT_LINUX_H_
2
+ #define PACKAGES_SERIALPORT_SRC_SERIALPORT_LINUX_H_
3
+
4
+ int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate);
5
+ int linuxGetSystemBaudRate(const int fd, int* const outbaud);
6
+ int linuxSetLowLatencyMode(const int fd, const bool enable);
7
+ int linuxGetLowLatencyMode(const int fd, bool* const enabled);
8
+
9
+ #endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_LINUX_H_
10
+