1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* oFono - Open Source Telephony
* Copyright (C) 2013 Intel Corporation
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/dbus.h>
#include <ofono/plugin.h>
#include <ofono/log.h>
#include <gdbus/gdbus.h>
#include "bluez5.h"
#define BLUEZ_PROFILE_MGMT_INTERFACE BLUEZ_SERVICE ".ProfileManager1"
struct finish_callback {
bt_finish_cb cb;
gpointer user_data;
char *member;
};
static void profile_register_cb(DBusPendingCall *call, gpointer user_data)
{
DBusMessage *reply;
DBusError derr;
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&derr);
if (dbus_set_error_from_message(&derr, reply)) {
ofono_error("RegisterProfile() replied an error: %s, %s",
derr.name, derr.message);
dbus_error_free(&derr);
goto done;
}
DBG("");
done:
dbus_message_unref(reply);
}
static void unregister_profile_cb(DBusPendingCall *call, gpointer user_data)
{
DBusMessage *reply;
DBusError derr;
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&derr);
if (dbus_set_error_from_message(&derr, reply)) {
ofono_error("UnregisterProfile() replied an error: %s, %s",
derr.name, derr.message);
dbus_error_free(&derr);
goto done;
}
DBG("");
done:
dbus_message_unref(reply);
}
int bt_register_profile(DBusConnection *conn, const char *uuid,
uint16_t version, const char *name,
const char *object, const char *role,
uint16_t features)
{
DBusMessageIter iter, dict;
DBusPendingCall *c;
DBusMessage *msg;
DBG("Bluetooth: Registering %s (%s) profile", uuid, name);
msg = dbus_message_new_method_call(BLUEZ_SERVICE, "/org/bluez",
BLUEZ_PROFILE_MGMT_INTERFACE, "RegisterProfile");
dbus_message_iter_init_append(msg, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &object);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uuid);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &dict);
ofono_dbus_dict_append(&dict, "Name", DBUS_TYPE_STRING, &name);
ofono_dbus_dict_append(&dict, "Version", DBUS_TYPE_UINT16, &version);
if (role)
ofono_dbus_dict_append(&dict, "Role", DBUS_TYPE_STRING, &role);
if (features)
ofono_dbus_dict_append(&dict, "Features", DBUS_TYPE_UINT16,
&features);
dbus_message_iter_close_container(&iter, &dict);
if (!dbus_connection_send_with_reply(conn, msg, &c, -1)) {
ofono_error("Sending RegisterProfile failed");
dbus_message_unref(msg);
return -EIO;
}
dbus_pending_call_set_notify(c, profile_register_cb, NULL, NULL);
dbus_pending_call_unref(c);
dbus_message_unref(msg);
return 0;
}
void bt_unregister_profile(DBusConnection *conn, const char *object)
{
DBusMessageIter iter;
DBusPendingCall *c;
DBusMessage *msg;
DBG("Bluetooth: Unregistering profile %s", object);
msg = dbus_message_new_method_call(BLUEZ_SERVICE, "/org/bluez",
BLUEZ_PROFILE_MGMT_INTERFACE, "UnregisterProfile");
dbus_message_iter_init_append(msg, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &object);
if (!dbus_connection_send_with_reply(conn, msg, &c, -1)) {
ofono_error("Sending UnregisterProfile failed");
dbus_message_unref(msg);
return;
}
if (c) {
dbus_pending_call_set_notify(c, unregister_profile_cb, NULL, NULL);
dbus_pending_call_unref(c);
}
dbus_message_unref(msg);
}
static void finish_profile_cb(DBusPendingCall *call, gpointer user_data)
{
struct finish_callback *callback = user_data;
DBusMessage *reply;
DBusError derr;
gboolean success;
reply = dbus_pending_call_steal_reply(call);
dbus_error_init(&derr);
success = TRUE;
if (dbus_set_error_from_message(&derr, reply)) {
success = FALSE;
ofono_error("%s() replied an error: %s, %s", callback->member,
derr.name, derr.message);
dbus_error_free(&derr);
}
if (callback->cb)
callback->cb(success, callback->user_data);
dbus_message_unref(reply);
}
static void finish_callback_free(void *data)
{
struct finish_callback *callback = data;
g_free(callback->member);
g_free(callback);
}
static void device_send_message(DBusConnection *conn, const char *device,
const char *member, const char *uuid,
bt_finish_cb cb, gpointer user_data)
{
struct finish_callback *callback;
DBusMessageIter iter;
DBusPendingCall *c;
DBusMessage *msg;
DBG("Bluetooth: sending %s for %s on %s", member, uuid, device);
msg = dbus_message_new_method_call(BLUEZ_SERVICE, device,
BLUEZ_DEVICE_INTERFACE, member);
dbus_message_iter_init_append(msg, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uuid);
if (!dbus_connection_send_with_reply(conn, msg, &c, -1)) {
ofono_error("Sending %s failed", member);
dbus_message_unref(msg);
return;
}
callback = g_new0(struct finish_callback, 1);
callback->cb = cb;
callback->user_data = user_data;
callback->member = g_strdup(dbus_message_get_member(msg));
dbus_pending_call_set_notify(c, finish_profile_cb, callback,
finish_callback_free);
dbus_pending_call_unref(c);
dbus_message_unref(msg);
}
void bt_connect_profile(DBusConnection *conn,
const char *device, const char *uuid,
bt_finish_cb cb, gpointer user_data)
{
device_send_message(conn, device, "ConnectProfile", uuid,
cb, user_data);
}
OFONO_PLUGIN_DEFINE(bluez5, "BlueZ 5 Utils Plugin", VERSION,
OFONO_PLUGIN_PRIORITY_DEFAULT, NULL, NULL)
|