I created some custom services in Bluetooth Developer studio and added the generated source to my SBP based app. I can read characteristics fine but when i try to enable notifications (from iOS or OS X) the app crashes. The stack doesn't give much useful info, the stack pointer points at "__exit", which doesn't tell much i guess.
I am looking for general ideas what might cause notifications to crash the app or stack?
/**********************************************************************************************
* Filename: battery_service.c
*
* Description: This file contains the implementation of the service.
*
* Generated by:
* BDS version: 1.0.1531.0
* Plugin: Texas Instruments CC26xx BLE SDK v2.1 GATT Server plugin 1.0.2 beta
* Time: Tue Oct 20 2015 19:39:01 GMT-07:00
*
* Copyright (c) 2015, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*************************************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include "bcomdef.h"
#include "OSAL.h"
#include "linkdb.h"
#include "att.h"
#include "gatt.h"
#include "gatt_uuid.h"
#include "gattservapp.h"
#include "gapbondmgr.h"
#include "battery_service.h"
/*********************************************************************
* MACROS
*/
/*********************************************************************
* CONSTANTS
*/
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
// battery_service Service UUID
CONST uint8_t BatteryServiceUUID[ATT_UUID_SIZE] =
{
BATTERY_SERVICE_SERV_UUID_BASE128(BATTERY_SERVICE_SERV_UUID)
};
// voltage UUID
CONST uint8_t bs_VoltageUUID[ATT_UUID_SIZE] =
{
BS_VOLTAGE_UUID_BASE128(BS_VOLTAGE_UUID)
};
/*********************************************************************
* LOCAL VARIABLES
*/
static BatteryServiceCBs_t *pAppCBs = NULL;
/*********************************************************************
* Profile Attributes - variables
*/
// Service declaration
static CONST gattAttrType_t BatteryServiceDecl = { ATT_UUID_SIZE, BatteryServiceUUID };
// Characteristic "voltage" Properties (for declaration)
static uint8_t bs_VoltageProps = GATT_PROP_NOTIFY | GATT_PROP_READ;
// Characteristic "voltage" Value variable
static uint8_t bs_VoltageVal[BS_VOLTAGE_LEN] = {0};
// Characteristic "voltage" CCCD
static gattCharCfg_t *bs_VoltageConfig;
/*********************************************************************
* Profile Attributes - Table
*/
static gattAttribute_t battery_serviceAttrTbl[] =
{
// battery_service Service Declaration
{
{ ATT_BT_UUID_SIZE, primaryServiceUUID },
GATT_PERMIT_READ,
0,
(uint8_t *)&BatteryServiceDecl
},
// voltage Characteristic Declaration
{
{ ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&bs_VoltageProps
},
// voltage Characteristic Value
{
{ ATT_UUID_SIZE, bs_VoltageUUID },
GATT_PERMIT_READ | GATT_PERMIT_READ,
0,
bs_VoltageVal
},
// voltage CCCD
{
{ ATT_BT_UUID_SIZE, clientCharCfgUUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8_t *)&bs_VoltageConfig
},
};
/*********************************************************************
* LOCAL FUNCTIONS
*/
static bStatus_t battery_service_ReadAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
uint16_t maxLen, uint8_t method );
static bStatus_t battery_service_WriteAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t len, uint16_t offset,
uint8_t method );
/*********************************************************************
* PROFILE CALLBACKS
*/
// Simple Profile Service Callbacks
CONST gattServiceCBs_t battery_serviceCBs =
{
battery_service_ReadAttrCB, // Read callback function pointer
battery_service_WriteAttrCB, // Write callback function pointer
NULL // Authorization callback function pointer
};
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*
* BatteryService_AddService- Initializes the BatteryService service by registering
* GATT attributes with the GATT server.
*
*/
bStatus_t BatteryService_AddService( void )
{
uint8_t status;
// Allocate Client Characteristic Configuration table
bs_VoltageConfig = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t) * linkDBNumConns );
if ( bs_VoltageConfig == NULL )
{
return ( bleMemAllocError );
}
// Initialize Client Characteristic Configuration attributes
GATTServApp_InitCharCfg( INVALID_CONNHANDLE, bs_VoltageConfig );
// Register GATT attribute list and CBs with GATT Server App
status = GATTServApp_RegisterService( battery_serviceAttrTbl,
GATT_NUM_ATTRS( battery_serviceAttrTbl ),
GATT_MAX_ENCRYPT_KEY_SIZE,
&battery_serviceCBs );
return ( status );
}
/*
* BatteryService_RegisterAppCBs - Registers the application callback function.
* Only call this function once.
*
* appCallbacks - pointer to application callbacks.
*/
bStatus_t BatteryService_RegisterAppCBs( BatteryServiceCBs_t *appCallbacks )
{
if ( appCallbacks )
{
pAppCBs = appCallbacks;
return ( SUCCESS );
}
else
{
return ( bleAlreadyInRequestedMode );
}
}
/*
* BatteryService_SetParameter - Set a BatteryService parameter.
*
* param - Profile parameter ID
* len - length of data to right
* value - pointer to data to write. This is dependent on
* the parameter ID and WILL be cast to the appropriate
* data type (example: data type of uint16_t will be cast to
* uint16_t pointer).
*/
bStatus_t BatteryService_SetParameter( uint8_t param, uint8_t len, void *value )
{
bStatus_t ret = SUCCESS;
switch ( param )
{
case BS_VOLTAGE_ID:
if ( len == BS_VOLTAGE_LEN )
{
memcpy(bs_VoltageVal, value, len);
// Try to send notification.
GATTServApp_ProcessCharCfg( bs_VoltageConfig, (uint8_t *)&bs_VoltageVal, FALSE,
battery_serviceAttrTbl, GATT_NUM_ATTRS( battery_serviceAttrTbl ),
INVALID_TASK_ID, battery_service_ReadAttrCB);
}
else
{
ret = bleInvalidRange;
}
break;
default:
ret = INVALIDPARAMETER;
break;
}
return ret;
}
/*
* BatteryService_GetParameter - Get a BatteryService parameter.
*
* param - Profile parameter ID
* value - pointer to data to write. This is dependent on
* the parameter ID and WILL be cast to the appropriate
* data type (example: data type of uint16_t will be cast to
* uint16_t pointer).
*/
bStatus_t BatteryService_GetParameter( uint8_t param, void *value )
{
bStatus_t ret = SUCCESS;
switch ( param )
{
default:
ret = INVALIDPARAMETER;
break;
}
return ret;
}
/*********************************************************************
* @fn battery_service_ReadAttrCB
*
* @brief Read an attribute.
*
* @param connHandle - connection message was received on
* @param pAttr - pointer to attribute
* @param pValue - pointer to data to be read
* @param pLen - length of data to be read
* @param offset - offset of the first octet to be read
* @param maxLen - maximum length of data to be read
* @param method - type of read message
*
* @return SUCCESS, blePending or Failure
*/
static bStatus_t battery_service_ReadAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
uint16_t maxLen, uint8_t method )
{
bStatus_t status = SUCCESS;
// See if request is regarding the Voltage Characteristic Value
if ( ! memcmp(pAttr->type.uuid, bs_VoltageUUID, pAttr->type.len) )
{
if ( offset > BS_VOLTAGE_LEN ) // Prevent malicious ATT ReadBlob offsets.
{
status = ATT_ERR_INVALID_OFFSET;
}
else
{
*pLen = MIN(maxLen, BS_VOLTAGE_LEN - offset); // Transmit as much as possible
memcpy(pValue, pAttr->pValue + offset, *pLen);
}
}
else
{
// If we get here, that means you've forgotten to add an if clause for a
// characteristic value attribute in the attribute table that has READ permissions.
*pLen = 0;
status = ATT_ERR_ATTR_NOT_FOUND;
}
return status;
}
/*********************************************************************
* @fn battery_service_WriteAttrCB
*
* @brief Validate attribute data prior to a write operation
*
* @param connHandle - connection message was received on
* @param pAttr - pointer to attribute
* @param pValue - pointer to data to be written
* @param len - length of data
* @param offset - offset of the first octet to be written
* @param method - type of write message
*
* @return SUCCESS, blePending or Failure
*/
static bStatus_t battery_service_WriteAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t len, uint16_t offset,
uint8_t method )
{
bStatus_t status = SUCCESS;
uint8_t paramID = 0xFF;
// See if request is regarding a Client Characterisic Configuration
if ( ! memcmp(pAttr->type.uuid, clientCharCfgUUID, pAttr->type.len) )
{
// Allow notification and indication, but do not check if really allowed per CCCD.
status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
offset, GATT_CLIENT_CFG_NOTIFY |
GATT_CLIENT_CFG_INDICATE );
}
else
{
// If we get here, that means you've forgotten to add an if clause for a
// characteristic value attribute in the attribute table that has WRITE permissions.
status = ATT_ERR_ATTR_NOT_FOUND;
}
// Let the application know something changed (if it did) by using the
// callback it registered earlier (if it did).
if (paramID != 0xFF)
if ( pAppCBs && pAppCBs->pfnChangeCb )
pAppCBs->pfnChangeCb( paramID ); // Call app function from stack task context.
return status;
}
/**********************************************************************************************
* Filename: battery_service.c
*
* Description: This file contains the implementation of the service.
*
* Generated by:
* BDS version: 1.0.1531.0
* Plugin: Texas Instruments CC26xx BLE SDK v2.1 GATT Server plugin 1.0.2 beta
* Time: Tue Oct 20 2015 19:39:01 GMT-07:00
*
* Copyright (c) 2015, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*************************************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include "bcomdef.h"
#include "OSAL.h"
#include "linkdb.h"
#include "att.h"
#include "gatt.h"
#include "gatt_uuid.h"
#include "gattservapp.h"
#include "gapbondmgr.h"
#include "battery_service.h"
/*********************************************************************
* MACROS
*/
/*********************************************************************
* CONSTANTS
*/
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
// battery_service Service UUID
CONST uint8_t BatteryServiceUUID[ATT_UUID_SIZE] =
{
BATTERY_SERVICE_SERV_UUID_BASE128(BATTERY_SERVICE_SERV_UUID)
};
// voltage UUID
CONST uint8_t bs_VoltageUUID[ATT_UUID_SIZE] =
{
BS_VOLTAGE_UUID_BASE128(BS_VOLTAGE_UUID)
};
/*********************************************************************
* LOCAL VARIABLES
*/
static BatteryServiceCBs_t *pAppCBs = NULL;
/*********************************************************************
* Profile Attributes - variables
*/
// Service declaration
static CONST gattAttrType_t BatteryServiceDecl = { ATT_UUID_SIZE, BatteryServiceUUID };
// Characteristic "voltage" Properties (for declaration)
static uint8_t bs_VoltageProps = GATT_PROP_NOTIFY | GATT_PROP_READ;
// Characteristic "voltage" Value variable
static uint8_t bs_VoltageVal[BS_VOLTAGE_LEN] = {0};
// Characteristic "voltage" CCCD
static gattCharCfg_t *bs_VoltageConfig;
/*********************************************************************
* Profile Attributes - Table
*/
static gattAttribute_t battery_serviceAttrTbl[] =
{
// battery_service Service Declaration
{
{ ATT_BT_UUID_SIZE, primaryServiceUUID },
GATT_PERMIT_READ,
0,
(uint8_t *)&BatteryServiceDecl
},
// voltage Characteristic Declaration
{
{ ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&bs_VoltageProps
},
// voltage Characteristic Value
{
{ ATT_UUID_SIZE, bs_VoltageUUID },
GATT_PERMIT_READ | GATT_PERMIT_READ,
0,
bs_VoltageVal
},
// voltage CCCD
{
{ ATT_BT_UUID_SIZE, clientCharCfgUUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8_t *)&bs_VoltageConfig
},
};
/*********************************************************************
* LOCAL FUNCTIONS
*/
static bStatus_t battery_service_ReadAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
uint16_t maxLen, uint8_t method );
static bStatus_t battery_service_WriteAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t len, uint16_t offset,
uint8_t method );
/*********************************************************************
* PROFILE CALLBACKS
*/
// Simple Profile Service Callbacks
CONST gattServiceCBs_t battery_serviceCBs =
{
battery_service_ReadAttrCB, // Read callback function pointer
battery_service_WriteAttrCB, // Write callback function pointer
NULL // Authorization callback function pointer
};
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*
* BatteryService_AddService- Initializes the BatteryService service by registering
* GATT attributes with the GATT server.
*
*/
bStatus_t BatteryService_AddService( void )
{
uint8_t status;
// Allocate Client Characteristic Configuration table
bs_VoltageConfig = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t) * linkDBNumConns );
if ( bs_VoltageConfig == NULL )
{
return ( bleMemAllocError );
}
// Initialize Client Characteristic Configuration attributes
GATTServApp_InitCharCfg( INVALID_CONNHANDLE, bs_VoltageConfig );
// Register GATT attribute list and CBs with GATT Server App
status = GATTServApp_RegisterService( battery_serviceAttrTbl,
GATT_NUM_ATTRS( battery_serviceAttrTbl ),
GATT_MAX_ENCRYPT_KEY_SIZE,
&battery_serviceCBs );
return ( status );
}
/*
* BatteryService_RegisterAppCBs - Registers the application callback function.
* Only call this function once.
*
* appCallbacks - pointer to application callbacks.
*/
bStatus_t BatteryService_RegisterAppCBs( BatteryServiceCBs_t *appCallbacks )
{
if ( appCallbacks )
{
pAppCBs = appCallbacks;
return ( SUCCESS );
}
else
{
return ( bleAlreadyInRequestedMode );
}
}
/*
* BatteryService_SetParameter - Set a BatteryService parameter.
*
* param - Profile parameter ID
* len - length of data to right
* value - pointer to data to write. This is dependent on
* the parameter ID and WILL be cast to the appropriate
* data type (example: data type of uint16_t will be cast to
* uint16_t pointer).
*/
bStatus_t BatteryService_SetParameter( uint8_t param, uint8_t len, void *value )
{
bStatus_t ret = SUCCESS;
switch ( param )
{
case BS_VOLTAGE_ID:
if ( len == BS_VOLTAGE_LEN )
{
memcpy(bs_VoltageVal, value, len);
// Try to send notification.
GATTServApp_ProcessCharCfg( bs_VoltageConfig, (uint8_t *)&bs_VoltageVal, FALSE,
battery_serviceAttrTbl, GATT_NUM_ATTRS( battery_serviceAttrTbl ),
INVALID_TASK_ID, battery_service_ReadAttrCB);
}
else
{
ret = bleInvalidRange;
}
break;
default:
ret = INVALIDPARAMETER;
break;
}
return ret;
}
/*
* BatteryService_GetParameter - Get a BatteryService parameter.
*
* param - Profile parameter ID
* value - pointer to data to write. This is dependent on
* the parameter ID and WILL be cast to the appropriate
* data type (example: data type of uint16_t will be cast to
* uint16_t pointer).
*/
bStatus_t BatteryService_GetParameter( uint8_t param, void *value )
{
bStatus_t ret = SUCCESS;
switch ( param )
{
default:
ret = INVALIDPARAMETER;
break;
}
return ret;
}
/*********************************************************************
* @fn battery_service_ReadAttrCB
*
* @brief Read an attribute.
*
* @param connHandle - connection message was received on
* @param pAttr - pointer to attribute
* @param pValue - pointer to data to be read
* @param pLen - length of data to be read
* @param offset - offset of the first octet to be read
* @param maxLen - maximum length of data to be read
* @param method - type of read message
*
* @return SUCCESS, blePending or Failure
*/
static bStatus_t battery_service_ReadAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
uint16_t maxLen, uint8_t method )
{
bStatus_t status = SUCCESS;
// See if request is regarding the Voltage Characteristic Value
if ( ! memcmp(pAttr->type.uuid, bs_VoltageUUID, pAttr->type.len) )
{
if ( offset > BS_VOLTAGE_LEN ) // Prevent malicious ATT ReadBlob offsets.
{
status = ATT_ERR_INVALID_OFFSET;
}
else
{
*pLen = MIN(maxLen, BS_VOLTAGE_LEN - offset); // Transmit as much as possible
memcpy(pValue, pAttr->pValue + offset, *pLen);
}
}
else
{
// If we get here, that means you've forgotten to add an if clause for a
// characteristic value attribute in the attribute table that has READ permissions.
*pLen = 0;
status = ATT_ERR_ATTR_NOT_FOUND;
}
return status;
}
/*********************************************************************
* @fn battery_service_WriteAttrCB
*
* @brief Validate attribute data prior to a write operation
*
* @param connHandle - connection message was received on
* @param pAttr - pointer to attribute
* @param pValue - pointer to data to be written
* @param len - length of data
* @param offset - offset of the first octet to be written
* @param method - type of write message
*
* @return SUCCESS, blePending or Failure
*/
static bStatus_t battery_service_WriteAttrCB( uint16_t connHandle, gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t len, uint16_t offset,
uint8_t method )
{
bStatus_t status = SUCCESS;
uint8_t paramID = 0xFF;
// See if request is regarding a Client Characterisic Configuration
if ( ! memcmp(pAttr->type.uuid, clientCharCfgUUID, pAttr->type.len) )
{
// Allow notification and indication, but do not check if really allowed per CCCD.
status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
offset, GATT_CLIENT_CFG_NOTIFY |
GATT_CLIENT_CFG_INDICATE );
}
else
{
// If we get here, that means you've forgotten to add an if clause for a
// characteristic value attribute in the attribute table that has WRITE permissions.
status = ATT_ERR_ATTR_NOT_FOUND;
}
// Let the application know something changed (if it did) by using the
// callback it registered earlier (if it did).
if (paramID != 0xFF)
if ( pAppCBs && pAppCBs->pfnChangeCb )
pAppCBs->pfnChangeCb( paramID ); // Call app function from stack task context.
return status;
}