#ifndef _SEND_OSC_C
#define _SEND_OSC_C

#define COMMON_SETUP(n)				\
  u8 packet[256];				\
  osc_data_t o[n];				\
  o[0].t = ntp;					\
  o[1].d = utc;					\
  o[2].h = frm;

#define COMMON_SEND(addr,dsc,incl)					\
  int packet_sz;							\
  packet_sz = osc_construct_message(addr, dsc, o, packet, 256);		\
  sendto_client_register(d->fd, d->cr, packet, packet_sz, incl); 

void send_jck_drift(struct jackosc *d, 
		    u64 ntp, f64 utc, i64 frm, 
		    i64 ntp_dif, f64 utc_dif)
{
  COMMON_SETUP(5);
  o[3].h = ntp_dif;
  o[4].d = utc_dif;
  COMMON_SEND("/drift", ",tdhhd", REQUEST_CORRECTION);
}

void send_jck_tick(struct jackosc *d, 
		   u64 ntp, f64 utc, i64 frm, 
		   i64 frame, f64 pulse)
{
  COMMON_SETUP(5);
  o[3].h = frame;
  o[4].d = pulse;
  COMMON_SEND("/tick", ",tdhhd", REQUEST_TICK);
}

void send_jck_current(int fd, struct sockaddr_in address, 
		      u64 ntp, f64 utc, i64 frm, 
		      i64 frame, f64 pulse)
{
  COMMON_SETUP(5);
  o[3].h = frame;
  o[4].d = pulse;
  int packet_sz;
  packet_sz = osc_construct_message("/current.reply", ",tdhhd", 
				    o, packet, 256);
  if(packet_sz) {
    sendto_exactly(fd, packet, packet_sz, address);
  }
}

void send_jck_pulse(struct jackosc *d, 
		    u64 ntp, f64 utc, i64 frm, 
		    u64 p_ntp, f64 p_utc, i64 p_frm, i32 pulse)
{
  COMMON_SETUP(7);
  o[3].t = p_ntp;
  o[4].d = p_utc;
  o[5].h = p_frm;
  o[6].i = pulse;
  COMMON_SEND("/pulse", ",tdhtdhi", REQUEST_PULSE);
}

void send_jck_transport(struct jackosc *d, 
			u64 ntp, f64 utc, i64 frm, 
			f64 fps, f64 ppm, f64 ppc, f64 pt, 
			i32 rolling)
{
  COMMON_SETUP(8);
  o[3].d = fps;
  o[4].d = ppm;
  o[5].d = ppc;
  o[6].d = pt;
  o[7].i = rolling;
  COMMON_SEND("/transport", ",tdhddddi", REQUEST_TRANSPORT);
}

void send_jck_status(int fd, struct sockaddr_in address, 
		     f64 fps, f64 ppm, f64 ppc, f64 pt, 
		     i32 rolling)
{
  u8 packet[256];
  osc_data_t o[5];
  int packet_sz;
  o[0].d = fps;
  o[1].d = ppm;
  o[2].d = ppc;
  o[3].d = pt;
  o[4].i = rolling;
  packet_sz = osc_construct_message("/status.reply", ",ddddi", 
				    o, packet, 256);
  if(packet_sz) {
    sendto_exactly(fd, packet, packet_sz, address);
  }
}

#endif
