This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Unable to read the 128 bit uuid with base uuid

Hello Team

I need to scan the peripheral dongle's 128 bit UUID service and its characteristics,my code is based on heart rate collector of pc-ble-driver,before scanning I am registering the uuid with the softdevice as per the suggestions from your forum posts as below, its adding and registering but the problem is its displaying the uuid as all 0's as 0x0000000000000000 ,I am also attaching the screenshots

ble_uuid_t    dfu_uuid;
    uint8_t       uuid_type = BLE_UUID_TYPE_VENDOR_BEGIN;
    
    /*Base UUIDs for DFU service characteristics*/
    ble_uuid128_t dfu_base_uuid = 
    {{0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}
     };


    dfu_uuid.type = BLE_UUID_TYPE_VENDOR_BEGIN;
    dfu_uuid.uuid = BLE_UUID_VALO_SERVICE;

    err_code = sd_ble_uuid_vs_add(m_adapter,&dfu_base_uuid, &uuid_type);
    if(err_code != NRF_SUCCESS)
    {
         printf("custom uuid failed\n");
         fflush(stdout);
    }
    else
    {
        printf("custom uuid added\n");
        fflush(stdout);
    }
     err_code = ble_db_discovery_evt_register(&dfu_uuid);
     if(err_code != NRF_SUCCESS)
    {
         printf("ble_db_discovery_evt_register failed\n");
         fflush(stdout);
    }
    else
    {
        printf("ble_db_discovery_evt_register added\n");
        fflush(stdout);
    }
    // Initiate procedure to find the primary BLE_UUID_MY_SERVICE.
    err_code = sd_ble_gattc_primary_services_discover(m_adapter,
                                                      m_connection_handle, 0x0E,
                                                      &dfu_uuid);
    if (err_code != NRF_SUCCESS)
    {
        printf("Failed to initiate or continue a GATT Primary Service Discovery procedure\n");
        fflush(stdout);
    }
    else
    {
        printf("Initiated or continue a GATT Primary Service Discovery procedure\n");
        fflush(stdout);
    }
    
output:
Parents
  • Hi,

    You have two uuid_type variables in your code (dfu_uuid.type and uuid_type). You initialize both to BLE_UUID_TYPE_VENDOR_BEGIN, and pass a pointer to uuid_type when you call sd_ble_uuid_vs_add(). But you do not update dfu_uuid.type with the assigned ID, so this may be wrong. If so, using dfu_uuid going forward will not work if the ID is something other than BLE_UUID_TYPE_VENDOR_BEGIN (in practice only if another UUID type has been added before). Could that be the case here? Can you double-check?

  • If I try to not update the dfu_uuid.type  then I "failed to ailed to initiate or continue a GATT Primary Service Discovery"

    Please check the below code

     ble_uuid_t    dfu_uuid;
        uint8_t       uuid_type = BLE_UUID_TYPE_VENDOR_BEGIN;
        
        /*Base UUIDs for DFU service characteristics*/
        ble_uuid128_t dfu_base_uuid = 
        {{0x4C0x660xE50x680x7C0x9D0x4D0x0E0x940x9B0x050x7A0x020xFF0xFC0xFA}
         };


        
        dfu_uuid.uuid = BLE_UUID_VALO_SERVICE;

        err_code = sd_ble_uuid_vs_add(m_adapter,&dfu_base_uuid, &uuid_type);
        if(err_code != NRF_SUCCESS)
        {
             printf("custom uuid failed\n");
             fflush(stdout);
        }
        else
        {
            printf("custom uuid added\n");
            fflush(stdout);
        }
         err_code = ble_db_discovery_evt_register(&dfu_uuid);
         if(err_code != NRF_SUCCESS)
        {
             printf("ble_db_discovery_evt_register failed\n");
             fflush(stdout);
        }
        else
        {
            printf("ble_db_discovery_evt_register added\n");
            fflush(stdout);
        }
        // Initiate procedure to find the primary BLE_UUID_VALO_SERVICE.
        err_code = sd_ble_gattc_primary_services_discover(m_adapter,
                                                          m_connection_handle, 0x0E,
                                                          &dfu_uuid);
        if (err_code != NRF_SUCCESS)
        {
            printf("Failed to initiate or continue a GATT Primary Service Discovery procedure\n");
            fflush(stdout);
        }
        else
        {
            printf("Initiated or continue a GATT Primary Service Discovery procedure\n");
            fflush(stdout);
        }
    ble_db_discovery_init failed 0
    Discovering primary services
    custom uuid added
    ble_db_discovery_evt_register added
    Failed to initiate or continue a GATT Primary Service Discovery procedure
    Received an un-handled event with ID: 18
    Discovering primary services
    custom uuid added
    ble_db_discovery_evt_register added
    Failed to initiate or continue a GATT Primary Service Discovery procedure
    Received an un-handled event with ID: 18
    Disconnected, reason: 0x08
  • Hi,

    Beulah Preethi Vallur said:
    Could you please correct my code ,you mean not to update the dfu_uuid.type with the assigned ID(BLE_UUID_TYPE_VENDOR_BEGIN)? Could you please elaborate your answer so I can understand

    You pass a pointer to one variable (uuid_type) to the call to sd_ble_uuid_vs_add(), and this variable is populated with the ID for this UUID base type by the sd_ble_uuid_vs_add() function (so it is an output). But you do not use this variable further or the data from it, you just use dfu_uuid.type. And since this is not set to the value of uuid_type that may not be correct. This may not be the issue though since it will be BLE_UUID_TYPE_VENDOR_BEGIN  if this is the first to be added. Can you print both and double-check their values? Then you should fix the issue, by passing &dfu_uuid.type instead of &uuid_type to sd_ble_uuid_vs_add()

    Beulah Preethi Vallur said:
    If I try to not update the dfu_uuid.type  then I "failed to ailed to initiate or continue a GATT Primary Service Discovery"

    I see. This suggests that BLE_UUID_TYPE_VENDOR_BEGIN was correct (by luck since this was the first base UUID to be added), so that is likely not the issue.

    Beulah Preethi Vallur said:
    Please check the below code

    With the current changes, the type (dfu_uuid.type) is random (whatever happens to be in memory), since you still pass &uuid_type but don't set dfu_uuid.type. So this will not work. Also, I strongly recomend that you don't just log that there was an error, you should also log the error code for all functions that don't return NRF_SUCCESS to make the logging a lot more useful.

    Can you try to either revert back your code, or do as I suggested, and then post the complete code in a readable way so that I can look at it properly? Either by inserting the files using Insert -> Insert image/video/file or using Insert -> Insert Code.

  • Hello Eric,

    Please find the attached code with printing the error code for all the functions and the values in dfu_uuid.type and uuid.type is '2' (i.e BLE_UUID_TYPE_VENDOR_BEGIN)

    static uint32_t service_discovery_start()
    {
        uint32_t   err_code;
        uint16_t   start_handle = 0x01;
        db_discovery_init();
        
        printf("Discovering primary services\n");
        fflush(stdout);
        ble_uuid_t    dfu_uuid;
        uint8_t       uuid_type = BLE_UUID_TYPE_VENDOR_BEGIN;
        
        /*Base UUIDs for DFU service characteristics*/
        ble_uuid128_t dfu_base_uuid = 
        {{0x4C, 0x66, 0xE5, 0x68, 0x7C, 0x9D, 0x4D, 0x0E, 0x94, 0x9B, 0x05, 0x7A, 0x02, 0xFF, 0xFC, 0xFA}
         };
    
        dfu_uuid.type = BLE_UUID_TYPE_VENDOR_BEGIN;
        dfu_uuid.uuid = BLE_UUID_VALO_SERVICE;
        
        err_code = sd_ble_uuid_vs_add(m_adapter,&dfu_base_uuid, &dfu_uuid.type);
        if(err_code != NRF_SUCCESS)
        {
             printf("custom uuid failed %d\n",err_code);
             fflush(stdout);
        }
        else
        {
            printf("custom uuid added %d\n",err_code);
            fflush(stdout);
        }
         err_code = ble_db_discovery_evt_register(&dfu_uuid);
         if(err_code != NRF_SUCCESS)
        {
             printf("ble_db_discovery_evt_register failed %d\n",err_code);
             fflush(stdout);
        }
        else
        {
            printf("ble_db_discovery_evt_register added %d\n",err_code);
            fflush(stdout);
        }
    
        // Initiate procedure to find the primary BLE_UUID_VALO_SERVICE.
        err_code = sd_ble_gattc_primary_services_discover(m_adapter,
                                                          m_connection_handle, 0x0E,
                                                          &dfu_uuid);
        printf("dfu_uuid.type = %d\n and dfu_uuid.uuid = %d\n",dfu_uuid.type,dfu_uuid.uuid);
        fflush(stdout);
        printf("uuid.type = %d\n",uuid_type);
        fflush(stdout);
        if (err_code != NRF_SUCCESS)
        {
            printf("Failed to initiate or continue a GATT Primary Service Discovery procedure %d\n",err_code);
            fflush(stdout);
        }
        else
        {
            printf("Initiated or continue a GATT Primary Service Discovery procedure %d\n",err_code);
            fflush(stdout);
        }
        
        return err_code;
    }

  • Hi,

    This looks better. I see you pass the correct &dfu_uuid.type to sd_ble_uuid_vs_add() now, which is good. Now it is populated with the correct value (you can also delete your uuid_type variable, which is no longer in use, but it is not important).

    From what I can see from your log output there are no errors now (at least in the screenshots). I see the printout with "Discovered valo service" with an all zero UUID, but I don't see any code showing where and how this ins printed, so I cannot say any details about this. Please share the relevant code and logs and describe how they do not match your expectations.

Reply
  • Hi,

    This looks better. I see you pass the correct &dfu_uuid.type to sd_ble_uuid_vs_add() now, which is good. Now it is populated with the correct value (you can also delete your uuid_type variable, which is no longer in use, but it is not important).

    From what I can see from your log output there are no errors now (at least in the screenshots). I see the printout with "Discovered valo service" with an all zero UUID, but I don't see any code showing where and how this ins printed, so I cannot say any details about this. Please share the relevant code and logs and describe how they do not match your expectations.

Children
  • /*
     * copyright (c) 2012 - 2018, nordic semiconductor asa
     * all rights reserved.
     *
     * redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     *
     * 1. redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     *
     * 2. redistributions in binary form, except as embedded into a nordic
     *    semiconductor asa integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     *
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     *
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     *
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     *
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /**@example examples/heart_rate_collector
     *
     * @brief Heart Rate Collector Sample Application main file.
     *
     * This file contains the source code for a sample application that acts as a BLE Central device.
     * This application scans for a Heart Rate Sensor device and reads it's heart rate data.
     * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml
     *
     * Structure of this file
     * - Includes
     * - Definitions
     * - Global variables
     * - Global functions
     * - Event functions
     * - Event dispatcher
     * - Main
     */
    
    /** Includes */
    
    #include <stdbool.h>
    #include <stdio.h>
    #include <string.h>
    
    #include "sd_rpc.h"
    #include "ble.h"
    //#include "ble_nus_c_modified.h"
    
    /** Definitions */
    #define DEFAULT_BAUD_RATE 1000000 /**< The baud rate to be used for serial communication with nRF5 device. */
    
    #ifdef _WIN32
    #define DEFAULT_UART_PORT_NAME "COM1"
    #endif
    #ifdef __APPLE__
    #define DEFAULT_UART_PORT_NAME "/dev/tty.usbmodem00000"
    #endif
    #ifdef __linux__
    #define DEFAULT_UART_PORT_NAME "/dev/ttyACM0"
    #endif
    
    enum
    {
        UNIT_0_625_MS = 625,  /**< Number of microseconds in 0.625 milliseconds. */
        UNIT_1_25_MS  = 1250, /**< Number of microseconds in 1.25 milliseconds. */
        UNIT_10_MS    = 10000 /**< Number of microseconds in 10 milliseconds. */
    };
    
    #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
    
    #define SCAN_INTERVAL 0x00A0 /**< Determines scan interval in units of 0.625 milliseconds. */
    #define SCAN_WINDOW   0x0050 /**< Determines scan window in units of 0.625 milliseconds. */
    #define SCAN_TIMEOUT  0x0    /**< Scan timeout between 0x01 and 0xFFFF in seconds, 0x0 disables timeout. */
    
    #define MIN_CONNECTION_INTERVAL         MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Determines minimum connection interval in milliseconds. */
    #define MAX_CONNECTION_INTERVAL         MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Determines maximum connection interval in milliseconds. */
    #define SLAVE_LATENCY                   0                                /**< Slave Latency in number of connection events. */
    #define CONNECTION_SUPERVISION_TIMEOUT  MSEC_TO_UNITS(4000, UNIT_10_MS)  /**< Determines supervision time-out in units of 10 milliseconds. */
    
    #define TARGET_DEV_NAME "VALO V0.30.0-g61844b08" /**< Connect to a peripheral using a given advertising name here. */
    #define MAX_PEER_COUNT 1            /**< Maximum number of peer's application intends to manage. */
    #define BLE_DB_DISCOVERY_MAX_SRV        21  
    #define DB_DISCOVERY_MAX_USERS BLE_DB_DISCOVERY_MAX_SRV 
    #define BLE_UUID_HEART_RATE_SERVICE          0x180D /**< Heart Rate service UUID. */
    #define BLE_UUID_VALO_Mode1_CHAR 0x0001 /**< MODE 1 status characteristic UUID. */
    #define BLE_UUID_CCCD                        0x2902
    #define BLE_CCCD_NOTIFY                      0x01
    #define BLE_UUID_VALO_SERVICE          0x0000 /**< VALO service UUID. */
    #define BLE_UUID_VALO_SERVICE1 0xFAFC00007A059B940E4D9D7C68E5664C
    #define STRING_BUFFER_SIZE 50
    #define UUID16_SIZE             2                               /**< Size of 16 bit UUID */
    #define UUID32_SIZE             4                               /**< Size of 32 bit UUID */
    #define UUID128_SIZE            16                              /**< Size of 128 bit UUID */
    /** @brief Compare for equality both type and uuid members of two (valid, non-null) ble_uuid_t pointers. */
    #define BLE_UUID_EQ(p_uuid1, p_uuid2) \
                (((p_uuid1)->type == (p_uuid2)->type) && ((p_uuid1)->uuid == (p_uuid2)->uuid))
    static const ble_uuid_t m_nus_uuid = 
      {
        .uuid = BLE_UUID_VALO_SERVICE, // NORDIC: Use our service UUID instead
        .type = BLE_UUID_TYPE_VENDOR_BEGIN  // NORDIC: Use type vendor specific
      };
    typedef struct
    {
        uint8_t *     p_data;   /**< Pointer to data. */
        uint16_t      data_len; /**< Length of data. */
    } data_t;
    
    
    /** Global variables */
    static ble_uuid_t m_registered_handlers[DB_DISCOVERY_MAX_USERS];
    static uint8_t     m_connected_devices          = 0;
    static uint16_t    m_connection_handle          = 0;
    static uint16_t    m_service_start_handle       = 0;
    static uint16_t    m_service_end_handle         = 0;
    static uint16_t    m_hrm_char_handle            = 0;
    static uint16_t    m_hrm_cccd_handle            = 0;
    static bool        m_connection_is_in_progress  = false;
    static adapter_t * m_adapter                    = NULL;
    static uint32_t m_num_of_handlers_reg;      /**< The number of handlers registered with the DB Discovery module. */
    static bool     m_initialized = false;      /**< This variable Indicates if the module is initialized or not. */
    
    /** @brief Name of the module used for logger messaging.
     */
    #define NRF_BALLOC_LOG_NAME balloc
    
    #if NRF_BALLOC_CONFIG_DEBUG_ENABLED || NRF_BALLOC_CLI_CMDS
    #define NRF_BALLOC_HAS_NAME 1
    #else
    #define NRF_BALLOC_HAS_NAME 0
    #endif
    
    /**@defgroup NRF_BALLOC_DEBUG Macros for preparing debug flags for block allocator module.
     * @{ */
    #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(words)        (((words) & 0xFF) << 0)
    #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET(flags)        (((flags) >> 0) & 0xFF)
    #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(words)        (((words) & 0xFF) << 8)
    #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET(flags)        (((flags) >> 8) & 0xFF)
    
    #define NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(enable)           (!!(enable) << 16)
    #define NRF_BALLOC_DEBUG_BASIC_CHECKS_GET(flags)            (flags & (1 << 16))
    #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(enable)      (!!(enable) << 17)
    #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_GET(flags)       (flags & (1 << 17))
    #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(enable)    (!!(enable) << 18)
    #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_GET(flags)     (flags & (1 << 18))
    /**@} */
    
    /**@brief Default debug flags for @ref nrf_balloc. This is used by the @ref NRF_BALLOC_DEF macro.
     *        Flags can be changed in @ref sdk_config.
     */
    #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
        #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS                                                      \
        (                                                                                           \
            NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS)           |   \
            NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS)           |   \
            NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED)           |   \
            NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED) |   \
            NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED) \
        )
    #else
        #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS   0
    #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
    #define NRF_LOG_INSTANCE_PTR_DECLARE(_p_name)
    /**@brief Block memory allocator control block.*/
    typedef struct
    {
        uint8_t * p_stack_pointer;          //!< Current allocation stack pointer.
        uint8_t   max_utilization;          //!< Maximum utilization of the memory pool.
    } nrf_balloc_cb_t;
    
    /**@brief Block memory allocator pool instance. The pool is made of elements of the same size. */
    typedef struct
    {
        nrf_balloc_cb_t * p_cb;             //!< Pointer to the instance control block.
        uint8_t         * p_stack_base;     //!< Base of the allocation stack.
                                            /**<
                                             * Stack is used to store handlers to not allocated elements.
                                             */
        uint8_t         * p_stack_limit;    //!< Maximum possible value of the allocation stack pointer.
        void            * p_memory_begin;   //!< Pointer to the start of the memory pool.
                                            /**<
                                             * Memory is used as a heap for blocks.
                                             */
        //NRF_LOG_INSTANCE_PTR_DECLARE(p_log) //!< Pointer to instance of the logger object (Conditionally compiled).
    #if NRF_BALLOC_HAS_NAME
        const char      * p_name;           //!< Pointer to string with pool name.
    #endif
    #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
        uint32_t          debug_flags;      //!< Debugging settings.
                                            /**<
                                             * Debug flag should be created by @ref NRF_BALLOC_DEBUG.
                                             */
    #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
        uint16_t          block_size;       //!< Size of the allocated block (including debug overhead).
                                            /**<
                                             * Single block contains user element with header and tail
                                             * words.
                                             */
    } nrf_balloc_t;
    /**
     * @brief Structure holding dynamic data associated with a module.
     *
     * See @ref NRF_LOG_MODULE_REGISTER and @ref NRF_LOG_INSTANCE_REGISTER.
     */
    typedef struct
    {
        uint16_t     order_idx;     ///< Ordered index of the module (used for auto-completion).
        uint16_t     filter;        ///< Current highest severity level accepted (redundant to @ref nrf_log_module_filter_data_t::filter_lvls, used for optimization)
    } nrf_log_module_dynamic_data_t;
    /* @brief Pool of memory objects.
     */
    typedef nrf_balloc_t nrf_memobj_pool_t;
    #define NRF_QUEUE_LOG_NAME queue
    
    /**@brief Queue control block. */
    typedef struct
    {
        volatile size_t front;          //!< Queue front index.
        volatile size_t back;           //!< Queue back index.
        size_t max_utilization;         //!< Maximum utilization of the queue.
    } nrf_queue_cb_t;
    
    /**@brief Supported queue modes. */
    typedef enum
    {
        NRF_QUEUE_MODE_OVERFLOW,        //!< If the queue is full, new element will overwrite the oldest.
        NRF_QUEUE_MODE_NO_OVERFLOW,     //!< If the queue is full, new element will not be accepted.
    } nrf_queue_mode_t;
    #define NRF_QUEUE_CLI_CMDS 0
    //#define NRF_LOG_INSTANCE_PTR_DECLARE p_name
    /**@brief Instance of the queue. */
    typedef struct
    {
        nrf_queue_cb_t * p_cb;              //!< Pointer to the instance control block.
        void           * p_buffer;          //!< Pointer to the memory that is used as storage.
        size_t           size;              //!< Size of the queue.
        size_t           element_size;      //!< Size of one element.
        nrf_queue_mode_t mode;              //!< Mode of the queue.
    #if NRF_QUEUE_CLI_CMDS
        const char      * p_name;           //!< Pointer to string with queue name.
    #endif
        //NRF_LOG_INSTANCE_PTR_DECLARE(p_log) //!< Pointer to instance of the logger object (Conditionally compiled).
    } nrf_queue_t;
    /**@brief Descriptor for the BLE GATT Queue instance. */
    typedef struct
    {
        uint16_t            const max_conns;      /**< Maximal number of connection handles that can be registered. */
        uint16_t                * p_conn_handles; /**< Pointer to array with registered connection handles.*/
        nrf_queue_t const * const p_req_queue;    /**< Pointer to array of queue instances used to hold nrf_ble_gq_req_t instances.*/
        nrf_queue_t const * const p_purge_queue;  /**< Pointer to the queue instance used to hold indexes of queues to purge.*/
        nrf_memobj_pool_t const * p_data_pool;    /**< Memory pool used to obtain nrf_memobj_t instances.*/
    } nrf_ble_gq_t;
    static nrf_ble_gq_t                   * mp_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
    //#define DB_DISCOVERY_MAX_USERS 6  /**< The maximum number of users/registrations allowed by this module. */
    #define MODULE_INITIALIZED (m_initialized == true) 
    //static ble_db_discovery_t       m_ble_db_discovery;             /**< Instance of database discovery module. Must be passed to all db_discovert API calls */
    #if NRF_SD_BLE_API >= 5
    static uint32_t    m_config_id                  = 1;
    #endif
    
    #if NRF_SD_BLE_API >= 6
    static uint8_t     mp_data[100]                 = { 0 };
    static ble_data_t  m_adv_report_buffer;
    #endif
    
    static const ble_gap_scan_params_t m_scan_param =
    {
    #if NRF_SD_BLE_API >= 6
        0,                       // Set if accept extended advertising packetets.
        0,                       // Set if report inomplete reports.
    #endif
        0,                       // Set if active scanning.
    #if NRF_SD_BLE_API < 6
        0,                       // Set if selective scanning.
    #endif
    #if NRF_SD_BLE_API >= 6
        BLE_GAP_SCAN_FP_ACCEPT_ALL,
        BLE_GAP_PHY_1MBPS,
    #endif
    #if NRF_SD_BLE_API == 2
        NULL,                    // Set white-list.
    #endif
    #if NRF_SD_BLE_API == 3 || NRF_SD_BLE_API == 5
        0,                       // Set adv_dir_report.
    #endif
        (uint16_t)SCAN_INTERVAL,
        (uint16_t)SCAN_WINDOW,
        (uint16_t)SCAN_TIMEOUT
    #if NRF_SD_BLE_API >= 6
        , { 0 }                  // Set chennel mask.
    #endif
    };
    typedef struct
    {
        ble_gattc_char_t characteristic;    /**< Structure containing information about the characteristic. */
        uint16_t         cccd_handle;       /**< CCCD Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if a CCCD is not present at the server. */
        uint16_t         ext_prop_handle;   /**< Extended Properties Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if an Extended Properties descriptor is not present at the server. */
        uint16_t         user_desc_handle;  /**< User Description Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if a User Description descriptor is not present at the server. */
        uint16_t         report_ref_handle; /**< Report Reference Handle value for this characteristic. This will be set to BLE_GATT_HANDLE_INVALID if a Report Reference descriptor is not present at the server. */
    } ble_gatt_db_char_t;
    #define BLE_GATT_DB_MAX_CHARS 6  
    
    /**@brief Structure for holding information about the service and the characteristics present on a
     *        server.
     */
    typedef struct
    {
        ble_uuid_t               srv_uuid;                                  /**< UUID of the service. */
        uint8_t                  char_count;                                /**< Number of characteristics present in the service. */
        ble_gattc_handle_range_t handle_range;                              /**< Service Handle Range. */
        ble_gatt_db_char_t       charateristics[BLE_GATT_DB_MAX_CHARS];     /**< Array of information related to the characteristics present in the service. This list can extend further than one. */
    } ble_gatt_db_srv_t;
    /**@brief DB Discovery event type. */
    typedef enum
    {
        BLE_DB_DISCOVERY_COMPLETE,      /**< Event indicating that the discovery of one service is complete. */
        BLE_DB_DISCOVERY_ERROR,         /**< Event indicating that an internal error has occurred in the DB Discovery module. This could typically be because of the SoftDevice API returning an error code during the DB discover.*/
        BLE_DB_DISCOVERY_SRV_NOT_FOUND, /**< Event indicating that the service was not found at the peer.*/
        BLE_DB_DISCOVERY_AVAILABLE      /**< Event indicating that the DB discovery instance is available.*/
    } ble_db_discovery_evt_type_t;
    
    /**@brief Structure containing the event from the DB discovery module to the application. */
    typedef struct
    {
        ble_db_discovery_evt_type_t evt_type;    /**< Type of event. */
        uint16_t                    conn_handle; /**< Handle of the connection for which this event has occurred. */
        union
        {
            ble_gatt_db_srv_t   discovered_db;  /**< Structure containing the information about the GATT Database at the server. This will be filled when the event type is @ref BLE_DB_DISCOVERY_COMPLETE. The UUID field of this will be filled when the event type is @ref BLE_DB_DISCOVERY_SRV_NOT_FOUND. */
            void const        * p_db_instance;  /**< Pointer to DB discovery instance @ref ble_db_discovery_t, indicating availability to the new discovery process. This will be filled when the event type is @ref BLE_DB_DISCOVERY_AVAILABLE. */
            uint32_t            err_code;       /**< nRF Error code indicating the type of error which occurred in the DB Discovery module. This will be filled when the event type is @ref BLE_DB_DISCOVERY_ERROR. */
        } params;
    } ble_db_discovery_evt_t;
    /**@brief DB Discovery event handler type. */
    typedef void (* ble_db_discovery_evt_handler_t)(ble_db_discovery_evt_t * p_evt);
    static ble_db_discovery_evt_handler_t   m_evt_handler;
    /**@brief     Function for fetching the event handler provided by a registered application module.
     *
     * @param[in] srv_uuid UUID of the service.
     *
     * @retval    evt_handler Event handler of the module, registered for the given service UUID.
     * @retval    NULL If no event handler is found.
     */
    static ble_db_discovery_evt_handler_t registered_handler_get(ble_uuid_t const * p_srv_uuid)
    {//static uint32_t m_num_of_handlers_reg;      /**< The number of handlers registered with the DB Discovery module. */
        for (uint32_t i = 0; i < m_num_of_handlers_reg; i++)
        {
            if (BLE_UUID_EQ(&(m_registered_handlers[i]), p_srv_uuid))
            {  
                return (m_evt_handler);
            }
        }
    
        return NULL;
    }
    
    
    /**@brief     Function for storing the event handler provided by a registered application module.
     *
     * @param[in] p_srv_uuid    The UUID of the service.
     * @param[in] p_evt_handler The event handler provided by the application.
     *
     * @retval    NRF_SUCCESS If the handler was stored or already present in the list.
     * @retval    NRF_ERROR_NO_MEM If there is no space left to store the handler.
     */
    static uint32_t registered_handler_set(ble_uuid_t                     const * p_srv_uuid,
                                           ble_db_discovery_evt_handler_t         p_evt_handler)
    {
       // static uint32_t m_num_of_handlers_reg;      /**< The number of handlers registered with the DB Discovery module. */
       // static bool     m_initialized = true;      /**< This variable Indicates if the module is initialized or not. */
        if (registered_handler_get(p_srv_uuid) != NULL)
        {
            return NRF_SUCCESS;
        }
    
        if (m_num_of_handlers_reg < DB_DISCOVERY_MAX_USERS)
        {
            m_registered_handlers[m_num_of_handlers_reg] = *p_srv_uuid;
            m_num_of_handlers_reg++;
    
            return NRF_SUCCESS;
        }
        else
        {
            return NRF_ERROR_NO_MEM;
        }
    }
    uint32_t ble_db_discovery_evt_register(ble_uuid_t const * p_uuid)
    {
        //VERIFY_PARAM_NOT_NULL(p_uuid);
        //VERIFY_MODULE_INITIALIZED();
    
        return registered_handler_set(p_uuid, m_evt_handler);
    }
    
    static const ble_gap_conn_params_t m_connection_param =
    {
        (uint16_t)MIN_CONNECTION_INTERVAL,
        (uint16_t)MAX_CONNECTION_INTERVAL,
        (uint16_t)SLAVE_LATENCY,
        (uint16_t)CONNECTION_SUPERVISION_TIMEOUT
    };
    
    
    /** Global functions */
    
    /**@brief Function for handling error message events from sd_rpc.
     *
     * @param[in] adapter The transport adapter.
     * @param[in] code Error code that the error message is associated with.
     * @param[in] message The error message that the callback is associated with.
     */
    static void status_handler(adapter_t * adapter, sd_rpc_app_status_t code, const char * message)
    {
        printf("Status: %d, message: %s\n", (uint32_t)code, message);
        fflush(stdout);
    }
    
    /**@brief Function for handling the log message events from sd_rpc.
     *
     * @param[in] adapter The transport adapter.
     * @param[in] severity Level of severity that the log message is associated with.
     * @param[in] message The log message that the callback is associated with.
     */
    static void log_handler(adapter_t * adapter, sd_rpc_log_severity_t severity, const char * message)
    {
        switch (severity)
        {
            case SD_RPC_LOG_ERROR:
                printf("Error: %s\n", message);
                fflush(stdout);
                break;
    
            case SD_RPC_LOG_WARNING:
                printf("Warning: %s\n", message);
                fflush(stdout);
                break;
    
            case SD_RPC_LOG_INFO:
                printf("Info: %s\n", message);
                fflush(stdout);
                break;
    
            default:
                printf("Log: %s\n", message);
                fflush(stdout);
                break;
        }
    }
    
    /**@brief Function for initializing serial communication with the target nRF5 Bluetooth slave.
     *
     * @param[in] serial_port The serial port the target nRF5 device is connected to.
     *
     * @return The new transport adapter.
     */
    static adapter_t * adapter_init(char * serial_port, uint32_t baud_rate)
    {
        physical_layer_t  * phy;
        data_link_layer_t * data_link_layer;
        transport_layer_t * transport_layer;
    
        phy = sd_rpc_physical_layer_create_uart(serial_port,
                                                baud_rate,
                                                SD_RPC_FLOW_CONTROL_NONE,
                                                SD_RPC_PARITY_NONE);
        data_link_layer = sd_rpc_data_link_layer_create_bt_three_wire(phy, 250);
        transport_layer = sd_rpc_transport_layer_create(data_link_layer, 1500);
        return sd_rpc_adapter_create(transport_layer);
    }
    
    /**@brief Function for converting a BLE address to a string.
     *
     * @param[in] address       Bluetooth Low Energy address.
     * @param[out] string_buffer The serial port the target nRF5 device is connected to.
     */
    static void ble_address_to_string_convert(ble_gap_addr_t address, uint8_t * string_buffer)
    {
        const int address_length = 6;
        char      temp_str[3];
    
        for (int i = address_length - 1; i >= 0; --i)
        {
            sprintf(temp_str, "%02X", address.addr[i]);
            strcat((char *)string_buffer, temp_str);
        }
    }
    
    /**
     * @brief Parses advertisement data, providing length and location of the field in case
     *        matching data is found.
     *
     * @param[in]  Type of data to be looked for in advertisement data.
     * @param[in]  Advertisement report length and pointer to report.
     * @param[out] If data type requested is found in the data report, type data length and
     *             pointer to data will be populated here.
     *
     * @retval NRF_SUCCESS if the data type is found in the report.
     * @retval NRF_ERROR_NOT_FOUND if the data type could not be found.
     */
    static uint32_t adv_report_parse(uint8_t type, data_t * p_advdata, data_t * p_typedata)
    {
        uint32_t  index = 0;
        uint8_t * p_data;
    
        p_data = p_advdata->p_data;
    
        while (index < p_advdata->data_len)
        {
            uint8_t field_length = p_data[index];
            uint8_t field_type   = p_data[index + 1];
    
            if (field_type == type)
            {
                p_typedata->p_data   = &p_data[index + 2];
                p_typedata->data_len = field_length - 1;
                return NRF_SUCCESS;
            }
            index += field_length + 1;
        }
        return NRF_ERROR_NOT_FOUND;
    }
    
    /**@brief Function for searching a given name in the advertisement packets.
     *
     * @details Use this function to parse received advertising data and to find a given
     * name in them either as 'complete_local_name' or as 'short_local_name'.
     *
     * @param[in]   p_adv_report   advertising data to parse.
     * @param[in]   name_to_find   name to search.
     * @return   true if the given name was found, false otherwise.
     */
    static bool find_adv_name(const ble_gap_evt_adv_report_t *p_adv_report, const char * name_to_find)
    {
        uint32_t err_code;
        data_t   adv_data;
        data_t   dev_name;
    
        // Initialize advertisement report for parsing
    #if NRF_SD_BLE_API >= 6
        adv_data.p_data     = (uint8_t *)p_adv_report->data.p_data;
        adv_data.data_len   = p_adv_report->data.len;
    #else
        adv_data.p_data     = (uint8_t *)p_adv_report->data;
        adv_data.data_len   = p_adv_report->dlen;
    #endif
    
        //search for advertising names
        err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                                    &adv_data,
                                    &dev_name);
        if (err_code == NRF_SUCCESS)
        {
            if (memcmp(name_to_find, dev_name.p_data, dev_name.data_len )== 0)
            {
                return true;
            }
        }
        else
        {
            // Look for the short local name if it was not found as complete
            err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME,
                                        &adv_data,
                                        &dev_name);
            if (err_code != NRF_SUCCESS)
            {
                return false;
            }
            if (memcmp(name_to_find, dev_name.p_data, dev_name.data_len )== 0)
            {
                return true;
            }
        }
        return false;
    }
    
    /**@brief Function for initializing the BLE stack.
     *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t ble_stack_init()
    {
        uint32_t            err_code;
        uint32_t *          app_ram_base = NULL;
    
    #if NRF_SD_BLE_API <= 3
        ble_enable_params_t ble_enable_params;
        memset(&ble_enable_params, 0, sizeof(ble_enable_params));
       // uint32_t      err_code;
        
        
        
    #endif
    
    #if NRF_SD_BLE_API == 3
        ble_enable_params.gatt_enable_params.att_mtu = GATT_MTU_SIZE_DEFAULT;
    #elif NRF_SD_BLE_API < 3
        ble_enable_params.gatts_enable_params.attr_tab_size     = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
        ble_enable_params.gatts_enable_params.service_changed   = false;
        ble_enable_params.common_enable_params.p_conn_bw_counts = NULL;
        ble_enable_params.common_enable_params.vs_uuid_count    = 1;
        printf("NRF_SD_BLE_API =3");
        fflush(stdout);
    #endif
    
    #if NRF_SD_BLE_API <= 3
        ble_enable_params.gap_enable_params.periph_conn_count   = 1;
        ble_enable_params.gap_enable_params.central_conn_count  = 1;
        ble_enable_params.gap_enable_params.central_sec_count   = 1;
        printf("NRF_SD_BLE_API <=3\n");
        fflush(stdout);
        err_code = sd_ble_enable(m_adapter, &ble_enable_params, app_ram_base);
        
    
    #else
        err_code = sd_ble_enable(m_adapter, app_ram_base);
    #endif
    
        switch (err_code) {
            case NRF_SUCCESS:
                break;
            case NRF_ERROR_INVALID_STATE:
                printf("BLE stack already enabled\n");
                fflush(stdout);
                break;
            default:
                printf("Failed to enable BLE stack. Error code: %d\n", err_code);
                fflush(stdout);
                break;
        }
    
        return err_code;
    }
    
    #if NRF_SD_BLE_API < 5
    /**@brief Set BLE option for the BLE role and connection bandwidth.
     *
     * @return NRF_SUCCESS on option set successfully, otherwise an error code.
     */
    static uint32_t ble_options_set()
    {
    #if NRF_SD_BLE_API <= 3
        ble_opt_t        opt;
        ble_common_opt_t common_opt;
    
        common_opt.conn_bw.role                 = BLE_GAP_ROLE_CENTRAL;
        common_opt.conn_bw.conn_bw.conn_bw_rx   = BLE_CONN_BW_HIGH;
        common_opt.conn_bw.conn_bw.conn_bw_tx   = BLE_CONN_BW_HIGH;
        opt.common_opt                          = common_opt;
    
        return sd_ble_opt_set(m_adapter, BLE_COMMON_OPT_CONN_BW, &opt);
    #else
        return NRF_ERROR_NOT_SUPPORTED;
    #endif
    }
    #endif
    
    #if NRF_SD_BLE_API >= 5
    /**@brief Function for setting configuration for the BLE stack.
     *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t ble_cfg_set(uint8_t conn_cfg_tag)
    {
        const uint32_t ram_start = 0; // Value is not used by ble-driver
        uint32_t error_code;
        ble_cfg_t ble_cfg;
    
        // Configure the connection roles.
        memset(&ble_cfg, 0, sizeof(ble_cfg));
    
    #if NRF_SD_BLE_API >= 6
        ble_cfg.gap_cfg.role_count_cfg.adv_set_count        = BLE_GAP_ADV_SET_COUNT_DEFAULT;
    #endif
        ble_cfg.gap_cfg.role_count_cfg.periph_role_count    = 0;
        ble_cfg.gap_cfg.role_count_cfg.central_role_count   = 1;
        ble_cfg.gap_cfg.role_count_cfg.central_sec_count    = 0;
    
        error_code = sd_ble_cfg_set(m_adapter, BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, ram_start);
        if (error_code != NRF_SUCCESS)
        {
            printf("sd_ble_cfg_set() failed when attempting to set BLE_GAP_CFG_ROLE_COUNT. Error code: 0x%02X\n", error_code);
            fflush(stdout);
            return error_code;
        }
    
        memset(&ble_cfg, 0x00, sizeof(ble_cfg));
        ble_cfg.conn_cfg.conn_cfg_tag                 = conn_cfg_tag;
        ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = 150;
    
        error_code = sd_ble_cfg_set(m_adapter, BLE_CONN_CFG_GATT, &ble_cfg, ram_start);
        if (error_code != NRF_SUCCESS)
        {
            printf("sd_ble_cfg_set() failed when attempting to set BLE_CONN_CFG_GATT. Error code: 0x%02X\n", error_code);
            fflush(stdout);
            return error_code;
        }
    
        return NRF_SUCCESS;
    }
    #endif
    
    /**@brief Start scanning (GAP Discovery procedure, Observer Procedure).
     * *
     * @return NRF_SUCCESS on successfully initiating scanning procedure, otherwise an error code.
     */
    static uint32_t scan_start()
    {
    #if NRF_SD_BLE_API >= 6
        m_adv_report_buffer.p_data = mp_data;
        m_adv_report_buffer.len = sizeof(mp_data);
    #endif
    
        uint32_t error_code = sd_ble_gap_scan_start(m_adapter, &m_scan_param
       
    #if NRF_SD_BLE_API >= 6
        , &m_adv_report_buffer
    #endif
        );
    
        if (error_code != NRF_SUCCESS)
        {
            printf("Scan start failed with error code: %d\n", error_code);
            fflush(stdout);
        } else
        {
            printf("\nScan started-perfect\n");
            fflush(stdout);
        }
    
        return error_code;
    }
    /**@brief DB discovery module initialization struct. */
    typedef struct
    {
        ble_db_discovery_evt_handler_t   evt_handler;  /**< Event handler to be called by the DB Discovery module. */
        nrf_ble_gq_t                   * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
    } ble_db_discovery_init_t;
    
    uint32_t ble_db_discovery_init(ble_db_discovery_init_t * p_db_init)
    {
        uint32_t err_code = NRF_SUCCESS;
        printf("here");
        fflush(stdout);
       // VERIFY_PARAM_NOT_NULL(p_db_init);
       // VERIFY_PARAM_NOT_NULL(p_db_init->evt_handler);
        //VERIFY_PARAM_NOT_NULL(p_db_init->p_gatt_queue);
    
        m_num_of_handlers_reg   = 0;
        m_initialized           = true;
        m_evt_handler           = p_db_init->evt_handler;
        mp_gatt_queue           = p_db_init->p_gatt_queue;
    
        printf("error code is %d\n",err_code);
        fflush(stdout);
        return err_code;
    }
    /**@brief Function for handling database discovery events.
     *
     * @details This function is callback function to handle events from the database discovery module.
     *          Depending on the UUIDs that are discovered, this function should forward the events
     *          to their respective services.
     *
     * @param[in] p_event  Pointer to the database discovery event.
     */
    static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
    {
        //ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
    
            if (p_evt->evt_type != BLE_DB_DISCOVERY_COMPLETE)                           
        {                                                                           
            return;                                                               
        }                                                                           
                                                                                 
        // we only care about the dfu service UUIDs                                 
        if (p_evt->params.discovered_db.srv_uuid.uuid == 0x0000)    
        {       
            printf("srv_uuid.uuid == 0x0000\n");
            fflush(stdout);                                                                     
            for (int i = 0; i < p_evt->params.discovered_db.char_count; i++)        
            {                                                                       
                const ble_gatt_db_char_t *const disc_char = &p_evt->params.discovered_db.charateristics[i];
                printf("here inside for loop\n");
                fflush(stdout);  
                printf("%d: uuid: %04X cccd:%04X. value:%04X\n",              
                        i,                                                          
                        disc_char->characteristic.uuid.uuid,                        
                        disc_char->cccd_handle,                                     
                        disc_char->characteristic.handle_value);    
                fflush(stdout);                
           }                                         
        }
    
    }
    /**@brief Function for initializing the Database Discovery Module. */
    static void db_discovery_init(void)
    {
        uint32_t err_code = ble_db_discovery_init(db_disc_handler);
        if(err_code != NRF_SUCCESS)
        {
          printf("\nble_db_discovery_init success %d\n",err_code);
          fflush(stdout);
        }else
        {
             printf("\nble_db_discovery_init failed %d\n",err_code);
          fflush(stdout);
        }
        
    }
    /**@brief Function called upon connecting to BLE peripheral.
     *
     * @details Initiates primary service discovery.
     *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t service_discovery_start()
    {
        uint32_t   err_code;
        uint16_t   start_handle = 0x01;
        db_discovery_init();
        
        printf("Discovering primary services\n");
        fflush(stdout);
        ble_uuid_t    dfu_uuid;
        uint8_t       uuid_type = BLE_UUID_TYPE_VENDOR_BEGIN;
        
        /*Base UUIDs for DFU service characteristics*/
        ble_uuid128_t dfu_base_uuid = 
        {{0x4C, 0x66, 0xE5, 0x68, 0x7C, 0x9D, 0x4D, 0x0E, 0x94, 0x9B, 0x05, 0x7A, 0x02, 0xFF, 0xFC, 0xFA}
         };
    
        dfu_uuid.type = BLE_UUID_TYPE_VENDOR_BEGIN;
        dfu_uuid.uuid = BLE_UUID_VALO_SERVICE;
        
        err_code = sd_ble_uuid_vs_add(m_adapter,&dfu_base_uuid, &dfu_uuid.type);
        if(err_code != NRF_SUCCESS)
        {
             printf("custom uuid failed %d\n",err_code);
             fflush(stdout);
        }
        else
        {
            printf("custom uuid added %d\n",err_code);
            fflush(stdout);
        }
         err_code = ble_db_discovery_evt_register(&dfu_uuid);
         if(err_code != NRF_SUCCESS)
        {
             printf("ble_db_discovery_evt_register failed %d\n",err_code);
             fflush(stdout);
        }
        else
        {
            printf("ble_db_discovery_evt_register added %d\n",err_code);
            fflush(stdout);
        }
    
        // Initiate procedure to find the primary BLE_UUID_VALO_SERVICE.
        err_code = sd_ble_gattc_primary_services_discover(m_adapter,
                                                          m_connection_handle, 0x0E,
                                                          &dfu_uuid);
        printf("dfu_uuid.type = %d\n and dfu_uuid.uuid = %d\n",dfu_uuid.type,dfu_uuid.uuid);
        fflush(stdout);
        printf("uuid.type = %d\n",uuid_type);
        fflush(stdout);
        if (err_code != NRF_SUCCESS)
        {
            printf("Failed to initiate or continue a GATT Primary Service Discovery procedure %d\n",err_code);
            fflush(stdout);
        }
        else
        {
            printf("Initiated or continue a GATT Primary Service Discovery procedure %d\n",err_code);
            fflush(stdout);
        }
        
        return err_code;
    }
    
    /**@brief Function called upon discovering a BLE peripheral's primary service(s).
     *
     * @details Initiates service's (m_service) characteristic discovery.
     *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t char_discovery_start()
    {
        ble_gattc_handle_range_t handle_range;
    
        printf("Discovering characteristics\n");
        fflush(stdout);
    
        handle_range.start_handle = m_service_start_handle;
        handle_range.end_handle = m_service_end_handle;
    
        return sd_ble_gattc_characteristics_discover(m_adapter, m_connection_handle, &handle_range);
    }
    
    /**@brief Function called upon discovering service's characteristics.
     *
     * @details Initiates heart rate monitor (m_hrm_char_handle) characteristic's descriptor discovery.
     *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t descr_discovery_start()
    {
        ble_gattc_handle_range_t handle_range;
    
        printf("Discovering characteristic's descriptors\n");
        fflush(stdout);
    
        if (m_hrm_char_handle == 0)
        {
            printf("No VALO characteristic handle found\n");
            fflush(stdout);
            return NRF_ERROR_INVALID_STATE;
        }
    
        handle_range.start_handle   = m_hrm_char_handle;
        handle_range.end_handle     = m_service_end_handle;
    
        return sd_ble_gattc_descriptors_discover(m_adapter, m_connection_handle, &handle_range);
    }
    
    /**@brief Function that write's the HRM characteristic's CCCD.
     * *
     * @return NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t hrm_cccd_set(uint8_t value)
    {
        ble_gattc_write_params_t write_params;
        uint8_t                  cccd_value[2] = {value, 0};
    
        printf("Setting HRM CCCD\n");
        fflush(stdout);
    
        if (m_hrm_cccd_handle == 0)
        {
            printf("Error. No CCCD handle has been found\n");
            fflush(stdout);
            return NRF_ERROR_INVALID_STATE;
        }
    
        write_params.handle     = m_hrm_cccd_handle;
        write_params.len        = 2;
        write_params.p_value    = cccd_value;
        write_params.write_op   = BLE_GATT_OP_WRITE_REQ;
        write_params.offset     = 0;
    
        return sd_ble_gattc_write(m_adapter, m_connection_handle, &write_params);
    }
    
    
    /** Event functions */
    
    /**@brief Function called on BLE_GAP_EVT_CONNECTED event.
     *
     * @details Update connection state and proceed to discovering the peer's GATT services.
     *
     * @param[in] p_ble_gap_evt GAP event.
     */
    static void on_connected(const ble_gap_evt_t * const p_ble_gap_evt)
    {
        printf("Connection established\n");
        fflush(stdout);
    
        m_connected_devices++;
        m_connection_handle         = p_ble_gap_evt->conn_handle;
        m_connection_is_in_progress = false;
    
        service_discovery_start();
    }
    static bool is_uuid_present(const ble_uuid_t *p_target_uuid, 
                                const ble_gap_evt_adv_report_t *p_adv_report)
    {
        uint32_t err_code;
        size_t  index = 0;
        uint8_t *p_data = (uint8_t *)&p_adv_report->data;
        ble_uuid_t extracted_uuid; 
        
        while (index < p_adv_report->dlen)
        {
            uint8_t  field_length = p_data[index];
            uint8_t  field_type   = p_data[index+1];
            printf("field_length is %d\n",field_length);
            fflush(stdout);  
            printf("field_type is %d\n",field_type);
            fflush(stdout);
            if ( (field_type == BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE)
               || (field_type == BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE)
               )
            {
                for (uint32_t u_index = 0; u_index < (field_length/(size_t )UUID16_SIZE); u_index++)
                {
                    err_code = sd_ble_uuid_decode(m_adapter, (size_t )UUID16_SIZE, 
                                                    &p_data[u_index * UUID16_SIZE + index + 2], 
                                                    &extracted_uuid);
                    if (err_code == NRF_SUCCESS)
                    {
                        if ((extracted_uuid.uuid == p_target_uuid->uuid)
                            && (extracted_uuid.type == p_target_uuid->type))
                        {
                            return true;
                        }
                    }
                }
            }
    
            else if ( (field_type == BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE)
                    || (field_type == BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_COMPLETE)
                    )
            {
                for (uint32_t u_index = 0; u_index < (field_length/(size_t)UUID32_SIZE); u_index++)
                {
                    err_code = sd_ble_uuid_decode(m_adapter,(uint8_t)UUID16_SIZE, 
                    &p_data[u_index * UUID32_SIZE + index + 2], 
                    &extracted_uuid);
                    if (err_code == NRF_SUCCESS)
                    {
                        if ((extracted_uuid.uuid == p_target_uuid->uuid)
                            && (extracted_uuid.type == p_target_uuid->type))
                        {
                            return true;
                        }
                    }
                }
            }
            
            else if ( (field_type == BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE)
                    || (field_type == BLE_GAP_AD_TYPE_SERVICE_DATA_128BIT_UUID)
                    )
            {
                 printf("enter\n");
                 fflush(stdout);
                err_code = sd_ble_uuid_decode(m_adapter,(uint8_t)UUID128_SIZE, 
                                              &p_data[index + 2], 
                                              &extracted_uuid);
                if (err_code == NRF_SUCCESS)
                {
                     printf("sd_ble_uuid_decode success\n");
                     fflush(stdout);
                    if ((extracted_uuid.uuid == p_target_uuid->uuid)
                        && (extracted_uuid.type == p_target_uuid->type))
                    {
                        return true;
                    }
                    else
                    {
                         printf("extract uuid failure\n");
                         fflush(stdout);
                    }
                    
                }
            }
            index += field_length + 1;
        }
        return false;
        
    }
    /**@brief Function called on BLE_GAP_EVT_ADV_REPORT event.
     *
     * @details Create a connection if received advertising packet corresponds to desired BLE device.
     *
     * @param[in] p_ble_gap_evt Advertising Report Event.
     */
    static void on_adv_report(const ble_gap_evt_t * const p_ble_gap_evt,const ble_gap_evt_adv_report_t * p_adv_report)
    {
        uint32_t err_code;
        uint8_t  str[STRING_BUFFER_SIZE] = {0};
         if (is_uuid_present(&m_nus_uuid, p_adv_report))
                {
                   printf("uuid is present %d\n",is_uuid_present(&m_nus_uuid, p_adv_report) );
                   fflush(stdout);
                }
                else
                {
                   printf("uuid is not present %d\n",is_uuid_present(&m_nus_uuid, p_adv_report) );
                   fflush(stdout);
                }
        // Log the Bluetooth device address of advertisement packet received.
        ble_address_to_string_convert(p_ble_gap_evt->params.adv_report.peer_addr, str);
        printf("Received advertisement report with device address: 0x%s\n", str);
        fflush(stdout);
    
        if (find_adv_name(&p_ble_gap_evt->params.adv_report, TARGET_DEV_NAME))
        {
            if (m_connected_devices >= MAX_PEER_COUNT || m_connection_is_in_progress)
            {
                return;
            }
    
            err_code = sd_ble_gap_connect(m_adapter,
                                          &(p_ble_gap_evt->params.adv_report.peer_addr),
                                          &m_scan_param,
                                          &m_connection_param
    #if NRF_SD_BLE_API >= 5
                                         , m_config_id
    #endif
                                         );
            if (err_code != NRF_SUCCESS)
            {
                printf("Connection Request Failed, reason %d\n", err_code);
                fflush(stdout);
                return;
            }
    
            m_connection_is_in_progress = true;
        }
    #if NRF_SD_BLE_API >= 6
        else {
            err_code = sd_ble_gap_scan_start(m_adapter, NULL, &m_adv_report_buffer);
    
            if (err_code != NRF_SUCCESS)
            {
                printf("Scan start failed with error code: %d\n", err_code);
                fflush(stdout);
            }
            else
            {
                printf("\nScan started\n");
                fflush(stdout);
            }
        }
    #endif
    
    }
    
    /**@brief Function called on BLE_GAP_EVT_TIMEOUT event.
     *
     * @param[in] ble_gap_evt_t Timeout Event.
     */
    static void on_timeout(const ble_gap_evt_t * const p_ble_gap_evt)
    {
        if (p_ble_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
        {
            m_connection_is_in_progress = false;
        }
        else if (p_ble_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
        {
            scan_start();
        }
    }
    
    /**@brief Function called on BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event.
     *
     * @details Update service state and proceed to discovering the service's GATT characteristics.
     *
     * @param[in] p_ble_gattc_evt Primary Service Discovery Response Event.
     */
    static void on_service_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        int count;
        int service_index;
        const ble_gattc_service_t * service;
        printf("Received service discovery response\n");
        fflush(stdout);
    
        if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
        {
            printf("Service discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
            fflush(stdout);
            return;
        }
    
        count = p_ble_gattc_evt->params.prim_srvc_disc_rsp.count;
        printf("count %d\n", count);
        fflush(stdout);
    
        if (count == 0)
        {
            printf("Service not found\n");
            fflush(stdout);
            return;
        }
    
        if (count > 1)
        {
            printf("Warning, discovered multiple primary services. Ignoring all but the first\n");
        }
    
        service_index = 0; /* We expect to discover only the Valo service as requested. */
        service = &(p_ble_gattc_evt->params.prim_srvc_disc_rsp.services[service_index]);
        if (service->uuid.uuid != BLE_UUID_VALO_SERVICE)
        {
            printf("Unknown service discovered with UUID: 0x%016X\n", service->uuid.uuid);
            fflush(stdout);
            return;
        }
    
        m_service_start_handle  = service->handle_range.start_handle;
        m_service_end_handle    = service->handle_range.end_handle;
    
        printf("Discovered valo service. UUID: 0x%016X, "
                       "start handle: 0x%04X, end handle: 0x%04X\n",
            service->uuid.uuid, m_service_start_handle, m_service_end_handle);
        fflush(stdout);
    
        char_discovery_start();
    }
    
    /**@brief Function called on BLE_GATTC_EVT_CHAR_DISC_RSP event.
     *
     * @details Update characteristic state and proceed to discovering the characteristicss descriptors.
     *
     * @param[in] p_ble_gattc_evt Characteristic Discovery Response Event.
     */
    static void on_characteristic_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        int count = p_ble_gattc_evt->params.char_disc_rsp.count;
    
        if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
        {
            printf("Characteristic discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
            fflush(stdout);
            return;
        }
    
        printf("Received characteristic discovery response, characteristics count: %d\n", count);
        fflush(stdout);
    
        for (int i = 0; i < count; i++)
        {
            printf("Characteristic handle: 0x%04X, UUID: 0x%016X\n",
                   p_ble_gattc_evt->params.char_disc_rsp.chars[i].handle_decl,
                   p_ble_gattc_evt->params.char_disc_rsp.chars[i].uuid.uuid);
            fflush(stdout);
    
            if (p_ble_gattc_evt->params.char_disc_rsp.chars[i].uuid.uuid ==
                BLE_UUID_VALO_Mode1_CHAR)
            {
                m_hrm_char_handle = p_ble_gattc_evt->params.char_disc_rsp.chars[i].handle_decl;
            }
        }
    
        descr_discovery_start();
    }
    
    /**@brief Function called on BLE_GATTC_EVT_DESC_DISC_RSP event.
     *
     * @details Update CCCD descriptor state and proceed to prompting user to toggle notifications.
     *
     * @param[in] p_ble_gattc_evt Descriptor Discovery Response Event.
     */
    static void on_descriptor_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        int count = p_ble_gattc_evt->params.desc_disc_rsp.count;
    
        if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
        {
            printf("Descriptor discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
            fflush(stdout);
            return;
        }
    
        printf("Received descriptor discovery response, descriptor count: %d\n", count);
        fflush(stdout);
    
        for (int i = 0; i < count; i++)
        {
            printf("Descriptor handle: 0x%04X, UUID: 0x%04X\n",
                   p_ble_gattc_evt->params.desc_disc_rsp.descs[i].handle,
                   p_ble_gattc_evt->params.desc_disc_rsp.descs[i].uuid.uuid);
            fflush(stdout);
    
            if (p_ble_gattc_evt->params.desc_disc_rsp.descs[i].uuid.uuid == BLE_UUID_CCCD)
            {
                m_hrm_cccd_handle = p_ble_gattc_evt->params.desc_disc_rsp.descs[i].handle;
                printf("Press enter to toggle notifications on the HRM characteristic\n");
                fflush(stdout);
            }
        }
    }
    
    /**@brief Function called on BLE_GATTC_EVT_WRITE_RSP event.
     *
     * @param[in] p_ble_gattc_evt Write Response Event.
     */
    static void on_write_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        printf("Received write response.\n");
        fflush(stdout);
    
        if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
        {
            printf("Error. Write operation failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
            fflush(stdout);
        }
    }
    
    /**@brief Function called on BLE_GATTC_EVT_HVX event.
     *
     * @details Logs the received heart rate measurement.
     *
     * @param[in] p_ble_gattc_evt Handle Value Notification/Indication Event.
     */
    static void on_hvx(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        if (p_ble_gattc_evt->params.hvx.handle >= m_hrm_char_handle ||
                p_ble_gattc_evt->params.hvx.handle <= m_hrm_cccd_handle) // Heart rate measurement.
        {
            // We know the heart rate reading is encoded as 2 bytes [flag, value].
            printf("Received heart rate measurement: %d\n", p_ble_gattc_evt->params.hvx.data[1]);
        }
        else // Unknown data.
        {
            printf("Un-parsed data received on handle: %04X\n", p_ble_gattc_evt->params.hvx.handle);
        }
    
        fflush(stdout);
    }
    
    /**@brief Function called on BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST event.
     *
     * @details Update GAP connection parameters.
     *
     * @param[in] p_ble_gap_evt Connection Parameter Update Event.
     */
    static void on_conn_params_update_request(const ble_gap_evt_t * const p_ble_gap_evt)
    {
        uint32_t err_code = sd_ble_gap_conn_param_update(m_adapter, m_connection_handle,
                                                &(p_ble_gap_evt->
                                                        params.conn_param_update_request.conn_params));
        if (err_code != NRF_SUCCESS)
        {
            printf("Conn params update failed, err_code %d\n", err_code);
            fflush(stdout);
        }
    }
    
    #if NRF_SD_BLE_API >= 3
    /**@brief Function called on BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST event.
     *
     * @details Replies to an ATT_MTU exchange request by sending an Exchange MTU Response to the client.
     *
     * @param[in] p_ble_gatts_evt Exchange MTU Request Event.
     */
    static void on_exchange_mtu_request(const ble_gatts_evt_t * const p_ble_gatts_evt)
    {
        uint32_t err_code = sd_ble_gatts_exchange_mtu_reply(
            m_adapter,
            m_connection_handle,
    #if NRF_SD_BLE_API < 5
            GATT_MTU_SIZE_DEFAULT);
    #else
            BLE_GATT_ATT_MTU_DEFAULT);
    #endif
    
        if (err_code != NRF_SUCCESS)
        {
            printf("MTU exchange request reply failed, err_code %d\n", err_code);
            fflush(stdout);
        }
    }
    
    /**@brief Function called on BLE_GATTC_EVT_EXCHANGE_MTU_RSP event.
     *
     * @details Logs the new BLE server RX MTU size.
     *
     * @param[in] p_ble_gattc_evt Exchange MTU Response Event.
     */
    static void on_exchange_mtu_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
    {
        uint16_t server_rx_mtu = p_ble_gattc_evt->params.exchange_mtu_rsp.server_rx_mtu;
    
        printf("MTU response received. New ATT_MTU is %d\n", server_rx_mtu);
        fflush(stdout);
    }
    #endif
    
    
    
    /** Event dispatcher */
    
    /**@brief Function for handling the Application's BLE Stack events.
     *
     * @param[in] adapter The transport adapter.
     * @param[in] p_ble_evt Bluetooth stack event.
     */
    static void ble_evt_dispatch(adapter_t * adapter, ble_evt_t * p_ble_evt)
    {
        const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;
        if (p_ble_evt == NULL)
        {
            printf("Received an empty BLE event\n");
            fflush(stdout);
            return;
        }
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                on_connected(&(p_ble_evt->evt.gap_evt));
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                printf("Disconnected, reason: 0x%02X\n",
                       p_ble_evt->evt.gap_evt.params.disconnected.reason);
                fflush(stdout);
                m_connected_devices--;
                m_connection_handle = 0;
                break;
    
            case BLE_GAP_EVT_ADV_REPORT:
                on_adv_report(&(p_ble_evt->evt.gap_evt),&p_gap_evt->params.adv_report);
                break;
    
            case BLE_GAP_EVT_TIMEOUT:
                on_timeout(&(p_ble_evt->evt.gap_evt));
                break;
    
            case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
                on_service_discovery_response(&(p_ble_evt->evt.gattc_evt));
                break;
    
            case BLE_GATTC_EVT_CHAR_DISC_RSP:
                on_characteristic_discovery_response(&(p_ble_evt->evt.gattc_evt));
                break;
    
            case BLE_GATTC_EVT_DESC_DISC_RSP:
                on_descriptor_discovery_response(&(p_ble_evt->evt.gattc_evt));
                break;
    
            case BLE_GATTC_EVT_WRITE_RSP:
                on_write_response(&(p_ble_evt->evt.gattc_evt));
                break;
    
            case BLE_GATTC_EVT_HVX:
                on_hvx(&(p_ble_evt->evt.gattc_evt));
                break;
    
            case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
                on_conn_params_update_request(&(p_ble_evt->evt.gap_evt));
                break;
    
        #if NRF_SD_BLE_API >= 3
            case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
                on_exchange_mtu_request(&(p_ble_evt->evt.gatts_evt));
                break;
    
            case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
                on_exchange_mtu_response(&(p_ble_evt->evt.gattc_evt));
                break;
        #endif
    
            default:
                printf("Received an un-handled event with ID: %d\n", p_ble_evt->header.evt_id);
                fflush(stdout);
                break;
        }
    }
    
    
    /** Main */
    
    /**@brief Function for application main entry.
     *
     * @param[in] argc Number of arguments (program expects 0 or 1 arguments).
     * @param[in] argv The serial port of the target nRF5 device (Optional).
     */
    int main(int argc, char * argv[])
    {
        uint32_t error_code;
        char *   serial_port = DEFAULT_UART_PORT_NAME;
        uint32_t baud_rate = DEFAULT_BAUD_RATE;
        uint8_t  cccd_value = 0;
    
        if (argc > 1)
        {
            serial_port = argv[1];
        }
    
        printf("Serial port used: %s\n", serial_port);
        printf("Baud rate used: %d\n", baud_rate);
        fflush(stdout);
    
        m_adapter =  adapter_init(serial_port, baud_rate);
        sd_rpc_log_handler_severity_filter_set(m_adapter, SD_RPC_LOG_INFO);
        error_code = sd_rpc_open(m_adapter, status_handler, ble_evt_dispatch, log_handler);
    
        if (error_code != NRF_SUCCESS)
        {
            printf("Failed to open nRF BLE Driver. Error code: 0x%02X\n", error_code);
            fflush(stdout);
            return error_code;
        }
    
    #if NRF_SD_BLE_API >= 5
        ble_cfg_set(m_config_id);
    #endif
    
        error_code = ble_stack_init();
    
        if (error_code != NRF_SUCCESS)
        {
            return error_code;
        }
    
    #if NRF_SD_BLE_API < 5
        error_code = ble_options_set();
    
        if (error_code != NRF_SUCCESS)
        {
            return error_code;
        }
    #endif
    
        error_code = scan_start();
    
        if (error_code != NRF_SUCCESS)
        {
            return error_code;
        }
    
        // Endlessly loop.
        for (;;)
        {
            char c = (char)getchar();
            if (c == 'q' || c == 'Q')
            {
                error_code = sd_rpc_close(m_adapter);
    
                if (error_code != NRF_SUCCESS)
                {
                    printf("Failed to close nRF BLE Driver. Error code: 0x%02X\n", error_code);
                    fflush(stdout);
                    return error_code;
                }
    
                printf("Closed\n");
                fflush(stdout);
    
                return NRF_SUCCESS;
            }
    
            // Toggle notifications on the HRM characteristic every time user input is received.
            cccd_value ^= BLE_CCCD_NOTIFY;
            hrm_cccd_set(cccd_value);
        }
    }
    

  • Please check the complete code and the complete logs are same as posted in my earlier post ,still posting again

Related