[AMBClient] - nearly working
authorKevron Rees <tripzero.kev@gmail.com>
Wed, 18 Feb 2015 03:03:34 +0000 (19:03 -0800)
committerKevron Rees <tripzero.kev@gmail.com>
Thu, 19 Feb 2015 02:07:32 +0000 (18:07 -0800)
plugins/bluetooth/CMakeLists.txt
plugins/bluetooth/bluetoothplugin.cpp
plugins/bluetooth/bluetoothplugin.h
plugins/common/jsonprotocol.cpp
plugins/common/jsonprotocol.h
tests/testProtocol.cpp
tests/testProtocolClient.cpp

index af9f7c3..af7aa94 100644 (file)
@@ -22,13 +22,13 @@ include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${gio_INCLUDE_DIRS}
 
 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)
 
index 600467b..7e38c6c 100644 (file)
@@ -36,29 +36,6 @@ using namespace std;
 
 #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)
@@ -81,7 +58,19 @@ void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value)
 
 }
 
-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)
 {
 
 }
@@ -156,33 +145,11 @@ void AbstractBluetoothSerialProfile::release()
 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)
index e7d42a7..6b9674e 100644 (file)
@@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <abstractsink.h>
 #include <serialport.hpp>
+#include <jsonprotocol.h>
+
 #include <string>
 
 #include <QDBusAbstractAdaptor>
@@ -69,20 +71,15 @@ public:
 
        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
@@ -96,9 +93,13 @@ public:
 
        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);
 };
 
 
index 3ce3514..88ef3b5 100644 (file)
@@ -242,6 +242,38 @@ void amb::AmbRemoteClient::hasJsonMessage(const picojson::value &json)
                        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)
@@ -348,7 +380,13 @@ void amb::BaseJsonMessageReader::canHasData()
        while(hasJson());
 }
 
+void amb::BaseJsonMessageReader::closed()
+{
+       mIo->close();
 
+       if(disconnected)
+               disconnected();
+}
 
 bool amb::BaseJsonMessageReader::hasJson()
 {
@@ -649,7 +687,7 @@ bool amb::SetMethodCall::fromJson(const picojson::value &json)
 {
        MethodCall::fromJson(json);
 
-       value = Object::fromJson(json.get<picojson::object>());
+       value = Object::fromJson(json.get("data").get<picojson::object>());
 
        return true;
 }
index 9a15197..9bf6e3a 100644 (file)
@@ -403,19 +403,19 @@ public:
 
        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();
@@ -442,6 +442,9 @@ public:
        BaseJsonMessageReader(AbstractIo* io);
 
        void canHasData();
+       void closed();
+
+       std::function<void (void)> disconnected;
 
 protected:
 
@@ -531,7 +534,7 @@ private:
        double serverTimeOffset;
 };
 
-class AmbRemoteServer : public BaseJsonMessageReader
+class AmbRemoteServer : public BaseJsonMessageReader, public PtrMaker<AmbRemoteServer>
 {
 public:
        AmbRemoteServer(AbstractIo* io, AbstractRoutingEngine* routingEngine);
@@ -563,8 +566,6 @@ protected:
 
 protected:
        AbstractRoutingEngine* routingEngine;
-
-
 };
 
 } //namespace amb
index 2457de6..e69bb23 100644 (file)
@@ -91,6 +91,8 @@ protected:
 
                        amb::PropertyChangeEvent event;
                        event.value = interface1;
+                       event.sourceUuid = call->sourceUuid;
+                       event.zone = call->zone;
 
                        send(event);
                }
@@ -104,7 +106,7 @@ int main(int argc, char** argv)
 {
        DebugOut::setDebugThreshhold(7);
        DebugOut::setThrowErr(true);
-       DebugOut::setThrowWarn(true);
+       DebugOut::setThrowWarn(false);
 
        DebugOut(0) << "Testing AMB json server/client" << endl;
 
index 8557f5a..8ebe500 100644 (file)
@@ -19,19 +19,30 @@ void runTest(amb::AmbRemoteClient *c)
                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);
        });
 }
 
@@ -49,7 +60,11 @@ int main(int argc, char** argv)
 
        socket.open();
 
-       socket.getSocket()->waitForConnected();
+       if(!socket.getSocket()->waitForConnected())
+       {
+               DebugOut("Could not connect");
+               return -1;
+       }
 
        DebugOut(0) << "We are connected!" << endl;