1 /******************************************************************************
2 *
3 * Copyright 2003-2012 Broadcom Corporation
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 /******************************************************************************
20 *
21 * This file contains the GATT server main functions and state machine.
22 *
23 ******************************************************************************/
24
25 #include "bt_target.h" // Must be first to define build configuration
26 #include "bta/gatt/bta_gatts_int.h"
27 #include "stack/include/bt_hdr.h"
28
29 #include <base/logging.h>
30
31 /* GATTS control block */
32 tBTA_GATTS_CB bta_gatts_cb;
33
34 /*******************************************************************************
35 *
36 * Function bta_gatts_hdl_event
37 *
38 * Description BTA GATT server main event handling function.
39 *
40 *
41 * Returns void
42 *
43 ******************************************************************************/
bta_gatts_hdl_event(BT_HDR_RIGID * p_msg)44 bool bta_gatts_hdl_event(BT_HDR_RIGID* p_msg) {
45 tBTA_GATTS_CB* p_cb = &bta_gatts_cb;
46
47 switch (p_msg->event) {
48 case BTA_GATTS_API_DISABLE_EVT:
49 bta_gatts_api_disable(p_cb);
50 break;
51
52 case BTA_GATTS_API_REG_EVT:
53 bta_gatts_register(p_cb, (tBTA_GATTS_DATA*)p_msg);
54 break;
55
56 case BTA_GATTS_INT_START_IF_EVT:
57 bta_gatts_start_if(p_cb, (tBTA_GATTS_DATA*)p_msg);
58 break;
59
60 case BTA_GATTS_API_DEREG_EVT:
61 bta_gatts_deregister(p_cb, (tBTA_GATTS_DATA*)p_msg);
62 break;
63
64 case BTA_GATTS_API_INDICATION_EVT:
65 bta_gatts_indicate_handle(p_cb, (tBTA_GATTS_DATA*)p_msg);
66 break;
67
68 case BTA_GATTS_API_OPEN_EVT:
69 bta_gatts_open(p_cb, (tBTA_GATTS_DATA*)p_msg);
70 break;
71
72 case BTA_GATTS_API_CANCEL_OPEN_EVT:
73 bta_gatts_cancel_open(p_cb, (tBTA_GATTS_DATA*)p_msg);
74 break;
75
76 case BTA_GATTS_API_CLOSE_EVT:
77 bta_gatts_close(p_cb, (tBTA_GATTS_DATA*)p_msg);
78 break;
79
80 case BTA_GATTS_API_RSP_EVT:
81 bta_gatts_send_rsp(p_cb, (tBTA_GATTS_DATA*)p_msg);
82 break;
83
84 case BTA_GATTS_API_DEL_SRVC_EVT: {
85 tBTA_GATTS_SRVC_CB* p_srvc_cb = bta_gatts_find_srvc_cb_by_srvc_id(
86 p_cb, ((tBTA_GATTS_DATA*)p_msg)->api_add_service.hdr.layer_specific);
87
88 if (p_srvc_cb != NULL)
89 bta_gatts_delete_service(p_srvc_cb, (tBTA_GATTS_DATA*)p_msg);
90 else
91 LOG(ERROR) << __func__ << ": can't delete service - no srvc_cb found";
92
93 break;
94 }
95
96 case BTA_GATTS_API_STOP_SRVC_EVT: {
97 tBTA_GATTS_SRVC_CB* p_srvc_cb = bta_gatts_find_srvc_cb_by_srvc_id(
98 p_cb, ((tBTA_GATTS_DATA*)p_msg)->api_add_service.hdr.layer_specific);
99
100 if (p_srvc_cb != NULL)
101 bta_gatts_stop_service(p_srvc_cb, (tBTA_GATTS_DATA*)p_msg);
102 else
103 LOG(ERROR) << __func__ << ": can't stop service - no srvc_cb found";
104
105 break;
106 }
107
108 case BTA_GATTS_API_INIT_BONDED_EVT:
109 gatt_load_bonded();
110 break;
111
112 default:
113 break;
114 }
115
116 return (true);
117 }
118