/** 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; }
int main (int argc, char *argv[]) { char cmd[10]; int num, end = 0; int cobex = FALSE, tcpobex = FALSE, btobex = FALSE, r320 = FALSE, usbobex = FALSE; obex_t *handle; #ifdef HAVE_BLUETOOTH bdaddr_t bdaddr; uint8_t channel = 0; #endif #ifdef HAVE_USB obex_interface_t *obex_intf; #endif struct context global_context = {0,}; #ifndef _WIN32 char *port; obex_ctrans_t custfunc; if( (argc == 2 || argc ==3) && (strcmp(argv[1], "-s") == 0 ) ) cobex = TRUE; if( (argc == 2 || argc ==3) && (strcmp(argv[1], "-r") == 0 ) ) { cobex = TRUE; r320 = TRUE; } #endif if( (argc == 2) && (strcmp(argv[1], "-i") == 0 ) ) tcpobex = TRUE; if( (argc >= 2) && (strcmp(argv[1], "-b") == 0 ) ) btobex = TRUE; if( (argc >= 2) && (strcmp(argv[1], "-u") == 0 ) ) usbobex = TRUE; if(cobex) { #ifndef _WIN32 if(argc == 3) port = argv[2]; else port = "/dev/ttyS0"; if(r320) printf("OBEX to R320 on %s!\n", port); else printf("OBEX on %s!\n", port); custfunc.customdata = cobex_open(port, r320); if(custfunc.customdata == NULL) { printf("cobex_open() failed\n"); return -1; } if(! (handle = OBEX_Init(OBEX_TRANS_CUSTOM, obex_event, 0))) { perror( "OBEX_Init failed"); return -1; } custfunc.connect = cobex_connect; custfunc.disconnect = cobex_disconnect; custfunc.write = cobex_write; custfunc.handleinput = cobex_handle_input; custfunc.listen = cobex_connect; // Listen and connect is 100% same on cable if(OBEX_RegisterCTransport(handle, &custfunc) < 0) { printf("Custom transport callback-registration failed\n"); } #else printf("Not implemented in Win32 yet.\n"); #endif // _WIN32 } else if(tcpobex) { printf("Using TCP transport\n"); if(! (handle = OBEX_Init(OBEX_TRANS_INET, obex_event, 0))) { perror( "OBEX_Init failed"); exit(0); } } else if(btobex) { #ifndef _WIN32 switch (argc) { #ifdef HAVE_BLUETOOTH case 4: str2ba(argv[2], &bdaddr); channel = atoi(argv[3]); break; case 3: str2ba(argv[2], &bdaddr); if (bacmp(&bdaddr, BDADDR_ANY) == 0) channel = atoi(argv[2]); else channel = BT_CHANNEL; break; case 2: bacpy(&bdaddr, BDADDR_ANY); channel = BT_CHANNEL; break; #endif default: printf("Wrong number of arguments\n"); exit(0); } printf("Using Bluetooth RFCOMM transport\n"); if(! (handle = OBEX_Init(OBEX_TRANS_BLUETOOTH, obex_event, 0))) { perror( "OBEX_Init failed"); exit(0); } #else printf("Not implemented in Win32 yet.\n"); #endif // _WIN32 } else if(usbobex) { #ifdef HAVE_USB int i, interfaces_number, intf_num; switch (argc) { case 2: printf("Using USB transport, querying available interfaces\n"); if(! (handle = OBEX_Init(OBEX_TRANS_USB, obex_event, 0))) { perror( "OBEX_Init failed"); exit(0); } interfaces_number = OBEX_FindInterfaces(handle, &obex_intf); for (i=0; i < interfaces_number; i++) printf("Interface %d: %s %s %s\n", i, obex_intf[i].usb.manufacturer, obex_intf[i].usb.product, obex_intf[i].usb.control_interface); printf("Use '%s -u interface_number' to run interactive OBEX test client\n", argv[0]); OBEX_Cleanup(handle); exit(0); break; case 3: intf_num = atoi(argv[2]); printf("Using USB transport \n"); if(! (handle = OBEX_Init(OBEX_TRANS_USB, obex_event, 0))) { perror( "OBEX_Init failed"); exit(0); } interfaces_number = OBEX_FindInterfaces(handle, &obex_intf); if (intf_num >= interfaces_number) { printf( "Invalid interface number\n"); exit(0); } obex_intf += intf_num; break; default: printf("Wrong number of arguments\n"); exit(0); } #else printf("Not compiled with USB support\n"); exit(0); #endif } else { printf("Using IrDA transport\n"); if(! (handle = OBEX_Init(OBEX_TRANS_IRDA, obex_event, 0))) { perror( "OBEX_Init failed"); exit(0); } } OBEX_SetUserData(handle, &global_context); printf( "OBEX Interactive test client/server.\n"); while (!end) { printf("> "); num = scanf("%s", cmd); switch (cmd[0] | 0x20) { case 'q': end=1; break; case 'g': get_client(handle, &global_context); break; case 't': setpath_client(handle); break; case 'p': put_client(handle); break; case 'x': push_client(handle); break; case 'c': /* First connect transport */ if(tcpobex) { if(TcpOBEX_TransportConnect(handle, NULL, 0) < 0) { printf("Transport connect error! (TCP)\n"); break; } } if(cobex) { if(OBEX_TransportConnect(handle, (void*) 1, 0) < 0) { printf("Transport connect error! (Serial)\n"); break; } } if(btobex) { #ifdef HAVE_BLUETOOTH if (bacmp(&bdaddr, BDADDR_ANY) == 0) { printf("Device address error! (Bluetooth)\n"); break; } if(BtOBEX_TransportConnect(handle, BDADDR_ANY, &bdaddr, channel) <0) { printf("Transport connect error! (Bluetooth)\n"); break; } #else printf("Transport not found! (Bluetooth)\n"); #endif } if (usbobex) { #ifdef HAVE_USB if (OBEX_InterfaceConnect(handle, obex_intf) < 0) { printf("Transport connect error! (USB)\n"); break; } #else printf("Transport not found! (USB)\n"); #endif } if (!tcpobex && !cobex && !btobex && !usbobex) { if(IrOBEX_TransportConnect(handle, IR_SERVICE) < 0) { printf("Transport connect error! (IrDA)\n"); break; } } // Now send OBEX-connect. connect_client(handle); break; case 'd': disconnect_client(handle); break; case 's': /* First register server */ if(tcpobex) { if(TcpOBEX_ServerRegister(handle, NULL, 0) < 0) { printf("Server register error! (TCP)\n"); break; } } if(cobex) { if(OBEX_ServerRegister(handle, (void*) 1, 0) < 0) { printf("Server register error! (Serial)\n"); break; } } if(btobex) { #ifdef HAVE_BLUETOOTH if(BtOBEX_ServerRegister(handle, BDADDR_ANY, channel) < 0) { printf("Server register error! (Bluetooth)\n"); break; } #else printf("Transport not found! (Bluetooth)\n"); #endif } if (usbobex) { printf("Transport not found! (USB)\n"); } if (!tcpobex && !cobex && !btobex && !usbobex) { if(IrOBEX_ServerRegister(handle, IR_SERVICE) < 0) { printf("Server register error! (IrDA)\n"); break; } } /* No process server events */ server_do(handle); break; default: printf("Unknown command %s\n", cmd); } } #ifndef _WIN32 if(cobex) cobex_close(custfunc.customdata); #endif return 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_FindInterfaces(handle, &obex_intf); for (i=0; i<num; i++) { if (!strcmp(obex_intf[i].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)); swizzle_usb(vendor, product); rc = OBEX_InterfaceConnect(handle, &obex_intf[i]); 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; }