summaryrefslogtreecommitdiff
path: root/plugins/obd2plugin/obdlib.h
blob: 5c3cc2b7c9e51d4b3a8e7a75ff755c9e6820d764 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright (C) 2010 by Michael Carpenter (malcom2073) malcom2073@gmail.com

This file is a part of libobd

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef OBDLIB_H
#define OBDLIB_H
#include <stdio.h>

#include <string>
//#define WINHACK

#ifdef WINHACK
#define M_SleepSec(x) Sleep(1000 * x);
#include <windows.h>
//#ifdef WIN32
//#define STDCALL __sttribute__((__stdcall__))
//#define STDCALL __stdcall
#define STDCALL  __declspec(dllexport) 
#else
#define M_SleepSec(x) usleep(x * 1000000);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#define STDCALL
#	define byte unsigned char
#	define HANDLE int
#	define DWORD long

#endif	// WIN32
#include <vector>



struct variantStruct
{
	int intVar;
	double doubleVarOne;
	double doubleVarTwo;
	byte byteVar;
	std::vector<byte> vectorBytes;
	byte typeVar;
	byte btMode;
	byte btPID;
};
struct obdInfoStruct
{
	DWORD dwItemType;
	std::string strItemDesc;
	byte btMode;
	byte btPID;
	byte btItemSizeBytes;
	DWORD dwOperation;
	double dFactor;
	double dOffset;
	int iRangeLow;
	int iRangeHigh;
	std::string strUnitLabel;
	int iBitLookupTable;
};

class obdLib
{
public:

	enum ObdError
	{
		NODATA=0,
		NOTCONNECTED,
		OTHER,
		SERIALWRITEERROR,
		SERIALREADERROR,
		TIMEOUT,
		NONE
	};
	enum DebugLevel
	{
		DEBUG_VERY_VERBOSE=0,
		DEBUG_VERBOSE=1,
		DEBUG_INFO=2,
		DEBUG_WARN=3,
		DEBUG_ERROR=4,
		DEBUG_FATAL=5
	};
	obdLib();
	int openPort(const char *portName,int baudrate);
	int openPort(const char *portName);
	void setPortHandle(HANDLE hdnl);
	int initPort();
	int closePort();
	bool connected();
	void flush();
	void setDebugCallback(void (*callbackptr)(const char*,void*,obdLib::DebugLevel),void *);
	void setCommsCallback(void (*callbackptr)(const char*,void*),void*);
	std::string monitorModeReadLine();
	//byte* sendRequest(byte *reqString,int length);
//	byte* sendRequest(char *asciiReqString, int length);
	static byte byteArrayToByte(byte b1, byte b2);
	std::string getVersion() { return versionString; }
	bool sendObdRequest(const char *req,int len,std::vector<byte> *reply);
	bool sendObdRequest(const char *req,int length,std::vector<byte> *reply,int sleep, int timeout);
	bool sendObdRequestString(const char *req,int len,std::vector<byte> *reply,int sleeptime);
	bool sendObdRequestString(const char *req,int length,std::vector<byte> *reply,int sleeptime, int timeout);
	bool sendObdRequestString(const char *req,int len,std::vector<byte> *reply);
	bool sendObdRequest(const char *req,int len);
	bool sendObdRequest(const char *req,int len,int timeout);
	ObdError lastError();


private:
	void debug(DebugLevel lvl,const char* msg,...);
	void commsDebug(const char *msg);
	ObdError m_lastError;
	std::vector<std::vector<int> > *modeByteCount;
	std::string versionString;
	HANDLE portHandle;
	byte* readBytes(int* bytesRead,int numBytesToRead);
	void writeBytes(byte *sendBuffer,int bufferSize,int *bytesWritten);
	byte *sendBuffer;
	byte *rcvdBuffer;
};
#endif //OBDLIB_H