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
7 * REVISED: $Log: testmain.c,v $
8 * REVISED: Revision 1.1 1998/07/21 11:34:11 ghn
9 * REVISED: Initial revision
11 * Copyright 1998 - Greg Nelson
12 * Redistributable under the terms of the GNU Public Licence (GPL)
25 #define BASKETHOST "halebopp.qtmsys.com"
27 char *baskethost = BASKETHOST;
29 void MakeAwake(AwakePacket *pkt);
30 void MakeDataPkt(char **pkt, EggCarton *src);
32 int main(int argc, char *argv[]) {
36 int32 collyes, collno, alignover, alignwait;
42 struct sockaddr_in rhost, bhost;
43 char *pktbuf, *outpktbuf;
44 uint16 pkttype, pktsize;
49 printf("Usage: %s <ttynum> <baud> <trialsz> <sec_rec> <samp_rec>\n", pgmname);
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();
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;
64 if (coll.opts.trialsz < MINBITS || coll.opts.trialsz > MAXBITS) {
65 fprintf(stderr, "Trial size must be between %d and %d.\n",
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);
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);
80 sps = (double)coll.opts.samp_rec / (double)coll.opts.sec_rec;
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);
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");
94 alignover = alignwait = 0;
97 sdlisten = InitNetwork(EGGPORT);
99 /* Get us aligned for the first time */
100 while(AlignRecord(&coll, TRUE) < 0);
103 res = NetListen(sdlisten, &pktbuf, &rhost, FALSE);
104 if (res < 0 && res != ERR_COMM_TMOUT) {
105 fprintf(stderr, "NetListen error: %d\n", res);
108 if (res == ERR_NONE) {
109 pkttype = ((GenPacket *)pktbuf)->type;
110 pktsize = ((GenPacket *)pktbuf)->pktsize;
113 fprintf(stderr, "Egg received data?\n");
116 /* Basket wants data! */
117 rpktp = (ReqPacket *)pktbuf;
119 /* Find the desired data. */
120 res = LoadNextPacket(rpktp->starttm, &retrcart);
122 if (res == ERR_NONE) {
123 /* NetPacketize it */
124 MakeDataPkt(&outpktbuf, &retrcart);
125 rhost.sin_port = htons(BASKETPORT);
126 res = NetTalk(&rhost, outpktbuf);
128 fprintf(stderr, "NetTalk failed (%d)\n", res);
134 fprintf(stderr, "Egg received awake?\n");
136 case SETTINGS_PACKET:
137 /* Install new settings! */
143 if (coll.sampct == 0) {
144 cdres = AlignRecord(&coll, FALSE);
146 if (cdres == -3) alignover++; else alignwait++;
150 cdres = CollectData(&coll);
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]);
176 void MakeAwake(AwakePacket *pkt) {
179 pkt->type = AWAKE_PACKET;
180 pkt->pktsize = sizeof(AwakePacket);
181 pkt->eggid = GetID();
183 pkt->nowtm = now.tv_sec;
186 void MakeDataPkt(char **pkt, EggCarton *src) {
187 uint16 pktsize, offset, rec, sbuf;
190 pktsize = sizeof(EggHeader);
191 pktsize += src->hdr.numrec * (sizeof(uint32) +
192 src->hdr.samp_rec * sizeof(trial));
193 pktsize += sizeof(uint16); /* Checksum */
195 src->hdr.type = DATA_PACKET;
196 src->hdr.pktsize = pktsize;
198 *pkt = (char *)malloc(pktsize);
199 memcpy(*pkt, &(src->hdr), sizeof(EggHeader));
200 offset = sizeof(EggHeader);
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;
212 sbuf = BlockCRC16(*pkt, offset);
213 memcpy((*pkt)+offset, &sbuf, sizeof(uint16));
214 offset += sizeof(uint16);
216 if (offset != pktsize) {
217 fprintf(stderr, "Error in packet generation!\n");