Disable IPSP feature
[platform/upstream/bluez.git] / emulator / hfp.c
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 <sys/socket.h>
34 #include <sys/un.h>
35
36 #include "src/shared/mainloop.h"
37 #include "src/shared/hfp.h"
38
39 static void hfp_debug(const char *str, void *user_data)
40 {
41         const char *prefix = user_data;
42
43         printf("%s%s\n", prefix, str);
44 }
45
46 static void command_handler(const char *command, void *user_data)
47 {
48         struct hfp_gw *hfp = user_data;
49
50         printf("Command: %s\n", command);
51
52         hfp_gw_send_result(hfp, HFP_RESULT_ERROR);
53 }
54
55 static bool open_connection(void)
56 {
57         static const char SOCKET_PATH[] = "\0hfp-headset";
58         struct hfp_gw *hfp;
59         struct sockaddr_un addr;
60         int fd;
61
62         fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
63         if (fd < 0) {
64                 perror("Failed to create Unix socket");
65                 return false;
66         }
67
68         memset(&addr, 0, sizeof(addr));
69         addr.sun_family = AF_UNIX;
70         memcpy(addr.sun_path, SOCKET_PATH, sizeof(SOCKET_PATH));
71
72         if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
73                 perror("Failed to connect Unix socket");
74                 close(fd);
75                 return false;
76         }
77
78         hfp = hfp_gw_new(fd);
79         if (!hfp) {
80                 close(fd);
81                 return false;
82         }
83
84         hfp_gw_set_close_on_unref(hfp, true);
85
86         hfp_gw_set_debug(hfp, hfp_debug, "HFP: ", NULL);
87
88         hfp_gw_set_command_handler(hfp, command_handler, hfp, NULL);
89
90         return true;
91 }
92
93 static void signal_callback(int signum, void *user_data)
94 {
95         switch (signum) {
96         case SIGINT:
97         case SIGTERM:
98                 mainloop_quit();
99                 break;
100         }
101 }
102
103 int main(int argc, char *argv[])
104 {
105         sigset_t mask;
106
107         mainloop_init();
108
109         sigemptyset(&mask);
110         sigaddset(&mask, SIGINT);
111         sigaddset(&mask, SIGTERM);
112
113         mainloop_set_signal(&mask, signal_callback, NULL, NULL);
114
115         if (!open_connection())
116                 return EXIT_FAILURE;
117
118         return mainloop_run();
119 }