1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Copyright 2021 NXP.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "uci_hmsgs.h"
23 #include "uci_log.h"
24 #include "uwa_dm_int.h"
25 #include "uwa_sys.h"
26 #include "uwb_api.h"
27 #include "uwb_gki.h"
28 #include "uwb_hal_api.h"
29 #include "uwb_hal_int.h"
30 #include "uwb_int.h"
31 #include "uwb_osal_common.h"
32 #include "uwb_target.h"
33
34 /****************************************************************************
35 ** Declarations
36 ****************************************************************************/
37 tUWB_CB uwb_cb;
38
39 /*******************************************************************************
40 **
41 ** Function uwb_state_name
42 **
43 ** Description This function returns the state name.
44 **
45 ** NOTE conditionally compiled to save memory.
46 **
47 ** Returns pointer to the name
48 **
49 *******************************************************************************/
uwb_state_name(uint8_t state)50 std::string uwb_state_name(uint8_t state) {
51 switch (state) {
52 case UWB_STATE_NONE:
53 return "NONE";
54 case UWB_STATE_W4_HAL_OPEN:
55 return "W4_HAL_OPEN";
56 case UWB_STATE_IDLE:
57 return "IDLE";
58 case UWB_STATE_ACTIVE:
59 return "ACTIVE";
60 case UWB_STATE_CLOSING:
61 return "CLOSING";
62 case UWB_STATE_W4_HAL_CLOSE:
63 return "W4_HAL_CLOSE";
64 default:
65 return "???? UNKNOWN STATE";
66 }
67 }
68
69 /*******************************************************************************
70 **
71 ** Function uwb_hal_event_name
72 **
73 ** Description This function returns the HAL event name.
74 **
75 ** NOTE conditionally compiled to save memory.
76 **
77 ** Returns pointer to the name
78 **
79 *******************************************************************************/
80
uwb_hal_event_name(uint8_t event)81 std::string uwb_hal_event_name(uint8_t event) {
82 switch (event) {
83 case HAL_UWB_OPEN_CPLT_EVT:
84 return "HAL_UWB_OPEN_CPLT_EVT";
85
86 case HAL_UWB_CLOSE_CPLT_EVT:
87 return "HAL_UWB_CLOSE_CPLT_EVT";
88
89 case HAL_UWB_ERROR_EVT:
90 return "HAL_UWB_ERROR_EVT";
91
92 default:
93 return "???? UNKNOWN EVENT";
94 }
95 }
96
97 /*******************************************************************************
98 **
99 ** Function uwb_main_notify_enable_status
100 **
101 ** Description Notify status of Enable/PowerOffSleep/PowerCycle
102 **
103 *******************************************************************************/
uwb_main_notify_enable_status(tUWB_STATUS uwb_status)104 static void uwb_main_notify_enable_status(tUWB_STATUS uwb_status) {
105 tUWB_RESPONSE evt_data;
106
107 evt_data.status = uwb_status;
108
109 if (uwb_cb.p_resp_cback) {
110 (*uwb_cb.p_resp_cback)(UWB_ENABLE_REVT, &evt_data);
111 }
112 }
113
114 /*******************************************************************************
115 **
116 ** Function uwb_enabled
117 **
118 ** Description UWBC enabled, proceed with stack start up.
119 **
120 ** Returns void
121 **
122 *******************************************************************************/
uwb_enabled(tUWB_STATUS uwb_status,UWB_HDR * p_init_rsp_msg)123 void uwb_enabled(tUWB_STATUS uwb_status,
124 __attribute__((unused)) UWB_HDR* p_init_rsp_msg) {
125 tUWB_RESPONSE evt_data;
126
127 memset(&evt_data, 0, sizeof(tUWB_RESPONSE));
128
129 if (uwb_status == UCI_STATUS_OK) {
130 uwb_set_state(UWB_STATE_IDLE);
131 }
132 uwb_main_notify_enable_status(uwb_status);
133 }
134
135 /*******************************************************************************
136 **
137 ** Function uwb_set_state
138 **
139 ** Description Set the state of UWB stack
140 **
141 ** Returns void
142 **
143 *******************************************************************************/
uwb_set_state(tUWB_STATE uwb_state)144 void uwb_set_state(tUWB_STATE uwb_state) {
145 UCI_TRACE_I("uwb_set_state %d (%s)->%d (%s)", uwb_cb.uwb_state,
146 uwb_state_name(uwb_cb.uwb_state).c_str(), uwb_state,
147 uwb_state_name(uwb_state).c_str());
148 uwb_cb.uwb_state = uwb_state;
149 }
150
151 /*******************************************************************************
152 **
153 ** Function uwb_gen_cleanup
154 **
155 ** Description Clean up for both going into low power mode and disabling
156 ** UWB
157 **
158 *******************************************************************************/
uwb_gen_cleanup(void)159 void uwb_gen_cleanup(void) {
160 /* clear any pending CMD/RSP */
161 uwb_main_flush_cmd_queue();
162 }
163
164 /*******************************************************************************
165 **
166 ** Function uwb_main_handle_hal_evt
167 **
168 ** Description Handle BT_EVT_TO_UWB_MSGS
169 **
170 *******************************************************************************/
uwb_main_handle_hal_evt(tUWB_HAL_EVT_MSG * p_msg)171 void uwb_main_handle_hal_evt(tUWB_HAL_EVT_MSG* p_msg) {
172 // uint8_t* ps;
173
174 UCI_TRACE_I("HAL event=0x%x", p_msg->hal_evt);
175
176 switch (p_msg->hal_evt) {
177 case HAL_UWB_OPEN_CPLT_EVT: /* only for failure case */
178 uwb_enabled(UWB_STATUS_FAILED, NULL);
179 break;
180
181 case HAL_UWB_CLOSE_CPLT_EVT:
182 if (uwb_cb.p_resp_cback) {
183 if (uwb_cb.uwb_state == UWB_STATE_W4_HAL_CLOSE) {
184 uwb_set_state(UWB_STATE_NONE);
185 (*uwb_cb.p_resp_cback)(UWB_DISABLE_REVT, NULL);
186 uwb_cb.p_resp_cback = NULL;
187 } else {
188 /* found error during initialization */
189 uwb_set_state(UWB_STATE_NONE);
190 uwb_main_notify_enable_status(UWB_STATUS_FAILED);
191 }
192 }
193 break;
194
195 case HAL_UWB_ERROR_EVT:
196 switch (p_msg->status) {
197 case HAL_UWB_STATUS_ERR_TRANSPORT:
198 /* Notify app of transport error */
199 if (uwb_cb.p_resp_cback) {
200 (*uwb_cb.p_resp_cback)(UWB_UWBS_TRANSPORT_ERR_REVT, NULL);
201
202 /* if enabling UWB, notify upper layer of failure after closing HAL
203 */
204 if (uwb_cb.uwb_state < UWB_STATE_IDLE) {
205 uwb_enabled(UWB_STATUS_FAILED, NULL);
206 }
207 }
208 break;
209 default:
210 break;
211 }
212 break;
213 default:
214 UCI_TRACE_E("unhandled event (0x%x).", p_msg->hal_evt);
215 break;
216 }
217 }
218
219 /*******************************************************************************
220 **
221 ** Function uwb_main_flush_cmd_queue
222 **
223 ** Description This function is called when setting power off sleep state.
224 **
225 ** Returns void
226 **
227 *******************************************************************************/
uwb_main_flush_cmd_queue(void)228 void uwb_main_flush_cmd_queue(void) {
229 UWB_HDR* p_msg;
230
231 UCI_TRACE_I(__func__);
232
233 /* initialize command window */
234 uwb_cb.uci_cmd_window = UCI_MAX_CMD_WINDOW;
235
236 /* Stop command-pending timer */
237 uwb_stop_quick_timer(&uwb_cb.uci_wait_rsp_timer);
238 uwb_cb.is_resp_pending = false;
239 uwb_cb.cmd_retry_count = 0;
240
241 /* dequeue and free buffer */
242 while ((p_msg = (UWB_HDR*)phUwb_GKI_dequeue(&uwb_cb.uci_cmd_xmit_q)) !=
243 NULL) {
244 phUwb_GKI_freebuf(p_msg);
245 }
246 }
247
248 /*******************************************************************************
249 **
250 ** Function uwb_main_post_hal_evt
251 **
252 ** Description This function posts HAL event to UWB_TASK
253 **
254 ** Returns void
255 **
256 *******************************************************************************/
uwb_main_post_hal_evt(uint8_t hal_evt,tUWB_STATUS status)257 void uwb_main_post_hal_evt(uint8_t hal_evt, tUWB_STATUS status) {
258 tUWB_HAL_EVT_MSG* p_msg;
259
260 p_msg = (tUWB_HAL_EVT_MSG*)phUwb_GKI_getbuf(sizeof(tUWB_HAL_EVT_MSG));
261 if (p_msg != NULL) {
262 /* Initialize UWB_HDR */
263 p_msg->hdr.len = 0;
264 p_msg->hdr.event = BT_EVT_TO_UWB_MSGS;
265 p_msg->hdr.offset = 0;
266 p_msg->hdr.layer_specific = 0;
267 p_msg->hal_evt = hal_evt;
268 p_msg->status = status;
269 phUwb_GKI_send_msg(UWB_TASK, UWB_MBOX_ID, p_msg);
270 } else {
271 UCI_TRACE_E("No buffer");
272 }
273 }
274
275 /*******************************************************************************
276 **
277 ** Function uwb_main_hal_cback
278 **
279 ** Description HAL event handler
280 **
281 ** Returns void
282 **
283 *******************************************************************************/
uwb_main_hal_cback(uint8_t event,tUWB_STATUS status)284 static void uwb_main_hal_cback(uint8_t event, tUWB_STATUS status) {
285 UCI_TRACE_I("uwb_main_hal_cback event: %s(0x%x), status=%d",
286 uwb_hal_event_name(event).c_str(), event, status);
287 switch (event) {
288 case HAL_UWB_OPEN_CPLT_EVT:
289 /*
290 ** if UWB_Disable() is called before receiving HAL_UWB_OPEN_CPLT_EVT,
291 ** then wait for HAL_UWB_CLOSE_CPLT_EVT.
292 */
293 if (uwb_cb.uwb_state == UWB_STATE_W4_HAL_OPEN) {
294 if (status == HAL_UWB_STATUS_OK) {
295 /* Notify UWB_TASK that UCI transport is initialized */
296 phUwb_GKI_send_event(UWB_TASK, UWB_TASK_EVT_TRANSPORT_READY);
297 } else {
298 uwb_main_post_hal_evt(event, status);
299 }
300 }
301 break;
302
303 case HAL_UWB_CLOSE_CPLT_EVT:
304 case HAL_UWB_ERROR_EVT:
305 uwb_main_post_hal_evt(event, status);
306 break;
307
308 default:
309 UCI_TRACE_E("uwb_main_hal_cback unhandled event %x", event);
310 break;
311 }
312 }
313
314 /*******************************************************************************
315 **
316 ** Function uwb_main_hal_data_cback
317 **
318 ** Description HAL data event handler
319 **
320 ** Returns void
321 **
322 *******************************************************************************/
uwb_main_hal_data_cback(uint16_t data_len,uint8_t * p_data)323 static void uwb_main_hal_data_cback(uint16_t data_len, uint8_t* p_data) {
324 UWB_HDR* p_msg;
325
326 /* ignore all data while shutting down Helio */
327 if (uwb_cb.uwb_state == UWB_STATE_W4_HAL_CLOSE ||
328 uwb_cb.uwb_state == UWB_STATE_W4_HAL_OPEN) {
329 return;
330 }
331 if (p_data) {
332 p_msg = (UWB_HDR*)phUwb_GKI_getpoolbuf(UWB_UCI_POOL_ID);
333 if (p_msg != NULL) {
334 /* Initialize UWB_HDR */
335 p_msg->len = data_len;
336 p_msg->event = BT_EVT_TO_UWB_UCI;
337 p_msg->offset = UWB_RECEIVE_MSGS_OFFSET;
338 /* no need to check length, it always less than pool size */
339 memcpy((uint8_t*)(p_msg + 1) + p_msg->offset, p_data, p_msg->len);
340
341 phUwb_GKI_send_msg(UWB_TASK, UWB_MBOX_ID, p_msg);
342 } else {
343 UCI_TRACE_E("No buffer");
344 }
345 }
346 }
347
348 /*******************************************************************************
349 **
350 ** Function UWB_Enable
351 **
352 ** Description This function enables UWBS. Prior to calling UWB_Enable:
353 ** - the UWBS must be powered up, and ready to receive
354 ** commands.
355 ** - GKI must be enabled
356 ** - UWB_TASK must be started
357 ** - UCIT_TASK must be started (if using dedicated UCI
358 ** transport)
359 **
360 ** This function opens the UCI transport (if applicable),
361 ** resets the UWB Subsystem, and initializes the UWB
362 ** subsystems.
363 **
364 ** When the UWB startup procedure is completed, an
365 ** UWB_ENABLE_REVT is returned to the application using the
366 ** tUWB_RESPONSE_CBACK.
367 **
368 ** Returns tUWB_STATUS
369 **
370 *******************************************************************************/
UWB_Enable(tUWB_RESPONSE_CBACK * p_cback,tUWB_TEST_RESPONSE_CBACK * p_test_cback)371 tUWB_STATUS UWB_Enable(tUWB_RESPONSE_CBACK* p_cback,
372 tUWB_TEST_RESPONSE_CBACK* p_test_cback) {
373 UCI_TRACE_I(__func__);
374 /* Validate callback */
375 if (!p_cback) {
376 return (UWB_STATUS_INVALID_PARAM);
377 }
378 uwb_set_state(UWB_STATE_W4_HAL_OPEN);
379 uwb_cb.p_resp_cback = p_cback;
380 uwb_cb.p_test_resp_cback = p_test_cback;
381 uwb_cb.p_hal->open(uwb_main_hal_cback, uwb_main_hal_data_cback);
382 return (UWB_STATUS_OK);
383 }
384
385 /*******************************************************************************
386 **
387 ** Function UWB_Disable
388 **
389 ** Description This function performs clean up routines for shutting down
390 ** UWB and closes the UCI transport (if using dedicated UCI
391 ** transport).
392 **
393 ** When the UWB shutdown procedure is completed, an
394 ** UWB_DISABLED_REVT is returned to the application using the
395 ** tUWB_RESPONSE_CBACK.
396 **
397 ** Returns nothing
398 **
399 *******************************************************************************/
UWB_Disable(void)400 void UWB_Disable(void) {
401 UCI_TRACE_I("uwb_state = %d", uwb_cb.uwb_state);
402
403 if (uwb_cb.uwb_state == UWB_STATE_NONE) {
404 uwb_set_state(UWB_STATE_NONE);
405 if (uwb_cb.p_resp_cback) {
406 (*uwb_cb.p_resp_cback)(UWB_DISABLE_REVT, NULL);
407 uwb_cb.p_resp_cback = NULL;
408 uwb_cb.p_test_resp_cback = NULL;
409 }
410 return;
411 }
412
413 /* Close transport and clean up */
414 uwb_task_shutdown_uwbc();
415 }
416
417 /*******************************************************************************
418 **
419 ** Function UWB_Init
420 **
421 ** Description This function initializes control block for UWB
422 **
423 ** Returns nothing
424 **
425 *******************************************************************************/
UWB_Init(tHAL_UWB_CONTEXT * p_hal_entry_cntxt)426 void UWB_Init(tHAL_UWB_CONTEXT* p_hal_entry_cntxt) {
427 /* Clear uwb control block */
428 memset(&uwb_cb, 0, sizeof(tUWB_CB));
429 uwb_cb.p_hal = p_hal_entry_cntxt->hal_entry_func;
430 uwb_cb.uwb_state = UWB_STATE_NONE;
431 uwb_cb.uci_cmd_window = UCI_MAX_CMD_WINDOW;
432 uwb_cb.retry_rsp_timeout =
433 ((UWB_CMD_RETRY_TIMEOUT * QUICK_TIMER_TICKS_PER_SEC) / 1000);
434 uwb_cb.uci_wait_rsp_tout =
435 ((UWB_CMD_CMPL_TIMEOUT * QUICK_TIMER_TICKS_PER_SEC) / 1000);
436 uwb_cb.pLast_cmd_buf = NULL;
437 uwb_cb.is_resp_pending = false;
438 uwb_cb.cmd_retry_count = 0;
439 uwb_cb.is_recovery_in_progress = false;
440 uwb_cb.IsConformaceTestEnabled = false;
441 }
442
443 /*******************************************************************************
444 **
445 ** Function UWB_GetDeviceInfo
446 **
447 ** Description This function is called to get Device Info
448 ** The response from UWBS is reported with an
449 ** UWB_GET_DEVICE_INFO_REVT
450 ** in the tUWB_RESPONSE_CBACK callback.
451 **
452 ** Parameters None
453 **
454 ** Returns none
455 **
456 *******************************************************************************/
UWB_GetDeviceInfo()457 tUWB_STATUS UWB_GetDeviceInfo() { return uci_snd_get_device_info_cmd(); }
458
459 /*******************************************************************************
460 **
461 ** Function UWB_DeviceResetCommand
462 **
463 ** Description This function is called to send Device Reset Command to
464 ** UWBS.
465 ** The response from UWBS is reported with an
466 ** UWB_DEVICE_RESET_REVT
467 ** in the tUWB_RESPONSE_CBACK callback.
468 **
469 ** Parameters resetConfig - Vendor Specific Reset Config to be sent
470 **
471 ** Returns tUWB_STATUS
472 **
473 *******************************************************************************/
474
UWB_DeviceResetCommand(uint8_t resetConfig)475 tUWB_STATUS UWB_DeviceResetCommand(uint8_t resetConfig) {
476 return uci_snd_device_reset_cmd(resetConfig);
477 }
478
479 /*******************************************************************************
480 **
481 ** Function UWB_SetCoreConfig
482 **
483 ** Description This function is called to send the configuration
484 ** parameters.
485 ** The response from UWBS is reported with an
486 ** UWB_SET_CORE_CONFIG_REVT
487 ** in the tUWB_RESPONSE_CBACK callback.
488 **
489 ** Parameters tlv_size - the length of p_param_tlvs.
490 ** p_param_tlvs - the parameter ID/Len/Value list
491 **
492 ** Returns tUWB_STATUS
493 **
494 *******************************************************************************/
UWB_SetCoreConfig(uint8_t tlv_size,uint8_t * p_param_tlvs)495 tUWB_STATUS UWB_SetCoreConfig(uint8_t tlv_size, uint8_t* p_param_tlvs) {
496 return uci_snd_core_set_config_cmd(p_param_tlvs, tlv_size);
497 }
498
499 /*******************************************************************************
500 **
501 ** Function UWB_GetCoreConfig
502 **
503 ** Description This function is called to retrieve the configuration
504 ** parameters from UWBS.
505 ** The response from UWBS is reported with an
506 ** UWB_GET_CORE_CONFIG_REVT
507 ** in the tUWB_RESPONSE_CBACK callback.
508 **
509 ** Parameters num_ids - the number of parameter IDs
510 ** p_param_ids - the parameter ID list.
511 **
512 ** Returns tUWB_STATUS
513 **
514 *******************************************************************************/
UWB_GetCoreConfig(uint8_t num_ids,uint8_t * p_param_ids)515 tUWB_STATUS UWB_GetCoreConfig(uint8_t num_ids, uint8_t* p_param_ids) {
516 return uci_snd_core_get_config_cmd(p_param_ids, num_ids);
517 }
518
519 /*******************************************************************************
520 **
521 ** Function UWB_SessionInit
522 **
523 ** Description This function is called to send session init command to
524 ** UWBS.
525 ** The response from UWBS is reported with an
526 ** UWB_SESSION_INIT_REVT
527 ** in the tUWB_RESPONSE_CBACK callback.
528 **
529 ** Returns tUWB_STATUS
530 **
531 *******************************************************************************/
UWB_SessionInit(uint32_t session_id,uint8_t session_type)532 tUWB_STATUS UWB_SessionInit(uint32_t session_id, uint8_t session_type) {
533 return uci_snd_session_init_cmd(session_id, session_type);
534 }
535
536 /*******************************************************************************
537 **
538 ** Function UWB_HalSessionInit
539 **
540 ** Description This function is called to send session init command to
541 ** HAL.
542 ** Returns tUWB_STATUS
543 **
544 *******************************************************************************/
UWB_HalSessionInit(uint32_t session_id)545 tUWB_STATUS UWB_HalSessionInit(uint32_t session_id) {
546 return uwb_cb.p_hal->SessionInitialization(session_id);
547 }
548
549 /*******************************************************************************
550 **
551 ** Function UWB_SessionDeInit
552 **
553 ** Description This function is called to send session DeInit command to
554 ** UWBS.
555 ** The response from UWBS is reported with an
556 ** UWB_SESSION_DEINIT_REVT
557 ** in the tUWB_RESPONSE_CBACK callback.
558 **
559 ** Returns tUWB_STATUS
560 **
561 *******************************************************************************/
UWB_SessionDeInit(uint32_t session_id)562 tUWB_STATUS UWB_SessionDeInit(uint32_t session_id) {
563 return uci_snd_session_deinit_cmd(session_id);
564 }
565
566 /*******************************************************************************
567 **
568 ** Function UWB_GetAppConfig
569 **
570 ** Description This function is called to retrieve the parameter TLV from
571 ** UWBS.
572 ** The response from UWBS is reported with an
573 ** UWB_GET_APP_CONFIG_REVT
574 ** in the tUWB_RESPONSE_CBACK callback.
575 **
576 ** Parameters session_id - All APP configurations belonging to this
577 ** Session ID
578 ** num_ids - the number of parameter IDs
579 ** length - Length of app parameter ID
580 ** p_param_ids - the parameter ID list.
581 **
582 ** Returns tUWB_STATUS
583 **
584 *******************************************************************************/
UWB_GetAppConfig(uint32_t session_id,uint8_t num_ids,uint8_t length,uint8_t * p_param_ids)585 tUWB_STATUS UWB_GetAppConfig(uint32_t session_id, uint8_t num_ids,
586 uint8_t length, uint8_t* p_param_ids) {
587 return uci_snd_app_get_config_cmd(session_id, num_ids, length, p_param_ids);
588 }
589
590 /*******************************************************************************
591 **
592 ** Function UWB_SetAppConfig
593 **
594 ** Description This function is called to set the parameter TLV to UWBS.
595 ** The response from UWBS is reported with an
596 ** UWB_SET_APP_CONFIG_REVT
597 ** in the tUWB_RESPONSE_CBACK callback.
598 **
599 ** Parameters session_id - All APP configurations belonging to this SessionId
600 ** num_ids - the number of parameter IDs
601 ** length - Length of app parameter data
602 ** p_data - SetAppConfig TLV data
603 **
604 ** Returns tUWB_STATUS
605 **
606 *******************************************************************************/
UWB_SetAppConfig(uint32_t session_id,uint8_t num_ids,uint8_t length,uint8_t * p_data)607 tUWB_STATUS UWB_SetAppConfig(uint32_t session_id, uint8_t num_ids,
608 uint8_t length, uint8_t* p_data) {
609 return uci_snd_app_set_config_cmd(session_id, num_ids, length, p_data);
610 }
611
612 /*******************************************************************************
613 **
614 ** Function UWB_GetSessionCount
615 **
616 ** Description This function is called to send get session count command to
617 ** UWBS.
618 ** The response from UWBS is reported with an
619 ** UWB_SESSION_GET_COUNT_REVT
620 ** in the tUWB_RESPONSE_CBACK callback.
621 **
622 ** Returns tUWB_STATUS
623 **
624 *******************************************************************************/
UWB_GetSessionCount()625 tUWB_STATUS UWB_GetSessionCount() { return uci_snd_get_session_count_cmd(); }
626
627 /*******************************************************************************
628 **
629 ** Function UWB_StartRanging
630 **
631 ** Description This function is called to send the range start command to
632 ** UWBS.
633 ** The response from UWBS is reported with an
634 ** UWB_START_RANGE_REVT
635 ** in the tUWB_RESPONSE_CBACK callback.
636 **
637 ** Parameters session_id - Session ID for which ranging shall start
638 **
639 ** Returns tUWB_STATUS
640 **
641 *******************************************************************************/
UWB_StartRanging(uint32_t session_id)642 tUWB_STATUS UWB_StartRanging(uint32_t session_id) {
643 return uci_snd_range_start_cmd(session_id);
644 }
645
646 /*******************************************************************************
647 **
648 ** Function UWB_StopRanging
649 **
650 ** Description This function is called to send the range stop command to
651 ** UWBS.
652 ** The response from UWBS is reported with an UWB_STOP_RANGE_REVT
653 ** in the tUWB_RESPONSE_CBACK callback.
654 **
655 ** Parameters session_id - Session ID for which ranging shall stop
656 **
657 ** Returns tUWB_STATUS
658 **
659 *******************************************************************************/
UWB_StopRanging(uint32_t session_id)660 tUWB_STATUS UWB_StopRanging(uint32_t session_id) {
661 return uci_snd_range_stop_cmd(session_id);
662 }
663
664 /*******************************************************************************
665 **
666 ** Function UWB_GetRangingCount
667 **
668 ** Description This function is called to send get ranging count command.
669 ** The response from UWBS is reported with an
670 ** UWB_GET_RANGE_COUNT_REVT
671 ** in the tUWB_RESPONSE_CBACK callback.
672 **
673 ** Parameters session_id - Session ID for which ranging round count is
674 ** required
675 **
676 ** Returns tUWB_STATUS
677 **
678 *******************************************************************************/
UWB_GetRangingCount(uint32_t session_id)679 tUWB_STATUS UWB_GetRangingCount(uint32_t session_id) {
680 return uci_snd_get_range_count_cmd(session_id);
681 }
682
683 /*******************************************************************************
684 **
685 ** Function UWB_GetSessionStatus
686 **
687 ** Description This function is called to send get session status command.
688 ** The response from UWBS is reported with an
689 ** UWB_SESSION_GET_STATE_REVT
690 ** in the tUWB_RESPONSE_CBACK callback.
691 **
692 ** Parameters session_id - Session ID for which session state is required
693 **
694 ** Returns tUWB_STATUS
695 **
696 *******************************************************************************/
UWB_GetSessionStatus(uint32_t session_id)697 tUWB_STATUS UWB_GetSessionStatus(uint32_t session_id) {
698 return uci_snd_get_session_status_cmd(session_id);
699 }
700
701 /*******************************************************************************
702 **
703 ** Function UWB_MulticastListUpdate
704 **
705 ** Description This function is called to send the Multicast list update
706 ** command.
707 ** The response from UWBS is reported with an
708 ** UWB_SESSION_UPDATE_MULTICAST_LIST_REVT
709 ** in the tUWB_RESPONSE_CBACK callback.
710 **
711 ** Parameters session_id - Session ID
712 ** action - action
713 ** noOfControlees - No Of Controlees
714 ** shortAddress - array of short address
715 ** subSessionId - array of sub session ID
716 **
717 ** Returns tUWB_STATUS
718 **
719 *******************************************************************************/
UWB_MulticastListUpdate(uint32_t session_id,uint8_t action,uint8_t noOfControlees,uint16_t * shortAddressList,uint32_t * subSessionIdList)720 tUWB_STATUS UWB_MulticastListUpdate(uint32_t session_id, uint8_t action,
721 uint8_t noOfControlees,
722 uint16_t* shortAddressList,
723 uint32_t* subSessionIdList) {
724 return uci_snd_multicast_list_update_cmd(session_id, action, noOfControlees,
725 shortAddressList, subSessionIdList);
726 }
727
728 /*******************************************************************************
729 **
730 ** Function UWB_SetCountryCode
731 **
732 ** Description This function is called to send the country code set
733 ** command.
734 ** The response from UWBS is reported with an
735 ** UWB_SESSION_SET_COUNTRY_CODE_REVT
736 ** in the tUWB_RESPONSE_CBACK callback.
737 **
738 ** Parameters country_code - ISO Country code
739 **
740 ** Returns tUWB_STATUS
741 **
742 *******************************************************************************/
UWB_SetCountryCode(uint8_t * countryCode)743 tUWB_STATUS UWB_SetCountryCode(uint8_t* countryCode) {
744 return uci_snd_set_country_code_cmd(countryCode);
745 }
746
747 /*******************************************************************************
748 **
749 ** Function UWB_CoreGetDeviceCapability
750 **
751 ** Description This function is called to send the Core Get Capability.
752 ** The response from UWBS is reported with an
753 ** UWB_CORE_GET_DEVICE_CAPABILITY_REVT
754 ** in the tUWB_RESPONSE_CBACK callback.
755 **
756 ** Parameters None
757 **
758 ** Returns tUWB_STATUS
759 **
760 *******************************************************************************/
UWB_CoreGetDeviceCapability(void)761 tUWB_STATUS UWB_CoreGetDeviceCapability(void) {
762 return uci_snd_core_get_device_capability();
763 }
764
765 /*******************************************************************************
766 **
767 ** Function UWB_SendBlinkData
768 **
769 ** Description This function is called to send blink data tx command.
770 ** The response from UWBS is reported with an
771 ** UWB_BLINK_DATA_TX_REVT
772 ** in the tUWB_RESPONSE_CBACK callback.
773 **
774 ** Parameters session_id - Session ID
775 ** repetition_count - repetition count
776 ** app_data_len - size of application data
777 ** app_data - application data
778 **
779 ** Returns tUWB_STATUS
780 **
781 *******************************************************************************/
UWB_SendBlinkData(uint32_t session_id,uint8_t repetition_count,uint8_t app_data_len,uint8_t * app_data)782 tUWB_STATUS UWB_SendBlinkData(uint32_t session_id, uint8_t repetition_count,
783 uint8_t app_data_len, uint8_t* app_data) {
784 return uci_snd_blink_data_cmd(session_id, repetition_count, app_data_len,
785 app_data);
786 }
787
788 /* APIs for UWB RF test functionality */
789
790 /*******************************************************************************
791 **
792 ** Function UWB_TestGetConfig
793 **
794 ** Description This function is called to retrieve the test configuration
795 ** parameter from UWBS.
796 ** The response from UWBS is reported with an
797 ** UWB_TEST_GET_CONFIG_REVT
798 ** in the tUWB_RESPONSE_CBACK callback.
799 **
800 ** Parameters session_id - All TEST configurations belonging to this SessionId
801 ** num_ids - the number of parameter IDs
802 ** length - Length of test parameter ID
803 ** p_param_ids - the parameter ID list.
804 **
805 ** Returns tUWB_STATUS
806 **
807 *******************************************************************************/
UWB_TestGetConfig(uint32_t session_id,uint8_t num_ids,uint8_t length,uint8_t * p_param_ids)808 tUWB_STATUS UWB_TestGetConfig(uint32_t session_id, uint8_t num_ids,
809 uint8_t length, uint8_t* p_param_ids) {
810 return uci_snd_test_get_config_cmd(session_id, num_ids, length, p_param_ids);
811 }
812
813 /*******************************************************************************
814 **
815 ** Function UWB_SetTestConfig
816 **
817 ** Description This function is called to set the test configuration
818 ** parameters.
819 ** The response from UWBS is reported with an
820 ** UWB_TEST_SET_CONFIG_REVT
821 ** in the tUWB_RESPONSE_CBACK callback.
822 **
823 ** Parameters session_id - All TEST configurations belonging to this SessionId
824 ** num_ids - the number of parameter IDs
825 ** length - Length of test parameter data
826 ** p_data - SetAppConfig TLV data
827 **
828 ** Returns tUWB_STATUS
829 **
830 *******************************************************************************/
UWB_SetTestConfig(uint32_t session_id,uint8_t num_ids,uint8_t length,uint8_t * p_data)831 tUWB_STATUS UWB_SetTestConfig(uint32_t session_id, uint8_t num_ids,
832 uint8_t length, uint8_t* p_data) {
833 return uci_snd_test_set_config_cmd(session_id, num_ids, length, p_data);
834 }
835
836 /*******************************************************************************
837 **
838 ** Function UWB_TestPeriodicTx
839 **
840 ** Description This function is called send periodic Tx test command.
841 ** The response from UWBS is reported with an
842 ** UWB_TEST_PERIODIC_TX_REVT
843 ** in the tUWB_RESPONSE_CBACK callback.
844 **
845 ** Parameters length - Length of psdu data.
846 ** p_data - psdu data
847 **
848 ** Returns tUWB_STATUS
849 **
850 *******************************************************************************/
UWB_TestPeriodicTx(uint16_t length,uint8_t * p_data)851 tUWB_STATUS UWB_TestPeriodicTx(uint16_t length, uint8_t* p_data) {
852 return uci_snd_test_periodic_tx_cmd(length, p_data);
853 }
854
855 /*******************************************************************************
856 **
857 ** Function UWB_TestPerRx
858 **
859 ** Description This function is called send Packet Error Rate(PER) Rx test
860 ** command.
861 ** The response from UWBS is reported with an
862 ** UWB_TEST_PER_RX_REVT
863 ** in the tUWB_RESPONSE_CBACK callback.
864 **
865 ** Parameters length - Length of psdu data.
866 ** p_data - psdu data
867 **
868 ** Returns tUWB_STATUS
869 **
870 *******************************************************************************/
UWB_TestPerRx(uint16_t length,uint8_t * p_data)871 tUWB_STATUS UWB_TestPerRx(uint16_t length, uint8_t* p_data) {
872 return uci_snd_test_per_rx_cmd(length, p_data);
873 }
874
875 /*******************************************************************************
876 **
877 ** Function UWB_TestUwbLoopBack
878 **
879 ** Description This function is called send Loop Back test command.
880 ** The response from UWBS is reported with an
881 ** UWB_TEST_LOOPBACK_REVT
882 ** in the tUWB_RESPONSE_CBACK callback.
883 **
884 ** Parameters length - Length of psdu data.
885 ** p_data - psdu data
886 **
887 ** Returns tUWB_STATUS
888 **
889 *******************************************************************************/
UWB_TestUwbLoopBack(uint16_t length,uint8_t * p_data)890 tUWB_STATUS UWB_TestUwbLoopBack(uint16_t length, uint8_t* p_data) {
891 return uci_snd_test_uwb_loopback_cmd(length, p_data);
892 }
893
894 /********************************************************************************
895 **
896 ** Function UWB_TestStopSession
897 **
898 ** Description This function is called to send test session stop command.
899 ** The response from UWBS is reported with an
900 ** UWB_TEST_STOP_SESSION_REVT
901 ** in the tUWB_RESPONSE_CBACK callback.
902 **
903 ** Parameters None
904 **
905 ** Returns tUWB_STATUS
906 **
907 *******************************************************************************/
UWB_TestStopSession(void)908 tUWB_STATUS UWB_TestStopSession(void) {
909 return uci_snd_test_stop_session_cmd();
910 }
911
912 /********************************************************************************
913 **
914 ** Function UWB_TestRx
915 **
916 ** Description This function is called send Rx Test command.
917 ** The response from UWBS is reported with an UWB_TEST_RX_REVT
918 ** in the tUWB_RESPONSE_CBACK callback.
919 **
920 ** Parameters None
921 **
922 ** Returns tUWB_STATUS
923 **
924 *******************************************************************************/
UWB_TestRx(void)925 tUWB_STATUS UWB_TestRx(void) { return uci_snd_test_rx_cmd(); }
926
927 /*******************************************************************************
928 **
929 ** Function UWB_SendRawCommand
930 **
931 ** Description This function is called to send the given raw command to
932 ** UWBS. The response from UWBC is reported to the given
933 ** tUWB_RAW_CBACK.
934 **
935 ** Parameters p_data - The command buffer
936 **
937 ** Returns tUWB_STATUS
938 **
939 *******************************************************************************/
UWB_SendRawCommand(UWB_HDR * p_data,tUWB_RAW_CBACK * p_cback)940 tUWB_STATUS UWB_SendRawCommand(UWB_HDR* p_data, tUWB_RAW_CBACK* p_cback) {
941 /* Validate parameters */
942 if (p_data == NULL) {
943 return UWB_STATUS_INVALID_PARAM;
944 }
945
946 p_data->event = BT_EVT_TO_UWB_UCI;
947 p_data->layer_specific = UWB_WAIT_RSP_RAW_CMD;
948 /* save the callback function in the BT_HDR, to receive the response */
949 ((tUWB_UCI_RAW_MSG*)p_data)->p_cback = p_cback;
950
951 uwb_ucif_check_cmd_queue(p_data);
952 return UWB_STATUS_OK;
953 }
954
955 /*******************************************************************************
956 **
957 ** Function UWB_EnableConformanceTest
958 **
959 ** Description This function is called to set MCTT/PCTT mode.
960 ** In this mode application is sending raw UCI packets.
961 **
962 ** Parameters p_data - The data buffer
963 **
964 ** Returns None
965 **
966 *******************************************************************************/
UWB_EnableConformanceTest(uint8_t enable)967 void UWB_EnableConformanceTest(uint8_t enable) {
968 uwb_cb.IsConformaceTestEnabled = enable;
969 }
970
971 /*******************************************************************************
972 **
973 ** Function UWB_GetStatusName
974 **
975 ** Description This function returns the status name.
976 **
977 ** NOTE conditionally compiled to save memory.
978 **
979 ** Returns pointer to the name
980 **
981 *******************************************************************************/
UWB_GetStatusName(tUWB_STATUS status)982 const uint8_t* UWB_GetStatusName(tUWB_STATUS status) {
983 switch (status) {
984 case UWB_STATUS_OK:
985 return (uint8_t*)"OK";
986 case UWB_STATUS_REJECTED:
987 return (uint8_t*)"REJECTED";
988 case UWB_STATUS_FAILED:
989 return (uint8_t*)"FAILED";
990 case UWB_STATUS_SYNTAX_ERROR:
991 return (uint8_t*)"SYNTAX_ERROR";
992 case UWB_STATUS_UNKNOWN_GID:
993 return (uint8_t*)"UNKNOWN_GID";
994 case UWB_STATUS_UNKNOWN_OID:
995 return (uint8_t*)"UNKNOWN_OID";
996 case UWB_STATUS_INVALID_PARAM:
997 return (uint8_t*)"INVALID_PARAM";
998 case UWB_STATUS_INVALID_RANGE:
999 return (uint8_t*)"INVALID_RANGE";
1000 case UWB_STATUS_READ_ONLY:
1001 return (uint8_t*)"READ_ONLY";
1002 case UWB_STATUS_COMMAND_RETRY:
1003 return (uint8_t*)"COMMAND_RETRY";
1004 case UWB_STATUS_SESSSION_NOT_EXIST:
1005 return (uint8_t*)"SESSION_NOT_EXIST";
1006 case UWB_STATUS_SESSSION_DUPLICATE:
1007 return (uint8_t*)"SESSION_DUPLICATE";
1008 case UWB_STATUS_SESSSION_ACTIVE:
1009 return (uint8_t*)"SESSION_IN_ACTIVE";
1010 case UWB_STATUS_MAX_SESSSIONS_EXCEEDED:
1011 return (uint8_t*)"MAX_SESSION_REACHED";
1012 case UWB_STATUS_SESSION_NOT_CONFIGURED:
1013 return (uint8_t*)"SESSION_NOT_CONFIGURED";
1014 case UWB_STATUS_RANGING_TX_FAILED:
1015 return (uint8_t*)"RANGING TX FAILED";
1016 case UWB_STATUS_RANGING_RX_TIMEOUT:
1017 return (uint8_t*)"RANGING RX TIMEOUT";
1018 case UWB_STATUS_RANGING_RX_PHY_DEC_FAILED:
1019 return (uint8_t*)"PHYSICAL DECODING FAILED";
1020 case UWB_STATUS_RANGING_RX_PHY_TOA_FAILED:
1021 return (uint8_t*)"PHYSICAL TOA FAILED";
1022 case UWB_STATUS_RANGING_RX_PHY_STS_FAILED:
1023 return (uint8_t*)"PHYSICAL STS FAILED";
1024 case UWB_STATUS_RANGING_RX_MAC_DEC_FAILED:
1025 return (uint8_t*)"MAC DECODING FAILED";
1026 case UWB_STATUS_RANGING_RX_MAC_IE_DEC_FAILED:
1027 return (uint8_t*)"MAC INFORMATION DECODING FAILED";
1028 case UWB_STATUS_RANGING_RX_MAC_IE_MISSING:
1029 return (uint8_t*)"MAC INFORMATION MISSING";
1030 default:
1031 return (uint8_t*)"UNKNOWN";
1032 }
1033 }
1034