#include "sender.h"
 
#define binary(bit) strtol(bit,NULL,2)
 
 
 
void SenderInit(){};
 
 
 
void SenderClose(){};
 
 
 
//------------------------------------------------------------------------------------------------------
 
// Send 1 data & address by UDP                                                                                                                                                  
 
void Senderudp_send( unsigned int address, int data){// data: send only 1 data length
 
  SiTCPRBCPskeleton(0xff, 0x80, address, 0x1, address);
 
  SiTCPsndData[0] = data;
 
 
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  int len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
}
 
   SiTCPrcvRBCP_ACK(1);
 
}
 
 
 
//------------------------------------------------------------------------------------------------------  
 
// Recv 1 data & address by UDP
 
 
 
int Senderudp_recv( unsigned int address, int data){// data: send only 1 data length
 
 
 
    SiTCPRBCPskeleton(0xff, 0xc0, address, 0x1, address);
 
  
 
  int len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
 
 
  int rd_data = 0;
 
  rd_data =  SiTCPrcvRBCP_ACK(1);
 
  // std::cout << rd_data << std::endl;
 
 
 
  return rd_data;
 
}
 
 
 
//------------------------------------------------------------------------------------------------------ 
 
// Send 1 packet data & address by UDP
 
 
 
void SenderRBCPpacket_send( unsigned int address,
 
                             unsigned char length, int* data){
 
  
 
  printf("enter packet function\n");  
  
 
  SiTCPRBCPskeleton(0xff, 0x80, address, length, address);
 
  
 
  for(int i=0; i<256; ++i){
 
    SiTCPsndData[i] = data[i];// <- sndData: 0~255
 
  }
 
 
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  
 
  int len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
 
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(0);
 
}
 
 
 
//------------------------------------------------------------------------------------------------------ 
 
// Recv 1 packet data & address by UDP
 
 
 
void SenderRBCPpacket_recv( unsigned int address,
 
                             unsigned char length, int* data){
 
  
 
  printf("enter packet function\n");  
  
 
  SiTCPRBCPskeleton(0xff, 0xc0, address, length, address);
 
 
 
  
 
  int len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
 
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(1);
 
}
 
//------------------------------------------------------------------------------------------------------   
 
//=============== multi packet sending method ===============
 
 
 
void SenderRBCP_multi_packet_send( unsigned int address,
 
                                    unsigned int total_length, int* data){
 
 
 
  unsigned int total_packet = ((total_length/255)+1)*2;
 
  unsigned int packetNo = 0;
 
 
 
  printf("------------------ RBCP : register access ------------------\n");      
  printf("Start sending packet data");      
 
 
  for(packetNo=0; packetNo<total_packet; ++packetNo){
 
 
 
    //    SiTCPrcvRBCP_ACK(0);
 
    SiTCPRBCPskeleton(0xff, 0x80, address+(packetNo*255), 0xff, address+(packetNo*255));
 
      
 
    for(int i=0; i<255; ++i){
 
      SiTCPsndData[i] = data[i+(packetNo*255)];// <- sndData: 0~255
 
    }
 
    
 
    memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  
 
    int len = 0;
 
    len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, sizeof SiTCPsndData + sizeof(struct bcp_header), 0,
 
                 (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
 
 
    if(len < 0){
 
      perror("Send DAQ Start Signal");  
    }
 
 
 
    SiTCPrcvRBCP_ACK(0);
 
    //    std::cout << SiTCPrcvRBCP_ACK(1) << std::endl;
 
 
 
  }
 
 
 
  printf("Number of sent packets = %d\n", packetNo
);  
 
 
}
 
//------------------------------------------------------------------------------------------------------          
 
//--------------------EASIROC MADC sender---------------------------
 
int Senderread_madc( int data){// data: send only 1 data length
 
 
 
 //Set ADC ch to FPGA reg
 
  SiTCPRBCPskeleton(0xff, 0x80, 0x00000012, 0x1, 0x00000012);
 
  SiTCPsndData[0] = data;
 
 
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  int len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(1);
 
  
 
  SiTCPRBCPskeleton(0xff, 0x80, 0x0000001f, 0x1, 0x0000001f);
 
  SiTCPsndData[0] = 1;
 
  
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(1);
 
  
 
  //Sleep(20000);//wait ADC ch change
 
  Sleep(2000);//wait ADC ch change
 
  
 
  //start Read ADC
 
  SiTCPRBCPskeleton(0xff, 0x80, 0x00000012, 0x1, 0x00000012);
 
  SiTCPsndData[0] = 240;
 
  
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(1);
 
 
 
  SiTCPRBCPskeleton(0xff, 0x80, 0x0000001f, 0x1, 0x0000001f);
 
  SiTCPsndData[0] = 0;
 
  
 
  memcpy(SiTCPsndBuf
+sizeof(struct bcp_header
), SiTCPsndData
, sizeof SiTCPsndData
);  
  len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  SiTCPrcvRBCP_ACK(1);
 
  
 
  //read data
 
  int rd_data = 0;
 
  int rd_data1 = 0;
 
  int rd_data2 = 0;
 
  SiTCPRBCPskeleton(0xff, 0xc0, 0x00000004, 0x1, 0x00000004);
 
  len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  rd_data1 =  SiTCPrcvRBCP_ACK(1);
 
  // std::cout << rd_data << std::endl;
 
  SiTCPRBCPskeleton(0xff, 0xc0, 0x00000005, 0x1, 0x00000005);
 
  len = 0;
 
  len = sendto(SiTCPGetUDPSock(), SiTCPsndBuf, 1 + sizeof(struct bcp_header), 0,
 
               (struct sockaddr *)&SiTCPudpAddr, sizeof(SiTCPudpAddr));//important!
 
  if(len < 0){
 
  }
 
  rd_data2 =  SiTCPrcvRBCP_ACK(1);
 
  // std::cout << rd_data << std::endl;
 
  rd_data = rd_data1*256 + rd_data2;
 
  
 
  return rd_data;
 
 
 
}
 
 
 
 
 
        
 
//------------------------------------------------------------------------------------------------------ 
 
void Senderclear_all(){
 
  unsigned char address;
 
  int data;
 
  //int nbyte;
 
 
 
  // Send register clear
 
  address = 0x00;
 
  data = 0x0;
 
  Senderudp_send(address, data);
 
 
 
  printf("Clear all registers in FPGA.\n");  
}