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

#ifndef _RADIO_CONTROL_H__
#define _RADIO_CONTROL_H__
#include <stdint.h>
#include <stdbool.h>
/** @brief callbacks for after radio event is complete */
typedef void (*radio_rx_cb_t)(uint8_t* p_data, bool success, uint32_t crc, uint8_t rssi);
typedef void (*radio_tx_cb_t)(uint8_t* p_data);

/** @brief callback for when the radio is out of things to do */
typedef void (*radio_idle_cb_t)(void);

typedef enum
{
    RADIO_EVENT_TYPE_TX,
    RADIO_EVENT_TYPE_RX,            /**< Regular RX. Will be stopped automatically ~100us after start if start time > 0 */
    RADIO_EVENT_TYPE_RX_PREEMPTABLE /**< Will be aborted when a new event comes in */
} radio_event_type_t;

/**
* @brief executable radio event type
*/
typedef struct
{
    uint8_t* packet_ptr;            /**< Packet pointer to use. */

    /** Access address index to operate on. Must be either 0 (the default BLE advertisement address) or 1 (the alternate address set through a call to radio_alt_aa_set()).  */
    uint8_t access_address;
    radio_event_type_t event_type;  /**< RX/TX */
    uint8_t channel;                /**< Channel to execute event on */
    uint8_t tx_power;               /**< Transmit power for TX events */
} radio_event_t;

/**
* @brief Starts the radio init procedure
*   Must be called at the beginning of each timeslot
*
* @param[in] idle_cb Callback indicating that the radio is out of events in its queue.
* @param[in] rx_cb Callback indicating that an RX event finished.
* @param[in] tx_cb Callback indicating that an TX event finished.
*/
void radio_init(radio_idle_cb_t idle_cb,
                radio_rx_cb_t   rx_cb,
                radio_tx_cb_t   tx_cb);

/**
* @brief Set the alternate access address.
*
* @detail The first access address is always the standard BLE advertisement
*   access address, 0x8E89BED6. Setting the alternate access address to
*   something other than the standard address allows you to transmit and
*   receive on both addresses.
*
* @param[in] access_address The 32bit alternate access address.
*/
void radio_alt_aa_set(uint32_t access_address);

/**
* @brief Schedule a radio event (tx/rx)
*
* @param[in] radio_event pointer to user-created radio event to be queued.
*   Is copied into queue, may be stack allocated
*/
uint32_t radio_order(radio_event_t* radio_event);

/**
* @brief Disable the radio. Overrides any ongoing rx or tx procedures
*/
void radio_disable(void);


/**
* @brief Radio event handler, checks any relevant events generated by the radio, and acts accordingly
*/
void radio_event_handler(void);

#endif /* _RADIO_CONTROL_H__*/
