#*************************************************************************
# Copyright (c) 2002 The University of Chicago, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# EPICS BASE is distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution.
#*************************************************************************

=title Event Record (event)

The normal use for this record type is to post a soft-event and/or process a
forward link.
C<Soft Channel> device support is provided to allow the soft-event name to be
read from an input link.
Hardware device support for this record can provide an interrupt handler routine
to trigger processing of the record when an I/O Intr event occurs.

=head2 Parameter Fields

The record-specific fields are described below, grouped by functionality.

=recordtype event

=cut

recordtype(event) {
	include "dbCommon.dbd"

    %
    %/* Declare Device Support Entry Table */
    %struct eventRecord;
    %typedef struct eventdset {
    %    dset common; /*init_record returns: (-1,0)=>(failure,success)*/
    %    long (*read_event)(struct eventRecord *prec); /*(0)=> success */
    %} eventdset;
    %#define HAS_eventdset
    %

=head3 Scan Parameters

The event record has the standard fields for specifying under what circumstances
it should be processed.
These fields are described in L<Scan Fields|dbCommonRecord/Scan Fields>.

=fields SCAN, PHAS, EVNT, PRIO, PINI

=head3 Event Name Parameters

The VAL field is a string (prior to the Base-3.15.1 release it was a short
integer) providing the name of an IOC soft-event.
This named soft-event gets posted whenever the record is processed.

When the soft-event name is known at design time, the VAL field should be set to
the name in a loaded database file.
Soft-event names do not have to be registered before use, a handle for the name
is automatically created and stored when a name is first seen and the same
handle returned later if the same name is re-used.

The EPVT field holds the handle for the soft-event named in the VAL field.
Looking up the handle for a soft-event name is fast and uses a hash table.

For records that use the default C<Soft Channel> device support, the soft-event
name can be fetched through the INP field link, written to the VAL field and the
handle looked up during record processing.

=fields VAL, EPVT

=cut

	field(VAL,DBF_STRING) {
		prompt("Event Name To Post")
		promptgroup("40 - Input")
		special(SPC_MOD)
		asl(ASL0)
		size(40)
	}
	%#include "dbScan.h"
	field(EPVT, DBF_NOACCESS) {
		prompt("Event private")
		special(SPC_NOMOD)
		interest(4)
		extra("EVENTPVT epvt")
	}

=head3 Input Specification

The device support routines use the address in this record to obtain input.
For records that provide an interrupt handler, the INP field should specify the
address of the I/O card, and the DTYP field should specify a valid device
support module.

The address format differs according to the card type  used. See
L<Address
Specification|https://docs.epics-controls.org/en/latest/process-database/EPICS_Process_Database_Concepts.html#address-specification>
for information on the format of hardware addresses and specifying links.

For soft records, the INP field can be a constant, a database link, or a channel
access link, and the DTYP field should be empty or set to C<Soft Channel>.

=fields INP, DTYP

=cut

	field(INP,DBF_INLINK) {
		prompt("Input Specification")
		promptgroup("40 - Input")
		interest(1)
	}

=head3 Operator Display Parameters

See L<Fields Common to All Record Types|dbCommonRecord/Operator Display
Parameters>
for more on the record name (NAME) and description (DESC) fields.

=fields NAME, DESC

=head3 Alarm Parameters

The Event record has the alarm parameters common to all record types.
L<Alarm Fields|dbCommonRecord/Alarm Fields> lists other fields related to alarms
that are common to all record types.

=fields STAT, SEVR, AMSG, NSTA, NSEV, NAMSG, ACKS, ACKT, UDF

=head3 Simulation Mode Parameters

The following fields are used to operate the event record in the simulation
mode.
See L<Fields Common to Input Record Types|dbCommonInput/Input Simulation Fields>
for more information on these fields.

=fields SIOL, SVAL, SIML, SIMM, SIMS, SSCN, SDLY

=cut

	field(SIOL,DBF_INLINK) {
		prompt("Sim Input Specifctn")
		promptgroup("90 - Simulate")
		interest(1)
	}
	field(SVAL,DBF_STRING) {
		prompt("Simulation Value")
		size(40)
	}
	field(SIML,DBF_INLINK) {
		prompt("Sim Mode Location")
		promptgroup("90 - Simulate")
		interest(1)
	}
	field(SIMM,DBF_MENU) {
		prompt("Simulation Mode")
		special(SPC_MOD)
		interest(1)
		menu(menuYesNo)
	}
	field(SIMS,DBF_MENU) {
		prompt("Sim mode Alarm Svrty")
		promptgroup("90 - Simulate")
		interest(2)
		menu(menuAlarmSevr)
	}
	field(OLDSIMM,DBF_MENU) {
		prompt("Prev. Simulation Mode")
		special(SPC_NOMOD)
		interest(4)
		menu(menuSimm)
	}
	field(SSCN,DBF_MENU) {
		prompt("Sim. Mode Scan")
		promptgroup("90 - Simulate")
		interest(1)
		menu(menuScan)
		initial("65535")
	}
	field(SDLY,DBF_DOUBLE) {
		prompt("Sim. Mode Async Delay")
		promptgroup("90 - Simulate")
		interest(2)
		initial("-1.0")
	}
	%#include "callback.h"
	field(SIMPVT,DBF_NOACCESS) {
		prompt("Sim. Mode Private")
		special(SPC_NOMOD)
		interest(4)
		extra("epicsCallback            *simpvt")
	}

=head2 Record Support

=head3 Record Support Routines

=head4 init_record

This routine initializes SIMM with the value of SIML if SIML type is a CONSTANT
link or creates a channel access link if SIML type is PV_LINK. SVAL is likewise
initialized if SIOL is CONSTANT or PV_LINK.

If device support includes C<init_record()>, it is called.

The string in VAL is converted to a soft-event handle in EPVT.

=head4 process

See next section.

=head4 special

When the VAL field is set, the new string is converted to a soft-event handle in
EPVT.

=head3 Record Processing

Routine C<process()> implements the following algorithm:

=over

=item 1.

C<readValue()> is called.
See L<Input Records|dbCommonInput/Input Records> for more information.

=item 2.

If PACT has changed to TRUE, the device support read routine has started
but has not completed reading a new soft-event name.
In this case, the processing routine returns immediately, leaving PACT TRUE.

=item 3.

Set PACT to TRUE.

=item 4.

Post the soft-event whose handle is in EPVT.

=item 5.

Check to see if monitors should be invoked.
Alarm monitors are invoked if the new alarm status or severity are non-zero.

=item 6.

Scan forward link if set, set PACT to FALSE, and return.

=back

=head2 Device Support

=head3 Fields of Interest To Device Support

Each record must have an associated set of device support routines. The device
support routines are primarily interested in the following fields:

=fields PACT, DPVT, UDF, NSEV, NSTA, INP, PRIO, VAL, EPVT

=head3 Device Support Routines

Device support consists of the following routines:

=head4 report

  long report(int level)

This optional routine is called by the IOC command C<dbior> and is passed the
report level that was requested by the user.
It should print a report on the state of the device support to stdout.
The C<level> parameter may be used to output increasingly more detailed
information at higher levels, or to select different types of information with
different levels.
Level zero should print only a 1-line summary.

=head4 init

  long init(int after)

This optional routine is called twice at IOC initialization time.
The first call happens before any of the C<init_record()> calls are made, with
the integer parameter C<after> set to 0.
The second call happens after all of the C<init_record()> calls have been made,
with C<after> set to 1.

=head4 init_record

  long init_record(precord)

This routine is optional. If provided, it is called by the record support
C<init_record()> routine.

=head4 get_ioint_info

  long get_ioint_info(int cmd, struct dbCommon *precord, IOSCANPVT *ppvt)

This routine is called by the dbScan system each time the record is added
or deleted from an I/O event scan list.
C<cmd> has the value (0, 1) if the record is being (added to, deleted from) an
I/O event list.
The C<get_ioint_info> routine is optional, but must be provided by any device
support that implements C<I/O Intr> scanning for the event record type.

=head4 read_event

  long read_event(precord)

This routine returns the following values:

=over

=item *

0: Success.

=item *

Other: Error.

=back

=head3 Device Support For Soft Records

A C<Soft Channel> device support module is available.
The INP link field is used to fetch the soft-event name.

The C<read_event()> routine reads a string through INP and stores it in VAL,
then looks up the named soft-event handle and sets EPVT.

See L<Input Records|dbCommonInput/Input Records> for details on soft input.

=cut

}
