Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / cc13x2_26x2 / chipOBleProfile.c
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 Texas Instruments Incorporated
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *        @file  chipOBLeProfile.c
21  *        This file contains the CHIPOBLE profile sample GATT service profile
22  *        for use with the BLE Manager.
23  */
24 /*********************************************************************
25  * INCLUDES
26  */
27 #include <string.h>
28
29 #include <icall.h>
30 #include <util.h>
31
32 #include "icall_ble_api.h"
33 #include "ti_ble_config.h"
34
35 #include "chipOBleProfile.h"
36
37 /*********************************************************************
38  * GLOBAL VARIABLES
39  */
40 // CHIPoBLE GATT Profile Service UUID
41 const uint8 chipOBleServUUID[ATT_UUID_SIZE] = { CHIPOBLEPROFILE_SERV_UUID_BASE128(CHIPOBLE_SERV_UUID) };
42
43 // CHIPoBLE Tx characteristic UUID
44 const uint8 chipOBleProfileTxCharUUID[ATT_UUID_SIZE] = {
45     // 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18
46     CHIPOBLEPROFILE_CHAR_UUID_BASE128(CHIPOBLEPROFILE_TX_CHAR_UUID)
47 };
48
49 // CHIPoBLE Rx characteristic UUID
50 const uint8 chipOBleProfileRxCharUUID[ATT_UUID_SIZE] = {
51     // 0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18
52     CHIPOBLEPROFILE_CHAR_UUID_BASE128(CHIPOBLEPROFILE_RX_CHAR_UUID)
53 };
54
55 /*********************************************************************
56  * Profile Attributes - variables
57  */
58
59 //  Remote Display Profile Service attribute
60 static const gattAttrType_t chipoBleProfile = { ATT_UUID_SIZE, chipOBleServUUID };
61
62 // ChipOBLE Tx Characteristic Properties
63 static uint8_t chipOBleProfileTxCharProps = GATT_PROP_READ | GATT_PROP_INDICATE;
64
65 // ChipOBLE Tx Characteristic Value
66 static uint8_t chipOBleProfileTxCharVal[CHIPOBLEPROFILE_CHAR_LEN] = { 0x00 };
67
68 // ChipOBLE Tx Characteristic User Description
69 static uint8_t chipOBleProfileTxdDataUserDesp[CHIPOBLEPROFILE_MAX_DESCRIPTION_LEN] = "ChipOBLE Tx Char";
70
71 static gattCharCfg_t * chipOBleProfileTxStateDataConfig;
72
73 // ChipOBLE Rx Characteristic Properties
74 static uint8_t chipOBleProfileRxCharProps = GATT_PROP_WRITE;
75
76 // ChipOBLE Rx Characteristic Value
77 static uint8_t chipOBleProfileRxCharVal[CHIPOBLEPROFILE_CHAR_LEN] = { 0x00 };
78
79 // ChipOBLE Rx Characteristic User Description
80 static uint8_t chipOBleProfileRxdDataUserDesp[CHIPOBLEPROFILE_MAX_DESCRIPTION_LEN] = "ChipOBLE Rx Char";
81
82 /*********************************************************************
83  * LOCAL VARIABLES
84  */
85
86 static chipOBleProfileCBs_t * chipOBleProfile_AppCBs = NULL;
87
88 /*********************************************************************
89  * Profile Attributes - Table
90  */
91
92 static gattAttribute_t chipoBleProfileAttrTbl[] = {
93     // CHIPoBLE Service
94     {
95         { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
96         GATT_PERMIT_READ,                         /* permissions */
97         0,                                        /* handle */
98         (uint8 *) &chipoBleProfile                /* pValue */
99     },
100     // CHIPoBLE Tx Characteristic Declaration
101     { { ATT_BT_UUID_SIZE, characterUUID }, GATT_PERMIT_READ, 0, &chipOBleProfileTxCharProps },
102     // CHIPoBLE Tx Characteristic Value
103     { { ATT_UUID_SIZE, chipOBleProfileTxCharUUID }, GATT_PERMIT_READ, 0, chipOBleProfileTxCharVal },
104
105     // CHIPoBLE Tx Characteristic configuration
106     { { ATT_BT_UUID_SIZE, clientCharCfgUUID },
107       GATT_PERMIT_READ | GATT_PERMIT_WRITE,
108       0,
109       (uint8 *) &chipOBleProfileTxStateDataConfig },
110
111     // CHIPoBLE Tx Characteristic User Description
112     { { ATT_BT_UUID_SIZE, charUserDescUUID }, GATT_PERMIT_READ, 0, chipOBleProfileTxdDataUserDesp },
113
114     // CHIPoBLE Rx Characteristic Declaration
115     { { ATT_BT_UUID_SIZE, characterUUID }, GATT_PERMIT_READ, 0, &chipOBleProfileRxCharProps },
116     // CHIPoBLE Rx Characteristic Value
117     { { ATT_UUID_SIZE, chipOBleProfileRxCharUUID }, GATT_PERMIT_WRITE, 0, chipOBleProfileRxCharVal },
118
119     // CHIPoBLE Rx Characteristic User Description
120     { { ATT_BT_UUID_SIZE, charUserDescUUID }, GATT_PERMIT_READ, 0, chipOBleProfileRxdDataUserDesp },
121 };
122
123 /*********************************************************************
124  * LOCAL FUNCTIONS
125  */
126 static bStatus_t CHIPoBLEProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t * pAttr, uint8_t * pValue, uint16_t * pLen,
127                                             uint16_t offset, uint16_t maxLen, uint8_t method);
128 static bStatus_t CHIPoBLEProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t * pAttr, uint8_t * pValue, uint16_t len,
129                                              uint16_t offset, uint8_t method);
130
131 /*********************************************************************
132  * PROFILE CALLBACKS
133  */
134
135 // CHIPoBLE Service Callbacks
136 // Note: When an operation on a characteristic requires authorization and
137 // pfnAuthorizeAttrCB is not defined for that characteristic's service, the
138 // Stack will report a status of ATT_ERR_UNLIKELY to the client.  When an
139 // operation on a characteristic requires authorization the Stack will call
140 // pfnAuthorizeAttrCB to check a client's authorization prior to calling
141 // pfnReadAttrCB or pfnWriteAttrCB, so no checks for authorization need to be
142 // made within these functions.
143 const gattServiceCBs_t chipOBleProfileCBs = {
144     CHIPoBLEProfile_ReadAttrCB,  // Read callback function pointer
145     CHIPoBLEProfile_WriteAttrCB, // Write callback function pointer
146     NULL                         // Authorization callback function pointer
147 };
148
149 /*********************************************************************
150  * PUBLIC FUNCTIONS
151  */
152
153 /*********************************************************************
154  * @fn      CHIPoBLEProfile_AddService
155  *
156  * @brief   Initializes the Simple Profile service by registering
157  *          GATT attributes with the GATT server.
158  *
159  * @param   services - services to add. This is a bit map and can
160  *                     contain more than one service.
161  *
162  * @return  Success or Failure
163  */
164 bStatus_t CHIPoBLEProfile_AddService(uint32 services)
165 {
166     uint8 status;
167
168     // Allocate Client Characteristic Configuration tables
169     chipOBleProfileTxStateDataConfig = (gattCharCfg_t *) ICall_malloc((uint_least16_t)(sizeof(gattCharCfg_t) * MAX_NUM_BLE_CONNS));
170     if (chipOBleProfileTxStateDataConfig == NULL)
171     {
172         return bleMemAllocError;
173     }
174
175     // Initialize Client Characteristic Configuration attributes
176     GATTServApp_InitCharCfg(LL_CONNHANDLE_INVALID, chipOBleProfileTxStateDataConfig);
177
178     if (services & CHIPOBLEPROFILE_SERVICE)
179     {
180         // Register GATT attribute list and CBs with GATT Server App
181         status = GATTServApp_RegisterService(chipoBleProfileAttrTbl, GATT_NUM_ATTRS(chipoBleProfileAttrTbl),
182                                              GATT_MAX_ENCRYPT_KEY_SIZE, &chipOBleProfileCBs);
183     }
184     else
185     {
186         status = SUCCESS;
187     }
188
189     return status;
190 }
191
192 /*********************************************************************
193  * @fn      CHIPoBLEProfile_RegisterAppCBs
194  *
195  * @brief   Registers the application callback function. Only call
196  *          this function once.
197  *
198  * @param   callbacks - pointer to application callbacks.
199  *
200  * @return  SUCCESS or bleAlreadyInRequestedMode
201  */
202 bStatus_t CHIPoBLEProfile_RegisterAppCBs(chipOBleProfileCBs_t * appCallbacks)
203 {
204     if (appCallbacks)
205     {
206         chipOBleProfile_AppCBs = appCallbacks;
207
208         return SUCCESS;
209     }
210     else
211     {
212         return bleAlreadyInRequestedMode;
213     }
214 }
215
216 /*********************************************************************
217  * @fn      CHIPoBLEProfile_SetParameter
218  *
219  * @brief   Set a CHIPoBLE Characteristic value
220  *
221  * @param   param - Profile parameter ID
222  * @param   len - length of data to write
223  * @param   value - pointer to data to write.  This is dependent on
224  *          the parameter ID and WILL be cast to the appropriate
225  *          data type (example: data type of uint16 will be cast to
226  *          uint16 pointer).
227  *
228  * @return  bStatus_t
229  */
230 extern uint8 gattAppTaskID;
231 bStatus_t CHIPoBLEProfile_SetParameter(uint8 param, uint8 len, void * value, uint8_t taskId)
232 {
233     bStatus_t ret = SUCCESS;
234     switch (param)
235     {
236     case CHIPOBLEPROFILE_TX_CHAR:
237
238         VOID memcpy(chipOBleProfileTxCharVal, value, len);
239
240         // See if Indications has been enabled
241         ret = GATTServApp_ProcessCharCfg(chipOBleProfileTxStateDataConfig, chipOBleProfileTxCharVal, FALSE, chipoBleProfileAttrTbl,
242                                          GATT_NUM_ATTRS(chipoBleProfileAttrTbl), taskId, CHIPoBLEProfile_ReadAttrCB);
243         break;
244     default:
245         ret = INVALIDPARAMETER;
246         break;
247     }
248
249     return ret;
250 }
251
252 /*********************************************************************
253  * @fn      CHIPoBLEProfile_GetParameter
254  *
255  * @brief   Get a CHIPoBLE Characteristic value
256  *
257  * @param   param - Profile parameter ID
258  * @param   value - pointer to data to put.  This is dependent on
259  *          the parameter ID and WILL be cast to the appropriate
260  *          data type (example: data type of uint16 will be cast to
261  *          uint16 pointer).
262  *
263  * @return  bStatus_t
264  */
265 bStatus_t CHIPoBLEProfile_GetParameter(uint8 param, void * value, uint16_t len)
266 {
267     bStatus_t ret = SUCCESS;
268     switch (param)
269     {
270     case CHIPOBLEPROFILE_RX_CHAR:
271         if (len != 0)
272         {
273             VOID memcpy(value, chipOBleProfileRxCharVal, len);
274         }
275         else
276         {
277             VOID memcpy(value, chipOBleProfileRxCharVal, CHIPOBLEPROFILE_CHAR_LEN);
278         }
279         break;
280     case CHIPOBLEPROFILE_CCCWrite:
281         *((uint8 *) value) = chipOBleProfileTxStateDataConfig->value;
282         break;
283     default:
284         ret = INVALIDPARAMETER;
285         break;
286     }
287
288     return ret;
289 }
290
291 /*********************************************************************
292  * @fn          CHIPoBLEProfile_ReadAttrCB
293  *
294  * @brief       Read an attribute.
295  *
296  * @param       connHandle - connection message was received on
297  * @param       pAttr - pointer to attribute
298  * @param       pValue - pointer to data to be read
299  * @param       pLen - length of data to be read
300  * @param       offset - offset of the first octet to be read
301  * @param       maxLen - maximum length of data to be read
302  * @param       method - type of read message
303  *
304  * @return      SUCCESS, blePending or Failure
305  */
306 static bStatus_t CHIPoBLEProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t * pAttr, uint8_t * pValue, uint16_t * pLen,
307                                             uint16_t offset, uint16_t maxLen, uint8_t method)
308 {
309     bStatus_t status = SUCCESS;
310     uint8 len        = maxLen;
311
312     if (offset + maxLen > CHIPOBLEPROFILE_CHAR_LEN)
313         len = CHIPOBLEPROFILE_CHAR_LEN - offset;
314
315     *pLen = len;
316     VOID osal_memcpy(pValue, (pAttr->pValue) + offset, len);
317
318     return status;
319 }
320
321 /*********************************************************************
322  * @fn      CHIPoBLEProfile_WriteAttrCB
323  *
324  * @brief   Validate attribute data prior to a write operation
325  *
326  * @param   connHandle - connection message was received on
327  * @param   pAttr - pointer to attribute
328  * @param   pValue - pointer to data to be written
329  * @param   len - length of data
330  * @param   offset - offset of the first octet to be written
331  * @param   method - type of write message
332  *
333  * @return  SUCCESS, blePending or Failure
334  */
335 static bStatus_t CHIPoBLEProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t * pAttr, uint8_t * pValue, uint16_t len,
336                                              uint16_t offset, uint8_t method)
337 {
338     bStatus_t status = SUCCESS;
339     uint8 notifyApp  = 0xFF;
340
341     if (pAttr->type.len == ATT_UUID_SIZE)
342     {
343         if (!memcmp(pAttr->type.uuid, chipOBleProfileRxCharUUID, pAttr->type.len))
344         {
345             VOID osal_memcpy((pAttr->pValue) + offset, pValue, len);
346
347             // Send notification to BLE Event handler
348             notifyApp = CHIPOBLEPROFILE_RX_CHAR;
349         }
350         else
351         {
352             status = ATT_ERR_ATTR_NOT_FOUND;
353         }
354     }
355
356     else if ((pAttr->type.len == ATT_BT_UUID_SIZE) &&
357              (BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]) == GATT_CLIENT_CHAR_CFG_UUID))
358     {
359
360         notifyApp = CHIPOBLEPROFILE_CCCWrite;
361
362         status = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len, offset, GATT_CLIENT_CFG_INDICATE);
363     }
364     else
365     {
366         // unsupported length
367         status = ATT_ERR_INVALID_HANDLE;
368     }
369
370     // If a characteristic value changed then callback function to notify application of change
371     if ((notifyApp != 0xFF) && chipOBleProfile_AppCBs && chipOBleProfile_AppCBs->pfnchipOBleProfileChange)
372     {
373         chipOBleProfile_AppCBs->pfnchipOBleProfileChange(notifyApp, len, connHandle);
374     }
375
376     return status;
377 }