OpenClovis Logo

Code Examples.

Code Examples.

The following example shows how to create a commport, sending and receiving messages on it and then deleting it.

As a first step, a communication port is created. For creating a commport we can specify a required communication port id, if the commport is going to serve as a well known server. This can be chosen from clIocServices.h file. If 0 is passed for commPortId then the IOC will chose one commomuincation port id for the application. For sending and receiving through this communication port the communication handle should be used.

ClRcT retCode = CL_OK;
ClIocCommPortHandleT commPortHandle; // This will filled and returned
// on successful creation of
// communication port.
ClUint32T commPortId = 0x8888; // It can be any port number within the
// range specified in clIocServices.h.
ClUint32T commMode = CL_IOC_RELIABLE_MESSAGING;
retCode = clIocCommPortCreate(commPortId, commMode, &commPortHandle );
if(CL_OK != retCode) {
// Most of the times the error will occur due to Less memory OR
// If the port is already being used by some other component.
// Cannot do any send and receive with this communication port in
// this application.
}
ClUint32T ClRcT
Clovis return code type.
Definition: clCommon.h:168
#define CL_OK
Every thing is OK.
Definition: clCommonErrors.h:68
ClWordT ClIocCommPortHandleT
The Communication port handle.
Definition: clIocApi.h:353
#define CL_IOC_RELIABLE_MESSAGING
Communication port creation related flags .
Definition: clIocApi.h:129
ClRcT clIocCommPortCreate(CL_IN ClIocPortT portId, CL_IN ClIocCommPortFlagsT portType, CL_OUT ClIocCommPortHandleT *pIocCommPortHdl)
Creates a communication port.

Here we can see how a message can be sent to a destination. The protocol field is a must for sending a packet, and it should be chosen from clIocProtocols.h file. The data to be sent should be passed as a buffer message. And the receiver address can be a physical, logical or a multicast address.

ClBufferHandleT sendBuffer = 0;
ClUint32T protocol;
ClIocAddressT receiverAddress ;
ClIocSendOptionT sendOption = {0};
// Create and fill the sendBuffer here.
// Fill the receiverAddress here.
protocol = USER_PROTOCOL; // This is information for the receiver
// to analyse the sendBuffer. This should
// be within the proper range specified in
// clIocProtocol.h file.
sendOption.priority = 0; // You can specify the priority of the
// message.
sendOption.timeout = CL_IOC_TIMEOUT_FOREVER; // Send will return if
// it is able to send the whole message or
// if the timeout occurs, whiever is the
// earliest.
retCode = clIocSend(commPortHandle,
sendBuffer,
protocol,
&receiverAddress,
&sendOption);
if(CL_OK != retCode)
{
// IOC send failed with error code in "retCode"
}
ClPtrT ClBufferHandleT
The type of the handle for the buffer messages.
Definition: clBufferApi.h:82
ClRcT clIocSend(CL_IN ClIocCommPortHandleT commPortHandle, CL_IN ClBufferHandleT message, CL_IN ClUint8T protoType, CL_IN ClIocAddressT *pDestAddr, CL_IN ClIocSendOptionT *pSendOption)
Sends message on a communication port.
#define CL_IOC_TIMEOUT_FOREVER
Infinite timeout.
Definition: clIocApi.h:115
IOC address.
Definition: clIocApi.h:401
Send related options.
Definition: clIocApi.h:457
ClUint32T timeout
The timeout interval in milliseconds.
Definition: clIocApi.h:481
ClUint8T priority
Message priority.
Definition: clIocApi.h:462


The clIocReceive() call will block on the commport for receiving a message. The call expects a valid buffer message handle to receive the data. On receiving some data the IOC unblocks the receiver thread, which is blocked on the commport. All the information relating to the message received( like sender info, length of the message, protocol for analysing the message), will be present in recvParam.

ClIocRecvOptionT recvOption;
ClIocRecvParamT recvParam;
// Create a recvBuffer for holding the receiver message.
recvOption.timeout = CL_IOC_TIMEOUT_FOREVER; // Timeout within which
// the IOC should unblock the receiver
// thread with or without a message.
retCode = clIocReceive(commPortHandle,
recvOption,
recvBuffer,
recvParam);
if(CL_OK != retCode)
{
// IOC receive failed with error code in "retCode"
}
// - recvParam.priority is the priority of the with which the sender
// sent the message.
// - recvParam.protoType is the protocol of the message with which the
// send and the receiver are communicating.
// - recvParam.length is the length of the received message.
// - recvParam.srcAddr is the address of the sender of the message.
ClRcT clIocReceive(CL_IN ClIocCommPortHandleT commPortHdl, CL_IN ClIocRecvOptionT *pRecvOption, CL_OUT ClBufferHandleT userMsg, CL_OUT ClIocRecvParamT *pRecvParam)
Receives message on communication port.
The IOC receive returns this structure along with the message.
Definition: clIocApi.h:491
IOC receive options.
Definition: clIocApi.h:523


For deleting a commport a valid commPortHandle must be passed. And before calling this API we need make sure that no thread is waiting for any data on this commport.

retCode = clIocCommPortDelete(commPortHandle);
if(CL_OK != retCode)
{
//Communication deletion failed.
}
ClRcT clIocCommPortDelete(CL_IN ClIocCommPortHandleT iocCommPortHdl)
Deletes the communication port.

Generated on Tue Jan 10 10:29:15 PST 2012 for OpenClovis SDK using Doxygen