Building Controls Virtual Test Bed
util/utilSocket.c File Reference
#include "utilSocket.h"
Include dependency graph for util/utilSocket.c:

Go to the source code of this file.

Functions

int save_append (char **buffer, const char *toAdd, int *bufLen)
 Appends a character array to another character array. More...
 
int assembleBuffer (int flag, int nDbl, int nInt, int nBoo, double curSimTim, double dblVal[], int intVal[], int booVal[], char **buffer, int *bufLen)
 Assembles the buffer that will be exchanged through the IPC. More...
 
int getIntCheckError (const char *nptr, char **endptr, const int base, int *val)
 Gets an integer and does the required error checking. More...
 
int getDoubleCheckError (const char *nptr, char **endptr, double *val)
 Gets a double and does the required error checking. More...
 
int disassembleHeaderBuffer (const char *buffer, char **endptr, const int base, int *fla, int *nDbl, int *nInt, int *nBoo)
 Disassembles the header of the buffer that has been received through the IPC. More...
 
int disassembleBuffer (const char *buffer, int *fla, int *nDbl, int *nInt, int *nBoo, double *curSimTim, double dblVal[], int intVal[], int booVal[])
 Disassembles the buffer that has been received through the IPC. More...
 
int getsocketportnumber (const char *const docname)
 Gets the port number for the BSD socket communication. More...
 
int getmainversionnumber ()
 Returns the version number of the client. More...
 
int getsockethost (const char *const docname, char *const hostname)
 Gets the hostname for the BSD socket communication. More...
 
int establishclientsocket (const char *const docname)
 Establishes a connection to the socket. More...
 
int writetosocket (const int *sockfd, const int *flaWri, const int *nDblWri, const int *nIntWri, const int *nBooWri, double *curSimTim, double dblValWri[], int intValWri[], int booValWri[])
 Writes data to the socket. More...
 
int sendclientmessage (const int *sockfd, const int *flaWri)
 Writes a message flag to the socket stream. More...
 
int getRequiredReadBufferLength (const int *sockfd)
 Returns the required socket buffer length by reading from the socket how many data it contains. More...
 
int getrequiredbufferlength (const int nDbl, const int nInt, const int nBoo)
 Returns the required socket buffer length. More...
 
int readfromsocket (const int *sockfd, int *flaRea, int *nDblRea, int *nIntRea, int *nBooRea, double *curSimTim, double dblValRea[], int intValRea[], int booValRea[])
 Reads data from the socket. More...
 
int readbufferfromsocket (const int *sockfd, char *buffer, int *bufLen)
 Reads a character buffer from the socket. More...
 
int exchangewithsocket (const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, const int *nIntWri, const int *nBooWri, int *nDblRea, int *nIntRea, int *nBooRea, double *simTimWri, double dblValWri[], int intValWri[], int booValWri[], double *simTimRea, double dblValRea[], int intValRea[], int booValRea[])
 Exchanges data with the socket. More...
 
int exchangedoubleswithsocket (const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, int *nDblRea, double *simTimWri, double dblValWri[], double *simTimRea, double dblValRea[])
 Exchanges data with the socket. More...
 
int exchangedoubleswithsocketFMU (const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, int *nDblRea, double *simTimWri, double dblValWri[], double *simTimRea, double dblValRea[], const int *flaExport)
 Exchanges data with the socket. More...
 
int closeipc (int *sockfd)
 Closes the inter process communication socket. More...
 
int main (int argc, const char *argv[])
 

Variables

int FMUEXPORT = 0
 

Function Documentation

int assembleBuffer ( int  flag,
int  nDbl,
int  nInt,
int  nBoo,
double  curSimTim,
double  dblVal[],
int  intVal[],
int  booVal[],
char **  buffer,
int *  bufLen 
)

Assembles the buffer that will be exchanged through the IPC.

Parameters
flagThe communication flag.
nDblThe number of double values.
nIntThe number of integer values.
nBooThe number of boolean values.
dblValThe array that stores the double values.
intValThe array that stores the integer values.
booValThe array that stores the boolean values.
bufferThe buffer into which the values will be written.
bufLenThe buffer length prior and after the call.
Returns
0 if no error occurred.

Definition at line 159 of file util/utilSocket.c.

References i, MAINVERSION, retVal, and save_append().

Referenced by writetosocket().

int closeipc ( int *  sockfd)

Closes the inter process communication socket.

Parameters
sockfdSocket file descripter.
Returns
The return value of the close function.

Definition at line 1193 of file util/utilSocket.c.

Referenced by closeBSDSocket(), closeModelicaClient(), and main().

int disassembleBuffer ( const char *  buffer,
int *  fla,
int *  nDbl,
int *  nInt,
int *  nBoo,
double *  curSimTim,
double  dblVal[],
int  intVal[],
int  booVal[] 
)

Disassembles the buffer that has been received through the IPC.

Parameters
bufferThe buffer that contains the values to be parsed.
flagThe communication flag.
nDblThe number of double values received.
nIntThe number of integer values received.
nBooThe number of boolean values received.
dblValThe array that stores the double values.
intValThe array that stores the integer values.
booValThe array that stores the boolean values.
Returns
0 if no error occurred.

Definition at line 343 of file util/utilSocket.c.

References disassembleHeaderBuffer(), f1, fprintf(), getDoubleCheckError(), getIntCheckError(), i, and retVal.

Referenced by readfromsocket().

int disassembleHeaderBuffer ( const char *  buffer,
char **  endptr,
const int  base,
int *  fla,
int *  nDbl,
int *  nInt,
int *  nBoo 
)

Disassembles the header of the buffer that has been received through the IPC.

This method is separated from disassemblebuffer since in the first call, we only need to peek at the header to assign a long enough buffer for the read operation.

Parameters
bufferThe buffer that contains the values to be parsed.
flagThe communication flag.
nDblThe number of double values received.
nIntThe number of integer values received.
nBooThe number of boolean values received.
Returns
0 if no error occurred.

Definition at line 297 of file util/utilSocket.c.

References getIntCheckError(), i, retVal, and SERVER_VERSION.

Referenced by disassembleBuffer(), and getRequiredReadBufferLength().

int establishclientsocket ( const char *const  docname)

Establishes a connection to the socket.

This method establishes the client socket.

Parameters
docnameName of xml file that contains the socket information.
Returns
The socket file descripter, or a negative value if an error occured.

Definition at line 468 of file util/utilSocket.c.

Referenced by establishBSDSocket(), establishModelicaClient(), first(), and main().

int exchangedoubleswithsocket ( const int *  sockfd,
const int *  flaWri,
int *  flaRea,
const int *  nDblWri,
int *  nDblRea,
double *  simTimWri,
double  dblValWri[],
double *  simTimRea,
double  dblValRea[] 
)

Exchanges data with the socket.

Clients can call this method to exchange data through the socket.

Parameters
sockfdSocket file descripter
flaWriCommunication flag to write to the socket stream.
flaReaCommunication flag read from the socket stream.
nDblWriNumber of double values to write.
nDblReaNumber of double values to read.
simTimWriCurrent simulation time in seconds to write.
dblValWriDouble values to write.
simTimReaCurrent simulation time in seconds read from socket.
dblValReaDouble values read from socket.
See Also
int establishclientsocket(uint16_t *portNo)
Returns
The exit value of send or read, or a negative value if an error occured.

Definition at line 1119 of file util/utilSocket.c.

Referenced by exchangeDoublesWithBSDSocket(), exchangeModelicaClient(), first(), and main().

int exchangedoubleswithsocketFMU ( const int *  sockfd,
const int *  flaWri,
int *  flaRea,
const int *  nDblWri,
int *  nDblRea,
double *  simTimWri,
double  dblValWri[],
double *  simTimRea,
double  dblValRea[],
const int *  flaExport 
)

Exchanges data with the socket.

Clients can call this method to exchange data through the socket. This methods differs from the exchangedoubleswithsocket. It has a flag that is used to set a global variable in readbufferfromsocket.

Parameters
sockfdSocket file descripter
flaWriCommunication flag to write to the socket stream.
flaReaCommunication flag read from the socket stream.
nDblWriNumber of double values to write.
nDblReaNumber of double values to read.
simTimWriCurrent simulation time in seconds to write.
dblValWriDouble values to write.
simTimReaCurrent simulation time in seconds read from socket.
dblValReaDouble values read from socket.
flaexportFlag for FMUExport.
See Also
int establishclientsocket(uint16_t *portNo)
Returns
The exit value of send or read, or a negative value if an error occured.

Definition at line 1160 of file util/utilSocket.c.

References exchangewithsocket(), and FMUEXPORT.

int exchangewithsocket ( const int *  sockfd,
const int *  flaWri,
int *  flaRea,
const int *  nDblWri,
const int *  nIntWri,
const int *  nBooWri,
int *  nDblRea,
int *  nIntRea,
int *  nBooRea,
double *  simTimWri,
double  dblValWri[],
int  intValWri[],
int  booValWri[],
double *  simTimRea,
double  dblValRea[],
int  intValRea[],
int  booValRea[] 
)

Exchanges data with the socket.

Clients can call this method to exchange data through the socket.

Parameters
sockfdSocket file descripter
flaWriCommunication flag to write to the socket stream.
flaReaCommunication flag read from the socket stream.
nDblWriNumber of double values to write.
nIntWriNumber of integer values to write.
nBooWriNumber of boolean values to write.
nDblReaNumber of double values to read.
nIntReaNumber of integer values to read.
nBooReaNumber of boolean values to read.
simTimWriCurrent simulation time in seconds to write.
dblValWriDouble values to write.
intValWriInteger values to write.
boolValWriBoolean values to write.
simTimReaCurrent simulation time in seconds read from socket.
dblValReaDouble values read from socket.
intValReaInteger values read from socket.
boolValReaBoolean values read from socket.
See Also
int establishclientsocket(uint16_t *portNo)
Returns
The exit value of send or read, or a negative value if an error occured.

Definition at line 1049 of file util/utilSocket.c.

References f1, fprintf(), getrequiredbufferlength(), readfromsocket(), REQUIRED_WRITE_LENGTH, retVal, and writetosocket().

Referenced by exchangedoubleswithsocket(), and exchangedoubleswithsocketFMU().

int getDoubleCheckError ( const char *  nptr,
char **  endptr,
double *  val 
)

Gets a double and does the required error checking.

Parameters
nptrPointer to character buffer that contains the number.
endptrAfter return, this variable contains a pointer to the character after the last character of the number.
Thevalue contained in the character buffer.
Returns
0 if no error occurred.

Definition at line 264 of file util/utilSocket.c.

References fprintf().

Referenced by disassembleBuffer().

int getIntCheckError ( const char *  nptr,
char **  endptr,
const int  base,
int *  val 
)

Gets an integer and does the required error checking.

Parameters
nptrPointer to character buffer that contains the number.
endptrAfter return, this variable contains a pointer to the character after the last character of the number.
baseBase for the integer.
Thevalue contained in the character buffer.
Returns
0 if no error occurred.

Definition at line 227 of file util/utilSocket.c.

References FMUEXPORT, and fprintf().

Referenced by disassembleBuffer(), and disassembleHeaderBuffer().

int getmainversionnumber ( )

Returns the version number of the client.

Returns the main version number.

Returns the main version number of the client.

This method returns the version number of the client. A negative return value is used in a dummy dll to check in EnergyPlus whether the BCVTB has been installed.

Returns
The main version number, or a negative value if an error occured.

Definition at line 443 of file util/utilSocket.c.

int getrequiredbufferlength ( const int  nDbl,
const int  nInt,
const int  nBoo 
)

Returns the required socket buffer length.

Parameters
nDblNumber of double values to read or write.
nIntNumber of integer values to read or write.
nBooNumber of boolean values to read or write.
Returns
nCha The nunber of characters needed to store the buffer

Definition at line 867 of file util/utilSocket.c.

References fprintf(), HEADER_LENGTH, and retVal.

Referenced by exchangewithsocket(), and getRequiredReadBufferLength().

int getRequiredReadBufferLength ( const int *  sockfd)

Returns the required socket buffer length by reading from the socket how many data it contains.

This method also set the global variable SERVER_VERSION

Parameters
sockfdSocket file descripter
Returns
nCha The nunber of characters needed to store the buffer

Definition at line 830 of file util/utilSocket.c.

References disassembleHeaderBuffer(), getrequiredbufferlength(), HEADER_LENGTH, and retVal.

Referenced by readfromsocket().

int getsockethost ( const char *const  docname,
char *const  hostname 
)

Gets the hostname for the BSD socket communication.

This method parses the xml file for the socket host name.

Parameters
docnameName of xml file.
hostnameThe hostname will be written to this argument.
Returns
0 if successful, or -1 if an error occured.

Definition at line 454 of file util/utilSocket.c.

References BUFFER_LENGTH, getxmlvalue(), i, and r.

Referenced by establishclientsocket().

int getsocketportnumber ( const char *const  docname)

Gets the port number for the BSD socket communication.

This method parses the xml file for the socket number.

Parameters
docnameName of xml file.
Returns
the socket port number if successful, or -1 if an error occured.

Definition at line 413 of file util/utilSocket.c.

Referenced by establishclientsocket().

int main ( int  argc,
const char *  argv[] 
)

Definition at line 1202 of file util/utilSocket.c.

References fprintf().

int readbufferfromsocket ( const int *  sockfd,
char *  buffer,
int *  bufLen 
)

Reads a character buffer from the socket.

This method is called by readfromsocket.

Parameters
sockfdThe socket file descripter.
bufferThe buffer into which the values will be written.
bufLenThe buffer length prior to the call.
Returns
The exit value of the read command.

Definition at line 977 of file util/utilSocket.c.

References f1, fprintf(), REQUIRED_READ_LENGTH, retVal, and SERVER_VERSION.

Referenced by readfromsocket(), and sendclientmessage().

int readfromsocket ( const int *  sockfd,
int *  flaRea,
int *  nDblRea,
int *  nIntRea,
int *  nBooRea,
double *  curSimTim,
double  dblValRea[],
int  intValRea[],
int  booValRea[] 
)

Reads data from the socket.

Clients can call this method to exchange data through the socket.

Parameters
sockfdSocket file descripter
flaReaCommunication flag read from the socket stream.
nDblReaNumber of double values to read.
nIntReaNumber of integer values to read.
nBooReaNumber of boolean values to read.
curSimTimCurrent simulation time in seconds read from socket.
dblValReaDouble values read from socket.
intValReaInteger values read from socket.
boolValReaBoolean values read from socket.
See Also
int establishclientsocket(uint16_t *portNo)

Definition at line 899 of file util/utilSocket.c.

References disassembleBuffer(), f1, fprintf(), getRequiredReadBufferLength(), i, readbufferfromsocket(), REQUIRED_READ_LENGTH, and retVal.

Referenced by exchangewithsocket().

int save_append ( char **  buffer,
const char *  toAdd,
int *  bufLen 
)

Appends a character array to another character array.

The array size of buffer may be extended by this function to prevent a buffer overflow. If realloc fails to allocate new memory, then this function calls perror(...) and returns EXIT_FAILURE.

Parameters
bufferThe buffer to which the character array will be added.
toAddThe character array that will be appended to buffer
bufLenThe length of the character array buffer. This parameter will be set to the new size of buffer if memory was reallocated.
Returns
0 if no error occurred.

Definition at line 125 of file util/utilSocket.c.

References f1, and fprintf().

Referenced by assembleBuffer().

int sendclientmessage ( const int *  sockfd,
const int *  flaWri 
)

Writes a message flag to the socket stream.

This method should be used by clients if they need to send a flag to the BCVTB.

The flag flaWri is defined as follows: +1: simulation reached end time. -1: simulation terminates due to an (unspecified) error. -10: simulation terminates due to error during initialization. -20: simulation terminates due to error during time integration.

Deprecated:
Use sendclientmessage instead
Parameters
sockfdSocket file descripter
flaWriFlag to be sent to the BCVTB

Definition at line 785 of file util/utilSocket.c.

Referenced by closeModelicaClient(), and main().

int writetosocket ( const int *  sockfd,
const int *  flaWri,
const int *  nDblWri,
const int *  nIntWri,
const int *  nBooWri,
double *  curSimTim,
double  dblValWri[],
int  intValWri[],
int  booValWri[] 
)

Writes data to the socket.

Clients can call this method to write data to the socket.

Parameters
sockfdSocket file descripter
flaWriCommunication flag to write to the socket stream.
nDblWriNumber of double values to write.
nIntWriNumber of integer values to write.
nBooWriNumber of boolean values to write.
curSimTimCurrent simulation time in seconds.
dblValWriDouble values to write.
intValWriInteger values to write.
boolValWriBoolean values to write.
See Also
int establishclientsocket(uint16_t *portNo)
Returns
The exit value of send, or a negative value if an error occured.

Definition at line 670 of file util/utilSocket.c.

References assembleBuffer(), f1, fprintf(), REQUIRED_WRITE_LENGTH, and retVal.

Referenced by exchangewithsocket(), and sendclientmessage().

Variable Documentation

int FMUEXPORT = 0

Definition at line 111 of file util/utilSocket.c.

Referenced by exchangedoubleswithsocketFMU(), and getIntCheckError().