00001 // HRFDatagram.h 00002 // 00003 // Author: Mike McCauley (mikem@open.com.au) 00004 // Copyright (C) 2009 Mike McCauley 00005 // $Id: HRFDatagram.h,v 1.1 2009/08/15 05:32:58 mikem Exp mikem $ 00006 00007 #ifndef HRFDatagram_h 00008 #define HRFDatagram_h 00009 00010 #include <HRFMessage.h> 00011 00012 // Broadcast address 00013 #define HRF_BROADCAST_ADDRESS 0xff 00014 00015 // The maximum payload length 00016 #define HRF_MAX_DATAGRAM_PAYLOAD HRF_MAX_PAYLOAD-2 00017 00018 ///////////////////////////////////////////////////////////////////// 00019 /// \class HRFDatagram HRFDatagram.h <HRFDatagram.h> 00020 /// \brief Class for addressed, unreliable messages 00021 /// 00022 /// Extends HRFMessage to define addressed, unreliable datagrams. 00023 /// Every node has an 8 bit address (defaults to 0). 00024 /// Datagrams have format LEN DEST SRC payload FCS-LO FCS-HI. 00025 /// Addresses (DEST and SRC) are 8 bit integers with an address of HRF_BROADCAST_ADDRESS (0xff) 00026 /// reserved for broadcast. 00027 /// 00028 /// Part of the Arduino HopeRF library for operating with HopeRF HM-TR transceivers 00029 /// (see http://www.hoperf.com). 00030 class HRFDatagram : public HRFMessage 00031 { 00032 protected: 00033 /// The address of this node. Defaults to 0. 00034 uint8_t _thisAddress; 00035 00036 public: 00037 /// \param[in] serial The instance of HardwareSerial to use for IO. Defaults to &Serial, 00038 /// \param[in] thisAddress The address to assign to this node. Defaults to 0 00039 HRFDatagram(HardwareSerial* serial = &Serial, uint8_t thisAddress = 0); 00040 00041 /// Sets the address of this node. Defaults to 0. If all the nodes leave the address unset (ie 0), 00042 /// HRFDatagram acts much the same as HRFMessage (with a bit more overhead). 00043 /// In a conventional multinode system, all nodes will have a unique address (which you could store in EEPROM). 00044 /// However, if you only have 2 nodes in the network, 00045 /// you can leave the addresses of each set to the default of 0. 00046 /// \param[in] thisAddress The address of this node 00047 virtual void setThisAddress(uint8_t thisAddress); 00048 00049 /// Sends a message to the node(s) with the given address 00050 /// HRF_BROADCAST_ADDRESS is a valid address which will cause the message 00051 /// to be accepted by all HRFDatagram nodes within range. 00052 /// \param[in] address The address to send the message to. 00053 /// \param[in] buf Pointer to the binary message to send 00054 /// \param[in] len Number of octets to send 00055 /// \return true if the message was transmitted. 00056 /// \return false if the message is too long (>HRF_MAX_DATAGRAM_PAYLOAD). 00057 virtual uint8_t sendto(uint8_t address, uint8_t* buf, uint8_t len); 00058 00059 /// If there is a message available for this node, copy it to buf and return true 00060 /// If the message was valid (ie good FCS), return true else return false. 00061 /// The SRC address is placed in *from if present and not NULL. 00062 /// The DEST address is placed in *to if present and not NULL. 00063 /// If promiscuous is present and true, all messages will be returned, not just those addressed 00064 /// to this node. 00065 /// If a message is copied, *len is set to the length. 00066 /// \param[in] buf Location to copy the received message 00067 /// \param[in] len Available space in buf. Set to the actual number of octets copied. 00068 /// \param[in] from If present and not NULL, the referenced uint8_t will be set to the SRC address 00069 /// \param[in] to If present and not NULL, the referenced uint8_t will be set to the DEST address 00070 /// \param[in] promiscuous If present and true, return all received messages 00071 /// (not just those addressed to this node). 00072 /// \return true if a valid (good FCS) message was copied to buf 00073 virtual uint8_t recvfrom(uint8_t* buf, uint8_t* len, uint8_t* from = NULL, uint8_t* to = NULL, uint8_t promiscuous = 0); 00074 00075 /// If there is a valid message available for this node, copy it to buf and return true 00076 /// else return false. You can't recover the SRC or DEST address with this call, so you would only use it 00077 /// if you do not intend to reply. 00078 /// If a message is copied, *len is set to the length. 00079 /// \param[in] buf Location to copy the received message 00080 /// \param[in] len Available space in buf. Set to the actual number of octets copied. 00081 /// \return true if a valid message was copied to buf 00082 virtual uint8_t recv(uint8_t* buf, uint8_t* len); 00083 00084 }; 00085 00086 #endif