#include "mag_hskp.h"
#include "df.h"

static int32 vdata_id_r, vdata_id_w;
/*     init write function    */

int32 init_wr_mag_hskp(int32 hdf_fp, int32 sd_id, char *classname)
{
  int32 retval=0;
  void mag_hskp_error();

  if ((vdata_id_w = VSattach(hdf_fp, -1, "w"))==FAIL) {
    fprintf(stderr,"VSattach: Could not attach hdf_fp\n");
    retval=-1;
  }
  VSsetname(vdata_id_w, "mag_hskp");
  VSsetclass(vdata_id_w, classname);

   if (VSfdefine(vdata_id_w, "mag_hskp_sctime", DFNT_UINT32, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_sctime error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_QAC", DFNT_UINT32, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_QAC error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st1a", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st1a error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st2a", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st2a error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st3", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st3 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st4", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st4 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st5", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st5 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st6", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st6 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_pctemp", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_pctemp error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_cmon", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_cmon error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st1b", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st1b error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st2b", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st2b error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_hk1", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_hk1 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_hk2", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_hk2 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_hk3", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_hk3 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_hk4", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_hk4 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_hk5", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_hk5 error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_sync", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_sync error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_st", DFNT_UINT16, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_st error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_in_temp", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_in_temp error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_out_temp", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_out_temp error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_in_pwrlvl", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_in_pwrlvl error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_out_pwrlvl", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_out_pwrlvl error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_current", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_current error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_SenIntTempPosY", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_SenIntTempPosY error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_SenIntTempNegY", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_SenIntTempNegY error");
     retval = -1;
   }
   if (VSfdefine(vdata_id_w, "mag_hskp_MagPowBits", DFNT_UINT8, (1) )) {
     mag_hskp_error("VSfdefine mag_hskp_MagPowBits error");
     retval = -1;
   }

  if (VSsetfields(vdata_id_w,"mag_hskp_sctime, mag_hskp_QAC, mag_hskp_st1a, mag_hskp_st2a, mag_hskp_st3, mag_hskp_st4, mag_hskp_st5, mag_hskp_st6, mag_hskp_pctemp, mag_hskp_cmon, mag_hskp_st1b, mag_hskp_st2b, mag_hskp_hk1, mag_hskp_hk2, mag_hskp_hk3, mag_hskp_hk4, mag_hskp_hk5, mag_hskp_sync, mag_hskp_st, mag_hskp_in_temp, mag_hskp_out_temp, mag_hskp_in_pwrlvl, mag_hskp_out_pwrlvl, mag_hskp_current, mag_hskp_SenIntTempPosY, mag_hskp_SenIntTempNegY, mag_hskp_MagPowBits")){
    mag_hskp_error("VSsetfields error");
    retval = -1;
  }
  return(retval);
}

/****** write function ******/

int32 write_mag_hskp(struct MagHskpSet MagHskpSet_struk)
{
int32 retval = 0;
uint8 *odata;
static int32 recnum_wr=0;

void pack_mag_hskp();

  odata = (uint8 *) malloc(sizeof(struct MagHskpSet));
  pack_mag_hskp(odata, &MagHskpSet_struk);

  if( VSwrite(vdata_id_w, (uint8 *)odata, 1, FULL_INTERLACE) == -1) {
    fprintf(stderr,"VSwrite: Problem writing MAG HSKP data\n");
  } 
  memset(&MagHskpSet_struk, 0, sizeof(struct MagHskpSet));
  free(odata);
  return(retval);
}

void close_wr_mag_hskp() {
  VSdetach(vdata_id_w);
}

/*     init read function    */

int32 init_rd_mag_hskp(int32 hdf_fp, int32 sd_id)
{
  int32 vdata_ref;
  int32 retval=0;

  void mag_hskp_error();


  if ((vdata_ref = VSfind(hdf_fp, "mag_hskp"))==FAIL) {
    fprintf(stderr,"VSfind: Could not get vdata_ref\n");
    retval=-1;
  }
  if ((vdata_id_r = VSattach(hdf_fp, vdata_ref, "r"))==FAIL) {
    fprintf(stderr,"VSattach: Could not attach hdf_fp\n");
    retval=-1;
  }

  if (VSsetfields(vdata_id_r,"mag_hskp_sctime, mag_hskp_QAC, mag_hskp_st1a, mag_hskp_st2a, mag_hskp_st3, mag_hskp_st4, mag_hskp_st5, mag_hskp_st6, mag_hskp_pctemp, mag_hskp_cmon, mag_hskp_st1b, mag_hskp_st2b, mag_hskp_hk1, mag_hskp_hk2, mag_hskp_hk3, mag_hskp_hk4, mag_hskp_hk5, mag_hskp_sync, mag_hskp_st, mag_hskp_in_temp, mag_hskp_out_temp, mag_hskp_in_pwrlvl, mag_hskp_out_pwrlvl, mag_hskp_current, mag_hskp_SenIntTempPosY, mag_hskp_SenIntTempNegY, mag_hskp_MagPowBits")) {
    mag_hskp_error("VSsetfields error");
  }
  return(retval);
}

/****** read function ******/

int32 read_mag_hskp(struct MagHskpSet *MagHskpSet_struk, int32 recnum_rd)
{
static int32 last_recnum = -1;
int32 maxrec;
int32 retval = 0;
uint8 *odata;

void unpack_mag_hskp();

  odata = (uint8 *) malloc(sizeof(struct MagHskpSet));
  VSinquire(vdata_id_r, &maxrec, NULL, NULL, NULL, NULL);
  if (recnum_rd >= maxrec || last_recnum>=maxrec-1) return(-1);
  if (recnum_rd >= 0) {
    last_recnum = recnum_rd;
    if (VSseek(vdata_id_r, recnum_rd)==FAIL) {
      fprintf(stderr,"VSseek unsuccessful\n");
      retval = -1;
    }
  } else {
    recnum_rd = ++last_recnum;
  }
  if(VSread(vdata_id_r, (uint8 *)odata, 1, FULL_INTERLACE)==FAIL) {
    fprintf(stderr,"VSread: Could not read\n");
    retval = -1;
  }
  unpack_mag_hskp(odata, MagHskpSet_struk);
  return(retval);
}

void mag_hskp_error(int8 *mess)
  { fprintf(stderr,"MAG HSKP INIT: %s\n",mess); }

void close_rd_mag_hskp() {
  VSdetach(vdata_id_r);
}
/*   pack function    */

void pack_mag_hskp(uint8 *data, struct MagHskpSet *MagHskpSet_ptr)
{
int32 ptr=0;

   memcpy(data+ptr, &MagHskpSet_ptr->sctime, ((4)*(1)) );
   ptr+= ((4)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->QAC, ((4)*(1)) );
   ptr+= ((4)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st1a, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st2a, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st3, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st4, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st5, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st6, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->pctemp, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->cmon, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st1b, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st2b, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->hk1, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->hk2, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->hk3, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->hk4, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->hk5, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->sync, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->st, ((2)*(1)) );
   ptr+= ((2)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->in_temp, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->out_temp, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->in_pwrlvl, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->out_pwrlvl, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->current, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->SenIntTempPosY, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->SenIntTempNegY, ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(data+ptr, &MagHskpSet_ptr->MagPowBits, ((1)*(1)) );
   ptr+= ((1)*(1));
}

/*   unpack function    */

void unpack_mag_hskp(uint8 *data, struct MagHskpSet *MagHskpSet_ptr)
{
int32 ptr=0;

   memcpy(&MagHskpSet_ptr->sctime, data+ptr,  ((4)*(1)) );
   ptr+= ((4)*(1));
   memcpy(&MagHskpSet_ptr->QAC, data+ptr,  ((4)*(1)) );
   ptr+= ((4)*(1));
   memcpy(&MagHskpSet_ptr->st1a, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st2a, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st3, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st4, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st5, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st6, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->pctemp, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->cmon, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st1b, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st2b, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->hk1, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->hk2, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->hk3, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->hk4, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->hk5, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->sync, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->st, data+ptr,  ((2)*(1)) );
   ptr+= ((2)*(1));
   memcpy(&MagHskpSet_ptr->in_temp, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->out_temp, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->in_pwrlvl, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->out_pwrlvl, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->current, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->SenIntTempPosY, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->SenIntTempNegY, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
   memcpy(&MagHskpSet_ptr->MagPowBits, data+ptr,  ((1)*(1)) );
   ptr+= ((1)*(1));
}
