From: Claudio Takahasi <cktakahasi@gmail.com>
To: bluez-devel@lists.sourceforge.net
Subject: Re: [Bluez-devel] hcid D-Bus patch
Date: Fri, 23 Sep 2005 14:15:15 -0300 [thread overview]
Message-ID: <e1effdeb050923101567fd59d4@mail.gmail.com> (raw)
In-Reply-To: <e1effdeb05092307283e73c546@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 2088 bytes --]
Hi folks,
Sorry, but the last patch(hcid_dbus_0007.patch) was not
based on the latest CVS version.
This new one is correct.
Regards,
Claudio.
On 9/23/05, Claudio Takahasi <cktakahasi@gmail.com> wrote:
>
> Hi folks,
>
> This patch implements inquiry, periodic inquiry, role switch and cancel
> periodic inquiry.
>
> Suggestions and comments are welcome!
>
> Regards,
> Claudio
>
>
> >>> Next action
> 1. move shared codes to common directory
> 2. fix the error code format for EFailed messages
> 3. handle multiple devices
> 4. develop other services
>
> >>> Configuration file
> location: /etc/dbus-1/system.d/hcid.conf
> The D-Bus configuration file is required for security policies.
>
> >>> How test
> This is the easiest way to test the hcid D-Bus services. Another option is
> develop a "c" client
> or a python client.
>
> //Sending Get Device Request
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.GetDevReq
>
> //Sending Inquiry Request
> 1o. param: length
> 2o. param: num response
> 3o. param: flags (0 or 1)
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.InquiryReq byte:4 byte:10 uint16:0
> comment: We have develop a client to extract the reply message arguments
>
> //Sending Periodic Inquiry Request
> 1o. param: length
> 2o. param: min period
> 3o. param: max period
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.PeriodicInquiryReq byte:4 byte:27 byte:49
>
> //Sending Cancel Periodic Inquiry Request
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.CancelPeriodicInqReq
>
> //Sending Role Switch Request
> 1o. param: Remote device
> 2o. param: Role
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.RoleSwitchReq string:00:20:E0:78:7F:7F byte:1
> $dbus-send --system --dest=org.bluez --type=method_call /org/bluez/hci
> org.bluez.hci.RoleSwitchReq string:00:20:E0:78:7F:7F byte:0
>
>
>
[-- Attachment #1.2: Type: text/html, Size: 2550 bytes --]
[-- Attachment #2: hcid_dbus_0008.patch --]
[-- Type: application/octet-stream, Size: 24373 bytes --]
--- bluez-utils-cvs.orig/hcid/dbus.h 1969-12-31 21:00:00.000000000 -0300
+++ bluez-utils-cvs-hcid-dbus/hcid/dbus.h 2005-09-23 10:08:16.000000000 -0300
@@ -0,0 +1,153 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2005 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
+ * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
+ * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
+ * SOFTWARE IS DISCLAIMED.
+ *
+ *
+ * $Id: dbus.h,v 0.1 2005/09/16 08:00:00 holtmann Exp $
+ */
+
+#ifndef __H_BLUEZ_DBUS_H__
+#define __H_BLUEZ_DBUS_H__
+
+#define __END_SIG__ DBUS_TYPE_INVALID_AS_STRING
+
+/* bluetoothd Bus name */
+#define BLUEZ_DBUS_NAME "org.bluez"
+
+
+/* ===== HCI definitions ===== */
+#define BLUEZ_DBUS_HCI_PATH "/org/bluez/hci"
+#define BLUEZ_DBUS_HCI_INTERFACE BLUEZ_DBUS_NAME".hci"
+
+//HCI signals
+#define BLUEZ_DBUS_HCI_SIG_INQ_START "InquiryStartSig"
+#define BLUEZ_DBUS_HCI_SIG_INQ_COMPLETE "InquiryCompleteSig"
+#define BLUEZ_DBUS_HCI_SIG_INQ_RESULT "InquiryResultSig"
+#define BLUEZ_DBUS_HCI_SIG_REMOTE_NAME "RemoteNameSig"
+
+//HCI Provided services
+#define HCI_GET_DEVICES_REQ "GetDevReq"
+#define HCI_PERIODIC_INQ_REQ "PeriodicInqReq"
+#define HCI_CANCEL_PERIODIC_INQ_REQ "CancelPeriodicInqReq"
+#define HCI_INQ_REQ "InquiryReq"
+#define HCI_ROLE_SWITCH_REQ "RoleSwitchReq"
+
+//HCI signatures
+#define HCI_GET_DEVICES_REQ_SIGNATURE __END_SIG__
+
+#define HCI_PERIODIC_INQ_REQ_SIGNATURE DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_BYTE_AS_STRING\
+ __END_SIG__
+
+#define HCI_CANCEL_PERIODIC_INQ_REQ_SIGNATURE __END_SIG__
+
+#define HCI_INQ_REQ_SIGNATURE DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_UINT16_AS_STRING\
+ __END_SIG__
+
+#define HCI_ROLE_SWITCH_REQ_SIGNATURE DBUS_TYPE_STRING_AS_STRING\
+ DBUS_TYPE_BYTE_AS_STRING\
+ __END_SIG__
+
+#define HCI_DEVICE_STRUCT_SIGNATURE DBUS_STRUCT_BEGIN_CHAR_AS_STRING\
+ DBUS_TYPE_STRING_AS_STRING\
+ DBUS_TYPE_STRING_AS_STRING\
+ DBUS_STRUCT_END_CHAR_AS_STRING
+/* yya(ss)*/
+#define HCI_GET_DEVICES_REPLY_SIGNATURE DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_BYTE_AS_STRING\
+ DBUS_TYPE_ARRAY_AS_STRING\
+ HCI_DEVICE_STRUCT_SIGNATURE\
+ __END_SIG__
+
+#define HCI_INQ_REPLY_SIGNATURE DBUS_STRUCT_BEGIN_CHAR_AS_STRING\
+ DBUS_TYPE_STRING_AS_STRING\
+ DBUS_TYPE_UINT32_AS_STRING\
+ DBUS_TYPE_UINT16_AS_STRING\
+ DBUS_STRUCT_END_CHAR_AS_STRING\
+ __END_SIG__
+
+
+/* ===== HCICONFIG definitions ===== */
+//HCICONFIG signals
+//HCICONFIG Provided services
+//HCICONFIG signatures
+
+/* ===== SDP definitions ===== */
+//SDP signals
+//SDP Provided services
+//SDP signatures
+
+
+/* ===== RFCOMM definitions ===== */
+//RFCOMM signals
+//RFCOMM Provided services
+//RFCOMM signatures
+
+
+/* ===== PAN definitions ===== */
+//PAN signals
+//PAN Provided services
+//PAN signatures
+
+/* ===== HID definitions ===== */
+//HID signals
+//HID Provided services
+//HID signatures
+
+
+/* ===== Common Error messages of org.bluez. =====*/
+#define BLUEZ_DBUS_ERR_FAILED BLUEZ_DBUS_NAME".EFailed"
+#define BLUEZ_DBUS_ERR_NO_MEMORY BLUEZ_DBUS_NAME".ENoMemory"
+//TODO What are the other error types?
+
+
+/* BLUEZ_DBUS_ERR_FAILED
+ * EFailed error messages signature is : sni
+ * Where the first argument is a string(error message description),
+ * the second is a uint16 that contains the error class(system, dbus or hci).
+ * The last argument is a int32 that contains the error code(errno
+ * for system errors, a dbus error code defined below or a hci error).
+ * For hci error see the bluetooth specification.
+ */
+
+/* first argument of the error msg */
+#define BLUEZ_ECLASS_SYSTEM (0x01)
+#define BLUEZ_ECLASS_DBUS (0x02)
+#define BLUEZ_ECLASS_HCI (0x03) /* see Bluetooth error code */
+
+/* D-Bus error code (second argument of the error msg)*/
+#define BLUEZ_EDBUS_UNKNOWN_METHOD (0x01)
+#define BLUEZ_EDBUS_WRONG_SIGNATURE (0x02)
+#define BLUEZ_EDBUS_WRONG_PARAM (0x03)
+#define BLUEZ_EDBUS_RECORD_NOT_FOUND (0x04)
+
+/* BLUEZ_DBUS_ERR_NO_MEMORY */
+#define BLUEZ_DBUS_ERR_NO_MEMORY_STR "No memory"
+
+
+#endif /*__H_BLUEZ_DBUS_H__*/
--- bluez-utils-cvs.orig/hcid/dbus.c 2005-09-23 14:09:28.000000000 -0300
+++ bluez-utils-cvs-hcid-dbus/hcid/dbus.c 2005-09-23 14:00:51.000000000 -0300
@@ -33,6 +33,9 @@
#endif
#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/syslog.h>
@@ -45,23 +48,159 @@
#include "glib-ectomy.h"
#include "hcid.h"
+#include "dbus.h"
static DBusConnection *connection;
#define TIMEOUT (30 * 1000) /* 30 seconds */
+#define BLUETOOTH_DEVICE_NAME_LEN (18)
+#define BLUETOOTH_DEVICE_ADDR_LEN (18)
#define SERVICE_NAME "org.bluez.PinAgent"
#define INTERFACE_NAME SERVICE_NAME
#define REQUEST_NAME "PinRequest"
#define PATH_NAME "/org/bluez/PinAgent"
-#define WRONG_ARGS_ERROR "org.bluez.Error.WrongArgs"
struct pin_request {
int dev;
bdaddr_t bda;
};
+/*
+ * This function should be moved to a common library
+ */
+int find_conn(int s, int dev_id, long arg)
+{
+ struct hci_conn_list_req *cl = NULL;
+ struct hci_conn_info *ci;
+ int i;
+ int result = 0;
+
+ if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
+ syslog(LOG_ERR, "Can't allocate memory\n");
+ return 0;
+ }
+
+ cl->dev_id = dev_id;
+ cl->conn_num = 10;
+ ci = cl->conn_info;
+
+ if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
+ syslog(LOG_ERR, "Can't get connection list\n");
+ return 0;
+ }
+
+ for (i = 0; i < cl->conn_num; i++, ci++) {
+ if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
+ result = 1;
+ goto cleanup;
+ }
+ }
+cleanup:
+ if(cl)
+ free(cl);
+ return result;
+}
+
+/*
+ * D-Bus error messages functions and declarations.
+ * This section should be moved to a common file
+ * in the future
+ *
+ */
+typedef struct {
+ uint8_t code;
+ const char *str;
+}bluez_error_t;
+
+static const bluez_error_t error_array[] = {
+ { BLUEZ_EDBUS_UNKNOWN_METHOD, "Method not found"},
+ { BLUEZ_EDBUS_WRONG_SIGNATURE, "Wrong method signature"},
+ { BLUEZ_EDBUS_WRONG_PARAM, "Invalid parameters"},
+ { BLUEZ_EDBUS_RECORD_NOT_FOUND, "No record found"},
+ { 0, NULL }
+};
+
+static const char *bluez_dbus_error_to_str(const uint16_t eclass, const int32_t ecode)
+{
+ const bluez_error_t *ptr;
+
+ if (eclass == BLUEZ_ECLASS_SYSTEM) {
+ return strerror(ecode);
+ } else { /* BLUEZ_ECLASS_DBUS */
+
+ for (ptr = error_array; ptr->code; ptr++) {
+ if(ptr->code == ecode) {
+ return ptr->str;
+ }
+ }
+ }
+ return NULL;
+}
+
+DBusMessage *bluez_new_failure_msg(DBusMessage *msg, const uint16_t eclass, const int32_t ecode)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply = NULL;
+ const char *error_msg = NULL;
+
+ error_msg = bluez_dbus_error_to_str(eclass, ecode);
+
+ if (error_msg) {
+
+ reply = dbus_message_new_error(msg, BLUEZ_DBUS_ERR_FAILED, error_msg);
+
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT16 ,&eclass);
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32 ,&ecode);
+ }
+
+ return reply;
+}
+
+/*
+ * Message handler functions object table declaration
+ *
+ */
+
+typedef DBusMessage* (service_handler_func_t)(DBusMessage *, void *);
+
+typedef struct
+{
+ const char *name;
+ service_handler_func_t *handler_func;
+ const char *signature;
+}service_table_t;
+
+static DBusHandlerResult hci_msg_func(DBusConnection *conn, DBusMessage *msg, void *data);
+static DBusHandlerResult hci_signal_filter (DBusConnection *conn, DBusMessage *msg, void *data);
+
+static DBusMessage* handle_get_devices_req(DBusMessage *msg, void *data);
+static DBusMessage* handle_periodic_inq_req(DBusMessage *msg, void *data);
+static DBusMessage* handle_cancel_periodic_inq_req(DBusMessage *msg, void *data);
+static DBusMessage* handle_inq_req(DBusMessage *msg, void *data);
+static DBusMessage* handle_role_switch_req(DBusMessage *msg, void *data);
+
+
+static const DBusObjectPathVTable hci_vtable = {
+ NULL,
+ &hci_msg_func,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+static const service_table_t hci_services[] = {
+ {HCI_GET_DEVICES_REQ, handle_get_devices_req, HCI_GET_DEVICES_REQ_SIGNATURE},
+ {HCI_PERIODIC_INQ_REQ, handle_periodic_inq_req, HCI_PERIODIC_INQ_REQ_SIGNATURE},
+ {HCI_CANCEL_PERIODIC_INQ_REQ, handle_cancel_periodic_inq_req, HCI_CANCEL_PERIODIC_INQ_REQ_SIGNATURE},
+ {HCI_ROLE_SWITCH_REQ, handle_role_switch_req, HCI_ROLE_SWITCH_REQ_SIGNATURE},
+ {HCI_INQ_REQ, handle_inq_req, HCI_INQ_REQ_SIGNATURE},
+ {NULL, NULL, NULL}
+};
+
static void reply_handler_function(DBusPendingCall *call, void *user_data)
{
struct pin_request *req = (struct pin_request *) user_data;
@@ -170,8 +309,9 @@
baswap(&tmp, local); local_addr = batostr(&tmp);
- message = dbus_message_new_signal("/org/bluez/DevAgent",
- "org.bluez.DevAgent", "InquiryStart");
+ message = dbus_message_new_signal(BLUEZ_DBUS_HCI_PATH,
+ BLUEZ_DBUS_HCI_INTERFACE,
+ BLUEZ_DBUS_HCI_SIG_INQ_START);
if (message == NULL) {
syslog(LOG_ERR, "Can't allocate D-BUS inquiry start message");
goto failed;
@@ -204,8 +344,9 @@
baswap(&tmp, local); local_addr = batostr(&tmp);
- message = dbus_message_new_signal("/org/bluez/DevAgent",
- "org.bluez.DevAgent", "InquiryComplete");
+ message = dbus_message_new_signal(BLUEZ_DBUS_HCI_PATH,
+ BLUEZ_DBUS_HCI_INTERFACE,
+ BLUEZ_DBUS_HCI_SIG_INQ_COMPLETE);
if (message == NULL) {
syslog(LOG_ERR, "Can't allocate D-BUS inquiry complete message");
goto failed;
@@ -241,8 +382,9 @@
baswap(&tmp, local); local_addr = batostr(&tmp);
baswap(&tmp, peer); peer_addr = batostr(&tmp);
- message = dbus_message_new_signal("/org/bluez/DevAgent",
- "org.bluez.DevAgent", "InquiryResult");
+ message = dbus_message_new_signal(BLUEZ_DBUS_HCI_PATH,
+ BLUEZ_DBUS_HCI_INTERFACE,
+ BLUEZ_DBUS_HCI_SIG_INQ_RESULT);
if (message == NULL) {
syslog(LOG_ERR, "Can't allocate D-BUS inquiry result message");
goto failed;
@@ -280,8 +422,9 @@
baswap(&tmp, local); local_addr = batostr(&tmp);
baswap(&tmp, peer); peer_addr = batostr(&tmp);
- message = dbus_message_new_signal("/org/bluez/DevAgent",
- "org.bluez.DevAgent", "RemoteName");
+ message = dbus_message_new_signal(BLUEZ_DBUS_HCI_PATH,
+ BLUEZ_DBUS_HCI_INTERFACE,
+ BLUEZ_DBUS_HCI_SIG_REMOTE_NAME);
if (message == NULL) {
syslog(LOG_ERR, "Can't allocate D-BUS remote name message");
goto failed;
@@ -398,8 +541,430 @@
return FALSE;
}
+ dbus_bus_request_name(connection, BLUEZ_DBUS_NAME,
+ DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT,
+ &error);
+
+ if (dbus_error_is_set (&error)) {
+ syslog(LOG_ERR,"D-Bus Error: %s\n", error.message);
+ dbus_error_free (&error);
+ return FALSE;
+ }
+
+ if (!dbus_connection_register_object_path(connection, BLUEZ_DBUS_HCI_PATH, &hci_vtable, NULL)) {
+ syslog(LOG_ERR,"DBUS failed to register %s object", BLUEZ_DBUS_HCI_PATH);
+ return FALSE;
+ }
+
+ if (!dbus_connection_add_filter (connection, hci_signal_filter, NULL, NULL)) {
+ syslog(LOG_ERR,"DBUS failed to add filter");
+ return FALSE;
+ }
+
dbus_connection_set_watch_functions(connection,
add_watch, remove_watch, watch_toggled, NULL, NULL);
return TRUE;
}
+
+/*****************************************************************
+ *
+ * Section reserved to D-Bus message handlers
+ *
+ *****************************************************************/
+
+static DBusHandlerResult hci_signal_filter (DBusConnection *conn, DBusMessage *msg, void *data)
+{
+ DBusHandlerResult ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ const char *iface;
+ const char *method;
+
+ if (!msg || !conn)
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ if (dbus_message_get_type (msg) != DBUS_MESSAGE_TYPE_SIGNAL)
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+ iface = dbus_message_get_interface(msg);
+ method = dbus_message_get_member(msg);
+
+ if (strcmp(iface, DBUS_INTERFACE_LOCAL) == 0) {
+ if (strcmp(method, "Disconnected") == 0){
+ ret = DBUS_HANDLER_RESULT_HANDLED;
+ }
+ } else if (strcmp(iface, DBUS_INTERFACE_DBUS) == 0) {
+ if (strcmp(method, "NameOwnerChanged") == 0) {
+ ret = DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (strcmp(method, "NameAcquired") == 0) {
+ ret = DBUS_HANDLER_RESULT_HANDLED;
+ }
+ }
+ return ret;
+}
+static DBusHandlerResult hci_msg_func(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ const service_table_t *ptr_handlers = hci_services;
+ DBusMessage *reply = NULL;
+ uint16_t type;
+ const char *iface;
+ const char *method;
+ const char *signature;
+ uint8_t result = BLUEZ_EDBUS_UNKNOWN_METHOD;
+ DBusHandlerResult ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;;
+
+ type = dbus_message_get_type(msg);
+ iface = dbus_message_get_interface(msg);
+ method = dbus_message_get_member (msg);
+ signature = dbus_message_get_signature(msg);
+
+ if ((type == DBUS_MESSAGE_TYPE_METHOD_CALL) &&
+ (strcmp(iface, BLUEZ_DBUS_HCI_INTERFACE) == 0) &&
+ (method != NULL)) {
+
+ for (; ptr_handlers->name; ptr_handlers++) {
+ if (strcmp(method, ptr_handlers->name) == 0) {
+ /* resetting unknown method. It's possible handle method overload */
+ result = BLUEZ_EDBUS_WRONG_SIGNATURE;
+ if (strcmp(ptr_handlers->signature, signature) == 0) {
+ reply = (ptr_handlers->handler_func)(msg, data);
+ result = 0; /* resetting wrong signature*/
+ break;
+ }
+
+ }
+ }
+
+ if (result)
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_DBUS, result);
+
+
+ /* send an error or the success reply*/
+ if (reply) {
+ if (!dbus_connection_send (conn, reply, NULL)) {
+ syslog(LOG_ERR, "%s line:%d Can't send reply message!", \
+ __PRETTY_FUNCTION__, __LINE__) ;
+ }
+ dbus_message_unref (reply);
+ }
+
+ ret = DBUS_HANDLER_RESULT_HANDLED;
+ }
+ return ret;
+}
+
+static DBusMessage* handle_get_devices_req(DBusMessage *msg, void *data)
+{
+ DBusMessageIter iter;
+ DBusMessageIter array_iter;
+ DBusMessageIter struct_iter;
+ DBusMessage *reply = NULL;
+
+ struct hci_dev_list_req *dl = NULL;
+ struct hci_dev_req *dr = NULL;
+ struct hci_dev_info di;
+ int16_t i;
+ uint8_t up_dev = 0;
+ int32_t sock = -1;
+
+ char aname[BLUETOOTH_DEVICE_NAME_LEN];
+ char aaddr[BLUETOOTH_DEVICE_ADDR_LEN];
+ char *paddr = aaddr;
+ char *pname = aname;
+ const char array_sig[] = HCI_DEVICE_STRUCT_SIGNATURE;
+
+ /* Create and bind HCI socket */
+ if ((sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
+ syslog(LOG_ERR, "Can't open HCI socket: %s (%d)",
+ strerror(errno), errno);
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ goto cleanup;
+ }
+
+
+ dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
+
+ if (!dl) {
+ syslog(LOG_ERR, "Can't allocate memory");
+ reply = dbus_message_new_error(msg, BLUEZ_DBUS_ERR_NO_MEMORY, BLUEZ_DBUS_ERR_NO_MEMORY_STR);
+ goto cleanup;
+ }
+
+ dl->dev_num = HCI_MAX_DEV;
+ dr = dl->dev_req;
+
+ if (ioctl(sock, HCIGETDEVLIST, (void *) dl) < 0) {
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ goto cleanup;
+ }
+
+
+ /* active bluetooth adapter found */
+ reply = dbus_message_new_method_return(msg);
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, array_sig, &array_iter);
+ dr = dl->dev_req;
+
+ for (i = 0; i < dl->dev_num; i++, dr++) {
+ if (hci_test_bit(HCI_UP, &dr->dev_opt)) {
+ up_dev++;
+ memset(&di, 0 , sizeof(struct hci_dev_info));
+ di.dev_id = dr->dev_id;
+
+ if (!ioctl(sock, HCIGETDEVINFO, (void *) &di)) {
+ strcpy(aname, di.name);
+ ba2str(&di.bdaddr, aaddr);
+ dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL,
+ &struct_iter);
+ dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING ,&pname);
+ dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING ,&paddr);
+ dbus_message_iter_close_container(&array_iter, &struct_iter);
+ }
+ }
+ }
+ dbus_message_iter_close_container(&iter, &array_iter);
+
+cleanup:
+ if (dl)
+ free(dl);
+
+ if (sock > 0)
+ close (sock);
+
+ return reply;
+}
+
+static DBusMessage* handle_periodic_inq_req(DBusMessage *msg, void *data)
+{
+ write_inquiry_mode_cp inq_mode;
+ periodic_inquiry_cp inq_param;
+ DBusMessageIter iter;
+ DBusMessage *reply = NULL;
+ uint8_t length;
+ uint8_t max_period;
+ uint8_t min_period;
+
+ int32_t sock = -1;
+ int32_t dev_id = -1;
+
+ if ((dev_id = hci_get_route(NULL)) < 0) {
+ syslog(LOG_ERR, "Bluetooth device is not available");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+
+ }
+
+ if ((sock = hci_open_dev(dev_id)) < 0) {
+ syslog(LOG_ERR, "HCI device open failed");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+ }
+
+ dbus_message_iter_init(msg, &iter);
+ dbus_message_iter_get_basic(&iter, &length);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_get_basic(&iter, &min_period);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_get_basic(&iter, &max_period);
+
+ if ((length >= min_period) || (min_period >= max_period)) {
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_DBUS, BLUEZ_EDBUS_WRONG_PARAM);
+ goto cleanup;
+ }
+
+ inq_param.num_rsp = 100;
+ inq_param.length = length;
+
+ inq_param.max_period = max_period;
+ inq_param.min_period = min_period;
+
+ /* General/Unlimited Inquiry Access Code (GIAC) */
+ inq_param.lap[0] = 0x33;
+ inq_param.lap[1] = 0x8b;
+ inq_param.lap[2] = 0x9e;
+
+ inq_mode.mode = 1; //INQUIRY_WITH_RSSI;
+
+ if (hci_send_cmd(sock, OGF_HOST_CTL, OCF_WRITE_INQUIRY_MODE,
+ WRITE_INQUIRY_MODE_CP_SIZE, &inq_mode) < 0) {
+ syslog(LOG_ERR, "Can't set inquiry mode:%s.", strerror(errno));
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ goto cleanup;
+ }
+
+ if (hci_send_cmd(sock, OGF_LINK_CTL, OCF_PERIODIC_INQUIRY,
+ PERIODIC_INQUIRY_CP_SIZE, &inq_param) < 0) {
+ syslog(LOG_ERR, "Can't send HCI commands:%s.", strerror(errno));
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ goto cleanup;
+ } else {
+
+ uint8_t result = 0;
+ /* return TRUE to indicate that operation was completed */
+ reply = dbus_message_new_method_return(msg);
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_BYTE ,&result);
+ }
+
+cleanup:
+
+ if (sock > 0)
+ close(sock);
+
+ return reply;
+}
+
+static DBusMessage* handle_cancel_periodic_inq_req(DBusMessage *msg, void *data)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply = NULL;
+ int32_t sock = -1;
+ int32_t dev_id = -1;
+
+ if ((dev_id = hci_get_route(NULL)) < 0) {
+ syslog(LOG_ERR, "Bluetooth device is not available");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+
+ }
+
+ if ((sock = hci_open_dev(dev_id)) < 0) {
+ syslog(LOG_ERR, "HCI device open failed");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+ }
+
+ if (hci_send_cmd(sock, OGF_LINK_CTL, OCF_EXIT_PERIODIC_INQUIRY, 0 , NULL) < 0) {
+ syslog(LOG_ERR, "Send hci command failed.");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ } else {
+ uint8_t result = 0;
+ /* return TRUE to indicate that operation was completed */
+ reply = dbus_message_new_method_return(msg);
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_BYTE ,&result);
+ }
+
+cleanup:
+ if (sock > 0)
+ close(sock);
+
+ return reply;
+}
+
+static DBusMessage* handle_inq_req(DBusMessage *msg, void *data)
+{
+ const char array_sig[] = HCI_INQ_REPLY_SIGNATURE;
+ DBusMessageIter iter;
+ DBusMessageIter array_iter;
+ DBusMessageIter struct_iter;
+ DBusMessage *reply = NULL;
+ inquiry_info *info = NULL;
+ char addr[18];
+ const char *paddr = addr;
+ int32_t dev_id = -1;
+ uint32_t class = 0;
+ uint16_t clock_offset;
+ uint16_t flags;
+ int8_t length;
+ int8_t num_rsp;
+ int8_t i;
+
+ if ((dev_id = hci_get_route(NULL)) < 0) {
+ syslog(LOG_ERR, "Bluetooth device is not available");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+
+ }
+
+ dbus_message_iter_init(msg, &iter);
+ dbus_message_iter_get_basic(&iter, &length);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_get_basic(&iter, &num_rsp);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_get_basic(&iter, &flags);
+
+ if ((length <= 0) || (num_rsp <= 0)) {
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_DBUS, BLUEZ_EDBUS_WRONG_PARAM);
+ goto cleanup;
+ }
+
+ num_rsp = hci_inquiry(dev_id, length, num_rsp, NULL, &info, flags);
+
+ if (num_rsp < 0) {
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ } else {
+ reply = dbus_message_new_method_return(msg);
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, array_sig, &array_iter);
+
+ for (i = 0; i < num_rsp; i++) {
+ ba2str(&(info+i)->bdaddr, addr);
+
+ clock_offset = btohs((info+i)->clock_offset);
+ /* only 3 bytes are used */
+ memcpy(&class, (info+i)->dev_class, 3);
+
+ dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter);
+ dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING , &paddr);
+ dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT32 , &class);
+ dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_UINT16 , &clock_offset);
+ dbus_message_iter_close_container(&array_iter, &struct_iter);
+ }
+ dbus_message_iter_close_container(&iter, &array_iter);
+ }
+
+cleanup:
+ if(info)
+ bt_free(info);
+
+ return NULL;
+}
+
+static DBusMessage* handle_role_switch_req(DBusMessage *msg, void *data)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply = NULL;
+ char *str_bdaddr = NULL;
+ bdaddr_t bdaddr;
+ uint8_t role;
+ int32_t dev_id = -1, sock = -1;
+
+ dbus_message_iter_init(msg, &iter);
+ dbus_message_iter_get_basic(&iter, &str_bdaddr);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_get_basic(&iter, &role);
+
+ str2ba(str_bdaddr, &bdaddr);
+
+ dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
+
+ if (dev_id < 0) {
+ syslog(LOG_ERR, "Bluetooth device failed\n");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+ }
+
+ sock = hci_open_dev(dev_id);
+
+ if (sock < 0) {
+ syslog(LOG_ERR, "HCI device open failed\n");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, ENODEV);
+ goto cleanup;
+ }
+
+ if (hci_switch_role(sock, &bdaddr, role, 10000) < 0) {
+ syslog(LOG_ERR, "Switch role request failed\n");
+ reply = bluez_new_failure_msg(msg, BLUEZ_ECLASS_SYSTEM, errno);
+ } else {
+ uint8_t result = 0;
+ /* return TRUE to indicate that operation was completed */
+ reply = dbus_message_new_method_return(msg);
+ dbus_message_iter_init_append(reply, &iter);
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_BYTE, &result);
+ }
+cleanup:
+
+ return reply;
+}
next prev parent reply other threads:[~2005-09-23 17:15 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-16 14:42 [Bluez-devel] hcid D-Bus patch Claudio Takahasi
2005-09-19 14:31 ` [Bluez-devel] Re: hcid D-Bus patch (RSSI question) Claudio Takahasi
2005-09-20 11:39 ` Marcel Holtmann
2005-09-21 8:51 ` [Bluez-devel] hcid D-Bus patch Marcel Holtmann
2005-09-21 12:49 ` Claudio Takahasi
2005-09-21 13:19 ` P. Durante
2005-09-21 13:52 ` Claudio Takahasi
2005-09-22 14:13 ` Marcel Holtmann
2005-09-22 17:12 ` Claudio Takahasi
2005-09-22 14:17 ` Marcel Holtmann
2005-09-22 16:51 ` Claudio Takahasi
2005-09-22 17:54 ` Marcel Holtmann
2005-09-23 14:28 ` Claudio Takahasi
2005-09-23 17:15 ` Claudio Takahasi [this message]
2005-09-25 10:33 ` Marcel Holtmann
2005-09-26 11:59 ` Claudio Takahasi
2005-09-28 8:45 ` Marcel Holtmann
2005-09-28 9:08 ` [Bluez-devel] help: HIDD and HID2HCI Charles Majola
2005-09-28 19:19 ` [Bluez-devel] hcid D-Bus patch Claudio Takahasi
2005-09-28 19:53 ` Eduardo Rocha
2005-09-29 16:25 ` Marcel Holtmann
2005-09-29 19:26 ` Claudio Takahasi
2005-09-30 8:37 ` Marcel Holtmann
2005-09-30 14:52 ` Claudio Takahasi
2005-09-30 15:03 ` Marcel Holtmann
2005-10-02 11:52 ` P. Durante
2005-10-03 13:57 ` Marcel Holtmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e1effdeb050923101567fd59d4@mail.gmail.com \
--to=cktakahasi@gmail.com \
--cc=bluez-devel@lists.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.