add_library(bluetoothplugin MODULE ${bluetooth_sources})
set_target_properties(bluetoothplugin PROPERTIES PREFIX "")
-target_link_libraries(bluetoothplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb-plugins-common -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${link_libraries} ${gio_LIBRARIES} ${QT_LIBRARIES})
+target_link_libraries(bluetoothplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb-plugins-common amb-json-protocol -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${link_libraries} ${gio_LIBRARIES} ${QT_LIBRARIES})
install(TARGETS bluetoothplugin LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ambbt.conf DESTINATION /etc/dbus-1/system.d )
add_executable(testAmbBt testAmbBt.cpp bluetoothplugin.cpp)
-target_link_libraries(testAmbBt ${link_libraries} amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb-plugins-common -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${QT_LIBRARIES})
+target_link_libraries(testAmbBt ${link_libraries} amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb-plugins-common amb-json-protocol -L${CMAKE_CURRENT_BINARY_DIR}/plugins/common ${QT_LIBRARIES})
install (TARGETS testAmbBt RUNTIME DESTINATION bin)
#include "debugout.h"
-bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data)
-{
-// DebugOut(5) << "Polling..." << condition << endl;
-
- if(condition & G_IO_ERR)
- {
- DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<<endl;
- }
-
- if (condition & G_IO_HUP)
- {
- //Hang up. Returning false closes out the GIOChannel.
- //printf("Callback on G_IO_HUP\n");
- DebugOut(DebugOut::Warning)<<"socket hangup event..."<<endl;
- return false;
- }
-
- AbstractBluetoothSerialProfile* p = static_cast<AbstractBluetoothSerialProfile*>(data);
-
- p->canHasData();
-
- return true;
-}
BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map<string, string> config)
:AbstractSink(re, config)
}
-void BluetoothSinkPlugin::dataReceived(QByteArray data)
+void BluetoothSinkPlugin::newConnection(string, QDBusUnixFileDescriptor fd, QVariantMap)
+{
+ SerialPort *bluetoothDev = new SerialPort();
+ bluetoothDev->setDescriptor(fd.fileDescriptor());
+ auto client = amb::make_shared(new amb::AmbRemoteServer(bluetoothDev, routingEngine));
+ client->disconnected = [this, client]() {
+ removeOne(&clients, client);
+ };
+
+ clients.push_back(client);
+}
+
+void BluetoothSinkPlugin::requestDisconnection(string path)
{
}
void AbstractBluetoothSerialProfile::newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props)
{
DebugOut()<<"new Connection! Path: "<<path<<" fd: "<<fd.fileDescriptor()<<endl;
-
- socket.setDescriptor(fd.fileDescriptor());
-
- GIOChannel *chan = g_io_channel_unix_new(socket.fileDescriptor());
- g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, this);
- g_io_channel_set_close_on_unref(chan, true);
- g_io_channel_unref(chan);
}
void AbstractBluetoothSerialProfile::requestDisconnection(string path)
{
DebugOut()<<"requestDisconnection called. Path: "<<path<<endl;
- socket.close();
-}
-
-void AbstractBluetoothSerialProfile::canHasData()
-{
- QByteArray data = socket.read().c_str();
-
- DebugOut()<<"data read: "<<data.constData()<<endl;
-
- dataReceived(data);
-}
-
-void AbstractBluetoothSerialProfile::write(const std::string & data)
-{
- socket.write(data);
}
BtProfileAdaptor::BtProfileAdaptor(AbstractBluetoothSerialProfile *parent)
#include <abstractsink.h>
#include <serialport.hpp>
+#include <jsonprotocol.h>
+
#include <string>
#include <QDBusAbstractAdaptor>
virtual void requestDisconnection(std::string path);
- virtual void canHasData();
-
- void write(const string & data);
-
protected:
virtual void connected() {}
virtual void disconnected() {}
- virtual void dataReceived(QByteArray data) {}
- SerialPort socket;
private:
QString role;
};
+
class BluetoothSinkPlugin: public AbstractBluetoothSerialProfile, public AbstractSink
{
Q_OBJECT
void propertyChanged(AbstractPropertyType* value);
-protected:
- virtual void dataReceived(QByteArray data);
+private:
+ std::vector<amb::AmbRemoteServer::Ptr> clients;
+ // AbstractBluetoothSerialProfile interface
+public:
+ void newConnection(string path, QDBusUnixFileDescriptor fd, QVariantMap props);
+ void requestDisconnection(string path);
};
DebugOut(DebugOut::Warning) << "Time Sync request failed" << endl;
}
}
+ else if(BaseMessage::is<EventMessage>(json))
+ {
+ if(PropertyChangeEvent::is(json))
+ {
+ DebugOut(7) << "property changed event" << endl;
+
+ PropertyChangeEvent::Ptr obj = PropertyChangeEvent::create();
+ if(!obj->fromJson(json))
+ return;
+
+ std::string subscribeId = createSubscriptionId(obj->value->interfaceName, obj->sourceUuid, obj->zone);
+
+ if(!amb::containsKey(mSubscriptions, subscribeId))
+ {
+ DebugOut(DebugOut::Warning) << "We haven't subscribed to this interface at this zone from this source..." << endl;
+ return;
+ }
+
+ auto list = mSubscriptions[subscribeId];
+
+ for(auto i : list)
+ {
+ i.callback(obj->value);
+ }
+ }
+ }
+ else
+ {
+ BaseMessage msg;
+ msg.fromJson(json);
+ DebugOut(DebugOut::Warning) << "Unhandled message: " << msg.name << " type: " << msg.type << endl;
+ }
}
string amb::AmbRemoteClient::createSubscriptionId(const string & objectName, const string & sourceUuid, Zone::Type zone)
while(hasJson());
}
+void amb::BaseJsonMessageReader::closed()
+{
+ mIo->close();
+ if(disconnected)
+ disconnected();
+}
bool amb::BaseJsonMessageReader::hasJson()
{
{
MethodCall::fromJson(json);
- value = Object::fromJson(json.get<picojson::object>());
+ value = Object::fromJson(json.get("data").get<picojson::object>());
return true;
}
static bool is(const BaseMessage & msg)
{
- return msg.type == "timeSync" && msg.name == "message";
+ return msg.type == "message" && msg.name == "timeSync";
}
static bool is(const picojson::value &json)
{
- return json.contains("serverTime") && json.get("type").to_str() == "timeSync" && json.get("serverTime").is<double>();
+ return json.contains("serverTime") && json.get("name").to_str() == "timeSync" && json.get("serverTime").is<double>();
}
};
class PropertyChangeEvent: public EventMessage, public PtrMaker<PropertyChangeEvent>
{
public:
- PropertyChangeEvent() : EventMessage("propertyChanged") {}
+ PropertyChangeEvent() : EventMessage("propertyChanged"), zone(Zone::None) {}
picojson::value toJson();
BaseJsonMessageReader(AbstractIo* io);
void canHasData();
+ void closed();
+
+ std::function<void (void)> disconnected;
protected:
double serverTimeOffset;
};
-class AmbRemoteServer : public BaseJsonMessageReader
+class AmbRemoteServer : public BaseJsonMessageReader, public PtrMaker<AmbRemoteServer>
{
public:
AmbRemoteServer(AbstractIo* io, AbstractRoutingEngine* routingEngine);
protected:
AbstractRoutingEngine* routingEngine;
-
-
};
} //namespace amb
amb::PropertyChangeEvent event;
event.value = interface1;
+ event.sourceUuid = call->sourceUuid;
+ event.zone = call->zone;
send(event);
}
{
DebugOut::setDebugThreshhold(7);
DebugOut::setThrowErr(true);
- DebugOut::setThrowWarn(true);
+ DebugOut::setThrowWarn(false);
DebugOut(0) << "Testing AMB json server/client" << endl;
g_assert(supported.size() == 2);
});
+ c->subscribe("interface1", [](amb::Object::Ptr obj)
+ {
+ DebugOut(0) << obj->interfaceName << " changed!" << endl;
+ });
+
DebugOut(0) << "calling client->get()" << endl;
- c->get("interface1", [&c](amb::Object::Ptr obj)
+ c->get("interface1", [](amb::Object::Ptr obj)
{
DebugOut(0) << "get call reply" << endl;
g_assert(obj->size() == 2);
obj->emplace("vehicleSpeed", amb::make_shared(new VehicleProperty::VehicleSpeedType(69)));
- c->set("interface1", obj, [](bool s)
- {
- DebugOut(0) << "set call reply status: " << (s ? "success!" : "fail") << endl;
- g_assert(s);
- });
+ });
+
+ amb::Object::Ptr obj = amb::Object::create();
+
+ obj->interfaceName = "interface1";
+ obj->emplace("vehicleSpeed", amb::make_shared(new VehicleProperty::VehicleSpeedType(22)));
+
+ c->set("interface1", obj, [](bool s)
+ {
+ DebugOut(0) << "set call reply status: " << (s ? "success!" : "fail") << endl;
+ g_assert(s);
});
}
socket.open();
- socket.getSocket()->waitForConnected();
+ if(!socket.getSocket()->waitForConnected())
+ {
+ DebugOut("Could not connect");
+ return -1;
+ }
DebugOut(0) << "We are connected!" << endl;