6. Code Overview

This section provides the code blocks needed to successfully execute this tutorial. It is recommended that you copy these blocks to the new project before reading the tutorial. Please watch the following video.

Presentation on preparing the SmartSnippets Studio for this tutorial.

6.1. Initialization Code

Code snippet of the initialization code block:

/* Definitions for the advertising interval */
#define FAST_ADV_INTERVAL   (0)
#define POWER_ADV_INTERVAL  (1)


OS_TIMER update_timer;
static uint16_t connection_index;

/*
 * 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', ' ', '?'
};

/* 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},
};


#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

6.2. Advertising Task Code

Code snippet of ble_adv_demo_task() function:

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);
        }
}

6.3. Connection Parameter Update Code

Code snippet of connection parameter update code block:

/* 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 the time for discovery, bonding, and encryption elapsed,
 * and connection parameters can be changed to preferred ones.
 */
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;

        /* Add a timer that, when expired, will renegotiate connection parameters. */
        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);
}

static void handle_evt_gap_disconnected(ble_evt_gap_disconnected_t *evt)
{
        /*
         * Manage behavior upon disconnection
         */

        // Restart advertising
        ble_gap_adv_start(GAP_CONN_MODE_UNDIRECTED);
}

Note

Don’t forget to delete the already existing handle_evt_gap_connected() and handle_evt_gap_disconnected() functions.

6.4. Custom Definitions Code

Code snippet for macro definitions defined in customer_config_qspi.h header file:

/* Peripheral specific config */

#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