ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcbPIX.hpp
Go to the documentation of this file.
1
7#ifndef ngcbPIX_H
8#define ngcbPIX_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15/*
16 * System Headers
17 */
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <errno.h>
23
24/*
25 * Local Headers
26 */
27#include <ngc/core/ngcb.h>
28#include <ngc/core/ngcbSocket.h>
29
35 int port;
36
38 unsigned int size;
39
41 unsigned int boards;
42
44 unsigned int channels;
45
47 unsigned int bytes;
48
50 unsigned int links;
51
54 port = 0; size = 8; channels = 4; boards = 1; bytes = 2; links = 1;
55 }
56
58 int Option(const char *name, const char *value) {
59 if (strcmp(name, "-fd") == 0)
60 {
61 return (1);
62 }
63 else if (strcmp(name, "-p") == 0)
64 {
65 ngcbStr2Int(value, &port);
66 if (port < 0) port = 0;
67 return (1);
68 }
69 else if (strcmp(name, "-s") == 0)
70 {
71 unsigned int n;
72 ngcbStr2UInt(value, &n);
73 if (n < 1) n = 1;
74 size = n * 4;
75 return (1);
76 }
77 else if (strcmp(name, "-b") == 0)
78 {
79 ngcbStr2UInt(value, &boards);
80 if (boards < 1) boards = 1;
81 return (1);
82 }
83 else if (strcmp(name, "-c") == 0)
84 {
85 ngcbStr2UInt(value, &channels);
86 if (channels < 1) channels = 1;
87 return (1);
88 }
89 else if (strcmp(name, "-nl") == 0)
90 {
91 ngcbStr2UInt(value, &links);
92 if (links < 1) links = 1;
93 return (1);
94 }
95 else if (strcmp(name, "-bitpix") == 0)
96 {
97 unsigned int bitPix;
98 ngcbStr2UInt(value, &bitPix);
99 bytes = bitPix / 8;
100 if (bytes < 1) bytes = 1;
101 return (1);
102 }
103 else
104 {
105 return (0);
106 }
107 }
108
110 void Usage(FILE *fd) {
111 fprintf(fd, "standard options:\n");
112 fprintf(fd, " -p <port> - service port\n");
113 fprintf(fd, " (default: 0)\n");
114 fprintf(fd, " -s <words> - ADC packet size (32-bit words)\n");
115 fprintf(fd, " (default: 2)\n");
116 fprintf(fd, " -b <boards> - number of ADC boards\n");
117 fprintf(fd, " (default: 1)\n");
118 fprintf(fd, " -c <channels> - channels per ADC board\n");
119 fprintf(fd, " (default: 4)\n");
120 fprintf(fd, " -bitpix <bits> - bits per pixel\n");
121 fprintf(fd, " (default: 16)\n");
122 }
123
124};
125
130{
131public:
133 ngcbPIX &operator=(const ngcbPIX &other) {
134 if (this != &other)
135 {
136 pktCfg = other.pktCfg;
137 Initialize_();
138 }
139 return (*this);
140 }
141
143 ngcbPIX(const ngcbPIX &other):pktCfg(other.pktCfg) {
144 Initialize_();
145 }
146
148 explicit ngcbPIX(const ngcb_pcfg_t &cfg):pktCfg(cfg) {
149 Initialize_();
150 }
151
153 virtual ~ngcbPIX() {
154 ngcbLog("shutdown service\n");
155
156 /*
157 * Disconnect client
158 */
159 Disconnect_();
160
161 /*
162 * Shutdown service
163 */
164 if (srv_ >= 0) ngcbTcpClose(srv_);
165
166 /*
167 * Free buffers
168 */
169 if (packetBuffer_ != NULL) free(packetBuffer_);
170 if (linkBuffer_ != NULL) free(linkBuffer_);
171 }
172
174 const char *ErrMsg() {return (erms_);}
175
177 int Port() {return (port_);}
178
180 int Initialized() {return (srv_ >= 0);}
181
183 void RstCnt() {cvt_ = 0;}
184
186 int Service() {
187
188 /*
189 * Main loop
190 */
191 ngcbLog("port = %d\n", port_);
192 while (Initialized())
193 {
194 /*
195 * Wait for client connection
196 */
197 if (Connect_() >= 0)
198 {
199 /*
200 * Packet loop
201 */
202 ngcbLog("connected\n");
203 while (Connected_()) Packet_();
204 ngcbLog("disconnected\n");
205 }
206 }
207
208 return (0);
209 }
210
211protected:
214
216 virtual unsigned short Sample16(size_t channel, size_t cvt) {
217 unsigned short pix;
218 pix = (unsigned short)channel;
219 return (pix);
220 }
221
223 virtual int Sample32(size_t channel, size_t cvt) {
224 int pix;
225 pix = (int)channel;
226 return (pix);
227 }
228
229private:
231 char erms_[256];
232
234 int port_;
235
237 int srv_;
238
240 int out_;
241
243 char *packetBuffer_;
244
246 char *linkBuffer_;
247
249 size_t size_;
250
252 size_t cvt_;
253
255 void Initialize_() {
256 erms_[0] = '\0';
257 port_ = 0;
258 out_ = -1;
259 srv_ = -1;
260 linkBuffer_ = NULL;
261 cvt_ = 0;
262 size_ = pktCfg.size * pktCfg.boards;
263 packetBuffer_ = (char *)malloc(size_);
264 if (packetBuffer_ == NULL)
265 {
266 strcpy(erms_, "cannot allocate packet buffer");
267 }
268 else
269 {
270 linkBuffer_ = (char *)malloc(size_);
271 if (linkBuffer_ == NULL)
272 {
273 strcpy(erms_, "cannot allocate link buffer");
274 free(packetBuffer_); packetBuffer_ = NULL;
275 }
276 else
277 {
278 /*
279 * Open service port
280 */
281 char s[32];
282 sprintf(s, "%d", pktCfg.port);
283 srv_ = ngcbTcpService(s, erms_);
284 if (srv_ >= 0)
285 {
286 /*
287 * Get actual port number
288 */
289 port_ = ngcbSocketPort(srv_);
290 if (port_ <= 0)
291 {
292 strcpy(erms_, "invalid port");
293 free(linkBuffer_); linkBuffer_ = NULL;
294 free(packetBuffer_); packetBuffer_ = NULL;
295 ngcbTcpClose(srv_); srv_ = -1;
296 }
297 }
298 else
299 {
300 free(linkBuffer_); linkBuffer_ = NULL;
301 free(packetBuffer_); packetBuffer_ = NULL;
302 }
303 }
304 }
305 }
306
308 int Connected_() {return (out_ >= 0);}
309
311 int Connect_() {
312
313 RstCnt();
314
315 if (srv_ < 0)
316 {
317 strcpy(erms_, "service not established");
318 return (-1);
319 }
320 else if (ngcbSocketWait(srv_, -1, erms_) != 0)
321 {
322 return (-1);
323 }
324 else
325 {
326 out_ = ngcbTcpAccept(srv_, erms_);
327 return (out_);
328 }
329 }
330
332 void Disconnect_() {
333 if (out_ >= 0)
334 {
335 ngcbTcpClose(out_); out_ = -1;
336 }
337 }
338
340 void Packet_() {
341 size_t b;
342 size_t nPix = pktCfg.size / pktCfg.bytes; // pixels per packet per board
343 size_t c0 = 0;
344 char *buffer;
345 unsigned int links;
346
347 if (pktCfg.links > 1)
348 {
349 buffer = linkBuffer_;
350 links = pktCfg.links;
351 }
352 else
353 {
354 buffer = packetBuffer_;
355 links = 1;
356 }
357
358 if (pktCfg.bytes == 2)
359 {
360 unsigned short *p = (unsigned short *)buffer;
361 for (b=0;b<pktCfg.boards;b++)
362 {
363 size_t n, c;
364 for (n=0;n<nPix/pktCfg.channels;n++)
365 {
366 for (c=0;c<pktCfg.channels;c++) *p++ = Sample16(c0 + c, cvt_ + n);
367 }
368 c0 += pktCfg.channels;
369 }
370 }
371 else if (pktCfg.bytes == 4)
372 {
373 int *p = (int *)buffer;
374 for (b=0;b<pktCfg.boards;b++)
375 {
376 size_t n, c;
377 for (n=0;n<nPix/pktCfg.channels;n++)
378 {
379 for (c=0;c<pktCfg.channels;c++) *p++ = Sample32(c0 + c, cvt_ + n);
380 }
381 c0 += pktCfg.channels;
382 }
383 }
384 else
385 {
386 memset(packetBuffer_, 0, size_); // just fill packet buffer with zero
387 links = 1;
388 }
389
390 if (links > 1)
391 {
392 /*
393 * Our data is in the link buffer - we still have to distribute it
394 */
395 size_t i, j;
396 size_t chunk = size_ / links;
397 size_t x = links * 8;
398
399 for (i=0;i<links;i++)
400 {
401 char *b1 = linkBuffer_ + (i * chunk);
402 char *b2 = packetBuffer_ + (i * 8);
403 for (j=0;j<links;j++)
404 {
405 memcpy(b2, b1, 8);
406 b1 += 8;
407 b2 += x;
408 }
409 }
410 }
411
412 if (ngcbTcpWrite(out_, packetBuffer_, size_, erms_) != 0) Disconnect_();
413
414 cvt_ += (nPix / pktCfg.channels);
415 }
416
417};
418
419#endif
420
421
422
int Service()
Service loop.
Definition ngcbPIX.hpp:186
ngcbPIX & operator=(const ngcbPIX &other)
Operator =.
Definition ngcbPIX.hpp:133
ngcbPIX(const ngcb_pcfg_t &cfg)
Constructor.
Definition ngcbPIX.hpp:148
virtual unsigned short Sample16(size_t channel, size_t cvt)
16-bit sample
Definition ngcbPIX.hpp:216
int Initialized()
Initialized.
Definition ngcbPIX.hpp:180
ngcb_pcfg_t pktCfg
Packet service configuration.
Definition ngcbPIX.hpp:213
ngcbPIX(const ngcbPIX &other)
Copy constructor.
Definition ngcbPIX.hpp:143
const char * ErrMsg()
Return error message.
Definition ngcbPIX.hpp:174
virtual ~ngcbPIX()
Destructor.
Definition ngcbPIX.hpp:153
void RstCnt()
Reset convert counter.
Definition ngcbPIX.hpp:183
int Port()
Return actual service port.
Definition ngcbPIX.hpp:177
virtual int Sample32(size_t channel, size_t cvt)
32-bit sample
Definition ngcbPIX.hpp:223
int ngcbSocketPort(int fd)
Definition ngcbSocket.c:201
int ngcbTcpClose(int fd)
Definition ngcbTcp.c:401
int ngcbTcpWrite(int fd, const void *buffer, size_t size, char *erms)
Definition ngcbTcp.c:459
int ngcbTcpService(const char *service, char *erms)
Definition ngcbTcp.c:38
int ngcbTcpAccept(int fd, char *erms)
Definition ngcbTcp.c:134
int ngcbSocketWait(int fd, int timeout, char *erms)
Definition ngcbSocket.c:241
#define ngcbLog(...)
Definition ngcb.h:118
int ngcbStr2UInt(const char *s, unsigned int *value)
Definition ngcbString.c:197
int ngcbStr2Int(const char *s, int *value)
Definition ngcbString.c:185
Definition ngcbPIX.hpp:33
int Option(const char *name, const char *value)
Handle command line option.
Definition ngcbPIX.hpp:58
unsigned int links
Number of links.
Definition ngcbPIX.hpp:50
unsigned int bytes
Bytes per pixel.
Definition ngcbPIX.hpp:47
int port
Port number.
Definition ngcbPIX.hpp:35
unsigned int size
Packet size (bytes)
Definition ngcbPIX.hpp:38
unsigned int boards
Number of boards.
Definition ngcbPIX.hpp:41
unsigned int channels
Number of channels per board.
Definition ngcbPIX.hpp:44
void Usage(FILE *fd)
Print option usage.
Definition ngcbPIX.hpp:110
ngcb_pcfg_t()
Constructor.
Definition ngcbPIX.hpp:53