This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52810 HID Mouse enter error code, the device will restart when running mouse_movement_send

void m_mouse_movement_send(int16_t x_delta, int16_t y_delta)
{
    ret_code_t err_code;

    if (m_in_boot_mode)
    {
        x_delta = MIN(x_delta, 0x00ff);
        y_delta = MIN(y_delta, 0x00ff);

        err_code = ble_hids_boot_mouse_inp_rep_send(&m_hids,
                                                    0x00,
                                                    (int8_t)x_delta,
                                                    (int8_t)y_delta,
                                                    0,
                                                    NULL);
    }
    else
    {
        uint8_t buffer[INPUT_REP_MOVEMENT_LEN];

        APP_ERROR_CHECK_BOOL(INPUT_REP_MOVEMENT_LEN == 3);

        x_delta = MIN(x_delta, 0x0fff);
        y_delta = MIN(y_delta, 0x0fff);

        buffer[0] = x_delta & 0x00ff;
        buffer[1] = ((y_delta & 0x000f) << 4) | ((x_delta & 0x0f00) >> 8);
        buffer[2] = (y_delta & 0x0ff0) >> 4;

        err_code = ble_hids_inp_rep_send(&m_hids,
                                         INPUT_REP_MOVEMENT_INDEX,
                                         INPUT_REP_MOVEMENT_LEN,
                                         buffer);
    }

    if ((err_code != NRF_SUCCESS) &&
        (err_code != NRF_ERROR_INVALID_STATE) &&
        (err_code != NRF_ERROR_RESOURCES) &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
       )
    {
        APP_ERROR_HANDLER(err_code);
    }
}

I created a 500ms timer to send mouse movement as follows.

m_mouse_movement_send(5,0); 

When the device runs there, the device will restart. The error code is NRF_ERROR_FORBIDDEN.

    if ((err_code != NRF_SUCCESS) &&
        (err_code != NRF_ERROR_INVALID_STATE) &&
        (err_code != NRF_ERROR_RESOURCES) &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
       )
    {
        APP_ERROR_HANDLER(err_code);
    }

When i add the NRF_ERROR_FORBIDDEN handle, the device runs normally.

    if ((err_code != NRF_SUCCESS) &&
        (err_code != NRF_ERROR_BUSY) &&
        (err_code != NRF_ERROR_RESOURCES) &&
        (err_code != NRF_ERROR_FORBIDDEN) &&
        (err_code != NRF_ERROR_INVALID_STATE) &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
       )
    {
        APP_ERROR_HANDLER(err_code);
    }

By the way, the hid_init is behind th services_init.

  • Hello Taylor,

    When the device runs there, the device will restart. The error code is NRF_ERROR_FORBIDDEN.

     This is returned by the sd_ble_gatts_hvx function. An exempt from the functions API reads:

    NRF_ERROR_FORBIDDEN The connection's current security level is lower than the one required by the write permissions of the CCCD associated with this characteristic.

    Which security level / configuration are you using for you connection?
    Are you basing your application on the BLE HIDS mouse example from the SDK?

    When i add the NRF_ERROR_FORBIDDEN handle, the device runs normally.

    When you say that it runs normally, do you mean that you are achieving the sought after functionality - moving the mouse?
    Or, do you mean that it does not crash, but also not update the mouse position?

    I would assume it is the latter.
    Usually, it is not advisable to just mask out error messages and look the other way like you are doing above - this is a viable solution when you know that the issue is caused by a specific transient state that you also know will end after a certain time, and that normal operation will resume shortly thereafter without having caused any long-term issues for your operation/application.

    Best regards,
    Karl

  • Hi Karl,

    My project is based on nRF52_SDK_14.2.0, of course I edited some codes.Where can I find the position of setting the CCCD?

    When you say that it runs normally, do you mean that you are achieving the sought after functionality - moving the mouse?
    Or, do you mean that it does not crash, but also not update the mouse position?

    Actually, when I add the error of NRF_ERROR_FORBIDDEN handle, the mouse will move.

    This is the main code.

    #define APP_BLE_CONN_CFG_TAG    1											/**< A tag identifying the SoftDevice BLE configuration. */
    #define APP_FEATURE_NOT_SUPPORTED       BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2    /**< Reply when unsupported features are requested. */
    
    #define DEVICE_NAME                     BLE_ADV_NAME                            /**< Name of device. Will be included in the advertising data. */
    #define APP_BLE_OBSERVER_PRIO   1											/**< Application's BLE observer priority. You shouldn't need to modify this value. */
    
    #define APP_ADV_INTERVAL                1600									/**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
    #define APP_FAC_ADV_INTERVAL            800//64 								/**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
    #define APP_ADV_FAST_INTERVAL           800                                      /**< Fast advertising interval (in units of 0.625 ms. This value corresponds to 25 ms.). */
    #define APP_ADV_FAST_TIMEOUT            90                                          /**< The duration of the fast advertising period (in seconds). */
    
    
    
    
    #define MIN_CONN_INTERVAL       MSEC_TO_UNITS(20, UNIT_1_25_MS) 			/**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
    #define MAX_CONN_INTERVAL       MSEC_TO_UNITS(75, UNIT_1_25_MS) 			/**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
    #define SLAVE_LATENCY           0											/**< Slave latency. */
    #define CONN_SUP_TIMEOUT        MSEC_TO_UNITS(4000, UNIT_10_MS) 			/**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
    #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000)					   /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
    #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000)					  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
    #define MAX_CONN_PARAMS_UPDATE_COUNT 3											 /**< Number of attempts before giving up the connection parameter negotiation. */
    
    
    #define DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
    
    
    #define COMPANY_IDENTIFIER      0x0000
    #define BLE_UUID_APP_SERVICE    0xAA00
    #define BLE_DFU_SERVICE_UUID    0xFE59
    uint8_t m_addl_adv_manuf_data[BLE_GAP_ADDR_LEN];
    #if 1
    static ble_uuid_t m_adv_uuids[] =
    {
        {BLE_UUID_APP_SERVICE, BLE_UUID_TYPE_BLE},
        {BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE, BLE_UUID_TYPE_BLE},
        {BLE_DFU_SERVICE_UUID, BLE_UUID_TYPE_BLE}    
    };   
    #else
    static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifiers. */
    {
        {
            BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE
        }
    };
    #endif
    NRF_BLE_GATT_DEF(m_gatt);                                                       /**< GATT module instance. */
    BLE_ADVERTISING_DEF(m_advertising);                                             /**< Advertising module instance. */
    
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                        /**< Handle of the current connection. */
    
    /* YOUR_JOB: Declare all services structure your application is using
     *  BLE_XYZ_DEF(m_xyz);
     */
    
    // YOUR_JOB: Use UUIDs for service(s) used in your application.
    
    
    static void advertising_start(bool erase_bonds);
    
    
    /**@brief Callback function for asserts in the SoftDevice.
     *
     * @details This function will be called in case of an assert in the SoftDevice.
     *
     * @warning This handler is an example only and does not fit a final product. You need to analyze
     *          how your product is supposed to react in case of Assert.
     * @warning On assert from the SoftDevice, the system can only recover on reset.
     *
     * @param[in] line_num   Line number of the failing ASSERT call.
     * @param[in] file_name  File name of the failing ASSERT call.
     */
    void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
    {
        app_error_handler(DEAD_BEEF, line_num, p_file_name);
    }
    
    
    
    /**@brief Function for the Timer initialization.
     *
     * @details Initializes the timer module. This creates and starts application timers.
     */
    static void timers_init(void)
    {
        // Initialize timer module.
        ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
        // Create timers.
    
        /* YOUR_JOB: Create any timers to be used by the application.
                     Below is an example of how to create a timer.
                     For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
                     one.
           ret_code_t err_code;
           err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
           APP_ERROR_CHECK(err_code); */
    }
    
    
    /**@brief Function for the GAP initialization.
     *
     * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
     *          device including the device name, appearance, and the preferred connection parameters.
     */
    static void gap_params_init(void)
    {
        ret_code_t              err_code;
        ble_gap_conn_params_t   gap_conn_params;
        ble_gap_conn_sec_mode_t sec_mode;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        if(SYS_FACTORY_MODE == system_get_mode())
        {
            err_code = sd_ble_gap_device_name_set(&sec_mode,
                                                  (const uint8_t *)BLE_FACTORY_NAME,
                                                  strlen(BLE_FACTORY_NAME));
        }
        else
        {
            err_code = sd_ble_gap_device_name_set(&sec_mode,
                                                  (const uint8_t *)DEVICE_NAME,
                                                  strlen(DEVICE_NAME));
        }
        APP_ERROR_CHECK(err_code);
        
        err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_HID_DIGITAL_PEN);
        APP_ERROR_CHECK(err_code);
        
        
    
        /* YOUR_JOB: Use an appearance value matching the application's use case.
           err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_);
           APP_ERROR_CHECK(err_code); */
    
        memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
        gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
        gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
        gap_conn_params.slave_latency     = SLAVE_LATENCY;
        gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;
    
        err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for initializing the GATT module.
     */
    static void gatt_init(void)
    {
        ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for handling the YYY Service events.
     * YOUR_JOB implement a service handler function depending on the event the service you are using can generate
     *
     * @details This function will be called for all YY Service events which are passed to
     *          the application.
     *
     * @param[in]   p_yy_service   YY Service structure.
     * @param[in]   p_evt          Event received from the YY Service.
     *
     *
    static void on_yys_evt(ble_yy_service_t     * p_yy_service,
                           ble_yy_service_evt_t * p_evt)
    {
        switch (p_evt->evt_type)
        {
            case BLE_YY_NAME_EVT_WRITE:
                APPL_LOG("[APPL]: charact written with value %s. ", p_evt->params.char_xx.value.p_str);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    */
    
    /**@brief Function for initializing services that will be used by the application.
     */
    												
    static void services_init(void)
    {
        ble_service_initialize();
        hids_init();
    		bas_init();
    }
    #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. */
    
    static pm_peer_id_t      m_peer_id;                                                 /**< Device reference handle to the current bonded central. */
    static pm_peer_id_t      m_whitelist_peers[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];       /**< List of peers currently in the whitelist. */
    static uint32_t          m_whitelist_peer_cnt;                                      /**< Number of peers currently in the whitelist. */
    static bool              m_is_wl_changed;                                           /**< Indicates if the whitelist has been changed since last time it has been updated in the Peer Manager. */
    
    
    /**@brief Fetch the list of peer manager peer IDs.
     *
     * @param[inout] p_peers   The buffer where to store the list of peer IDs.
     * @param[inout] p_size    In: The size of the @p p_peers buffer.
     *                         Out: The number of peers copied in the buffer.
     */
    static void peer_list_get(pm_peer_id_t * p_peers, uint32_t * p_size)
    {
        pm_peer_id_t peer_id;
        uint32_t     peers_to_copy;
    
        peers_to_copy = (*p_size < BLE_GAP_WHITELIST_ADDR_MAX_COUNT) ?
                         *p_size : BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
    
        peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
        *p_size = 0;
    
        while ((peer_id != PM_PEER_ID_INVALID) && (peers_to_copy--))
        {
            p_peers[(*p_size)++] = peer_id;
            peer_id = pm_next_peer_id_get(peer_id);
        }
    }
    
    
    /**@brief Clear bond information from persistent storage.
     */
    static void delete_bonds(void)
    {
        ret_code_t err_code;
    
        NRF_LOG_INFO("Erase bonds!");
    
        err_code = pm_peers_delete();
        APP_ERROR_CHECK(err_code);
    }
    
    
    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:
            {
                NRF_LOG_INFO("Connected to a previously bonded device.");
            } break;
    
            case PM_EVT_CONN_SEC_SUCCEEDED:
            {
                NRF_LOG_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);
    
                m_peer_id = p_evt->peer_id;
    
                // Note: You should check on what kind of white list policy your application should use.
                if (p_evt->params.conn_sec_succeeded.procedure == PM_LINK_SECURED_PROCEDURE_BONDING)
                {
                    NRF_LOG_INFO("New Bond, add the peer to the whitelist if possible");
                    NRF_LOG_INFO("\tm_whitelist_peer_cnt %d, MAX_PEERS_WLIST %d",
                                   m_whitelist_peer_cnt + 1,
                                   BLE_GAP_WHITELIST_ADDR_MAX_COUNT);
    
                    if (m_whitelist_peer_cnt < BLE_GAP_WHITELIST_ADDR_MAX_COUNT)
                    {
                        // Bonded to a new peer, add it to the whitelist.
                        m_whitelist_peers[m_whitelist_peer_cnt++] = m_peer_id;
                        m_is_wl_changed = true;
                    }
                }
            } 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 = true};
                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_BUSY || err_code == FDS_ERR_NO_SPACE_IN_QUEUES)
                {
                    // Retry.
                }
                else
                {
                    APP_ERROR_CHECK(err_code);
                }
            } break;
    
            case PM_EVT_PEERS_DELETE_SUCCEEDED:
            {
                advertising_start(false);
            } break;
    
            case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED:
            {
                // The local database has likely changed, send service changed indications.
                pm_local_database_has_changed();
            } 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_SERVICE_CHANGED_IND_SENT:
            case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
            default:
                break;
        }
    }
    
    
    static void peer_manager_init(void)
    {
        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 handling the Connection Parameters Module.
     *
     * @details This function will be called for all events in the Connection Parameters Module which
     *          are passed to the application.
     *          @note All this function does is to disconnect. This could have been done by simply
     *                setting the disconnect_on_fail config parameter, but instead we use the event
     *                handler mechanism to demonstrate its use.
     *
     * @param[in] p_evt  Event received from the Connection Parameters Module.
     */
    static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
    {
        ret_code_t err_code;
    
        if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
        {
            err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
            APP_ERROR_CHECK(err_code);
        }
    }
     
    
    /**@brief Function for handling a Connection Parameters error.
     *
     * @param[in] nrf_error  Error code containing information about what went wrong.
     */
    static void conn_params_error_handler(uint32_t nrf_error)
    {
        APP_ERROR_HANDLER(nrf_error);
    }
    
    
    /**@brief Function for initializing the Connection Parameters module.
     */
    static void conn_params_init(void)
    {
        ret_code_t             err_code;
        ble_conn_params_init_t cp_init;
    
        memset(&cp_init, 0, sizeof(cp_init));
    
        cp_init.p_conn_params                  = NULL;
        cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
        cp_init.next_conn_params_update_delay  = NEXT_CONN_PARAMS_UPDATE_DELAY;
        cp_init.max_conn_params_update_count   = MAX_CONN_PARAMS_UPDATE_COUNT;
        cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
        cp_init.disconnect_on_fail             = false;
        cp_init.evt_handler                    = on_conn_params_evt;
        cp_init.error_handler                  = conn_params_error_handler;
    
        err_code = ble_conn_params_init(&cp_init);
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for starting timers.
     */
    static void application_timers_start(void)
    {
        /* YOUR_JOB: Start your timers. below is an example of how to start a timer.
           ret_code_t err_code;
           err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
           APP_ERROR_CHECK(err_code); */
        utimer_second_start();
    }
    
    
    /**@brief Function for putting the chip into sleep mode.
     *
     * @note This function will not return.
     */
    
    
    /**@brief Function for handling advertising events.
     *
     * @details This function will be called for advertising events which are passed to the application.
     *
     * @param[in] ble_adv_evt  Advertising event.
     */
    static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
    {
        ret_code_t err_code;
        
        switch (ble_adv_evt)
        {
            case BLE_ADV_EVT_DIRECTED:
                NRF_LOG_INFO("Directed advertising.");
                break;
            case BLE_ADV_EVT_FAST:
                NRF_LOG_INFO("Fast advertising.");
                break;
            case BLE_ADV_EVT_SLOW:
                NRF_LOG_INFO("Slow advertising.");
                break;
            case BLE_ADV_EVT_FAST_WHITELIST:
                NRF_LOG_INFO("Fast advertising with whitelist.");
                break;
            case BLE_ADV_EVT_SLOW_WHITELIST:
                NRF_LOG_INFO("Slow advertising with whitelist.");
                break;
            case BLE_ADV_EVT_IDLE:
                ble_adv_start();
                break;
            case BLE_ADV_EVT_WHITELIST_REQUEST:
            {
                ble_gap_addr_t whitelist_addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
                ble_gap_irk_t  whitelist_irks[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
                uint32_t       addr_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
                uint32_t       irk_cnt  = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
    
                err_code = pm_whitelist_get(whitelist_addrs, &addr_cnt,
                                            whitelist_irks,  &irk_cnt);
                APP_ERROR_CHECK(err_code);
                NRF_LOG_DEBUG("pm_whitelist_get returns %d addr in whitelist and %d irk whitelist",
                               addr_cnt,
                               irk_cnt);
    
                // Apply the whitelist.
                err_code = ble_advertising_whitelist_reply(&m_advertising,
                                                           whitelist_addrs,
                                                           addr_cnt,
                                                           whitelist_irks,
                                                           irk_cnt);
                APP_ERROR_CHECK(err_code);
            }
            break;
    
            case BLE_ADV_EVT_PEER_ADDR_REQUEST:
            {
                pm_peer_data_bonding_t peer_bonding_data;
    
                // Only Give peer address if we have a handle to the bonded peer.
                if (m_peer_id != PM_PEER_ID_INVALID)
                {
    
                    err_code = pm_peer_data_bonding_load(m_peer_id, &peer_bonding_data);
                    if (err_code != NRF_ERROR_NOT_FOUND)
                    {
                        APP_ERROR_CHECK(err_code);
    
                        ble_gap_addr_t * p_peer_addr = &(peer_bonding_data.peer_ble_id.id_addr_info);
                        err_code = ble_advertising_peer_addr_reply(&m_advertising, p_peer_addr);
                        APP_ERROR_CHECK(err_code);
                    }
    
                }
            }
            break;
    
            default:
                break;
        }
    }
    
    
    /**@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)
    {
        ret_code_t err_code = NRF_SUCCESS;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected.");
                // LED indication will be changed when advertising starts.
            
    //            if (m_is_wl_changed)
    //            {
    //                // The whitelist has been modified, update it in the Peer Manager.
    //                err_code = pm_whitelist_set(m_whitelist_peers, m_whitelist_peer_cnt);
    //                APP_ERROR_CHECK(err_code);
    
    //                err_code = pm_device_identities_list_set(m_whitelist_peers, m_whitelist_peer_cnt);
    //                if (err_code != NRF_ERROR_NOT_SUPPORTED)
    //                {
    //                    APP_ERROR_CHECK(err_code);
    //                }
    
    //                m_is_wl_changed = false;
    //            }
            
            
                break;
    
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected.");
                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                break;
    
    #ifndef S140
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                NRF_LOG_DEBUG("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;
    #endif
    
    //        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
    //            // Pairing not supported
    //            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
    //            APP_ERROR_CHECK(err_code);
    //            break;
    
            case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
                {
                    ble_gap_data_length_params_t dl_params;
    
                    // Clearing the struct will effectivly set members to @ref BLE_GAP_DATA_LENGTH_AUTO
                    memset(&dl_params, 0, sizeof(ble_gap_data_length_params_t));
                    err_code = sd_ble_gap_data_length_update(p_ble_evt->evt.gap_evt.conn_handle, &dl_params, NULL);
                    APP_ERROR_CHECK(err_code);
                }
                break;
    
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                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.
                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;
    
            case BLE_EVT_USER_MEM_REQUEST:
                err_code = sd_ble_user_mem_reply(p_ble_evt->evt.gattc_evt.conn_handle, NULL);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
            {
                ble_gatts_evt_rw_authorize_request_t  req;
                ble_gatts_rw_authorize_reply_params_t auth_reply;
    
                req = p_ble_evt->evt.gatts_evt.params.authorize_request;
    
                if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
                {
                    if ((req.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ)     ||
                        (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) ||
                        (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
                    {
                        if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
                        {
                            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
                        }
                        else
                        {
                            auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
                        }
                        auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
                        err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle,
                                                                   &auth_reply);
                        APP_ERROR_CHECK(err_code);
                        }
                    }
                }
                break; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST
    
            default:
                // No implementation needed.
                break;
        }
    }
    
    
    /**@brief Function for initializing the BLE stack.
     *
     * @details Initializes the SoftDevice and the BLE event interrupt.
     */
    static void ble_stack_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_sdh_enable_request();
        APP_ERROR_CHECK(err_code);
    
        // Configure the BLE stack using the default settings.
        // Fetch the start address of the application RAM.
        uint32_t ram_start = 0;
        err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Enable BLE stack.
        err_code = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Register a handler for BLE events.
        NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }
    
    
    
    /**@brief Function for initializing the Advertising functionality.
     */
    static void advertising_init(void)
    {
        uint32_t err_code;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
        init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
        init.advdata.include_appearance = true;
        
        init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.srdata.uuids_complete.p_uuids = m_adv_uuids;
        
        init.config.ble_adv_whitelist_enabled      = false;
        init.config.ble_adv_directed_enabled       = false;
        init.config.ble_adv_directed_slow_enabled  = false;
        init.config.ble_adv_directed_slow_interval = 0;
        init.config.ble_adv_directed_slow_timeout  = 0;
        
        init.config.ble_adv_fast_enabled           = true;
        init.config.ble_adv_fast_interval          = APP_ADV_FAST_INTERVAL;
        init.config.ble_adv_fast_timeout           = APP_ADV_FAST_TIMEOUT;
        
        init.config.ble_adv_slow_enabled           = true;
        init.config.ble_adv_slow_timeout           = 0;
        
        if(SYS_FACTORY_MODE == system_get_mode())
        {
            init.config.ble_adv_slow_interval = APP_FAC_ADV_INTERVAL;
        }
        else
        {
            init.config.ble_adv_slow_interval = APP_ADV_INTERVAL;
        }
    
        
    #if 1
        ble_advdata_manuf_data_t manuf_data;
    
        manuf_data.company_identifier = 0x0000;
        manuf_data.data.p_data = m_addl_adv_manuf_data;
        manuf_data.data.size = 6;
        init.advdata.p_manuf_specific_data = &manuf_data;
    
    #endif
    
        init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }
    
    
    /**@brief Function for initializing buttons and leds.
     *
     * @param[out] p_erase_bonds  Will be true if the clear bonding button was pressed to wake the application up.
     */
    
    /**@brief Function for initializing the nrf log module.
     */
    static void log_init(void)
    {
        ret_code_t err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    }
    
    
    /**@brief Function for the Power manager.
     */
    static void power_manage(void)
    {
        ret_code_t err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for starting advertising.
     */
    static void advertising_start(bool erase_bonds)
    {
        if (erase_bonds == true)
        {
            delete_bonds();
            // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
        }
        else
        {
            ret_code_t ret;
    
            memset(m_whitelist_peers, PM_PEER_ID_INVALID, sizeof(m_whitelist_peers));
            m_whitelist_peer_cnt = (sizeof(m_whitelist_peers) / sizeof(pm_peer_id_t));
    
            peer_list_get(m_whitelist_peers, &m_whitelist_peer_cnt);
    
            ret = pm_whitelist_set(m_whitelist_peers, m_whitelist_peer_cnt);
            APP_ERROR_CHECK(ret);
    
            // Setup the device identies list.
            // Some SoftDevices do not support this feature.
            ret = pm_device_identities_list_set(m_whitelist_peers, m_whitelist_peer_cnt);
            if (ret != NRF_ERROR_NOT_SUPPORTED)
            {
                APP_ERROR_CHECK(ret);
            }
    
            m_is_wl_changed = false;
    
            ret = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
            APP_ERROR_CHECK(ret);
        }
    }
    
    void get_mac_addr(uint8_t * p_mac_addr)
    {
        uint32_t error_code;
        ble_gap_addr_t p_mac_addr_t;                    //= (ble_gap_addr_t *)	malloc(sizeof(ble_gap_addr_t));
    
        //ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t *)	malloc(sizeof(ble_gap_addr_t));
        error_code = sd_ble_gap_addr_get(&p_mac_addr_t);
    
        NRF_LOG_INFO("michael printf get mac error_code=%d", error_code);
    
        APP_ERROR_CHECK(error_code);
        uint8_t * d = p_mac_addr_t.addr;
    
        for (uint8_t i = 6; i > 0; )
        {
            i--;
            p_mac_addr[5 - i] = d[i];
        }
    }
    
    void ble_adv_start()
    {
        ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    
        APP_ERROR_CHECK(err_code);  
    }

    And this is the hid code that I have added the NRF_ERROR_FORBIDDEN.

    const static uint8_t rep_map_data[] =
        {
            0x05, 0x01, // Usage Page (Generic Desktop)
            0x09, 0x02, // Usage (Mouse)
    
            0xA1, 0x01, // Collection (Application)
    
            // Report ID 1: Mouse buttons + scroll/pan
            0x85, 0x01,       // Report Id 1
            0x09, 0x01,       // Usage (Pointer)
            0xA1, 0x00,       // Collection (Physical)
            0x95, 0x05,       // Report Count (3)
            0x75, 0x01,       // Report Size (1)
            0x05, 0x09,       // Usage Page (Buttons)
            0x19, 0x01,       // Usage Minimum (01)
            0x29, 0x05,       // Usage Maximum (05)
            0x15, 0x00,       // Logical Minimum (0)
            0x25, 0x01,       // Logical Maximum (1)
            0x81, 0x02,       // Input (Data, Variable, Absolute)
            0x95, 0x01,       // Report Count (1)
            0x75, 0x03,       // Report Size (3)
            0x81, 0x01,       // Input (Constant) for padding
            0x75, 0x08,       // Report Size (8)
            0x95, 0x01,       // Report Count (1)
            0x05, 0x01,       // Usage Page (Generic Desktop)
            0x09, 0x38,       // Usage (Wheel)
            0x15, 0x81,       // Logical Minimum (-127)
            0x25, 0x7F,       // Logical Maximum (127)
            0x81, 0x06,       // Input (Data, Variable, Relative)
            0x05, 0x0C,       // Usage Page (Consumer)
            0x0A, 0x38, 0x02, // Usage (AC Pan)
            0x95, 0x01,       // Report Count (1)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0xC0,             // End Collection (Physical)
    
            // Report ID 2: Mouse motion
            0x85, 0x02,       // Report Id 2
            0x09, 0x01,       // Usage (Pointer)
            0xA1, 0x00,       // Collection (Physical)
            0x75, 0x0C,       // Report Size (12)
            0x95, 0x02,       // Report Count (2)
            0x05, 0x01,       // Usage Page (Generic Desktop)
            0x09, 0x30,       // Usage (X)
            0x09, 0x31,       // Usage (Y)
            0x16, 0x01, 0xF8, // Logical maximum (2047)
            0x26, 0xFF, 0x07, // Logical minimum (-2047)
            0x81, 0x06,       // Input (Data, Variable, Relative)
            0xC0,             // End Collection (Physical)
            0xC0,             // End Collection (Application)
    
            // Report ID 3: Advanced buttons
            0x05, 0x0C,       // Usage Page (Consumer)
            0x09, 0x01,       // Usage (Consumer Control)
            0xA1, 0x01,       // Collection (Application)
            0x85, 0x03,       // Report Id (3)
            0x15, 0x00,       // Logical minimum (0)
            0x25, 0x01,       // Logical maximum (1)
            0x75, 0x01,       // Report Size (1)
            0x95, 0x01,       // Report Count (1)
    
            0x09, 0xCD,       // Usage (Play/Pause)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x0A, 0x83, 0x01, // Usage (AL Consumer Control Configuration)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x09, 0xB5,       // Usage (Scan Next Track)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x09, 0xB6,       // Usage (Scan Previous Track)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
    
            0x09, 0xEA,       // Usage (Volume Down)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x09, 0xE9,       // Usage (Volume Up)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x0A, 0x25, 0x02, // Usage (AC Forward)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0x0A, 0x24, 0x02, // Usage (AC Back)
            0x81, 0x06,       // Input (Data,Value,Relative,Bit Field)
            0xC0              // End Collection
        };
    
    
    
    /**@brief Function for handling Service 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 service_error_handler(uint32_t nrf_error)
    {
        APP_ERROR_HANDLER(nrf_error);
    }
    
    /**@brief Function for initializing HID Service.
     */
    void hids_init(void)
    {
        ret_code_t                err_code;
        ble_hids_init_t           hids_init_obj;
        ble_hids_inp_rep_init_t   inp_rep_array[INPUT_REPORT_COUNT];
        ble_hids_inp_rep_init_t * p_input_report;
        uint8_t                   hid_info_flags;
    
        
    
        memset((void *)inp_rep_array, 0, sizeof(inp_rep_array));
        // Initialize HID Service.
        p_input_report                      = &inp_rep_array[INPUT_REP_BUTTONS_INDEX];
        p_input_report->max_len             = INPUT_REP_BUTTONS_LEN;
        p_input_report->rep_ref.report_id   = INPUT_REP_REF_BUTTONS_ID;
        p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.cccd_write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.write_perm);
    
        p_input_report                      = &inp_rep_array[INPUT_REP_MOVEMENT_INDEX];
        p_input_report->max_len             = INPUT_REP_MOVEMENT_LEN;
        p_input_report->rep_ref.report_id   = INPUT_REP_REF_MOVEMENT_ID;
        p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.cccd_write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.write_perm);
    
        p_input_report                      = &inp_rep_array[INPUT_REP_MPLAYER_INDEX];
        p_input_report->max_len             = INPUT_REP_MEDIA_PLAYER_LEN;
        p_input_report->rep_ref.report_id   = INPUT_REP_REF_MPLAYER_ID;
        p_input_report->rep_ref.report_type = BLE_HIDS_REP_TYPE_INPUT;
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.cccd_write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&p_input_report->security_mode.write_perm);
    
        hid_info_flags = HID_INFO_FLAG_REMOTE_WAKE_MSK | HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK;
    
        memset(&hids_init_obj, 0, sizeof(hids_init_obj));
    
        hids_init_obj.evt_handler                    = on_hids_evt;
        hids_init_obj.error_handler                  = service_error_handler;
        hids_init_obj.is_kb                          = false;
        hids_init_obj.is_mouse                       = true;
        hids_init_obj.inp_rep_count                  = INPUT_REPORT_COUNT;
        hids_init_obj.p_inp_rep_array                = inp_rep_array;
        hids_init_obj.outp_rep_count                 = 0;
        hids_init_obj.p_outp_rep_array               = NULL;
        hids_init_obj.feature_rep_count              = 0;
        hids_init_obj.p_feature_rep_array            = NULL;
        hids_init_obj.rep_map.data_len               = sizeof(rep_map_data);
        hids_init_obj.rep_map.p_data                 = (uint8_t *)rep_map_data;
        hids_init_obj.hid_information.bcd_hid        = BASE_USB_HID_SPEC_VERSION;
        hids_init_obj.hid_information.b_country_code = 0;
        hids_init_obj.hid_information.flags          = hid_info_flags;
        hids_init_obj.included_services_count        = 0;
        hids_init_obj.p_included_services_array      = NULL;
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&hids_init_obj.rep_map.security_mode.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hids_init_obj.rep_map.security_mode.write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&hids_init_obj.hid_information.security_mode.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hids_init_obj.hid_information.security_mode.write_perm);
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(
            &hids_init_obj.security_mode_boot_mouse_inp_rep.cccd_write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(
            &hids_init_obj.security_mode_boot_mouse_inp_rep.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(
            &hids_init_obj.security_mode_boot_mouse_inp_rep.write_perm);
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&hids_init_obj.security_mode_protocol.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&hids_init_obj.security_mode_protocol.write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hids_init_obj.security_mode_ctrl_point.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&hids_init_obj.security_mode_ctrl_point.write_perm);
    
        err_code = ble_hids_init(&m_hids, &hids_init_obj);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for handling HID events.
     *
     * @details This function will be called for all HID events which are passed to the application.
     *
     * @param[in]   p_hids  HID service structure.
     * @param[in]   p_evt   Event received from the HID service.
     */
    static void on_hids_evt(ble_hids_t * p_hids, ble_hids_evt_t * p_evt)
    {
        switch (p_evt->evt_type)
        {
            case BLE_HIDS_EVT_BOOT_MODE_ENTERED:
                m_in_boot_mode = true;
                break;
    
            case BLE_HIDS_EVT_REPORT_MODE_ENTERED:
                m_in_boot_mode = false;
                break;
    
            case BLE_HIDS_EVT_NOTIF_ENABLED:
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    
    
    /**@brief Function for sending a Mouse Movement.
     *
     * @param[in]   x_delta   Horizontal movement.
     * @param[in]   y_delta   Vertical movement.
     */
    void m_mouse_movement_send(int16_t x_delta, int16_t y_delta)
    {
        ret_code_t err_code;
    
        if (m_in_boot_mode)
        {
            x_delta = MIN(x_delta, 0x00ff);
            y_delta = MIN(y_delta, 0x00ff);
    
            err_code = ble_hids_boot_mouse_inp_rep_send(&m_hids,
                                                        0x00,
                                                        (int8_t)x_delta,
                                                        (int8_t)y_delta,
                                                        0,
                                                        NULL);
        }
        else
        {
            uint8_t buffer[INPUT_REP_MOVEMENT_LEN];
    
            APP_ERROR_CHECK_BOOL(INPUT_REP_MOVEMENT_LEN == 3);
    
            x_delta = MIN(x_delta, 0x0fff);
            y_delta = MIN(y_delta, 0x0fff);
    
            buffer[0] = x_delta & 0x00ff;
            buffer[1] = ((y_delta & 0x000f) << 4) | ((x_delta & 0x0f00) >> 8);
            buffer[2] = (y_delta & 0x0ff0) >> 4;
    
            err_code = ble_hids_inp_rep_send(&m_hids,
                                             INPUT_REP_MOVEMENT_INDEX,
                                             INPUT_REP_MOVEMENT_LEN,
                                             buffer);
        }
    
        if ((err_code != NRF_SUCCESS) &&
            (err_code != NRF_ERROR_BUSY) &&
            (err_code != NRF_ERROR_RESOURCES) &&
            (err_code != NRF_ERROR_FORBIDDEN) &&
            (err_code != NRF_ERROR_INVALID_STATE) &&
            (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
           )
        {
            APP_ERROR_HANDLER(err_code);
        }
    }
    
    void bas_init(void)
    {
        ret_code_t     err_code;
        ble_bas_init_t bas_init_obj;
    
        memset(&bas_init_obj, 0, sizeof(bas_init_obj));
    
        bas_init_obj.evt_handler          = NULL;
        bas_init_obj.support_notification = true;
        bas_init_obj.p_report_ref         = NULL;
        bas_init_obj.initial_batt_level   = 100;
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&bas_init_obj.battery_level_char_attr_md.cccd_write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&bas_init_obj.battery_level_char_attr_md.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init_obj.battery_level_char_attr_md.write_perm);
    
        BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&bas_init_obj.battery_level_report_read_perm);
    
        err_code = ble_bas_init(&m_bas, &bas_init_obj);
        APP_ERROR_CHECK(err_code);
    }
    
    void  m_mouse_timer_timeout_handler(void *arg )
    {
        m_mouse_movement_send(MOVEMENT_SPEED,MOVEMENT_SPEED);
    }
    
    /**@brief Function for mouse timer init.
     * 
    */  
    void m_mouse_timer_init(void)
    {
        uint32_t err_code = app_timer_create(&m_mouse_timer_id,
                                    APP_TIMER_MODE_REPEATED,
                                    m_mouse_timer_timeout_handler);
        APP_ERROR_CHECK(err_code);
        
        err_code = app_timer_start(m_mouse_timer_id,APP_TIMER_TICKS(500),NULL);
        APP_ERROR_CHECK(err_code);
    }

    The SDK doesn't handle the NRF_ERROR_FORBIDDEN. So I want to know the reason of my problem. Even though I solved this problem by adding NRF_ERROR_FORBIDDEN.

  • Hello,

    Taylor said:
    My project is based on nRF52_SDK_14.2.0, of course I edited some codes.Where can I find the position of setting the CCCD?

    The security requirements of the CCCD is configured in the hids_init function. The unmodified hid mouse example uses SEC_JUST_WORKS.

    Taylor said:
    Actually, when I add the error of NRF_ERROR_FORBIDDEN handle, the mouse will move.

    That is strange.
    Is the call ever successful? It must be, if the notification goes through.

    Taylor said:
    The SDK doesn't handle the NRF_ERROR_FORBIDDEN. So I want to know the reason of my problem. Even though I solved this problem by adding NRF_ERROR_FORBIDDEN.

    Well, as I wrote in my last reply the error NRF_ERROR_FORBIDDEN is generated when there is a mismatch between the security level of the connection and the requirements for read/writing the characteristic.

    Taylor said:
    This is the main code.

    Where is your hids_init function? Is there any part of the code you have shared here you would like me to take a look at in particular?

    Best regards,
    Karl

  • Hi Taylor,

    I met almost the same problem. It seems that when the interval between using "mouse_movement_send()" is less than around 500ms, the error just ocurrs. When it's set longer, the error disappears. However, the solution you mentioned in the reply does works for me too. I have no idea about the relationship with security level, since params in hids_init() about CCCD are already set SEC_JUST_WORK.

Related