/* Sense/Net Bus Interface Library for Raspberry Pi. * Sensiplicity Systems * * Copyright 2015 * * Department of Botany and Plant Pathology * Center for Genome Research and Biocomputing * Oregon State University * Corvallis, OR 97331 * * This program is not free software; you can not redistribute it and/or * modify it at all. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ #ifndef SENSE_NET_H #define SENSE_NET_H #include #include /* This library interfaces to the Sense/Net bus, a bus protocol * for connecting large numbers of environmental sensors. It's designed to * work with an RS-485 transceiver connected to a serial port, with the RS-485 * DE/RE_n line connected to the DTR signal. When DTR is up, receive is enabled. * When DTR is down, transmit is enabled. */ /* Open the serial port on the raspberry pi bridge. * Returns 0 on success, -1 on error. Errors messages are printed to stdout. */ int sn_init(); /* Node ID datatype. */ typedef uint8_t node_t[9]; /* Get sensor data from a node. * The structure of the data is defined by the node type. * bus is the bus the node is located on. * *node is the node to get data from. * *data will be filled with the data from the node. It should be able to contain * 128 bytes. * *size will be set to the number of bytes in data. * If an error occurs, *data is not set and *size is set to 0. * Returns 0 on success, -1 on bus error, -2 on node error. */ int sn_get_data(int bus, node_t node, char *data, int *size); /* Send a packet to a node. If the node replies with a packet, it will * be placed into *reply_data. * bus is the bus the node is located on. * cmd is the header command number. Allowed range is 0-7. * *node is the node to send the packet to. * *data is the data to be sent. * data_size is the size of the data to be sent (max. 128). * *status will be set to the status code received from the packet, if any. * *reply_data will be filled with data from the reply packet. It must * be able to hold 128 bytes. * *reply_size will be set to the size of the reply. * *status, *reply_data, and *reply size may be NULL. If *reply_size is NULL, * *reply_data will not be set. *data may be null if data_size is 0. * Returns 0 on success (no reply), 1 on success (reply), * -1 on I/O error, -2 on corrupted reply */ int sn_send_packet(int bus, int cmd, node_t node, const char *data, int data_size, int *status, char *reply_data, int *reply_size); /* Send a packet to a node. If the node replies with a packet, it will * be placed into *reply_data. If a reply is not received, the request will be re-tried up to * two more times. * bus is the bus the node is located on. * cmd is the header command number. Allowed range is 0-7. * *node is the node to send the packet to. * *data is the data to be sent. * data_size is the size of the data to be sent (max. 128). * *status will be set to the status code received from the packet, if any. * *reply_data will be filled with data from the reply packet. It must * be able to hold 128 bytes. * *reply_size will be set to the size of the reply. * *status, *reply_data, and *reply size may be NULL. If *reply_size is NULL, * *reply_data will not be set. *data may be null if data_size is 0. * Returns 0 on success (no reply), 1 on success (reply), * -1 on I/O error, -2 on corrupted reply */ int sn_send_packet_r(int bus, int cmd, node_t node, const char *data, int data_size, int *status, char *reply_data, int *reply_size); /* Struct containing a node ID and type code. */ typedef struct node_enum_t { int bus; /* The bus the node is on. */ node_t id; /* Node ID. */ uint16_t type; /* Node typecode. */ } node_enum_t; /* Discover the nodes on the bus. * bus is the bus the node is located on. * **nodes will be set to point to an array of node_enum_ts. * Free() this array when you are done with it. * *num_nodes will be set to the number of node IDs in the array. */ void sn_enumerate(int bus, node_enum_t **nodes, int *num_nodes); /* Shutdown the library and close the serial port. */ void sn_shutdown(void); /* Return a human-readable description of a node typecode. */ const char *sn_type_str(uint16_t typecode); /* Calculate CRC of a Sense/Net packet. packet[0] is the header. * If this function is computed over a whole packet, including the * CRC at the end, a return value of 0 indicates a valid packet. */ uint16_t calc_crc(uint8_t *packet, int packet_length); /* Extract a uint16_t from a little-endian data packet. * Index is the index of the first byte of the field in the packet data. */ uint16_t sn_uint16(uint8_t *packet_data, uint8_t index); /* Extract a uint32_t from a little-endian data packet. * Index is the index of the first byte of the field in the packet data. */ uint32_t sn_uint32(uint8_t *packet_data, uint8_t index); #endif