Skip to content

Commit f786aa2

Browse files
committed
revert back to first implementation
1 parent 9cbf57a commit f786aa2

File tree

6 files changed

+162
-232
lines changed

6 files changed

+162
-232
lines changed

nRF51822/projects/uartservice/btle.c

+117-109
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,101 @@
4747
#include "btle_gap.h"
4848
#include "btle_advertising.h"
4949
#include "custom_helper.h"
50+
#include "btle_uart.h"
5051

51-
static void service_error_callback ( uint32_t nrf_error );
52-
void assert_nrf_callback ( uint16_t line_num, const uint8_t * p_file_name );
53-
void app_error_handler ( uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name );
54-
static error_t bond_manager_init ( void );
55-
static void btle_handler ( ble_evt_t * p_ble_evt );
56-
static void btle_soc_event_handler ( uint32_t sys_evt );
52+
//--------------------------------------------------------------------+
53+
// MACRO CONSTANT TYPEDEF
54+
//--------------------------------------------------------------------+
55+
// standard service UUID to driver offset
56+
#define UUID2OFFSET(uuid) ((uuid)-0x1800)
5757

58-
/**************************************************************************/
59-
/*!
60-
Checks is all values in the supplied array are zero
58+
btle_service_driver_t const btle_service_driver[] =
59+
{
60+
#if CFG_BLE_DEVICE_INFORMATION
61+
[UUID2OFFSET(BLE_UUID_DEVICE_INFORMATION_SERVICE)] =
62+
{
63+
.init = device_information_init,
64+
.event_handler = NULL,
65+
},
66+
#endif
6167

62-
@returns 'true' if all values in the array are zero
63-
'false' if any value is non-zero
64-
*/
65-
/**************************************************************************/
66-
static inline bool is_all_zeros(uint8_t arr[], uint32_t count) ATTR_ALWAYS_INLINE ATTR_PURE;
67-
static inline bool is_all_zeros(uint8_t arr[], uint32_t count)
68+
#if CFG_BLE_BATTERY
69+
[UUID2OFFSET(BLE_UUID_BATTERY_SERVICE)] =
70+
{
71+
.init = battery_init,
72+
.event_handler = battery_handler,
73+
},
74+
#endif
75+
76+
#if CFG_BLE_HEART_RATE
77+
// [UUID2OFFSET(BLE_UUID_HEART_RATE_SERVICE)] =
78+
// {
79+
// .init = heart_rate_init,
80+
// .event_handler = heart_rate_handler,
81+
// },
82+
#endif
83+
84+
#if CFG_BLE_IMMEDIATE_ALERT
85+
[UUID2OFFSET(BLE_UUID_IMMEDIATE_ALERT_SERVICE)] =
86+
{
87+
.init = immediate_alert_init,
88+
.event_handler = immediate_alert_handler,
89+
},
90+
#endif
91+
92+
#if CFG_BLE_TX_POWER
93+
[UUID2OFFSET(BLE_UUID_TX_POWER_SERVICE)] =
94+
{
95+
.init = tx_power_init,
96+
.event_handler = NULL,
97+
},
98+
#endif
99+
100+
#if CFG_BLE_LINK_LOSS
101+
[UUID2OFFSET(BLE_UUID_LINK_LOSS_SERVICE)] =
102+
{
103+
.init = link_loss_init,
104+
.event_handler = link_loss_handler,
105+
},
106+
#endif
107+
};
108+
109+
enum {
110+
BTLE_SERVICE_MAX = sizeof(btle_service_driver) / sizeof(btle_service_driver_t)
111+
};
112+
113+
btle_service_custom_driver_t btle_service_custom_driver[] =
68114
{
69-
for ( uint32_t i = 0; i < count; i++)
70-
{
71-
if (arr[i] != 0 ) return false;
72-
}
115+
{
116+
.uuid_base = BLE_UART_UUID_BASE,
117+
.service_uuid.uuid = BLE_UART_UUID_PRIMARY_SERVICE,
118+
.init = uart_service_init,
119+
.event_handler = uart_service_handler
120+
},
121+
};
73122

74-
return true;
75-
}
123+
enum {
124+
BTLE_SERVICE_CUSTOM_MAX = sizeof(btle_service_custom_driver) / sizeof(btle_service_custom_driver_t)
125+
};
126+
127+
//--------------------------------------------------------------------+
128+
// INTERNAL OBJECT & FUNCTION DECLARATION
129+
//--------------------------------------------------------------------+
130+
static void service_error_callback(uint32_t nrf_error);
131+
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name);
132+
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name);
133+
134+
static error_t bond_manager_init(void);
135+
136+
static void btle_handler(ble_evt_t * p_ble_evt);
137+
static void btle_soc_event_handler(uint32_t sys_evt);
76138

77139
/**************************************************************************/
78140
/*!
79141
Initialises BTLE and the underlying HW/SoftDevice
80142
*/
81143
/**************************************************************************/
82-
error_t btle_init(btle_service_t service_list[], uint8_t const service_count)
144+
error_t btle_init(void)
83145
{
84146
/* Initialise the SoftDevice using an external 32kHz XTAL for LFCLK */
85147
SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
@@ -94,105 +156,35 @@ error_t btle_init(btle_service_t service_list[], uint8_t const service_count)
94156
/* Initialise GAP */
95157
btle_gap_init();
96158

97-
/* Initialise any services present on the GATT Server */
98-
/* This sequences will be repeated for every service */
99-
for ( uint8_t sid = 0; sid < service_count; sid++ )
159+
/* Standard Services */
160+
for(uint16_t i=0; i<BTLE_SERVICE_MAX; i++)
100161
{
101-
btle_service_t * p_service = &service_list[sid];
102-
103-
/* If we are using a custom UUID we first need to add it to the stack */
104-
if ( is_all_zeros(p_service->uuid_base, 16) )
162+
if ( btle_service_driver[i].init != NULL )
105163
{
106-
/* Seems to be a standard 16-bit BLE UUID */
107-
p_service->uuid_type = BLE_UUID_TYPE_BLE;
164+
ASSERT_STATUS( btle_service_driver[i].init() );
108165
}
109-
else
110-
{
111-
/* Seems to be a custom UUID, which needs to be added */
112-
p_service->uuid_type = custom_add_uuid_base( p_service->uuid_base );
113-
ASSERT( p_service->uuid_type >= BLE_UUID_TYPE_VENDOR_BEGIN, ERROR_INVALIDPARAMETER );
114-
}
115-
116-
/* Add the primary GATT service first ... */
117-
ble_uuid_t service_uuid = { .type = p_service->uuid_type, .uuid = p_service->uuid };
118-
ASSERT_STATUS( sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &service_uuid, &p_service->handle) );
166+
}
119167

120-
/* ... then add all of the GATT characteristics */
121-
for(uint8_t cid=0; cid<p_service->char_count; cid++)
168+
/*Custom Services */
169+
for(uint16_t i=0; i<BTLE_SERVICE_CUSTOM_MAX; i++)
170+
{
171+
if ( btle_service_custom_driver[i].init != NULL )
122172
{
123-
btle_characteristic_t * p_char = p_service->char_pool[cid];
124-
ble_uuid_t char_uuid = { .type = p_service->uuid_type, .uuid = p_char->uuid };
173+
/* add the custom UUID to stack */
174+
uint8_t uuid_type = custom_add_uuid_base(btle_service_custom_driver[i].uuid_base);
175+
ASSERT( uuid_type >= BLE_UUID_TYPE_VENDOR_BEGIN, ERROR_INVALIDPARAMETER);
125176

126-
ASSERT_STATUS(custom_add_in_characteristic(p_service->handle, &char_uuid, p_char->properties,
127-
p_char->init_value, p_char->len_min, p_char->len_max,
128-
&p_char->handle) );
177+
btle_service_custom_driver[i].service_uuid.type = uuid_type; /* store for later use in advertising */
178+
ASSERT_STATUS( btle_service_custom_driver[i].init(uuid_type) );
129179
}
130180
}
131181

132-
/* Now we can start the advertising process */
133-
btle_advertising_init(service_list, service_count);
182+
btle_advertising_init(btle_service_driver, BTLE_SERVICE_MAX, btle_service_custom_driver, BTLE_SERVICE_CUSTOM_MAX);
134183
btle_advertising_start();
135184

136185
return ERROR_NONE;
137186
}
138187

139-
/**************************************************************************/
140-
/*!
141-
This function will attempt to update the value of a GATT characteristic
142-
143-
@param[in] p_char A pointer to the characteristic to update
144-
@param[in] p_data A pointer to the data to use when updating
145-
@param[in] len The size (in bytes) of p_data
146-
147-
@returns ERROR_NONE if the update was successful, otherwise an
148-
appropriate error_t value
149-
*/
150-
/**************************************************************************/
151-
error_t btle_characteristic_update(btle_characteristic_t const * p_char, void const * p_data, uint16_t len)
152-
{
153-
uint16_t const conn_handle = btle_gap_get_connection();
154-
155-
/* Special treatment is required if notify or indicate flags are set */
156-
if ( (p_char->properties.notify || p_char->properties.indicate) && (conn_handle != BLE_CONN_HANDLE_INVALID) )
157-
{
158-
/* HVX params for the characteristic value */
159-
ble_gatts_hvx_params_t const hvx_params =
160-
{
161-
.handle = p_char->handle.value_handle,
162-
.type = p_char->properties.notify ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION,
163-
.offset = 0,
164-
.p_data = (uint8_t*) p_data,
165-
.p_len = &len
166-
};
167-
168-
/* Now pass the params down to the SD ... */
169-
error_t error = sd_ble_gatts_hvx(conn_handle, &hvx_params);
170-
171-
/* ... and make sure nothing unfortunate happened */
172-
switch(error)
173-
{
174-
/* Ignore the following three error types */
175-
case ERROR_NONE :
176-
case ERROR_INVALID_STATE : // Notification/indication bit not enabled in CCCD
177-
case ERROR_BLEGATTS_SYS_ATTR_MISSING :
178-
break;
179-
180-
/* Make sure that the local value is updated and handle any other */
181-
/* error types errors here */
182-
default:
183-
ASSERT_STATUS ( sd_ble_gatts_value_set(p_char->handle.value_handle, 0, &len, p_data) );
184-
ASSERT_STATUS ( error );
185-
break;
186-
}
187-
}
188-
else
189-
{
190-
/* If notify or indicate aren't set we can update the value like this */
191-
ASSERT_STATUS ( sd_ble_gatts_value_set(p_char->handle.value_handle, 0, &len, p_data) );
192-
}
193-
194-
return ERROR_NONE;
195-
}
196188

197189
/**************************************************************************/
198190
/*!
@@ -216,7 +208,23 @@ static void btle_handler(ble_evt_t * p_ble_evt)
216208
ble_bondmngr_on_ble_evt(p_ble_evt);
217209
ble_conn_params_on_ble_evt(p_ble_evt);
218210

219-
/* Next call the application specific event handlers */
211+
/* Standard Service Handler */
212+
for(uint16_t i=0; i<BTLE_SERVICE_MAX; i++)
213+
{
214+
if ( btle_service_driver[i].event_handler != NULL )
215+
{
216+
btle_service_driver[i].event_handler(p_ble_evt);
217+
}
218+
}
219+
220+
/* Custom Service Handler */
221+
for(uint16_t i=0; i<BTLE_SERVICE_CUSTOM_MAX; i++)
222+
{
223+
if ( btle_service_custom_driver[i].event_handler != NULL )
224+
{
225+
btle_service_custom_driver[i].event_handler(p_ble_evt);
226+
}
227+
}
220228
switch (p_ble_evt->header.evt_id)
221229
{
222230
case BLE_GAP_EVT_CONNECTED:
@@ -225,7 +233,7 @@ static void btle_handler(ble_evt_t * p_ble_evt)
225233

226234
case BLE_GAP_EVT_DISCONNECTED:
227235
/* Since we are not in a connection and have not started advertising, store bonds */
228-
ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store() );
236+
(void) ble_bondmngr_bonded_centrals_store();
229237
/* Start advertising again (change this if necessary!) */
230238
btle_advertising_start();
231239
break;

nRF51822/projects/uartservice/btle.h

+11-26
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,18 @@
4545
#include "ble.h"
4646
#include "btle_uart.h"
4747

48-
enum
49-
{
50-
BTLE_MAX_CHARACTERISTIC_PER_SERVICE = 10
51-
};
48+
typedef struct {
49+
error_t (* const init) (void);
50+
void (* const event_handler) (ble_evt_t * );
51+
}btle_service_driver_t;
5252

53-
/* Abstraction for custom GATT characteristics */
54-
typedef struct
55-
{
56-
uint16_t uuid;
57-
ble_gatt_char_props_t properties; // Characteristic properties/access-rights
58-
uint16_t len_min; // Min length (in bytes) for the char value
59-
uint16_t len_max; // Max length (in bytes) for the char value
60-
uint8_t const * init_value; // Initial value for the char
61-
ble_gatts_char_handles_t handle; // Characteristic handle
62-
} btle_characteristic_t;
53+
typedef struct {
54+
error_t (* const init) (uint8_t);
55+
void (* const event_handler) (ble_evt_t * );
6356

64-
/* Abstraction for custom GATT services */
65-
typedef struct
66-
{
67-
uint8_t uuid_base[16]; // All zeroes = standard BLE service
68-
uint16_t uuid; // The primary service UUID
69-
uint8_t uuid_type; // Standard = 1, Custom = 2, Invalid = 0
70-
uint16_t handle; // Service handle
71-
uint8_t char_count; // Number of characteristics
72-
btle_characteristic_t* char_pool[BTLE_MAX_CHARACTERISTIC_PER_SERVICE];
73-
} btle_service_t;
57+
uint8_t const uuid_base[16];
58+
ble_uuid_t service_uuid;
59+
}btle_service_custom_driver_t;
7460

7561
/* Characteristic Presentation Format unit values aren't defined by Nordic */
7662
/* See https://developer.bluetooth.org/gatt/units/Pages/default.aspx */
@@ -187,8 +173,7 @@ typedef enum ble_gatt_unit_e
187173
BLE_GATT_CPF_UNIT_IRRADIANCE_WATT_PER_SQUARE_METRE = 0x27B6
188174
} ble_gatt_unit_t;
189175

190-
error_t btle_init ( btle_service_t service_list[], uint8_t const service_count );
191-
error_t btle_characteristic_update ( btle_characteristic_t const * p_char, void const * p_data, uint16_t len );
176+
error_t btle_init(void);
192177

193178
#ifdef __cplusplus
194179
}

0 commit comments

Comments
 (0)