/
serctl.erl
453 lines (379 loc) · 12.2 KB
/
serctl.erl
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
%% Copyright (c) 2011-2012, Michael Santos <michael.santos@gmail.com>
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%%
%% Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in the
%% documentation and/or other materials provided with the distribution.
%%
%% Neither the name of the author nor the names of its contributors
%% may be used to endorse or promote products derived from this software
%% without specific prior written permission.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(serctl).
-include("serctl.hrl").
-export([
open/1,
close/1,
read/2,
write/2,
readx/2, readx/3,
tcgetattr/1,
tcsetattr/3,
cfsetispeed/2,
cfsetospeed/2,
ioctl/3,
constant/0, constant/1,
termios/1,
setflag/2,
getflag/3,
flow/1, flow/2,
mode/1,
ispeed/1, ispeed/2,
ospeed/1, ospeed/2,
baud/1,
getfd/1,
offset/2,
wordalign/1, wordalign/2
]).
-export([
init/0
]).
-on_load(on_load/0).
%%--------------------------------------------------------------------
%%% NIF Stubs
%%--------------------------------------------------------------------
init() ->
on_load().
on_load() ->
erlang:load_nif(progname(), []).
open({fd, FD}) ->
fdopen(FD);
open(Dev) ->
open_nif(Dev).
open_nif(_) ->
erlang:error(not_implemented).
fdopen(_) ->
erlang:error(not_implemented).
close(_) ->
erlang:error(not_implemented).
read(_,_) ->
erlang:error(not_implemented).
write(_,_) ->
erlang:error(not_implemented).
tcgetattr(_) ->
erlang:error(not_implemented).
tcsetattr(FD, Action, Termios) when is_list(Action) ->
Option = lists:foldl(fun(X,N) -> constant(X) bxor N end, 0, Action),
tcsetattr(FD, Option, Termios);
tcsetattr(FD, Action, Termios) when is_atom(Action) ->
tcsetattr(FD, constant(Action), Termios);
tcsetattr(FD, Action, #termios{} = Termios) ->
tcsetattr(FD, Action, termios(Termios));
tcsetattr(FD, Action, Termios) ->
tcsetattr_nif(FD, Action, Termios).
tcsetattr_nif(_,_,_) ->
erlang:error(not_implemented).
cfsetispeed(#termios{} = Termios, Speed) ->
cfsetispeed(termios(Termios), Speed);
cfsetispeed(Termios, Speed) when is_atom(Speed) ->
cfsetispeed(termios(Termios), constant(Speed));
cfsetispeed(Termios, Speed) ->
cfsetispeed_nif(Termios, Speed).
cfsetispeed_nif(_,_) ->
erlang:error(not_implemented).
cfsetospeed(#termios{} = Termios, Speed) ->
cfsetospeed(termios(Termios), Speed);
cfsetospeed(Termios, Speed) when is_atom(Speed) ->
cfsetospeed(termios(Termios), constant(Speed));
cfsetospeed(Termios, Speed) ->
cfsetospeed_nif(Termios, Speed).
cfsetospeed_nif(_,_) ->
erlang:error(not_implemented).
ioctl(_,_,_) ->
erlang:error(not_implemented).
constant() ->
erlang:error(not_implemented).
constant(_) ->
erlang:error(not_implemented).
getfd(_) ->
erlang:error(not_implemented).
%%--------------------------------------------------------------------
%%% API
%%--------------------------------------------------------------------
readx(FD, N) ->
readx(FD, N, infinity).
readx(FD, N, Timeout) ->
Self = self(),
Pid = spawn(fun() -> poll(FD, N, Self) end),
receive
Any ->
Any
after
Timeout ->
exit(Pid, kill),
{error, eintr}
end.
setflag(Termios, Opt) when is_binary(Termios) ->
setflag(termios(Termios), Opt);
setflag(#termios{
cflag = Cflag0,
lflag = Lflag0,
iflag = Iflag0,
oflag = Oflag0
} = Termios, Opt) when is_list(Opt) ->
Cflag = setflag_1(Cflag0, proplists:get_value(cflag, Opt)),
Lflag = setflag_1(Lflag0, proplists:get_value(lflag, Opt)),
Iflag = setflag_1(Iflag0, proplists:get_value(iflag, Opt)),
Oflag = setflag_1(Oflag0, proplists:get_value(oflag, Opt)),
Termios#termios{
cflag = Cflag,
lflag = Lflag,
iflag = Iflag,
oflag = Oflag
}.
setflag_1(Val, undefined) ->
Val;
setflag_1(Val, []) ->
Val;
setflag_1(Bin, [{Offset, Val}|Rest]) when is_binary(Bin), Offset >= 0, Val >= 0 ->
setflag_1(offset(Bin, {Offset, Val}), Rest);
setflag_1(Val, [{Key, false}|Rest]) ->
Val1 = Val band bnot constant(Key),
setflag_1(Val1, Rest);
setflag_1(Val, [{Key, true}|Rest]) ->
Val1 = Val bor constant(Key),
setflag_1(Val1, Rest);
setflag_1(Val, [Key|Rest]) when is_atom(Key) ->
setflag_1(Val, [{Key, true}|Rest]).
getflag(Termios, Flag, Opt) when is_binary(Termios) ->
getflag(termios(Termios), Flag, Opt);
getflag(#termios{} = Termios, Flag, Opt) ->
getflag_1(Termios, Flag, Opt).
getflag_1(#termios{cflag = Flag}, cflag, Opt) ->
getflag_2(Flag, Opt);
getflag_1(#termios{lflag = Flag}, lflag, Opt) ->
getflag_2(Flag, Opt);
getflag_1(#termios{iflag = Flag}, iflag, Opt) ->
getflag_2(Flag, Opt);
getflag_1(#termios{oflag = Flag}, oflag, Opt) ->
getflag_2(Flag, Opt).
getflag_2(Flag, Opt) ->
N = constant(Opt),
N == Flag band N.
flow(Termios) ->
getflag(Termios, cflag, crtscts).
flow(Termios, Bool) when Bool == true; Bool == false ->
setflag(Termios, [{cflag, [{crtscts, Bool}]}]).
mode(raw) ->
#termios{
cc = lists:foldl(
fun({Offset, Val}, Bin) ->
offset(Bin, {Offset, Val})
end,
<<0:(constant(nccs)*8)>>, % zero'ed bytes
[
{constant(vmin), 1}, % Minimum number of characters
{constant(vtime), 0} % Timeout in deciseconds
]),
iflag = constant(ignpar), % ignore (discard) parity errors
cflag = constant(cs8)
bor constant(clocal)
bor constant(crtscts)
bor constant(cread)
}.
ispeed(Speed) when is_binary(Speed) ->
ispeed(termios(Speed));
ispeed(#termios{ispeed = Speed}) ->
Speed.
ispeed(Termios, Speed) when is_binary(Termios) ->
ispeed(termios(Termios), Speed);
ispeed(Termios, Speed) when is_atom(Speed) ->
ispeed(Termios, constant(Speed));
ispeed(#termios{} = Termios, Speed) when is_integer(Speed) ->
termios(cfsetispeed(Termios, Speed)).
ospeed(Speed) when is_binary(Speed) ->
ospeed(termios(Speed));
ospeed(#termios{ospeed = Speed}) ->
Speed.
ospeed(Termios, Speed) when is_binary(Termios) ->
ospeed(termios(Termios), Speed);
ospeed(Termios, Speed) when is_atom(Speed) ->
ospeed(Termios, constant(Speed));
ospeed(#termios{} = Termios, Speed) when is_integer(Speed) ->
termios(cfsetospeed(Termios, Speed)).
baud(Speed) when is_integer(Speed) ->
constant(list_to_atom("b" ++ integer_to_list(Speed))).
%% Terminal interface structure
%%
%% struct termios is used to control the behaviour of
%% the serial port. We pass the actual struct between
%% Erlang and C. Sending junk might cause the C side
%% to crash if there is a bug in the terminal lib. Using
%% a NIF resource would help but would require moving
%% some of the logic from Erlang to C (this would help
%% with portability though).
%%
%% Only the first 4 fields of the struct are standardized.
%% A simple way of handling portablity would be to parse
%% the first 4 fields and leave the rest as a binary.
%%
%% Linux:
%% #define NCCS 32
%% struct termios
%% {
%% tcflag_t c_iflag; /* input mode flags */
%% tcflag_t c_oflag; /* output mode flags */
%% tcflag_t c_cflag; /* control mode flags */
%% tcflag_t c_lflag; /* local mode flags */
%% cc_t c_line; /* line discipline */
%% cc_t c_cc[NCCS]; /* control characters */
%% speed_t c_ispeed; /* input speed */
%% speed_t c_ospeed; /* output speed */
%% #define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
%% #define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
%% };
%%
%% BSD (Max OS X, FreeBSD):
%% #define NCCS 20
%% struct termios {
%% tcflag_t c_iflag; /* input flags */
%% tcflag_t c_oflag; /* output flags */
%% tcflag_t c_cflag; /* control flags */
%% tcflag_t c_lflag; /* local flags */
%% cc_t c_cc[NCCS]; /* control chars */
%% speed_t c_ispeed; /* input speed */
%% speed_t c_ospeed; /* output speed */
%% };
termios(<<
?UINT32(Iflag), % input mode flags
?UINT32(Oflag), % output mode flags
?UINT32(Cflag), % control mode flags
?UINT32(Lflag), % local mode flags
Rest/binary>>) ->
LineSz = case os() of
linux -> 8;
bsd -> 0
end,
NCCS = constant(nccs),
<<
Line:LineSz, % line discipline
Cc:NCCS/bytes, % control characters
Rest1/binary
>> = Rest,
Pad = wordalign(LineSz div 8 + NCCS, 4),
<<
_:Pad,
?UINT32(Ispeed), % input speed
?UINT32(Ospeed) % output speed
>> = Rest1,
#termios{
iflag = Iflag,
oflag = Oflag,
cflag = Cflag,
lflag = Lflag,
line = Line,
cc = Cc,
ispeed = Ispeed,
ospeed = Ospeed
};
termios(#termios{
iflag = Iflag,
oflag = Oflag,
cflag = Cflag,
lflag = Lflag,
line = Line,
cc = Cc,
ispeed = Ispeed,
ospeed = Ospeed
}) ->
LineSz = case os() of
linux -> 8;
bsd -> 0
end,
NCCS = constant(nccs),
Cc1 = case Cc of
<<>> -> <<0:(NCCS*8)>>;
_ -> Cc
end,
Pad = wordalign(LineSz div 8 + NCCS, 4),
<<
?UINT32(Iflag),
?UINT32(Oflag),
?UINT32(Cflag),
?UINT32(Lflag),
Line:LineSz,
Cc1/binary,
0:Pad,
?UINT32(Ispeed),
?UINT32(Ospeed)
>>.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
progname() ->
filename:join([
filename:dirname(code:which(?MODULE)),
"..",
"priv",
?MODULE
]).
% Return pad size in bits
wordalign(Offset) ->
wordalign(Offset, erlang:system_info({wordsize, external})).
wordalign(Offset, Align) ->
((Align - (Offset rem Align)) rem Align) * 8.
offset(Cc, {Offset, Val}) when is_binary(Cc) ->
tuple_to_binary(
setelement(
Offset,
binary_to_tuple(Cc),
Val
)
).
binary_to_tuple(N) when is_binary(N) ->
list_to_tuple(binary_to_list(N)).
tuple_to_binary(N) when is_tuple(N) ->
list_to_binary(tuple_to_list(N)).
os() ->
case os:type() of
{unix, linux} -> linux;
{unix, freebsd} -> bsd;
{unix, darwin} -> bsd
end.
poll(FD, N, Pid) ->
poll(FD, N, N, Pid, []).
poll(FD, Total, N, Pid, Acc) ->
Size = iolist_size(Acc),
case read(FD, N) of
{ok, Buf} when byte_size(Buf) == Total ->
Pid ! {ok, Buf};
{ok, Buf} when byte_size(Buf) + Size == Total ->
Pid ! {ok, iolist_to_binary(lists:reverse([Buf|Acc]))};
{ok, Buf} ->
poll(FD, Total, N-byte_size(Buf), Pid, [Buf|Acc]);
{error, eagain} ->
timer:sleep(10),
poll(FD, Total, N, Pid, Acc);
{error, Error} ->
% XXX throw away away buffered data
Pid ! {error, Error}
end.