ThingWorx C SDK
Classes
twOSPort.h File Reference

Wrappers for OS-specific functionality. More...

#include <stdint.h>
#include <stdlib.h>
#include "twConfig.h"
#include "twDefaultSettings.h"
#include "twDefinitions.h"
#include "twLogger.h"

Go to the source code of this file.

Classes

struct  twSocket
 twSocket base type definition. More...
 

Functions

Logging functions
char * levelString (enum LogLevel level)
 See twLogger.h.
 
void LOGGING_FUNCTION (enum LogLevel level, const char *timestamp, const char *message)
 See twLogger.h.
 
Time Functions

Time is represented as a 64 bit value representing milliseconds since the epoch.

char twTimeGreaterThan (DATETIME t1, DATETIME t2)
 Compares two DATETIME variables to see if one is greater. More...
 
char twTimeLessThan (DATETIME t1, DATETIME t2)
 Compares two DATETIME variables to see if one is smaller. More...
 
DATETIME twAddMilliseconds (DATETIME t1, int32_t msec)
 Adds milliseconds to a DATETIME. More...
 
DATETIME twGetSystemTime (char utc)
 Gets the current system time. More...
 
uint64_t twGetSystemMillisecondCount ()
 Gets the current system time in milliseconds. More...
 
void twGetSystemTimeString (char *s, const char *format, int length, char msec, char utc)
 Gets the current system time as a string. More...
 
void twGetTimeString (DATETIME time, char *s, const char *format, int length, char msec, char utc)
 Converts a DATETIME to a string. More...
 
void twSleepMsec (int milliseconds)
 
Mutex Functions
TW_MUTEX twMutex_Create ()
 Creates a new ::twMutex. More...
 
void twMutex_Delete (TW_MUTEX m)
 Frees all memory associated with a ::twMutex and all of its owned substructures. More...
 
void twMutex_Lock (TW_MUTEX m)
 Locks a ::twMutex. More...
 
void twMutex_Unlock (TW_MUTEX m)
 Unlocks a ::twMutex. More...
 
Thread/Task Functions
void twTasker_Start ()
 Starts the ::twTasker.
 
void twTasker_Stop ()
 Stops the ::twTasker.
 
File Transfer Functions
int twDirectory_GetFileInfo (char *filename, uint64_t *size, DATETIME *lastModified, char *isDirectory, char *isReadOnly)
 Gets information about a file. More...
 
char twDirectory_FileExists (char *name)
 Checks to see if a file exists. More...
 
int twDirectory_CreateFile (char *name)
 Creates a new file. More...
 
int twDirectory_MoveFile (char *fromName, char *toName)
 Moves an existing file. More...
 
int twDirectory_DeleteFile (char *name)
 Deletes a file. More...
 
int twDirectory_CreateDirectory (char *name)
 Creates a directory. More...
 
int twDirectory_DeleteDirectory (char *name)
 Deletes a directory. More...
 
TW_DIR twDirectory_IterateEntries (char *dirName, TW_DIR dir, char **name, uint64_t *size, DATETIME *lastModified, char *isDirectory, char *isReadOnly)
 
int twDirectory_GetLastError ()
 Gets the errno of the last operation. More...
 

Sockets

#define CLOSED   (char)0
 
#define OPEN   (char)1
 
#define MSG_NOSIGNAL   0
 MSG_NOSIGNAL is not defined on some implementations.
 
typedef struct twSocket twSocket
 twSocket base type definition.
 
twSockettwSocket_Create (const char *host, int16_t port, uint32_t options)
 Creates a new twSocket. More...
 
int twSocket_Connect (twSocket *s)
 Connects a twSocket. More...
 
int twSocket_Reconnect (twSocket *s)
 Reconnects a twSocket. More...
 
int twSocket_Close (twSocket *s)
 Closes a twSocket. More...
 
int twSocket_WaitFor (twSocket *s, int timeout)
 Checks to see if a twSocket is ready for I/O. More...
 
int twSocket_Read (twSocket *s, char *buf, int len, int timeout)
 Reads data from a twSocket. More...
 
int twSocket_Write (twSocket *s, char *buf, int len, int timeout)
 Writes data to a twSocket. More...
 
int twSocket_Delete (twSocket *s)
 Frees all memory associated with a twSocket. More...
 
int twSocket_GetLastError ()
 Gets the errno of the last operation. More...
 

Detailed Description

Wrappers for OS-specific functionality.

Function Documentation

DATETIME twAddMilliseconds ( DATETIME  t1,
int32_t  msec 
)

Adds milliseconds to a DATETIME.

Parameters
[in]t1The DATETIME to add to.
[in]msecThe number of milliseconds to add.
Returns
t1 advanced by msec milliseconds.
int twDirectory_CreateDirectory ( char *  name)

Creates a directory.

Parameters
[in]nameThe full path of the directory to create.
Returns
0 on success or an appropriate errno if an error was encountered.
int twDirectory_CreateFile ( char *  name)

Creates a new file.

Parameters
[in]nameThe full path of the file to create.
Returns
0 on success or an appropriate errno if an error was encountered.
int twDirectory_DeleteDirectory ( char *  name)

Deletes a directory.

Parameters
[in]nameThe full path of the directory to delete.
Returns
0 on success or an appropriate errno if an error was encountered.
int twDirectory_DeleteFile ( char *  name)

Deletes a file.

Parameters
[in]nameThe full path of the file to delete.
Returns
0 on success or an appropriate errno if an error was encountered.
char twDirectory_FileExists ( char *  name)

Checks to see if a file exists.

Parameters
[in]nameThe full path of the file to check.
Returns
#TRUE if the file exists, #FALSE otherwise.
int twDirectory_GetFileInfo ( char *  filename,
uint64_t *  size,
DATETIME lastModified,
char *  isDirectory,
char *  isReadOnly 
)

Gets information about a file.

Parameters
[in]filenameThe full path of the file or directory to get the information of.
[out]sizeA pointer to an integer to store the size of the file in.
[out]lastModifiedA pointer to a DATETIME to store the date/time of the last modification to the file in.
[out]isDirectoryA pointer to a char to store #TRUE if the file is a directory or #FALSE if it isn't.
[out]isReadOnlyA pointer to a char to store #TRUE if the file is read-only or #FALSE if it isn't.
Returns
0 on success or an appropriate errno if an error was encountered.
Note
The calling function will retain ownership of all pointers passed to twDirectory_GetFileInfo().
int twDirectory_GetLastError ( )

Gets the errno of the last operation.

Returns
The errno of the last operation.
TW_DIR twDirectory_IterateEntries ( char *  dirName,
TW_DIR  dir,
char **  name,
uint64_t *  size,
DATETIME lastModified,
char *  isDirectory,
char *  isReadOnly 
)
Parameters
[in]dirNameThe full path of the directory to iterate through.
[out]dirA handle for the directory.
[out]nameA pointer to a string to store the name of the current entry in.
[out]sizeA pointer to an integer to store the size of the current entry in.
[out]lastModifiedA pointer to a DATETIME to store the date/time of the last modification to the current entry in.
[out]isDirectoryA pointer to a char to store #TRUE if the current entry is a directory or #FALSE if it isn't.
[out]isReadOnlyA pointer to a char to store #TRUE if the current entry is read-only or #FALSE if it isn't.
Returns
A handle to the directory or 0 if either there are no more entries in the directory or an error was encountered.
Note
If twDirectory_IterateEntries() is called on dirName for the first time, the function will open the directory and return a handle to the opened directory. If there are no more entries in the directory the directory will be closed and the function will return 0.
The calling function will retain ownership of all pointers passed to twDirectory_IterateEntries().
int twDirectory_MoveFile ( char *  fromName,
char *  toName 
)

Moves an existing file.

Parameters
[in]fromNameThe full path of the file to move.
[in]toNameThe full path to move the file to.
Returns
0 on success or an appropriate errno if an error was encountered.
uint64_t twGetSystemMillisecondCount ( )

Gets the current system time in milliseconds.

Returns
The current system time in milliseconds.
DATETIME twGetSystemTime ( char  utc)

Gets the current system time.

Parameters
[in]utcCurrently unused.
Returns
The current system time.
void twGetSystemTimeString ( char *  s,
const char *  format,
int  length,
char  msec,
char  utc 
)

Gets the current system time as a string.

Parameters
[in,out]sA pointer to the string to write the time to.
[in]formatA string describing how to format the time (via sprintf()).
[in]lengthThe length of the string.
[in]msecIf #TRUE, get time in milliseconds.
[in]utcIf #TRUE, get coordinated universal time.
Returns
Nothing.
void twGetTimeString ( DATETIME  time,
char *  s,
const char *  format,
int  length,
char  msec,
char  utc 
)

Converts a DATETIME to a string.

Parameters
[in]timeThe DATETIME to convert.
[in,out]sA pointer to the string to write the time to.
[in]formatA string describing how to format the time (via sprintf()).
[in]lengthThe length of the string.
[in]msecIf #TRUE, get time in milliseconds.
[in]utcIf #TRUE, get coordinated universal time.
Returns
Nothing.
TW_MUTEX twMutex_Create ( )

Creates a new ::twMutex.

Returns
A pointer to the newly allocated ::twMutex.
Note
The calling function will gain ownership of the returned ::twMutex and is responsible for freeing it via twMutex_Delete().
void twMutex_Delete ( TW_MUTEX  m)

Frees all memory associated with a ::twMutex and all of its owned substructures.

Parameters
[in]mA pointer to the ::twMutex to delete.
Returns
Nothing.
void twMutex_Lock ( TW_MUTEX  m)

Locks a ::twMutex.

Parameters
[in]mA pointer to the ::twMutex to lock.
Returns
Nothing.
void twMutex_Unlock ( TW_MUTEX  m)

Unlocks a ::twMutex.

Parameters
[in]mA pointer to the ::twMutex to unlock.
Returns
Nothing.
int twSocket_Close ( twSocket s)

Closes a twSocket.

Parameters
[in]sA pointer to the twSocket to close.
Returns
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned.
int twSocket_Connect ( twSocket s)

Connects a twSocket.

Parameters
[in]sA pointer to the twSocket to connect.
Returns
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned.
twSocket* twSocket_Create ( const char *  host,
int16_t  port,
uint32_t  options 
)

Creates a new twSocket.

Parameters
[in]hostThe host name of the server.
[in]portThe port the server is listening on.
[in]optionsCurrently unused.
Returns
A pointer to the newly allocated twSocket.
Note
The calling function will gain ownership of the returned twSocket and is responsible for freeing it via twSocket_Delete().
int twSocket_Delete ( twSocket s)

Frees all memory associated with a twSocket.

Parameters
[in]sA pointer to the twSocket to be deleted.
Returns
0 on success, -1 if an error was encountered.
int twSocket_GetLastError ( )

Gets the errno of the last operation.

Returns
The errno of the last operation.
int twSocket_Read ( twSocket s,
char *  buf,
int  len,
int  timeout 
)

Reads data from a twSocket.

Parameters
[in]sA pointer to the twSocket to read from.
[out]bufA buffer to store the read data.
[in]lenThe length of data to be read.
[in]timeoutThe amount of time (in milliseconds) to wait for I/O.
Returns
The number of bytes read or -1 if an error was encountered.
int twSocket_Reconnect ( twSocket s)

Reconnects a twSocket.

Parameters
[in]sA pointer to the twSocket to reconnect.
Returns
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned.
int twSocket_WaitFor ( twSocket s,
int  timeout 
)

Checks to see if a twSocket is ready for I/O.

Parameters
[in]sA pointer to the twSocket to check.
[in]timeoutThe amount of time (in milliseconds) to wait.
Returns
0 if ready, 1 if not ready, -1 if an error was encountered.
int twSocket_Write ( twSocket s,
char *  buf,
int  len,
int  timeout 
)

Writes data to a twSocket.

Parameters
[in]sA pointer to the twSocket to write to.
[in]bufA buffer to of data to write.
[in]lenThe length of data to be written.
[in]timeoutThe amount of time (in milliseconds) to wait for I/O.
Returns
The number of bytes written or -1 if an error was encountered.
char twTimeGreaterThan ( DATETIME  t1,
DATETIME  t2 
)

Compares two DATETIME variables to see if one is greater.

Parameters
[in]t1The DATETIME to compare against.
[in]t1The DATETIME to compare with.
Returns
#TRUE if t1 > t2, #FALSE otherwise.
char twTimeLessThan ( DATETIME  t1,
DATETIME  t2 
)

Compares two DATETIME variables to see if one is smaller.

Parameters
[in]t1The DATETIME to compare against.
[in]t1The DATETIME to compare with.
Returns
#TRUE if t1 < t2, #FALSE otherwise.