1 /*******************************************************************************
2 * \file cybt_host_stack_platform_interface.c
3 *
4 * \brief
5 * This file implements the WICED BT stack porting interface.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2018-2019 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24
25 #include <string.h>
26
27 #include "cyabs_rtos.h"
28 #include "wiced_bt_stack_platform.h"
29 #include "cybt_platform_hci.h"
30 #include "cybt_platform_task.h"
31 #include "cybt_platform_interface.h"
32 #include "cybt_platform_config.h"
33 #include "cybt_platform_trace.h"
34 #include "cybt_platform_util.h"
35 #include "cyhal_lptimer.h"
36 //#include "cycfg_system.h"
37 #include "cybt_platform_internal.h"
38
39 /******************************************************************************
40 * Variables Definitions
41 ******************************************************************************/
42 cy_mutex_t bt_stack_mutex;
43 extern wiced_bool_t bSupressProtocolTraces;
44 char bt_trace_buf[CYBT_TRACE_BUFFER_SIZE];
45
46 /******************************************************************************
47 * Function Declarations
48 ******************************************************************************/
49 extern uint8_t *cybt_platform_hci_get_buffer(hci_packet_type_t pti, uint32_t size);
50
51 /******************************************************************************
52 * Function Definitions
53 ******************************************************************************/
host_stack_exception_handler(uint16_t code,char * msg,void * ptr)54 void host_stack_exception_handler(uint16_t code, char* msg, void* ptr)
55 {
56 SPIF_TRACE_ERROR("[Exception] code = 0x%x, msg = %s", code, msg);
57 }
58
59 BTSTACK_PORTING_SECTION_BEGIN
host_stack_mutex_lock(void * p_lock_context)60 void host_stack_mutex_lock(void * p_lock_context)
61 {
62 cy_rtos_get_mutex(&bt_stack_mutex, CY_RTOS_NEVER_TIMEOUT);
63 }
64 BTSTACK_PORTING_SECTION_END
65
66 BTSTACK_PORTING_SECTION_BEGIN
host_stack_mutex_unlock(void * p_lock_context)67 void host_stack_mutex_unlock(void * p_lock_context)
68 {
69 cy_rtos_set_mutex(&bt_stack_mutex);
70 }
71 BTSTACK_PORTING_SECTION_END
72
73 BTSTACK_PORTING_SECTION_BEGIN
host_stack_get_acl_to_lower_buffer(wiced_bt_transport_t transport,uint32_t size)74 uint8_t *host_stack_get_acl_to_lower_buffer(wiced_bt_transport_t transport, uint32_t size)
75 {
76 uint8_t *p_bt_msg;
77
78 p_bt_msg = cybt_platform_hci_get_buffer(HCI_PACKET_TYPE_ACL, size);
79
80 if(NULL == p_bt_msg)
81 {
82 SPIF_TRACE_ERROR("get_acl_to_lower_buffer() failure ");
83 }
84
85 return (p_bt_msg);
86 }
87 BTSTACK_PORTING_SECTION_END
88
89 BTSTACK_PORTING_SECTION_BEGIN
host_stack_send_acl_to_lower(wiced_bt_transport_t transport,uint8_t * p_data,uint16_t len)90 wiced_result_t host_stack_send_acl_to_lower(wiced_bt_transport_t transport,
91 uint8_t *p_data,
92 uint16_t len
93 )
94 {
95 cybt_result_t result;
96 wiced_result_t ret_val = WICED_SUCCESS;
97
98 SPIF_TRACE_DEBUG("send_acl_to_lower(): p_data = %p, len = %d",
99 p_data,
100 len
101 );
102
103 if(NULL == p_data || 0 == len)
104 {
105 SPIF_TRACE_ERROR("send_acl_to_lower(): Invalid data(%p) or length(%d)",
106 p_data,
107 len
108 );
109 return WICED_ERROR;
110 }
111
112 result = cybt_platform_hci_write(HCI_PACKET_TYPE_ACL, p_data, len);
113 if(CYBT_SUCCESS != result)
114 {
115 SPIF_TRACE_ERROR("send_acl_to_lower(): hci write failed (ret = 0x%x)",
116 result
117 );
118 ret_val = WICED_ERROR;
119 }
120
121 return (ret_val);
122 }
123 BTSTACK_PORTING_SECTION_END
124
125 BTSTACK_PORTING_SECTION_BEGIN
host_stack_send_iso_to_lower(uint8_t * p_data,uint16_t len)126 wiced_result_t host_stack_send_iso_to_lower(uint8_t *p_data,
127 uint16_t len
128 )
129 {
130 cybt_result_t result;
131 wiced_result_t ret_val = WICED_SUCCESS;
132 uint8_t *p_bt_msg;
133
134 SPIF_TRACE_DEBUG("send_iso_to_lower(): p_data = %p, len = %d",
135 p_data,
136 len
137 );
138
139 if(NULL == p_data || 0 == len)
140 {
141 SPIF_TRACE_ERROR("send_iso_to_lower(): Invalid data(%p) or length(%d)",
142 p_data,
143 len
144 );
145 return WICED_ERROR;
146 }
147
148 p_bt_msg = cybt_platform_hci_get_buffer(HCI_PACKET_TYPE_ISO, len);
149
150 if(NULL == p_bt_msg)
151 {
152 SPIF_TRACE_ERROR("send_iso_to_lower(): get buffer failure ");
153 return WICED_ERROR;
154 }
155
156 memcpy(p_bt_msg, p_data, len);
157
158 result = cybt_platform_hci_write(HCI_PACKET_TYPE_ISO, p_bt_msg, len);
159
160 if(CYBT_SUCCESS != result)
161 {
162 SPIF_TRACE_ERROR("send_iso_to_lower(): hci write failed (ret = 0x%x)",
163 result
164 );
165 ret_val = WICED_ERROR;
166 }
167
168 return (ret_val);
169 }
170 BTSTACK_PORTING_SECTION_END
171
host_stack_send_cmd_to_lower(uint8_t * p_cmd,uint16_t cmd_len)172 wiced_result_t host_stack_send_cmd_to_lower(uint8_t *p_cmd, uint16_t cmd_len)
173 {
174 cybt_result_t result;
175 uint8_t *p_bt_msg;
176
177 SPIF_TRACE_DEBUG("send_cmd_to_lower(): cmd = %p, cmd_len = %d",
178 p_cmd,
179 cmd_len
180 );
181
182 if(NULL == p_cmd || 0 == cmd_len)
183 {
184 SPIF_TRACE_ERROR("send_cmd_to_lower(): Invalid command (%p) or length(%d)",
185 p_cmd,
186 cmd_len
187 );
188 return WICED_ERROR;
189 }
190
191 p_bt_msg = cybt_platform_hci_get_buffer(HCI_PACKET_TYPE_COMMAND, cmd_len);
192
193 if(NULL == p_bt_msg)
194 return WICED_ERROR;
195
196 memcpy(p_bt_msg, p_cmd, cmd_len);
197
198 result = cybt_platform_hci_write(HCI_PACKET_TYPE_COMMAND, p_bt_msg, cmd_len);
199
200 if(CYBT_SUCCESS != result)
201 {
202 SPIF_TRACE_ERROR("send_cmd_cmd_lower(): hci write failed (ret = 0x%x)",
203 result
204 );
205 return WICED_ERROR;
206 }
207
208 return WICED_SUCCESS;
209 }
210
host_stack_get_sco_to_lower_buffer(uint32_t size)211 uint8_t *host_stack_get_sco_to_lower_buffer(uint32_t size)
212 {
213 //NA
214 return (NULL);
215 }
216
host_stack_send_sco_to_lower(uint8_t * p_data,uint8_t len)217 wiced_result_t host_stack_send_sco_to_lower(uint8_t* p_data, uint8_t len)
218 {
219 //NA
220 return WICED_SUCCESS;
221 }
222
host_stack_print_trace_log(char * p_trace_buf,int trace_buf_len,wiced_bt_trace_type_t trace_type)223 void host_stack_print_trace_log(char *p_trace_buf,
224 int trace_buf_len,
225 wiced_bt_trace_type_t trace_type
226 )
227 {
228 switch(trace_type)
229 {
230 case WICED_BT_TRACE_ERROR:
231 STACK_TRACE_ERROR("%s", p_trace_buf);
232 break;
233 case WICED_BT_TRACE_WARN:
234 STACK_TRACE_WARNING("%s", p_trace_buf);
235 break;
236 case WICED_BT_TRACE_API:
237 STACK_TRACE_API("%s", p_trace_buf);
238 break;
239 case WICED_BT_TRACE_EVENT:
240 STACK_TRACE_EVENT("%s", p_trace_buf);
241 break;
242 case WICED_BT_TRACE_DEBUG:
243 STACK_TRACE_DEBUG("%s", p_trace_buf);
244 break;
245 default:
246 break;
247 }
248 }
249
host_stack_platform_interface_init(void)250 void host_stack_platform_interface_init(void)
251 {
252 wiced_bt_stack_platform_t platform_interface = {0};
253 wiced_result_t result;
254 cy_rslt_t cy_result;
255
256 extern void bt_post_reset_cback(void);
257
258 platform_interface.pf_exception = host_stack_exception_handler;
259 platform_interface.pf_os_malloc = cybt_platform_malloc;
260 platform_interface.pf_os_free = cybt_platform_free;
261 platform_interface.pf_get_tick_count_64 = cybt_platform_get_tick_count_us;
262 platform_interface.pf_set_next_timeout = cybt_platform_set_next_timeout;
263 platform_interface.stack_lock.p_lock_context = NULL;
264 platform_interface.stack_lock.pf_lock_func = host_stack_mutex_lock;
265 platform_interface.stack_lock.pf_unlock_func = host_stack_mutex_unlock;
266 platform_interface.pf_get_acl_to_lower_buffer = host_stack_get_acl_to_lower_buffer;
267 platform_interface.pf_write_acl_to_lower = host_stack_send_acl_to_lower;
268 platform_interface.pf_write_cmd_to_lower = host_stack_send_cmd_to_lower;
269 platform_interface.pf_get_sco_to_lower_buffer = host_stack_get_sco_to_lower_buffer;
270 platform_interface.pf_write_sco_to_lower = host_stack_send_sco_to_lower;
271 platform_interface.pf_write_iso_to_lower = host_stack_send_iso_to_lower;
272 platform_interface.pf_hci_trace_cback_t = NULL;
273 platform_interface.pf_debug_trace = host_stack_print_trace_log;
274 platform_interface.trace_buffer = bt_trace_buf;
275 platform_interface.trace_buffer_len = CYBT_TRACE_BUFFER_SIZE;
276 platform_interface.pf_patch_download = PATCH_DOWNLOAD_FN;
277 platform_interface.is_legacy_bless_controller = BLESS_CONTROLLER;
278 memset(bt_trace_buf, 0, CYBT_TRACE_BUFFER_SIZE);
279
280 cy_result = cy_rtos_init_mutex(&bt_stack_mutex);
281 if(CY_RSLT_SUCCESS != cy_result)
282 {
283 SPIF_TRACE_ERROR("platform_interface_init(): lock init failed, result = 0x%x", cy_result);
284 }
285
286 result = wiced_bt_stack_platform_initialize(&platform_interface);
287
288 if(WICED_SUCCESS != result)
289 {
290 SPIF_TRACE_ERROR("platform_interface_init(): failed, result = 0x%x", result);
291 }
292 }
293
host_stack_platform_interface_deinit(void)294 void host_stack_platform_interface_deinit(void)
295 {
296 cy_rtos_deinit_mutex(&bt_stack_mutex);
297 }
298
299