4. Code Overview

This section provides the code blocks needed to successfully execute this tutorial.

4.1. Advertising Task Code

In main.c file, replace the ble_adv_demo_task() application task with the following code:

/* Initialize the BLE structure related to BD address value. */
static const own_address_t user_bd_address = {
       .addr_type = PRIVATE_STATIC_ADDRESS,
       .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
};


static void ble_adv_demo_task(void *pvParameters)
{
        int8_t wdog_id;

        gap_adv_chnl_t channel_map = GAP_ADV_CHANNEL_37;

        // Remove compiler warnings about the unused parameter
        ( void ) pvParameters;

        /* Register ble_adv_demo task to be monitored by watchdog */
        wdog_id = sys_watchdog_register(false);

        // Start BLE module as a peripheral device
        ble_peripheral_start();

        // Set device name
        ble_gap_device_name_set("Dialog TTT Demo", ATT_PERM_READ);

        // Set BD address to the preferred value
        ble_gap_address_set(&user_bd_address, 0x00FF);

        // Set advertising interval
        ble_gap_adv_intv_set(min,max);

        ble_gap_adv_chnl_map_set(channel_map);

        // Set advertising data
        ble_gap_adv_data_set(sizeof(adv_data), adv_data, 0, NULL);

        // Start advertising
        ble_gap_adv_start(GAP_CONN_MODE_UNDIRECTED);

        for (;;) {
                ble_evt_hdr_t *hdr;

                /* Notify watchdog on each loop */
                sys_watchdog_notify(wdog_id);

                /* Suspend watchdog while blocking on ble_get_event() */
                sys_watchdog_suspend(wdog_id);

                /*
                 * Wait for a BLE event - this task will block
                 * indefinitely until something is received.
                 */
                hdr = ble_get_event(true);

                /* Resume watchdog */
                sys_watchdog_notify_and_resume(wdog_id);

                if (!hdr) {
                        continue;
                }

                switch (hdr->evt_code) {
                case BLE_EVT_GAP_CONNECTED:
                        handle_evt_gap_connected((ble_evt_gap_connected_t *) hdr);
                        break;
                case BLE_EVT_GAP_DISCONNECTED:
                        handle_evt_gap_disconnected((ble_evt_gap_disconnected_t *) hdr);
                        break;
                case BLE_EVT_GAP_PAIR_REQ:
                        handle_evt_gap_pair_req((ble_evt_gap_pair_req_t *) hdr);
                        break;
                default:
                        ble_handle_event_default(hdr);
                        break;
                }

                // Free event buffer
                OS_FREE(hdr);
        }
}

In main.c file, replace the handle_evt_gap_connected() routine with the following code used for triggering the Update Parameter process upon a connection event.

/* Connection index */
static uint16_t connection_index;

/* Advertising interval mode */
#define FAST_ADV_INTERVAL   (0)


#if (FAST_ADV_INTERVAL == 1)
        static const uint16_t min = BLE_ADV_INTERVAL_FROM_MS(80);  // 80 ms
        static const uint16_t max = BLE_ADV_INTERVAL_FROM_MS(100); // 100 ms
#else
        static const uint16_t min = BLE_ADV_INTERVAL_FROM_MS(1000); // 1000 ms
        static const uint16_t max = BLE_ADV_INTERVAL_FROM_MS(1500); // 1500 ms
#endif

/* Update connection parameters. */
static void conn_param_update(uint16_t conn_idx)
{
     gap_conn_params_t cp;

     cp.interval_min  = defaultBLE_PPCP_INTERVAL_MIN;
     cp.interval_max  = defaultBLE_PPCP_INTERVAL_MAX;
     cp.slave_latency = defaultBLE_PPCP_SLAVE_LATENCY;
     cp.sup_timeout   = defaultBLE_PPCP_SUP_TIMEOUT;

     ble_gap_conn_param_update(conn_idx, &cp);
}

/*
 * This timer callback notifies the task that time for discovery, bonding and
 * encryption elapsed. Connection parameters can be changed.
 */
static void conn_param_timer_cb(OS_TIMER timer)
{
        /* Call the function which is responsible for the connection parameters update. */
        conn_param_update(connection_index);
}

static void handle_evt_gap_connected(ble_evt_gap_connected_t *evt)
{
        /*
         * Manage behavior upon connection
         */
        connection_index = evt->conn_idx;

        /* When the timer expires connection parameters will be re-negotiated */
        OS_TIMER update_timer = OS_TIMER_CREATE("conn_timer", OS_MS_2_TICKS(5000),
                                  OS_TIMER_FAIL, (uint32_t) OS_GET_CURRENT_TASK(),
                                                              conn_param_timer_cb);
        OS_TIMER_START(update_timer, OS_TIMER_FOREVER);
}

Optionally, in main.c file change the advertising data:

/*
 * BLE adv demo advertising data
 */
static const uint8_t adv_data[] = {
        0x14, GAP_DATA_TYPE_LOCAL_NAME,
        'H', 'o', 'w', ' ', 'A', 'r', 'e', ' ', 'Y', 'o', 'u', ' ', 'D', 'o', 'd', 'a',
        'y', ' ', '?'
};

4.2. Macro Definitions

In customer_config_xxx.h file, add the following macro definitions:

/* Peripheral-specific configurations */
#define defaultBLE_PPCP_INTERVAL_MIN  (BLE_CONN_INTERVAL_FROM_MS(500)) // 500 ms
#define defaultBLE_PPCP_INTERVAL_MAX  (BLE_CONN_INTERVAL_FROM_MS(750)) // 750 ms
#define defaultBLE_PPCP_SLAVE_LATENCY (0) // 0 events
#define defaultBLE_PPCP_SUP_TIMEOUT   (BLE_SUPERVISION_TMO_FROM_MS(6000)) // 6000 ms