This commit extract local and remote address from the socket and
store them into the card.
---
 src/emulator.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/src/emulator.c b/src/emulator.c
index b75d0bd..079a5b8 100644
--- a/src/emulator.c
+++ b/src/emulator.c
@@ -23,11 +23,15 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include <glib.h>
+#include <sys/socket.h>
 
 #define OFONO_API_SUBJECT_TO_CHANGE
 #include <ofono/handsfree-audio.h>
@@ -37,6 +41,7 @@
 #include "hfp.h"
 #include "gatserver.h"
 #include "gatppp.h"
+#include "bluetooth.h"
 
 #define RING_TIMEOUT 3
 
@@ -1002,11 +1007,56 @@ struct ofono_emulator *ofono_emulator_create(struct 
ofono_modem *modem,
        return em;
 }
 
+static int card_set_local_remote(struct ofono_handsfree_card *card, int fd)
+{
+       struct sockaddr_rc saddr;
+       socklen_t optlen;
+       char local[18], remote[18];
+       int err;
+
+       memset(&saddr, 0, sizeof(saddr));
+       optlen = sizeof(saddr);
+
+       if (getsockname(fd, (struct sockaddr *) &saddr, &optlen) < 0) {
+               err = errno;
+               ofono_error("RFCOMM getsockname: %s (%d)", strerror(err), err);
+               return -err;
+       }
+
+       bt_ba2str(&saddr.rc_bdaddr, local);
+
+       DBG("local %s", local);
+
+       memset(&saddr, 0, sizeof(saddr));
+       optlen = sizeof(saddr);
+
+       if (getpeername(fd, (struct sockaddr *) &saddr, &optlen) < 0) {
+               err = errno;
+               ofono_error("RFCOMM getpeername: %s (%d)", strerror(err), err);
+               return -err;
+       }
+
+       bt_ba2str(&saddr.rc_bdaddr, remote);
+
+       DBG("remote %s", remote);
+
+       ofono_handsfree_card_set_local(card, local);
+       ofono_handsfree_card_set_remote(card, remote);
+
+       return 0;
+}
+
 int ofono_emulator_create_card(struct ofono_emulator *em, int version)
 {
+       GIOChannel *io;
+       int fd;
+
        em->card = ofono_handsfree_card_create(0, NULL, NULL);
 
-       return 0;
+       io = g_at_server_get_channel(em->server);
+       fd = g_io_channel_unix_get_fd(io);
+
+       return card_set_local_remote(em->card, fd);
 }
 
 void ofono_emulator_remove(struct ofono_emulator *em)
-- 
1.7.9.5

_______________________________________________
ofono mailing list
[email protected]
https://lists.ofono.org/mailman/listinfo/ofono

Reply via email to