1 /*******************************************************************************
2 * \file cybt_serialize.c
3 *
4 *
5 * \brief
6 * Provides some application utility functions.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright 2018-2019 Cypress Semiconductor Corporation
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *     http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 #include "wiced_bt_version.h"
26 
27 #if (defined(BTSTACK_VER) && (BTSTACK_VER >= 0x03080000))
28 #include "cyabs_rtos.h"
29 #include "cybt_result.h"
30 #include "cybt_platform_task.h"
31 #include "wiced_bt_serialize.h"
32 #include "cybt_platform_interface.h"
33 
34 /**
35 *  @addtogroup    app_utils   Application Utility Functions
36 *
37 *  @{
38 */
39 
40 // The maximum number of outstanding serialized calls allowed
41 #ifndef SERIALIZE_APP_QUEUE_SIZE
42     #define SERIALIZE_APP_QUEUE_SIZE   (4)
43 #endif
44 
45 typedef struct
46 {
47     cy_queue_t   serialQ;
48     wiced_bool_t bInitialized;
49 } CYBT_SER_Q_t;
50 
51 typedef struct
52 {
53     wiced_bt_serialized_app_func_t   pf;        // App function pointer
54     void                             *pp;       // App parameter
55 } CYBT_SER_Q_ENTRY_t;
56 
57 static uint16_t     ser_event = BT_EVT_APP_SERIALIZATION;
58 
59 CYBT_SER_Q_t serial_q_struct = {0};
60 
61 /**
62 * Called by applications to serialize the execution of an application function in the BT stack context
63 *
64 * @param[in] p_func   Function to be called in the BT stack context
65 * @param[in] param:   Parameter to be passed
66 *
67 * @returns  WICED_BT_SUCCESS if success else error reason.
68 */
69 BTSTACK_PORTING_SECTION_BEGIN
wiced_bt_app_serialize_function(wiced_bt_serialized_app_func_t p_func,void * param)70 wiced_result_t wiced_bt_app_serialize_function (wiced_bt_serialized_app_func_t p_func, void *param)
71 {
72     CYBT_SER_Q_ENTRY_t   entry;
73 
74     // First time called, initialize the queue
75     if (!serial_q_struct.bInitialized)
76     {
77         if (cy_rtos_init_queue(&serial_q_struct.serialQ, SERIALIZE_APP_QUEUE_SIZE, sizeof (CYBT_SER_Q_ENTRY_t)) == CY_RSLT_SUCCESS)
78             serial_q_struct.bInitialized = WICED_TRUE;
79         else
80             return WICED_QUEUE_ERROR;
81     }
82 
83     entry.pf = p_func;
84     entry.pp = param;
85 
86     if (cy_rtos_put_queue(&serial_q_struct.serialQ, &entry, 0, false) == CY_RSLT_SUCCESS)
87     {
88         cybt_send_msg_to_bt_task(&ser_event, WICED_FALSE);
89         return (WICED_BT_SUCCESS);
90     }
91     else
92     {
93         return (WICED_QUEUE_ERROR);
94     }
95 }
96 BTSTACK_PORTING_SECTION_END
97 
98 /**
99 * Call back to the application in the BT stack context
100 *
101 * @param[in] void
102 *
103 * @returns  void
104 */
105 BTSTACK_PORTING_SECTION_BEGIN
cybt_call_app_in_stack_context(void)106 void cybt_call_app_in_stack_context (void)
107 {
108     CYBT_SER_Q_ENTRY_t   entry;
109 
110     if (cy_rtos_get_queue(&serial_q_struct.serialQ, &entry, 0, false) == CY_RSLT_SUCCESS)
111         (entry.pf)(entry.pp);
112 }
113 BTSTACK_PORTING_SECTION_END
114 #endif // BTSTACK_VER
115 
116 /**@} */
117