battservice.c
5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Copyright by Optek in 2015
*/
#include "c_def.h"
#include "debug.h"
#include "include/bt_def.h"
#include "include/bluetooth.h"
#include "include/hci.h"
#include "include/hci_core.h"
#include "include/l2cap.h"
#include "../bluez/uuid.h"
//#include "../blueZ/bluetooth.h"
//#include "../blueZ/sdp_lib.h"
#include "include/list.h"
#include "malloc.h"
#include "include/g_slist.h"
#include "../attrib/att.h"
#include "../attrib/gattrib.h"
#include "../attrib/gatt.h"
#include "../attrib/gatt_profile_uuid.h"
#include "../attrib/gatt-service.h"
/*********************************************************************
* CONSTANTS
*/
// Battery Service Get/Set Parameters
#define BATT_PARAM_LEVEL 0
#define BATT_PARAM_CRITICAL_LEVEL 1
#define BATT_PARAM_SERVICE_HANDLE 2
#define BATT_PARAM_BATT_LEVEL_IN_REPORT 3
// Callback events
#define BATT_LEVEL_NOTI_ENABLED 1
#define BATT_LEVEL_NOTI_DISABLED 2
// HID Report IDs for the service
#define HID_RPT_ID_BATT_LEVEL_IN 4 // Battery Level input report ID
// Battery Service attribute
static const bt_uuid_t battService = { ATT_BT_UUID_SIZE, BATT_SERV_UUID };
static const bt_uuid_t battLevelUuid = { ATT_BT_UUID_SIZE, BATT_LEVEL_UUID };
// Battery level characteristic
static uint8_t battLevel = 100;
static gattCharCfg_t battLevelClientCharCfg[GATT_MAX_NUM_CONN];
//define in hiddev.h
#define HID_REPORT_REF_LEN 2 // HID Report Reference Descriptor
#define HID_REPORT_TYPE_INPUT 1
// HID Report Reference characteristic descriptor, battery level
static uint8_t hidReportRefBattLevel[HID_REPORT_REF_LEN] =
{ HID_RPT_ID_BATT_LEVEL_IN, HID_REPORT_TYPE_INPUT };
/*********************************************************************
* Profile Attributes - Table
*/
static gattAttribute_t battAttrTbl[] =
{
// Battery Service
{
//{ ATT_BT_UUID_SIZE, GATT_PRIM_SVC_UUID }, /* type */
&gattPrimSvc,
GATT_PROP_READ, /* permissions */
0, /* handle */
(uint8_t *)&battService /* pValue */
},
// Battery Level Declaration
{
//{ ATT_BT_UUID_SIZE, GATT_CHARAC_UUID },
&gattCharac,
GATT_PROP_READ,
0,
&gattReadProps
},
// Battery Level Value
{
//{ ATT_BT_UUID_SIZE, BATT_LEVEL_UUID },
&battLevelUuid,
GATT_PROP_READ,
0,
&battLevel
},
// Battery Level Client Characteristic Configuration
{
//{ ATT_BT_UUID_SIZE, GATT_CLIENT_CHARAC_CFG_UUID },
&gattClientCharacCFG,
GATT_PROP_READ | GATT_PROP_WRITE,
0,
(uint8_t *) &battLevelClientCharCfg
},
// HID Report Reference characteristic descriptor, batter level input
{
//{ ATT_BT_UUID_SIZE, GATT_REPORT_REFERENCE },
&gattCharacReportRef,
GATT_PROP_READ,
0,
hidReportRefBattLevel
}
};
/*********************************************************************
* LOCAL FUNCTIONS
*/
static uint8_t battReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen );
static bStatus_t battWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
uint8_t *pValue, uint8_t len, uint16_t offset );
//static void battNotifyCB( linkDBItem_t *pLinkItem );
static uint8_t battMeasure( void );
static void battNotifyLevel( void );
/*********************************************************************
* PROFILE CALLBACKS
*/
// Battery Service Callbacks
const gattServiceCBs_t battCBs =
{
NULL,
NULL,
battReadAttrCB, // Read callback function pointer
battWriteAttrCB, // Write callback function pointer
NULL // Authorization callback function pointer
};
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*********************************************************************
* @fn Batt_AddService
*
* @brief Initializes the Battery Service by registering
* GATT attributes with the GATT server.
*
* @return Success or Failure
*/
bStatus_t Batt_AddService( void )
{
uint8_t status;
// Initialize Client Characteristic Configuration attributes
GATTServApp_InitCharCfg( INVALID_CONNHANDLE, battLevelClientCharCfg );
// Register GATT attribute list and CBs with GATT Server App
status = GATTServApp_RegisterService( battAttrTbl,
GATT_NUM_ATTRS( battAttrTbl ),
&battCBs );
return ( status );
}
static uint8_t battReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen )
{
bStatus_t status = SUCCESS;
uint16_t uuid = pAttr->pType->value.u16_val;
// Make sure it's not a blob operation (no attributes in the profile are long)
if ( offset > 0 )
{
return ( ATT_ERR_ATTR_NOT_LONG );
}
// Measure battery level if reading level
if ( uuid == BATT_LEVEL_UUID )
{
*pLen = 1;
pValue[0] = 80; //80%
}
else if ( uuid == GATT_REPORT_REFERENCE )
{
*pLen = HID_REPORT_REF_LEN;
CFasm_memcpy( pValue, pAttr->pValue, HID_REPORT_REF_LEN );
}
else
{
*pLen = 0;
status = ATT_ERR_ATTR_NOT_FOUND;
}
return ( status );
}
static bStatus_t battWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
uint8_t *pValue, uint8_t len, uint16_t offset )
{
asm ("break 1,1");
return SUCCESS;
}