bas_c.h
Go to the documentation of this file.
1 /**
2 *****************************************************************************************
3 *
4 * @file bas_c.h
5 *
6 * @brief Battery Service Client API
7 *
8 *****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 *****************************************************************************************
36 */
37
38 /**
39 * @addtogroup BLE_SRV BLE Services
40 * @{
41 */
42
43 /**
44 * @defgroup BLE_SDK_BAS_C Battery Service Client (BAS_C)
45 * @{
46 * @brief Battery Service Client module.
47 *
48 * @details The Battery Service Client contains the APIs and types, which can be used by the
49 * application to discovery of Battery Service of peer and interact with it.
50 *
51 * The application must provide an event handler to register, then call \ref bas_client_init().
52 * After Battery Service Client discoveries peer Battery Service, application can call
53 * \ref bas_c_bat_level_notify_set() and \ref bas_c_bat_level_read() to get battery
54 * data from peer.
55 */
56
57 #ifndef __BAS_C_H__
58 #define __BAS_C_H__
59
60 #include "gr55xx_sys.h"
61 #include "ble_prf_types.h"
62 #include "custom_config.h"
63 #include <stdint.h>
64 #include <stdbool.h>
65
66 /**
67 * @defgroup BAS_C_MACRO Defines
68 * @{
69 */
70 #define BAS_C_CONNECTION_MAX (10 < CFG_MAX_CONNECTIONS ?\
71 10 : CFG_MAX_CONNECTIONS) /**< Maximum number of BAS Client connections. */
72 /** @} */
73
74 /**
75 * @defgroup BAS_C_ENUM Enumerations
76 * @{
77 */
78 /**@brief Battery Service Client event type. */
79 typedef enum
80 {
81 BAS_C_EVT_INVALID, /**< BAS Client invalid event. */
82 BAS_C_EVT_DISCOVERY_COMPLETE, /**< BAS Client has found BAS service and its characteristics. */
83 BAS_C_EVT_DISCOVERY_FAIL, /**< BAS Client found BAS service failed because of invalid operation or no found at the peer. */
84 BAS_C_EVT_BAT_LEVEL_NTF_SET_SUCCESS, /**< BAS Client has enabled Notification of Battery Level characteristics. */
85 BAS_C_EVT_BAT_LEVEL_NTF_SET_ERR, /**< Error occured when BAS Client set Notification of Battery Level characteristics. */
86 BAS_C_EVT_BAT_LEVE_RECEIVE, /**< BAS Client has received Battery Level value (Read or Notification from peer). */
87 } bas_c_evt_type_t;
88 /** @} */
89
90 /**
91 * @defgroup BAS_C_STRUCT Structures
92 * @{
93 */
94 /**@brief Handles on the connected peer device needed to interact with it. */
95 typedef struct
96 {
97 uint16_t bas_srvc_start_handle; /**< BAS Service start handle. */
98 uint16_t bas_srvc_end_handle; /**< BAS Service end handle. */
99 uint16_t bas_bat_level_handle; /**< BAS Battery Level characteristic Value handle which has been got from peer. */
100 uint16_t bas_bat_level_cccd_handle; /**< BAS CCCD handle of Battery Level characteristic which has been got from peer. */
101 uint16_t bas_bat_level_pres_handle; /**< BAS Presentation Format Descriptor handle of Battery Level characteristic which has been got from peer. */
102 } bas_c_handles_t;
103
104 /**@brief Battery Service Client event. */
105 typedef struct
106 {
107 uint8_t conn_idx; /**< The connection index. */
108 bas_c_evt_type_t evt_type; /**< BAS Client event type. */
109 uint8_t bat_level; /**< Battery level. */
110 } bas_c_evt_t;
111 /** @} */
112
113 /**
114 * @defgroup BAS_C_TYPEDEF Typedefs
115 * @{
116 */
117 /**@brief Battery Service Client event handler type. */
118 typedef void (*bas_c_evt_handler_t)(bas_c_evt_t *p_evt);
119 /** @} */
120
121 /**
122 * @defgroup BAS_C_FUNCTION Functions
123 * @{
124 */
125 /**
126 *****************************************************************************************
127 * @brief Register BAS Client event handler.
128 *
129 * @param[in] evt_handler: Battery Service Client event handler.
130 *
131 * @return Result of initialization.
132 *****************************************************************************************
133 */
134 sdk_err_t bas_client_init(bas_c_evt_handler_t evt_handler);
135
136 /**
137 *****************************************************************************************
138 * @brief Discovery Battery Service on peer.
139 *
140 * @param[in] conn_idx: Index of connection.
141 *
142 * @return Operation result.
143 *****************************************************************************************
144 */
145 sdk_err_t bas_c_disc_srvc_start(uint8_t conn_idx);
146
147 /**
148 *****************************************************************************************
149 * @brief Enable or disable peer Battery Level characteristic notify.
150 *
151 * @param[in] conn_idx: Index of connection.
152 * @param[in] is_enable: true or false.
153 *
154 * @return Operation result.
155 *****************************************************************************************
156 */
157 sdk_err_t bas_c_bat_level_notify_set(uint8_t conn_idx, bool is_enable);
158
159 /**
160 *****************************************************************************************
161 * @brief Read Battery Level characteristic value.
162 *
163 * @param[in] conn_idx: Index of connection.
164 *
165 * @return Operation result.
166 *****************************************************************************************
167 */
168 sdk_err_t bas_c_bat_level_read(uint8_t conn_idx);
169 /** @} */
170
171 #endif
172 /** @} */
173 /** @} */
bas_c_handles_t::bas_bat_level_handle
uint16_t bas_bat_level_handle
BAS Battery Level characteristic Value handle which has been got from peer.
Definition: bas_c.h:99
bas_c_handles_t::bas_bat_level_cccd_handle
uint16_t bas_bat_level_cccd_handle
BAS CCCD handle of Battery Level characteristic which has been got from peer.
Definition: bas_c.h:100
bas_c_evt_t::bat_level
uint8_t bat_level
Battery level.
Definition: bas_c.h:109
BAS_C_EVT_BAT_LEVEL_NTF_SET_ERR
@ BAS_C_EVT_BAT_LEVEL_NTF_SET_ERR
Error occured when BAS Client set Notification of Battery Level characteristics.
Definition: bas_c.h:85
bas_c_bat_level_notify_set
sdk_err_t bas_c_bat_level_notify_set(uint8_t conn_idx, bool is_enable)
Enable or disable peer Battery Level characteristic notify.
BAS_C_EVT_DISCOVERY_FAIL
@ BAS_C_EVT_DISCOVERY_FAIL
BAS Client found BAS service failed because of invalid operation or no found at the peer.
Definition: bas_c.h:83
gr55xx_sys.h
GR55XX System API.
bas_c_handles_t::bas_srvc_end_handle
uint16_t bas_srvc_end_handle
BAS Service end handle.
Definition: bas_c.h:98
bas_c_evt_t::evt_type
bas_c_evt_type_t evt_type
BAS Client event type.
Definition: bas_c.h:108
bas_c_handles_t::bas_bat_level_pres_handle
uint16_t bas_bat_level_pres_handle
BAS Presentation Format Descriptor handle of Battery Level characteristic which has been got from pee...
Definition: bas_c.h:101
bas_c_evt_t::conn_idx
uint8_t conn_idx
The connection index.
Definition: bas_c.h:107
bas_c_bat_level_read
sdk_err_t bas_c_bat_level_read(uint8_t conn_idx)
Read Battery Level characteristic value.
BAS_C_EVT_INVALID
@ BAS_C_EVT_INVALID
BAS Client invalid event.
Definition: bas_c.h:81
bas_c_evt_type_t
bas_c_evt_type_t
Battery Service Client event type.
Definition: bas_c.h:80
BAS_C_EVT_DISCOVERY_COMPLETE
@ BAS_C_EVT_DISCOVERY_COMPLETE
BAS Client has found BAS service and its characteristics.
Definition: bas_c.h:82
ble_prf_types.h
Profile/Service Common Types.
bas_c_disc_srvc_start
sdk_err_t bas_c_disc_srvc_start(uint8_t conn_idx)
Discovery Battery Service on peer.
sdk_err_t
uint16_t sdk_err_t
SDK API result type.
Definition: ble_error.h:243
bas_c_evt_t
Battery Service Client event.
Definition: bas_c.h:106
bas_client_init
sdk_err_t bas_client_init(bas_c_evt_handler_t evt_handler)
Register BAS Client event handler.
BAS_C_EVT_BAT_LEVE_RECEIVE
@ BAS_C_EVT_BAT_LEVE_RECEIVE
BAS Client has received Battery Level value (Read or Notification from peer).
Definition: bas_c.h:86
bas_c_handles_t
Handles on the connected peer device needed to interact with it.
Definition: bas_c.h:96
bas_c_evt_handler_t
void(* bas_c_evt_handler_t)(bas_c_evt_t *p_evt)
Battery Service Client event handler type.
Definition: bas_c.h:118
bas_c_handles_t::bas_srvc_start_handle
uint16_t bas_srvc_start_handle
BAS Service start handle.
Definition: bas_c.h:97
BAS_C_EVT_BAT_LEVEL_NTF_SET_SUCCESS
@ BAS_C_EVT_BAT_LEVEL_NTF_SET_SUCCESS
BAS Client has enabled Notification of Battery Level characteristics.
Definition: bas_c.h:84