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 // TODO(b/369381361) Enfore -Wmissing-prototypes
29 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
30 
31 /*****************************************************************************
32  *  Local type definitions
33  ****************************************************************************/
34 
35 #define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50
36 
37 typedef struct {
38   bool enable;
39   uint8_t num_clients;
40   tGATTS_SRV_CHG srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE];
41 } __attribute__((packed)) btif_gatts_srv_chg_cb_t;
42 
43 /*****************************************************************************
44  *  Static variables
45  ****************************************************************************/
46 
47 static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb;
48 
49 /*****************************************************************************
50  *  Static functions
51  ****************************************************************************/
52 
btif_gatts_check_init(void)53 static void btif_gatts_check_init(void) {
54   btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb;
55 
56   if (!p_cb->enable) {
57     memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t));
58     p_cb->enable = true;
59   }
60 }
61 
62 /*****************************************************************************
63  *  Externally called functions
64  ****************************************************************************/
65 
btif_gatts_add_bonded_dev_from_nv(const RawAddress & bda)66 void btif_gatts_add_bonded_dev_from_nv(const RawAddress& bda) {
67   btif_gatts_srv_chg_cb_t* p_cb = &btif_gatts_srv_chg_cb;
68   bool found = false;
69   uint8_t i;
70 
71   btif_gatts_check_init();
72 
73   for (i = 0; i != p_cb->num_clients; ++i) {
74     if (p_cb->srv_chg[i].bda == bda) {
75       found = true;
76       break;
77     }
78   }
79 
80   if (!found) {
81     if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE) {
82       p_cb->srv_chg[p_cb->num_clients].bda = bda;
83       p_cb->srv_chg[p_cb->num_clients].srv_changed = false;
84       p_cb->num_clients++;
85     }
86   }
87 }
88