#ifndef _H_CAMREAL
#define _H_CAMREAL 1

#include <termios.h>
#include <time.h>

#include "buffer2d.h"

#define RC_OK                0
#define RC_ERROR             1
#define RC_NO_FILE           2
#define RC_BAUD_SET          3
#define RC_TIMEOUT           4
#define RC_SELECT            5
#define RC_CAMERA_GET        6
#define RC_NOT_READY         7
#define RC_CAMERA_PUT        8
#define RC_GETACK            9
#define RC_ILLEGAL_PACKET   10
#define RC_CAMERA_GETPACKET 11
#define RC_GET_LENGTH       12
#define RC_PACKET_BODY      13
#define RC_CHECKSUM         14
#define RC_GET_ID           15
#define RC_GET_MODEL        16     
#define RC_GET_LENS         17
#define RC_GET_FLASH        18
#define RC_GET_QUALITY      19
#define RC_GETPICTS         20
#define RC_GETNPICTS        21
#define RC_GETINT_PAKET     22
#define RC_SETINT_PAKET     23
#define RC_GETSTR_PAKET     24
#define RC_UNSUPPORTED_BAUD 25
#define RC_GETPICT_PAKET    26
#define RC_WRONG_ID         27
#define RC_ILLEGAL_DEVICE   28
#define RC_ABORT            29
#define RC_DATEFILE         30
#define RC_GET_DATE         31
#define RC_GETBATTERY       32
#define RC_GETSERIAL        33
#define RC_GETMEMORY        34
#define RC_GETPOWEROFF      35
#define RC_GETBRIGHTNESS    36
#define RC_SET_DATE         37
#define RC_SETPOWEROFF      38
#define RC_SETBRIGHTNESS    39
#define RC_SET_LENS         40
#define RC_SET_FLASH        41
#define RC_SET_QUALITY      42
#define RC_GETDONE          43
#define RC_NO_IMAGE_TAKEN   44

class CameraApplicationClass;

typedef bool (*CameraRealProgressCB) (void *, float);

#define CameraOptionID               0x001
#define CameraOptionType             0x002
#define CameraOptionSerial           0x004
#define CameraOptionBattery          0x008
#define CameraOptionMemory           0x010
#define CameraOptionClock            0x020
#define CameraOptionShutdownCamera   0x040
#define CameraOptionShutdownLCD      0x080
#define CameraOptionShutdownComputer 0x100
#define CameraOptionBrightness       0x200
#define CameraOptionQuality          0x400
#define CameraOptionLens             0x800
#define CameraOptionFlash            0x1000
#define CameraOptionAll              0xffffffff

class CameraRealOptionGetClass
{
   public:
   
   char ID[255];
   char Type[255];
   char Serial[255];
   int  Battery;
   int  Memory;
   time_t Clock;
   int  ShutdownCamera,ShutdownLCD,ShutdownComputer;
   int  Brightness;
   int  Quality;
   int  Lens;
   int  Flash;
};

class CameraRealClass
{
   public:

      CameraRealClass(CameraApplicationClass * application);
      ~CameraRealClass(){};


      int GetNumberOfImages(int *numberOfImages);
   
      int LoadAllImages();
      int LoadImage(int index,char *filename);

      int LoadThumbNail(int index,buffer2d **image);
   
      int IsCameraDevice(char *deviceName);
      
      void SetProgressCallback(CameraRealProgressCB ProgressCB,
                            void * ProgressData);   
      int GetOptions(int OptionMask,CameraRealOptionGetClass &Optionen);

      int SetOptions(int OptionMask,CameraRealOptionGetClass &Optionen);

      int GetTime(time_t *camediaZeit,bool Set=FALSE);
      int DeleteAllImages();
      int DeleteSingleImage(int image[],int anzahl);
      int MakeImage(char *filename);
      bool AutoSetTime;
   
   private:
   
   CameraApplicationClass *cameraApplication;   
   int deviceHandle;

   int fd0;
   int baudrateflag ;
   int dumpmaxlen ;
   int openflag ;

   int verbose;

   char camedia_buf[1024 * 4];
   int camedia_len;

   CameraRealProgressCB ProgressCB;
   void * ProgressData;
   
   speed_t baudconv(int baud);
   int setbaud(int fd, int baud);
   void dump_stream(int dir, char *buf, int len);
   int  camedia_init();
   int camedia_wait(int rlen);
   int camedia_get(char *buf, int len);
   int camedia_put(char * buf, int len);
   int camedia_attention();
   int camedia_checkDevice();
   int camedia_getack();
   int camedia_getDone();
   int camedia_sendcmd(int ch, char *buf, int len);
   int camedia_getpacket(char *buf, int *plen, int *pfinal, int *ack, int *sector);
   int camedia_getint(int cmd, int *n);
   int camedia_setint(int cmd, int n);
   int camedia_getstr(int cmd, char *buf, int *plen);
   int camedia_setspeed(int baud);
   int camedia_checkcamera();
   int camedia_getnpicts(int *n);
   int camedia_getnpictremain(int *n);
   int camedia_getpict(int n, int thumbnail, int fd);
   int camedia_getstatus();
   int camedia_deleteAllImages();
   int camedia_deleteSingleImage(int image);
   int camedia_pushShutter();

   int camediaOpenDevice(char *deviceName,int speed,int *fd);
   int camediaCloseDevice(int fd);

   int camediaSetDateToFile(char *filename);
   int camediaGetDate(time_t *camediaZeit,bool Set=FALSE);
   int camedia_getoptions(int OptionMask,CameraRealOptionGetClass &Optionen);
   int camedia_setoptions(int OptionMask,CameraRealOptionGetClass &Optionen);
   int camedia_setAutoTime();
};

#endif
