view dds.c @ 206:c700a2d38fb8

New AD9956 interface
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:38 +0200
parents
children fcc988c6f841
line wrap: on
line source

#ifdef _CVI_
#include <winsock2.h>
#include <ansi_c.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#endif

#include "dds.h"

int msend(int fd, char *buffer, int n)
{
	int r;

	buffer[n++] = '\r';
	buffer[n++] = '\n';

	r = send(fd, buffer, n, 0);
	if (r < 0)
		return r;     

	return 0;
}


int mrecv(int fd, char *buffer, int len)
{
	int n;

	n = recv(fd, buffer, len, 0);
	if (n < 0)
		return n;

	if ((buffer[--n] != '\n') || (buffer[--n] != '\r'))
		return -1;

	buffer[n] = '\0';

	return n;
}


int command(int fd, char *frmt, ...)
{
	int r, n;
	char buffer[1024];
	va_list v;

	va_start(v, frmt);
	n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
	va_end(v);

	r = msend(fd, buffer, n);
	if (r < 0)
		return r;

	r = mrecv(fd, buffer, sizeof(buffer));
	if (r < 0)
		return r;

	if (! streq(buffer, "OK"))
		return -1;

	return 0;
}