*/
static const Property SteeringWheelAngle;
PROPERTYTYPE(SteeringWheelAngle, SteeringWheelAngleType, BasicPropertyType<uint16_t>, uint16_t)
- //typedef BasicPropertyType<uint16_t> SteeringWheelAngleType;
/// TODO: Rename to "SteeringWheel" in 0.13
static const Property SteeringWheelAngleW3C;
/**< 0=off, 1=right, 2=left, 3=hazard */
static const Property TurnSignal;
PROPERTYTYPEBASIC(TurnSignal, TurnSignals::TurnSignalType)
- //typedef BasicPropertyType<TurnSignals::TurnSignalType> TurnSignalType;
/**< Clutch pedal status 0=off, 1=on */
static const Property ClutchStatus;
PROPERTYTYPE(ClutchStatus, ClutchStatusType,BasicPropertyType<bool>,bool)
- //typedef BasicPropertyType<bool> ClutchStatusType;
/**< Oil pressure TODO: units */
static const Property EngineOilPressure;
- PROPERTYTYPE(EngineOilPressure, EngineOilPressureType,BasicPropertyType<uint16_t>, uint16_t)
- //typedef BasicPropertyType<uint16_t> EngineOilPressureType;
+ PROPERTYTYPE(EngineOilPressure, EngineOilPressureType, BasicPropertyType<uint16_t>, uint16_t)
/**< Engine coolant temperature in degrees celcius **/
static const Property EngineCoolantTemperature;
PROPERTYTYPE(EngineCoolantTemperature, EngineCoolantTemperatureType, BasicPropertyType<int>, int)
- //typedef BasicPropertyType<int> EngineCoolantTemperatureType;
static const Property EngineCoolantLevel;
PROPERTYTYPE(EngineCoolantLevel, EngineCoolantLevelType, BasicPropertyType<uint>, uint)
/**< 0=off, 1=on */
static const Property MachineGunTurretStatus;
PROPERTYTYPEBASIC(MachineGunTurretStatus, bool)
- //typedef BasicPropertyType<bool> MachineGunTurretStatusType;
/**< Acceleration on the 'x' axis in 1/1000 gravitational acceleration "g-force" */
static const Property AccelerationX;
PROPERTYTYPE(AccelerationX, AccelerationXType, BasicPropertyType<uint16_t>, uint16_t)
- //typedef BasicPropertyType<uint16_t> AccelerationType;
/**< Acceleration on the 'y' axis in 1/1000 gravitational acceleration "g-force" */
static const Property AccelerationY;
}
}
+bool Bluetooth5::setDevice(string address)
+{
+ mPath = findDevice(address);
+
+
+ if(mPath == "")
+ {
+ DebugOut(DebugOut::Error) << "device path not found. Not paired? " << endl;
+ return false;
+ }
+
+ return true;
+}
+
void Bluetooth5::getDeviceForAddress(std::string address, ConnectedCallback connectedCallback)
{
mConnected = connectedCallback;
- std::string devicePath = findDevice(address);
+ if(!setDevice(address))
+ return;
- DebugOut() << "Bluetooth device path: " << devicePath << endl;
+ DebugOut() << "Bluetooth device path: " << mPath << endl;
- if(devicePath == "")
+ connect(connectedCallback);
+}
+
+void Bluetooth5::connected(int fd)
+{
+ try
{
- DebugOut(DebugOut::Error) << "device path not found. Not paired? " << endl;
+ mConnected(fd);
}
+ catch(...)
+ {
+ DebugOut(DebugOut::Error) << "Error calling connected callback" << endl;
+ }
+}
+
+void Bluetooth5::connect(ConnectedCallback onconnectedCallback)
+{
+ mConnected = onconnectedCallback;
GError* error = nullptr;
- GDBusProxy * deviceProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,G_DBUS_PROXY_FLAGS_NONE,NULL,
- "org.bluez", devicePath.c_str(), "org.bluez.Device1", nullptr, &error);
+ auto deviceProxyPtr = amb::make_super(g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,G_DBUS_PROXY_FLAGS_NONE,NULL,
+ "org.bluez", mPath.c_str(), "org.bluez.Device1", nullptr, &error));
auto errorPtr = amb::make_super(error);
return;
}
- g_dbus_proxy_call(deviceProxy, "Connect", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr,
+ g_dbus_proxy_call(deviceProxyPtr.get(), "Connect", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr,
[](GObject *source_object, GAsyncResult *res, gpointer user_data)
{
GError* error = nullptr;
- g_dbus_proxy_call_finish(G_DBUS_PROXY (source_object),res, &error);
+ g_dbus_proxy_call_finish(G_DBUS_PROXY (source_object), res, &error);
auto errorPtr = amb::make_super(error);
this);
}
-void Bluetooth5::connected(int fd)
+void Bluetooth5::disconnect()
{
- //try
- {
- mConnected(fd);
- }
- //catch(...)
+ GError* error = nullptr;
+
+ auto deviceProxyPtr = amb::make_super(g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,G_DBUS_PROXY_FLAGS_NONE,NULL,
+ "org.bluez", mPath.c_str(), "org.bluez.Device1", nullptr, &error));
+
+ auto errorPtr = amb::make_super(error);
+
+ if(errorPtr)
{
- //DebugOut(DebugOut::Error) << "Error calling connected callback" << endl;
+ DebugOut(DebugOut::Error) << "Error getting bluetooth device proxy " << errorPtr->message <<endl;
+ return;
}
+
+ g_dbus_proxy_call(deviceProxyPtr.get(), "Disconnect", nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr,[](GObject *source_object,
+ GAsyncResult *res, gpointer user_data){
+ GError* error = nullptr;
+
+ g_dbus_proxy_call_finish(G_DBUS_PROXY (source_object), res, &error);
+
+ auto errorPtr = amb::make_super(error);
+
+ if(errorPtr)
+ {
+ DebugOut(DebugOut::Error) << "error trying to disconnect: " << errorPtr->message << endl;
+ }
+ }, nullptr);
}
#include "debugout.h"
#include "bluetooth.hpp"
#include "timestamp.h"
+#include "bluetooth5.h"
+#include "superptr.hpp"
+
+#ifdef USE_BLUEZ5
+Bluetooth5 bt;
+#endif
#define __SMALLFILE__ std::string(__FILE__).substr(std::string(__FILE__).rfind("/")+1)
AbstractRoutingEngine *m_re;
bool sendElmCommand(obdLib *obd,std::string command)
{
- std::vector<unsigned char> replyVector;
+ std::vector<unsigned char> replyVector;
std::string reply;
obd->sendObdRequestString(command.append("\r").c_str(),command.length()+1,&replyVector,10,3);
for (unsigned int i=0;i<replyVector.size();i++)
if(fd != -1)
{
- if(obd->openPort(fd,baud) == -1)
+ if(obd->openPort(fd, baud) == -1)
return false;
}
else
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Error turning off linefeeds"<<endl;
return false;
}
- obd->sendObdRequestString("010C1\r",6,&replyVector,500,5);
+ obd->sendObdRequestString("010C1\r", 6, &replyVector, 500, 5);
return true;
}
GAsyncQueue *privSubscriptionAddQueue = g_async_queue_ref(((OBD2Source*)data)->subscriptionAddQueue);
GAsyncQueue *privSubscriptionRemoveQueue = g_async_queue_ref(((OBD2Source*)data)->subscriptionRemoveQueue);
GAsyncQueue *privStatusQueue = g_async_queue_ref(((OBD2Source*)data)->statusQueue);
-
+
obdLib *obd = new obdLib();
OBD2Source *source = (OBD2Source*)data;
obd->setCommsCallback([](const char* mssg, void* data) { DebugOut(6)<<mssg<<endl; },NULL);
obd->setDebugCallback([](const char* mssg, void* data, obdLib::DebugLevel debugLevel) { DebugOut(debugLevel)<<mssg<<endl; },NULL);
-
+
std::list<ObdPid*> reqList;
std::list<ObdPid*> repeatReqList;
ObdPid::ByteArray replyVector;
- std::string reply;
std::string port;
std::string baud;
bool connected=false;
int timeoutCount = 0;
while (source->m_threadLive)
{
- //gpointer query = g_async_queue_pop(privCommandQueue);
-
-
gpointer query = g_async_queue_try_pop(privSingleShotQueue);
if (query != nullptr)
{
//printf("Got request!\n");
-
+
ObdPid *req = (ObdPid*)query;
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Got single shot request: " << req->pid.substr(0,req->pid.length()-1) << ":" << req->property <<endl;
repeatReqList.push_back(req);
//commandMap[req->req] = req->arg;
//printf("Command: %s\n",req->req.c_str());
DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Command:" << req->req << endl;
- if (req->req == "connect" )
- {
-
- if (source->m_isBluetooth)
- {
-
- BluetoothDevice bt;
- std::string tempPort = bt.getDeviceForAddress(source->m_btDeviceAddress, source->m_btAdapterAddress);
- if(tempPort != "")
- {
- DebugOut(3)<<"Using bluetooth device \""<<source->m_btDeviceAddress<<"\" bound to: "<<tempPort<<endl;
- port = tempPort;
- }
- }
- else
- {
- port = req->arglist[0];
- baud = req->arglist[1];
- }
- connected = connect(obd,port,baud);
-
- if(connected)
- {
- StatusMessage *statusreq = new StatusMessage();
- statusreq->statusStr = "connected";
- g_async_queue_push(privStatusQueue,statusreq);
- }
- else
- {
- StatusMessage *statusreq = new StatusMessage();
- statusreq->statusStr = "disconnected";
- g_async_queue_push(privStatusQueue,statusreq);
- }
-
- }
- else if (req->req == "connectifnot")
+ if (req->req == "connectifnot")
{
if (!connected)
{
if (source->m_isBluetooth)
{
+#ifdef USE_BLUEZ5
+ bt.getDeviceForAddress(source->m_btDeviceAddress, [&obd, baud, &privStatusQueue](int fd)
+ {
+ bool connected = connect(obd, "", baud, fd);
+
+ if(connected)
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "connected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
+ else
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "disconnected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
+ });
+#else
BluetoothDevice bt;
std::string tempPort = bt.getDeviceForAddress(source->m_btDeviceAddress, source->m_btAdapterAddress);
if(tempPort != "")
{
- DebugOut(3)<<"Using bluetooth device \""<<source->m_btDeviceAddress<<"\" bound to: "<<tempPort<<endl;
+ DebugOut(3) << "Using bluetooth device \"" << source->m_btDeviceAddress << "\" bound to: " << tempPort << endl;
port = tempPort;
}
else
{
- DebugOut(DebugOut::Error)<<"Error creating bluetooth device"<<endl;
+ DebugOut(DebugOut::Error) << "Error creating bluetooth device" << endl;
continue;
}
- }
- connected = connect(obd,port,baud);
+ connected = connect(obd, port, baud);
- if(connected)
- {
- StatusMessage *statusreq = new StatusMessage();
- statusreq->statusStr = "connected";
- g_async_queue_push(privStatusQueue,statusreq);
+ if(connected)
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "connected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
+ else
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "disconnected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
+
+#endif
}
else
{
- StatusMessage *statusreq = new StatusMessage();
- statusreq->statusStr = "disconnected";
- g_async_queue_push(privStatusQueue,statusreq);
+
+ connected = connect(obd, port, baud);
+
+ if(connected)
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "connected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
+ else
+ {
+ StatusMessage *statusreq = new StatusMessage();
+ statusreq->statusStr = "disconnected";
+ g_async_queue_push(privStatusQueue, statusreq);
+ }
}
}
}
{
DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Using queued disconnect" << (ulong)req << endl;
obd->closePort();
+#ifdef USE_BLUEZ5
+ bt.disconnect();
+#else
BluetoothDevice bt;
bt.disconnect(source->m_btDeviceAddress, source->m_btAdapterAddress);
+#endif
connected = false;
StatusMessage *statusreq = new StatusMessage();
statusreq->statusStr = "disconnected";
int count = (*source->m_blacklistPidCountMap.find((*i)->pid)).second;
if (count > 10)
{
-
+
}
source->m_blacklistPidCountMap.erase(source->m_blacklistPidCountMap.find((*i)->pid));
source->m_blacklistPidCountMap.insert(pair<std::string,int>((*i)->pid,count));
{
DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "OBD Other error:" << obd->lastError() << endl;
}
-
+
CommandRequest *req = new CommandRequest();
DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Queuing up a disconnect" << (ulong)req << endl;
req->req = "disconnect";
//usleep(10000);
}
repeatReqList.clear();
-
+
}
if (connected)
{
{
OBD2Source* src = (OBD2Source*)data;
-
+
while (gpointer retval = g_async_queue_try_pop(src->statusQueue))
{
StatusMessage *reply = (StatusMessage*)retval;
if (reply->statusStr == "disconnected")
{
OBD2Source::Obd2ConnectType val(Obd2Connected,false);
- src->updateProperty(Obd2Connected,&val);
+ src->updateProperty(&val);
}
else if (reply->statusStr == "connected")
{
OBD2Source::Obd2ConnectType val(Obd2Connected, true);
- src->updateProperty(Obd2Connected,&val);
+ val.priority = OBD2Source::Obd2ConnectType::Instant;
+ src->updateProperty(&val);
}
else if (reply->statusStr == "error:nodata" || reply->statusStr == "error:timeout")
{
{
ObdPid *reply = (ObdPid*)retval;
-
- AbstractPropertyType* value = VehicleProperty::getPropertyTypeForPropertyNameValue(reply->property, reply->value);
- src->updateProperty(reply->property, value);
- delete value;
+ auto value = amb::make_unique(VehicleProperty::getPropertyTypeForPropertyNameValue(reply->property, reply->value));
+ value->priority = AbstractPropertyType::Instant;
+ src->updateProperty(value.get());
}
return true;
}
-void OBD2Source::updateProperty(VehicleProperty::Property property,AbstractPropertyType* value)
+void OBD2Source::updateProperty(AbstractPropertyType* value)
{
+ VehicleProperty::Property property = value->name;
if(property == Obd2Connected)
obd2Connected.setValue(value->anyValue());
-
+
if (propertyReplyMap.find(property) != propertyReplyMap.end())
{
propertyReplyMap[property]->value = value;
{
// //Config has been passed, let's start stuff up.
configuration = config;
-
+
//Default values
std::string port = "/dev/ttyUSB0";
std::string baud = "115200";
std::string btadapter = "";
m_isBluetooth = false;
-
+
//Try to load config
//printf("OBD2Source::setConfiguration\n");
for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
req->arglist.push_back(port);
req->arglist.push_back(baud);
g_async_queue_push(commandQueue,req);
-
+
m_port = port;
m_baud = baud;
m_gThread = g_thread_new("mythread",(GThreadFunc)&threadLoop,this);
}
clientConnected = false;
- m_re = re;
+ m_re = re;
m_threadLive = true;
Obd2Amb obd2amb;
extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
{
return new OBD2Source(routingengine, config);
-
+
}
const string OBD2Source::uuid()
{
if(!pid)
{
+ DebugOut(DebugOut::Warning) << "We don't support this property: " << property <<endl;
return;
}
-
+
//If the pid is currently in the blacklist map, erase it. This allows for applications
//to "un-blacklist" a pid by re-subscribing to it.
if (m_blacklistPidCountMap.find(pid->pid) != m_blacklistPidCountMap.end())
{
m_blacklistPidCountMap.erase(m_blacklistPidCountMap.find(pid->pid));
}
-
-
- g_async_queue_push(subscriptionAddQueue,pid);
+
+
+ g_async_queue_push(subscriptionAddQueue, pid);
CommandRequest *req = new CommandRequest();
req->req = "connectifnot";
g_async_queue_push(commandQueue,req);