-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserialport.h
More file actions
207 lines (171 loc) · 5.74 KB
/
Copy pathserialport.h
File metadata and controls
207 lines (171 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef PACKAGES_SERIALPORT_SRC_SERIALPORT_H_
#define PACKAGES_SERIALPORT_SRC_SERIALPORT_H_
// Workaround for electron 11 abi issue https://github.com/serialport/node-serialport/issues/2191
// TODO Replace with ABI stable runtime check (per https://github.com/serialport/node-serialport/pull/2305#discussion_r697542996)
#include <node_version.h>
#if CHECK_NODE_API_MODULE_VERSION && NODE_API_MODULE_VERSION == 85
#define V8_REVERSE_JSARGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <napi.h>
#include <string>
#define ERROR_STRING_SIZE 1088
Napi::Value Open(const Napi::CallbackInfo& info);
Napi::Value Update(const Napi::CallbackInfo& info);
Napi::Value Close(const Napi::CallbackInfo& info);
Napi::Value Flush(const Napi::CallbackInfo& info);
Napi::Value Set(const Napi::CallbackInfo& info);
Napi::Value Get(const Napi::CallbackInfo& info);
Napi::Value GetBaudRate(const Napi::CallbackInfo& info);
Napi::Value Drain(const Napi::CallbackInfo& info);
enum SerialPortParity {
SERIALPORT_PARITY_NONE = 1,
SERIALPORT_PARITY_MARK = 2,
SERIALPORT_PARITY_EVEN = 3,
SERIALPORT_PARITY_ODD = 4,
SERIALPORT_PARITY_SPACE = 5
};
enum SerialPortStopBits {
SERIALPORT_STOPBITS_ONE = 1,
SERIALPORT_STOPBITS_ONE_FIVE = 2,
SERIALPORT_STOPBITS_TWO = 3
};
enum SerialPortRtsMode {
SERIALPORT_RTSMODE_ENABLE = 1,
SERIALPORT_RTSMODE_HANDSHAKE = 2,
SERIALPORT_RTSMODE_TOGGLE = 3
};
SerialPortParity ToParityEnum(const Napi::String& str);
SerialPortStopBits ToStopBitEnum(double stopBits);
SerialPortRtsMode ToRtsModeEnum(const Napi::String& str);
struct OpenBaton : public Napi::AsyncWorker {
OpenBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:OpenBaton"),
errorString(), path() {}
char errorString[ERROR_STRING_SIZE];
char path[1024];
int fd = 0;
int result = 0;
int baudRate = 0;
int dataBits = 0;
bool rtscts = false;
bool xon = false;
bool xoff = false;
bool xany = false;
bool hupcl = false;
bool lock = false;
SerialPortParity parity;
SerialPortStopBits stopBits;
SerialPortRtsMode rtsMode;
#ifndef WIN32
uint8_t vmin = 0;
uint8_t vtime = 0;
#endif
void Execute() override;
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Callback().Call({env.Null(), Napi::Number::New(env, result)});
}
};
struct ConnectionOptions {
ConnectionOptions() : errorString() {}
char errorString[ERROR_STRING_SIZE];
int fd = 0;
int baudRate = 0;
};
struct ConnectionOptionsBaton : ConnectionOptions , Napi::AsyncWorker {
ConnectionOptionsBaton(Napi::Function& callback) : ConnectionOptions() , Napi::AsyncWorker(callback, "node-serialport:ConnectionOptionsBaton") {}
void Execute() override;
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Callback().Call({env.Null()});
}
};
struct SetBaton : public Napi::AsyncWorker {
SetBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:SetBaton"),
errorString() {}
int fd = 0;
int result = 0;
char errorString[ERROR_STRING_SIZE];
bool rts = false;
bool cts = false;
bool dtr = false;
bool dsr = false;
bool brk = false;
bool lowLatency = false;
void Execute() override;
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Callback().Call({env.Null()});
}
};
struct GetBaton : public Napi::AsyncWorker {
GetBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:GetBaton"),
errorString() {}
int fd = 0;
char errorString[ERROR_STRING_SIZE];
bool cts = false;
bool dsr = false;
bool dcd = false;
bool lowLatency = false;
void Execute() override;
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Napi::Object results = Napi::Object::New(env);
results.Set("cts", cts);
results.Set("dsr", dsr);
results.Set("dcd", dcd);
results.Set("lowLatency", lowLatency);
Callback().Call({env.Null(), results});
}
};
struct GetBaudRateBaton : public Napi::AsyncWorker {
GetBaudRateBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:GetBaudRateBaton"),
errorString() {}
int fd = 0;
char errorString[ERROR_STRING_SIZE];
int baudRate = 0;
void Execute() override;
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Napi::Object results = Napi::Object::New(env);
(results).Set(Napi::String::New(env, "baudRate"), Napi::Number::New(env, baudRate));
Callback().Call({env.Null(),results});
}
};
struct VoidBaton : public Napi::AsyncWorker {
VoidBaton(Napi::Function& callback, const char *resource_name) : Napi::AsyncWorker(callback, resource_name),
errorString() {}
int fd = 0;
char errorString[ERROR_STRING_SIZE];
void OnOK() override {
Napi::Env env = Env();
Napi::HandleScope scope(env);
Callback().Call({env.Null()});
}
};
struct CloseBaton : VoidBaton {
CloseBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:CloseBaton") {}
void Execute() override;
};
struct DrainBaton : VoidBaton {
DrainBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:DrainBaton") {}
void Execute() override;
};
struct FlushBaton : VoidBaton {
FlushBaton(Napi::Function& callback) : VoidBaton(callback, "node-serialport:FlushBaton") {}
void Execute() override;
};
int setup(int fd, OpenBaton *data);
int setBaudRate(ConnectionOptions *data);
#ifndef WIN32
void markClosingFd(int fd);
void unmarkClosingFd(int fd);
#endif
#endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_H_