From 8e3c4d76d7dc6c4745e503af796b4e94b1fc53f0 Mon Sep 17 00:00:00 2001 From: brianjjones Date: Fri, 4 Apr 2014 13:29:31 -0700 Subject: [PATCH] Changing dbus calls to use bluez 5 API calls. This allows the phone app to connect to a phone Change-Id: I62150236231709ff09ae944f8cae50cc8f79d1c5 --- src/Logger.h | 2 +- src/bluez.cpp | 395 +++++++++++++++++++++++++++++++++------------------------- src/bluez.h | 1 + src/obex.cpp | 102 ++++++++------- src/ofono.cpp | 8 +- 5 files changed, 285 insertions(+), 223 deletions(-) diff --git a/src/Logger.h b/src/Logger.h index b28f1a5..9ad08c7 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -21,7 +21,7 @@ std::ostringstream platformLog; \ platformLog << fmt; \ char buf[1024]; \ - strcpy(buf, platformLog.str().c_str(), ##args); \ + sprintf(buf, platformLog.str().c_str(), ##args); \ printf("%s(%d) > %s\n", __func__, __LINE__, buf); \ } while(0) diff --git a/src/bluez.cpp b/src/bluez.cpp index 743d9a6..fef604c 100644 --- a/src/bluez.cpp +++ b/src/bluez.cpp @@ -15,54 +15,16 @@ namespace PhoneD { #define BLUEZ_PREFIX "org.bluez" #define BLUEZ_SERVICE BLUEZ_PREFIX -#define BLUEZ_MANAGER_IFACE BLUEZ_PREFIX ".Manager" -#define BLUEZ_ADAPTER_IFACE BLUEZ_PREFIX ".Adapter" -#define BLUEZ_DEVICE_IFACE BLUEZ_PREFIX ".Device" -#define BLUEZ_AGENT_IFACE BLUEZ_PREFIX ".Agent" +#define BLUEZ_ADAPTER_IFACE BLUEZ_PREFIX ".Adapter1" +#define BLUEZ_DEVICE_IFACE BLUEZ_PREFIX ".Device1" +#define BLUEZ_AGENT_IFACE BLUEZ_PREFIX ".Agent1" -#define AGENT_PATH "/org/bluez/agent_poc" +#define AGENT_PATH "/org/bluez/poc_agent" #define AGENT_CAPABILITIES "KeyboardDisplay" #define AGENT_PASSKEY 123456 #define AGENT_PINCODE "123456" -#define AGENT_INTERFACE_XML \ - "" \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - " " \ - "" - /* NOTE: * "Release" ... does nothing * "Authorize" ... automatically authorized @@ -88,12 +50,12 @@ Bluez::Bluez() : } memset(&mAgentIfaceVTable, 0, sizeof(mAgentIfaceVTable)); - // subscribe for AdapterAdded/AdapterRemoved to get notification about the change - Utils::setSignalListener(G_BUS_TYPE_SYSTEM, BLUEZ_SERVICE, BLUEZ_MANAGER_IFACE, - "/", "AdapterAdded", Bluez::handleSignal, + // subscribe for InterfacesAdded/InterfacesRemoved to get notification about the change + Utils::setSignalListener(G_BUS_TYPE_SYSTEM, BLUEZ_SERVICE, "org.freedesktop.DBus.ObjectManager", + "/", "InterfacesAdded", Bluez::handleSignal, this); - Utils::setSignalListener(G_BUS_TYPE_SYSTEM, BLUEZ_SERVICE, BLUEZ_MANAGER_IFACE, - "/", "AdapterRemoved", Bluez::handleSignal, + Utils::setSignalListener(G_BUS_TYPE_SYSTEM, BLUEZ_SERVICE, "org.freedesktop.DBus.ObjectManager", + "/", "InterfacesRemoved", Bluez::handleSignal, this); if(mAdapterPath) { @@ -118,58 +80,74 @@ Bluez::~Bluez() { gchar* Bluez::getDefaultAdapter() { - GError *err = NULL; - GVariant *reply = NULL; - reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL), - BLUEZ_SERVICE, - "/", - BLUEZ_MANAGER_IFACE, - "DefaultAdapter", - NULL, - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &err); - if(err || !reply) { - if(err) { - LoggerE("Failed to get default adapter: " << err->message); - g_error_free(err); - } - if(!reply) - LoggerE("Reply from 'DefaultAdapter' is null"); - return NULL; - } - - char *adapter = NULL; - g_variant_get(reply, "(o)", &adapter); - LoggerD("DefaultAdapter: " << adapter); - - // make a copy of adapter, 'cause it will be destroyed when 'reply' is un-refed - char *result = adapter?strdup(adapter):NULL; - - g_variant_unref(reply); - - return result; + char *result; + + GError * error = nullptr; + GDBusProxy * managerProxy = 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); + if(error) + { + LoggerE("could not create ObjManager proxy"); + // DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<message<message<message); @@ -199,35 +177,64 @@ void Bluez::handleSignal(GDBusConnection *connection, return; } - if(!strcmp(interface_name, BLUEZ_MANAGER_IFACE)) { - if(!strcmp(signal_name, "AdapterAdded")) { - const char *adapter = NULL; - g_variant_get(parameters, "(o)", &adapter); - if(adapter) { - LoggerD("Adapter added: " << adapter); - if(!ctx->mAdapterPath) { - // make added adapter as default - ctx->mAdapterPath = strdup(adapter); - //ctx->setupAgent(); - ctx->registerAgent(); - ctx->defaultAdapterAdded(); - } - } + if(!strcmp(interface_name, "org.freedesktop.DBus.ObjectManager")) { + if(!strcmp(signal_name, "InterfacesAdded")) + { + char *objPath = NULL; + GVariantIter* iter; + + g_variant_get(parameters, "(oa{sa{sv}})", &objPath, &iter); + + if(objPath) + { + GVariantIter* iter2; + char *interface = NULL; + + while(g_variant_iter_next(iter, "{sa{sv}}",&interface, &iter2)) + { + + if(!strcmp(interface, "org.bluez.Adapter1")) + { + LoggerD("Adapter added: " << objPath); + if(!ctx->mAdapterPath) { + // make added adapter as default + ctx->mAdapterPath = strdup(objPath); + //ctx->setupAgent(); + //ctx->registerAgent(); + ctx->defaultAdapterAdded(); + } + } + } } - else if(!strcmp(signal_name, "AdapterRemoved")) { - const char *adapter = NULL; - g_variant_get(parameters, "(o)", &adapter); - if(adapter) { - LoggerD("Adapter removed: " << adapter); - if(ctx->mAdapterPath && !strcmp(ctx->mAdapterPath, adapter)) { - // removed the default adapter - free(ctx->mAdapterPath); - ctx->mAdapterPath = NULL; - ctx->defaultAdapterRemoved(); - } + } + else if(!strcmp(signal_name, "InterfacesRemoved")) { + char *objPath = NULL; + GVariantIter* iter; + + g_variant_get(parameters, "(oa{sa{sv}})", &objPath, &iter); + + if(objPath) + { + GVariantIter* iter2; + char *interface = NULL; + + while(g_variant_iter_next(iter, "{sa{sv}}",&interface, &iter2)) + { + + if(!strcmp(interface, "org.bluez.Adapter1")) + { + LoggerD("Adapter removed: " << objPath); + if(ctx->mAdapterPath && !strcmp(ctx->mAdapterPath, objPath)) { + // removed the default adapter + free(ctx->mAdapterPath); + ctx->mAdapterPath = NULL; + ctx->defaultAdapterRemoved(); + } + } } } } +} else if(!strcmp(interface_name, BLUEZ_ADAPTER_IFACE)) { if(!strcmp(signal_name, "DeviceCreated")) { const char *device; @@ -292,7 +299,7 @@ void Bluez::agentHandleMethodCall( GDBusConnection *connection, return; } - if(!strcmp(method_name, "Authorize")) { + if(!strcmp(method_name, "AuthorizeService")) { g_dbus_method_invocation_return_value(invocation, NULL); } else if(!strcmp(method_name, "RequestPinCode")) { @@ -315,25 +322,104 @@ void Bluez::agentHandleMethodCall( GDBusConnection *connection, } } +gchar* Bluez::getDeviceFromAddress(std::string &address) +{ + char *result = NULL; + bool done = false; + + GError * error = nullptr; + GDBusProxy * managerProxy = 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); + if(error) + { + LoggerE("could not create ObjManager proxy"); + // DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<message<message<data1 = strdup(fileName); - Utils::setSignalListener(G_BUS_TYPE_SESSION, OBEX_CLIENT_SERVICE, - OBEX_TRANSFER_IFACE, transfer, "Complete", + data->data1 = strdup(fileName); + Utils::setSignalListener(G_BUS_TYPE_SESSION, OBEX_PREFIX, + "org.freedesktop.DBus.Properties", transfer, "PropertiesChanged", Obex::handleSignal, data); } } @@ -812,33 +805,46 @@ void Obex::handleSignal(GDBusConnection *connection, return; } - if(!strcmp(interface_name, OBEX_TRANSFER_IFACE)) { - if(!strcmp(signal_name, "Complete") || !strcmp(signal_name, "Error")) { - Utils::removeSignalListener(G_BUS_TYPE_SESSION, OBEX_CLIENT_SERVICE, - OBEX_TRANSFER_IFACE, object_path, - "Complete"); - Utils::removeSignalListener(G_BUS_TYPE_SESSION, OBEX_CLIENT_SERVICE, - OBEX_TRANSFER_IFACE, object_path, - "Error"); - if(ctx->mActiveTransfer) { - free(ctx->mActiveTransfer); - ctx->mActiveTransfer = NULL; - } - - if(!strcmp(signal_name, "Complete")) { - const char *path = static_cast(data->data1); - const char *type = static_cast(data->data2); - const char *origin = static_cast(data->cb); - ctx->processVCards(path, type, origin); - } - else if(!strcmp(signal_name, "Error")) { - // dont' have to do anything - } - if(data->data1) free(data->data1); // path - to the file containing received VCards - if(data->cb) free(data->cb); // origin - MAC address of selected remote device - delete data; - ctx->initiateNextSyncRequest(); - } + if(!strcmp(signal_name, "PropertiesChanged")) + { + char *objPath = NULL; + GVariantIter* iter, iter2; + + g_variant_get(parameters, "(sa{sv}as)", &objPath, &iter, &iter2); + + if(objPath) + { + GVariant* var; + char *prop = NULL; + + while(g_variant_iter_next(iter, "{sv}", &prop, &var)) + { + if(!strcmp(prop, "Status")) + { + char *status_str=0; + g_variant_get(var, "s", &status_str); + //LoggerD("Status is: " << status_str); + + if(!strcmp(status_str, "complete")) + { + const char *path = static_cast(data->data1); + const char *type = static_cast(data->data2); + const char *origin = static_cast(data->cb); + ctx->processVCards(path, type, origin); + + + if(data->data1) free(data->data1); // path - to the file containing received VCards + if(data->cb) free(data->cb); // origin - MAC address of selected remote device + delete data; + ctx->initiateNextSyncRequest(); + } + } + } + } + else + { + LoggerD("No objectPath found. Exiting."); + } } } diff --git a/src/ofono.cpp b/src/ofono.cpp index 1a60cfa..aba31d9 100644 --- a/src/ofono.cpp +++ b/src/ofono.cpp @@ -244,7 +244,13 @@ void OFono::asyncSelectModemCallback(GObject *source, GAsyncResult *result, gpoi std::string pathString(path); size_t idx = pathString.find( "_" ) + 1; // index of address of remote device std::string modemRemoteBtAddress = pathString.substr (idx, pathString.length()-idx); - if(!modemRemoteBtAddress.compare(btAddress)) { + + // remove additional underscores + modemRemoteBtAddress.erase(std::remove_if(modemRemoteBtAddress.begin(),modemRemoteBtAddress.end(),isnxdigit),modemRemoteBtAddress.end()); + LoggerD("modem paths to be compared: %s, %s", modemRemoteBtAddress.c_str(),btAddress.c_str()); + + if(!modemRemoteBtAddress.compare(btAddress)) + { ctx->addModem(path, props); // currently only one modem is supported - that's why the break is here // take the first one from the list and make it 'default' -- 2.7.4