// Copyright (C)2025, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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.
using System.Runtime.InteropServices;
namespace IO.Bindings
{
///
///
/// Wrapper class for the
///
/// Wio-E5 LoRa Transceiver Module Driver Library libwioe5p2p.so
/// using test mode aka P2P (Point to Point or Peer to Peer)
/// broadcast mode.
///
///
/// P2P is misleading because there is no station addressing and all
/// transmissions are broadcasts. Any station with the same RF settings
/// (frequency, spreading factor, and chirp bandwidth) will be able to
/// receive what you transmit with this library.
///
///
/// In test aka P2P mode, the Wio-E5 transmits unencrypted
/// "implicit header" frames consisting of a configurable number of
/// preamble bits, 1 to 253 payload bytes, and two CRC bytes. Upon
/// reception of each frame, the Wio-E5 verifies the CRC, discarding
/// erroneous frames and passing valid ones to the device driver.
///
///
/// Unlike LoRaWan mode, frames with up to 253 payload bytes can be sent
/// and received using any data rate scheme (the combination of
/// spreading factor, modulation bandwidth, and the derived RF symbol
/// rate).
///
///
public static class libwioe5p2p
{
///
/// Initialize the Wio-E5 driver shared library and transceiver module.
///
/// Serial port device name e.g.
/// /dev/ttyAMA0 or /dev/ttyUSB0.
/// Serial port baud rate in bits per second
/// (9600, 19200, 38400, 57600, 115200, or 230400).
///
/// RF center frequency in MHz, 863.0 to 870.0
/// (European Union
///
/// ISM Band) or 902.0 to 928.0 (United States
///
/// ISM Band).
/// Spreading factor (7 to 12).
/// Spread spectrum chirp bandwidth in kHz
/// (125, 250, or 500).
/// Number of transmit preamble bits (12 is
/// recommended).
/// Number of receive preamble bits (15 is
/// recommended).
/// Transmit power in dBm (0 to 22).
/// Wio-E5 device handle.
/// Error code. Zero upon success.
[DllImport("wioe5p2p")]
public static extern void wioe5p2p_init
(string portname,
int baudrate,
float freqmhz,
int spreading,
int bandwidth,
int txpreamble,
int rxpreamble,
int txpower,
out int handle,
out int error);
///
/// Terminate the Wio-E5 driver shared library background task.
///
/// Wio-E5 device handle.
/// Error code. Zero upon success.
[DllImport("wioe5p2p")]
public static extern void wioe5p2p_exit
(int handle,
out int error);
///
/// Receive a binary message frame, if available.
///
/// Wio-E5 device handle.
/// Payload buffer (253 bytes).
/// Number of payload bytes received.
/// Zero indicates queue empty, no RF frame available.
/// Received Signal Strength in dBm.
/// Signal to Noise Ratio in dB.
/// Error code. Zero upon success.
[DllImport("wioe5p2p")]
public static extern void wioe5p2p_receive
(int handle,
byte[] msg,
out int len,
out int RSS,
out int SNR,
out int error);
///
/// Transmit a binary message frame.
///
/// Wio-E5 device handle.
/// Payload buffer (253 bytes).
/// Number of payload bytes to transmit
/// (1 to 253).
/// Error code. Zero upon success.
[DllImport("wioe5p2p")]
public static extern void wioe5p2p_send
(int handle,
byte[] msg,
int len,
out int error);
///
/// Transmit a string message frame.
///
/// Wio-E5 device handle.
/// Message string (1 to 253 ASCII characters).
/// Error code. Zero upon success.
[DllImport("wioe5p2p")]
public static extern void wioe5p2p_send_string
(int handle,
string msg,
out int error);
}
}