2018-04-02 21:05:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/wireless/bluetooth/btsak/btsak_scan.c
|
|
|
|
*
|
2021-06-09 16:39:27 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-04-02 21:05:09 +02:00
|
|
|
*
|
2021-06-09 16:39:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-04-02 21:05:09 +02:00
|
|
|
*
|
2021-06-09 16:39:27 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-04-02 21:05:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-09 16:39:27 +02:00
|
|
|
/* Based loosely on the i8sak IEEE 802.15.4 program by Anthony Merlino and
|
|
|
|
* Sebastien Lorquet. Commands inspired from btshell example in the
|
|
|
|
* Intel/Zephyr Arduino 101 package (BSD license).
|
|
|
|
*/
|
|
|
|
|
2018-04-02 21:05:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <errno.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2018-04-02 21:05:09 +02:00
|
|
|
|
2018-04-29 20:44:03 +02:00
|
|
|
#include <nuttx/wireless/bluetooth/bt_ioctl.h>
|
2018-04-02 21:05:09 +02:00
|
|
|
|
|
|
|
#include "btsak.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_scan_showusage
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Show usage of the scan command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_scan_showusage(FAR const char *progname,
|
|
|
|
FAR const char *cmd, int exitcode)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Scan commands:\n", cmd);
|
|
|
|
fprintf(stderr, "Usage:\n\n");
|
|
|
|
fprintf(stderr, "\t%s <ifname> %s [-h] <start [-d]|get|stop>\n",
|
|
|
|
progname, cmd);
|
|
|
|
fprintf(stderr, "\nWhere the options do the following:\n\n");
|
2021-06-09 17:29:09 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"\tstart\t- Starts scanning. The -d option enables duplicate\n");
|
2018-04-02 21:05:09 +02:00
|
|
|
fprintf(stderr, "\t\t filtering.\n");
|
2018-04-21 20:18:17 +02:00
|
|
|
fprintf(stderr, "\tget\t- Shows new accumulated scan results\n");
|
2018-04-02 21:05:09 +02:00
|
|
|
fprintf(stderr, "\tstop\t- Stops scanning\n");
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_cmd_scanstart
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Scan start command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_cmd_scanstart(FAR struct btsak_s *btsak, FAR char *cmd,
|
|
|
|
int argc, FAR char *argv[])
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
struct btreq_s btreq;
|
2018-04-02 21:05:09 +02:00
|
|
|
int argind;
|
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
2018-04-06 19:48:19 +02:00
|
|
|
memset(&btreq, 0, sizeof(struct btreq_s));
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(btreq.btr_name, btsak->ifname, IFNAMSIZ);
|
2018-04-06 17:43:29 +02:00
|
|
|
|
2018-04-02 21:05:09 +02:00
|
|
|
/* Check if an option was provided */
|
|
|
|
|
|
|
|
argind = 1;
|
2018-04-06 17:43:29 +02:00
|
|
|
btreq.btr_dupenable = false;
|
2018-04-02 21:05:09 +02:00
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[argind], "-d") == 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
btreq.btr_dupenable = true;
|
2018-04-02 21:05:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized option: %s\n",
|
|
|
|
argv[argind]);
|
|
|
|
btsak_scan_showusage(btsak->progname, cmd, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the IOCTL to start scanning */
|
|
|
|
|
|
|
|
sockfd = btsak_socket(btsak);
|
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
ret = ioctl(sockfd, SIOCBTSCANSTART,
|
|
|
|
(unsigned long)((uintptr_t)&btreq));
|
2018-04-02 21:05:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: ioctl(SIOCBTSCANSTART) failed: %d\n",
|
2018-04-02 21:05:09 +02:00
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_cmd_scanget
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Scan get command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_cmd_scanget(FAR struct btsak_s *btsak, FAR char *cmd,
|
|
|
|
int argc, FAR char *argv[])
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
struct btreq_s btreq;
|
|
|
|
struct bt_scanresponse_s result[5];
|
2018-04-02 21:05:09 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Perform the IOCTL to get the scan results so far */
|
|
|
|
|
2018-04-06 19:48:19 +02:00
|
|
|
memset(&btreq, 0, sizeof(struct btreq_s));
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(btreq.btr_name, btsak->ifname, IFNAMSIZ);
|
2018-04-06 19:48:19 +02:00
|
|
|
btreq.btr_nrsp = 5;
|
|
|
|
btreq.btr_rsp = result;
|
2018-04-02 21:05:09 +02:00
|
|
|
|
|
|
|
sockfd = btsak_socket(btsak);
|
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
ret = ioctl(sockfd, SIOCBTSCANGET,
|
|
|
|
(unsigned long)((uintptr_t)&btreq));
|
2018-04-02 21:05:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: ioctl(SIOCBTSCANGET) failed: %d\n",
|
2018-04-02 21:05:09 +02:00
|
|
|
errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show scan results */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct bt_scanresponse_s *rsp;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
printf("Scan result:\n");
|
2018-04-06 19:48:19 +02:00
|
|
|
for (i = 0; i < btreq.btr_nrsp; i++)
|
2018-04-02 21:05:09 +02:00
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
rsp = &result[i];
|
2023-06-10 18:23:35 +02:00
|
|
|
printf("%2d.\taddr: "
|
2018-04-02 21:05:09 +02:00
|
|
|
"%02x:%02x:%02x:%02x:%02x:%02x type: %d\n",
|
2018-04-19 17:15:21 +02:00
|
|
|
i + 1,
|
2018-12-17 18:12:23 +01:00
|
|
|
rsp->sr_addr.val[5], rsp->sr_addr.val[4],
|
|
|
|
rsp->sr_addr.val[3], rsp->sr_addr.val[2],
|
|
|
|
rsp->sr_addr.val[1], rsp->sr_addr.val[0],
|
2018-04-02 21:05:09 +02:00
|
|
|
rsp->sr_addr.type);
|
|
|
|
printf("\trssi: %d\n", rsp->sr_rssi);
|
|
|
|
printf("\tresponse type: %u\n", rsp->sr_type);
|
2018-04-18 21:27:27 +02:00
|
|
|
printf("\tadvertiser data:");
|
2018-04-02 21:05:09 +02:00
|
|
|
|
|
|
|
for (j = 0; j < rsp->sr_len; j += 16)
|
|
|
|
{
|
2018-04-18 21:27:27 +02:00
|
|
|
if (j > 0)
|
|
|
|
{
|
|
|
|
printf("\t ");
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:05:09 +02:00
|
|
|
for (k = 0; k < 16 && (j + k) < rsp->sr_len; k++)
|
|
|
|
{
|
|
|
|
printf(" %02x", rsp->sr_data[j + k]);
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 19:47:20 +02:00
|
|
|
|
|
|
|
printf("\n");
|
2018-04-02 21:05:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_cmd_scanstop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Scan stop command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_cmd_scanstop(FAR struct btsak_s *btsak, FAR char *cmd,
|
|
|
|
int argc, FAR char *argv[])
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
struct btreq_s btreq;
|
2018-04-02 21:05:09 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
2018-04-02 22:20:53 +02:00
|
|
|
/* Perform the IOCTL to stop scanning and flush any buffered responses. */
|
2018-04-02 21:05:09 +02:00
|
|
|
|
2018-04-06 19:48:19 +02:00
|
|
|
memset(&btreq, 0, sizeof(struct btreq_s));
|
2023-02-07 12:33:52 +01:00
|
|
|
strlcpy(btreq.btr_name, btsak->ifname, IFNAMSIZ);
|
2018-04-02 21:05:09 +02:00
|
|
|
|
|
|
|
sockfd = btsak_socket(btsak);
|
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
ret = ioctl(sockfd, SIOCBTSCANSTOP,
|
|
|
|
(unsigned long)((uintptr_t)&btreq));
|
2018-04-02 21:05:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: ioctl(SIOCBTSCANSTOP) failed: %d\n",
|
2018-04-02 21:05:09 +02:00
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-04-03 01:40:41 +02:00
|
|
|
* Name: btsak_cmd_scan
|
2018-04-02 21:05:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-04-03 01:40:41 +02:00
|
|
|
* scan [-h] <start [-d] |get|stop> command
|
2018-04-02 21:05:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void btsak_cmd_scan(FAR struct btsak_s *btsak, int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
int argind;
|
|
|
|
|
|
|
|
/* Verify that an option was provided */
|
|
|
|
|
|
|
|
argind = 1;
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing scan command\n");
|
|
|
|
btsak_scan_showusage(btsak->progname, argv[0], EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for command */
|
|
|
|
|
|
|
|
if (strcmp(argv[argind], "-h") == 0)
|
|
|
|
{
|
|
|
|
btsak_scan_showusage(btsak->progname, argv[0], EXIT_SUCCESS);
|
|
|
|
}
|
2018-04-02 22:20:53 +02:00
|
|
|
else if (strcmp(argv[argind], "start") == 0)
|
2018-04-02 21:05:09 +02:00
|
|
|
{
|
|
|
|
btsak_cmd_scanstart(btsak, argv[0], argc - argind, &argv[argind]);
|
|
|
|
}
|
2018-04-02 22:20:53 +02:00
|
|
|
else if (strcmp(argv[argind], "get") == 0)
|
2018-04-02 21:05:09 +02:00
|
|
|
{
|
|
|
|
btsak_cmd_scanget(btsak, argv[0], argc - argind, &argv[argind]);
|
|
|
|
}
|
2018-04-02 22:20:53 +02:00
|
|
|
else if (strcmp(argv[argind], "stop") == 0)
|
2018-04-02 21:05:09 +02:00
|
|
|
{
|
|
|
|
btsak_cmd_scanstop(btsak, argv[0], argc - argind, &argv[argind]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-09 17:29:09 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Unrecognized scan command: %s\n", argv[argind]);
|
2018-04-02 21:05:09 +02:00
|
|
|
btsak_scan_showusage(btsak->progname, argv[0], EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|