diff options
author | Matt Ranostay <matt.ranostay@konsulko.com> | 2019-04-04 17:21:59 -0700 |
---|---|---|
committer | Matt Ranostay <matt.ranostay@konsulko.com> | 2019-05-05 00:30:29 -0700 |
commit | 91e1d0697da98971ab6375bfd745ed158b7b7185 (patch) | |
tree | 849dc9a8c6143db342e0cde988b37f2c513379d6 | |
parent | 967b96853fa13a206c959ecd536416151313c63d (diff) |
binding: bluetooth-map: add initial MAP binding
This patchset brings initial Bluetooth MAP (Message Access Profile)
support.
Bug-AGL: SPEC-2351
Change-Id: I76b974978f72869f593526c4f6926bb5c27c48a9
Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
-rw-r--r-- | CMakeLists.txt | 21 | ||||
-rw-r--r-- | LICENSE | 204 | ||||
-rw-r--r-- | README.md | 52 | ||||
-rwxr-xr-x | autobuild/agl/autobuild | 79 | ||||
-rwxr-xr-x | autobuild/linux/autobuild | 79 | ||||
-rw-r--r-- | binding/CMakeLists.txt | 35 | ||||
-rw-r--r-- | binding/bluetooth-map-api.c | 538 | ||||
-rw-r--r-- | binding/bluetooth-map-api.h | 146 | ||||
-rw-r--r-- | binding/bluetooth-map-bluez.c | 481 | ||||
-rw-r--r-- | binding/bluetooth-map-common.h | 163 | ||||
-rw-r--r-- | conf.d/cmake/config.cmake | 164 | ||||
-rw-r--r-- | conf.d/wgt/config.xml.in | 24 |
12 files changed, 1986 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b485097 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Romain Forlot <romain.forlot@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + +CMAKE_MINIMUM_REQUIRED(VERSION 3.3) + +include(${CMAKE_CURRENT_SOURCE_DIR}/conf.d/cmake/config.cmake) @@ -0,0 +1,204 @@ + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don`t include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c382e18 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# Bluetooth MAP (Message Service Profile) Service + +## Overview + +Bluetooth MAP (Message Access Profile) service uses the respective profile support from BlueZ +to enable message notifications from SMS/email/etc. + +## Verbs + +| Name | Description | JSON Response | +|-------------------|------------------------------------------|-------------------------------------------| +| subscribe | subscribe to MAP service events | *Request:* {"value": "notification"} | +| unsubscribe | unsubscribe to MAP service events | *Request:* {"value": "notification"} | + +## Events + +| Name | Description | JSON Event Data | +|-------------------|------------------------------------------|-------------------------------------------| +| notification | report notification message | see **notification event** section | + + +### notification event + +<pre> +{ + "bmessage": + "BEGIN:BMSG\r\n + VERSION:1.0\r\n + STATUS:UNREAD\r\n + TYPE:SMS_GSM\r\n + FOLDER:telecom/msg/inbox\r\n + NOTIFICATION:1\r\n + BEGIN:VCARD\r\n + VERSION:2.1\r\n + FN;CHARSET=UTF-8:Satoshi Nakamoto\r\n + N;CHARSET=UTF-8:Satoshi\r\n + TEL:\r\n + END:VCARD\r\n + BEGIN:BENV\r\n + BEGIN:BBODY\r\n + CHARSET:UTF-8\r\n + LANGUAGE:UNKNOWN\r\n + LENGTH:46\r\n + BEGIN:MSG\r\n + Meet at Victor 23 at 6p? + END:MSG\r\n + END:BBODY\r\n + END:BENV\r\n + END:BMSG\r\n + " +} +</pre> diff --git a/autobuild/agl/autobuild b/autobuild/agl/autobuild new file mode 100755 index 0000000..db00c1a --- /dev/null +++ b/autobuild/agl/autobuild @@ -0,0 +1,79 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Author "Romain Forlot" <romain.forlot@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +BUILD_DIR := $(abspath $(dir $(THISFILE))/../../build) +DEST := ${BUILD_DIR} + +.PHONY: all clean distclean configure build package help update + +all: help + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in your ${CMAKE_INSTALL_DIR} directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +update: configure + @cmake --build ${BUILD_DIR} --target autobuild + +clean: + @([ -d ${BUILD_DIR} ] && make -C ${BUILD_DIR} ${CLEAN_ARGS} clean) || echo Nothing to clean + +distclean: + @rm -rf ${BUILD_DIR} + +configure: + @[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR} + @[ -f ${BUILD_DIR}/Makefile ] || (cd ${BUILD_DIR} && cmake ${CONFIGURE_ARGS} ..) + +build: configure + @cmake --build ${BUILD_DIR} ${BUILD_ARGS} --target all + +package: build + @mkdir -p ${BUILD_DIR}/$@/bin + @mkdir -p ${BUILD_DIR}/$@/etc + @mkdir -p ${BUILD_DIR}/$@/lib + @mkdir -p ${BUILD_DIR}/$@/htdocs + @mkdir -p ${BUILD_DIR}/$@/var + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target widget + @if [ "${DEST}" != "${BUILD_DIR}" ]; then \ + mkdir -p ${DEST} && cp ${BUILD_DIR}/*.wgt ${DEST}; \ + fi + +package-test: build + @mkdir -p ${BUILD_DIR}/$@/bin + @mkdir -p ${BUILD_DIR}/$@/etc + @mkdir -p ${BUILD_DIR}/$@/lib + @mkdir -p ${BUILD_DIR}/$@/htdocs + @mkdir -p ${BUILD_DIR}/$@/var + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target widget + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target test_widget + @if [ "${DEST}" != "${BUILD_DIR}" ]; then \ + mkdir -p ${DEST} && cp ${BUILD_DIR}/*.wgt ${DEST}; \ + fi + +install: build + @cmake --build ${BUILD_DIR} ${INSTALL_ARGS} --target install diff --git a/autobuild/linux/autobuild b/autobuild/linux/autobuild new file mode 100755 index 0000000..db00c1a --- /dev/null +++ b/autobuild/linux/autobuild @@ -0,0 +1,79 @@ +#!/usr/bin/make -f +# Copyright (C) 2015 - 2018 "IoT.bzh" +# Author "Romain Forlot" <romain.forlot@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +THISFILE := $(lastword $(MAKEFILE_LIST)) +BUILD_DIR := $(abspath $(dir $(THISFILE))/../../build) +DEST := ${BUILD_DIR} + +.PHONY: all clean distclean configure build package help update + +all: help + +help: + @echo "List of targets available:" + @echo "" + @echo "- all" + @echo "- clean" + @echo "- distclean" + @echo "- configure" + @echo "- build: compilation, link and prepare files for package into a widget" + @echo "- package: output a widget file '*.wgt'" + @echo "- install: install in your ${CMAKE_INSTALL_DIR} directory" + @echo "" + @echo "Usage: ./autobuild/agl/autobuild package DEST=${HOME}/opt" + @echo "Don't use your build dir as DEST as wgt file is generated at this location" + +update: configure + @cmake --build ${BUILD_DIR} --target autobuild + +clean: + @([ -d ${BUILD_DIR} ] && make -C ${BUILD_DIR} ${CLEAN_ARGS} clean) || echo Nothing to clean + +distclean: + @rm -rf ${BUILD_DIR} + +configure: + @[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR} + @[ -f ${BUILD_DIR}/Makefile ] || (cd ${BUILD_DIR} && cmake ${CONFIGURE_ARGS} ..) + +build: configure + @cmake --build ${BUILD_DIR} ${BUILD_ARGS} --target all + +package: build + @mkdir -p ${BUILD_DIR}/$@/bin + @mkdir -p ${BUILD_DIR}/$@/etc + @mkdir -p ${BUILD_DIR}/$@/lib + @mkdir -p ${BUILD_DIR}/$@/htdocs + @mkdir -p ${BUILD_DIR}/$@/var + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target widget + @if [ "${DEST}" != "${BUILD_DIR}" ]; then \ + mkdir -p ${DEST} && cp ${BUILD_DIR}/*.wgt ${DEST}; \ + fi + +package-test: build + @mkdir -p ${BUILD_DIR}/$@/bin + @mkdir -p ${BUILD_DIR}/$@/etc + @mkdir -p ${BUILD_DIR}/$@/lib + @mkdir -p ${BUILD_DIR}/$@/htdocs + @mkdir -p ${BUILD_DIR}/$@/var + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target widget + @cmake --build ${BUILD_DIR} ${PACKAGE_ARGS} --target test_widget + @if [ "${DEST}" != "${BUILD_DIR}" ]; then \ + mkdir -p ${DEST} && cp ${BUILD_DIR}/*.wgt ${DEST}; \ + fi + +install: build + @cmake --build ${BUILD_DIR} ${INSTALL_ARGS} --target install diff --git a/binding/CMakeLists.txt b/binding/CMakeLists.txt new file mode 100644 index 0000000..6c137e9 --- /dev/null +++ b/binding/CMakeLists.txt @@ -0,0 +1,35 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll <fulup@iot.bzh> +# contrib: Romain Forlot <romain.forlot@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + +# Add target to project dependency list +PROJECT_TARGET_ADD(bluetooth-map-binding) + + # Define project Targets + add_library(bluetooth-map-binding MODULE bluetooth-map-api.c bluetooth-map-bluez.c) + + # Binder exposes a unique public entry point + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + PREFIX "afb-" + LABELS "BINDING" + LINK_FLAGS ${BINDINGS_LINK_FLAG} + OUTPUT_NAME ${TARGET_NAME} + ) + + # Library dependencies (include updates automatically) + TARGET_LINK_LIBRARIES(${TARGET_NAME} ${link_libraries}) diff --git a/binding/bluetooth-map-api.c b/binding/bluetooth-map-api.c new file mode 100644 index 0000000..798c9fa --- /dev/null +++ b/binding/bluetooth-map-api.c @@ -0,0 +1,538 @@ +/* + * Copyright (C) 2019 Konsulko Group + * Author: Matt Ranostay <matt.ranostay@konsulko.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <time.h> + +#include <glib.h> +#include <gio/gio.h> +#include <json-c/json.h> + +#define AFB_BINDING_VERSION 3 +#include <afb/afb-binding.h> + +#include "bluetooth-map-api.h" +#include "bluetooth-map-common.h" + +/** + * The global thread + */ +static GThread *global_thread; + +static void signal_init_done(struct init_data *id, int rc) +{ + g_mutex_lock(&id->mutex); + id->init_done = TRUE; + id->rc = rc; + g_cond_signal(&id->cond); + g_mutex_unlock(&id->mutex); +} + +struct map_state *map_get_userdata(afb_req_t request) { + afb_api_t api = afb_req_get_api(request); + return afb_api_get_userdata(api); +} + +void call_work_lock(struct map_state *ns) +{ + g_mutex_lock(&ns->cw_mutex); +} + +void call_work_unlock(struct map_state *ns) +{ + g_mutex_unlock(&ns->cw_mutex); +} + +static afb_event_t get_event_from_value(struct map_state *ns, + const char *value) +{ + if (!g_strcmp0(value, "notification")) + return ns->notification_event; + + return NULL; +} + +static void map_subscribe_unsubscribe(afb_req_t request, + gboolean unsub) +{ + struct map_state *ns = map_get_userdata(request); + json_object *jresp = json_object_new_object(); + const char *value; + afb_event_t event; + int rc; + + /* if value exists means to set offline mode */ + value = afb_req_value(request, "value"); + if (!value) { + afb_req_fail_f(request, "failed", "Missing \"value\" event"); + return; + } + + event = get_event_from_value(ns, value); + if (!event) { + afb_req_fail_f(request, "failed", "Bad \"value\" event \"%s\"", + value); + return; + } + + if (!unsub) { + rc = afb_req_subscribe(request, event); + } else { + rc = afb_req_unsubscribe(request, event); + } + if (rc != 0) { + afb_req_fail_f(request, "failed", + "%s error on \"value\" event \"%s\"", + !unsub ? "subscribe" : "unsubscribe", + value); + return; + } + + afb_req_success_f(request, jresp, "Bluetooth MAP %s to event \"%s\"", + !unsub ? "subscribed" : "unsubscribed", + value); +} + +static void subscribe(afb_req_t request) +{ + map_subscribe_unsubscribe(request, FALSE); +} + +static void unsubscribe(afb_req_t request) +{ + map_subscribe_unsubscribe(request, TRUE); +} + +static void map_request_message(struct map_state *ns, const gchar *path) +{ + GVariant *params = + g_variant_new("(&sb)", "", g_variant_new_boolean(FALSE)); + bluez_call(ns, BLUEZ_AT_MESSAGE, path, "Get", params, NULL); +} + +static void map_notification_event(struct map_state *ns, gchar *filename) +{ + json_object *jresp; + gchar *buf; + + if (!g_file_get_contents(filename, &buf, NULL, NULL)) + return; + + jresp = json_object_new_object(); + json_object_object_add(jresp, "bmessage", json_object_new_string(buf)); + + afb_event_push(ns->notification_event, jresp); + g_free(buf); +} + +static void bluez_map_signal_callback( + GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + struct map_state *ns = user_data; + GVariant *var = NULL; + const gchar *path = NULL; + const gchar *key = NULL; + GVariantIter *array = NULL, *array1 = NULL; + + /* AFB_INFO("sender=%s", sender_name); + AFB_INFO("object_path=%s", object_path); + AFB_INFO("interface=%s", interface_name); + AFB_INFO("signal=%s", signal_name); */ + + if (!g_strcmp0(signal_name, "InterfacesAdded")) { + + g_variant_get(parameters, "(&oa{sa{sv}})", &path, &array); + + while (g_variant_iter_next(array, "{&s@a{sv}}", &key, &var)) { + const char *name = NULL; + GVariant *val = NULL; + + if (!g_strcmp0(key, BLUEZ_OBEX_MESSAGE_INTERFACE)) { + map_request_message(ns, path); + continue; + } + + if (g_strcmp0(key, BLUEZ_OBEX_TRANSFER_INTERFACE)) + continue; + + array1 = g_variant_iter_new(var); + + while (g_variant_iter_next(array1, "{&sv}", &name, &val)) { + if (g_strcmp0(name, "Filename")) + continue; + + call_work_lock(ns); + g_hash_table_insert(ns->xfer_queue, g_strdup(path), + g_strdup(g_variant_get_string(val, NULL))); + call_work_unlock(ns); + break; + } + } + + } else if (!g_strcmp0(signal_name, "PropertiesChanged")) { + + g_variant_get(parameters, "(&sa{sv}as)", &path, &array, &array1); + + if (g_strcmp0(path, BLUEZ_OBEX_TRANSFER_INTERFACE)) + return; + + while (g_variant_iter_next(array, "{&sv}", &key, &var)) { + gchar *filename; + + // only check Status field + if (g_strcmp0(key, "Status")) + continue; + + // only need the "complete" Status + if (g_strcmp0(g_variant_get_string(var, NULL), "complete")) + return; + + call_work_lock(ns); + filename = (gchar *) g_hash_table_lookup(ns->xfer_queue, object_path); + if (filename) { + g_hash_table_remove(ns->xfer_queue, object_path); + call_work_unlock(ns); + + map_notification_event(ns, filename); + g_free(filename); + break; + } + call_work_unlock(ns); + } + } +} + +static struct map_state *map_init(GMainLoop *loop) +{ + struct map_state *ns; + GError *error = NULL; + + ns = g_try_malloc0(sizeof(*ns)); + if (!ns) { + AFB_ERROR("out of memory allocating map state"); + goto err_no_ns; + } + + AFB_INFO("connecting to dbus"); + + ns->loop = loop; + ns->conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error); + if (!ns->conn) { + if (error) + g_dbus_error_strip_remote_error(error); + AFB_ERROR("Cannot connect to D-Bus, %s", + error ? error->message : "unspecified"); + g_error_free(error); + goto err_no_conn; + + } + + AFB_INFO("connected to dbus"); + + ns->notification_event = + afb_daemon_make_event("notification"); + + if (!afb_event_is_valid(ns->notification_event)) { + AFB_ERROR("Cannot create events"); + goto err_no_events; + } + + ns->xfer_queue = g_hash_table_new(g_str_hash, g_str_equal); + ns->message_sub = g_dbus_connection_signal_subscribe( + ns->conn, + BLUEZ_OBEX_SERVICE, + NULL, /* interface */ + NULL, /* member */ + NULL, /* object path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + bluez_map_signal_callback, + ns, + NULL); + if (!ns->message_sub) { + AFB_ERROR("Unable to subscribe to interface signals"); + goto err_no_message_sub; + } + + g_mutex_init(&ns->cw_mutex); + ns->next_cw_id = 1; + + return ns; + +err_no_message_sub: + /* no way to clear the events */ +err_no_events: + g_dbus_connection_close(ns->conn, NULL, NULL, NULL); +err_no_conn: + g_free(ns); +err_no_ns: + return NULL; +} + +static void map_cleanup(struct map_state *ns) +{ + g_dbus_connection_signal_unsubscribe(ns->conn, ns->message_sub); + g_dbus_connection_close(ns->conn, NULL, NULL, NULL); + g_free(ns); +} + +static gpointer map_func(gpointer ptr) +{ + struct init_data *id = ptr; + struct map_state *ns; + GMainLoop *loop; + + loop = g_main_loop_new(NULL, FALSE); + if (!loop) { + AFB_ERROR("Unable to create main loop"); + goto err_no_loop; + } + + /* real map init */ + ns = map_init(loop); + if (!ns) { + AFB_ERROR("map_init() failed"); + goto err_no_ns; + } + + id->ns = ns; + ns->loop = loop; + + afb_api_set_userdata(id->api, ns); + signal_init_done(id, 0); + + g_main_loop_run(loop); + g_main_loop_unref(ns->loop); + + map_cleanup(ns); + afb_api_set_userdata(id->api, NULL); + + return NULL; + +err_no_ns: + g_main_loop_unref(loop); + +err_no_loop: + return NULL; +} + +static gboolean map_create_session(struct map_state *ns, const char *address) +{ + GVariant *reply, *params, *dict; + GVariantBuilder builder; + gchar *val; + + g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(&builder, "{sv}", "Target", g_variant_new_string("map")); + dict = g_variant_builder_end(&builder); + params = g_variant_new("(&s@a{sv})", address, dict); + + reply = bluez_call(ns, BLUEZ_AT_CLIENT, NULL, "CreateSession", params, NULL); + if (!reply) { + AFB_ERROR("Cannot open MAP OBEX session"); + return FALSE; + } + + call_work_lock(ns); + + g_variant_get(reply, "(&o)", &val); + ns->session_path = g_strdup(val); + + call_work_unlock(ns); + + g_variant_unref(reply); + + return TRUE; +} + +static gboolean is_map_dev_and_init(struct map_state *ns, struct json_object *dev) +{ + struct json_object *props = NULL, *val = NULL; + int i; + + json_object_object_get_ex(dev, "properties", &props); + if (!props) + return FALSE; + + json_object_object_get_ex(props, "connected", &val); + if (!val || !json_object_get_boolean(val)) + return FALSE; + + json_object_object_get_ex(props, "uuids", &val); + for (i = 0; i < json_object_array_length(val); i++) { + const char *uuid = json_object_get_string(json_object_array_get_idx(val, i)); + const char *address = NULL; + struct json_object *val1 = NULL; + + if (g_strcmp0(MAP_UUID, uuid)) + continue; + + json_object_object_get_ex(props, "address", &val1); + address = json_object_get_string(val1); + + if (!address) + return FALSE; + + if (map_create_session(ns, address)) { + json_object_object_get_ex(dev, "device", &val1); + AFB_NOTICE("MAP device connected: %s", json_object_get_string(val1)); + + return TRUE; + } + break; + } + + return FALSE; +} + +static void discovery_result_cb(void *closure, struct json_object *result, + const char *error, const char *info, + afb_api_t api) +{ + struct map_state *ns = afb_api_get_userdata(api); + enum json_type type; + struct json_object *dev, *tmp; + int i; + + if (!json_object_object_get_ex(result, "devices", &tmp)) + return; + type = json_object_get_type(tmp); + + if (type != json_type_array) + return; + + for (i = 0; i < json_object_array_length(tmp); i++) { + dev = json_object_array_get_idx(tmp, i); + if (is_map_dev_and_init(ns, dev)) + return; + } +} + +static void process_connection_event(afb_api_t api, struct json_object *object) +{ + struct json_object *val = NULL, *props = NULL; + const char *action, *device; + + json_object_object_get_ex(object, "action", &val); + if (!val) + return; + action = json_object_get_string(val); + if (g_strcmp0("changed", action)) + return; + + json_object_object_get_ex(object, "properties", &props); + if (!props) + return; + + json_object_object_get_ex(props, "connected", &val); + if (!val) + return; + + if (json_object_get_boolean(val)) { + struct json_object *args = json_object_new_object(); + afb_api_call(api, "Bluetooth-Manager", "managed_objects", + args, discovery_result_cb, NULL); + return; + } + + json_object_object_get_ex(object, "device", &val); + if (!val) + return; + + device = json_object_get_string(val); + + AFB_NOTICE("MAP device disconnected: %s", device); +} + +static int init(afb_api_t api) +{ + struct init_data init_data, *id = &init_data; + json_object *args; + gint64 end_time; + int ret; + + memset(id, 0, sizeof(*id)); + id->init_done = FALSE; + id->rc = 0; + id->api = api; + g_cond_init(&id->cond); + g_mutex_init(&id->mutex); + + ret = afb_daemon_require_api("Bluetooth-Manager", 1); + if (ret) { + AFB_ERROR("unable to initialize bluetooth binding"); + return -EINVAL; + } + + global_thread = g_thread_new("agl-service-bluetooth-map", map_func, id); + + AFB_INFO("bluetooth-map binding waiting for init done"); + + /* wait maximum 3 seconds for init done */ + end_time = g_get_monotonic_time () + 3 * G_TIME_SPAN_SECOND; + g_mutex_lock(&id->mutex); + while (!id->init_done) { + if (!g_cond_wait_until(&id->cond, &id->mutex, end_time)) + break; + } + g_mutex_unlock(&id->mutex); + + /* subscribe to Bluetooth-Manager events */ + args = json_object_new_object(); + json_object_object_add(args , "value", json_object_new_string("device_changes")); + afb_api_call_sync(api, "Bluetooth-Manager", "subscribe", args, NULL, NULL, NULL); + + args = json_object_new_object(); + afb_api_call(api, "Bluetooth-Manager", "managed_objects", args, discovery_result_cb, NULL); + + return id->rc; +} + +static void onevent(afb_api_t api, const char *event, struct json_object *object) +{ + if (!g_ascii_strcasecmp(event, "Bluetooth-Manager/device_changes")) + process_connection_event(api, object); + else + AFB_ERROR("Unsupported event: %s\n", event); +} + +static const afb_verb_t binding_verbs[] = { + { .verb = "subscribe", .callback = subscribe, .info = "Subscribe to events" }, + { .verb = "unsubscribe",.callback = unsubscribe,.info = "Unsubscribe to events" }, + {} +}; + +/* + * description of the binding for afb-daemon + */ +const afb_binding_t afbBindingV3 = { + .api = "bluetooth-map", + .verbs = binding_verbs, + .init = init, + .onevent = onevent, +}; diff --git a/binding/bluetooth-map-api.h b/binding/bluetooth-map-api.h new file mode 100644 index 0000000..906c360 --- /dev/null +++ b/binding/bluetooth-map-api.h @@ -0,0 +1,146 @@ +/* + * Copyright 2019 Konsulko Group + * Author: Matt Ranostay <matt.ranostay@konsulko.com> + * Author: Pantelis Antoniou <pantelis.antoniou@konsulko.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BLUETOOTH_MAP_API_H +#define BLUETOOTH_MAP_API_H + +#include <stdio.h> +#include <stddef.h> +#include <string.h> +#include <glib.h> +#include <json-c/json.h> + +#define BLUEZ_OBEX_SERVICE "org.bluez.obex" +#define BLUEZ_OBEX_CLIENT_INTERFACE BLUEZ_OBEX_SERVICE ".Client1" +#define BLUEZ_OBEX_SESSION_INTERFACE BLUEZ_OBEX_SERVICE ".Session1" +#define BLUEZ_OBEX_TRANSFER_INTERFACE BLUEZ_OBEX_SERVICE ".Transfer1" +#define BLUEZ_OBEX_MESSAGE_INTERFACE BLUEZ_OBEX_SERVICE ".Message1" + +#define BLUEZ_OBJECT_PATH "/" +#define BLUEZ_OBEX_PATH "/org/bluez/obex" + +#define BLUEZ_ERRMSG(error) \ + (error ? error->message : "unspecified") + +#define FREEDESKTOP_INTROSPECT "org.freedesktop.DBus.Introspectable" +#define FREEDESKTOP_PROPERTIES "org.freedesktop.DBus.Properties" +#define FREEDESKTOP_OBJECTMANAGER "org.freedesktop.DBus.ObjectManager" + +#define DBUS_REPLY_TIMEOUT (120 * 1000) +#define DBUS_REPLY_TIMEOUT_SHORT (10 * 1000) + +#define BLUEZ_AT_OBJECT "object" +#define BLUEZ_AT_CLIENT "client" +#define BLUEZ_AT_SESSION "session" +#define BLUEZ_AT_TRANSFER "transfer" +#define BLUEZ_AT_MESSAGE "message" + +struct map_state; + +struct map_state *map_get_userdata(afb_req_t request); + +struct call_work *call_work_create_unlocked(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, const char *bluez_method, + GError **error); + +void call_work_destroy_unlocked(struct call_work *cw); + +void call_work_lock(struct map_state *ns); + +void call_work_unlock(struct map_state *ns); + +struct call_work *call_work_lookup_unlocked( + struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method); + +const struct property_info *bluez_get_property_info( + const char *access_type, GError **error); + +gboolean bluez_property_dbus2json(const char *access_type, + json_object *jprop, const gchar *key, GVariant *var, + gboolean *is_config, + GError **error); + +GVariant *bluez_call(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, GVariant *params, GError **error); + +json_object *bluez_get_properties(struct map_state *ns, + const char *access_type, const char *path, + GError **error); + +json_object *bluez_get_property(struct map_state *ns, + const char *access_type, const char *type_arg, + gboolean is_json_name, const char *name, GError **error); + +gboolean bluez_set_property(struct map_state *ns, + const char *access_type, const char *type_arg, + gboolean is_json_name, const char *name, json_object *jval, + GError **error); + +gboolean bluetooth_autoconnect(gpointer data); + +/* convenience access methods */ +static inline gboolean session_property_dbus2json(json_object *jprop, + const gchar *key, GVariant *var, gboolean *is_config, + GError **error) +{ + return bluez_property_dbus2json(BLUEZ_AT_SESSION, + jprop, key, var, is_config, error); +} + +static inline gboolean transfer_property_dbus2json(json_object *jprop, + const gchar *key, GVariant *var, gboolean *is_config, + GError **error) +{ + return bluez_property_dbus2json(BLUEZ_AT_TRANSFER, + jprop, key, var, is_config, error); +} + +static inline json_object *object_properties(struct map_state *ns, + GError **error) +{ + return bluez_get_properties(ns, + BLUEZ_AT_OBJECT, BLUEZ_OBJECT_PATH, error); +} + +struct bluez_pending_work { + struct map_state *ns; + void *user_data; + GCancellable *cancel; + void (*callback)(void *user_data, GVariant *result, GError **error); +}; + +void bluez_cancel_call(struct map_state *ns, + struct bluez_pending_work *cpw); + +struct bluez_pending_work * +bluez_call_async(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, GVariant *params, GError **error, + void (*callback)(void *user_data, GVariant *result, GError **error), + void *user_data); + +void bluez_decode_call_error(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, + GError **error); + +#endif /* BLUETOOTH_MAP_API_H */ diff --git a/binding/bluetooth-map-bluez.c b/binding/bluetooth-map-bluez.c new file mode 100644 index 0000000..6d70fab --- /dev/null +++ b/binding/bluetooth-map-bluez.c @@ -0,0 +1,481 @@ +/* + * Copyright (C) 2019 Konsulko Group + * Author: Matt Ranostay <matt.ranostay@konsulko.com> + * Author: Pantelis Antoniou <pantelis.antoniou@konsulko.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <pthread.h> +#include <semaphore.h> + +#include <glib.h> +#include <stdlib.h> +#include <gio/gio.h> +#include <glib-object.h> + +#include <json-c/json.h> + +#define AFB_BINDING_VERSION 3 +#include <afb/afb-binding.h> + +#include "bluetooth-map-api.h" +#include "bluetooth-map-common.h" + +G_DEFINE_QUARK(bluetooth-map-error-quark, nb_error); + +static const struct property_info session_props[] = { + { .name = "Source", .fmt = "s", }, + { .name = "Destination", .fmt = "s", }, + { .name = "Channel", .fmt = "y", }, + { .name = "Target", .fmt = "s", }, + { .name = "Root", .fmt = "s", }, + {}, +}; + +static const struct property_info transfer_props[] = { + { .name = "Status", .fmt = "s", }, + { .name = "Session", .fmt = "o", }, + { .name = "Name", .fmt = "s", }, + { .name = "Type", .fmt = "s", }, + { .name = "Time", .fmt = "t", }, + { .name = "Size", .fmt = "t", }, + { .name = "Transferred", .fmt = "t", }, + { .name = "Filename", .fmt = "s", }, + {}, +}; + +static const struct property_info message_props[] = { + { .name = "Folder", .fmt = "s", }, + { .name = "Subject", .fmt = "s", }, + { .name = "Timestamp", .fmt = "s", }, + { .name = "Sender", .fmt = "s", }, + { .name = "SenderAddress", .fmt = "s", }, + { .name = "ReplyTo", .fmt = "s", }, + { .name = "Recipient", .fmt = "s", }, + { .name = "RecipientAddress", .fmt = "s", }, + { .name = "Type", .fmt = "s", }, + { .name = "Size", .fmt = "t", }, + { .name = "Status", .fmt = "s", }, + { .name = "Priority", .fmt = "b", }, + { .name = "Read", .fmt = "b", }, + { .name = "Deleted", .fmt = "b", }, + { .name = "Sent", .fmt = "b", }, + { .name = "Protected", .fmt = "b", }, + {}, +}; + +const struct property_info *bluez_get_property_info( + const char *access_type, GError **error) +{ + const struct property_info *pi = NULL; + + if (!strcmp(access_type, BLUEZ_AT_SESSION)) + pi = session_props; + else if (!strcmp(access_type, BLUEZ_AT_TRANSFER)) + pi = transfer_props; + else if (!strcmp(access_type, BLUEZ_AT_MESSAGE)) + pi = message_props; + else + g_set_error(error, NB_ERROR, NB_ERROR_ILLEGAL_ARGUMENT, + "illegal %s argument", access_type); + return pi; +} + +gboolean bluez_property_dbus2json(const char *access_type, + json_object *jprop, const gchar *key, GVariant *var, + gboolean *is_config, + GError **error) +{ + const struct property_info *pi; + gboolean ret; + + *is_config = FALSE; + pi = bluez_get_property_info(access_type, error); + if (!pi) + return FALSE; + + ret = root_property_dbus2json(jprop, pi, key, var, is_config); + if (!ret) + g_set_error(error, NB_ERROR, NB_ERROR_UNKNOWN_PROPERTY, + "unknown %s property %s", + access_type, key); + + return ret; +} + +void bluez_decode_call_error(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, + GError **error) +{ + if (!error || !*error) + return; + + if (strstr((*error)->message, + "org.freedesktop.DBus.Error.UnknownObject")) { + + if (!strcmp(method, "Set") || + !strcmp(method, "Get") || + !strcmp(method, "GetAll")) { + + g_clear_error(error); + g_set_error(error, NB_ERROR, + NB_ERROR_UNKNOWN_PROPERTY, + "unknown %s property on %s", + access_type, type_arg); + + } else if (!strcmp(method, "CreateSession") || + !strcmp(method, "RemoveSession")) { + + g_clear_error(error); + g_set_error(error, NB_ERROR, + NB_ERROR_UNKNOWN_SERVICE, + "unknown service %s", + type_arg); + + } else if (!strcmp(method, "Cancel") || + !strcmp(method, "Suspend") || + !strcmp(method, "Resume")) { + + g_clear_error(error); + g_set_error(error, NB_ERROR, + NB_ERROR_UNKNOWN_TRANSFER, + "unknown transfer %s", + type_arg); + + } + } +} + +GVariant *bluez_call(struct map_state *ns, + const char *access_type, const char *path, + const char *method, GVariant *params, GError **error) +{ + const char *interface; + GVariant *reply; + + if (!path && (!strcmp(access_type, BLUEZ_AT_SESSION) || + !strcmp(access_type, BLUEZ_AT_TRANSFER) || + !strcmp(access_type, BLUEZ_AT_MESSAGE))) { + g_set_error(error, NB_ERROR, NB_ERROR_MISSING_ARGUMENT, + "missing %s argument", + access_type); + return NULL; + } + + if (!strcmp(access_type, BLUEZ_AT_CLIENT)) { + path = BLUEZ_OBEX_PATH; + interface = BLUEZ_OBEX_CLIENT_INTERFACE; + } else if (!strcmp(access_type, BLUEZ_AT_SESSION)) { + interface = BLUEZ_OBEX_SESSION_INTERFACE; + } else if (!strcmp(access_type, BLUEZ_AT_TRANSFER)) { + interface = BLUEZ_OBEX_TRANSFER_INTERFACE; + } else if (!strcmp(access_type, BLUEZ_AT_MESSAGE)) { + interface = BLUEZ_OBEX_MESSAGE_INTERFACE; + } else { + g_set_error(error, NB_ERROR, NB_ERROR_ILLEGAL_ARGUMENT, + "illegal %s argument", + access_type); + return NULL; + } + + reply = g_dbus_connection_call_sync(ns->conn, + BLUEZ_OBEX_SERVICE, path, interface, method, params, + NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, + NULL, error); + bluez_decode_call_error(ns, access_type, path, method, + error); + if (!reply && error) { + if (*error) + g_dbus_error_strip_remote_error(*error); + AFB_ERROR("Error calling %s%s%s %s method %s", + access_type, + path ? "/" : "", + path ? path : "", + method, + error && *error ? (*error)->message : + "unspecified"); + } + + return reply; +} + +static void bluez_call_async_ready(GObject *source_object, + GAsyncResult *res, gpointer user_data) +{ + struct bluez_pending_work *cpw = user_data; + struct map_state *ns = cpw->ns; + GVariant *result; + GError *error = NULL; + + result = g_dbus_connection_call_finish(ns->conn, res, &error); + + cpw->callback(cpw->user_data, result, &error); + + g_clear_error(&error); + g_cancellable_reset(cpw->cancel); + g_free(cpw); +} + +void bluez_cancel_call(struct map_state *ns, + struct bluez_pending_work *cpw) +{ + g_cancellable_cancel(cpw->cancel); +} + +struct bluez_pending_work * +bluez_call_async(struct map_state *ns, + const char *access_type, const char *type_arg, + const char *method, GVariant *params, GError **error, + void (*callback)(void *user_data, GVariant *result, GError **error), + void *user_data) +{ + const char *path; + const char *interface; + struct bluez_pending_work *cpw; + + if (!type_arg && (!strcmp(access_type, BLUEZ_AT_SESSION) || + !strcmp(access_type, BLUEZ_AT_TRANSFER))) { + g_set_error(error, NB_ERROR, NB_ERROR_MISSING_ARGUMENT, + "missing %s argument", + access_type); + return NULL; + } + + if (!strcmp(access_type, BLUEZ_AT_SESSION)) { + path = type_arg; + interface = BLUEZ_OBEX_SESSION_INTERFACE; + } else if (!strcmp(access_type, BLUEZ_AT_TRANSFER)) { + path = type_arg; + interface = BLUEZ_OBEX_TRANSFER_INTERFACE; + } else { + g_set_error(error, NB_ERROR, NB_ERROR_ILLEGAL_ARGUMENT, + "illegal %s argument", + access_type); + return NULL; + } + + cpw = g_malloc(sizeof(*cpw)); + if (!cpw) { + g_set_error(error, NB_ERROR, NB_ERROR_OUT_OF_MEMORY, + "out of memory"); + return NULL; + } + cpw->ns = ns; + cpw->user_data = user_data; + cpw->cancel = g_cancellable_new(); + if (!cpw->cancel) { + g_free(cpw); + g_set_error(error, NB_ERROR, NB_ERROR_OUT_OF_MEMORY, + "out of memory"); + return NULL; + } + cpw->callback = callback; + + g_dbus_connection_call(ns->conn, + BLUEZ_OBEX_SERVICE, path, interface, method, params, + NULL, /* reply type */ + G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, + cpw->cancel, /* cancellable? */ + bluez_call_async_ready, + cpw); + + return cpw; +} + +json_object *bluez_get_properties(struct map_state *ns, + const char *access_type, const char *path, + GError **error) +{ + const struct property_info *pi = NULL; + const char *method = NULL; + GVariant *reply = NULL, *var = NULL; + GVariantIter *array; + const char *interface, *interface2; + const gchar *key = NULL; + json_object *jprop = NULL, *jresp = NULL; + gboolean is_config; + + if (!strcmp(access_type, BLUEZ_AT_SESSION) || + !strcmp(access_type, BLUEZ_AT_TRANSFER) || + !strcmp(access_type, BLUEZ_AT_MESSAGE)) { + + pi = bluez_get_property_info(access_type, error); + if (!pi) + return NULL; + + interface = FREEDESKTOP_PROPERTIES; + method = "GetAll"; + } else if (!strcmp(access_type, BLUEZ_AT_OBJECT)) { + interface = FREEDESKTOP_OBJECTMANAGER; + method = "GetManagedObjects"; + } else { + return FALSE; + } + + if (!strcmp(access_type, BLUEZ_AT_SESSION)) + interface2 = BLUEZ_OBEX_SESSION_INTERFACE; + else if (!strcmp(access_type, BLUEZ_AT_TRANSFER)) + interface2 = BLUEZ_OBEX_TRANSFER_INTERFACE; + else if (!strcmp(access_type, BLUEZ_AT_MESSAGE)) + interface2 = BLUEZ_OBEX_MESSAGE_INTERFACE; + else if (!strcmp(access_type, BLUEZ_AT_OBJECT)) + interface2 = NULL; + else + return FALSE; + + if (!method) { + g_set_error(error, NB_ERROR, NB_ERROR_ILLEGAL_ARGUMENT, + "illegal %s argument", + access_type); + return NULL; + } + + reply = g_dbus_connection_call_sync(ns->conn, + BLUEZ_OBEX_SERVICE, path, interface, method, + interface2 ? g_variant_new("(s)", interface2) : NULL, + NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, + NULL, error); + + if (!reply) + return NULL; + + if (!strcmp(access_type, BLUEZ_AT_SESSION) || + !strcmp(access_type, BLUEZ_AT_TRANSFER) || + !strcmp(access_type, BLUEZ_AT_MESSAGE)) { + jprop = json_object_new_object(); + g_variant_get(reply, "(a{sv})", &array); + while (g_variant_iter_loop(array, "{sv}", &key, &var)) { + root_property_dbus2json(jprop, pi, + key, var, &is_config); + } + g_variant_iter_free(array); + g_variant_unref(reply); + jresp = jprop; + } else if (!strcmp(access_type, BLUEZ_AT_OBJECT)) { + /* TODO: maybe not needed */ + + g_variant_iter_free(array); + g_variant_unref(reply); + } + + if (!jresp) { + g_set_error(error, NB_ERROR, NB_ERROR_ILLEGAL_ARGUMENT, + "No %s", access_type); + } + + return jresp; +} + +json_object *bluez_get_property(struct map_state *ns, + const char *access_type, const char *path, + gboolean is_json_name, const char *name, GError **error) +{ + const struct property_info *pi; + json_object *jprop, *jval; + + pi = bluez_get_property_info(access_type, error); + if (!pi) + return NULL; + + jprop = bluez_get_properties(ns, access_type, path, error); + if (!jprop) + return NULL; + + jval = get_named_property(pi, is_json_name, name, jprop); + json_object_put(jprop); + + if (!jval) + g_set_error(error, NB_ERROR, NB_ERROR_BAD_PROPERTY, + "Bad property %s on %s%s%s", name, + access_type, + path ? "/" : "", + path ? path : ""); + return jval; +} + +/* NOTE: jval is consumed */ +gboolean bluez_set_property(struct map_state *ns, + const char *access_type, const char *path, + gboolean is_json_name, const char *name, json_object *jval, + GError **error) +{ + const struct property_info *pi; + GVariant *reply, *arg; + const char *interface; + gboolean is_config; + gchar *propname; + + g_assert(path); + + /* get start of properties */ + pi = bluez_get_property_info(access_type, error); + if (!pi) + return FALSE; + + /* get actual property */ + pi = property_by_name(pi, is_json_name, name, &is_config); + if (!pi) { + g_set_error(error, NB_ERROR, NB_ERROR_UNKNOWN_PROPERTY, + "unknown property with name %s", name); + json_object_put(jval); + return FALSE; + } + + /* convert to gvariant */ + arg = property_json_to_gvariant(pi, NULL, NULL, jval, error); + + /* we don't need this anymore */ + json_object_put(jval); + jval = NULL; + + /* no variant? error */ + if (!arg) + return FALSE; + + if (!is_config) + propname = g_strdup(pi->name); + else + propname = configuration_dbus_name(pi->name); + + if (!strcmp(access_type, BLUEZ_AT_SESSION)) + interface = BLUEZ_OBEX_SESSION_INTERFACE; + else if (!strcmp(access_type, BLUEZ_AT_TRANSFER)) + interface = BLUEZ_OBEX_TRANSFER_INTERFACE; + else if (!strcmp(access_type, BLUEZ_AT_MESSAGE)) + interface = BLUEZ_OBEX_MESSAGE_INTERFACE; + else + return FALSE; + + reply = g_dbus_connection_call_sync(ns->conn, + BLUEZ_OBEX_SERVICE, path, FREEDESKTOP_PROPERTIES, "Set", + g_variant_new("(ssv)", interface, propname, arg), + NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, + NULL, error); + + g_free(propname); + + if (!reply) + return FALSE; + + g_variant_unref(reply); + + return TRUE; +} diff --git a/binding/bluetooth-map-common.h b/binding/bluetooth-map-common.h new file mode 100644 index 0000000..0ce618e --- /dev/null +++ b/binding/bluetooth-map-common.h @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2019 Konsulko Group + * Author: Matt Ranostay <matt.ranostay@konsulko.com> + * Author: Pantelis Antoniou <pantelis.antoniou@konsulko.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BLUETOOTH_MAP_COMMON_H +#define BLUETOOTH_MAP_COMMON_H + +#include <stddef.h> + +#define _GNU_SOURCE +#include <glib.h> +#include <stdlib.h> +#include <gio/gio.h> +#include <glib-object.h> + +#include <json-c/json.h> + +#define AFB_BINDING_VERSION 3 +#include <afb/afb-binding.h> + +#define MAP_UUID "00001133-0000-1000-8000-00805f9b34fb" + +struct call_work; + +struct map_state { + GMainLoop *loop; + GDBusConnection *conn; + guint message_sub; + + afb_event_t notification_event; + + /* NOTE: single connection allowed for now */ + /* NOTE: needs locking and a list */ + GMutex cw_mutex; + int next_cw_id; + GSList *cw_pending; + struct call_work *cw; + + /* OBEX session path */ + gchar *session_path; + + /* xfer queue table */ + GHashTable *xfer_queue; +}; + +struct init_data { + GCond cond; + GMutex mutex; + gboolean init_done; + afb_api_t api; + struct map_state *ns; /* before setting afb_api_set_userdata() */ + int rc; +}; + +struct call_work { + struct bluetooth_state *ns; + int id; + gchar *access_type; + gchar *type_arg; + gchar *method; + struct bluez_pending_work *cpw; + afb_req_t request; + GDBusMethodInvocation *invocation; +}; + +/** + * Structure for converting from dbus properties to json + * and vice-versa. + * Note this is _not_ a generic dbus json bridge since + * some constructs are not easily mapped. + */ +struct property_info { + const char *name; /* the connman property name */ + const char *json_name; /* the json name (if NULL autoconvert) */ + const char *fmt; + unsigned int flags; + const struct property_info *sub; +}; + +#define PI_CONFIG (1U << 0) + +const struct property_info *property_by_dbus_name( + const struct property_info *pi, + const gchar *dbus_name, + gboolean *is_config); +const struct property_info *property_by_json_name( + const struct property_info *pi, + const gchar *json_name, + gboolean *is_config); +const struct property_info *property_by_name( + const struct property_info *pi, + gboolean is_json_name, const gchar *name, + gboolean *is_config); + +gchar *property_get_json_name(const struct property_info *pi, + const gchar *name); +gchar *property_get_name(const struct property_info *pi, + const gchar *json_name); + +gchar *configuration_dbus_name(const gchar *dbus_name); +gchar *configuration_json_name(const gchar *json_name); + +gchar *property_name_dbus2json(const struct property_info *pi, + gboolean is_config); + +json_object *property_dbus2json( + const struct property_info **pip, + const gchar *key, GVariant *var, + gboolean *is_config); + +gboolean root_property_dbus2json( + json_object *jparent, + const struct property_info *pi, + const gchar *key, GVariant *var, + gboolean *is_config); + +GVariant *property_json_to_gvariant( + const struct property_info *pi, + const char *fmt, /* if NULL use pi->fmt */ + const struct property_info *pi_parent, + json_object *jval, + GError **error); + +typedef enum { + NB_ERROR_BAD_TECHNOLOGY, + NB_ERROR_BAD_SERVICE, + NB_ERROR_OUT_OF_MEMORY, + NB_ERROR_NO_SERVICES, + NB_ERROR_BAD_PROPERTY, + NB_ERROR_UNIMPLEMENTED, + NB_ERROR_UNKNOWN_PROPERTY, + NB_ERROR_UNKNOWN_SERVICE, + NB_ERROR_UNKNOWN_TRANSFER, + NB_ERROR_MISSING_ARGUMENT, + NB_ERROR_ILLEGAL_ARGUMENT, + NB_ERROR_CALL_IN_PROGRESS, +} NBError; + +#define NB_ERROR (nb_error_quark()) + +extern GQuark nb_error_quark(void); + +json_object *get_property_collect(json_object *jreqprop, json_object *jprop, + GError **error); +json_object *get_named_property(const struct property_info *pi, + gboolean is_json_name, const char *name, json_object *jprop); + + +#endif /* BLUETOOTH_MAP_COMMON_H */ diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake new file mode 100644 index 0000000..b286ef1 --- /dev/null +++ b/conf.d/cmake/config.cmake @@ -0,0 +1,164 @@ +########################################################################### +# Copyright 2018 Konsulko Group +# +# Author: Pantelis Antoniou <pantelis.antoniou@konsulko.com> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + +# Project Info +# ------------------ +set(PROJECT_NAME agl-service-bluetooth-map) +set(PROJECT_VERSION "1.0") +set(PROJECT_PRETTY_NAME "AFM binding for Bluetooth MAP profile") +set(PROJECT_DESCRIPTION "Binding for Bluetooth MAP profile") +set(PROJECT_ICON "icon.png") +set(PROJECT_AUTHOR "Matt Ranostay") +set(PROJECT_AUTHOR_MAIL "matt.ranostay@konsulko.com") +set(PROJECT_LICENSE "APL2.0") +set(PROJECT_LANGUAGES,"C") +set(API_NAME "bluetooth-map") + +# Where are stored the project configuration files +# relative to the root project directory +set(PROJECT_CMAKE_CONF_DIR "conf.d") + +# Where are stored your external libraries for your project. This is 3rd party library that you don't maintain +# but used and must be built and linked. +# set(PROJECT_LIBDIR "libs") + +# Where are stored data for your application. Pictures, static resources must be placed in that folder. +# set(PROJECT_RESOURCES "data") + +# Which directories inspect to find CMakeLists.txt target files +# set(PROJECT_SRC_DIR_PATTERN "*") + +# Compilation Mode (DEBUG, RELEASE) +# ---------------------------------- +set(BUILD_TYPE "RELEASE") + +# Kernel selection if needed. You can choose between a +# mandatory version to impose a minimal version. +# Or check Kernel minimal version and just print a Warning +# about missing features and define a preprocessor variable +# to be used as preprocessor condition in code to disable +# incompatibles features. Preprocessor define is named +# KERNEL_MINIMAL_VERSION_OK. +# +# NOTE*** FOR NOW IT CHECKS KERNEL Yocto environment and +# Yocto SDK Kernel version. +# ----------------------------------------------- +#set(kernel_mandatory_version 4.8) + +# Compiler selection if needed. Impose a minimal version. +# ----------------------------------------------- +set (gcc_minimal_version 4.9) + +# PKG_CONFIG required packages +# ----------------------------- +set (PKG_REQUIRED_LIST + json-c + libsystemd>=222 + afb-daemon + glib-2.0 + gio-2.0 + gobject-2.0 +) + +# Static constante definition +# ----------------------------- +add_compile_options(-DPB_FIELD_16BIT) +add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-pthread>) + +# Customize link option +# ----------------------------- +list (APPEND link_libraries -pthread) + +# (BUG!!!) as PKG_CONFIG_PATH does not work [should be an env variable] +# --------------------------------------------------------------------- +set(INSTALL_PREFIX $ENV{HOME}/opt) +set(CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX}/lib64/pkgconfig ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig) +set(LD_LIBRARY_PATH ${CMAKE_INSTALL_PREFIX}/lib64 ${CMAKE_INSTALL_PREFIX}/lib) + +# Optional location for config.xml.in +# ----------------------------------- +set(WIDGET_CONFIG_TEMPLATE ${CMAKE_CURRENT_SOURCE_DIR}/conf.d/wgt/config.xml.in) + +# Mandatory widget Mimetype specification of the main unit +# -------------------------------------------------------------------------- +# Choose between : +#- text/html : HTML application, +# content.src designates the home page of the application +# +#- application/vnd.agl.native : AGL compatible native, +# content.src designates the relative path of the binary. +# +# - application/vnd.agl.service: AGL service, content.src is not used. +# +#- ***application/x-executable***: Native application, +# content.src designates the relative path of the binary. +# For such application, only security setup is made. +# +set(WIDGET_TYPE application/vnd.agl.service) + +# Mandatory Widget entry point file of the main unit +# -------------------------------------------------------------- +# This is the file that will be executed, loaded, +# at launch time by the application framework. +# +set(WIDGET_ENTRY_POINT lib/afb-bluetooth-map-binding.so) + +# Print a helper message when every thing is finished +# ---------------------------------------------------- +set(CLOSING_MESSAGE "Test with: afb-daemon --rootdir=\$\$(pwd)/package --binding=\$\$(pwd)/package/${WIDGET_ENTRY_POINT} --port=1234 --tracereq=common --token=\"1\" --verbose") +set(PACKAGE_MESSAGE "Install widget file using in the target : afm-util install ${PROJECT_NAME}.wgt") + + + +# Optional dependencies order +# --------------------------- +#set(EXTRA_DEPENDENCIES_ORDER) + +# Optional Extra global include path +# ----------------------------------- +#set(EXTRA_INCLUDE_DIRS) + +# Optional extra libraries +# ------------------------- +#set(EXTRA_LINK_LIBRARIES) + +# Optional force binding installation +# ------------------------------------ +# set(BINDINGS_INSTALL_PREFIX PrefixPath ) + +# Optional force binding Linking flag +# ------------------------------------ +# set(BINDINGS_LINK_FLAG LinkOptions ) + +# Optional force package prefix generation, like widget +# ----------------------------------------------------- +# set(PKG_PREFIX DestinationPath) + +# Optional Application Framework security token +# and port use for remote debugging. +#------------------------------------------------------------ +#set(AFB_TOKEN "" CACHE PATH "Default AFB_TOKEN") +#set(AFB_REMPORT "1234" CACHE PATH "Default AFB_TOKEN") + +# This include is mandatory and MUST happens at the end +# of this file, else you expose you to unexpected behavior +# +# This CMake module could be found at the following url: +# https://gerrit.automotivelinux.org/gerrit/#/admin/projects/src/cmake-apps-module +# ----------------------------------------------------------- +include(CMakeAfbTemplates) diff --git a/conf.d/wgt/config.xml.in b/conf.d/wgt/config.xml.in new file mode 100644 index 0000000..4c33036 --- /dev/null +++ b/conf.d/wgt/config.xml.in @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<widget xmlns="http://www.w3.org/ns/widgets" id="@PROJECT_NAME@" version="@PROJECT_VERSION@"> + <name>@PROJECT_NAME@</name> + <icon src="@PROJECT_ICON@"/> + <content src="@WIDGET_ENTRY_POINT@" type="@WIDGET_TYPE@"/> + <description>@PROJECT_DESCRIPTION@</description> + <author>@PROJECT_AUTHOR@ <@PROJECT_AUTHOR_MAIL@></author> + <license>@PROJECT_LICENSE@</license> + + <feature name="urn:AGL:widget:required-permission"> + <param name="urn:AGL:permission::public:hidden" value="required" /> + <param name="http://tizen.org/privilege/internal/dbus" value="required" /> + </feature> + + <feature name="urn:AGL:widget:provided-api"> + <param name="bluetooth-map" value="ws" /> + </feature> + + <feature name="urn:AGL:widget:required-api"> + <param name="Bluetooth-Manager" value="ws"/> + <param name="@WIDGET_ENTRY_POINT@" value="local" /> + </feature> + +</widget> |