Exemplo n.º 1
0
int obex_disconnect(int od)
{
	obex_t *handle = obex_handles[0];
	struct obex_context *context;
	int err;

	obex_object_t *object;

	if (!(context = OBEX_GetUserData(handle)))
		return -1;

	if (!(object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT)))
		return -1;

	context->state = OBEX_DISCONN;

	if ((err = OBEX_Request(handle, object)) < 0)
		return err;

	while (1) {
		OBEX_HandleInput(handle, OBEX_TIMEOUT);

		if (context->state == OBEX_CLOSED)
			break;
	}

	return err;
}
Exemplo n.º 2
0
int obex_setpath(int od, const char *path, uint8_t flags)
{
	unsigned char taildata[2] = { 0x00, 0x00 };
	obex_t *handle = obex_handles[0];
	struct obex_context *context;
	char unicode[200];
	int err, size;

	obex_object_t *object;
	obex_headerdata_t hd;

	if (!(context = OBEX_GetUserData(handle)))
		return -1;

	if (context->state != OBEX_CONNECTED)
		return -1;

	if (context->mode != OBEX_IDLE)
		return -1;

	context->mode = OBEX_REQUEST;

	if (!(object = OBEX_ObjectNew(handle, OBEX_CMD_SETPATH)))
		return -1;

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

	if (path) {
		size = OBEX_CharToUnicode((uint8_t *) unicode, (uint8_t *) path, sizeof(unicode));
		hd.bs = (uint8_t *) unicode;
		OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_NAME, hd, size, OBEX_FL_FIT_ONE_PACKET);
	}

	taildata[0] = flags;
	OBEX_ObjectSetNonHdrData(object, taildata, sizeof(taildata));

	if ((err = OBEX_Request(handle, object)) < 0)
		return err;

	while (1) {
		OBEX_HandleInput(handle, OBEX_TIMEOUT);

		if (context->mode == OBEX_ERROR) {
			err = -EIO;
			break;
		}

		if (context->mode == OBEX_DONE)
			break;
	}

	context->mode = OBEX_IDLE;

	return err;
}
Exemplo n.º 3
0
int
RequestSync(obex_t* handle, obex_object_t* object, int async)
{
    int ret;
    OBEX_Request(handle, object);
    ret = WaitRsp();
    sendingFinished = FALSE;
    return ret;
}
Exemplo n.º 4
0
/**
	Do an OBEX request synchronous.
 */
static int cli_sync_request(obexftp_client_t *cli, obex_object_t *object)
{
	DEBUG(3, "%s()\n", __func__);

	if (cli->finished == FALSE)
		return -EBUSY;
	cli->finished = FALSE;
	(void) OBEX_Request(cli->obexhandle, object);

	return obexftp_sync (cli);
}
Exemplo n.º 5
0
static int obex_send_or_queue(obex_t *handle, obex_object_t *object)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	int err;

	err = OBEX_Request(handle, object);

	if (err == -EBUSY && !context->pending) {
		context->pending = object;
		return 0;
	}

	return err;
}
Exemplo n.º 6
0
static char *get_named_object(obex_t *handle, char *name, int *len)
{
	struct obex_state *state;
	int req_done;
	obex_object_t *obj;
	obex_headerdata_t hd;
	int size, i;
	glong num;

	state = OBEX_GetUserData(handle);

	obj = OBEX_ObjectNew(handle, OBEX_CMD_GET);
	hd.bq4 = state->connid;
	size = 4;
	OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION,
			     hd, size, OBEX_FL_FIT_ONE_PACKET);

	hd.bs = (unsigned char *)g_utf8_to_utf16(name, strlen(name),
						 NULL, &num, NULL);

	for (i=0; i<num; i++) {
		uint16_t *wchar = (uint16_t*)&hd.bs[i*2];
		*wchar = ntohs(*wchar);
	}
	size = (num+1) * sizeof(uint16_t);
	OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_NAME, hd, size, OBEX_FL_FIT_ONE_PACKET);

	if (OBEX_Request(handle, obj) < 0)
		return NULL;

	req_done = state->req_done;
	while (state->req_done == req_done) {
		OBEX_HandleInput(handle, 100);
	}

	if (state->body) {
		*len = state->body_len;
		state->body[state->body_len] = '\0';
	} else {
		*len = 0;
	}
	return state->body;
}
Exemplo n.º 7
0
int obex_connect(int od, int mode)
{
	obex_t *handle = obex_handles[0];
	struct obex_context *context;
	int err;

	obex_object_t *object;
	obex_headerdata_t hd;

	if (!(context = OBEX_GetUserData(handle)))
		return -1;

	if (!(object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT)))
		return -1;

	if (mode == OBEX_FILE_TRANSFER) {
		hd.bs = target_ftseq;
		if (OBEX_ObjectAddHeader(handle, object,
				OBEX_HDR_TARGET, hd, 16, OBEX_FL_FIT_ONE_PACKET) < 0) {
			OBEX_ObjectDelete(handle, object);
			return -1;
		}
	} else
		context->cid = 0;

	if ((err = OBEX_Request(handle, object)) < 0)
		return err;

	while (1) {
		OBEX_HandleInput(handle, OBEX_TIMEOUT);

		if (context->state == OBEX_CLOSED) {
			err = -ENODEV;
			break;
		}

		if (context->state == OBEX_CONNECTED)
			break;
	}

	return err;
}
Exemplo n.º 8
0
//
// Do an OBEX request sync.
//
static int cli_sync_request(ircp_client_t *cli, obex_object_t *object)
{
	int ret;
	DEBUG(4, "\n");

	cli->finished = FALSE;
	OBEX_Request(cli->obexhandle, object);

	while(cli->finished == FALSE) {
		ret = OBEX_HandleInput(cli->obexhandle, 20);
		DEBUG(4, "ret = %d\n", ret);

		if (ret <= 0)
			return -1;
	}

	DEBUG(4, "Done success=%d\n", cli->success);

	if(cli->success)
		return 1;
	else
		return -1;
}
Exemplo n.º 9
0
void smartpen_disconnect (obex_t *handle)
{
	struct obex_state *state;
	int req_done;
	obex_object_t *obj;
	obex_headerdata_t hd;
	int size;

	state = OBEX_GetUserData(handle);
	obj = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
	hd.bq4 = state->connid;
	size = 4;
	OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_CONNECTION,
			     hd, size, OBEX_FL_FIT_ONE_PACKET);

	if (OBEX_Request(handle, obj) < 0)
		return;

	req_done = state->req_done;
	while (state->req_done == req_done) {
		OBEX_HandleInput(handle, 100);
	}
}
Exemplo n.º 10
0
obex_t *smartpen_connect(short vendor, short product)
{
	obex_t *handle;
	obex_object_t *obj;
	int rc, num, i;
	struct obex_state *state;
	obex_interface_t *obex_intf;
	obex_headerdata_t hd;
	int size, count;
	char *buf;

again:
	handle = OBEX_Init(OBEX_TRANS_USB, obex_event, 0);
	if (!handle)
		goto out;

        num = OBEX_EnumerateInterfaces(handle);
	for (i=0; i<num; i++) {
                obex_intf = OBEX_GetInterfaceByIndex(handle, i);
		if (!strcmp(obex_intf->usb.manufacturer, "Livescribe"))
			break;
	}

        if (i == num) {
		printf("No such device\n");
		handle = NULL;
		goto out;
        }

	state = malloc(sizeof(struct obex_state));
	if (!state) {
		handle = NULL;
		goto out;
	}
	memset(state, 0, sizeof(struct obex_state));

	if (!swizzle_usb(vendor, product)) {
		handle = NULL;
		goto out;
	}

        rc = OBEX_InterfaceConnect(handle, obex_intf);
        if (rc < 0) {
		printf("Connect failed %d\n", rc);
		handle = NULL;
		goto out;
	}

        OBEX_SetUserData(handle, state);
        OBEX_SetTransportMTU(handle, 0x400, 0x400);

        obj = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
        hd.bs = (unsigned char *)"LivescribeService";
        size = strlen((char*)hd.bs)+1;
        OBEX_ObjectAddHeader(handle, obj, OBEX_HDR_TARGET, hd, size, 0);

        rc = OBEX_Request(handle, obj);

	count = state->req_done;
        while (rc == 0 && state->req_done <= count) {
            OBEX_HandleInput(handle, 100);
        }

	if (rc < 0 || !state->got_connid) {
		printf("Retry connection...\n");
		OBEX_Cleanup(handle);
		goto again;
	}

	buf = get_named_object(handle, "ppdata?key=pp0000", &rc);
	if (!buf) {
		printf("Retry connection...\n");
		OBEX_Cleanup(handle);
		pen_reset(vendor, product);
		goto again;
	}

out:
	return handle;
}
Exemplo n.º 11
0
static void obex_event(obex_t *handle, obex_object_t *object,
			int mode, int event, int command, int response)
{
	obex_context_t *context = OBEX_GetUserData(handle);

	switch (event) {
	case OBEX_EV_PROGRESS:
		obex_progress(handle, object);
		break;

	case OBEX_EV_REQHINT:
		OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, response);
		break;

	case OBEX_EV_REQ:
		OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, response);
		break;

	case OBEX_EV_REQDONE:
		debug("OBEX_EV_REQDONE");

		context->obex_rsp = response;
		queue_event(context, OBEX_EV_REQDONE);

		if (context->pending && OBEX_Request(handle, context->pending) == 0) {
			if (OBEX_ObjectGetCommand(handle, context->pending) == OBEX_CMD_PUT)
				queue_event(context, OBEX_EV_STREAMEMPTY);
			context->pending = NULL;
		}

	        switch (command) {
	        case OBEX_CMD_CONNECT:
			obex_connect_done(handle, object, response);
			break;
		case OBEX_CMD_DISCONNECT:
			obex_disconnect_done(handle, object, response);
			break;
		case OBEX_CMD_PUT:
		case OBEX_CMD_GET:
			break;
		case OBEX_CMD_SETPATH:
			break;
		case OBEX_CMD_SESSION:
			break;
		case OBEX_CMD_ABORT:
			break;
		}
		break;

	case OBEX_EV_LINKERR:
		OBEX_TransportDisconnect(handle);
		break;

	case OBEX_EV_PARSEERR:
		OBEX_TransportDisconnect(handle);
		break;

	case OBEX_EV_ACCEPTHINT:
		break;

	case OBEX_EV_ABORT:
		queue_event(context, OBEX_EV_ABORT);
		break;

	case OBEX_EV_STREAMEMPTY:
		debug("OBEX_EV_STREAMEMPTY");
		obex_writestream(handle, object);
		break;

	case OBEX_EV_STREAMAVAIL:
		debug("OBEX_EV_STREAMAVAIL");
		obex_readstream(handle, object);
		break;

	case OBEX_EV_UNEXPECTED:
		break;

	case OBEX_EV_REQCHECK:
		break;
	}
}
Exemplo n.º 12
0
int obex_get(int od, const char *type, const char *name, unsigned char **data, size_t *size)
{
	obex_t *handle = obex_handles[0];
	struct obex_context *context;
	char *unicode;
	int err, len, ulen;

	obex_object_t *object;
	obex_headerdata_t hd;

	if (!(context = OBEX_GetUserData(handle)))
		return -1;

	if (context->state != OBEX_CONNECTED)
		return -1;

	if (context->mode != OBEX_IDLE)
		return -1;

	context->command = OBEX_CMD_GET;
	context->response = 0;

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

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

	if (type) {
		hd.bs = (uint8_t *) type;
		OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_TYPE, hd, strlen(type) + 1, OBEX_FL_FIT_ONE_PACKET);
	}

	if (name) {
		ulen = (strlen(name) + 1) * 2;
		unicode = malloc(ulen);
		if (!unicode) {
			OBEX_ObjectDelete(handle, object);
			return -1;
		}

		len = OBEX_CharToUnicode((uint8_t *) unicode, (uint8_t *) name, ulen);
		hd.bs = (uint8_t *) unicode;
		OBEX_ObjectAddHeader(handle, object,
			OBEX_HDR_NAME, hd, len, OBEX_FL_FIT_ONE_PACKET);

		free(unicode);
	}

	if ((err = OBEX_Request(handle, object)) < 0)
		return err;

	context->time = time(0);

	while (1) {
		OBEX_HandleInput(handle, OBEX_TIMEOUT);
		if (context->response)
			break;
	}

	//printf("Rsp: %02x\n", context->response);
	//printf("Err: %s\n", strerror(obex_error(context->response)));

	if (context->response == OBEX_RSP_SUCCESS) {
		*data = context->data_buf;
		*size = context->data_len;
	} else {
		free(context->data_buf);
		err = -1;
	}

	context->mode = OBEX_IDLE;

	return err;
}