Exemple #1
0
byte set_current(int ch, float i, float *v)
	{
	// Sets CTMU current 'i' on a channel 'ch' and returns the voltage measured across the load. 
	// Allowed values of current are .55, 5.5, 55 and 550 micro ampleres. ch=0 puts CTMU Off.
	byte res[2], irange;
	int  iv;

	if(i > 500)			// 550 uA range
		irange = 0;
	else if(i > 50)		//	55 uA
		irange = 3;
	else if(i > 5)		// 5.5 uA
		irange = 2;
	else				// 0.55 uA
		irange = 1;

	if( (ch != 0) && (ch != 3) && ( ch !=4) )
		{
		fprintf(stderr, "Current to be set only on IN1 or IN2. %d\n",ch);
		return INVARG;
		}
	sendByte(SETCURRENT);
	sendByte(ch);
	sendByte(irange);
	*res = COMERR;
	sread(1, res);
	if( *res != 'D') return *res;
	if(sread(2,res)!= 2) return COMERR;
	iv = res[0] | (res[1] << 8);
	*v = m12[ch] * iv + c[ch];
	return 0;
	}
Exemple #2
0
byte measure_cv(int ch, int ctime, float i, float* v)
//	Using the CTMU of PIC, charges a capacitor connected to IN1, IN2 or SEN, for 'ctime' microseconds
//		and then mesures the voltage across it.
//		The value of current can be set to .55uA, 5.5 uA, 55uA or 550 uA
	{  
	byte res[2], irange;
	int  iv;

	if(i > 500)			// 550 uA range
		irange = 0;
	else if(i > 50)		//	55 uA
		irange = 3;
	else if(i > 5)		// 5.5 uA
		irange = 2;
	else				// 0.55 uA
		irange = 1;

	if( (ch != 3) && ( ch !=4) )
		{
		fprintf(stderr, "Current to be set only on IN1 or IN2. %d\n",ch);
		return INVARG;
		}
	sendByte(MEASURECV);
	sendByte(ch);
	sendByte(irange);
	sendInt(ctime);
	*res = COMERR;
	sread(1, res);
	if (*res != 'D') return *res;
	if(sread(2,res) != 2) return COMERR;
	iv = res[0] | (res[1] << 8);
	*v = m12[ch] * iv + c[ch];
	return 0;
	}
Exemple #3
0
byte capture2(int ch1, int ch2, int ns, int tg, float* data) 
// Returns 4 vectors(of size 'ns'), T1, V1,T2,V2 starting at pointer *data
	{
	byte 	res[MAXBUF], *bp=res;
	u16		k;

	sendByte(CAPTURE2);
	sendByte(ch1);
	sendByte(ch2);
	sendInt(ns);
	sendInt(tg);
	*res = COMERR;				// fill response with error, call should correct it.
	sread(1, res);				// response byte
	if(*res != 'D')
		{
		fprintf(stderr,"Invalid argument list\n");
		return *res;
		}
	sread(1, res);				// Read & ingnore this byte, due to word alignment of uC
	k = sread(2*ns, res);		// Read '2*ns' data bytes, comes inter leaved, like, a1, b1, a2, b2 ...
	if(k != 2*ns)
		{
		fprintf(stderr, "CAPTURE:Expected %d bytes. Got %d only\n", 2*ns, k);
		return INVSIZE;
		}
	for(k=0; k < ns; ++k)		// Fill the inter-leaved data as T1, V1, T2, V2
		{
		data[k] = 0.001 * k * tg;					// Fill T1, microseconds to milliseconds
		data[k + 2*ns] = 0.001 * k * tg + tgap;		// Fill T2, with offset
		data[k + ns] = *bp++ * m8[ch1] + c[ch1]; 	// Fill V1
		data[k + 3*ns] = *bp++ * m8[ch2] + c[ch2]; 	// Fill V2
		}
	return 0;
	}
Exemple #4
0
/*------------ capture functions (8bit data)--------------------
Accepts channel numbers (ch, ch2 etc), Number of samples "ns",and Time interval between two samples "tg".
The return value consists of arrays of Time & Voltage, starting at location 'data'. The first 'ns' floats are the Time, 
followed by another 'ns' floats of voltage. This repeats for each channel captured.
*/
byte capture(int ch, int ns, int tg, float* data) 
// Returns 2 vectors(of size 'ns'), T1, V1 starting at pointer *data
	{
	byte 	res[MAXBUF], *bp=res;
	u16		k;
	
	//	Arguments : channel number , number of samples and timegap. data out is Time (ns*float), Voltage(ns*float)
	if( (ch < 0) || (ch > 12) || (tg < 4) || (ns > 1800))
		{
		fprintf(stderr,"ch= %d ns = %d tg = %d\n", ch, ns, tg);
		return INVARG;
		}
	sendByte(CAPTURE);
	sendByte(ch);
	sendInt(ns);
	sendInt(tg);
	*res = COMERR;				// fill response with error, call should correct it.
	sread(1, res);				// get response
	if(*res != 'D')
		{
		fprintf(stderr,"Invalid argument list\n");
		return *res;
		}
	sread(1, res);				// Read & ingnore this byte, due to word alignment of uC
	k = sread(ns, res);			// Read 'ns' data bytes
	if(k != ns)
		{
		fprintf(stderr, "CAPTURE:Expected %d bytes. Got %d only\n", ns, k);
		return INVSIZE;
		}
	for(k=0; k < ns; ++k) *data++ = 0.001 * k * tg;			// Fill Time, microseconds to milliseconds
	for(k=0; k < ns; ++k) *data++ = *bp++ * m8[ch] + c[ch]; // Fill voltage
	return 0;
	}
Exemple #5
0
//----------------------- Capture with 12 bit resolution, each item is 2byte in size -----------------
byte capture_hr(int ch, int ns, int tg, float* data) 
// Returns two vectors(of size 'ns'), T1, V1 starting at pointer *data
	{
	byte 	res[MAXBUF];
	u16		k, *ip = (u16*)res;
	
	sendByte(CAPTURE_HR);
	sendByte(ch);
	sendInt(ns);
	sendInt(tg);
	sread(1, res);				// response byte
	if(*res != 'D')
		return COMERR;
	sread(1, res);				// Read & ingnore this byte, due to word alignment of uC
	k = sread(2*ns, res);		// Read 2*ns data bytes
	if(k != 2*ns)
		{
		fprintf(stderr, "CAPTURE_HR:Expected %d bytes. Got %d only\n", 2*ns, k);
		return INVSIZE;
		}
	for(k=0; k < ns; ++k) *data++ = 0.001 * k * tg;				// Fill Time, microseconds to milliseconds
	ip = (u16*)res;
	for(k=0; k < ns; ++k) *data++ = *ip++ * m12[ch] + c[ch]; 	// Fill voltage
	return 0;
	}
Exemple #6
0
byte capture2_hr(int ch1, int ch2, int ns, int tg, float* data) 
// Returns four vectors, T1, V1, T2, V2 , starting at pointer *data
	{
	byte 	res[MAXBUF];
	u16		k, *ip = (u16*)res;
	
	sendByte(CAPTURE2_HR);
	sendByte(ch1);
	sendByte(ch2);
	sendInt(ns);
	sendInt(tg);
	sread(1, res);				// response byte
	if(*res != 'D')
		return COMERR;
	sread(1, res);				// Read & ingnore this byte, due to word alignment of uC
	k = sread(4*ns, res);		// Read 2*2*ns data bytes, each data 2 bytes
	if(k != 4*ns)
		{
		fprintf(stderr, "CAPTURE:Expected %d bytes. Got %d only\n", 4*ns, k);
		return INVSIZE;
		}
	for(k=0; k < ns; ++k)		// Fill the inter-leaved data as T1, V1, T2, V2
		{
		data[k] = 0.001 * k * tg;					// Fill T1, microseconds to milliseconds
		data[k + 2*ns] = 0.001 * k * tg + tgap;		// Fill T2, with offset
		data[k + ns] = *ip++ * m12[ch1] + c[ch1]; 	// Fill V1
		data[k + 3*ns] = *ip++ * m12[ch2] + c[ch2];	// Fill V2
		}
	return 0;
	}
Exemple #7
0
/*---------------------------------------------------------------------------*/
#if 1 /* But ok! */
unsigned
sht11_sreg(void)
{
  unsigned sreg, rcrc;

  sstart();			/* Start transmission */
  if(!swrite(STATUS_REG_R)) {
    goto fail;
  }

  sreg = sread(1);
  rcrc = sread(0);

#ifdef CRC_CHECK
  {
    unsigned crc;
    crc = crc8_add(0x0, STATUS_REG_R);
    crc = crc8_add(crc, sreg);
    if (crc != rev8bits(rcrc))
      goto fail;
  }
#endif

  return sreg;

 fail:
  sreset();
  return -1;
}
Exemple #8
0
byte get_version(byte* res)
	{
	if(sendByte(GETVERSION)== FALSE) return COMERR;
	*res = COMERR;	// Assume en error
	sread(1, res);
	if(*res != 'D') return *res;
	if(sread(5,res)==5) return 0;
	return COMERR;
	}
Exemple #9
0
/*
 * Only commands MEASURE_HUMI or MEASURE_TEMP!
 */
static unsigned int
scmd(unsigned cmd)
{
  unsigned long n;

  if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
    return -1;
  }
  MCUCR |= (1<<JTD);
  MCUCR |= (1<<JTD);

  sreset();
  /* Start transmission */
  if(!swrite(cmd)) {
	  //printf("-2");
	  goto fail;
  }

  for(n = 0; n < 250000; n++) {
    if(!SDA_IS_1) {
      unsigned t0, t1, rcrc;
      t0 = sread(1);
      t1 = sread(1);
      rcrc = sread(0);
      //printf("t0:%d",t0);
      //printf("t1:%d",t1);
#ifdef CRC_CHECK
      {
	unsigned crc;
	crc = crc8_add(0x0, cmd);
	crc = crc8_add(crc, t0);
	crc = crc8_add(crc, t1);
	if(crc != rev8bits(rcrc)) {
		// printf("-3");
		goto fail;
	}
      }
#endif
      MCUCR &= (0<<JTD);
      MCUCR &= (0<<JTD);

      return (t0 << 8) | t1;
    }
  }

 fail:
  sreset();
  //printf("-1");
  MCUCR &= (0<<JTD);
  MCUCR &= (0<<JTD);

  return -1;
}
Exemple #10
0
//=================== Charge Time Measurement Unit related functions ==========================
byte read_temp(int* temp)
	{
//	Reads the temperature of uC, currently of no use. Have to see whether this can be used for correcting
//	the drift of the 5V regulator with temeperature.
	byte res[2];

	sendByte(READTEMP);
	*res = COMERR;		// Assume an error
	sread(1,res);
	if( *res != 'D') return *res;
	if(sread(2,res) != 2) return COMERR;
	*temp = res[0] | (res[1] << 8);
	return 0;
	}
Exemple #11
0
byte read_adcNS(byte ch, u16* iv)	// Read ADC, without entering SLEEP mode
	{
	byte res[2];
	if ((ch < 0) || (ch > 12))
		return INVARG;
	sendByte(READADC);
	sendByte(ch);
	*res = COMERR;
	sread(1, res);
	if (*res != 'D') return *res;
	if(sread(2, res) != 2) return COMERR;
	*iv = res[0] | (res[1] << 8);
	return 0;
	}
Exemple #12
0
byte set_pwm(byte osc, float ds, byte resol) // osc#, duty cycle, resolution 
	{
	// Sets PWM on SQR1 / SQR2. The frequency is decided by the resolution in bits.
	byte res[1];
	u16 ocxrs, ocx;
	
	if( (ds > 100) || (resol < 6) || (resol > 16) )
		return INVARG;
	ocxrs = pow(2.0, resol);  
	ocx = (u16)(0.01 * ds * ocxrs + 0.5);
	if(osc == 0)
		sendByte(SETPWM1);
	else
		sendByte(SETPWM2);
	sendInt(ocxrs-1);			// ocxrs
	sendInt(ocx);				//ocx
	*res = COMERR;
	sread(1, res);
	if(*res != 'D')
		{
		fprintf(stderr, "SETPWM error\n");
		return *res;
		}
	return 0;
	}
Exemple #13
0
CURLcode Curl_read_plain(curl_socket_t sockfd,
                         char *buf,
                         size_t bytesfromsocket,
                         ssize_t *n)
{
  ssize_t nread = sread(sockfd, buf, bytesfromsocket);

  if(-1 == nread) {
    int err = SOCKERRNO;
    int return_error;
#ifdef USE_WINSOCK
    return_error = WSAEWOULDBLOCK == err;
#else
    return_error = EWOULDBLOCK == err || EAGAIN == err || EINTR == err;
#endif
    if(return_error)
      return CURLE_AGAIN;
    else
      return CURLE_RECV_ERROR;
  }

  /* we only return number of bytes read when we return OK */
  *n = nread;
  return CURLE_OK;
}
Exemple #14
0
char *
fbuf_read (gfc_unit * u, int * len)
{
  char *ptr;
  int oldact, oldpos;
  int readlen = 0;

  fbuf_debug (u, "fbuf_read, len %d: ", *len);
  oldact = u->fbuf->act;
  oldpos = u->fbuf->pos;
  ptr = fbuf_alloc (u, *len);
  u->fbuf->pos = oldpos;
  if (oldpos + *len > oldact)
    {
      fbuf_debug (u, "reading %d bytes starting at %d ", 
                  oldpos + *len - oldact, oldact);
      readlen = sread (u->s, u->fbuf->buf + oldact, oldpos + *len - oldact);
      if (readlen < 0)
	return NULL;
      *len = oldact - oldpos + readlen;
    }
  u->fbuf->act = oldact + readlen;
  fbuf_debug (u, "fbuf_read done: ");
  return ptr;
}
Exemple #15
0
ssize_t Curl_recv_plain(struct connectdata *conn, int num, char *buf,
                        size_t len, CURLcode *code)
{
  curl_socket_t sockfd = conn->sock[num];
  ssize_t nread = sread(sockfd, buf, len);

  *code = CURLE_OK;
  if(-1 == nread) {
    int err = SOCKERRNO;

    if(
#ifdef WSAEWOULDBLOCK
      /* This is how Windows does it */
      (WSAEWOULDBLOCK == err)
#else
      /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
         due to its inability to send off data without blocking. We therefor
         treat both error codes the same here */
      (EWOULDBLOCK == err) || (EAGAIN == err) || (EINTR == err)
#endif
      ) {
      /* this is just a case of EWOULDBLOCK */
      *code = CURLE_AGAIN;
    }
    else {
      failf(conn->data, "Recv failure: %s",
            Curl_strerror(conn, err));
      conn->data->state.os_errno = err;
      *code = CURLE_RECV_ERROR;
    }
  }
  return nread;
}
Exemple #16
0
static CURLcode ntlm_wb_response(struct connectdata *conn,
                                 const char *input, curlntlm state)
{
  ssize_t size;
  char buf[200]; /* enough, type 1, 3 message length is less then 200 */
  char *tmpbuf = buf;
  size_t len_in = strlen(input), len_out = sizeof(buf);

  while(len_in > 0) {
    ssize_t written = swrite(conn->ntlm_auth_hlpr_socket, input, len_in);
    if(written == -1) {
      /* Interrupted by a signal, retry it */
      if(errno == EINTR)
        continue;
      /* write failed if other errors happen */
      goto done;
    }
    input += written;
    len_in -= written;
  }
  /* Read one line */
  while(len_out > 0) {
    size = sread(conn->ntlm_auth_hlpr_socket, tmpbuf, len_out);
    if(size == -1) {
      if(errno == EINTR)
        continue;
      goto done;
    }
    else if(size == 0)
      goto done;
    else if(tmpbuf[size - 1] == '\n') {
      tmpbuf[size - 1] = '\0';
      goto wrfinish;
    }
    tmpbuf += size;
    len_out -= size;
  }
  goto done;
wrfinish:
  /* Samba/winbind installed but not configured */
  if(state == NTLMSTATE_TYPE1 &&
     size == 3 &&
     buf[0] == 'P' && buf[1] == 'W')
    return CURLE_REMOTE_ACCESS_DENIED;
  /* invalid response */
  if(size < 4)
    goto done;
  if(state == NTLMSTATE_TYPE1 &&
     (buf[0]!='Y' || buf[1]!='R' || buf[2]!=' '))
    goto done;
  if(state == NTLMSTATE_TYPE2 &&
     (buf[0]!='K' || buf[1]!='K' || buf[2]!=' ') &&
     (buf[0]!='A' || buf[1]!='F' || buf[2]!=' '))
    goto done;

  conn->response_header = aprintf("NTLM %.*s", size - 4, buf + 3);
  return CURLE_OK;
done:
  return CURLE_REMOTE_ACCESS_DENIED;
}
Exemple #17
0
int
PREFIX(fgetc) (const int * unit, char * c, gfc_charlen_type c_len)
{
  int ret;
  gfc_unit * u = find_unit (*unit);

  if (u == NULL)
    return -1;

  fbuf_reset (u);
  if (u->mode == WRITING)
    {
      sflush (u->s);
      u->mode = READING;
    }

  memset (c, ' ', c_len);
  ret = sread (u->s, c, 1);
  unlock_unit (u);

  if (ret < 0)
    return ret;

  if (ret != 1)
    return -1;
  else
    return 0;
}
Exemple #18
0
short frdmsg(char *str)
{
   char *smax;
   char *s = str;

   smax = s + MAXPACK - 1;
   for (;;) {
      if (sread(s, 1, M_fPacketTimeout) <= 0)
      {
         printmsg(0,"frdmsg: timeout reading message");
         *s++ = '\0';
         goto msgerr;
      }
      if (*s == '\r')
         break;
      if (*s < ' ')
         continue;
      if (s++ >= smax)
      {
         printmsg(0,"frdmsg: buffer overflow");
         *--s = '\0';
         goto msgerr;
      } /* if (s++ >= smax) */
   }
   *s = '\0';
   return DCP_OK;

msgerr:
   printmsg(0,"frdmsg: Message received \"%s\"", str);
   return DCP_FAILED;
} /* frdmsg */
Exemple #19
0
/*
 * do_command_response() - set up and send a command to the server, read and process the response.
 */
static int
do_command_response(acs_cli_info_t *ctx)
{
	int len;

	len = create_server_command(ctx);
	if (len < 0) {
		return -1;
	}

	/* Send it */
	if (swrite(ctx->socket, ctx->cmd_buf, len) < len) {
		fprintf(stderr, "Failed to send command to server: %s\n", strerror(errno));
		return BCME_ERROR;
	}

	/* Help server get the data till EOF */
	shutdown(ctx->socket, SHUT_WR);

	/* Read the response */

	len = sread(ctx->socket, ctx->cmd_buf, ACSD_BUFSIZE_4K-1);
	if (len < 0) {
		fprintf(stderr, "Failed to read response from server: %s\n", strerror(errno));
		return BCME_ERROR;
	}

	/* Process the response */
	return process_response(ctx, len);
}
Exemple #20
0
void
srv(Srv *srv)
{
	Req *r;

	fmtinstall('D', dirfmt);
	fmtinstall('F', fcallfmt);

	if(srv->fpool == nil)
		srv->fpool = allocfidpool(srv->destroyfid);
	if(srv->rpool == nil)
		srv->rpool = allocreqpool(srv->destroyreq);
	if(srv->msize == 0)
		srv->msize = 8192+IOHDRSZ;

	changemsize(srv, srv->msize);

	srv->fpool->srv = srv;
	srv->rpool->srv = srv;

	while(r = getreq(srv)){
		if(r->error){
			respond(r, r->error);
			continue;	
		}
		switch(r->ifcall.type){
		default:
			respond(r, "unknown message");
			break;
		case Tversion:	sversion(srv, r);	break;
		case Tauth:	sauth(srv, r);	break;
		case Tattach:	sattach(srv, r);	break;
		case Tflush:	sflush(srv, r);	break;
		case Twalk:	swalk(srv, r);	break;
		case Topen:	sopen(srv, r);	break;
		case Tcreate:	screate(srv, r);	break;
		case Tread:	sread(srv, r);	break;
		case Twrite:	swrite(srv, r);	break;
		case Tclunk:	sclunk(srv, r);	break;
		case Tremove:	sremove(srv, r);	break;
		case Tstat:	sstat(srv, r);	break;
		case Twstat:	swstat(srv, r);	break;
		}
	}

	free(srv->rbuf);
	srv->rbuf = nil;
	free(srv->wbuf);
	srv->wbuf = nil;
	srv->msize = 0;
	freefidpool(srv->fpool);
	srv->fpool = nil;
	freereqpool(srv->rpool);
	srv->rpool = nil;

	if(srv->end)
		srv->end(srv);
}
Exemple #21
0
/*
 * Only commands MEASURE_HUMI or MEASURE_TEMP!
 */
static unsigned int
scmd(unsigned cmd)
{
  unsigned int n;

  if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
    PRINTF("Illegal command: %d\n", cmd);
    return -1;
  }

  sstart();			/* Start transmission */
  if(!swrite(cmd)) {
    PRINTF("SHT11: scmd - swrite failed\n");
    goto fail;
  }

  for(n = 0; n < 20000; n++) {
    if(!SDA_IS_1) {
      unsigned t0, t1, rcrc;
      t0 = sread(1);
      t1 = sread(1);
      rcrc = sread(0);
      PRINTF("SHT11: scmd - read %d, %d\n", t0, t1);
#ifdef CRC_CHECK
      {
	unsigned crc;
	crc = crc8_add(0x0, cmd);
	crc = crc8_add(crc, t0);
	crc = crc8_add(crc, t1);
	if(crc != rev8bits(rcrc)) {
	  PRINTF("SHT11: scmd - crc check failed %d vs %d\n",
		 crc, rev8bits(rcrc));
	  goto fail;
	}
      }
#endif
      return (t0 << 8) | t1;
    }
    /* short wait before next loop */
    clock_wait(1);
  }
 fail:
  sreset();
  return -1;
}
Exemple #22
0
static ssize_t Curl_gtls_pull(void *s, void *buf, size_t len)
{
  ssize_t ret = sread(GNUTLS_POINTER_TO_INT_CAST(s), buf, len);
#if defined(USE_WINSOCK) && !defined(GNUTLS_MAPS_WINSOCK_ERRORS)
  if(ret < 0)
    gnutls_transport_set_global_errno(gtls_mapped_sockerrno());
#endif
  return ret;
}
Exemple #23
0
byte get_state(byte pin, byte *st)
	{
	//	gets the status of the digital input pin. IN1, IN2 & SEN are set to digital mode before sensing input level.
	byte res[1];

	sendByte(GETSTATE);	
	sendByte(pin);	
	*res = COMERR;
	sread(1, res);
	if(*res != 'D')
		{
		fprintf(stderr,"GETSTATE error\n");
		return *res;
		} 
	if(sread(1,res) != 1) return COMERR;
	*st = *res;
	return 0;
	}
Exemple #24
0
byte set_sqrs(float freq, float diff, float *fset)       // Freq in Hertz, phase difference in % of T
	{
	// Sets the output frequency of both SQR1 & SQR2. 'fset' returns actual value set. 
	// The second argument is the phase difference between them  in percentage.
	static	float mtvals[4] = {0.125e-6, 8*0.125e-6, 64*0.125e-6, 256*0.125e-6};	// Possible Timer period values
	float	per;
	byte k, res[1], TCKPS = 0;		// TCKPS, TG & OCRS are uC registers
	u16 TG, OCRS = 0;

	if(freq == 0)				// Disable both Square waves
		{
		set_sqr1(0, fset);
		set_sqr2(0, fset);
		return 0;
		}
	else if(freq < 0)			// Disable both Square waves
		{
		set_sqr1(-1, fset);
		set_sqr2(-1, fset);
		return 0;
		}
	if( (diff < 0) || (diff >= 100.0) )
		{
		fprintf(stderr,"Invalid phase difference\n");
		return INVARG;
		}
	per = 1.0/freq;						// period T requested
	for(k=0; k < 4; ++k)				// Find the optimum scaling, OCR value
		if(per < mtvals[k]*50000)
			{
			TCKPS = k;
			OCRS = per/mtvals[k];
			OCRS = (int)(OCRS+0.5);
			freq = 1./(mtvals[k]*OCRS);
			break;
			}
	if( (TCKPS < 4) && (OCRS == 0) )
		{
		fprintf(stderr,"Invalid Freqency\n");
		return INVARG;
		}
	TG = (int)(diff*OCRS/100.0 +0.5);
	if(TG == 0) TG = 1;		// Need to examine this
	//print 'TCKPS ', TCKPS, 'ocrs ', OCRS, TG
	sendByte(SETSQRS);
	sendByte(TCKPS);		// prescaling for timer
	sendInt(OCRS);			// OCRS value
	sendInt(TG)	;			// time difference
	*res = COMERR;
	sread(1, res);
	if(*res != 'D')
		return *res;
	*fset = freq;
	return 0;
	}
Exemple #25
0
int
fill_buf(Sock *sock,char *buf, int len, char *msg)
{
  int bytes =  0, ret_val;
  while(bytes < len) {
    ret_val = sread(sock, buf + bytes, len - bytes, msg);
    if (ret_val == -1) return -1;
    bytes += ret_val;
  }
  return bytes;
}
Exemple #26
0
static ares_ssize_t socket_recv(ares_channel channel,
   ares_socket_t s,
   void * data,
   size_t data_len)
{
   if (channel->sock_funcs)
      return channel->sock_funcs->arecvfrom(s, data, data_len, 0, 0, 0,
	 channel->sock_func_cb_data);

   return sread(s, data, data_len);
}
Exemple #27
0
static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
{
    int status = 0;
    Msg *msg = NULL;
    uint32_t header;

    status = (int) sread (host->s, &header, 4);

    if (status != 0)
        return -1;

    size_t size = ntohl(header);

    // Buffer on the stack since acknowledges are typically small.
    u_char buffer[size];
    memset (buffer, 0, size);

    status = (int) sread (host->s, buffer, size);

    if (status != 0)
        return status;

    msg = msg__unpack (NULL, size, buffer);

    if (msg == NULL)
        return -1;

    if (!msg->ok)
    {
        ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
               (host->node != NULL) ? host->node : RIEMANN_HOST,
               (host->service != NULL) ? host->service : RIEMANN_PORT,
               msg->error);

        msg__free_unpacked(msg, NULL);
        return -1;
    }

    msg__free_unpacked (msg, NULL);
    return 0;
} /* }}} int riemann_recv_ack */
Exemple #28
0
/* return 0 on success, non-zero on failure */
static int get_request(int sock, struct httprequest *req)
{
  int fail= FALSE;
  char *reqbuf = req->reqbuf;

  /*** Init the httpreqest structure properly for the upcoming request ***/
  memset(req, 0, sizeof(struct httprequest));

  /* here's what should not be 0 from the start */
  req->testno = DOCNUMBER_NOTHING; /* safe default */
  req->open = TRUE; /* connection should remain open and wait for more
                       commands */

  /*** end of httprequest init ***/

  while (req->offset < REQBUFSIZ) {
    int got = sread(sock, reqbuf + req->offset, REQBUFSIZ - req->offset);
    if (got <= 0) {
      if (got < 0) {
        perror("recv");
        logmsg("recv() returned error");
        return DOCNUMBER_INTERNAL;
      }
      logmsg("Connection closed by client");
      reqbuf[req->offset]=0;

      /* dump the request receivied so far to the external file */
      storerequest(reqbuf);
      return DOCNUMBER_INTERNAL;
    }
    req->offset += got;

    reqbuf[req->offset] = 0;

    if(ProcessRequest(req))
      break;
  }

  if (req->offset >= REQBUFSIZ) {
    logmsg("Request buffer overflow, closing connection");
    reqbuf[REQBUFSIZ-1]=0;
    fail = TRUE;
    /* dump the request to an external file anyway */
  }
  else
    reqbuf[req->offset]=0;

  /* dump the request to an external file */
  storerequest(reqbuf);

  return fail; /* success */
}
Exemple #29
0
int
main(int argc, char **argv)
{
  freopen("/tmp/scylla.log", "a+", stderr);

  if (argc<3 || argc>4)
    {
      printf("Usage: %s directory password [ssl-key]\n"
	     "\tRun Scylla (this here) from inetd.\n"
	     "\tScylla assumes that Charybdis feeds a file [with SSL].\n"
	     "\tScylla writes the file into the given directory.\n"
	     "\tMissing sub-directories are created as needed.\n"
	     "\tExisting files are renamed.\n", argv[0]);
      return 1;
    }

  if (chdir(argv[1]))
    ex("chdir");

  sock	= 0;

#ifndef NO_SSL
  tino_ssl_server((argc>3 ? argv[3] : "/etc/scylla.pem"));
#endif

  alarm_init(30,10);

  if (sread_match(argv[2], 0))
    ex("auth fail");

  swrite("scylla " VERSION);

  do
    {
      char	*name;

      name	= sread();
      if (!strcmp(name, "..") || !strncmp(name, "../", 3) ||
	  *name=='/' || strstr(name, "/../"))
	ex("- illegal filename");

      dofile(name);
      free(name);

    }
  while (!sread_match("m", 0));

#ifndef NO_SSL
  tino_ssl_close();
#endif
  return 0;
}
Exemple #30
0
byte capture4(int ch1, int ch2, int ch3, int ch4, int ns, int tg, float* data) 
// Returns 8 vectors(of size 'ns'), T1, V1,T2,V2,T3,V3,T4,V4 starting at pointer *data
	{
	byte 	ch12, ch34, res[MAXBUF], *bp=res;
	u16		k;
	sendByte(CAPTURE4);
	ch12 = (ch2 << 4) | ch1;		// ch1 & ch2 packed into one byte
	ch34 = (ch4 << 4) | ch3;		// ch3 & ch4 packed into one byte
	sendByte(ch12);
	sendByte(ch34);
	sendInt(ns);
	sendInt(tg);
	*res = COMERR;				// fill response with error, call should correct it.
	sread(1, res);				// response byte
	if(*res != 'D')
		{
		fprintf(stderr,"Invalid argument list\n");
		return *res;
		}
	sread(1, res);				// Read & ingnore this byte, due to word alignment of uC
	k = sread(4*ns, res);			// Read '2*ns' data bytes
	if(k != 4*ns)
		{
		fprintf(stderr, "CAPTURE:Expected %d bytes. Got %d only\n", 4*ns, k);
		return INVSIZE;
		}
	for(k=0; k < ns; ++k)		// Fill the inter-leaved data as T1, V1, T2, V2
		{
		data[k] = 0.001 * k * tg;					// Fill T1, microseconds to milliseconds
		data[k + 2*ns] = 0.001 * k * tg + tgap;		// Fill T2, with 1*offset
		data[k + 4*ns] = 0.001 * k * tg + 2*tgap;	// Fill T3, with 2*offset
		data[k + 6*ns] = 0.001 * k * tg + 3*tgap;	// Fill T4, with 3*offset
		data[k + ns] = *bp++ * m8[ch1] + c[ch1]; 	// Fill V1
		data[k + 3*ns] = *bp++ * m8[ch2] + c[ch2]; 	// Fill V2
		data[k + 5*ns] = *bp++ * m8[ch3] + c[ch3]; 	// Fill V3
		data[k + 7*ns] = *bp++ * m8[ch4] + c[ch4]; 	// Fill V3
		}
	return 0;
	}