All of lore.kernel.org
 help / color / mirror / Atom feed
* [Bluez-users] connect() returns "Operation already in progress"
@ 2007-02-11 16:35 Timur Alperovich
  2007-02-12 19:38 ` Marcel Holtmann
  0 siblings, 1 reply; 2+ messages in thread
From: Timur Alperovich @ 2007-02-11 16:35 UTC (permalink / raw)
  To: bluez-users


[-- Attachment #1.1: Type: text/plain, Size: 657 bytes --]

Hi there,

I'm trying to write a simple client/server application to communicate
between a Motorola A780 phone and a bluetooth usb dongle. Interestingly
enough when I run the server application on my laptop, to which the dongle
is attached, the client application on the phone returns "Operation already
in progress" when connect is called. On the other hand, when I run the
server on the phone and connect from my laptop, it works fine. What could be
causing this?

The usb dongle is a Targus ACB10US, with broadcom chipset. The device on the
Motorola phone is a Broadcom BCM2035.

I attached the code for the server and client programs.

Thank you,
Timur

[-- Attachment #1.2: Type: text/html, Size: 704 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: client.cc --]
[-- Type: text/x-c++src; name="client.cc", Size: 2644 bytes --]

/* Proof of concept bluetooth application
 * for the A780 phone. The client searches for
 * the bluetooth device to associate with by
 * name and then initiates the connection on
 * the specified port/channel (use 1 for now).
 *
 * In the future should implement a mechanism for
 * secure identification of the client.
 *
 * Client to be run on the phone.
 * Timur Alperovich, 2007
 *
 * Thank you to Albert Huang for the bluetooth programming guide.
 */

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <error.h>

#define SCAN_LEN    5
#define __LAPTOP
#ifndef __LAPTOP
#define SERVER_NAME "Targus-Laptop"
#else
#define SERVER_NAME "Broadcom BCM2035"
#endif
#define MAX_RSP     255
#define CHANNEL     1

using namespace std;

int main(int argc, char** argv){
  // scan for the device
  inquiry_info *ii = NULL;
  char name[40] = {0};

  int dev_id = hci_get_route(NULL);
  int sock = hci_open_dev(dev_id);

  if(dev_id < 0 || sock < 0)
    error(1, errno, "error creating a socket");

  int flags = IREQ_CACHE_FLUSH;
  ii = (inquiry_info*)malloc(MAX_RSP * sizeof(inquiry_info));

  int num_rsp = hci_inquiry(dev_id, SCAN_LEN, MAX_RSP, NULL, &ii, flags);
  if( num_rsp < 0 ) perror("hci_inquiry");

  struct sockaddr_rc addr = {0};
  for (int i = 0; i < num_rsp; i++) {
    memset(name, 0, sizeof(name));
    hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0);
    name[39] = '\0';
    if(!strcmp(name, SERVER_NAME)){
      cout << "match found: " << name << endl;
      bacpy(&addr.rc_bdaddr, &(ii+i)->bdaddr);
      break;
    }
    else cout << "no match: " << name << endl;
  }
  free(ii);
  close(sock);

  cout << "closed sock" << endl;

  char address[19] = {0};
  ba2str(&addr.rc_bdaddr, address);
  cout << "address to use: " << address << endl;

  if(!bacmp(&addr.rc_bdaddr, BDADDR_ANY))
    return 0;

  // have the address. connect
  sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  addr.rc_family = AF_BLUETOOTH;
  addr.rc_channel = CHANNEL;

  cout << "grabbed a socket" << endl;

  if(connect(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0)
    error(1, errno, "error connecting");
  
  cout << "connected" << endl;

  // send and receive a message
  char msg[50] = {0};
  strcpy(msg, "HelloWorld\0");
  int bytes = send(sock, msg, 11, 0);
  cout << "sent " << bytes << " " << msg << endl;

  memset(msg, 0, 50);

  bytes = read(sock, msg, 16);
  cout << "received: " << bytes << " " << msg << endl;
  
  close(sock);

  return 0;
}

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: server.cc --]
[-- Type: text/x-c++src; name="server.cc", Size: 4976 bytes --]

/* Proof of concept application
 * to interact between a motorola A780 phone
 * and a bluetooth device located on a laptop
 * (or in the future a gumstick).
 *
 * The application creats a service thread for each
 * connection; maintains a list of connections (can be used 
 * for logging purposes); prints the data transmitted (will change
 * to more functionality); also, sends a challenge to the client. 
 * Written in C++ and linked against the libbluetooth.so.1 
 * provided in the bluez package.
 *
 * Author: Timur Alperovich, 2007
 */

#include <list>
#include <sys/socket.h>
#include <iostream>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <signal.h>

#define CHANNEL 1
#define CONN_QUEUE  10
#define THREAD_MEM  1024*1024

#define __PTHREAD

using namespace std;

struct node{
  char address[18];
  char name[31];
};

struct service{
  int sock;
  int client_id;
};

list<struct node*> clients;
#ifdef __PTHREAD
pthread_mutex_t* node_list;
#endif

static void service_request(int sock);
static void quit_handle(int signo);
static void clean_up();

typedef void* (*func)(void*);
typedef unsigned int uint;

// variable for forcing to quit
bool force_quit = false;

int main(int argc, char** argv){
  // switch statement on argv/argc in the future
  int channel = CHANNEL;
  
  struct sockaddr_rc loc_addr, rem_addr;
  int sock;
  int client_sock;
  int addr_size = sizeof(rem_addr);

  // create a socket
  if((sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
    error(1, errno, "error occured: %d\n", errno);

  // bind to an available adapter
  loc_addr.rc_family = AF_BLUETOOTH;
  loc_addr.rc_bdaddr = *BDADDR_ANY;
  loc_addr.rc_channel = (uint8_t) channel;
  
  if(bind(sock, (struct sockaddr*) &loc_addr, sizeof(loc_addr)) < 0)
    error(1, errno, "error occured\n");

  #ifdef __PTHREAD
  // setup pthread_attr
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, THREAD_MEM);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  pthread_t handle;

  // initialize the node list lock
  node_list = new pthread_mutex_t();
  pthread_mutex_init(node_list, NULL);
  #endif

  // ignore dropped connections
  signal(SIGPIPE, SIG_IGN);
  signal(SIGHUP, SIG_IGN);

  // register a clean up handler
  struct sigaction signal_action;
  sigemptyset(&signal_action.sa_mask);
  sigaddset(&signal_action.sa_mask, SIGTERM);
  sigaddset(&signal_action.sa_mask, SIGINT);
  sigaddset(&signal_action.sa_mask, SIGQUIT);
  signal_action.sa_flags = 0;

  signal_action.sa_handler = quit_handle;
  sigaction(SIGTERM, &signal_action, 0);

  signal_action.sa_handler = quit_handle;
  sigaction(SIGINT, &signal_action, 0);

  signal_action.sa_handler = quit_handle;
  sigaction(SIGQUIT, &signal_action, 0);
  
  // start listening
  listen(sock, CONN_QUEUE);

  cout << "Server started!\n";

  // while loop to accept connections
  while(!force_quit){
    // accept a connection
    if((client_sock = accept(sock, (struct sockaddr*) &rem_addr, (socklen_t*) &addr_size)) < 0)
      error(0, errno, "connection error occured\n");
    
    // spawn the thread and then add the client to the list
    #ifdef __PTHREAD
    pthread_create(&handle, &attr, (func) *service_request, (void *) client_sock);
    #endif
    #ifndef __PTHREAD
    service_request(client_sock);
    #endif

    struct node* temp = new struct node();
    ba2str(&rem_addr.rc_bdaddr, temp->address);
    if(hci_read_remote_name(sock, &rem_addr.rc_bdaddr, sizeof(temp->name), temp->name, 0) < 0)
      strcpy(temp->name, "unknown\0");
    #ifdef __PTHREAD
    pthread_mutex_lock(node_list);
    #endif
    clients.push_back(temp);
    #ifdef __PTHREAD
    pthread_mutex_unlock(node_list);
    #endif
  }

  // free memory used
  clean_up();

  return 0;
}

/* service_request(int sock) -- handles a request when it comes in
 * responsible for negotiating with the client and establishing itself
 * as the server. Accepts the token from the client after that point.
 * Currently, accepts a string of text and prints it to stdout; then
 * sends a string of text to the client and then quits.
 */
void service_request(int sock){
  // read in 11 bytes (HelloWorld\0)
  char msg[255] = {0};
  int bytes = read(sock, msg, 11);
  cout << "incoming: " << msg << endl;

  memset(msg, 0, 255);

  // send the client a message
  strcpy(msg, "Goodbye newbie!\0");
  bytes = send(sock, msg, 16, 0);

  close(sock);
}

// exit handler
static void quit_handle(int signo){
  force_quit = true;
}

// free the memory used by the list
static void clean_up(){
  // clean the list
  #ifdef __PTHREAD
  pthread_mutex_lock(node_list);
  #endif
  for(uint i = 0; i < clients.size(); i++){
    delete clients.front();
    clients.pop_front();
  }
  #ifdef __PTHREAD
  pthread_mutex_unlock(node_list);
  delete node_list;
  #endif
  return;
}

[-- Attachment #4: Type: text/plain, Size: 374 bytes --]

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

[-- Attachment #5: Type: text/plain, Size: 164 bytes --]

_______________________________________________
Bluez-users mailing list
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Bluez-users] connect() returns "Operation already in progress"
  2007-02-11 16:35 [Bluez-users] connect() returns "Operation already in progress" Timur Alperovich
@ 2007-02-12 19:38 ` Marcel Holtmann
  0 siblings, 0 replies; 2+ messages in thread
From: Marcel Holtmann @ 2007-02-12 19:38 UTC (permalink / raw)
  To: BlueZ users

Hi Timur,

> I'm trying to write a simple client/server application to communicate
> between a Motorola A780 phone and a bluetooth usb dongle.
> Interestingly enough when I run the server application on my laptop,
> to which the dongle is attached, the client application on the phone
> returns "Operation already in progress" when connect is called. On the
> other hand, when I run the server on the phone and connect from my
> laptop, it works fine. What could be causing this? 

run "hcidump -X -V" to see what is causing this error message.

Regards

Marcel



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Bluez-users mailing list
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-02-12 19:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-11 16:35 [Bluez-users] connect() returns "Operation already in progress" Timur Alperovich
2007-02-12 19:38 ` Marcel Holtmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.