101
102
static struct serial_port *find_port(GSList *ports, const char *pattern)
108
channel = strtol(pattern, &endptr, 10);
105
110
for (l = ports; l != NULL; l = l->next) {
106
111
struct serial_port *port = l->data;
110
if (!strcasecmp(port->uuid, pattern))
115
if (port->uuid && !strcasecmp(port->uuid, pattern))
118
if (endptr && *endptr == '\0' && port->channel == channel)
113
121
if (port->dev && !strcmp(port->dev, pattern))
116
127
uuid_str = bt_name2string(pattern);
120
131
ret = strcasecmp(port->uuid, uuid_str);
122
132
g_free(uuid_str);
355
377
port->listener_id = 0;
380
static void get_record_cb(sdp_list_t *recs, int err, gpointer user_data)
382
struct serial_port *port = user_data;
383
struct serial_device *device = port->device;
384
sdp_record_t *record = NULL;
389
if (!port->listener_id) {
395
error("Unable to get service record: %s (%d)", strerror(-err),
397
reply = failed(port->msg, strerror(-err));
401
if (!recs || !recs->data) {
402
error("No record found");
403
reply = failed(port->msg, "No record found");
409
if (sdp_get_access_protos(record, &protos) < 0) {
410
error("Unable to get access protos from port record");
411
reply = failed(port->msg, "Invalid channel");
415
port->channel = sdp_get_proto_port(protos, RFCOMM_UUID);
417
sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL);
418
sdp_list_free(protos, NULL);
420
port->io = bt_io_connect(BT_IO_RFCOMM, rfcomm_connect_cb, port,
422
BT_IO_OPT_SOURCE_BDADDR, &device->src,
423
BT_IO_OPT_DEST_BDADDR, &device->dst,
424
BT_IO_OPT_CHANNEL, port->channel,
427
error("%s", gerr->message);
428
reply = failed(port->msg, gerr->message);
436
g_dbus_remove_watch(device->conn, port->listener_id);
437
port->listener_id = 0;
438
g_dbus_send_message(device->conn, reply);
441
static int connect_port(struct serial_port *port)
443
struct serial_device *device = port->device;
450
err = bt_string2uuid(&uuid, port->uuid);
454
sdp_uuid128_to_uuid(&uuid);
456
return bt_search_service(&device->src, &device->dst, &uuid,
457
get_record_cb, port, NULL);
460
port->io = bt_io_connect(BT_IO_RFCOMM, rfcomm_connect_cb, port,
462
BT_IO_OPT_SOURCE_BDADDR, &device->src,
463
BT_IO_OPT_DEST_BDADDR, &device->dst,
464
BT_IO_OPT_CHANNEL, port->channel,
472
static struct serial_port *create_port(struct serial_device *device,
473
const char *uuid, uint8_t channel)
475
struct serial_port *port;
477
port = g_new0(struct serial_port, 1);
478
port->uuid = g_strdup(uuid);
479
port->channel = channel;
480
port->device = device;
484
device->ports = g_slist_append(device->ports, port);
358
489
static DBusMessage *port_connect(DBusConnection *conn,
359
490
DBusMessage *msg, void *user_data)
361
492
struct serial_device *device = user_data;
362
493
struct serial_port *port;
367
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
497
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
368
498
DBUS_TYPE_INVALID) == FALSE)
371
port = find_port(device->ports, uuid);
373
return does_not_exist(msg, "Does not match");
501
port = find_port(device->ports, pattern);
506
channel = strtol(pattern, &endptr, 10);
507
if ((endptr && *endptr != '\0') || channel < 1 || channel > 30)
508
return does_not_exist(msg, "Does not match");
510
port = create_port(device, NULL, channel);
375
513
if (port->listener_id)
376
514
return failed(msg, "Port already in use");
382
520
port->msg = dbus_message_ref(msg);
384
io = bt_io_connect(BT_IO_RFCOMM, rfcomm_connect_cb, port,
386
BT_IO_OPT_SOURCE_BDADDR, &device->src,
387
BT_IO_OPT_DEST_BDADDR, &device->dst,
388
BT_IO_OPT_CHANNEL, port->channel,
522
err = connect_port(port);
391
524
DBusMessage *reply;
393
error("%s", err->message);
526
error("%s", strerror(-err));
394
527
g_dbus_remove_watch(conn, port->listener_id);
395
528
port->listener_id = 0;
396
reply = failed(msg, err->message);
529
reply = failed(msg, strerror(-err));
401
g_io_channel_unref(io);