1/******************************************************************************
2 *
3 *  Copyright (C) 2014 Google, Inc.
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19#include "base.h"
20#include "support/callbacks.h"
21
22void adapter_state_changed(bt_state_t state);
23void adapter_properties(bt_status_t status,
24                        int num_properties,
25                        bt_property_t *properties);
26void discovery_state_changed(bt_discovery_state_t state);
27
28void pan_control_state_changed(btpan_control_state_t state, int local_role, bt_status_t error, const char *ifname);
29void pan_connection_state_changed(btpan_connection_state_t state, bt_status_t error, const bt_bdaddr_t *bd_addr, int local_role, int remote_role);
30
31static void remote_device_properties(bt_status_t status,
32                                     bt_bdaddr_t *bd_addr,
33                                     int num_properties,
34                                     bt_property_t *properties) {
35  CALLBACK_RET();
36}
37
38static void thread_evt(bt_cb_thread_evt evt) {
39  CALLBACK_RET();
40}
41
42static struct {
43  const char *name;
44  sem_t semaphore;
45} callback_data[] = {
46  // Adapter callbacks
47  { "adapter_state_changed" },
48  { "adapter_properties" },
49  { "remote_device_properties" },
50  {},
51  { "discovery_state_changed" },
52  {},
53  {},
54  {},
55  {},
56  { "thread_evt" },
57  {},
58  {},
59
60  // PAN callbacks
61  { "pan_control_state_changed" },
62  { "pan_connection_state_changed" },
63};
64
65static bt_callbacks_t bt_callbacks = {
66  sizeof(bt_callbacks_t),
67  adapter_state_changed,     // adapter_state_changed_callback
68  adapter_properties,        // adapter_properties_callback
69  remote_device_properties,  // remote_device_properties_callback
70  NULL,                      // device_found_callback
71  discovery_state_changed,   // discovery_state_changed_callback
72  NULL,                      // pin_request_callback
73  NULL,                      // ssp_request_callback
74  NULL,                      // bond_state_changed_callback
75  NULL,                      // acl_state_changed_callback
76  thread_evt,                // callback_thread_event
77  NULL,                      // dut_mode_recv_callback
78  NULL,                      // le_test_mode_callback
79  NULL,
80};
81
82static btpan_callbacks_t pan_callbacks = {
83  sizeof(btpan_callbacks_t),
84  pan_control_state_changed,     // btpan_control_state_callback
85  pan_connection_state_changed,  // btpan_connection_state_callback
86};
87
88void callbacks_init() {
89  for (size_t i = 0; i < ARRAY_SIZE(callback_data); ++i) {
90    sem_init(&callback_data[i].semaphore, 0, 0);
91  }
92}
93
94void callbacks_cleanup() {
95  for (size_t i = 0; i < ARRAY_SIZE(callback_data); ++i) {
96    sem_destroy(&callback_data[i].semaphore);
97  }
98}
99
100bt_callbacks_t *callbacks_get_adapter_struct() {
101  return &bt_callbacks;
102}
103
104btpan_callbacks_t *callbacks_get_pan_struct() {
105  return &pan_callbacks;
106}
107
108sem_t *callbacks_get_semaphore(const char *name) {
109  for (size_t i = 0; i < ARRAY_SIZE(callback_data); ++i) {
110    if (callback_data[i].name && !strcmp(name, callback_data[i].name)) {
111      return &callback_data[i].semaphore;
112    }
113  }
114  return NULL;
115}
116