# Setting up the IMGT service

Before an application can use functions from the IMGT service, the application must initialize and open the IMGT service on an open context.

Note: Remember to start the NMS ISDN protocol stack before starting the IMGT service.

## Initializing the IMGT service

To open the IMGT service, include the IMGT service and the ADI Service Manager in the call to ctaInitialize.

The following code excerpt demonstrates initializing the IMGT service together with the ADI service:

void MyServiceInit()
{
DWORD ret;
CTA_INIT_PARMS initparms = { 0 };
CTA_ERROR_HANDLER hdlr;
CTA_SERVICE_NAME InitServices[] = /* For ctaInitialize */
{ { "ADI", "ADIMGR" },
{ "IMGT", "ADIMGR" },
};
/* Initialize size of init parms structure */
initparms.size = sizeof(CTA_INIT_PARMS);
if ( ( ret = ctaInitialize(InitServices,
sizeof(InitServices)/sizeof(InitServices[0]),
&initparms)) != SUCCESS)
{
/* Handle error conditions here.... */
}
}

After invoking ctaInitialize, the application must:

1. Create an event queue attached to the ADI Service Manager by calling ctaCreateQueue. ADIMGR can be explicitly specified in the call. If NULL is passed, all service managers specified in ctaInitialize are attached.

2. Create a context by calling ctaCreateContext.

## Opening the IMGT service

The IMGT service must be opened on a context to use the IMGT service's library of C functions. Opening the IMGT service starts the trunk monitor software on the board and enables it to make status information accessible to the application at specific intervals. Only one instance of the IMGT service can be opened per context.

When the application opens a service, it specifies a board. When the application opens the trunk monitor, it binds it to a context.

To open the IMGT service on a context, the application invokes ctaOpenServices. This function takes an array of CTA_SERVICE_DESC structures as an input argument. Each CTA_SERVICE_DESC structure defines a service (in this case, the IMGT service). If the service is opened successfully, a CTAEVN_OPEN_SERVICES_DONE event with the reason CTA_REASON_FINISHED is delivered to the application.

The CTA_SERVICE_DESC structure is defined as follows:

typedef struct CTA_SERVICE_DESC
{
CTA_SERVICE_NAME name; /* service name */
CTA_SERVICE_ADDR svcaddr; /* reserved */
CTA_SERVICE_ARGS svcargs; /* passes service-specific arguments */
CTA_MVIP_ADDR mvipaddr; /* board #, stream, timeslot, mode */
}CTA_SERVICE_DESC;

The following code sample demonstrates opening the IMGT service:

DWORD ret ;
CTA_EVENT event ;
CTA_SERVICE_DESC services[] =
{
{ {"IMGT", "ADIMGR"}, { 0 }, { 0 }, { 0 } } ,
} ;
/* Open the IMGT service */
ret = ctaOpenServices( ctahd, /* A context handle */
services,
sizeof(InitServices)/sizeof(InitServices[0]),);
if(ret != SUCCESS)
{
/* Opening IMGT service failed */
printf ( "OpenServices failed\n" );
}
else
{
/* Wait for the CTAEVN_OPEN_SERVICES_DONE event */
while (1)
{
ctaWaitEvent( ctaqueuehd, &event, CTA_WAIT_FOREVER);
if (event.id == CTAEVN_OPEN_SERVICES_DONE)
{
/* Check the reason of completion */
if (event.value != CTA_REASON_FINISHED)
{
printf ( "Open services failed\n" );
}
break;
}
else
{
/* Process other (unrelated) messages */
...
}
}
}