source: fact/tools/FAD/fadfake/fadfake.cpp@ 11905

Last change on this file since 11905 was 9887, checked in by neise, 14 years ago
added 'fadfake' a programm acting as a FAD board. still with very limited funcionality, but nice for testing... added a function OpenServerSocket() to SocketFunctions.cpp
File size: 5.0 KB
Line 
1#include <stdio.h>
2
3#include <stdlib.h>
4#include <cstdlib>
5
6#include <iostream>
7
8#include <sys/socket.h>
9#include <arpa/inet.h>
10#include <time.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <string.h>
16#include <sstream>
17#include <signal.h>
18#include <sys/socket.h>
19#include <netdb.h>
20#include <arpa/inet.h>
21#include <time.h>
22
23#include "fadfake.h"
24
25#include "../SocketFunctions/SocketFunctions.h"
26#include "../format_headers/packed_formatfad.h"
27
28using namespace std;
29
30int fadfake() {
31 int ReadResult;
32 unsigned long triggerID=0;
33 int *Sockets;
34 timeval timeout;
35 int select_return;
36 fd_set ReadFileDescriptor;
37
38 unsigned short Command[80];
39
40 Sockets = OpenServerSockets (8, 5000) ;
41
42
43 while(1){
44
45
46 // Try to read command from socket
47 memset(Command, 0xAA, sizeof(Command));
48 ReadResult = read(Sockets[0], Command, MAX_COM_SIZE);
49 cout << "just read sometinhg." << endl;
50 if (ReadResult==0) break; // Client does not exist anymore
51 if (ReadResult==-1) {
52 printf("Error read from socket \n");
53 break;
54 }
55 if (ntohs(Command[0]) == CMD_Start)
56 {
57 printf ("# -> Start Run\n");
58 }
59 else if (ntohs(Command[0]) == CMD_Stop)
60 {
61 printf ("# -> Stop Run\n");
62 }
63
64 else if (ntohs(Command[0]) == 0x0000)
65 {
66 printf ("# -> received 0x0000\n");
67 }
68
69 else if (ntohs(Command[0]) == CMD_Trigger)
70 {
71 printf ("# -> Single Trigger\n");
72 sendEvent(Sockets[0],triggerID,1024);
73 ++triggerID;
74 printf ("Event %ld nach Socket %d gesendet\n\n", triggerID,0);
75 }
76
77 else if (ntohs(Command[0]) == CMD_Trigger_C)
78 {
79 printf ("# -> Continuous Trigger\n");
80 while(true){
81 fflush (stdout);
82 FD_ZERO (&ReadFileDescriptor);
83 FD_SET (Sockets[0], &ReadFileDescriptor);
84 timeout.tv_sec = 0;
85 timeout.tv_usec = 1000;
86 sendEvent(Sockets[0],triggerID,1024);
87 ++triggerID;
88 printf ("Event nach Socket %d gesendet\n", 0);
89 select_return=select (((int) Sockets[0]) + 1 , &ReadFileDescriptor, NULL, NULL, &timeout);
90 if (select_return < 0){
91 fprintf(stderr, "error: select returned %d.\n" , select_return );
92 }
93 if (FD_ISSET (Sockets[0], &ReadFileDescriptor)){
94 ReadResult = read(Sockets[0], Command, MAX_COM_SIZE);
95 }
96 if (ReadResult <= 0) {
97 fprintf(stderr, "error: read returned %d.\n", ReadResult);
98 }
99 else if (ntohs(Command[0]) == CMD_Trigger_S)
100 {
101 printf ("# -> Stop Trigger\n");
102 break;
103 }
104
105 }
106 }
107 else if (ntohs(Command[0]) == CMD_Trigger_S)
108 {
109 printf ("# -> Stop Trigger\nThere was no Continious Trigger.\n");
110 }
111 else printf("# -> Command not supported.\n");
112
113 }
114
115
116
117CloseSockets (Sockets, 8);
118return 0;
119}
120
121
122int sendEvent(int socketd, unsigned long triggerID, int ROI) {
123cout << "I am here: triggerID: " << triggerID << endl;
124
125 // we need to generate an Eventheader
126 PEVNT_HEADER head;
127 // as well as 36 channels
128 PCHANNELHEADER chead;
129 // First we will fill the header
130 // in order to be able to send it directly
131 // I will hton() everything.
132
133 head.start_package_flag = hton((unsigned short)(0xFB01));
134 head.package_length = hton((unsigned short)(130+36*ROI+2));
135 head.version_no = hton((unsigned short)(0x0102));
136 head.trigger_id = hton((unsigned long)(triggerID));
137 head.trigger_type = hton((unsigned char)(0x55));
138 head.trigger_crc = hton((unsigned char)(0xAA));
139 head.local_trigger_id = hton((unsigned long)(triggerID));
140 head.local_trigger_type = hton((unsigned char)(0x55));
141 head.local_trigger_crc = hton((unsigned char)(0xAA));
142 head.board_id = hton((unsigned short)(0x0123));
143 for (int i=0; i<4; ++i){
144 head.drs_temperature[i] = hton(short(10+i));
145 }
146 for (int i=0; i<8; ++i){
147 head.dac[i] = hton((unsigned short)(i));
148 }
149
150 // now I will send the header.
151 write (socketd, &head, sizeof(PEVNT_HEADER));
152 cout << "header was sent away " << endl;
153
154 // next I will generate some channel data
155 // and send it in a loop
156 for (int ch=0; ch<9; ++ch) {
157 for (int drs=0; drs<4; ++drs) {
158 chead.channel_id = hton((unsigned short)(ch*0xFF00+drs));
159 chead.channel_start_cell = hton((unsigned short)(0));
160 chead.channel_roi = hton((unsigned short)(ROI));
161
162 unsigned short *data;
163 data = (unsigned short *) malloc( ROI * sizeof(unsigned short) );
164
165 for (int cell = 0; cell < ROI; ++cell){
166// data[(cell+20*(ch+drs*11))/2] = hton((unsigned short)(cell/(drs+1)));
167 data[cell] = hton((unsigned short)(cell+20*(drs*9+ch)));
168 }
169 write (socketd, &chead, sizeof(PCHANNELHEADER));
170 write (socketd, data, ROI*sizeof(unsigned short));
171 cout << "channel was sent away DRS:" << drs<<" CH:" << ch << endl;
172 }
173 }
174
175 // now I should better not forget to send the package end as well
176 unsigned short package_crc=hton((unsigned short)(0x1234)); // new in version 0x0101
177 unsigned short end_package_flag=hton((unsigned short)(0x04FE));
178
179 write (socketd, &package_crc, sizeof(unsigned short));
180 write (socketd, &end_package_flag, sizeof(unsigned short));
181
182
183 return 0;
184}
Note: See TracBrowser for help on using the repository browser.