25 #pragma comment(lib, "wsock32.lib") 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <arpa/inet.h> 34 #include <netinet/in.h> 46 #if !defined(__LITTLE_ENDIAN__) 47 #if !defined(__BIG_ENDIAN__) 49 #if __BYTE_ORDER == __LITTLE_ENDIAN 50 #define __LITTLE_ENDIAN__ 51 #elif __BYTE_ORDER == __BIG_ENDIAN 52 #define __BIG_ENDIAN__ 62 #define DebugPrint( message ) fprintf( stderr, message ) 63 #define DebugPrint2( f, a, b ) fprintf( stderr, f, a, b ) 65 #define DebugPrint( message ) 66 #define DebugPrint2( f, a, b ) 73 #define LOCALBUFFER_SZ 1024 75 #define LOCALRECBUFFER_SZ 1024 81 #define BCAP_SIZE_SOH 1 82 #define BCAP_SIZE_EOT 1 83 #define BCAP_SIZE_LEN 4 84 #define BCAP_SIZE_SERIAL 2 85 #define BCAP_SIZE_RESERVE 2 86 #define BCAP_SIZE_FUNCID 4 87 #define BCAP_SIZE_ARGNUM 2 88 #define BCAP_SIZE_CRC 2 91 #define BCAP_SIZE_BASE (BCAP_SIZE_SOH + BCAP_SIZE_EOT + \ 92 BCAP_SIZE_LEN + BCAP_SIZE_SERIAL + \ 93 BCAP_SIZE_RESERVE + BCAP_SIZE_FUNCID + \ 98 #define BCAP_SIZE_ARGLEN 4 99 #define BCAP_SIZE_ARGTYPE 2 100 #define BCAP_SIZE_ARGARRAYS 4 101 #define BCAP_SIZE_ARGBASE (BCAP_SIZE_ARGLEN+BCAP_SIZE_ARGTYPE+BCAP_SIZE_ARGARRAYS) 103 #define BCAP_SIZE_ARGSTRLEN 4 106 #define BCAP_MAX_PACKET_SIZE 0x1000000 107 #define BCAP_MAX_ARG_SIZE 0x1000000 111 #define BCAP_FUNC_Service_Start 1 112 #define BCAP_FUNC_Service_Stop 2 113 #define BCAP_FUNC_Controller_Connect 3 114 #define BCAP_FUNC_Controller_Disconnect 4 115 #define BCAP_FUNC_Controller_GetRobot 7 116 #define BCAP_FUNC_Controller_GetTask 8 117 #define BCAP_FUNC_Controller_GetVariable 9 118 #define BCAP_FUNC_Controller_Execute 17 120 #define BCAP_FUNC_Robot_GetVariable 62 121 #define BCAP_FUNC_Robot_Execute 64 122 #define BCAP_FUNC_Robot_Change 66 123 #define BCAP_FUNC_Robot_Move 72 124 #define BCAP_FUNC_Robot_Release 84 126 #define BCAP_FUNC_Task_GetVariable 85 127 #define BCAP_FUNC_Task_Start 88 128 #define BCAP_FUNC_Task_Stop 89 129 #define BCAP_FUNC_Task_Release 99 131 #define BCAP_FUNC_Variable_PutValue 102 132 #define BCAP_FUNC_Variable_GetValue 101 133 #define BCAP_FUNC_Variable_PutValue 102 134 #define BCAP_FUNC_Variable_Release 111 202 static void *
bMalloc(
size_t size);
203 static void bFree(
void *pPtr);
208 #ifdef BCAP_CONNECTION_UDP 209 static struct sockaddr_in m_sockServerAddr;
229 #ifdef BCAP_CONNECTION_COM 237 COMMTIMEOUTS stTimeOut;
239 sprintf(szFileName,
"//./COM%d", iPort);
240 hSerial = CreateFile(szFileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
241 if (GetCommState(hSerial, &dcb)) {
244 dcb.Parity = NOPARITY;
245 dcb.StopBits = ONESTOPBIT;
248 dcb.fOutxCtsFlow = FALSE;
249 dcb.fRtsControl = RTS_CONTROL_ENABLE;
250 dcb.fOutxDsrFlow = FALSE;
251 dcb.fDtrControl = DTR_CONTROL_ENABLE;
252 dcb.fDsrSensitivity = FALSE;
254 bRet = SetCommState(hSerial, &dcb);
257 stTimeOut.ReadIntervalTimeout = MAXDWORD ;
258 stTimeOut.ReadTotalTimeoutMultiplier = MAXDWORD ;
259 stTimeOut.ReadTotalTimeoutConstant = 1000;
260 stTimeOut.WriteTotalTimeoutMultiplier = MAXDWORD;
261 stTimeOut.WriteTotalTimeoutConstant = 1000;
262 bRet = SetCommTimeouts( hSerial, &stTimeOut );
264 *iSockFd = (int)hSerial;
271 sprintf(dev,
"/dev/ttyS%d", iPort);
272 *iSockFd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK)
274 bzero(&tio,
sizeof(tio));
276 tio.c_iflag = IGNPAR;
282 tcflush(*iSockFd, TCIFLUSH);
283 tcsetattr(*iSockFd, TCSANOW, &tio);
284 fcntl(*iSockFd, F_SETFL, FNDELAY);
288 struct sockaddr_in serverAddr;
293 WSAStartup(MAKEWORD(2,0), &wsaData);
296 sockAddrSize =
sizeof(
struct sockaddr_in);
297 memset( (
char *)&serverAddr, 0, sockAddrSize);
298 serverAddr.sin_family = AF_INET;
300 serverAddr.sin_port = htons((
short)iPort);
303 serverAddr.sin_addr.S_un.S_addr = inet_addr(pIPStr);
305 serverAddr.sin_addr.s_addr = inet_addr(pIPStr);
310 #ifdef BCAP_CONNECTION_UDP 311 memcpy( (
char *)&m_sockServerAddr, (
char *)&serverAddr, sockAddrSize);
313 if( (*iSockFd = socket( AF_INET, SOCK_DGRAM, 0)) == -1) {
314 perror(
"Fail.. Function:socket(UDP) in bCap_Open()");
322 if (setsockopt(*iSockFd, SOL_SOCKET, SO_RCVTIMEO,&tv,
sizeof(tv)) < 0) {
328 if( (*iSockFd = socket( AF_INET, SOCK_STREAM, 0)) == -1) {
329 perror(
"Fail.. Function:socket(TCP) in bCap_Open()");
335 iResult = connect(*iSockFd, (
struct sockaddr *)&serverAddr,
sizeof(serverAddr));
363 #ifdef BCAP_CONNECTION_COM 365 CloseHandle((HANDLE)iSockFd);
373 closesocket(iSockFd);
374 if (WSACleanup() == 0){
403 if (pPacket != NULL){
407 if (pRecPacket != NULL){
435 if (pPacket != NULL){
439 if (pRecPacket != NULL){
472 u_int *plhController){
480 if (pPacket != NULL){
516 if (pRecPacket != NULL){
550 if (pSndPacket != NULL){
561 if (pRecPacket != NULL){
598 if (pSndPacket != NULL){
622 if (pRecPacket != NULL){
663 if (pSndPacket != NULL){
693 if (pRecPacket != NULL){
696 if (pRecPacket->
iArgs >= 1){
734 if (pSndPacket != NULL){
764 if (pRecPacket != NULL){
767 if (pRecPacket->
iArgs >= 1){
808 if (pSndPacket != NULL){
838 if (pRecPacket != NULL){
875 if (pSndPacket != NULL){
895 if (pVntOption != NULL){
908 if (pRecPacket != NULL){
911 if (pRecPacket->
iArgs >= 1){
945 if (pSndPacket != NULL){
956 if (pRecPacket != NULL){
990 if (pSndPacket != NULL){
1020 if (pRecPacket != NULL){
1023 if (pRecPacket->
iArgs >= 1){
1062 if (pSndPacket != NULL){
1092 if (pRecPacket != NULL){
1129 if (pSndPacket != NULL){
1149 if (pVntOption != NULL){
1162 if (pRecPacket != NULL){
1165 if (pRecPacket->
iArgs >= 1){
1202 if (pSndPacket != NULL){
1224 if (pRecPacket != NULL){
1260 if (pSndPacket != NULL){
1298 if (pRecPacket != NULL){
1335 if (pSndPacket != NULL){
1364 if (pRecPacket != NULL){
1398 if (pSndPacket != NULL){
1409 if (pRecPacket != NULL){
1442 if (pSndPacket != NULL){
1472 if (pRecPacket != NULL){
1475 if (pRecPacket->
iArgs >= 1){
1511 if (pSndPacket != NULL){
1542 if (pRecPacket != NULL){
1577 if (pSndPacket != NULL){
1608 if (pRecPacket != NULL){
1642 if (pSndPacket != NULL){
1653 if (pRecPacket != NULL){
1684 if (pSndPacket != NULL){
1695 if (pRecPacket != NULL){
1698 if (pRecPacket->
iArgs >= 1){
1735 if (pSndPacket != NULL){
1769 if (pDstData != NULL){
1770 u_char *pDstPtr = pDstData;
1778 pArg =
Arg_Create( iType, lArrays, lDataLen, pDstData);
1795 if (pRecPacket != NULL){
1828 int retry_count = 0;
1830 if ( (pRecPacket != NULL) && (pSndPacket != NULL) ){
1831 struct timespec prev_time;
1832 clock_gettime(CLOCK_MONOTONIC, &prev_time);
1834 struct timespec send_after_time;
1835 clock_gettime(CLOCK_MONOTONIC, &send_after_time);
1838 struct timespec recv_after_time;
1839 clock_gettime(CLOCK_MONOTONIC, &recv_after_time);
1840 const int NSEC_PER_SECOND = 1e+9;
1856 else if(pRec != NULL){
1896 u_char *pPacketBuff = NULL;
1903 #ifdef BCAP_CONNECTION_UDP 1905 struct sockaddr_in serverAddr;
1915 while (lRecvSize < lHeaderLen) {
1916 #if BCAP_CONNECTION_COM 1918 ReadFile((HANDLE)iSockFd, (
char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize, (LPDWORD)&lRecLen, NULL);
1920 lRecLen = read(iSockFd, (
char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize);
1923 #ifdef BCAP_CONNECTION_UDP 1924 fromlen =
sizeof(serverAddr);
1925 memset( (
char *)&serverAddr, 0,
sizeof(
struct sockaddr_in));
1927 lRecLen = recvfrom(iSockFd, (
char *)&(pRcvBuffer[lRecvSize]),
LOCALRECBUFFER_SZ - lRecvSize, 0, (
struct sockaddr *)&serverAddr, &fromlen);
1934 if ((serverAddr.sin_addr.s_addr != m_sockServerAddr.sin_addr.s_addr) || (serverAddr.sin_port != m_sockServerAddr.sin_port)) {
1938 lRecLen = recv(iSockFd, (
char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize, 0);
1944 lRecvSize += lRecLen;
1946 pTop = (
u_char *)memchr((
const void *)pRcvBuffer,
BCAP_SOH, lRecvSize);
1951 if (pTop != pRcvBuffer){
1952 lRecvSize = lRecvSize - (pTop - pRcvBuffer);
1953 memmove (pRcvBuffer, pTop, lRecvSize);
1964 lRemainSize = lPacketSize - lRecvSize;
1966 pPacketBuff = (
unsigned char *)
bMalloc(lPacketSize);
1967 if (pPacketBuff != NULL){
1968 memcpy(pPacketBuff, pRcvBuffer, lRecvSize);
1969 pRemainData = pPacketBuff + lRecvSize;
1976 while (lRecvSize < lRemainSize) {
1977 #if BCAP_CONNECTION_COM 1980 ReadFile((HANDLE)iSockFd, (
char *)&(pRemainData[lRecvSize]), lRemainSize - lRecvSize, (LPDWORD)&lRecLen, NULL);
1982 lRecLen = read(iSockFd, (
char *)&(pRcvBuffer[lRecvSize]), lHeaderLen - lRecvSize);
1986 #ifdef BCAP_CONNECTION_UDP 1987 fromlen =
sizeof(serverAddr);
1988 lRecLen = recvfrom(iSockFd, (
char *)&(pRemainData[lRecvSize]), lRemainSize -lRecvSize , 0, (
struct sockaddr *)&serverAddr, &fromlen);
1990 if ((serverAddr.sin_addr.s_addr != m_sockServerAddr.sin_addr.s_addr) || (serverAddr.sin_port != m_sockServerAddr.sin_port)) {
1995 lRecLen = recv(iSockFd, (
char *)&(pRemainData[lRecvSize]), lRemainSize -lRecvSize , 0);
2002 lRecvSize += lRecLen;
2006 if (pPacketBuff[lPacketSize - 1] !=
BCAP_EOT) {
2015 if (pPacketBuff != NULL) {
2045 #if BCAP_CONNECTION_COM 2048 WriteFile((HANDLE)iSockFd, pBuff, iLen, (LPDWORD)&iSent, NULL);
2050 iSent = write(iSockFd, pBuff, iLen);
2054 #ifdef BCAP_CONNECTION_UDP 2055 iSent = sendto(iSockFd, (
char *)pBuff, iLen, 0, (
struct sockaddr *)&m_sockServerAddr,
sizeof(m_sockServerAddr));
2057 iSent = send(iSockFd, (
char *)pBuff, iLen, 0);
2086 if (pbSendData != NULL){
2113 if (pPacket != NULL){
2115 pArg = pPacket->
pArg;
2116 pLastArg = &(pPacket->
pArg);
2120 while(pArg != NULL){
2127 if (pLastArg != NULL){
2150 if ( (pPacket != NULL) && (pNewArg != NULL)) {
2152 if (pLastArg == NULL){
2159 *pLastArg = pNewArg;
2189 if (pPacket != NULL){
2211 if (pNewPacket != NULL){
2213 pNewPacket->
iArgs = 0;
2216 pNewPacket->
lFuncID = lFuncID;
2219 #if BCAP_CONNECTION_COM 2222 pNewPacket->
pArg = NULL;
2250 pDstPtr = (
u_char *)pDstBinData;
2267 pArgPtr = pSrcPacket->
pArg;
2269 for (i =0 ; i < pSrcPacket->
iArgs ; i++){
2270 if (pArgPtr != NULL){
2278 for (j= 0; j < pArgPtr->
lArrays; j++){
2292 pDstPtr +=
copyValue( pDstPtr, pbValue, lValueSize);
2293 pbValue += lValueSize;
2304 pDstPtr +=
copyValue( pDstPtr, pbValue, lStrLen);
2331 #if BCAP_CONNECTION_COM 2336 for (dwPos = 1; dwPos < pSrcPacket->
lMsgLength - 3; dwPos++) {
2337 crc ^= (((
u_char *)pDstBinData)[dwPos] << 8);
2338 for (cnt = 0; cnt < 8; cnt++) {
2340 crc = (crc << 1) ^ 0x1021;
2347 pDstPtr +=
copyValue( pDstPtr, &crc,
sizeof(crc));
2379 pSrcPtr = (
u_char *)pSrcBinData;
2391 pSrcPtr +=
copyValue( &iArgs , pSrcPtr,
sizeof(pDstPacket->
iArgs));
2397 pArgPtr = pDstPacket->
pArg;
2399 for (i = 0 ; i < iArgs ; i++){
2405 pSrcPtr +=
copyValue( &lLength, pSrcPtr,
sizeof(lLength));
2406 pSrcPtr +=
copyValue( &iType, pSrcPtr,
sizeof(iType));
2407 pSrcPtr +=
copyValue( &lArrays, pSrcPtr,
sizeof(lArrays));
2411 pArgPtr =
Arg_Create(iType, lArrays, lDataSize, pSrcPtr);
2412 if (pArgPtr != NULL){
2413 pSrcPtr = pSrcPtr + lDataSize;
2429 #if BCAP_CONNECTION_COM 2433 pSrcPtr +=
copyValue(&iCRC, pSrcPtr,
sizeof(iCRC));
2460 if (pRecPacket != NULL){
2461 if (pRecPacket->
iArgs >= 1){
2473 for (i = 0;i < pArgValue->
lArrays;i++){
2485 for (i = 0;i < pArgValue->
lArrays;i++){
2522 if (pNewArg != NULL){
2527 pNewArg->
data = NULL;
2530 if (pNewArg->
data != NULL ){
2532 memcpy(pNewArg->
data, data, lDataSize);
2564 while (pArg != NULL){
2569 if (pArg->
data != NULL){
2592 if (vntValue.
Arrays > 0) {
2600 pData = (
char *)(&vntValue.
Value);
2601 for (i =0; i < vntValue.
Arrays; i++){
2602 lStrLen = (
u_int)strlen(pData);
2631 u_int lValueSize = 0;
2690 u_int lStrLen,lLen2;
2693 lStrLen = strlen((
const char *)pbSrc);
2694 lLen2 = lStrLen * 2;
2699 for (i = 0;i < lStrLen; i++){
2700 *pbDst++ = *pbSrc++;
2720 u_int lStrLen,lLen2;
2726 lLen2 = lStrLen / 2;
2729 for (i = 0;i < lLen2; i++){
2758 if ((pVntDst != NULL) && (pArg != NULL)){
2770 for (i = 0;i < pArg->
lArrays;i++){
2782 for (i = 0;i < pArg->
lArrays;i++){
2847 #if defined(__BIG_ENDIAN__) 2858 pbSrc = (
u_char *)(pVntValue) + lLength -1;
2861 for (i = 0 ; i <
lLength ; i++){
2862 *pbDst++ = *pbSrc--;
2867 memcpy(pDst, pVntValue, lLength);
2889 pPtr = malloc(size);
#define BCAP_FUNC_Robot_Move
#define BCAP_FUNC_Controller_Execute
BCAP_HRESULT bCap_VariableRelease(int iSockFd, u_int lhVar)
#define BCAP_SIZE_ARGARRAYS
b-CAP client library header
static BCAP_HRESULT Packet_Send(int iSd, BCAP_PACKET *pPacket)
BCAP_HRESULT bCap_ControllerExecute(int iSockFd, u_int lhController, char *pStrCommand, char *pStrOption, void *pResult)
#define BCAP_FUNC_Service_Start
#define BCAP_FUNC_Task_Release
struct BCAP_PACKET BCAP_PACKET
BCAP_HRESULT bCap_Close(int iSockFd)
#define BCAP_FUNC_Controller_GetRobot
BCAP_HRESULT
BCAP_HRESULT values.
BCAP_HRESULT bCap_RobotMove(int iSockFd, u_int lhRobot, long lComp, char *pStrPose, char *pStrOption)
#define BCAP_FUNC_Variable_GetValue
static u_short m_iSerialNo
#define BCAP_FUNC_Robot_Change
static void Packet_Release(BCAP_PACKET *pPacket)
static u_int copyFromBSTR(void *pDstAsciiPtr, void *pSrcBstrPtr)
#define BCAP_SIZE_RESERVE
BCAP_HRESULT bCap_TaskStart(int iSockFd, u_int lhTask, long lMode, char *pStrOption)
BCAP_HRESULT bCap_VariablePutValue(int iSockFd, u_int lhVar, u_short iType, u_int lArrays, void *pVntValue)
int failed_to_send_packet
#define BCAP_FUNC_Controller_GetTask
struct BCAP_ARG * pNextArg
static BCAP_HRESULT Packet_Serialize(BCAP_PACKET *pPacket, void *pBinData)
static u_int sizeOfVarType(u_short iType)
BCAP_HRESULT bCap_ControllerDisconnect(int iSockFd, u_int lhController)
union BCAP_VARIANT::@0 Value
static BCAP_ARG * Arg_Create(u_short iType, u_int lArrays, u_int lLength, void *data)
void shutdown(boost::shared_ptr< DensoRobotHW > robot, boost::shared_ptr< ros::AsyncSpinner > spinner)
static void * bMalloc(size_t size)
static BCAP_PACKET * Packet_Create(u_int lFuncID)
static u_int copyVariantFromArg(BCAP_VARIANT *pVntDst, BCAP_ARG *pArg)
static u_int sizeOfVariant(BCAP_VARIANT vntValue)
BCAP_HRESULT bCap_ControllerConnect(int iSockFd, char *pStrCtrlname, char *pStrProvName, char *pStrPcName, char *pStrOption, u_int *plhController)
#define BCAP_FUNC_Robot_GetVariable
static BCAP_HRESULT Packet_AddArg(BCAP_PACKET *pPacket, BCAP_ARG *pNewArg)
#define BCAP_FUNC_Robot_Execute
#define BCAP_FUNC_Controller_Disconnect
BCAP_HRESULT bCap_RobotRelease(int iSockFd, u_int lhRobot)
#define BCAP_FUNC_Robot_Release
static u_int copyValue(void *pDst, void *pVntValue, u_int lLength)
#define BCAP_FUNC_Service_Stop
#define BCAP_FUNC_Task_GetVariable
BCAP_HRESULT bCap_ServiceStop(int iSockFd)
BCAP_HRESULT bCap_ControllerGetTask(int iSockFd, u_int lhController, char *pTskName, char *pstrOption, u_int *plhVar)
BCAP_HRESULT bCap_ServiceStart(int iSockFd)
#define SUCCEEDED(Status)
BCAP_HRESULT bCap_TaskRelease(int iSockFd, u_int lhTask)
BCAP_HRESULT bCap_TaskStop(int iSockFd, u_int lhTask, long lMode, char *pStrOption)
BCAP_HRESULT bCap_ControllerExecute2(int iSockFd, u_int lhController, char *pStrCommand, BCAP_VARIANT *pVntOption, BCAP_VARIANT *pVntResult)
#define BCAP_FUNC_Controller_GetVariable
#define BCAP_FUNC_Task_Start
BCAP_HRESULT bCap_RobotExecute(int iSockFd, u_int lhRobot, char *pStrCommand, char *pStrOption, void *pResult)
#define BCAP_MAX_ARG_SIZE
BCAP_HRESULT bCap_RobotExecuteSlaveMove(int iSockFd, u_int lhRobot, char *pStrCommand, float *pfOption, void *pResult)
static long m_lAllocCount
static void bFree(void *pPtr)
#define BCAP_FUNC_Controller_Connect
static u_int copyToBSTR(void *pbDstPtr, void *pbSrcPtr)
#define BCAP_SIZE_ARGTYPE
static BCAP_ARG ** Packet_GetLastArgHandle(BCAP_PACKET *pPacket)
BCAP_HRESULT bCap_VariableGetValue(int iSockFd, u_int lhVar, void *pVntValue)
BCAP_HRESULT bCap_Open(const char *pIPStr, int iPort, int *iSockFd)
BCAP_HRESULT bCap_ControllerGetRobot(int iSockFd, u_int lhController, char *pStrRobotName, char *pStrOption, u_int *plhRobot)
BCAP_HRESULT bCap_RobotExecute2(int iSockFd, u_int lhRobot, char *pStrCommand, BCAP_VARIANT *pVntOption, BCAP_VARIANT *pVntResult)
BCAP_HRESULT bCap_RobotChange(int iSockFd, u_int lhRobot, char *pStrCommand)
static BCAP_HRESULT Packet_GetResult(BCAP_PACKET *pRecPacket, void *pResult)
#define BCAP_SIZE_ARGSTRLEN
BCAP_HRESULT bCap_TaskGetVariable(int iSockFd, u_int lhTask, char *pVarName, char *pstrOption, u_int *plhVar)
BCAP_HRESULT bCap_RobotGetVariable(int iSockFd, u_int lhRobot, char *pVarName, char *pstrOption, u_int *plhVar)
static BCAP_HRESULT Packet_Deserialize(void *pBinData, BCAP_PACKET *pPacket)
static BCAP_HRESULT bCapSendAndRec(int iSockFd, BCAP_PACKET *pSndPacket, BCAP_PACKET *pRecPacket)
BCAP_HRESULT bCap_ControllerGetVariable(int iSockFd, u_int lhController, char *pVarName, char *pstrOption, u_int *plhVar)
#define BCAP_FUNC_Variable_PutValue
#define BCAP_FUNC_Task_Stop
#define LOCALRECBUFFER_SZ
static BCAP_HRESULT sendBinary(int iSockFd, u_char *pBuff, u_int lSize)
static u_char * receivePacket(int iSockFd, int *retryp)
static void Arg_Release(BCAP_ARG *pArg)
#define BCAP_FUNC_Variable_Release