| 89 | } |
| 90 | |
| 91 | void xs_serial_constructor(xsMachine *the) |
| 92 | { |
| 93 | char device[128], format[64]; |
| 94 | speed_t baud; |
| 95 | CFSocketContext context; |
| 96 | int fd; |
| 97 | xsSerial s; |
| 98 | xsSlot onReadable, onWritable, onError; |
| 99 | |
| 100 | xsmcVars(2); |
| 101 | |
| 102 | xsmcGet(xsVar(0), xsArg(0), xsID_device); |
| 103 | xsmcToStringBuffer(xsVar(0), device, sizeof(device)); |
| 104 | |
| 105 | xsmcGet(xsVar(0), xsArg(0), xsID_baud); |
| 106 | baud = xsmcToInteger(xsVar(0)); |
| 107 | |
| 108 | xsmcGet(onReadable, xsArg(0), xsID_onReadable); |
| 109 | xsmcGet(onWritable, xsArg(0), xsID_onWritable); |
| 110 | xsmcGet(onError, xsArg(0), xsID_onError); |
| 111 | |
| 112 | if (xsmcGet(xsVar(0), xsArg(0), xsID_format)) |
| 113 | xsmcToStringBuffer(xsVar(0), format, sizeof(format)); |
| 114 | else |
| 115 | format[0] = 0; |
| 116 | |
| 117 | if (xsmcGet(xsVar(0), xsArg(0), xsID_target)) |
| 118 | xsmcSet(xsThis, xsID_target, xsVar(0)); |
| 119 | |
| 120 | s = calloc(1, sizeof(xsSerialRecord)); |
| 121 | if (!s) |
| 122 | xsUnknownError("no memory"); |
| 123 | xsmcSetHostData(xsThis, s); |
| 124 | s->the = the; |
| 125 | s->obj = xsThis; |
| 126 | s->hasOnWritable = xsmcTest(onWritable); |
| 127 | s->hasOnReadable = xsmcTest(onReadable); |
| 128 | s->hasOnError = xsmcTest(onError); |
| 129 | |
| 130 | if (format[0]) |
| 131 | fxSerialSetFormat(the, s, format); |
| 132 | else |
| 133 | s->bufferFormat = 1; |
| 134 | |
| 135 | fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 136 | if (-1 == fd) |
| 137 | xsUnknownError("can't open serial"); |
| 138 | |
| 139 | if (-1 == ioctl(fd, TIOCEXCL)) { |
| 140 | close(fd); |
| 141 | xsUnknownError("Error setting TIOCEXCL"); |
| 142 | } |
| 143 | |
| 144 | if (-1 == ioctl(fd, IOSSIOSPEED, &baud)) { |
| 145 | close(fd); |
| 146 | xsUnknownError( "Error calling ioctl(..., IOSSIOSPEED, ..."); |
| 147 | } |
| 148 |
nothing calls this directly
no test coverage detected