1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef __BLUFI_INT_H__
16 #define __BLUFI_INT_H__
17 
18 #include "btc/btc_task.h"
19 #include "esp_blufi_api.h"
20 #if (BLUFI_INCLUDED == TRUE)
21 
22 #define BTC_BLUFI_GREAT_VER   0x01  //Version + Subversion
23 #define BTC_BLUFI_SUB_VER     0x02  //Version + Subversion
24 #define BTC_BLUFI_VERSION     ((BTC_BLUFI_GREAT_VER<<8)|BTC_BLUFI_SUB_VER)  //Version + Subversion
25 
26 typedef UINT8 tGATT_IF;
27 /* service engine control block */
28 typedef struct {
29     /* Protocol reference */
30     tGATT_IF                gatt_if;
31     UINT8                   srvc_inst;
32     UINT16                  handle_srvc;
33     UINT16                  handle_char_p2e;
34     UINT16                  handle_char_e2p;
35     UINT16                  handle_descr_e2p;
36     UINT16                  conn_id;
37     BOOLEAN                 is_connected;
38     BD_ADDR                 remote_bda;
39     UINT32                  trans_id;
40     UINT8                   congest;
41     UINT16                  frag_size;
42 #define BLUFI_PREPAIR_BUF_MAX_SIZE 1024
43     uint8_t                 *prepare_buf;
44     int                     prepare_len;
45     /* Control reference */
46     esp_blufi_callbacks_t   *cbs;
47     BOOLEAN                 enabled;
48     uint8_t                 send_seq;
49     uint8_t                 recv_seq;
50     uint8_t                 sec_mode;
51     uint8_t                 *aggr_buf;
52     uint16_t                 total_len;
53     uint16_t                offset;
54 } tBLUFI_ENV;
55 
56 /* BLUFI protocol */
57 struct blufi_hdr{
58     uint8_t type;
59     uint8_t fc;
60     uint8_t seq;
61     uint8_t data_len;
62     uint8_t data[0];
63 };
64 typedef struct blufi_hdr blufi_hd_t;
65 
66 struct blufi_frag_hdr {
67     uint8_t type;
68     uint8_t fc;
69     uint8_t seq;
70     uint8_t data_len;
71     uint16_t total_len;
72     uint8_t content[0];
73 };
74 typedef struct blufi_frag_hdr blufi_frag_hdr_t;
75 
76 #if GATT_DYNAMIC_MEMORY == FALSE
77 extern tBLUFI_ENV blufi_env;
78 #else
79 extern tBLUFI_ENV *blufi_env_ptr;
80 #define blufi_env (*blufi_env_ptr)
81 #endif
82 
83 #define BLUFI_DATA_SEC_MODE_CHECK_MASK  0x01
84 #define BLUFI_DATA_SEC_MODE_ENC_MASK    0x02
85 #define BLUFI_CTRL_SEC_MODE_CHECK_MASK  0x10
86 #define BLUFI_CTRL_SEC_MODE_ENC_MASK    0x20
87 #define BLUFI_MAX_DATA_LEN              255
88 
89 // packet type
90 #define BLUFI_TYPE_MASK         0x03
91 #define BLUFI_TYPE_SHIFT        0
92 #define BLUFI_SUBTYPE_MASK      0xFC
93 #define BLUFI_SUBTYPE_SHIFT     2
94 
95 #define BLUFI_GET_TYPE(type)    ((type) & BLUFI_TYPE_MASK)
96 #define BLUFI_GET_SUBTYPE(type) (((type) & BLUFI_SUBTYPE_MASK) >>BLUFI_SUBTYPE_SHIFT)
97 #define BLUFI_BUILD_TYPE(type, subtype) (((type) & BLUFI_TYPE_MASK) | ((subtype)<<BLUFI_SUBTYPE_SHIFT))
98 
99 #define BLUFI_TYPE_CTRL                                 0x0
100 #define BLUFI_TYPE_CTRL_SUBTYPE_ACK                     0x00
101 #define BLUFI_TYPE_CTRL_SUBTYPE_SET_SEC_MODE            0x01
102 #define BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE         0x02
103 #define BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP              0x03
104 #define BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP         0x04
105 #define BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS         0x05
106 #define BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA      0x06
107 #define BLUFI_TYPE_CTRL_SUBTYPE_GET_VERSION             0x07
108 #define BLUFI_TYPE_CTRL_SUBTYPE_DISCONNECT_BLE          0x08
109 #define BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_LIST           0x09
110 
111 #define BLUFI_TYPE_DATA                                 0x1
112 #define BLUFI_TYPE_DATA_SUBTYPE_NEG                     0x00
113 #define BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID               0x01
114 #define BLUFI_TYPE_DATA_SUBTYPE_STA_SSID                0x02
115 #define BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD              0x03
116 #define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID             0x04
117 #define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD           0x05
118 #define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM     0x06
119 #define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE        0x07
120 #define BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL          0x08
121 #define BLUFI_TYPE_DATA_SUBTYPE_USERNAME                0x09
122 #define BLUFI_TYPE_DATA_SUBTYPE_CA                      0x0a
123 #define BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT             0x0b
124 #define BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT             0x0c
125 #define BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY         0x0d
126 #define BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY         0x0e
127 #define BLUFI_TYPE_DATA_SUBTYPE_WIFI_REP                0x0f
128 #define BLUFI_TYPE_DATA_SUBTYPE_REPLY_VERSION           0x10
129 #define BLUFI_TYPE_DATA_SUBTYPE_WIFI_LIST               0x11
130 #define BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO              0x12
131 #define BLUFI_TYPE_DATA_SUBTYPE_CUSTOM_DATA             0x13
132 #define BLUFI_TYPE_IS_CTRL(type)        (BLUFI_GET_TYPE((type)) == BLUFI_TYPE_CTRL)
133 #define BLUFI_TYPE_IS_DATA(type)        (BLUFI_GET_TYPE((type)) == BLUFI_TYPE_DATA)
134 
135 #define BLUFI_TYPE_IS_CTRL_ACK(type)                 (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_ACK)
136 #define BLUFI_TYPE_IS_CTRL_START_NEG(type)           (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_START_NEG)
137 #define BLUFI_TYPE_IS_CTRL_STOP_NEG(type)            (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_STOP_NEG)
138 #define BLUFI_TYPE_IS_CTRL_SET_WIFI_OPMODE(type)     (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_SET_WIFI_OPMODE)
139 #define BLUFI_TYPE_IS_CTRL_CONN_WIFI(type)           (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_CONN_TO_AP)
140 #define BLUFI_TYPE_IS_CTRL_DISCONN_WIFI(type)        (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_DISCONN_FROM_AP)
141 #define BLUFI_TYPE_IS_CTRL_GET_WIFI_STATUS(type)     (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_GET_WIFI_STATUS)
142 #define BLUFI_TYPE_IS_CTRL_DEAUTHENTICATE_STA(type)  (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_DEAUTHENTICATE_STA)
143 #define BLUFI_TYPE_IS_CTRL_GET_VERSION(type)         (BLUFI_TYPE_IS_CTRL((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_CTRL_SUBTYPE_GET_VERSION)
144 
145 #define BLUFI_TYPE_IS_DATA_NEG(type)                 (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_NEG)
146 #define BLUFI_TYPE_IS_DATA_STA_BSSID(type)           (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_BSSID)
147 #define BLUFI_TYPE_IS_DATA_STA_SSID(type)            (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_SSID)
148 #define BLUFI_TYPE_IS_DATA_STA_PASSWD(type)          (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_STA_PASSWD)
149 #define BLUFI_TYPE_IS_DATA_SOFTAP_SSID(type)         (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_SSID)
150 #define BLUFI_TYPE_IS_DATA_SOFTAP_PASSWD(type)       (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_PASSWD)
151 #define BLUFI_TYPE_IS_DATA_SOFTAP_MAX_CONN_NUM(type) (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM)
152 #define BLUFI_TYPE_IS_DATA_SOFTAP_AUTH_MODE(type)    (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_AUTH_MODE)
153 #define BLUFI_TYPE_IS_DATA_SOFTAP_CHANNEL(type)      (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL)
154 #define BLUFI_TYPE_IS_DATA_USERNAME(type)            (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_USERNAME)
155 #define BLUFI_TYPE_IS_DATA_CA(type)                  (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CA)
156 #define BLUFI_TYPE_IS_DATA_CLEINT_CERT(type)         (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CLIENT_CERT)
157 #define BLUFI_TYPE_IS_DATA_SERVER_CERT(type)         (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SERVER_CERT)
158 #define BLUFI_TYPE_IS_DATA_CLIENT_PRIV_KEY(type)     (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_CLIENT_PRIV_KEY)
159 #define BLUFI_TYPE_IS_DATA_SERVER_PRIV_KEY(type)     (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_SERVER_PRIV_KEY)
160 #define BLUFI_TYPE_IS_DATA_ERROR_INFO(type)          (BLUFI_TYPE_IS_DATA((type)) && BLUFI_GET_SUBTYPE((type)) == BLUFI_TYPE_DATA_SUBTYPE_ERROR_INFO)
161 
162 // packet frame control
163 #define BLUFI_FC_ENC_MASK       0x01
164 #define BLUFI_FC_CHECK_MASK     0x02
165 #define BLUFI_FC_DIR_MASK       0x04
166 #define BLUFI_FC_REQ_ACK_MASK   0x08
167 #define BLUFI_FC_FRAG_MASK      0x10
168 
169 #define BLUFI_FC_ENC            0x01
170 #define BLUFI_FC_CHECK          0x02
171 #define BLUFI_FC_DIR_P2E        0x00
172 #define BLUFI_FC_DIR_E2P        0x04
173 #define BLUFI_FC_REQ_ACK        0x08
174 #define BLUFI_FC_FRAG           0x10
175 
176 #define BLUFI_FC_IS_ENC(fc)       ((fc) & BLUFI_FC_ENC_MASK)
177 #define BLUFI_FC_IS_CHECK(fc)     ((fc) & BLUFI_FC_CHECK_MASK)
178 #define BLUFI_FC_IS_DIR_P2E(fc)   ((fc) & BLUFI_FC_DIR_P2E_MASK)
179 #define BLUFI_FC_IS_DIR_E2P(fc)   (!((fc) & BLUFI_DIR_P2E_MASK))
180 #define BLUFI_FC_IS_REQ_ACK(fc)   ((fc) & BLUFI_FC_REQ_ACK_MASK)
181 #define BLUFI_FC_IS_FRAG(fc)      ((fc) & BLUFI_FC_FRAG_MASK)
182 
183 /* default GATT MTU size over LE link
184 */
185 #define GATT_DEF_BLE_MTU_SIZE               23
186 /* BLUFI HEADER + TOTAL(REMAIN) LENGTH + CRC + L2CAP RESERVED */
187 #define BLUFI_MTU_RESERVED_SIZE     (sizeof(struct blufi_hdr) + 2 + 2 + 3)
188 #define BLUFI_FRAG_DATA_DEFAULT_LEN (GATT_DEF_BLE_MTU_SIZE - BLUFI_MTU_RESERVED_SIZE)
189 
190 //function declare
191 void btc_blufi_protocol_handler(uint8_t type, uint8_t *data, int len);
192 
193 void btc_blufi_send_encap(uint8_t type, uint8_t *data, int data_len);
194 
195 void btc_blufi_set_callbacks(esp_blufi_callbacks_t *callbacks);
196 
197 void btc_blufi_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
198 
199 void btc_blufi_cb_deep_free(btc_msg_t *msg);
200 
201 #endif /* __BLUFI_INT_H__ */
202 #endif  ///BLUFI_INCLUDED == TRUE
203