This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

strange behavior with API GAPRole_SendUpdateParam

I'm seeing a strange behavior with the API GAPRole_SendUpdateParam(). Immediately after GAP_EstablishLink (see BTool Log [21] below), the first GAP_LinkParamUpdate [22] works well. After this, if I were to call GAPRole_SendUpdateParam() from the application with a desiredMaxInterval that is less than the current max interval, that also works well [23][24]. However, if I were to call the API with a desiredMaxInterval that is greater than the current max interval, then I don't get a response. What's also strange is that if the API is called with a desiredMaxInterval that is greater than the current max interval along with a different desiredConnTimeout, then this would work [25][26].

 

Please help me understand this behavior. (Attached is a simpleBLEPeripheral application that can use the keys on the SmartRF06 to change the connection parameters)

 

Thanks,

Danny

 

Environment:

CC2650DK

tirtos_simplelink_2_13_00_06

cc26xxwaare_2_21_01_15600

xdctools_3_31_01_33_core

IAR

 

BTool Log:

--------------------------------------------------------------------

[18] : <Tx> - 10:16:28.441

-Type          : 0x01 (Command)

-OpCode         : 0xFE09 (GAP_EstablishLinkRequest)

-Data Length   : 0x09 (9) byte(s)

HighDutyCycle : 0x00 (0) (Disable)

WhiteList     : 0x00 (0) (Disable)

AddrTypePeer   : 0x00 (0) (Public)

PeerAddr       : 68:C9:0B:10:60:8C

Dump(Tx):

0000:01 09 FE 09 00 00 00 8C 60 10 0B C9 68         ........`...h

--------------------------------------------------------------------

[19] : <Rx> - 10:16:28.459

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x06 (6) bytes(s)

Event         : 0x067F (1663) (GAP_HCI_ExtentionCommandStatus)

Status         : 0x00 (0) (Success)

OpCode         : 0xFE09 (GAP_EstablishLinkRequest)

DataLength     : 0x00 (0)

Dump(Rx):

0000:04 FF 06 7F 06 00 09 FE 00                     .........

--------------------------------------------------------------------

[20] : <Info> - 10:16:28.564

Device Connected

Handle = 0x0000

Addr Type = 0x00 (Public)

BDAddr = 68:C9:0B:10:60:8C

--------------------------------------------------------------------

[21] : <Rx> - 10:16:28.539

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x14 (20) bytes(s)

Event         : 0x0605 (1541) (GAP_EstablishLink)

Status         : 0x00 (0) (Success)

DevAddrType   : 0x00 (0) (Public)

DevAddr       : 68:C9:0B:10:60:8C

ConnHandle     : 0x0000 (0)

ConnRole       : 0x08 (8) (Central)

ConnInterval   : 0x0050 (80)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x07D0 (2000)

ClockAccuracy : 0x00 (0)

Dump(Rx):

0000:04 FF 14 05 06 00 00 8C 60 10 0B C9 68 00 00 08 ........`...h...

0010:50 00 00 00 D0 07 00                           P......

--------------------------------------------------------------------

[22] : <Rx> - 10:16:35.195

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x0B (11) bytes(s)

Event         : 0x0607 (1543) (GAP_LinkParamUpdate)

Status         : 0x00 (0) (Success)

ConnHandle     : 0x0000 (0)

ConnInterval   : 0x0668 (1640)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x024E (590)

Dump(Rx):

0000:04 FF 0B 07 06 00 00 00 68 06 00 00 4E 02       ........h...N.

--------------------------------------------------------------------

[23] : <Rx> - 10:17:10.553

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x0B (11) bytes(s)

Event         : 0x0607 (1543) (GAP_LinkParamUpdate)

Status         : 0x00 (0) (Success)

ConnHandle     : 0x0000 (0)

ConnInterval   : 0x05F0 (1520)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x0258 (600)

Dump(Rx):

0000:04 FF 0B 07 06 00 00 00 F0 05 00 00 58 02       ............X.

--------------------------------------------------------------------

[24] : <Rx> - 10:17:43.090

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x0B (11) bytes(s)

Event         : 0x0607 (1543) (GAP_LinkParamUpdate)

Status         : 0x00 (0) (Success)

ConnHandle     : 0x0000 (0)

ConnInterval   : 0x05A0 (1440)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x0258 (600)

Dump(Rx):

0000:04 FF 0B 07 06 00 00 00 A0 05 00 00 58 02       ............X.

--------------------------------------------------------------------

[25] : <Rx> - 10:19:19.403

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x0B (11) bytes(s)

Event         : 0x0607 (1543) (GAP_LinkParamUpdate)

Status         : 0x00 (0) (Success)

ConnHandle     : 0x0000 (0)

ConnInterval   : 0x06E0 (1760)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x024E (590)

Dump(Rx):

0000:04 FF 0B 07 06 00 00 00 E0 06 00 00 4E 02       ............N.

--------------------------------------------------------------------

[26] : <Rx> - 10:20:05.281

-Type           : 0x04 (Event)

-EventCode     : 0x00FF (Event)

-Data Length   : 0x0B (11) bytes(s)

Event         : 0x0607 (1543) (GAP_LinkParamUpdate)

Status         : 0x00 (0) (Success)

ConnHandle     : 0x0000 (0)

ConnInterval   : 0x0730 (1840)

ConnLatency   : 0x0000 (0)

ConnTimeout   : 0x0258 (600)

Dump(Rx):

0000:04 FF 0B 07 06 00 00 00 30 07 00 00 58 02       ........0...X.

--------------------------------------------------------------------

/**************************************************************************************************
  Filename:       simpleBLEPeripheral.c
  Revised:        $Date: 2015-01-28 17:22:05 -0800 (Wed, 28 Jan 2015) $
  Revision:       $Revision: 42106 $

  Description:    This file contains the Simple BLE Peripheral sample application
                  for use with the CC2650 Bluetooth Low Energy Protocol Stack.

  Copyright 2013 - 2015 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/

/*********************************************************************
 * INCLUDES
 */
#include <string.h>
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Queue.h>
#include <ICall.h>
#include "gatt.h"
#include "hci.h"
#include "gapgattserver.h"
#include "gattservapp.h"
#include "devinfoservice.h"
#include "simpleGATTprofile.h"
#include "peripheral.h"
#include "gapbondmgr.h"
#include "osal_snv.h"
#include "ICallBleAPIMSG.h"
#include "util.h"
#include "board_lcd.h"
#include "board_key.h"
#include "Board.h"
#include "simpleBLEPeripheral.h"
#include <ti/drivers/lcd/LCDDogm1286.h>

/*********************************************************************
 * CONSTANTS
 */
// Advertising interval when device is discoverable (units of 625us, 96=60ms)
#define DEFAULT_ADVERTISING_INTERVAL          96

// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL

// Minimum connection interval (units of 1.25ms, eg: 40=50ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL     40

// Maximum connection interval (units of 1.25ms, eg: 40=50ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL     1600

// Slave latency to use if automatic parameter update request is enabled
#define DEFAULT_DESIRED_SLAVE_LATENCY         0

// Supervision timeout value (units of 10ms, eg: 500=5s) if automatic parameter
// update request is enabled
#define DEFAULT_DESIRED_CONN_TIMEOUT          600

// Whether to enable automatic parameter update request when a connection is
// formed
#define DEFAULT_ENABLE_UPDATE_REQUEST         TRUE

// Connection Pause Peripheral time value (in seconds)
#define DEFAULT_CONN_PAUSE_PERIPHERAL         6

// How often to perform periodic event (in msec)
#define SBP_PERIODIC_EVT_PERIOD               500

// Task configuration
#define SBP_TASK_PRIORITY                     1
#define SBP_TASK_STACK_SIZE                   644

// Internal Events for RTOS application
#define SBP_STATE_CHANGE_EVT                  0x0001
#define SBP_CHAR_CHANGE_EVT                   0x0002
#define SBP_KEY_CHANGE_EVT                    0x0004
#define SBP_PERIODIC_EVT                      0x0008

/*********************************************************************
 * TYPEDEFS
 */

// App event passed from profiles.
typedef struct
{
  uint8_t event;  // Which profile's event
  uint8_t status; // New status
} sbpEvt_t;

/*********************************************************************
 * LOCAL VARIABLES
 */

// Entity ID globally used to check for source and/or destination of messages
static ICall_EntityID selfEntity;

// Semaphore globally used to post events to the application thread
static ICall_Semaphore sem;

// Clock instances for internal periodic events.
static Clock_Struct periodicClock;

// Queue object used for app messages
static Queue_Struct appMsg;
static Queue_Handle appMsgQueue;

// events flag for internal application events.
static uint16_t events;

// Task configuration
Task_Struct sbpTask;
Char sbpTaskStack[SBP_TASK_STACK_SIZE];

// GAP - SCAN RSP data (max size = 31 bytes)
static uint8_t scanRspData[] =
{
  // complete name
  0x0E,   // length of this data
  GAP_ADTYPE_LOCAL_NAME_COMPLETE,
  (uint8_t)'C',
  (uint8_t)'G',
  (uint8_t)'M',
  (uint8_t)'P',
  (uint8_t)'e',
  (uint8_t)'r',
  (uint8_t)'i',
  (uint8_t)'p',
  (uint8_t)'h',
  (uint8_t)'e',
  (uint8_t)'r',
  (uint8_t)'a',
  (uint8_t)'l',

  // connection interval range
  0x05,   // length of this data
  GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
  LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
  HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
  LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
  HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),

  // Tx power level
  0x02,   // length of this data
  GAP_ADTYPE_POWER_LEVEL,
  0       // 0dBm
};

// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertisting)
static uint8_t advertData[] =
{
  // Flags; this sets the device to use limited discoverable
  // mode (advertises for 30 seconds at a time) instead of general
  // discoverable mode (advertises indefinitely)
  0x02,   // length of this data
  GAP_ADTYPE_FLAGS,
  DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

  // service UUID, to notify central devices what services are included
  // in this peripheral
  0x03,   // length of this data
  GAP_ADTYPE_16BIT_MORE,      // some of the UUID's, but not all
  LO_UINT16(SIMPLEPROFILE_SERV_UUID),
  HI_UINT16(SIMPLEPROFILE_SERV_UUID)
};

// GAP GATT Attributes
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "CGM Peripheral";

// For Application Use
static uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST;
static uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
static uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
static uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY;
static uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT;
static uint16_t connectionParamIdx = GAPROLE_PARAM_UPDATE_ENABLE;

/*********************************************************************
 * LOCAL FUNCTIONS
 */

static void SimpleBLEPeripheral_init( void );
static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1);
static void SimpleBLEPeripheral_processStackMsg(ICall_Hdr *pMsg);
static void SimpleBLEPeripheral_processGATTMsg(gattMsgEvent_t *pMsg);
static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg);
static void SimpleBLEPeripheral_processStateChangeEvt(gaprole_States_t newState);
static void SimpleBLEPeripheral_processCharValueChangeEvt(uint8_t paramID);
static void SimpleBLEPeripheral_performPeriodicTask(void);
static void SimpleBLEPeripheral_stateChangeCB(gaprole_States_t newState);
static void SimpleBLEPeripheral_charValueChangeCB(uint8_t paramID);
static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t status);
static void SimpleBLEPeripheral_clockHandler(UArg arg);

static void LCD_ConnectionParameter()
{
  switch( connectionParamIdx )
  {
  case GAPROLE_PARAM_UPDATE_ENABLE:
    LCD_WRITE_STRING("enableUpdateRequest", LCD_PAGE5);
    if( enableUpdateRequest == TRUE )
      LCD_WRITE_STRING(">>  TRUE", LCD_PAGE6);
    else if( enableUpdateRequest == FALSE )
      LCD_WRITE_STRING(">>  FALSE", LCD_PAGE6);
    else
      LCD_WRITE_STRING(">>  N/A", LCD_PAGE6);
    break;

  case GAPROLE_MIN_CONN_INTERVAL:
    LCD_WRITE_STRING("desiredMinInterval", LCD_PAGE5);
    LCD_WRITE_STRING_VALUE(">>", (uint16_t)(desiredMinInterval*1.25), 10, LCD_PAGE6);
    break;

  case GAPROLE_MAX_CONN_INTERVAL:
    LCD_WRITE_STRING("desiredMaxInterval", LCD_PAGE5);
    LCD_WRITE_STRING_VALUE(">>", (uint16_t)(desiredMaxInterval*1.25), 10, LCD_PAGE6);
    break;

  case GAPROLE_SLAVE_LATENCY:
    LCD_WRITE_STRING("desiredSlaveLatency", LCD_PAGE5);
    LCD_WRITE_STRING_VALUE(">>", (uint16_t)desiredSlaveLatency, 10, LCD_PAGE6);
    break;

  case GAPROLE_TIMEOUT_MULTIPLIER:
    LCD_WRITE_STRING("desiredConnTimeout", LCD_PAGE5);
    LCD_WRITE_STRING_VALUE(">>", (uint16_t)desiredConnTimeout*10, 10, LCD_PAGE6);
    break;
  }
}

static void KEY_UP_ConnectionParameter()
{
  switch( connectionParamIdx )
  {
  case GAPROLE_PARAM_UPDATE_ENABLE:
    if( enableUpdateRequest == TRUE )
      enableUpdateRequest = false;
    else
      enableUpdateRequest = true;
    break;

  case GAPROLE_MIN_CONN_INTERVAL:
    if( desiredMinInterval < 3200 )
      desiredMinInterval += 40;
    break;

  case GAPROLE_MAX_CONN_INTERVAL:
    if( desiredMaxInterval < 3200 )
      desiredMaxInterval += 40;
    break;

  case GAPROLE_SLAVE_LATENCY:
    if( desiredSlaveLatency < 499 )
      desiredSlaveLatency += 1;
    break;

  case GAPROLE_TIMEOUT_MULTIPLIER:
    if( desiredConnTimeout < 3200 )
      desiredConnTimeout += 10;
    break;
  }
}

static void KEY_DOWN_ConnectionParameter()
{
  switch( connectionParamIdx )
  {
  case GAPROLE_PARAM_UPDATE_ENABLE:
    if( enableUpdateRequest == TRUE )
      enableUpdateRequest = false;
    else
      enableUpdateRequest = true;
    break;

  case GAPROLE_MIN_CONN_INTERVAL:
    if( desiredMinInterval > 40 )
      desiredMinInterval -= 40;
    break;

  case GAPROLE_MAX_CONN_INTERVAL:
    if( desiredMaxInterval > 40 )
      desiredMaxInterval -= 40;
    break;

  case GAPROLE_SLAVE_LATENCY:
    if( desiredSlaveLatency > 0 )
      desiredSlaveLatency -= 1;
    break;

  case GAPROLE_TIMEOUT_MULTIPLIER:
    if( desiredConnTimeout > 10 )
      desiredConnTimeout -= 10;
    break;
  }
}

static void KEY_LEFT_ConnectionParameter()
{
  switch( connectionParamIdx )
  {
  case GAPROLE_PARAM_UPDATE_ENABLE:
    connectionParamIdx = GAPROLE_TIMEOUT_MULTIPLIER;
    break;
  case GAPROLE_MIN_CONN_INTERVAL:
    connectionParamIdx = GAPROLE_PARAM_UPDATE_ENABLE;
    break;
  case GAPROLE_MAX_CONN_INTERVAL:
    connectionParamIdx = GAPROLE_MIN_CONN_INTERVAL;
    break;
  case GAPROLE_SLAVE_LATENCY:
    connectionParamIdx = GAPROLE_MAX_CONN_INTERVAL;
    break;
  case GAPROLE_TIMEOUT_MULTIPLIER:
    connectionParamIdx = GAPROLE_SLAVE_LATENCY;
    break;
  }
}

static void KEY_RIGHT_ConnectionParameter()
{
  switch( connectionParamIdx )
  {
  case GAPROLE_PARAM_UPDATE_ENABLE:
    connectionParamIdx = GAPROLE_MIN_CONN_INTERVAL;
    break;
  case GAPROLE_MIN_CONN_INTERVAL:
    connectionParamIdx = GAPROLE_MAX_CONN_INTERVAL;
    break;
  case GAPROLE_MAX_CONN_INTERVAL:
    connectionParamIdx = GAPROLE_SLAVE_LATENCY;
    break;
  case GAPROLE_SLAVE_LATENCY:
    connectionParamIdx = GAPROLE_TIMEOUT_MULTIPLIER;
    break;
  case GAPROLE_TIMEOUT_MULTIPLIER:
    connectionParamIdx = GAPROLE_PARAM_UPDATE_ENABLE;
    break;
  }
}

static void KEY_SELECT_ConnectionParameter()
{
  uint8_t state;

  GAPRole_GetParameter( GAPROLE_STATE, &state );

  if( state == GAPROLE_ADVERTISING )
  {
    GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enableUpdateRequest);
    GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desiredMinInterval);
    GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desiredMaxInterval);
    GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desiredSlaveLatency);
    GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desiredConnTimeout);
  }
  else if( state == GAPROLE_CONNECTED )
  {
    GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enableUpdateRequest);

    GAPRole_SendUpdateParam( desiredMinInterval,
                             desiredMaxInterval,
                             desiredSlaveLatency,
                             desiredConnTimeout,
                             GAPROLE_TERMINATE_LINK );
  }
}

static void KeyPressHandler(uint8_t keys)
{
  SimpleBLEPeripheral_enqueueMsg(SBP_KEY_CHANGE_EVT, keys);
}

static void HandleKeys(uint8_t shift, uint8_t keys)
{
  if (keys & KEY_UP)
  {
    KEY_UP_ConnectionParameter();
  }

  if (keys & KEY_DOWN)
  {
    KEY_DOWN_ConnectionParameter();
  }

  if (keys & KEY_LEFT)
  {
    KEY_LEFT_ConnectionParameter();
  }

  if (keys & KEY_RIGHT)
  {
    KEY_RIGHT_ConnectionParameter();
  }

  if (keys & KEY_SELECT)
  {
    KEY_SELECT_ConnectionParameter();
  }

  LCD_ConnectionParameter();
}

/*********************************************************************
 * PROFILE CALLBACKS
 */

// GAP Role Callbacks
static gapRolesCBs_t SimpleBLEPeripheral_gapRoleCBs =
{
  SimpleBLEPeripheral_stateChangeCB,    // Profile State Change Callbacks
  NULL                                  // When a valid RSSI is read from
                                        // controller (not used by app)
};

// GAP Bond Manager Callbacks
static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
{
  NULL, // Passcode callback (not used by application)
  NULL  // Pairing / Bonding state Callback (not used by application)
};

// Simple GATT Profile Callbacks
static simpleProfileCBs_t SimpleBLEPeripheral_simpleProfileCBs =
{
  SimpleBLEPeripheral_charValueChangeCB // Characteristic value change callback
};

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      SimpleBLEPeripheral_createTask
 *
 * @brief   Task creation function for the Simple BLE Peripheral.
 *
 * @param   None.
 *
 * @return  None.
 */
void SimpleBLEPeripheral_createTask(void)
{
  Task_Params taskParams;

  Task_Params_init(&taskParams);
  taskParams.stack = sbpTaskStack;
  taskParams.stackSize = SBP_TASK_STACK_SIZE;
  taskParams.priority = SBP_TASK_PRIORITY;

  Task_construct(&sbpTask, SimpleBLEPeripheral_taskFxn, &taskParams, NULL);
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_init
 *
 * @brief   Called during initialization and contains application
 *          specific initialization (ie. hardware initialization/setup,
 *          table initialization, power up notification, etc), and
 *          profile initialization/setup.
 *
 * @param   None.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_init(void)
{
	// ******************************************************************
  // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp
  // ******************************************************************
  // Register the current thread as an ICall dispatcher application
  // so that the application can send and receive messages.
  ICall_registerApp(&selfEntity, &sem);

  // Create an RTOS queue for message from profile to be sent to app.
  appMsgQueue = Util_constructQueue(&appMsg);

  // Create one-shot clocks for internal periodic events.
  Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler, SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT);

  Board_openLCD();

  // Initialize keys on SRF06.
  Board_initKeys(KeyPressHandler);

  // Setup the GAP
  GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);

  // Setup the GAP Peripheral Role Profile
  {
    // For all hardware platforms, device starts advertising upon initialization
    uint8_t initialAdvertEnable = TRUE;

    // By setting this to zero, the device will go into the waiting state after
    // being discoverable for 30.72 second, and will not being advertising again
    // until the enabler is set back to TRUE
    uint16_t advertOffTime = 0;

    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initialAdvertEnable);
    GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t), &advertOffTime);
    GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
    GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);

    GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t), &enableUpdateRequest);
    GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desiredMinInterval);
    GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desiredMaxInterval);
    GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t), &desiredSlaveLatency);
    GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t), &desiredConnTimeout);
  }

  // Set the GAP Characteristics
  GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);

  // Set advertising interval
  {
    uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;

    GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt);
    GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt);
    GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt);
    GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt);
  }

  // Setup the GAP Bond Manager
  {
    uint32_t passkey = 0; // passkey "000000"
    uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
    uint8_t mitm = TRUE;
    uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
    uint8_t bonding = TRUE;

    GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
    GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode);
    GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm);
    GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
    GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding);
  }

   // Initialize GATT attributes
  GGS_AddService(GATT_ALL_SERVICES);           // GAP
  GATTServApp_AddService(GATT_ALL_SERVICES);   // GATT attributes
  DevInfo_AddService();                        // Device Information Service
  SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile

  // Setup the SimpleProfile Characteristic Values
  {
    uint8_t charValue1 = 1;
    uint8_t charValue2 = 2;
    uint8_t charValue3 = 3;
    uint8_t charValue4 = 4;
    uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };

    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, sizeof(uint8_t), &charValue1);
    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, sizeof(uint8_t), &charValue2);
    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR3, sizeof(uint8_t), &charValue3);
    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t), &charValue4);
    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5);
  }

  // Register callback with SimpleGATTprofile
  SimpleProfile_RegisterAppCBs(&SimpleBLEPeripheral_simpleProfileCBs);

  // Start the Device
  VOID GAPRole_StartDevice(&SimpleBLEPeripheral_gapRoleCBs);

  // Start Bond Manager
  VOID GAPBondMgr_Register(&simpleBLEPeripheral_BondMgrCBs);

  LCD_WRITE_STRING("CGM Peripheral", LCD_PAGE0);
  LCD_ConnectionParameter();
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_taskFxn
 *
 * @brief   Application task entry point for the Simple BLE Peripheral.
 *
 * @param   a0, a1 - not used.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1)
{
  // Initialize application
  SimpleBLEPeripheral_init();

  // Application main loop
  for (;;)
  {
    // Waits for a signal to the semaphore associated with the calling thread.
    // Note that the semaphore associated with a thread is signaled when a
    // message is queued to the message receive queue of the thread or when
    // ICall_signal() function is called onto the semaphore.
    ICall_Errno errno = ICall_wait(ICALL_TIMEOUT_FOREVER);

    if (errno == ICALL_ERRNO_SUCCESS)
    {
      ICall_EntityID dest;
      ICall_ServiceEnum src;
      ICall_HciExtEvt *pMsg = NULL;

      if (ICall_fetchServiceMsg(&src, &dest, (void **)&pMsg) == ICALL_ERRNO_SUCCESS)
      {
        if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
        {
          // Process inter-task message
          SimpleBLEPeripheral_processStackMsg((ICall_Hdr *)pMsg);
        }

        if (pMsg)
        {
          ICall_freeMsg(pMsg);
        }
      }

      // If RTOS queue is not empty, process app message.
      if (!Queue_empty(appMsgQueue))
      {
        sbpEvt_t *pMsg = (sbpEvt_t *)Util_dequeueMsg(appMsgQueue);
        if (pMsg)
        {
          // Process message.
          SimpleBLEPeripheral_processAppMsg(pMsg);

          // Free the space from the message.
          ICall_free(pMsg);
        }
      }
    }

    if (events & SBP_PERIODIC_EVT)
    {
      events &= ~SBP_PERIODIC_EVT;

      Util_startClock(&periodicClock);

      // Perform periodic application task
      SimpleBLEPeripheral_performPeriodicTask();
    }
  }
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_processStackMsg
 *
 * @brief   Process an incoming stack message.
 *
 * @param   pMsg - message to process
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_processStackMsg(ICall_Hdr *pMsg)
{
  switch (pMsg->event)
  {
    case GATT_MSG_EVENT:
      // Process GATT message
      SimpleBLEPeripheral_processGATTMsg((gattMsgEvent_t *)pMsg);
      break;

    default:
      // do nothing
      break;
  }
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_processGATTMsg
 *
 * @brief   Process GATT messages
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_processGATTMsg(gattMsgEvent_t *pMsg)
{
  GATT_bm_free(&pMsg->msg, pMsg->method);
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_processAppMsg
 *
 * @brief   Process an incoming callback from a profile.
 *
 * @param   pMsg - message to process
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg)
{
  switch (pMsg->event)
  {
    case SBP_STATE_CHANGE_EVT:
      SimpleBLEPeripheral_processStateChangeEvt((gaprole_States_t)pMsg->status);
      break;

    case SBP_CHAR_CHANGE_EVT:
      SimpleBLEPeripheral_processCharValueChangeEvt(pMsg->status);
      break;

    case SBP_KEY_CHANGE_EVT:
      HandleKeys(0, pMsg->status);
      break;

    default:
      // Do nothing.
      break;
  }
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_stateChangeCB
 *
 * @brief   Callback from GAP Role indicating a role state change.
 *
 * @param   newState - new state
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_stateChangeCB(gaprole_States_t newState)
{
  SimpleBLEPeripheral_enqueueMsg(SBP_STATE_CHANGE_EVT, newState);
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_processStateChangeEvt
 *
 * @brief   Process a pending GAP Role state change event.
 *
 * @param   newState - new state
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_processStateChangeEvt(gaprole_States_t newState)
{
  switch ( newState )
  {
    case GAPROLE_STARTED:
      {
        uint8_t ownAddress[B_ADDR_LEN];
        uint8_t systemId[DEVINFO_SYSTEM_ID_LEN];

        GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress);

        // use 6 bytes of device address for 8 bytes of system ID value
        systemId[0] = ownAddress[0];
        systemId[1] = ownAddress[1];
        systemId[2] = ownAddress[2];

        // set middle bytes to zero
        systemId[4] = 0x00;
        systemId[3] = 0x00;

        // shift three bytes up
        systemId[7] = ownAddress[5];
        systemId[6] = ownAddress[4];
        systemId[5] = ownAddress[3];

        DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);

        // Display device address
        LCD_WRITE_STRING(Util_convertBdAddr2Str(ownAddress), LCD_PAGE1);
        LCD_WRITE_STRING("Initialized", LCD_PAGE2);
      }
      break;

    case GAPROLE_ADVERTISING:
      LCD_WRITE_STRING("Advertising", LCD_PAGE2);
      break;

    case GAPROLE_CONNECTED:
      {
        uint8_t peerAddress[B_ADDR_LEN];

        GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR, peerAddress);

        Util_startClock(&periodicClock);

        LCD_WRITE_STRING("Connected", LCD_PAGE2);
        LCD_WRITE_STRING(Util_convertBdAddr2Str(peerAddress), LCD_PAGE3);
      }
      break;

    case GAPROLE_CONNECTED_ADV:
      LCD_WRITE_STRING("Connected Advertising", LCD_PAGE2);
      break;

    case GAPROLE_WAITING:
      Util_stopClock(&periodicClock);

      LCD_WRITE_STRING("Disconnected", LCD_PAGE2);

      // Clear remaining lines
      LCD_WRITE_STRING("", LCD_PAGE3);
      LCD_WRITE_STRING("", LCD_PAGE4);
      break;

    case GAPROLE_WAITING_AFTER_TIMEOUT:
      LCD_WRITE_STRING("Timed Out", LCD_PAGE2);
      break;

    case GAPROLE_ERROR:
      LCD_WRITE_STRING("Error", LCD_PAGE2);
      break;

    default:
      LCD_WRITE_STRING("", LCD_PAGE2);
      break;
  }
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_charValueChangeCB
 *
 * @brief   Callback from Simple Profile indicating a characteristic
 *          value change.
 *
 * @param   paramID - parameter ID of the value that was changed.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_charValueChangeCB(uint8_t paramID)
{
  SimpleBLEPeripheral_enqueueMsg(SBP_CHAR_CHANGE_EVT, paramID);
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_processCharValueChangeEvt
 *
 * @brief   Process a pending Simple Profile characteristic value change
 *          event.
 *
 * @param   paramID - parameter ID of the value that was changed.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_processCharValueChangeEvt(uint8_t paramID)
{
  uint8_t newValue;

  switch(paramID)
  {
    case SIMPLEPROFILE_CHAR1:
      SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR1, &newValue);

      LCD_WRITE_STRING_VALUE("Char 1:", (uint16_t)newValue, 10, LCD_PAGE4);
      break;

    case SIMPLEPROFILE_CHAR3:
      SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR3, &newValue);

      LCD_WRITE_STRING_VALUE("Char 3:", (uint16_t)newValue, 10, LCD_PAGE4);
      break;

    default:
      // should not reach here!
      break;
  }
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_performPeriodicTask
 *
 * @brief   Perform a periodic application task. This function gets called
 *          every five seconds (SBP_PERIODIC_EVT_PERIOD). In this example,
 *          the value of the third characteristic in the SimpleGATTProfile
 *          service is retrieved from the profile, and then copied into the
 *          value of the the fourth characteristic.
 *
 * @param   None.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_performPeriodicTask(void)
{
  // Call to set that value of the fourth characteristic in the profile.
  // Note that if notifications of the fourth characteristic have been
  // enabled by a GATT client device, then a notification will be sent
  // every time this function is called.

#if 0
  uint8_t valueToCopy;

  // Call to retrieve the value of the third characteristic in the profile
  if (SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR3, &valueToCopy) == SUCCESS)
  {
    SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t), &valueToCopy);
  }
#else
  static uint8_t valueToCopy = 0;

  SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t), &valueToCopy);

  valueToCopy++;
#endif
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_clockHandler
 *
 * @brief   Handler function for clock timeouts.
 *
 * @param   arg - event type
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_clockHandler(UArg arg)
{
  // Store the event.
  events |= arg;

  // Wake up the application.
  Semaphore_post(sem);
}

/*********************************************************************
 * @fn      SimpleBLEPeripheral_enqueueMsg
 *
 * @brief   Creates a message and puts the message in RTOS queue.
 *
 * @param   event  - message event.
 * @param   status - message status.
 *
 * @return  None.
 */
static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t status)
{
  sbpEvt_t *pMsg;

  if( pMsg = ICall_malloc(sizeof(sbpEvt_t)) )
  {
    pMsg->event = event;
    pMsg->status = status;

    Util_enqueueMsg( appMsgQueue, sem, (uint8*)pMsg );
  }
}

/*********************************************************************
*********************************************************************/