Added a new GATT service to freertos with Nordic NRF52840

ljaynes wrote on April 30, 2019:

I am trying to add a new service to the Nordic MQTT over BLE demo but I cannot seem to get it to work. I have posted my code below. Can someone tell me what I am doing wrong? The code runs and returns success on IotBle_CreateService but I do not see a new service when I check the BLE connection.

#define BLE_CHAR_UUID_TEST		 				{ 0x20, 0xFF}
#define	BLE_CHAR_UUID_CTRL                                              { 0x2E, 0xFF}

#define BLE_SERVICE_UUID_TEST_TYPE \
{ \
    .uu.uu128 = BLE_CHAR_UUID_TEST,\
    .ucType   = eBTuuidType128 \
}


#define BLE_CHAR_UUID_CTRL_TYPE \
{ \
    .uu.uu128 = BLE_CHAR_UUID_CTRL,\
    .ucType   = eBTuuidType128 \
}


static uint16_t _handlesBuffer[1];

static BTService_t _BLEServices[ 1 ] = { 0 };


static void test_callback( IotBleAttributeEvent_t * pEventParam );


static const IotBleAttributeEventCallback_t pCallBackArray[2] =
        {
  NULL,
  test_callback,
};



static const BTAttribute_t pAttributeTable[] = {
     {    
          .xServiceUUID = BLE_SERVICE_UUID_TEST_TYPE
     },
     {
         .xAttributeType = eBTDbCharacteristic,
         .xCharacteristic = 
         {
              .xUuid = BLE_CHAR_UUID_CTRL_TYPE,
              .xPermissions = ( IOT_BLE_CHAR_READ_PERM | IOT_BLE_CHAR_WRITE_PERM ),
              .xProperties = ( eBTPropRead | eBTPropWrite )
          }
     }
};


void ble_init( void )
{

  BTService_t pBTService;
  BTStatus_t status = eBTStatusFail;
  BaseType_t error = pdFAIL;

  /* Initialize service */
  pBTService.pusHandlesBuffer = _handlesBuffer;
  pBTService.ucInstId = 1;
  pBTService.xNumberOfAttributes = 1;
  pBTService.pxBLEAttributes = (BTAttribute_t *)pAttributeTable;

  status = IotBle_CreateService( &pBTService, (IotBleAttributeEventCallback_t *)pCallBackArray );

  if (status != eBTStatusSuccess)
    SEGGER_RTT_printf(0, "Error creating BLE service
");
  else
    SEGGER_RTT_printf(0, "Ble service created
");

}



static void test_callback( IotBleAttributeEvent_t * pEventParam )
{

  SEGGER_RTT_printf(0, "Ble test callback
");

},

ljaynes wrote on May 02, 2019:

I figured it out.

IOT_BLE_ADD_CUSTOM_SERVICES must be defined as 1.

IotBle_AddCustomServicesCb() function must be created inside of your application. Your service init function will be called inside of the IotBle_AddCustomServicesCb() function.

IotBle_AddCustomServicesCb() is called in the Iot_ble_gap.c if IOT_BLE_ADD_CUSTOM_SERVICES is defined.

Your service will not work if you do not call it inside the IotBle_AddCustomServicesCb().

I could not find this documented anywhere.

hbouvier-AWS wrote on May 03, 2019:

Hello,

That is correct! Thanks for the feedback, we will add it in our documents.
Hugues