automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
obdlib.h
1 /*
2 Copyright (C) 2010 by Michael Carpenter (malcom2073) malcom2073@gmail.com
3 
4 This file is a part of libobd
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10 
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 
22 #ifndef OBDLIB_H
23 #define OBDLIB_H
24 #include <stdio.h>
25 
26 #include <string>
27 //#define WINHACK
28 
29 #ifdef WINHACK
30 #define M_SleepSec(x) Sleep(1000 * x);
31 #include <windows.h>
32 //#ifdef WIN32
33 //#define STDCALL __sttribute__((__stdcall__))
34 //#define STDCALL __stdcall
35 #define STDCALL __declspec(dllexport)
36 #else
37 #define M_SleepSec(x) usleep(x * 1000000);
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <termios.h>
42 #define STDCALL
43 # define byte unsigned char
44 # define HANDLE int
45 # define DWORD long
46 
47 #endif // WIN32
48 #include <vector>
49 
50 
51 
53 {
54  int intVar;
55  double doubleVarOne;
56  double doubleVarTwo;
57  byte byteVar;
58  std::vector<byte> vectorBytes;
59  byte typeVar;
60  byte btMode;
61  byte btPID;
62 };
64 {
65  DWORD dwItemType;
66  std::string strItemDesc;
67  byte btMode;
68  byte btPID;
69  byte btItemSizeBytes;
70  DWORD dwOperation;
71  double dFactor;
72  double dOffset;
73  int iRangeLow;
74  int iRangeHigh;
75  std::string strUnitLabel;
76  int iBitLookupTable;
77 };
78 
79 class obdLib
80 {
81 public:
82 
83  enum ObdError
84  {
85  NODATA=0,
86  NOTCONNECTED,
87  OTHER,
88  SERIALWRITEERROR,
89  SERIALREADERROR,
90  TIMEOUT,
91  NONE
92  };
93  enum DebugLevel
94  {
95  DEBUG_VERY_VERBOSE=0,
96  DEBUG_VERBOSE=1,
97  DEBUG_INFO=2,
98  DEBUG_WARN=3,
99  DEBUG_ERROR=4,
100  DEBUG_FATAL=5
101  };
102  obdLib();
103  int openPort(const char *portName,int baudrate);
104  int openPort(const int fd, int baudrate);
105  int openPort(const char *portName);
106  void setPortHandle(HANDLE hdnl);
107  int initPort();
108  int closePort();
109  bool connected();
110  void flush();
111  void setDebugCallback(void (*callbackptr)(const char*,void*,obdLib::DebugLevel),void *);
112  void setCommsCallback(void (*callbackptr)(const char*,void*),void*);
113  std::string monitorModeReadLine();
114  //byte* sendRequest(byte *reqString,int length);
115 // byte* sendRequest(char *asciiReqString, int length);
116  static byte byteArrayToByte(byte b1, byte b2);
117  std::string getVersion() { return versionString; }
118  bool sendObdRequest(const char *req,int len,std::vector<byte> *reply);
119  bool sendObdRequest(const char *req,int length,std::vector<byte> *reply,int sleep, int timeout);
120  bool sendObdRequestString(const char *req,int len,std::vector<byte> *reply,int sleeptime);
121  bool sendObdRequestString(const char *req,int length,std::vector<byte> *reply,int sleeptime, int timeout);
122  bool sendObdRequestString(const char *req,int len,std::vector<byte> *reply);
123  bool sendObdRequest(const char *req,int len);
124  bool sendObdRequest(const char *req,int len,int timeout);
125  ObdError lastError();
126 
127 
128 private:
129  void debug(DebugLevel lvl,const char* msg,...);
130  void commsDebug(const char *msg);
131  ObdError m_lastError;
132  std::vector<std::vector<int> > *modeByteCount;
133  std::string versionString;
134  HANDLE portHandle;
135  byte* readBytes(int* bytesRead,int numBytesToRead);
136  void writeBytes(byte *sendBuffer,int bufferSize,int *bytesWritten);
137  byte *sendBuffer;
138  byte *rcvdBuffer;
139 };
140 #endif //OBDLIB_H
141