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