Example #1
0
static void obex_readstream(obex_t *handle, obex_object_t *object)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	const uint8_t *buf;
	int actual, free_space;

	if (context->counter == 0)
		get_target_size_and_time(handle, object, context);

	actual = OBEX_ObjectReadStream(handle, object, &buf);
	if (actual <= 0) {
		debug("Error or no data on OBEX stream");
		return;
	}

	context->counter += actual;

	debug("obex_readstream: got %d bytes (%d in total)", actual, context->counter);

	free_space = sizeof(context->buf) - (context->data_start + context->data_length);
	if (actual > free_space) {
		/* This should never happen */
		debug("Out of buffer space: actual=%d, free=%d", actual, free_space);
		return;
	}

	memcpy(&context->buf[context->data_start], buf, actual);
	context->data_length += actual;

	debug("OBEX_SuspendRequest");
	OBEX_SuspendRequest(handle, object);

	queue_event(context, OBEX_EV_STREAMAVAIL);
}
Example #2
0
//
// Read data from stream.
//
static int readstream(obex_t *handle, obex_object_t *object)
{
	int 		actual;
	obexsrv_t	*srv = OBEX_GetUserData(handle);
	const uint8_t	*buf;
	int		len;

	if (srv->sfd < 0) {
		/* create temporary storage for an object */
		srv->name = strdup("/tmp/obex_tmp_XXXXXX");
		if (!srv->name)
			return -1;
		srv->sfd = mkstemp(srv->name);
		if (srv->sfd < 0) {
			DBPRT("unable to create tmp file: %s\n", srv->name);
			free(srv->name);
			srv->name = NULL;
			return srv->sfd;
		}
		DBPRT("created tmp file: %s\n", srv->name);
		srv->flags = 0x01;
	}
	srv->streamming = TRUE;
	actual = OBEX_ObjectReadStream(handle, object, &buf);
	DBPRT("got stream: %d\n", actual);
	if (actual > 0) {
		len = write(srv->sfd, buf, actual);
	}
	return actual;
}
Example #3
0
void obexsrv_reqhint(obex_t *handle, obex_object_t *object, int cmd)
{
	DBPRT("reqhint: %#x\n", cmd);
	switch(cmd) {
		case OBEX_CMD_PUT: 
			OBEX_ObjectReadStream(handle, object, NULL);
			break;
		default:
			break;
	}
	/* accept all */
	OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
}
Example #4
0
int lightblueobex_streamtofile(obex_t *obex, obex_object_t *obj, PyObject *fileobj)
{
    const uint8_t *buf;
    int buflen;

    DEBUG("%s()\n", __func__);

    if (fileobj == NULL)
        return -1;

    buflen = OBEX_ObjectReadStream(obex, obj, &buf);
    if (buflen == 0)
        return 0;

    if (buflen < 0) {
        DEBUG("\tunable to read body data from request\n");
        return -1;
    }

    DEBUG("\treading %d bytes\n", buflen);

    PyObject *pybuf = PyBuffer_FromMemory((void*)buf, buflen);
    if (pybuf == NULL) {
        DEBUG("\terror reading received body\n");
        return -1;
    }

    PyObject *result = PyObject_CallMethod(fileobj, "write", "O", pybuf);
    Py_DECREF(pybuf);

    if (result == NULL) {
        if (PyErr_Occurred()) {
            PyErr_Print();
            PyErr_Clear();  /* let caller set exception */
        }
        DEBUG("error calling write() on file object\n");
        return -1;
    }

    Py_DECREF(result);
    return buflen;
}
Example #5
0
static void
obexserver_incomingrequest(OBEXServer *self, obex_object_t *obj, int obex_cmd)
{
    self->notifiednewrequest = 0;
    self->hasbodydata = 0;
    Py_XDECREF(self->tempbuf);
    Py_XDECREF(self->fileobj);

    // signal we want to stream body data
    if (obex_cmd == OBEX_CMD_PUT) {
        if (OBEX_ObjectReadStream(self->obex, obj, NULL) < 0) {
            DEBUG("\tUnable to stream body data\n");
            OBEX_ObjectSetRsp(obj, OBEX_RSP_INTERNAL_SERVER_ERROR,
                              OBEX_RSP_INTERNAL_SERVER_ERROR);
            return;
        }
    }

    OBEX_ObjectSetRsp(obj, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);

#ifdef LIGHTBLUEOBEX_SERVER_TEST
    self->puttotal = 0;
#endif
}
Example #6
0
int obex_get(obex_t *handle, const char *type, const char *name)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	obex_object_t *object;
	obex_headerdata_t hd;
	int err, cmd;
	g_return_val_if_fail(context != NULL, -1);

	if (context->state != OBEX_OPEN && context->state != OBEX_CONNECT
					&& context->state != OBEX_CONNECTED)
		return -ENOTCONN;

	cmd = OBEX_ObjectGetCommand(handle, NULL);
	if (cmd == OBEX_CMD_GET || cmd == OBEX_CMD_PUT)
		return -EBUSY;

	/* Initialize transfer variables */
	context->data_start = 0;
	context->data_length = 0;
	context->counter = 0;
	context->target_size = -1;
	context->modtime = -1;
	context->close = 0;

	object = OBEX_ObjectNew(handle, OBEX_CMD_GET);
	if (!object)
		return -ENOMEM;

	if (context->cid != CID_INVALID) {
		hd.bq4 = context->cid;
		OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_CONNECTION, hd, 4, OBEX_FL_FIT_ONE_PACKET);
	}

	if (type) {
		int len = strlen(type) + 1;

		hd.bs = (uint8_t *) type;

		err = OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_TYPE, hd, len, OBEX_FL_FIT_ONE_PACKET);
		if (err < 0) {
			OBEX_ObjectDelete(handle, object);
			return err;
                }
        }

	if (name) {
		int len, ulen = (strlen(name) + 1) * 2;
		uint8_t *unicode = malloc(ulen);

		if (!unicode) {
			OBEX_ObjectDelete(handle, object);
			return -ENOMEM;
		}

		len = OBEX_CharToUnicode(unicode, (uint8_t *) name, ulen);
		hd.bs = unicode;

		err = OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_NAME, hd, len, OBEX_FL_FIT_ONE_PACKET);
		if (err < 0) {
			OBEX_ObjectDelete(handle, object);
			free(unicode);
			return err;
                }

                free(unicode);
        }

	OBEX_ObjectReadStream(handle, object, NULL);

	err = obex_send_or_queue(handle, object);
	if (err < 0)
		OBEX_ObjectDelete(handle, object);

	return err;
}