# Program structure and coding features

This topic explains the demonstration program structure and its routines in detail. Use it as a model when developing your own application.

## The main function

The main function of trunklog starts by calling initconsole. initconsole sets the keyboard to raw mode if trunklog is compiled for UNIX.

main then calls init_ct_access. init_ct_access performs the following tasks:

Task | Description
1 | Calls ctaSetErrorHandler to specify the errorhandler function as the error handling routine for trunklog. If an error occurs, errorhandler sends a message to stderr and calls doexit to terminate the program. errorhandler does not report an error when calls to dtmStartTrunkMonitor return CTAERR_FUNCTION_NOT_AVAIL or CTAERR_INVALID_BOARD. The start_monitor function is not interrupted if it attempts to start up the DTM service on a board number for which there is no corresponding board.
2 | Calls ctaInitialize to establish a list of available services for the application, including the DTM and ADI services.
3 | Calls ctaCreateQueue to create an event queue.
4 | Calls ctaCreateContext to create a context to handle the incoming call.
5 | Calls ctaOpenServices to start up the DTM and ADI services.
6 | Calls ctaWaitEvent to wait for CTAEVN_OPEN_SERVICES_DONE with a value field of 0, meaning that all services started successfully. If it receives this event, it returns. If it times out or if value does not equal 0, it displays an error message and calls doexit to terminate the program.

main then calls start_monitor, which performs the following tasks:

Task | Description
1 | Sets the parameters in the DTM_START_PARMS structure. It sets maxevents to 2, so the DTM service will return two events per second. It sets reportmask to DTM_REPORT_ALARMS, so DTM will report any change in the alarm state.
2 | Starts monitoring trunks on boards 0 through 15 by calling dtmStartTrunkMonitor iteratively for each board number. Even if dtmStartTrunkMonitor returns an error (for example, there is something wrong with a given board, or no board exists for the board number), start_monitor continues until it has tried all 16 board numbers.

If monitoring services are successfully started on at least one board, main then calls process_events. process_events performs the following tasks:

Task | Description
1 | Checks repeatedly to see if you pressed the ESC or F3 key. If you press one of these keys, process_events calls doexit to terminate the program.
2 | Calls ctaWaitEvent repeatedly and waits for the following events: DTMEVN_MONITOR_STARTED Monitoring started on a trunk. process_events calls dtmGetTrunkStatus (or (dtmGetBriTrunkStatus) to determine on which board and trunk the monitoring started, and then calls display_data to send the trunk's initial conditions to stdout. DTMEVN_TRUNK_STATUS An alarm state changed on a trunk. process_events calls dtmGetTrunkStatus (or dtmGetBriTrunkStatus) to determine on which board and trunk the state change took place, and then calls display_data to send the change to stdout. CTAEVN_WAIT_TIMEOUT ctaWaitEvent timed out. process_events calls ctaWaitEvent again. DTMEVN_MONITOR_DONE Monitoring ended on a board. process_events determines on which board monitoring ended, and sends a message to stdout. ADIEVN_BOARD_ERROR An unexpected error occurred on a board. process_events sends a message to stderr.