1 /******************************************************************************
2 *
3 * Copyright (C) 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 "common/bt_target.h"
26
27 #if defined(GATTS_INCLUDED) && (GATTS_INCLUDED == 1)
28
29 #include <string.h>
30
31 #include "bta_gatts_int.h"
32 #include "osi/allocator.h"
33
34 /* type for service building action functions */
35 typedef void (*tBTA_GATTS_SRVC_ACT)(tBTA_GATTS_SRVC_CB *p_rcb, tBTA_GATTS_DATA *p_data);
36
37 /* service building action function list */
38 const tBTA_GATTS_SRVC_ACT bta_gatts_srvc_build_act[] = {
39 bta_gatts_add_include_srvc,
40 bta_gatts_add_char,
41 bta_gatts_add_char_descr,
42 bta_gatts_delete_service,
43 bta_gatts_start_service,
44 bta_gatts_stop_service,
45 };
46
47 /* GATTS control block */
48 #if BTA_DYNAMIC_MEMORY == 0
49 tBTA_GATTS_CB bta_gatts_cb;
50 #else
51 tBTA_GATTS_CB *bta_gatts_cb_ptr;
52 #endif
53
54 /*******************************************************************************
55 **
56 ** Function bta_gatts_hdl_event
57 **
58 ** Description BTA GATT server main event handling function.
59 **
60 **
61 ** Returns void
62 **
63 *******************************************************************************/
bta_gatts_hdl_event(BT_HDR * p_msg)64 BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg)
65 {
66 tBTA_GATTS_CB *p_cb = &bta_gatts_cb;
67 tBTA_GATTS_SRVC_CB *p_srvc_cb = NULL;
68
69 switch (p_msg->event) {
70 case BTA_GATTS_API_DISABLE_EVT:
71 bta_gatts_api_disable(p_cb);
72 break;
73
74 case BTA_GATTS_API_REG_EVT:
75 bta_gatts_register(p_cb, (tBTA_GATTS_DATA *) p_msg);
76 break;
77
78 case BTA_GATTS_INT_START_IF_EVT:
79 bta_gatts_start_if(p_cb, (tBTA_GATTS_DATA *) p_msg);
80 break;
81
82 case BTA_GATTS_API_DEREG_EVT:
83 bta_gatts_deregister(p_cb, (tBTA_GATTS_DATA *) p_msg);
84 break;
85
86 case BTA_GATTS_API_CREATE_SRVC_EVT:
87 bta_gatts_create_srvc(p_cb, (tBTA_GATTS_DATA *) p_msg);
88 break;
89
90 case BTA_GATTS_API_INDICATION_EVT:
91 bta_gatts_indicate_handle(p_cb, (tBTA_GATTS_DATA *) p_msg);
92 break;
93
94 case BTA_GATTS_API_OPEN_EVT:
95 bta_gatts_open(p_cb, (tBTA_GATTS_DATA *) p_msg);
96 break;
97
98 case BTA_GATTS_API_CANCEL_OPEN_EVT:
99 bta_gatts_cancel_open(p_cb, (tBTA_GATTS_DATA *) p_msg);
100 break;
101
102 case BTA_GATTS_API_CLOSE_EVT:
103 bta_gatts_close(p_cb, (tBTA_GATTS_DATA *) p_msg);
104 break;
105
106 case BTA_GATTS_API_RSP_EVT:
107 bta_gatts_send_rsp(p_cb, (tBTA_GATTS_DATA *) p_msg);
108 break;
109 case BTA_GATTS_API_SET_ATTR_VAL_EVT:{
110 UINT16 attr_id = ((tBTA_GATTS_DATA *) p_msg)->api_set_val.hdr.layer_specific;
111 p_srvc_cb = bta_gatts_find_srvc_cb_by_attr_id(p_cb, attr_id);
112 bta_gatts_set_attr_value(p_srvc_cb, (tBTA_GATTS_DATA *) p_msg);
113 break;
114 }
115 case BTA_GATTS_API_LISTEN_EVT:
116 bta_gatts_listen(p_cb, (tBTA_GATTS_DATA *) p_msg);
117 break;
118 case BTA_GATTS_API_ADD_INCL_SRVC_EVT:
119 case BTA_GATTS_API_ADD_CHAR_EVT:
120 case BTA_GATTS_API_ADD_DESCR_EVT:
121 case BTA_GATTS_API_DEL_SRVC_EVT:
122 case BTA_GATTS_API_START_SRVC_EVT:
123 case BTA_GATTS_API_STOP_SRVC_EVT:
124 p_srvc_cb = bta_gatts_find_srvc_cb_by_srvc_id(p_cb,
125 ((tBTA_GATTS_DATA *)p_msg)->api_add_incl_srvc.hdr.layer_specific);
126
127 if (p_srvc_cb != NULL) {
128 bta_gatts_srvc_build_act[p_msg->event - BTA_GATTS_API_ADD_INCL_SRVC_EVT](p_srvc_cb, (tBTA_GATTS_DATA *) p_msg);
129 } else {
130 APPL_TRACE_ERROR("service not created\n");
131 }
132 break;
133 case BTA_GATTS_API_SEND_SERVICE_CHANGE_EVT:
134 bta_gatts_send_service_change_indication((tBTA_GATTS_DATA *) p_msg);
135 break;
136 default:
137 break;
138 }
139
140
141 return (1);
142 }
143
bta_gatts_deinit(void)144 void bta_gatts_deinit(void)
145 {
146 memset(&bta_gatts_cb, 0, sizeof(tBTA_GATTS_CB));
147 #if BTA_DYNAMIC_MEMORY
148 FREE_AND_RESET(bta_gatts_cb_ptr);
149 #endif /* #if BTA_DYNAMIC_MEMORY */
150 }
151
152 #endif /* GATTS_INCLUDED */
153