: bharatvaj/forwarder-initiators

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
#ifndef _BLUETOOTH_H
#define _BLUETOOTH_H
#include <thread>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <crosssocket.h>
#include <functional>
bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}});

typedef std::function<void(xs_SOCKET)> OnDeviceAdded;

class Bluetooth {
public:
  Bluetooth() {}
  OnDeviceAdded onDeviceAdded;

  void setOnDeviceConnected(OnDeviceAdded onDeviceAdded) {
    this->onDeviceAdded = onDeviceAdded;
  }
  bool startSlave() {

    struct sockaddr_rc loc_addr = {0}, rem_addr = {0};
    xs_SOCKET s;
    socklen_t opt = sizeof(rem_addr);

    // allocate socket
    s = xs_socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    if (s < 0) {
      return false;
    }

    // bind socket to port 1 of the first available
    // local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = inc;
    loc_addr.rc_channel = (uint8_t)1;
    if (bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) == -1) {
      return false;
    }

    // put socket into listening mode
    if (::listen(s, 1) == -1) {
      return false;
    }

    // accept one connection
new std::thread([&](int so){
    xs_SOCKET c = accept(so, (struct sockaddr *)&rem_addr, &opt);

    close(s);
      try {
    if (c < 0) {
	perror("socket\n");
      onDeviceAdded(-1);
	return;
    }
        onDeviceAdded(c);
      } catch (std::bad_function_call bfc) {
      }

}, s);

    return true;
  }
};
#endif