~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to emulator/hfp.c

  • Committer: tim
  • Date: 2015-09-04 08:03:03 UTC
  • Revision ID: tim@tim-inspiron-5442-20150904080303-75u7z8bdsl17xz8q
* init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2011-2012  Intel Corporation
 
6
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
 
7
 *
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 *
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#include <stdio.h>
 
30
#include <errno.h>
 
31
#include <unistd.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
 
34
#include <sys/socket.h>
 
35
#include <sys/un.h>
 
36
 
 
37
#include "src/shared/mainloop.h"
 
38
#include "src/shared/hfp.h"
 
39
 
 
40
static void hfp_debug(const char *str, void *user_data)
 
41
{
 
42
        const char *prefix = user_data;
 
43
 
 
44
        printf("%s%s\n", prefix, str);
 
45
}
 
46
 
 
47
static void command_handler(const char *command, void *user_data)
 
48
{
 
49
        struct hfp_gw *hfp = user_data;
 
50
 
 
51
        printf("Command: %s\n", command);
 
52
 
 
53
        hfp_gw_send_result(hfp, HFP_RESULT_ERROR);
 
54
}
 
55
 
 
56
static bool open_connection(void)
 
57
{
 
58
        static const char SOCKET_PATH[] = "\0hfp-headset";
 
59
        struct hfp_gw *hfp;
 
60
        struct sockaddr_un addr;
 
61
        int fd;
 
62
 
 
63
        fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
 
64
        if (fd < 0) {
 
65
                perror("Failed to create Unix socket");
 
66
                return false;
 
67
        }
 
68
 
 
69
        memset(&addr, 0, sizeof(addr));
 
70
        addr.sun_family = AF_UNIX;
 
71
        memcpy(addr.sun_path, SOCKET_PATH, sizeof(SOCKET_PATH));
 
72
 
 
73
        if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 
74
                perror("Failed to connect Unix socket");
 
75
                close(fd);
 
76
                return false;
 
77
        }
 
78
 
 
79
        hfp = hfp_gw_new(fd);
 
80
        if (!hfp) {
 
81
                close(fd);
 
82
                return false;
 
83
        }
 
84
 
 
85
        hfp_gw_set_close_on_unref(hfp, true);
 
86
 
 
87
        hfp_gw_set_debug(hfp, hfp_debug, "HFP: ", NULL);
 
88
 
 
89
        hfp_gw_set_command_handler(hfp, command_handler, hfp, NULL);
 
90
 
 
91
        return true;
 
92
}
 
93
 
 
94
static void signal_callback(int signum, void *user_data)
 
95
{
 
96
        switch (signum) {
 
97
        case SIGINT:
 
98
        case SIGTERM:
 
99
                mainloop_quit();
 
100
                break;
 
101
        }
 
102
}
 
103
 
 
104
int main(int argc, char *argv[])
 
105
{
 
106
        sigset_t mask;
 
107
 
 
108
        mainloop_init();
 
109
 
 
110
        sigemptyset(&mask);
 
111
        sigaddset(&mask, SIGINT);
 
112
        sigaddset(&mask, SIGTERM);
 
113
 
 
114
        mainloop_set_signal(&mask, signal_callback, NULL, NULL);
 
115
 
 
116
        if (!open_connection())
 
117
                return EXIT_FAILURE;
 
118
 
 
119
        return mainloop_run();
 
120
}