| 1 | /* |
| 2 | * RPLIDAR SDK |
| 3 | * |
| 4 | * Copyright (c) 2009 - 2014 RoboPeak Team |
| 5 | * http://www.robopeak.com |
| 6 | * Copyright (c) 2014 - 2018 Shanghai Slamtec Co., Ltd. |
| 7 | * http://www.slamtec.com |
| 8 | * |
| 9 | */ |
| 10 | /* |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions are met: |
| 13 | * |
| 14 | * 1. Redistributions of source code must retain the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer. |
| 16 | * |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | * this list of conditions and the following disclaimer in the documentation |
| 19 | * and/or other materials provided with the distribution. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 28 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 31 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | //#include "arch/linux/arch_linux.h" |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | #include <assert.h> |
| 41 | // linux specific |
| 42 | |
| 43 | //#include <errno.h> |
| 44 | #include <fcntl.h> |
| 45 | |
| 46 | #include <time.h> |
| 47 | #include "hal/types.h" |
| 48 | #include "arch/linux/net_serial.h" |
| 49 | #include <sys/select.h> |
| 50 | |
| 51 | #include <algorithm> |
| 52 | //__GNUC__ |
| 53 | #if defined(__GNUC__) |
| 54 | // for Linux extension |
| 55 | #include <asm/ioctls.h> |
| 56 | #include <asm/termbits.h> |
| 57 | #include <sys/ioctl.h> |
| 58 | extern "C" int tcflush(int fildes, int queue_selector); |
| 59 | #else |
| 60 | // for other standard UNIX |
| 61 | #include <termios.h> |
| 62 | #include <sys/ioctl.h> |
| 63 | |
| 64 | #endif |
| 65 | |
| 66 | |
| 67 | namespace rp{ namespace arch{ namespace net{ |
| 68 | |
| 69 | raw_serial::raw_serial() |
| 70 | : rp::hal::serial_rxtx() |
| 71 | , _baudrate(0) |
| 72 | , _flags(0) |
| 73 | , serial_fd(-1) |
| 74 | { |
| 75 | _init(); |
| 76 | } |
| 77 | |
| 78 | raw_serial::~raw_serial() |
| 79 | { |
| 80 | close(); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | bool raw_serial::open() |
| 85 | { |
| 86 | return open(_portName, _baudrate, _flags); |
| 87 | } |
| 88 | |
| 89 | bool raw_serial::bind(const char * portname, uint32_t baudrate, uint32_t flags) |
| 90 | { |
| 91 | volatile int len = sizeof(_portName); // Just to silent truncation warning |
| 92 | strncpy(_portName, portname, len); |
| 93 | _baudrate = baudrate; |
| 94 | _flags = flags; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool raw_serial::open(const char * portname, uint32_t baudrate, uint32_t flags) |
| 99 | { |
| 100 | if (isOpened()) close(); |
| 101 | |
| 102 | serial_fd = ::open(portname, O_RDWR | O_NOCTTY | O_NDELAY); |
| 103 | |
| 104 | if (serial_fd == -1) return false; |
| 105 | |
| 106 | |
| 107 | |
| 108 | #if !defined(__GNUC__) |
| 109 | // for standard UNIX |
| 110 | struct termios options, oldopt; |
| 111 | tcgetattr(serial_fd, &oldopt); |
| 112 | bzero(&options,sizeof(struct termios)); |
| 113 | |
| 114 | // enable rx and tx |
| 115 | options.c_cflag |= (CLOCAL | CREAD); |
| 116 | |
| 117 | _u32 termbaud = getTermBaudBitmap(baudrate); |
| 118 | |
| 119 | if (termbaud == (_u32)-1) { |
| 120 | close(); |
| 121 | return false; |
| 122 | } |
| 123 | cfsetispeed(&options, termbaud); |
| 124 | cfsetospeed(&options, termbaud); |
| 125 | |
| 126 | options.c_cflag &= ~PARENB; //no checkbit |
| 127 | options.c_cflag &= ~CSTOPB; //1bit stop bit |
| 128 | options.c_cflag &= ~CRTSCTS; //no flow control |
| 129 | |
| 130 | options.c_cflag &= ~CSIZE; |
| 131 | options.c_cflag |= CS8; /* Select 8 data bits */ |
| 132 | |
| 133 | #ifdef CNEW_RTSCTS |
| 134 | options.c_cflag &= ~CNEW_RTSCTS; // no hw flow control |
| 135 | #endif |
| 136 | |
| 137 | options.c_iflag &= ~(IXON | IXOFF | IXANY); // no sw flow control |
| 138 | |
| 139 | // raw input mode |
| 140 | options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 141 | // raw output mode |
| 142 | options.c_oflag &= ~OPOST; |
| 143 | |
| 144 | |
| 145 | |
| 146 | if (tcsetattr(serial_fd, TCSANOW, &options)) |
| 147 | { |
| 148 | close(); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | #else |
| 153 | |
| 154 | // using Linux extension ... |
| 155 | struct termios2 tio; |
| 156 | |
| 157 | ioctl(serial_fd, TCGETS2, &tio); |
| 158 | bzero(&tio, sizeof(struct termios2)); |
| 159 | |
| 160 | tio.c_cflag = BOTHER; |
| 161 | tio.c_cflag |= (CLOCAL | CREAD | CS8); //8 bit no hardware handshake |
| 162 | |
| 163 | tio.c_cflag &= ~CSTOPB; //1 stop bit |
| 164 | tio.c_cflag &= ~CRTSCTS; //No CTS |
| 165 | tio.c_cflag &= ~PARENB; //No Parity |
| 166 | |
| 167 | #ifdef CNEW_RTSCTS |
| 168 | tio.c_cflag &= ~CNEW_RTSCTS; // no hw flow control |
| 169 | #endif |
| 170 | |
| 171 | tio.c_iflag &= ~(IXON | IXOFF | IXANY); // no sw flow control |
| 172 | |
| 173 | |
| 174 | tio.c_cc[VMIN] = 0; //min chars to read |
| 175 | tio.c_cc[VTIME] = 0; //time in 1/10th sec wait |
| 176 | |
| 177 | tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 178 | // raw output mode |
| 179 | tio.c_oflag &= ~OPOST; |
| 180 | |
| 181 | tio.c_ispeed = baudrate; |
| 182 | tio.c_ospeed = baudrate; |
| 183 | |
| 184 | |
| 185 | ioctl(serial_fd, TCSETS2, &tio); |
| 186 | |
| 187 | #endif |
| 188 | |
| 189 | |
| 190 | tcflush(serial_fd, TCIFLUSH); |
| 191 | |
| 192 | if (fcntl(serial_fd, F_SETFL, FNDELAY)) |
| 193 | { |
| 194 | close(); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | _is_serial_opened = true; |
| 200 | _operation_aborted = false; |
| 201 | |
| 202 | //Clear the DTR bit to let the motor spin |
| 203 | clearDTR(); |
| 204 | do { |
| 205 | // create self pipeline for wait cancellation |
| 206 | if (pipe(_selfpipe) == -1) break; |
| 207 | |
| 208 | int flags2 = fcntl(_selfpipe[0], F_GETFL); |
| 209 | if (flags2 == -1) |
| 210 | break; |
| 211 | |
| 212 | flags2 |= O_NONBLOCK; /* Make read end nonblocking */ |
| 213 | if (fcntl(_selfpipe[0], F_SETFL, flags2) == -1) |
| 214 | break; |
| 215 | |
| 216 | flags2 = fcntl(_selfpipe[1], F_GETFL); |
| 217 | if (flags2 == -1) |
| 218 | break; |
| 219 | |
| 220 | flags2 |= O_NONBLOCK; /* Make write end nonblocking */ |
| 221 | if (fcntl(_selfpipe[1], F_SETFL, flags2) == -1) |
| 222 | break; |
| 223 | |
| 224 | } while (0); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | void raw_serial::close() |
| 230 | { |
| 231 | if (serial_fd != -1) |
| 232 | ::close(serial_fd); |
| 233 | serial_fd = -1; |
| 234 | |
| 235 | if (_selfpipe[0] != -1) |
| 236 | ::close(_selfpipe[0]); |
| 237 | |
| 238 | if (_selfpipe[1] != -1) |
| 239 | ::close(_selfpipe[1]); |
| 240 | |
| 241 | _selfpipe[0] = _selfpipe[1] = -1; |
| 242 | |
| 243 | _operation_aborted = false; |
| 244 | _is_serial_opened = false; |
| 245 | } |
| 246 | |
| 247 | int raw_serial::senddata(const unsigned char * data, size_t size) |
| 248 | { |
| 249 | // FIXME: non-block io should be used |
| 250 | if (!isOpened()) return 0; |
| 251 | |
| 252 | if (data == NULL || size ==0) return 0; |
| 253 | |
| 254 | size_t tx_len = 0; |
| 255 | required_tx_cnt = 0; |
| 256 | do { |
| 257 | int ans = ::write(serial_fd, data + tx_len, size-tx_len); |
| 258 | |
| 259 | if (ans == -1) return tx_len; |
| 260 | |
| 261 | tx_len += ans; |
| 262 | required_tx_cnt = tx_len; |
| 263 | }while (tx_len<size); |
| 264 | |
| 265 | |
| 266 | return tx_len; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | int raw_serial::recvdata(unsigned char * data, size_t size) |
| 271 | { |
| 272 | if (!isOpened()) return 0; |
| 273 | |
| 274 | int ans = ::read(serial_fd, data, size); |
| 275 | |
| 276 | if (ans == -1) ans=0; |
| 277 | required_rx_cnt = ans; |
| 278 | return ans; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void raw_serial::flush( _u32 flags) |
| 283 | { |
| 284 | tcflush(serial_fd,TCIFLUSH); |
| 285 | } |
| 286 | |
| 287 | int raw_serial::waitforsent(_u32 timeout, size_t * returned_size) |
| 288 | { |
| 289 | if (returned_size) *returned_size = required_tx_cnt; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int raw_serial::waitforrecv(_u32 timeout, size_t * returned_size) |
| 294 | { |
| 295 | if (!isOpened() ) return -1; |
| 296 | |
| 297 | if (returned_size) *returned_size = required_rx_cnt; |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | int raw_serial::waitfordata(size_t data_count, _u32 timeout, size_t * returned_size) |
| 302 | { |
| 303 | size_t length = 0; |
| 304 | if (returned_size==NULL) returned_size=(size_t *)&length; |
| 305 | *returned_size = 0; |
| 306 | |
| 307 | int max_fd; |
| 308 | fd_set input_set; |
| 309 | struct timeval timeout_val; |
| 310 | |
| 311 | /* Initialize the input set */ |
| 312 | FD_ZERO(&input_set); |
| 313 | FD_SET(serial_fd, &input_set); |
| 314 | |
| 315 | if (_selfpipe[0] != -1) |
| 316 | FD_SET(_selfpipe[0], &input_set); |
| 317 | |
| 318 | max_fd = std::max<int>(serial_fd, _selfpipe[0]) + 1; |
| 319 | |
| 320 | /* Initialize the timeout structure */ |
| 321 | timeout_val.tv_sec = timeout / 1000; |
| 322 | timeout_val.tv_usec = (timeout % 1000) * 1000; |
| 323 | |
| 324 | if ( isOpened() ) |
| 325 | { |
| 326 | int nread; |
| 327 | |
| 328 | if ( ioctl(serial_fd, FIONREAD, &nread) == -1) return ANS_DEV_ERR; |
| 329 | |
| 330 | *returned_size = nread; |
| 331 | |
| 332 | if (*returned_size >= data_count) |
| 333 | { |
| 334 | return 0; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | while ( isOpened() ) |
| 339 | { |
| 340 | /* Do the select */ |
| 341 | int n = ::select(max_fd, &input_set, NULL, NULL, &timeout_val); |
| 342 | |
| 343 | if (n < 0) |
| 344 | { |
| 345 | // select error |
| 346 | *returned_size = 0; |
| 347 | return ANS_DEV_ERR; |
| 348 | } |
| 349 | else if (n == 0) |
| 350 | { |
| 351 | // time out |
| 352 | *returned_size =0; |
| 353 | return ANS_TIMEOUT; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | if (FD_ISSET(_selfpipe[0], &input_set)) { |
| 358 | // require aborting the current operation |
| 359 | int ch; |
| 360 | for (;;) { |
| 361 | if (::read(_selfpipe[0], &ch, 1) == -1) { |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | |
| 367 | // treat as timeout |
| 368 | *returned_size = 0; |
| 369 | return ANS_TIMEOUT; |
| 370 | } |
| 371 | |
| 372 | // data avaliable |
| 373 | assert (FD_ISSET(serial_fd, &input_set)); |
| 374 | |
| 375 | |
| 376 | if ( ioctl(serial_fd, FIONREAD, returned_size) == -1) return ANS_DEV_ERR; |
| 377 | if (*returned_size >= data_count) |
| 378 | { |
| 379 | return 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | } |
| 384 | |
| 385 | *returned_size=0; |
| 386 | return ANS_DEV_ERR; |
| 387 | } |
| 388 | |
| 389 | size_t raw_serial::rxqueue_count() |
| 390 | { |
| 391 | if ( !isOpened() ) return 0; |
| 392 | size_t remaining; |
| 393 | |
| 394 | if (::ioctl(serial_fd, FIONREAD, &remaining) == -1) return 0; |
| 395 | return remaining; |
| 396 | } |
| 397 | |
| 398 | void raw_serial::setDTR() |
| 399 | { |
| 400 | if ( !isOpened() ) return; |
| 401 | |
| 402 | uint32_t dtr_bit = TIOCM_DTR; |
| 403 | ioctl(serial_fd, TIOCMBIS, &dtr_bit); |
| 404 | } |
| 405 | |
| 406 | void raw_serial::clearDTR() |
| 407 | { |
| 408 | if ( !isOpened() ) return; |
| 409 | |
| 410 | uint32_t dtr_bit = TIOCM_DTR; |
| 411 | ioctl(serial_fd, TIOCMBIC, &dtr_bit); |
| 412 | } |
| 413 | |
| 414 | void raw_serial::_init() |
| 415 | { |
| 416 | serial_fd = -1; |
| 417 | _portName[0] = 0; |
| 418 | required_tx_cnt = required_rx_cnt = 0; |
| 419 | _operation_aborted = false; |
| 420 | _selfpipe[0] = _selfpipe[1] = -1; |
| 421 | } |
| 422 | |
| 423 | void raw_serial::cancelOperation() |
| 424 | { |
| 425 | _operation_aborted = true; |
| 426 | if (_selfpipe[1] == -1) return; |
| 427 | |
| 428 | int ret = ::write(_selfpipe[1], "x", 1); |
| 429 | (void)ret; |
| 430 | } |
| 431 | |
| 432 | _u32 raw_serial::getTermBaudBitmap(_u32 baud) |
| 433 | { |
| 434 | #define BAUD_CONV( _baud_) case _baud_: return B##_baud_ |
| 435 | switch (baud) { |
| 436 | BAUD_CONV(1200); |
| 437 | BAUD_CONV(1800); |
| 438 | BAUD_CONV(2400); |
| 439 | BAUD_CONV(4800); |
| 440 | BAUD_CONV(9600); |
| 441 | BAUD_CONV(19200); |
| 442 | BAUD_CONV(38400); |
| 443 | BAUD_CONV(57600); |
| 444 | BAUD_CONV(115200); |
| 445 | BAUD_CONV(230400); |
| 446 | BAUD_CONV(460800); |
| 447 | BAUD_CONV(500000); |
| 448 | BAUD_CONV(576000); |
| 449 | BAUD_CONV(921600); |
| 450 | BAUD_CONV(1000000); |
| 451 | BAUD_CONV(1152000); |
| 452 | BAUD_CONV(1500000); |
| 453 | BAUD_CONV(2000000); |
| 454 | BAUD_CONV(2500000); |
| 455 | BAUD_CONV(3000000); |
| 456 | BAUD_CONV(3500000); |
| 457 | BAUD_CONV(4000000); |
| 458 | } |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | }}} //end rp::arch::net |
| 463 | |
| 464 | //begin rp::hal |
| 465 | namespace rp{ namespace hal{ |
| 466 | |
| 467 | serial_rxtx * serial_rxtx::CreateRxTx() |
| 468 | { |
| 469 | return new rp::arch::net::raw_serial(); |
| 470 | } |
| 471 | |
| 472 | void serial_rxtx::ReleaseRxTx(serial_rxtx *rxtx) |
| 473 | { |
| 474 | delete rxtx; |
| 475 | } |
| 476 | |
| 477 | }} //end rp::hal |