mrb_value mrb_serialport_open(mrb_state *mrb, mrb_value self) { int fd; mrb_value mrb_portname = IV_GET("@port_name"); mrb_value mrb_baud = IV_GET("@baud"); mrb_value mrb_blocking = IV_GET("@blocking"); const char *portname = mrb_string_value_cstr(mrb, &mrb_portname); unsigned int baud = mrb_fixnum(mrb_baud); fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (!isatty(fd)) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (set_interface_attribs(fd, baud, 0)) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (set_blocking(fd, mrb_bool(mrb_blocking) ? 1 : 0) != 0) { IV_SET("@error", mrb_str_new_cstr(mrb, "Could not set blocking behavior")); mrb_raise(mrb, E_RUNTIME_ERROR, "Could not set blocking behavior"); } IV_SET("@fd", mrb_fixnum_value(fd)); return self; }
mrb_value mrb_mraa_uart_read_to_prompt(mrb_state *mrb, mrb_value self) { mraa_uart_context uart; mrb_int timeout, nargs, i; char prompt, str_arg[1], buf[1]; mrb_value result; timeout = mrb_fixnum(IV_GET("@timeout")); nargs = mrb_get_args(mrb, "|z", &str_arg); if (nargs == 0) { char *str = mrb_str_to_cstr(mrb, IV_GET("@prompt")); prompt = str[0]; } else prompt = str_arg[0]; uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type); result = mrb_str_buf_new(mrb, mrb_fixnum(IV_GET("@read_bufsize"))); while (mraa_uart_data_available(uart, timeout) > 0) { i = mraa_uart_read(uart, buf, 1); if (i == 0) break; if (*buf == prompt) break; result = mrb_str_cat_cstr(mrb, result, buf); } return result; }
static mrb_value method_eql(mrb_state *mrb, mrb_value self) { mrb_value other, receiver, orig_proc, other_proc; struct RClass *owner, *klass; struct RProc *orig_rproc, *other_rproc; mrb_get_args(mrb, "o", &other); if (!mrb_obj_is_instance_of(mrb, other, mrb_class(mrb, self))) return mrb_false_value(); if (mrb_class(mrb, self) != mrb_class(mrb, other)) return mrb_false_value(); klass = mrb_class_ptr(IV_GET(self, "@klass")); if (klass != mrb_class_ptr(IV_GET(other, "@klass"))) return mrb_false_value(); owner = mrb_class_ptr(IV_GET(self, "@owner")); if (owner != mrb_class_ptr(IV_GET(other, "@owner"))) return mrb_false_value(); receiver = IV_GET(self, "@recv"); if (!mrb_obj_equal(mrb, receiver, IV_GET(other, "@recv"))) return mrb_false_value(); orig_proc = IV_GET(self, "proc"); other_proc = IV_GET(other, "proc"); if (mrb_nil_p(orig_proc) && mrb_nil_p(other_proc)) { if (mrb_symbol(IV_GET(self, "@name")) == mrb_symbol(IV_GET(other, "@name"))) return mrb_true_value(); else return mrb_false_value(); } if (mrb_nil_p(orig_proc)) return mrb_false_value(); if (mrb_nil_p(other_proc)) return mrb_false_value(); orig_rproc = mrb_proc_ptr(orig_proc); other_rproc = mrb_proc_ptr(other_proc); if (MRB_PROC_CFUNC_P(orig_rproc)) { if (!MRB_PROC_CFUNC_P(other_rproc)) return mrb_false_value(); if (orig_rproc->body.func != other_rproc->body.func) return mrb_false_value(); } else { if (MRB_PROC_CFUNC_P(other_rproc)) return mrb_false_value(); if (orig_rproc->body.irep != other_rproc->body.irep) return mrb_false_value(); } return mrb_true_value(); }
mrb_value mrb_serialport_p_read(mrb_state *mrb, mrb_value self) { mrb_value r_buffer_size, r_result; ssize_t buf_len; char *string = NULL; int fd = mrb_fixnum(IV_GET("@fd")); mrb_get_args(mrb, "i", &r_buffer_size); buf_len = mrb_fixnum(r_buffer_size); string = (char *)realloc(string, buf_len * sizeof(char)); if (!string) { free(string); string = NULL; mrb_raise(mrb, E_RUNTIME_ERROR, "Could not allocate memory"); } bzero(string, buf_len); buf_len = read(fd, string, buf_len * sizeof(char)); if ( buf_len == 0 ) { return mrb_nil_value(); } else if ( buf_len < 0 ) { if ( errno == EAGAIN ) return mrb_nil_value(); update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } r_result = mrb_str_new_cstr(mrb, string); free(string); return r_result; }
mrb_value mrb_serialport_flush(mrb_state *mrb, mrb_value self) { int fd = mrb_fixnum(IV_GET("@fd")); if (tcflush(fd, TCIOFLUSH) != 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } return mrb_true_value(); }
mrb_value mrb_serialport_available(mrb_state *mrb, mrb_value self) { int fd = mrb_fixnum(IV_GET("@fd")); int bytes_available; if (ioctl(fd, FIONREAD, &bytes_available) != 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } return mrb_fixnum_value(bytes_available); }
mrb_value mrb_serialport_close(mrb_state *mrb, mrb_value self) { int fd = mrb_fixnum(IV_GET("@fd")); if (close(fd) == 0) { IV_SET("@fd", mrb_fixnum_value(-1)); return mrb_true_value(); } update_error(mrb, self); return mrb_false_value(); }
mrb_value mrb_serialport_p_write(mrb_state *mrb, mrb_value self) { char *string; size_t l = 0; ssize_t res = 0; int fd = mrb_fixnum(IV_GET("@fd")); mrb_get_args(mrb, "s", &string, &l); res = write(fd, (char *)string, l); if (res < 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } return mrb_fixnum_value(l); }
mrb_value mrb_serialport_read_char(mrb_state *mrb, mrb_value self) { char ch[1]; ssize_t len = 0; int fd = mrb_fixnum(IV_GET("@fd")); len = read(fd, ch, sizeof(char)); if ( len == 0 ) { return mrb_nil_value(); } else if ( len < 0 ) { if ( errno == EAGAIN ) return mrb_nil_value(); update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } return mrb_str_new_cstr(mrb, ch); }
mrb_value mrb_mraa_uart_read(mrb_state *mrb, mrb_value self) { mraa_uart_context uart; mraa_result_t result; char *string; mrb_value mrb_result; int bufsize; if ((bufsize = mrb_fixnum(IV_GET("@read_bufsize"))) <= 0) bufsize = UART_DEFAULT_BUFSIZE; string = calloc(bufsize, sizeof(char)); uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type); result = mraa_uart_read(uart, string, bufsize); if (result < 0) { mrb_raisef(mrb, E_RUNTIME_ERROR, "Could not read (err %S)", mrb_fixnum_value(result)); } mrb_result = mrb_str_new_cstr(mrb, string); free(string); return mrb_result; }
mrb_value mrb_toggle_dtr(mrb_state *mrb, mrb_value self) { int fd = mrb_fixnum(IV_GET("@fd")); int val = 0, res = 0; int status = 0; mrb_get_args(mrb, "i", &val); if (val == 0) { #ifdef TIOCCDTR res = ioctl(fd, TIOCCDTR, NULL); #elif defined(TIOCM_DTR) int iFlags; iFlags = TIOCM_DTR; res = ioctl(fd, TIOCMBIC, &iFlags); #else #error Dunno how to manage DTR #endif } else if (val == 1) { #ifdef TIOCCDTR res = ioctl(fd, TIOCSDTR, NULL); #elif defined(TIOCM_DTR) int iFlags; iFlags = TIOCM_DTR; res = ioctl(fd, TIOCMBIS, &iFlags); #else #error Dunno how to manage DTR #endif } else { ioctl(fd, TIOCMGET, &status); return mrb_fixnum_value(status); } if (res != 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } return mrb_true_value(); }