<commit_before>/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <glib.h>

#include "log.h"
#include "src/sdpd.h"

#include "lib/bluetooth.h"
#include "lib/mgmt.h"
#include "src/shared/mgmt.h"

#include "adapter.h"
#include "hid.h"
#include "hal-msg.h"
#include "ipc.h"

static GMainLoop *event_loop;
static struct mgmt *mgmt_if = NULL;

static uint8_t mgmt_version = 0;
static uint8_t mgmt_revision = 0;

static uint16_t adapter_index = MGMT_INDEX_NONE;

static GIOChannel *hal_cmd_io = NULL;
static GIOChannel *hal_notif_io = NULL;

static volatile sig_atomic_t __terminated = 0;

static bool services[HAL_SERVICE_ID_MAX + 1] = { false };

static void service_register(void *buf, uint16_t len)
{
	struct hal_msg_cmd_register_module *m = buf;

	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
		goto error;

	switch (m->service_id) {
	default:
		DBG("service %u not supported", m->service_id);
		goto error;
	}

	services[m->service_id] = true;

	ipc_send(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE, 0,
								NULL, -1);

	info("Service ID=%u registered", m->service_id);
	return;
error:
	ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_ERROR_FAILED);
}

static void service_unregister(void *buf, uint16_t len)
{
	struct hal_msg_cmd_unregister_module *m = buf;

	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id]) {
		ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE,
							HAL_ERROR_FAILED);
		return;
	}

	services[m->service_id] = false;

	ipc_send(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_MSG_OP_UNREGISTER_MODULE,
								0, NULL, -1);

	info("Service ID=%u unregistered", m->service_id);
}

static void handle_service_core(uint8_t opcode, void *buf, uint16_t len)
{
	switch (opcode) {
	case HAL_MSG_OP_REGISTER_MODULE:
		service_register(buf, len);
		break;
	case HAL_MSG_OP_UNREGISTER_MODULE:
		service_unregister(buf, len);
		break;
	default:
		ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE,
							HAL_ERROR_FAILED);
		break;
	}
}

static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
							gpointer user_data)
{
	char buf[BLUEZ_HAL_MTU];
	struct hal_msg_hdr *msg = (void *) buf;
	ssize_t ret;
	int fd;

	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
		info("HAL command socket closed, terminating");
		goto fail;
	}<commit_msg>android: Add initial code for services unregister<commit_after>/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <glib.h>

#include "log.h"
#include "src/sdpd.h"

#include "lib/bluetooth.h"
#include "lib/mgmt.h"
#include "src/shared/mgmt.h"

#include "adapter.h"
#include "hid.h"
#include "hal-msg.h"
#include "ipc.h"

static GMainLoop *event_loop;
static struct mgmt *mgmt_if = NULL;

static uint8_t mgmt_version = 0;
static uint8_t mgmt_revision = 0;

static uint16_t adapter_index = MGMT_INDEX_NONE;

static GIOChannel *hal_cmd_io = NULL;
static GIOChannel *hal_notif_io = NULL;

static volatile sig_atomic_t __terminated = 0;

static bool services[HAL_SERVICE_ID_MAX + 1] = { false };

static void service_register(void *buf, uint16_t len)
{
	struct hal_msg_cmd_register_module *m = buf;

	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
		goto error;

	switch (m->service_id) {
	default:
		DBG("service %u not supported", m->service_id);
		goto error;
	}

	services[m->service_id] = true;

	ipc_send(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_MSG_OP_REGISTER_MODULE, 0,
								NULL, -1);

	info("Service ID=%u registered", m->service_id);
	return;
error:
	ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_ERROR_FAILED);
}

static void service_unregister(void *buf, uint16_t len)
{
	struct hal_msg_cmd_unregister_module *m = buf;

	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id])
		goto error;

	switch (m->service_id) {
	default:
		/* This would indicate bug in HAL, as unregister should not be
		 * called in init failed */
		DBG("service %u not supported", m->service_id);
		goto error;
	}

	services[m->service_id] = false;

	ipc_send(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_MSG_OP_UNREGISTER_MODULE,
								0, NULL, -1);

	info("Service ID=%u unregistered", m->service_id);
	return;
error:
	ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE, HAL_ERROR_FAILED);
}

static void handle_service_core(uint8_t opcode, void *buf, uint16_t len)
{
	switch (opcode) {
	case HAL_MSG_OP_REGISTER_MODULE:
		service_register(buf, len);
		break;
	case HAL_MSG_OP_UNREGISTER_MODULE:
		service_unregister(buf, len);
		break;
	default:
		ipc_send_error(hal_cmd_io, HAL_SERVICE_ID_CORE,
							HAL_ERROR_FAILED);
		break;
	}
}

static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
							gpointer user_data)
{
	char buf[BLUEZ_HAL_MTU];
	struct hal_msg_hdr *msg = (void *) buf;
	ssize_t ret;
	int fd;

	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
		info("HAL command socket closed, terminating");
		goto fail;
	}
{'fraction_non_alphanumeric': 0.10716300056401579, 'fraction_numerical': 0.009870276367738297, 'mean_word_length': 4.563137254901961, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4174928', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2597, 'n_words': 722}