MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xs_serial_constructor

Function xs_serial_constructor

modules/io/serial/mac/serial.c:91–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89}
90
91void 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

Callers

nothing calls this directly

Calls 4

fxSerialSetFormatFunction · 0.85
openFunction · 0.50
closeFunction · 0.50
modTimerAddFunction · 0.50

Tested by

no test coverage detected