summaryrefslogtreecommitdiff
path: root/plugins/obd2plugin/obd2source.h
blob: ba8cc2bf61760f819224f5e8b116cd3d9d1a0aec (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright (C) 2012 Intel Corporation

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 OBD2SOURCE_H
#define OBD2SOURCE_H



#include <abstractsource.h>
#include <string>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include "obdlib.h"
#include <glib.h>

#include "obdpid.h"

class ObdRequest
{
public:
  VehicleProperty::Property property;
  std::string req;
  std::string arg;
};


class CommandRequest
{
public:
  std::string req;
  std::vector<std::string> arglist;
};
class StatusMessage
{
public:
  std::string statusStr;
  VehicleProperty::Property property;
};
class ObdReply
{
public:
  VehicleProperty::Property property;
  std::string req;
  std::string reply;
};



class Obd2Amb
{
public:

	typedef function<std::string (std::string)> ConversionFunction;

	typedef std::vector<unsigned char> ByteArray;
	Obd2Amb()
	{
		supportedPidsList.push_back(new VehicleSpeedPid());
		supportedPidsList.push_back(new EngineSpeedPid());
		supportedPidsList.push_back(new MassAirFlowPid());
		supportedPidsList.push_back(new VinPid());
		supportedPidsList.push_back(new WmiPid());
		supportedPidsList.push_back(new FuelConsumptionPid());
		supportedPidsList.push_back(new EngineCoolantPid());
		supportedPidsList.push_back(new AirIntakeTemperaturePid());
		supportedPidsList.push_back(new EngineLoadPid());
		supportedPidsList.push_back(new ThrottlePositionPid());
		supportedPidsList.push_back(new BatteryVoltagePid());
	}

	~Obd2Amb()
	{
		for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
		{
			delete *itr;
		}
	}

	ObdPid* createPidFromReply(ByteArray replyVector)
	{
		ByteArray replyVectorClean = ObdPid::cleanup(replyVector);
		ByteArray replyVectorCompressed = ObdPid::compress(replyVectorClean);

		for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
		{
			ObdPid* p = *itr;

			if(p->needsCompress())
			{
					replyVector = replyVectorCompressed;
			}
			else replyVector = replyVectorClean;

			if (!p->tryParse(replyVector))
			{
				continue;
			}

			ObdPid* pid = (*itr)->create();
			return pid;
		}
		return 0;
	}
	ObdPid* createPidforProperty(VehicleProperty::Property property)
	{
		for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
		{
			VehicleProperty::Property p = (*itr)->property;
			if(p == property)
			{
				ObdPid* obj = *itr;
				return obj->create();
			}
		}
		return NULL;
	}

	std::list<ObdPid*> supportedPidsList;
};

class OBD2Source : public AbstractSource
{

public:
	OBD2Source(AbstractRoutingEngine* re, map<string, string> config);
	~OBD2Source();
	const string uuid();
	int portHandle;
	void getPropertyAsync(AsyncPropertyReply *reply);
	void getRangePropertyAsync(AsyncRangePropertyReply *reply){}
	AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
	void subscribeToPropertyChanges(VehicleProperty::Property property);
	void unsubscribeToPropertyChanges(VehicleProperty::Property property);
	PropertyList supported();

	int supportedOperations();

	PropertyList queuedRequests;
	bool clientConnected;
	PropertyList activeRequests;
	void engineSpeed(double speed);
	void vehicleSpeed(int speed);
	void mafValue(double maf);
	void engineCoolantTemp(int temp);
	PropertyList removeRequests;
	void setSupported(PropertyList list);

	void supportedChanged(const PropertyList &) {}
	GAsyncQueue* commandQueue;
	GAsyncQueue* statusQueue;
	GAsyncQueue* subscriptionAddQueue;
	GAsyncQueue* subscriptionRemoveQueue;
	GAsyncQueue* singleShotQueue;
	GAsyncQueue* responseQueue;
	std::list<std::string> m_blacklistPidList;
	std::map<std::string,int> m_blacklistPidCountMap;
	void setConfiguration(map<string, string> config);
	//void randomizeProperties();
	std::string m_port;
	std::string m_baud;
	bool m_isBluetooth;
	std::string m_btDeviceAddress;
	std::string m_btAdapterAddress;
	std::vector<AsyncPropertyReply*> propertyReplyList;
	void updateProperty(AbstractPropertyType *value);
	obdLib * obd;
	bool m_threadLive;
	GThread *m_gThread;

	typedef BasicPropertyType<bool> Obd2ConnectType;
	Obd2ConnectType obd2Connected;

private:
	PropertyList m_supportedProperties;
	std::map<VehicleProperty::Property, AbstractPropertyType*> oldValueMap;
	GMutex *threadQueueMutex;


};

#endif // OBD2SOURCE_H