#include "CommandPkg.h"
#include "time.h"

CommandPkg::CommandPkg(int a, const char command[8], time_t when, typeOfCommand cmdType, bool ack) : TelepatiPkg(){
  type=COMMAND;
  aibo=a;
  needAck=ack;
  _when=when;
  _cmdType=cmdType;
  strcpy(_command, command);
}

CommandPkg::CommandPkg(byte* ptr) : TelepatiPkg(ptr){
  _cmdType=(typeOfCommand)(ptr[STD_HEADER_SIZE] == 1? LEGS: HEAD);
  memcpy( _command, (ptr+STD_HEADER_SIZE+1), 8);
  memcpy( &_when, (ptr+STD_HEADER_SIZE+1+8), sizeof(_when));
}


CommandPkg::~CommandPkg(){
}

TelepatiPkg* CommandPkg::clone(){
  CommandPkg* clone=new CommandPkg(aibo, _command, _when, _cmdType, needAck);
  clone->serialNum=serialNum;
  return clone;
}



byte* CommandPkg::getMsg(){
  delete [] message;
  // dimensione di _when + 1byte (typeofcommand) + 8 byte (_command)
  size_t dim=sizeof(_when)+1+8;
  message=createStdHeader(dim);
  *(message+STD_HEADER_SIZE)=(byte)(_cmdType == LEGS? 1: 0);
  memcpy( (message+STD_HEADER_SIZE+1), _command, 8);
  memcpy( (message+STD_HEADER_SIZE+1+8), &_when, sizeof(_when));

  return message;
}

