summaryrefslogtreecommitdiffstats
path: root/binding/bluetooth-map-bmessage.c
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/bluetooth-map-bmessage.c
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/bluetooth-map-bmessage.c')
-rw-r--r--binding/bluetooth-map-bmessage.c89
1 files changed, 89 insertions, 0 deletions
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;
+}