-void uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
-{
- DBG("Serial port speed requested to be set to %d bauds.", uiPortSpeed);
- // Set port speed (Input and Output)
- speed_t stPortSpeed = B9600;
- switch(uiPortSpeed) {
- case 9600: stPortSpeed = B9600;
- break;
- case 19200: stPortSpeed = B19200;
- break;
- case 38400: stPortSpeed = B38400;
- break;
- case 57600: stPortSpeed = B57600;
- break;
- case 115200: stPortSpeed = B115200;
- break;
- case 230400: stPortSpeed = B230400;
- break;
-#ifdef B460800
- case 460800: stPortSpeed = B460800;
- break;
-#endif
- default:
-#ifdef B460800
- ERR("Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed);
-#else
- ERR("Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200 or 230400.", uiPortSpeed);
-#endif
+void uart_close(const serial_port sp) {
+ serial_port_unix* spu = (serial_port_unix*)sp;
+ tcflush(spu->fd,TCIOFLUSH);
+ tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
+ struct flock fl;
+ fl.l_type = F_UNLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fl.l_pid = getpid();
+
+ // Does the system allows us to place a lock on this file descriptor
+ int err = fcntl(spu->fd, F_SETLK, &fl);
+ if ( err == -1) {
+ perror("fcntl");
+ }
+ close(spu->fd);
+ free(sp);
+}
+
+bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
+ const serial_port_unix* spu = (serial_port_unix*)sp;
+ speed_t stPortSpeed;
+ switch (uiPortSpeed) {
+ case 0: stPortSpeed = B0; break;
+ case 50: stPortSpeed = B50; break;
+ case 75: stPortSpeed = B75; break;
+ case 110: stPortSpeed = B110; break;
+ case 134: stPortSpeed = B134; break;
+ case 150: stPortSpeed = B150; break;
+ case 300: stPortSpeed = B300; break;
+ case 600: stPortSpeed = B600; break;
+ case 1200: stPortSpeed = B1200; break;
+ case 1800: stPortSpeed = B1800; break;
+ case 2400: stPortSpeed = B2400; break;
+ case 4800: stPortSpeed = B4800; break;
+ case 9600: stPortSpeed = B9600; break;
+ case 19200: stPortSpeed = B19200; break;
+ case 38400: stPortSpeed = B38400; break;
+# ifdef B57600
+ case 57600: stPortSpeed = B57600; break;
+# endif
+# ifdef B115200
+ case 115200: stPortSpeed = B115200; break;
+# endif
+# ifdef B230400
+ case 230400: stPortSpeed = B230400; break;
+# endif
+# ifdef B460800
+ case 460800: stPortSpeed = B460800; break;
+# endif
+# ifdef B921600
+ case 921600: stPortSpeed = B921600; break;
+# endif
+ default: return false;