botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
libBotSense.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // File: libBotSense.h
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2012-11-25 10:42:41 -0700 (Sun, 25 Nov 2012) $
10  * $Rev: 2544 $
11  *
12  * \brief \h_botsense client library declarations.
13  *
14  * \note This file must be swig-able to generate a python extension module.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2009-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #ifndef _BSCLIENT_H
50 #define _BSCLIENT_H
51 
52 #include "errno.h"
53 
54 #include "rnr/rnrconfig.h"
55 #include "rnr/log.h"
56 
57 #include "botsense/BotSense.h"
58 
59 
60 #ifndef SWIG
61 C_DECLS_BEGIN
62 #endif
63 
64 // ---------------------------------------------------------------------------
65 // Types and Defines
66 // ---------------------------------------------------------------------------
67 
68 /*!
69  * \brief Pointer to client structure forward declaration.
70  */
71 typedef struct _bsClientStruct *BsClient_P;
72 
73 /*!
74  * \brief \h_botsense client library information and callbacks to
75  * application-specific data.
76  *
77  * When an application (library) establishes an application-specific virtual
78  * connection, this static information and callbacks are passed to the client
79  * library.
80  *
81  * The information and callbacks are optional. The \h_botsense library will
82  * ignore any member value that is set to NULL or if the pointer to the info
83  * is NULL (everything is ignored).
84  *
85  */
86 typedef struct
87 {
88  const char *app_name; ///< application name
89  const char *brief; ///< brief one-line description
90  const char *version; ///< dotted version x.y.z[-app]
91  const char *date; ///< version date
92  const char *maintainer; ///< maintainer/owner
93  const char *license; ///< short license/copyright statement
94 
95  /*!
96  * \brief Get the message name callback function.
97  *
98  * \param pClient \h_botsense client.
99  * \param hndVConn Virtual connection handle.
100  * \param uMsgId Message id associated with virtual connection interface.
101  *
102  * \return
103  * Returns static null-terminated string.
104  */
105  const char *(*fnGetMsgName)(BsClient_P pClient,
106  BsVConnHnd_T hndVConn,
107  uint_t uMsgId);
109 
110 /*!
111  * \h_botsense Client Connection State Structure Type
112  */
113 typedef struct
114 {
115  bool_t m_bIsConnected; ///< client is [not] connected to server
116  const char *m_sServerHostName; ///< server host name
118 
119 /*!
120  * \h_botsense Server Vector of Client Virtual Connection Handles Type
121  */
122 typedef struct
123 {
124  size_t m_uCount; ///< vector length
125  BsVConnHnd_T m_vecHnd[BSPROXY_VCONN_CLIENT_MAX]; ///< vector of handles
127 
128 
129 /*!
130  * Maximum size (including null) of a virtual connection information string.
131  */
132 #define BSCLIENT_INFO_STR_MAX_SIZE 256
133 
134 /*!
135  * \h_botsense Server Proxied Device Information Type
136  */
137 typedef struct
138 {
139  byte_t m_vconn; ///< virtual connection handle
140  int m_rd; ///< resource descriptor
141  char m_client[BSCLIENT_INFO_STR_MAX_SIZE]; ///< client name
142  char m_devuri[BSCLIENT_INFO_STR_MAX_SIZE]; ///< device URI
143  char m_moduri[BSCLIENT_INFO_STR_MAX_SIZE]; ///< i/f module URI
144  char m_modver[BSCLIENT_INFO_STR_MAX_SIZE]; ///< i/f module version
145  char m_moddate[BSCLIENT_INFO_STR_MAX_SIZE]; ///< i/f module date
146 } BsVConnInfo_T;
147 
148 /*!
149  * \brief Log Warning.
150  *
151  * \param pClient \h_botsense client.
152  * \param ecode \h_botsense error code.
153  * \param wfmt Warning output format string literal.
154  * \param ... Warning variable arguments.
155  */
156 #define BSCLIENT_LOG_WARN(pClient, ecode, wfmt, ...) \
157  LOGDIAG3("Warning: %s: %s(): %s(ecode=%d): " wfmt, \
158  bsClientAttrGetName(pClient), LOGFUNCNAME, \
159  bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \
160  ##__VA_ARGS__)
161 
162 /*!
163  * \brief Log Error.
164  *
165  * \param pClient \h_botsense client.
166  * \param ecode \h_botsense error code.
167  * \param efmt Error output format string literal.
168  * \param ... Error variable arguments.
169  */
170 #define BSCLIENT_LOG_ERROR(pClient, ecode, efmt, ...) \
171  LOGERROR("%s: %s(): %s(ecode=%d): " efmt, \
172  bsClientAttrGetName(pClient), LOGFUNCNAME, \
173  bsStrError(ecode), (ecode>=0? ecode: -ecode), \
174  ##__VA_ARGS__)
175 
176 /*!
177  * \brief Log NetMsgs Error.
178  *
179  * \param pClient \h_botsense client.
180  * \param nmecode NetMsgs error code.
181  * \param efmt Error output format string literal.
182  * \param ... Error variable arguments.
183  */
184 #define BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt, ...) \
185  BSCLIENT_LOG_ERROR(pClient, BS_ECODE_BAD_MSG, "%s(nmecode=%d): " efmt, \
186  nmStrError(nmecode), (nmecode>=0? nmecode: -nmecode), ##__VA_ARGS__)
187 
188 /*!
189  * \brief Log System Error.
190  *
191  * \param pClient \h_botsense client.
192  * \param ecode \h_botsense error code.
193  * \param efmt Error output format string litteral.
194  * \param ... Error variable arguments.
195  */
196 #define BSCLIENT_LOG_SYSERROR(pClient, ecode, efmt, ...) \
197  LOGERROR("%s: %s(): %s(ecode=%d): %s(errno=%d)" efmt, \
198  bsClientAttrGetName(pClient), LOGFUNCNAME, \
199  bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \
200  strerror(errno), errno, ##__VA_ARGS__)
201 
202 /*!
203  * \brief Check if \h_botsense return value is not an error (\h_lt 0).
204  *
205  * If the check is false, an appropriate error is logged and the calling
206  * function is immediately exited by invoking a <b>return</b> with the \h_lt 0
207  * error code.
208  *
209  * \param pClient \h_botsense client.
210  * \param ecode \h_botsense error code.
211  * \param efmt Error output format string literal.
212  * \param ... Error variable arguments.
213  */
214 #define BSCLIENT_TRY_ECODE(pClient, ecode, efmt, ...) \
215  do \
216  { \
217  if( (ecode) < 0 ) \
218  { \
219  BSCLIENT_LOG_ERROR(pClient, ecode, efmt, ##__VA_ARGS__); \
220  return (ecode); \
221  } \
222  } while(0)
223 
224 /*!
225  * \brief Check if NetMsgs (un)packing return value is not an error (\h_lt 0).
226  *
227  * If the check is false, an appropriate error is logged and the calling
228  * function is immediately exited by invoking a <b>return</b> with the \h_lt 0
229  * error code.
230  *
231  * \param pClient \h_botsense client.
232  * \param nmecode NetMsgs error code.
233  * \param efmt Error output format string literal.
234  * \param ... Error variable arguments.
235  */
236 #define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt, ...) \
237  do \
238  { \
239  if( (nmecode) < 0 ) \
240  { \
241  BSCLIENT_LOG_NM_ERROR(pClient, nmecode, efmt, ##__VA_ARGS__); \
242  return -BS_ECODE_BAD_MSG; \
243  } \
244  } while(0)
245 
246 /*!
247  * \brief Check if expression evaluates to true.
248  *
249  * If the check is false, an appropriate error is logged and the calling
250  * function is immediately exited by invoking a <b>return</b> with the \h_lt 0
251  * error code.
252  *
253  * \param pClient \h_botsense client.
254  * \param expr Validation expression.
255  * \param ecode \h_botsense error code.
256  * \param efmt Error output format string literal.
257  * \param ... Error variable arguments.
258  */
259 #define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt, ...) \
260  do \
261  { \
262  if( !(expr) ) \
263  { \
264  BSCLIENT_LOG_ERROR(pClient, ecode, "%s: " efmt, #expr, ##__VA_ARGS__); \
265  return (ecode)>0? -(ecode): (ecode); \
266  } \
267  } while(0)
268 
269 /*!
270  * \brief Test if the handle is in the valid client virtual connection range.
271  *
272  * \param hnd Virtual connection handle.
273  */
274 #define BSCLIENT_IS_VCONN_HANDLE(hnd) \
275  (((int)(hnd) >= BSPROXY_VCONN_MOD_MIN) && \
276  ((int)(hnd) <= BSPROXY_VCONN_MOD_MAX))
277 
278 /*!
279  * \brief Tests if the handle is valid and there is an established
280  * virtual connection.
281  *
282  * \param pClient \h_botsense client.
283  * \param hnd Virtual connection handle.
284  */
285 #define BSCLIENT_HAS_VCONN(pClient, hnd) \
286  (BSCLIENT_IS_VCONN_HANDLE(hnd) && bsClientAttrHasVConn(pClient, hnd))
287 
288 
289 
290 // ---------------------------------------------------------------------------
291 // Prototypes
292 // ---------------------------------------------------------------------------
293 
294 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
295 // Utility Prototypes
296 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
297 
298 extern const char *bsStrError(int nECode);
299 
300 extern int bsPackMsgHdr(BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize);
301 
302 extern int bsUnpackMsgHdr(byte_t buf[],
303  size_t bufSize,
304  BsProxyMsgHdr_T *pMsgHdr);
305 
306 #ifdef LOG
307 
308 extern void bsLogBuf(const char *sBufName, byte_t buf[], size_t uCount);
309 
310 extern void bsLogAsciiBuf(const char *sBufName, byte_t buf[], size_t uCount);
311 
312 #endif // LOG
313 
314 
315 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
316 // Client-Specific Prototypes
317 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
318 
319 extern BsClient_P bsClientNew(const char *sClientName);
320 
321 extern void bsClientDelete(BsClient_P pClient);
322 
323 extern int bsClientTrans(BsClient_P pClient,
324  int hndVConn,
325  uint_t uReqMsgId,
326  byte_t bufReq[],
327  size_t uReqBodyLen,
328  uint_t uRspMsgId,
329  byte_t bufRsp[],
330  size_t sizeRspBuf);
331 
332 extern const char *bsClientAttrGetName(BsClient_P pClient);
333 
334 extern void bsClientAttrGetTimeouts(BsClient_P pClient,
335  uint_t *pReqTimeout,
336  uint_t *pRspTimeout);
337 
338 extern void bsClientAttrSetTimeouts(BsClient_P pClient,
339  uint_t uReqTimeout,
340  uint_t uRspTimeout);
341 
342 extern bool_t bsClientAttrGetTraceState(BsClient_P pClient,
343  BsVConnHnd_T hndVConn);
344 
345 extern int bsClientAttrGetVConnCount(BsClient_P pClient);
346 
347 extern bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn);
348 
349 extern const char *bsClientAttrGetDevName(BsClient_P pClient,
350  BsVConnHnd_T hndVConn);
351 
352 extern const char *bsClientAttrGetModName(BsClient_P pClient,
353  BsVConnHnd_T hndVConn);
354 
355 extern void bsClientAttrSetLogging(BsClient_P pClient, int nLevel);
356 
357 extern void bsClientAttrGetConnState(BsClient_P pClient,
358  BsClientConnState_T *pConnState);
359 
360 extern void bsClientFillMsgHdr(BsClient_P pClient,
361  BsVConnHnd_T hndVConn,
362  uint_t uMsgId,
363  size_t uBodyLen,
364  BsProxyMsgHdr_T *pMsgHdr);
365 
366 extern const char *bsClientGetMsgName(BsClient_P pClient,
367  BsVConnHnd_T hndVConn,
368  uint_t uMsgId);
369 
370 #ifdef LOG
371 
372 extern void bsClientLogMsgHdr(BsClient_P pClient,
373  const char *sPreface,
374  BsProxyMsgHdr_T *pMsgHdr);
375 
376 #endif // LOG
377 
378 
379 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
380 // Client - Server (bsProxy) Connection, Control, and Info Requests Prototypes
381 //. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
382 
383 extern int bsServerConnect(BsClient_P pClient,
384  const char *sServerHostName,
385  int ipPortServer);
386 
387 extern int bsServerDisconnect(BsClient_P pClient);
388 
389 extern int bsServerReqGetVersion(BsClient_P pClient,
390  char buf[],
391  size_t sizeBuf);
392 
393 extern int bsServerReqLoopback(BsClient_P pClient, char sLoopbackMsg[]);
394 
395 extern int bsServerReqSetLogging(BsClient_P pClient, int nLogLevel);
396 
397 extern int bsServerReqMsgTrace(BsClient_P pClient,
398  BsVConnHnd_T hndVConn,
399  bool_t bTrace);
400 
401 extern int bsServerReqOpenDev(BsClient_P pClient,
402  const char *sDevName,
403  const char *sModName,
404  byte_t argbuf[],
405  size_t arglen,
406  const BsClientAppInfo_T *pAppInfo,
407  bool_t bTrace);
408 
409 extern int bsServerReqCloseDev(BsClient_P pClient, BsVConnHnd_T hndVConn);
410 
411 extern int bsServerReqGetVConnList(BsClient_P pClient,
412  BsVecHandles_T *pVecHandles);
413 
414 extern int bsServerReqGetVConnInfo(BsClient_P pClient,
415  BsVConnHnd_T hndVConn,
416  BsVConnInfo_T *pVConnInfo);
417 
418 #ifndef SWIG
419 C_DECLS_END
420 #endif
421 
422 
423 #endif // _BSCLIENT_H
BsClient_P bsClientNew(const char *sClientName)
Create a new unconnected proxied client.
Definition: bsLibClient.c:1396
void bsClientAttrSetTimeouts(BsClient_P pClient, uint_t uReqTimeout, uint_t uRspTimeout)
Set client request (write) and response (read) timeouts.
Definition: bsLibClient.c:950
void bsClientFillMsgHdr(BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId, size_t uBodyLen, BsProxyMsgHdr_T *pMsgHdr)
Fill in message header.
Definition: bsLibClient.c:1148
int bsPackMsgHdr(BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize)
Pack <b><i>BotSense</i></b> bsProxy message header.
Definition: bsLibUtils.c:85
int bsServerReqGetVersion(BsClient_P pClient, char buf[], size_t sizeBuf)
Request server to return the server&#39;s version string.
Definition: bsLibServer.c:354
byte_t m_vconn
virtual connection handle
Definition: libBotSense.h:139
const char * brief
brief one-line description
Definition: libBotSense.h:89
bool_t m_bIsConnected
client is [not] connected to server
Definition: libBotSense.h:115
const char * date
version date
Definition: libBotSense.h:91
int bsServerReqGetVConnList(BsClient_P pClient, BsVecHandles_T *pVecHandles)
Request server to retrieve the server&#39;s list of virtual connection handles for this client...
Definition: bsLibServer.c:672
const char * bsStrError(int nECode)
Get the error string describing the <b><i>BotSense</i></b> error code.
Definition: bsLibError.c:122
const char * maintainer
maintainer/owner
Definition: libBotSense.h:92
The Client Structure Type.
Definition: bsLibInternal.h:96
const char * license
short license/copyright statement
Definition: libBotSense.h:93
const char * m_sServerHostName
server host name
Definition: libBotSense.h:116
const char * version
dotted version x.y.z[-app]
Definition: libBotSense.h:90
const char * bsClientGetMsgName(BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId)
Get the message name.
Definition: bsLibClient.c:1175
bool_t bsClientAttrGetTraceState(BsClient_P pClient, BsVConnHnd_T hndVConn)
Get client virtual connection trace state.
Definition: bsLibClient.c:970
int bsServerDisconnect(BsClient_P pClient)
Disconnect from the bsProxy server.
Definition: bsLibServer.c:207
int bsServerReqLoopback(BsClient_P pClient, char sLoopbackMsg[])
Request server to loopback the requested message data.
Definition: bsLibServer.c:230
const char * bsClientAttrGetName(BsClient_P pClient)
Get client name.
Definition: bsLibClient.c:918
void bsClientDelete(BsClient_P pClient)
Delete a proxied client.
Definition: bsLibClient.c:1438
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.
Definition: bsLibServer.c:485
const char * bsClientAttrGetModName(BsClient_P pClient, BsVConnHnd_T hndVConn)
Get client virtual connection interface module name.
Definition: bsLibClient.c:1083
int bsClientAttrGetVConnCount(BsClient_P pClient)
Get the number of active virtual connections for this client.
Definition: bsLibClient.c:998
void bsLogAsciiBuf(const char *sBufName, byte_t buf[], size_t uCount)
Log ascii data bytes.
Definition: bsLibUtils.c:175
int bsUnpackMsgHdr(byte_t buf[], size_t bufSize, BsProxyMsgHdr_T *pMsgHdr)
Unpack <b><i>BotSense</i></b> bsProxy message header.
Definition: bsLibUtils.c:115
<b><i>BotSense</i></b> client library information and callbacks to application-specific data...
Definition: libBotSense.h:86
void bsClientAttrSetLogging(BsClient_P pClient, int nLevel)
Set client&#39;s diagnostics logging threshold.
Definition: bsLibClient.c:1109
const char * bsClientAttrGetDevName(BsClient_P pClient, BsVConnHnd_T hndVConn)
Get client virtual connection device name.
Definition: bsLibClient.c:1054
size_t m_uCount
vector length
Definition: libBotSense.h:124
bool_t bsClientAttrHasVConn(BsClient_P pClient, BsVConnHnd_T hndVConn)
Test if client has a virtual connection identified by the handle.
Definition: bsLibClient.c:1019
void bsClientAttrGetTimeouts(BsClient_P pClient, uint_t *pReqTimeout, uint_t *pRspTimeout)
Get client request (write) and response (read) timeouts.
Definition: bsLibClient.c:930
int bsServerConnect(BsClient_P pClient, const char *sServerHostName, int ipPortServer)
Connect to the bsProxy server.
Definition: bsLibServer.c:167
#define BSCLIENT_INFO_STR_MAX_SIZE
Definition: libBotSense.h:132
void bsClientAttrGetConnState(BsClient_P pClient, BsClientConnState_T *pConnState)
Get client&#39;s connection state.
Definition: bsLibClient.c:1120
void bsClientLogMsgHdr(BsClient_P pClient, const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
Log [pre/un]packed message header.
Definition: bsLibClient.c:1219
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.
Definition: bsLibClient.c:1279
int bsServerReqCloseDev(BsClient_P pClient, BsVConnHnd_T hndVConn)
Request server to close a client&#39;s vitual connection.
Definition: bsLibServer.c:597
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
struct _bsClientStruct * BsClient_P
Pointer to client structure forward declaration.
Definition: libBotSense.h:71
#define BSPROXY_VCONN_CLIENT_MAX
max number of virtual conn/client
Definition: BotSense.h:122
int m_rd
resource descriptor
Definition: libBotSense.h:140
int bsServerReqMsgTrace(BsClient_P pClient, BsVConnHnd_T hndVConn, bool_t bTrace)
Request server to enable/disable message tracing on a virtual connection.
Definition: bsLibServer.c:411
int bsServerReqGetVConnInfo(BsClient_P pClient, BsVConnHnd_T hndVConn, BsVConnInfo_T *pVConnInfo)
Request server to retrieve the server&#39;s information for a given virtual connection.
Definition: bsLibServer.c:735
const char * app_name
application name
Definition: libBotSense.h:88
<b><i>BotSense</i></b> package top-level, unifying header declarations.
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151
void bsLogBuf(const char *sBufName, byte_t buf[], size_t uCount)
Log data bytes.
Definition: bsLibUtils.c:145
int bsServerReqSetLogging(BsClient_P pClient, int nLogLevel)
Request server to set the server&#39;s logging level.
Definition: bsLibServer.c:298