fuse_lutlist_content.cpp

00001
00002 /***************************************************************************
00003  *  fuse_lutlist_content.cpp - FUSE LUT list content encapsulation
00004  *
00005  *  Created: Wed Nov 21 16:33:56 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <fvutils/net/fuse_lutlist_content.h>
00025 #include <netcomm/utils/dynamic_buffer.h>
00026
00027 #include <core/exceptions/software.h>
00028
00029 #include <cstdlib>
00030 #include <cstring>
00031 #include <netinet/in.h>
00032
00033 using namespace fawkes;
00034 
00035 /** @class FuseLutListContent <fvutils/net/fuse_lutlist_content.h>
00036  * FUSE lookup table list content.
00037  * This content provides means to send an arbitrary length list of LUT
00038  * information chunks.
00039  * @author Tim Niemueller
00040  * @ingroup FUSE
00041  * @ingroup FireVision
00042  */
00043 
00044 /** Constructor.
00045  * Creates an empty list.
00046  */
00047 FuseLutListContent::FuseLutListContent()
00048 {
00049   __list = new DynamicBuffer(&(__lutlist_msg.lut_list));
00050
00051   _payload_size = 0;
00052   _payload = NULL;
00053 }
00054
00055 
00056 /** Parsing constructor.
00057  * Can be used with the FuseContent::fmsg() method to get correctly parsed output.
00058  * @param type message type, must be FUSE_MT_LUT_LIST
00059  * @param payload payload
00060  * @param payload_size size of payload
00061  * @exception TypeMismatchException thrown if the type is not FUSE_MT_LUT_LIST
00062  */
00063 FuseLutListContent::FuseLutListContent(uint32_t type, void *payload, size_t payload_size)
00064 {
00065   FUSE_lutlist_message_t *tmsg = (FUSE_lutlist_message_t *)payload;
00066   void *list_payload = (void *)((size_t)payload + sizeof(FUSE_lutlist_message_t));
00067   __list = new DynamicBuffer(&(tmsg->lut_list), list_payload,
00068                              payload_size - sizeof(FUSE_lutlist_message_t));
00069 }
00070
00071 
00072 /** Destructor. */
00073 FuseLutListContent::~FuseLutListContent()
00074 {
00075   delete __list;
00076 }
00077
00078 
00079 /** Add LUT info.
00080  * @param lut_id LUT ID
00081  * @param width width of LUT
00082  * @param height height of LUT
00083  * @param depth depth of LUT
00084  * @param bytes_per_cell bytes per cell
00085  */
00086 void
00087 FuseLutListContent::add_lutinfo(const char *lut_id,
00088                                 unsigned int width, unsigned int height,
00089                                 unsigned int depth, unsigned int bytes_per_cell)
00090 {
00091   FUSE_lutinfo_t lutinfo;
00092   memset(&lutinfo, 0, sizeof(lutinfo));
00093
00094   strncpy(lutinfo.lut_id, lut_id, LUT_ID_MAX_LENGTH);
00095   lutinfo.width = ntohl(width);
00096   lutinfo.height = ntohl(height);
00097   lutinfo.depth  = ntohl(depth);
00098   lutinfo.bytes_per_cell = ntohl(bytes_per_cell);
00099
00100   __list->append(&lutinfo, sizeof(lutinfo));
00101 }
00102
00103 
00104 /** Reset iterator. */
00105 void
00106 FuseLutListContent::reset_iterator()
00107 {
00108   __list->reset_iterator();
00109 }
00110
00111 
00112 /** Check if another LUT info is available.
00113  * @return true if another LUT info is available, false otherwise
00114  */
00115 bool
00116 FuseLutListContent::has_next()
00117 {
00118   return __list->has_next();
00119 }
00120
00121 
00122 /** Get next LUT info.
00123  * @return next LUT info
00124  * @exception TypeMismatchException thrown if the content contained invalid data
00125  * @exception OutOfBoundsException thrown if no more data is available
00126  */
00127 FUSE_lutinfo_t *
00128 FuseLutListContent::next()
00129 {
00130   size_t size;
00131   void *tmp = __list->next(&size);
00132   if ( size != sizeof(FUSE_lutinfo_t) ) {
00133     throw TypeMismatchException("Lut list content contains element that is of an "
00134                                 "unexpected size");
00135   }
00136
00137   return (FUSE_lutinfo_t *)tmp;
00138 }
00139
00140
00141 void
00142 FuseLutListContent::serialize()
00143 {
00144   _payload_size = sizeof(FUSE_lutlist_message_t) + __list->buffer_size();
00145   _payload = malloc(_payload_size);
00146
00147   copy_payload(0, &__lutlist_msg, sizeof(FUSE_lutlist_message_t));
00148   copy_payload(sizeof(FUSE_lutlist_message_t), __list->buffer(), __list->buffer_size());
00149 }