Header
Product Comparison: USB I2C | USB SPI | USB GPIO
  • My Account
  • Shopping Cart
  • Products
  • My Account
  • About
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

main.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Diolan                                          *
00003  *   www.diolan.com                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "osdep/osdep.h"
00021 
00022 #include "encoder.h"
00023 #include "parser/parser.h"
00024 #include "parser/parameters.h"
00025 #include "encoder_usage.h"
00026 #include "image/argumentimage.h"
00027 #include "image/binimage.h"
00028 #include "image/intel_hex_image.h"
00029 #include "image/cout_image.h"
00030 #include "exception/exception.h"
00031 #include "encoder_buffer.h"
00032 
00033 Image* createInputImage(const Parameters& params)
00034 {
00035  if (params.contain(ARG_INPUT_BIN))
00036   return new BinImage(params[ARG_INPUT_BIN].value());
00037  if (params.contain(ARG_INPUT_DATA))
00038   return new ArgumentImage(ARG_INPUT_DATA, params);
00039  if (params.contain(ARG_INPUT_HEX))
00040   return new IntelHexImage(params[ARG_INPUT_HEX].value());
00041 
00042  throw DEImageAbsent();
00043 // return new CoutImage();
00044 
00045 }
00046 
00047 Image* createOutputImage(const Parameters& params)
00048 {
00049  if (params.contain(ARG_OUTPUT_BIN))
00050   return new BinImage(params[ARG_OUTPUT_BIN].value());
00051  if (params.contain(ARG_OUTPUT_HEX))
00052   return new IntelHexImage(params[ARG_OUTPUT_HEX].value());
00053 
00054  return new CoutImage();
00055 
00056 }
00057 
00058 void save(FragBuffer *buffer, const Parameters& params)
00059 {
00060  Image *image = NULL;
00061  try
00062  {
00063   image = createOutputImage(params);
00064   image->open(false);
00065   const size_t dataSize = 0x100;
00066   unsigned char data[dataSize];
00067   size_t size, address;
00068 
00069   FragBuffer::iterator it = buffer->begin();
00070   while (it != buffer->end())
00071   {
00072    address = buffer->address(it);
00073    size = buffer->read(data, dataSize, address);
00074 //    printf("%08X : %08X\n", address, size);
00075    image->setWriteAddress(address);
00076    image->write(data, size);
00077    it = buffer->lower_bound(address + dataSize);
00078   }
00079 
00080   image->close();
00081   delete image;
00082   image = NULL;
00083  }
00084  catch(...)
00085  {
00086   if (image != NULL)
00087    delete image;
00088   throw;
00089  }
00090 
00091 }
00092 
00093 void load(FragBuffer* buffer, const Parameters& params, size_t *start, size_t *end)
00094 {
00095  Image *image = NULL;
00096  const size_t dataSize = 0x100;
00097  unsigned char data[dataSize];
00098  *start = 0xFFFFFFFF;
00099  *end = 0x0;
00100 
00101  try
00102  {
00103   image = createInputImage(params);
00104   image->open(true);
00105   buffer->clear();
00106 
00107   size_t size, address;
00108   while (0 != (size = image->read(data, dataSize, &address)))
00109   {
00110 //    printf("%08X : %08X\n", address, size);
00111    buffer->write(data, size, address);
00112    if (*end < address+size)
00113     *end = address+size;
00114    if (*start > address)
00115     *start = address;
00116   }
00117   image->close();
00118   delete image;
00119  }
00120  catch(...)
00121  {
00122   if (image != NULL)
00123    delete image;
00124   throw;
00125  }
00126 }
00127 
00128 void getKey(const Parameters& params, const unsigned int argument, XTEA_KEY_T key[4])
00129 {
00130  Image * image;
00131  size_t address, size;
00132  try
00133  {
00134   image = new ArgumentImage(argument, params);
00135   image->open(true);
00136   if (image->getSize() != 16)
00137    throw DEBadArgument(params[argument].argument());
00138   size = image->read((unsigned char*)key, 16, &address);
00139   if (size != 16)
00140    throw DEBadArgument(params[argument].argument());
00141   image->close();
00142   delete image;
00143  }
00144  catch(...)
00145  {
00146   delete image;
00147   throw;
00148  }
00149 }
00150 
00151 int main (int argc, char *argv[])
00152 {
00153  try
00154  {
00155   EncoderUsage usage;
00156   Parser parser(usage);
00157   Parameters params(usage);
00158   parser.parse(argc, argv, &params);
00159   if (params.contain(ARG_HELP))
00160   {
00161    cout << "USAGE:\n";
00162    cout << "encoder <COMMAND> [OPTIONS...]\n";
00163    usage.print();
00164    return 0;
00165   }
00166 
00167   EncoderBuffer buffer;
00168   size_t begin, end;
00169   load(&buffer, params, &begin, &end);
00170   XTEA_KEY_T key[4];
00171   unsigned int command = params.command();
00172   switch(command)
00173   {
00174   case ARG_CMD_ENCODE:
00175    getKey(params, ARG_CMD_ENCODE, key);
00176    buffer.encode(key);
00177    break;
00178   case ARG_CMD_DECODE:
00179    getKey(params, ARG_CMD_DECODE, key);
00180    buffer.decode(key);
00181    break;
00182   case ARG_CMD_CONVERT:
00183    break;
00184   default:
00185    throw DEBadArgument(params[command].argument());
00186   }
00187 
00188   save(&buffer, params);
00189  }
00190  catch(DException &err)
00191  {
00192   cout << err.getErrMessage() << endl;
00193   cout << "Operation aborted.\n";
00194   return -1;
00195  }
00196  catch (...)
00197  {
00198   return -1;
00199  }
00200 
00201 
00202 
00203 }
  • Products
  • PC-I2C/SPI/GPIO Adapters
  • Comparison
  • PC-I2C/SPI/GPIO Adapter DLN-1
  • USB-I2C/SPI/GPIO Adapter DLN-2
  • Multiprotocol Master DLN-4M
  • Multiprotocol Slave DLN-4S
  • Downloads
  • Software
  • Documentation
  • SW Tools & Examples
  • Development Boards
  • Open Source Projects
  • Discontinued Products
  • API Documentation
  • Downloads
  • SW Tools and Examples
footer top footer bottom
© Copyright Diolan 2006-2012