b-Cap.c
Go to the documentation of this file.
00001 
00011 /*
00012 [NOTES]
00013  This is a sample source code. Copy and modify this code in accordance with a device and a device version. Especially please note timeout and timeout-retry settings.
00014 */
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 
00020 /* OS Switching */
00021 #ifdef WIN32
00022         /* Windows */
00023 #include <winsock2.h>
00024 #pragma comment(lib, "wsock32.lib")
00025 
00026 #else
00027         /* Other */
00028 #include <unistd.h>
00029 
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <arpa/inet.h>
00033 #include <netinet/in.h>
00034 #include <netdb.h>
00035 #include <fcntl.h>
00036 #include <termios.h>
00037 #endif
00038 
00039 
00040 /* b-CAP functions */
00041 #include "b-Cap.h"
00042 
00043 
00044 /* ENDIAN switching */
00045 #if !defined(__LITTLE_ENDIAN__) 
00046 #if !defined(__BIG_ENDIAN__)
00047 
00048 #if __BYTE_ORDER == __LITTLE_ENDIAN
00049 #define __LITTLE_ENDIAN__
00050 #elif __BYTE_ORDER == __BIG_ENDIAN
00051 #define __BIG_ENDIAN__
00052 #endif
00053 
00054 #endif
00055 #endif
00056 
00057 /* DEBUG */
00058 /* #define      DEBUG                                   1       Set to debug mode */
00059 
00060 #ifdef DEBUG
00061 #define DebugPrint( message )   fprintf( stderr, message )
00062 #define DebugPrint2( f, a, b )  fprintf( stderr, f, a, b )
00063 #else
00064 #define DebugPrint( message )   
00065 #define DebugPrint2( f, a, b )
00066 #endif
00067 
00068 
00069 /* Constant values */
00070 
00071 /* length of temporary string buffer */
00072 #define LOCALBUFFER_SZ                  1024
00073 /* length of temporary recieve buffer (must be >= 16bytes) */
00074 #define LOCALRECBUFFER_SZ               1024
00075 
00076 
00077 /* b-CAP packet constant */
00078 #define BCAP_SOH                                0x01            /* size of packet header(SOH) */
00079 #define BCAP_EOT                                0x04            /* size of packet terminater(EOT) */
00080 #define BCAP_SIZE_SOH                   1                       /* size of header(SOH)   */
00081 #define BCAP_SIZE_EOT                   1                       /* size of terminater(EOT)  */
00082 #define BCAP_SIZE_LEN                   4                       /* size of message size */
00083 #define BCAP_SIZE_SERIAL                2                       /* size of serial number */
00084 #define BCAP_SIZE_RESERVE               2                       /* size of reserved */
00085 #define BCAP_SIZE_FUNCID                4                       /* size of FunctionID */
00086 #define BCAP_SIZE_ARGNUM                2                       /* size of Args */
00087 #define BCAP_SIZE_CRC                   2                       /* size of CRC */
00088 
00089                                                                                         /* b-CAP Packet header size */
00090 #define BCAP_SIZE_BASE                  (BCAP_SIZE_SOH + BCAP_SIZE_EOT + \
00091                                                                  BCAP_SIZE_LEN + BCAP_SIZE_SERIAL + \
00092                                                                  BCAP_SIZE_RESERVE + BCAP_SIZE_FUNCID + \
00093                                                                  BCAP_SIZE_ARGNUM)
00094 
00095 
00096 /* b-CAP argument constant */
00097 #define BCAP_SIZE_ARGLEN                4                       /* size of length  */
00098 #define BCAP_SIZE_ARGTYPE               2                       /* size of type */
00099 #define BCAP_SIZE_ARGARRAYS             4                       /* size of arrays */
00100 #define BCAP_SIZE_ARGBASE               (BCAP_SIZE_ARGLEN+BCAP_SIZE_ARGTYPE+BCAP_SIZE_ARGARRAYS)
00101                                                                                         /* b-CAP Arg header size  */
00102 #define BCAP_SIZE_ARGSTRLEN             4                       /* size of string length */
00103 
00104 
00105 #define BCAP_MAX_PACKET_SIZE    0x1000000       /* max packet size (bytes) */
00106 #define BCAP_MAX_ARG_SIZE               0x1000000       /* max arg size (bytes) */
00107 
00108 
00109 /* b-CAP function IDs */
00110 #define BCAP_FUNC_Service_Start 1
00111 #define BCAP_FUNC_Service_Stop  2
00112 #define BCAP_FUNC_Controller_Connect    3
00113 #define BCAP_FUNC_Controller_Disconnect 4
00114 #define BCAP_FUNC_Controller_GetRobot   7
00115 #define BCAP_FUNC_Controller_GetTask    8
00116 #define BCAP_FUNC_Controller_GetVariable        9
00117 #define BCAP_FUNC_Controller_Execute    17
00118 
00119 #define BCAP_FUNC_Robot_GetVariable     62
00120 #define BCAP_FUNC_Robot_Execute 64
00121 #define BCAP_FUNC_Robot_Change  66
00122 #define BCAP_FUNC_Robot_Move    72
00123 #define BCAP_FUNC_Robot_Release 84
00124 
00125 #define BCAP_FUNC_Task_GetVariable      85
00126 #define BCAP_FUNC_Task_Start    88
00127 #define BCAP_FUNC_Task_Stop     89
00128 #define BCAP_FUNC_Task_Release  99
00129 
00130 #define BCAP_FUNC_Variable_PutValue     102
00131 #define BCAP_FUNC_Variable_GetValue     101
00132 #define BCAP_FUNC_Variable_PutValue     102
00133 #define BCAP_FUNC_Variable_Release      111
00134 
00135 /* b-CAP Argment structure */
00140 typedef struct BCAP_ARG {
00141         u_int   lLength;
00142         
00143         u_short iType;
00144         u_int   lArrays;
00145         void    *data;
00146 
00147         struct BCAP_ARG *pNextArg;                      /* pointer to the next argument  */
00148 } BCAP_ARG;
00149 
00150 /* b-CAP Packet structure */
00155 typedef struct BCAP_PACKET {
00156 
00157         u_int   lMsgLength;
00158  
00159         u_short iSerialNo;
00160         u_short iReserved;
00161 
00162         u_int   lFuncID;
00163 
00164         u_short iArgs;
00165 
00166         struct BCAP_ARG *pArg;                          /* pointer to the first argment */
00167 } BCAP_PACKET;
00168 
00169 
00170 /* module function prototypes */ 
00171 static BCAP_HRESULT             Packet_Send(int iSd, BCAP_PACKET *pPacket);
00172 
00173 /* packet class */
00174 static BCAP_PACKET              *Packet_Create(u_int lFuncID);                  
00175 static void                             Packet_Release( BCAP_PACKET *pPacket);          /* Release allocated packet and the arguments */
00176 static BCAP_HRESULT             Packet_Serialize(BCAP_PACKET *pPacket, void *pBinData );                /* struct ---> bin */
00177 static BCAP_HRESULT             Packet_Deserialize(void *pBinData, BCAP_PACKET *pPacket );              /* bin ---> struct  */
00178 static BCAP_HRESULT             Packet_AddArg( BCAP_PACKET *pPacket, BCAP_ARG *pNewArg);
00179 static BCAP_HRESULT             Packet_GetResult( BCAP_PACKET *pRecPacket, void *pResult);
00180 
00181 /* argument class */
00182 static BCAP_ARG                 *Arg_Create( u_short iType, u_int lArrays, u_int lLength, void *data);
00183 static void                             Arg_Release( BCAP_ARG *pArg);   /* free the allocated argument */
00184 /* static BCAP_HRESULT  Arg_Serialize(void *pBinData);  */                                                                      /* bin <--- struct  */
00185 /* static BCAP_HRESULT  Arg_Deserialize(BCAP_ARG *pArg, void *pBinData);        */                              /* struct <--- bin  */
00186 
00187 /* module socket utility functions */
00188 static BCAP_HRESULT             bCapSendAndRec(int iSockFd, BCAP_PACKET *pSndPacket, BCAP_PACKET *pRecPacket);
00189 static BCAP_HRESULT             sendBinary(int iSockFd, u_char *pBuff, u_int lSize);
00190 static u_char                   *receivePacket(int iSockFd, int* retryp);
00191 
00192 /* module utility functions */
00193 static u_int                    sizeOfVariant(BCAP_VARIANT vntValue);
00194 static u_int                    copyVariantFromArg(BCAP_VARIANT *pVntDst, BCAP_ARG *pArg);
00195 /* static u_int                 copyArgValue(void *pDst, BCAP_ARG *pArg); */
00196 
00197 static u_int                    sizeOfVarType(u_short iType);
00198 static u_int                    copyValue(void *pDst, void *pVntValue, u_int lLength);
00199 static u_int                    copyToBSTR(void *pbDstPtr, void *pbSrcPtr);
00200 static u_int                    copyFromBSTR(void *pDstAsciiPtr, void *pSrcBstrPtr);
00201 static void                             *bMalloc(size_t size);
00202 static void                             bFree(void *pPtr);
00203 
00204 /* module variables */
00205 static u_short                  m_iSerialNo = 1;                                                /* packet serial number: cyclic from 0x0001 to 0xFFFF */
00206 
00207 #if BCAP_CONNECTION_UDP
00208 static  struct sockaddr_in      m_sockServerAddr;                                               /* server's socket address */ 
00209 #endif
00210 
00211 /*--------------------------------------------------------------------
00212                         b-Cap library public functions          
00213  --------------------------------------------------------------------*/
00214 
00215 
00224 BCAP_HRESULT    bCap_Open(const char *pIPStr, int iPort, int *iSockFd) { 
00225 
00226         BCAP_HRESULT hr = BCAP_E_FAIL;
00227 
00228 #ifdef BCAP_CONNECTION_COM
00229 
00230 #ifdef WIN32
00231         BOOL bRet;
00232         char szFileName[16];
00233         DCB dcb;
00234         HANDLE hSerial;
00235 
00236         COMMTIMEOUTS stTimeOut;
00237 
00238         sprintf(szFileName, "//./COM%d", iPort);
00239         hSerial = CreateFile(szFileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00240         if (GetCommState(hSerial, &dcb)) {
00241                 dcb.BaudRate = SERIAL_BAUDRATE;
00242                 dcb.ByteSize = 8;
00243                 dcb.Parity = NOPARITY;
00244                 dcb.StopBits = ONESTOPBIT;
00245                 dcb.fOutX = FALSE;
00246                 dcb.fInX = FALSE;
00247                 dcb.fOutxCtsFlow = FALSE;
00248                 dcb.fRtsControl = RTS_CONTROL_ENABLE;
00249                 dcb.fOutxDsrFlow = FALSE;
00250                 dcb.fDtrControl = DTR_CONTROL_ENABLE;
00251                 dcb.fDsrSensitivity = FALSE;
00252 
00253                 bRet = SetCommState(hSerial, &dcb);
00254 
00255                 /* timeout setting */
00256                 stTimeOut.ReadIntervalTimeout = MAXDWORD ;
00257                 stTimeOut.ReadTotalTimeoutMultiplier = MAXDWORD ;
00258                 stTimeOut.ReadTotalTimeoutConstant = 1000;
00259                 stTimeOut.WriteTotalTimeoutMultiplier = MAXDWORD;
00260                 stTimeOut.WriteTotalTimeoutConstant = 1000;
00261                 bRet = SetCommTimeouts( hSerial, &stTimeOut );
00262 
00263                 *iSockFd = (int)hSerial;
00264                 hr = BCAP_S_OK;
00265         }
00266 #else
00267         struct termios tio;
00268         char dev[16];
00269 
00270         sprintf(dev, "/dev/ttyS%d", iPort);
00271         *iSockFd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)
00272 
00273         bzero(&tio, sizeof(tio));
00274         tio.c_cflag = SERIAL_BAUDRATE | CS8 | CLOCAL | CREAD;
00275         tio.c_iflag = IGNPAR;
00276         tio.c_oflag = 0;
00277         tio.c_lflag = 0;
00278         tio.c_cc[VTIME] = 0;
00279         tio.c_cc[VMIN] = 1;
00280 
00281         tcflush(*iSockFd, TCIFLUSH);
00282         tcsetattr(*iSockFd, TCSANOW, &tio);
00283         fcntl(*iSockFd, F_SETFL, FNDELAY);
00284 #endif
00285 
00286 #else 
00287         struct sockaddr_in      serverAddr;                     /* server's socket address */ 
00288         int                                     sockAddrSize;           /* size of socket address structure */ 
00289 
00290 #ifdef WIN32
00291         WSADATA wsaData;
00292         WSAStartup(MAKEWORD(2,0), &wsaData);
00293 #endif
00294 
00295         sockAddrSize = sizeof( struct sockaddr_in);
00296         memset( (char *)&serverAddr, 0, sockAddrSize);
00297         serverAddr.sin_family = AF_INET;
00298 
00299         serverAddr.sin_port = htons((short)iPort);                                      /* SERVER_PORT_NUM */
00300 
00301 #ifdef WIN32
00302         serverAddr.sin_addr.S_un.S_addr = inet_addr(pIPStr);    /* SERVER_IP_ADDRESS */
00303 #else
00304         serverAddr.sin_addr.s_addr = inet_addr(pIPStr); /* SERVER_IP_ADDRESS */
00305 /*      serverAddr.sin_len = (u_char)sockAddrSize;
00306         inet_aton(pIPStr, &(serverAddr.sin_addr));*/
00307 #endif
00308 
00309 #if BCAP_CONNECTION_UDP
00310         memcpy( (char *)&m_sockServerAddr, (char *)&serverAddr, sockAddrSize);
00311         /* socket  */
00312         if( (*iSockFd = socket( AF_INET, SOCK_DGRAM, 0)) == -1) {
00313                 perror( "Fail.. Function:socket(UDP) in bCap_Open()");
00314                 hr = BCAP_E_UNEXPECTED;
00315         }
00316         else{
00317                 hr = BCAP_S_OK;
00318                 struct timeval tv;
00319                 tv.tv_sec = 0;
00320                 tv.tv_usec = 20000; /* 20msec */
00321                 if (setsockopt(*iSockFd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
00322                   perror("Error");
00323                 }
00324         }
00325 #else /* TCP */
00326         /* socket  */
00327         if( (*iSockFd = socket( AF_INET, SOCK_STREAM, 0)) == -1) {
00328                 perror( "Fail.. Function:socket(TCP) in bCap_Open()");
00329                 hr = BCAP_E_UNEXPECTED;
00330         }
00331         else{           
00332                 /* connect to server */
00333                 int iResult;
00334                 iResult = connect(*iSockFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
00335 
00336                 if (iResult == 0){
00337                         hr = BCAP_S_OK;
00338                 }
00339                 else{
00340                         hr = BCAP_E_UNEXPECTED; 
00341                 }
00342         }
00343 #endif
00344 #endif
00345 
00346         return hr;
00347 }
00348 
00349 
00358 BCAP_HRESULT    bCap_Close(int iSockFd){
00359 
00360         BCAP_HRESULT hr = BCAP_E_FAIL;
00361 
00362 #ifdef BCAP_CONNECTION_COM
00363 #ifdef WIN32
00364         CloseHandle((HANDLE)iSockFd);
00365 #else
00366         close(iSockFd);
00367 #endif
00368         hr = BCAP_S_OK;
00369 #else
00370 #ifdef WIN32
00371         shutdown(iSockFd, SD_BOTH);
00372         closesocket(iSockFd);
00373         if (WSACleanup() == 0){
00374                 hr = BCAP_S_OK;
00375         }
00376 #else
00377         shutdown(iSockFd, SHUT_RDWR);
00378         close(iSockFd);
00379         hr = BCAP_S_OK;
00380 #endif
00381 #endif
00382 
00383         return hr;
00384 }
00385 
00386 
00395 BCAP_HRESULT bCap_ServiceStart(int iSockFd){
00396         BCAP_PACKET             *pPacket;
00397         BCAP_PACKET             *pRecPacket;
00398 
00399         BCAP_HRESULT hr = BCAP_E_FAIL;
00400 
00401         pPacket = Packet_Create(BCAP_FUNC_Service_Start);               /* Service_Start */
00402         if (pPacket != NULL){
00403 
00404                 {
00405                         pRecPacket = Packet_Create(BCAP_S_OK);                                  /* storing a new packet from RC    */
00406                         if (pRecPacket != NULL){
00407                                 hr = bCapSendAndRec(iSockFd, pPacket, pRecPacket);
00408 
00409                         }
00410                         Packet_Release(pRecPacket);
00411                 }
00412                 Packet_Release(pPacket);
00413         }
00414 
00415         return hr;
00416 
00417 }
00418 
00427 BCAP_HRESULT bCap_ServiceStop(int iSockFd){
00428         BCAP_PACKET             *pPacket;
00429         BCAP_PACKET             *pRecPacket;
00430 
00431         BCAP_HRESULT hr = BCAP_E_FAIL;
00432 
00433         pPacket = Packet_Create(BCAP_FUNC_Service_Stop);                /* alloc new packet:Service_Stop */
00434         if (pPacket != NULL){
00435 
00436                 {
00437                         pRecPacket = Packet_Create(BCAP_S_OK);                                  /*  alloc new packet storing a packet from RC    */
00438                         if (pRecPacket != NULL){
00439                                 hr = bCapSendAndRec(iSockFd, pPacket, pRecPacket);
00440 
00441                         }
00442                         Packet_Release(pRecPacket);
00443                 }
00444                 Packet_Release(pPacket);
00445         }
00446 
00447         return hr;
00448 
00449 }
00450 
00451 
00452 
00466 BCAP_HRESULT bCap_ControllerConnect(    int iSockFd,
00467                                                                                 char *pStrCtrlname, 
00468                                                                                 char *pStrProvName, 
00469                                                                                 char *pStrPcName, 
00470                                                                                 char *pStrOption, 
00471                                                                                 u_int *plhController){
00472         BCAP_PACKET             *pPacket;
00473         BCAP_PACKET             *pRecPacket;
00474         BCAP_ARG                *pArg;
00475 
00476         BCAP_HRESULT hr = BCAP_E_FAIL;
00477 
00478         pPacket = Packet_Create(BCAP_FUNC_Controller_Connect);          /* Controller_Connect */
00479         if (pPacket != NULL){
00480 
00481                 u_char buff[LOCALBUFFER_SZ];
00482                 u_int lLen;
00483 
00484                 {
00485                         lLen = copyToBSTR(buff,pStrCtrlname);
00486                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00487                         if (pArg != NULL){
00488                                 Packet_AddArg(pPacket, pArg);
00489                         }
00490                 }
00491                 {
00492                         lLen = copyToBSTR(buff,pStrProvName);
00493                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00494                         if (pArg != NULL){
00495                                 Packet_AddArg(pPacket, pArg);
00496                         }
00497                 }
00498                 {
00499                         lLen = copyToBSTR(buff,pStrPcName);
00500                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00501                         if (pArg != NULL){
00502                                 Packet_AddArg(pPacket, pArg);
00503                         }
00504                 }
00505                 {
00506                         lLen = copyToBSTR(buff,pStrOption);
00507                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00508                         if (pArg != NULL){
00509                                 Packet_AddArg(pPacket, pArg);
00510                         }
00511                 }
00512 
00513                 {
00514                         pRecPacket = Packet_Create(BCAP_S_OK);
00515                         if (pRecPacket != NULL){
00516                                 hr = bCapSendAndRec(iSockFd, pPacket, pRecPacket);
00517                                 if (SUCCEEDED(hr)){
00518                                   copyValue(plhController, pRecPacket->pArg->data, 4);
00519                                 }
00520 
00521                         }
00522                         Packet_Release(pRecPacket);
00523                 }
00524                 Packet_Release(pPacket);
00525         }
00526 
00527         return hr;
00528 
00529 }
00530 
00531 
00541 BCAP_HRESULT bCap_ControllerDisconnect(int iSockFd, u_int lhController){
00542         BCAP_PACKET             *pSndPacket;
00543         BCAP_PACKET             *pRecPacket;
00544         BCAP_ARG                *pArg;
00545 
00546         BCAP_HRESULT hr = BCAP_E_FAIL;
00547 
00548         pSndPacket = Packet_Create(BCAP_FUNC_Controller_Disconnect);            /* Controller_Disconnect */
00549         if (pSndPacket != NULL){
00550 
00551                 {
00552                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00553                         if (pArg != NULL){
00554                                 Packet_AddArg(pSndPacket, pArg);
00555                         }
00556                 }
00557 
00558                 {
00559                         pRecPacket = Packet_Create(BCAP_S_OK);                                  /* storing a new packet from RC    */
00560                         if (pRecPacket != NULL){
00561                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00562 
00563                         }
00564                         Packet_Release(pRecPacket);
00565                 }
00566                 Packet_Release(pSndPacket);
00567         }
00568 
00569         return hr;
00570 
00571 }
00572 
00573 
00586 BCAP_HRESULT bCap_ControllerGetRobot(int iSockFd, u_int lhController, char *pStrRobotName, char *pStrOption, u_int *plhRobot){
00587         BCAP_PACKET             *pSndPacket;
00588         BCAP_PACKET             *pRecPacket;
00589         BCAP_ARG                *pArg;
00590 
00591         u_char buff[LOCALBUFFER_SZ];
00592         u_int lLen;
00593 
00594         BCAP_HRESULT hr = BCAP_E_FAIL;
00595 
00596         pSndPacket = Packet_Create(BCAP_FUNC_Controller_GetRobot);              /* BCAP_FUNC_Controller_GetRobot */
00597         if (pSndPacket != NULL){
00598 
00599                 {
00600                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00601                         if (pArg != NULL){
00602                                 Packet_AddArg(pSndPacket, pArg);
00603                         }
00604                 }
00605                 {
00606                         lLen = copyToBSTR(buff,pStrRobotName);
00607                         pArg = Arg_Create( VT_BSTR, 1, lLen, buff);
00608                         if (pArg != NULL){
00609                                 Packet_AddArg(pSndPacket, pArg);
00610                         }
00611                 }
00612                 {
00613                         lLen = copyToBSTR(buff,pStrOption);
00614                         pArg = Arg_Create( VT_BSTR, 1, lLen, buff);
00615                         if (pArg != NULL){
00616                                 Packet_AddArg(pSndPacket, pArg);
00617                         }
00618                 }
00619                 {
00620                         pRecPacket = Packet_Create(BCAP_S_OK);
00621                         if (pRecPacket != NULL){
00622                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00623                                 if SUCCEEDED(hr) {
00624                                         copyValue(plhRobot, pRecPacket->pArg->data, 4);
00625                                   }
00626                                 else {
00627                                         /* NO Argument */
00628                                         hr = BCAP_E_FAIL;
00629                                 }
00630 
00631                         }
00632                         Packet_Release(pRecPacket);
00633                 }
00634                 Packet_Release(pSndPacket);
00635         }
00636 
00637         return hr;
00638 }
00639 
00640 
00654 BCAP_HRESULT bCap_ControllerGetVariable(int iSockFd, u_int lhController, char *pVarName, char *pstrOption, u_int *plhVar){
00655         BCAP_PACKET             *pSndPacket;
00656         BCAP_PACKET             *pRecPacket;
00657         BCAP_ARG                *pArg;
00658 
00659         BCAP_HRESULT hr = BCAP_E_FAIL;
00660 
00661         pSndPacket = Packet_Create(BCAP_FUNC_Controller_GetVariable);           /* BCAP_FUNC_Controller_GetVariable */
00662         if (pSndPacket != NULL){
00663 
00664                 u_char buff[LOCALBUFFER_SZ];
00665                 u_int lLen;
00666 
00667                 {
00668                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00669                         if (pArg != NULL){
00670                                 Packet_AddArg(pSndPacket, pArg);
00671                         }
00672                 }
00673 
00674                 {
00675                         lLen = copyToBSTR(buff,pVarName);
00676                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00677                         if (pArg != NULL){
00678                                 Packet_AddArg(pSndPacket, pArg);
00679                         }
00680                 }
00681 
00682                 {
00683                         lLen = copyToBSTR(buff,pstrOption);
00684                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00685                         if (pArg != NULL){
00686                                 Packet_AddArg(pSndPacket, pArg);
00687                         }
00688                 }
00689 
00690                 {
00691                         pRecPacket = Packet_Create(BCAP_S_OK);
00692                         if (pRecPacket != NULL){
00693                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00694                                 if SUCCEEDED(hr){
00695                                     if (pRecPacket->iArgs >= 1){
00696                                       copyValue(plhVar, pRecPacket->pArg->data, 4);
00697                                     }
00698                                     else{
00699                                       /* NO Argument */
00700                                       hr = BCAP_E_FAIL;
00701                                     }
00702                                   }
00703                                 
00704                         }
00705                         Packet_Release(pRecPacket);
00706                 }
00707                 Packet_Release(pSndPacket);
00708         }
00709 
00710         return hr;
00711 }
00712 
00725 BCAP_HRESULT bCap_ControllerGetTask(int iSockFd, u_int lhController, char *pTskName, char *pstrOption, u_int *plhVar){
00726         BCAP_PACKET             *pSndPacket;
00727         BCAP_PACKET             *pRecPacket;
00728         BCAP_ARG                *pArg;
00729 
00730         BCAP_HRESULT hr = BCAP_E_FAIL;
00731 
00732         pSndPacket = Packet_Create(BCAP_FUNC_Controller_GetTask);               /* BCAP_FUNC_Controller_GetTask */
00733         if (pSndPacket != NULL){
00734 
00735                 u_char buff[LOCALBUFFER_SZ];
00736                 u_int lLen;
00737 
00738                 {
00739                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00740                         if (pArg != NULL){
00741                                 Packet_AddArg(pSndPacket, pArg);
00742                         }
00743                 }
00744 
00745                 {
00746                         lLen = copyToBSTR(buff,pTskName);
00747                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00748                         if (pArg != NULL){
00749                                 Packet_AddArg(pSndPacket, pArg);
00750                         }
00751                 }
00752 
00753                 {
00754                         lLen = copyToBSTR(buff, pstrOption);
00755                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00756                         if (pArg != NULL){
00757                                 Packet_AddArg(pSndPacket, pArg);
00758                         }
00759                 }
00760 
00761                 {
00762                         pRecPacket = Packet_Create(BCAP_S_OK);
00763                         if (pRecPacket != NULL){
00764                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00765                                 if SUCCEEDED(hr){
00766                                     if (pRecPacket->iArgs >= 1){
00767                                       copyValue(plhVar, pRecPacket->pArg->data, 4);
00768                                     }
00769                                     else{
00770                                       /* NO Argument */
00771                                       hr = BCAP_E_FAIL;
00772                                     }
00773                                   }
00774                                     
00775                         }
00776                         Packet_Release(pRecPacket);
00777                 }
00778                 Packet_Release(pSndPacket);
00779         }
00780 
00781         return hr;
00782 }
00783 
00784 
00799 BCAP_HRESULT bCap_ControllerExecute(int iSockFd, u_int lhController, char *pStrCommand, char *pStrOption, void *pResult){
00800         BCAP_PACKET             *pSndPacket;
00801         BCAP_PACKET             *pRecPacket;
00802         BCAP_ARG                *pArg;
00803 
00804         BCAP_HRESULT hr = BCAP_E_FAIL;
00805 
00806         pSndPacket = Packet_Create(BCAP_FUNC_Controller_Execute);               /* BCAP_FUNC_Controller_Execute */
00807         if (pSndPacket != NULL){
00808 
00809                 u_char buff[LOCALBUFFER_SZ];
00810                 u_int lLen;
00811 
00812                 {
00813                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00814                         if (pArg != NULL){
00815                                 Packet_AddArg(pSndPacket, pArg);
00816                         }
00817                 }
00818 
00819                 {
00820                         lLen = copyToBSTR(buff,pStrCommand);
00821                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00822                         if (pArg != NULL){
00823                                 Packet_AddArg(pSndPacket, pArg);
00824                         }
00825                 }
00826 
00827                 {
00828                         lLen = copyToBSTR(buff, pStrOption);
00829                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00830                         if (pArg != NULL){
00831                                 Packet_AddArg(pSndPacket, pArg);
00832                         }
00833                 }
00834 
00835                 {
00836                         pRecPacket = Packet_Create(BCAP_S_OK);
00837                         if (pRecPacket != NULL){
00838                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00839                                 if SUCCEEDED(hr){
00840                                     hr = Packet_GetResult(pRecPacket, pResult);
00841                                   }
00842 
00843                         }
00844                         Packet_Release(pRecPacket);
00845                 }
00846                 Packet_Release(pSndPacket);
00847         }
00848 
00849         return hr;
00850 }
00851 
00852 
00866 BCAP_HRESULT    bCap_ControllerExecute2(int iSockFd, u_int lhController, char *pStrCommand, BCAP_VARIANT *pVntOption, BCAP_VARIANT *pVntResult){
00867         BCAP_PACKET             *pSndPacket;
00868         BCAP_PACKET             *pRecPacket;
00869         BCAP_ARG                *pArg;
00870 
00871         BCAP_HRESULT hr = BCAP_E_FAIL;
00872 
00873         pSndPacket = Packet_Create(BCAP_FUNC_Controller_Execute);               /* BCAP_FUNC_Controller_Execute */
00874         if (pSndPacket != NULL){
00875 
00876                 u_char buff[LOCALBUFFER_SZ];
00877                 u_int lLen;
00878 
00879                 {
00880                         pArg = Arg_Create( VT_I4, 1, 4, &lhController);
00881                         if (pArg != NULL){
00882                                 Packet_AddArg(pSndPacket, pArg);
00883                         }
00884                 }
00885 
00886                 {
00887                         lLen = copyToBSTR(buff,pStrCommand);
00888                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
00889                         if (pArg != NULL){
00890                                 Packet_AddArg(pSndPacket, pArg);
00891                         }
00892                 }
00893 
00894                 if (pVntOption != NULL){
00895                         u_int lSize;
00896                         lSize = sizeOfVariant(*pVntOption);
00897                         //if (lSize >= 0){ //always true
00898                                 pArg = Arg_Create( pVntOption->Type, pVntOption->Arrays, lSize, &(pVntOption->Value));
00899                                 if (pArg != NULL){
00900                                         Packet_AddArg(pSndPacket, pArg);
00901                                 }
00902                                 //}
00903                 }
00904 
00905                 {
00906                         pRecPacket = Packet_Create(BCAP_S_OK);
00907                         if (pRecPacket != NULL){
00908                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00909                                 if SUCCEEDED(hr){
00910                                     if (pRecPacket->iArgs >= 1){
00911                                       // copyValue(plResult, pRecPacket->pArg->data, 4);
00912                                       copyVariantFromArg(pVntResult, pRecPacket->pArg);
00913                                       // copyArgValue(plResult, pRecPacket->pArg);
00914                                     }
00915                                   }
00916 
00917                         }
00918                         Packet_Release(pRecPacket);
00919                 }
00920                 Packet_Release(pSndPacket);
00921         }
00922 
00923         return hr;
00924 }
00925 
00935 BCAP_HRESULT bCap_RobotRelease(int iSockFd, u_int lhRobot){
00936         BCAP_PACKET             *pSndPacket;
00937         BCAP_PACKET             *pRecPacket;
00938         BCAP_ARG                *pArg;
00939 
00940 
00941         BCAP_HRESULT hr = BCAP_E_FAIL;
00942 
00943         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Release);            /* FuncID:RobotRelease */
00944         if (pSndPacket != NULL){
00945 
00946                 {
00947                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
00948                         if (pArg != NULL){
00949                                 Packet_AddArg(pSndPacket, pArg);                                /* Add 1st argument */
00950                         }
00951                 }
00952 
00953                 {       
00954                         pRecPacket = Packet_Create(BCAP_S_OK);                          /* Alloc for storing received packet */
00955                         if (pRecPacket != NULL){
00956                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
00957 
00958                         }
00959                         Packet_Release(pRecPacket);                                                     /* Release recieved packet */
00960                 }
00961                 Packet_Release(pSndPacket);
00962         }
00963 
00964         return hr;
00965 }
00966 
00967 
00968 
00981 BCAP_HRESULT bCap_RobotGetVariable(int iSockFd, u_int lhRobot, char *pVarName, char *pstrOption, u_int *plhVar){
00982         BCAP_PACKET             *pSndPacket;
00983         BCAP_PACKET             *pRecPacket;
00984         BCAP_ARG                *pArg;
00985 
00986         BCAP_HRESULT hr = BCAP_E_FAIL;
00987 
00988         pSndPacket = Packet_Create(BCAP_FUNC_Robot_GetVariable);                /* BCAP_FUNC_Robot_GetVariable */
00989         if (pSndPacket != NULL){
00990 
00991                 u_char buff[LOCALBUFFER_SZ];
00992                 u_int lLen;
00993 
00994                 {
00995                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
00996                         if (pArg != NULL){
00997                                 Packet_AddArg(pSndPacket, pArg);
00998                         }
00999                 }
01000 
01001                 {
01002                         lLen = copyToBSTR(buff,pVarName);
01003                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01004                         if (pArg != NULL){
01005                                 Packet_AddArg(pSndPacket, pArg);
01006                         }
01007                 }
01008 
01009                 {
01010                         lLen = copyToBSTR(buff, pstrOption);
01011                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01012                         if (pArg != NULL){
01013                                 Packet_AddArg(pSndPacket, pArg);
01014                         }
01015                 }
01016 
01017                 {
01018                         pRecPacket = Packet_Create(BCAP_S_OK);
01019                         if (pRecPacket != NULL){
01020                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01021                                 if SUCCEEDED(hr){
01022                                     if (pRecPacket->iArgs >= 1){
01023                                       copyValue(plhVar, pRecPacket->pArg->data, 4);
01024                                     }
01025                                     else{
01026                                       
01027                                     }
01028                                   }
01029                         }
01030                         Packet_Release(pRecPacket);
01031                 }
01032                 Packet_Release(pSndPacket);
01033         }
01034 
01035         return hr;
01036 }
01037 
01038 
01053 BCAP_HRESULT bCap_RobotExecute(int iSockFd, u_int lhRobot, char *pStrCommand, char *pStrOption, void *pResult){
01054         BCAP_PACKET             *pSndPacket;
01055         BCAP_PACKET             *pRecPacket;
01056         BCAP_ARG                *pArg;
01057 
01058         BCAP_HRESULT hr = BCAP_E_FAIL;
01059 
01060         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Execute);            /* BCAP_FUNC_Robot_Execute */
01061         if (pSndPacket != NULL){
01062 
01063                 u_char buff[LOCALBUFFER_SZ];
01064                 u_int lLen;
01065 
01066                 {
01067                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
01068                         if (pArg != NULL){
01069                                 Packet_AddArg(pSndPacket, pArg);
01070                         }
01071                 }
01072 
01073                 {
01074                         lLen = copyToBSTR(buff,pStrCommand);
01075                         pArg = Arg_Create( VT_BSTR, 1, lLen, buff);
01076                         if (pArg != NULL){
01077                                 Packet_AddArg(pSndPacket, pArg);
01078                         }
01079                 }
01080 
01081                 {
01082                         lLen = copyToBSTR(buff, pStrOption);
01083                         pArg = Arg_Create( VT_BSTR, 1, lLen, buff);
01084                         if (pArg != NULL){
01085                                 Packet_AddArg(pSndPacket, pArg);
01086                         }
01087                 }
01088 
01089                 {
01090                         pRecPacket = Packet_Create(BCAP_S_OK);
01091                         if (pRecPacket != NULL){
01092                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01093                                 if SUCCEEDED(hr){
01094                                     hr = Packet_GetResult(pRecPacket, pResult);
01095                                   }
01096 
01097                         }
01098                         Packet_Release(pRecPacket);
01099                 }
01100                 Packet_Release(pSndPacket);
01101         }
01102 
01103         return hr;
01104 }
01105 
01106 
01120 BCAP_HRESULT    bCap_RobotExecute2(int iSockFd, u_int lhRobot, char *pStrCommand, BCAP_VARIANT *pVntOption, BCAP_VARIANT *pVntResult){
01121         BCAP_PACKET             *pSndPacket;
01122         BCAP_PACKET             *pRecPacket;
01123         BCAP_ARG                *pArg;
01124 
01125         BCAP_HRESULT hr = BCAP_E_FAIL;
01126 
01127         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Execute);            /* BCAP_FUNC_Controller_Execute */
01128         if (pSndPacket != NULL){
01129 
01130                 u_char buff[LOCALBUFFER_SZ];
01131                 u_int lLen;
01132 
01133                 {
01134                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
01135                         if (pArg != NULL){
01136                                 Packet_AddArg(pSndPacket, pArg);
01137                         }
01138                 }
01139 
01140                 {
01141                         lLen = copyToBSTR(buff,pStrCommand);
01142                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01143                         if (pArg != NULL){
01144                                 Packet_AddArg(pSndPacket, pArg);
01145                         }
01146                 }
01147 
01148                 if (pVntOption != NULL){
01149                         u_int lSize;
01150                         lSize = sizeOfVariant(*pVntOption);
01151                         //if (lSize >= 0){
01152                                 pArg = Arg_Create( pVntOption->Type, pVntOption->Arrays, lSize, &(pVntOption->Value));
01153                                 if (pArg != NULL){
01154                                         Packet_AddArg(pSndPacket, pArg);
01155                                 }
01156                                 //}
01157                 }
01158 
01159                 {
01160                         pRecPacket = Packet_Create(BCAP_S_OK);
01161                         if (pRecPacket != NULL){
01162                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01163                                 if SUCCEEDED(hr){
01164                                     if (pRecPacket->iArgs >= 1){
01165                                       // copyValue(plResult, pRecPacket->pArg->data, 4);
01166                                       copyVariantFromArg(pVntResult, pRecPacket->pArg);
01167                                       // copyArgValue(plResult, pRecPacket->pArg);
01168                                     }
01169                                     
01170                                   }
01171 
01172                         }
01173                         Packet_Release(pRecPacket);
01174                 }
01175                 Packet_Release(pSndPacket);
01176         }
01177 
01178         return hr;
01179 }
01180 
01193 BCAP_HRESULT bCap_RobotChange(int iSockFd, u_int lhRobot, char *pStrCommand){
01194         BCAP_PACKET             *pSndPacket;
01195         BCAP_PACKET             *pRecPacket;
01196         BCAP_ARG                *pArg;
01197 
01198         BCAP_HRESULT hr = BCAP_E_FAIL;
01199 
01200         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Change);             /* BCAP_FUNC_Robot_Change */
01201         if (pSndPacket != NULL){
01202 
01203                 u_char buff[LOCALBUFFER_SZ];
01204                 u_int lLen;
01205 
01206                 {
01207                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
01208                         if (pArg != NULL){
01209                                 Packet_AddArg(pSndPacket, pArg);
01210                         }
01211                 }
01212 
01213                 {
01214                         lLen = copyToBSTR(buff,pStrCommand);
01215                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01216                         if (pArg != NULL){
01217                                 Packet_AddArg(pSndPacket, pArg);
01218                         }
01219                 }
01220 
01221                 {
01222                         pRecPacket = Packet_Create(BCAP_S_OK);
01223                         if (pRecPacket != NULL){
01224                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01225                                 if SUCCEEDED(hr){
01226                                     ;
01227                                   }
01228 
01229                         }
01230                         Packet_Release(pRecPacket);
01231                 }
01232                 Packet_Release(pSndPacket);
01233         }
01234 
01235         return hr;
01236 }
01237 
01238 
01251 BCAP_HRESULT bCap_RobotMove(int iSockFd, u_int lhRobot, long lComp, char *pStrPose, char *pStrOption){
01252         BCAP_PACKET             *pSndPacket;
01253         BCAP_PACKET             *pRecPacket;
01254         BCAP_ARG                *pArg;
01255 
01256         BCAP_HRESULT hr = BCAP_E_FAIL;
01257 
01258         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Move);               /* BCAP_FUNC_Robot_Move */
01259         if (pSndPacket != NULL){
01260 
01261                 u_char buff[LOCALBUFFER_SZ];
01262                 u_int lLen;
01263 
01264                 {
01265                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);              /* Arg1 Handle of the robot */
01266                         if (pArg != NULL){
01267                                 Packet_AddArg(pSndPacket, pArg);
01268                         }
01269                 }
01270         
01271                 {
01272                         pArg = Arg_Create( VT_I4, 1, 4, &lComp);                /* Arg2 Completion param */
01273                         if (pArg != NULL){
01274                                 Packet_AddArg(pSndPacket, pArg);
01275                         }
01276                 }
01277 
01278                 {
01279                         lLen = copyToBSTR(buff,pStrPose);                               /* Arg3 Pose param */           
01280                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01281                         if (pArg != NULL){
01282                                 Packet_AddArg(pSndPacket, pArg);
01283                         }
01284                 }
01285 
01286                 {
01287                         lLen = copyToBSTR(buff,pStrOption);                             /* Arg4 option param */
01288                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01289                         if (pArg != NULL){
01290                                 Packet_AddArg(pSndPacket, pArg);
01291                         }
01292                 }
01293 
01294 
01295                 {       
01296                         pRecPacket = Packet_Create(BCAP_S_OK);
01297                         if (pRecPacket != NULL){
01298                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01299                                 if SUCCEEDED(hr){
01300                                     ;
01301                                   }
01302 
01303                         }
01304                         Packet_Release(pRecPacket);
01305                 }
01306                 Packet_Release(pSndPacket);
01307         }
01308 
01309         return hr;
01310 }
01311 
01326 BCAP_HRESULT bCap_RobotExecuteSlaveMove(int iSockFd, u_int lhRobot, char *pStrCommand, float *pfOption, void *pResult){
01327         BCAP_PACKET             *pSndPacket;
01328         BCAP_PACKET             *pRecPacket;
01329         BCAP_ARG                *pArg;
01330 
01331         BCAP_HRESULT hr = BCAP_E_FAIL;
01332 
01333         pSndPacket = Packet_Create(BCAP_FUNC_Robot_Execute);            /* BCAP_FUNC_Robot_Execute */
01334         if (pSndPacket != NULL){
01335 
01336                 u_char buff[LOCALBUFFER_SZ];
01337                 u_int lLen;
01338 
01339                 {
01340                         pArg = Arg_Create( VT_I4, 1, 4, &lhRobot);
01341                         if (pArg != NULL){
01342                                 Packet_AddArg(pSndPacket, pArg);
01343                         }
01344                 }
01345 
01346                 {
01347                         lLen = copyToBSTR(buff,pStrCommand);
01348                         pArg = Arg_Create( VT_BSTR, 1, lLen, buff);
01349                         if (pArg != NULL){
01350                                 Packet_AddArg(pSndPacket, pArg);
01351                         }
01352                 }
01353 
01354                 {       /* R4”z—ñ‚ňʒu‚ðŽw’è */
01355                         pArg = Arg_Create( VT_R4 | VT_ARRAY, 8, sizeof(float) * 8, pfOption);   /* 8Ž²‚Ô‚ñ */
01356                         if (pArg != NULL){
01357                                 Packet_AddArg(pSndPacket, pArg);
01358                         }
01359                 }
01360 
01361                 {
01362                         pRecPacket = Packet_Create(BCAP_S_OK);
01363                         if (pRecPacket != NULL){
01364                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01365                                 if SUCCEEDED(hr){
01366                                     hr = Packet_GetResult(pRecPacket, pResult);
01367                                   }
01368 
01369                         }
01370                         Packet_Release(pRecPacket);
01371                 }
01372                 Packet_Release(pSndPacket);
01373         }
01374 
01375         return hr;
01376 }
01377 
01378 
01388 BCAP_HRESULT bCap_TaskRelease(int iSockFd, u_int lhTask){
01389         BCAP_PACKET             *pSndPacket;
01390         BCAP_PACKET             *pRecPacket;
01391         BCAP_ARG                *pArg;
01392 
01393 
01394         BCAP_HRESULT hr = BCAP_E_FAIL;
01395 
01396         pSndPacket = Packet_Create(BCAP_FUNC_Task_Release);             /* BCAP_FUNC_Task_Release */
01397         if (pSndPacket != NULL){
01398 
01399                 {
01400                         pArg = Arg_Create( VT_I4, 1, 4, &lhTask);
01401                         if (pArg != NULL){
01402                                 Packet_AddArg(pSndPacket, pArg);                                /* Arg1 lhTask */
01403                         }
01404                 }
01405 
01406                 {       
01407                         pRecPacket = Packet_Create(BCAP_S_OK);                          /* Alloc for storing recieved packet */
01408                         if (pRecPacket != NULL){
01409                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01410 
01411                         }
01412                         Packet_Release(pRecPacket);                                                     /* Release recieved packet */
01413                 }
01414                 Packet_Release(pSndPacket);
01415         }
01416 
01417         return hr;
01418 }
01419 
01420 
01433 BCAP_HRESULT bCap_TaskGetVariable(int iSockFd, u_int lhTask, char *pVarName, char *pstrOption, u_int *plhVar){
01434         BCAP_PACKET             *pSndPacket;
01435         BCAP_PACKET             *pRecPacket;
01436         BCAP_ARG                *pArg;
01437 
01438         BCAP_HRESULT hr = BCAP_E_FAIL;
01439 
01440         pSndPacket = Packet_Create(BCAP_FUNC_Task_GetVariable);         /* BCAP_FUNC_Task_GetVariable */
01441         if (pSndPacket != NULL){
01442 
01443                 u_char buff[LOCALBUFFER_SZ];
01444                 u_int lLen;
01445 
01446                 {
01447                         pArg = Arg_Create( VT_I4, 1, 4, &lhTask);
01448                         if (pArg != NULL){
01449                                 Packet_AddArg(pSndPacket, pArg);
01450                         }
01451                 }
01452 
01453                 {
01454                         lLen = copyToBSTR(buff,pVarName);
01455                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01456                         if (pArg != NULL){
01457                                 Packet_AddArg(pSndPacket, pArg);
01458                         }
01459                 }
01460 
01461                 {
01462                         lLen = copyToBSTR(buff, pstrOption);
01463                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01464                         if (pArg != NULL){
01465                                 Packet_AddArg(pSndPacket, pArg);
01466                         }
01467                 }
01468 
01469                 {
01470                         pRecPacket = Packet_Create(BCAP_S_OK);
01471                         if (pRecPacket != NULL){
01472                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01473                                 if SUCCEEDED(hr){
01474                                     if (pRecPacket->iArgs >= 1){
01475                                       copyValue(plhVar, pRecPacket->pArg->data, 4);
01476                                     }
01477                                     else{
01478                                       
01479                                     }
01480                                   }
01481                         }
01482                         Packet_Release(pRecPacket);
01483                 }
01484                 Packet_Release(pSndPacket);
01485         }
01486 
01487         return hr;
01488 }
01489 
01490 
01502 BCAP_HRESULT bCap_TaskStart(int iSockFd, u_int lhTask, long lMode, char *pStrOption){
01503         BCAP_PACKET             *pSndPacket;
01504         BCAP_PACKET             *pRecPacket;
01505         BCAP_ARG                *pArg;
01506 
01507         BCAP_HRESULT hr = BCAP_E_FAIL;
01508 
01509         pSndPacket = Packet_Create(BCAP_FUNC_Task_Start);               /* BCAP_FUNC_Task_Start */
01510         if (pSndPacket != NULL){
01511 
01512                 u_char buff[LOCALBUFFER_SZ];
01513                 u_int lLen;
01514 
01515                 {
01516                         pArg = Arg_Create( VT_I4, 1, 4, &lhTask);               /* Arg1 Handle of the task */
01517                         if (pArg != NULL){
01518                                 Packet_AddArg(pSndPacket, pArg);
01519                         }
01520                 }
01521         
01522                 {
01523                         pArg = Arg_Create( VT_I4, 1, 4, &lMode);                /* Arg2 start param */
01524                         if (pArg != NULL){
01525                                 Packet_AddArg(pSndPacket, pArg);
01526                         }
01527                 }
01528 
01529 
01530                 {
01531                         lLen = copyToBSTR(buff,pStrOption);                             /* Arg3 option param */
01532                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01533                         if (pArg != NULL){
01534                                 Packet_AddArg(pSndPacket, pArg);
01535                         }
01536                 }
01537 
01538 
01539                 {       
01540                         pRecPacket = Packet_Create(BCAP_S_OK);
01541                         if (pRecPacket != NULL){
01542                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01543                                 if SUCCEEDED(hr){
01544                                     ;
01545                                   }
01546 
01547                         }
01548                         Packet_Release(pRecPacket);
01549                 }
01550                 Packet_Release(pSndPacket);
01551         }
01552 
01553         return hr;
01554 }
01555 
01556 
01568 BCAP_HRESULT bCap_TaskStop(int iSockFd, u_int lhTask, long lMode, char *pStrOption){
01569         BCAP_PACKET             *pSndPacket;
01570         BCAP_PACKET             *pRecPacket;
01571         BCAP_ARG                *pArg;
01572 
01573         BCAP_HRESULT hr = BCAP_E_FAIL;
01574 
01575         pSndPacket = Packet_Create(BCAP_FUNC_Task_Stop);                /* BCAP_FUNC_Task_Stop */
01576         if (pSndPacket != NULL){
01577 
01578                 u_char buff[LOCALBUFFER_SZ];
01579                 u_int lLen;
01580 
01581                 {
01582                         pArg = Arg_Create( VT_I4, 1, 4, &lhTask);               /* Arg1 Handle of the task */
01583                         if (pArg != NULL){
01584                                 Packet_AddArg(pSndPacket, pArg);
01585                         }
01586                 }
01587         
01588                 {
01589                         pArg = Arg_Create( VT_I4, 1, 4, &lMode);                /* Arg2 stop param */
01590                         if (pArg != NULL){
01591                                 Packet_AddArg(pSndPacket, pArg);
01592                         }
01593                 }
01594 
01595 
01596                 {
01597                         lLen = copyToBSTR(buff,pStrOption);                             /* Arg3 option param */
01598                         pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01599                         if (pArg != NULL){
01600                                 Packet_AddArg(pSndPacket, pArg);
01601                         }
01602                 }
01603 
01604 
01605                 {       
01606                         pRecPacket = Packet_Create(BCAP_S_OK);
01607                         if (pRecPacket != NULL){
01608                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01609                                 if SUCCEEDED(hr){
01610                                     ;
01611                                   }
01612 
01613                         }
01614                         Packet_Release(pRecPacket);
01615                 }
01616                 Packet_Release(pSndPacket);
01617         }
01618 
01619         return hr;
01620 }
01621 
01622 
01632 BCAP_HRESULT bCap_VariableRelease(int iSockFd, u_int lhVar){
01633         BCAP_PACKET             *pSndPacket;
01634         BCAP_PACKET             *pRecPacket;
01635         BCAP_ARG                *pArg;
01636 
01637 
01638         BCAP_HRESULT hr = BCAP_E_FAIL;
01639 
01640         pSndPacket = Packet_Create(BCAP_FUNC_Variable_Release);         /* BCAP_FUNC_Task_Release */
01641         if (pSndPacket != NULL){
01642 
01643                 {
01644                         pArg = Arg_Create( VT_I4, 1, 4, &lhVar);
01645                         if (pArg != NULL){
01646                                 Packet_AddArg(pSndPacket, pArg);                                /* Arg1 lhVariable */
01647                         }
01648                 }
01649 
01650                 {       
01651                         pRecPacket = Packet_Create(BCAP_S_OK);                          /* Alloc for storing recieved packet */
01652                         if (pRecPacket != NULL){
01653                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01654 
01655                         }
01656                         Packet_Release(pRecPacket);                                                     /* Release recieved packet */
01657                 }
01658                 Packet_Release(pSndPacket);
01659         }
01660 
01661         return hr;
01662 }
01663 
01675 BCAP_HRESULT bCap_VariableGetValue(int iSockFd, u_int lhVar, void *pVntValue){
01676         BCAP_PACKET             *pSndPacket;
01677         BCAP_PACKET             *pRecPacket;
01678         BCAP_ARG                *pArg;
01679 
01680         BCAP_HRESULT hr = BCAP_E_FAIL;
01681 
01682         pSndPacket = Packet_Create(BCAP_FUNC_Variable_GetValue);                /* BCAP_FUNC_Variable_GetValue */
01683         if (pSndPacket != NULL){
01684 
01685                 {
01686                         pArg = Arg_Create( VT_I4, 1, 4, &lhVar);                                /* Arg1: Variable handle */
01687                         if (pArg != NULL){
01688                                 Packet_AddArg(pSndPacket, pArg);
01689                         }
01690                 }
01691 
01692                 {
01693                         pRecPacket = Packet_Create(BCAP_S_OK);
01694                         if (pRecPacket != NULL){
01695                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01696                                 if SUCCEEDED(hr){
01697                                     if (pRecPacket->iArgs >= 1){
01698                                       hr = Packet_GetResult(pRecPacket, pVntValue);
01699                                     }
01700                                     else{
01701                                       /* NO Argument */
01702                                       hr = BCAP_E_FAIL;
01703                                     }
01704                                   }
01705 
01706                         }
01707                         Packet_Release(pRecPacket);
01708                 }
01709                 Packet_Release(pSndPacket);
01710         }
01711 
01712         return hr;
01713 }
01714 
01726 BCAP_HRESULT bCap_VariablePutValue(int iSockFd, u_int lhVar, u_short iType, u_int lArrays, void  *pVntValue){
01727         BCAP_PACKET             *pSndPacket;
01728         BCAP_PACKET             *pRecPacket;
01729         BCAP_ARG                *pArg;
01730 
01731         BCAP_HRESULT hr = BCAP_E_FAIL;
01732 
01733         pSndPacket = Packet_Create(BCAP_FUNC_Variable_PutValue);                /* BCAP_FUNC_Variable_PutValue */
01734         if (pSndPacket != NULL){
01735 
01736                 {
01737                         pArg = Arg_Create( VT_I4, 1, 4, &lhVar);
01738                         if (pArg != NULL){
01739                                 Packet_AddArg(pSndPacket, pArg);
01740                         }
01741                 }
01742 
01743                 {
01744                         u_int lDataLen = 0; 
01745 
01746                         if ((iType  & ~VT_ARRAY) == VT_BSTR) {          /* Mask "Array" */      /* IMPL:Not Support String array now. */
01747                                 /* String data */
01748 
01749                                 u_char buff[LOCALBUFFER_SZ];
01750                                 u_int lLen;
01751 
01752                                 lLen = copyToBSTR(buff,pVntValue);
01753                                 pArg = Arg_Create( VT_BSTR, 1, lLen,buff);
01754                                 if (pArg != NULL){
01755                                         Packet_AddArg(pSndPacket, pArg);
01756                                 }
01757                         }
01758                         else{
01759                                 /* Not string data */
01760 
01761                                 u_int lLen;
01762                                 lLen = sizeOfVarType((u_short)(iType  & ~VT_ARRAY));
01763                                 lDataLen = lLen * lArrays;
01764                                 if (lDataLen != 0){
01765                                         u_int i;
01766                                         u_char *pSrcData = (u_char *)pVntValue;
01767                                         u_char *pDstData = (u_char *)bMalloc(lDataLen);
01768                                         if (pDstData != NULL){
01769                                                 u_char *pDstPtr = pDstData;
01770                                                 for (i = 0;i < lArrays;i++){
01771 
01772                                                         copyValue(pDstPtr, pSrcData, lLen);
01773                                                         pDstPtr += lLen;
01774                                                         pSrcData += lLen;
01775                                                 }
01776 
01777                                                 pArg = Arg_Create( iType, lArrays, lDataLen, pDstData);
01778                                                 if (pArg != NULL){
01779                                                         Packet_AddArg(pSndPacket, pArg);
01780                                                 }
01781                                                 bFree(pDstData);
01782                                         }
01783                                 }
01784                                 else{   /* lDataLen = 0,then Unknown data type */
01785                                         Packet_Release(pSndPacket);
01786                                         return (BCAP_E_INVALIDARGTYPE);
01787                                 }
01788                         }
01789                 }
01790 
01791 
01792                 {
01793                         pRecPacket = Packet_Create(BCAP_S_OK);
01794                         if (pRecPacket != NULL){
01795                                 hr = bCapSendAndRec(iSockFd, pSndPacket, pRecPacket);
01796                                 if SUCCEEDED(hr){
01797                                         ;       
01798                                   }
01799 
01800                         }
01801                         Packet_Release(pRecPacket);
01802                 }
01803                 Packet_Release(pSndPacket);
01804         }
01805 
01806         return hr;
01807 }
01808 
01809 /*--------------------------------------------------------------------
01810                         b-Cap library utility functions         
01811  --------------------------------------------------------------------*/
01812 
01813 
01823 int failed_to_send_packet = 0;
01824 static BCAP_HRESULT bCapSendAndRec(int iSockFd, BCAP_PACKET             *pSndPacket, BCAP_PACKET                *pRecPacket){
01825         failed_to_send_packet = 0;
01826         BCAP_HRESULT hr = BCAP_E_FAIL;
01827         int retry_count = 0;
01828 StartPoint:
01829         if ( (pRecPacket != NULL) && (pSndPacket != NULL) ){
01830           struct timespec prev_time;
01831           clock_gettime(CLOCK_MONOTONIC, &prev_time);
01832                 if (Packet_Send(iSockFd, pSndPacket) == BCAP_S_OK){
01833                   struct timespec send_after_time;
01834                   clock_gettime(CLOCK_MONOTONIC, &send_after_time);
01835                   int retryp = 0;
01836                         u_char *pRec = receivePacket(iSockFd, &retryp);  /* Receive packet and alloc memory for storing received binary */
01837                         struct timespec recv_after_time;
01838                         clock_gettime(CLOCK_MONOTONIC, &recv_after_time);
01839                         const int NSEC_PER_SECOND = 1e+9;
01840                         fprintf(stderr, "send time: %f\n", 
01841                                 (send_after_time.tv_sec + double(send_after_time.tv_nsec)/NSEC_PER_SECOND)- (prev_time.tv_sec + double(prev_time.tv_nsec)/NSEC_PER_SECOND) );
01842                         fprintf(stderr, "recv time: %f\n", 
01843                                 (recv_after_time.tv_sec + double(recv_after_time.tv_nsec)/NSEC_PER_SECOND) - (send_after_time.tv_sec + double(send_after_time.tv_nsec)/NSEC_PER_SECOND)
01844                                 );
01845 
01846                         if (retryp) {
01847                           failed_to_send_packet = 1;
01848                           ++retry_count;
01849                           ROS_WARN("should retry %u (%d)", pSndPacket->iSerialNo, retry_count);
01850                           pSndPacket->iReserved = pSndPacket->iSerialNo;
01851                           pSndPacket->iSerialNo = ++m_iSerialNo;
01852 
01853                           /* struct timeval tv; */
01854                           /* tv.tv_sec = 0; */
01855                           /* tv.tv_usec = 1000 * 100; /\* 100msec *\/ */
01856                           /* setsockopt(iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); */
01857                           goto StartPoint;
01858                         }
01859                         else if(pRec != NULL){
01860                                 hr = Packet_Deserialize(pRec, pRecPacket );
01861                                 if SUCCEEDED(hr){
01862                                     hr = (BCAP_HRESULT)pRecPacket->lFuncID;
01863                                   }
01864                                 bFree(pRec);                                            /* Free received packet */
01865                         }
01866                         else{
01867                                 /* allocation error */
01868                                 hr = BCAP_E_UNEXPECTED;
01869                         }
01870                 }
01871                 else{
01872                         /* send error  */
01873                         hr = BCAP_E_UNEXPECTED;
01874                 }
01875         }
01876         else{
01877                 /* Null pointer is asigned */
01878                 hr = BCAP_E_FAIL;
01879         }
01880 
01881         return hr;
01882  
01883 }
01884 
01885 
01896 static u_char *receivePacket(int iSockFd, int* retryp){
01897         u_char  pRcvBuffer[LOCALRECBUFFER_SZ];
01898         u_char *pTop;
01899         u_char  *pPacketBuff = NULL;
01900         u_char  *pRemainData;
01901 
01902         u_int lRecvSize;
01903         int lRecLen;
01904         u_int lHeaderLen;
01905         
01906 #if BCAP_CONNECTION_UDP
01907         socklen_t         fromlen;
01908         struct sockaddr_in      serverAddr;                     /* server's socket address */ 
01909 #endif
01910 
01911         /* b-CAP header = 15 bytes, this should be recieved at first */ 
01912         lHeaderLen = BCAP_SIZE_SOH + BCAP_SIZE_LEN + 
01913                                 BCAP_SIZE_SERIAL + BCAP_SIZE_RESERVE +
01914                                 BCAP_SIZE_FUNCID + BCAP_SIZE_ARGNUM;
01915 
01916         /* Receive b-Cap header */
01917         lRecvSize = 0;
01918         while (lRecvSize < lHeaderLen) {
01919 #if BCAP_CONNECTION_COM /* COM */
01920 #ifdef WIN32
01921                 ReadFile((HANDLE)iSockFd, (char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize, (LPDWORD)&lRecLen, NULL);
01922 #else
01923                 lRecLen = read(iSockFd, (char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize);
01924 #endif
01925 #else
01926 #if BCAP_CONNECTION_UDP
01927                 fromlen = sizeof(serverAddr);
01928                 memset( (char *)&serverAddr, 0, sizeof(struct sockaddr_in));
01929                 *retryp = 0;
01930                 lRecLen = recvfrom(iSockFd, (char *)&(pRcvBuffer[lRecvSize]), LOCALRECBUFFER_SZ - lRecvSize, 0, (struct sockaddr *)&serverAddr, &fromlen);
01931                 /* if the sock is not from the server, then ignore  */
01932                 if(lRecLen <= 0) {      /* if sock errer has detected, then exit  */
01933                   fprintf(stderr, "failed to receive a packet\n");
01934                   *retryp = 1;
01935                   goto ExitPoint;
01936                 }
01937                 if ((serverAddr.sin_addr.s_addr != m_sockServerAddr.sin_addr.s_addr) || (serverAddr.sin_port != m_sockServerAddr.sin_port)) {
01938                         continue;
01939                 }
01940 #else /* TCP */
01941                 lRecLen = recv(iSockFd, (char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize, 0); 
01942 #endif
01943 #endif
01944                 if(lRecLen <= 0) {      /* if sock errer has detected, then exit  */
01945                         goto ExitPoint;
01946                 }
01947                 lRecvSize += lRecLen;                   /* add read bytes */
01948 
01949                 pTop = (u_char *)memchr((const void *)pRcvBuffer, BCAP_SOH, lRecvSize);
01950                 if (pTop == NULL){                              /* Is there SOH ? */
01951                         lRecvSize = 0;                          /* If No SOH, then all read data are discarded */
01952                 }
01953                 else{
01954                         if (pTop != pRcvBuffer){        /* if (pTop == pRcvBuffer) then SOH is already in the top. */   
01955                                 lRecvSize = lRecvSize - (pTop - pRcvBuffer);    /* exclude before SOH  */
01956                                 memmove (pRcvBuffer, pTop, lRecvSize);
01957                         }
01958                 }
01959         }
01960 
01961         /* Receive the left data of this packet */
01962         {
01963                 u_int lPacketSize;
01964                 u_int lRemainSize;
01965 
01966                 copyValue(&lPacketSize, &(pRcvBuffer[1]), BCAP_SIZE_LEN);
01967                 lRemainSize  = lPacketSize - lRecvSize;
01968 
01969                 pPacketBuff = (unsigned char *)bMalloc(lPacketSize);    
01970                 if (pPacketBuff != NULL){
01971                         memcpy(pPacketBuff, pRcvBuffer, lRecvSize);
01972                         pRemainData = pPacketBuff + lRecvSize;
01973                 }
01974                 else{
01975                         goto ExitPoint; /* out of memory */
01976                 }
01977 
01978                 lRecvSize = 0;
01979                 while (lRecvSize < lRemainSize) {
01980 #if BCAP_CONNECTION_COM /* COM */
01981 
01982 #ifdef WIN32
01983                         ReadFile((HANDLE)iSockFd, (char *)&(pRemainData[lRecvSize]), lRemainSize - lRecvSize, (LPDWORD)&lRecLen, NULL);
01984 #else
01985                         lRecLen = read(iSockFd, (char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize);
01986 #endif
01987 
01988 #else
01989 #if BCAP_CONNECTION_UDP
01990                         fromlen = sizeof(serverAddr);
01991                         lRecLen = recvfrom(iSockFd, (char *)&(pRemainData[lRecvSize]), lRemainSize -lRecvSize , 0, (struct sockaddr *)&serverAddr, &fromlen);
01992                         /* if the sock is not from the server, then ignore  */
01993                         if ((serverAddr.sin_addr.s_addr != m_sockServerAddr.sin_addr.s_addr) || (serverAddr.sin_port != m_sockServerAddr.sin_port)) {
01994                                 continue;
01995                         }
01996 
01997 #else /* TCP */
01998                         lRecLen = recv(iSockFd, (char *)&(pRemainData[lRecvSize]), lRemainSize -lRecvSize , 0); 
01999 #endif
02000 #endif
02001                         if(lRecLen <= 0) {      /* if sock errer has detected, then exit  */
02002 
02003                                 goto ExitPoint;
02004                         }
02005                         lRecvSize += lRecLen;                   /* add read bytes */
02006                 }
02007 
02008                 /* Check Terminator EOT  */
02009                 if (pPacketBuff[lPacketSize - 1] != BCAP_EOT) { 
02010                         goto ExitPoint;
02011                 }
02012 
02013         }
02014 
02015         return pPacketBuff;
02016 
02017 ExitPoint:
02018         if (pPacketBuff != NULL) { 
02019                 free(pPacketBuff);
02020                 pPacketBuff = NULL;
02021         }
02022         return NULL;
02023 
02024 }
02025 
02026                                                                                         
02027 
02039  static BCAP_HRESULT sendBinary(int iSockFd, u_char *pBuff, u_int lSize){
02040 
02041         BCAP_HRESULT hr = BCAP_E_FAIL;
02042         int iLen;
02043         int iSent;
02044 
02045         iLen = (int)lSize;
02046         if (iLen > 0){
02047 
02048 #if BCAP_CONNECTION_COM /* COM */
02049 
02050 #ifdef WIN32
02051                 WriteFile((HANDLE)iSockFd, pBuff, iLen, (LPDWORD)&iSent, NULL);
02052 #else
02053                 iSent = write(iSockFd, pBuff, iLen);
02054 #endif
02055 
02056 #else
02057 #if BCAP_CONNECTION_UDP
02058                 iSent = sendto(iSockFd, (char *)pBuff, iLen, 0, (struct sockaddr *)&m_sockServerAddr, sizeof(m_sockServerAddr));
02059 #else /* TCP */
02060                 iSent = send(iSockFd, (char *)pBuff, iLen, 0);
02061 #endif
02062 #endif
02063 
02064                 if (iSent == iLen){
02065                          hr = BCAP_S_OK;
02066                 }
02067         
02068         }
02069         return hr;
02070 }
02071 
02072 
02082 static BCAP_HRESULT                     Packet_Send(int iSockFd, BCAP_PACKET *pPacket){
02083 
02084         BCAP_HRESULT hr = BCAP_E_FAIL;
02085 
02086         void *pbSendData; 
02087 
02088         pbSendData = bMalloc(pPacket->lMsgLength);      
02089         if (pbSendData != NULL){
02090                 if (Packet_Serialize(pPacket, pbSendData) == BCAP_S_OK){
02091 
02092                         hr = sendBinary(iSockFd, (u_char *)pbSendData, pPacket->lMsgLength);    /* Send packet  */
02093                         if (hr == BCAP_S_OK){
02094                                 ;
02095                         }
02096                 }
02097                 bFree(pbSendData);
02098         }
02099         
02100         return hr;
02101 }
02102 
02103 
02112 static BCAP_ARG         **Packet_GetLastArgHandle( BCAP_PACKET *pPacket){
02113         BCAP_ARG **pLastArg = NULL;
02114         BCAP_ARG *pArg;
02115 
02116         if (pPacket != NULL){
02117 
02118                 pArg = pPacket->pArg;                                   /* set base pointer in packet struct */
02119                 pLastArg =      &(pPacket->pArg);                               /* if this is NULL in this time, this packet has no argment.
02120                                                                                                         So, pPacket->pArg is set as the last arg. */
02121 
02122                 /* search the last arg pointer */
02123                 while(pArg != NULL){    
02124                         pLastArg = &(pArg->pNextArg);
02125 
02126                         pArg = pArg->pNextArg;                          /* set next arg pointer. it might be NULL */
02127                 }
02128         }
02129 
02130         if (pLastArg != NULL){
02131                 return pLastArg;
02132         }
02133         else{
02134                 return NULL;
02135         }
02136 }
02137 
02138 
02148 static BCAP_HRESULT                     Packet_AddArg( BCAP_PACKET *pPacket, BCAP_ARG *pNewArg){
02149 
02150         BCAP_HRESULT hr = BCAP_S_OK;
02151         BCAP_ARG **pLastArg = NULL;
02152 
02153         if ( (pPacket != NULL) && (pNewArg != NULL)) {
02154                 pLastArg = Packet_GetLastArgHandle(pPacket);    /* get a pointer of the last arg. */
02155                 if (pLastArg == NULL){                          
02156 
02157                         /* Bad pointer */
02158                         hr = BCAP_E_FAIL;
02159                 }
02160                 else{
02161                         /* Not NULL: then success to find the last arg */
02162                         *pLastArg  = pNewArg;           /* Set the new arg pointer as the next arg pointer */
02163                 }
02164 
02165                 pPacket->iArgs++;                                               /* Increment arg counter of this packet */
02166 
02167                 pPacket->lMsgLength += (pNewArg->lLength + BCAP_SIZE_ARGLEN);
02168                                                                                                 /* Add arg size into Packet length */
02169                 hr = BCAP_S_OK;
02170         }
02171         else{
02172 
02173                 /* Bad pointer */
02174                 hr = BCAP_E_FAIL;
02175         }
02176 
02177         return hr;
02178 }
02179 
02188 static void                     Packet_Release( BCAP_PACKET *pPacket){
02189 
02190 
02191         /* release poacket  */
02192         if (pPacket != NULL){
02193                 /* release all args  */
02194                 Arg_Release(pPacket->pArg);
02195 
02196                 /* release instance of the packet */            
02197                 bFree(pPacket);
02198         }
02199 }
02200 
02209 static BCAP_PACKET      *Packet_Create( u_int lFuncID){
02210         
02211          BCAP_PACKET *pNewPacket = NULL;
02212 
02213          pNewPacket = (BCAP_PACKET *)bMalloc(sizeof(BCAP_PACKET));      /* alloc packet */
02214          if (pNewPacket != NULL){
02215 
02216                 pNewPacket->iArgs = 0;                                          /* args count */
02217                 pNewPacket->iReserved = 0;
02218                 pNewPacket->iSerialNo = m_iSerialNo;
02219                 pNewPacket->lFuncID = lFuncID;
02220 
02221                 pNewPacket->lMsgLength = BCAP_SIZE_BASE;
02222 #if BCAP_CONNECTION_COM /* COM */
02223                 pNewPacket->lMsgLength += BCAP_SIZE_CRC;
02224 #endif
02225                 pNewPacket->pArg = NULL;
02226 
02227                 m_iSerialNo++;                                                          /* increment serial number */
02228                 if (m_iSerialNo == 0){
02229                         m_iSerialNo = 1;                                                /* Not use Zero as a serial number */
02230                 }
02231          }
02232 
02233         return pNewPacket;
02234 }
02235 
02236 
02237 
02247 static BCAP_HRESULT             Packet_Serialize(BCAP_PACKET *pSrcPacket, void *pDstBinData){
02248 
02249 
02250         BCAP_HRESULT hr = BCAP_S_OK;
02251         u_char *pDstPtr;
02252 
02253         pDstPtr = (u_char *)pDstBinData;
02254 
02255         /* SOH */
02256         *pDstPtr = BCAP_SOH;    
02257         pDstPtr += BCAP_SIZE_SOH;
02258         /* Header */
02259         pDstPtr += copyValue( pDstPtr, &(pSrcPacket->lMsgLength), sizeof(pSrcPacket->lMsgLength));
02260         pDstPtr += copyValue( pDstPtr, &(pSrcPacket->iSerialNo), sizeof(pSrcPacket->iSerialNo));
02261         pDstPtr += copyValue( pDstPtr, &(pSrcPacket->iReserved), sizeof(pSrcPacket->iReserved));
02262         pDstPtr += copyValue( pDstPtr, &(pSrcPacket->lFuncID), sizeof(pSrcPacket->lFuncID));
02263         pDstPtr += copyValue( pDstPtr, &(pSrcPacket->iArgs), sizeof(pSrcPacket->iArgs));
02264 
02265 
02266         {       /* Copy args */
02267                 unsigned int i,j;
02268                 BCAP_ARG *pArgPtr;
02269                 u_char *pbValue;
02270                 pArgPtr = pSrcPacket->pArg;
02271 
02272                 for (i =0 ; i < pSrcPacket->iArgs ; i++){
02273                         if (pArgPtr != NULL){
02274                                 {
02275                                         pDstPtr += copyValue( pDstPtr, &(pArgPtr->lLength), sizeof(pArgPtr->lLength));
02276                                         pDstPtr += copyValue( pDstPtr, &(pArgPtr->iType), sizeof(pArgPtr->iType));
02277                                         pDstPtr += copyValue( pDstPtr, &(pArgPtr->lArrays), sizeof(pArgPtr->lArrays));
02278                                         
02279                                         pbValue = (u_char *)pArgPtr->data;                                      /* value stored pointer is set */
02280 
02281                                         for (j= 0; j < pArgPtr->lArrays; j++){
02282                                                 u_int lValueSize;
02283                                                 u_short iVarType = pArgPtr->iType & ~VT_ARRAY;  /* Mask "Array" */
02284                                                 switch (iVarType){
02285                                                         case VT_UI1:
02286                                                         case VT_I2:
02287                                                         case VT_UI2:
02288                                                         case VT_I4:
02289                                                         case VT_UI4:
02290                                                         case VT_R4:
02291                                                         case VT_R8:
02292                                                         case VT_BOOL:
02293                                                                 {
02294                                                                         lValueSize = sizeOfVarType(iVarType);
02295                                                                         pDstPtr += copyValue( pDstPtr, pbValue, lValueSize);
02296                                                                         pbValue += lValueSize;                                  /* value stored pointer is added */
02297                                                                 }
02298                                                                 break;
02299 
02300                                                         case VT_BSTR:
02301                                                                 {
02302                                                                         u_int lStrLen;
02303                                                                         copyValue( &lStrLen, pbValue, BCAP_SIZE_ARGSTRLEN);
02304                                                                         pDstPtr += copyValue( pDstPtr, &lStrLen, BCAP_SIZE_ARGSTRLEN);  /* Set String length (4 bytes) */
02305                                                                         pbValue += BCAP_SIZE_ARGSTRLEN;         /* value stored pointer is added */
02306 
02307                                                                         pDstPtr += copyValue( pDstPtr, pbValue, lStrLen);       /* Set string data */
02308                                                                         pbValue += lStrLen;
02309                                                                 }
02310                                                                 break;
02311 
02312                                                         case VT_VARIANT:
02313                                                                 /* NOT_IMPLEMENT */
02314                                                                 {
02315                                                                         
02316 
02317                                                                 }
02318                                         
02319 
02320                                                                 break;
02321                                                         
02322                                                         default:
02323                                                                 break;
02324                                                 }
02325                                         }
02326 
02327                                 }
02328                                 pArgPtr = pArgPtr->pNextArg;    /* Set pointer to the next arg. */
02329                         }
02330                 }
02331                 
02332         }
02333 
02334 #if BCAP_CONNECTION_COM /* COM */
02335         {       /* CRC calculation */
02336                 u_int dwPos, cnt;
02337                 u_short crc;
02338                 crc = 0xFFFF;           
02339                 for (dwPos = 1; dwPos < pSrcPacket->lMsgLength - 3; dwPos++) {
02340                         crc ^= (((u_char *)pDstBinData)[dwPos] << 8);
02341                         for (cnt = 0; cnt < 8; cnt++) {
02342                                 if (crc & 0x8000) {
02343                                         crc = (crc << 1) ^ 0x1021; /* CRC-CCITT */
02344                                 }
02345                                 else {
02346                                         crc <<= 1;
02347                                 }
02348                         }       
02349                 }
02350                 pDstPtr += copyValue( pDstPtr, &crc, sizeof(crc));
02351         }
02352 #endif
02353 
02354         /* EOT */
02355         *pDstPtr = BCAP_EOT;    
02356         pDstPtr += BCAP_SIZE_EOT;
02357 
02358         return hr;
02359 }
02360 
02361 
02373 static BCAP_HRESULT             Packet_Deserialize(void *pSrcBinData, BCAP_PACKET *pDstPacket){
02374 
02375 
02376         BCAP_HRESULT hr = BCAP_S_OK;
02377         u_char *pSrcPtr;
02378 
02379         u_short iArgs;
02380         u_int   lMsgLength;
02381         
02382         pSrcPtr = (u_char *)pSrcBinData;
02383 
02384         /* SOH */
02385         /* pDstPtr += copyValue( &(pDstPacket->Header), pDstPtr, BCAP_SIZE_SOH)); */ 
02386         pSrcPtr += BCAP_SIZE_SOH;
02387 
02388         /* Header */    
02389         /* "pkt->lMsgLength" and "pkt->lArgs" are calcurated in Packet_AddArg()  automatically */
02390         pSrcPtr += copyValue( &lMsgLength                               , pSrcPtr, sizeof(pDstPacket->lMsgLength));     /*  calcurated in Packet_AddArg()  automatically */
02391         pSrcPtr += copyValue( &(pDstPacket->iSerialNo)  , pSrcPtr, sizeof(pDstPacket->iSerialNo));
02392         pSrcPtr += copyValue( &(pDstPacket->iReserved)  , pSrcPtr, sizeof(pDstPacket->iReserved));
02393         pSrcPtr += copyValue( &(pDstPacket->lFuncID)    , pSrcPtr, sizeof(pDstPacket->lFuncID));
02394         pSrcPtr += copyValue( &iArgs                                    , pSrcPtr, sizeof(pDstPacket->iArgs));          /*  calcurated in Packet_AddArg()  automatically */
02395 
02396 
02397         {       /* Copy args */
02398                 unsigned int i;
02399                 BCAP_ARG *pArgPtr;
02400                 pArgPtr = pDstPacket->pArg;
02401 
02402                 for (i = 0 ; i < iArgs ; i++){
02403                         u_int   lDataSize;      /* size of "*data" */
02404                         u_int   lLength;        /* size of argument block */
02405                         u_short iType;
02406                         u_int   lArrays;
02407 
02408                         pSrcPtr += copyValue( &lLength, pSrcPtr, sizeof(lLength));              /* size of a argument block  */
02409                         pSrcPtr += copyValue( &iType, pSrcPtr, sizeof(iType));
02410                         pSrcPtr += copyValue( &lArrays, pSrcPtr, sizeof(lArrays));
02411 
02412                         lDataSize = lLength - BCAP_SIZE_ARGTYPE -BCAP_SIZE_ARGARRAYS;   /* size of "*data" */
02413 
02414                         pArgPtr = Arg_Create(iType, lArrays, lDataSize, pSrcPtr);               /* Create new arg */
02415                         if (pArgPtr != NULL){
02416                                 pSrcPtr = pSrcPtr + lDataSize;                                                          /* skip "*data"  */
02417                         }
02418                         else{
02419                                 hr = BCAP_E_FAIL;
02420                                 break;
02421                         }
02422                         
02423                         if (Packet_AddArg(pDstPacket, pArgPtr) != BCAP_S_OK){                           /* Add new arg to packet */
02424                                 Arg_Release(pArgPtr);                                                                           /* Fail to add, then release this arg. */
02425                                 hr = BCAP_E_FAIL;
02426                                 break;
02427                         }
02428                 }
02429                 
02430         }
02431 
02432 #if BCAP_CONNECTION_COM /* COM */
02433         {
02434                 u_short iCRC;
02435                 /* CRC */
02436                 pSrcPtr += copyValue(&iCRC, pSrcPtr, sizeof(iCRC));
02437         }
02438 #endif
02439 
02440         /* EOT */
02441         if (hr == BCAP_S_OK){
02442                 if (*pSrcPtr != BCAP_EOT){                                                                                      /* If end of the binnary is not EOT, then error */
02443                         hr = BCAP_E_UNEXPECTED; 
02444                 }
02445         }
02446         pSrcPtr += BCAP_SIZE_EOT;
02447 
02448         return hr;
02449 }
02450 
02459 BCAP_HRESULT Packet_GetResult(BCAP_PACKET *pRecPacket, void *pResult){
02460 
02461         BCAP_HRESULT hr = BCAP_E_FAIL;
02462 
02463         if (pRecPacket != NULL){
02464                 if (pRecPacket->iArgs >= 1){
02465                         {       /* Copy values */
02466                                 u_int i;
02467                                 u_int lSize;
02468                                 u_short iType;
02469                                 BCAP_ARG        *pArgValue = pRecPacket->pArg;
02470 
02471                                 iType = (pArgValue->iType) & ~VT_ARRAY; /* Mask "Array" */
02472                                 if (iType == VT_BSTR){
02473                                         u_char *pDstAscii = (u_char *)pResult;
02474                                         u_char *pSrcBstr = (u_char *)pArgValue->data;
02475 
02476                                         for (i = 0;i < pArgValue->lArrays;i++){
02477                                                 lSize = copyFromBSTR(pDstAscii, pSrcBstr);
02478                                                 pDstAscii += lSize;
02479                                                 pSrcBstr += BCAP_SIZE_ARGSTRLEN + ((lSize -1) * 2);     /* lSize include Terminator,so (lSize -1) * 2) */
02480                                         }
02481                                 }
02482                                 else{
02483                                         lSize = sizeOfVarType((u_short)(pArgValue->iType));
02484                                         if (lSize != 0){
02485                                                 u_char *pDst = (u_char *)pResult;
02486                                                 u_char *pSrc = (u_char *)(u_char *)pArgValue->data;
02487 
02488                                                 for (i = 0;i < pArgValue->lArrays;i++){
02489                                                         copyValue(pDst, pSrc, lSize);
02490                                                         pDst += lSize;
02491                                                         pSrc += lSize;
02492                                                 }
02493                                         }
02494                                 }
02495                         }
02496                 }
02497                 else{
02498                         /* NO Argument */
02499                 }
02500                 hr = BCAP_S_OK;
02501         }
02502         return hr;
02503 }
02504 
02518 static BCAP_ARG         *Arg_Create( u_short iType, u_int lArrays, u_int lDataSize, void *data){
02519 
02520         BCAP_ARG *pNewArg = NULL;
02521 
02522         if ((lDataSize < BCAP_MAX_ARG_SIZE)){           /* (0 < ) has something data, (<BCAP_MAX_ARG_SIZE) not over size */
02523 
02524                 pNewArg = (BCAP_ARG *)bMalloc(sizeof(BCAP_ARG));                        /* alloc argument */
02525                 if (pNewArg != NULL){
02526                         pNewArg->iType = iType;
02527                         pNewArg->lArrays = lArrays;
02528                         pNewArg->lLength = lDataSize + BCAP_SIZE_ARGTYPE + BCAP_SIZE_ARGARRAYS;         
02529                         pNewArg->pNextArg = NULL;
02530                         pNewArg->data = NULL;
02531 
02532                         pNewArg->data = bMalloc(lDataSize);
02533                         if (pNewArg->data != NULL ){
02534                                 if (data != NULL){
02535                                         memcpy(pNewArg->data, data, lDataSize);
02536                                 }
02537                                 else{
02538                                         /* Fail to copy from *data, then release this argumant.  */
02539                                         Arg_Release(pNewArg);
02540                                         pNewArg = NULL;                                                         /* return value (allocated pointer) */
02541                                 }
02542 
02543                         }
02544                         else{   /* fail to alloc memory */
02545                                 /* Fail to alloc memory, then release this argumant.  */
02546                                 Arg_Release(pNewArg);
02547                                 pNewArg = NULL;                                                         /* return value (allocated pointer) */
02548                         }
02549                 }
02550          }
02551 
02552 
02553         return pNewArg;
02554 }
02555 
02556 
02565 static void                     Arg_Release( BCAP_ARG *pArg){
02566 
02567         while (pArg != NULL){
02568                 BCAP_ARG *pNextArg;
02569 
02570                 pNextArg = pArg->pNextArg;                                                      /* store next pointer in temporary */
02571 
02572                 if (pArg->data != NULL){
02573                         bFree(pArg->data);                                                              /* free value of argument */
02574                 }
02575 
02576                 bFree(pArg);                                                                                    /* free argument */
02577                 pArg = pNextArg;                                                                        /* set next pointer */
02578         }
02579 }
02580 
02581 
02582 
02592 static u_int sizeOfVariant(BCAP_VARIANT vntValue){
02593         u_int lSize = 0;
02594 
02595         if (vntValue.Arrays > 0) {
02596                 switch (vntValue.Type & ~VT_ARRAY){
02597                         case VT_BSTR:
02598                                 {
02599                                         u_int i;
02600                                         u_int lStrLen;
02601                                         char    *pData;
02602 
02603                                         pData = (char *)(&vntValue.Value);
02604                                         for (i =0; i < vntValue.Arrays; i++){
02605                                                 lStrLen = (u_int)strlen(pData);
02606                                                 lSize += lStrLen;
02607                                                 pData += lStrLen;
02608                                         }
02609                                 }
02610                                 break;
02611                         default:
02612                                 lSize = sizeOfVarType(vntValue.Type) * vntValue.Arrays;
02613                                 break;
02614                 }
02615         }       
02616         else{
02617                 lSize = 0;
02618         }
02619         
02620         return lSize;
02621 }
02622 
02632 static u_int sizeOfVarType(u_short iType){
02633 
02634          u_int lValueSize = 0;
02635 
02636          switch (iType & ~VT_ARRAY){
02637                 case VT_UI1:
02638                         lValueSize = 1;
02639                         break;
02640                 case VT_I2:
02641                         lValueSize = 2;
02642                         break;
02643                 case VT_UI2:
02644                         lValueSize = 2;
02645                         break;
02646                 case VT_I4:
02647                         lValueSize = 4;
02648                         break;
02649                 case VT_UI4:
02650                         lValueSize = 4;
02651                         break;
02652                 case VT_R4:
02653                         lValueSize = 4;
02654                         break;
02655                 case VT_R8:
02656                         lValueSize = 8;
02657                         break;
02658                 case VT_BOOL:
02659                         lValueSize = 2;
02660                         break;
02661                 case VT_BSTR:
02662                         /* In this function  */
02663                         lValueSize = BCAP_SIZE_ARGSTRLEN;       
02664                         break;
02665 
02666                 case VT_VARIANT:        /* Not implement */
02667                         break;
02668                 
02669                 default:                /*  Not implement */
02670                                                 /*      VT_EMPTY         */
02671                                                 /*      VT_NULL          */
02672                                                 /*      VT_ERROR         */
02673                                                 /*      VT_CY            */
02674                                                 /*      VT_DATE          */
02675                         break;
02676         }
02677 
02678          return lValueSize;
02679 }
02680 
02690  u_int copyToBSTR(void *pDstBstrPtr, void *pSrcAsciiPtr){
02691         u_char *pbDst = (u_char *)pDstBstrPtr;
02692         u_char *pbSrc = (u_char *)pSrcAsciiPtr;
02693         u_int   lStrLen,lLen2;
02694         u_int   i;
02695 
02696         lStrLen = strlen((const char *)pbSrc);                                                                  /* length of source string (ascii) */
02697         lLen2 = lStrLen * 2;
02698 
02699         if (pbDst != NULL){
02700                 pbDst += copyValue(pbDst, &lLen2, BCAP_SIZE_ARGSTRLEN);
02701                 
02702                 for (i = 0;i < lStrLen; i++){
02703                         *pbDst++ =  *pbSrc++;
02704                         *pbDst++ =  0;
02705                 }
02706 
02707         }
02708         return (lStrLen * 2 + BCAP_SIZE_ARGSTRLEN);
02709 }
02710 
02720 static u_int copyFromBSTR(void *pDstAsciiPtr, void *pSrcBstrPtr){
02721         u_char *pbDst = (u_char *)pDstAsciiPtr;
02722         u_char *pbSrc = (u_char *)pSrcBstrPtr;
02723         u_int   lStrLen,lLen2;
02724         u_int   i;
02725 
02726         copyValue(&lStrLen, pbSrc, BCAP_SIZE_ARGSTRLEN);                                        /* Get BStr length */
02727         pbSrc += BCAP_SIZE_ARGSTRLEN;
02728 
02729         lLen2 = lStrLen / 2;
02730 
02731         if (pbDst != NULL){
02732                 for (i = 0;i < lLen2; i++){
02733                         *pbDst = *pbSrc;
02734                         pbDst += 1; /* Ascii */
02735                         pbSrc += 2;     /* BSTR */
02736 
02737                 }
02738                 *pbDst = 0;             /* Zero termination */
02739                 lLen2++;                /* +1 = (Zero termination) */
02740         }
02741         return (lLen2);
02742 }
02743 
02744 
02754 static u_int copyVariantFromArg(BCAP_VARIANT *pVntDst, BCAP_ARG *pArg){
02755 
02756         u_int i;
02757         u_int lSize;
02758         u_short iType;
02759         u_int lLength = 0;
02760 
02761         if ((pVntDst != NULL) && (pArg != NULL)){
02762 
02763                 pVntDst->Type   = pArg->iType;
02764                 pVntDst->Arrays = pArg->lArrays;
02765                 // pVntDst->Value
02766 
02767 
02768                 iType = (pArg->iType) & ~VT_ARRAY;      /* Mask "Array" */
02769                 if (iType == VT_BSTR){
02770                         u_char *pDstAscii = (u_char *)(&(pVntDst->Value));
02771                         u_char *pSrcBstr = (u_char *)pArg->data;
02772 
02773                         for (i = 0;i < pArg->lArrays;i++){
02774                                 lSize = copyFromBSTR(pDstAscii, pSrcBstr);
02775                                 pDstAscii += lSize;
02776                                 pSrcBstr += BCAP_SIZE_ARGSTRLEN + ((lSize -1) * 2);     /* lSize include Terminator,so (lSize -1) * 2) */
02777                         }
02778                 }
02779                 else{
02780                         lSize = sizeOfVarType((u_short)(pArg->iType));
02781                         if (lSize != 0){
02782                                 u_char *pDstPtr = (u_char *)(&(pVntDst->Value));
02783                                 u_char *pSrcPtr = (u_char *)pArg->data;
02784 
02785                                 for (i = 0;i < pArg->lArrays;i++){ 
02786                                         copyValue(pDstPtr, pSrcPtr, lSize);
02787                                         pDstPtr += lSize;
02788                                         pSrcPtr += lSize;
02789                                 }
02790                         }
02791                 }
02792         }
02793 
02794         return lLength;                                                                 /* return copied bytes size */
02795 }
02796 
02806 /* static u_int copyArgValue(void *pDst, BCAP_ARG *pArg){ */
02807 
02808 /*      u_int i; */
02809 /*      u_int lSize; */
02810 /*      u_short iType; */
02811 /*      u_int lLength = 0; */
02812 
02813 /*      iType = (pArg->iType) & ~VT_ARRAY;      /\* Mask "Array" *\/ */
02814 /*      if (iType == VT_BSTR){ */
02815 /*              u_char *pDstAscii = (u_char *)pDst; */
02816 /*              u_char *pSrcBstr = (u_char *)pArg->data; */
02817 
02818 /*              for (i = 0;i < pArg->lArrays;i++){ */
02819 /*                      lSize = copyFromBSTR(pDstAscii, pSrcBstr); */
02820 /*                      pDstAscii += lSize; */
02821 /*                      pSrcBstr += BCAP_SIZE_ARGSTRLEN + ((lSize -1) * 2);     /\* lSize include Terminator,so (lSize -1) * 2) *\/ */
02822 /*              } */
02823 /*      } */
02824 /*      else{ */
02825 /*              lSize = sizeOfVarType((u_short)(pArg->iType)); */
02826 /*              if (lSize != 0){ */
02827 /*                //u_char *pDstPtr = (u_char *)pDst; */
02828 /*                      u_char *pSrcPtr = (u_char *)pArg->data; */
02829 
02830 /*                      for (i = 0;i < pArg->lArrays;i++){  */
02831 /*                              copyValue(pSrcPtr, pSrcPtr, lSize); */
02832 /*                              pSrcPtr += lSize; */
02833 /*                              pSrcPtr += lSize; */
02834 /*                      } */
02835 /*              } */
02836 /*      } */
02837 
02838 
02839 /*      return lLength;                                                                 /\* return copied bytes size *\/ */
02840 /* } */
02841 
02842 
02843  /*
02844  *      copy value in the "Little Endian" 
02845  *              
02846  *              alternate of htonl()/ntohl()
02847  */
02848 static u_int copyValue(void *pDst, void *pVntValue, u_int lLength){
02849 
02850 #if defined(__BIG_ENDIAN__)
02851 
02852         /* SPARC/MC68xxx */
02853 
02854         /* copy values inversion. b-CAP is based on little-endian */
02855         {
02856                 u_int i;
02857 
02858                 u_char *pbDst;
02859                 u_char *pbSrc;
02860 
02861                 pbSrc = (u_char *)(pVntValue) + lLength -1;
02862                 pbDst = (u_char *)pDst;
02863 
02864                 for (i = 0 ; i < lLength ; i++){
02865                         *pbDst++ = *pbSrc--; 
02866                 }
02867         }
02868 #else
02869 
02870         memcpy(pDst, pVntValue, lLength);
02871 #endif
02872 
02873         return lLength;                                                                 /* return copied bytes size */
02874 }
02875 
02876 /*
02877  *      Memory allocation counter 
02878  */
02879 static long m_lAllocCount = 0;
02880 static long m_lAllocSize = 0;
02881 
02882 /*
02883  *      alternative of Malloc()
02884  */
02885 static void *bMalloc(size_t size){
02886 
02887         void *pPtr;     
02888 
02889         m_lAllocCount++;
02890         m_lAllocSize += size;
02891 
02892         pPtr = malloc(size);
02893 
02894 #ifdef DEBUG
02895         printf("AllocCount:%d\n",m_lAllocCount);
02896 #endif
02897 
02898         return pPtr;
02899 }
02900 
02901 /*
02902  *      alternative of Free()
02903  */
02904 static void bFree(void *pPtr){
02905 
02906         m_lAllocCount--;
02907 #ifdef DEBUG
02908         printf("AllocCount:%d\n",m_lAllocCount);
02909 #endif
02910         free(pPtr);
02911 }
02912 
02913 
02914 


denso_controller
Author(s): Ryohei Ueda
autogenerated on Wed Aug 26 2015 11:21:21