2012-10-25 14:43:30

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 00/18] CSCP plugin

Hi,

Here are patches to add cyclingspeed plugin which is CSC profile
implementation. All features for Collector Role are implemented.

It's not yet fully tested so sending this as RFC. Reason for this
is that PTS testcases for CSCP are buggy and I'm not able to verify
my implementation reliably. I already filled few issues and hopefully
they will be addressed soon.

Anyway, it works fine with Wahoo BlueSC (but it does not support all
features).

Comments are welcome.


Andrzej Kaczmarek (18):
cyclingspeed: Add CSC profile plugin skeleton
cyclingspeed: Add attio callbacks
cyclingspeed: Discover CSCS characteristics
cyclingspeed: Discover characteristics CCC
cyclingspeed: Read CSC Feature characteristic value
cyclingspeed: Read Sensor Location characteristic value
cyclingspeed: Add CyclingSpeedManager interface
cyclingspeed: Add support to enable measurement notifications
cyclingspeed: Process measurement notifications
cyclingspeed: Add DBus.Properties for org.bluez.CyclingSpeed
interface
doc: Remove Get-/SetProperties from CSC API document
cyclingspeed: Add stub to use SC Control Point
cyclingspeed: Add support for Request Supported Sensor Locations
cyclingspeed: Add support for Update Sensor Location
cyclingspeed: Add support for Set Cumulative Value
core: Add CyclingSpeedWatcher interface to default policy
test: Add cyclingspeed test script
doc: Rename cycling API to cyclingspeed

Makefile.am | 9 +-
Makefile.tools | 2 +-
doc/cycling-api.txt | 118 ----
doc/cyclingspeed-api.txt | 100 +++
lib/uuid.h | 6 +
profiles/cyclingspeed/cyclingspeed.c | 1225 ++++++++++++++++++++++++++++++++++
profiles/cyclingspeed/cyclingspeed.h | 26 +
profiles/cyclingspeed/main.c | 53 ++
profiles/cyclingspeed/manager.c | 96 +++
profiles/cyclingspeed/manager.h | 24 +
src/bluetooth.conf | 1 +
test/test-cyclingspeed | 115 ++++
12 files changed, 1654 insertions(+), 121 deletions(-)
delete mode 100644 doc/cycling-api.txt
create mode 100644 doc/cyclingspeed-api.txt
create mode 100644 profiles/cyclingspeed/cyclingspeed.c
create mode 100644 profiles/cyclingspeed/cyclingspeed.h
create mode 100644 profiles/cyclingspeed/main.c
create mode 100644 profiles/cyclingspeed/manager.c
create mode 100644 profiles/cyclingspeed/manager.h
create mode 100755 test/test-cyclingspeed

--
1.8.0



2012-10-26 04:49:35

by Lucas De Marchi

[permalink] [raw]
Subject: Re: [RFC 10/18] cyclingspeed: Add DBus.Properties for org.bluez.CyclingSpeed interface

On Thu, Oct 25, 2012 at 12:43 PM, Andrzej Kaczmarek
<[email protected]> wrote:
> ---
> profiles/cyclingspeed/cyclingspeed.c | 99 +++++++++++++++++++++++++++++++++++-
> 1 file changed, 98 insertions(+), 1 deletion(-)
>
> diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
> index f7ffd2e..4f48121 100644
> --- a/profiles/cyclingspeed/cyclingspeed.c
> +++ b/profiles/cyclingspeed/cyclingspeed.c
> @@ -43,6 +43,7 @@
>
> #define CYCLINGSPEED_MANAGER_INTERFACE "org.bluez.CyclingSpeedManager"
> #define CYCLINGSPEED_WATCHER_INTERFACE "org.bluez.CyclingSpeedWatcher"
> +#define CYCLINGSPEED_INTERFACE "org.bluez.CyclingSpeed"
>
> #define WHEEL_REV_SUPPORT 0x01
> #define CRANK_REV_SUPPORT 0x02
> @@ -102,6 +103,21 @@ struct characteristic {
>
> static GSList *csc_adapters = NULL;
>
> +static const char * const location_enum[] = {
> + "other", "top-of-shoe", "in-shoe", "hip", "front-wheel", "left-crank",
> + "right-crank", "left-pedal", "right-pedal", "front-hub",
> + "rear-dropout", "chainstay", "rear-wheel", "rear-hub"
> +};
> +
> +static const gchar *location2str(uint8_t value)
> +{
> + if (value < G_N_ELEMENTS(location_enum))
> + return location_enum[value];
> +
> + info("Body Sensor Location [%d] is RFU", value);
> + return location_enum[0];
> +}
> +
> static gint cmp_adapter(gconstpointer a, gconstpointer b)
> {
> const struct csc_adapter *cadapter = a;
> @@ -687,6 +703,74 @@ void csc_adapter_unregister(struct btd_adapter *adapter)
> CYCLINGSPEED_MANAGER_INTERFACE);
> }
>
> +static gboolean property_get_location(const GDBusPropertyTable *property,
> + DBusMessageIter *iter, void *data)
> +{
> + struct csc *csc = data;
> + char *loc;
> +
> + if (!csc->has_location)
> + return FALSE;
> +
> + loc = g_strdup(location2str(csc->location));
> +
> + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &loc);
> +
> + g_free(loc);

you don't need to dup/free the string

Lucas De Marchi

2012-10-25 14:43:40

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 10/18] cyclingspeed: Add DBus.Properties for org.bluez.CyclingSpeed interface

---
profiles/cyclingspeed/cyclingspeed.c | 99 +++++++++++++++++++++++++++++++++++-
1 file changed, 98 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index f7ffd2e..4f48121 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -43,6 +43,7 @@

#define CYCLINGSPEED_MANAGER_INTERFACE "org.bluez.CyclingSpeedManager"
#define CYCLINGSPEED_WATCHER_INTERFACE "org.bluez.CyclingSpeedWatcher"
+#define CYCLINGSPEED_INTERFACE "org.bluez.CyclingSpeed"

#define WHEEL_REV_SUPPORT 0x01
#define CRANK_REV_SUPPORT 0x02
@@ -102,6 +103,21 @@ struct characteristic {

static GSList *csc_adapters = NULL;

+static const char * const location_enum[] = {
+ "other", "top-of-shoe", "in-shoe", "hip", "front-wheel", "left-crank",
+ "right-crank", "left-pedal", "right-pedal", "front-hub",
+ "rear-dropout", "chainstay", "rear-wheel", "rear-hub"
+};
+
+static const gchar *location2str(uint8_t value)
+{
+ if (value < G_N_ELEMENTS(location_enum))
+ return location_enum[value];
+
+ info("Body Sensor Location [%d] is RFU", value);
+ return location_enum[0];
+}
+
static gint cmp_adapter(gconstpointer a, gconstpointer b)
{
const struct csc_adapter *cadapter = a;
@@ -687,6 +703,74 @@ void csc_adapter_unregister(struct btd_adapter *adapter)
CYCLINGSPEED_MANAGER_INTERFACE);
}

+static gboolean property_get_location(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct csc *csc = data;
+ char *loc;
+
+ if (!csc->has_location)
+ return FALSE;
+
+ loc = g_strdup(location2str(csc->location));
+
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &loc);
+
+ g_free(loc);
+
+ return TRUE;
+}
+
+static gboolean property_exists_location(const GDBusPropertyTable *property,
+ void *data)
+{
+ struct csc *csc = data;
+
+ return csc->has_location;
+}
+
+static gboolean property_exists_locations(const GDBusPropertyTable *property,
+ void *data)
+{
+ struct csc *csc = data;
+
+ return !!(csc->feature & MULTI_SENSOR_LOC_SUPPORT);
+}
+
+static gboolean property_get_wheel_rev_sup(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct csc *csc = data;
+ dbus_bool_t val;
+
+ val = !!(csc->feature & WHEEL_REV_SUPPORT);
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
+
+ return TRUE;
+}
+
+static gboolean property_get_multi_loc_sup(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct csc *csc = data;
+ dbus_bool_t val;
+
+ val = !!(csc->feature & MULTI_SENSOR_LOC_SUPPORT);
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &val);
+
+ return TRUE;
+}
+
+static const GDBusPropertyTable cyclingspeed_device_properties[] = {
+ { "Location", "s", property_get_location, NULL,
+ property_exists_location },
+ { "SupportedLocations", "as", NULL, NULL,
+ property_exists_locations },
+ { "WheelRevolutionDataSupported", "b", property_get_wheel_rev_sup },
+ { "MultipleLocationsSupported", "b", property_get_multi_loc_sup },
+ { }
+};
+
int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
{
struct btd_adapter *adapter;
@@ -703,6 +787,18 @@ int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
csc->dev = btd_device_ref(device);
csc->cadapter = cadapter;

+ if (!g_dbus_register_interface(btd_get_dbus_connection(),
+ device_get_path(device),
+ CYCLINGSPEED_INTERFACE,
+ NULL, NULL,
+ cyclingspeed_device_properties,
+ csc, destroy_csc)) {
+ error("D-Bus failed to register %s interface",
+ CYCLINGSPEED_INTERFACE);
+ destroy_csc(csc);
+ return -EIO;
+ }
+
csc->svc_range = g_new0(struct att_range, 1);
csc->svc_range->start = prim->range.start;
csc->svc_range->end = prim->range.end;
@@ -736,5 +832,6 @@ void csc_device_unregister(struct btd_device *device)

cadapter->devices = g_slist_remove(cadapter->devices, csc);

- destroy_csc(csc);
+ g_dbus_unregister_interface(btd_get_dbus_connection(),
+ device_get_path(device), CYCLINGSPEED_INTERFACE);
}
--
1.8.0


2012-10-25 14:43:44

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 14/18] cyclingspeed: Add support for Update Sensor Location

---
profiles/cyclingspeed/cyclingspeed.c | 116 ++++++++++++++++++++++++++++++++++-
1 file changed, 114 insertions(+), 2 deletions(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 164a525..bf8ea46 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -74,6 +74,7 @@ struct controlpoint_req {
struct csc *csc;
uint8_t opcode;
guint timeout;
+ GDBusPendingReply reply_id;
};

struct csc_adapter {
@@ -148,6 +149,17 @@ static const gchar *location2str(uint8_t value)
return location_enum[0];
}

+static int str2location(const char *location)
+{
+ size_t i;
+
+ for (i = 0; i < G_N_ELEMENTS(location_enum); i++)
+ if (!strcmp(location_enum[i], location))
+ return i;
+
+ return -1;
+}
+
static gint cmp_adapter(gconstpointer a, gconstpointer b)
{
const struct csc_adapter *cadapter = a;
@@ -271,6 +283,12 @@ static gboolean controlpoint_timeout(gpointer user_data)
{
struct controlpoint_req *req = user_data;

+ if (req->opcode == UPDATE_SENSOR_LOC) {
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".Failed",
+ "Operation failed (timeout)");
+ }
+
req->csc->pending_req = NULL;
g_free(req);

@@ -285,6 +303,12 @@ static void controlpoint_write_cb(guint8 status, const guint8 *pdu, guint16 len,
if (status != 0) {
error("SC Control Point write failed (opcode=%d)", req->opcode);

+ if (req->opcode == UPDATE_SENSOR_LOC) {
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".Failed",
+ "Operation failed (%d)", status);
+ }
+
req->csc->pending_req = NULL;
g_free(req);

@@ -558,6 +582,40 @@ static void measurement_notify_handler(const uint8_t *pdu, uint16_t len,
process_measurement(csc, pdu + 3, len - 3);
}

+static void controlpoint_property_reply(struct controlpoint_req *req,
+ uint8_t code)
+{
+ switch (code) {
+ case RSP_SUCCESS:
+ g_dbus_pending_property_success(req->reply_id);
+ break;
+
+ case RSP_NOT_SUPPORTED:
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".NotSupported",
+ "Feature is not supported");
+ break;
+
+ case RSP_INVALID_PARAM:
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".InvalidArguments",
+ "Invalid arguments in method call");
+ break;
+
+ case RSP_FAILED:
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".Failed",
+ "Operation failed");
+ break;
+
+ default:
+ g_dbus_pending_property_error(req->reply_id,
+ ERROR_INTERFACE ".Failed",
+ "Operation failed (%d)", code);
+ break;
+ }
+}
+
static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
gpointer user_data)
{
@@ -565,6 +623,7 @@ static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
struct controlpoint_req *req = csc->pending_req;
uint8_t opcode;
uint8_t req_opcode;
+ uint8_t rsp_code;
uint8_t *opdu;
uint16_t olen;
size_t plen;
@@ -598,7 +657,7 @@ static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
}

req_opcode = *pdu;
- /* skip response code for now */
+ rsp_code = *(pdu + 1);
pdu += 2;
len -= 2;

@@ -616,6 +675,10 @@ static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
error("Failed to read Supported Sendor Locations");
}
break;
+
+ case UPDATE_SENSOR_LOC:
+ controlpoint_property_reply(req, rsp_code);
+ break;
}

csc->pending_req = NULL;
@@ -884,6 +947,55 @@ static gboolean property_get_location(const GDBusPropertyTable *property,
return TRUE;
}

+static void property_set_location(const GDBusPropertyTable *property,
+ DBusMessageIter *iter,
+ GDBusPendingPropertySet id, void *data)
+{
+ struct csc *csc = data;
+ char *loc;
+ int loc_val;
+ uint8_t att_val[2];
+ struct controlpoint_req *req;
+
+ if (csc->pending_req != NULL)
+ return g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".InProgress",
+ "Operation already in progress");
+
+ if (!(csc->feature & MULTI_SENSOR_LOC_SUPPORT))
+ return g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".NotSupported",
+ "Feature is not supported");
+
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
+ return g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".InvalidArguments",
+ "Invalid arguments in method call");
+
+ dbus_message_iter_get_basic(iter, &loc);
+
+ loc_val = str2location(loc);
+
+ if (loc_val < 0)
+ return g_dbus_pending_property_error(id,
+ ERROR_INTERFACE ".InvalidArguments",
+ "Invalid arguments in method call");
+
+ req = g_new(struct controlpoint_req, 1);
+ req->csc = csc;
+ req->reply_id = id;
+ req->opcode = UPDATE_SENSOR_LOC;
+
+ csc->pending_req = req;
+
+ att_val[0] = UPDATE_SENSOR_LOC;
+ att_val[1] = loc_val;
+
+ gatt_write_char(csc->attrib, csc->controlpoint_val_handle, att_val,
+ sizeof(att_val), controlpoint_write_cb, req);
+
+}
+
static gboolean property_exists_location(const GDBusPropertyTable *property,
void *data)
{
@@ -948,7 +1060,7 @@ static gboolean property_get_multi_loc_sup(const GDBusPropertyTable *property,
}

static const GDBusPropertyTable cyclingspeed_device_properties[] = {
- { "Location", "s", property_get_location, NULL,
+ { "Location", "s", property_get_location, property_set_location,
property_exists_location },
{ "SupportedLocations", "as", property_get_locations, NULL,
property_exists_locations },
--
1.8.0


2012-10-25 14:43:45

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 15/18] cyclingspeed: Add support for Set Cumulative Value

---
profiles/cyclingspeed/cyclingspeed.c | 92 +++++++++++++++++++++++++++++++++++-
1 file changed, 91 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index bf8ea46..7b5c3f5 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -75,6 +75,7 @@ struct controlpoint_req {
uint8_t opcode;
guint timeout;
GDBusPendingReply reply_id;
+ DBusMessage *msg;
};

struct csc_adapter {
@@ -287,6 +288,15 @@ static gboolean controlpoint_timeout(gpointer user_data)
g_dbus_pending_property_error(req->reply_id,
ERROR_INTERFACE ".Failed",
"Operation failed (timeout)");
+ } else if (req->opcode == SET_CUMULATIVE_VALUE) {
+ DBusMessage *reply;
+
+ reply = btd_error_failed(req->msg,
+ "Operation failed (timeout)");
+
+ g_dbus_send_message(btd_get_dbus_connection(), reply);
+
+ dbus_message_unref(req->msg);
}

req->csc->pending_req = NULL;
@@ -307,6 +317,15 @@ static void controlpoint_write_cb(guint8 status, const guint8 *pdu, guint16 len,
g_dbus_pending_property_error(req->reply_id,
ERROR_INTERFACE ".Failed",
"Operation failed (%d)", status);
+ } else if (req->opcode == SET_CUMULATIVE_VALUE) {
+ DBusMessage *reply;
+
+ reply = btd_error_failed(req->msg,
+ "Operation failed");
+
+ g_dbus_send_message(btd_get_dbus_connection(), reply);
+
+ dbus_message_unref(req->msg);
}

req->csc->pending_req = NULL;
@@ -616,6 +635,34 @@ static void controlpoint_property_reply(struct controlpoint_req *req,
}
}

+static void controlpoint_method_reply(struct controlpoint_req *req,
+ uint8_t code)
+{
+ DBusMessage *reply;
+
+ switch (code) {
+ case RSP_SUCCESS:
+ reply = dbus_message_new_method_return(req->msg);
+ break;
+ case RSP_NOT_SUPPORTED:
+ reply = btd_error_not_supported(req->msg);
+ break;
+ case RSP_INVALID_PARAM:
+ reply = btd_error_invalid_args(req->msg);
+ break;
+ case RSP_FAILED:
+ reply = btd_error_failed(req->msg, "Failed");
+ break;
+ default:
+ reply = btd_error_failed(req->msg, "Unknown error");
+ break;
+ }
+
+ g_dbus_send_message(btd_get_dbus_connection(), reply);
+
+ dbus_message_unref(req->msg);
+}
+
static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
gpointer user_data)
{
@@ -667,6 +714,10 @@ static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
}

switch (req->opcode) {
+ case SET_CUMULATIVE_VALUE:
+ controlpoint_method_reply(req, rsp_code);
+ break;
+
case REQUEST_SUPPORTED_SENSOR_LOC:
if (rsp_code == RSP_SUCCESS) {
csc->num_locations = len;
@@ -1069,6 +1120,44 @@ static const GDBusPropertyTable cyclingspeed_device_properties[] = {
{ }
};

+static DBusMessage *set_cumulative_wheel_rev(DBusConnection *conn,
+ DBusMessage *msg, void *data)
+{
+ struct csc *csc = data;
+ dbus_uint32_t value;
+ struct controlpoint_req *req;
+ uint8_t att_val[5]; /* uint8 opcode + uint32 value */
+
+ if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32, &value,
+ DBUS_TYPE_INVALID))
+ return btd_error_invalid_args(msg);
+
+ if (csc->pending_req != NULL)
+ return btd_error_in_progress(msg);
+
+ req = g_new(struct controlpoint_req, 1);
+ req->csc = csc;
+ req->opcode = SET_CUMULATIVE_VALUE;
+ req->msg = dbus_message_ref(msg);
+
+ csc->pending_req = req;
+
+ att_val[0] = SET_CUMULATIVE_VALUE;
+ att_put_u32(value, att_val + 1);
+
+ gatt_write_char(csc->attrib, csc->controlpoint_val_handle, att_val,
+ sizeof(att_val), controlpoint_write_cb, req);
+
+ return NULL;
+}
+
+static const GDBusMethodTable cyclingspeed_device_methods[] = {
+ { GDBUS_ASYNC_METHOD("SetCumulativeWheelRevolutions",
+ GDBUS_ARGS({ "value", "u" }), NULL,
+ set_cumulative_wheel_rev) },
+ { }
+};
+
int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
{
struct btd_adapter *adapter;
@@ -1088,7 +1177,8 @@ int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
if (!g_dbus_register_interface(btd_get_dbus_connection(),
device_get_path(device),
CYCLINGSPEED_INTERFACE,
- NULL, NULL,
+ cyclingspeed_device_methods,
+ NULL,
cyclingspeed_device_properties,
csc, destroy_csc)) {
error("D-Bus failed to register %s interface",
--
1.8.0


2012-10-25 14:43:47

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 17/18] test: Add cyclingspeed test script

---
Makefile.tools | 2 +-
test/test-cyclingspeed | 115 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 116 insertions(+), 1 deletion(-)
create mode 100755 test/test-cyclingspeed

diff --git a/Makefile.tools b/Makefile.tools
index ec79cea..9168be9 100644
--- a/Makefile.tools
+++ b/Makefile.tools
@@ -215,4 +215,4 @@ EXTRA_DIST += test/sap_client.py test/hsplay test/hsmicro \
test/test-health test/test-health-sink test/service-record.dtd \
test/service-did.xml test/service-spp.xml test/service-opp.xml \
test/service-ftp.xml test/simple-player test/test-nap \
- test/test-heartrate test/test-alert
+ test/test-heartrate test/test-alert test/test-cycling
diff --git a/test/test-cyclingspeed b/test/test-cyclingspeed
new file mode 100755
index 0000000..f3771d3
--- /dev/null
+++ b/test/test-cyclingspeed
@@ -0,0 +1,115 @@
+#!/usr/bin/python
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+'''
+Cycling Speed and Cadence test script
+'''
+
+import gobject
+
+import sys
+import dbus
+import dbus.service
+import dbus.mainloop.glib
+from optparse import OptionParser, make_option
+
+class Watcher(dbus.service.Object):
+ @dbus.service.method("org.bluez.CyclingSpeedWatcher",
+ in_signature="oa{sv}", out_signature="")
+ def MeasurementReceived(self, device, measure):
+ print("Measurement received from %s" % device)
+
+ if "WheelRevolutions" in measure:
+ print("WheelRevolutions: ", measure["WheelRevolutions"])
+
+ if "LastWheelEventTime" in measure:
+ print("LastWheelEventTime: ", measure["LastWheelEventTime"])
+
+ if "CrankRevolutions" in measure:
+ print("CrankRevolutions: ", measure["CrankRevolutions"])
+
+ if "LastCrankEventTime" in measure:
+ print("LastCrankEventTime: ", measure["LastCrankEventTime"])
+
+if __name__ == "__main__":
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ bus = dbus.SystemBus()
+
+ option_list = [
+ make_option("-i", "--adapter", action="store",
+ type="string", dest="adapter"),
+ make_option("-b", "--device", action="store",
+ type="string", dest="address"),
+ ]
+
+ parser = OptionParser(option_list=option_list)
+
+ (options, args) = parser.parse_args()
+
+ if not options.address:
+ print("Usage: %s [-i <adapter>] -b <bdaddr> [-c <value>] [cmd]" % (sys.argv[0]))
+ print("Possible commands:")
+ print("\tShowSupportedLocations")
+ print("\tSetLocation <location>")
+ print("\tSetCumulativeWheelRevolutions <value>")
+ sys.exit(1)
+
+ manager = dbus.Interface(bus.get_object("org.bluez", "/"),
+ "org.bluez.Manager")
+ if options.adapter:
+ adapter_path = manager.FindAdapter(options.adapter)
+ else:
+ adapter_path = manager.DefaultAdapter()
+
+ adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
+ "org.bluez.Adapter")
+
+ device_path = adapter.FindDevice(options.address)
+
+ cscmanager = dbus.Interface(bus.get_object("org.bluez", adapter_path),
+ "org.bluez.CyclingSpeedManager")
+
+ watcher_path = "/test/watcher"
+ watcher = Watcher(bus, watcher_path)
+ cscmanager.RegisterWatcher(watcher_path)
+
+ csc = dbus.Interface(bus.get_object("org.bluez", device_path),
+ "org.bluez.CyclingSpeed")
+
+ device_prop = dbus.Interface(bus.get_object("org.bluez", device_path),
+ "org.freedesktop.DBus.Properties")
+
+ properties = device_prop.GetAll("org.bluez.CyclingSpeed")
+
+ if "Location" in properties:
+ print("Sensor location: %s" % properties["Location"])
+ else:
+ print("Sensor location is not supported")
+
+ if len(args) > 0:
+ if args[0] == "ShowSupportedLocations":
+ if properties["MultipleSensorLocationsSupported"]:
+ print("Supported locations: ", properties["SupportedLocations"])
+ else:
+ print("Multiple sensor locations not supported")
+
+ elif args[0] == "SetLocation":
+ if properties["MultipleSensorLocationsSupported"]:
+ device_prop.Set("org.bluez.CyclingSpeed", "Location", args[1])
+ else:
+ print("Multiple sensor locations not supported")
+
+ elif args[0] == "SetCumulativeWheelRevolutions":
+ if properties["WheelRevolutionDataSupported"]:
+ csc.SetCumulativeWheelRevolutions(dbus.UInt32(args[1]))
+ else:
+ print("Wheel revolution data not supported")
+
+ else:
+ print("Unknown command")
+ sys.exit(1)
+
+ mainloop = gobject.MainLoop()
+ mainloop.run()
--
1.8.0


2012-10-25 14:43:42

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 12/18] cyclingspeed: Add stub to use SC Control Point

This patch implements common functions to use SC Control Point.
Individual procedures will be implemented in subsequent patches.
---
profiles/cyclingspeed/cyclingspeed.c | 145 +++++++++++++++++++++++++++++++++--
1 file changed, 140 insertions(+), 5 deletions(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 4f48121..93b91a2 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -41,6 +41,11 @@
#include "log.h"
#include "cyclingspeed.h"

+/* min length for ATT indication or notification: opcode (1b) + handle (2b) */
+#define ATT_HDR_LEN 3
+
+#define ATT_TIMEOUT 30
+
#define CYCLINGSPEED_MANAGER_INTERFACE "org.bluez.CyclingSpeedManager"
#define CYCLINGSPEED_WATCHER_INTERFACE "org.bluez.CyclingSpeedWatcher"
#define CYCLINGSPEED_INTERFACE "org.bluez.CyclingSpeed"
@@ -52,6 +57,25 @@
#define WHEEL_REV_PRESENT 0x01
#define CRANK_REV_PRESENT 0x02

+#define SET_CUMULATIVE_VALUE 0x01
+#define START_SENSOR_CALIBRATION 0x02
+#define UPDATE_SENSOR_LOC 0x03
+#define REQUEST_SUPPORTED_SENSOR_LOC 0x04
+#define RESPONSE_CODE 0x10
+
+#define RSP_SUCCESS 0x01
+#define RSP_NOT_SUPPORTED 0x02
+#define RSP_INVALID_PARAM 0x03
+#define RSP_FAILED 0x04
+
+struct csc;
+
+struct controlpoint_req {
+ struct csc *csc;
+ uint8_t opcode;
+ guint timeout;
+};
+
struct csc_adapter {
struct btd_adapter *adapter;
GSList *devices; /* list of registered devices */
@@ -66,6 +90,8 @@ struct csc {
guint attioid;
/* attio id for measurement characteristics value notifications */
guint attio_measurement_id;
+ /* attio id for SC Control Point characteristics value indications */
+ guint attio_controlpoint_id;

struct att_range *svc_range;

@@ -75,6 +101,8 @@ struct csc {
uint16_t feature;
gboolean has_location;
uint8_t location;
+
+ struct controlpoint_req *pending_req;
};

struct watcher {
@@ -216,6 +244,7 @@ static void destroy_csc(gpointer user_data)

if (csc->attrib != NULL) {
g_attrib_unregister(csc->attrib, csc->attio_measurement_id);
+ g_attrib_unregister(csc->attrib, csc->attio_controlpoint_id);
g_attrib_unref(csc->attrib);
}

@@ -235,6 +264,35 @@ static void char_write_cb(guint8 status, const guint8 *pdu, guint16 len,
g_free(msg);
}

+static gboolean controlpoint_timeout(gpointer user_data)
+{
+ struct controlpoint_req *req = user_data;
+
+ req->csc->pending_req = NULL;
+ g_free(req);
+
+ return FALSE;
+}
+
+__attribute__((unused)) /* TODO: remove once controlpoint ops are implemented */
+static void controlpoint_write_cb(guint8 status, const guint8 *pdu, guint16 len,
+ gpointer user_data)
+{
+ struct controlpoint_req *req = user_data;
+
+ if (status != 0) {
+ error("SC Control Point write failed (opcode=%d)", req->opcode);
+
+ req->csc->pending_req = NULL;
+ g_free(req);
+
+ return;
+ }
+
+ req->timeout = g_timeout_add_seconds(ATT_TIMEOUT, controlpoint_timeout,
+ req);
+}
+
static void read_feature_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
@@ -312,6 +370,8 @@ static void discover_desc_cb(guint8 status, const guint8 *pdu,
for (i = 0; i < list->num; i++) {
uint8_t *value;
uint16_t handle, uuid;
+ uint8_t attr_val[2];
+ char *msg;

value = list->data[i];
handle = att_get_u16(value);
@@ -321,9 +381,6 @@ static void discover_desc_cb(guint8 status, const guint8 *pdu,
continue;

if (g_strcmp0(ch->uuid, CSC_MEASUREMENT_UUID) == 0) {
- char *msg;
- uint8_t attr_val[2];
-
ch->csc->measurement_ccc_handle = handle;

if (g_slist_length(ch->csc->cadapter->watchers) == 0) {
@@ -335,10 +392,16 @@ static void discover_desc_cb(guint8 status, const guint8 *pdu,
msg = g_strdup("Enable measurement");
}

- gatt_write_char(ch->csc->attrib, handle, attr_val,
- sizeof(attr_val), char_write_cb, msg);
+ } else if (g_strcmp0(ch->uuid, SC_CONTROL_POINT_UUID) == 0) {
+ att_put_u16(GATT_CLIENT_CHARAC_CFG_IND_BIT, attr_val);
+ msg = g_strdup("Enable SC Control Point indications");
+ } else {
+ break;
}

+ gatt_write_char(ch->csc->attrib, handle, attr_val,
+ sizeof(attr_val), char_write_cb, msg);
+
/* We only want CCC, can break here */
break;
}
@@ -476,6 +539,67 @@ static void measurement_notify_handler(const uint8_t *pdu, uint16_t len,
process_measurement(csc, pdu + 3, len - 3);
}

+static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
+ gpointer user_data)
+{
+ struct csc *csc = user_data;
+ struct controlpoint_req *req = csc->pending_req;
+ uint8_t opcode;
+ uint8_t req_opcode;
+ uint8_t *opdu;
+ uint16_t olen;
+ size_t plen;
+
+ if (len < ATT_HDR_LEN) {
+ error("Invalid PDU received");
+ return;
+ }
+
+ /* skip ATT header */
+ pdu += ATT_HDR_LEN;
+ len -= ATT_HDR_LEN;
+
+ if (len < 1) {
+ error("Op Code missing");
+ goto done;
+ }
+
+ opcode = *pdu;
+ pdu++;
+ len--;
+
+ if (opcode != RESPONSE_CODE) {
+ DBG("Unsupported Op Code received (%d)", opcode);
+ goto done;
+ }
+
+ if (len < 2) {
+ error("Invalid Response Code PDU received");
+ goto done;
+ }
+
+ req_opcode = *pdu;
+ /* skip response code for now */
+ pdu += 2;
+ len -= 2;
+
+ if (req == NULL || req->opcode != req_opcode) {
+ DBG("Indication received without pending request");
+ goto done;
+ }
+
+ /* TODO: handle response */
+
+ csc->pending_req = NULL;
+ g_source_remove(req->timeout);
+ g_free(req);
+
+done:
+ opdu = g_attrib_get_buffer(csc->attrib, &plen);
+ olen = enc_confirmation(opdu, plen);
+ if (olen > 0)
+ g_attrib_send(csc->attrib, 0, opdu, olen, NULL, NULL, NULL);
+}

static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
{
@@ -509,6 +633,12 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
} else if (g_strcmp0(c->uuid, SC_CONTROL_POINT_UUID) == 0) {
DBG("SC Control Point supported");
csc->controlpoint_val_handle = c->value_handle;
+
+ csc->attio_controlpoint_id = g_attrib_register(
+ csc->attrib, ATT_OP_HANDLE_IND,
+ c->value_handle,
+ controlpoint_ind_handler, csc, NULL);
+
discover_desc(csc, c, c_next);
}
}
@@ -576,6 +706,11 @@ static void attio_disconnected_cb(gpointer user_data)
csc->attio_measurement_id = 0;
}

+ if (csc->attio_controlpoint_id > 0) {
+ g_attrib_unregister(csc->attrib, csc->attio_controlpoint_id);
+ csc->attio_controlpoint_id = 0;
+ }
+
g_attrib_unref(csc->attrib);
csc->attrib = NULL;
}
--
1.8.0


2012-10-25 14:43:41

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 11/18] doc: Remove Get-/SetProperties from CSC API document

cyclingspeed plugin uses DBus.Properties instead of custom methods.
---
doc/cycling-api.txt | 20 +-------------------
1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/doc/cycling-api.txt b/doc/cycling-api.txt
index adbcd33..08e11c8 100644
--- a/doc/cycling-api.txt
+++ b/doc/cycling-api.txt
@@ -28,31 +28,13 @@ Service org.bluez
Interface org.bluez.CyclingSpeed
Object path [variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX

-Methods void SetProperty(string name, variant value)
-
- Changes the value of the specified property. Only
- read-write properties can be changed. On success
- this will emit a PropertyChanged signal.
-
- Possible Errors: org.bluez.Error.InvalidArguments
-
- dict GetProperties()
-
- Returns all properties for the interface. See the
- Properties section for the available properties.
-
- SetCumulativeWheelRevolutions(uint32 value)
+Methods SetCumulativeWheelRevolutions(uint32 value)

Sets cumulative wheel revolutions value if
Cumulative Wheel Revolutions feature is supported.

Possible Errors: org.bluez.Error.NotSupported

-Signals PropertyChanged(string name, variant value)
-
- This signal indicates a changed value of the given
- property.
-
Properties string Location (optional) [readwrite]

Current sensor location, if supported.
--
1.8.0


2012-10-25 14:43:43

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 13/18] cyclingspeed: Add support for Request Supported Sensor Locations

---
profiles/cyclingspeed/cyclingspeed.c | 57 ++++++++++++++++++++++++++++++++++--
1 file changed, 54 insertions(+), 3 deletions(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 93b91a2..164a525 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -101,6 +101,8 @@ struct csc {
uint16_t feature;
gboolean has_location;
uint8_t location;
+ uint8_t num_locations;
+ uint8_t *locations;

struct controlpoint_req *pending_req;
};
@@ -250,6 +252,7 @@ static void destroy_csc(gpointer user_data)

btd_device_unref(csc->dev);
g_free(csc->svc_range);
+ g_free(csc->locations);
g_free(csc);
}

@@ -274,7 +277,6 @@ static gboolean controlpoint_timeout(gpointer user_data)
return FALSE;
}

-__attribute__((unused)) /* TODO: remove once controlpoint ops are implemented */
static void controlpoint_write_cb(guint8 status, const guint8 *pdu, guint16 len,
gpointer user_data)
{
@@ -293,6 +295,20 @@ static void controlpoint_write_cb(guint8 status, const guint8 *pdu, guint16 len,
req);
}

+static void read_supported_locations(struct csc *csc)
+{
+ struct controlpoint_req *req;
+
+ req = g_new0(struct controlpoint_req, 1);
+ req->csc = csc;
+ req->opcode = REQUEST_SUPPORTED_SENSOR_LOC;
+
+ csc->pending_req = req;
+
+ gatt_write_char(csc->attrib, csc->controlpoint_val_handle, &req->opcode,
+ sizeof(req->opcode), controlpoint_write_cb, req);
+}
+
static void read_feature_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
@@ -317,6 +333,9 @@ static void read_feature_cb(guint8 status, const guint8 *pdu,
}

csc->feature = att_get_u16(value);
+
+ if (csc->feature & MULTI_SENSOR_LOC_SUPPORT)
+ read_supported_locations(csc);
}

static void read_location_cb(guint8 status, const guint8 *pdu,
@@ -588,7 +607,16 @@ static void controlpoint_ind_handler(const uint8_t *pdu, uint16_t len,
goto done;
}

- /* TODO: handle response */
+ switch (req->opcode) {
+ case REQUEST_SUPPORTED_SENSOR_LOC:
+ if (rsp_code == RSP_SUCCESS) {
+ csc->num_locations = len;
+ csc->locations = g_memdup(pdu, len);
+ } else {
+ error("Failed to read Supported Sendor Locations");
+ }
+ break;
+ }

csc->pending_req = NULL;
g_source_remove(req->timeout);
@@ -864,6 +892,29 @@ static gboolean property_exists_location(const GDBusPropertyTable *property,
return csc->has_location;
}

+static gboolean property_get_locations(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct csc *csc = data;
+ DBusMessageIter entry;
+ int i;
+
+ if (!(csc->feature & MULTI_SENSOR_LOC_SUPPORT))
+ return FALSE;
+
+ dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING, &entry);
+ for (i = 0; i < csc->num_locations; i++) {
+ char *loc = g_strdup(location2str(csc->locations[i]));
+ dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &loc);
+ g_free(loc);
+ }
+
+ dbus_message_iter_close_container(iter, &entry);
+
+ return TRUE;
+}
+
static gboolean property_exists_locations(const GDBusPropertyTable *property,
void *data)
{
@@ -899,7 +950,7 @@ static gboolean property_get_multi_loc_sup(const GDBusPropertyTable *property,
static const GDBusPropertyTable cyclingspeed_device_properties[] = {
{ "Location", "s", property_get_location, NULL,
property_exists_location },
- { "SupportedLocations", "as", NULL, NULL,
+ { "SupportedLocations", "as", property_get_locations, NULL,
property_exists_locations },
{ "WheelRevolutionDataSupported", "b", property_get_wheel_rev_sup },
{ "MultipleLocationsSupported", "b", property_get_multi_loc_sup },
--
1.8.0


2012-10-25 14:43:46

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 16/18] core: Add CyclingSpeedWatcher interface to default policy

---
src/bluetooth.conf | 1 +
1 file changed, 1 insertion(+)

diff --git a/src/bluetooth.conf b/src/bluetooth.conf
index 2db43d9..a1269a4 100644
--- a/src/bluetooth.conf
+++ b/src/bluetooth.conf
@@ -19,6 +19,7 @@
<allow send_interface="org.bluez.AlertAgent"/>
<allow send_interface="org.bluez.Profile"/>
<allow send_interface="org.bluez.HeartRateWatcher"/>
+ <allow send_interface="org.bluez.CyclingSpeedWatcher"/>
</policy>

<policy at_console="true">
--
1.8.0


2012-10-25 14:43:39

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 09/18] cyclingspeed: Process measurement notifications

---
profiles/cyclingspeed/cyclingspeed.c | 139 ++++++++++++++++++++++++++++++++++-
1 file changed, 138 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 53fbb02..f7ffd2e 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -42,6 +42,14 @@
#include "cyclingspeed.h"

#define CYCLINGSPEED_MANAGER_INTERFACE "org.bluez.CyclingSpeedManager"
+#define CYCLINGSPEED_WATCHER_INTERFACE "org.bluez.CyclingSpeedWatcher"
+
+#define WHEEL_REV_SUPPORT 0x01
+#define CRANK_REV_SUPPORT 0x02
+#define MULTI_SENSOR_LOC_SUPPORT 0x04
+
+#define WHEEL_REV_PRESENT 0x01
+#define CRANK_REV_PRESENT 0x02

struct csc_adapter {
struct btd_adapter *adapter;
@@ -55,6 +63,8 @@ struct csc {

GAttrib *attrib;
guint attioid;
+ /* attio id for measurement characteristics value notifications */
+ guint attio_measurement_id;

struct att_range *svc_range;

@@ -73,6 +83,18 @@ struct watcher {
char *path;
};

+struct measurement {
+ struct csc *csc;
+
+ bool has_wheel_rev;
+ uint32_t wheel_rev;
+ uint16_t last_wheel_time;
+
+ bool has_crank_rev;
+ uint16_t crank_rev;
+ uint16_t last_crank_time;
+};
+
struct characteristic {
struct csc *csc;
char uuid[MAX_LEN_UUID_STR + 1];
@@ -176,8 +198,10 @@ static void destroy_csc(gpointer user_data)
if (csc->attioid > 0)
btd_device_remove_attio_callback(csc->dev, csc->attioid);

- if (csc->attrib != NULL)
+ if (csc->attrib != NULL) {
+ g_attrib_unregister(csc->attrib, csc->attio_measurement_id);
g_attrib_unref(csc->attrib);
+ }

btd_device_unref(csc->dev);
g_free(csc->svc_range);
@@ -334,6 +358,109 @@ static void discover_desc(struct csc *csc, struct gatt_char *c,
gatt_find_info(csc->attrib, start, end, discover_desc_cb, ch);
}

+static void update_watcher(gpointer data, gpointer user_data)
+{
+ struct watcher *w = data;
+ struct measurement *m = user_data;
+ struct csc *csc = m->csc;
+ const gchar *path = device_get_path(csc->dev);
+ DBusMessageIter iter;
+ DBusMessageIter dict;
+ DBusMessage *msg;
+
+ msg = dbus_message_new_method_call(w->srv, w->path,
+ CYCLINGSPEED_WATCHER_INTERFACE, "MeasurementReceived");
+ if (msg == NULL)
+ return;
+
+ dbus_message_iter_init_append(msg, &iter);
+
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH , &path);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+ DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
+ DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
+
+ if (m->has_wheel_rev) {
+ dict_append_entry(&dict, "WheelRevolutions",
+ DBUS_TYPE_UINT32, &m->wheel_rev);
+ dict_append_entry(&dict, "LastWheelEventTime",
+ DBUS_TYPE_UINT16, &m->last_wheel_time);
+ }
+
+ if (m->has_crank_rev) {
+ dict_append_entry(&dict, "CrankRevolutions",
+ DBUS_TYPE_UINT16, &m->crank_rev);
+ dict_append_entry(&dict, "LastCrankEventTime",
+ DBUS_TYPE_UINT16, &m->last_crank_time);
+ }
+
+ dbus_message_iter_close_container(&iter, &dict);
+
+ dbus_message_set_no_reply(msg, TRUE);
+ g_dbus_send_message(btd_get_dbus_connection(), msg);
+}
+
+static void process_measurement(struct csc *csc, const uint8_t *pdu,
+ uint16_t len)
+{
+ struct measurement m;
+ uint8_t flags;
+
+ flags = *pdu;
+
+ pdu++;
+ len--;
+
+ memset(&m, 0, sizeof(m));
+
+ if ((flags & WHEEL_REV_PRESENT) && (csc->feature & WHEEL_REV_SUPPORT)) {
+ if (len < 6) {
+ error("Wheel revolutions data fields missing");
+ return;
+ }
+
+ m.has_wheel_rev = true;
+ m.wheel_rev = att_get_u32(pdu);
+ m.last_wheel_time = att_get_u16(pdu + 4);
+ pdu += 6;
+ len -= 6;
+ }
+
+ if ((flags & CRANK_REV_PRESENT) && (csc->feature & CRANK_REV_SUPPORT)) {
+ if (len < 4) {
+ error("Crank revolutions data fields missing");
+ return;
+ }
+
+ m.has_crank_rev = true;
+ m.crank_rev = att_get_u16(pdu);
+ m.last_crank_time = att_get_u16(pdu + 2);
+ pdu += 4;
+ len -= 4;
+ }
+
+ /* Notify all registered watchers */
+ m.csc = csc;
+ g_slist_foreach(csc->cadapter->watchers, update_watcher, &m);
+}
+
+static void measurement_notify_handler(const uint8_t *pdu, uint16_t len,
+ gpointer user_data)
+{
+ struct csc *csc = user_data;
+
+ /* should be at least opcode (1b) + handle (2b) */
+ if (len < 3) {
+ error("Invalid PDU received");
+ return;
+ }
+
+ process_measurement(csc, pdu + 3, len - 3);
+}
+
+
static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
{
struct csc *csc = user_data;
@@ -351,6 +478,11 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
(chars->next ? chars->next->data : NULL);

if (g_strcmp0(c->uuid, CSC_MEASUREMENT_UUID) == 0) {
+ csc->attio_measurement_id =
+ g_attrib_register(csc->attrib,
+ ATT_OP_HANDLE_NOTIFY, c->value_handle,
+ measurement_notify_handler, csc, NULL);
+
discover_desc(csc, c, c_next);
} else if (g_strcmp0(c->uuid, CSC_FEATURE_UUID) == 0) {
feature_val_handle = c->value_handle;
@@ -423,6 +555,11 @@ static void attio_disconnected_cb(gpointer user_data)

DBG("");

+ if (csc->attio_measurement_id > 0) {
+ g_attrib_unregister(csc->attrib, csc->attio_measurement_id);
+ csc->attio_measurement_id = 0;
+ }
+
g_attrib_unref(csc->attrib);
csc->attrib = NULL;
}
--
1.8.0


2012-10-25 14:43:48

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 18/18] doc: Rename cycling API to cyclingspeed

---
doc/cycling-api.txt | 100 -----------------------------------------------
doc/cyclingspeed-api.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+), 100 deletions(-)
delete mode 100644 doc/cycling-api.txt
create mode 100644 doc/cyclingspeed-api.txt

diff --git a/doc/cycling-api.txt b/doc/cycling-api.txt
deleted file mode 100644
index 08e11c8..0000000
--- a/doc/cycling-api.txt
+++ /dev/null
@@ -1,100 +0,0 @@
-Cycling Speed and Cadence API description
-*****************************************
-
-Copyright (C) 2012 Tieto Poland
-
-Cycling Speed and Cadence Manager hierarchy
-===========================================
-
-Service org.bluez
-Interface org.bluez.CyclingSpeedManager
-Object path [variable prefix]/{hci0,hci1,...}
-
-Methods RegisterWatcher(object agent)
-
- Registers a watcher to monitor cycling speed and
- cadence measurements.
-
- Possible Errors: org.bluez.Error.InvalidArguments
-
- UnregisterWatcher(object agent)
-
- Unregisters a watcher.
-
-Cycling Speed and Cadence Profile hierarchy
-===========================================
-
-Service org.bluez
-Interface org.bluez.CyclingSpeed
-Object path [variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX
-
-Methods SetCumulativeWheelRevolutions(uint32 value)
-
- Sets cumulative wheel revolutions value if
- Cumulative Wheel Revolutions feature is supported.
-
- Possible Errors: org.bluez.Error.NotSupported
-
-Properties string Location (optional) [readwrite]
-
- Current sensor location, if supported.
- If Multiple Sensor Locations feature is supported,
- this property can be set to one of values read from
- SupportedLocations property.
-
- Possible values: "other", "top-of-shoe", "in-shoe",
- "hip", "front-wheel", "left-crank",
- "right-crank", "left-pedal",
- "right-pedal", "front-hub",
- "rear-dropout", "chainstay",
- "rear-wheel", "rear-hub"
-
- array{string} SupportedLocations (optional) [readonly]
-
- List of locations supported by sensor, only present
- if Multiple Sensor Locations feature is supported.
-
- boolean WheelRevolutionDataSupported [readonly]
-
- true if sensor can read and set Cumulative Wheel
- Revolutions value, false otherwise.
-
- boolean MultipleSensorLocationsSupported [readonly]
-
- true if sensor supports Multiple Sensor Locations
- feature and can set Location, false otherwise.
-
-Cycling Speed and Cadence Watcher hierarchy
-===========================================
-
-Service unique name
-Interface org.bluez.CyclingSpeedWatcher
-Object path freely definable
-
-Methods void MeasurementReceived(object device, dict measurement)
-
- This callback is called whenever wheel and/or crank
- revolutions measurement is received from sensor.
-
- Measurement:
-
- uint32 WheelRevolutions (optional):
-
- Cumulative number of wheel revolutions.
-
- uint16 LastWheelEventTime (optional):
-
- Time of last event from wheel sensor.
- Value is expressed in 1/1024 second
- units and can roll over during a ride.
-
- uint16 CrankRevolutions (optional):
-
- Cumulative number of crank revolutions.
- This value can occasionally roll over.
-
- uint16 LastCrankEventTime (optional):
-
- Time of last event from crank sensor.
- Value is expressed in 1/1024 second
- units and can roll over during a ride.
diff --git a/doc/cyclingspeed-api.txt b/doc/cyclingspeed-api.txt
new file mode 100644
index 0000000..08e11c8
--- /dev/null
+++ b/doc/cyclingspeed-api.txt
@@ -0,0 +1,100 @@
+Cycling Speed and Cadence API description
+*****************************************
+
+Copyright (C) 2012 Tieto Poland
+
+Cycling Speed and Cadence Manager hierarchy
+===========================================
+
+Service org.bluez
+Interface org.bluez.CyclingSpeedManager
+Object path [variable prefix]/{hci0,hci1,...}
+
+Methods RegisterWatcher(object agent)
+
+ Registers a watcher to monitor cycling speed and
+ cadence measurements.
+
+ Possible Errors: org.bluez.Error.InvalidArguments
+
+ UnregisterWatcher(object agent)
+
+ Unregisters a watcher.
+
+Cycling Speed and Cadence Profile hierarchy
+===========================================
+
+Service org.bluez
+Interface org.bluez.CyclingSpeed
+Object path [variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX
+
+Methods SetCumulativeWheelRevolutions(uint32 value)
+
+ Sets cumulative wheel revolutions value if
+ Cumulative Wheel Revolutions feature is supported.
+
+ Possible Errors: org.bluez.Error.NotSupported
+
+Properties string Location (optional) [readwrite]
+
+ Current sensor location, if supported.
+ If Multiple Sensor Locations feature is supported,
+ this property can be set to one of values read from
+ SupportedLocations property.
+
+ Possible values: "other", "top-of-shoe", "in-shoe",
+ "hip", "front-wheel", "left-crank",
+ "right-crank", "left-pedal",
+ "right-pedal", "front-hub",
+ "rear-dropout", "chainstay",
+ "rear-wheel", "rear-hub"
+
+ array{string} SupportedLocations (optional) [readonly]
+
+ List of locations supported by sensor, only present
+ if Multiple Sensor Locations feature is supported.
+
+ boolean WheelRevolutionDataSupported [readonly]
+
+ true if sensor can read and set Cumulative Wheel
+ Revolutions value, false otherwise.
+
+ boolean MultipleSensorLocationsSupported [readonly]
+
+ true if sensor supports Multiple Sensor Locations
+ feature and can set Location, false otherwise.
+
+Cycling Speed and Cadence Watcher hierarchy
+===========================================
+
+Service unique name
+Interface org.bluez.CyclingSpeedWatcher
+Object path freely definable
+
+Methods void MeasurementReceived(object device, dict measurement)
+
+ This callback is called whenever wheel and/or crank
+ revolutions measurement is received from sensor.
+
+ Measurement:
+
+ uint32 WheelRevolutions (optional):
+
+ Cumulative number of wheel revolutions.
+
+ uint16 LastWheelEventTime (optional):
+
+ Time of last event from wheel sensor.
+ Value is expressed in 1/1024 second
+ units and can roll over during a ride.
+
+ uint16 CrankRevolutions (optional):
+
+ Cumulative number of crank revolutions.
+ This value can occasionally roll over.
+
+ uint16 LastCrankEventTime (optional):
+
+ Time of last event from crank sensor.
+ Value is expressed in 1/1024 second
+ units and can roll over during a ride.
--
1.8.0


2012-10-25 14:43:38

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 08/18] cyclingspeed: Add support to enable measurement notifications

---
profiles/cyclingspeed/cyclingspeed.c | 72 +++++++++++++++++++++++++++++++++++-
1 file changed, 71 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 0b3742a..53fbb02 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -184,6 +184,17 @@ static void destroy_csc(gpointer user_data)
g_free(csc);
}

+static void char_write_cb(guint8 status, const guint8 *pdu, guint16 len,
+ gpointer user_data)
+{
+ char *msg = user_data;
+
+ if (status != 0)
+ error("%s failed", msg);
+
+ g_free(msg);
+}
+
static void read_feature_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
@@ -269,9 +280,25 @@ static void discover_desc_cb(guint8 status, const guint8 *pdu,
if (uuid != GATT_CLIENT_CHARAC_CFG_UUID)
continue;

- if (g_strcmp0(ch->uuid, CSC_MEASUREMENT_UUID) == 0)
+ if (g_strcmp0(ch->uuid, CSC_MEASUREMENT_UUID) == 0) {
+ char *msg;
+ uint8_t attr_val[2];
+
ch->csc->measurement_ccc_handle = handle;

+ if (g_slist_length(ch->csc->cadapter->watchers) == 0) {
+ att_put_u16(0x0000, attr_val);
+ msg = g_strdup("Disable measurement");
+ } else {
+ att_put_u16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT,
+ attr_val);
+ msg = g_strdup("Enable measurement");
+ }
+
+ gatt_write_char(ch->csc->attrib, handle, attr_val,
+ sizeof(attr_val), char_write_cb, msg);
+ }
+
/* We only want CCC, can break here */
break;
}
@@ -343,6 +370,40 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
read_feature_cb, csc);
}

+static void enable_measurement(gpointer data, gpointer user_data)
+{
+ struct csc *csc = data;
+ uint16_t handle = csc->measurement_ccc_handle;
+ uint8_t value[2];
+ char *msg;
+
+ if (csc->attrib == NULL || !handle)
+ return;
+
+ att_put_u16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT, value);
+ msg = g_strdup("Enable measurement");
+
+ gatt_write_char(csc->attrib, handle, value, sizeof(value),
+ char_write_cb, msg);
+}
+
+static void disable_measurement(gpointer data, gpointer user_data)
+{
+ struct csc *csc = data;
+ uint16_t handle = csc->measurement_ccc_handle;
+ uint8_t value[2];
+ char *msg;
+
+ if (csc->attrib == NULL || !handle)
+ return;
+
+ att_put_u16(0x0000, value);
+ msg = g_strdup("Disable measurement");
+
+ gatt_write_char(csc->attrib, handle, value, sizeof(value),
+ char_write_cb, msg);
+}
+
static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
{
struct csc *csc = user_data;
@@ -375,6 +436,9 @@ static void watcher_exit_cb(DBusConnection *conn, void *user_data)

cadapter->watchers = g_slist_remove(cadapter->watchers, watcher);
g_dbus_remove_watch(conn, watcher->id);
+
+ if (g_slist_length(cadapter->watchers) == 0)
+ g_slist_foreach(cadapter->devices, disable_measurement, 0);
}

static DBusMessage *register_watcher(DBusConnection *conn, DBusMessage *msg,
@@ -400,6 +464,9 @@ static DBusMessage *register_watcher(DBusConnection *conn, DBusMessage *msg,
watcher->srv = g_strdup(sender);
watcher->path = g_strdup(path);

+ if (g_slist_length(cadapter->watchers) == 0)
+ g_slist_foreach(cadapter->devices, enable_measurement, 0);
+
cadapter->watchers = g_slist_prepend(cadapter->watchers, watcher);

DBG("cycling watcher [%s] registered", path);
@@ -426,6 +493,9 @@ static DBusMessage *unregister_watcher(DBusConnection *conn, DBusMessage *msg,
cadapter->watchers = g_slist_remove(cadapter->watchers, watcher);
g_dbus_remove_watch(conn, watcher->id);

+ if (g_slist_length(cadapter->watchers) == 0)
+ g_slist_foreach(cadapter->devices, disable_measurement, 0);
+
DBG("cycling watcher [%s] unregistered", path);

return dbus_message_new_method_return(msg);
--
1.8.0


2012-10-25 14:43:37

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 07/18] cyclingspeed: Add CyclingSpeedManager interface

This patch registers org.bluez.CyclingSpeedManager interface for each
adapter to allow registration and deregistration of measurement watchers.
---
profiles/cyclingspeed/cyclingspeed.c | 154 ++++++++++++++++++++++++++++++++++-
1 file changed, 153 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 67963d1..0b3742a 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -24,13 +24,16 @@
#include <config.h>
#endif

+#include <gdbus.h>
#include <errno.h>
#include <stdbool.h>
#include <glib.h>
#include <bluetooth/uuid.h>

#include "adapter.h"
+#include "dbus-common.h"
#include "device.h"
+#include "error.h"
#include "gattrib.h"
#include "att.h"
#include "gatt.h"
@@ -38,9 +41,12 @@
#include "log.h"
#include "cyclingspeed.h"

+#define CYCLINGSPEED_MANAGER_INTERFACE "org.bluez.CyclingSpeedManager"
+
struct csc_adapter {
struct btd_adapter *adapter;
GSList *devices; /* list of registered devices */
+ GSList *watchers;
};

struct csc {
@@ -60,6 +66,13 @@ struct csc {
uint8_t location;
};

+struct watcher {
+ struct csc_adapter *cadapter;
+ guint id;
+ char *srv;
+ char *path;
+};
+
struct characteristic {
struct csc *csc;
char uuid[MAX_LEN_UUID_STR + 1];
@@ -89,6 +102,19 @@ static gint cmp_device(gconstpointer a, gconstpointer b)
return -1;
}

+static gint cmp_watcher(gconstpointer a, gconstpointer b)
+{
+ const struct watcher *watcher = a;
+ const struct watcher *match = b;
+ int ret;
+
+ ret = g_strcmp0(watcher->srv, match->srv);
+ if (ret != 0)
+ return ret;
+
+ return g_strcmp0(watcher->path, match->path);
+}
+
static struct csc_adapter *find_csc_adapter(struct btd_adapter *adapter)
{
GSList *l = g_slist_find_custom(csc_adapters, adapter, cmp_adapter);
@@ -99,10 +125,47 @@ static struct csc_adapter *find_csc_adapter(struct btd_adapter *adapter)
return l->data;
}

+static void destroy_watcher(gpointer user_data)
+{
+ struct watcher *watcher = user_data;
+
+ g_free(watcher->path);
+ g_free(watcher->srv);
+ g_free(watcher);
+}
+
+static struct watcher *find_watcher(GSList *list, const char *sender,
+ const char *path)
+{
+ struct watcher *match;
+ GSList *l;
+
+ match = g_new0(struct watcher, 1);
+ match->srv = g_strdup(sender);
+ match->path = g_strdup(path);
+
+ l = g_slist_find_custom(list, match, cmp_watcher);
+ destroy_watcher(match);
+
+ if (l != NULL)
+ return l->data;
+
+ return NULL;
+}
+
+static void remove_watcher(gpointer user_data)
+{
+ struct watcher *watcher = user_data;
+
+ g_dbus_remove_watch(btd_get_dbus_connection(), watcher->id);
+}
+
static void destroy_csc_adapter(gpointer user_data)
{
struct csc_adapter *cadapter = user_data;

+ g_slist_free_full(cadapter->watchers, remove_watcher);
+
g_free(cadapter);
}

@@ -303,6 +366,81 @@ static void attio_disconnected_cb(gpointer user_data)
csc->attrib = NULL;
}

+static void watcher_exit_cb(DBusConnection *conn, void *user_data)
+{
+ struct watcher *watcher = user_data;
+ struct csc_adapter *cadapter = watcher->cadapter;
+
+ DBG("cycling watcher [%s] disconnected", watcher->path);
+
+ cadapter->watchers = g_slist_remove(cadapter->watchers, watcher);
+ g_dbus_remove_watch(conn, watcher->id);
+}
+
+static DBusMessage *register_watcher(DBusConnection *conn, DBusMessage *msg,
+ void *data)
+{
+ struct csc_adapter *cadapter = data;
+ struct watcher *watcher;
+ const char *sender = dbus_message_get_sender(msg);
+ char *path;
+
+ if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID))
+ return btd_error_invalid_args(msg);
+
+ watcher = find_watcher(cadapter->watchers, sender, path);
+ if (watcher != NULL)
+ return btd_error_already_exists(msg);
+
+ watcher = g_new0(struct watcher, 1);
+ watcher->cadapter = cadapter;
+ watcher->id = g_dbus_add_disconnect_watch(conn, sender, watcher_exit_cb,
+ watcher, destroy_watcher);
+ watcher->srv = g_strdup(sender);
+ watcher->path = g_strdup(path);
+
+ cadapter->watchers = g_slist_prepend(cadapter->watchers, watcher);
+
+ DBG("cycling watcher [%s] registered", path);
+
+ return dbus_message_new_method_return(msg);
+}
+
+static DBusMessage *unregister_watcher(DBusConnection *conn, DBusMessage *msg,
+ void *data)
+{
+ struct csc_adapter *cadapter = data;
+ struct watcher *watcher;
+ const char *sender = dbus_message_get_sender(msg);
+ char *path;
+
+ if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID))
+ return btd_error_invalid_args(msg);
+
+ watcher = find_watcher(cadapter->watchers, sender, path);
+ if (watcher == NULL)
+ return btd_error_does_not_exist(msg);
+
+ cadapter->watchers = g_slist_remove(cadapter->watchers, watcher);
+ g_dbus_remove_watch(conn, watcher->id);
+
+ DBG("cycling watcher [%s] unregistered", path);
+
+ return dbus_message_new_method_return(msg);
+}
+
+static const GDBusMethodTable cyclingspeed_manager_methods[] = {
+ { GDBUS_METHOD("RegisterWatcher",
+ GDBUS_ARGS({ "agent", "o" }), NULL,
+ register_watcher) },
+ { GDBUS_METHOD("UnregisterWatcher",
+ GDBUS_ARGS({ "agent", "o" }), NULL,
+ unregister_watcher) },
+ { }
+};
+
int csc_adapter_register(struct btd_adapter *adapter)
{
struct csc_adapter *cadapter;
@@ -312,6 +450,18 @@ int csc_adapter_register(struct btd_adapter *adapter)

csc_adapters = g_slist_prepend(csc_adapters, cadapter);

+ if (!g_dbus_register_interface(btd_get_dbus_connection(),
+ adapter_get_path(adapter),
+ CYCLINGSPEED_MANAGER_INTERFACE,
+ cyclingspeed_manager_methods,
+ NULL, NULL, cadapter,
+ destroy_csc_adapter)) {
+ error("D-Bus failed to register %s interface",
+ CYCLINGSPEED_MANAGER_INTERFACE);
+ destroy_csc_adapter(cadapter);
+ return -EIO;
+ }
+
return 0;
}

@@ -325,7 +475,9 @@ void csc_adapter_unregister(struct btd_adapter *adapter)

csc_adapters = g_slist_remove(csc_adapters, cadapter);

- destroy_csc_adapter(cadapter);
+ g_dbus_unregister_interface(btd_get_dbus_connection(),
+ adapter_get_path(cadapter->adapter),
+ CYCLINGSPEED_MANAGER_INTERFACE);
}

int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
--
1.8.0


2012-10-25 14:43:36

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 06/18] cyclingspeed: Read Sensor Location characteristic value

---
profiles/cyclingspeed/cyclingspeed.c | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index fa97911..67963d1 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -56,6 +56,8 @@ struct csc {
uint16_t controlpoint_val_handle;

uint16_t feature;
+ gboolean has_location;
+ uint8_t location;
};

struct characteristic {
@@ -145,6 +147,33 @@ static void read_feature_cb(guint8 status, const guint8 *pdu,
csc->feature = att_get_u16(value);
}

+static void read_location_cb(guint8 status, const guint8 *pdu,
+ guint16 len, gpointer user_data)
+{
+ struct csc *csc = user_data;
+ uint8_t value;
+ ssize_t vlen;
+
+ if (status) {
+ error("Sensor Location read failed: %s", att_ecode2str(status));
+ return;
+ }
+
+ vlen = dec_read_resp(pdu, len, &value, sizeof(value));
+ if (vlen < 0) {
+ error("Protocol error");
+ return;
+ }
+
+ if (vlen != sizeof(value)) {
+ error("Invalid value length for Sensor Location");
+ return;
+ }
+
+ csc->has_location = TRUE;
+ csc->location = value;
+}
+
static void discover_desc_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
@@ -237,7 +266,8 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
feature_val_handle = c->value_handle;
} else if (g_strcmp0(c->uuid, SENSOR_LOCATION_UUID) == 0) {
DBG("Sensor Location supported");
- /* TODO: read characterictic value */
+ gatt_read_char(csc->attrib, c->value_handle,
+ read_location_cb, csc);
} else if (g_strcmp0(c->uuid, SC_CONTROL_POINT_UUID) == 0) {
DBG("SC Control Point supported");
csc->controlpoint_val_handle = c->value_handle;
--
1.8.0


2012-10-25 14:43:35

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 05/18] cyclingspeed: Read CSC Feature characteristic value

---
profiles/cyclingspeed/cyclingspeed.c | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 0191f1b..fa97911 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -54,6 +54,8 @@ struct csc {

uint16_t measurement_ccc_handle;
uint16_t controlpoint_val_handle;
+
+ uint16_t feature;
};

struct characteristic {
@@ -117,6 +119,32 @@ static void destroy_csc(gpointer user_data)
g_free(csc);
}

+static void read_feature_cb(guint8 status, const guint8 *pdu,
+ guint16 len, gpointer user_data)
+{
+ struct csc *csc = user_data;
+ uint8_t value[2];
+ ssize_t vlen;
+
+ if (status) {
+ error("CSC Feature read failed: %s", att_ecode2str(status));
+ return;
+ }
+
+ vlen = dec_read_resp(pdu, len, value, sizeof(value));
+ if (vlen < 0) {
+ error("Protocol error");
+ return;
+ }
+
+ if (vlen != sizeof(value)) {
+ error("Invalid value length for CSC Feature");
+ return;
+ }
+
+ csc->feature = att_get_u16(value);
+}
+
static void discover_desc_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
@@ -190,6 +218,7 @@ static void discover_desc(struct csc *csc, struct gatt_char *c,
static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
{
struct csc *csc = user_data;
+ uint16_t feature_val_handle = 0;

if (status) {
error("Discover CSCS characteristics: %s",
@@ -205,7 +234,7 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
if (g_strcmp0(c->uuid, CSC_MEASUREMENT_UUID) == 0) {
discover_desc(csc, c, c_next);
} else if (g_strcmp0(c->uuid, CSC_FEATURE_UUID) == 0) {
- /* TODO: read characterictic value */
+ feature_val_handle = c->value_handle;
} else if (g_strcmp0(c->uuid, SENSOR_LOCATION_UUID) == 0) {
DBG("Sensor Location supported");
/* TODO: read characterictic value */
@@ -215,6 +244,10 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
discover_desc(csc, c, c_next);
}
}
+
+ if (feature_val_handle > 0)
+ gatt_read_char(csc->attrib, feature_val_handle,
+ read_feature_cb, csc);
}

static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
--
1.8.0


2012-10-25 14:43:33

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 03/18] cyclingspeed: Discover CSCS characteristics

---
lib/uuid.h | 4 ++++
profiles/cyclingspeed/cyclingspeed.c | 43 +++++++++++++++++++++++++++++++++++-
profiles/cyclingspeed/cyclingspeed.h | 2 +-
profiles/cyclingspeed/manager.c | 19 +++++++++++++++-
4 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/lib/uuid.h b/lib/uuid.h
index ebc6d27..def4fea 100644
--- a/lib/uuid.h
+++ b/lib/uuid.h
@@ -75,6 +75,10 @@ extern "C" {
#define MEASUREMENT_INTERVAL_UUID "00002a21-0000-1000-8000-00805f9b34fb"

#define CYCLING_SC_UUID "00001816-0000-1000-8000-00805f9b34fb"
+#define CSC_MEASUREMENT_UUID "00002a5b-0000-1000-8000-00805f9b34fb"
+#define CSC_FEATURE_UUID "00002a5c-0000-1000-8000-00805f9b34fb"
+#define SENSOR_LOCATION_UUID "00002a5d-0000-1000-8000-00805f9b34fb"
+#define SC_CONTROL_POINT_UUID "00002a55-0000-1000-8000-00805f9b34fb"

#define RFCOMM_UUID_STR "00000003-0000-1000-8000-00805f9b34fb"

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 4d98810..5744891 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -49,6 +49,10 @@ struct csc {

GAttrib *attrib;
guint attioid;
+
+ struct att_range *svc_range;
+
+ uint16_t controlpoint_val_handle;
};

static GSList *csc_adapters = NULL;
@@ -103,9 +107,38 @@ static void destroy_csc(gpointer user_data)
g_attrib_unref(csc->attrib);

btd_device_unref(csc->dev);
+ g_free(csc->svc_range);
g_free(csc);
}

+static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
+{
+ struct csc *csc = user_data;
+
+ if (status) {
+ error("Discover CSCS characteristics: %s",
+ att_ecode2str(status));
+ return;
+ }
+
+ for (; chars; chars = chars->next) {
+ struct gatt_char *c = chars->data;
+
+ if (g_strcmp0(c->uuid, CSC_MEASUREMENT_UUID) == 0) {
+ /* TODO: discover CCC handle */
+ } else if (g_strcmp0(c->uuid, CSC_FEATURE_UUID) == 0) {
+ /* TODO: read characterictic value */
+ } else if (g_strcmp0(c->uuid, SENSOR_LOCATION_UUID) == 0) {
+ DBG("Sensor Location supported");
+ /* TODO: read characterictic value */
+ } else if (g_strcmp0(c->uuid, SC_CONTROL_POINT_UUID) == 0) {
+ DBG("SC Control Point supported");
+ csc->controlpoint_val_handle = c->value_handle;
+ /* TODO: discover CCC handle */
+ }
+ }
+}
+
static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
{
struct csc *csc = user_data;
@@ -113,6 +146,10 @@ static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
DBG("");

csc->attrib = g_attrib_ref(attrib);
+
+ gatt_discover_char(csc->attrib, csc->svc_range->start,
+ csc->svc_range->end, NULL,
+ discover_char_cb, csc);
}

static void attio_disconnected_cb(gpointer user_data)
@@ -150,7 +187,7 @@ void csc_adapter_unregister(struct btd_adapter *adapter)
destroy_csc_adapter(cadapter);
}

-int csc_device_register(struct btd_device *device)
+int csc_device_register(struct btd_device *device, struct gatt_primary *prim)
{
struct btd_adapter *adapter;
struct csc_adapter *cadapter;
@@ -166,6 +203,10 @@ int csc_device_register(struct btd_device *device)
csc->dev = btd_device_ref(device);
csc->cadapter = cadapter;

+ csc->svc_range = g_new0(struct att_range, 1);
+ csc->svc_range->start = prim->range.start;
+ csc->svc_range->end = prim->range.end;
+
cadapter->devices = g_slist_prepend(cadapter->devices, csc);

csc->attioid = btd_device_add_attio_callback(device, attio_connected_cb,
diff --git a/profiles/cyclingspeed/cyclingspeed.h b/profiles/cyclingspeed/cyclingspeed.h
index b83fa9e..3400d5f 100644
--- a/profiles/cyclingspeed/cyclingspeed.h
+++ b/profiles/cyclingspeed/cyclingspeed.h
@@ -22,5 +22,5 @@

int csc_adapter_register(struct btd_adapter *adapter);
void csc_adapter_unregister(struct btd_adapter *adapter);
-int csc_device_register(struct btd_device *device);
+int csc_device_register(struct btd_device *device, struct gatt_primary *prim);
void csc_device_unregister(struct btd_device *device);
diff --git a/profiles/cyclingspeed/manager.c b/profiles/cyclingspeed/manager.c
index e5f7553..c147af2 100644
--- a/profiles/cyclingspeed/manager.c
+++ b/profiles/cyclingspeed/manager.c
@@ -34,6 +34,14 @@
#include "cyclingspeed.h"
#include "manager.h"

+static gint primary_uuid_cmp(gconstpointer a, gconstpointer b)
+{
+ const struct gatt_primary *prim = a;
+ const char *uuid = b;
+
+ return g_strcmp0(prim->uuid, uuid);
+}
+
static int csc_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
{
return csc_adapter_register(adapter);
@@ -48,7 +56,16 @@ static void csc_adapter_remove(struct btd_profile *p,
static int csc_device_probe(struct btd_profile *p,
struct btd_device *device, GSList *uuids)
{
- return csc_device_register(device);
+ GSList *primaries;
+ GSList *l;
+
+ primaries = btd_device_get_primaries(device);
+
+ l = g_slist_find_custom(primaries, CYCLING_SC_UUID, primary_uuid_cmp);
+ if (l == NULL)
+ return -EINVAL;
+
+ return csc_device_register(device, l->data);
}

static void csc_device_remove(struct btd_profile *p,
--
1.8.0


2012-10-25 14:43:34

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 04/18] cyclingspeed: Discover characteristics CCC

---
profiles/cyclingspeed/cyclingspeed.c | 82 +++++++++++++++++++++++++++++++++++-
1 file changed, 80 insertions(+), 2 deletions(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 5744891..0191f1b 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -52,9 +52,15 @@ struct csc {

struct att_range *svc_range;

+ uint16_t measurement_ccc_handle;
uint16_t controlpoint_val_handle;
};

+struct characteristic {
+ struct csc *csc;
+ char uuid[MAX_LEN_UUID_STR + 1];
+};
+
static GSList *csc_adapters = NULL;

static gint cmp_adapter(gconstpointer a, gconstpointer b)
@@ -111,6 +117,76 @@ static void destroy_csc(gpointer user_data)
g_free(csc);
}

+static void discover_desc_cb(guint8 status, const guint8 *pdu,
+ guint16 len, gpointer user_data)
+{
+ struct characteristic *ch = user_data;
+ struct att_data_list *list = NULL;
+ uint8_t format;
+ int i;
+
+ if (status != 0) {
+ error("Discover %s descriptors failed: %s", ch->uuid,
+ att_ecode2str(status));
+ goto done;
+ }
+
+ list = dec_find_info_resp(pdu, len, &format);
+ if (list == NULL)
+ goto done;
+
+ if (format != ATT_FIND_INFO_RESP_FMT_16BIT)
+ goto done;
+
+ for (i = 0; i < list->num; i++) {
+ uint8_t *value;
+ uint16_t handle, uuid;
+
+ value = list->data[i];
+ handle = att_get_u16(value);
+ uuid = att_get_u16(value + 2);
+
+ if (uuid != GATT_CLIENT_CHARAC_CFG_UUID)
+ continue;
+
+ if (g_strcmp0(ch->uuid, CSC_MEASUREMENT_UUID) == 0)
+ ch->csc->measurement_ccc_handle = handle;
+
+ /* We only want CCC, can break here */
+ break;
+ }
+
+done:
+ if (list)
+ att_data_list_free(list);
+ g_free(ch);
+}
+
+static void discover_desc(struct csc *csc, struct gatt_char *c,
+ struct gatt_char *c_next)
+{
+ struct characteristic *ch;
+ uint16_t start, end;
+
+ start = c->value_handle + 1;
+
+ if (c_next != NULL) {
+ if (start == c_next->handle)
+ return;
+ end = c_next->handle - 1;
+ } else if (c->value_handle != csc->svc_range->end) {
+ end = csc->svc_range->end;
+ } else {
+ return;
+ }
+
+ ch = g_new0(struct characteristic, 1);
+ ch->csc = csc;
+ memcpy(ch->uuid, c->uuid, sizeof(c->uuid));
+
+ gatt_find_info(csc->attrib, start, end, discover_desc_cb, ch);
+}
+
static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
{
struct csc *csc = user_data;
@@ -123,9 +199,11 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)

for (; chars; chars = chars->next) {
struct gatt_char *c = chars->data;
+ struct gatt_char *c_next =
+ (chars->next ? chars->next->data : NULL);

if (g_strcmp0(c->uuid, CSC_MEASUREMENT_UUID) == 0) {
- /* TODO: discover CCC handle */
+ discover_desc(csc, c, c_next);
} else if (g_strcmp0(c->uuid, CSC_FEATURE_UUID) == 0) {
/* TODO: read characterictic value */
} else if (g_strcmp0(c->uuid, SENSOR_LOCATION_UUID) == 0) {
@@ -134,7 +212,7 @@ static void discover_char_cb(GSList *chars, guint8 status, gpointer user_data)
} else if (g_strcmp0(c->uuid, SC_CONTROL_POINT_UUID) == 0) {
DBG("SC Control Point supported");
csc->controlpoint_val_handle = c->value_handle;
- /* TODO: discover CCC handle */
+ discover_desc(csc, c, c_next);
}
}
}
--
1.8.0


2012-10-25 14:43:32

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 02/18] cyclingspeed: Add attio callbacks

---
profiles/cyclingspeed/cyclingspeed.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 9674a94..4d98810 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -31,6 +31,11 @@

#include "adapter.h"
#include "device.h"
+#include "gattrib.h"
+#include "att.h"
+#include "gatt.h"
+#include "attio.h"
+#include "log.h"
#include "cyclingspeed.h"

struct csc_adapter {
@@ -41,6 +46,9 @@ struct csc_adapter {
struct csc {
struct btd_device *dev;
struct csc_adapter *cadapter;
+
+ GAttrib *attrib;
+ guint attioid;
};

static GSList *csc_adapters = NULL;
@@ -88,10 +96,35 @@ static void destroy_csc(gpointer user_data)
{
struct csc *csc = user_data;

+ if (csc->attioid > 0)
+ btd_device_remove_attio_callback(csc->dev, csc->attioid);
+
+ if (csc->attrib != NULL)
+ g_attrib_unref(csc->attrib);
+
btd_device_unref(csc->dev);
g_free(csc);
}

+static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
+{
+ struct csc *csc = user_data;
+
+ DBG("");
+
+ csc->attrib = g_attrib_ref(attrib);
+}
+
+static void attio_disconnected_cb(gpointer user_data)
+{
+ struct csc *csc = user_data;
+
+ DBG("");
+
+ g_attrib_unref(csc->attrib);
+ csc->attrib = NULL;
+}
+
int csc_adapter_register(struct btd_adapter *adapter)
{
struct csc_adapter *cadapter;
@@ -135,6 +168,9 @@ int csc_device_register(struct btd_device *device)

cadapter->devices = g_slist_prepend(cadapter->devices, csc);

+ csc->attioid = btd_device_add_attio_callback(device, attio_connected_cb,
+ attio_disconnected_cb, csc);
+
return 0;
}

--
1.8.0


2012-10-25 14:43:31

by Andrzej Kaczmarek

[permalink] [raw]
Subject: [RFC 01/18] cyclingspeed: Add CSC profile plugin skeleton

This patch adds stub profile driver plugin for CSC profile.
---
Makefile.am | 9 +-
lib/uuid.h | 2 +
profiles/cyclingspeed/cyclingspeed.c | 163 +++++++++++++++++++++++++++++++++++
profiles/cyclingspeed/cyclingspeed.h | 26 ++++++
profiles/cyclingspeed/main.c | 53 ++++++++++++
profiles/cyclingspeed/manager.c | 79 +++++++++++++++++
profiles/cyclingspeed/manager.h | 24 ++++++
7 files changed, 354 insertions(+), 2 deletions(-)
create mode 100644 profiles/cyclingspeed/cyclingspeed.c
create mode 100644 profiles/cyclingspeed/cyclingspeed.h
create mode 100644 profiles/cyclingspeed/main.c
create mode 100644 profiles/cyclingspeed/manager.c
create mode 100644 profiles/cyclingspeed/manager.h

diff --git a/Makefile.am b/Makefile.am
index 35b1520..9bdfd71 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -212,7 +212,7 @@ endif

if GATTMODULES
builtin_modules += thermometer alert time gatt_example proximity deviceinfo \
- gatt scanparam heartrate
+ gatt scanparam heartrate cyclingspeed
builtin_sources += profiles/thermometer/main.c \
profiles/thermometer/manager.h \
profiles/thermometer/manager.c \
@@ -251,7 +251,12 @@ builtin_sources += profiles/thermometer/main.c \
profiles/heartrate/manager.c \
profiles/heartrate/manager.h \
profiles/heartrate/heartrate.c \
- profiles/heartrate/heartrate.h
+ profiles/heartrate/heartrate.h \
+ profiles/cyclingspeed/main.c \
+ profiles/cyclingspeed/manager.c \
+ profiles/cyclingspeed/manager.h \
+ profiles/cyclingspeed/cyclingspeed.c \
+ profiles/cyclingspeed/cyclingspeed.h

endif

diff --git a/lib/uuid.h b/lib/uuid.h
index a812309..ebc6d27 100644
--- a/lib/uuid.h
+++ b/lib/uuid.h
@@ -74,6 +74,8 @@ extern "C" {
#define INTERMEDIATE_TEMPERATURE_UUID "00002a1e-0000-1000-8000-00805f9b34fb"
#define MEASUREMENT_INTERVAL_UUID "00002a21-0000-1000-8000-00805f9b34fb"

+#define CYCLING_SC_UUID "00001816-0000-1000-8000-00805f9b34fb"
+
#define RFCOMM_UUID_STR "00000003-0000-1000-8000-00805f9b34fb"

#define HDP_UUID "00001400-0000-1000-8000-00805f9b34fb"
diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
new file mode 100644
index 0000000..9674a94
--- /dev/null
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -0,0 +1,163 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Tieto Poland
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <stdbool.h>
+#include <glib.h>
+#include <bluetooth/uuid.h>
+
+#include "adapter.h"
+#include "device.h"
+#include "cyclingspeed.h"
+
+struct csc_adapter {
+ struct btd_adapter *adapter;
+ GSList *devices; /* list of registered devices */
+};
+
+struct csc {
+ struct btd_device *dev;
+ struct csc_adapter *cadapter;
+};
+
+static GSList *csc_adapters = NULL;
+
+static gint cmp_adapter(gconstpointer a, gconstpointer b)
+{
+ const struct csc_adapter *cadapter = a;
+ const struct btd_adapter *adapter = b;
+
+ if (adapter == cadapter->adapter)
+ return 0;
+
+ return -1;
+}
+
+static gint cmp_device(gconstpointer a, gconstpointer b)
+{
+ const struct csc *csc = a;
+ const struct btd_device *dev = b;
+
+ if (dev == csc->dev)
+ return 0;
+
+ return -1;
+}
+
+static struct csc_adapter *find_csc_adapter(struct btd_adapter *adapter)
+{
+ GSList *l = g_slist_find_custom(csc_adapters, adapter, cmp_adapter);
+
+ if (!l)
+ return NULL;
+
+ return l->data;
+}
+
+static void destroy_csc_adapter(gpointer user_data)
+{
+ struct csc_adapter *cadapter = user_data;
+
+ g_free(cadapter);
+}
+
+static void destroy_csc(gpointer user_data)
+{
+ struct csc *csc = user_data;
+
+ btd_device_unref(csc->dev);
+ g_free(csc);
+}
+
+int csc_adapter_register(struct btd_adapter *adapter)
+{
+ struct csc_adapter *cadapter;
+
+ cadapter = g_new0(struct csc_adapter, 1);
+ cadapter->adapter = adapter;
+
+ csc_adapters = g_slist_prepend(csc_adapters, cadapter);
+
+ return 0;
+}
+
+void csc_adapter_unregister(struct btd_adapter *adapter)
+{
+ struct csc_adapter *cadapter;
+
+ cadapter = find_csc_adapter(adapter);
+ if (cadapter == NULL)
+ return;
+
+ csc_adapters = g_slist_remove(csc_adapters, cadapter);
+
+ destroy_csc_adapter(cadapter);
+}
+
+int csc_device_register(struct btd_device *device)
+{
+ struct btd_adapter *adapter;
+ struct csc_adapter *cadapter;
+ struct csc *csc;
+
+ adapter = device_get_adapter(device);
+
+ cadapter = find_csc_adapter(adapter);
+ if (cadapter == NULL)
+ return -1;
+
+ csc = g_new0(struct csc, 1);
+ csc->dev = btd_device_ref(device);
+ csc->cadapter = cadapter;
+
+ cadapter->devices = g_slist_prepend(cadapter->devices, csc);
+
+ return 0;
+}
+
+void csc_device_unregister(struct btd_device *device)
+{
+ struct btd_adapter *adapter;
+ struct csc_adapter *cadapter;
+ struct csc *csc;
+ GSList *l;
+
+ adapter = device_get_adapter(device);
+
+ cadapter = find_csc_adapter(adapter);
+ if (cadapter == NULL)
+ return;
+
+ l = g_slist_find_custom(cadapter->devices, device, cmp_device);
+ if (l == NULL)
+ return;
+
+ csc = l->data;
+
+ cadapter->devices = g_slist_remove(cadapter->devices, csc);
+
+ destroy_csc(csc);
+}
diff --git a/profiles/cyclingspeed/cyclingspeed.h b/profiles/cyclingspeed/cyclingspeed.h
new file mode 100644
index 0000000..b83fa9e
--- /dev/null
+++ b/profiles/cyclingspeed/cyclingspeed.h
@@ -0,0 +1,26 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Tieto Poland
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+int csc_adapter_register(struct btd_adapter *adapter);
+void csc_adapter_unregister(struct btd_adapter *adapter);
+int csc_device_register(struct btd_device *device);
+void csc_device_unregister(struct btd_device *device);
diff --git a/profiles/cyclingspeed/main.c b/profiles/cyclingspeed/main.c
new file mode 100644
index 0000000..c2ef765
--- /dev/null
+++ b/profiles/cyclingspeed/main.c
@@ -0,0 +1,53 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Tieto Poland
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdint.h>
+#include <glib.h>
+#include <errno.h>
+
+#include "plugin.h"
+#include "manager.h"
+#include "hcid.h"
+#include "log.h"
+
+static int cyclingspeed_init(void)
+{
+ if (!main_opts.gatt_enabled) {
+ DBG("GATT is disabled");
+ return -ENOTSUP;
+ }
+
+ return cyclingspeed_manager_init();
+}
+
+static void cyclingspeed_exit(void)
+{
+ cyclingspeed_manager_exit();
+}
+
+BLUETOOTH_PLUGIN_DEFINE(cyclingspeed, VERSION,
+ BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
+ cyclingspeed_init, cyclingspeed_exit)
diff --git a/profiles/cyclingspeed/manager.c b/profiles/cyclingspeed/manager.c
new file mode 100644
index 0000000..e5f7553
--- /dev/null
+++ b/profiles/cyclingspeed/manager.c
@@ -0,0 +1,79 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Tieto Poland
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <gdbus.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <bluetooth/uuid.h>
+
+#include "adapter.h"
+#include "device.h"
+#include "profile.h"
+#include "att.h"
+#include "gattrib.h"
+#include "gatt.h"
+#include "cyclingspeed.h"
+#include "manager.h"
+
+static int csc_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
+{
+ return csc_adapter_register(adapter);
+}
+
+static void csc_adapter_remove(struct btd_profile *p,
+ struct btd_adapter *adapter)
+{
+ csc_adapter_unregister(adapter);
+}
+
+static int csc_device_probe(struct btd_profile *p,
+ struct btd_device *device, GSList *uuids)
+{
+ return csc_device_register(device);
+}
+
+static void csc_device_remove(struct btd_profile *p,
+ struct btd_device *device)
+{
+ csc_device_unregister(device);
+}
+
+static struct btd_profile cscp_profile = {
+ .name = "Cycling Speed and Cadence GATT Driver",
+ .remote_uuids = BTD_UUIDS(CYCLING_SC_UUID),
+
+ .adapter_probe = csc_adapter_probe,
+ .adapter_remove = csc_adapter_remove,
+
+ .device_probe = csc_device_probe,
+ .device_remove = csc_device_remove,
+};
+
+int cyclingspeed_manager_init(void)
+{
+ return btd_profile_register(&cscp_profile);
+}
+
+void cyclingspeed_manager_exit(void)
+{
+ btd_profile_unregister(&cscp_profile);
+}
diff --git a/profiles/cyclingspeed/manager.h b/profiles/cyclingspeed/manager.h
new file mode 100644
index 0000000..7d25ae4
--- /dev/null
+++ b/profiles/cyclingspeed/manager.h
@@ -0,0 +1,24 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012 Tieto Poland
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+int cyclingspeed_manager_init(void);
+void cyclingspeed_manager_exit(void);
--
1.8.0