// SippPacket.hh // This file is part of SlugBee; see http://chezphil.org/slugbee // (C) 2006-2007 Philip Endecott // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // any later version. // // 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. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef SippPacket_hh #define SippPacket_hh // "SIPP" is a packet format used for communication with DLP ZigBee // modules. See the DLP module datasheets for details. #include "FileDescriptor.hh" #include #include #include #include typedef uint16_t SippID; struct SippPacket { SippID destination_id; SippID source_id; uint8_t command; typedef std::string data_t; data_t data; void send(pbe::FileDescriptor& fd) { fd.binwrite(5+data.size()); fd.binwrite(htons(destination_id)); fd.binwrite(htons(source_id)); fd.binwrite(command); fd.write(data); } class UnderLength: public pbe::StrException { public: UnderLength(): pbe::StrException("SIPP packet is under-length") {} }; void rcv(pbe::FileDescriptor& fd) { const uint8_t length = fd.binread(); if (length<5) { throw UnderLength(); } destination_id = ntohs(fd.binread()); source_id = ntohs(fd.binread()); command = fd.binread(); const int data_length = length-5; if (data_length>0) { data = fd.readall(data_length); } } }; inline std::ostream& operator<<(std::ostream& strm, const SippPacket& pkt) { strm << "source: " << pkt.source_id << " destination: " << pkt.destination_id << " command: " << std::hex << static_cast(pkt.command) << " data: "; for (SippPacket::data_t::const_iterator i=pkt.data.begin(); i!=pkt.data.end(); ++i) { strm << std::hex << static_cast((*i)&0xff) << " "; } return strm; } #endif