/**
 * Clear the whitelist.
 * 
 * @return int 0: success, BLE error code otherwise 
 */
int
ble_ll_whitelist_clear(void)
{

    /* Check proper state */
    if (!ble_ll_whitelist_chg_allowed()) {
        return BLE_ERR_CMD_DISALLOWED;
    }

#ifdef BLE_USES_HW_WHITELIST
    ble_hw_whitelist_clear();
#else
    int i;
    struct ble_ll_whitelist_entry *wl;

    /* Set the number of entries to 0 */
    wl = &g_ble_ll_whitelist[0];
    for (i = 0; i < NIMBLE_OPT_LL_WHITELIST_SIZE; ++i) {
        wl->wl_valid = 0;
        ++wl;
    }
#endif

    return BLE_ERR_SUCCESS;
}
/**
 * Remove a device from the whitelist
 *
 * @param cmdbuf
 *
 * @return int 0: success, BLE error code otherwise
 */
int
ble_ll_whitelist_rmv(uint8_t *addr, uint8_t addr_type)
{
    int position;

    /* Must be in proper state */
    if (!ble_ll_whitelist_chg_allowed()) {
        return BLE_ERR_CMD_DISALLOWED;
    }

    position = ble_ll_whitelist_search(addr, addr_type);
    if (position) {
        g_ble_ll_whitelist[position - 1].wl_valid = 0;
    }

#if (BLE_USES_HW_WHITELIST == 1)
    ble_hw_whitelist_rmv(addr, addr_type);
#endif

    return BLE_ERR_SUCCESS;
}
/**
 * Add a device to the whitelist 
 * 
 * @return int 
 */
int
ble_ll_whitelist_add(uint8_t *addr, uint8_t addr_type)
{
    int rc;

    /* Must be in proper state */
    if (!ble_ll_whitelist_chg_allowed()) {
        return BLE_ERR_CMD_DISALLOWED;
    }

    /* Check if we have any open entries */
#ifdef BLE_USES_HW_WHITELIST
    rc = ble_hw_whitelist_add(addr, addr_type);
#else
    int i;
    struct ble_ll_whitelist_entry *wl;

    rc = BLE_ERR_SUCCESS;
    if (!ble_ll_is_on_whitelist(addr, addr_type)) {
        wl = &g_ble_ll_whitelist[0];
        for (i = 0; i < NIMBLE_OPT_LL_WHITELIST_SIZE; ++i) {
            if (wl->wl_valid == 0) {
                memcpy(&wl->wl_dev_addr[0], addr, BLE_DEV_ADDR_LEN);
                wl->wl_addr_type = addr_type;
                wl->wl_valid = 1;
                break;
            }
            ++wl;
        }

        if (i == NIMBLE_OPT_LL_WHITELIST_SIZE) {
            rc = BLE_ERR_MEM_CAPACITY;
        }
    }
#endif

    return rc;
}