bluez/android/hal-health.c
Tedd Ho-Jeong An 44b4662529 android: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 Apache-2.0           :     80
 LGPL-2.1-or-later    :     42
 GPL-2.0-or-later     :      9

License: Apache-2.0
   android/bluetoothd-wrapper.c
   android/tester-hidhost.c
   android/tester-map-client.c
   android/hal-audio-aptx.c
   android/hal-audio.c
   android/hal-handsfree-client.c
   android/tester-gatt.c
   android/hal-pan.c
   android/hal-socket.c
   android/tester-hdp.c
   android/tester-pan.c
   android/tester-avrcp.c
   android/hal-a2dp-sink.c
   android/hal-gatt.c
   android/hal-avrcp.c
   android/tester-a2dp.c
   android/hal-audio.h
   android/hal-bluetooth.c
   android/tester-socket.c
   android/hal-health.c
   android/hal-a2dp.c
   android/hal-ipc.c
   android/hal-avrcp-ctrl.c
   android/hal-sco.c
   android/hal-ipc.h
   android/tester-main.c
   android/hal-audio-sbc.c
   android/hal-utils.c
   android/hal-map-client.c
   android/hal-handsfree.c
   android/hal-log.h
   android/hal.h
   android/hal-utils.h
   android/tester-bluetooth.c
   android/hal-hidhost.c
   android/audio_utils/resampler.h
   android/audio_utils/resampler.c
   android/system/audio.h
   android/hardware/bt_sock.h
   android/hardware/bt_gatt_client.h
   android/hardware/bt_hh.h
   android/hardware/bluetooth.h
   android/hardware/hardware.h
   android/hardware/bt_hf_client.h
   android/hardware/bt_rc.h
   android/hardware/bt_hf.h
   android/hardware/bt_pan.h
   android/hardware/bt_gatt_types.h
   android/hardware/hardware.c
   android/hardware/audio_effect.h
   android/hardware/audio.h
   android/hardware/bt_gatt_server.h
   android/hardware/bt_gatt.h
   android/hardware/bt_hl.h
   android/hardware/bt_mce.h
   android/hardware/bt_av.h
   android/client/if-hh.c
   android/client/if-sco.c
   android/client/pollhandler.h
   android/client/haltest.c
   android/client/tabcompletion.c
   android/client/if-hf-client.c
   android/client/history.h
   android/client/if-audio.c
   android/client/terminal.h
   android/client/if-av.c
   android/client/if-main.h
   android/client/if-av-sink.c
   android/client/terminal.c
   android/client/if-mce.c
   android/client/if-hf.c
   android/client/pollhandler.c
   android/client/if-sock.c
   android/client/if-rc-ctrl.c
   android/client/if-rc.c
   android/client/history.c
   android/client/if-pan.c
   android/client/if-gatt.c
   android/client/if-bt.c
   android/client/if-hl.c

License: LGPL-2.1-or-later
   android/sco.h
   android/socket.c
   android/tester-main.h
   android/hidhost.h
   android/map-client.h
   android/utils.h
   android/hal-msg.h
   android/health.h
   android/avrcp-lib.c
   android/bluetoothd-snoop.c
   android/a2dp.c
   android/bluetooth.h
   android/a2dp-sink.c
   android/handsfree-client.c
   android/audio-msg.h
   android/main.c
   android/pan.c
   android/ipc-tester.c
   android/health.c
   android/socket.h
   android/handsfree.h
   android/avrcp-lib.h
   android/a2dp-sink.h
   android/system-emulator.c
   android/gatt.c
   android/avrcp.h
   android/pan.h
   android/test-ipc.c
   android/hidhost.c
   android/a2dp.h
   android/avrcp.c
   android/sco-msg.h
   android/bluetooth.c
   android/map-client.c
   android/handsfree-client.h
   android/ipc.c
   android/handsfree.c
   android/gatt.h
   android/ipc.h
   android/ipc-common.h
   android/compat/wordexp.h
   android/cutils/properties.h

License: GPL-2.0-or-later
   android/avdtp.h
   android/avctp.c
   android/avdtptest.c
   android/sco.c
   android/log.c
   android/avdtp.c
   android/avctp.h
   android/compat/readline/history.h
   android/compat/readline/readline.h
2020-09-21 16:19:33 -07:00

287 lines
6.3 KiB
C

// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2014 Intel Corporation
*
*/
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "hal-log.h"
#include "hal.h"
#include "hal-msg.h"
#include "ipc-common.h"
#include "hal-ipc.h"
static const bthl_callbacks_t *cbacks = NULL;
static bool interface_ready(void)
{
return cbacks != NULL;
}
static void handle_app_registration_state(void *buf, uint16_t len, int fd)
{
struct hal_ev_health_app_reg_state *ev = buf;
if (cbacks->app_reg_state_cb)
cbacks->app_reg_state_cb(ev->id, ev->state);
}
static void handle_channel_state(void *buf, uint16_t len, int fd)
{
struct hal_ev_health_channel_state *ev = buf;
int flags;
if (fd < 0)
goto end;
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
error("health: fcntl GETFL error: %s", strerror(errno));
return;
}
/* Clean O_NONBLOCK fd flag as Android Java layer expects */
if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
error("health: fcntl SETFL error: %s", strerror(errno));
return;
}
end:
if (cbacks->channel_state_cb)
cbacks->channel_state_cb(ev->app_id, (bt_bdaddr_t *) ev->bdaddr,
ev->mdep_index, ev->channel_id,
ev->channel_state, fd);
}
/*
* handlers will be called from notification thread context,
* index in table equals to 'opcode - HAL_MINIMUM_EVENT'
*/
static const struct hal_ipc_handler ev_handlers[] = {
/* HAL_EV_HEALTH_APP_REG_STATE */
{ handle_app_registration_state, false,
sizeof(struct hal_ev_health_app_reg_state) },
/* HAL_EV_HEALTH_CHANNEL_STATE */
{ handle_channel_state, false,
sizeof(struct hal_ev_health_channel_state) },
};
static bt_status_t register_application(bthl_reg_param_t *reg, int *app_id)
{
uint8_t buf[IPC_MTU];
struct hal_cmd_health_reg_app *cmd = (void *) buf;
struct hal_rsp_health_reg_app rsp;
size_t rsp_len = sizeof(rsp);
bt_status_t status;
uint16_t off, len;
int i;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!reg || !app_id || !reg->application_name)
return BT_STATUS_PARM_INVALID;
*app_id = -1;
memset(buf, 0, IPC_MTU);
cmd->num_of_mdep = reg->number_of_mdeps;
off = 0;
cmd->app_name_off = off;
len = strlen(reg->application_name) + 1;
memcpy(cmd->data, reg->application_name, len);
off += len;
cmd->provider_name_off = off;
if (reg->provider_name) {
len = strlen(reg->provider_name) + 1;
memcpy(cmd->data + off, reg->provider_name, len);
off += len;
}
cmd->service_name_off = off;
if (reg->srv_name) {
len = strlen(reg->srv_name) + 1;
memcpy(cmd->data + off, reg->srv_name, len);
off += len;
}
cmd->service_descr_off = off;
if (reg->srv_desp) {
len = strlen(reg->srv_desp) + 1;
memcpy(cmd->data + off, reg->srv_desp, len);
off += len;
}
cmd->len = off;
status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_REG_APP,
sizeof(*cmd) + cmd->len, buf,
&rsp_len, &rsp, NULL);
if (status != BT_STATUS_SUCCESS)
return status;
for (i = 0; i < reg->number_of_mdeps; i++) {
struct hal_cmd_health_mdep *mdep = (void *) buf;
memset(buf, 0, IPC_MTU);
mdep->app_id = rsp.app_id;
mdep->role = reg->mdep_cfg[i].mdep_role;
mdep->data_type = reg->mdep_cfg[i].data_type;
mdep->channel_type = reg->mdep_cfg[i].channel_type;
if (reg->mdep_cfg[i].mdep_description) {
mdep->descr_len =
strlen(reg->mdep_cfg[i].mdep_description) + 1;
memcpy(mdep->descr, reg->mdep_cfg[i].mdep_description,
mdep->descr_len);
}
status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
sizeof(*mdep) + mdep->descr_len,
buf, NULL, NULL, NULL);
if (status != BT_STATUS_SUCCESS)
return status;
}
*app_id = rsp.app_id;
return status;
}
static bt_status_t unregister_application(int app_id)
{
struct hal_cmd_health_unreg_app cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
cmd.app_id = app_id;
return hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_UNREG_APP,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
static bt_status_t connect_channel(int app_id, bt_bdaddr_t *bd_addr,
int mdep_cfg_index, int *channel_id)
{
struct hal_cmd_health_connect_channel cmd;
struct hal_rsp_health_connect_channel rsp;
size_t len = sizeof(rsp);
bt_status_t status;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!bd_addr || !channel_id)
return BT_STATUS_PARM_INVALID;
*channel_id = -1;
cmd.app_id = app_id;
cmd.mdep_index = mdep_cfg_index;
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
status = hal_ipc_cmd(HAL_SERVICE_ID_HEALTH,
HAL_OP_HEALTH_CONNECT_CHANNEL,
sizeof(cmd), &cmd, &len, &rsp, NULL);
if (status == BT_STATUS_SUCCESS)
*channel_id = rsp.channel_id;
return status;
}
static bt_status_t destroy_channel(int channel_id)
{
struct hal_cmd_health_destroy_channel cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
cmd.channel_id = channel_id;
return hal_ipc_cmd(HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_DESTROY_CHANNEL,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
static bt_status_t init(bthl_callbacks_t *callbacks)
{
struct hal_cmd_register_module cmd;
int ret;
DBG("");
if (interface_ready())
return BT_STATUS_DONE;
/* store reference to user callbacks */
cbacks = callbacks;
hal_ipc_register(HAL_SERVICE_ID_HEALTH, ev_handlers,
sizeof(ev_handlers)/sizeof(ev_handlers[0]));
cmd.service_id = HAL_SERVICE_ID_HEALTH;
cmd.mode = HAL_MODE_DEFAULT;
cmd.max_clients = 1;
ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
if (ret != BT_STATUS_SUCCESS) {
cbacks = NULL;
hal_ipc_unregister(HAL_SERVICE_ID_HEALTH);
}
return ret;
}
static void cleanup(void)
{
struct hal_cmd_unregister_module cmd;
DBG("");
if (!interface_ready())
return;
cmd.service_id = HAL_SERVICE_ID_HEALTH;
hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
hal_ipc_unregister(HAL_SERVICE_ID_HEALTH);
cbacks = NULL;
}
static bthl_interface_t health_if = {
.size = sizeof(health_if),
.init = init,
.register_application = register_application,
.unregister_application = unregister_application,
.connect_channel = connect_channel,
.destroy_channel = destroy_channel,
.cleanup = cleanup
};
bthl_interface_t *bt_get_health_interface(void)
{
return &health_if;
}