📄 irtransport.c
字号:
** Initiate a connection to a remote peer transport.** When a connection has been created, the passed 'connectionid' is set. This will be** provided by the caller on all subsuquent calls made against the active connection.*/ObxRc iobxIrTransportConnect( void **connectionid ) { ObxRc rc = OBX_RC_OK; ObxIrConnectionBlock *conblk = NULL; OBXDBGFLOW(("iobxIrTransportConnect() entry, connid=0x%08x\n", *connectionid)); if ( *connectionid ) { conblk = *connectionid; if ( conblk->connected ) { sclose( conblk->fd ); } conblk->fd = socket( AF_IRDA, SOCK_STREAM, 0 ); if ( conblk->fd >= 0 ) { memset( &conblk->peer, 0, sizeof(struct sockaddr_irda) ); conblk->peer.sir_family = AF_IRDA; strncpy(conblk->peer.sir_name, obxMeta.service, sizeof(conblk->peer.sir_name) ); if ( (rc=iobxIrDiscoverDevices( conblk )) == OBX_RC_OK ) { if ( connect( conblk->fd, (struct sockaddr *)&conblk->peer, sizeof(struct sockaddr_irda)) >= 0 ) { OBXDBGINFO(("iobxIrTransportConnect() connect() succeeds.\n")); conblk->connected = TRUE; } else { OBXDBGERR(("[ERROR] iobxIrTransportConnect() socket connect fails.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET_CONNECT; } } else { OBXDBGERR(("[ERROR] iobxIrTransportConnect() unexpected rc from iobxIrDiscoverDevices(), rc=%d.\n", rc)); } } else { OBXDBGERR(("[ERROR] iobxIrTransportConnect() socket create fails.\n")); rc = OBX_RC_ERR_SOCKET_CREATE; } } else { OBXDBGERR(("[ERROR] iobxIrTransportConnect() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* no con block */ } return rc;}/* ************************************** *//* Functions used on connected transports *//* ************************************** *//*** Send 'length' bytes of data from 'buf', set the actual number** written in 'wrote'.** Note that the inbound 'connectionid' was created by either a connect() or accept() call.*/ObxRc iobxIrTransportSend( void **connectionid, const void *buf, int length, int *wrote, short allowShort ) { ObxRc rc = OBX_RC_OK; ObxIrConnectionBlock *conblk = NULL; int remainingSend = length; void *cursor; int sent; OBXDBGFLOW(("iobxIrTransportSend() entry, connid=0x%08x\tlength=%d\tallowShort=%d\n", *connectionid, length, allowShort)); OBXDBGMEM(("iobxIrTransportSend()", buf, length)); *wrote = 0; if ( *connectionid ) { conblk = *connectionid; cursor = (void *)buf; do { sent = swrite( conblk->fd, cursor, remainingSend ); if ( sent != SOCKET_ERROR ) { OBXDBGINFO(("iobxIrTransportSend() socket send, %d bytes sent.\n", sent)); remainingSend -= sent; (unsigned char *)cursor += sent; /* Treat as bytes */ *wrote += sent; } else { OBXDBGERR(("[ERROR] iobxIrTransportSend() socket send resulted in SOCKET_ERROR.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET; } } while ( allowShort == FALSE && remainingSend > 0 && rc == OBX_RC_OK ); } else { OBXDBGERR(("[ERROR] iobxIrTransportSend() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* No con block */ } return rc;}/*** Receive 'length' bytes of data and place into 'buf', set the actual** number of bytes read in 'actual'.** Note that the inbound 'connectionid' was created by either a connect() or accept() call.*/ObxRc iobxIrTransportRecv( void **connectionid, void *buf, int length, int *actual, short allowShort ) { ObxRc rc = OBX_RC_OK; ObxIrConnectionBlock *conblk = NULL; int remainingRead = length; void *cursor; int iread; OBXDBGFLOW(("iobxIrTransportRecv() entry, connid=0x%08x\tlength=%d\n", *connectionid, length)); *actual = 0; if ( *connectionid ) { conblk = *connectionid; cursor = buf; do { iread = sread( conblk->fd, cursor, remainingRead ); switch ( iread ) { case SOCKET_ERROR: OBXDBGERR(("[ERROR] iobxIrTransportRecv() socket send resulted in SOCKET_ERROR.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET; break; case 0: OBXDBGINFO(("iobxIrTransportRecv() socket read results in EOF.\n")); rc = OBX_RC_ERR_SOCKET_EOF; break; default: OBXDBGINFO(("iobxIrTransportRecv() socket read, %d bytes read.\n", iread)); OBXDBGMEM(("iobxIrTransportRecv()", cursor, iread)); remainingRead -= iread; (unsigned char *)cursor += iread; /* Treat as bytes */ *actual += iread; break; } } while ( allowShort == FALSE && remainingRead > 0 && rc == OBX_RC_OK ); } else { OBXDBGERR(("[ERROR] iobxIrTransportRecv() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* No con block */ } return rc;}/*** Clean up all internals, subsuquent use of this 'connectionid' should result in an error.** Note that the inbound 'connectionid' was created by an open() call.** Passing in a null connectionid is ignored.*/ObxRc iobxIrTransportClose( void **connectionid ) { ObxIrConnectionBlock *conblk = NULL; OBXDBGFLOW(("iobxIrTransportClose() entry, connid=0x%08x\n", *connectionid)); if ( connectionid ) { conblk = *connectionid; if ( conblk ) { if ( conblk->connected ) { sclose( conblk->fd ); } free( conblk ); *connectionid = NULL; } } return OBX_RC_OK;}/*** Discover devices when attempting to connect.*/#ifdef _WIN32/*** Win32*/ObxRc iobxIrDiscoverDevices( ObxIrConnectionBlock *conblk ) { DEVICELIST list; unsigned int index; int len = sizeof( list ); int scanCount = 0; int maxScanSecs = 5; memset( &list, 0, sizeof( list ) ); OBXDBGFLOW(("iobxIrDiscoverDevices() entry, conblk=0x%08x.\n", conblk)); while ( list.numDevice == 0 && scanCount <= maxScanSecs ) { if ( getsockopt( conblk->fd, SOL_IRLMP, IRLMP_ENUMDEVICES, (char *)&list, &len) == SOCKET_ERROR ) { scanCount++; } } if (list.numDevice == 0) { OBXDBGERR(("[ERROR] iobxIrDiscoverDevices() No IR devices discovered.\n")); return OBX_RC_ERR_TRANSPORT; } else { OBXDBGINFO(("iobxIrDiscoverDevices() Discovered: %d device(s), as follows:\n", list.numDevice)); for ( index=0; index<list.numDevice; index++) { OBXDBGINFO(("Device: %d\tname:%s\tdaddr:0x%08x\n", index, list.Device[0].irdaDeviceName, list.Device[0].sir_addr)); } for ( index=0; index <= 3; index++ ) { conblk->peer.sir_addr[index] = list.Device[0].sir_addr[index]; } } return OBX_RC_OK;}#else/*** Non-Win32*/ObxRc iobxIrDiscoverDevices( ObxIrConnectionBlock *conblk ) { struct irda_device_list *list; unsigned char *buf; int len; int i; OBXDBGFLOW(("iobxIrDiscoverDevices() entry, conblk=0x%08x.\n", conblk)); len = sizeof(struct irda_device_list) - sizeof(struct irda_device_info) + sizeof(struct irda_device_info) * MAX_DEVICES; buf = malloc( len ); OBXDBGBUF(("iobxIrDiscoverDevices() malloc, addr=0x%08x, len=%d.\n", buf, len )); list = (struct irda_device_list *) buf; if ( getsockopt( conblk->fd, SOL_IRLMP, IRLMP_ENUMDEVICES, buf, &len ) ) { free( buf ); return OBX_RC_ERR_TRANSPORT; } if ( len > 0 ) { OBXDBGINFO(("iobxIrDiscoverDevices() Discovered: %d device(s), as follows:\n", list->len)); for (i=0; i<list->len; i++) { OBXDBGINFO(("Device: %d\tname:%s\n\tdaddr:0x%08x\tsaddr:0x%08x\n", i, list->dev[i].info, list->dev[i].daddr, list->dev[i].saddr )); } for (i=0;i<list->len;i++) { OBXDBGINFO((" name: %s\n", list->dev[i].info)); OBXDBGINFO((" daddr: 0x%08x\n", list->dev[i].daddr)); OBXDBGINFO((" saddr: 0x%08x\n", list->dev[i].saddr)); OBXDBGINFO(("\n")); conblk->peer.sir_addr = list->dev[i].daddr; free( buf ); return OBX_RC_OK; } } OBXDBGERR(("[ERROR] iobxIrDiscoverDevices() No IR devices discovered.\n")); return OBX_RC_ERR_TRANSPORT;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -