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