src.dualinventive.com/jjacobs/dinetrpcll-sniffer/libdi/include/di/drv/xbee.h

153 lines
5.1 KiB
C
Executable File

/**
* @file include/di/drv/xbee.h
* @defgroup XBee
* @brief DI XBee driver modules
* @date March 30, 2017
* @author svlies
* @copyright 2017 Dual Inventive Technology Centre B.V.
*
* DI XBee driver modules
* @{
*/
#ifndef LIBDI_INCLUDE_DI_DRV_XBEE_H_
#define LIBDI_INCLUDE_DI_DRV_XBEE_H_
#include <di/buffer.h>
#include <di/device/uid.h>
#ifdef __cplusplus
extern "C" {
#endif
struct di_drv_xbee_ctx;
struct di_drv_xbee_frame;
#define DI_DRV_XBEE_ADRR_LENGTH 17
#define DI_DRV_XBEE_CMD_HEADER_SIZE 4
#define DI_DRV_XBEE_REMOTE_CMD_HEADER_SIZE 15
/**
* Xbee sleep options.
*/
enum di_drv_xbee_sleep_options {
DI_DRV_XBEE_SLEEP_OPTIONS_SLEEP_COORDINATOR = 1, /**<Set module as sleep coordinator.*/
DI_DRV_XBEE_SLEEP_OPTIONS_NON_SLEEP_COORDINATOR = 2, /**<Set module as non-sleep coordinator.*/
DI_DRV_XBEE_SLEEP_OPTIONS_NORMAL_WITH_API = 4, /**<Set module as normal module.*/
DI_DRV_XBEE_SLEEP_OPTIONS_SLEEP_COORDINATOR_WITH_API = 5 /**<set module as sleep coordinator with API mode on.*/
};
/**
* Xbee sleep mode.
*/
enum di_drv_xbee_sleep_mode {
DI_DRV_XBEE_SLEEP_MODE_NORMAL = 0,/**<set sleep mode to normal(no sleep).*/
DI_DRV_XBEE_SLEEP_MODE_PIN_SLEEP = 1,/**<set sleep mode to sleep with pin wake-up.*/
DI_DRV_XBEE_SLEEP_MODE_ASYNC_CYCLIC_SLEEP = 4,/**<set sleep mode to asynchronous cyclic sleep.*/
DI_DRV_XBEE_SLEEP_MODE_ASYNC_CYCLIC_SLEEP_PIN = 5,/**<set sleep mode to asynchronous cyclic sleep with pin.*/
DI_DRV_XBEE_SLEEP_MODE_SLEEP_SUPPORT_MODE = 7,/**<set sleep mode to sleep support.*/
DI_DRV_XBEE_SLEEP_MODE_SYNCHONOUS_CYCLIC_SLEEP = 8 /**<set sleep mode to synchronous cyclic sleep.*/
};
/**
* Xbee baudrate.
*/
enum di_drv_xbee_baudrate {
DI_DRV_XBEE_BAUDRATE_9K6 = 3, /**<Baudrate 9600.*/
DI_DRV_XBEE_BAUDRATE_19K2 = 4, /**<Baudrate 19200.*/
DI_DRV_XBEE_BAUDRATE_38K4 = 5, /**<Baudrate 38400.*/
DI_DRV_XBEE_BAUDRATE_57K6 = 6, /**<Baudrate 57600.*/
DI_DRV_XBEE_BAUDRATE_115K2 = 7, /**<Baudrate 115200.*/
DI_DRV_XBEE_BAUDRATE_230K4 = 8 /**<Baudrate 230400.*/
};
/**
* Xbee api modes
*/
enum di_drv_xbee_api_modes {
DI_DRV_XBEE_TRANSPARANT_MODE, /**<Set API mode to transparant mode.*/
DI_DRV_XBEE_API_MODE_WITHOUT_ESCAPES, /**<Set API mode to API without escapes.*/
DI_DRV_XBEE_API_MODE_WITH_ESCAPES /**<Set API mode to API with escapes.*/
};
/**
* Xbee context
*/
struct di_drv_xbee_ctx {
uint64_t sn; /**<Serial number of the local XBee radio.*/
uint32_t fw_version; /**<Firmware version of the local XBee radio.*/
uint16_t netid; /**<Current network ID of the local XBee radio.*/
uint8_t frame_id; /**<Frame id counter. */
struct di_buffer buf; /**<DI_buffer for storing frames and data.*/
size_t (*read)(uint8_t *buf); /**<Read function of the XBee driver.*/
size_t (*write)(uint8_t *buf, size_t n);/**<Write function of the XBee driver.*/
};
static inline void di_drv_xbee_set_read_cb(struct di_drv_xbee_ctx *ctx, size_t (*read)(uint8_t *buf))
{
ctx->read = read;
}
static inline void di_drv_xbee_set_write_cb(struct di_drv_xbee_ctx *ctx, size_t (*write)(uint8_t *buf, size_t n))
{
ctx->write = write;
}
/**
* xbee initialize context
* - Resets ALL fields in ctx
*/
void di_drv_xbee_init(struct di_drv_xbee_ctx *ctx);
/**
* Send an AT command to the local XBee radio.
* @param ctx Context of the XBee driver.
* @param at_command The 2 letters of the AT command (see Xbee_defines).
* @return di_errno_t Returns DNOK if successful, else an errorcode.
*/
di_errno_t di_drv_xbee_cmd(struct di_drv_xbee_ctx *ctx, const char *at_command);
/**
* Send an AT command to a remote XBee radio.
* @param ctx Context of the XBee driver.
* @param dst_addr 64 bit destination address of the remote XBee radio.
* @param at_command The 2 letters of the AT command (see Xbee_defines).
* @return Returns DNOK if successful, else an errorcode.
*/
di_errno_t di_drv_xbee_remote_cmd(struct di_drv_xbee_ctx *ctx, uint64_t dst_addr, const char *at_command);
/**
* Check if a frame is available at the read function, and checks the size and checksum of the received frame.
* @param ctx Context of the XBee driver.
* @return Returns true if an correct frame is received.
*/
bool di_drv_xbee_recv_frame(struct di_drv_xbee_ctx *ctx, struct di_drv_xbee_frame *frame);
/**
* Reconstructs the 64 bit MSB or LSB part of the local XBee radio address from the frame to the ctx.
* @param ctx Context of the XBee driver to write to.
* @param frame Frame with xbee frame.
* @return Returns false if incorrect AT command response.
*/
di_errno_t di_drv_xbee_build_sn(struct di_drv_xbee_ctx *ctx, struct di_drv_xbee_frame *frame);
/**
* converts the xbee radio address to an RPC UID.
* @param xbee_mac buffer with the XBee address.
* @param device_uid buffer to write the RPC UID to.
* @return DNOK if successful.
*/
di_errno_t di_drv_xbee_mac_to_device_uid(const char *xbee_mac, char device_uid[DI_DEVICE_UID_LEN]);
#include <di/drv/xbee/frame.h>
#include <di/drv/xbee/cmd.h>
#include <di/drv/xbee/checksum.h>
#include <di/drv/xbee/definitions.h>
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* LIBDI_INCLUDE_DI_DRV_XBEE_H__ */