/******************************************************************************
 * @file    ble_lora_mngt.h
 * @author  Insight SiP
 * @version V2.2.0
 * @date    20-dec-2019
 * @brief   LoRa mngt ble service header file.
 *
 * @attention
 *	THIS SOFTWARE IS PROVIDED BY INSIGHT SIP "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 INSIGHT SIP 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.
 *
  *****************************************************************************/

#include "app_util_platform.h"
#include "ble_srv_common.h"

#ifndef BLE_LORAMNGT_H__
#define BLE_LORAMNGT_H__

#ifdef __GNUC__
    #ifdef PACKED
        #undef PACKED
    #endif

    #define PACKED(TYPE) TYPE __attribute__ ((packed))
#endif

/**@brief   Macro for defining a ble_senslora instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_LORAMNGT_DEF(_name)               		\
static ble_loramngt_t _name;                         	\
NRF_SDH_BLE_OBSERVER(_name ## _obs,               	\
                     APP_BLE_OBSERVER_PRIO,             \
                     ble_loramngt_on_ble_evt, &_name)


#define BLE_UUID_LORAMNGT_SERVICE 0x1000                      /**< The UUID of the loramngt Service. */

typedef PACKED( struct
{
    uint8_t data_rate;
    uint8_t tx_power;
    uint16_t tx_request_interval;
    uint8_t enable_adr;
}) ble_loramngt_config_t;

typedef PACKED( struct
{
    uint8_t frame_number;
    int16_t rssi;
    int8_t snr;
}) ble_loramngt_rxdiag_t;

#define LORAMNGT_DATA_RATE_MAX          6
#define LORAMNGT_TX_POWER_MAX           7
#define LORAMNGT_TX_INTERVAL_MIN        5
#define LORAMNGT_TX_INTERVAL_MAX 	3600
#define LORAMNGT_ADR_MAX                1

typedef enum
{
    BLE_LORAMNGT_EVT_NOTIF_RXDIAG,
    BLE_LORAMNGT_EVT_CONFIG_RECEIVED
}ble_loramngt_evt_type_t;

/* Forward declaration of the ble_loramngt_t type. */
typedef struct ble_loramngt_s ble_loramngt_t;

/**@brief loramngt service event handler type. */
typedef void (*ble_loramngt_evt_handler_t) (ble_loramngt_t          *p_loramngt,
                                            ble_loramngt_evt_type_t evt_type,
                                            uint8_t                 *p_data,
                                            uint16_t                length);

/**@brief loramngt configuration Service initialization structure.
 *
 * @details This structure contains the initialization information for the service. The application
 * must fill this structure and pass it to the service using the @ref ble_loramngt_init function.
 */
typedef struct
{
    ble_loramngt_config_t           *p_init_config;
    ble_loramngt_rxdiag_t           *p_init_rxdiag;
    ble_loramngt_evt_handler_t      evt_handler;            /**< Event handler to be called for handling received data. */
} ble_loramngt_init_t;


/**@brief loramngt service structure.
 *
 * @details This structure contains status information related to the service.
 */
struct ble_loramngt_s
{
    uint8_t                  	uuid_type;                    	/**< UUID type for loramngt Service Base UUID. */
    uint16_t                 	service_handle;               	/**< Handle of loramngt Service (as provided by the S132 SoftDevice). */
    ble_gatts_char_handles_t 	config_handles;          	/**< Handles related to the configuration characteristic (as provided by the S132 SoftDevice). */
    ble_gatts_char_handles_t 	rxdiag_handles;          	/**< Handles related to the rx diagnotics characteristic (as provided by the S132 SoftDevice). */
    bool                        is_rxdiag_notif_enabled;    	/**< Variable to indicate if the peer has enabled notification of the characteristic.*/
    uint16_t                 	conn_handle;                  	/**< Handle of the current connection (as provided by the S132 SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */
    ble_loramngt_evt_handler_t	evt_handler;                  	/**< Event handler to be called for handling received data. */
};

/**@brief Function for initializing the loramngt service.
 *
 * @param[out] p_loramngt 	loramngt service structure. This structure must be supplied
 *                        	by the application. It is initialized by this function and will
 *                        	later be used to identify this particular service instance.
 * @param[in] p_loramngt_init  	Information needed to initialize the service.
 *
 * @retval NRF_SUCCESS If the service was successfully initialized. Otherwise, an error code is returned.
 * @retval NRF_ERROR_NULL If either of the pointers p_tes or p_tes_init is NULL.
 */
uint32_t ble_loramngt_init(ble_loramngt_t *p_loramngt, const ble_loramngt_init_t *p_loramngt_init);

/**@brief Function for handling the loramngt Service's BLE events.
 *
 * @details The loramngt Service expects the application to call this function each time an
 * event is received from the S132 SoftDevice. This function processes the event if it
 * is relevant and calls the loramngt Service event handler of the
 * application if necessary.
 *
 * @param[in] p_loramngt     	loramngt service structure.
 * @param[in] p_ble_evt   	Event received from the S132 SoftDevice.
 */
void ble_loramngt_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);

/**@brief Function for setting the rxdiag.
 *
 * @details This function sends the input rxdiag as an rxdiag characteristic notification to the peer.
 *
 * @param[in] p_loramngt  Pointer to the loramngt Service structure.
 * @param[in] p_data      Pointer to the rxdiag data.
 *
 * @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
 */
uint32_t ble_loramngt_rxdiag_set(ble_loramngt_t * p_loramngt, ble_loramngt_rxdiag_t * p_data);

#endif

