-
Notifications
You must be signed in to change notification settings - Fork 29
/
devMrfBufTx.cpp
180 lines (148 loc) · 4.34 KB
/
devMrfBufTx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdlib.h>
#include <math.h>
#include <dbAccess.h>
#include <devSup.h>
#include <recGbl.h>
#include <devLib.h> // For S_dev_*
#include <alarm.h>
#include <mbboRecord.h>
#include <waveformRecord.h>
#include <menuFtype.h>
#include <epicsEndian.h>
#ifdef _WIN32
#include <Winsock2.h>
#else
// for htons() et al.
#include <netinet/in.h> // on rtems
#include <arpa/inet.h> // on linux
#endif
#include "linkoptions.h"
#include "devObj.h"
#include "mrf/databuf.h"
#include <stdexcept>
#include <string>
#include <cfloat>
#include <epicsExport.h>
struct s_priv
{
char obj[40];
epicsUInt32 proto;
char prop[20];
dataBufTx *priv;
epicsUInt8 *scratch;
};
static const
linkOptionDef eventdef[] =
{
linkString (s_priv, obj , "OBJ" , 1, 0),
linkInt32 (s_priv, proto, "Proto", 1, 0),
linkString (s_priv, prop , "P", 1, 0),
linkOptionEnd
};
static long add_record_waveform(dbCommon *praw)
{
waveformRecord *prec=(waveformRecord*)praw;
long ret=0;
try {
assert(prec->inp.type==INST_IO);
mrf::auto_ptr<s_priv> paddr(new s_priv);
if (linkOptionsStore(eventdef, paddr.get(), prec->inp.value.instio.string, 0))
throw std::runtime_error("Couldn't parse link string");
mrf::Object *O=mrf::Object::getObject(paddr->obj);
if(!O) {
errlogPrintf("%s: failed to find object '%s'\n", prec->name, paddr->obj);
return S_db_errArg;
}
paddr->priv=dynamic_cast<dataBufTx*>(O);
if(!paddr->priv)
throw std::runtime_error("Failed to lookup device");
// scratch space for endian swap if needed
if(dbValueSize(prec->ftvl)>1 && dbValueSize(prec->ftvl)<=8)
paddr->scratch = new epicsUInt8[prec->nelm*dbValueSize(prec->ftvl)];
else
paddr->scratch = 0;
// prec->dpvt is set again to indicate
// This also serves to indicate successful
// initialization to other dset functions
prec->dpvt = paddr.release();
return 0;
} catch(std::runtime_error& e) {
recGblRecordError(S_dev_noDevice, (void*)prec, e.what());
ret=S_dev_noDevice;
} catch(std::exception& e) {
recGblRecordError(S_db_noMemory, (void*)prec, e.what());
ret=S_db_noMemory;
}
return ret;
}
static long del_record_waveform(dbCommon *praw)
{
long ret=0;
if (!praw->dpvt) return 0;
try {
mrf::auto_ptr<s_priv> paddr((s_priv*)praw->dpvt);
praw->dpvt = 0;
delete[] paddr->scratch;
} catch(std::runtime_error& e) {
recGblRecordError(S_dev_noDevice, (void*)praw, e.what());
ret=S_dev_noDevice;
} catch(std::exception& e) {
recGblRecordError(S_db_noMemory, (void*)praw, e.what());
ret=S_db_noMemory;
}
return ret;
}
static long write_waveform(waveformRecord* prec)
{
if (!prec->dpvt) {(void)recGblSetSevr(prec, COMM_ALARM, INVALID_ALARM); return -1; }
try {
s_priv *paddr=static_cast<s_priv*>(prec->dpvt);
epicsUInt32 capacity=paddr->priv->lenMax();
const long esize=dbValueSize(prec->ftvl);
epicsUInt32 requested=prec->nord*esize;
if (requested > capacity)
requested=capacity;
epicsUInt8 *buf;
if(esize==1 || esize>8)
buf=static_cast<epicsUInt8*>(prec->bptr);
else {
buf=paddr->scratch;
for(size_t i=0; i<requested; i+=esize) {
switch(esize) {
case 2:
*(epicsUInt16*)(buf+i) = htons( *(epicsUInt16*)((char*)prec->bptr+i) );
break;
case 4:
*(epicsUInt32*)(buf+i) = htonl( *(epicsUInt32*)((char*)prec->bptr+i) );
break;
case 8:
#if EPICS_BYTE_ORDER == EPICS_ENDIAN_BIG
*(epicsUInt32*)(buf+i) = *(epicsUInt32*)((char*)prec->bptr+i);
*(epicsUInt32*)(buf+i+4) = *(epicsUInt32*)((char*)prec->bptr+i+4);
#else
*(epicsUInt32*)(buf+i+4) = htonl( *(epicsUInt32*)((char*)prec->bptr+i) );
*(epicsUInt32*)(buf+i) = htonl( *(epicsUInt32*)((char*)prec->bptr+i+4) );
#endif
break;
}
}
}
paddr->priv->dataSend(requested,buf);
return 0;
} catch(std::exception& e) {
recGblRecordError(S_db_noMemory, (void*)prec, e.what());
return S_db_noMemory;
}
}
/*************** boiler plate *******************/
extern "C" {
dsxt dxtwaveformBufTx={add_record_waveform,del_record_waveform};
static common_dset devwaveformoutdataBufTx = {
6, NULL,
dset_cast(&init_dset<&dxtwaveformBufTx>),
(DEVSUPFUN) init_record_empty,
NULL,
dset_cast(&write_waveform),
NULL };
epicsExportAddress(dset,devwaveformoutdataBufTx);
}