📄 obex.c
字号:
} } else { OBXDBGERR(("[ERROR] ObxStreamingRecv() stream state was not STREAM_UNOPEN!\n")); rc = OBX_RC_ERR_INAPPROPRIATE; } } else { OBXDBGERR(("[ERROR] ObxStreamingRecv() Bad plist on call\n")); rc = OBX_RC_ERR_BADPLIST; } return rc;}OBEX_EXPORT ObxRc ObxStreamSend( ObxStream *stream, unsigned char *data, int len ) { ObxRc rc = OBX_RC_OK; OBXDBGFLOW(("ObxStreamSend() entry, stream=0x%08x\tlen=%d\n",stream, len)); if ( stream ) { /* ** Must be in SENDING state. */ if ( stream->state == STREAM_SENDING ) { /* ** Append the new data to any existing data. ** If it's over the max packet length, auto flush for the user. */ iobxBufWrite( stream->data, data, len ); if ( iobxBufSize( stream->data ) > stream->handle->maxPacketLen ) { if ( (rc=ObxStreamFlush( stream )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamSend() unexpected rc from ObxStreamFlush()\n")); } } } else { OBXDBGERR(("[ERROR] ObxStreamSend() stream state was not STREAM_SENDING!\n")); rc = OBX_RC_ERR_INAPPROPRIATE; } } else { OBXDBGERR(("[ERROR] ObxStreamSend() Bad plist on call\n")); rc = OBX_RC_ERR_BADPLIST; } return rc;}OBEX_EXPORT ObxRc ObxStreamRecv( ObxStream *stream, ObxList *headerList ) { ObxRc rc = OBX_RC_OK; ObxIterator *obxiterator; OBXDBGFLOW(("ObxStreamRecv() entry, stream=0x%08x\theaderList=0x%08x\n", stream, headerList)); if ( stream && headerList ) { /* ** Must be in RECEIVING mode... */ if ( stream->state == STREAM_RECEIVING ) { /* ** Reset the request object. We release any headers contained within, since ** it can only be our buffer, otherwise, it would be free'ed during the reset. */ iobxListRelease( iobxObjectGetHeaderList( stream->request ) ); if ( (rc=iobxObjectReset( stream->request )) == OBX_RC_OK ) { stream->request->stream = TRUE; /* ** Receive a block of headers from the peer. */ if ( (rc=ObxTransactionRecv( stream->handle, stream->request, FALSE )) == OBX_RC_OK ) { /* ** Hand over all the headers received to passed list. */ if ( (obxiterator=iobxListGetIterator( iobxObjectGetHeaderList( stream->request ) )) ) { iobxIteratorReset( obxiterator ); while ( iobxIteratorHasNext( obxiterator ) ) { iobxListAppend( headerList, iobxIteratorNext( obxiterator ) ); } /* ** Remove from request list, don't free since we're giving them to 'headerList'. */ iobxListRelease( iobxObjectGetHeaderList( stream->request ) ); iobxIteratorFree( obxiterator ); } else { OBXDBGERR(("[ERROR] ObxStreamRecv() unable to get iterator\n")); rc = OBX_RC_ERR_UNSPECIFIED; } /* ** Peer done sending to us? */ if ( stream->request->cmd & OBEX_CMD_FINAL ) { OBXDBGINFO(("ObxStreamRecv() success, 'FINAL' from peer detected.\n")); stream->state = STREAM_FINISHED; rc = OBX_RC_STREAM_EOF; } else { OBXDBGINFO(("ObxStreamRecv() success, but more data from peer expected.\n")); } } else { OBXDBGERR(("[ERROR] ObxStreamRecv() unexpected rc calling ObxTransactionRecv().\n")); } } else { OBXDBGERR(("[ERROR] ObxStreamRecv() unexpected rc on call to iobxObjectReset()\n")); } } else if ( stream->state == STREAM_FINISHED ) { OBXDBGINFO(("[WARNING] ObxStreamRecv() read after EOF detected.\n")); rc = OBX_RC_STREAM_EOF; } else { OBXDBGERR(("[ERROR] ObxStreamRecv() stream state was not STREAM_RECEIVING or STREAM_FINISHED!\n")); rc = OBX_RC_ERR_INAPPROPRIATE; } } else { OBXDBGERR(("[ERROR] ObxStreamRecv() Bad plist on call\n")); rc = OBX_RC_ERR_BADPLIST; } return rc;}OBEX_EXPORT ObxRc ObxStreamFlush( ObxStream *stream ) { ObxRc rc = OBX_RC_OK; ObxHeader *header; ObxObject *response; OBXDBGFLOW(("ObxStreamFlush() entry, stream=0x%08x\n", stream)); if ( stream ) { /* ** State ok? */ if ( stream->state == STREAM_SENDING ) { /* ** Any data to flush? */ if ( iobxBufSize( stream->data ) ) { /* ** Reset the request object. We release any headers contained within, since ** it can only be our buffer, otherwise, it would be free'ed during the reset. */ iobxListRelease( iobxObjectGetHeaderList( stream->request ) ); if ( (rc=iobxObjectReset( stream->request )) == OBX_RC_OK ) { /* ** Reset the stream object and create a local response object. */ stream->request->stream = TRUE; stream->request->cmd = stream->cmd; response = iobxObjectNew(); /* ** Create header of type BODY, set it's value, add the header to the request, send it. */ if ( !(header = iobxHeaderNew( OBEX_HEADER_BODY )) ) { OBXDBGERR(("[ERROR] ObxStreamFlush() Error creating header.\n")); rc = OBX_RC_ERR_MEMORY; } else if ( (rc=iobxHeaderSetByteSequenceValue( header, stream->data )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamFlush() unexpected rc calling iobxHeaderSetByteSequenceValue().\n")); } else if ( (rc=iobxObjectAddHeader( stream->request, header, stream->handle )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamFlush() unexpected rc calling iobxObjectAddHeader().\n")); } else if ( (rc= ObxTransactionSend( stream->handle, stream->request, response )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamFlush() unexpected rc calling ObxTransactionSend().\n")); } else { OBXDBGINFO(("ObxStreamFlush() success.\n")); /* ** We play games here, a bit, to avoid having to reallocate our stream->data buffer ** over and over. */ iobxHeaderSetByteSequenceValue( header, NULL ); iobxHeaderFree( header ); } /* ** Done with response. */ iobxObjectFree( response ); } else { OBXDBGERR(("[ERROR] ObxStreamFlush() unexpected rc on call to iobxObjectReset()\n")); } } else { OBXDBGINFO(("[WARNING] ObxStreamFlush() no data to flush, ignoring call.\n")); } } else { OBXDBGERR(("[ERROR] ObxStreamFlush() stream state was not STREAM_SENDING\n")); rc = OBX_RC_ERR_INAPPROPRIATE; } } else { OBXDBGERR(("[ERROR] ObxStreamFlush() Bad plist on call\n")); rc = OBX_RC_ERR_BADPLIST; } return rc;}OBEX_EXPORT ObxRc ObxStreamClose( ObxStream *stream ) { ObxRc rc = OBX_RC_OK; ObxHeader *header; ObxObject *response; OBXDBGFLOW(("ObxStreamClose() entry, stream=0x%08x\n", stream)); /* ** State ok? */ if ( stream->state != STREAM_CLOSED ) { /* ** Flush data, if writing. */ if ( stream->state == STREAM_SENDING ) { /* ** Flush any remaining data. */ if ( (rc=ObxStreamFlush( stream )) == OBX_RC_OK ) { /* ** Reset the request object. We release any headers contained within, since ** it can only be our buffer, otherwise, it would be free'ed during the reset. */ iobxListRelease( iobxObjectGetHeaderList( stream->request ) ); if ( (rc=iobxObjectReset( stream->request )) == OBX_RC_OK ) { /* ** Set the stream object up.. we're now closed. ** We clear the stream bit. This will cause the object send, (called by ** ObxTransactionSend()) to set the final bit when it transmits. */ stream->request->stream = FALSE; stream->request->cmd = stream->cmd; stream->state = STREAM_CLOSED; /* ** New up a response, create a header, BODY_END this time, set the value, flow it. */ response = iobxObjectNew(); if ( !(header = iobxHeaderNew( OBEX_HEADER_BODY_END )) ) { OBXDBGERR(("[ERROR] ObxStreamClose() Error creating header.\n")); rc = OBX_RC_ERR_MEMORY; } else if ( (rc=iobxHeaderSetByteSequenceValue( header, stream->data )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamClose() unexpected rc calling iobxHeaderSetByteSequenceValue().\n")); } else if ( (rc=iobxObjectAddHeader( stream->request, header, stream->handle )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamClose() unexpected rc calling iobxObjectAddHeader().\n")); } else if ( (rc= ObxTransactionSend( stream->handle, stream->request, response )) != OBX_RC_OK ) { OBXDBGERR(("[ERROR] ObxStreamClose() unexpected rc calling ObxTransactionSend().\n")); } else { OBXDBGINFO(("ObxStreamFlush() success.\n")); /* ** We play games here, a bit, to avoid having to reallocate our stream->data buffer ** over and over. */ iobxHeaderSetByteSequenceValue( header, NULL ); iobxHeaderFree( header ); } /* ** Done with response. */ iobxObjectFree( response ); } else { OBXDBGERR(("[ERROR] ObxStreamFlush() unexpected rc on call to iobxObjectReset()\n")); } } else { OBXDBGERR(("[ERROR] ObxStreamClose() unexpected rc from ObxStreamFlush()\n")); } } else { /* ** must be a receiving stream, not much to do. */ stream->state = STREAM_CLOSED; } } else { OBXDBGERR(("[ERROR] ObxStreamFlush() stream state already closed.\n")); rc = OBX_RC_ERR_INAPPROPRIATE; } return rc;}// ********************// T.K. for GET we need two new functions - not the transaction Send/Recv pair.// The scenario is like this:// The server has created an Obexobject and wait for the client to request this// object via get.// server: ObxGetSend// -> this waits for a client GET request and sends the object and stores the clients answer in Answer// client: ObxGetRecv// -> requests an Object from the server and stores it in getObj//We added the third parameter, mimeType, in this function to check if the received object is what we want by checking Mime Type.OBEX_EXPORT ObxRc ObxGetSend(ObxHandle *handle, ObxObject *getAnswer, char *mimeType) { short done = FALSE; ObxRc rc = OBX_RC_OK; ObxObject *request = NULL; ObxIterator *obxiterator = NULL; ObxList *obxlist = NULL; ObxHeader *obxheader = NULL; int typeHeaderCount=0; int connectionIDHeaderCount=0; int length; unsigned char *buffer; OBXDBGFLOW(("ObxGetSend() entry, handle=0x%08x\n"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -