From: Kevron Rees Date: Fri, 5 Sep 2014 16:05:25 +0000 (-0700) Subject: fixed up bluez5 support code X-Git-Tag: 0.12.903~121 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6564310e2b453df05beff843973f78179ec2c8c3;p=profile%2Fivi%2Fautomotive-message-broker.git fixed up bluez5 support code --- diff --git a/lib/superptr.hpp b/lib/superptr.hpp index 53de9ba..26e9087 100644 --- a/lib/superptr.hpp +++ b/lib/superptr.hpp @@ -67,8 +67,8 @@ template super_ptr make_super(T* t) { return super_ptr(t); } -template unique_ptr make_unique(T* t) { - return unique_ptr(t); +template ::std::unique_ptr make_unique(T* t) { + return ::std::unique_ptr(t); } } diff --git a/plugins/bluetooth/CMakeLists.txt b/plugins/bluetooth/CMakeLists.txt index 27c13a1..360b0d2 100644 --- a/plugins/bluetooth/CMakeLists.txt +++ b/plugins/bluetooth/CMakeLists.txt @@ -27,8 +27,7 @@ target_link_libraries(bluetoothplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib amb install(TARGETS bluetoothplugin LIBRARY DESTINATION lib/automotive-message-broker) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ambbt.conf DESTINATION /etc/dbus-1/system.d ) - -add_executable(testAmbBt "testAmbBt.cpp") +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}) endif(bluetooth_plugin) diff --git a/plugins/bluetooth/bluetoothplugin.cpp b/plugins/bluetooth/bluetoothplugin.cpp index b605726..e5ffa32 100644 --- a/plugins/bluetooth/bluetoothplugin.cpp +++ b/plugins/bluetooth/bluetoothplugin.cpp @@ -61,6 +61,33 @@ bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data) BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map config) :AbstractSink(re, config) { + +} + +extern "C" AbstractSink * create(AbstractRoutingEngine* routingengine, map config) +{ + return new BluetoothSinkPlugin(routingengine, config); + +} + +const string BluetoothSinkPlugin::uuid() +{ + return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66"; +} + +void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value) +{ + +} + +void BluetoothSinkPlugin::dataReceived(QByteArray data) +{ + +} + +AbstractBluetoothSerialProfile::AbstractBluetoothSerialProfile(QString r) + :role(r) +{ new BtProfileAdaptor(this); if(!QDBusConnection::systemBus().registerService("org.automotive.message.broker.bluetooth")) @@ -72,9 +99,10 @@ BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map reply = profileManagerIface.call("RegisterProfile", qVariantFromValue(QDBusObjectPath("/org/bluez/spp")), "00001101-0000-1000-8000-00805F9B34FB", options); @@ -84,30 +112,12 @@ BluetoothSinkPlugin::BluetoothSinkPlugin(AbstractRoutingEngine* re, map config) -{ - return new BluetoothSinkPlugin(routingengine, config); - -} - -const string BluetoothSinkPlugin::uuid() -{ - return "47ec4ed0-dc45-11e3-9c1a-0800200c9a66"; -} - -void BluetoothSinkPlugin::propertyChanged(AbstractPropertyType *value) -{ - -} - -void BluetoothSinkPlugin::release() +void AbstractBluetoothSerialProfile::release() { DebugOut()<<"release called."< #include -class BluetoothSinkPlugin; +class AbstractBluetoothSerialProfile; class BtProfileAdaptor : public QDBusAbstractAdaptor { @@ -38,7 +38,7 @@ class BtProfileAdaptor : public QDBusAbstractAdaptor public: - BtProfileAdaptor(BluetoothSinkPlugin* parent); + BtProfileAdaptor(AbstractBluetoothSerialProfile* parent); public Q_SLOTS: @@ -50,34 +50,49 @@ public Q_SLOTS: private: - BluetoothSinkPlugin* mParent; + AbstractBluetoothSerialProfile* mParent; }; -class BluetoothSinkPlugin: public QObject, public AbstractSink +class AbstractBluetoothSerialProfile: public QObject { -Q_OBJECT + Q_OBJECT public: - BluetoothSinkPlugin(AbstractRoutingEngine* re, map config); - - const string uuid(); + AbstractBluetoothSerialProfile(QString role = "server"); - void supportedChanged(const PropertyList &) {} + virtual void release(); - void propertyChanged(AbstractPropertyType* value); + virtual void newConnection(std::string path, QDBusUnixFileDescriptor fd, QVariantMap props); - void release(); + virtual void requestDisconnection(std::string path); - void newConnection(std::string path, QDBusUnixFileDescriptor fd, QVariantMap props); + virtual void canHasData(); - void requestDisconnection(std::string path); +protected: + virtual void dataReceived(QByteArray data) = 0; + SerialPort socket; - void canHasData(); +private: + QString role; +}; +class BluetoothSinkPlugin: public AbstractBluetoothSerialProfile, public AbstractSink +{ + Q_OBJECT + +public: + BluetoothSinkPlugin(AbstractRoutingEngine* re, map config); + + const string uuid(); + + void supportedChanged(const PropertyList &) {} + + void propertyChanged(AbstractPropertyType* value); + +protected: + virtual void dataReceived(QByteArray data); -private: - SerialPort socket; }; diff --git a/plugins/bluetooth/testAmbBt.cpp b/plugins/bluetooth/testAmbBt.cpp index 53b470d..7b078a2 100644 --- a/plugins/bluetooth/testAmbBt.cpp +++ b/plugins/bluetooth/testAmbBt.cpp @@ -5,32 +5,17 @@ #include #include -#include "bluetooth5.h" -#include +#include "bluetoothplugin.h" -SerialPort *s = new SerialPort(); - -bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data) +class Bluetooth5: public AbstractBluetoothSerialProfile { -// DebugOut(5) << "Polling..." << condition << endl; - - if(condition & G_IO_ERR) - { - DebugOut(DebugOut::Error)<<"GpsNmeaSource polling error."<read(); - - return true; -} +}; int main(int argc, char** argv) { @@ -42,23 +27,9 @@ int main(int argc, char** argv) qDebug()<<"connecting to: "<setDescriptor(fd); - - GIOChannel *chan = g_io_channel_unix_new(s->fileDescriptor()); - g_io_add_watch(chan, GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR),(GIOFunc)readCallback, nullptr); - g_io_channel_set_close_on_unref(chan, true); - g_io_channel_unref(chan); - - s->write("ping\r\n"); - }); + Bluetooth5 client; app.exec(); - s->close(); - return 1; } diff --git a/plugins/common/bluetooth5.cpp b/plugins/common/bluetooth5.cpp index b1934db..5244e70 100644 --- a/plugins/common/bluetooth5.cpp +++ b/plugins/common/bluetooth5.cpp @@ -1,6 +1,9 @@ #include "bluetooth5.h" +#include "superptr.hpp" + #include "debugout.h" #include +#include #include static const gchar introspection_xml[] = @@ -45,8 +48,40 @@ static void handleMethodCall(GDBusConnection *connection, gint32 fd; GVariantIter* iter; + DebugOut() << "parameters signature: " << g_variant_get_type_string(parameters) << endl; + g_variant_get(parameters,"(oha{sv})", &device, &fd, &iter); + DebugOut() << "device: " << device << endl; + + auto message = g_dbus_method_invocation_get_message(invocation); + + auto fdList = g_dbus_message_get_unix_fd_list(message); + + GError* error = nullptr; + + fd = g_unix_fd_list_get(fdList, 0, &error); + + auto errorPtr = amb::make_super(error); + + if(errorPtr) + { + DebugOut(DebugOut::Error) << "Error trying to get fd: " << errorPtr->message << endl; + return; + } + + char* propertyName; + GVariant* value; + + DebugOut() << "trying to see what properties we got with this call" << endl; + + while(g_variant_iter_next(iter,"{sv}", &propertyName, &value)) + { + auto keyPtr = amb::make_super(propertyName); + auto valuePtr = amb::make_super(value); + DebugOut() << "key " << keyPtr.get() << "value signature: " << g_variant_get_type_string(valuePtr.get()) << endl; + } + manager->connected(fd); } else if(method == "RequestDisconnection") @@ -81,106 +116,33 @@ static const GDBusInterfaceVTable interfaceVTable = setProperty }; - -std::string findAdapterPath(std::string adapterAddy) +std::string findDevice(std::string address, std::string adapterPath="") { - std::string adapterObjectPath; + std::string objectPath; - GError * error = nullptr; - GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, + GError * proxyError = nullptr; + auto managerProxy = amb::make_super(g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.bluez", "/", "org.freedesktop.DBus.ObjectManager", - nullptr,&error); + nullptr, &proxyError)); - if(error) + auto proxyErrorPtr = amb::make_super(proxyError); + if(proxyErrorPtr) { - DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<message<message<message<message<message<message<message<message <message << endl; + } + }, + this); } void Bluetooth5::connected(int fd) { - try + //try { mConnected(fd); } - catch(...) + //catch(...) { - + //DebugOut(DebugOut::Error) << "Error calling connected callback" << endl; } } diff --git a/plugins/common/serialport.hpp b/plugins/common/serialport.hpp index 0908687..238a4a4 100644 --- a/plugins/common/serialport.hpp +++ b/plugins/common/serialport.hpp @@ -50,19 +50,19 @@ public: int ret = 0; switch(newspeed) { - case 2400: + case 2400: speed = B2400; break; - case 4800: + case 4800: speed = B4800; break; - case 9600: + case 9600: speed = B9600; break; - case 19200: + case 19200: speed = B19200; break; - case 38400: + case 38400: speed = B38400; break; default: @@ -75,8 +75,6 @@ public: { fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY); - - return setupDevice(); } @@ -97,12 +95,11 @@ public: char buff; std::string result; int bytesread = 0; - while( bytesread = ::read(fd,&buff,1) > 0 ) + while( bytesread = ::read(fd, &buff, 1) > 0 ) { result += buff; } - if(bytesread == -1) perror("Error while reading: "); diff --git a/plugins/gpsnmea/gpsnmea.cpp b/plugins/gpsnmea/gpsnmea.cpp index 1925945..c250d2b 100644 --- a/plugins/gpsnmea/gpsnmea.cpp +++ b/plugins/gpsnmea/gpsnmea.cpp @@ -390,11 +390,11 @@ bool readCallback(GIOChannel *source, GIOCondition condition, gpointer data) extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map config) { return new GpsNmeaSource(routingengine, config); - + } GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map config) - :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b"), device(nullptr) + :AbstractSource(re,config), mUuid("33d86462-1708-4f78-a001-99ea8d55422b"), device(nullptr), bt(nullptr) { int baudrate = 0; location =new Location(re, mUuid); @@ -425,11 +425,14 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map conf if(config.find("device")!= config.end()) { std::string dev = config["device"]; + +#ifdef USE_BLUEZ5 if(dev.find(":") != string::npos) { -#ifdef USE_BLUEZ5 - Bluetooth5 bt; - bt.getDeviceForAddress(dev,[this](int fd) { + + bt = new Bluetooth5(); + bt->getDeviceForAddress(dev, [this](int fd) { + DebugOut() << "fd: " << fd << endl; device = new SerialPort(fd); int baudrate=0; @@ -439,59 +442,73 @@ GpsNmeaSource::GpsNmeaSource(AbstractRoutingEngine *re, map conf DebugOut(DebugOut::Error)<<"Unsupported baudrate " << configuration["baudrate"] << endl; } - if(!device->open()) - { - DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<read()<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); //Pass ownership of the GIOChannel to the watch. + g_io_channel_unref(chan); }); +#else + bt = new BluetoothDevice(); + dev = bt->getDeviceForAddress(dev, btaddapter); - } - } + device = new SerialPort(dev); -#else - BluetoothDevice bt; - dev = bt.getDeviceForAddress(dev, btaddapter); + if(baudrate!=0) + { + if((static_cast(device))->setSpeed(baudrate)) + DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl; + } - } + if(!device->open()) + { + DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<read()<(device))->setSpeed(baudrate)) - DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl; + GIOChannel *chan = g_io_channel_unix_new(device->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); +#endif } - - if(!device->open()) + else { - DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<read()<(device))->setSpeed(baudrate)) + DebugOut(DebugOut::Error)<<"Unsupported baudrate " << config["baudrate"] << endl; + } + + if(!device->open()) + { + DebugOut(DebugOut::Error)<<"Failed to open gps tty: "<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); //Pass ownership of the GIOChannel to the watch. + DebugOut()<<"read from device: "<read()<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); + } } -#endif } GpsNmeaSource::~GpsNmeaSource() { if(device && device->isOpen()) device->close(); + if(bt) + delete bt; } const string GpsNmeaSource::uuid() diff --git a/plugins/gpsnmea/gpsnmea.h b/plugins/gpsnmea/gpsnmea.h index 2179ed5..5336a97 100644 --- a/plugins/gpsnmea/gpsnmea.h +++ b/plugins/gpsnmea/gpsnmea.h @@ -27,6 +27,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA using namespace std; class Location; +class Bluetooth5; +class BluetoothDevice; class GpsNmeaSource: public AbstractSource { @@ -34,7 +36,7 @@ class GpsNmeaSource: public AbstractSource public: GpsNmeaSource(AbstractRoutingEngine* re, map config); ~GpsNmeaSource(); - + const string uuid(); void getPropertyAsync(AsyncPropertyReply *reply); void getRangePropertyAsync(AsyncRangePropertyReply *reply); @@ -44,9 +46,9 @@ public: PropertyList supported(); int supportedOperations(); - + void supportedChanged(const PropertyList &) {} - + PropertyInfo getPropertyInfo(VehicleProperty::Property property) { if(propertyInfoMap.find(property) != propertyInfoMap.end()) @@ -60,9 +62,6 @@ public: void test(); private: - - - bool tryParse(std::string data); void addPropertySupport(VehicleProperty::Property property, Zone::Type zone); @@ -80,6 +79,11 @@ private: std::string mUuid; std::string buffer; +#ifdef USE_BLUEZ5 + Bluetooth5 * bt; +#else + BluetoothDevice *bt +#endif }; #endif // EXAMPLEPLUGIN_H