2 * Copyright (C) 2013 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include "hal-utils.h"
28 static const btpan_callbacks_t *cbs = NULL;
30 static bool interface_ready(void)
35 static void handle_conn_state(void *buf, uint16_t len, int fd)
37 struct hal_ev_pan_conn_state *ev = buf;
39 if (cbs->connection_state_cb)
40 cbs->connection_state_cb(ev->state, ev->status,
41 (bt_bdaddr_t *) ev->bdaddr,
42 ev->local_role, ev->remote_role);
45 static void handle_ctrl_state(void *buf, uint16_t len, int fd)
47 struct hal_ev_pan_ctrl_state *ev = buf;
49 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
50 if (cbs->control_state_cb)
51 cbs->control_state_cb(ev->state, ev->local_role, ev->status,
55 * Callback declared in bt_pan.h is 'typedef void
56 * (*btpan_control_state_callback)(btpan_control_state_t state,
57 * bt_status_t error, int local_role, const char* ifname);
58 * But PanService.Java defined it wrong way.
59 * private void onControlStateChanged(int local_role, int state,
60 * int error, String ifname).
61 * First and third parameters are misplaced, so sending data according
64 if (cbs->control_state_cb)
65 cbs->control_state_cb(ev->local_role, ev->state, ev->status,
71 * handlers will be called from notification thread context,
72 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
74 static const struct hal_ipc_handler ev_handlers[] = {
75 /* HAL_EV_PAN_CTRL_STATE */
76 { handle_ctrl_state, false, sizeof(struct hal_ev_pan_ctrl_state) },
77 /* HAL_EV_PAN_CONN_STATE */
78 { handle_conn_state, false, sizeof(struct hal_ev_pan_conn_state) },
81 static bt_status_t pan_enable(int local_role)
83 struct hal_cmd_pan_enable cmd;
87 if (!interface_ready())
88 return BT_STATUS_NOT_READY;
90 cmd.local_role = local_role;
92 return hal_ipc_cmd(HAL_SERVICE_ID_PAN, HAL_OP_PAN_ENABLE,
93 sizeof(cmd), &cmd, NULL, NULL, NULL);
96 static int pan_get_local_role(void)
98 struct hal_rsp_pan_get_role rsp;
99 size_t len = sizeof(rsp);
104 if (!interface_ready())
105 return BTPAN_ROLE_NONE;
107 status = hal_ipc_cmd(HAL_SERVICE_ID_PAN, HAL_OP_PAN_GET_ROLE, 0, NULL,
109 if (status != BT_STATUS_SUCCESS)
110 return BTPAN_ROLE_NONE;
112 return rsp.local_role;
115 static bt_status_t pan_connect(const bt_bdaddr_t *bd_addr, int local_role,
118 struct hal_cmd_pan_connect cmd;
122 if (!interface_ready())
123 return BT_STATUS_NOT_READY;
125 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
126 cmd.local_role = local_role;
127 cmd.remote_role = remote_role;
129 return hal_ipc_cmd(HAL_SERVICE_ID_PAN, HAL_OP_PAN_CONNECT,
130 sizeof(cmd), &cmd, NULL, NULL, NULL);
133 static bt_status_t pan_disconnect(const bt_bdaddr_t *bd_addr)
135 struct hal_cmd_pan_disconnect cmd;
139 if (!interface_ready())
140 return BT_STATUS_NOT_READY;
142 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
144 return hal_ipc_cmd(HAL_SERVICE_ID_PAN, HAL_OP_PAN_DISCONNECT,
145 sizeof(cmd), &cmd, NULL, NULL, NULL);
148 static bt_status_t pan_init(const btpan_callbacks_t *callbacks)
150 struct hal_cmd_register_module cmd;
155 if (interface_ready())
156 return BT_STATUS_DONE;
160 hal_ipc_register(HAL_SERVICE_ID_PAN, ev_handlers,
161 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
163 cmd.service_id = HAL_SERVICE_ID_PAN;
164 cmd.mode = HAL_MODE_DEFAULT;
167 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
168 sizeof(cmd), &cmd, NULL, NULL, NULL);
170 if (ret != BT_STATUS_SUCCESS) {
172 hal_ipc_unregister(HAL_SERVICE_ID_PAN);
178 static void pan_cleanup(void)
180 struct hal_cmd_unregister_module cmd;
184 if (!interface_ready())
187 cmd.service_id = HAL_SERVICE_ID_PAN;
189 hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
190 sizeof(cmd), &cmd, NULL, NULL, NULL);
192 hal_ipc_unregister(HAL_SERVICE_ID_PAN);
197 static btpan_interface_t pan_if = {
198 .size = sizeof(pan_if),
200 .enable = pan_enable,
201 .get_local_role = pan_get_local_role,
202 .connect = pan_connect,
203 .disconnect = pan_disconnect,
204 .cleanup = pan_cleanup
207 btpan_interface_t *bt_get_pan_interface(void)