1 /******************************************************************************
2  *
3  *  Copyright 2009-2013 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 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "bta_api.h"
23 #include "btif_util.h"
24 #include "osi/include/osi.h"
25 #include "stack/include/gatt_api.h"
26 #include "types/raw_address.h"
27 
28 /*****************************************************************************
29  *  Local type definitions
30  ****************************************************************************/
31 
32 #define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50
33 
34 typedef struct {
35   bool enable;
36   uint8_t num_clients;
37   tGATTS_SRV_CHG srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE];
38 } __attribute__((packed)) btif_gatts_srv_chg_cb_t;
39 
40 /*****************************************************************************
41  *  Static variables
42  ****************************************************************************/
43 
44 static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb;
45 
46 /*****************************************************************************
47  *  Static functions
48  ****************************************************************************/
49 
btif_gatts_check_init(void)50 static void btif_gatts_check_init(void) {
51   btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb;
52 
53   if (!p_cb->enable) {
54     memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t));
55     p_cb->enable = true;
56   }
57 }
58 
59 /*****************************************************************************
60  *  Externally called functions
61  ****************************************************************************/
62 
btif_gatts_add_bonded_dev_from_nv(const RawAddress & bda)63 void btif_gatts_add_bonded_dev_from_nv(const RawAddress& bda) {
64   btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb;
65   bool found = false;
66   uint8_t i;
67 
68   btif_gatts_check_init();
69 
70   for (i = 0; i != p_cb->num_clients; ++i) {
71     if (p_cb->srv_chg[i].bda == bda) {
72       found = true;
73       break;
74     }
75   }
76 
77   if (!found) {
78     if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE) {
79       p_cb->srv_chg[p_cb->num_clients].bda = bda;
80       p_cb->srv_chg[p_cb->num_clients].srv_changed = false;
81       p_cb->num_clients++;
82     }
83   }
84 }
85