main/task_ble.c

Mon, 17 Apr 2023 14:54:35 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 17 Apr 2023 14:54:35 +0200
changeset 31
ec5c7794dcd6
permissions
-rw-r--r--

Added basic BLE code.

/**
 * @file task_ble.c
 * @brief BLE task. Secure GATT server.
 */

#include "config.h"


static const char *TAG = "task_ble";



void task_ble( void * pvParameters )
{
    esp_err_t	ret;

    ESP_LOGI(TAG, "Starting BLE task");

    /*
     * Initialize Bluetooth
     */
    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(TAG, "init controller failed: %s", esp_err_to_name(ret));
        goto deadend;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(TAG, "enable controller failed: %s", esp_err_to_name(ret));
        goto deadend;
    }

    ESP_LOGI(TAG, "init bluetooth");
    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(TAG, "init bluetooth failed: %s", esp_err_to_name(ret));
        goto deadend;
    }

    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(TAG, "enable bluetooth failed: %s", esp_err_to_name(ret));
        goto deadend;
    }

    ret = ble_balkon_gatts_register();
    if (ret)
	goto deadend;

    ble_balkon_gap_register();

    ret = ble_balkon_gatts_app_register();
    if (ret)
	goto deadend;

    ret = ble_balkon_gatts_set_mtu();
    if (ret)
	goto deadend;

    ESP_LOGI(TAG, "Start BLE done, enter loop");

    for(;;) {


	vTaskDelay(20 / portTICK_PERIOD_MS);
    } /* for(;;) */


    /*
     * Error point for init errors.
     */
deadend:
    ESP_LOGI(TAG, "Startup error loop");
    for(;;) {
	vTaskDelay(50 / portTICK_PERIOD_MS);
    }
}

mercurial