1 /*******************************************************************************
2 * \file cybt_platform_task.c
3 *
4 * \brief
5 * Provides API for OS task communication setup.
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 
26 #ifndef CYBT_PLATFORM_TASK_H
27 #define CYBT_PLATFORM_TASK_H
28 
29 #include <stdint.h>
30 #include <stdbool.h>
31 
32 #include "cyabs_rtos.h"
33 
34 #include "cybt_result.h"
35 
36 #include "wiced_bt_version.h"
37 
38 /*****************************************************************************
39  *                                Macros
40  *****************************************************************************/
41 #define BT_TASK_ID_BTU                     (0)
42 #define BT_TASK_NUM                        (1)
43 #define BT_TASK_NAME_BTU                   "CYBT_BT_Task"
44 #define BTU_TASK_PRIORITY                  (CY_RTOS_PRIORITY_HIGH)
45 #define BTU_TASK_STACK_SIZE                (0x2400)
46 #define CYBT_TASK_DEFAULT_POOL_SIZE        (2048)
47 #define CYBT_INVALID_QUEUE_UTILIZATION     (0xFF)
48 
49 #define BT_EVT_STATIC_GROUP                (0x8000)
50 #define BT_EVT_DYNAMIC_GROUP               (0x0000)
51 
52 #define BT_EVT_ANY                         (0x0)
53 #define BT_EVT_TIMEOUT                     (BT_EVT_STATIC_GROUP | 0x0401)
54 #define BT_EVT_CORE_HCI                    (BT_EVT_STATIC_GROUP | 0x0201)
55 #define BT_EVT_CORE_HCI_WRITE_BUF_AVAIL    (BT_EVT_STATIC_GROUP | 0x0202)
56 #define BT_EVT_TASK_BOOT_COMPLETES         (BT_EVT_STATIC_GROUP | 0x0601)
57 #define BT_EVT_TASK_SHUTDOWN               (BT_EVT_STATIC_GROUP | 0x0101)
58 #define BT_EVT_TASK_RESET_COMPLETE         (BT_EVT_STATIC_GROUP | 0x0102)
59 
60 #if (defined(BTSTACK_VER) && (BTSTACK_VER >= 0x03080000))
61 #define BT_EVT_APP_SERIALIZATION           (BT_EVT_STATIC_GROUP | 0x0801)
62 #endif // BTSTACK_VER
63 
64 #define IS_BT_EVT_STATIC(e)                ( ( (e) & (BT_EVT_STATIC_GROUP) ) ? true : false )
65 
66 /*****************************************************************************
67  *                           Type Definitions
68  *****************************************************************************/
69 
70 /**
71  * Task queue related declaration
72  */
73 
74 #define BTU_TASK_QUEUE_COUNT        (64)
75 
76 /* Queue will have pointer, hence item size is sizeof(uint32_t) */
77 #define BTU_TASK_QUEUE_ITEM_SIZE    (sizeof(uint32_t))
78 
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #endif
83 
84 /*****************************************************************************
85  *                           Function Declarations
86  ****************************************************************************/
87 
88 /**
89  * Initialize Bluetooth related OS tasks.
90  *
91  * @param[in] p_arg: Pointer to an argument if any
92  *
93  * @returns  CYBT_SUCCESS
94  *           CYBT_ERR_INIT_MEMPOOL_FAILED
95  *           CYBT_ERR_INIT_QUEUE_FAILED
96  *           CYBT_ERR_CREATE_TASK_FAILED
97  */
98 cybt_result_t cybt_platform_task_init(void *p_arg);
99 
100 
101 /**
102  * Delete Bluetooth related OS tasks.
103  *
104  * @returns  CYBT_SUCCESS
105  *           CYBT_ERR_OUT_OF_MEMORY
106  *           CYBT_ERR_SEND_QUEUE_FAILED
107  */
108 cybt_result_t cybt_platform_task_deinit(void);
109 
110 
111 /**
112  * Initialize task memory pool.
113  *
114  * @param[in] total_size: the request size of memory pool
115  *
116  * @returns  CYBT_SUCCESS
117  *           CYBT_ERR_OUT_OF_MEMORY
118  *
119  */
120 cybt_result_t cybt_platform_task_mempool_init(uint32_t total_size);
121 
122 
123 /**
124  * Allocate the memory block from task memory pool.
125  *
126  * @param[in] req_size: the request size of memory block
127  *
128  * @returns  the pointer of memory block
129  *
130  */
131 void *cybt_platform_task_mempool_alloc(uint32_t req_size);
132 
133 
134 /**
135  * Free and return the memory block to task memory pool.
136  *
137  * @param[in]   p_mem_block: the pointer of memory block
138  *
139  * @returns     void
140  *
141  */
142 void cybt_platform_task_mempool_free(void *p_mem_block);
143 
144 
145 /**
146  * Release task memory pool.
147  *
148  * @returns     void
149  */
150 void cybt_platform_task_mempool_deinit(void);
151 
152 
153 /**
154  * Send message to BT task.
155  *
156  * @param[in] p_bt_msg    : the pointer of the message
157  * @param[in] is_from_isr : true if this function is called from isr context
158  *                         otherwise false
159  *
160  * @returns  CYBT_SUCCESS
161  *           CYBT_ERR_BADARG
162  *           CYBT_ERR_QUEUE_ALMOST_FULL
163  *           CYBT_ERR_SEND_QUEUE_FAILED
164  */
165 cybt_result_t cybt_send_msg_to_bt_task(void *p_bt_msg,
166                                        bool is_from_isr
167                                        );
168 
169 #ifdef __cplusplus
170 } /* extern "C" */
171 #endif
172 
173 #endif
174 
175