comparison 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
comparison
equal deleted inserted replaced
205:31093786d41d 206:c700a2d38fb8
1 #ifdef _CVI_
2 #include <winsock2.h>
3 #include <ansi_c.h>
4 #else
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <netdb.h>
15 #include <ctype.h>
16 #include <math.h>
17 #include <stdarg.h>
18 #endif
19
20 #include "dds.h"
21
22 int msend(int fd, char *buffer, int n)
23 {
24 int r;
25
26 buffer[n++] = '\r';
27 buffer[n++] = '\n';
28
29 r = send(fd, buffer, n, 0);
30 if (r < 0)
31 return r;
32
33 return 0;
34 }
35
36
37 int mrecv(int fd, char *buffer, int len)
38 {
39 int n;
40
41 n = recv(fd, buffer, len, 0);
42 if (n < 0)
43 return n;
44
45 if ((buffer[--n] != '\n') || (buffer[--n] != '\r'))
46 return -1;
47
48 buffer[n] = '\0';
49
50 return n;
51 }
52
53
54 int command(int fd, char *frmt, ...)
55 {
56 int r, n;
57 char buffer[1024];
58 va_list v;
59
60 va_start(v, frmt);
61 n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
62 va_end(v);
63
64 r = msend(fd, buffer, n);
65 if (r < 0)
66 return r;
67
68 r = mrecv(fd, buffer, sizeof(buffer));
69 if (r < 0)
70 return r;
71
72 if (! streq(buffer, "OK"))
73 return -1;
74
75 return 0;
76 }