Nordic Thingy:52 v2.2.0
ble_tss.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification,
6  are permitted provided that the following conditions are met:
7 
8  1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  2. Redistributions in binary form, except as embedded into a Nordic
12  Semiconductor ASA integrated circuit in a product or a software update for
13  such product, must reproduce the above copyright notice, this list of
14  conditions and the following disclaimer in the documentation and/or other
15  materials provided with the distribution.
16 
17  3. Neither the name of Nordic Semiconductor ASA nor the names of its
18  contributors may be used to endorse or promote products derived from this
19  software without specific prior written permission.
20 
21  4. This software, with or without modification, must only be used with a
22  Nordic Semiconductor ASA integrated circuit.
23 
24  5. Any software provided in binary form under this license must not be reverse
25  engineered, decompiled, modified and/or disassembled.
26 
27  THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
28  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
30  DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
31  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
33  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
52 #ifndef BLE_TSS_H__
53 #define BLE_TSS_H__
54 
55 #include "ble.h"
56 #include "ble_srv_common.h"
57 #include "app_util_platform.h"
58 #include "drv_audio_config.h"
59 #include <stdint.h>
60 #include <stdbool.h>
61 
62 #define BLE_UUID_TSS_SERVICE 0x0500
63 #define BLE_TSS_MAX_DATA_LEN (BLE_GATT_ATT_MTU_DEFAULT - 3)
65 #ifdef __GNUC__
66  #ifdef PACKED
67  #undef PACKED
68  #endif
69 
70  #define PACKED(TYPE) TYPE __attribute__ ((packed))
71 #endif
72 
73 #define BLE_TSS_SPKR_STAT_FINISHED 0x00
74 #define BLE_TSS_SPKR_STAT_BUFFER_WARNING 0x01
75 #define BLE_TSS_SPKR_STAT_BUFFER_READY 0x02
76 #define BLE_TSS_SPKR_STAT_PACKET_DISREGARDED 0x10
77 #define BLE_TSS_SPKR_STAT_INVALID_COMMAND 0x11
78 
79 #define BLE_TSS_MIC_FRAME_SIZE CONFIG_AUDIO_FRAME_SIZE_BYTES
80 
81 typedef uint8_t ble_tss_spkr_stat_t;
82 
83 typedef uint8_t ble_tss_spkr_sample_t;
84 
85 typedef PACKED( struct
86 {
87  uint16_t freq;
88  uint16_t duration_ms;
89  uint8_t volume;
90 }) ble_tss_spkr_freq_t;
91 
92 typedef PACKED( union
93 {
94  ble_tss_spkr_freq_t freq;
95  uint8_t pcm[273];
96  ble_tss_spkr_sample_t sample_id;
97 }) ble_tss_spkr_t;
98 
99 typedef PACKED( union
100 {
101  uint8_t spl;
102  uint8_t raw[BLE_TSS_MIC_FRAME_SIZE];
103 }) ble_tss_mic_t;
104 
105 typedef enum
106 {
107  BLE_TSS_SPKR_MODE_FIRST,
108  BLE_TSS_SPKR_MODE_FREQ,
109  BLE_TSS_SPKR_MODE_PCM,
110  BLE_TSS_SPKR_MODE_SAMPLE,
111  BLE_TSS_SPKR_MODE_LAST
112 } ble_tss_spkr_mode_t;
113 
114 typedef enum
115 {
116  BLE_TSS_MIC_MODE_FIRST,
117  BLE_TSS_MIC_MODE_ADPCM,
118  BLE_TSS_MIC_MODE_SPL,
119  BLE_TSS_MIC_MODE_LAST
120 } ble_tss_mic_mode_t;
121 
122 typedef PACKED( struct
123 {
124  ble_tss_spkr_mode_t spkr_mode;
125  ble_tss_mic_mode_t mic_mode;
126 }) ble_tss_config_t;
127 
128 #define BLE_TSS_CONFIG_SPKR_MODE_MIN (BLE_TSS_SPKR_MODE_FIRST + 1)
129 #define BLE_TSS_CONFIG_SPKR_MODE_MAX (BLE_TSS_SPKR_MODE_LAST - 1)
130 #define BLE_TSS_CONFIG_MIC_MODE_MIN (BLE_TSS_MIC_MODE_FIRST + 1)
131 #define BLE_TSS_CONFIG_MIC_MODE_MAX (BLE_TSS_MIC_MODE_LAST - 1)
132 
133 typedef enum
134 {
135  BLE_TSS_EVT_NOTIF_MIC,
136  BLE_TSS_EVT_CONFIG_RECEIVED,
137  BLE_TSS_EVT_SPKR_RECEIVED,
138  BLE_TSS_EVT_NOTIF_SPKR_STAT
139 }ble_tss_evt_type_t;
140 
141 /* Forward declaration of the ble_tss_t type. */
142 typedef struct ble_tss_s ble_tss_t;
143 
145 typedef void (*ble_tss_evt_handler_t) (ble_tss_t * p_tss,
146  ble_tss_evt_type_t evt_type,
147  uint8_t * p_data,
148  uint16_t length);
149 
155 typedef struct
156 {
157  ble_tss_config_t * p_init_config;
160 
165 struct ble_tss_s
166 {
167  uint8_t uuid_type;
168  uint16_t service_handle;
169  ble_gatts_char_handles_t spkr_handles;
170  ble_gatts_char_handles_t mic_handles;
171  ble_gatts_char_handles_t spkr_stat_handles;
172  ble_gatts_char_handles_t config_handles;
173  uint16_t conn_handle;
177 };
178 
189 uint32_t ble_tss_init(ble_tss_t * p_tss, const ble_tss_init_t * p_tss_init);
190 
201 void ble_tss_on_ble_evt(ble_tss_t * p_tss, ble_evt_t * p_ble_evt);
202 
213 uint32_t ble_tss_mic_set(ble_tss_t * p_tss, uint8_t * p_data, uint16_t size);
214 
224 uint32_t ble_tss_spkr_stat_set(ble_tss_t * p_tss, ble_tss_spkr_stat_t status);
225 
226 #endif // BLE_TSS_H__
227 
ble_gatts_char_handles_t spkr_stat_handles
Definition: ble_tss.h:171
uint32_t ble_tss_mic_set(ble_tss_t *p_tss, uint8_t *p_data, uint16_t size)
Function for sending microphone data.
ble_tss_evt_handler_t evt_handler
Definition: ble_tss.h:158
bool is_spkr_stat_notif_enabled
Definition: ble_tss.h:175
ble_gatts_char_handles_t config_handles
Definition: ble_tss.h:172
uint16_t service_handle
Definition: ble_tss.h:168
ble_gatts_char_handles_t mic_handles
Definition: ble_tss.h:170
uint32_t ble_tss_spkr_stat_set(ble_tss_t *p_tss, ble_tss_spkr_stat_t status)
Function for sending speaker status.
Thingy Sound Service structure.
Definition: ble_tss.h:165
Thingy Sound Service initialization structure.
Definition: ble_tss.h:155
bool is_mic_notif_enabled
Definition: ble_tss.h:174
ble_tss_evt_handler_t evt_handler
Definition: ble_tss.h:176
uint16_t conn_handle
Definition: ble_tss.h:173
uint32_t ble_tss_init(ble_tss_t *p_tss, const ble_tss_init_t *p_tss_init)
Function for initializing the Thingy Sound Service.
ble_gatts_char_handles_t spkr_handles
Definition: ble_tss.h:169
void ble_tss_on_ble_evt(ble_tss_t *p_tss, ble_evt_t *p_ble_evt)
Function for handling the Thingy Sound Service&#39;s BLE events.
void(* ble_tss_evt_handler_t)(ble_tss_t *p_tss, ble_tss_evt_type_t evt_type, uint8_t *p_data, uint16_t length)
Thingy Sound Service event handler type.
Definition: ble_tss.h:145
uint8_t uuid_type
Definition: ble_tss.h:167