merge 64-bit fixes from Fernando Lucas Rodriguez <fernando_lr@terra.es>
[debian/gcpegg] / testmain.c
1 /* PROGRAM:     testmain
2  * FILE:        $Header: /home/egg/src/RCS/testmain.c,v 1.1 1998/07/21 11:34:11 ghn Exp $
3  * PURPOSE:     First draft eggsh
4  * AUTHOR:      Greg Nelson
5  * DATE:        98-05-09
6  *
7  * REVISED:     $Log: testmain.c,v $
8  * REVISED:     Revision 1.1  1998/07/21 11:34:11  ghn
9  * REVISED:     Initial revision
10  * REVISED:
11  * Copyright 1998 - Greg Nelson
12  * Redistributable under the terms of the GNU Public Licence (GPL)
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "global.h"
18 #include "genlib.h"
19 #include "hwapi.h"
20 #include "collect.h"
21 #include "storage.h"
22 #include "network.h"
23 #include "errnos.h"
24
25 #define BASKETHOST      "halebopp.qtmsys.com"
26
27 char *baskethost = BASKETHOST;
28
29 void MakeAwake(AwakePacket *pkt);
30 void MakeDataPkt(char **pkt, EggCarton *src);
31
32 int main(int argc, char *argv[]) {
33   int                   res, cdres, i;
34   int32 sdlisten;
35   double sps;
36   int32 collyes, collno, alignover, alignwait;
37   DevOpts devopts;
38   CollectRecord         coll;
39   EggCarton             retrcart;
40   AwakePacket           awake;
41   ReqPacket             *rpktp;
42   struct sockaddr_in    rhost, bhost;
43   char                  *pktbuf, *outpktbuf;
44   uint16                pkttype, pktsize;
45
46   pgmname = argv[0];
47
48   if (argc != 6) {
49     printf("Usage: %s <ttynum> <baud> <trialsz> <sec_rec> <samp_rec>\n", pgmname);
50     exit(-1);
51   }
52
53   devopts.port = atoi(argv[1]);
54   devopts.baud = atoi(argv[2]);
55   coll.opts.trialsz = atoi(argv[3]);
56   coll.opts.sec_rec = atoi(argv[4]);
57   coll.opts.samp_rec = atoi(argv[5]);
58   coll.opts.eggid = GetID();
59
60   coll.opts.rec_pkt = SEC_PKT / coll.opts.sec_rec;
61   if (coll.opts.rec_pkt < 1) coll.opts.rec_pkt = 1;
62   if (coll.opts.rec_pkt > MAXREC_PKT) coll.opts.rec_pkt = MAXREC_PKT;
63
64   if (coll.opts.trialsz < MINBITS || coll.opts.trialsz > MAXBITS) {
65     fprintf(stderr, "Trial size must be between %d and %d.\n",
66             MINBITS, MAXBITS);
67     exit(-1);
68   }
69
70   if (coll.opts.sec_rec < 1 || coll.opts.sec_rec > MAXSEC_REC) {
71     fprintf(stderr, "Seconds/record must be between 1 and %d.\n", MAXSEC_REC);
72     exit(-1);
73   }
74
75   if (coll.opts.samp_rec < 1 || coll.opts.samp_rec > MAXSAMP_REC) {
76     fprintf(stderr, "Samples/record must be between 1 and %d.\n", MAXSAMP_REC);
77     exit(-1);
78   }
79
80   sps = (double)coll.opts.samp_rec / (double)coll.opts.sec_rec;
81
82   printf("Effective sample rate is about %f samp/sec or %f bits/sec\n",
83          sps, coll.opts.trialsz * sps);
84   printf("Packets contain %d records\n", coll.opts.rec_pkt);
85
86   if ((coll.dd = OpenDev(&devopts)) < 0) exit(1);
87   if (EvalSpeed(coll.dd) < coll.opts.trialsz * sps) {
88     fprintf(stderr, "Requested speed exceeds device capabilities.\n");
89     exit(-1);
90   }
91
92   coll.sampct = 0;
93   collyes = collno = 0;
94   alignover = alignwait = 0;
95
96   InitStorage(NULL);
97   sdlisten = InitNetwork(EGGPORT);
98
99   /* Get us aligned for the first time */
100   while(AlignRecord(&coll, TRUE) < 0);
101
102   while(1) {
103     res = NetListen(sdlisten, &pktbuf, &rhost, FALSE);
104     if (res < 0 && res != ERR_COMM_TMOUT) {
105       fprintf(stderr, "NetListen error: %d\n", res);
106       continue;
107     }
108     if (res == ERR_NONE) {
109       pkttype = ((GenPacket *)pktbuf)->type;
110       pktsize = ((GenPacket *)pktbuf)->pktsize;
111       switch(pkttype) {
112       case DATA_PACKET:
113         fprintf(stderr, "Egg received data?\n");
114         break;
115       case REQ_PACKET:
116         /* Basket wants data! */
117         rpktp = (ReqPacket *)pktbuf;
118         
119         /* Find the desired data. */
120         res = LoadNextPacket(rpktp->starttm, &retrcart);
121
122         if (res == ERR_NONE) {
123           /* NetPacketize it */
124           MakeDataPkt(&outpktbuf, &retrcart);
125           rhost.sin_port = htons(BASKETPORT);
126           res = NetTalk(&rhost, outpktbuf);
127           if (res < 0) {
128             fprintf(stderr, "NetTalk failed (%d)\n", res);
129           }
130           free(outpktbuf);
131         }
132         break;
133       case AWAKE_PACKET:
134         fprintf(stderr, "Egg received awake?\n");
135         break;
136       case SETTINGS_PACKET:
137         /* Install new settings! */
138         break;
139       }
140       free(pktbuf);
141     }
142     
143     if (coll.sampct == 0) {
144       cdres = AlignRecord(&coll, FALSE);
145       if (cdres < 0) {
146         if (cdres == -3) alignover++; else alignwait++;
147         continue;
148       }
149     }
150     cdres = CollectData(&coll);
151     if (cdres < 0) {
152       collno++;
153       continue;
154     }
155     collyes++;
156     if (cdres == 1) {
157       SaveRecord(&coll);
158       MakeAwake(&awake);
159       i = NetGetAddr(&bhost, baskethost, BASKETPORT);
160       i = NetTalk(&bhost, (char *)&awake);
161       printf(" collect:nodata over:wait = %ld:%ld %ld:%ld\n",
162              collyes, collno, alignover, alignwait);
163       printf("Record: %d points (transmission %d)\n  %ld: ",
164              coll.sampct, i, coll.data.timestamp);
165       for (i = 0; i < coll.sampct; i++)
166         printf("%3d ", coll.data.trials[i]);
167       printf("\n");
168       coll.sampct = 0;
169     }
170   }
171
172   return 0;
173 }
174
175
176 void MakeAwake(AwakePacket *pkt) {
177   struct timeval now;
178
179   pkt->type = AWAKE_PACKET;
180   pkt->pktsize = sizeof(AwakePacket);
181   pkt->eggid = GetID();
182   getzulutime(&now);
183   pkt->nowtm = now.tv_sec;
184 }
185
186 void MakeDataPkt(char **pkt, EggCarton *src) {
187   uint16 pktsize, offset, rec, sbuf;
188   uint32 lbuf;
189
190   pktsize = sizeof(EggHeader);
191   pktsize += src->hdr.numrec * (sizeof(uint32) + 
192                                 src->hdr.samp_rec * sizeof(trial));
193   pktsize += sizeof(uint16);    /* Checksum */
194
195   src->hdr.type = DATA_PACKET;
196   src->hdr.pktsize = pktsize;
197
198   *pkt = (char *)malloc(pktsize);
199   memcpy(*pkt, &(src->hdr), sizeof(EggHeader));
200   offset = sizeof(EggHeader);
201
202   for (rec = 0; rec < src->hdr.numrec; rec++) {
203     lbuf = src->records[rec].timestamp;
204     memcpy((*pkt)+offset, &lbuf, sizeof(uint32));
205     offset += sizeof(uint32);
206     /* Assumes sizeof(trial) = 1 */
207     memcpy((*pkt)+offset, &(src->records[rec].trials), src->hdr.samp_rec);
208     offset += src->hdr.samp_rec;
209   }
210
211   /* Normal CRC */
212   sbuf = BlockCRC16(*pkt, offset);
213   memcpy((*pkt)+offset, &sbuf, sizeof(uint16));
214   offset += sizeof(uint16);
215
216   if (offset != pktsize) {
217     fprintf(stderr, "Error in packet generation!\n");
218   }
219 }
220