Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

DFU_Problem

Hi,

I am using  Nrf Sdk Mesh V3.1.0 with softdeviceS132 and sdk board Nrf52832

I am trying to implement Normal DFU in Mesh network.I don't need Mesh DFU.

So i have added all the DFU related files and instances in LIGHT_SWITCH_SERVER example code.So when i am trying to connect using Nrf_Connect app i'm getting this error

  0> .<t:     227079>, app_error_weak.c,  119, Mesh error 8 at 0x00000000 (:0).it mesns Invalid state, operation disallowed in this state.

Here i have also checked all functions were initilized properly or not,but it seems there were initilized properly by checking in RTT_Viewer

  0> .<t:      20098>, main.c,  753, All_func init  end
 0> .<t:          0>, main.c,  347, ----- BLE Mesh Light Switch Server Demo -----
 0> <t:       8627>, nrf_sdh_ble.c,  239, RAM starts at 0x20003A38....
 0> <t:       9187>, main.c,  263, Initializing and adding models
 0> <t:       9190>, main.c,  158, App OnOff Model Handle: 2
 0> <t:      14068>, mesh_app_utils.c,   65, Device UUID (raw): BEE80A4EECAE014F8887D6CAAE5117C6
 0> <t:      14071>, mesh_app_utils.c,   70, Device UUID : 4E0AE8BE-AEEC-4F01-8887-D6CAAE5117C6
 0> <t:      14084>, main.c,  647, services_init
 0> .<t:      14086>, main.c,  657, nrf_ble_qwr_init
 0> .<t:      14091>, main.c,  672, ble_dfu_buttonless_init
 0> .<t:      14094>, main.c,  753, All_func init  end

i am attaching my main.c  file

So please check weather added DFU instances  are correct or not?

please help me to sort out this Mesh error 8 problem and help me to do Normal DFU process in Mesh Network.

Main.c


#include <stdint.h>
#include <string.h>

/* HAL */
#include "boards.h"
#include "simple_hal.h"
#include "app_timer.h"

/* Core */
#include "nrf_mesh_config_core.h"
#include "nrf_mesh_gatt.h"
#include "nrf_mesh_configure.h"
#include "nrf_mesh.h"
#include "mesh_stack.h"
#include "device_state_manager.h"
#include "access_config.h"
#include "proxy.h"

/* Provisioning and configuration */
#include "mesh_provisionee.h"
#include "mesh_app_utils.h"

/* Models */
#include "generic_onoff_server.h"

/* Logging and RTT */
#include "log.h"
#include "rtt_input.h"

/* Example specific includes */
#include "app_config.h"
#include "example_common.h"
#include "nrf_mesh_config_examples.h"
#include "light_switch_example_common.h"
#include "app_onoff.h"
#include "ble_softdevice_support.h"
#include "mesh_adv.h"


#include "nrf_bootloader_info.h"
#include "nrf_ble_qwr.h"
#include "ble_dfu.h"
#include "nrf_sdh.h"
#include "nrf_dfu_ble_svci_bond_sharing.h"
#include "nrf_svci_async_function.h"
#include "nrf_svci_async_handler.h"
#include "peer_manager.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_power.h"
#include "fds.h"
#include "nrf_dfu_types.h"
#include "ble_conn_state.h"
#include "ble.h"
#include "nrf_sdh_ble.h"
#include "nrf_ble_gatt.h"


#define ONOFF_SERVER_0_LED          (BSP_LED_0)
#define APP_ONOFF_ELEMENT_INDEX     (0)

static bool m_device_provisioned;


#define OPCODE_LENGTH        1
#define HANDLE_LENGTH        2
#define APP_BLE_OBSERVER_PRIO           3                                           /**< Application's BLE observer priority. You shouldn't need to modify this value. */
#define APP_BLE_CONN_CFG_TAG            1                                           /**< A tag identifying the SoftDevice BLE configuration. */
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                            /**< Handle of the current connection. */                                   /**< Forward declaration of advertising start function */
static uint16_t   m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3;            /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */

NRF_BLE_QWR_DEF(m_qwr);                                                             /**< Context for the Queued Write module.*/
APP_TIMER_DEF(Gp_timer_id);	                                                    /*timer Instance for display time Inc.*/
ble_gap_addr_t mac;                                                                 /*mac_id instance*/
NRF_BLE_GATT_DEF(m_gatt);                                                           /**< GATT module instance. */


#define SEC_PARAM_BOND                  1                                           /**< Perform bonding. */
#define SEC_PARAM_MITM                  0                                           /**< Man In The Middle protection not required. */
#define SEC_PARAM_LESC                  0                                           /**< LE Secure Connections not enabled. */
#define SEC_PARAM_KEYPRESS              0                                           /**< Keypress notifications not enabled. */
#define SEC_PARAM_IO_CAPABILITIES       BLE_GAP_IO_CAPS_NONE                        /**< No I/O capabilities. */
#define SEC_PARAM_OOB                   0                                           /**< Out Of Band data not available. */
#define SEC_PARAM_MIN_KEY_SIZE          7                                           /**< Minimum encryption key size. */
#define SEC_PARAM_MAX_KEY_SIZE          16                                          /**< Maximum encryption key size. */
                                          
uint8_t NUS_array[50] = {0};          /*array for storing recveived data from message handle.*/

/*************************************************************************************************/
static void app_onoff_server_set_cb(const app_onoff_server_t * p_server, bool onoff);
static void app_onoff_server_get_cb(const app_onoff_server_t * p_server, bool * p_present_onoff);

/* Generic OnOff server structure definition and initialization */
APP_ONOFF_SERVER_DEF(m_onoff_server_0,
                     APP_CONFIG_FORCE_SEGMENTATION,
                     APP_CONFIG_MIC_SIZE,
                     app_onoff_server_set_cb,
                     app_onoff_server_get_cb)

/* Callback for updating the hardware state */
static void app_onoff_server_set_cb(const app_onoff_server_t * p_server, bool onoff)
{
    /* Resolve the server instance here if required, this example uses only 1 instance. */

    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Setting GPIO value: %d\n", onoff)

    hal_led_pin_set(ONOFF_SERVER_0_LED, onoff);
}

/* Callback for reading the hardware state */
static void app_onoff_server_get_cb(const app_onoff_server_t * p_server, bool * p_present_onoff)
{
    /* Resolve the server instance here if required, this example uses only 1 instance. */

    *p_present_onoff = hal_led_pin_get(ONOFF_SERVER_0_LED);
}

static void app_model_init(void)
{
    /* Instantiate onoff server on element index APP_ONOFF_ELEMENT_INDEX */
    ERROR_CHECK(app_onoff_init(&m_onoff_server_0, APP_ONOFF_ELEMENT_INDEX));
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "App OnOff Model Handle: %d\n", m_onoff_server_0.server.model_handle);
}

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

static void node_reset(void)
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "----- Node reset  -----\n");
    hal_led_blink_ms(LEDS_MASK, LED_BLINK_INTERVAL_MS, LED_BLINK_CNT_RESET);
    /* This function may return if there are ongoing flash operations. */
    mesh_stack_device_reset();
}

static void config_server_evt_cb(const config_server_evt_t * p_evt)
{
    if (p_evt->type == CONFIG_SERVER_EVT_NODE_RESET)
    {
        node_reset();
    }
}

static void button_event_handler(uint32_t button_number)
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Button %u pressed\n", button_number);
    switch (button_number)
    {
        /* Pressing SW1 on the Development Kit will result in LED state to toggle and trigger
        the STATUS message to inform client about the state change. This is a demonstration of
        state change publication due to local event. */
        case 0:
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "User action \n");
            hal_led_pin_set(ONOFF_SERVER_0_LED, !hal_led_pin_get(ONOFF_SERVER_0_LED));
            app_onoff_status_publish(&m_onoff_server_0);
            break;
        }

        /* Initiate node reset */
        case 3:
        {
            /* Clear all the states to reset the node. */
            if (mesh_stack_is_device_provisioned())
            {
#if MESH_FEATURE_GATT_PROXY_ENABLED
                (void) proxy_stop();
#endif
                mesh_stack_config_clear();
                node_reset();
            }
            else
            {
                __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "The device is unprovisioned. Resetting has no effect.\n");
            }
            break;
        }

        default:
            break;
    }
}

static void app_rtt_input_handler(int key)
{
    if (key >= '0' && key <= '4')
    {
        uint32_t button_number = key - '0';
        button_event_handler(button_number);
    }
}

static void device_identification_start_cb(uint8_t attention_duration_s)
{
    hal_led_mask_set(LEDS_MASK, false);
    hal_led_blink_ms(BSP_LED_2_MASK  | BSP_LED_3_MASK,
                     LED_BLINK_ATTENTION_INTERVAL_MS,
                     LED_BLINK_ATTENTION_COUNT(attention_duration_s));
}

static void provisioning_aborted_cb(void)
{
    hal_led_blink_stop();
}

static void provisioning_complete_cb(void)
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Successfully provisioned\n");

#if MESH_FEATURE_GATT_ENABLED
    /* Restores the application parameters after switching from the Provisioning
     * service to the Proxy  */
    gap_params_init();
    conn_params_init();
#endif

    dsm_local_unicast_address_t node_address;
    dsm_local_unicast_addresses_get(&node_address);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Node Address: 0x%04x \n", node_address.address_start);

    hal_led_blink_stop();
    hal_led_mask_set(LEDS_MASK, LED_MASK_STATE_OFF);
    hal_led_blink_ms(LEDS_MASK, LED_BLINK_INTERVAL_MS, LED_BLINK_CNT_PROV);
}

static void models_init_cb(void)
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Initializing and adding models\n");
    app_model_init();
}

static void mesh_init(void)
{
    mesh_stack_init_params_t init_params =
    {
        .core.irq_priority       = NRF_MESH_IRQ_PRIORITY_LOWEST,
        .core.lfclksrc           = DEV_BOARD_LF_CLK_CFG,
        .core.p_uuid             = NULL,
        .models.models_init_cb   = models_init_cb,
        .models.config_server_cb = config_server_evt_cb
    };
    ERROR_CHECK(mesh_stack_init(&init_params, &m_device_provisioned));
}



/**********************************************************
 *@brief Function for handling BLE events.
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 **********************************************************/
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
	uint32_t err_code = NRF_SUCCESS;

	switch (p_ble_evt->header.evt_id)
	{
          case BLE_GAP_EVT_DISCONNECTED:
                          // LED indication will be changed when advertising starts.
//                 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"BLE_GAP_EVT_DISCONNECTED\r\n");
                 break;

          case BLE_GAP_EVT_CONNECTED:
//                  err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
//                  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"BLE_GAP_EVT_CONNECTED\r\n");
                  APP_ERROR_CHECK(err_code);
                  m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                  err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                  APP_ERROR_CHECK(err_code);
                  break;

          case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
          {
//                  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"PHY update request.");
                ble_gap_phys_t const phys =
                {
                                .rx_phys = BLE_GAP_PHY_AUTO,
                                .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);
          } break;

          case BLE_GATTC_EVT_TIMEOUT:
              // Disconnect on GATT Client timeout event.
//              __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"GATT Client Timeout.");
              err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                                                                                                               BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
              APP_ERROR_CHECK(err_code);
              break;

          case BLE_GATTS_EVT_TIMEOUT:
              // Disconnect on GATT Server timeout event.
//              __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"GATT Server Timeout.");
              err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                                                                                                                               BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
              APP_ERROR_CHECK(err_code);
              break;

          default:
              // No implementation needed.
              break;
	}
}




static void initialize(void)
{
    __LOG_INIT(LOG_SRC_APP | LOG_SRC_ACCESS | LOG_SRC_BEARER, LOG_LEVEL_INFO, LOG_CALLBACK_DEFAULT);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "----- BLE Mesh Light Switch Server Demo -----\n");

    ERROR_CHECK(app_timer_init());
    hal_leds_init();

#if BUTTON_BOARD
    ERROR_CHECK(hal_buttons_init(button_event_handler));
#endif

    ble_stack_init();
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);

#if MESH_FEATURE_GATT_ENABLED
    gap_params_init();
    conn_params_init();
#endif

    mesh_init();
}

static void start(void)
{
    rtt_input_enable(app_rtt_input_handler, RTT_INPUT_POLL_PERIOD_MS);

    if (!m_device_provisioned)
    {
        static const uint8_t static_auth_data[NRF_MESH_KEY_SIZE] = STATIC_AUTH_DATA;
        mesh_provisionee_start_params_t prov_start_params =
        {
            .p_static_data    = static_auth_data,
            .prov_complete_cb = provisioning_complete_cb,
            .prov_device_identification_start_cb = device_identification_start_cb,
            .prov_device_identification_stop_cb = NULL,
            .prov_abort_cb = provisioning_aborted_cb,
            .p_device_uri = EX_URI_LS_SERVER
        };
        ERROR_CHECK(mesh_provisionee_prov_start(&prov_start_params));
    }

    mesh_app_uuid_print(nrf_mesh_configure_device_uuid_get());

    ERROR_CHECK(mesh_stack_start());

    hal_led_mask_set(LEDS_MASK, LED_MASK_STATE_OFF);
    hal_led_blink_ms(LEDS_MASK, LED_BLINK_INTERVAL_MS, LED_BLINK_CNT_START);
}







/*********************************************************************************************
 *@brief Handler for shutdown preparation.
 * @details During shutdown procedures, this function will be called at a 1 second interval
 *          untill the function returns true. When the function returns true, it means that the
 *          app is ready to reset to DFU mode.
 * @param[in]   event   Power manager event.
 * @retval  True if shutdown is allowed by this power manager handler, otherwise false.
 **********************************************************************************************/
static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
{
    switch (event)
    {
        case NRF_PWR_MGMT_EVT_PREPARE_DFU:
//             __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Power management wants to reset to DFU mode.");
  
                app_timer_stop_all();

            break;

        default:
           
            return true;
    }

//     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Power management allowed to reset to DFU mode.");
    return true;
}

/****************************************************************
 *@brief Register application shutdown handler with priority 0.
 ****************************************************************/
NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);

/****************************************************************
 *@brief Register application shutdown handler with priority 0.
 ****************************************************************/

static void buttonless_dfu_sdh_state_observer(nrf_sdh_state_evt_t state, void * p_context)
{
    if (state == NRF_SDH_EVT_STATE_DISABLED)
    {
        // Softdevice was disabled before going into reset. Inform bootloader to skip CRC on next boot.
        nrf_power_gpregret2_set(BOOTLOADER_DFU_SKIP_CRC);

        //Go to system off.
        nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
    }
}

/* nrf_sdh state observer. */
NRF_SDH_STATE_OBSERVER(m_buttonless_dfu_state_obs, 0) =
{
    .handler = buttonless_dfu_sdh_state_observer,
};

/************************************************************************************
 * @brief Function for handling dfu events from the Buttonless Secure DFU service
 * YOUR_JOB: Update this code if you want to do anything given a DFU event (optional).
 * @param[in]   event   Event from the Buttonless Secure DFU service.
 ***********************************************************************************/
static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event)
{
    switch (event)
    {
        case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE:
//            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Device is preparing to enter bootloader mode.");
            // YOUR_JOB: Disconnect all bonded devices that currently are connected.
            //           This is required to receive a service changed indication
            //           on bootup after a successful (or aborted) Device Firmware Update.
            break;

        case BLE_DFU_EVT_BOOTLOADER_ENTER:
            // YOUR_JOB: Write app-specific unwritten data to FLASH, control finalization of this
            //           by delaying reset by reporting false in app_shutdown_handler
//            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Device will enter bootloader mode.");
            break;

        case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED:
//             __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Request to enter bootloader mode failed asynchroneously.");
            // YOUR_JOB: Take corrective measures to resolve the issue
            //           like calling APP_ERROR_CHECK to reset the device.
            break;

        case BLE_DFU_EVT_RESPONSE_SEND_ERROR:
//            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Request to send a response to client failed.");
            // YOUR_JOB: Take corrective measures to resolve the issue
            //           like calling APP_ERROR_CHECK to reset the device.
            APP_ERROR_CHECK(false);
            break;

        default:
//             __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Unknown event from ble_dfu_buttonless.");
            break;
    }
}

/********************************************************
 * @brief Function for handling Peer Manager events.
 * @param[in] p_evt  Peer Manager event.
 ********************************************************/
static void pm_evt_handler(pm_evt_t const * p_evt)
{
	ret_code_t err_code;

	switch (p_evt->evt_id)
	{
            case PM_EVT_BONDED_PEER_CONNECTED:
            {
//               __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Connected to a previously bonded device.");
            } break;

            case PM_EVT_CONN_SEC_SUCCEEDED:
            {
//               __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"Connection secured: role: %d, conn_handle: 0x%x, procedure: %d.",
//                                                               ble_conn_state_role(p_evt->conn_handle),
//                                                               p_evt->conn_handle,
//                                                               p_evt->params.conn_sec_succeeded.procedure);
            } break;

            case PM_EVT_CONN_SEC_FAILED:
            {
                /* Often, when securing fails, it shouldn't be restarted, for security reasons.
                 * Other times, it can be restarted directly.
                 * Sometimes it can be restarted, but only after changing some Security Parameters.
                 * Sometimes, it cannot be restarted until the link is disconnected and reconnected.
                 * Sometimes it is impossible, to secure the link, or the peer device does not support it.
                 * How to handle this error is highly application dependent. */
            } break;

            case PM_EVT_CONN_SEC_CONFIG_REQ:
            {
                // Reject pairing request from an already bonded peer.
                pm_conn_sec_config_t conn_sec_config = {.allow_repairing = false};
                pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config);
            } break;

            case PM_EVT_STORAGE_FULL:
            {
                // Run garbage collection on the flash.
                err_code = fds_gc();
                if (err_code == FDS_ERR_NO_SPACE_IN_QUEUES)
                {
                                // Retry.
                }
                else
                {
                                APP_ERROR_CHECK(err_code);
                }
            } break;

            case PM_EVT_PEERS_DELETE_SUCCEEDED:
            {
//				advertising_start();
            } break;

            case PM_EVT_PEER_DATA_UPDATE_FAILED:
            {
                // Assert.
                APP_ERROR_CHECK(p_evt->params.peer_data_update_failed.error);
            } break;

            case PM_EVT_PEER_DELETE_FAILED:
            {
              // Assert.
              APP_ERROR_CHECK(p_evt->params.peer_delete_failed.error);
            } break;

            case PM_EVT_PEERS_DELETE_FAILED:
            {
                // Assert.
                APP_ERROR_CHECK(p_evt->params.peers_delete_failed_evt.error);
            } break;

            case PM_EVT_ERROR_UNEXPECTED:
            {
              // Assert.
              APP_ERROR_CHECK(p_evt->params.error_unexpected.error);
            } break;

            case PM_EVT_CONN_SEC_START:
            case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
            case PM_EVT_PEER_DELETE_SUCCEEDED:
            case PM_EVT_LOCAL_DB_CACHE_APPLIED:
            case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED:
                            // This can happen when the local DB has changed.
            case PM_EVT_SERVICE_CHANGED_IND_SENT:
            case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
            default:
                            break;
	}
}

/***************************************************************************************************
 * @brief Function for handling Queued Write Module errors.
 * @details A pointer to this function will be passed to each service which may need to inform the
 *          application about an error.
 * @param[in]   nrf_error   Error code containing information about what went wrong.
 **************************************************************************************************/
static void nrf_qwr_error_handler(uint32_t nrf_error)
{
//     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"nrf_qwr_error_handler.");
    APP_ERROR_HANDLER(nrf_error);
   

}


/*********************************************************
 *@brief Function for the Peer Manager initialization.
 *********************************************************/
static void peer_manager_init()
{
    ble_gap_sec_params_t sec_param;
    ret_code_t           err_code;

    err_code = pm_init();
    APP_ERROR_CHECK(err_code);

    memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));

    // Security parameters to be used for all security procedures.
    sec_param.bond           = SEC_PARAM_BOND;
    sec_param.mitm           = SEC_PARAM_MITM;
    sec_param.lesc           = SEC_PARAM_LESC;
    sec_param.keypress       = SEC_PARAM_KEYPRESS;
    sec_param.io_caps        = SEC_PARAM_IO_CAPABILITIES;
    sec_param.oob            = SEC_PARAM_OOB;
    sec_param.min_key_size   = SEC_PARAM_MIN_KEY_SIZE;
    sec_param.max_key_size   = SEC_PARAM_MAX_KEY_SIZE;
    sec_param.kdist_own.enc  = 1;
    sec_param.kdist_own.id   = 1;
    sec_param.kdist_peer.enc = 1;
    sec_param.kdist_peer.id  = 1;

    err_code = pm_sec_params_set(&sec_param);
    APP_ERROR_CHECK(err_code);

    err_code = pm_register(pm_evt_handler);
    APP_ERROR_CHECK(err_code);
}


/********************************************************************************
 *@brief Function for initializing services that will be used by the application.
 ********************************************************************************/
static void services_init(void)
{
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"services_init\n.");
    uint32_t                  err_code;
    nrf_ble_qwr_init_t        qwr_init  = {0};
    ble_dfu_buttonless_init_t dfus_init = {0};

    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
    APP_ERROR_CHECK(err_code);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"nrf_ble_qwr_init\n.");


    // Initialize the async SVCI interface to bootloader.
    err_code = ble_dfu_buttonless_async_svci_init();
    APP_ERROR_CHECK(err_code);
//    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"ble_dfu_buttonless_async_svci_init\n.");

    dfus_init.evt_handler = ble_dfu_evt_handler;
//    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"ble_dfu_evt_handler\n.");


    err_code = ble_dfu_buttonless_init(&dfus_init);
//   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"ble_dfu_buttonless_init %d\n.",err_code);
    APP_ERROR_CHECK(err_code);
     __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"ble_dfu_buttonless_init \n.");
		
}


/************************************************************
** @brief Handler for app timer (@Gp_timer_handler) events.
*************************************************************/
void Gp_timer_handler(void * p_context)
{
          mesh_adv_stop();
          mesh_adv_data_set(0x0d99, NUS_array, 20);
          mesh_adv_start(); 
}

uint8_t AF_TimerCreat(void)
{
    uint32_t err_code;
    /*Create  New Timer to Handle Feeding Time and Relay Functionality */
    err_code = app_timer_create(&Gp_timer_id,APP_TIMER_MODE_REPEATED,Gp_timer_handler);
    APP_ERROR_CHECK(err_code);
    /*Start GeneralPourpose Timer*/
    err_code = app_timer_start(Gp_timer_id, APP_TIMER_TICKS(250), NULL);
    APP_ERROR_CHECK(err_code);
  return 0;
}


/****************************************************************
 * @brief Function for handling events from the GATT library.
 ****************************************************************/
void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
{
    if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
    {
        m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
//        NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
    }
}

/*******************************************************
 *@brief Function for initializing the GATT library. 
 *******************************************************/
void gatt_init(void)
{
    ret_code_t err_code;

    err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
    APP_ERROR_CHECK(err_code);
}





int main(void)
{
    initialize();
    start();
    peer_manager_init();
    services_init();
    gatt_init();
    AF_TimerCreat();

     /*Read BLE Mac Address.*/
    sd_ble_gap_addr_get(&mac);

    NUS_array[0] = 5;
    NUS_array[1] = mac.addr[5];
    NUS_array[2] = mac.addr[4];
    NUS_array[3] = mac.addr[3];
    NUS_array[4] = mac.addr[2];
    NUS_array[5] = mac.addr[1];
    NUS_array[6] = mac.addr[0];

    NUS_array[7]  = 0;                       
    NUS_array[8]  = 13;
    NUS_array[9]  = 0xaa;
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,"All_func init  end\n.");


    for (;;)
    {
       
    }
}

Related