00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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
00058
00059
00060
00061
00062
00063 #define BSPROXY_CLIENT_DEV_MAX 8
00064 #define BSPROXY_REG_CLIENT_MAX 8
00065
00066
00067
00068
00069 #define BSPROXY_DEVCLASS_NONE 0
00070 #define BSPROXY_DEVCLASS_I2C 1
00071 #define BSPROXY_DEVCLASS_SER 2
00072 #define BSPROXY_DEVCLASS_SWR 3
00073 #define BSPROXY_DEVCLASS_RCB3 4
00074
00075
00076
00077
00078 typedef struct
00079 {
00080 int m_fd;
00081 } BsProxyDevI2C_T;
00082
00083
00084
00085
00086 typedef struct
00087 {
00088 int m_fd;
00089 int m_nBaudRate;
00090 int m_nByteSize;
00091 char m_cParity;
00092 int m_nStopBits;
00093 bool_t m_bRtsCts;
00094 bool_t m_bXonXoff;
00095 } BsProxyDevRS232_T;
00096
00097
00098
00099
00100 typedef struct
00101 {
00102 handle_t m_nHandle;
00103 } BsProxyDevRCB3_T;
00104
00105
00106
00107
00108 typedef struct
00109 {
00110 const char *m_sDevName;
00111 int m_eDevClass;
00112 int m_nDevRefCnt;
00113 pthread_mutex_t m_mutDev;
00114 union
00115 {
00116 BsProxyDevI2C_T i2c;
00117 BsProxyDevRS232_T rs232;
00118 BsProxyDevRCB3_T rcb3;
00119 } m_unDevSpec;
00120 } BsProxyDev_T;
00121
00122
00123
00124
00125 typedef struct
00126 {
00127 int m_eDevType;
00128 BsProxyDev_T *m_pProxDev;
00129 uint_t m_i2cAddr;
00130 } BsProxyClientDev_T;
00131
00132
00133
00134
00135 typedef struct
00136 {
00137 const char *m_sClientName;
00138 Socket_T *m_pClientSock;
00139 BsProxyClientDev_T m_tblClientDev[BSPROXY_CLIENT_DEV_MAX];
00140
00141 } BsProxyClient_T;
00142
00143
00144
00145
00146
00147 typedef struct
00148 {
00149 const char *m_sServerName;
00150 int m_nServerPort;
00151 SockSet_T *m_pServerSockSet;
00152 Socket_T *m_pServerSockListener;
00153 int m_nServerRegClientCount;
00154 BsProxyClient_T *m_pServerRegClient[FD_SETSIZE];
00155 } BsProxyServer_T;
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
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
00178
00179
00180
00181
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
00193
00194
00195
00196
00197
00198
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
00212
00213
00214
00215
00216
00217
00218
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
00233
00234
00235
00236
00237
00238
00239
00240
00241 INLINE_IN_H int BSPACK16(uint_t uVal, byte_t bytes[])
00242 {
00243 bytes[0] = (byte_t)((uVal >> 8) & 0xFF);
00244 bytes[1] = (byte_t)(uVal & 0xFF);
00245 return 2;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254 INLINE_IN_H int BSUNPACK16(byte_t bytes[], uint_t *pVal)
00255 {
00256 *pVal = (uint_t)((((uint_t)bytes[0]) << 8) + (uint_t)bytes[1]);
00257 return 2;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 INLINE_IN_H int BSPACK32(uint_t uVal, byte_t bytes[])
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
00277
00278
00279
00280
00281 INLINE_IN_H int BSUNPACK32(byte_t bytes[], 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
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
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
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);