Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source "https://rubygems.org"
source "http://gems.github.com"

gemspec
3 changes: 3 additions & 0 deletions ext/native/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
exit(1) if not have_header("termios.h") or not have_header("unistd.h")
end

have_func("rb_io_descriptor") # ruby-3.1+
have_func("rb_io_open_descriptor") # ruby-3.3+

create_makefile('serialport')
107 changes: 32 additions & 75 deletions ext/native/posix_serialport_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,23 @@ static char sTcsetattr[] = "tcsetattr";
static char sIoctl[] = "ioctl";


int get_fd_helper(obj)
VALUE obj;
int get_fd_helper(VALUE io)
{
#ifdef HAVE_RUBY_IO_H
rb_io_t *fptr;
#ifdef HAVE_RB_IO_DESCRIPTOR
return rb_io_descriptor(io);
#else
OpenFile *fptr;
#endif
GetOpenFile(obj, fptr);
#ifdef HAVE_RUBY_IO_H
return (fptr->fd);
#else
return (fileno(fptr->f));
rb_io_t* fp;
GetOpenFile(io, fp);
return fp->fd;
#endif
}

VALUE sp_create_impl(class, _port)
VALUE class, _port;
VALUE sp_create_impl(VALUE class, VALUE _port)
{
#ifdef HAVE_RUBY_IO_H
rb_io_t *fp;
#else
OpenFile *fp;
#endif

int fd;
int num_port;
char *port;
char *ports[] = {
long num_port;
const char *port;
const char *ports[] = {
#if defined(OS_LINUX) || defined(OS_CYGWIN)
"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6", "/dev/ttyS7"
Expand All @@ -108,16 +96,13 @@ VALUE sp_create_impl(class, _port)
#endif
};
struct termios params;

NEWOBJ(sp, struct RFile);
OBJSETUP(sp, class, T_FILE);
MakeOpenFile((VALUE) sp, fp);
VALUE sp;

switch(TYPE(_port))
{
case T_FIXNUM:
num_port = FIX2INT(_port);
if (num_port < 0 || num_port > sizeof(ports) / sizeof(ports[0]))
num_port = FIX2LONG(_port);
if (num_port < 0 || num_port > (long)(sizeof(ports) / sizeof(ports[0])))
{
rb_raise(rb_eArgError, "illegal port number");
}
Expand Down Expand Up @@ -166,19 +151,12 @@ VALUE sp_create_impl(class, _port)
rb_sys_fail(sTcsetattr);
}

#ifdef HAVE_RUBY_IO_H
fp->fd = fd;
#else
fp->f = rb_fdopen(fd, "r+");
#endif
fp->mode = FMODE_READWRITE | FMODE_SYNC;
sp = rb_io_open_descriptor(class, fd, FMODE_READWRITE | FMODE_SYNC, Qnil, Qnil, NULL);

return (VALUE) sp;
return sp;
}

VALUE sp_set_modem_params_impl(argc, argv, self)
int argc;
VALUE *argv, self;
VALUE sp_set_modem_params_impl(int argc, VALUE *argv, VALUE self)
{
int fd;
struct termios params;
Expand Down Expand Up @@ -399,9 +377,7 @@ VALUE sp_set_modem_params_impl(argc, argv, self)
return argv[0];
}

void get_modem_params_impl(self, mp)
VALUE self;
struct modem_params *mp;
void get_modem_params_impl(VALUE self, struct modem_params *mp)
{
int fd;
struct termios params;
Expand Down Expand Up @@ -493,8 +469,7 @@ void get_modem_params_impl(self, mp)
}
}

VALUE sp_set_flow_control_impl(self, val)
VALUE self, val;
VALUE sp_set_flow_control_impl(VALUE self, VALUE val)
{
int fd;
int flowc;
Expand Down Expand Up @@ -540,8 +515,7 @@ VALUE sp_set_flow_control_impl(self, val)
return val;
}

VALUE sp_get_flow_control_impl(self)
VALUE self;
VALUE sp_get_flow_control_impl(VALUE self)
{
int ret;
int fd;
Expand Down Expand Up @@ -570,8 +544,7 @@ VALUE sp_get_flow_control_impl(self)
return INT2FIX(ret);
}

VALUE sp_set_read_timeout_impl(self, val)
VALUE self, val;
VALUE sp_set_read_timeout_impl(VALUE self, VALUE val)
{
int timeout;
int fd;
Expand Down Expand Up @@ -610,8 +583,7 @@ VALUE sp_set_read_timeout_impl(self, val)
return val;
}

VALUE sp_get_read_timeout_impl(self)
VALUE self;
VALUE sp_get_read_timeout_impl(VALUE self)
{
int fd;
struct termios params;
Expand All @@ -630,22 +602,17 @@ VALUE sp_get_read_timeout_impl(self)
return INT2FIX(params.c_cc[VTIME] * 100);
}

VALUE sp_set_write_timeout_impl(self, val)
VALUE self, val;
NORETURN(VALUE sp_set_write_timeout_impl(VALUE self, VALUE val))
{
rb_notimplement();
return self;
}

VALUE sp_get_write_timeout_impl(self)
VALUE self;
NORETURN(VALUE sp_get_write_timeout_impl(VALUE self))
{
rb_notimplement();
return self;
}

VALUE sp_break_impl(self, time)
VALUE self, time;
VALUE sp_break_impl(VALUE self, VALUE time)
{
int fd;

Expand All @@ -661,9 +628,7 @@ VALUE sp_break_impl(self, time)
return Qnil;
}

void get_line_signals_helper_impl(obj, ls)
VALUE obj;
struct line_signals *ls;
void get_line_signals_helper_impl(VALUE obj, struct line_signals *ls)
{
int fd, status;

Expand All @@ -682,9 +647,7 @@ void get_line_signals_helper_impl(obj, ls)
ls->ri = (status & TIOCM_RI ? 1 : 0);
}

VALUE set_signal_impl(obj, val, sig)
VALUE obj,val;
int sig;
VALUE set_signal_impl(VALUE obj, VALUE val, int sig)
{
int status;
int fd;
Expand Down Expand Up @@ -721,29 +684,25 @@ VALUE set_signal_impl(obj, val, sig)
return val;
}

VALUE sp_set_rts_impl(self, val)
VALUE self, val;
VALUE sp_set_rts_impl(VALUE self, VALUE val)
{
return set_signal_impl(self, val, TIOCM_RTS);
}

VALUE sp_set_dtr_impl(self, val)
VALUE self, val;
VALUE sp_set_dtr_impl(VALUE self, VALUE val)
{
return set_signal_impl(self, val, TIOCM_DTR);
}

VALUE sp_get_rts_impl(self)
VALUE self;
VALUE sp_get_rts_impl(VALUE self)
{
struct line_signals ls;

get_line_signals_helper_impl(self, &ls);
return INT2FIX(ls.rts);
}

VALUE sp_get_dtr_impl(self)
VALUE self;
VALUE sp_get_dtr_impl(VALUE self)
{
struct line_signals ls;

Expand All @@ -752,8 +711,7 @@ VALUE sp_get_dtr_impl(self)
return INT2FIX(ls.dtr);
}

VALUE sp_flush_input_data_impl(self)
VALUE self;
VALUE sp_flush_input_data_impl(VALUE self)
{
int fd;
int ret;
Expand All @@ -768,8 +726,7 @@ VALUE self;
return Qtrue;
}

VALUE sp_flush_output_data_impl(self)
VALUE self;
VALUE sp_flush_output_data_impl(VALUE self)
{
int fd;
int ret;
Expand Down
Loading