Blame emulator/hfp.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2011-2012  Intel Corporation
Packit 34410b
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <errno.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <sys/socket.h>
Packit 34410b
#include <sys/un.h>
Packit 34410b
Packit 34410b
#include "src/shared/mainloop.h"
Packit 34410b
#include "src/shared/hfp.h"
Packit 34410b
Packit 34410b
static void hfp_debug(const char *str, void *user_data)
Packit 34410b
{
Packit 34410b
	const char *prefix = user_data;
Packit 34410b
Packit 34410b
	printf("%s%s\n", prefix, str);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void command_handler(const char *command, void *user_data)
Packit 34410b
{
Packit 34410b
	struct hfp_gw *hfp = user_data;
Packit 34410b
Packit 34410b
	printf("Command: %s\n", command);
Packit 34410b
Packit 34410b
	hfp_gw_send_result(hfp, HFP_RESULT_ERROR);
Packit 34410b
}
Packit 34410b
Packit 34410b
static bool open_connection(void)
Packit 34410b
{
Packit 34410b
	static const char SOCKET_PATH[] = "\0hfp-headset";
Packit 34410b
	struct hfp_gw *hfp;
Packit 34410b
	struct sockaddr_un addr;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
Packit 34410b
	if (fd < 0) {
Packit 34410b
		perror("Failed to create Unix socket");
Packit 34410b
		return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	memset(&addr, 0, sizeof(addr));
Packit 34410b
	addr.sun_family = AF_UNIX;
Packit 34410b
	memcpy(addr.sun_path, SOCKET_PATH, sizeof(SOCKET_PATH));
Packit 34410b
Packit 34410b
	if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Packit 34410b
		perror("Failed to connect Unix socket");
Packit 34410b
		close(fd);
Packit 34410b
		return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	hfp = hfp_gw_new(fd);
Packit 34410b
	if (!hfp) {
Packit 34410b
		close(fd);
Packit 34410b
		return false;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	hfp_gw_set_close_on_unref(hfp, true);
Packit 34410b
Packit 34410b
	hfp_gw_set_debug(hfp, hfp_debug, "HFP: ", NULL);
Packit 34410b
Packit 34410b
	hfp_gw_set_command_handler(hfp, command_handler, hfp, NULL);
Packit 34410b
Packit 34410b
	return true;
Packit 34410b
}
Packit 34410b
Packit 34410b
static void signal_callback(int signum, void *user_data)
Packit 34410b
{
Packit 34410b
	switch (signum) {
Packit 34410b
	case SIGINT:
Packit 34410b
	case SIGTERM:
Packit 34410b
		mainloop_quit();
Packit 34410b
		break;
Packit 34410b
	}
Packit 34410b
}
Packit 34410b
Packit 34410b
int main(int argc, char *argv[])
Packit 34410b
{
Packit 34410b
	mainloop_init();
Packit 34410b
Packit 34410b
	if (!open_connection())
Packit 34410b
		return EXIT_FAILURE;
Packit 34410b
Packit 34410b
	return mainloop_run_with_signal(signal_callback, NULL);
Packit 34410b
}