/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#ifndef INCLUDED_PacketDecoder_h
#define INCLUDED_PacketDecoder_h

#include <string.h>

#include "../../../agent/headers/CircBufPacket.h"
#include "../../../agent/headers/DogTypes.h"

static const int MaxPacketSize=100*1024;

/*------------------------------------------------------------------
CLASS
  PacketDecoder

DESCRIPTION
  Support routines for decoding packets.
------------------------------------------------------------------*/
class PacketDecoder {
public:
  static const uchar VisionRLELead      =(uchar)'\xBF';
  static const uchar TextLead           =(uchar)'\xBE';
  static const uchar VisionObjLead      =(uchar)'\xBD';
  static const uchar LocalizationLead   =(uchar)'\xBC';
  static const uchar ModelObjLead       =(uchar)'\xBB';
  static const uchar MotionUpdateLead   =(uchar)'\xBA';
  static const uchar GSensorLead        =(uchar)'\xB9';
  static const uchar VisionRawLead      =(uchar)'\xB8';
  static const uchar VisionAvgColorLead =(uchar)'\xB7';
  static const uchar VisionColorAreaLead=(uchar)'\xB6';
  static const uchar VisionRadialMapLead=(uchar)'\xB5';
  static const uchar ModelRadialMapLead =(uchar)'\xB4';
  static const uchar SharedModelObjLead =(uchar)'\xB3';
  static const uchar FootSensorLead     =(uchar)'\xB2';
  static const uchar BehaviorNamesLead  =(uchar)'\xB1';
  static const uchar BehaviorTraceLead  =(uchar)'\xB0';
  static const uchar DutyCycleLead      =(uchar)'\xAF';
  static const uchar VisionRLE2Lead     =(uchar)'\xAE';
  static const uchar VisionRaw2Lead     =(uchar)'\xAD';
  static const uchar TrackerLead        =(uchar)'\xAC';
  static const uchar WMDebugLead        =(uchar)'\xAB';

  PacketDecoder();
  
  static void dumpBinary(uchar *data, int length);

  template<class T>
  static int countBits(T val) {
    int cnt=0;
    for(unsigned int i=0; i<sizeof(T)*8; i++)
      if((1 << i) & val)
        cnt++;
    return cnt;
  }

  static void grabFromEncoding(uchar **buf,uchar *dest,int length) {
    memcpy(dest,*buf,length);
    (*buf)+=length;
  }

  template<class T>
  static T grab(uchar **buf) {
    T tmp;
    grabFromEncoding(buf,(uchar *)&tmp,sizeof(T));
    return tmp;
  }

  bool isPrintable(char c);

private:
  uchar buf_8bit[8];
  uchar buf_7bit[8];
};

#endif
