botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
libBotSense.h File Reference

BotSense client library declarations. More...

#include "errno.h"
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "botsense/BotSense.h"

Go to the source code of this file.

Classes

struct  BsClientAppInfo_T
  BotSense client library information and callbacks to application-specific data. More...
 
struct  BsClientConnState_T
 
struct  BsVecHandles_T
 
struct  BsVConnInfo_T
 

Macros

#define BSCLIENT_INFO_STR_MAX_SIZE   256
 
#define BSCLIENT_LOG_WARN(pClient, ecode, wfmt, ...)
 Log Warning. More...
 
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt, ...)
 Log Error. More...
 
#define BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt, ...)
 Log NetMsgs Error. More...
 
#define BSCLIENT_LOG_SYSERROR(pClient, ecode, efmt, ...)
 Log System Error. More...
 
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt, ...)
 Check if BotSense return value is not an error ( < 0). More...
 
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt, ...)
 Check if NetMsgs (un)packing return value is not an error ( < 0). More...
 
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt, ...)
 Check if expression evaluates to true. More...
 
#define BSCLIENT_IS_VCONN_HANDLE(hnd)
 Test if the handle is in the valid client virtual connection range. More...
 
#define BSCLIENT_HAS_VCONN(pClient, hnd)   (BSCLIENT_IS_VCONN_HANDLE(hnd) && bsClientAttrHasVConn(pClient, hnd))
 Tests if the handle is valid and there is an established virtual connection. More...
 

Typedefs

typedef struct _bsClientStructBsClient_P
 Pointer to client structure forward declaration.
 

Functions

const char * bsStrError (int nECode)
 Get the error string describing the BotSense error code. More...
 
int bsPackMsgHdr (BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize)
 Pack BotSense bsProxy message header. More...
 
int bsUnpackMsgHdr (byte_t buf[], size_t bufSize, BsProxyMsgHdr_T *pMsgHdr)
 Unpack BotSense bsProxy message header. More...
 
void bsLogBuf (const char *sBufName, byte_t buf[], size_t uCount)
 Log data bytes. More...
 
void bsLogAsciiBuf (const char *sBufName, byte_t buf[], size_t uCount)
 Log ascii data bytes. More...
 
BsClient_P bsClientNew (const char *sClientName)
 Create a new unconnected proxied client. More...
 
void bsClientDelete (BsClient_P pClient)
 Delete a proxied client. More...
 
int bsClientTrans (BsClient_P pClient, int hndVConn, uint_t uReqMsgId, byte_t bufReq[], size_t uReqBodyLen, uint_t uRspMsgId, byte_t bufRsp[], size_t sizeRspBuf)
 Execute a request - response transaction with the server. More...
 
const char * bsClientAttrGetName (BsClient_P pClient)
 Get client name. More...
 
void bsClientAttrGetTimeouts (BsClient_P pClient, uint_t *pReqTimeout, uint_t *pRspTimeout)
 Get client request (write) and response (read) timeouts. More...
 
void bsClientAttrSetTimeouts (BsClient_P pClient, uint_t uReqTimeout, uint_t uRspTimeout)
 Set client request (write) and response (read) timeouts. More...
 
bool_t bsClientAttrGetTraceState (BsClient_P pClient, BsVConnHnd_T hndVConn)
 Get client virtual connection trace state. More...
 
int bsClientAttrGetVConnCount (BsClient_P pClient)
 Get the number of active virtual connections for this client. More...
 
bool_t bsClientAttrHasVConn (BsClient_P pClient, BsVConnHnd_T hndVConn)
 Test if client has a virtual connection identified by the handle. More...
 
const char * bsClientAttrGetDevName (BsClient_P pClient, BsVConnHnd_T hndVConn)
 Get client virtual connection device name. More...
 
const char * bsClientAttrGetModName (BsClient_P pClient, BsVConnHnd_T hndVConn)
 Get client virtual connection interface module name. More...
 
void bsClientAttrSetLogging (BsClient_P pClient, int nLevel)
 Set client's diagnostics logging threshold. More...
 
void bsClientAttrGetConnState (BsClient_P pClient, BsClientConnState_T *pConnState)
 Get client's connection state. More...
 
void bsClientFillMsgHdr (BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId, size_t uBodyLen, BsProxyMsgHdr_T *pMsgHdr)
 Fill in message header. More...
 
const char * bsClientGetMsgName (BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId)
 Get the message name. More...
 
void bsClientLogMsgHdr (BsClient_P pClient, const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
 Log [pre/un]packed message header. More...
 
int bsServerConnect (BsClient_P pClient, const char *sServerHostName, int ipPortServer)
 Connect to the bsProxy server. More...
 
int bsServerDisconnect (BsClient_P pClient)
 Disconnect from the bsProxy server. More...
 
int bsServerReqGetVersion (BsClient_P pClient, char buf[], size_t sizeBuf)
 Request server to return the server's version string. More...
 
int bsServerReqLoopback (BsClient_P pClient, char sLoopbackMsg[])
 Request server to loopback the requested message data. More...
 
int bsServerReqSetLogging (BsClient_P pClient, int nLogLevel)
 Request server to set the server's logging level. More...
 
int bsServerReqMsgTrace (BsClient_P pClient, BsVConnHnd_T hndVConn, bool_t bTrace)
 Request server to enable/disable message tracing on a virtual connection. More...
 
int bsServerReqOpenDev (BsClient_P pClient, const char *sDevName, const char *sModName, byte_t argbuf[], size_t arglen, const BsClientAppInfo_T *pAppInfo, bool_t bTrace)
 Request server to establish a virtual connection to the device end point. More...
 
int bsServerReqCloseDev (BsClient_P pClient, BsVConnHnd_T hndVConn)
 Request server to close a client's vitual connection. More...
 
int bsServerReqGetVConnList (BsClient_P pClient, BsVecHandles_T *pVecHandles)
 Request server to retrieve the server's list of virtual connection handles for this client. More...
 
int bsServerReqGetVConnInfo (BsClient_P pClient, BsVConnHnd_T hndVConn, BsVConnInfo_T *pVConnInfo)
 Request server to retrieve the server's information for a given virtual connection. More...
 

Detailed Description

BotSense client library declarations.

LastChangedDate
2012-11-25 10:42:41 -0700 (Sun, 25 Nov 2012)
Rev
2544
Note
This file must be swig-able to generate a python extension module.
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file libBotSense.h.

Macro Definition Documentation

#define BSCLIENT_HAS_VCONN (   pClient,
  hnd 
)    (BSCLIENT_IS_VCONN_HANDLE(hnd) && bsClientAttrHasVConn(pClient, hnd))

Tests if the handle is valid and there is an established virtual connection.

Parameters
pClient

Definition at line 285 of file libBotSense.h.

Referenced by bsI2CReqRead(), bsI2CReqScan(), bsI2CReqTrans(), bsI2CReqWrite(), bsNullReqWrite(), bsSerialReqRead(), bsSerialReqTrans(), bsSerialReqWrite(), bsServerReqCloseDev(), and bsServerReqGetVConnInfo().

#define BSCLIENT_INFO_STR_MAX_SIZE   256

Maximum size (including null) of a virtual connection information string.

Definition at line 132 of file libBotSense.h.

#define BSCLIENT_IS_VCONN_HANDLE (   hnd)
Value:
(((int)(hnd) >= BSPROXY_VCONN_MOD_MIN) && \
((int)(hnd) <= BSPROXY_VCONN_MOD_MAX))
#define BSPROXY_VCONN_MOD_MIN
minimum module-specific handle value
Definition: BotSense.h:141
#define BSPROXY_VCONN_MOD_MAX
maximum module-specific handle value
Definition: BotSense.h:142

Test if the handle is in the valid client virtual connection range.

Parameters
hndVirtual connection handle.

Definition at line 274 of file libBotSense.h.

Referenced by bsClientAttrHasVConn(), bsGetVConn(), bsVConnAdd(), and bsVConnRemove().

#define BSCLIENT_LOG_ERROR (   pClient,
  ecode,
  efmt,
  ... 
)
Value:
LOGERROR("%s: %s(): %s(ecode=%d): " efmt, \
bsClientAttrGetName(pClient), LOGFUNCNAME, \
bsStrError(ecode), (ecode>=0? ecode: -ecode), \
##__VA_ARGS__)
const char * bsStrError(int nECode)
Get the error string describing the <b><i>BotSense</i></b> error code.
Definition: bsLibError.c:122
const char * bsClientAttrGetName(BsClient_P pClient)
Get client name.
Definition: bsLibClient.c:918

Log Error.

Parameters
pClient

Definition at line 170 of file libBotSense.h.

Referenced by bsClientRecvRsp(), bsClientTrans(), bsResync(), bsServerReqOpenDev(), bsTransLoadCached(), bsVConnAdd(), bsVConnDelete(), bsVConnNew(), bsVConnRemove(), LtThreadI2C(), LtThreadNull(), and LtThreadSerial().

#define BSCLIENT_LOG_NM_ERROR (   pClient,
  nmecode,
  efmt,
  ... 
)
Value:
BSCLIENT_LOG_ERROR(pClient, BS_ECODE_BAD_MSG, "%s(nmecode=%d): " efmt, \
nmStrError(nmecode), (nmecode>=0? nmecode: -nmecode), ##__VA_ARGS__)
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt,...)
Log Error.
Definition: libBotSense.h:170
#define BS_ECODE_BAD_MSG
bad message
Definition: BotSense.h:76

Log NetMsgs Error.

Parameters
pClient

Definition at line 184 of file libBotSense.h.

Referenced by bsServerReqOpenDev().

#define BSCLIENT_LOG_SYSERROR (   pClient,
  ecode,
  efmt,
  ... 
)
Value:
LOGERROR("%s: %s(): %s(ecode=%d): %s(errno=%d)" efmt, \
bsClientAttrGetName(pClient), LOGFUNCNAME, \
bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \
strerror(errno), errno, ##__VA_ARGS__)
const char * bsStrError(int nECode)
Get the error string describing the <b><i>BotSense</i></b> error code.
Definition: bsLibError.c:122
const char * bsClientAttrGetName(BsClient_P pClient)
Get client name.
Definition: bsLibClient.c:918

Log System Error.

Parameters
pClient

Definition at line 196 of file libBotSense.h.

Referenced by bsClientRead(), bsClientWrite(), bsServerConnect(), bsTransLock(), bsTransUnlock(), ClientStartTestThreadI2C(), ClientStartTestThreadNull(), ClientStartTestThreadSerial(), and ClientStartTestThreadServer().

#define BSCLIENT_LOG_WARN (   pClient,
  ecode,
  wfmt,
  ... 
)
Value:
LOGDIAG3("Warning: %s: %s(): %s(ecode=%d): " wfmt, \
bsClientAttrGetName(pClient), LOGFUNCNAME, \
bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \
##__VA_ARGS__)
const char * bsStrError(int nECode)
Get the error string describing the <b><i>BotSense</i></b> error code.
Definition: bsLibError.c:122
const char * bsClientAttrGetName(BsClient_P pClient)
Get client name.
Definition: bsLibClient.c:918

Log Warning.

Parameters
pClient

Definition at line 156 of file libBotSense.h.

Referenced by bsClientDelete().

#define BSCLIENT_TRY_ECODE (   pClient,
  ecode,
  efmt,
  ... 
)
Value:
do \
{ \
if( (ecode) < 0 ) \
{ \
BSCLIENT_LOG_ERROR(pClient, ecode, efmt, ##__VA_ARGS__); \
return (ecode); \
} \
} while(0)
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt,...)
Log Error.
Definition: libBotSense.h:170

Check if BotSense return value is not an error ( < 0).

If the check is false, an appropriate error is logged and the calling function is immediately exited by invoking a return with the < 0 error code.

Parameters
pClient

Definition at line 214 of file libBotSense.h.

Referenced by bsClientRecvRsp(), bsClientSendReq(), bsClientTrans(), bsI2CReqOpen(), bsI2CReqRead(), bsI2CReqScan(), bsI2CReqTrans(), bsI2CReqWrite(), bsNullReqOpen(), bsNullReqWrite(), bsSerialReqOpen(), bsSerialReqRead(), bsSerialReqTrans(), bsSerialReqWrite(), bsServerReqCloseDev(), bsServerReqGetVConnInfo(), bsServerReqGetVConnList(), bsServerReqGetVersion(), bsServerReqLoopback(), bsServerReqMsgTrace(), and bsServerReqSetLogging().

#define BSCLIENT_TRY_EXPR (   pClient,
  expr,
  ecode,
  efmt,
  ... 
)
Value:
do \
{ \
if( !(expr) ) \
{ \
BSCLIENT_LOG_ERROR(pClient, ecode, "%s: " efmt, #expr, ##__VA_ARGS__); \
return (ecode)>0? -(ecode): (ecode); \
} \
} while(0)
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt,...)
Log Error.
Definition: libBotSense.h:170

Check if expression evaluates to true.

If the check is false, an appropriate error is logged and the calling function is immediately exited by invoking a return with the < 0 error code.

Parameters
pClient

Definition at line 259 of file libBotSense.h.

Referenced by bsClientPackMsgHdr(), bsClientRecvRsp(), bsClientSendReq(), bsClientTrans(), bsClientUnpackMsgHdr(), bsI2CReqRead(), bsI2CReqScan(), bsI2CReqTrans(), bsI2CReqWrite(), bsNullReqWrite(), bsSerialReqRead(), bsSerialReqTrans(), bsSerialReqWrite(), bsServerReqCloseDev(), bsServerReqGetVConnInfo(), bsServerReqOpenDev(), bsVConnAdd(), bsVConnDelete(), and bsVConnRemove().

#define BSCLIENT_TRY_NM_ECODE (   pClient,
  nmecode,
  efmt,
  ... 
)
Value:
do \
{ \
if( (nmecode) < 0 ) \
{ \
BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt, ##__VA_ARGS__); \
return -BS_ECODE_BAD_MSG; \
} \
} while(0)
#define BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt,...)
Log NetMsgs Error.
Definition: libBotSense.h:184
#define BS_ECODE_BAD_MSG
bad message
Definition: BotSense.h:76

Check if NetMsgs (un)packing return value is not an error ( < 0).

If the check is false, an appropriate error is logged and the calling function is immediately exited by invoking a return with the < 0 error code.

Parameters
pClient

Definition at line 236 of file libBotSense.h.

Referenced by bsI2CReqRead(), bsI2CReqScan(), bsI2CReqTrans(), bsI2CReqWrite(), bsNullReqWrite(), bsSerialReqOpen(), bsSerialReqRead(), bsSerialReqTrans(), bsSerialReqWrite(), bsServerReqCloseDev(), bsServerReqGetVConnInfo(), bsServerReqGetVConnList(), bsServerReqGetVersion(), bsServerReqLoopback(), bsServerReqMsgTrace(), and bsServerReqSetLogging().

Function Documentation

void bsClientAttrGetConnState ( BsClient_P  pClient,
BsClientConnState_T pConnState 
)

Get client's connection state.

Parameters
pClient

Definition at line 1120 of file bsLibClient.c.

References BsClientConnState_T::m_bIsConnected, _bsClientStruct::m_pSocket, and BsClientConnState_T::m_sServerHostName.

1122 {
1123  static BsClientConnState_T noconn = {false, ""};
1124 
1125  Socket_T *pSocket = pClient->m_pSocket;
1126 
1127  if( (pSocket == NULL) || !SocketStateIsOpen(pSocket) )
1128  {
1129  *pConnState = noconn;
1130  }
1131 
1132  else
1133  {
1134  pConnState->m_bIsConnected = true;
1135  pConnState->m_sServerHostName = SocketAttrGetRemoteName(pSocket);
1136  }
1137 }
Socket_T * m_pSocket
IP socket to server.
Definition: bsLibInternal.h:99
bool_t m_bIsConnected
client is [not] connected to server
Definition: libBotSense.h:115
const char * m_sServerHostName
server host name
Definition: libBotSense.h:116
const char* bsClientAttrGetDevName ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn 
)

Get client virtual connection device name.

Parameters
pClient

Definition at line 1054 of file bsLibClient.c.

References bsClientAttrHasVConn(), BSPROXY_VCONN_SERVER, BsVConn_T::m_sDevName, and _bsClientStruct::m_tblVConn.

1055 {
1056  if( pClient == NULL )
1057  {
1058  return NULL;
1059  }
1060  else if( hndVConn == BSPROXY_VCONN_SERVER )
1061  {
1062  return "server";
1063  }
1064  else if( !bsClientAttrHasVConn(pClient, hndVConn) )
1065  {
1066  return NULL;
1067  }
1068  else
1069  {
1070  return pClient->m_tblVConn[hndVConn]->m_sDevName;
1071  }
1072 }
BsVConn_T * m_tblVConn[BSPROXY_VCONN_CLIENT_MAX]
table of virtual connections
bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn)
Test if client has a virtual connection identified by the handle.
Definition: bsLibClient.c:1019
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
const char * m_sDevName
device path name
Definition: bsLibInternal.h:88
const char* bsClientAttrGetModName ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn 
)

Get client virtual connection interface module name.

Parameters
pClient

Definition at line 1083 of file bsLibClient.c.

References bsClientAttrHasVConn(), BSPROXY_VCONN_SERVER, BsVConn_T::m_sModName, and _bsClientStruct::m_tblVConn.

1084 {
1085  if( pClient == NULL )
1086  {
1087  return NULL;
1088  }
1089  else if( hndVConn == BSPROXY_VCONN_SERVER )
1090  {
1091  return "server";
1092  }
1093  else if( !bsClientAttrHasVConn(pClient, hndVConn) )
1094  {
1095  return NULL;
1096  }
1097  else
1098  {
1099  return pClient->m_tblVConn[hndVConn]->m_sModName;
1100  }
1101 }
BsVConn_T * m_tblVConn[BSPROXY_VCONN_CLIENT_MAX]
table of virtual connections
const char * m_sModName
interface module path name
Definition: bsLibInternal.h:89
bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn)
Test if client has a virtual connection identified by the handle.
Definition: bsLibClient.c:1019
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
const char* bsClientAttrGetName ( BsClient_P  pClient)

Get client name.

Parameters
pClient

Definition at line 918 of file bsLibClient.c.

References _bsClientStruct::m_sClientName.

Referenced by bsTransCacheRsp(), and bsTransLoadCached().

919 {
920  return pClient!=NULL? pClient->m_sClientName: NULL;
921 }
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
void bsClientAttrGetTimeouts ( BsClient_P  pClient,
uint_t *  pReqTimeout,
uint_t *  pRspTimeout 
)

Get client request (write) and response (read) timeouts.

Parameters
pClient

Definition at line 930 of file bsLibClient.c.

References _bsClientStruct::m_uReqTimeout, and _bsClientStruct::m_uRspTimeout.

933 {
934  if( pClient != NULL )
935  {
936  *pReqTimeout = pClient->m_uReqTimeout / 1000000;
937  *pRspTimeout = pClient->m_uRspTimeout / 1000000;
938  }
939 }
uint_t m_uReqTimeout
client request timeout
uint_t m_uRspTimeout
client response timeout
bool_t bsClientAttrGetTraceState ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn 
)

Get client virtual connection trace state.

Parameters
pClient

Definition at line 970 of file bsLibClient.c.

References bsClientAttrHasVConn(), bsGetVConn(), BSPROXY_VCONN_SERVER, BsVConn_T::m_bTrace, and _bsClientStruct::m_bTraceServer.

971 {
972  if( pClient == NULL )
973  {
974  return false;
975  }
976  else if( hndVConn == BSPROXY_VCONN_SERVER )
977  {
978  return pClient->m_bTraceServer;
979  }
980  else if( bsClientAttrHasVConn(pClient, hndVConn) )
981  {
982  return bsGetVConn(pClient, hndVConn)->m_bTrace;
983  }
984  else
985  {
986  return false;
987  }
988 }
bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn)
Test if client has a virtual connection identified by the handle.
Definition: bsLibClient.c:1019
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
INLINE_IN_H BsVConn_T * bsGetVConn(BsClient_T *pClient, BsVConnHnd_T hndVConn)
bool_t m_bTraceServer
do [not] trace server-ended msgs
bool_t m_bTrace
do [not] trace this vconn msgs
Definition: bsLibInternal.h:87
int bsClientAttrGetVConnCount ( BsClient_P  pClient)

Get the number of active virtual connections for this client.

Parameters
pClient

Definition at line 998 of file bsLibClient.c.

References _bsClientStruct::m_nVConnCount.

Referenced by bsVConnNew().

999 {
1000  if( pClient == NULL )
1001  {
1002  return 0;
1003  }
1004  else
1005  {
1006  return pClient->m_nVConnCount;
1007  }
1008 }
int m_nVConnCount
number of active v. connections
bool_t bsClientAttrHasVConn ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn 
)

Test if client has a virtual connection identified by the handle.

Parameters
pClient

Definition at line 1019 of file bsLibClient.c.

References BSCLIENT_IS_VCONN_HANDLE, BSPROXY_VCONN_UNDEF, _bsClientStruct::m_tblHndIndex, and _bsClientStruct::m_tblVConn.

Referenced by bsClientAttrGetDevName(), bsClientAttrGetModName(), bsClientAttrGetTraceState(), and bsClientGetMsgName().

1020 {
1021  int index;
1022 
1023  if( pClient == NULL )
1024  {
1025  return false;
1026  }
1027  else if( !BSCLIENT_IS_VCONN_HANDLE(hndVConn) )
1028  {
1029  return false;
1030  }
1031  else if( (index = pClient->m_tblHndIndex[hndVConn]) == BSPROXY_VCONN_UNDEF )
1032  {
1033  return false;
1034  }
1035  else if( pClient->m_tblVConn[index] == NULL )
1036  {
1037  return false;
1038  }
1039  else
1040  {
1041  return true;
1042  }
1043 }
BsVConn_T * m_tblVConn[BSPROXY_VCONN_CLIENT_MAX]
table of virtual connections
#define BSCLIENT_IS_VCONN_HANDLE(hnd)
Test if the handle is in the valid client virtual connection range.
Definition: libBotSense.h:274
#define BSPROXY_VCONN_UNDEF
undefined virtual connection handle
Definition: BotSense.h:139
byte_t m_tblHndIndex[BSPROXY_VCONN_MOD_NUMOF]
handle to vconn index table
void bsClientAttrSetLogging ( BsClient_P  pClient,
int  nLevel 
)

Set client's diagnostics logging threshold.

Parameters
pClient

Definition at line 1109 of file bsLibClient.c.

1110 {
1111  LOG_SET_THRESHOLD(nLevel);
1112 }
void bsClientAttrSetTimeouts ( BsClient_P  pClient,
uint_t  uReqTimeout,
uint_t  uRspTimeout 
)

Set client request (write) and response (read) timeouts.

A timeout value of 0 means block forever until i/o operation is complete.

Parameters
pClient

Definition at line 950 of file bsLibClient.c.

References _bsClientStruct::m_uReqTimeout, and _bsClientStruct::m_uRspTimeout.

953 {
954  if( pClient != NULL )
955  {
956  pClient->m_uReqTimeout = uReqTimeout * 1000000;
957  pClient->m_uRspTimeout = uRspTimeout * 1000000;
958  }
959 }
uint_t m_uReqTimeout
client request timeout
uint_t m_uRspTimeout
client response timeout
void bsClientDelete ( BsClient_P  pClient)

Delete a proxied client.

Warning
The client should disconnect prior to deletion.
Parameters
pClient

Definition at line 1438 of file bsLibClient.c.

References BS_ECODE_GEN, BSCLIENT_LOG_WARN, bsTransForget(), bsVConnClearAll(), _bsClientStruct::m_mutexTrans, _bsClientStruct::m_pSocket, _bsClientStruct::m_sClientName, and _bsClientStruct::m_tblTransCache.

1439 {
1440  size_t i;
1441 
1442  if( pClient == NULL )
1443  {
1444  return;
1445  }
1446 
1447  if( SocketStateIsOpen(pClient->m_pSocket) )
1448  {
1450  "Connection still open, auto-disconnecting.");
1451  (void)bsServerDisconnect(pClient);
1452  }
1453 
1454  for(i=0; i<arraysize(pClient->m_tblTransCache); ++i)
1455  {
1456  bsTransForget(pClient, (BsTid_T)i);
1457  }
1458 
1459  bsVConnClearAll(pClient);
1460 
1461  pthread_mutex_destroy(&(pClient->m_mutexTrans));
1462 
1463  delete((void *)(pClient->m_sClientName));
1464 
1465  delete(pClient);
1466 }
static void bsTransForget(BsClient_T *pClient, BsTid_T uTid)
Delete any cached transaction state from the cache.
Definition: bsLibClient.c:184
#define BS_ECODE_GEN
general, unspecified error
Definition: BotSense.h:68
Socket_T * m_pSocket
IP socket to server.
Definition: bsLibInternal.h:99
uint_t BsTid_T
client transaction id type [0-255].
Definition: BotSense.h:172
pthread_mutex_t m_mutexTrans
client transaction mutex
#define BSCLIENT_LOG_WARN(pClient, ecode, wfmt,...)
Log Warning.
Definition: libBotSense.h:156
BsTransInfo_T * m_tblTransCache[BSPROXY_TID_NUMOF]
transaction cache
void bsVConnClearAll(BsClient_T *pClient)
Remove and delete all of a client&#39;s vConnections.
Definition: bsLibClient.c:1478
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
void bsClientFillMsgHdr ( BsClient_P  pClient,
int  hndVConn,
uint_t  uMsgId,
size_t  uBodyLen,
BsProxyMsgHdr_T pMsgHdr 
)

Fill in message header.

Parameters
pClient

Definition at line 1148 of file bsLibClient.c.

References bsNextTid(), BSPROXY_MSG_MAGIC, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

Referenced by bsClientTrans().

1153 {
1154  pMsgHdr->m_hdrMagic = (ushort_t)BSPROXY_MSG_MAGIC;
1155  pMsgHdr->m_hdrTid = (byte_t)bsNextTid(pClient);
1156  pMsgHdr->m_hdrVConn = (byte_t)hndVConn;
1157  pMsgHdr->m_hdrMsgId = (ushort_t)uMsgId;
1158  pMsgHdr->m_hdrBodyLen = (ushort_t)uBodyLen;
1159 }
static BsTid_T bsNextTid(BsClient_T *pClient)
Atomically get the next available transaction id.
Definition: bsLibClient.c:315
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define BSPROXY_MSG_MAGIC
message magic pattern
Definition: BotSense.h:261
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
const char* bsClientGetMsgName ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn,
uint_t  uMsgId 
)

Get the message name.

For each (virtual connection, message id) 2-tuple, there can be a known name string (provided the id is valid and an application provides the information).

Parameters
pClient

Definition at line 1175 of file bsLibClient.c.

References bsClientAttrHasVConn(), bsGetVConn(), BSPROXY_VCONN_SERVER, BsProxyLookupMsgDef(), BsClientAppInfo_T::fnGetMsgName, and BsVConn_T::m_pAppInfo.

Referenced by bsClientLogMsgHdr().

1178 {
1179  static const char *sUnknown = "unknown";
1180  const NMMsgDef_T *pMsgDef;
1181  BsVConn_T *pVConn;
1182  const BsClientAppInfo_T *pAppInfo;
1183 
1184  if( hndVConn == BSPROXY_VCONN_SERVER )
1185  {
1186  pMsgDef = BsProxyLookupMsgDef((BsProxyMsgId_T)uMsgId);
1187  return pMsgDef!=NULL? pMsgDef->m_sMsgName: sUnknown;
1188  }
1189  else if( !bsClientAttrHasVConn(pClient, hndVConn) )
1190  {
1191  return sUnknown;
1192  }
1193  else if( (pVConn = bsGetVConn(pClient, hndVConn)) == NULL )
1194  {
1195  return sUnknown;
1196  }
1197  else if( (pAppInfo = pVConn->m_pAppInfo) == NULL )
1198  {
1199  return sUnknown;
1200  }
1201  else if( pAppInfo->fnGetMsgName == NULL )
1202  {
1203  return sUnknown;
1204  }
1205  else
1206  {
1207  return pAppInfo->fnGetMsgName(pClient, hndVConn, uMsgId);
1208  }
1209 }
const BsClientAppInfo_T * m_pAppInfo
app-specific info (optional)
Definition: bsLibInternal.h:90
const NMMsgDef_T * BsProxyLookupMsgDef(BsProxyMsgId_T eMsgId)
Look up the message definition associated with the message id.
Definition: bsProxyMsgs.c:874
bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn)
Test if client has a virtual connection identified by the handle.
Definition: bsLibClient.c:1019
Client Virtual Connection Info Structure.
Definition: bsLibInternal.h:84
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
INLINE_IN_H BsVConn_T * bsGetVConn(BsClient_T *pClient, BsVConnHnd_T hndVConn)
const char *(* fnGetMsgName)(BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId)
Get the message name callback function.
Definition: libBotSense.h:105
<b><i>BotSense</i></b> client library information and callbacks to application-specific data...
Definition: libBotSense.h:86
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
void bsClientLogMsgHdr ( BsClient_P  pClient,
const char *  sPreface,
BsProxyMsgHdr_T pMsgHdr 
)

Log [pre/un]packed message header.

Parameters
pClient

Definition at line 1219 of file bsLibClient.c.

References bsClientGetMsgName(), BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

1222 {
1223  FILE *fp;
1224  const char *sMsgName;
1225 
1226  fp = LOG_GET_LOGFP();
1227 
1228  if( pMsgHdr != NULL )
1229  {
1230  sMsgName = bsClientGetMsgName(pClient,
1231  (BsVConnHnd_T)pMsgHdr->m_hdrVConn,
1232  (uint_t)pMsgHdr->m_hdrMsgId);
1233 
1234  fprintf(fp, "%s MsgHdr = {\n", sPreface);
1235  fprintf(fp, " Magic: 0x%04x\n", pMsgHdr->m_hdrMagic);
1236  fprintf(fp, " Tid: %u\n", (uint_t)(pMsgHdr->m_hdrTid));
1237  fprintf(fp, " VConn: %u\n", (uint_t)(pMsgHdr->m_hdrVConn));
1238  fprintf(fp, " MsgId: %u %s \n", pMsgHdr->m_hdrMsgId, sMsgName);
1239  fprintf(fp, " BodyLen: %u\n", pMsgHdr->m_hdrBodyLen);
1240  fprintf(fp, "}\n");
1241  }
1242  else
1243  {
1244  fprintf(fp, "%s MsgHdr: (null)\n", sPreface);
1245  }
1246 }
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
const char * bsClientGetMsgName(BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId)
Get the message name.
Definition: bsLibClient.c:1175
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151
BsClient_P bsClientNew ( const char *  sClientName)

Create a new unconnected proxied client.

Parameters
sClientNameProxied client (robot)'s name.
Returns
Returns pointer to new client on success. Else returns NULL.

Definition at line 1396 of file bsLibClient.c.

References BSCLIENT_T_READ, BSCLIENT_T_WRITE, BSPROXY_VCONN_UNDEF, _bsClientStruct::m_bTraceServer, _bsClientStruct::m_mutexTrans, _bsClientStruct::m_nVConnCount, _bsClientStruct::m_pSocket, _bsClientStruct::m_sClientName, _bsClientStruct::m_tblHndIndex, _bsClientStruct::m_tblTransCache, _bsClientStruct::m_tblVConn, _bsClientStruct::m_uReqTimeout, _bsClientStruct::m_uRspTimeout, and _bsClientStruct::m_uTidCounter.

1397 {
1398  BsClient_T *pClient = NEW(BsClient_T);
1399  size_t i;
1400 
1401  pClient->m_sClientName = new_strdup(sClientName);
1402  pClient->m_pSocket = NULL;
1403 
1404  pthread_mutex_init(&(pClient->m_mutexTrans), NULL);
1405 
1406  pClient->m_uReqTimeout = BSCLIENT_T_WRITE;
1407  pClient->m_uRspTimeout = BSCLIENT_T_READ;
1408  pClient->m_uTidCounter = 0;
1409  pClient->m_bTraceServer = false;
1410 
1411  for(i=0; i<arraysize(pClient->m_tblTransCache); ++i)
1412  {
1413  pClient->m_tblTransCache[i] = NULL;
1414  }
1415 
1416  pClient->m_nVConnCount = 0;
1417 
1418  for(i=0; i<arraysize(pClient->m_tblHndIndex); ++i)
1419  {
1420  pClient->m_tblHndIndex[i] = BSPROXY_VCONN_UNDEF;
1421  }
1422 
1423  for(i=0; i<arraysize(pClient->m_tblVConn); ++i)
1424  {
1425  pClient->m_tblVConn[i] = NULL;
1426  }
1427 
1428  return pClient;
1429 }
BsVConn_T * m_tblVConn[BSPROXY_VCONN_CLIENT_MAX]
table of virtual connections
uint_t m_uReqTimeout
client request timeout
Socket_T * m_pSocket
IP socket to server.
Definition: bsLibInternal.h:99
#define BSCLIENT_T_WRITE
5 second write timeout default
Definition: bsLibInternal.h:67
pthread_mutex_t m_mutexTrans
client transaction mutex
The Client Structure Type.
Definition: bsLibInternal.h:96
uint_t m_uRspTimeout
client response timeout
#define BSPROXY_VCONN_UNDEF
undefined virtual connection handle
Definition: BotSense.h:139
uint_t m_uTidCounter
transaction id counter
BsTransInfo_T * m_tblTransCache[BSPROXY_TID_NUMOF]
transaction cache
#define BSCLIENT_T_READ
10 second read timeout default
Definition: bsLibInternal.h:66
bool_t m_bTraceServer
do [not] trace server-ended msgs
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
byte_t m_tblHndIndex[BSPROXY_VCONN_MOD_NUMOF]
handle to vconn index table
int m_nVConnCount
number of active v. connections
int bsClientTrans ( BsClient_P  pClient,
int  hndVConn,
uint_t  uReqMsgId,
byte_t  bufReq[],
size_t  uReqBodyLen,
uint_t  uRspMsgId,
byte_t  bufRsp[],
size_t  sizeRspBuf 
)

Execute a request - response transaction with the server.

Note
The request and response buffer can be the same buffer if packed request contents does not need to be preserved.
Warning
There must be exactly BSPROXY_MSG_HDR_LEN bytes at the front of the request buffer available to pack the BotSense header. (Reduces the number of buffer copies.)
Request Format:
msghdr msgbody
Response Format:
msghdr msgbody
Parameters
pClient

Definition at line 1279 of file bsLibClient.c.

References _BS_LOG_REQ, _BS_LOG_RSP, BS_ECODE_MSG_BAD_HDR, BSCLIENT_LOG_ERROR, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_EXPR, bsClientFillMsgHdr(), bsClientRecvRsp(), bsClientSendReq(), BSPROXY_MSG_HDR_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdRspErr, BsProxyUnpackRspErr(), bsTransCacheRsp(), bsTransForget(), bsTransLoadCached(), bsTransMark(), BsProxyRspErr_T::m_ecode, BsProxyRspErr_T::m_emsg, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

1287 {
1288  BsProxyMsgHdr_T hdrReq; // request message header
1289  BsProxyMsgHdr_T hdrRsp; // response message header
1290  BsTid_T tidReq; // request transaction id
1291  BsProxyRspErr_T msgRspErr; // common error response message
1292  bool_t bPending; // resposne is [not] pending
1293  int n; // number of bytes/return code
1294 
1295  // fill request header
1296  bsClientFillMsgHdr(pClient, hndVConn, uReqMsgId, uReqBodyLen, &hdrReq);
1297 
1298  // this request's transaction id
1299  tidReq = hdrReq.m_hdrTid;
1300 
1301  // mark request for tracking
1302  bsTransMark(pClient, tidReq);
1303 
1304  // send request
1305  n = bsClientSendReq(pClient, &hdrReq, bufReq,
1306  BSPROXY_MSG_HDR_LEN+uReqBodyLen);
1307 
1308  BSCLIENT_TRY_ECODE(pClient, n,
1309  "MsgId=%u: Failed to send request.", uReqMsgId);
1310 
1311  // log successful request sent event
1312  _BS_LOG_REQ(pClient, &hdrReq);
1313 
1314  bPending = true;
1315 
1316  // receive response
1317  while( bPending )
1318  {
1319  // check transaction cache for any cached response.
1320  n = bsTransLoadCached(pClient, tidReq, &hdrRsp, bufRsp, sizeRspBuf);
1321 
1322  BSCLIENT_TRY_ECODE(pClient, n,
1323  "MsgId=%u: Cached response failed for request.", uReqMsgId);
1324 
1325  // found a good, cached response
1326  if( n > 0 )
1327  {
1328  bsTransForget(pClient, tidReq);
1329  bPending = false;
1330  }
1331 
1332  // receive a response
1333  else
1334  {
1335  n = (int)bsClientRecvRsp(pClient, &hdrRsp, bufRsp, sizeRspBuf);
1336 
1337  BSCLIENT_TRY_ECODE(pClient, n,
1338  "MsgId=%u: Receive response failed for request.", uReqMsgId);
1339 
1340  // response was for this request
1341  if( tidReq == hdrRsp.m_hdrTid )
1342  {
1343  bsTransForget(pClient, tidReq);
1344  bPending = false;
1345  }
1346 
1347  // response is for another thread - cache it
1348  else
1349  {
1350  bsTransCacheRsp(pClient, tidReq, &hdrRsp, bufRsp);
1351  }
1352  }
1353  }
1354 
1355  n = (int)hdrRsp.m_hdrBodyLen;
1356 
1357  // received common error response
1358  if( (hdrRsp.m_hdrVConn == BSPROXY_VCONN_SERVER) &&
1359  (hdrRsp.m_hdrMsgId == BsProxyMsgIdRspErr) )
1360  {
1361  n = BsProxyUnpackRspErr(bufRsp, (size_t)n, &msgRspErr, false);
1362 
1363  BSCLIENT_TRY_ECODE(pClient, n,
1364  "MsgId=%u: Failed to unpack error response message body.",
1365  hdrRsp.m_hdrMsgId);
1366 
1367  n = -(int)(msgRspErr.m_ecode);
1368 
1369  BSCLIENT_LOG_ERROR(pClient, n, "%s", msgRspErr.m_emsg);
1370 
1371  return n;
1372  }
1373 
1374  // received application-specific response
1375  else
1376  {
1377  BSCLIENT_TRY_EXPR(pClient, (hdrRsp.m_hdrMsgId == uRspMsgId),
1379  "MsgId=%u: Unexpected response message id, expected MsgId=%u.",
1380  hdrRsp.m_hdrMsgId, uRspMsgId);
1381  }
1382 
1383  // log response received event
1384  _BS_LOG_RSP(pClient, &hdrRsp);
1385 
1386  return n;
1387 }
#define _BS_LOG_REQ(pClient, pMsgHdr)
Log successful request recieved event.
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
static void bsTransForget(BsClient_T *pClient, BsTid_T uTid)
Delete any cached transaction state from the cache.
Definition: bsLibClient.c:184
uint_t BsTid_T
client transaction id type [0-255].
Definition: BotSense.h:172
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt,...)
Log Error.
Definition: libBotSense.h:170
INLINE_IN_H int BsProxyUnpackRspErr(byte_t buf[], size_t uMsgLen, BsProxyRspErr_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspErr ITV message in big-endian byte order from the input buffer.
Definition: bsProxyMsgs.h:366
void bsClientFillMsgHdr(BsClient_P pClient, int hndVConn, uint_t uMsgId, size_t uBodyLen, BsProxyMsgHdr_T *pMsgHdr)
Fill in message header.
Definition: bsLibClient.c:1148
char m_emsg[(NMFVAL_LEN_MAX_STRING)+1]
emsg
Definition: bsProxyMsgs.h:75
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define _BS_LOG_RSP(pClient, pMsgHdr)
Log successful repsonse recieved event.
#define BS_ECODE_MSG_BAD_HDR
bad message header
Definition: BotSense.h:72
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt,...)
Check if expression evaluates to true.
Definition: libBotSense.h:259
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
byte_t m_ecode
ecode
Definition: bsProxyMsgs.h:74
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
static void bsTransCacheRsp(BsClient_T *pClient, BsTid_T uTid, BsProxyMsgHdr_T *pMsgHdr, byte_t bufRsp[])
Cache a transaction response.
Definition: bsLibClient.c:214
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
static int bsClientSendReq(BsClient_T *pClient, BsProxyMsgHdr_T *pMsgHdr, byte_t bufReq[], size_t bufSize)
Send a request message to the server.
Definition: bsLibClient.c:771
static void bsTransMark(BsClient_T *pClient, BsTid_T uTid)
Mark the start of a client&#39;s server transaction in the transaction cache.
Definition: bsLibClient.c:152
static int bsTransLoadCached(BsClient_T *pClient, BsTid_T uTid, BsProxyMsgHdr_T *pMsgHdr, byte_t bufRsp[], size_t sizeRsp)
Load cached response from transaction cache.
Definition: bsLibClient.c:258
static int bsClientRecvRsp(BsClient_T *pClient, BsProxyMsgHdr_T *pMsgHdr, byte_t bufRsp[], size_t bufSize)
Read response message from server.
Definition: bsLibClient.c:829
void bsLogAsciiBuf ( const char *  sBufName,
byte_t  buf[],
size_t  uCount 
)

Log ascii data bytes.

Print diagnostic logging of the contents of a buffer of ASCII bytes.

Parameters
sBufNameName of buffer.
bufBuffer.
uCountNumber of bytes to log.

Definition at line 175 of file bsLibUtils.c.

176 {
177  FILE *fp; // log file pointer
178  size_t i; // working index
179 
180  fp = LOG_GET_LOGFP();
181 
182  fprintf(fp, "%sDiag%d: %s=", LOG_PREFACE, LOG_GET_THRESHOLD()-1, sBufName);
183  for(i=0; i<uCount; ++i)
184  {
185  if( isprint(buf[i]) || isspace(buf[i]) )
186  {
187  switch(buf[i])
188  {
189  case '\f':
190  fprintf(fp, "\\f");
191  break;
192  case '\n':
193  fprintf(fp, "\\n");
194  break;
195  case '\r':
196  fprintf(fp, "\\r");
197  break;
198  case '\t':
199  fprintf(fp, "\\t");
200  break;
201  case '\v':
202  fprintf(fp, "\\v");
203  break;
204  default:
205  fprintf(fp, "%c", buf[i]);
206  break;
207  }
208  }
209  else
210  {
211  fprintf(fp, "\\x%02x", buf[i]);
212  }
213  }
214  fprintf(fp, "\n");
215 }
void bsLogBuf ( const char *  sBufName,
byte_t  buf[],
size_t  uCount 
)

Log data bytes.

Print diagnostic logging of the contents of a buffer of bytes.

Parameters
sBufNameName of buffer.
bufBuffer.
uCountNumber of bytes to log.

Definition at line 145 of file bsLibUtils.c.

146 {
147  FILE *fp; // log file pointer
148  size_t i; // working index
149 
150  fp = LOG_GET_LOGFP();
151 
152  fprintf(fp, "%sDiag%d: %s=", LOG_PREFACE, LOG_GET_THRESHOLD()-1, sBufName);
153  for(i=0; i<uCount; ++i)
154  {
155  if( (i % 16) == 0 )
156  {
157  fprintf(fp, "\n");
158  }
159  fprintf(fp, " 0x%02x", buf[i]);
160  }
161  fprintf(fp, "\n");
162 }
int bsPackMsgHdr ( BsProxyMsgHdr_T pMsgHdr,
byte_t  buf[],
size_t  bufSize 
)

Pack BotSense bsProxy message header.

Parameters
[in]pMsgHdrPointer to message header structure.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
Returns
On success, returns the number of bytes packed.

On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 85 of file bsLibUtils.c.

References BS_ECODE_INTERNAL, BSPROXY_MSG_HDR_LEN, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

86 {
87  int n = 0;
88 
89  if( bufSize < BSPROXY_MSG_HDR_LEN )
90  {
91  LOGERROR("Buffer size=%zu too small.", bufSize);
92  return -BS_ECODE_INTERNAL;
93  }
94 
95  n += nmPackU16(pMsgHdr->m_hdrMagic, buf+n, bufSize-(size_t)n, NMEndianBig);
96  n += nmPackU8(pMsgHdr->m_hdrTid, buf+n, bufSize-(size_t)n, NMEndianBig);
97  n += nmPackU8(pMsgHdr->m_hdrVConn, buf+n, bufSize-(size_t)n, NMEndianBig);
98  n += nmPackU16(pMsgHdr->m_hdrMsgId, buf+n, bufSize-(size_t)n, NMEndianBig);
99  n += nmPackU16(pMsgHdr->m_hdrBodyLen, buf+n, bufSize-(size_t)n, NMEndianBig);
100 
101  return n;
102 }
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
#define BS_ECODE_INTERNAL
internal error (bug)
Definition: BotSense.h:93
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
int bsServerConnect ( BsClient_P  pClient,
const char *  sServerHostName,
int  ipServerPort 
)

Connect to the bsProxy server.

Parameters
pClient

Definition at line 167 of file bsLibServer.c.

References BS_ECODE_SERVER_CONN_FAIL, BS_OK, BSCLIENT_LOG_SYSERROR, bsServerDisconnect(), _bsClientStruct::m_pSocket, and _bsClientStruct::m_sClientName.

170 {
171  Socket_T *pSocket;
172 
173  // close socket if already opened
174  if( SocketStateIsOpen(pClient->m_pSocket) )
175  {
176  bsServerDisconnect(pClient);
177  }
178 
179  // make TCP connection
180  pSocket = SocketOpenTcpConnection(sServerHostName, ipServerPort);
181 
182  if( pSocket == NULL )
183  {
185  "%s:%d", sServerHostName, ipServerPort);
187  }
188 
189  // set socket for non-block I/O
190  SocketAttrSetNonBlocking(pSocket);
191 
192  LOGDIAG2("%s: Connected to server at %s.\n",
193  pClient->m_sClientName, SocketAttrGetRemoteName(pSocket));
194 
195  pClient->m_pSocket = pSocket;
196 
197  return BS_OK;
198 }
Socket_T * m_pSocket
IP socket to server.
Definition: bsLibInternal.h:99
#define BS_ECODE_SERVER_CONN_FAIL
cannot connect to server
Definition: BotSense.h:89
#define BSCLIENT_LOG_SYSERROR(pClient, ecode, efmt,...)
Log System Error.
Definition: libBotSense.h:196
int bsServerDisconnect(BsClient_P pClient)
Disconnect from the bsProxy server.
Definition: bsLibServer.c:207
#define BS_OK
not an error, success
Definition: BotSense.h:66
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
int bsServerDisconnect ( BsClient_P  pClient)

Disconnect from the bsProxy server.

Parameters
pClient

Definition at line 207 of file bsLibServer.c.

References BS_OK, bsVConnClearAll(), _bsClientStruct::m_pSocket, and _bsClientStruct::m_sClientName.

Referenced by bsServerConnect().

208 {
209  if( SocketStateIsOpen(pClient->m_pSocket) )
210  {
211  LOGDIAG2("%s: Disconnected from server at %s.\n",
212  pClient->m_sClientName, SocketAttrGetRemoteName(pClient->m_pSocket));
213  SocketClose(pClient->m_pSocket);
214  SocketDelete(pClient->m_pSocket);
215  pClient->m_pSocket = NULL;
216  bsVConnClearAll(pClient);
217  }
218 
219  return BS_OK;
220 }
Socket_T * m_pSocket
IP socket to server.
Definition: bsLibInternal.h:99
#define BS_OK
not an error, success
Definition: BotSense.h:66
void bsVConnClearAll(BsClient_T *pClient)
Remove and delete all of a client&#39;s vConnections.
Definition: bsLibClient.c:1478
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
int bsServerReqCloseDev ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn 
)

Request server to close a client's vitual connection.

Parameters
pClient

Definition at line 597 of file bsLibServer.c.

References BS_ECODE_BAD_VAL, BS_OK, BSCLIENT_HAS_VCONN, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_EXPR, BSCLIENT_TRY_NM_ECODE, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqDevClose, BsProxyMsgIdRspOk, BsProxyPackReqDevClose(), bsVConnDelete(), bsVConnRemove(), _bsClientStruct::m_sClientName, and BsProxyReqDevClose_T::m_vconn.

598 {
599  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqDevClose;
600  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspOk;
601 
602  BsProxyReqDevClose_T msgReq; // request message
603  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
604  bool_t bTrace; // do [not] trace messages
605  int n; // number of bytes/return code
606  int index; // internal vconn index
607 
608  //
609  // Parameter checks.
610  //
611  BSCLIENT_TRY_EXPR(pClient, BSCLIENT_HAS_VCONN(pClient, hndVConn),
612  BS_ECODE_BAD_VAL, "VConn=%d", hndVConn);
613 
614 
615  // trace state
616  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
617 
618  //
619  // Set request message values.
620  //
621  msgReq.m_vconn = (byte_t)hndVConn;
622 
623  //
624  // Pack request.
625  //
626  n = BsProxyPackReqDevClose(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
627 
628  // check packing return code
629  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
630 
631  //
632  // Execute request-response transaction.
633  //
634  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
635  msgIdReq, buf, (size_t)n,
636  msgIdRsp, buf, sizeof(buf));
637 
638  // check transaction return code
639  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
640 
641  //
642  // Unpack response. No response body.
643  //
644 
645  //
646  // Set return values from response. No response values.
647  //
648 
649  //
650  // Remove and delete client vConnection.
651  //
652  if( (index = bsVConnRemove(pClient, hndVConn)) >= 0 )
653  {
654  bsVConnDelete(pClient, index);
655  }
656 
657  LOGDIAG2("%s: Virtual connecton %u closed.",
658  pClient->m_sClientName, (uint_t)hndVConn);
659 
660  return BS_OK;
661 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
#define BS_ECODE_BAD_VAL
bad value
Definition: BotSense.h:77
int bsVConnRemove(BsClient_T *pClient, BsVConnHnd_T hndVConn)
Remove a vConnection from the client&#39;s tblHndIndex table.
Definition: bsLibClient.c:1565
INLINE_IN_H int BsProxyPackReqDevClose(BsProxyReqDevClose_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqDevClose ITV message in big-endian byte order into the output buffer.
Definition: bsProxyMsgs.h:689
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BS_OK
not an error, success
Definition: BotSense.h:66
int bsVConnDelete(BsClient_T *pClient, int index)
Delete a vConnection (and proxied device) from the client&#39;s tblVConn table.
Definition: bsLibClient.c:1684
const char * m_sClientName
client name
Definition: bsLibInternal.h:98
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt,...)
Check if expression evaluates to true.
Definition: libBotSense.h:259
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSCLIENT_HAS_VCONN(pClient, hnd)
Tests if the handle is valid and there is an established virtual connection.
Definition: libBotSense.h:285
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
byte_t m_vconn
vconn
Definition: bsProxyMsgs.h:216
int bsServerReqGetVConnInfo ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn,
BsVConnInfo_T pVConnInfo 
)

Request server to retrieve the server's information for a given virtual connection.

Parameters
pClient

Definition at line 735 of file bsLibServer.c.

References BS_ECODE_BAD_VAL, BS_OK, BSCLIENT_HAS_VCONN, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_EXPR, BSCLIENT_TRY_NM_ECODE, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqGetVConnInfo, BsProxyMsgIdRspGetVConnInfo, BsProxyPackReqGetVConnInfo(), BsProxyUnpackRspGetVConnInfo(), BsVConnInfo_T::m_client, BsProxyRspGetVConnInfo_T::m_client, BsVConnInfo_T::m_devuri, BsProxyRspGetVConnInfo_T::m_devuri, BsVConnInfo_T::m_moddate, BsProxyRspGetVConnInfo_T::m_moddate, BsVConnInfo_T::m_moduri, BsProxyRspGetVConnInfo_T::m_moduri, BsVConnInfo_T::m_modver, BsProxyRspGetVConnInfo_T::m_modver, BsVConnInfo_T::m_rd, BsProxyRspGetVConnInfo_T::m_rd, BsVConnInfo_T::m_vconn, BsProxyReqGetVConnInfo_T::m_vconn, and BsProxyRspGetVConnInfo_T::m_vconn.

738 {
741 
742  BsProxyReqGetVConnInfo_T msgReq; // request message
743  BsProxyRspGetVConnInfo_T msgRsp; // response message
744  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
745  bool_t bTrace; // do [not] trace messages
746  int n; // num bytes/return code
747 
748  //
749  // Parameter checks.
750  //
751  BSCLIENT_TRY_EXPR(pClient, BSCLIENT_HAS_VCONN(pClient, hndVConn),
752  BS_ECODE_BAD_VAL, "VConn=%d", hndVConn);
753 
754 
755  // trace state
756  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
757 
758  //
759  // Set request message values.
760  //
761  msgReq.m_vconn = (byte_t)hndVConn;
762 
763  //
764  // Pack request.
765  //
766  n = BsProxyPackReqGetVConnInfo(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
767 
768  // check packing return code
769  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
770 
771  //
772  // Execute request-response transaction.
773  //
774  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
775  msgIdReq, buf, (size_t)n,
776  msgIdRsp, buf, sizeof(buf));
777 
778  // check transaction return code
779  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
780 
781  //
782  // Unpack response.
783  //
784  n = BsProxyUnpackRspGetVConnInfo(buf, (size_t)n, &msgRsp, bTrace);
785 
786  // check unpack return code
787  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdRsp);
788 
789  //
790  // Set return values from response.
791  //
792  pVConnInfo->m_vconn = msgRsp.m_vconn;
793  pVConnInfo->m_rd = msgRsp.m_rd;
794  strcpy_s(pVConnInfo->m_client, sizeof(pVConnInfo->m_client),
795  msgRsp.m_client);
796  strcpy_s(pVConnInfo->m_devuri, sizeof(pVConnInfo->m_devuri), msgRsp.m_devuri);
797  strcpy_s(pVConnInfo->m_moduri, sizeof(pVConnInfo->m_moduri), msgRsp.m_moduri);
798  strcpy_s(pVConnInfo->m_modver, sizeof(pVConnInfo->m_modver), msgRsp.m_modver);
799  strcpy_s(pVConnInfo->m_moddate, sizeof(pVConnInfo->m_moddate),
800  msgRsp.m_moddate);
801 
802  return BS_OK;
803 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
#define BS_ECODE_BAD_VAL
bad value
Definition: BotSense.h:77
byte_t m_vconn
virtual connection handle
Definition: libBotSense.h:139
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
INLINE_IN_H int BsProxyPackReqGetVConnInfo(BsProxyReqGetVConnInfo_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqGetVConnInfo ITV message in big-endian byte order into the output buffer...
Definition: bsProxyMsgs.h:775
ReqGetVConnInfo.
Definition: bsProxyMsgs.h:51
#define BS_OK
not an error, success
Definition: BotSense.h:66
RspGetVConnInfo.
Definition: bsProxyMsgs.h:52
char m_moddate[256]
i/f module date
Definition: libBotSense.h:145
char m_modver[256]
i/f module version
Definition: libBotSense.h:144
char m_devuri[(NMFVAL_LEN_MAX_STRING)+1]
devuri
Definition: bsProxyMsgs.h:283
char m_client[256]
client name
Definition: libBotSense.h:141
INLINE_IN_H int BsProxyUnpackRspGetVConnInfo(byte_t buf[], size_t uMsgLen, BsProxyRspGetVConnInfo_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspGetVConnInfo ITV message in big-endian byte order from the input buffer...
Definition: bsProxyMsgs.h:839
char m_moduri[256]
i/f module URI
Definition: libBotSense.h:143
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt,...)
Check if expression evaluates to true.
Definition: libBotSense.h:259
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
char m_client[(NMFVAL_LEN_MAX_STRING)+1]
client
Definition: bsProxyMsgs.h:281
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSCLIENT_HAS_VCONN(pClient, hnd)
Tests if the handle is valid and there is an established virtual connection.
Definition: libBotSense.h:285
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
int m_rd
resource descriptor
Definition: libBotSense.h:140
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
char m_modver[(NMFVAL_LEN_MAX_STRING)+1]
modver
Definition: bsProxyMsgs.h:287
char m_moduri[(NMFVAL_LEN_MAX_STRING)+1]
moduri
Definition: bsProxyMsgs.h:285
char m_devuri[256]
device URI
Definition: libBotSense.h:142
char m_moddate[(NMFVAL_LEN_MAX_STRING)+1]
moddate
Definition: bsProxyMsgs.h:289
int bsServerReqGetVConnList ( BsClient_P  pClient,
BsVecHandles_T pVecHandles 
)

Request server to retrieve the server's list of virtual connection handles for this client.

Parameters
pClient

Definition at line 672 of file bsLibServer.c.

References BS_OK, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_NM_ECODE, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_CLIENT_MAX, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqGetVConnList, BsProxyMsgIdRspGetVConnList, BsProxyUnpackRspGetVConnList(), BsProxyRspGetVConnList_T::m_count, BsVecHandles_T::m_uCount, BsProxyRspGetVConnList_T::m_vconn, BsVecHandles_T::m_vecHnd, and BsProxyRspGetVConnList_T::u.

674 {
677 
678  BsProxyRspGetVConnList_T msgRsp; // response message
679  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
680  bool_t bTrace; // do [not] trace messages
681  int n; // num bytes/return code
682  int i; // working index
683 
684  // trace state
685  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
686 
687  //
688  // Set request message values. No request values.
689  //
690 
691  //
692  // Pack request. No request body.
693  //
694 
695  //
696  // Execute request-response transaction.
697  //
698  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
699  msgIdReq, buf, (size_t)0,
700  msgIdRsp, buf, sizeof(buf));
701 
702  // check transaction return code
703  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
704 
705  //
706  // Unpack response.
707  //
708  n = BsProxyUnpackRspGetVConnList(buf, (size_t)n, &msgRsp, bTrace);
709 
710  // check unpack return code
711  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdRsp);
712 
713  //
714  // Set return values from response.
715  //
716  for(i=0; (i<msgRsp.m_vconn.m_count) && (i<BSPROXY_VCONN_CLIENT_MAX); ++i)
717  {
718  pVecHandles->m_vecHnd[i] = (BsVConnHnd_T)msgRsp.m_vconn.u.m_buf[i];
719  }
720  pVecHandles->m_uCount = msgRsp.m_vconn.m_count;
721 
722  return BS_OK;
723 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
union BsProxyRspGetVConnList_T::@12::@13 u
aligned vector items
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
INLINE_IN_H int BsProxyUnpackRspGetVConnList(byte_t buf[], size_t uMsgLen, BsProxyRspGetVConnList_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspGetVConnList ITV message in big-endian byte order from the input buffer...
Definition: bsProxyMsgs.h:753
#define BS_OK
not an error, success
Definition: BotSense.h:66
BsVConnHnd_T m_vecHnd[BSPROXY_VCONN_CLIENT_MAX]
vector of handles
Definition: libBotSense.h:125
struct BsProxyRspGetVConnList_T::@12 m_vconn
vector
RspGetVConnList.
Definition: bsProxyMsgs.h:50
size_t m_uCount
vector length
Definition: libBotSense.h:124
ReqGetVConnList.
Definition: bsProxyMsgs.h:49
size_t m_count
vector item count
Definition: bsProxyMsgs.h:231
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSPROXY_VCONN_CLIENT_MAX
max number of virtual conn/client
Definition: BotSense.h:122
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151
int bsServerReqGetVersion ( BsClient_P  pClient,
char  bufVer[],
size_t  bufSize 
)

Request server to return the server's version string.

Parameters
pClient

Definition at line 354 of file bsLibServer.c.

References BS_OK, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_NM_ECODE, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqGetVersion, BsProxyMsgIdRspGetVersion, BsProxyUnpackRspGetVersion(), and BsProxyRspGetVersion_T::m_version.

355 {
356  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqGetVersion;
357  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspGetVersion;
358 
359  BsProxyRspGetVersion_T msgRsp; // response message
360  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
361  bool_t bTrace; // do [not] trace messages
362  int n; // num. of bytes/return code
363 
364  // trace state
365  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
366 
367  //
368  // Set request message values. No request values.
369  //
370 
371  //
372  // Pack request. No request body.
373  //
374 
375  //
376  // Execute request-response transaction.
377  //
378  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
379  msgIdReq, buf, (size_t)0,
380  msgIdRsp, buf, sizeof(buf));
381 
382  // check transaction return code
383  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
384 
385  //
386  // Unpack response.
387  //
388  n = BsProxyUnpackRspGetVersion(buf, (size_t)n, &msgRsp, bTrace);
389 
390  // check unpack return code
391  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdRsp);
392 
393  //
394  // Set return values from response.
395  //
396  strcpy_s(bufVer, bufSize, msgRsp.m_version);
397 
398  return BS_OK;
399 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BS_OK
not an error, success
Definition: BotSense.h:66
char m_version[(NMFVAL_LEN_MAX_STRING)+1]
version
Definition: bsProxyMsgs.h:139
INLINE_IN_H int BsProxyUnpackRspGetVersion(byte_t buf[], size_t uMsgLen, BsProxyRspGetVersion_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspGetVersion ITV message in big-endian byte order from the input buffer...
Definition: bsProxyMsgs.h:538
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
int bsServerReqLoopback ( BsClient_P  pClient,
char  sLoopbackMsg[] 
)

Request server to loopback the requested message data.

Parameters
pClient

Definition at line 230 of file bsLibServer.c.

References BS_OK, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_NM_ECODE, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqLoopback, BsProxyMsgIdRspLoopback, BsProxyPackReqLoopback(), BsProxyUnpackRspLoopback(), BsProxyReqLoopback_T::m_cdata, and BsProxyRspLoopback_T::m_cdata.

231 {
232  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqLoopback;
233  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspLoopback;
234 
235  BsProxyReqLoopback_T msgReq; // request message
236  BsProxyRspLoopback_T msgRsp; // response message
237  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
238  bool_t bTrace; // do [not] trace messages
239  int n; // number of bytes/return code
240  size_t m; // loopback cdata length
241 
242  // trace state
243  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
244 
245  //
246  // Set request message values.
247  //
248  m = strlen(sLoopbackMsg) + 1;
249  if( sizeof(msgReq.m_cdata) < m )
250  {
251  m = sizeof(msgReq.m_cdata);
252  }
253 
254  strcpy_s(msgReq.m_cdata, m, sLoopbackMsg);
255 
256  //
257  // Pack request.
258  //
259  n = BsProxyPackReqLoopback(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
260 
261  // check packing return code
262  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
263 
264  //
265  // Execute request-response transaction.
266  //
267  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
268  msgIdReq, buf, (size_t)n,
269  msgIdRsp, buf, sizeof(buf));
270 
271  // check transaction return code
272  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
273 
274  //
275  // Unpack response.
276  //
277  n = BsProxyUnpackRspLoopback(buf, (size_t)n, &msgRsp, bTrace);
278 
279  // check unpack return code
280  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdRsp);
281 
282  //
283  // Set return values from response.
284  //
285  strcpy_s(sLoopbackMsg, m, msgRsp.m_cdata);
286 
287  return BS_OK;
288 }
char m_cdata[(NMFVAL_LEN_MAX_STRING)+1]
cdata
Definition: bsProxyMsgs.h:109
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
INLINE_IN_H int BsProxyPackReqLoopback(BsProxyReqLoopback_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqLoopback ITV message in big-endian byte order into the output buffer.
Definition: bsProxyMsgs.h:388
char m_cdata[(NMFVAL_LEN_MAX_STRING)+1]
cdata
Definition: bsProxyMsgs.h:92
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BS_OK
not an error, success
Definition: BotSense.h:66
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
INLINE_IN_H int BsProxyUnpackRspLoopback(byte_t buf[], size_t uMsgLen, BsProxyRspLoopback_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspLoopback ITV message in big-endian byte order from the input buffer.
Definition: bsProxyMsgs.h:452
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
int bsServerReqMsgTrace ( BsClient_P  pClient,
BsVConnHnd_T  hndVConn,
bool_t  bNewTrace 
)

Request server to enable/disable message tracing on a virtual connection.

Parameters
pClient

Definition at line 411 of file bsLibServer.c.

References BS_OK, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_NM_ECODE, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqMsgTrace, BsProxyMsgIdRspOk, BsProxyPackReqMsgTrace(), BsProxyReqMsgTrace_T::m_trace, and BsProxyReqMsgTrace_T::m_vconn.

414 {
415  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqMsgTrace;
416  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspOk;
417 
418  BsProxyReqMsgTrace_T msgReq; // request message
419  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
420  bool_t bTrace; // do [not] trace messages
421  int n; // number of bytes/return code
422 
423  // trace state
424  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
425 
426  //
427  // Set request message values.
428  //
429  msgReq.m_vconn = (byte_t)hndVConn;
430  msgReq.m_trace = bNewTrace;
431 
432  //
433  // Pack request.
434  //
435  n = BsProxyPackReqMsgTrace(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
436 
437  // check packing return code
438  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
439 
440  //
441  // Execute request-response transaction.
442  //
443  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
444  msgIdReq, buf, (size_t)n,
445  msgIdRsp, buf, sizeof(buf));
446 
447  // check transaction return code
448  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
449 
450  //
451  // Unpack response. No response body.
452  //
453 
454  //
455  // Set return values from response. No response values.
456  //
457 
458  return BS_OK;
459 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
bool_t m_trace
trace
Definition: bsProxyMsgs.h:154
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BS_OK
not an error, success
Definition: BotSense.h:66
INLINE_IN_H int BsProxyPackReqMsgTrace(BsProxyReqMsgTrace_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqMsgTrace ITV message in big-endian byte order into the output buffer.
Definition: bsProxyMsgs.h:560
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
byte_t m_vconn
vconn
Definition: bsProxyMsgs.h:153
int bsServerReqOpenDev ( BsClient_P  pClient,
const char *  sDevName,
const char *  sModName,
byte_t  argbuf[],
size_t  arglen,
const BsClientAppInfo_T pAppInfo,
bool_t  bInitTrace 
)

Request server to establish a virtual connection to the device end point.

The device is open if not already opened by another virtual connection. Otherwise it is attached to this vconn.

The interface module is dynamically loaded into the server and provides the set of services for the client application communicating with the device.

Parameters
pClient

Definition at line 485 of file bsLibServer.c.

References _BS_LOG_VCONN_OPEN, BS_ECODE_BAD_MSG, BS_ECODE_BAD_VAL, BSCLIENT_LOG_ERROR, BSCLIENT_LOG_NM_ERROR, BSCLIENT_TRY_EXPR, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_REQDEVOPEN_ARGBUF_LEN, BSPROXY_REQDEVOPEN_DEVNAME_LEN, BSPROXY_REQDEVOPEN_MODNAME_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqDevOpen, BsProxyMsgIdRspDevOpen, BsProxyPackReqDevOpen(), BsProxyUnpackRspDevOpen(), bsVConnAdd(), bsVConnDelete(), bsVConnNew(), BsProxyReqDevOpen_T::m_argbuf, BsProxyReqDevOpen_T::m_count, BsProxyReqDevOpen_T::m_devname, BsProxyReqDevOpen_T::m_modname, BsProxyReqDevOpen_T::m_trace, BsProxyRspDevOpen_T::m_vconn, and BsProxyReqDevOpen_T::u.

492 {
493  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqDevOpen;
494  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspDevOpen;
495 
496  BsProxyReqDevOpen_T msgReq; // request message
497  BsProxyRspDevOpen_T msgRsp; // response message
498  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
499  bool_t bTrace; // do [not] trace messages
500  int index; // internal vconn index
501  int n; // number of bytes/return code
502  int rc; // return code
503 
504  //
505  // Parameter checks.
506  //
507  BSCLIENT_TRY_EXPR(pClient, (strlen(sDevName)<=BSPROXY_REQDEVOPEN_DEVNAME_LEN),
509  "%zu > %u", strlen(sDevName), BSPROXY_REQDEVOPEN_DEVNAME_LEN);
510 
511  BSCLIENT_TRY_EXPR(pClient, (strlen(sModName)<=BSPROXY_REQDEVOPEN_MODNAME_LEN),
513  "%zu > %u", strlen(sModName), BSPROXY_REQDEVOPEN_MODNAME_LEN);
514 
517  "%zu > %u", arglen, BSPROXY_REQDEVOPEN_ARGBUF_LEN);
518 
519  //
520  // Create and reserve new client vConnection.
521  //
522  if((index=bsVConnNew(pClient, sDevName, sModName, pAppInfo, bInitTrace)) < 0)
523  {
524  return index;
525  }
526 
527  // server-ended trace state
528  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
529 
530  //
531  // Set request message values.
532  //
533  msgReq.m_trace = bInitTrace;
534  strcpy(msgReq.m_devname, sDevName);
535  strcpy(msgReq.m_modname, sModName);
536  memcpy(msgReq.m_argbuf.u.m_buf, argbuf, arglen);
537  msgReq.m_argbuf.m_count = arglen;
538 
539  //
540  // Pack request.
541  //
542  if( (n = BsProxyPackReqDevOpen(&msgReq, BSPROXY_BUF_BODY(buf), bTrace)) < 0 )
543  {
544  BSCLIENT_LOG_NM_ERROR(pClient, n, "MsgId=%u", msgIdReq);
545  rc = -BS_ECODE_BAD_MSG;
546  }
547 
548  //
549  // Execute request-response transaction.
550  //
551  else if( (n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
552  msgIdReq, buf, (size_t)n,
553  msgIdRsp, buf, sizeof(buf))) < 0 )
554  {
555  BSCLIENT_LOG_ERROR(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
556  rc = n;
557  }
558 
559  //
560  // Unpack response.
561  //
562  else if( (n = BsProxyUnpackRspDevOpen(buf, (size_t)n, &msgRsp, bTrace)) < 0 )
563  {
564  BSCLIENT_LOG_NM_ERROR(pClient, n, "MsgId=%u", msgIdRsp);
565  rc = -BS_ECODE_BAD_MSG;
566  }
567 
568  //
569  // Success
570  //
571  else
572  {
573  bsVConnAdd(pClient, msgRsp.m_vconn, index);
574  _BS_LOG_VCONN_OPEN(pClient, index);
575  rc = (int)msgRsp.m_vconn;
576  }
577 
578  //
579  // Error clean up
580  //
581  if( rc < 0 )
582  {
583  bsVConnDelete(pClient, index);
584  }
585 
586  return rc;
587 }
int bsVConnNew(BsClient_T *pClient, const char *sDevName, const char *sModName, const BsClientAppInfo_T *pAppInfo, bool_t bTrace)
Reserve a new client vConnection (and proxied device) in the client&#39;s tblVConn table.
Definition: bsLibClient.c:1614
#define BS_ECODE_BAD_VAL
bad value
Definition: BotSense.h:77
#define BSPROXY_REQDEVOPEN_MODNAME_LEN
Definition: bsProxyMsgs.h:166
char m_modname[(NMFVAL_LEN_MAX_STRING)+1]
modname
Definition: bsProxyMsgs.h:179
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BSCLIENT_LOG_ERROR(pClient, ecode, efmt,...)
Log Error.
Definition: libBotSense.h:170
bool_t m_trace
trace
Definition: bsProxyMsgs.h:176
INLINE_IN_H int BsProxyUnpackRspDevOpen(byte_t buf[], size_t uMsgLen, BsProxyRspDevOpen_T *pStruct, bool_t bTrace)
Unpack a BsProxyRspDevOpen ITV message in big-endian byte order from the input buffer.
Definition: bsProxyMsgs.h:667
#define BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt,...)
Log NetMsgs Error.
Definition: libBotSense.h:184
size_t m_count
vector item count
Definition: bsProxyMsgs.h:183
int bsVConnAdd(BsClient_T *pClient, BsVConnHnd_T hndVConn, int index)
Add a created vConnection to the client&#39;s tblHndIndex table.
Definition: bsLibClient.c:1506
#define BS_ECODE_BAD_MSG
bad message
Definition: BotSense.h:76
#define BSPROXY_REQDEVOPEN_DEVNAME_LEN
Definition: bsProxyMsgs.h:163
#define _BS_LOG_VCONN_OPEN(pClient, index)
Log virtual connection open event.
Definition: bsLibServer.c:76
int bsVConnDelete(BsClient_T *pClient, int index)
Delete a vConnection (and proxied device) from the client&#39;s tblVConn table.
Definition: bsLibClient.c:1684
byte_t m_vconn
vconn
Definition: bsProxyMsgs.h:203
union BsProxyReqDevOpen_T::@10::@11 u
aligned vector items
struct BsProxyReqDevOpen_T::@10 m_argbuf
vector
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt,...)
Check if expression evaluates to true.
Definition: libBotSense.h:259
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSPROXY_REQDEVOPEN_ARGBUF_LEN
Definition: bsProxyMsgs.h:169
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
char m_devname[(NMFVAL_LEN_MAX_STRING)+1]
devname
Definition: bsProxyMsgs.h:177
INLINE_IN_H int BsProxyPackReqDevOpen(BsProxyReqDevOpen_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqDevOpen ITV message in big-endian byte order into the output buffer.
Definition: bsProxyMsgs.h:603
int bsServerReqSetLogging ( BsClient_P  pClient,
int  nLogLevel 
)

Request server to set the server's logging level.

Parameters
pClient

Definition at line 298 of file bsLibServer.c.

References BS_OK, BSCLIENT_TRY_ECODE, BSCLIENT_TRY_NM_ECODE, BSPROXY_BUF_BODY, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdReqSetLogging, BsProxyMsgIdRspOk, BsProxyPackReqSetLogging(), and BsProxyReqSetLogging_T::m_level.

299 {
300  static BsProxyMsgId_T msgIdReq = BsProxyMsgIdReqSetLogging;
301  static BsProxyMsgId_T msgIdRsp = BsProxyMsgIdRspOk;
302 
303  BsProxyReqSetLogging_T msgReq; // request message
304  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
305  bool_t bTrace; // do [not] trace messages
306  int n; // num. of bytes/return code
307 
308  // trace state
309  bTrace = bsClientAttrGetTraceState(pClient, BSPROXY_VCONN_SERVER);
310 
311  //
312  // Set request message values.
313  //
314  msgReq.m_level = nLogLevel;
315 
316  //
317  // Pack request.
318  //
319  n = BsProxyPackReqSetLogging(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
320 
321  // check packing return code
322  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
323 
324  //
325  // Execute request-response transaction.
326  //
327  n = bsClientTrans(pClient, BSPROXY_VCONN_SERVER,
328  msgIdReq, buf, (size_t)n,
329  msgIdRsp, buf, sizeof(buf));
330 
331  // check transaction return code
332  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
333 
334  //
335  // Unpack response. No response body.
336  //
337 
338  //
339  // Set return values from response. No response values.
340  //
341 
342  return BS_OK;
343 }
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
#define BS_OK
not an error, success
Definition: BotSense.h:66
INLINE_IN_H int BsProxyPackReqSetLogging(BsProxyReqSetLogging_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsProxyReqSetLogging ITV message in big-endian byte order into the output buffer.
Definition: bsProxyMsgs.h:474
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
const char* bsStrError ( int  ecode)

Get the error string describing the BotSense error code.

The absolute value of the error code is taken prior retrieving the string. An unknown or out-of-range error code will be mapped to BS_ECODE_BADEC.

Parameters
ecode

Definition at line 122 of file bsLibError.c.

References BS_ECODE_BADEC, and bsEcodeStrTbl.

123 {
124  if( ecode < 0 )
125  {
126  ecode = -ecode;
127  }
128 
129  if( ecode >= arraysize(bsEcodeStrTbl) )
130  {
131  ecode = BS_ECODE_BADEC;
132  }
133  return bsEcodeStrTbl[ecode];
134 }
#define BS_ECODE_BADEC
bad error code
Definition: BotSense.h:95
static const char * bsEcodeStrTbl[]
<b><i>BotSense</i></b> Error Code String Table.
Definition: bsLibError.c:73
int bsUnpackMsgHdr ( byte_t  buf[],
size_t  bufSize,
BsProxyMsgHdr_T pMsgHdr 
)

Unpack BotSense bsProxy message header.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pMsgHdrPointer to message header structure.
Returns
On success, returns the number of bytes unpacked.

On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 115 of file bsLibUtils.c.

References BS_ECODE_INTERNAL, BSPROXY_MSG_HDR_LEN, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

116 {
117  int n = 0;
118 
119  if( bufSize < BSPROXY_MSG_HDR_LEN )
120  {
121  LOGERROR("Buffer size=%zu too small.", bufSize);
122  return -BS_ECODE_INTERNAL;
123  }
124 
125  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrMagic, NMEndianBig);
126  n += nmUnpackU8(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrTid, NMEndianBig);
127  n += nmUnpackU8(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrVConn, NMEndianBig);
128  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrMsgId, NMEndianBig);
129  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrBodyLen,
130  NMEndianBig);
131 
132  return n;
133 }
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
#define BS_ECODE_INTERNAL
internal error (bug)
Definition: BotSense.h:93
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282