Exemplo n.º 1
0
int obex_connect(obex_t *handle, const unsigned char *target, size_t size)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	obex_object_t *object;
	obex_headerdata_t hd;
	int err, ret;
	g_return_val_if_fail(context != NULL, -1);

	if (context->state != OBEX_OPEN && context->state != OBEX_CLOSED)
		return -EISCONN;

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

	if (target) {
                hd.bs = target;

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

	context->state = OBEX_CONNECT;

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

	return ret;
}
Exemplo n.º 2
0
int obex_disconnect(obex_t *handle)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	obex_object_t *object;
	int ret;
	g_return_val_if_fail(context != NULL, -1);

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

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

	context->state = OBEX_DISCONN;

	if (context->callback && context->callback->disconn_ind)
		context->callback->disconn_ind(handle, context->user_data);

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

	return ret;
}
Exemplo n.º 3
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.º 4
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.º 5
0
/**
	Disconnect this ObexFTP client by sending an OBEX DISCONNECT request.

	\param cli an obexftp_client_t created by obexftp_open().

	\return the result of the DISCONNECT request
 */
int obexftp_disconnect(obexftp_client_t *cli)
{
	obex_object_t *object;
	obex_headerdata_t hv;
	int ret;

	DEBUG(3, "%s()\n", __func__);
	return_val_if_fail(cli != NULL, -EINVAL);

	cli->infocb(OBEXFTP_EV_DISCONNECTING, "", 0, cli->infocb_data);

	object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_DISCONNECT);
	if(cli->connection_id != 0xffffffff) {
		hv.bq4 = cli->connection_id;
		(void) OBEX_ObjectAddHeader(cli->obexhandle, object, OBEX_HDR_CONNECTION,
				    hv, sizeof(uint32_t), OBEX_FL_FIT_ONE_PACKET);
	}
	ret = cli_sync_request(cli, object);

	if(ret < 0)
		cli->infocb(OBEXFTP_EV_ERR, "disconnect", 0, cli->infocb_data);
	else
		cli->infocb(OBEXFTP_EV_OK, "", 0, cli->infocb_data);

	/* don't -- obexftp_close will handle this with OBEX_Cleanup */
	/* OBEX_TransportDisconnect(cli->obexhandle); */
	return ret;
}
Exemplo n.º 6
0
//
// Do OBEX SetPath
//
static int ircp_setpath(ircp_client_t *cli, char *name, int up)
{
	obex_object_t *object;
	obex_headerdata_t hdd;

	uint8_t setpath_nohdr_data[2] = {0,};
	uint8_t *ucname;
	int ucname_len;
	int ret;

	DEBUG(4, "%s\n", name);

	object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_SETPATH);

	if(up) {
		setpath_nohdr_data[0] = 1;
	}
	else {
		ucname_len = strlen(name)*2 + 2;
		ucname = malloc(ucname_len);
		if(ucname == NULL) {
			OBEX_ObjectDelete(cli->obexhandle, object);
			return -1;
		}
		ucname_len = OBEX_CharToUnicode(ucname, (uint8_t *) name, ucname_len);

		hdd.bs = ucname;
		OBEX_ObjectAddHeader(cli->obexhandle, object, OBEX_HDR_NAME, hdd, ucname_len, 0);
		free(ucname);
	}

	OBEX_ObjectSetNonHdrData(object, setpath_nohdr_data, 2);
	ret = cli_sync_request(cli, object);
	return ret;
}
Exemplo n.º 7
0
obex_object_t*
BuildObjFromFile(obex_t* handle, const char* filename)
{
    obex_headerdata_t hdd;
    uint8_t unicodebuf[200];
    int namebuflen;
    obex_object_t *object;
    uint32_t creatorid = MEMO_PAD;
    int filesize;
    char* name = NULL;
    uint8_t* buf;

    buf = ReadEasily(filename, &filesize);
    if (buf == NULL)
        return NULL;

    creatorid = MEMO_PAD;
    name = strchr(filename, '.');
    if (name) {
        name++;
        if (strcmp(name, "vcf") == 0) {
            printf( "This is a Address Book file\n");
            creatorid = ADDRESS_BOOK;
        } else if (strcmp(name, "vcs") == 0) {
            printf( "This is a Date Book file\n");
            creatorid = DATE_BOOK;
        } else if (strcmp(name, "txt") == 0) {
            printf("This is a Memo pad file\n");
            creatorid = MEMO_PAD;
        } else if (strcmp(name, "prc") == 0) {
            printf("This is a Pilot resource file\n");
            creatorid = PILOT_RESOURCE;
        }
    }

    object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
    // makhtar
    //namebuflen = OBEX_CharToUnicode(unicodebuf, (const uint8_t *)filename, sizeof(unicodebuf));
    strcpy(unicodebuf, filename);
    namebuflen = sizeof(unicodebuf);

    hdd.bs = unicodebuf;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME, hdd, namebuflen, 0);

    hdd.bq4 = filesize;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hdd, sizeof(uint32_t), 0);

    if (obexProtocolType != 1) {
        hdd.bq4 = creatorid;
        OBEX_ObjectAddHeader(handle, object, HEADER_CREATOR_ID, hdd, sizeof(uint32_t), 0);
    }

    hdd.bs = buf;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hdd, filesize, 0);
    free(buf);
    return object;
}
Exemplo n.º 8
0
int
Send(char* fileName)
{
    obex_object_t* object;
    int ret, exitval = 0;

    handleSend = OBEX_Init(OBEX_TRANS_IRDA, ObexEvent, 0);
    ret = IrOBEX_TransportConnect(handleSend, "OBEX");
    if (ret < 0) {
        printf("OBEX: Sorry, unable to connect! - Stage 1 -\n");
        return 1;
    }

    object = OBEX_ObjectNew(handleSend, OBEX_CMD_CONNECT);
    ret = RequestSync(handleSend, object, FALSE);
    if ((lastSendRsp != OBEX_RSP_SUCCESS) || (ret < 0)) {
        printf("OBEX: Sorry, unable to connect! - Stage 2 -\n");
        return 1;
    }

    if ((object = BuildObjFromFile(handleSend, fileName ))) {
        ret = RequestSync(handleSend, object, FALSE);
        if ((lastSendRsp != OBEX_RSP_SUCCESS) || (ret < 0))
            exitval = 1;
    } else
        exitval = 1;

    object = OBEX_ObjectNew(handleSend, OBEX_CMD_DISCONNECT);
    ret = RequestSync(handleSend, object, FALSE);

    if ((lastSendRsp != OBEX_RSP_SUCCESS) || (ret < 0))
        exitval = 1;

    if (exitval == 0)
        printf("OBEX: PUT successful\n");
    else
        printf("OBEX: PUT failed\n");

    return exitval;
}
Exemplo n.º 9
0
int obex_delete(obex_t *handle, const char *name)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	obex_object_t *object;
	obex_headerdata_t hd;
	int err;
	g_return_val_if_fail(context != NULL, -1);

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

	object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
	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 (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);
        }

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

	return err;
}
Exemplo n.º 10
0
//
// Create an object from a file. Attach some info-headers to it
//
obex_object_t *build_object_from_file(obex_t *handle, const char *localname, const char *remotename)
{
    obex_object_t *object = NULL;
    obex_headerdata_t hdd;
    uint8_t *ucname;
    int ucname_len, size;
    int i;
    char lastmod[21*2] = {"1970-01-01T00:00:00Z"};

    /* Get filesize and modification-time */
    size = get_fileinfo(localname, lastmod);

    object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
    if(object == NULL)
        return NULL;

    ucname_len = strlen(remotename)*2 + 2;
    ucname = malloc(ucname_len);
    if(ucname == NULL)
        goto err;

    ucname_len = strcpy_utf8_to_unicode(ucname, remotename, ucname_len);

    //ucname_len = OBEX_CharToUnicode(ucname, remotename, ucname_len);
    //printf("ucname len %d\n", ucname_len);
    //for(i=0; i< ucname_len; i++)
    //	printf("%d ", ucname[i]);

    //printf("\n");

    hdd.bs = ucname;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME, hdd, ucname_len, 0);
    free(ucname);

    hdd.bq4 = size;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hdd, sizeof(uint32_t), 0);

    hdd.bs = NULL;
    OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,
                         hdd, 0, OBEX_FL_STREAM_START);

    DEBUG(4, "Lastmod = %s\n", lastmod);
    return object;

err:
    if(object != NULL)
        OBEX_ObjectDelete(handle, object);
    return NULL;
}
Exemplo n.º 11
0
//
// Create an object from a file. Attach some info-headers to it
//
obex_object_t *build_object_from_file(obex_t *handle, const char *localname, const char *remotename)
{
	obex_object_t *object = NULL;
	obex_headerdata_t hdd;
	uint8_t *ucname;
	int ucname_len, size;
	char lastmod[21*2] = {"1970-01-01T00:00:00Z"};
		
	/* Get filesize and modification-time */
	size = get_fileinfo(localname, lastmod);

	object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
	if(object == NULL)
		return NULL;

	ucname_len = strlen(remotename)*2 + 2;
	ucname = malloc(ucname_len);
	if(ucname == NULL)
		goto err;

	ucname_len = OBEX_CharToUnicode(ucname, remotename, ucname_len);

	hdd.bs = ucname;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME, hdd, ucname_len, 0);
	free(ucname);

	hdd.bq4 = size;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hdd, sizeof(uint32_t), 0);

#if 0
	/* Win2k excpects this header to be in unicode. I suspect this in
	   incorrect so this will have to wait until that's investigated */
	hdd.bs = lastmod;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME, hdd, strlen(lastmod)+1, 0);
#endif
		
	hdd.bs = NULL;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,
				hdd, 0, OBEX_FL_STREAM_START);

	DEBUG(4, "Lastmod = %s\n", lastmod);
	return object;

err:
	if(object != NULL)
		OBEX_ObjectDelete(handle, object);
	return NULL;
}
Exemplo n.º 12
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.º 13
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.º 14
0
//
// Do connect as client
//
int ircp_cli_connect(ircp_client_t *cli)
{
	obex_object_t *object;
	int ret;


	DEBUG(4, "\n");
	ircp_return_val_if_fail(cli != NULL, -1);

	cli->infocb(IRCP_EV_CONNECTING, "");
#ifdef DEBUG_TCP
	{
		struct sockaddr_in peer;
		in_addr_t inaddr;
		if ((inaddr = inet_addr("127.0.0.1")) != INADDR_NONE) {
			memcpy((char *) &peer.sin_addr, (char *) &inaddr,
			      sizeof(inaddr));  
		}

		ret = OBEX_TransportConnect(cli->obexhandle, (struct sockaddr *) &peer,
					  sizeof(struct sockaddr_in));
	}
		
#else
	ret = IrOBEX_TransportConnect(cli->obexhandle, "OBEX:IrXfer");
#endif
	if (ret < 0) {
		cli->infocb(IRCP_EV_ERR, "");
		return -1;
	}

	object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_CONNECT);
	ret = cli_sync_request(cli, object);

	if(ret < 0)
		cli->infocb(IRCP_EV_ERR, "");
	else
		cli->infocb(IRCP_EV_OK, "");

	return ret;
}
Exemplo n.º 15
0
//
// Do disconnect as client
//
int ircp_cli_disconnect(ircp_client_t *cli)
{
	obex_object_t *object;
	int ret;

	DEBUG(4, "\n");
	ircp_return_val_if_fail(cli != NULL, -1);

	cli->infocb(IRCP_EV_DISCONNECTING, "");

	object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_DISCONNECT);
	ret = cli_sync_request(cli, object);

	if(ret < 0)
		cli->infocb(IRCP_EV_ERR, "");
	else
		cli->infocb(IRCP_EV_OK, "");

	OBEX_TransportDisconnect(cli->obexhandle);
	return ret;
}
Exemplo n.º 16
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.º 17
0
int obex_setpath(obex_t *handle, const char *path, int create)
{
	obex_context_t *context = OBEX_GetUserData(handle);
	obex_object_t *object;
	obex_headerdata_t hd;
	obex_setpath_hdr_t sphdr;
	int ret;
	g_return_val_if_fail(context != NULL, -1);

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

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

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

	memset(&sphdr, 0, sizeof(obex_setpath_hdr_t));

	if (strcmp(path, "..") == 0) {
		/* Can't create parent dir */
		if (create)
			return -EINVAL;
		sphdr.flags = 0x03;
	} else {
		int len, ulen = (strlen(path) + 1) * 2;
		uint8_t *unicode = malloc(ulen);

		if (!create)
			sphdr.flags = 0x02;

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

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

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

		free(unicode);
	}

	OBEX_ObjectSetNonHdrData(object, (uint8_t *) &sphdr, 2);

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

	return ret;
}
Exemplo n.º 18
0
obex_object_t *build_object_from_file(obex_t *handle, const char *filename, uint32_t creator_id)
{
	obex_headerdata_t hdd;
	uint8_t unicode_buf[200];
	int namebuf_len;
 	obex_object_t *object;
	//uint32_t creator_id;
	int file_size;
	char *name = NULL;
	uint8_t *buf;


	buf = easy_readfile(filename, &file_size);
	if(buf == NULL)
		return NULL;

	/* Set Memopad as the default creator ID */
	if(creator_id == 0)
		creator_id = MEMO_PAD;

	/* Find the . in the filename */
	name = strchr(filename, '.');
	if (name) {
		name++;
		if (strcmp(name, "vcf") == 0) {
			printf( "This is a Address Book file\n");
			creator_id = ADDRESS_BOOK;
		} else if (strcmp(name, "vcs") == 0) {
			printf( "This is a Date Book file\n");
			creator_id = DATE_BOOK;
		} else if (strcmp(name, "txt") == 0) {
			printf("This is a Memo pad file\n");
			creator_id = MEMO_PAD;
		} else if (strcmp(name, "prc") == 0) {
			printf("This is a Pilot resource file\n");
			creator_id = PILOT_RESOURCE;
		}
	}
	/* Build object */
	object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);

	namebuf_len = OBEX_CharToUnicode(unicode_buf, (uint8_t *) filename, sizeof(unicode_buf));

	hdd.bs = unicode_buf;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME,
				hdd, namebuf_len, 0);

	hdd.bq4 = file_size;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH,
				hdd, sizeof(uint32_t), 0);

#if 0
	/* Optional header for win95 irxfer, allows date to be set on file */
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME2,
				(obex_headerdata_t) (uint32_t) stats.st_mtime,
				sizeof(uint32_t), 0);
#endif
	if (obex_protocol_type != 1) {
		/* Optional header for Palm Pilot */
		/* win95 irxfer does not seem to like this one */
		hdd.bq4 = creator_id;
		OBEX_ObjectAddHeader(handle, object, HEADER_CREATOR_ID,
					hdd, sizeof(uint32_t), 0);
	}

	hdd.bs = buf;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY,
				hdd, file_size, 0);

	free(buf);
	return object;
}
Exemplo n.º 19
0
static int get_peer_addr(char *name, struct sockaddr_storage *peer) 
{
	struct addrinfo hint = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_protocol = 0,
		.ai_flags = AI_ADDRCONFIG

	};
	struct addrinfo *info;

	int err = getaddrinfo(name, NULL, &hint, &info);
	if (err)
		return err;
	memcpy(peer, info->ai_addr, info->ai_addrlen);
	freeaddrinfo(info);
	return 0;
}

/*
 * Function main (argc, )
 *
 *    Starts all the fun!
 *
 */
int main(int argc, char *argv[])
{
	struct sockaddr_storage peer;

	obex_object_t *object;
	int ret;

	printf("Send and receive files over TCP OBEX\n");
	if ( ((argc < 3) || (argc > 3)) && (argc != 1) )	{
		printf ("Usage: %s [name] [peer]\n", argv[0]); 
		return -1;
	}

	handle = OBEX_Init(OBEX_TRANS_INET, obex_event, 0);

	if (argc == 1)	{
		printf("Waiting for files\n");
		ret = TcpOBEX_ServerRegister(handle, NULL, 0);
		if(ret < 0) {
                        printf("Cannot listen to socket\n");
			exit(ret);
		}

		while (!finished) {
			ret = OBEX_HandleInput(handle, 10);
			if (ret == 0) {
				printf("Timeout waiting for connection\n");
				break;
			} else if (ret < 0) {
			        printf("Error waiting for connection\n");
				break;
			}
		}
	}
	else {
		/* We are a client */

		ret = get_peer_addr(argv[2], &peer);
		if (ret) {
			perror("Bad name");
			exit(1);
		}
		ret = TcpOBEX_TransportConnect(handle, (struct sockaddr *) &peer,
					  sizeof(peer));

		if (ret < 0) {
			printf("Sorry, unable to connect!\n");
			exit(1);
		}

		object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
		ret = do_sync_request(handle, object, 0);

		if( (object = build_object_from_file(handle, argv[1], 0)) )	{
			ret = do_sync_request(handle, object, 0);
		}
		else	{
			perror("PUT failed");
		}

		object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
		ret = do_sync_request(handle, object, 0);

		printf("PUT successful\n");
	}
	return 0;
}
Exemplo n.º 20
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;
}
Exemplo n.º 21
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.º 22
0
int obex_put(obex_t *handle, const char *type, const char *name, int size, time_t mtime)
{
	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 = size;
	context->modtime = mtime;
	context->close = 0;

	object = OBEX_ObjectNew(handle, OBEX_CMD_PUT);
	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);
        }

	/* Add a time header if possible */
	if (context->modtime >= 0) {
		char tstr[17];
		int len;

		len = make_iso8601(context->modtime, tstr, sizeof(tstr));

		if (len >= 0) {
			debug("Adding time header: %s", tstr);
			hd.bs = (uint8_t *) tstr;
			OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME, hd, len, 0);
		}
	}

	/* Add a length header if possible */
	if (context->target_size > 0) {
		debug("Adding length header: %d", context->target_size);
		hd.bq4 = (uint32_t) context->target_size;
		OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hd, 4, 0);
	}

	hd.bs = NULL;
	OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hd, 0, OBEX_FL_STREAM_START);

	/* We need to suspend until the user has provided some data
	 * by calling obex_client_write */
	debug("OBEX_SuspendRequest");
	OBEX_SuspendRequest(handle, object);

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

	return err;
}
Exemplo n.º 23
0
/**
	Connect this ObexFTP client using a given source address by sending an OBEX CONNECT request.

	\param cli an obexftp_client_t created by obexftp_open().
	\param src optional local source interface address (transport specific)
	\param device the device address to connect to (transport specific)
	\param port the port/channel for the device address
	\param uuid UUID string for CONNECT (no default)
	\param uuid_len length of the UUID string (excluding terminating zero)

	\return the result of the CONNECT request, -1 on error

	\note Always use a UUID (except for OBEX PUSH)
 */
int obexftp_connect_src(obexftp_client_t *cli, const char *src, const char *device, int port, const uint8_t uuid[], uint32_t uuid_len)
{
	struct sockaddr_in peer;
#ifdef HAVE_BLUETOOTH
	char *devicedup, *devicep;
	bdaddr_t bdaddr, src_addr;
#endif
#ifdef HAVE_USB
	int obex_intf_cnt;
	obex_interface_t *obex_intf;
#endif
	obex_object_t *object;
	obex_headerdata_t hv;
	int ret = -1; /* no connection yet */

	DEBUG(3, "%s()\n", __func__);
	return_val_if_fail(cli != NULL, -EINVAL);

	cli->infocb(OBEXFTP_EV_CONNECTING, "", 0, cli->infocb_data);

	switch (cli->transport) {

	case OBEX_TRANS_IRDA:
		ret = IrOBEX_TransportConnect(cli->obexhandle, "OBEX");
		DEBUG(3, "%s() IR %d\n", __func__, ret);
		break;
		
	case OBEX_TRANS_INET:
		if (!device) {
			ret = -EINVAL;
			break;
		}
#ifdef _WIN32
		peer.sin_addr.s_addr = inet_addr(device);
		ret = (peer.sin_addr.s_addr == INADDR_NONE) ? 0 : 1;
#else
		ret = inet_aton(device, &peer.sin_addr);
#endif
		if (ret) {
			peer.sin_family = AF_INET;
			peer.sin_port = htons(port); /* overridden with OBEX_PORT 650 anyhow */
			ret = InOBEX_TransportConnect(cli->obexhandle, (struct sockaddr *) &peer,
						  sizeof(struct sockaddr_in));
			DEBUG(3, "%s() TCP %d\n", __func__, ret);
		} else
			ret = -EINVAL; /* is there a better errno? */
		break;

	case OBEX_TRANS_CUSTOM:
		/* don't change the custom transport once it is in place */
		if (cli->ctrans == NULL) {
			cli->ctrans = cobex_ctrans (device);
                	if(OBEX_RegisterCTransport(cli->obexhandle, cli->ctrans) < 0) {
                        	DEBUG(1, "Custom transport callback-registration failed\n");
	                }
	        }
		ret = OBEX_TransportConnect(cli->obexhandle, NULL, 0);
		DEBUG(3, "%s() TC %d\n", __func__, ret);
		break;

#ifdef HAVE_BLUETOOTH
	case OBEX_TRANS_BLUETOOTH:
		if (!src) {
			bacpy(&src_addr, BDADDR_ANY);
		}
#ifdef HAVE_SDPLIB
		else if (!strncmp(src, "hci", 3)) {
			hci_devba(atoi(src + 3), &src_addr);
		}
		else if (atoi(src) != 0) {
			hci_devba(atoi(src), &src_addr);
		}
#endif
		else {
			str2ba(src, &src_addr);
		}
		if (!device) {
			ret = -EINVAL;
			break;
		}
		if (port < 1) {
			port = obexftp_browse_bt(device, OBEX_FTP_SERVICE);
		}
		/* transform some chars to colons */
		devicedup = devicep = strdup(device);
		for (; *devicep; devicep++) {
			if (*devicep == '-') *devicep = ':';
			if (*devicep == '_') *devicep = ':';
			if (*devicep == '/') *devicep = ':';
		}
		(void) str2ba(devicedup, &bdaddr);
		free(devicedup);
		ret = BtOBEX_TransportConnect(cli->obexhandle, &src_addr, &bdaddr, (uint8_t)port);
		DEBUG(3, "%s() BT %d\n", __func__, ret);
		break;
#endif /* HAVE_BLUETOOTH */

#ifdef HAVE_USB
	case OBEX_TRANS_USB:
		obex_intf_cnt = OBEX_FindInterfaces(cli->obexhandle, &obex_intf);
		DEBUG(3, "%s() \n", __func__);
		if (obex_intf_cnt <= 0) {
			DEBUG(1, "%s() there are no valid USB interfaces\n", __func__);
			ret = -EINVAL; /* is there a better errno? */
		}
		else if (port >= obex_intf_cnt) {
			DEBUG(1, "%s() %d is an invalid USB interface number\n", __func__, port);
			ret = -EINVAL; /* is there a better errno? */
		} else
			ret = OBEX_InterfaceConnect(cli->obexhandle, &obex_intf[port]);
		DEBUG(3, "%s() USB %d\n", __func__, ret);
		break;
#endif /* HAVE_USB */

	default:
		ret = -ESOCKTNOSUPPORT;
		break;
	}

	if (ret < 0) {
		/* could be -EBUSY or -ESOCKTNOSUPPORT */
		cli->infocb(OBEXFTP_EV_ERR, "connect", 0, cli->infocb_data);
		return ret;
	}

#ifdef COMPAT_S45
	// try S45 UUID first.
	object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_CONNECT);
	hv.bs = UUID_S45;
	if(OBEX_ObjectAddHeader(cli->obexhandle, object, OBEX_HDR_TARGET,
                                hv, sizeof(UUID_S45), OBEX_FL_FIT_ONE_PACKET) < 0) {
                DEBUG(1, "Error adding header\n");
                OBEX_ObjectDelete(cli->obexhandle, object);
                return -1;
        }
	cli->connection_id = 0xffffffff;
	ret = cli_sync_request(cli, object);

	if(ret < 0) {
		cli->infocb(OBEXFTP_EV_ERR, "S45 UUID", 0, cli->infocb_data);
#endif
		object = OBEX_ObjectNew(cli->obexhandle, OBEX_CMD_CONNECT);
		if (uuid) {
			hv.bs = uuid;
			if(OBEX_ObjectAddHeader(cli->obexhandle, object, OBEX_HDR_TARGET,
        	        	                hv, uuid_len, OBEX_FL_FIT_ONE_PACKET) < 0) {
	        	        DEBUG(1, "Error adding header\n");
		                OBEX_ObjectDelete(cli->obexhandle, object);
                		return -1;
	        	}
		}
		cli->connection_id = 0xffffffff;
		ret = cli_sync_request(cli, object);
		if (!OBEXFTP_USE_CONN_HEADER(cli->quirks))
			cli->connection_id = 0xffffffff;
#ifdef COMPAT_S45
	}
#endif

	if(ret < 0)
		cli->infocb(OBEXFTP_EV_ERR, "send UUID", 0, cli->infocb_data);
	else
		cli->infocb(OBEXFTP_EV_OK, "", 0, cli->infocb_data);

	return ret;
}
Exemplo n.º 24
0
/*
 * Function main (argc, )
 *
 *    Starts all the fun!
 *
 */
int main(int argc, char *argv[])
{
	struct sockaddr_in peer;

	obex_object_t *object;
	int ret;

	printf("Send and receive files over TCP OBEX\n");
	if ( ((argc < 3) || (argc > 3)) && (argc != 1) )	{
		printf ("Usage: %s [name] [peer]\n", argv[0]); 
		return -1;
	}

	handle = OBEX_Init(OBEX_TRANS_INET, obex_event, 0);

	if (argc == 1)	{
		printf("Waiting for files\n");
		ret = InOBEX_ServerRegister(handle);
		if(ret < 0) {
                        printf("Cannot listen to socket\n");
			exit(ret);
		}

		while (!finished) {
			ret = OBEX_HandleInput(handle, 10);
			if (ret == 0) {
				printf("Timeout waiting for connection\n");
				break;
			} else if (ret < 0) {
			        printf("Error waiting for connection\n");
				break;
			}
		}
	}
	else {
		/* We are a client */

		get_peer_addr(argv[2], &peer);
		ret = OBEX_TransportConnect(handle, (struct sockaddr *) &peer,
					  sizeof(struct sockaddr_in));

		if (ret < 0) {
			printf("Sorry, unable to connect!\n");
			exit(ret);
		}

		object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT);
		ret = do_sync_request(handle, object, 0);

		if( (object = build_object_from_file(handle, argv[1], 0)) )	{
			ret = do_sync_request(handle, object, 0);
		}
		else	{
			perror("PUT failed");
		}

		object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT);
		ret = do_sync_request(handle, object, 0);

		printf("PUT successful\n");
	}
	return 0;
}
Exemplo n.º 25
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;
}