-
- if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0 ||
- tcsetattr (fd, TCSANOW, &newtio) < 0)
- {
+
+ if (custom_baud_rate == FALSE) {
+ if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
+ return FALSE;
+ } else {
+#if __linux__ || defined(HAVE_IOKIT)
+
+ /* On Linux to set a custom baud rate, we must set the
+ * "standard" baud_rate to 38400. On Apple we set it purely
+ * so that tcsetattr has something to use (and report back later), but
+ * the Apple specific API is still opaque to these APIs, see:
+ * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
+ */
+ if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
+ return FALSE;
+#endif
+ }
+
+ if (tcsetattr (fd, TCSANOW, &newtio) < 0)