summaryrefslogtreecommitdiffstats
path: root/binding
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2019-05-12 21:41:13 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2019-05-13 03:00:12 -0700
commit179f93118b7ecfc50c85c3b8714419ebb3fa2325 (patch)
tree51d709d27b0cafbb4daf0a756e7717bbc0b1d4f3 /binding
parent0cf29634f36160c69d2759456315df878793e23f (diff)
binding: bluetooth-map: add encoder for bMessage composing
Bug-AGL: SPEC-2351 SPEC-2392 Change-Id: I40425ed3dff4aabacfc520e5e3887e93f82a7b5e Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding')
-rw-r--r--binding/bluetooth-map-api.c8
-rw-r--r--binding/bluetooth-map-bmessage.c89
-rw-r--r--binding/bluetooth-map-common.h1
3 files changed, 94 insertions, 4 deletions
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 */