Esempio n. 1
0
/*
 * Function obex_data_indication (self)
 *
 *    Read/Feed some input from device and find out which packet it is
 *
 */
int obex_data_indication(obex_t *self, uint8_t *buf, int buflen)
{
	obex_common_hdr_t *hdr;
	buf_t *msg;
	int final;
	int actual = 0;
	unsigned int size;
	int ret;
	
	DEBUG(4, "\n");

	obex_return_val_if_fail(self != NULL, -1);

	msg = self->rx_msg;

	if (self->trans.fmt == OBEX_MT_SEQPACKET) {
		actual = obex_transport_read(self, msg->data_avail, buf, buflen);
		
		DEBUG(4, "Got %d bytes\n", actual);
		
		if (buf == NULL && buflen == 0 && actual <= 0)	{
			obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
			return actual;
		}
		
		hdr = (obex_common_hdr_t *) msg->data;
		size = ntohs(hdr->len);
	} 
	else {
		/* First we need 3 bytes to be able to know how much data to read */
		if(msg->data_size < 3)  {
			actual = obex_transport_read(self, 3 - (msg->data_size), buf, buflen);
			DEBUG(4, "Got %d bytes\n", actual);

			/* Check if we are still connected */
			/* do not error if the data is from non-empty but partial buffer (custom transport) */
			if (buf == NULL && buflen == 0 && actual <= 0)	{
				obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
				return actual;
			}
			buf += actual;
			buflen -= actual;
		}

		/* If we have 3 bytes data we can decide how big the packet is */
		if(msg->data_size >= 3) {
			hdr = (obex_common_hdr_t *) msg->data;
			size = ntohs(hdr->len);

			actual = 0;
			if(msg->data_size < (int) ntohs(hdr->len)) {

				actual = obex_transport_read(self, size - msg->data_size, buf,
					buflen);
				/* hdr might not be valid anymore if the _read did a realloc */
				hdr = (obex_common_hdr_t *) msg->data;

				/* Check if we are still connected */
				/* do not error if the data is from non-empty but partial buffer (custom transport) */
				if (buf == NULL && buflen == 0 && actual <= 0)	{
					obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
					return actual;
				}
			}
		}
        	else {
			/* Wait until we have at least 3 bytes data */
			DEBUG(3, "Need at least 3 bytes got only %d!\n", msg->data_size);
			return actual;
        	}
	}

	/* New data has been inserted at the end of message */
	DEBUG(1, "Got %d bytes msg len=%d\n", actual, msg->data_size);

	/*
	 * Make sure that the buffer we have, actually has the specified
	 * number of bytes. If not the frame may have been fragmented, and
	 * we will then need to read more from the socket.  
	 */

	/* Make sure we have a whole packet */
	if (size > msg->data_size) {
		DEBUG(3, "Need more data, size=%d, len=%d!\n",
		      size, msg->data_size);

		/* I'll be back! */
		return msg->data_size;
	}

	DUMPBUFFER(2, "Rx", msg);

	actual = msg->data_size;
	final = hdr->opcode & OBEX_FINAL; /* Extract final bit */
Esempio n. 2
0
/*
 * Function obex_data_indication (self)
 *
 *    Read/Feed some input from device and find out which packet it is
 *
 */
int obex_data_indication(obex_t *self, uint8_t *buf, int buflen)
{
	obex_common_hdr_t *hdr;
	GNetBuf *msg;
	int final;
	int actual = 0;
	unsigned int size;
	int ret;
	
	DEBUG(4, "\n");

	obex_return_val_if_fail(self != NULL, -1);

	msg = self->rx_msg;
	
	/* First we need 3 bytes to be able to know how much data to read */
	if(msg->len < 3)  {
		actual = obex_transport_read(self, 3 - (msg->len), buf, buflen);
		
		DEBUG(4, "Got %d bytes\n", actual);

		/* Check if we are still connected */
		if (actual <= 0)	{
			obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
			return actual;
		}
		buf += actual;
		buflen -= actual;
		g_netbuf_put(msg, actual);
	}

	/* If we have 3 bytes data we can decide how big the packet is */
	if(msg->len >= 3) {
		hdr = (obex_common_hdr_t *) msg->data;
		size = ntohs(hdr->len);

		actual = 0;
		if(msg->len != (int) ntohs(hdr->len)) {

			actual = obex_transport_read(self, size - msg->len, buf,
				buflen);

			/* Check if we are still connected */
			if (actual <= 0)	{
				obex_deliver_event(self, OBEX_EV_LINKERR, 0, 0, TRUE);
				return actual;
			}
		}
	}
        else {
		/* Wait until we have at least 3 bytes data */
		DEBUG(3, "Need at least 3 bytes got only %d!\n", msg->len);
		return actual;
        }


	/* New data has been inserted at the end of message */
	g_netbuf_put(msg, actual);
	DEBUG(1, "Got %d bytes msg len=%d\n", actual, msg->len);

	/*
	 * Make sure that the buffer we have, actually has the specified
	 * number of bytes. If not the frame may have been fragmented, and
	 * we will then need to read more from the socket.  
	 */

	/* Make sure we have a whole packet */
	if (size > msg->len) {
		DEBUG(3, "Need more data, size=%d, len=%d!\n",
		      size, msg->len);

		/* I'll be back! */
		return msg->len;
	}

	DUMPBUFFER(2, "Rx", msg);

	actual = msg->len;
	final = hdr->opcode & OBEX_FINAL; /* Extract final bit */