+#ifdef LINUX
+ /**
+ * Broadcast a HELLO message for peer discovery
+ *
+ * @param dev pointer to the device struct
+ * @param dev pointer to the socket which was added to the set
+ * @return 0 on success
+ */
+ static int
+ send_broadcast (struct HardwareInfos *dev, int *sendsocket)
+ {
+ int new_device = 0;
+ int loops = 0;
+
+ search_for_devices:
+ if ((neighbours.size == neighbours.pos && new_device == 1) || neighbours.size == 0)
+ {
+ inquiry_devices: //skip the conditions and force a inquiry for new devices
+ {
+ /**
+ * It means that I sent HELLO messages to all the devices from the list and I should search
+ * for new ones or that this is the first time when I do a search.
+ */
+ inquiry_info *devices = NULL;
+ int i, responses, max_responses = MAX_PORTS;
+
+ /* sanity checks */
+ if (neighbours.size >= MAX_PORTS)
+ {
+ fprintf (stderr, "%.*s reached the top limit for the discovarable devices\n", IFNAMSIZ, dev->iface);
+ return 2;
+ }
+
+ /* Get the device id */
+ if (neighbours.dev_id == -1)
+ {
+ char addr[19] = { 0 }; //the device MAC address
+
+ ba2str ((bdaddr_t *) &dev->pl_mac, addr);
+ neighbours.dev_id = hci_devid (addr);
+ if (neighbours.dev_id < 0)
+ {
+ fprintf (stderr, "Failed to get the device id for interface %.*s : %s\n", IFNAMSIZ,
+ dev->iface, strerror (errno));
+ return 1;
+ }
+ }
+
+ devices = malloc (max_responses * sizeof (inquiry_info));
+ if (devices == NULL)
+ {
+ fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %.*s\n", IFNAMSIZ,
+ dev->iface);
+ return 1;
+ }
+
+ responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH);
+ if (responses < 0)
+ {
+ fprintf (stderr, "Failed to inquiry on interface %.*s\n", IFNAMSIZ, dev->iface);
+ return 1;
+ }
+
+ fprintf (stderr, "LOG : Found %d devices\n", responses); //FIXME delete it after debugging stage
+
+ if (responses == 0)
+ {
+ fprintf (stderr, "LOG : No devices discoverable\n");
+ return 1;
+ }
+
+ for (i = 0; i < responses; i++)
+ {
+ int j;
+ int found = 0;
+
+ /* sanity check */
+ if (i >= MAX_PORTS)
+ {
+ fprintf (stderr, "%.*s reached the top limit for the discoverable devices (after inquiry)\n", IFNAMSIZ,
+ dev->iface);
+ return 2;
+ }
+
+ /* Search if the address already exists on the list */
+ for (j = 0; j < neighbours.size; j++)
+ {
+ if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0)
+ {
+ found = 1;
+ fprintf (stderr, "LOG : the device already exists on the list\n"); //FIXME debugging message
+ break;
+ }
+ }
+
+ if (found == 0)
+ {
+ char addr[19] = { 0 };
+
+ ba2str (&(devices +i)->bdaddr, addr);
+ fprintf (stderr, "LOG : %s was added to the list\n", addr); //FIXME debugging message
+ GNUNET_memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t));
+ }
+ }
+
+ free (devices);
+ }
+ }
+
+ int connection_successful = 0;
+ struct sockaddr_rc addr_rc = { 0 };
+ int errno_copy = 0;
+ addr_rc.rc_family = AF_BLUETOOTH;
+
+ /* Try to connect to a new device from the list */
+ while (neighbours.pos < neighbours.size)
+ {
+ /* Check if we are already connected to this device */
+ if (neighbours.fds[neighbours.pos] == -1)
+ {
+
+ memset (&addr_rc.rc_bdaddr, 0, sizeof (addr_rc.rc_bdaddr));
+ GNUNET_memcpy (&addr_rc.rc_bdaddr, &(neighbours.devices[neighbours.pos]), sizeof (addr_rc.rc_bdaddr));
+
+ addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr);
+
+ *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+ if ( (-1 < *sendsocket) &&
+ (0 == connect (*sendsocket,
+ (struct sockaddr *) &addr_rc,
+ sizeof (addr_rc))) )
+ {
+ neighbours.fds[neighbours.pos++] = *sendsocket;
+ connection_successful = 1;
+ char addr[19] = { 0 };
+ ba2str (&(neighbours.devices[neighbours.pos - 1]), addr);
+ fprintf (stderr, "LOG : Connected to %s\n", addr);
+ break;
+ }
+ else
+ {
+ char addr[19] = { 0 };
+ errno_copy = errno; //Save a copy for later
+
+ if (-1 != *sendsocket)
+ {
+ (void) close (*sendsocket);
+ *sendsocket = -1;
+ }
+ ba2str (&(neighbours.devices[neighbours.pos]), addr);
+ fprintf (stderr,
+ "LOG : Couldn't connect on device %s, error : %s\n",
+ addr,
+ strerror (errno));
+ if (errno != ECONNREFUSED) //FIXME be sure that this works
+ {
+ fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos);
+ /* Remove the device from the list */
+ GNUNET_memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t));
+ memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t));
+ neighbours.fds[neighbours.pos] = neighbours.fds[neighbours.size - 1];
+ neighbours.fds[neighbours.size - 1] = -1;
+ neighbours.size -= 1;
+ }
+
+ neighbours.pos += 1;
+
+ if (neighbours.pos >= neighbours.size)
+ neighbours.pos = 0;
+
+ loops += 1;
+
+ if (loops == MAX_LOOPS) //don't get stuck trying to connect to one device
+ return 1;
+ }
+ }
+ else
+ {
+ fprintf (stderr, "LOG : Search for a new device\n"); //FIXME debugging message
+ neighbours.pos += 1;
+ }
+ }
+
+ /* Cycle on the list */
+ if (neighbours.pos == neighbours.size)
+ {
+ neighbours.pos = 0;
+ searching_devices_count += 1;
+
+ if (searching_devices_count == MAX_LOOPS)
+ {
+ fprintf (stderr, "LOG : Force to inquiry for new devices\n");
+ searching_devices_count = 0;
+ goto inquiry_devices;
+ }
+ }
+ /* If a new device wasn't found, search an old one */
+ if (connection_successful == 0)
+ {
+ int loop_check = neighbours.pos;
+ while (neighbours.fds[neighbours.pos] == -1)
+ {
+ if (neighbours.pos == neighbours.size)
+ neighbours.pos = 0;
+
+ if (neighbours.pos == loop_check)
+ {
+ if (errno_copy == ECONNREFUSED)
+ {
+ fprintf (stderr, "LOG : No device found. Go back and search again\n"); //FIXME debugging message
+ new_device = 1;
+ loops += 1;
+ goto search_for_devices;
+ }
+ else
+ {
+ return 1; // Skip the broadcast message
+ }
+ }
+
+ neighbours.pos += 1;
+ }
+
+ *sendsocket = neighbours.fds[neighbours.pos++];
+ }
+
+ return 0;
+ }
+#endif