/**
 * Copyright (c) 2012 - 2017, 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.
 * 
 */

/** @file
 *
 * @defgroup ble_sdk_app_hids_keyboard_main main.c
 * @{
 * @ingroup ble_sdk_app_hids_keyboard
 * @brief HID Keyboard Sample Application main file.
 *
 * This file contains is the source code for a sample application using the HID, Battery and Device
 * Information Services for implementing a simple keyboard functionality.
 * Pressing Button 0 will send text 'hello' to the connected peer. On receiving output report,
 * it toggles the state of LED 2 on the mother board based on whether or not Caps Lock is on.
 * This application uses the @ref app_scheduler.
 *
 * Also it would accept pairing requests from any peer device.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

#include "nordic_common.h"
#include "nrf.h"
#include "nrf_assert.h"
#include "app_error.h"

#include "nrf_cli.h"
#include "nrf_cli_types.h"
#include "boards.h"
#include "cli.h"


#define CLI_LOG_QUEUE_SIZE 16

#include "nrf_uart.h"
#include "cli_uart.h"
CLI_UART_DEF(m_cli_uart_transport, 0, 2048, 2048);

NRF_CLI_DEF(m_cli_uart,
            "uart_cli:~$ ",
            &m_cli_uart_transport.transport,
            '\r',
            CLI_LOG_QUEUE_SIZE);

static bool m_active;

void cli_init(void)
{
    if (m_active)
    {
        return;
    }

    cli_uart_config_t uart_config = {
            .tx_pin = TX_PIN_NUMBER,
            .rx_pin = RX_PIN_NUMBER,
            .baudrate = (uint32_t)NRF_UART_BAUDRATE_115200
    };

    ret_code_t ret = nrf_cli_init(&m_cli_uart, &uart_config, true, true, NRF_LOG_SEVERITY_INFO);
    APP_ERROR_CHECK(ret);

    ret = nrf_cli_start(&m_cli_uart);
    APP_ERROR_CHECK(ret);

    m_active = true;
}

void cli_uninit(void)
{
    if (m_active)
    {
        ret_code_t ret;
        ret = nrf_cli_uninit(&m_cli_uart);
        APP_ERROR_CHECK(ret);

        m_active = false;
    }
}

void cli_process(void)
{
    nrf_cli_process(&m_cli_uart);
}

void cli_fprintf(char const * p_fmt, ...)
{
    va_list args = {0};
    va_start(args, p_fmt);

    uint32_t arg0 = va_arg ( args, uint32_t );
    nrf_cli_fprintf(&m_cli_uart, NRF_CLI_INFO, p_fmt, arg0);

    va_end(args);
}
