2018-04-02 22:20:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/wireless/bluetooth/btsak/btsak_advertise.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 22:20:53 +02:00
|
|
|
*
|
2021-06-09 16:39:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-04-02 22:20:53 +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 22:20:53 +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 22:20:53 +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 22:20:53 +02:00
|
|
|
|
2018-04-29 20:44:03 +02:00
|
|
|
#include <nuttx/wireless/bluetooth/bt_core.h>
|
|
|
|
#include <nuttx/wireless/bluetooth/bt_hci.h>
|
|
|
|
#include <nuttx/wireless/bluetooth/bt_ioctl.h>
|
2018-04-02 22:20:53 +02:00
|
|
|
|
|
|
|
#include "btsak.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_advertise_showusage
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Show usage of the advertise command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_advertise_showusage(FAR const char *progname,
|
|
|
|
FAR const char *cmd, int exitcode)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Advertise commands:\n", cmd);
|
|
|
|
fprintf(stderr, "Usage:\n\n");
|
|
|
|
fprintf(stderr, "\t%s <ifname> %s [-h] <start|stop>\n",
|
|
|
|
progname, cmd);
|
|
|
|
fprintf(stderr, "\nWhere the options do the following:\n\n");
|
|
|
|
fprintf(stderr, "\tstart\t- Starts advertising (type ADV_IND).\n");
|
|
|
|
fprintf(stderr, "\tstop\t- Stops advertising\n");
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_cmd_advertisestart
|
|
|
|
*
|
|
|
|
* Description:
|
2018-04-06 17:43:29 +02:00
|
|
|
* Advertise start command
|
2018-04-02 22:20:53 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-09 17:29:09 +02:00
|
|
|
static void btsak_cmd_advertisestart(FAR struct btsak_s *btsak,
|
|
|
|
FAR char *cmd,
|
2018-04-02 22:20:53 +02:00
|
|
|
int argc, FAR char *argv[])
|
|
|
|
{
|
2018-04-06 19:48:19 +02:00
|
|
|
struct btreq_s btreq;
|
2018-04-06 17:43:29 +02:00
|
|
|
struct bt_eir_s ad[2]; /* Data for advertisement packets */
|
|
|
|
struct bt_eir_s sd[2]; /* Data for scan response packets */
|
2018-04-02 22:20:53 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* REVISIT: Should support all advertising type. Only ADV_IND is
|
|
|
|
* supported:
|
|
|
|
*
|
2018-08-13 15:47:26 +02:00
|
|
|
* ADV_IND
|
2018-04-02 22:20:53 +02:00
|
|
|
* Known as Advertising Indications (ADV_IND), where a peripheral device
|
|
|
|
* requests connection to any central device (i.e., not directed at a
|
2018-08-13 15:47:26 +02:00
|
|
|
* particular central device).
|
|
|
|
* Example: A smart watch requesting connection to any central device.
|
|
|
|
* ADV_DIRECT_IND
|
2018-04-02 22:20:53 +02:00
|
|
|
* Similar to ADV_IND, yet the connection request is directed at a
|
|
|
|
* specific central device.
|
|
|
|
* Example: A smart watch requesting connection to a specific central
|
|
|
|
* device.
|
2018-08-13 15:47:26 +02:00
|
|
|
* ADV_NONCONN_IND
|
2018-04-02 22:20:53 +02:00
|
|
|
* Non connectible devices, advertising information to any listening
|
|
|
|
* device.
|
|
|
|
* Example: Beacons in museums defining proximity to specific exhibits.
|
2018-08-13 15:47:26 +02:00
|
|
|
* ADV_SCAN_IND
|
2018-04-02 22:20:53 +02:00
|
|
|
* Similar to ADV_NONCONN_IND, with the option additional information via
|
2018-08-13 15:47:26 +02:00
|
|
|
* scan responses.
|
2018-04-02 22:20:53 +02:00
|
|
|
* Example: A warehouse pallet beacon allowing a central device to
|
2018-08-13 15:47:26 +02:00
|
|
|
* request additional information about the pallet.
|
2018-04-02 22:20:53 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-06 17:43:29 +02:00
|
|
|
/* The data for advertisement and response packets are provided as arrays
|
|
|
|
* terminated by an entry with len=2.
|
|
|
|
*
|
|
|
|
* REVISIT: To be useful for anything other than testing, there must
|
|
|
|
* be some mechanism to specify the advertise and response data.
|
|
|
|
*/
|
2018-04-02 22:43:57 +02:00
|
|
|
|
2018-04-06 17:43:29 +02:00
|
|
|
memset(&ad, 0, 2 * sizeof(struct bt_eir_s));
|
|
|
|
ad[0].len = 2;
|
|
|
|
ad[0].type = BT_EIR_FLAGS;
|
|
|
|
ad[0].data[0] = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;
|
2018-04-02 22:43:57 +02:00
|
|
|
|
2018-04-06 17:43:29 +02:00
|
|
|
memset(&sd, 0, 2 * sizeof(struct bt_eir_s));
|
|
|
|
sd[1].len = sizeof("btsak");
|
|
|
|
sd[1].type = BT_EIR_NAME_COMPLETE;
|
|
|
|
strcpy((FAR char *)sd[1].data, "btsak");
|
2018-04-02 22:43:57 +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-06 17:43:29 +02:00
|
|
|
btreq.btr_advtype = BT_LE_ADV_IND;
|
|
|
|
btreq.btr_advad = ad;
|
|
|
|
btreq.btr_advsd = sd;
|
2018-04-02 22:20:53 +02:00
|
|
|
|
|
|
|
/* Perform the IOCTL to start advertising */
|
|
|
|
|
|
|
|
sockfd = btsak_socket(btsak);
|
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
ret = ioctl(sockfd, SIOCBTADVSTART,
|
|
|
|
(unsigned long)((uintptr_t)&btreq));
|
2018-04-02 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: ioctl(SIOCBTADVSTART) failed: %d\n",
|
2018-04-02 22:20:53 +02:00
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btsak_cmd_advertisestop
|
|
|
|
*
|
|
|
|
* Description:
|
2018-04-06 17:43:29 +02:00
|
|
|
* Advertise stop command
|
2018-04-02 22:20:53 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void btsak_cmd_advertisestop(FAR struct btsak_s *btsak, FAR char *cmd,
|
2018-04-06 17:43:29 +02:00
|
|
|
int argc, FAR char *argv[])
|
2018-04-02 22:20:53 +02:00
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
struct btreq_s btreq;
|
2018-04-02 22:20:53 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Perform the IOCTL to stop advertising */
|
|
|
|
|
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 22:20:53 +02:00
|
|
|
|
|
|
|
sockfd = btsak_socket(btsak);
|
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
ret = ioctl(sockfd, SIOCBTADVSTOP,
|
|
|
|
(unsigned long)((uintptr_t)&btreq));
|
2018-04-02 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-04-06 17:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: ioctl(SIOCBTADVSTOP) failed: %d\n",
|
2018-04-02 22:20:53 +02:00
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-04-03 01:40:41 +02:00
|
|
|
* Name: btsak_cmd_advertise
|
2018-04-02 22:20:53 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-04-03 01:40:41 +02:00
|
|
|
* advertise [-h] <start [-d] |get|stop> command
|
2018-04-02 22:20:53 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-09 17:29:09 +02:00
|
|
|
void btsak_cmd_advertise(FAR struct btsak_s *btsak,
|
|
|
|
int argc, FAR char *argv[])
|
2018-04-02 22:20:53 +02:00
|
|
|
{
|
|
|
|
int argind;
|
|
|
|
|
|
|
|
/* Verify that an option was provided */
|
|
|
|
|
|
|
|
argind = 1;
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing advertise command\n");
|
|
|
|
btsak_advertise_showusage(btsak->progname, argv[0], EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for command */
|
|
|
|
|
|
|
|
if (strcmp(argv[argind], "-h") == 0)
|
|
|
|
{
|
|
|
|
btsak_advertise_showusage(btsak->progname, argv[0], EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[argind], "start") == 0)
|
|
|
|
{
|
|
|
|
btsak_cmd_advertisestart(btsak, argv[0], argc - argind, &argv[argind]);
|
|
|
|
}
|
2018-04-03 01:40:41 +02:00
|
|
|
else if (strcmp(argv[argind], "stop") == 0)
|
2018-04-02 22:20:53 +02:00
|
|
|
{
|
|
|
|
btsak_cmd_advertisestop(btsak, argv[0], argc - argind, &argv[argind]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-09 17:29:09 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Unrecognized advertise command: %s\n", argv[argind]);
|
2018-04-02 22:20:53 +02:00
|
|
|
btsak_advertise_showusage(btsak->progname, argv[0], EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|