SLCANInterface.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2015, 2016 Hubert Denkmair
  3. This file is part of cangaroo.
  4. cangaroo is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. cangaroo is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with cangaroo. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include "../CanInterface.h"
  17. #include <core/MeasurementInterface.h>
  18. #include <QtSerialPort/QSerialPort>
  19. #include <QtSerialPort/QSerialPortInfo>
  20. #include <QMutex>
  21. // Maximum rx buffer len
  22. #define SLCAN_MTU 138 + 1 + 16 // canfd 64 frame plus \r plus some padding
  23. #define SLCAN_STD_ID_LEN 3
  24. #define SLCAN_EXT_ID_LEN 8
  25. #define RXCIRBUF_LEN 8192 // Buffer for received serial data, serviced at 1ms intervals
  26. class SLCANDriver;
  27. typedef struct {
  28. bool supports_canfd;
  29. bool supports_timing;
  30. uint32_t state;
  31. uint32_t base_freq;
  32. uint32_t sample_point;
  33. uint32_t ctrl_mode;
  34. uint32_t restart_ms;
  35. } can_config_t;
  36. typedef struct {
  37. uint32_t can_state;
  38. uint64_t rx_count;
  39. int rx_errors;
  40. uint64_t rx_overruns;
  41. uint64_t tx_count;
  42. int tx_errors;
  43. uint64_t tx_dropped;
  44. } can_status_t;
  45. class SLCANInterface: public CanInterface {
  46. public:
  47. SLCANInterface(SLCANDriver *driver, int index, QString name, bool fd_support);
  48. virtual ~SLCANInterface();
  49. QString getDetailsStr() const;
  50. virtual QString getName() const;
  51. void setName(QString name);
  52. virtual QList<CanTiming> getAvailableBitrates();
  53. virtual void applyConfig(const MeasurementInterface &mi);
  54. virtual bool readConfig();
  55. virtual bool readConfigFromLink(struct rtnl_link *link);
  56. bool supportsTimingConfiguration();
  57. bool supportsCanFD();
  58. bool supportsTripleSampling();
  59. virtual unsigned getBitrate();
  60. virtual uint32_t getCapabilities();
  61. virtual void open();
  62. virtual void close();
  63. virtual bool isOpen();
  64. virtual void sendMessage(const CanMessage &msg);
  65. virtual bool readMessage(CanMessage &msg, unsigned int timeout_ms);
  66. virtual bool updateStatistics();
  67. virtual uint32_t getState();
  68. virtual int getNumRxFrames();
  69. virtual int getNumRxErrors();
  70. virtual int getNumRxOverruns();
  71. virtual int getNumTxFrames();
  72. virtual int getNumTxErrors();
  73. virtual int getNumTxDropped();
  74. int getIfIndex();
  75. private:
  76. typedef enum {
  77. ts_mode_SIOCSHWTSTAMP,
  78. ts_mode_SIOCGSTAMPNS,
  79. ts_mode_SIOCGSTAMP
  80. } ts_mode_t;
  81. int _idx;
  82. bool _isOpen;
  83. QSerialPort* _serport;
  84. QStringList _msg_queue;
  85. QMutex _serport_mutex;
  86. QString _name;
  87. char _rx_linbuf[SLCAN_MTU];
  88. int _rx_linbuf_ctr;
  89. char _rxbuf[RXCIRBUF_LEN];
  90. uint32_t _rxbuf_head;
  91. uint32_t _rxbuf_tail;
  92. QMutex _rxbuf_mutex;
  93. MeasurementInterface _settings;
  94. can_config_t _config;
  95. can_status_t _status;
  96. ts_mode_t _ts_mode;
  97. bool updateStatus();
  98. bool parseMessage(CanMessage &msg);
  99. };