Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FreeBSD #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ addons:
env:
- RUBYOPT=-rbundler/deprecate # workaround for bundler on rbx
rvm:
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- jruby-9.1.2.0
- rbx-3
- ruby-head
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
source "http://rubygems.org"

gemspec
gem 'rspec', '~> 3.0.0'
gem 'rspec', '~> 3'
gem 'simplecov', require: false, group: :test
gem "codeclimate-test-reporter", '~> 1.0', group: :test, require: false
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RubySerial is a simple Ruby gem for reading from and writing to serial ports.

Unlike other Ruby serial port implementations, it supports all of the most popular Ruby implementations (MRI, JRuby, & Rubinius) on the most popular operating systems (OSX, Linux, & Windows). And it does not require any native compilation thanks to using RubyFFI [https://github.com/ffi/ffi](https://github.com/ffi/ffi).
Unlike other Ruby serial port implementations, it supports all of the most popular Ruby implementations (MRI, JRuby, & Rubinius) on the most popular operating systems (macOS, Linux, FreeBSD & Windows). And it does not require any native compilation thanks to using RubyFFI [https://github.com/ffi/ffi](https://github.com/ffi/ffi).

The interface to RubySerial should be (mostly) compatible with other Ruby serialport gems, so you should be able to drop in the new gem, change the `require` and use it as a replacement. If not, please let us know so we can address any issues.

Expand Down Expand Up @@ -51,9 +51,9 @@ The test suite is written using rspec, just use the `rspec` command.

### Test dependencies

To run the tests on OS X and Linux, you must also have the `socat` utility program installed.
To run the tests on macOS, Linux and FreeBSD, you must also have the `socat` utility program installed.

#### Installing socat on OS X
#### Installing socat on macOS

```
brew install socat
Expand All @@ -65,6 +65,12 @@ brew install socat
sudo apt-get install socat
```

#### Installing socat on FreeBSD

```
pkg install socat
```

#### Test on Windows

To run the tests on Windows requires com0com which can be downloaded from here:
Expand Down
6 changes: 2 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ test_script:

environment:
matrix:
- ruby_version: 26
- ruby_version: 25
- ruby_version: 24
- ruby_version: 23
- ruby_version: 22
- ruby_version: 21
- ruby_version: 200

build: off
5 changes: 4 additions & 1 deletion lib/rubyserial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module RubySerial
ON_WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
ON_LINUX = RbConfig::CONFIG['host_os'] =~ /linux/i
ON_FREEBSD = RbConfig::CONFIG['host_os'] =~ /freebsd/i
class Error < IOError
end
end
Expand All @@ -16,8 +17,10 @@ class Error < IOError
else
if RubySerial::ON_LINUX
require 'rubyserial/linux_constants'
elsif RubySerial::ON_FREEBSD
require 'rubyserial/freebsd_constants'
else
require 'rubyserial/osx_constants'
require 'rubyserial/darwin_constants'
end
require 'rubyserial/posix'
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ module Posix
extend FFI::Library
ffi_lib FFI::Library::LIBC

O_RDWR = 0x0002
O_NOCTTY = 0x20000
O_NONBLOCK = 0x0004
O_NOCTTY = 0x20000
O_RDWR = 0x0002
F_GETFL = 3
F_SETFL = 4
VTIME = 17
TCSANOW = 0

IGNPAR = 0x00000004
PARENB = 0x00001000
PARODD = 0x00002000
VMIN = 16
VTIME = 17
CLOCAL = 0x00008000
CSTOPB = 0x00000400
CREAD = 0x00000800
CCTS_OFLOW = 0x00010000 # Clearing this disables RTS AND CTS.
TCSANOW = 0
CLOCAL = 0x00008000
VMIN = 16
NCCS = 20
CCTS_OFLOW = 0x00010000 # Clearing this disables RTS AND CTS.

DATA_BITS = {
5 => 0x00000000,
Expand All @@ -29,7 +30,7 @@ module Posix
8 => 0x00000300
}

BAUDE_RATES = {
BAUD_RATES = {
0 => 0,
50 => 50,
75 => 75,
Expand Down
223 changes: 223 additions & 0 deletions lib/rubyserial/freebsd_constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
# Copyright (c) 2014-2016 The Hybrid Group

module RubySerial
module Posix
extend FFI::Library
ffi_lib FFI::Library::LIBC

O_NONBLOCK = 0x0004
O_NOCTTY = 0x8000
O_RDWR = 0x0002
F_GETFL = 3
F_SETFL = 4
VTIME = 17
TCSANOW = 0

IGNPAR = 0x00000004
PARENB = 0x00001000
PARODD = 0x00002000
CSTOPB = 0x00000400
CREAD = 0x00000800
CLOCAL = 0x00008000
VMIN = 16
NCCS = 20
CCTS_OFLOW = 0x00010000 # Clearing this disables RTS AND CTS.

DATA_BITS = {
5 => 0x00000000,
6 => 0x00000100,
7 => 0x00000200,
8 => 0x00000300
}

BAUD_RATES = {
0 => 0,
50 => 50,
75 => 75,
110 => 110,
134 => 134,
150 => 150,
200 => 200,
300 => 300,
600 => 600,
1200 => 1200,
1800 => 1800,
2400 => 2400,
4800 => 4800,
9600 => 9600,
19200 => 19200,
38400 => 38400,
7200 => 7200,
14400 => 14400,
28800 => 28800,
57600 => 57600,
76800 => 76800,
115200 => 115200,
230400 => 230400
}

PARITY = {
:none => 0x00000000,
:even => PARENB,
:odd => PARENB | PARODD,
}

STOPBITS = {
1 => 0x00000000,
2 => CSTOPB
}

ERROR_CODES = {
1 => "EPERM",
2 => "ENOENT",
3 => "ESRCH",
4 => "EINTR",
5 => "EIO",
6 => "ENXIO",
7 => "E2BIG",
8 => "ENOEXEC",
9 => "EBADF",
10 => "ECHILD",
11 => "EAGAIN",
12 => "ENOMEM",
13 => "EACCES",
14 => "EFAULT",
15 => "ENOTBLK",
16 => "EBUSY",
17 => "EEXIST",
18 => "EXDEV",
19 => "ENODEV",
20 => "ENOTDIR",
21 => "EISDIR",
22 => "EINVAL",
23 => "ENFILE",
24 => "EMFILE",
25 => "ENOTTY",
26 => "ETXTBSY",
27 => "EFBIG",
28 => "ENOSPC",
29 => "ESPIPE",
30 => "EROFS",
31 => "EMLINK",
32 => "EPIPE",
33 => "EDOM",
34 => "ERANGE",
35 => "EDEADLK",
36 => "ENAMETOOLONG",
37 => "ENOLCK ",
38 => "ENOSYS",
39 => "ENOTEMPTY",
40 => "ELOOP",

42 => "ENOMSG",
43 => "EIDRM",
44 => "ECHRNG",
45 => "EL2NSYNC",
46 => "EL3HLT",
47 => "EL3RST",
48 => "ELNRNG",
49 => "EUNATCH",
50 => "ENOCSI",
51 => "EL2HLT",
52 => "EBADE",
53 => "EBADR",
54 => "EXFULL",
55 => "ENOANO",
56 => "EBADRQC",
57 => "EBADSLT",

59 => "EBFONT",
60 => "ENOSTR",
61 => "ENODATA",
62 => "ETIME",
63 => "ENOSR",
64 => "ENONET",
65 => "ENOPKG",
66 => "EREMOTE",
67 => "ENOLINK",
68 => "EADV",
69 => "ESRMNT",
70 => "ECOMM",
71 => "EPROTO",
72 => "EMULTIHOP",
73 => "EDOTDOT",
74 => "EBADMSG",
75 => "EOVERFLOW",
76 => "ENOTUNIQ",
77 => "EBADFD",
78 => "EREMCHG",
79 => "ELIBACC",
80 => "ELIBBAD",
81 => "ELIBSCN",
82 => "ELIBMAX",
83 => "ELIBEXEC",
84 => "EILSEQ",
85 => "ERESTART",
86 => "ESTRPIPE",
87 => "EUSERS",
88 => "ENOTSOCK",
89 => "EDESTADDRREQ",
90 => "EMSGSIZE",
91 => "EPROTOTYPE",
92 => "ENOPROTOOPT",
93 => "EPROTONOSUPPORT",
94 => "ESOCKTNOSUPPORT",
95 => "EOPNOTSUPP",
96 => "EPFNOSUPPORT",
97 => "EAFNOSUPPORT",
98 => "EADDRINUSE",
99 => "EADDRNOTAVAIL",
100 => "ENETDOWN",
101 => "ENETUNREACH",
102 => "ENETRESET",
103 => "ECONNABORTED",
104 => "ECONNRESET",
105 => "ENOBUFS",
106 => "EISCONN",
107 => "ENOTCONN",
108 => "ESHUTDOWN",
109 => "ETOOMANYREFS",
110 => "ETIMEDOUT",
111 => "ECONNREFUSED",
112 => "EHOSTDOWN",
113 => "EHOSTUNREACH",
114 => "EALREADY",
115 => "EINPROGRESS",
116 => "ESTALE",
117 => "EUCLEAN",
118 => "ENOTNAM",
119 => "ENAVAIL",
120 => "EISNAM",
121 => "EREMOTEIO",
122 => "EDQUOT",
123 => "ENOMEDIUM",
124 => "EMEDIUMTYPE",
125 => "ECANCELED",
126 => "ENOKEY",
127 => "EKEYEXPIRED",
128 => "EKEYREVOKED",
129 => "EKEYREJECTED",
130 => "EOWNERDEAD",
131 => "ENOTRECOVERABLE"
}

class Termios < FFI::Struct
layout :c_iflag, :uint,
:c_oflag, :uint,
:c_cflag, :uint,
:c_lflag, :uint,
:c_line, :uchar,
:cc_c, [ :uchar, NCCS ],
:c_ispeed, :uint,
:c_ospeed, :uint
end

attach_function :ioctl, [ :int, :ulong, RubySerial::Posix::Termios], :int, blocking: true
attach_function :tcsetattr, [ :int, :int, RubySerial::Posix::Termios ], :int, blocking: true
attach_function :fcntl, [:int, :int, :varargs], :int, blocking: true
attach_function :open, [:pointer, :int], :int, blocking: true
attach_function :close, [:int], :int, blocking: true
attach_function :write, [:int, :pointer, :int],:int, blocking: true
attach_function :read, [:int, :pointer, :int],:int, blocking: true
end
end
6 changes: 4 additions & 2 deletions lib/rubyserial/linux_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Posix
8 => 0000060
}

BAUDE_RATES = {
BAUD_RATES = {
0 => 0000000,
50 => 0000001,
75 => 0000002,
Expand Down Expand Up @@ -94,7 +94,7 @@ module Posix
17 => "EEXIST",
18 => "EXDEV",
19 => "ENODEV",
20 => "ENOTDIR ",
20 => "ENOTDIR",
21 => "EISDIR",
22 => "EINVAL",
23 => "ENFILE",
Expand All @@ -115,6 +115,7 @@ module Posix
38 => "ENOSYS",
39 => "ENOTEMPTY",
40 => "ELOOP",

42 => "ENOMSG",
43 => "EIDRM",
44 => "ECHRNG",
Expand All @@ -131,6 +132,7 @@ module Posix
55 => "ENOANO",
56 => "EBADRQC",
57 => "EBADSLT",

59 => "EBFONT",
60 => "ENOSTR",
61 => "ENODATA",
Expand Down
Loading