Merge ~bfrk/epics-base:address-modifiers into ~epics-core/epics-base/+git/epics-base:7.0

Proposed by Ben Franksen
Status: Needs review
Proposed branch: ~bfrk/epics-base:address-modifiers
Merge into: ~epics-core/epics-base/+git/epics-base:7.0
Diff against target: 1262 lines (+847/-99)
16 files modified
modules/database/src/ioc/db/Makefile (+3/-0)
modules/database/src/ioc/db/arrayRangeModifier.c (+167/-0)
modules/database/src/ioc/db/arrayRangeModifier.h (+69/-0)
modules/database/src/ioc/db/dbAccess.c (+36/-15)
modules/database/src/ioc/db/dbAccessDefs.h (+10/-0)
modules/database/src/ioc/db/dbAddrModifier.h (+48/-0)
modules/database/src/ioc/db/dbChannel.c (+21/-63)
modules/database/src/ioc/db/dbChannel.h (+3/-0)
modules/database/src/ioc/db/dbDbLink.c (+1/-2)
modules/database/src/ioc/db/dbUnitTest.c (+2/-2)
modules/database/src/std/filters/arr.c (+1/-17)
modules/database/test/std/rec/Makefile (+11/-0)
modules/database/test/std/rec/addrModifierTest.c (+247/-0)
modules/database/test/std/rec/addrModifierTest.db (+24/-0)
modules/database/test/std/rec/arroutRecord.c (+169/-0)
modules/database/test/std/rec/arroutRecord.dbd (+35/-0)
Reviewer Review Type Date Requested Status
EPICS Core Developers Pending
Review via email: mp+400517@code.launchpad.net

This proposal supersedes a proposal from 2020-04-03.

Description of the change

As discussed on core-talk, this enables interpretation of "array address modifiers" a la "PV[s:i:e]" when using dbChannelPut or dbChannelPutField. This means the feature works for all PV links (I changed DB links to call dbChannelPut) as well as for caput.

The work is based on Dirk's dbChannelForDBLink branch (merged into 7.0) and some of my previous work (see branch address-modifiers-prerequisites).

The current implementation is /very/ liberal in that it never fails if the address modifier is out-of-bounds wrt the (target) record field. In this case the put operation is truncated to the overlap between the modifier and the target array, which may be empty (i.e. no change in the target). Similarly, if the source array has too few elements or excess elements, relative to the modifier, the request gets truncated to the smaller of these numbers.

Another design decision was that a put operation with a modifier never changes the current number of elements of the target. So you cannot currently "extend" an array using a modifier by writing beyond its current size.

All of these particular behaviors are, of course, open for debate. I can easily change any aspect as the implementation is quite simple.

To post a comment you must log in.
Revision history for this message
Andrew Johnson (anj) wrote : Posted in a previous version of this proposal

Hi Ben, this looks very interesting; could you take it a little further? See my diff comments, in particular the one in dbAddrModifier.h where I speculate about enabling future modifiers. I don't have any arguments with your design decisions as described, and the code generally looks nice.

Revision history for this message
Ben Franksen (bfrk) wrote : Posted in a previous version of this proposal
Download full text (3.8 KiB)

Hi Andrew, thanks for your comments.

>> +long dbPutModifier(DBADDR *paddr, short dbrType,
>> + const void *pbuffer, long nRequest, dbAddrModifier *pmod)
>> {
>> dbCommon *precord = paddr->precord;
>> short field_type = paddr->field_type;
>> + short field_size = paddr->field_size;
>> long no_elements = paddr->no_elements;
>> long special = paddr->special;
>> void *pfieldsave = paddr->pfield;
>> rset *prset = dbGetRset(paddr);
>> long status = 0;
>> + long available_no_elements = no_elements;
>
> s/available_no_elements/capacity/ maybe? It's a lot shorter...

Ah, but it's used with the opposite meaning: while initialized with the
capacity, it gets overwritten with the /current/ number of elements by
get_array_info which was previously ignored (passing &dummy).

> Please add the license header to all new source files; the Copyright lines aren't essential, but the 2 lines starting "EPICS BASE is distributed..." should be included.

Will do.

> I could see us wanting to add more kinds of modifiers in the future, although I'm not looking to make them pluggable at run-time. (NB: I'm speculating here).

Should that allow to combine multiple address modifiers, like for
filters? This would be very hard to get right. For the moment I will
assume that only one address modifier can be given per channel.

(Run-time pluggability indeed seems excessive, given that we have to
restart IOCs anyway whenever the database gets re-configured i.e.
records added or removed).

> Could you extract the array-specific operations and data and make dbAddrModifier polymorphic, with the implementation collected into a separate source file maybe, so other modifiers could be added too?

Separating out the code I added to dbPut was already on my TODO list.
Especially after adding ad-hoc index lists as another form of modifier
as you had suggested previously, it became clear that this doesn't
scale. So I will start with this move and then see if/how I can make
address modifiers polymorphic.

> I still want to be able to create a put filter that could accept a JSON string as the value, but this implementation currently hard-codes the dbAddrModifier to only support array filters. My filter doesn't have to use JSON in the PV name modifier, but it does need to do different things when the value comes in and I don't want to have to add that processing code to the dbAccess.c file. Maybe you could even extract the code for the '$' modifier into a separate parse-time routine too?
>
> I'm trying to increase modularity here.

I understand your point. Be warned though that this will increase the
overall complexity, not sure yet how much. Also, and unfortunately,
polymorphism in C is always kind of hazardous and an invitation for
stupid mistakes that should be type errors but can't because the type
checker is too dumb (see the error I recently made with &prec->val
versus prec->val).

>> +/** @brief The address modifier that modifies nothing. */
>> +#define defaultAddrModifier (struct dbAddrModifier){0,1,-1}
>
> Not sure that all our supported C compilers can accept this syntax, but this macro isn't actually being used anywhere in you...

Read more...

Revision history for this message
Ben Franksen (bfrk) wrote : Posted in a previous version of this proposal

@Andrew I have addressed most of the issues you raised and made the address modifier API polymorphic.

The parsing is not yet encapsulated. There is a trade-off here: we use the same syntax as a short-cut for filters. Not sure what to do here; perhaps create a separate module just for this parser. I'll have to experiment to see if that results in a better structure.

Revision history for this message
Ben Franksen (bfrk) wrote :

Resubmitted this MR to drop obsolete prerequisites.

Unmerged commits

8ca85b3... by Ben Franksen

fix (justified) warnings in dbUnitTest.c

aaf6163... by Ben Franksen

move array range parsing to arrayRangeModifier module

6aaf531... by Ben Franksen

fix API docs in arrayRangeModifier.h

a30969c... by Ben Franksen

address modifier API is now polymorphic

There is still only one implementation, the array range modifier, but
alternative ones could be added now. The concrete behavior of the array
range modifier is unchanged and merely hidden in a module. However parsing
is still part of the dbChannel module. Disentangling this part from
dbChannel will require a bit more restructuring.

Since the generic dbAddrModifier now has a small constant size (two
pointers), it makes more sense to embed it fully inside dbChannel.

8f4ed7d... by Ben Franksen

factor out dbHandleModifier from dbPutModifier

This is the first step in changing address modifiers to be polymorphic.

f0dbd54... by Ben Franksen

add tests for address modifiers in output links

2ca474f... by Ben Franksen

add support for array address modifiers on put

This is independent from server-side filters. It only works with the bracket
notation "PV[s:i:e]", and not with the JSON filter syntax. However, we
re-use the function wrapArrayIndices from the array filters, moving it to
the ioc/db layer.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/modules/database/src/ioc/db/Makefile b/modules/database/src/ioc/db/Makefile
index ebb78e2..e590cc6 100644
--- a/modules/database/src/ioc/db/Makefile
+++ b/modules/database/src/ioc/db/Makefile
@@ -11,10 +11,12 @@
1111
12SRC_DIRS += $(IOCDIR)/db12SRC_DIRS += $(IOCDIR)/db
1313
14INC += arrayRangeModifier.h
14INC += callback.h15INC += callback.h
15INC += dbAccess.h16INC += dbAccess.h
16INC += dbAccessDefs.h17INC += dbAccessDefs.h
17INC += dbAddr.h18INC += dbAddr.h
19INC += dbAddrModifier.h
18INC += dbBkpt.h20INC += dbBkpt.h
19INC += dbCa.h21INC += dbCa.h
20INC += dbChannel.h22INC += dbChannel.h
@@ -66,6 +68,7 @@ HTMLS += dbCommonRecord.html
66HTMLS += dbCommonInput.html68HTMLS += dbCommonInput.html
67HTMLS += dbCommonOutput.html69HTMLS += dbCommonOutput.html
6870
71dbCore_SRCS += arrayRangeModifier.c
69dbCore_SRCS += dbLock.c72dbCore_SRCS += dbLock.c
70dbCore_SRCS += dbAccess.c73dbCore_SRCS += dbAccess.c
71dbCore_SRCS += dbBkpt.c74dbCore_SRCS += dbBkpt.c
diff --git a/modules/database/src/ioc/db/arrayRangeModifier.c b/modules/database/src/ioc/db/arrayRangeModifier.c
72new file mode 10064475new file mode 100644
index 0000000..1feb7bc
--- /dev/null
+++ b/modules/database/src/ioc/db/arrayRangeModifier.c
@@ -0,0 +1,167 @@
1/*************************************************************************\
2* EPICS BASE is distributed subject to a Software License Agreement found
3* in file LICENSE that is included with this distribution.
4\*************************************************************************/
5
6#include <assert.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10#include "arrayRangeModifier.h"
11#include "dbAccessDefs.h"
12#include "dbConvertFast.h"
13#include "dbConvert.h"
14
15typedef struct {
16 long start;
17 long incr;
18 long end;
19} arrayRangeModifier;
20
21long wrapArrayIndices(long *start, const long increment,
22 long *end, const long no_elements)
23{
24 if (*start < 0) *start = no_elements + *start;
25 if (*start < 0) *start = 0;
26 if (*start > no_elements) *start = no_elements;
27
28 if (*end < 0) *end = no_elements + *end;
29 if (*end < 0) *end = 0;
30 if (*end >= no_elements) *end = no_elements - 1;
31
32 if (*end - *start >= 0)
33 return 1 + (*end - *start) / increment;
34 else
35 return 0;
36}
37
38static long handleArrayRangeModifier(DBADDR *paddr, short dbrType, const void
39 *pbuffer, long nRequest, void *pvt, long offset, long available)
40{
41 arrayRangeModifier *pmod = (arrayRangeModifier *)pvt;
42 long status = 0;
43 long start = pmod->start;
44 long end = pmod->end;
45 /* Note that this limits the return value to be <= available */
46 long n = wrapArrayIndices(&start, pmod->incr, &end, available);
47
48 assert(pmod->incr > 0);
49 if (pmod->incr > 1) {
50 long i, j;
51 long (*putCvt) (const void *from, void *to, const dbAddr * paddr) =
52 dbFastPutConvertRoutine[dbrType][paddr->field_type];
53 short dbr_size = dbValueSize(dbrType);
54 if (nRequest > n)
55 nRequest = n;
56 for (i = 0, j = (offset + start) % paddr->no_elements; i < nRequest;
57 i++, j = (j + pmod->incr) % paddr->no_elements) {
58 status = putCvt(pbuffer + (i * dbr_size),
59 paddr->pfield + (j * paddr->field_size), paddr);
60 }
61 } else {
62 offset = (offset + start) % paddr->no_elements;
63 if (nRequest > n)
64 nRequest = n;
65 if (paddr->no_elements <= 1) {
66 status = dbFastPutConvertRoutine[dbrType][paddr->field_type] (pbuffer,
67 paddr->pfield, paddr);
68 } else {
69 if (paddr->no_elements < nRequest)
70 nRequest = paddr->no_elements;
71 status = dbPutConvertRoutine[dbrType][paddr->field_type] (paddr,
72 pbuffer, nRequest, paddr->no_elements, offset);
73 }
74 }
75 return status;
76}
77
78long createArrayRangeModifier(dbAddrModifier *pmod, long start, long incr, long end)
79{
80 arrayRangeModifier *pvt =
81 (arrayRangeModifier *) malloc(sizeof(arrayRangeModifier));
82 if (incr <= 0) {
83 return S_db_errArg;
84 }
85 if (!pvt) {
86 return S_db_noMemory;
87 }
88 pvt->start = start;
89 pvt->incr = incr;
90 pvt->end = end;
91 pmod->pvt = pvt;
92 pmod->handle = handleArrayRangeModifier;
93 return 0;
94}
95
96void deleteArrayRangeModifier(dbAddrModifier *pmod)
97{
98 if (pmod->pvt)
99 free(pmod->pvt);
100}
101
102long parseArrayRange(dbAddrModifier *pmod, const char **pstring)
103{
104 long start = 0;
105 long end = -1;
106 long incr = 1;
107 long tmp;
108 const char *pcurrent = *pstring;
109 char *pnext;
110 ptrdiff_t advance;
111 long status = 0;
112
113 if (*pcurrent != '[') {
114 return -1;
115 }
116 pcurrent++;
117 /* If no number is present, strtol() returns 0 and sets pnext=pcurrent,
118 else pnext points to the first char after the number */
119 tmp = strtol(pcurrent, &pnext, 0);
120 advance = pnext - pcurrent;
121 if (advance) start = tmp;
122 pcurrent = pnext;
123 if (*pcurrent == ']' && advance) {
124 end = start;
125 goto done;
126 }
127 if (*pcurrent != ':') {
128 return -1;
129 }
130 pcurrent++;
131 tmp = strtol(pcurrent, &pnext, 0);
132 advance = pnext - pcurrent;
133 pcurrent = pnext;
134 if (*pcurrent == ']') {
135 if (advance) end = tmp;
136 goto done;
137 }
138 if (advance) incr = tmp;
139 if (*pcurrent != ':') {
140 return -1;
141 }
142 pcurrent++;
143 tmp = strtol(pcurrent, &pnext, 0);
144 advance = pnext - pcurrent;
145 if (advance) end = tmp;
146 pcurrent = pnext;
147 if (*pcurrent != ']') {
148 return -1;
149 }
150
151done:
152 status = createArrayRangeModifier(pmod, start, incr, end);
153 if (status) {
154 return status;
155 }
156 pcurrent++;
157 *pstring = pcurrent;
158 return 0;
159}
160
161void getArrayRange(dbAddrModifier *pmod, long *pstart, long *pincr, long *pend)
162{
163 arrayRangeModifier *pvt = (arrayRangeModifier *)pmod->pvt;
164 *pstart = pvt->start;
165 *pincr = pvt->incr;
166 *pend = pvt->end;
167}
diff --git a/modules/database/src/ioc/db/arrayRangeModifier.h b/modules/database/src/ioc/db/arrayRangeModifier.h
0new file mode 100644168new file mode 100644
index 0000000..8e0cd2f
--- /dev/null
+++ b/modules/database/src/ioc/db/arrayRangeModifier.h
@@ -0,0 +1,69 @@
1/*************************************************************************\
2* EPICS BASE is distributed subject to a Software License Agreement found
3* in file LICENSE that is included with this distribution.
4\*************************************************************************/
5#ifndef ARRAYRANGEMODIFIER_H
6#define ARRAYRANGEMODIFIER_H
7
8#include "dbAddrModifier.h"
9#include "shareLib.h"
10
11/** @brief The array range address modifier. */
12
13/** @brief Given a number of elements, convert negative start and end indices
14 * to non-negative ones by counting from the end of the array.
15 *
16 * @param start Pointer to possibly negative start index
17 * @param increment Increment
18 * @param end Pointer to possibly negative end index
19 * @param no_elements Number of array elements
20 * @return Final number of elements
21 */
22epicsShareFunc
23long wrapArrayIndices(long *start, long increment, long *end, long no_elements);
24
25/** @brief Create an array range modifier from start index, increment, and end index
26 *
27 * @param pmod Pointer to address modifier (user allocated)
28 * @param start Start index (possibly negative)
29 * @param incr Increment (must be positive)
30 * @param end End index (possibly negative)
31 * @return Status
32 */
33epicsShareFunc
34long createArrayRangeModifier(dbAddrModifier *pmod, long start, long incr, long end);
35
36/** @brief Alternative creation function that parses the data from a string.
37 *
38 * If successful, advances 'pstring' to point after the recognized address modifier.
39 *
40 * @param pmod Pointer to uninitialized address modifier (user allocated)
41 * @param pstring Pointer to string to parse (in/out)
42 * @return 0 (success) or -1 (failure)
43 */
44epicsShareFunc
45long parseArrayRange(dbAddrModifier *pmod, const char **pstring);
46
47/** @brief Extract the private data
48 *
49 * The 'pmod' argument must point to a valid array range address modifier.
50 * The other aruments must point to appropriately sized storage.
51 *
52 * @param pmod Pointer to uninitialized address modifier (user allocated)
53 * @param pstart Start index (out)
54 * @param pincr Increment (out)
55 * @param pend End index (out)
56 */
57epicsShareFunc
58void getArrayRange(dbAddrModifier *pmod, long *pstart, long *pincr, long *pend);
59
60/** @brief Free private memory associated with an array range modifier
61 *
62 * Note this does not free 'pmod' which is always user allocated.
63 *
64 * @param pmod Pointer to address modifier (user allocated)
65 */
66epicsShareFunc
67void deleteArrayRangeModifier(dbAddrModifier *pmod);
68
69#endif /* ARRAYRANGEMODIFIER_H */
diff --git a/modules/database/src/ioc/db/dbAccess.c b/modules/database/src/ioc/db/dbAccess.c
index bac208f..52169c0 100644
--- a/modules/database/src/ioc/db/dbAccess.c
+++ b/modules/database/src/ioc/db/dbAccess.c
@@ -40,6 +40,7 @@
40#include "callback.h"40#include "callback.h"
41#include "dbAccessDefs.h"41#include "dbAccessDefs.h"
42#include "dbAddr.h"42#include "dbAddr.h"
43#include "dbAddrModifier.h"
43#include "dbBase.h"44#include "dbBase.h"
44#include "dbBkpt.h"45#include "dbBkpt.h"
45#include "dbCommonPvt.h"46#include "dbCommonPvt.h"
@@ -1225,6 +1226,12 @@ cleanup:
1225long dbPutField(DBADDR *paddr, short dbrType,1226long dbPutField(DBADDR *paddr, short dbrType,
1226 const void *pbuffer, long nRequest)1227 const void *pbuffer, long nRequest)
1227{1228{
1229 return dbPutFieldModifier(paddr, dbrType, pbuffer, nRequest, NULL);
1230}
1231
1232long dbPutFieldModifier(DBADDR *paddr, short dbrType,
1233 const void *pbuffer, long nRequest, dbAddrModifier *pmod)
1234{
1228 long status = 0;1235 long status = 0;
1229 long special = paddr->special;1236 long special = paddr->special;
1230 dbFldDes *pfldDes = paddr->pfldDes;1237 dbFldDes *pfldDes = paddr->pfldDes;
@@ -1242,7 +1249,7 @@ long dbPutField(DBADDR *paddr, short dbrType,
1242 return dbPutFieldLink(paddr, dbrType, pbuffer, nRequest);1249 return dbPutFieldLink(paddr, dbrType, pbuffer, nRequest);
12431250
1244 dbScanLock(precord);1251 dbScanLock(precord);
1245 status = dbPut(paddr, dbrType, pbuffer, nRequest);1252 status = dbPutModifier(paddr, dbrType, pbuffer, nRequest, pmod);
1246 if (status == 0) {1253 if (status == 0) {
1247 if (paddr->pfield == &precord->proc ||1254 if (paddr->pfield == &precord->proc ||
1248 (pfldDes->process_passive &&1255 (pfldDes->process_passive &&
@@ -1296,8 +1303,13 @@ static long putAcks(DBADDR *paddr, const void *pbuffer, long nRequest,
1296 return 0;1303 return 0;
1297}1304}
12981305
1299long dbPut(DBADDR *paddr, short dbrType,1306long dbPut(DBADDR *paddr, short dbrType, const void *pbuffer, long nRequest)
1300 const void *pbuffer, long nRequest)1307{
1308 return dbPutModifier(paddr, dbrType, pbuffer, nRequest, NULL);
1309}
1310
1311long dbPutModifier(DBADDR *paddr, short dbrType,
1312 const void *pbuffer, long nRequest, dbAddrModifier *pmod)
1301{1313{
1302 dbCommon *precord = paddr->precord;1314 dbCommon *precord = paddr->precord;
1303 short field_type = paddr->field_type;1315 short field_type = paddr->field_type;
@@ -1306,6 +1318,7 @@ long dbPut(DBADDR *paddr, short dbrType,
1306 void *pfieldsave = paddr->pfield;1318 void *pfieldsave = paddr->pfield;
1307 rset *prset = dbGetRset(paddr);1319 rset *prset = dbGetRset(paddr);
1308 long status = 0;1320 long status = 0;
1321 long available_no_elements = no_elements;
1309 long offset;1322 long offset;
1310 dbFldDes *pfldDes;1323 dbFldDes *pfldDes;
1311 int isValueField;1324 int isValueField;
@@ -1332,25 +1345,33 @@ long dbPut(DBADDR *paddr, short dbrType,
13321345
1333 if (paddr->pfldDes->special == SPC_DBADDR &&1346 if (paddr->pfldDes->special == SPC_DBADDR &&
1334 prset && prset->get_array_info) {1347 prset && prset->get_array_info) {
1335 long dummy;
13361348
1337 status = prset->get_array_info(paddr, &dummy, &offset);1349 status = prset->get_array_info(paddr, &available_no_elements, &offset);
1338 /* paddr->pfield may be modified */1350 /* paddr->pfield may be modified */
1339 if (status) goto done;1351 if (status) goto done;
1340 if (no_elements < nRequest)1352 } else
1341 nRequest = no_elements;1353 offset = 0;
1342 status = dbPutConvertRoutine[dbrType][field_type](paddr, pbuffer,1354
1343 nRequest, no_elements, offset);1355 if (pmod && pmod->handle && pmod->pvt) {
1344 /* update array info */1356 status = pmod->handle(paddr, dbrType, pbuffer, nRequest,
1345 if (!status && prset->put_array_info)1357 pmod->pvt, offset, available_no_elements);
1346 status = prset->put_array_info(paddr, nRequest);
1347 } else {1358 } else {
1348 if (nRequest < 1) {1359 if (no_elements <= 1) {
1349 recGblSetSevr(precord, LINK_ALARM, INVALID_ALARM);
1350 } else {
1351 status = dbFastPutConvertRoutine[dbrType][field_type](pbuffer,1360 status = dbFastPutConvertRoutine[dbrType][field_type](pbuffer,
1352 paddr->pfield, paddr);1361 paddr->pfield, paddr);
1353 nRequest = 1;1362 nRequest = 1;
1363 } else {
1364 if (no_elements < nRequest)
1365 nRequest = no_elements;
1366 status = dbPutConvertRoutine[dbrType][field_type](paddr, pbuffer,
1367 nRequest, no_elements, offset);
1368 }
1369
1370 /* update array info unless writing failed */
1371 if (!status &&
1372 paddr->pfldDes->special == SPC_DBADDR &&
1373 prset && prset->put_array_info) {
1374 status = prset->put_array_info(paddr, nRequest);
1354 }1375 }
1355 }1376 }
13561377
diff --git a/modules/database/src/ioc/db/dbAccessDefs.h b/modules/database/src/ioc/db/dbAccessDefs.h
index f2cf091..45e9f6e 100644
--- a/modules/database/src/ioc/db/dbAccessDefs.h
+++ b/modules/database/src/ioc/db/dbAccessDefs.h
@@ -27,7 +27,9 @@
2727
28#include "dbBase.h"28#include "dbBase.h"
29#include "dbAddr.h"29#include "dbAddr.h"
30#include "dbAddrModifier.h"
30#include "recSup.h"31#include "recSup.h"
32#include "db_field_log.h"
3133
32#ifdef __cplusplus34#ifdef __cplusplus
33extern "C" {35extern "C" {
@@ -247,11 +249,19 @@ epicsShareFunc long dbGetField(
247epicsShareFunc long dbGet(249epicsShareFunc long dbGet(
248 struct dbAddr *,short dbrType,void *pbuffer,long *options,250 struct dbAddr *,short dbrType,void *pbuffer,long *options,
249 long *nRequest,void *pfl);251 long *nRequest,void *pfl);
252
250epicsShareFunc long dbPutField(253epicsShareFunc long dbPutField(
251 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest);254 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest);
252epicsShareFunc long dbPut(255epicsShareFunc long dbPut(
253 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest);256 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest);
254257
258epicsShareFunc long dbPutFieldModifier(
259 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest,
260 dbAddrModifier *pmod);
261epicsShareFunc long dbPutModifier(
262 struct dbAddr *,short dbrType,const void *pbuffer,long nRequest,
263 dbAddrModifier *pmod);
264
255typedef void(*SPC_ASCALLBACK)(struct dbCommon *);265typedef void(*SPC_ASCALLBACK)(struct dbCommon *);
256/*dbSpcAsRegisterCallback called by access security */266/*dbSpcAsRegisterCallback called by access security */
257epicsShareFunc void dbSpcAsRegisterCallback(SPC_ASCALLBACK func);267epicsShareFunc void dbSpcAsRegisterCallback(SPC_ASCALLBACK func);
diff --git a/modules/database/src/ioc/db/dbAddrModifier.h b/modules/database/src/ioc/db/dbAddrModifier.h
258new file mode 100644268new file mode 100644
index 0000000..dcc3c86
--- /dev/null
+++ b/modules/database/src/ioc/db/dbAddrModifier.h
@@ -0,0 +1,48 @@
1/*************************************************************************\
2* EPICS BASE is distributed subject to a Software License Agreement found
3* in file LICENSE that is included with this distribution.
4\*************************************************************************/
5#ifndef DBADDRMODIFIER_H
6#define DBADDRMODIFIER_H
7
8#include "dbAddr.h"
9
10/** @brief Generic API for address modifiers. */
11
12/** @brief Type of functions that handle an address modifier.
13 *
14 * This function should write the value in 'pbuffer' to the target 'pAddr',
15 * according to the number of requested elements 'nRequest', the requested type
16 * 'dbrType', and the address modifier private data 'pvt'. It may also take
17 * into account the actual number of elements 'available' in the target, and
18 * the 'offset', both of which usually result from a previous call to
19 * 'get_array_info'.
20 * The targeted record must be locked. Furthermore, the lock must not be given
21 * up between the call to 'get_array_info' and call to this function, otherwise
22 * 'offset' and 'available' may be out of sync.
23 *
24 * @param paddr Target (field) address
25 * @param dbrType Requested (element) type
26 * @param pbuffer Data requested to be written
27 * @param nRequest Number of elements in pbuffer
28 * @param pvt Private modifier data
29 * @param offset Current offset in the target field
30 * @param available Current number of elements in the target field
31 * @return Status
32 */
33typedef long dbHandleAddrModifier(DBADDR *paddr, short dbrType,
34 const void *pbuffer, long nRequest, void *pvt, long offset,
35 long available);
36
37/** @brief Structure of an address modifier.
38 *
39 * This will normally be allocated by user code. An address modifier
40 * implementation should supply a function to create the private data 'pvt'
41 * and initialize 'handle' with a function to handle the modifier.
42 */
43typedef struct {
44 void *pvt; /** @brief Private modifier data */
45 dbHandleAddrModifier *handle; /** @brief Function to handle the modifier */
46} dbAddrModifier;
47
48#endif /* DBADDRMODIFIER_H */
diff --git a/modules/database/src/ioc/db/dbChannel.c b/modules/database/src/ioc/db/dbChannel.c
index c71d842..a9eb4e2 100644
--- a/modules/database/src/ioc/db/dbChannel.c
+++ b/modules/database/src/ioc/db/dbChannel.c
@@ -30,6 +30,7 @@
30#include "yajl_parse.h"30#include "yajl_parse.h"
3131
32#define epicsExportSharedSymbols32#define epicsExportSharedSymbols
33#include "arrayRangeModifier.h"
33#include "dbAccessDefs.h"34#include "dbAccessDefs.h"
34#include "dbBase.h"35#include "dbBase.h"
35#include "dbChannel.h"36#include "dbChannel.h"
@@ -349,75 +350,27 @@ if (Func) { \
349 if (result != parse_continue) goto failure; \350 if (result != parse_continue) goto failure; \
350}351}
351352
352static long parseArrayRange(dbChannel* chan, const char *pname, const char **ppnext) {353static long createArrayRangeFilter(dbChannel* chan) {
353 epicsInt32 start = 0;354 long start, incr, end;
354 epicsInt32 end = -1;
355 epicsInt32 incr = 1;
356 epicsInt32 l;
357 char *pnext;
358 ptrdiff_t exist;
359 chFilter *filter;355 chFilter *filter;
360 const chFilterPlugin *plug;356 const chFilterPlugin *plug;
361 parse_result result;357 parse_result result;
362 long status = 0;
363
364 /* If no number is present, strtol() returns 0 and sets pnext=pname,
365 else pnext points to the first char after the number */
366 pname++;
367 l = strtol(pname, &pnext, 0);
368 exist = pnext - pname;
369 if (exist) start = l;
370 pname = pnext;
371 if (*pname == ']' && exist) {
372 end = start;
373 goto insertplug;
374 }
375 if (*pname != ':') {
376 status = S_dbLib_fieldNotFound;
377 goto finish;
378 }
379 pname++;
380 l = strtol(pname, &pnext, 0);
381 exist = pnext - pname;
382 pname = pnext;
383 if (*pname == ']') {
384 if (exist) end = l;
385 goto insertplug;
386 }
387 if (exist) incr = l;
388 if (*pname != ':') {
389 status = S_dbLib_fieldNotFound;
390 goto finish;
391 }
392 pname++;
393 l = strtol(pname, &pnext, 0);
394 exist = pnext - pname;
395 if (exist) end = l;
396 pname = pnext;
397 if (*pname != ']') {
398 status = S_dbLib_fieldNotFound;
399 goto finish;
400 }
401
402 insertplug:
403 pname++;
404 *ppnext = pname;
405358
406 plug = dbFindFilter("arr", 3);359 plug = dbFindFilter("arr", 3);
407 if (!plug) {360 if (!plug) {
408 status = S_dbLib_fieldNotFound;361 /* "arr" plugin not found, this is not an error! */
409 goto finish;362 return 0;
410 }363 }
411364
412 filter = freeListCalloc(chFilterFreeList);365 filter = freeListCalloc(chFilterFreeList);
413 if (!filter) {366 if (!filter) {
414 status = S_db_noMemory;367 return S_db_noMemory;
415 goto finish;
416 }368 }
417 filter->chan = chan;369 filter->chan = chan;
418 filter->plug = plug;370 filter->plug = plug;
419 filter->puser = NULL;371 filter->puser = NULL;
420372
373 getArrayRange(&chan->addrModifier, &start, &incr, &end);
421 TRY(filter->plug->fif->parse_start, (filter));374 TRY(filter->plug->fif->parse_start, (filter));
422 TRY(filter->plug->fif->parse_start_map, (filter));375 TRY(filter->plug->fif->parse_start_map, (filter));
423 if (start != 0) {376 if (start != 0) {
@@ -438,12 +391,9 @@ static long parseArrayRange(dbChannel* chan, const char *pname, const char **ppn
438 ellAdd(&chan->filters, &filter->list_node);391 ellAdd(&chan->filters, &filter->list_node);
439 return 0;392 return 0;
440393
441 failure:394failure:
442 freeListFree(chFilterFreeList, filter);395 freeListFree(chFilterFreeList, filter);
443 status = S_dbLib_fieldNotFound;396 return S_dbLib_fieldNotFound;
444
445 finish:
446 return status;
447}397}
448398
449dbChannel * dbChannelCreate(const char *name)399dbChannel * dbChannelCreate(const char *name)
@@ -481,6 +431,8 @@ dbChannel * dbChannelCreate(const char *name)
481 goto finish;431 goto finish;
482432
483 /* Handle field modifiers */433 /* Handle field modifiers */
434 chan->addrModifier.pvt = NULL;
435 chan->addrModifier.handle = NULL;
484 if (*pname) {436 if (*pname) {
485 short dbfType = paddr->field_type;437 short dbfType = paddr->field_type;
486438
@@ -505,12 +457,15 @@ dbChannel * dbChannelCreate(const char *name)
505 pname++;457 pname++;
506 }458 }
507459
508 if (*pname == '[') {460 /* Try to recognize an array range expression */
509 status = parseArrayRange(chan, pname, &pname);461 if (parseArrayRange(&chan->addrModifier, &pname)==0) {
462 status = createArrayRangeFilter(chan);
510 if (status) goto finish;463 if (status) goto finish;
511 }464 }
512465
513 /* JSON may follow */466 /* JSON may follow */
467 /* Note that chf_parse issues error messages if it fails,
468 which is why we have to check for the opening brace here. */
514 if (*pname == '{') {469 if (*pname == '{') {
515 status = chf_parse(chan, &pname);470 status = chf_parse(chan, &pname);
516 if (status) goto finish;471 if (status) goto finish;
@@ -655,13 +610,15 @@ long dbChannelGetField(dbChannel *chan, short dbrType, void *pbuffer,
655long dbChannelPut(dbChannel *chan, short type, const void *pbuffer,610long dbChannelPut(dbChannel *chan, short type, const void *pbuffer,
656 long nRequest)611 long nRequest)
657{612{
658 return dbPut(&chan->addr, type, pbuffer, nRequest);613 return dbPutModifier(&chan->addr, type, pbuffer, nRequest,
614 &chan->addrModifier);
659}615}
660616
661long dbChannelPutField(dbChannel *chan, short type, const void *pbuffer,617long dbChannelPutField(dbChannel *chan, short type, const void *pbuffer,
662 long nRequest)618 long nRequest)
663{619{
664 return dbPutField(&chan->addr, type, pbuffer, nRequest);620 return dbPutFieldModifier(&chan->addr, type, pbuffer, nRequest,
621 &chan->addrModifier);
665}622}
666623
667void dbChannelShow(dbChannel *chan, int level, const unsigned short indent)624void dbChannelShow(dbChannel *chan, int level, const unsigned short indent)
@@ -715,6 +672,7 @@ void dbChannelDelete(dbChannel *chan)
715 freeListFree(chFilterFreeList, filter);672 freeListFree(chFilterFreeList, filter);
716 }673 }
717 free((char *) chan->name);674 free((char *) chan->name);
675 deleteArrayRangeModifier(&chan->addrModifier);
718 freeListFree(dbChannelFreeList, chan);676 freeListFree(dbChannelFreeList, chan);
719}677}
720678
diff --git a/modules/database/src/ioc/db/dbChannel.h b/modules/database/src/ioc/db/dbChannel.h
index ec86e9e..60fbb9d 100644
--- a/modules/database/src/ioc/db/dbChannel.h
+++ b/modules/database/src/ioc/db/dbChannel.h
@@ -23,6 +23,7 @@
23#include "epicsTypes.h"23#include "epicsTypes.h"
24#include "errMdef.h"24#include "errMdef.h"
25#include "db_field_log.h"25#include "db_field_log.h"
26#include "dbAddrModifier.h"
26#include "dbEvent.h"27#include "dbEvent.h"
27#include "dbCoreAPI.h"28#include "dbCoreAPI.h"
2829
@@ -54,6 +55,8 @@ typedef struct chFilter chFilter;
54typedef struct dbChannel {55typedef struct dbChannel {
55 const char *name;56 const char *name;
56 dbAddr addr; /* address structure for record/field */57 dbAddr addr; /* address structure for record/field */
58 dbAddrModifier addrModifier;
59 /* optional: which indices are targeted */
57 long final_no_elements; /* final number of elements (arrays) */60 long final_no_elements; /* final number of elements (arrays) */
58 short final_field_size; /* final size of element */61 short final_field_size; /* final size of element */
59 short final_type; /* final type of database field */62 short final_type; /* final type of database field */
diff --git a/modules/database/src/ioc/db/dbDbLink.c b/modules/database/src/ioc/db/dbDbLink.c
index b281314..fe7c372 100644
--- a/modules/database/src/ioc/db/dbDbLink.c
+++ b/modules/database/src/ioc/db/dbDbLink.c
@@ -364,9 +364,8 @@ static long dbDbPutValue(struct link *plink, short dbrType,
364 struct pv_link *ppv_link = &plink->value.pv_link;364 struct pv_link *ppv_link = &plink->value.pv_link;
365 dbChannel *chan = linkChannel(plink);365 dbChannel *chan = linkChannel(plink);
366 struct dbCommon *psrce = plink->precord;366 struct dbCommon *psrce = plink->precord;
367 DBADDR *paddr = &chan->addr;
368 dbCommon *pdest = dbChannelRecord(chan);367 dbCommon *pdest = dbChannelRecord(chan);
369 long status = dbPut(paddr, dbrType, pbuffer, nRequest);368 long status = dbChannelPut(chan, dbrType, pbuffer, nRequest);
370369
371 recGblInheritSevr(ppv_link->pvlMask & pvlOptMsMode, pdest, psrce->nsta,370 recGblInheritSevr(ppv_link->pvlMask & pvlOptMsMode, pdest, psrce->nsta,
372 psrce->nsev);371 psrce->nsev);
diff --git a/modules/database/src/ioc/db/dbUnitTest.c b/modules/database/src/ioc/db/dbUnitTest.c
index 0b40d92..1dd4909 100644
--- a/modules/database/src/ioc/db/dbUnitTest.c
+++ b/modules/database/src/ioc/db/dbUnitTest.c
@@ -267,7 +267,7 @@ done:
267void testdbPutArrFieldOk(const char* pv, short dbrType, unsigned long count, const void *pbuf)267void testdbPutArrFieldOk(const char* pv, short dbrType, unsigned long count, const void *pbuf)
268{268{
269 dbChannel *chan = dbChannelCreate(pv);269 dbChannel *chan = dbChannelCreate(pv);
270 long status;270 long status = 0;
271271
272 if(!chan || (status=dbChannelOpen(chan))) {272 if(!chan || (status=dbChannelOpen(chan))) {
273 testFail("Channel error (%p, %ld) : %s", chan, status, pv);273 testFail("Channel error (%p, %ld) : %s", chan, status, pv);
@@ -290,7 +290,7 @@ void testdbGetArrFieldEqual(const char* pv, short dbfType, long nRequest, unsign
290 const long vSize = dbValueSize(dbfType);290 const long vSize = dbValueSize(dbfType);
291 const long nStore = vSize * nRequest;291 const long nStore = vSize * nRequest;
292 long status = S_dbLib_recNotFound;292 long status = S_dbLib_recNotFound;
293 char *gbuf, *gstore;293 char *gbuf, *gstore = NULL;
294 const char *pbuf = pbufraw;294 const char *pbuf = pbufraw;
295295
296 if(!chan || (status=dbChannelOpen(chan))) {296 if(!chan || (status=dbChannelOpen(chan))) {
diff --git a/modules/database/src/std/filters/arr.c b/modules/database/src/std/filters/arr.c
index ffe3fce..ed42c67 100644
--- a/modules/database/src/std/filters/arr.c
+++ b/modules/database/src/std/filters/arr.c
@@ -13,6 +13,7 @@
1313
14#include <stdio.h>14#include <stdio.h>
1515
16#include "arrayRangeModifier.h"
16#include "chfPlugin.h"17#include "chfPlugin.h"
17#include "dbAccessDefs.h"18#include "dbAccessDefs.h"
18#include "dbExtractArray.h"19#include "dbExtractArray.h"
@@ -74,23 +75,6 @@ static void freeArray(db_field_log *pfl)
74 }75 }
75}76}
7677
77static long wrapArrayIndices(long *start, const long increment, long *end,
78 const long no_elements)
79{
80 if (*start < 0) *start = no_elements + *start;
81 if (*start < 0) *start = 0;
82 if (*start > no_elements) *start = no_elements;
83
84 if (*end < 0) *end = no_elements + *end;
85 if (*end < 0) *end = 0;
86 if (*end >= no_elements) *end = no_elements - 1;
87
88 if (*end - *start >= 0)
89 return 1 + (*end - *start) / increment;
90 else
91 return 0;
92}
93
94static db_field_log* filter(void* pvt, dbChannel *chan, db_field_log *pfl)78static db_field_log* filter(void* pvt, dbChannel *chan, db_field_log *pfl)
95{79{
96 myStruct *my = (myStruct*) pvt;80 myStruct *my = (myStruct*) pvt;
diff --git a/modules/database/test/std/rec/Makefile b/modules/database/test/std/rec/Makefile
index e8c5464..71884b4 100644
--- a/modules/database/test/std/rec/Makefile
+++ b/modules/database/test/std/rec/Makefile
@@ -15,6 +15,7 @@ USR_CPPFLAGS += -DUSE_TYPED_DSET
1515
16TESTLIBRARY = dbRecStdTest16TESTLIBRARY = dbRecStdTest
1717
18dbRecStdTest_SRCS += arroutRecord.c
18dbRecStdTest_SRCS += asTestLib.c19dbRecStdTest_SRCS += asTestLib.c
19dbRecStdTest_LIBS += dbRecStd dbCore ca Com20dbRecStdTest_LIBS += dbRecStd dbCore ca Com
2021
@@ -23,6 +24,7 @@ PROD_LIBS = dbRecStdTest dbRecStd dbCore ca Com
23TARGETS += $(COMMON_DIR)/recTestIoc.dbd24TARGETS += $(COMMON_DIR)/recTestIoc.dbd
24DBDDEPENDS_FILES += recTestIoc.dbd$(DEP)25DBDDEPENDS_FILES += recTestIoc.dbd$(DEP)
25recTestIoc_DBD = base.dbd26recTestIoc_DBD = base.dbd
27recTestIoc_DBD += arroutRecord.dbd
26TESTFILES += $(COMMON_DIR)/recTestIoc.dbd28TESTFILES += $(COMMON_DIR)/recTestIoc.dbd
2729
28testHarness_SRCS += recTestIoc_registerRecordDeviceDriver.cpp30testHarness_SRCS += recTestIoc_registerRecordDeviceDriver.cpp
@@ -166,6 +168,13 @@ testHarness_SRCS += linkFilterTest.c
166TESTFILES += ../linkFilterTest.db168TESTFILES += ../linkFilterTest.db
167TESTS += linkFilterTest169TESTS += linkFilterTest
168170
171TESTPROD_HOST += addrModifierTest
172addrModifierTest_SRCS += addrModifierTest.c
173addrModifierTest_SRCS += recTestIoc_registerRecordDeviceDriver.cpp
174testHarness_SRCS += addrModifierTest.c
175TESTFILES += ../addrModifierTest.db
176TESTS += addrModifierTest
177
169# dbHeader* is only a compile test178# dbHeader* is only a compile test
170# no need to actually run179# no need to actually run
171TESTPROD += dbHeaderTest180TESTPROD += dbHeaderTest
@@ -199,5 +208,7 @@ endif
199208
200include $(TOP)/configure/RULES209include $(TOP)/configure/RULES
201210
211arroutRecord$(DEP): $(COMMON_DIR)/arroutRecord.h
212
202rtemsTestData.c : $(TESTFILES) $(TOOLS)/epicsMakeMemFs.pl213rtemsTestData.c : $(TESTFILES) $(TOOLS)/epicsMakeMemFs.pl
203 $(PERL) $(TOOLS)/epicsMakeMemFs.pl $@ epicsRtemsFSImage $(TESTFILES)214 $(PERL) $(TOOLS)/epicsMakeMemFs.pl $@ epicsRtemsFSImage $(TESTFILES)
diff --git a/modules/database/test/std/rec/addrModifierTest.c b/modules/database/test/std/rec/addrModifierTest.c
204new file mode 100644215new file mode 100644
index 0000000..d555b91
--- /dev/null
+++ b/modules/database/test/std/rec/addrModifierTest.c
@@ -0,0 +1,247 @@
1/*************************************************************************\
2* Copyright (c) 2020 Dirk Zimoch
3* EPICS BASE is distributed subject to a Software License Agreement found
4* in file LICENSE that is included with this distribution.
5\*************************************************************************/
6
7#include <string.h>
8
9#include "dbAccess.h"
10#include "devSup.h"
11#include "alarm.h"
12#include "dbUnitTest.h"
13#include "errlog.h"
14#include "epicsThread.h"
15
16#include "longinRecord.h"
17
18#include "testMain.h"
19
20void recTestIoc_registerRecordDeviceDriver(struct dbBase *);
21
22static void startTestIoc(const char *dbfile)
23{
24 testdbPrepare();
25 testdbReadDatabase("recTestIoc.dbd", NULL, NULL);
26 recTestIoc_registerRecordDeviceDriver(pdbbase);
27 testdbReadDatabase(dbfile, NULL, NULL);
28
29 eltc(0);
30 testIocInitOk();
31 eltc(1);
32}
33
34struct pv {
35 const char *name;
36 long count;
37 double values[4];
38};
39
40static void expectProcSuccess(struct pv *pv)
41{
42 char fieldname[20];
43 testdbPutFieldOk("reset.PROC", DBF_LONG, 1);
44 testDiag("expecting success from %s", pv->name);
45 sprintf(fieldname, "%s.PROC", pv->name);
46 testdbPutFieldOk(fieldname, DBF_LONG, 1);
47 sprintf(fieldname, "%s.SEVR", pv->name);
48 testdbGetFieldEqual(fieldname, DBF_LONG, NO_ALARM);
49 sprintf(fieldname, "%s.STAT", pv->name);
50 testdbGetFieldEqual(fieldname, DBF_LONG, NO_ALARM);
51}
52
53#if 0
54static void expectProcFailure(struct pv *pv)
55{
56 char fieldname[20];
57 testdbPutFieldOk("reset.PROC", DBF_LONG, 1);
58 testDiag("expecting failure S_db_badField %#x from %s", S_db_badField, pv->name);
59 sprintf(fieldname, "%s.PROC", pv->name);
60 testdbPutFieldFail(S_db_badField, fieldname, DBF_LONG, 1);
61 sprintf(fieldname, "%s.SEVR", pv->name);
62 testdbGetFieldEqual(fieldname, DBF_LONG, INVALID_ALARM);
63 sprintf(fieldname, "%s.STAT", pv->name);
64 testdbGetFieldEqual(fieldname, DBF_LONG, LINK_ALARM);
65}
66#endif
67
68static double initial[] = {0,1,2,3,4,5,6,7,8,9};
69static double buf[10];
70
71static void changeRange(struct pv *pv, long start, long incr, long end)
72{
73 char linkstring[60];
74 char pvstring[10];
75
76 if (incr)
77 sprintf(linkstring, "tgt.[%ld:%ld:%ld]", start, incr, end);
78 else if (end)
79 sprintf(linkstring, "tgt.[%ld:%ld]", start, end);
80 else
81 sprintf(linkstring, "tgt.[%ld]", start);
82 testDiag("modifying %s.OUT link: %s", pv->name, linkstring);
83 sprintf(pvstring, "%s.OUT", pv->name);
84 testdbPutFieldOk(pvstring, DBF_STRING, linkstring);
85}
86
87static void expectRange(double *values, long start, long incr, long end)
88{
89 int i,j;
90 if (!incr)
91 incr = 1;
92 for (i=0; i<10; i++)
93 buf[i] = initial[i];
94 for (i=0, j=start; j<=end; i++, j+=incr)
95 buf[j] = values[i];
96 testdbGetFieldEqual("tgt.NORD", DBF_LONG, 8);
97 testdbGetFieldEqual("tgt.SEVR", DBF_LONG, NO_ALARM);
98 testdbGetFieldEqual("tgt.STAT", DBF_LONG, NO_ALARM);
99 testdbGetArrFieldEqual("tgt.VAL", DBF_DOUBLE, 10, 8, buf);
100}
101
102#if 0
103static void expectEmptyArray(void)
104{
105 /* empty arrays are now allowed at the moment */
106 testDiag("expecting empty array");
107 testdbGetFieldEqual("tgt.NORD", DBF_LONG, 0);
108}
109#endif
110
111struct pv ao_pv = {"ao",1,{20,0,0,0}};
112struct pv src_pv = {"src",4,{30,40,50,60}};
113struct pv *ao = &ao_pv;
114struct pv *src = &src_pv;
115
116static double pini_values[] = {20,30,40,50};
117
118#define expectEmptyRange() expectRange(0,0,0,-1)
119
120MAIN(addrModifierTest)
121{
122 testPlan(205);
123 startTestIoc("addrModifierTest.db");
124
125 testdbGetFieldEqual("src.NORD", DBF_LONG, 4);
126 testDiag("PINI");
127 expectRange(pini_values,2,1,5);
128
129 testDiag("after processing");
130 testdbPutFieldOk("ao.PROC", DBF_LONG, 1);
131 testdbPutFieldOk("src.PROC", DBF_LONG, 1);
132 expectRange(pini_values,2,1,5);
133
134 testDiag("after processing target record");
135 testdbPutFieldOk("tgt.PROC", DBF_LONG, 1);
136 expectRange(pini_values,2,1,5);
137
138 testDiag("modify range");
139
140 changeRange(ao,3,0,0);
141 expectProcSuccess(ao);
142 expectRange(ao->values,3,1,3);
143
144 changeRange(src,4,0,7);
145 expectProcSuccess(src);
146 expectRange(src->values,4,1,7);
147
148 testDiag("put more than available");
149
150 changeRange(ao,3,0,6);
151 expectProcSuccess(ao);
152 expectRange(ao->values,3,1,3); /* clipped range */
153
154 changeRange(src,3,0,9);
155 expectProcSuccess(src);
156 expectRange(src->values,3,1,6); /* clipped range */
157
158 testDiag("backward range");
159
160 changeRange(ao,5,0,3);
161 expectProcSuccess(ao);
162 expectEmptyRange();
163
164 changeRange(src,5,0,3);
165 expectProcSuccess(src);
166 expectEmptyRange();
167
168 testDiag("step 2");
169
170 changeRange(ao,1,2,6);
171 expectProcSuccess(ao);
172 expectRange(ao->values,1,1,1); /* clipped range */
173
174 changeRange(src,1,2,6);
175 expectProcSuccess(src);
176 expectRange(src->values,1,2,5);
177
178 testDiag("range start beyond tgt.NORD");
179
180 changeRange(ao,8,0,0);
181 expectProcSuccess(ao);
182 expectEmptyRange();
183
184 changeRange(src,8,0,9);
185 expectProcSuccess(src);
186 expectEmptyRange();
187
188 testDiag("range end beyond tgt.NORD");
189
190 changeRange(ao,3,0,9);
191 expectProcSuccess(ao);
192 expectRange(ao->values,3,1,3); /* clipped range */
193
194 changeRange(src,3,0,9);
195 expectProcSuccess(src);
196 expectRange(src->values,3,1,6); /* clipped range */
197
198 testDiag("range start beyond tgt.NELM");
199
200 changeRange(ao,11,0,12);
201 expectProcSuccess(ao);
202 expectEmptyRange();
203
204 changeRange(src,11,0,12);
205 expectProcSuccess(src);
206 expectEmptyRange();
207
208 testDiag("range end beyond tgt.NELM");
209
210 changeRange(src,4,0,12);
211 expectProcSuccess(src);
212 expectRange(src->values,4,1,7); /* clipped range */
213
214 testDiag("single value beyond tgt.NORD");
215
216 changeRange(ao,8,0,0);
217 expectProcSuccess(ao);
218 expectEmptyRange();
219
220 changeRange(src,8,0,0);
221 expectProcSuccess(src);
222 expectEmptyRange();
223
224 testDiag("single value");
225
226 changeRange(ao,5,0,0);
227 expectProcSuccess(ao);
228 expectRange(ao->values,5,1,5);
229
230 changeRange(src,5,0,0);
231 expectProcSuccess(src);
232 expectRange(src->values,5,1,5);
233
234 testDiag("single values beyond tgt.NELM");
235
236 changeRange(ao,12,0,0);
237 expectProcSuccess(ao);
238 expectEmptyRange();
239
240 changeRange(src,12,0,0);
241 expectProcSuccess(src);
242 expectEmptyRange();
243
244 testIocShutdownOk();
245 testdbCleanup();
246 return testDone();
247}
diff --git a/modules/database/test/std/rec/addrModifierTest.db b/modules/database/test/std/rec/addrModifierTest.db
0new file mode 100644248new file mode 100644
index 0000000..ed28a80
--- /dev/null
+++ b/modules/database/test/std/rec/addrModifierTest.db
@@ -0,0 +1,24 @@
1record(arrout, "tgt") {
2 field(NELM, "10")
3 field(FTVL, "SHORT")
4 field(DOL, [0, 1, 2, 3, 4, 5, 6, 7])
5 field(PINI, "YES")
6}
7record(ao, "ao") {
8 field(OUT, "tgt.[2]")
9 field(DOL, 20)
10 field(PINI, "YES")
11}
12record(arrout, "src") {
13 field(NELM, "5")
14 field(FTVL, "DOUBLE")
15 field(DOL, [30.0, 40.0, 50.0, 60.0])
16 field(OUT, "tgt.[3:5]")
17 field(PINI, "YES")
18}
19record(arrout, "reset") {
20 field(NELM, "10")
21 field(FTVL, "SHORT")
22 field(DOL, [0, 1, 2, 3, 4, 5, 6, 7])
23 field(OUT, "tgt PP")
24}
diff --git a/modules/database/test/std/rec/arroutRecord.c b/modules/database/test/std/rec/arroutRecord.c
0new file mode 10064425new file mode 100644
index 0000000..61c7680
--- /dev/null
+++ b/modules/database/test/std/rec/arroutRecord.c
@@ -0,0 +1,169 @@
1/*************************************************************************\
2* Copyright (c) 2010 Brookhaven National Laboratory.
3* Copyright (c) 2010 Helmholtz-Zentrum Berlin
4* fuer Materialien und Energie GmbH.
5* Copyright (c) 2008 UChicago Argonne LLC, as Operator of Argonne
6* National Laboratory.
7* Copyright (c) 2002 The Regents of the University of California, as
8* Operator of Los Alamos National Laboratory.
9* EPICS BASE is distributed subject to a Software License Agreement found
10* in file LICENSE that is included with this distribution.
11\*************************************************************************/
12
13/* arroutRecord.c - minimal array output record for test purposes: no processing */
14
15/* adapted from: arrRecord.c
16 *
17 * Author: Ralph Lange <Ralph.Lange@bessy.de>
18 *
19 * vaguely implemented like parts of recWaveform.c by Bob Dalesio
20 *
21 */
22
23#include <stdio.h>
24
25#include "alarm.h"
26#include "epicsPrint.h"
27#include "dbAccess.h"
28#include "dbEvent.h"
29#include "dbFldTypes.h"
30#include "recSup.h"
31#include "recGbl.h"
32#include "cantProceed.h"
33#define GEN_SIZE_OFFSET
34#include "arroutRecord.h"
35#undef GEN_SIZE_OFFSET
36#include "epicsExport.h"
37
38/* Create RSET - Record Support Entry Table*/
39#define report NULL
40#define initialize NULL
41static long init_record(struct dbCommon *, int);
42static long process(struct dbCommon *);
43#define special NULL
44#define get_value NULL
45static long cvt_dbaddr(DBADDR *);
46static long get_array_info(DBADDR *, long *, long *);
47static long put_array_info(DBADDR *, long);
48#define get_units NULL
49#define get_precision NULL
50#define get_enum_str NULL
51#define get_enum_strs NULL
52#define put_enum_str NULL
53#define get_graphic_double NULL
54#define get_control_double NULL
55#define get_alarm_double NULL
56
57rset arroutRSET = {
58 RSETNUMBER,
59 report,
60 initialize,
61 init_record,
62 process,
63 special,
64 get_value,
65 cvt_dbaddr,
66 get_array_info,
67 put_array_info,
68 get_units,
69 get_precision,
70 get_enum_str,
71 get_enum_strs,
72 put_enum_str,
73 get_graphic_double,
74 get_control_double,
75 get_alarm_double
76};
77epicsExportAddress(rset, arroutRSET);
78
79static long init_record(struct dbCommon *pcommon, int pass)
80{
81 struct arroutRecord *prec = (struct arroutRecord *)pcommon;
82
83 if (pass == 0) {
84 if (prec->nelm <= 0)
85 prec->nelm = 1;
86 if (prec->ftvl > DBF_ENUM)
87 prec->ftvl = DBF_UCHAR;
88 prec->val = callocMustSucceed(prec->nelm, dbValueSize(prec->ftvl),
89 "arr calloc failed");
90 return 0;
91 } else {
92 /* copied from devWfSoft.c */
93 long nelm = prec->nelm;
94 long status = dbLoadLinkArray(&prec->dol, prec->ftvl, prec->val, &nelm);
95
96 if (!status && nelm > 0) {
97 prec->nord = nelm;
98 prec->udf = FALSE;
99 }
100 else
101 prec->nord = 0;
102 return status;
103 }
104}
105
106static long process(struct dbCommon *pcommon)
107{
108 struct arroutRecord *prec = (struct arroutRecord *)pcommon;
109 long status = 0;
110
111 prec->pact = TRUE;
112 /* read DOL */
113 if (!dbLinkIsConstant(&prec->dol)) {
114 long nReq = prec->nelm;
115
116 status = dbGetLink(&prec->dol, prec->ftvl, prec->val, 0, &nReq);
117 if (status) {
118 recGblSetSevr(prec, LINK_ALARM, INVALID_ALARM);
119 } else {
120 prec->nord = nReq;
121 }
122 }
123 /* soft "device support": write OUT */
124 status = dbPutLink(&prec->out, prec->ftvl, prec->val, prec->nord);
125 if (status) {
126 recGblSetSevr(prec, LINK_ALARM, INVALID_ALARM);
127 }
128 recGblGetTimeStamp(prec);
129 recGblResetAlarms(prec);
130 recGblFwdLink(prec);
131 prec->pact = FALSE;
132 return status;
133}
134
135static long cvt_dbaddr(DBADDR *paddr)
136{
137 arroutRecord *prec = (arroutRecord *) paddr->precord;
138 int fieldIndex = dbGetFieldIndex(paddr);
139
140 if (fieldIndex == arroutRecordVAL) {
141 paddr->pfield = prec->val;
142 paddr->no_elements = prec->nelm;
143 paddr->field_type = prec->ftvl;
144 paddr->field_size = dbValueSize(prec->ftvl);
145 paddr->dbr_field_type = prec->ftvl;
146 }
147 return 0;
148}
149
150static long get_array_info(DBADDR *paddr, long *no_elements, long *offset)
151{
152 arroutRecord *prec = (arroutRecord *) paddr->precord;
153
154 *no_elements = prec->nord;
155 *offset = prec->off;
156
157 return 0;
158}
159
160static long put_array_info(DBADDR *paddr, long nNew)
161{
162 arroutRecord *prec = (arroutRecord *) paddr->precord;
163
164 prec->nord = nNew;
165 if (prec->nord > prec->nelm)
166 prec->nord = prec->nelm;
167
168 return 0;
169}
diff --git a/modules/database/test/std/rec/arroutRecord.dbd b/modules/database/test/std/rec/arroutRecord.dbd
0new file mode 100644170new file mode 100644
index 0000000..2c99e64
--- /dev/null
+++ b/modules/database/test/std/rec/arroutRecord.dbd
@@ -0,0 +1,35 @@
1include "menuGlobal.dbd"
2include "menuConvert.dbd"
3include "menuScan.dbd"
4recordtype(arrout) {
5 include "dbCommon.dbd"
6 field(VAL, DBF_NOACCESS) {
7 prompt("Value")
8 special(SPC_DBADDR)
9 pp(TRUE)
10 extra("void *val")
11 }
12 field(OUT, DBF_OUTLINK) {
13 prompt("Output Link")
14 }
15 field(NELM, DBF_ULONG) {
16 prompt("Number of Elements")
17 special(SPC_NOMOD)
18 initial("1")
19 }
20 field(FTVL, DBF_MENU) {
21 prompt("Field Type of Value")
22 special(SPC_NOMOD)
23 menu(menuFtype)
24 }
25 field(NORD, DBF_ULONG) {
26 prompt("Number elements read")
27 special(SPC_NOMOD)
28 }
29 field(OFF, DBF_ULONG) {
30 prompt("Offset into array")
31 }
32 field(DOL, DBF_INLINK) {
33 prompt("Desired Output Link")
34 }
35}

Subscribers

People subscribed via source and target branches