2 * Copyright (C) 2014 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.
23 #include <hardware/bluetooth.h>
24 #include <hardware/bt_hl.h>
27 #include "pollhandler.h"
28 #include "../hal-utils.h"
30 SINTMAP(bthl_mdep_role_t, -1, "(unknown)")
31 DELEMENT(BTHL_MDEP_ROLE_SOURCE),
32 DELEMENT(BTHL_MDEP_ROLE_SINK),
35 SINTMAP(bthl_channel_type_t, -1, "(unknown)")
36 DELEMENT(BTHL_CHANNEL_TYPE_RELIABLE),
37 DELEMENT(BTHL_CHANNEL_TYPE_STREAMING),
38 DELEMENT(BTHL_CHANNEL_TYPE_ANY),
41 SINTMAP(bthl_app_reg_state_t, -1, "(unknown)")
42 DELEMENT(BTHL_APP_REG_STATE_REG_SUCCESS),
43 DELEMENT(BTHL_APP_REG_STATE_REG_FAILED),
44 DELEMENT(BTHL_APP_REG_STATE_DEREG_SUCCESS),
45 DELEMENT(BTHL_APP_REG_STATE_DEREG_FAILED),
48 SINTMAP(bthl_channel_state_t, -1, "(unknown)")
49 DELEMENT(BTHL_CONN_STATE_CONNECTING),
50 DELEMENT(BTHL_CONN_STATE_CONNECTED),
51 DELEMENT(BTHL_CONN_STATE_DISCONNECTING),
52 DELEMENT(BTHL_CONN_STATE_DISCONNECTED),
53 DELEMENT(BTHL_CONN_STATE_DESTROYED),
56 #define APP_ID_SIZE 20
57 #define MDEP_CFG_SIZE 10
58 #define CHANNEL_ID_SIZE 50
66 struct channel_info channel[CHANNEL_ID_SIZE];
70 struct mdep_cfg mdep[MDEP_CFG_SIZE];
73 const bthl_interface_t *if_hl = NULL;
75 static void app_reg_state_cb(int app_id, bthl_app_reg_state_t state)
77 haltest_info("%s: app_id=%d app_reg_state=%s\n", __func__,
78 app_id, bthl_app_reg_state_t2str(state));
81 static void channel_state_cb(int app_id, bt_bdaddr_t *bd_addr,
82 int index, int channel_id,
83 bthl_channel_state_t state, int fd)
85 char addr[MAX_ADDR_STR_LEN];
87 haltest_info("%s: app_id=%d bd_addr=%s mdep_cfg_index=%d\n"
88 "channel_id=%d channel_state=%s fd=%d\n", __func__,
89 app_id, bt_bdaddr_t2str(bd_addr, addr), index,
90 channel_id, bthl_channel_state_t2str(state), fd);
92 if (app_id >= APP_ID_SIZE || index >= MDEP_CFG_SIZE
93 || channel_id >= CHANNEL_ID_SIZE) {
94 haltest_error("exceeds maximum limit");
98 if (state == BTHL_CONN_STATE_CONNECTED) {
99 app[app_id].mdep[index].channel[channel_id].fd = fd;
102 * PTS expects dummy data on fd when it
103 * connects in source role.
105 if (app[app_id].mdep[index].role == BTHL_MDEP_ROLE_SOURCE)
106 if (write(fd, "0", sizeof("0")) < 0)
107 haltest_error("writing data on fd failed\n");
112 if (state == BTHL_CONN_STATE_DISCONNECTED ||
113 state == BTHL_CONN_STATE_DESTROYED) {
114 if (app[app_id].mdep[index].channel[channel_id].fd >= 0) {
115 close(app[app_id].mdep[index].channel[channel_id].fd);
116 app[app_id].mdep[index].channel[channel_id].fd = -1;
121 static bthl_callbacks_t hl_cbacks = {
122 .size = sizeof(hl_cbacks),
123 .app_reg_state_cb = app_reg_state_cb,
124 .channel_state_cb = channel_state_cb,
129 static void init_p(int argc, const char **argv)
133 for (i = 0; i < APP_ID_SIZE; i++) {
134 for (j = 0; j < MDEP_CFG_SIZE; j++) {
135 app[i].mdep[j].role = 0;
136 for (k = 0; k < CHANNEL_ID_SIZE; k++)
137 app[i].mdep[j].channel[k].fd = -1;
142 RETURN_IF_NULL(if_hl);
144 EXEC(if_hl->init, &hl_cbacks);
147 /* register_application */
149 static void register_application_p(int argc, const char **argv)
151 bthl_reg_param_t reg;
152 uint16_t mdep_argc_init, mdep_argc_off;
156 RETURN_IF_NULL(if_hl);
159 haltest_error("No app name is specified\n");
164 haltest_error("No provider is specified\n");
169 haltest_error("No service name is specified\n");
174 haltest_error("No service description is specified\n");
179 haltest_error("No num of mdeps is specified\n");
183 memset(®, 0, sizeof(reg));
185 if (argc != ((atoi(argv[6]) * 4) + 7)) {
186 haltest_error("mdep cfg argumetns are not proper\n");
190 reg.application_name = argv[2];
192 if (strcmp("-", argv[3]))
193 reg.provider_name = argv[3];
195 if (strcmp("-", argv[4]))
196 reg.srv_name = argv[4];
198 if (strcmp("-", argv[5]))
199 reg.srv_desp = argv[5];
201 reg.number_of_mdeps = atoi(argv[6]);
203 reg.mdep_cfg = malloc(reg.number_of_mdeps * sizeof(bthl_mdep_cfg_t));
205 haltest_error("malloc failed\n");
210 for (i = 0; i < reg.number_of_mdeps; i++) {
211 mdep_argc_off = mdep_argc_init + (4 * i);
212 reg.mdep_cfg[i].mdep_role =
213 str2bthl_mdep_role_t(argv[mdep_argc_off]);
214 reg.mdep_cfg[i].data_type = atoi(argv[mdep_argc_off + 1]);
215 reg.mdep_cfg[i].channel_type =
216 str2bthl_channel_type_t(argv[mdep_argc_off + 2]);
218 if (!strcmp("-", argv[mdep_argc_off + 3])) {
219 reg.mdep_cfg[i].mdep_description = NULL;
223 reg.mdep_cfg[i].mdep_description = argv[mdep_argc_off + 3];
226 EXEC(if_hl->register_application, ®, &app_id);
228 for (i = 0; i < reg.number_of_mdeps; i++)
229 app[app_id].mdep[i].role = reg.mdep_cfg[i].mdep_role;
234 /* unregister_application */
236 static void unregister_application_p(int argc, const char **argv)
240 RETURN_IF_NULL(if_hl);
243 haltest_error("No app id is specified");
247 app_id = (uint32_t) atoi(argv[2]);
249 EXEC(if_hl->unregister_application, app_id);
252 /* connect_channel */
254 static void connect_channel_p(int argc, const char **argv)
256 uint32_t app_id, mdep_cfg_index;
260 RETURN_IF_NULL(if_hl);
263 haltest_error("No app id is specified");
267 VERIFY_ADDR_ARG(3, &bd_addr);
270 haltest_error("No mdep cfg index is specified");
274 app_id = (uint32_t) atoi(argv[2]);
275 mdep_cfg_index = (uint32_t) atoi(argv[4]);
277 EXEC(if_hl->connect_channel, app_id, &bd_addr, mdep_cfg_index,
281 /* destroy_channel */
283 static void destroy_channel_p(int argc, const char **argv)
287 RETURN_IF_NULL(if_hl);
290 haltest_error("No channel id is specified");
294 channel_id = (uint32_t) atoi(argv[2]);
296 EXEC(if_hl->destroy_channel, channel_id);
301 static void close_channel_p(int argc, const char **argv)
307 RETURN_IF_NULL(if_hl);
310 haltest_error("No app id is specified");
315 haltest_error("No mdep_cfg_index is specified");
320 haltest_error("No channel_id is specified");
324 app_id = (uint32_t) atoi(argv[2]);
325 if (app_id >= APP_ID_SIZE) {
326 haltest_error("Wrong app_id specified: %u\n", app_id);
330 index = (uint8_t) atoi(argv[3]);
331 if (index >= MDEP_CFG_SIZE) {
332 haltest_error("Wrong mdep cfg index: %u\n", index);
336 channel_id = atoi(argv[4]);
337 if (channel_id >= CHANNEL_ID_SIZE) {
338 haltest_error("Wrong channel id: %u\n", channel_id);
342 if (app[app_id].mdep[index].channel[channel_id].fd >= 0) {
343 shutdown(app[app_id].mdep[index].channel[channel_id].fd,
345 app[app_id].mdep[index].channel[channel_id].fd = -1;
351 static void cleanup_p(int argc, const char **argv)
353 RETURN_IF_NULL(if_hl);
355 EXECV(if_hl->cleanup);
359 static struct method methods[] = {
361 STD_METHODH(register_application,
362 "<app_name> <provider_name> <srv_name> <srv_descr>\n"
364 "[[<mdep_role>] [<data_type>] [<channel_type>] [<mdep_descr>]]"
366 STD_METHODH(unregister_application, "<app_id>"),
367 STD_METHODH(connect_channel, "<app_id> <bd_addr> <mdep_cfg_index>"),
368 STD_METHODH(destroy_channel, "<channel_id>"),
369 STD_METHODH(close_channel, "<app_id> <mdep_cfg_index> <channel_id>"),
374 const struct interface hl_if = {