-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.cpp
194 lines (143 loc) · 5 KB
/
binding.cpp
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
#include <napi.h>
#include <node_version.h>
#if CHECK_NODE_API_MODULE_VERSION && NODE_API_MODULE_VERSION == 85
#define V8_REVERSE_JSARGS
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <vector>
int set_interface_attribs(int fd, int speed) {
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
/* setup for non-canonical mode */
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
/* fetch bytes as they become available */
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 5;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
return -1;
}
return 0;
}
/*
VMIN = 0, VTIME = 0
No blocking, return immediately with what is available
VMIN > 0, VTIME = 0
This will make read() always wait for bytes (exactly how many is determined by VMIN),
so read() could block indefinitely.
VMIN = 0, VTIME > 0
This is a blocking read of any number of chars with a maximum timeout (given by VTIME).
read() will block until either any amount of data is available, or the timeout occurs.
VMIN > 0, VTIME > 0
Block until either VMIN characters have been received, or VTIME after first character has elapsed.
Note that the timeout for VTIME does not begin until the first character is received.
*/
int set_blocking(int fd, int mcount) {
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
return - 1;
}
tty.c_cc[VMIN] = mcount;
tty.c_cc[VTIME] = 5; /* half second timer */
if (tcsetattr(fd, TCSANOW, &tty) < 0)
return -1;
return 0;
}
Napi::Value serial_open(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsObject()) {
Napi::TypeError::New(env, "First argument must be an object").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Object params = info[0].ToObject();
std::string path = params.Get("path").ToString();
int baudRate = params.Get("baudRate").ToNumber().Int32Value();
int fd = open(path.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
Napi::TypeError::New(env, "Error opening: " + path).ThrowAsJavaScriptException();
return env.Null();
}
if (set_interface_attribs(fd, baudRate) < 0) {
Napi::TypeError::New(env, "Error setting attribs").ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, fd);
}
Napi::Value serial_close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// file descriptor
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "First argument must be an int").ThrowAsJavaScriptException();
return env.Undefined();
}
int fd = info[0].ToNumber().Int32Value();
close(fd);
return env.Undefined();
}
Napi::Value serial_read(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// file descriptor
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "First argument must be an int").ThrowAsJavaScriptException();
return env.Undefined();
}
int fd = info[0].As<Napi::Number>().Int32Value();
std::vector<char> buffer;
char temp[16];
int nbytes;
while ((nbytes = read(fd, temp, 16)) > 0) {
for (int i = 0; i < nbytes; i++)
buffer.push_back(temp[i]);
}
return Napi::Buffer<char>::Copy(env, buffer.data(), buffer.size());
}
Napi::Value serial_write(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// file descriptor
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "First argument must be an int").ThrowAsJavaScriptException();
return env.Undefined();
}
// data
if (!info[1].IsBuffer() && !info[1].IsString()) {
Napi::TypeError::New(env, "First argument must be a buffer or a string").ThrowAsJavaScriptException();
return env.Undefined();
}
int fd = info[0].As<Napi::Number>().Int32Value();
if (info[1].IsBuffer()) {
Napi::Buffer<char> data = info[1].As<Napi::Buffer<char>>();
int nbytes = write(fd, data.Data(), data.Length());
return Napi::Number::New(env, nbytes);
}
if (info[1].IsString()) {
std::string data = info[1].ToString(); //.Utf8Value();
int nbytes = write(fd, data.c_str(), data.size());
return Napi::Number::New(env, nbytes);
}
return Napi::Number::New(env, -1);
}
Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set("open", Napi::Function::New(env, serial_open));
exports.Set("close", Napi::Function::New(env, serial_close));
exports.Set("read", Napi::Function::New(env, serial_read));
exports.Set("write", Napi::Function::New(env, serial_write));
return exports;
}
NODE_API_MODULE(serial, init)