bsproxy/bsproxy.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //
00003 // Package:   botsense
00004 //
00005 // File:      bsproxy.h
00006 //
00007 /*! \file
00008  *
00009  * \version $LastChangedDate$ $Rev$
00010  *
00011  * \brief Botsense proxy IP server declarations.
00012  *
00013  * \author Robin Knight (robin.knight@roadnarrows.com)
00014  *
00015  * \par Copyright:
00016  * (C) 2007.  RoadNarrows LLC.
00017  * (http://www.roadnarrows.com)
00018  * \n All Rights Reserved
00019  */
00020 // Permission is hereby granted, without written agreement and without
00021 // license or royalty fees, to use, copy, modify, and distribute this
00022 // software and its documentation for any purpose, provided that
00023 // (1) The above copyright notice and the following two paragraphs
00024 // appear in all copies of the source code and (2) redistributions
00025 // including binaries reproduces these notices in the supporting
00026 // documentation.   Substantial modifications to this software may be
00027 // copyrighted by their authors and need not follow the licensing terms
00028 // described here, provided that the new terms are clearly indicated in
00029 // all files where they apply.
00030 //
00031 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
00032 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
00033 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
00034 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
00035 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
00036 // THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
00039 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00040 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
00041 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
00042 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00043 //
00044 ////////////////////////////////////////////////////////////////////////////////
00045 
00046 #include <pthread.h>
00047 
00048 #include "rnr/rnrconfig.h"
00049 #include "rnr/log.h"
00050 #include "rnr/sock.h"
00051 #include "rnr/sockset.h"
00052 
00053 #include "rcb3/rcb3lib.h"
00054 
00055 
00056 //-----------------------------------------------------------------------------
00057 // Data Types and Defines
00058 //-----------------------------------------------------------------------------
00059 
00060 /*!
00061  * BotSense Proxy Limits
00062  */
00063 #define BSPROXY_CLIENT_DEV_MAX  8 ///< max. num of proxied devices per client
00064 #define BSPROXY_REG_CLIENT_MAX  8 ///< max. num of simultaneous reg. clients
00065 
00066 /*!
00067  * Device Classes
00068  */
00069 #define BSPROXY_DEVCLASS_NONE   0 ///< no device
00070 #define BSPROXY_DEVCLASS_I2C    1 ///< shared I2C bus
00071 #define BSPROXY_DEVCLASS_SER    2 ///< point-to-point serial
00072 #define BSPROXY_DEVCLASS_SWR    3 ///< software resource
00073 #define BSPROXY_DEVCLASS_RCB3   4 ///< RCB-3 
00074 
00075 /*!
00076  * Generic I2C proxied device
00077  */
00078 typedef struct
00079 {
00080   int           m_fd;                 ///< opened device descriptor
00081 } BsProxyDevI2C_T;
00082 
00083 /*!
00084  * Generic RS-232 proxied device
00085  */
00086 typedef struct
00087 {
00088   int           m_fd;         ///< opened device descriptor
00089   int           m_nBaudRate;  ///< baud rate
00090   int           m_nByteSize;  ///< byte size
00091   char          m_cParity;    ///< parity
00092   int           m_nStopBits;  ///< stop bits
00093   bool_t        m_bRtsCts;    ///< hardware flow control
00094   bool_t        m_bXonXoff;   ///< software flow control
00095 } BsProxyDevRS232_T;
00096 
00097 /*!
00098  * RCB-3 robot controller RS-232 proxied device
00099  */
00100 typedef struct
00101 {
00102   handle_t      m_nHandle;    ///< libRCB3 created interface handle
00103 } BsProxyDevRCB3_T;
00104 
00105 /*!
00106  * BotSense Proxied Device Control Block Structure
00107  */
00108 typedef struct
00109 {
00110   const char       *m_sDevName;     ///< device name
00111   int               m_eDevClass;    ///< device class
00112   int               m_nDevRefCnt;   ///< reference count
00113   pthread_mutex_t   m_mutDev;       ///< shared device mutex
00114   union
00115   {
00116     BsProxyDevI2C_T     i2c;
00117     BsProxyDevRS232_T   rs232;
00118     BsProxyDevRCB3_T    rcb3;
00119   } m_unDevSpec;                    ///< specific proxied device data
00120 } BsProxyDev_T;
00121 
00122 /*!
00123  * BotSense Proxy Client Device Info
00124  */
00125 typedef struct
00126 {
00127   int           m_eDevType;   ///< device type
00128   BsProxyDev_T *m_pProxDev;   ///< proxied device 
00129   uint_t        m_i2cAddr;    ///< BrainPack I2C device address
00130 } BsProxyClientDev_T;
00131 
00132 /*!
00133  * BotSense Proxy Client Control Block Structure
00134  */
00135 typedef struct
00136 {
00137   const char         *m_sClientName;            ///< client's (remote) name
00138   Socket_T           *m_pClientSock;            ///< client opened TCP socket
00139   BsProxyClientDev_T  m_tblClientDev[BSPROXY_CLIENT_DEV_MAX];
00140                                                 ///< client's proxied devices
00141 } BsProxyClient_T;
00142 
00143 
00144 /*!
00145  * BotSense Proxy Server Control Block Structure
00146  */
00147 typedef struct
00148 {
00149   const char       *m_sServerName;                  ///< server's name
00150   int               m_nServerPort;                  ///< server's listener port
00151   SockSet_T        *m_pServerSockSet;               ///< server socket set
00152   Socket_T         *m_pServerSockListener;          ///< server listener socket
00153   int               m_nServerRegClientCount;        ///< num of reg. clients
00154   BsProxyClient_T  *m_pServerRegClient[FD_SETSIZE]; ///< registered clients
00155 } BsProxyServer_T;
00156 
00157 
00158 //-----------------------------------------------------------------------------
00159 // Command Logging and Checking Macros
00160 //-----------------------------------------------------------------------------
00161 
00162 /*!
00163  * \brief Log command invocation.
00164  *
00165  * \param pClient   BotSense client.
00166  * \param bufCmd    Received command buffer.
00167  * \param lenCmd    Received command length (number of bytes).
00168  */
00169 #define BSPROXY_LOG_CMD(pClient, bufCmd, lenCmd) \
00170   LOGDIAG2("%s: %s(msgid=0x%02x, tid=%u, blen=%u)", \
00171       pClient->m_sClientName, LOGFUNCNAME, \
00172       (uint_t)bufCmd[BSPROXY_CMD_HDR_MSGID_IDX], \
00173       (uint_t)bufCmd[BSPROXY_CMD_HDR_TID_IDX], \
00174       (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX])
00175 
00176 /*!
00177  * \brief Log device specific command invocation.
00178  *
00179  * \param pClient   BotSense client.
00180  * \param bufCmd    Received command buffer.
00181  * \param lenCmd    Received command length (number of bytes).
00182  */
00183 #define BSPROXY_LOG_DEV_CMD(pClient, bufCmd, lenCmd) \
00184   LOGDIAG2("%s: %s(msgid=0x%02x, tid=%u, blen=%u, cmdid=0x%02x)", \
00185       pClient->m_sClientName, LOGFUNCNAME, \
00186       (uint_t)bufCmd[BSPROXY_CMD_HDR_MSGID_IDX], \
00187       (uint_t)bufCmd[BSPROXY_CMD_HDR_TID_IDX] ,\
00188       (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX], \
00189       (uint_t)bufCmd[BSPROXY_CMD_HDR_LEN+1])
00190 
00191 /*!
00192  * \brief Check that length of command body equals the expected value.
00193  *
00194  * Automatically returns from embedding function if false.
00195  *
00196  * \param pClient   BotSense client.
00197  * \param bufCmd    Received command buffer.
00198  * \param lenValid  Expected length (number of bytes).
00199  */
00200 #define chkCmdBLenEQ(pClient, bufCmd, lenValid) \
00201   {if( bufCmd[BSPROXY_CMD_HDR_BLEN_IDX] != lenValid ) \
00202   { \
00203     LOGERROR("%s: lenBody=%u != lenValid=%u: bad command body length", \
00204         pClient->m_sClientName, (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX], \
00205         (uint_t)(lenValid)); \
00206     return ClientSendFailRsp(pClient, bufCmd, "%u: bad command body length", \
00207         (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX]); \
00208   }}
00209 
00210 /*!
00211  * \brief Check that length of command body is greater than or equal the
00212  * minimum value.
00213  *
00214  * Automatically returns from embedding function if false.
00215  *
00216  * \param pClient   BotSense client.
00217  * \param bufCmd    Received command buffer.
00218  * \param lenMin    Expected minimum length (number of bytes).
00219  */
00220 #define chkCmdBLenGE(pClient, bufCmd, lenMin) \
00221   {if( bufCmd[BSPROXY_CMD_HDR_BLEN_IDX] < lenMin ) \
00222   { \
00223     LOGERROR("%s: lenBody=%u < lenMin=%u: bad command body length", \
00224         pClient->m_sClientName, (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX], \
00225         (uint_t)(lenMin)); \
00226     return ClientSendFailRsp(pClient, bufCmd, "%u: bad command body length", \
00227         (uint_t)bufCmd[BSPROXY_CMD_HDR_BLEN_IDX]); \
00228   }}
00229 
00230 
00231 //-----------------------------------------------------------------------------
00232 // Byte Stream Parameter Packing/Unpacking Functions
00233 //-----------------------------------------------------------------------------
00234 
00235 /*!
00236  * \brief Pack lower 16 bits into the next 2 bytes of the byte stream.
00237  * \param uVal Value to pack.
00238  * \param bytes[] Output byte stream.
00239  * \return Returns number of bytes packed.
00240  */
00241 INLINE_IN_H int BSPACK16(uint_t uVal, byte_t bytes[/*2+*/])
00242 {
00243   bytes[0] = (byte_t)((uVal >> 8) & 0xFF);    
00244   bytes[1] = (byte_t)(uVal & 0xFF);
00245   return 2;
00246 }
00247 
00248 /*!
00249  * \brief Unpack the next 2 bytes from the byte stream into lower 16 bits.
00250  * \param bytes[] Input byte stream.
00251  * \param pVal Output unpacked value. 
00252  * \return Returns number of bytes unpacked.
00253  */
00254 INLINE_IN_H int BSUNPACK16(byte_t bytes[/*2+*/], uint_t *pVal)
00255 {
00256   *pVal = (uint_t)((((uint_t)bytes[0]) << 8) + (uint_t)bytes[1]);
00257   return 2;
00258 }
00259 
00260 /*!
00261  * \brief Pack lower 32 bits into the next 4 bytes of the byte stream.
00262  * \param uVal Value to pack.
00263  * \param bytes[] Output byte stream.
00264  * \return Returns number of bytes packed.
00265  */
00266 INLINE_IN_H int BSPACK32(uint_t uVal, byte_t bytes[/*4+*/])
00267 {
00268   bytes[0] = (byte_t)((uVal >> 24) & 0xFF);    
00269   bytes[1] = (byte_t)((uVal >> 16) & 0xFF);    
00270   bytes[2] = (byte_t)((uVal >>  8) & 0xFF);    
00271   bytes[3] = (byte_t)(uVal & 0xFF);
00272   return 4;
00273 }
00274 
00275 /*!
00276  * \brief Unpack the next 4 bytes from the byte stream into lower 32 bits.
00277  * \param bytes[] Input byte stream.
00278  * \param pVal Output unpacked value. 
00279  * \return Returns number of bytes unpacked.
00280  */
00281 INLINE_IN_H int BSUNPACK32(byte_t bytes[/*4+*/], uint_t *pVal)
00282 {
00283   *pVal = (uint_t)((((uint_t)bytes[0]) << 24) + 
00284           (uint_t)(((uint_t)bytes[1]) << 16) + 
00285           (uint_t)(((uint_t)bytes[2]) <<  8) + 
00286           (uint_t)bytes[1]);
00287   return 4;
00288 }
00289 
00290 
00291 //-----------------------------------------------------------------------------
00292 // Proxied Devices Prototypes
00293 //-----------------------------------------------------------------------------
00294 
00295 extern BsProxyDev_T *ProxDevOpen(int eDevType, const char *sDevName);
00296 
00297 extern void ProxDevClose(BsProxyDev_T *pProxDev);
00298 
00299 extern int ProxDevI2CScan(BsProxyDev_T *pProxDev,
00300                           byte_t bufScan[], size_t sizeBuf);
00301 
00302 extern int ProxDevI2CTrans(BsProxyDev_T *pProxDev, uint_t i2cAddr,
00303                     byte_t bufWrite[], size_t countWrite,
00304                     byte_t bufRead[], size_t countRead);
00305 
00306 extern int ProxDevI2CWrite(BsProxyDev_T *pProxDev, uint_t i2cAddr,
00307                     byte_t bufWrite[], size_t countWrite);
00308 
00309 extern int ProxDevI2CRead(BsProxyDev_T *pProxDev, uint_t i2cAddr,
00310                    byte_t bufRead[], size_t countRead);
00311 
00312 extern int ProxDevRCB3Read(BsProxyDev_T *pProxDev,
00313                     byte_t bufRead[], size_t countRead);
00314 
00315 extern int ProxDevRCB3Write(BsProxyDev_T *pProxDev,
00316                     byte_t bufWrite[], size_t countWrite);
00317 
00318 extern int ProxDevRCB3Trans(BsProxyDev_T *pProxDev,
00319                       byte_t bufWrite[], size_t countWrite,
00320                       byte_t bufRead[], size_t countRead);
00321 
00322 
00323 //-----------------------------------------------------------------------------
00324 // Client General Support Prototypes
00325 //-----------------------------------------------------------------------------
00326 
00327 extern int ClientDispatch(BsProxyServer_T *pServer, BsProxyClient_T *pClient,
00328                           byte_t bufCmd[], size_t lenCmd);
00329 
00330 extern int ClientSendPassRsp(BsProxyClient_T *pClient, byte_t bufCmd[],
00331                              byte_t bufRsp[], size_t lenRsp);
00332 
00333 extern int ClientSendFailRsp(BsProxyClient_T *pClient, byte_t bufCmd[],
00334                              const char *sErrFmt, ...);
00335 
00336 extern BsProxyClient_T *ClientNew(Socket_T *pSockClient);
00337 
00338 extern void ClientDelete(BsProxyClient_T *pClient);
00339 
00340 
00341 //-----------------------------------------------------------------------------
00342 // Proxied Device Specific SubDispather Prototypes
00343 //-----------------------------------------------------------------------------
00344 
00345 extern int BPFootDispatch(BsProxyServer_T *pServer, BsProxyClient_T *pClient,
00346                         BsProxyClientDev_T *pDev,
00347                         byte_t bufCmd[], size_t lenCmd);
00348 
00349 extern int BPIMUDispatch(BsProxyServer_T *pServer, BsProxyClient_T *pClient,
00350                         BsProxyClientDev_T *pDev,
00351                         byte_t bufCmd[], size_t lenCmd);
00352 
00353 extern int RCB3Dispatch(BsProxyServer_T *pServer, BsProxyClient_T *pClient,
00354                         BsProxyClientDev_T *pDev,
00355                         byte_t bufCmd[], size_t lenCmd);

Generated on Wed Nov 28 11:01:08 2007 for botsense by doxygen 1.4.6