示例#1
0
// bt callback advertiser found
void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)
{
  if (action == action_broadcast) {
    fprintf(stderr,"advertisement from: ");
    print_bdaddr(msg->sender);
    fprintf(stderr," data: ");
    int i;
    for (i = 0; i < msg->data.len; i++) {
      fprintf(stderr,"%02x ", msg->data.data[i]);
    }
    fprintf(stderr,"\n");
    if (sock[0])
      sendto(sock[0], msg->data.data, msg->data.len, MSG_DONTWAIT,
             (struct sockaddr *)&send_addr[0], sizeof(struct sockaddr));
  } else {
    uint8_t i;
    char *name = NULL;

    // Check if this device already found, if not add to the list

    if (!connect_all) {
      fprintf(stderr,"New device found: ");

      // Parse data
      for (i = 0; i < msg->data.len;) {
        int8 len = msg->data.data[i++];
        if (!len) { continue; }
        if (i + len > msg->data.len) { break; } // not enough data
        uint8 type = msg->data.data[i++];
        switch (type) {
          case 0x09:  // device name
            name = malloc(len);
            memcpy(name, msg->data.data + i, len - 1);
            name[len - 1] = '\0';
        }
        i += len - 1;
      }

      print_bdaddr(msg->sender);
      // printf(" RSSI:%d", msg->rssi);

      fprintf(stderr," Name:");
      if (name) { fprintf(stderr,"%s", name); }
      else { fprintf(stderr,"Unknown"); }
      fprintf(stderr,"\n");

      free(name);
    }

    // automatically connect if reponding device has appropriate mac address hearder
    if (connect_all && cmp_addr(msg->sender.addr, MAC_ADDR) >= 4) {
      fprintf(stderr,"Trying to connect to "); print_bdaddr(msg->sender); fprintf(stderr,"\n");
      //change_state(state_connecting);
      // connection interval unit 1.25ms
      // connection interval must be divisible by number of connection * 2.5ms and larger than minimum (7.5ms)

      ble_cmd_gap_connect_direct(&msg->sender.addr, gap_address_type_public, 6, 16, 100, 9);
    }
  }
}
示例#2
0
void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)
{
    int i;
    char *name = NULL;

    // Parse data
    for (i = 0; i < msg->data.len; ) {
        int8 len = msg->data.data[i++];
        if (!len) continue;
        if (i + len > msg->data.len) break; // not enough data
        uint8 type = msg->data.data[i++];
        switch (type) {
        case 0x09:
            name = malloc(len);
            memcpy(name, msg->data.data + i, len - 1);
            name[len - 1] = '\0';
        }

        i += len - 1;
    }

    print_bdaddr(msg->sender);
    printf(" RSSI:%u", msg->rssi);

    printf(" Name:");
    if (name) printf("%s", name);
    else printf("Unknown");
    printf("\n");

    free(name);
}
示例#3
0
// [1] スキャンしたアドバタイズパケットの取得
void ble_evt_gap_scan_response(
  const struct ble_msg_gap_scan_response_evt_t *msg
){

/*

  if (found_devices_count >= MAX_DEVICES) {
    change_state(state_finish);
  }

  // Check if this device already found
  for (i = 0; i < found_devices_count; i++) {
    if (!cmp_bdaddr(msg->sender, found_devices[i])) return;
  }
  found_devices_count++;
  memcpy(found_devices[i].addr, msg->sender.addr, sizeof(bd_addr));
*/

  // パケットデータ全体のダンプ
//  printf("#packet:");
//  print_hex(msg->data.data, msg->data.len);
//  printf("\n");
  
  // Parse data
  memset(&ap, 0, sizeof(adv_pkt));

  parse_packet(msg->data.data, msg->data.len);

  // 時刻
  print_now();
  // アドレス
  printf(",");
  print_bdaddr(msg->sender);
  // RSSI
  printf(",RSSI:%d", msg->rssi);
  print_flags();
  if (ap.name[0] != 0) {
    printf(",Name:%s", ap.name);
  }
  if (ap.ibeacon) {
    printf(",iBeacon");
    printf (",UUID:");
    print_hex(ap.uuid, 16);
    printf (",major:%d,minor:%d,txpower:%d", ap.major, ap.minor, ap.txpower);
  }
  if (ap.services_len != 0) {
    print_services();
  }
#ifdef DEBUG
  // パケットデータ全体のダンプ
  printf(",packet:");
  print_hex(msg->data.data, msg->data.len);
#endif
  printf("\n");
}
示例#4
0
文件: main.c 项目: De-Night/paparazzi
/**
 * "connection_disconnected" event handler
 * Occurs whenever the BLE connection to the peripheral device has been terminated
 *
 * @param msg Event packet data payload
 */
void ble_evt_connection_disconnected(const struct ble_msg_connection_disconnected_evt_t *msg)
{
  // reset connection parameters
  connected[msg->connection] = 0;

  extract_idx[msg->connection] = 0;
  insert_idx[msg->connection] = 0;

  // remove found device from list
  //change_state(state_disconnected);
  fprintf(stderr, "Connection %d terminated\n" , msg->connection);
  if (!connect_all) {
    ble_cmd_gap_connect_direct(&connect_addr, gap_address_type_public, 8, 16, 100, 0);
    fprintf(stderr, "Trying to reconnection to ");
    print_bdaddr(connect_addr);
  }
  //change_state(state_connecting);

  //change_state(state_finish);
}
示例#5
0
文件: main.c 项目: De-Night/paparazzi
void ble_rsp_system_address_get(const struct ble_msg_system_address_get_rsp_t *msg)
{
  fprintf(stderr, "My address: ");
  print_bdaddr(msg->address);
  fprintf(stderr, "\n");
}
示例#6
0
文件: main.c 项目: De-Night/paparazzi
/**
 * "gap_scan_response" event handler
 * Occurs whenever an advertisement packet is detected while scanning
 * (see "gap_discover" command in API reference guide)
 *
 * @param msg Event packet data payload
 */
void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)
{

#ifdef DEBUG_BROADCAST
  if(cmp_addr(msg->sender.addr, MAC_ADDR) >= 3 )// && msg->sender.addr[0] == 0xdf)
  {
    gettimeofday(&tm, NULL); //Time zone struct is obsolete, hence NULL
    mytime = (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
    fprintf(stderr, "%f %x %d, ", mytime, msg->sender.addr[0], msg->rssi);
    uint8_t i = 0;
    for(i = 0; i < msg->data.len; i++)
      fprintf(stderr, "%02x ", msg->data.data[i]);
    fprintf(stderr, "\n");
  }
#endif

  if(rssi_fp)
  {
    fprintf(rssi_fp, "%f %d %d\n", mytime, msg->sender.addr[0], msg->rssi);
    fflush(rssi_fp);
  }

  if (action == action_broadcast) {
    if (sock[0])
      sendto(sock[0], msg->data.data+13, msg->data.len-13, MSG_DONTWAIT,
             (struct sockaddr *)&send_addr[0], sizeof(struct sockaddr));
    //printf("first: %02x, last: %02x\n", msg->data.data[13], msg->data.data[msg->data.len]);
  }

  if (action != action_broadcast) {
    uint8_t i, j;
    char *name = NULL;

    // Check if this device already found, if not add to the list

    if (!connect_all) {
      //fprintf(stderr, "New device found: ");

      // Parse data
      for (i = 0; i < msg->data.len;) {
        int8 len = msg->data.data[i++];
        if (!len) { continue; }
        if (i + len > msg->data.len) { break; } // not enough data
        uint8 type = msg->data.data[i++];
        switch (type) {
          case 0x01:
            // flags field
            break;

          case 0x02:
            // partial list of 16-bit UUIDs
          case 0x03:
            // complete list of 16-bit UUIDs
            /*
            for (j = 0; j < len - 1; j += 2)
            {
                // loop through UUIDs 2 bytes at a time
                uint16 test_uuid = msg -> data.data[i + j] + (msg -> data.data[i + j + 1] << 8);
                if (test_uuid == THERMOMETER_SERVICE_UUID)
                {
                    // found the thermometer service UUID in the list of advertised UUIDs!
                    matching_uuid = 1;
                }
            }
            */
            break;

          case 0x04:
            // partial list of 32-bit UUIDs
          case 0x05:
            // complete list of 32-bit UUIDs
            for (j = 0; j < len - 1; j += 4) {
              // loop through UUIDs 4 bytes at a time
              // TODO: test for desired UUID here, if 32-bit UUID
            }
            break;

          case 0x06:
            // partial list of 128-bit UUIDs
          case 0x07:
            // complete list of 128-bit UUIDs
            for (j = 0; j < len - 1; j += 16) {
              // loop through UUIDs 16 bytes at a time
              // TODO: test for desired UUID here, if 128-bit UUID
            }
            break;
          case 0x09:  // device name
            name = malloc(len);
            memcpy(name, msg->data.data + i, len - 1);
            name[len - 1] = '\0';
        }
        i += len - 1;
      }

      print_bdaddr(msg->sender);
      // printf(" RSSI:%d", msg->rssi);

      fprintf(stderr, " Name:");
      if (name) { fprintf(stderr, "%s", name); }
      else { fprintf(stderr, "Unknown"); }
      fprintf(stderr, "\n");

      free(name);
    }

    // automatically connect if responding device has appropriate mac address header
    // check if bluegiga drone and connectable
    if (connect_all && msg->packet_type == 0 && cmp_addr(msg->sender.addr, MAC_ADDR) >= 3) {
	uint8 i = 0;
    	while(i++ < MAX_DEVICES)
    	{
    	  if (!cmp_addr(msg->sender.addr, connected_addr[i].addr))
    	    return;
    	}
    
      fprintf(stderr, "Trying to connect to "); print_bdaddr(msg->sender); fprintf(stderr, "\n");
      //change_state(state_connecting);
      // connection interval unit 1.25ms
      // connection interval must be divisible by number of connection * 2.5ms and larger than minimum (7.5ms)

      // send "gap_connect_direct" command
      // arguments:
      //  - MAC address
      //  - use detected address type (will work with either public or private addressing)
      //  - 6 = 6*1.25ms = 7.5ms minimum connection interval
      //  - 48 = 16*1.25ms = 20ms maximum connection interval
      //  - 100 = 100*10ms = 1000ms supervision timeout
      //  - 9 = 9 connection interval max slave latency
      ble_cmd_gap_connect_direct(&msg->sender.addr, gap_address_type_public, 8, 16, 100, 0);
    }
  }
}
示例#7
0
/**
 * "gap_scan_response" event handler
 * Occurs whenever an advertisement packet is detected while scanning
 * (see "gap_discover" command in API reference guide)
 *
 * @param msg Event packet data payload
 */
void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)
{
  if (action == action_broadcast) {
    fprintf(stderr, "advertisement from: ");
    print_bdaddr(msg->sender);
    fprintf(stderr, " data: ");
    int i;
    for (i = 0; i < msg->data.len; i++) {
      fprintf(stderr, "%02x ", msg->data.data[i]);
    }
    fprintf(stderr, "\n");
    if (sock[0])
      sendto(sock[0], msg->data.data, msg->data.len, MSG_DONTWAIT,
             (struct sockaddr *)&send_addr[0], sizeof(struct sockaddr));
  } else {
    uint8_t i, j;
    char *name = NULL;

    // Check if this device already found, if not add to the list

    if (!connect_all) {
      fprintf(stderr, "New device found: ");

      // Parse data
      for (i = 0; i < msg->data.len;) {
        int8 len = msg->data.data[i++];
        if (!len) { continue; }
        if (i + len > msg->data.len) { break; } // not enough data
        uint8 type = msg->data.data[i++];
        switch (type) {
          case 0x01:
            // flags field
            break;

          case 0x02:
            // partial list of 16-bit UUIDs
          case 0x03:
            // complete list of 16-bit UUIDs
            /*
            for (j = 0; j < len - 1; j += 2)
            {
                // loop through UUIDs 2 bytes at a time
                uint16 test_uuid = msg -> data.data[i + j] + (msg -> data.data[i + j + 1] << 8);
                if (test_uuid == THERMOMETER_SERVICE_UUID)
                {
                    // found the thermometer service UUID in the list of advertised UUIDs!
                    matching_uuid = 1;
                }
            }
            */
            break;

          case 0x04:
            // partial list of 32-bit UUIDs
          case 0x05:
            // complete list of 32-bit UUIDs
            for (j = 0; j < len - 1; j += 4) {
              // loop through UUIDs 4 bytes at a time
              // TODO: test for desired UUID here, if 32-bit UUID
            }
            break;

          case 0x06:
            // partial list of 128-bit UUIDs
          case 0x07:
            // complete list of 128-bit UUIDs
            for (j = 0; j < len - 1; j += 16) {
              // loop through UUIDs 16 bytes at a time
              // TODO: test for desired UUID here, if 128-bit UUID
            }
            break;
          case 0x09:  // device name
            name = malloc(len);
            memcpy(name, msg->data.data + i, len - 1);
            name[len - 1] = '\0';
        }
        i += len - 1;
      }

      print_bdaddr(msg->sender);
      // printf(" RSSI:%d", msg->rssi);

      fprintf(stderr, " Name:");
      if (name) { fprintf(stderr, "%s", name); }
      else { fprintf(stderr, "Unknown"); }
      fprintf(stderr, "\n");

      free(name);
    }

    // automatically connect if responding device has appropriate mac address header
    if (connect_all && cmp_addr(msg->sender.addr, MAC_ADDR) >= 4) {
      fprintf(stderr, "Trying to connect to "); print_bdaddr(msg->sender); fprintf(stderr, "\n");
      //change_state(state_connecting);
      // connection interval unit 1.25ms
      // connection interval must be divisible by number of connection * 2.5ms and larger than minimum (7.5ms)

      // send "gap_connect_direct" command
      // arguments:
      //  - MAC address
      //  - use detected address type (will work with either public or private addressing)
      //  - 6 = 6*1.25ms = 7.5ms minimum connection interval
      //  - 48 = 16*1.25ms = 20ms maximum connection interval
      //  - 100 = 100*10ms = 1000ms supervision timeout
      //  - 9 = 9 connection interval max slave latency
      ble_cmd_gap_connect_direct(&msg->sender.addr, gap_address_type_public, 6, 16, 100, 0);
    }
  }
}