botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
BotSense.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // File: BotSense.h
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2011-11-09 08:46:08 -0700 (Wed, 09 Nov 2011) $
10  * $Rev: 1506 $
11  *
12  * \brief \h_botsense package top-level, unifying header 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 2008-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 _BOTSENSE_H
50 #define _BOTSENSE_H
51 
52 #include "rnr/rnrconfig.h"
53 
54 #ifndef SWIG
55 C_DECLS_BEGIN
56 #endif
57 
58 /*!
59  * \ingroup botsense_h
60  * \defgroup bs_ecodes BotSense Error Codes
61  *
62  * \h_botsense package-wide error codes.
63  *
64  * \{
65  */
66 #define BS_OK 0 ///< not an error, success
67 
68 #define BS_ECODE_GEN 1 ///< general, unspecified error
69 #define BS_ECODE_BAD_RECV 2 ///< bad receive
70 #define BS_ECODE_BAD_SEND 3 ///< bad send
71 #define BS_ECODE_BAD_RESYNC 4 ///< bad resync with server/client
72 #define BS_ECODE_MSG_BAD_HDR 5 ///< bad message header
73 #define BS_ECODE_MSG_FRAG 6 ///< message fragment
74 #define BS_ECODE_MSG_TOO_BIG 7 ///< message too big
75 #define BS_ECODE_BUF_TOO_SMALL 8 ///< buffer too small
76 #define BS_ECODE_BAD_MSG 9 ///< bad message
77 #define BS_ECODE_BAD_VAL 10 ///< bad value
78 #define BS_ECODE_MSG_BAD_TID 11 ///< cannot match transaction id
79 #define BS_ECODE_BAD_VCONN_HND 12 ///< bad virtual connection handle
80 #define BS_ECODE_NO_VCONN 13 ///< virtual connection not found
81 #define BS_ECODE_UNKNOWN_REQ 14 ///< unknown request
82 #define BS_ECODE_NO_DEV 15 ///< no proxied device
83 #define BS_ECODE_NO_MOD 16 ///< no interface module
84 #define BS_ECODE_BAD_MOD 17 ///< bad interface module
85 #define BS_ECODE_NO_RSRC 18 ///< no resource available
86 #define BS_ECODE_BUSY 19 ///< resource busy
87 #define BS_ECODE_TIMEDOUT 20 ///< operation timed out
88 #define BS_ECODE_NO_EXEC 21 ///< cannot execute
89 #define BS_ECODE_SERVER_CONN_FAIL 22 ///< cannot connect to server
90 #define BS_ECODE_SERVER_CONN_DENY 23 ///< server denied connection
91 #define BS_ECODE_SERVER_BAD_CLIENT 24 ///< server detected bad client
92 #define BS_ECODE_SYS 25 ///< system (errno) error
93 #define BS_ECODE_INTERNAL 26 ///< internal error (bug)
94 
95 #define BS_ECODE_BADEC 27 ///< bad error code
96 
97 #define BS_ECODE_NUMOF 28 ///< number of error codes
98 /*! \} */
99 
100 /*!
101  * \ingroup botsense_h
102  * \defgroup bs_uri BotSense Uniform Resource Indentifier Attributes
103  *
104  * Applications may used a URI to specify local or BotSense proxied services.
105  * See the RoadNarrows Dynamixel package for an example.
106  * \{
107  */
108 #define BSPROXY_URI_SCHEME "botsense" ///< URI scheme
109 #define BSPROXY_URI_HOSTNAME_DFT "localhost" ///< URI hostname default
110 /*! \} */
111 
112 /*!
113  * \ingroup botsense_h
114  * \defgroup bs_tunes BotSense bsProxy Server Compiled Default Tunables
115  *
116  * These defaults can be overridden in the botsense XML configuration file(s).
117  *
118  * \{
119  */
120 #define BSPROXY_LISTEN_PORT_DFT 9195 ///< default bsProxy passive socket
121 #define BSPROXY_REG_CLIENT_MAX 16 ///< max number of simultaneous clients
122 #define BSPROXY_VCONN_CLIENT_MAX 16 ///< max number of virtual conn/client
123 #define BSPROXY_MSG_BODY_MAX 2048 ///< maximum msg body length (sans header)
124 /*! \} */
125 
126 
127 /*!
128  * \ingroup botsense_h
129  * \defgroup bs_vconn_handle BotSense Handle Attributes
130  *
131  * Virtual connection handles provide a server unique identifier for virtual
132  * connections between \h_botsense clients and opened interface module/proxied
133  * device pairs. One special handle number is reserved for bsProxy server
134  * terminated requests.
135  *
136  * \{
137  */
138 #define BSPROXY_VCONN_MASK 0xff ///< virtual connection handle mask
139 #define BSPROXY_VCONN_UNDEF 255 ///< undefined virtual connection handle
140 #define BSPROXY_VCONN_SERVER 254 ///< handle for server-terminated msgs
141 #define BSPROXY_VCONN_MOD_MIN 0 ///< minimum module-specific handle value
142 #define BSPROXY_VCONN_MOD_MAX 253 ///< maximum module-specific handle value
143 #define BSPROXY_VCONN_MOD_NUMOF 254 ///< number of module-specific handles
144 #define BSPROXY_VCONN_NUMOF (BSPROXY_VCONN_MOD_NUMOF+1)
145  ///< total number of module handles
146 #define BSPROXY_VCONN_MIN (BSPROXY_VCONN_MOD_MIN)
147  ///< minimum handle value
148 #define BSPROXY_VCONN_MAX (BSPROXY_VCONN_MOD_NUMOF)
149  ///< maximum handle value
150 
151 typedef int BsVConnHnd_T; ///< virtual connection handle type
152 /*! \} */
153 
154 
155 /*!
156  * \ingroup botsense_h
157  * \defgroup bs_tid BotSense Transaction Id Attributes
158  *
159  * A transaction id provides an identifier to map responses to requests. Since
160  * a \h_botsense client can be mulit-threaded and the bsProxy server is multi-
161  * threaded by proxied device, responses can be received by the client out of
162  * order. The \h_botsense client library automatically reorders the responses
163  * and delivers the correct response to the given request.
164  *
165  * \{
166  */
167 #define BSPROXY_TID_MIN 0 ///< minimum transaction id
168 #define BSPROXY_TID_MAX 255 ///< maximum transaction id
169 #define BSPROXY_TID_NUMOF 256 ///< number of unique tid's
170 #define BSPROXY_TID_MASK 0xff ///< transaction id mask
171 
172 typedef uint_t BsTid_T; ///< client transaction id type [0-255].
173 /*! \} */
174 
175 
176 /*!
177  * \ingroup botsense_h
178  * \defgroup bs_msgid BotSense Application-Specific Message Id Attributes
179  *
180  * A message id is a client virtual connection unique message identifier.
181  * 16-bits are reserved in the \h_botsense header for upto 64k unique messages.
182  *
183  * \{
184  */
185 #define BSPROXY_MSGID_NUMOF 0x10000 ///< number of vconn unique message ids
186 #define BSPROXY_MSGID_MASK 0xffff ///< message id mask
187 
188 typedef uint_t BsMsgId_T; ///< client message id type [0-64k].
189 /*! \} */
190 
191 
192 /*!
193  * \ingroup botsense_h
194  * \defgroup bs_msguid BotSense Server-Wide Message Unique Id Attributes
195  *
196  * A MSGUID is a server instance unique message id. It is the combination of the
197  * server assigned virtual connection handle (or the reserved server handle)
198  * and the application-specific message id.
199  *
200  * Although the MSGUID is not used by the \h_botsense client library nor by the
201  * bsProxy server directly, it can be a useful construct for client applications
202  * that need this level of uniqueness. The MSGUID is composed if two values:
203  * \li Virtual connection handle.
204  * \li Application-specific message id.
205  *
206  * \{
207  */
208 #define BSPROXY_MSGUID_VCONN_SHIFT 16 ///< v.conn bit shift
209 #define BSPROXY_MSGUID_VCONN_MASK \
210  (BSPROXY_VCONN_MASK << BSPROXY_MSGUID_VCONN_SHIFT) ///< v.connection mask
211 #define BSPROXY_MSGUID_MSGID_MASK (BSPROXY_MSGID_MASK) ///< app message id mask
212 
213 /*!
214  * \brief Make a MSGUID.
215  * \param hndVConn Virtual connection handle.
216  * \param msgid Application specific message id.
217  */
218 #define BSPROXY_MAKE_MSGUID(hndVConn, msgid) \
219  (BsMsgUid_T)( \
220  (((hndVConn) << BSPROXY_MSGUID_VCONN_SHIFT) & BSPROXY_MSGUID_VCONN_MASK) \
221  | ((msgid) & BSPROXY_MSGUID_MSGID_MASK) )
222 
223 /*!
224  * \brief Make a server-specific MSGUID.
225  * \param msgid Application specific message id.
226  */
227 #define BSPROXY_MAKE_SERVER_MSGUID(msgid) \
228  BSPROXY_MAKE_MSGUID(BSPROXY_VCONN_SERVER, msgid)
229 
230 /*!
231  * \brief Get the virtual connection handle from the MSGUID.
232  * \param msguid MSGUID.
233  */
234 #define BSPROXY_GET_MSGUID_HND(msguid) \
235  (BsMsgUid_T)( \
236  (((msguid) >> BSPROXY_MSGUID_VCONN_SHFIT) & BSPROXY_MSGUID_VCONN_MASK) )
237 
238 /*!
239  * \brief Get the message id from the MSGUID.
240  * \param msguid MSGUID.
241  */
242 #define BSPROXY_GET_MSGUID_MSGID(msguid) \
243  (BsMsgUid_T)( ((msguid) & BSPROXY_MSGUID_MSGID_MASK) )
244 
245 typedef uint_t BsMsgUid_T; ///< client message unique id
246 /*! \} */
247 
248 
249 /*!
250  * \ingroup botsense_h
251  * \defgroup bs_hdr BotSense Message Header Attributes
252  *
253  * All \h_botsense server-directed message requests and client-directed
254  * responses contain a fixed-format message header.
255  *
256  * \{
257  */
258 #define BSPROXY_MSG_HDR_LEN 8 ///< message header length (bytes)
259 #define BSPROXY_MSG_MAX_LEN (BSPROXY_MSG_HDR_LEN+BSPROXY_MSG_BODY_MAX)
260  ///< total message maximum length
261 #define BSPROXY_MSG_MAGIC 0xaaaa ///< message magic pattern
262 
263 /*!
264  * \brief Convenience macro to produce a buffer (offset, size) 2-tuple.
265  *
266  * The offset allows libbsclient to pack the header in front of the buffer
267  * without doing any unnecessary copies, while the buffer size is decremented
268  * to account for the header bytes to be packed.
269  *
270  * \param buf Buffer.
271  */
272 #define BSPROXY_BUF_BODY(buf) \
273  (buf)+BSPROXY_MSG_HDR_LEN, sizeof(buf)-(size_t)BSPROXY_MSG_HDR_LEN
274 
275 /*!
276  * \brief BotSense Proxy Message Header Structure
277  */
278 typedef struct
279 {
280  ushort_t m_hdrMagic; ///< "unique" magic pattern
281  byte_t m_hdrTid; ///< transaction id
282  byte_t m_hdrVConn; ///< virtual connection handle (server unique)
283  ushort_t m_hdrMsgId; ///< message id (vConnection unique)
284  ushort_t m_hdrBodyLen; ///< message body length
286 /*! \} */
287 
288 #ifndef SWIG
289 C_DECLS_END
290 #endif
291 
292 
293 #endif // _BOTSENSE_H
uint_t BsMsgId_T
client message id type [0-64k].
Definition: BotSense.h:188
uint_t BsMsgUid_T
client message unique id
Definition: BotSense.h:245
uint_t BsTid_T
client transaction id type [0-255].
Definition: BotSense.h:172
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
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
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