From 179f93118b7ecfc50c85c3b8714419ebb3fa2325 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Sun, 12 May 2019 21:41:13 -0700 Subject: binding: bluetooth-map: add encoder for bMessage composing Bug-AGL: SPEC-2351 SPEC-2392 Change-Id: I40425ed3dff4aabacfc520e5e3887e93f82a7b5e Signed-off-by: Matt Ranostay --- README.md | 23 +---------- binding/bluetooth-map-api.c | 8 ++-- binding/bluetooth-map-bmessage.c | 89 ++++++++++++++++++++++++++++++++++++++++ binding/bluetooth-map-common.h | 1 + 4 files changed, 96 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 66d92be..f496365 100644 --- a/README.md +++ b/README.md @@ -31,27 +31,8 @@ Send a message (if supported) via MAP profile:
 {
-  "bmessage":
-    "BEGIN:BMSG\r\n
-     VERSION:1.0\r\n
-     TYPE:SMS_GSM\r\n
-     FOLDER:telecom/msg/outbox\r\n
-     BEGIN:BENV\r\n
-     BEGIN:VCARD\r\n
-     VERSION:2.1\r\n
-     TEL:+13605551212\r\n
-     END:VCARD\r\n
-     BEGIN:BBODY\r\n
-     CHARSET:UTF-8\r\n
-     LANGUAGE:UNKNOWN\r\n
-     LENGTH:48\r\n
-     BEGIN:MSG\r\n
-     Sounds good. See you then.\r\n
-     END:MSG\r\n
-     END:BBODY\r\n
-     END:BENV\r\n
-     END:BMSG\r\n
-    "
+  "recipient": "+13605551212",
+  "message": "Sounds good. See you then."
 }
 
diff --git a/binding/bluetooth-map-api.c b/binding/bluetooth-map-api.c index df90ae5..5023c5a 100644 --- a/binding/bluetooth-map-api.c +++ b/binding/bluetooth-map-api.c @@ -308,13 +308,11 @@ out_free: call_work_destroy(cw); } - - static void compose(afb_req_t request) { struct map_state *ns = map_get_userdata(request); - const char *message = afb_req_value(request, "bmessage"); GError *error = NULL; + GString *message = NULL; GVariant *params, *reply; gchar *name, *session; struct call_work *cw; @@ -329,6 +327,7 @@ static void compose(afb_req_t request) session = g_strdup(ns->session_path); call_work_unlock(ns); + message = bmessage_encoder(request); if (!message) { afb_req_fail_f(request, "failed", "no valid message provided"); goto err_msg_invalid; @@ -337,7 +336,8 @@ static void compose(afb_req_t request) fd = g_file_open_tmp("obex-clientXXXXXX", &name, NULL); close(fd); - g_file_set_contents(name, message, -1, NULL); + g_file_set_contents(name, message->str, -1, NULL); + g_string_free(message, TRUE); params = g_variant_new("(&s)", "telecom/msg/OUTBOX"); reply = bluez_call(ns, BLUEZ_AT_MESSAGEACCESS, session, "SetFolder", params, NULL); diff --git a/binding/bluetooth-map-bmessage.c b/binding/bluetooth-map-bmessage.c index ab51b51..fd19772 100644 --- a/binding/bluetooth-map-bmessage.c +++ b/binding/bluetooth-map-bmessage.c @@ -195,3 +195,92 @@ json_object *bmessage_parse(const gchar *bmessage) return jresp; } + +#define CRLF "\r\n" +#define BEGINMSG "BEGIN:MSG" CRLF +#define ENDMSG "END:MSG" CRLF + +#define body_append(str, value) g_string_append_printf(str, "%s%s", value, CRLF) + +GString *msg2dos(const gchar *data) +{ + gchar **msg, **tmp; + GString *value; + + if (!data) + return NULL; + + msg = g_strsplit(data, "\n", -1); + value = g_string_new(NULL); + + for (tmp = msg; *tmp; tmp++) { + g_string_append(value, *tmp); + + if (!g_str_has_suffix(*tmp, "\r")) + g_string_append(value, CRLF); + else + g_string_append(value, "\n"); + } + + g_strfreev(msg); + + return value; +} + +GString *bmessage_encoder(afb_req_t request) +{ + const gchar *recipient; + GString *msg, *body = g_string_new(NULL); + gchar *tmp; + + recipient = afb_req_value(request, "recipient"); + if (!recipient) { + g_string_free(body, TRUE); + return NULL; + } + + msg = msg2dos(afb_req_value(request, "message")); + if (!msg) { + g_string_free(body, TRUE); + return NULL; + } + + body_append(body, "BEGIN:BMSG"); + body_append(body, "VERSION:1.0"); + + // TODO: maybe add support for MMS + body_append(body, "TYPE:SMS_GSM"); + + body_append(body, "FOLDER:telecom/msg/outbox"); + body_append(body, "BEGIN:BENV"); + + body_append(body, "BEGIN:VCARD"); + body_append(body, "VERSION:2.1"); + + // TODO: get contact information from agl-service-bluetooth-pbap + tmp = g_strdup_printf("TEL:%s", recipient); + body_append(body, tmp); + g_free(tmp); + + body_append(body, "END:VCARD"); + + body_append(body, "BEGIN:BBODY"); + body_append(body, "CHARSET:UTF-8"); + + tmp = g_strdup_printf("LENGTH:%lu", + strlen(BEGINMSG) + msg->len + strlen(ENDMSG)); + + body_append(body, tmp); + g_free(tmp); + + g_string_append(body, BEGINMSG); + g_string_append(body, msg->str); + g_string_free(msg, TRUE); + g_string_append(body, ENDMSG); + + body_append(body, "END:BBODY"); + body_append(body, "END:BENV"); + body_append(body, "END:BMSG"); + + return body; +} diff --git a/binding/bluetooth-map-common.h b/binding/bluetooth-map-common.h index ba68781..8b2f469 100644 --- a/binding/bluetooth-map-common.h +++ b/binding/bluetooth-map-common.h @@ -174,5 +174,6 @@ json_object *get_named_property(const struct property_info *pi, /* functions defined in bluetooth-map-bmessage.c */ json_object *bmessage_parse(const gchar *bmessage); +GString *bmessage_encoder(afb_req_t request); #endif /* BLUETOOTH_MAP_COMMON_H */ -- cgit 1.2.3-korg