3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2020 Texas Instruments Incorporated
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @file chipOBLeProfile.c
21 * This file contains the CHIPOBLE profile sample GATT service profile
22 * for use with the BLE Manager.
24 /*********************************************************************
32 #include "icall_ble_api.h"
33 #include "ti_ble_config.h"
35 #include "chipOBleProfile.h"
37 /*********************************************************************
40 // CHIPoBLE GATT Profile Service UUID
41 const uint8 chipOBleServUUID[ATT_UUID_SIZE] = { CHIPOBLEPROFILE_SERV_UUID_BASE128(CHIPOBLE_SERV_UUID) };
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)
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)
55 /*********************************************************************
56 * Profile Attributes - variables
59 // Remote Display Profile Service attribute
60 static const gattAttrType_t chipoBleProfile = { ATT_UUID_SIZE, chipOBleServUUID };
62 // ChipOBLE Tx Characteristic Properties
63 static uint8_t chipOBleProfileTxCharProps = GATT_PROP_READ | GATT_PROP_INDICATE;
65 // ChipOBLE Tx Characteristic Value
66 static uint8_t chipOBleProfileTxCharVal[CHIPOBLEPROFILE_CHAR_LEN] = { 0x00 };
68 // ChipOBLE Tx Characteristic User Description
69 static uint8_t chipOBleProfileTxdDataUserDesp[CHIPOBLEPROFILE_MAX_DESCRIPTION_LEN] = "ChipOBLE Tx Char";
71 static gattCharCfg_t * chipOBleProfileTxStateDataConfig;
73 // ChipOBLE Rx Characteristic Properties
74 static uint8_t chipOBleProfileRxCharProps = GATT_PROP_WRITE;
76 // ChipOBLE Rx Characteristic Value
77 static uint8_t chipOBleProfileRxCharVal[CHIPOBLEPROFILE_CHAR_LEN] = { 0x00 };
79 // ChipOBLE Rx Characteristic User Description
80 static uint8_t chipOBleProfileRxdDataUserDesp[CHIPOBLEPROFILE_MAX_DESCRIPTION_LEN] = "ChipOBLE Rx Char";
82 /*********************************************************************
86 static chipOBleProfileCBs_t * chipOBleProfile_AppCBs = NULL;
88 /*********************************************************************
89 * Profile Attributes - Table
92 static gattAttribute_t chipoBleProfileAttrTbl[] = {
95 { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
96 GATT_PERMIT_READ, /* permissions */
98 (uint8 *) &chipoBleProfile /* pValue */
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 },
105 // CHIPoBLE Tx Characteristic configuration
106 { { ATT_BT_UUID_SIZE, clientCharCfgUUID },
107 GATT_PERMIT_READ | GATT_PERMIT_WRITE,
109 (uint8 *) &chipOBleProfileTxStateDataConfig },
111 // CHIPoBLE Tx Characteristic User Description
112 { { ATT_BT_UUID_SIZE, charUserDescUUID }, GATT_PERMIT_READ, 0, chipOBleProfileTxdDataUserDesp },
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 },
119 // CHIPoBLE Rx Characteristic User Description
120 { { ATT_BT_UUID_SIZE, charUserDescUUID }, GATT_PERMIT_READ, 0, chipOBleProfileRxdDataUserDesp },
123 /*********************************************************************
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);
131 /*********************************************************************
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
149 /*********************************************************************
153 /*********************************************************************
154 * @fn CHIPoBLEProfile_AddService
156 * @brief Initializes the Simple Profile service by registering
157 * GATT attributes with the GATT server.
159 * @param services - services to add. This is a bit map and can
160 * contain more than one service.
162 * @return Success or Failure
164 bStatus_t CHIPoBLEProfile_AddService(uint32 services)
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)
172 return bleMemAllocError;
175 // Initialize Client Characteristic Configuration attributes
176 GATTServApp_InitCharCfg(LL_CONNHANDLE_INVALID, chipOBleProfileTxStateDataConfig);
178 if (services & CHIPOBLEPROFILE_SERVICE)
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);
192 /*********************************************************************
193 * @fn CHIPoBLEProfile_RegisterAppCBs
195 * @brief Registers the application callback function. Only call
196 * this function once.
198 * @param callbacks - pointer to application callbacks.
200 * @return SUCCESS or bleAlreadyInRequestedMode
202 bStatus_t CHIPoBLEProfile_RegisterAppCBs(chipOBleProfileCBs_t * appCallbacks)
206 chipOBleProfile_AppCBs = appCallbacks;
212 return bleAlreadyInRequestedMode;
216 /*********************************************************************
217 * @fn CHIPoBLEProfile_SetParameter
219 * @brief Set a CHIPoBLE Characteristic value
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
230 extern uint8 gattAppTaskID;
231 bStatus_t CHIPoBLEProfile_SetParameter(uint8 param, uint8 len, void * value, uint8_t taskId)
233 bStatus_t ret = SUCCESS;
236 case CHIPOBLEPROFILE_TX_CHAR:
238 VOID memcpy(chipOBleProfileTxCharVal, value, len);
240 // See if Indications has been enabled
241 ret = GATTServApp_ProcessCharCfg(chipOBleProfileTxStateDataConfig, chipOBleProfileTxCharVal, FALSE, chipoBleProfileAttrTbl,
242 GATT_NUM_ATTRS(chipoBleProfileAttrTbl), taskId, CHIPoBLEProfile_ReadAttrCB);
245 ret = INVALIDPARAMETER;
252 /*********************************************************************
253 * @fn CHIPoBLEProfile_GetParameter
255 * @brief Get a CHIPoBLE Characteristic value
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
265 bStatus_t CHIPoBLEProfile_GetParameter(uint8 param, void * value, uint16_t len)
267 bStatus_t ret = SUCCESS;
270 case CHIPOBLEPROFILE_RX_CHAR:
273 VOID memcpy(value, chipOBleProfileRxCharVal, len);
277 VOID memcpy(value, chipOBleProfileRxCharVal, CHIPOBLEPROFILE_CHAR_LEN);
280 case CHIPOBLEPROFILE_CCCWrite:
281 *((uint8 *) value) = chipOBleProfileTxStateDataConfig->value;
284 ret = INVALIDPARAMETER;
291 /*********************************************************************
292 * @fn CHIPoBLEProfile_ReadAttrCB
294 * @brief Read an attribute.
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
304 * @return SUCCESS, blePending or Failure
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)
309 bStatus_t status = SUCCESS;
312 if (offset + maxLen > CHIPOBLEPROFILE_CHAR_LEN)
313 len = CHIPOBLEPROFILE_CHAR_LEN - offset;
316 VOID osal_memcpy(pValue, (pAttr->pValue) + offset, len);
321 /*********************************************************************
322 * @fn CHIPoBLEProfile_WriteAttrCB
324 * @brief Validate attribute data prior to a write operation
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
333 * @return SUCCESS, blePending or Failure
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)
338 bStatus_t status = SUCCESS;
339 uint8 notifyApp = 0xFF;
341 if (pAttr->type.len == ATT_UUID_SIZE)
343 if (!memcmp(pAttr->type.uuid, chipOBleProfileRxCharUUID, pAttr->type.len))
345 VOID osal_memcpy((pAttr->pValue) + offset, pValue, len);
347 // Send notification to BLE Event handler
348 notifyApp = CHIPOBLEPROFILE_RX_CHAR;
352 status = ATT_ERR_ATTR_NOT_FOUND;
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))
360 notifyApp = CHIPOBLEPROFILE_CCCWrite;
362 status = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len, offset, GATT_CLIENT_CFG_INDICATE);
366 // unsupported length
367 status = ATT_ERR_INVALID_HANDLE;
370 // If a characteristic value changed then callback function to notify application of change
371 if ((notifyApp != 0xFF) && chipOBleProfile_AppCBs && chipOBleProfile_AppCBs->pfnchipOBleProfileChange)
373 chipOBleProfile_AppCBs->pfnchipOBleProfileChange(notifyApp, len, connHandle);