⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 obex_object.c

📁 这是Linux环境下的openobex
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************* *                 * Filename:      obex_object.c * Version:       0.8 * Description:   OBEX object related functions * Status:        Experimental. * Author:        Dag Brattli <dagb@cs.uit.no> * Created at:    Fri Apr 23 14:04:29 1999 * CVS ID:        $Id: obex_object.c,v 1.23 2006/01/03 18:36:15 holtmann Exp $ * *     Copyright (c) 1999, 2000 Pontus Fuchs, All Rights Reserved. *     Copyright (c) 1999, 2000 Dag Brattli, All Rights Reserved. *      *     This library is free software; you can redistribute it and/or *     modify it under the terms of the GNU Lesser General Public *     License as published by the Free Software Foundation; either *     version 2 of the License, or (at your option) any later version. * *     This library is distributed in the hope that it will be useful, *     but WITHOUT ANY WARRANTY; without even the implied warranty of *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *     Lesser General Public License for more details. * *     You should have received a copy of the GNU Lesser General Public *     License along with this library; if not, write to the Free Software *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,  *     MA  02111-1307  USA     * ********************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <string.h>#include "obex_main.h"#include "obex_object.h"#include "obex_header.h"#include "obex_connect.h"/* * Function obex_object_new () * *    Create a new OBEX object * */obex_object_t *obex_object_new(void){	obex_object_t *object;	object =  malloc(sizeof(obex_object_t));	if (object == NULL)		return(NULL);	memset(object, 0, sizeof(obex_object_t));	obex_object_setrsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED);	return object;}/* * Function free_headerq(q) * *    Free all headers in a header queue. * */static inline void free_headerq(slist_t **q){	struct obex_header_element *h;		DEBUG(4, "\n");	while(*q != NULL) {	 	h = (*q)->data;		*q = slist_remove(*q, h);		g_netbuf_free(h->buf);		free(h);	}}/* * Function obex_object_delete (object) * *    Delete OBEX object * */int obex_object_delete(obex_object_t *object){	DEBUG(4, "\n");	obex_return_val_if_fail(object != NULL, -1);	/* Free the headerqueues */	free_headerq(&object->tx_headerq);	free_headerq(&object->rx_headerq);	free_headerq(&object->rx_headerq_rm);	/* Free tx and rx msgs */	g_netbuf_free(object->tx_nonhdr_data);	object->tx_nonhdr_data = NULL;	g_netbuf_free(object->rx_nonhdr_data);	object->rx_nonhdr_data = NULL;		g_netbuf_free(object->rx_body);	object->rx_body = NULL;		free(object);	return 0;}/* * Function obex_object_setcmd () * *    Set command of object * */int obex_object_setcmd(obex_object_t *object, uint8_t cmd, uint8_t lastcmd){	DEBUG(4,"%02x\n", cmd);	object->cmd = cmd;	object->opcode = cmd;	object->lastopcode = lastcmd;	return 1;}/* * Function obex_object_setrsp () * *    Set the response for an object * */int obex_object_setrsp(obex_object_t *object, uint8_t rsp, uint8_t lastrsp){	DEBUG(4,"\n");	object->opcode = rsp;	object->lastopcode = lastrsp;	return 1;}/* * Function int obex_object_addheader(obex_object_t *object, uint8_t hi,  *                      obex_headerdata_t hv, uint32_t hv_size, unsigned int flags) * *    Add a header to the TX-queue. * */int obex_object_addheader(obex_t *self, obex_object_t *object, uint8_t hi,	obex_headerdata_t hv, uint32_t hv_size, unsigned int flags){	int ret = -1;	struct obex_header_element *element;	unsigned int maxlen;	DEBUG(4, "\n");	/* End of stream marker */	if(flags & OBEX_FL_STREAM_DATAEND)	{		if(self->object == NULL)			return -1;		self->object->s_stop = TRUE;		self->object->s_buf = hv.bs;		self->object->s_len = hv_size;		return 1;	}	/* Stream data */	if(flags & OBEX_FL_STREAM_DATA)	{		if(self->object == NULL)			return -1;		self->object->s_buf = hv.bs;		self->object->s_len = hv_size;		return 1;	}		if(flags & OBEX_FL_FIT_ONE_PACKET)	{		/* In this command all headers must fit in one packet! */		DEBUG(3, "Fit one packet!\n");		maxlen = self->mtu_tx - object->totallen - sizeof(struct obex_common_hdr);	}	else	{		maxlen = self->mtu_tx - sizeof(struct obex_common_hdr);	}	element = malloc(sizeof(struct obex_header_element));	if(element == NULL)		return -1;	memset(element, 0, sizeof(struct obex_header_element));	element->hi = hi;		/* Is this a stream? */	if(flags & OBEX_FL_STREAM_START)	{		DEBUG(3, "Adding stream\n");		element->stream = TRUE;		object->tx_headerq = slist_append(object->tx_headerq, element);		return 1;	}		switch (hi & OBEX_HI_MASK) {	case OBEX_INT:		DEBUG(2, "4BQ header %d\n", hv.bq4);				element->buf = g_netbuf_new(sizeof(struct obex_uint_hdr));		if(element->buf) {			element->length = (unsigned int) sizeof(struct obex_uint_hdr);			ret = insert_uint_header(element->buf, hi, hv.bq4);		}		break;			case OBEX_BYTE:		DEBUG(2, "1BQ header %d\n", hv.bq1);		element->buf = g_netbuf_new(sizeof(struct obex_ubyte_hdr));		if(element->buf) {			element->length = sizeof(struct obex_ubyte_hdr);			ret = insert_ubyte_header(element->buf, hi, hv.bq1);		}		break;	case OBEX_BYTE_STREAM:		DEBUG(2, "BS  header size %d\n", hv_size);		element->buf = g_netbuf_new(hv_size + sizeof(struct obex_byte_stream_hdr) );		if(element->buf) {			element->length = hv_size + sizeof(struct obex_byte_stream_hdr);			ret = insert_byte_stream_header(element->buf, hi, hv.bs, hv_size);		}		break;		case OBEX_UNICODE:		DEBUG(2, "Unicode header size %d\n", hv_size);		element->buf = g_netbuf_new(hv_size + sizeof(struct obex_unicode_hdr) );		if(element->buf) {			element->length = hv_size + sizeof(struct obex_unicode_hdr);			ret = insert_unicode_header(element->buf, hi, hv.bs, hv_size);		}		break;		default:		DEBUG(2, "Unsupported encoding %02x\n", hi & OBEX_HI_MASK);		ret = -1;		break;	}	/* Check if you can send this header without violating MTU or OBEX_FIT_ONE_PACKET */	if( (element->hi != OBEX_HDR_BODY) || (flags & OBEX_FL_FIT_ONE_PACKET) )	{		if(maxlen < element->length)	{			DEBUG(0, "Header to big\n");			ret = -1;		}	}	if (ret > 0) {		object->totallen += ret;		object->tx_headerq = slist_append(object->tx_headerq, element);		ret = 1;	} else {		g_netbuf_free(element->buf);		free(element);	}	return ret;}/* * Function send_stream(object, header, txmsg, tx_left) * *  Send a streaming header. * */static int send_stream(obex_t *self,				struct obex_header_element *h,				GNetBuf *txmsg, unsigned int tx_left){	obex_object_t *object;	struct obex_byte_stream_hdr *body_txh;	int actual; 	/* Number of bytes sent in this fragment */	DEBUG(4, "\n");		object = self->object;		/* Fill in length and header type later, but reserve space for it */	body_txh  = (struct obex_byte_stream_hdr*) g_netbuf_put(txmsg,				sizeof(struct obex_byte_stream_hdr) );	tx_left -= sizeof(struct obex_byte_stream_hdr);	actual = sizeof(struct obex_byte_stream_hdr);		do {		if(object->s_len == 0) {			/* Ask app for more data if no more */			object->s_offset = 0;			object->s_buf = NULL;			obex_deliver_event(self, OBEX_EV_STREAMEMPTY, 0, 0, FALSE);			DEBUG(4, "s_len=%d, s_stop = %d\n",						object->s_len, object->s_stop);			/* End of stream ?*/			if(object->s_stop)				break;			/* User suspended and didn't provide any new data */			if (object->suspend && object->s_buf == NULL)				break;			/* Error ?*/			if(object->s_buf == NULL) {				DEBUG(1, "Unexpected end-of-stream\n");				return -1;			}		}				if(tx_left < object->s_len) {			/*燭here is more data left in buffer than tx_left */			DEBUG(4, "More data than tx_left. Buffer will not be empty\n");						g_netbuf_put_data(txmsg, (uint8_t*) object->s_buf + object->s_offset, tx_left);			object->s_len -= tx_left;			object->s_offset += tx_left;			actual += tx_left;			tx_left = 0;		}		else {			/* There less data in buffer than tx_left */			DEBUG(4, "Less data that tx_left. Buffer will be empty\n");			g_netbuf_put_data(txmsg, (uint8_t*) object->s_buf + object->s_offset, object->s_len);			tx_left -= object->s_len;			object->s_offset += object->s_len;			actual += object->s_len;			object->s_len = 0;			if (object->suspend)				tx_left = 0;		}	} while(tx_left > 0);		DEBUG(4, "txmsg full or no more stream-data. actual = %d\n", actual);	body_txh->hi = OBEX_HDR_BODY;		if(object->s_stop && object->s_len == 0) {		/* We are done. Remove header from tx-queue */		object->tx_headerq = slist_remove(object->tx_headerq, h);		body_txh->hi = OBEX_HDR_BODY_END;		g_netbuf_free(h->buf);		free(h);	}		body_txh->hl = htons((uint16_t)actual);	return actual;}/* * Function send_body(object, header, txmsg, tx_left) * *  Fragment and send the body * */static int send_body(obex_object_t *object,				struct obex_header_element *h,				GNetBuf *txmsg, unsigned int tx_left){	struct obex_byte_stream_hdr *body_txh;	unsigned int actual;        			body_txh = (struct obex_byte_stream_hdr*) txmsg->tail;				if(!h->body_touched) {		/* This is the first time we try to send this header		   obex_object_addheaders has added a struct_byte_stream_hdr		   before the actual body-data. We shall send this in every fragment		   so we just remove it for now.*/		g_netbuf_pull(h->buf,  sizeof(struct obex_byte_stream_hdr) );		h->body_touched = TRUE;	}			if(tx_left < ( h->buf->len +			sizeof(struct obex_byte_stream_hdr) ) )	{		DEBUG(4, "Add BODY header\n");		body_txh->hi = OBEX_HDR_BODY;		body_txh->hl = htons((uint16_t)tx_left);		g_netbuf_put(txmsg, sizeof(struct obex_byte_stream_hdr) );		g_netbuf_put_data(txmsg, h->buf->data, tx_left				- sizeof(struct obex_byte_stream_hdr) );		g_netbuf_pull(h->buf, tx_left				- sizeof(struct obex_byte_stream_hdr) );		/* We have completely filled the tx-buffer */		actual = tx_left;	}	else	{		DEBUG(4, "Add BODY_END header\n");		body_txh->hi = OBEX_HDR_BODY_END;		body_txh->hl = htons((uint16_t) (h->buf->len + sizeof(struct obex_byte_stream_hdr)));		g_netbuf_put(txmsg, sizeof(struct obex_byte_stream_hdr) );		g_netbuf_put_data(txmsg, h->buf->data, h->buf->len);		actual = h->buf->len;				object->tx_headerq = slist_remove(object->tx_headerq, h);		g_netbuf_free(h->buf);		free(h);	}	return actual;}/* * Function obex_object_send() * *    Send away all headers attached to an object. Returns: *       1 on sucessfully done *       0 on progress made *     < 0 on error */int obex_object_send(obex_t *self, obex_object_t *object,		      int allowfinalcmd, int forcefinalbit){	struct obex_header_element *h;	GNetBuf *txmsg;	int actual, finished = 0;	uint16_t tx_left;	int addmore = TRUE;	int real_opcode;	DEBUG(4, "\n");	/* Calc how many bytes of headers we can fit in this package */	tx_left = self->mtu_tx - sizeof(struct obex_common_hdr);	switch(self->trans.type)	{#ifdef HAVE_IRDA	case OBEX_TRANS_IRDA:		if(self->trans.mtu > 0 && self->mtu_tx > self->trans.mtu)		{			tx_left -= self->mtu_tx%self->trans.mtu;		}		break;#endif /*HAVE_IRDA*/	default:		break;	}	/* Reuse transmit buffer */	txmsg = g_netbuf_recycle(self->tx_msg);		/* Reserve space for common header */	g_netbuf_reserve(txmsg, sizeof(obex_common_hdr_t));	/* Add nonheader-data first if any (SETPATH, CONNECT)*/	if(object->tx_nonhdr_data) {		DEBUG(4, "Adding %d bytes of non-headerdata\n", object->tx_nonhdr_data->len);		g_netbuf_put_data(txmsg, object->tx_nonhdr_data->data, object->tx_nonhdr_data->len);				g_netbuf_free(object->tx_nonhdr_data);		object->tx_nonhdr_data = NULL;	}	DEBUG(4, "4\n");		/* Take headers from the tx queue and try to stuff as

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -