diff options
Diffstat (limited to 'src/libs/mynewt-nimble/apps/btshell')
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/pkg.yml | 39 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/btshell.h | 212 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd.c | 4659 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd.h | 68 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.c | 587 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.h | 39 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.c | 325 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.h | 33 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/gatt_svr.c | 638 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/main.c | 2630 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/misc.c | 163 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/src/parse.c | 734 | ||||
-rw-r--r-- | src/libs/mynewt-nimble/apps/btshell/syscfg.yml | 42 |
13 files changed, 10169 insertions, 0 deletions
diff --git a/src/libs/mynewt-nimble/apps/btshell/pkg.yml b/src/libs/mynewt-nimble/apps/btshell/pkg.yml new file mode 100644 index 00000000..29541b74 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/pkg.yml @@ -0,0 +1,39 @@ +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# +pkg.name: apps/btshell +pkg.type: app +pkg.description: Shell application exposing the nimble GAP and GATT. +pkg.author: "Apache Mynewt <dev@mynewt.apache.org>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/sys/log/full" + - "@apache-mynewt-core/sys/log/modlog" + - "@apache-mynewt-core/sys/stats/full" + - "@apache-mynewt-core/sys/console/full" + - "@apache-mynewt-core/sys/shell" + - nimble/host + - nimble/host/services/gap + - nimble/host/services/gatt + - nimble/host/store/ram + - nimble/transport + +pkg.deps.BTSHELL_ANS: + - nimble/host/services/ans diff --git a/src/libs/mynewt-nimble/apps/btshell/src/btshell.h b/src/libs/mynewt-nimble/apps/btshell/src/btshell.h new file mode 100644 index 00000000..7c978221 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/btshell.h @@ -0,0 +1,212 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef H_BTSHELL_PRIV_ +#define H_BTSHELL_PRIV_ + +#include <inttypes.h> +#include "os/mynewt.h" +#include "nimble/ble.h" +#include "nimble/nimble_opt.h" +#include "modlog/modlog.h" + +#include "host/ble_gatt.h" +#include "host/ble_gap.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_gap_white_entry; +struct ble_hs_adv_fields; +struct ble_gap_upd_params; +struct ble_gap_conn_params; +struct hci_adv_params; +struct ble_l2cap_sig_update_req; +struct ble_l2cap_sig_update_params; +union ble_store_value; +union ble_store_key; +struct ble_gap_adv_params; +struct ble_gap_conn_desc; +struct ble_gap_disc_params; + +struct btshell_dsc { + SLIST_ENTRY(btshell_dsc) next; + struct ble_gatt_dsc dsc; +}; +SLIST_HEAD(btshell_dsc_list, btshell_dsc); + +struct btshell_chr { + SLIST_ENTRY(btshell_chr) next; + struct ble_gatt_chr chr; + + struct btshell_dsc_list dscs; +}; +SLIST_HEAD(btshell_chr_list, btshell_chr); + +struct btshell_svc { + SLIST_ENTRY(btshell_svc) next; + struct ble_gatt_svc svc; + struct btshell_chr_list chrs; + bool discovered; +}; + +SLIST_HEAD(btshell_svc_list, btshell_svc); + +struct btshell_l2cap_coc { + SLIST_ENTRY(btshell_l2cap_coc) next; + struct ble_l2cap_chan *chan; + bool stalled; +}; + +SLIST_HEAD(btshell_l2cap_coc_list, btshell_l2cap_coc); + +struct btshell_conn { + uint16_t handle; + struct btshell_svc_list svcs; + struct btshell_l2cap_coc_list coc_list; +}; + +struct btshell_scan_opts { + uint16_t limit; + uint8_t ignore_legacy:1; + uint8_t periodic_only:1; +}; + +extern struct btshell_conn btshell_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)]; +extern int btshell_num_conns; + +int btshell_exchange_mtu(uint16_t conn_handle); +int btshell_disc_svcs(uint16_t conn_handle); +int btshell_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid); +int btshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle); +int btshell_disc_all_chrs_in_svc(uint16_t conn_handle, struct btshell_svc *svc); +int btshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid); +int btshell_disc_all_dscs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle); +int btshell_disc_full(uint16_t conn_handle); +int btshell_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle); +int btshell_read(uint16_t conn_handle, uint16_t attr_handle); +int btshell_read_long(uint16_t conn_handle, uint16_t attr_handle, + uint16_t offset); +int btshell_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid); +int btshell_read_mult(uint16_t conn_handle, uint16_t *attr_handles, + int num_attr_handles); +int btshell_write(uint16_t conn_handle, uint16_t attr_handle, + struct os_mbuf *om); +int btshell_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, + struct os_mbuf *om); +int btshell_write_long(uint16_t conn_handle, uint16_t attr_handle, + uint16_t offset, struct os_mbuf *om); +int btshell_write_reliable(uint16_t conn_handle, + struct ble_gatt_attr *attrs, int num_attrs); +#if MYNEWT_VAL(BLE_EXT_ADV) +int btshell_ext_adv_configure(uint8_t instance, + const struct ble_gap_ext_adv_params *params, + int8_t *selected_tx_power); +int btshell_ext_adv_start(uint8_t instance, int duration, + int max_events, bool restart); +int btshell_ext_adv_stop(uint8_t instance); +#endif +int btshell_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr, + int32_t duration_ms, + const struct ble_gap_adv_params *params, + bool restart); +int btshell_adv_stop(void); +int btshell_conn_initiate(uint8_t own_addr_type, const ble_addr_t *peer_addr, + int32_t duration_ms, + struct ble_gap_conn_params *params); +int btshell_ext_conn_initiate(uint8_t own_addr_type, + const ble_addr_t *peer_addr, + int32_t duration_ms, + struct ble_gap_conn_params *phy_1m_params, + struct ble_gap_conn_params *phy_2m_params, + struct ble_gap_conn_params *phy_coded_params); +int btshell_conn_cancel(void); +int btshell_term_conn(uint16_t conn_handle, uint8_t reason); +int btshell_wl_set(ble_addr_t *addrs, int addrs_count); +int btshell_scan(uint8_t own_addr_type, int32_t duration_ms, + const struct ble_gap_disc_params *disc_params, void *cb_args); +int btshell_ext_scan(uint8_t own_addr_type, uint16_t duration, uint16_t period, + uint8_t filter_duplicates, uint8_t filter_policy, + uint8_t limited, + const struct ble_gap_ext_disc_params *uncoded_params, + const struct ble_gap_ext_disc_params *coded_params, + void *cb_args); +int btshell_scan_cancel(void); +int btshell_update_conn(uint16_t conn_handle, + struct ble_gap_upd_params *params); +void btshell_notify(uint16_t attr_handle); +int btshell_datalen(uint16_t conn_handle, uint16_t tx_octets, + uint16_t tx_time); +int btshell_l2cap_update(uint16_t conn_handle, + struct ble_l2cap_sig_update_params *params); +int btshell_sec_start(uint16_t conn_handle); +int btshell_sec_pair(uint16_t conn_handle); +int btshell_sec_unpair(ble_addr_t *peer_addr); +int btshell_sec_restart(uint16_t conn_handle, uint8_t key_size, + uint8_t *ltk, uint16_t ediv, + uint64_t rand_val, int auth); +int btshell_tx_start(uint16_t conn_handle, uint16_t len, uint16_t rate, + uint16_t num); +void btshell_tx_stop(void); +int btshell_rssi(uint16_t conn_handle, int8_t *out_rssi); +int btshell_l2cap_create_srv(uint16_t psm, uint16_t mtu, int accept_response); +int btshell_l2cap_connect(uint16_t conn, uint16_t psm, uint16_t mtu, uint8_t num); +int btshell_l2cap_disconnect(uint16_t conn, uint16_t idx); +int btshell_l2cap_send(uint16_t conn, uint16_t idx, uint16_t bytes); +int btshell_l2cap_reconfig(uint16_t conn_handle, uint16_t mtu, + uint8_t num, uint8_t idxs[]); + +int btshell_gap_event(struct ble_gap_event *event, void *arg); +void btshell_sync_stats(uint16_t handle); + +/** GATT server. */ +#define GATT_SVR_SVC_ALERT_UUID 0x1811 +#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47 +#define GATT_SVR_CHR_NEW_ALERT 0x2A46 +#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48 +#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID 0x2A45 +#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT 0x2A44 + +void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); +int gatt_svr_init(void); +void gatt_svr_print_svcs(void); + +/** Misc. */ +void print_bytes(const uint8_t *bytes, int len); +void print_mbuf(const struct os_mbuf *om); +void print_addr(const void *addr); +void print_uuid(const ble_uuid_t *uuid); +int svc_is_empty(const struct btshell_svc *svc); +uint16_t chr_end_handle(const struct btshell_svc *svc, + const struct btshell_chr *chr); +int chr_is_empty(const struct btshell_svc *svc, const struct btshell_chr *chr); +void print_conn_desc(const struct ble_gap_conn_desc *desc); +void print_svc(struct btshell_svc *svc); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd.c b/src/libs/mynewt-nimble/apps/btshell/src/cmd.c new file mode 100644 index 00000000..8a878756 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd.c @@ -0,0 +1,4659 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <assert.h> +#include <inttypes.h> +#include <errno.h> +#include <string.h> +#include "os/mynewt.h" +#include "bsp/bsp.h" + +#include "nimble/ble.h" +#include "nimble/nimble_opt.h" +#include "nimble/hci_common.h" +#include "host/ble_gap.h" +#include "host/ble_hs_adv.h" +#include "host/ble_sm.h" +#include "host/ble_eddystone.h" +#include "host/ble_hs_id.h" +#include "services/gatt/ble_svc_gatt.h" +#include "../src/ble_hs_priv.h" + +#include "console/console.h" +#include "shell/shell.h" + +#include "cmd.h" +#include "btshell.h" +#include "cmd_gatt.h" +#include "cmd_l2cap.h" + +#define BTSHELL_MODULE "btshell" + +int +cmd_parse_conn_start_end(uint16_t *out_conn, uint16_t *out_start, + uint16_t *out_end) +{ + int rc; + + *out_conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + return rc; + } + + *out_start = parse_arg_uint16("start", &rc); + if (rc != 0) { + return rc; + } + + *out_end = parse_arg_uint16("end", &rc); + if (rc != 0) { + return rc; + } + + return 0; +} + +static const struct kv_pair cmd_own_addr_types[] = { + { "public", BLE_OWN_ADDR_PUBLIC }, + { "random", BLE_OWN_ADDR_RANDOM }, + { "rpa_pub", BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT }, + { "rpa_rnd", BLE_OWN_ADDR_RPA_RANDOM_DEFAULT }, + { NULL } +}; + +static const struct kv_pair cmd_peer_addr_types[] = { + { "public", BLE_ADDR_PUBLIC }, + { "random", BLE_ADDR_RANDOM }, + { "public_id", BLE_ADDR_PUBLIC_ID }, + { "random_id", BLE_ADDR_RANDOM_ID }, + { NULL } +}; + +static const struct kv_pair cmd_addr_type[] = { + { "public", BLE_ADDR_PUBLIC }, + { "random", BLE_ADDR_RANDOM }, + { NULL } +}; + + +static int +parse_dev_addr(const char *prefix, const struct kv_pair *addr_types, + ble_addr_t *addr) +{ + char name[32]; + int rc; + + /* XXX string operations below are not quite safe, but do we care? */ + + if (!prefix) { + name[0] = '\0'; + } else { + strcpy(name, prefix); + } + + strcat(name, "addr"); + rc = parse_arg_addr(name, addr); + if (rc == ENOENT) { + /* not found */ + return rc; + } else if (rc == EAGAIN) { + /* address found, but no type provided */ + strcat(name, "_type"); + addr->type = parse_arg_kv(name, addr_types, &rc); + if (rc == ENOENT) { + addr->type = BLE_ADDR_PUBLIC; + } else if (rc != 0) { + return rc; + } + } else if (rc != 0) { + /* error parsing address */ + return rc; + } else { + /* full address found, but let's just make sure there is no type arg */ + strcat(name, "_type"); + if (parse_arg_extract(name)) { + return E2BIG; + } + } + + return 0; +} + +/***************************************************************************** + * $advertise * + *****************************************************************************/ +static const struct kv_pair cmd_adv_filt_types[] = { + { "none", BLE_HCI_ADV_FILT_NONE }, + { "scan", BLE_HCI_ADV_FILT_SCAN }, + { "conn", BLE_HCI_ADV_FILT_CONN }, + { "both", BLE_HCI_ADV_FILT_BOTH }, + { NULL } +}; + +#if MYNEWT_VAL(BLE_EXT_ADV) +static struct kv_pair cmd_ext_adv_phy_opts[] = { + { "1M", 0x01 }, + { "2M", 0x02 }, + { "coded", 0x03 }, + { NULL } +}; + +static int +cmd_advertise_configure(int argc, char **argv) +{ + struct ble_gap_ext_adv_params params = {0}; + int8_t selected_tx_power; + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + memset(¶ms, 0, sizeof(params)); + + params.legacy_pdu = parse_arg_bool_dflt("legacy", 0, &rc); + if (rc != 0) { + console_printf("invalid 'legacy' parameter\n"); + return rc; + } + + if (params.legacy_pdu) { + params.connectable = 1; + params.scannable = 1; + } + + params.connectable = parse_arg_bool_dflt("connectable", params.connectable, &rc); + if (rc != 0) { + console_printf("invalid 'connectable' parameter\n"); + return rc; + } + + params.scannable = parse_arg_bool_dflt("scannable", params.scannable, &rc); + if (rc != 0) { + console_printf("invalid 'scannable' parameter\n"); + return rc; + } + + params.high_duty_directed = parse_arg_bool_dflt("high_duty", 0, &rc); + if (rc != 0) { + console_printf("invalid 'high_duty' parameter\n"); + return rc; + } + + params.anonymous = parse_arg_bool_dflt("anonymous", 0, &rc); + if (rc != 0) { + console_printf("invalid 'anonymous' parameter\n"); + return rc; + } + + params.include_tx_power = parse_arg_bool_dflt("include_tx_power", 0, &rc); + if (rc != 0) { + console_printf("invalid 'include_tx_power' parameter\n"); + return rc; + } + + params.scan_req_notif = parse_arg_bool_dflt("scan_req_notif", 0, &rc); + if (rc != 0) { + console_printf("invalid 'scan_req_notif' parameter\n"); + return rc; + } + + rc = parse_dev_addr("peer_", cmd_peer_addr_types, ¶ms.peer); + if (rc == 0) { + params.directed = 1; + } else if (rc == ENOENT) { + /* skip, no peer address provided */ + } else { + console_printf("invalid 'peer_addr' parameter\n"); + return rc; + } + + + params.directed = parse_arg_bool_dflt("directed", params.directed, &rc); + if (rc != 0) { + console_printf("invalid 'directed' parameter\n"); + return rc; + } + + if (params.directed && params.legacy_pdu) { + params.scannable = 0; + } + + params.own_addr_type = parse_arg_kv_dflt("own_addr_type", + cmd_own_addr_types, + BLE_OWN_ADDR_PUBLIC, &rc); + if (rc != 0) { + console_printf("invalid 'own_addr_type' parameter\n"); + return rc; + } + + params.channel_map = parse_arg_uint8_dflt("channel_map", 0, &rc); + if (rc != 0) { + console_printf("invalid 'channel_map' parameter\n"); + return rc; + } + + params.filter_policy = parse_arg_kv_dflt("filter", cmd_adv_filt_types, + BLE_HCI_ADV_FILT_NONE, &rc); + if (rc != 0) { + console_printf("invalid 'filter' parameter\n"); + return rc; + } + + params.itvl_min = parse_arg_time_dflt("interval_min", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + params.itvl_max = parse_arg_time_dflt("interval_max", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + params.tx_power = parse_arg_long_bounds_dflt("tx_power", + -127, 127, 127, &rc); + if (rc != 0) { + console_printf("invalid 'tx_power' parameter\n"); + return rc; + } + + params.primary_phy = parse_arg_kv_dflt("primary_phy", cmd_ext_adv_phy_opts, + 1, &rc); + if (rc != 0) { + console_printf("invalid 'primary_phy' parameter\n"); + return rc; + } + + params.secondary_phy = parse_arg_kv_dflt("secondary_phy", + cmd_ext_adv_phy_opts, + params.primary_phy, &rc); + if (rc != 0) { + console_printf("invalid 'secondary_phy' parameter\n"); + return rc; + } + + params.sid = parse_arg_uint8_dflt("sid", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sid' parameter\n"); + return rc; + } + + rc = btshell_ext_adv_configure(instance, ¶ms, &selected_tx_power); + if (rc) { + console_printf("failed to configure advertising instance\n"); + return rc; + } + + console_printf("Instance %u configured (selected tx power: %d)\n", + instance, selected_tx_power); + + return 0; +} + +static int +cmd_advertise_set_addr(int argc, char **argv) +{ + ble_addr_t addr; + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + rc = parse_arg_mac("addr", addr.val); + if (rc != 0) { + console_printf("invalid 'addr' parameter\n"); + return rc; + } + + addr.type = BLE_ADDR_RANDOM; + + rc = ble_gap_ext_adv_set_addr(instance, &addr); + if (rc) { + console_printf("failed to start advertising instance\n"); + return rc; + } + + return 0; +} + +static int +cmd_advertise_start(int argc, char **argv) +{ + int max_events; + uint8_t instance; + int duration; + bool restart; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + duration = parse_arg_uint16_dflt("duration", 0, &rc); + if (rc != 0) { + console_printf("invalid 'duration' parameter\n"); + return rc; + } + + max_events = parse_arg_uint8_dflt("max_events", 0, &rc); + if (rc != 0) { + console_printf("invalid 'max_events' parameter\n"); + return rc; + } + + restart = parse_arg_bool_dflt("restart", 0, &rc); + if (rc != 0) { + console_printf("invalid 'restart' parameter\n"); + return rc; + } + + rc = btshell_ext_adv_start(instance, duration, max_events, restart); + if (rc) { + console_printf("failed to start advertising instance\n"); + return rc; + } + + return 0; +} + +static int +cmd_advertise_stop(int argc, char **argv) +{ + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + rc = btshell_ext_adv_stop(instance); + if (rc) { + console_printf("failed to stop advertising instance\n"); + return rc; + } + + return 0; +} + +static int +cmd_advertise_remove(int argc, char **argv) +{ + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + rc = ble_gap_ext_adv_remove(instance); + if (rc) { + console_printf("failed to remove advertising instance\n"); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param advertise_configure_params[] = { + {"instance", "default: 0"}, + {"connectable", "connectable advertising, usage: =[0-1], default: 0"}, + {"scannable", "scannable advertising, usage: =[0-1], default: 0"}, + {"directed", "directed advertising, usage: =[0-1], default: 0"}, + {"peer_addr_type", "usage: =[public|random|public_id|random_id], default: public"}, + {"peer_addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {"own_addr_type", "usage: =[public|random|rpa_pub|rpa_rnd], default: public"}, + {"channel_map", "usage: =[0x00-0xff], default: 0"}, + {"filter", "usage: =[none|scan|conn|both], default: none"}, + {"interval_min", "usage: =[0-UINT32_MAX], default: 0"}, + {"interval_max", "usage: =[0-UINT32_MAX], default: 0"}, + {"tx_power", "usage: =[-127-127], default: 127"}, + {"primary_phy", "usage: =[1M|coded], default: 1M"}, + {"secondary_phy", "usage: =[1M|2M|coded], default: primary_phy"}, + {"sid", "usage: =[0-UINT8_MAX], default: 0"}, + {"high_duty", "usage: =[0-1], default: 0"}, + {"anonymous", "enable anonymous advertising, usage: =[0-1], default: 0"}, + {"legacy", "use legacy PDUs, usage: =[0-1], default: 0"}, + {"include_tx_power", "include TX power in PDU, usage: =[0-1], default: 0"}, + {"scan_req_notif", "enable Scan Request notification usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_configure_help = { + .summary = "configure new advertising instance", + .usage = NULL, + .params = advertise_configure_params, +}; + +static const struct shell_param advertise_set_addr_params[] = { + {"instance", "default: 0"}, + {"addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_set_addr_help = { + .summary = "set advertising instance random address", + .usage = NULL, + .params = advertise_set_addr_params, +}; + +static const struct shell_param advertise_start_params[] = { + {"instance", "default: 0"}, + {"duration", "advertising duration in 10ms units, default: 0 (forever)"}, + {"max_events", "max number of advertising events, default: 0 (no limit)"}, + {"restart", "restart advertising after disconnect, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_start_help = { + .summary = "start advertising instance", + .usage = NULL, + .params = advertise_start_params, +}; + +static const struct shell_param advertise_stop_params[] = { + {"instance", "default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_stop_help = { + .summary = "stop advertising instance", + .usage = NULL, + .params = advertise_stop_params, +}; + +static const struct shell_param advertise_remove_params[] = { + {"instance", "default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_remove_help = { + .summary = "remove advertising instance", + .usage = NULL, + .params = advertise_remove_params, +}; +#endif + +#else +static const struct kv_pair cmd_adv_conn_modes[] = { + { "non", BLE_GAP_CONN_MODE_NON }, + { "und", BLE_GAP_CONN_MODE_UND }, + { "dir", BLE_GAP_CONN_MODE_DIR }, + { NULL } +}; + +static const struct kv_pair cmd_adv_disc_modes[] = { + { "non", BLE_GAP_DISC_MODE_NON }, + { "ltd", BLE_GAP_DISC_MODE_LTD }, + { "gen", BLE_GAP_DISC_MODE_GEN }, + { NULL } +}; + +static int +cmd_advertise(int argc, char **argv) +{ + struct ble_gap_adv_params params; + int32_t duration_ms; + ble_addr_t peer_addr; + ble_addr_t *peer_addr_param = &peer_addr; + uint8_t own_addr_type; + bool restart; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + if (argc > 1 && strcmp(argv[1], "stop") == 0) { + rc = btshell_adv_stop(); + if (rc != 0) { + console_printf("advertise stop fail: %d\n", rc); + return rc; + } + + return 0; + } + + params.conn_mode = parse_arg_kv_dflt("conn", cmd_adv_conn_modes, + BLE_GAP_CONN_MODE_UND, &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + params.disc_mode = parse_arg_kv_dflt("discov", cmd_adv_disc_modes, + BLE_GAP_DISC_MODE_GEN, &rc); + if (rc != 0) { + console_printf("invalid 'discov' parameter\n"); + return rc; + } + + rc = parse_dev_addr("peer_", cmd_peer_addr_types, &peer_addr); + if (rc == ENOENT) { + peer_addr_param = NULL; + } else if (rc != 0) { + console_printf("invalid 'peer_addr' parameter\n"); + return rc; + } + + restart = parse_arg_bool_dflt("restart", 0, &rc); + if (rc != 0) { + console_printf("invalid 'restart' parameter\n"); + return rc; + } + + own_addr_type = parse_arg_kv_dflt("own_addr_type", cmd_own_addr_types, + BLE_OWN_ADDR_PUBLIC, &rc); + if (rc != 0) { + console_printf("invalid 'own_addr_type' parameter\n"); + return rc; + } + + params.channel_map = parse_arg_uint8_dflt("channel_map", 0, &rc); + if (rc != 0) { + console_printf("invalid 'channel_map' parameter\n"); + return rc; + } + + params.filter_policy = parse_arg_kv_dflt("filter", cmd_adv_filt_types, + BLE_HCI_ADV_FILT_NONE, &rc); + if (rc != 0) { + console_printf("invalid 'filter' parameter\n"); + return rc; + } + + params.itvl_min = parse_arg_time_dflt("interval_min", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + params.itvl_max = parse_arg_time_dflt("interval_max", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + params.high_duty_cycle = parse_arg_bool_dflt("high_duty", 0, &rc); + if (rc != 0) { + console_printf("invalid 'high_duty' parameter\n"); + return rc; + } + + duration_ms = parse_arg_long_bounds_dflt("duration", 1, INT32_MAX, + BLE_HS_FOREVER, &rc); + if (rc != 0) { + console_printf("invalid 'duration' parameter\n"); + return rc; + } + + rc = btshell_adv_start(own_addr_type, peer_addr_param, duration_ms, + ¶ms, restart); + if (rc != 0) { + console_printf("advertise fail: %d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param advertise_params[] = { + {"stop", "stop advertising procedure"}, + {"conn", "connectable mode, usage: =[non|und|dir], default: und"}, + {"discov", "discoverable mode, usage: =[non|ltd|gen], default: gen"}, + {"peer_addr_type", "usage: =[public|random|public_id|random_id], default: public"}, + {"peer_addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {"own_addr_type", "usage: =[public|random|rpa_pub|rpa_rnd], default: public"}, + {"channel_map", "usage: =[0x00-0xff], default: 0"}, + {"filter", "usage: =[none|scan|conn|both], default: none"}, + {"interval_min", "usage: =[0-UINT16_MAX], default: 0"}, + {"interval_max", "usage: =[0-UINT16_MAX], default: 0"}, + {"high_duty", "usage: =[0-1], default: 0"}, + {"duration", "usage: =[1-INT32_MAX], default: INT32_MAX"}, + {"restart", "restart advertising after disconnect, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help advertise_help = { + .summary = "start/stop advertising with specific parameters", + .usage = NULL, + .params = advertise_params, +}; +#endif +#endif + +/***************************************************************************** + * $connect * + *****************************************************************************/ + +static struct kv_pair cmd_ext_conn_phy_opts[] = { + { "none", 0x00 }, + { "1M", 0x01 }, + { "coded", 0x02 }, + { "both", 0x03 }, + { "all", 0x04 }, + { NULL } +}; + +static int +cmd_connect(int argc, char **argv) +{ + struct ble_gap_conn_params phy_1M_params = {0}; + struct ble_gap_conn_params phy_coded_params = {0}; + struct ble_gap_conn_params phy_2M_params = {0}; + uint8_t ext; + int32_t duration_ms; + ble_addr_t peer_addr; + ble_addr_t *peer_addr_param = &peer_addr; + int own_addr_type; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + if (argc > 1 && strcmp(argv[1], "cancel") == 0) { + rc = btshell_conn_cancel(); + if (rc != 0) { + console_printf("connection cancel fail: %d\n", rc); + return rc; + } + + return 0; + } + + ext = parse_arg_kv_dflt("extended", cmd_ext_conn_phy_opts, 0, &rc); + if (rc != 0) { + console_printf("invalid 'extended' parameter\n"); + return rc; + } + + rc = parse_dev_addr("peer_", cmd_peer_addr_types, &peer_addr); + if (rc == ENOENT) { + /* With no "peer_addr" specified we'll use white list */ + peer_addr_param = NULL; + } else if (rc != 0) { + console_printf("invalid 'peer_addr' parameter\n"); + return rc; + } + + own_addr_type = parse_arg_kv_dflt("own_addr_type", cmd_own_addr_types, + BLE_OWN_ADDR_PUBLIC, &rc); + if (rc != 0) { + console_printf("invalid 'own_addr_type' parameter\n"); + return rc; + } + + duration_ms = parse_arg_long_bounds_dflt("duration", 1, INT32_MAX, 0, &rc); + if (rc != 0) { + console_printf("invalid 'duration' parameter\n"); + return rc; + } + + phy_1M_params.scan_itvl = parse_arg_time_dflt("scan_interval", 625, 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'scan_interval' parameter\n"); + return rc; + } + + phy_1M_params.scan_window = parse_arg_time_dflt("scan_window", 625, 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'scan_window' parameter\n"); + return rc; + } + + phy_1M_params.itvl_min = parse_arg_time_dflt("interval_min", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MIN, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + phy_1M_params.itvl_max = parse_arg_time_dflt("interval_max", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MAX, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + phy_1M_params.latency = parse_arg_uint16_dflt("latency", 0, &rc); + if (rc != 0) { + console_printf("invalid 'latency' parameter\n"); + return rc; + } + + phy_1M_params.supervision_timeout = parse_arg_time_dflt("timeout", 10000, + 0x0100, &rc); + if (rc != 0) { + console_printf("invalid 'timeout' parameter\n"); + return rc; + } + + phy_1M_params.min_ce_len = parse_arg_time_dflt("min_conn_event_len", 625, + 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'min_conn_event_len' parameter\n"); + return rc; + } + + phy_1M_params.max_ce_len = parse_arg_time_dflt("max_conn_event_len", 625, + 0x0300, &rc); + if (rc != 0) { + console_printf("invalid 'max_conn_event_len' parameter\n"); + return rc; + } + + if (ext == 0x00) { + rc = btshell_conn_initiate(own_addr_type, peer_addr_param, duration_ms, + &phy_1M_params); + if (rc) { + console_printf("error connecting; rc=%d\n", rc); + } + return rc; + } + + if (ext == 0x01) { + rc = btshell_ext_conn_initiate(own_addr_type, peer_addr_param, + duration_ms, &phy_1M_params, + NULL, NULL); + if (rc) { + console_printf("error connecting; rc=%d\n", rc); + } + return rc; + } + + /* Get coded params */ + phy_coded_params.scan_itvl = parse_arg_time_dflt("coded_scan_interval", + 625, 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'coded_scan_interval' parameter\n"); + return rc; + } + + phy_coded_params.scan_window = parse_arg_time_dflt("coded_scan_window", + 625, 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'coded_scan_window' parameter\n"); + return rc; + } + + phy_coded_params.itvl_min = parse_arg_time_dflt("coded_interval_min", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MIN, + &rc); + if (rc != 0) { + console_printf("invalid 'coded_interval_min' parameter\n"); + return rc; + } + + phy_coded_params.itvl_max = parse_arg_time_dflt("coded_interval_max", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MAX, + &rc); + if (rc != 0) { + console_printf("invalid 'coded_interval_max' parameter\n"); + return rc; + } + + phy_coded_params.latency = + parse_arg_uint16_dflt("coded_latency", 0, &rc); + if (rc != 0) { + console_printf("invalid 'coded_latency' parameter\n"); + return rc; + } + + phy_coded_params.supervision_timeout = + parse_arg_time_dflt("coded_timeout", 10000, 0x0100, &rc); + + if (rc != 0) { + console_printf("invalid 'coded_timeout' parameter\n"); + return rc; + } + + phy_coded_params.min_ce_len = + parse_arg_time_dflt("coded_min_conn_event", 625, 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'coded_min_conn_event' parameter\n"); + return rc; + } + + phy_coded_params.max_ce_len = parse_arg_time_dflt("coded_max_conn_event", + 625, 0x0300, &rc); + if (rc != 0) { + console_printf("invalid 'coded_max_conn_event' parameter\n"); + return rc; + } + + /* Get 2M params */ + phy_2M_params.itvl_min = parse_arg_time_dflt("2M_interval_min", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MIN, + &rc); + if (rc != 0) { + console_printf("invalid '2M_interval_min' parameter\n"); + return rc; + } + + phy_2M_params.itvl_max = parse_arg_time_dflt("2M_interval_max", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MAX, &rc); + if (rc != 0) { + console_printf("invalid '2M_interval_max' parameter\n"); + return rc; + } + + phy_2M_params.latency = + parse_arg_uint16_dflt("2M_latency", 0, &rc); + if (rc != 0) { + console_printf("invalid '2M_latency' parameter\n"); + return rc; + } + + phy_2M_params.supervision_timeout = parse_arg_time_dflt("2M_timeout", 10000, + 0x0100, &rc); + + if (rc != 0) { + console_printf("invalid '2M_timeout' parameter\n"); + return rc; + } + + phy_2M_params.min_ce_len = parse_arg_time_dflt("2M_min_conn_event", 625, + 0x0010, &rc); + if (rc != 0) { + console_printf("invalid '2M_min_conn_event' parameter\n"); + return rc; + } + + phy_2M_params.max_ce_len = parse_arg_time_dflt("2M_max_conn_event", 625, + 0x0300, &rc); + if (rc != 0) { + console_printf("invalid '2M_max_conn_event' parameter\n"); + return rc; + } + + if (ext == 0x02) { + rc = btshell_ext_conn_initiate(own_addr_type, peer_addr_param, + duration_ms, NULL, NULL, &phy_coded_params); + return rc; + } + + if (ext == 0x03) { + rc = btshell_ext_conn_initiate(own_addr_type, peer_addr_param, + duration_ms, &phy_1M_params, NULL, + &phy_coded_params); + return rc; + } + + rc = btshell_ext_conn_initiate(own_addr_type, peer_addr_param, + duration_ms, &phy_1M_params, + &phy_2M_params, + &phy_coded_params); + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param connect_params[] = { + {"cancel", "cancel connection procedure"}, + {"extended", "usage: =[none|1M|coded|both|all], default: none"}, + {"peer_addr_type", "usage: =[public|random|public_id|random_id], default: public"}, + {"peer_addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {"own_addr_type", "usage: =[public|random|rpa_pub|rpa_rnd], default: public"}, + {"duration", "usage: =[1-INT32_MAX], default: 0"}, + {"scan_interval", "usage: =[0-UINT16_MAX], default: 0x0010"}, + {"scan_window", "usage: =[0-UINT16_MAX], default: 0x0010"}, + {"interval_min", "usage: =[0-UINT16_MAX], default: 30"}, + {"interval_max", "usage: =[0-UINT16_MAX], default: 50"}, + {"latency", "usage: =[UINT16], default: 0"}, + {"timeout", "usage: =[UINT16], default: 0x0100"}, + {"min_conn_event_len", "usage: =[UINT16], default: 0x0010"}, + {"max_conn_event_len", "usage: =[UINT16], default: 0x0300"}, + {"coded_scan_interval", "usage: =[0-UINT16_MAX], default: 0x0010"}, + {"coded_scan_window", "usage: =[0-UINT16_MAX], default: 0x0010"}, + {"coded_interval_min", "usage: =[0-UINT16_MAX], default: 30"}, + {"coded_interval_max", "usage: =[0-UINT16_MAX], default: 50"}, + {"coded_latency", "usage: =[UINT16], default: 0"}, + {"coded_timeout", "usage: =[UINT16], default: 0x0100"}, + {"coded_min_conn_event_len", "usage: =[UINT16], default: 0x0010"}, + {"coded_max_conn_event_len", "usage: =[UINT16], default: 0x0300"}, + {"2M_interval_min", "usage: =[0-UINT16_MAX], default: 30"}, + {"2M_interval_max", "usage: =[0-UINT16_MAX], default: 50"}, + {"2M_latency", "usage: =[UINT16], default: 0"}, + {"2M_timeout", "usage: =[UINT16], default: 0x0100"}, + {"2M_min_conn_event_len", "usage: =[UINT16], default: 0x0010"}, + {"2M_max_conn_event_len", "usage: =[UINT16], default: 0x0300"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help connect_help = { + .summary = "start/stop connection procedure with specific parameters", + .usage = NULL, + .params = connect_params, +}; +#endif + +/***************************************************************************** + * $disconnect * + *****************************************************************************/ + +static int +cmd_disconnect(int argc, char **argv) +{ + uint16_t conn_handle; + uint8_t reason; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + reason = parse_arg_uint8_dflt("reason", BLE_ERR_REM_USER_CONN_TERM, &rc); + if (rc != 0) { + console_printf("invalid 'reason' parameter\n"); + return rc; + } + + rc = btshell_term_conn(conn_handle, reason); + if (rc != 0) { + console_printf("error terminating connection; rc=%d\n", rc); + return rc; + } + + return 0; +} + +static int +cmd_show_conn(int argc, char **argv) +{ + struct ble_gap_conn_desc conn_desc; + struct btshell_conn *conn; + int rc; + int i; + + for (i = 0; i < btshell_num_conns; i++) { + conn = btshell_conns + i; + + rc = ble_gap_conn_find(conn->handle, &conn_desc); + if (rc == 0) { + print_conn_desc(&conn_desc); + } + } + + return 0; +} + +static int +cmd_show_addr(int argc, char **argv) +{ + uint8_t id_addr[6]; + int rc; + + console_printf("public_id_addr="); + rc = ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, id_addr, NULL); + if (rc == 0) { + print_addr(id_addr); + } else { + console_printf("none"); + } + + console_printf(" random_id_addr="); + rc = ble_hs_id_copy_addr(BLE_ADDR_RANDOM, id_addr, NULL); + if (rc == 0) { + print_addr(id_addr); + } else { + console_printf("none"); + } + console_printf("\n"); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param disconnect_params[] = { + {"conn", "connection handle parameter, usage: =<UINT16>"}, + {"reason", "disconnection reason, usage: =[UINT8], default: 19 (remote user terminated connection)"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help disconnect_help = { + .summary = "disconnect command", + .usage = NULL, + .params = disconnect_params, +}; +#endif + +/***************************************************************************** + * $set-scan-opts * + *****************************************************************************/ + +static struct btshell_scan_opts g_scan_opts = { + .limit = UINT16_MAX, + .ignore_legacy = 0, +}; + +static int +cmd_set_scan_opts(int argc, char **argv) +{ + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + g_scan_opts.limit = parse_arg_uint16_dflt("decode_limit", UINT16_MAX, &rc); + if (rc != 0) { + console_printf("invalid 'decode_limit' parameter\n"); + return rc; + } + + g_scan_opts.ignore_legacy = parse_arg_bool_dflt("ignore_legacy", 0, &rc); + if (rc != 0) { + console_printf("invalid 'ignore_legacy' parameter\n"); + return rc; + } + + g_scan_opts.periodic_only = parse_arg_bool_dflt("periodic_only", 0, &rc); + if (rc != 0) { + console_printf("invalid 'periodic_only' parameter\n"); + return rc; + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param set_scan_opts_params[] = { + {"decode_limit", "usage: =[0-UINT16_MAX], default: UINT16_MAX"}, + {"ignore_legacy", "usage: =[0-1], default: 0"}, + {"periodic_only", "usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help set_scan_opts_help = { + .summary = "set scan options", + .usage = NULL, + .params = set_scan_opts_params, +}; +#endif + +/***************************************************************************** + * $scan * + *****************************************************************************/ + +static const struct kv_pair cmd_scan_filt_policies[] = { + { "no_wl", BLE_HCI_SCAN_FILT_NO_WL }, + { "use_wl", BLE_HCI_SCAN_FILT_USE_WL }, + { "no_wl_inita", BLE_HCI_SCAN_FILT_NO_WL_INITA }, + { "use_wl_inita", BLE_HCI_SCAN_FILT_USE_WL_INITA }, + { NULL } +}; + +static struct kv_pair cmd_scan_ext_types[] = { + { "none", 0x00 }, + { "1M", 0x01 }, + { "coded", 0x02 }, + { "both", 0x03 }, + { NULL } +}; + +static struct btshell_scan_opts g_scan_opts; + +static int +cmd_scan(int argc, char **argv) +{ + struct ble_gap_disc_params params = {0}; + struct ble_gap_ext_disc_params uncoded = {0}; + struct ble_gap_ext_disc_params coded = {0}; + uint8_t extended; + int32_t duration_ms; + uint8_t own_addr_type; + uint16_t duration; + uint16_t period; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + if (argc > 1 && strcmp(argv[1], "cancel") == 0) { + rc = btshell_scan_cancel(); + if (rc != 0) { + console_printf("scan cancel fail: %d\n", rc); + return rc; + } + return 0; + } + + extended = parse_arg_kv_dflt("extended", cmd_scan_ext_types, 0, &rc); + if (rc != 0) { + console_printf("invalid 'extended' parameter\n"); + return rc; + } + + duration_ms = parse_arg_time_dflt("duration", 10000, BLE_HS_FOREVER, &rc); + if (rc != 0) { + console_printf("invalid 'duration' parameter\n"); + return rc; + } + + params.limited = parse_arg_bool_dflt("limited", 0, &rc); + if (rc != 0) { + console_printf("invalid 'limited' parameter\n"); + return rc; + } + + params.passive = parse_arg_bool_dflt("passive", 0, &rc); + if (rc != 0) { + console_printf("invalid 'passive' parameter\n"); + return rc; + } + + params.itvl = parse_arg_time_dflt("interval", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval' parameter\n"); + return rc; + } + + params.window = parse_arg_time_dflt("window", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'window' parameter\n"); + return rc; + } + + params.filter_policy = parse_arg_kv_dflt("filter", cmd_scan_filt_policies, + BLE_HCI_SCAN_FILT_NO_WL, &rc); + if (rc != 0) { + console_printf("invalid 'filter' parameter\n"); + return rc; + } + + params.filter_duplicates = parse_arg_bool_dflt("nodups", 0, &rc); + if (rc != 0) { + console_printf("invalid 'nodups' parameter\n"); + return rc; + } + + own_addr_type = parse_arg_kv_dflt("own_addr_type", cmd_own_addr_types, + BLE_OWN_ADDR_PUBLIC, &rc); + if (rc != 0) { + console_printf("invalid 'own_addr_type' parameter\n"); + return rc; + } + + if (extended == 0) { + rc = btshell_scan(own_addr_type, duration_ms, ¶ms, &g_scan_opts); + if (rc != 0) { + console_printf("error scanning; rc=%d\n", rc); + return rc; + } + + return 0; + } + + /* Copy above parameters to uncoded params */ + uncoded.passive = params.passive; + uncoded.itvl = params.itvl; + uncoded.window = params.window; + + duration = parse_arg_time_dflt("extended_duration", 10000, 0, &rc); + if (rc != 0) { + console_printf("invalid 'extended_duration' parameter\n"); + return rc; + } + + period = parse_arg_time_dflt("extended_period", 1280000, 0, &rc); + if (rc != 0) { + console_printf("invalid 'extended_period' parameter\n"); + return rc; + } + + coded.itvl = parse_arg_time_dflt("longrange_interval", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'longrange_interval' parameter\n"); + return rc; + } + + coded.window = parse_arg_time_dflt("longrange_window", 625, 0, &rc); + if (rc != 0) { + console_printf("invalid 'longrange_window' parameter\n"); + return rc; + } + + coded.passive = parse_arg_uint16_dflt("longrange_passive", 0, &rc); + if (rc != 0) { + console_printf("invalid 'longrange_passive' parameter\n"); + return rc; + } + + switch (extended) { + case 0x01: + rc = btshell_ext_scan(own_addr_type, duration, period, + params.filter_duplicates, params.filter_policy, + params.limited, &uncoded, NULL, + &g_scan_opts); + break; + case 0x02: + rc = btshell_ext_scan(own_addr_type, duration, period, + params.filter_duplicates, params.filter_policy, + params.limited, NULL, &coded, + &g_scan_opts); + break; + case 0x03: + rc = btshell_ext_scan(own_addr_type, duration, period, + params.filter_duplicates, params.filter_policy, + params.limited, &uncoded, &coded, + &g_scan_opts); + break; + default: + assert(0); + break; + } + + if (rc != 0) { + console_printf("error scanning; rc=%d\n", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param scan_params[] = { + {"cancel", "cancel scan procedure"}, + {"extended", "usage: =[none|1M|coded|both], default: none"}, + {"duration", "usage: =[1-INT32_MAX], default: INT32_MAX"}, + {"limited", "usage: =[0-1], default: 0"}, + {"passive", "usage: =[0-1], default: 0"}, + {"interval", "usage: =[0-UINT16_MAX], default: 0"}, + {"window", "usage: =[0-UINT16_MAX], default: 0"}, + {"filter", "usage: =[no_wl|use_wl|no_wl_inita|use_wl_inita], default: no_wl"}, + {"nodups", "usage: =[0-1], default: 0"}, + {"own_addr_type", "usage: =[public|random|rpa_pub|rpa_rnd], default: public"}, + {"extended_duration", "usage: =[0-UINT16_MAX], default: 0"}, + {"extended_period", "usage: =[0-UINT16_MAX], default: 0"}, + {"longrange_interval", "usage: =[0-UINT16_MAX], default: 0"}, + {"longrange_window", "usage: =[0-UINT16_MAX], default: 0"}, + {"longrange_passive", "usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help scan_help = { + .summary = "start/stop scan procedure with specific parameters", + .usage = NULL, + .params = scan_params, +}; +#endif + +/***************************************************************************** + * $set * + *****************************************************************************/ + +static int +cmd_set_addr(void) +{ + ble_addr_t addr; + int rc; + + rc = parse_dev_addr(NULL, cmd_addr_type, &addr); + if (rc != 0) { + console_printf("invalid 'addr' parameter\n"); + return rc; + } + + switch (addr.type) { +#if MYNEWT_VAL(BLE_CONTROLLER) + case BLE_ADDR_PUBLIC: + /* We shouldn't be writing to the controller's address (g_dev_addr). + * There is no standard way to set the local public address, so this is + * our only option at the moment. + */ + memcpy(g_dev_addr, addr.val, 6); + ble_hs_id_set_pub(g_dev_addr); + break; +#endif + + case BLE_ADDR_RANDOM: + rc = ble_hs_id_set_rnd(addr.val); + if (rc != 0) { + return rc; + } + break; + + default: + return BLE_HS_EUNKNOWN; + } + + return 0; +} + +static int +cmd_set(int argc, char **argv) +{ + uint16_t mtu; + uint8_t irk[16]; + int good = 0; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = parse_arg_find_idx("addr"); + if (rc != -1) { + rc = cmd_set_addr(); + if (rc != 0) { + return rc; + } + good = 1; + } + + mtu = parse_arg_uint16("mtu", &rc); + if (rc == 0) { + rc = ble_att_set_preferred_mtu(mtu); + if (rc == 0) { + good = 1; + } + } else if (rc != ENOENT) { + console_printf("invalid 'mtu' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream_exact_length("irk", irk, 16); + if (rc == 0) { + good = 1; + ble_hs_pvcy_set_our_irk(irk); + } else if (rc != ENOENT) { + console_printf("invalid 'irk' parameter\n"); + return rc; + } + + if (!good) { + console_printf("Error: no valid settings specified\n"); + return -1; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param set_params[] = { + {"addr", "set device address, usage: =[XX:XX:XX:XX:XX:XX]"}, + {"addr_type", "set device address type, usage: =[public|random], default: public"}, + {"mtu", "Maximum Transimssion Unit, usage: =[0-UINT16_MAX]"}, + {"irk", "Identity Resolving Key, usage: =[XX:XX...], len=16 octets"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help set_help = { + .summary = "set device parameters", + .usage = NULL, + .params = set_params, +}; +#endif + +/***************************************************************************** + * $set-adv-data * + *****************************************************************************/ + +#define CMD_ADV_DATA_MAX_UUIDS16 8 +#define CMD_ADV_DATA_MAX_UUIDS32 8 +#define CMD_ADV_DATA_MAX_UUIDS128 2 +#define CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS 8 +#define CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN BLE_HS_ADV_MAX_FIELD_SZ +#define CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN BLE_HS_ADV_MAX_FIELD_SZ +#define CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN BLE_HS_ADV_MAX_FIELD_SZ +#define CMD_ADV_DATA_URI_MAX_LEN BLE_HS_ADV_MAX_FIELD_SZ +#define CMD_ADV_DATA_MFG_DATA_MAX_LEN BLE_HS_ADV_MAX_FIELD_SZ + +#if MYNEWT_VAL(BLE_EXT_ADV) +static void +update_pattern(uint8_t *buf, int counter) +{ + int i; + + for (i = 0; i < 10; i += 2) { + counter += 2; + buf[i] = (counter / 1000) << 4 | (counter / 100 % 10); + buf[i + 1] = (counter / 10 % 10) << 4 | (counter % 10); + } +} +#endif + +static int +cmd_set_adv_data_or_scan_rsp(int argc, char **argv, bool scan_rsp, + bool periodic) +{ + static bssnz_t ble_uuid16_t uuids16[CMD_ADV_DATA_MAX_UUIDS16]; + static bssnz_t ble_uuid32_t uuids32[CMD_ADV_DATA_MAX_UUIDS32]; + static bssnz_t ble_uuid128_t uuids128[CMD_ADV_DATA_MAX_UUIDS128]; + static bssnz_t uint8_t + public_tgt_addrs[CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS] + [BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN]; + static bssnz_t uint8_t slave_itvl_range[BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN]; + static bssnz_t uint8_t + svc_data_uuid16[CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN]; + static bssnz_t uint8_t + svc_data_uuid32[CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN]; + static bssnz_t uint8_t + svc_data_uuid128[CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN]; + static bssnz_t uint8_t uri[CMD_ADV_DATA_URI_MAX_LEN]; + static bssnz_t uint8_t mfg_data[CMD_ADV_DATA_MFG_DATA_MAX_LEN]; + struct ble_hs_adv_fields adv_fields; + uint32_t uuid32; + uint16_t uuid16; + uint8_t uuid128[16]; + uint8_t public_tgt_addr[BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN]; + uint8_t eddystone_url_body_len; + uint8_t eddystone_url_suffix; + uint8_t eddystone_url_scheme; + int8_t eddystone_measured_power = 0; + char eddystone_url_body[BLE_EDDYSTONE_URL_MAX_LEN]; + char *eddystone_url_full; + int svc_data_uuid16_len; + int svc_data_uuid32_len; + int svc_data_uuid128_len; + int uri_len; + int mfg_data_len; + int tmp; + int rc; +#if MYNEWT_VAL(BLE_EXT_ADV) + uint8_t instance; + uint8_t extra_data[10]; + uint16_t counter; + uint16_t extra_data_len; + struct os_mbuf *adv_data; +#endif + + /* cannot set scan rsp for periodic */ + if (scan_rsp && periodic) { + return -1; + } + + memset(&adv_fields, 0, sizeof adv_fields); + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + +#if MYNEWT_VAL(BLE_EXT_ADV) + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } +#endif + + tmp = parse_arg_uint8("flags", &rc); + if (rc == 0) { + adv_fields.flags = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'flags' parameter\n"); + return rc; + } + + while (1) { + uuid16 = parse_arg_uint16("uuid16", &rc); + if (rc == 0) { + if (adv_fields.num_uuids16 >= CMD_ADV_DATA_MAX_UUIDS16) { + console_printf("invalid 'uuid16' parameter\n"); + return EINVAL; + } + uuids16[adv_fields.num_uuids16] = (ble_uuid16_t) BLE_UUID16_INIT(uuid16); + adv_fields.num_uuids16++; + } else if (rc == ENOENT) { + break; + } else { + console_printf("invalid 'uuid16' parameter\n"); + return rc; + } + } + if (adv_fields.num_uuids16 > 0) { + adv_fields.uuids16 = uuids16; + } + + tmp = parse_arg_bool_dflt("uuids16_is_complete", 0, &rc); + if (rc != 0) { + console_printf("invalid 'uuids16_is_complete' parameter\n"); + return rc; + } + + while (1) { + uuid32 = parse_arg_uint32("uuid32", &rc); + if (rc == 0) { + if (adv_fields.num_uuids32 >= CMD_ADV_DATA_MAX_UUIDS32) { + console_printf("invalid 'uuid32' parameter\n"); + return EINVAL; + } + uuids32[adv_fields.num_uuids32] = (ble_uuid32_t) BLE_UUID32_INIT(uuid32); + adv_fields.num_uuids32++; + } else if (rc == ENOENT) { + break; + } else { + console_printf("invalid 'uuid32' parameter\n"); + return rc; + } + } + if (adv_fields.num_uuids32 > 0) { + adv_fields.uuids32 = uuids32; + } + + tmp = parse_arg_bool_dflt("uuids32_is_complete", 0, &rc); + if (rc != 0) { + console_printf("invalid 'uuids32_is_complete' parameter\n"); + return rc; + } + + while (1) { + rc = parse_arg_byte_stream_exact_length("uuid128", uuid128, 16); + if (rc == 0) { + if (adv_fields.num_uuids128 >= CMD_ADV_DATA_MAX_UUIDS128) { + console_printf("invalid 'uuid128' parameter\n"); + return EINVAL; + } + ble_uuid_init_from_buf((ble_uuid_any_t *) &uuids128[adv_fields.num_uuids128], + uuid128, 16); + adv_fields.num_uuids128++; + } else if (rc == ENOENT) { + break; + } else { + console_printf("invalid 'uuid128' parameter\n"); + return rc; + } + } + if (adv_fields.num_uuids128 > 0) { + adv_fields.uuids128 = uuids128; + } + + tmp = parse_arg_bool_dflt("uuids128_is_complete", 0, &rc); + if (rc != 0) { + console_printf("invalid 'uuids128_is_complete' parameter\n"); + return rc; + } + + adv_fields.name = (uint8_t *)parse_arg_extract("name"); + if (adv_fields.name != NULL) { + adv_fields.name_len = strlen((char *)adv_fields.name); + } + + tmp = parse_arg_long_bounds("tx_power_level", INT8_MIN, INT8_MAX, &rc); + if (rc == 0) { + adv_fields.tx_pwr_lvl = tmp; + adv_fields.tx_pwr_lvl_is_present = 1; + } else if (rc != ENOENT) { + console_printf("invalid 'tx_power_level' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream_exact_length("slave_interval_range", + slave_itvl_range, + BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); + if (rc == 0) { + adv_fields.slave_itvl_range = slave_itvl_range; + } else if (rc != ENOENT) { + console_printf("invalid 'slave_interval_range' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream("service_data_uuid16", + CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN, + svc_data_uuid16, &svc_data_uuid16_len); + if (rc == 0) { + adv_fields.svc_data_uuid16 = svc_data_uuid16; + adv_fields.svc_data_uuid16_len = svc_data_uuid16_len; + } else if (rc != ENOENT) { + console_printf("invalid 'service_data_uuid16' parameter\n"); + return rc; + } + + while (1) { + rc = parse_arg_byte_stream_exact_length( + "public_target_address", public_tgt_addr, + BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN); + if (rc == 0) { + if (adv_fields.num_public_tgt_addrs >= + CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS) { + + console_printf("invalid 'public_target_address' parameter\n"); + return EINVAL; + } + memcpy(public_tgt_addrs[adv_fields.num_public_tgt_addrs], + public_tgt_addr, BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN); + adv_fields.num_public_tgt_addrs++; + } else if (rc == ENOENT) { + break; + } else { + console_printf("invalid 'public_target_address' parameter\n"); + return rc; + } + } + if (adv_fields.num_public_tgt_addrs > 0) { + adv_fields.public_tgt_addr = (void *)public_tgt_addrs; + } + + adv_fields.appearance = parse_arg_uint16("appearance", &rc); + if (rc == 0) { + adv_fields.appearance_is_present = 1; + } else if (rc != ENOENT) { + console_printf("invalid 'appearance' parameter\n"); + return rc; + } + + adv_fields.adv_itvl = parse_arg_uint16("advertising_interval", &rc); + if (rc == 0) { + adv_fields.adv_itvl_is_present = 1; + } else if (rc != ENOENT) { + console_printf("invalid 'advertising_interval' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream("service_data_uuid32", + CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN, + svc_data_uuid32, &svc_data_uuid32_len); + if (rc == 0) { + adv_fields.svc_data_uuid32 = svc_data_uuid32; + adv_fields.svc_data_uuid32_len = svc_data_uuid32_len; + } else if (rc != ENOENT) { + console_printf("invalid 'service_data_uuid32' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream("service_data_uuid128", + CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN, + svc_data_uuid128, &svc_data_uuid128_len); + if (rc == 0) { + adv_fields.svc_data_uuid128 = svc_data_uuid128; + adv_fields.svc_data_uuid128_len = svc_data_uuid128_len; + } else if (rc != ENOENT) { + console_printf("invalid 'service_data_uuid128' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream("uri", CMD_ADV_DATA_URI_MAX_LEN, uri, &uri_len); + if (rc == 0) { + adv_fields.uri = uri; + adv_fields.uri_len = uri_len; + } else if (rc != ENOENT) { + console_printf("invalid 'uri' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream("mfg_data", CMD_ADV_DATA_MFG_DATA_MAX_LEN, + mfg_data, &mfg_data_len); + if (rc == 0) { + adv_fields.mfg_data = mfg_data; + adv_fields.mfg_data_len = mfg_data_len; + } else if (rc != ENOENT) { + console_printf("invalid 'mfg_data' parameter\n"); + return rc; + } + + tmp = parse_arg_long_bounds("eddystone_measured_power", -100, 20, &rc); + if (rc == 0) { + eddystone_measured_power = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'eddystone_measured_power' parameter\n"); + return rc; + } + + eddystone_url_full = parse_arg_extract("eddystone_url"); + if (eddystone_url_full != NULL) { + rc = parse_eddystone_url(eddystone_url_full, &eddystone_url_scheme, + eddystone_url_body, + &eddystone_url_body_len, + &eddystone_url_suffix); + if (rc != 0) { + goto done; + } + + rc = ble_eddystone_set_adv_data_url(&adv_fields, eddystone_url_scheme, + eddystone_url_body, + eddystone_url_body_len, + eddystone_url_suffix, + eddystone_measured_power); + } else { +#if MYNEWT_VAL(BLE_EXT_ADV) + /* Default to legacy PDUs size, mbuf chain will be increased if needed + */ + adv_data = os_msys_get_pkthdr(BLE_HCI_MAX_ADV_DATA_LEN, 0); + if (!adv_data) { + rc = ENOMEM; + goto done; + } + + rc = ble_hs_adv_set_fields_mbuf(&adv_fields, adv_data); + if (rc) { + os_mbuf_free_chain(adv_data); + goto done; + } + + /* Append some extra data, if requested */ + extra_data_len = parse_arg_uint16("extra_data_len", &rc); + if (rc == 0) { + counter = 0; + extra_data_len = min(extra_data_len, 1650); + while (counter < extra_data_len) { + update_pattern(extra_data, counter); + + rc = os_mbuf_append(adv_data, extra_data, + min(extra_data_len - counter, 10)); + if (rc) { + os_mbuf_free_chain(adv_data); + goto done; + } + + counter += 10; + } + } + + if (scan_rsp) { + rc = ble_gap_ext_adv_rsp_set_data(instance, adv_data); +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + } else if (periodic) { + rc = ble_gap_periodic_adv_set_data(instance, adv_data); +#endif + } else { + rc = ble_gap_ext_adv_set_data(instance, adv_data); + } +#else + if (scan_rsp) { + rc = ble_gap_adv_rsp_set_fields(&adv_fields); + } else { + rc = ble_gap_adv_set_fields(&adv_fields); + } +#endif + } +done: + if (rc != 0) { + console_printf("error setting advertisement data; rc=%d\n", rc); + return rc; + } + + return 0; +} + +static int +cmd_set_adv_data(int argc, char **argv) +{ + return cmd_set_adv_data_or_scan_rsp(argc, argv, false, false); +} + +static int +cmd_set_scan_rsp(int argc, char **argv) +{ + return cmd_set_adv_data_or_scan_rsp(argc, argv, true, false); +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param set_adv_data_params[] = { + {"instance", "default: 0"}, + {"flags", "usage: =[0-UINT8_MAX]"}, + {"uuid16", "usage: =[UINT16]"}, + {"uuid16_is_complete", "usage: =[0-1], default=0"}, + {"uuid32", "usage: =[UINT32]"}, + {"uuid32_is_complete", "usage: =[0-1], default=0"}, + {"uuid128", "usage: =[XX:XX...], len=16 octets"}, + {"uuid128_is_complete", "usage: =[0-1], default=0"}, + {"tx_power_level", "usage: =[INT8_MIN-INT8_MAX]"}, + {"slave_interval_range", "usage: =[XX:XX:XX:XX]"}, + {"public_target_address", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {"appearance", "usage: =[UINT16]"}, + {"name", "usage: =[string]"}, + {"advertising_interval", "usage: =[UINT16]"}, + {"service_data_uuid16", "usage: =[XX:XX...]"}, + {"service_data_uuid32", "usage: =[XX:XX...]"}, + {"service_data_uuid128", "usage: =[XX:XX...]"}, + {"uri", "usage: =[XX:XX...]"}, + {"mfg_data", "usage: =[XX:XX...]"}, + {"measured_power", "usage: =[-100-20]"}, + {"eddystone_url", "usage: =[string]"}, +#if MYNEWT_VAL(BLE_EXT_ADV) + {"extra_data_len", "usage: =[UINT16]"}, +#endif + {NULL, NULL} +}; + +static const struct shell_cmd_help set_adv_data_help = { + .summary = "set advertising data", + .usage = NULL, + .params = set_adv_data_params, +}; + +static const struct shell_cmd_help set_scan_rsp_help = { + .summary = "set scan response", + .usage = NULL, + .params = set_adv_data_params, +}; +#endif + +/***************************************************************************** + * $set-priv-mode * + *****************************************************************************/ + +static int +cmd_set_priv_mode(int argc, char **argv) +{ + ble_addr_t addr; + uint8_t priv_mode; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = parse_dev_addr(NULL, cmd_addr_type, &addr); + if (rc != 0) { + console_printf("invalid 'addr' parameter\n"); + return rc; + } + + priv_mode = parse_arg_uint8("mode", &rc); + if (rc != 0) { + console_printf("missing mode\n"); + return rc; + } + + return ble_gap_set_priv_mode(&addr, priv_mode); +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param set_priv_mode_params[] = { + {"addr", "set priv mode for device address, usage: =[XX:XX:XX:XX:XX:XX]"}, + {"addr_type", "set priv mode for device address type, usage: =[public|random], default: public"}, + {"mode", "set priv mode, usage: =[0-UINT8_MAX]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help set_priv_mode_help = { + .summary = "set priv mode", + .usage = NULL, + .params = set_priv_mode_params, +}; +#endif + +/***************************************************************************** + * $white-list * + *****************************************************************************/ + +#define CMD_WL_MAX_SZ 8 + +static int +cmd_white_list(int argc, char **argv) +{ + static ble_addr_t addrs[CMD_WL_MAX_SZ]; + int addrs_cnt; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + addrs_cnt = 0; + while (1) { + if (addrs_cnt >= CMD_WL_MAX_SZ) { + return EINVAL; + } + + rc = parse_dev_addr(NULL, cmd_addr_type, &addrs[addrs_cnt]); + if (rc == ENOENT) { + break; + } else if (rc != 0) { + console_printf("invalid 'addr' parameter #%d\n", addrs_cnt + 1); + return rc; + } + + addrs_cnt++; + } + + if (addrs_cnt == 0) { + return EINVAL; + } + + btshell_wl_set(addrs, addrs_cnt); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param white_list_params[] = { + {"addr", "white-list device addresses, usage: =[XX:XX:XX:XX:XX:XX]"}, + {"addr_type", "white-list address types, usage: =[public|random]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help white_list_help = { + .summary = "set white-list addresses", + .usage = NULL, + .params = white_list_params, +}; +#endif + +/***************************************************************************** + * $conn-rssi * + *****************************************************************************/ + +static int +cmd_conn_rssi(int argc, char **argv) +{ + uint16_t conn_handle; + int8_t rssi; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = btshell_rssi(conn_handle, &rssi); + if (rc != 0) { + console_printf("error reading rssi; rc=%d\n", rc); + return rc; + } + + console_printf("conn=%d rssi=%d\n", conn_handle, rssi); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param conn_rssi_params[] = { + {"conn", "connection handle parameter, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help conn_rssi_help = { + .summary = "check connection rssi", + .usage = NULL, + .params = conn_rssi_params, +}; +#endif + +/***************************************************************************** + * $conn-update-params * + *****************************************************************************/ + +static int +cmd_conn_update_params(int argc, char **argv) +{ + struct ble_gap_upd_params params; + uint16_t conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + params.itvl_min = parse_arg_time_dflt("interval_min", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MIN, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + params.itvl_max = parse_arg_time_dflt("interval_max", 1250, + BLE_GAP_INITIAL_CONN_ITVL_MAX, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + params.latency = parse_arg_uint16_dflt("latency", 0, &rc); + if (rc != 0) { + console_printf("invalid 'latency' parameter\n"); + return rc; + } + + params.supervision_timeout = parse_arg_time_dflt("timeout", 10000, 0x0100, + &rc); + if (rc != 0) { + console_printf("invalid 'timeout' parameter\n"); + return rc; + } + + params.min_ce_len = parse_arg_time_dflt("min_conn_event_len", 625, + 0x0010, &rc); + if (rc != 0) { + console_printf("invalid 'min_conn_event_len' parameter\n"); + return rc; + } + + params.max_ce_len = parse_arg_time_dflt("max_conn_event_len", 625, + 0x0300, &rc); + if (rc != 0) { + console_printf("invalid 'max_conn_event_len' parameter\n"); + return rc; + } + + rc = btshell_update_conn(conn_handle, ¶ms); + if (rc != 0) { + console_printf("error updating connection; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param conn_update_params_params[] = { + {"conn", "conn_update_paramsion handle, usage: =<UINT16>"}, + {"interval_min", "usage: =[0-UINT16_MAX], default: 30"}, + {"interval_max", "usage: =[0-UINT16_MAX], default: 50"}, + {"latency", "usage: =[UINT16], default: 0"}, + {"timeout", "usage: =[UINT16], default: 0x0100"}, + {"min_conn_event_len", "usage: =[UINT16], default: 0x0010"}, + {"max_conn_event_len", "usage: =[UINT16], default: 0x0300"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help conn_update_params_help = { + .summary = "update connection parameters", + .usage = "conn_update_params usage", + .params = conn_update_params_params, +}; +#endif + +/***************************************************************************** + * $conn-datalen * + *****************************************************************************/ + +static int +cmd_conn_datalen(int argc, char **argv) +{ + uint16_t conn_handle; + uint16_t tx_octets; + uint16_t tx_time; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + tx_octets = parse_arg_uint16("octets", &rc); + if (rc != 0) { + console_printf("invalid 'octets' parameter\n"); + return rc; + } + + tx_time = parse_arg_uint16("time", &rc); + if (rc != 0) { + console_printf("invalid 'time' parameter\n"); + return rc; + } + + rc = btshell_datalen(conn_handle, tx_octets, tx_time); + if (rc != 0) { + console_printf("error setting data length; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param conn_datalen_params[] = { + {"conn", "Connection handle, usage: =<UINT16>"}, + {"octets", "Max payload size to include in LL Data PDU, " + "range=<27-251>, usage: =<UINT16>"}, + {"time", "Max number of microseconds the controller should use to tx " + "single LL packet, range=<328-17040>, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help conn_datalen_help = { + .summary = "set data length parameters for connection", + .usage = NULL, + .params = conn_datalen_params, +}; +#endif + +/***************************************************************************** + * keystore * + *****************************************************************************/ + +static const struct kv_pair cmd_keystore_entry_type[] = { + { "msec", BLE_STORE_OBJ_TYPE_PEER_SEC }, + { "ssec", BLE_STORE_OBJ_TYPE_OUR_SEC }, + { "cccd", BLE_STORE_OBJ_TYPE_CCCD }, + { NULL } +}; + +static int +cmd_keystore_parse_keydata(int argc, char **argv, union ble_store_key *out, + int *obj_type) +{ + int rc; + + memset(out, 0, sizeof(*out)); + *obj_type = parse_arg_kv("type", cmd_keystore_entry_type, &rc); + if (rc != 0) { + console_printf("invalid 'type' parameter\n"); + return rc; + } + + switch (*obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_OUR_SEC: + rc = parse_dev_addr(NULL, cmd_addr_type, &out->sec.peer_addr); + if (rc != 0) { + console_printf("invalid 'addr' parameter\n"); + return rc; + } + + out->sec.ediv = parse_arg_uint16("ediv", &rc); + if (rc != 0) { + console_printf("invalid 'ediv' parameter\n"); + return rc; + } + + out->sec.rand_num = parse_arg_uint64("rand", &rc); + if (rc != 0) { + console_printf("invalid 'rand' parameter\n"); + return rc; + } + return 0; + + default: + return EINVAL; + } +} + +static int +cmd_keystore_parse_valuedata(int argc, char **argv, + int obj_type, + union ble_store_key *key, + union ble_store_value *out) +{ + int rc; + int valcnt = 0; + memset(out, 0, sizeof(*out)); + + switch (obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_OUR_SEC: + rc = parse_arg_byte_stream_exact_length("ltk", out->sec.ltk, 16); + if (rc == 0) { + out->sec.ltk_present = 1; + swap_in_place(out->sec.ltk, 16); + valcnt++; + } else if (rc != ENOENT) { + console_printf("invalid 'ltk' parameter\n"); + return rc; + } + rc = parse_arg_byte_stream_exact_length("irk", out->sec.irk, 16); + if (rc == 0) { + out->sec.irk_present = 1; + swap_in_place(out->sec.irk, 16); + valcnt++; + } else if (rc != ENOENT) { + console_printf("invalid 'irk' parameter\n"); + return rc; + } + rc = parse_arg_byte_stream_exact_length("csrk", out->sec.csrk, 16); + if (rc == 0) { + out->sec.csrk_present = 1; + swap_in_place(out->sec.csrk, 16); + valcnt++; + } else if (rc != ENOENT) { + console_printf("invalid 'csrk' parameter\n"); + return rc; + } + out->sec.peer_addr = key->sec.peer_addr; + out->sec.ediv = key->sec.ediv; + out->sec.rand_num = key->sec.rand_num; + break; + } + + if (valcnt) { + return 0; + } + return -1; +} + +/***************************************************************************** + * keystore-add * + *****************************************************************************/ + +static int +cmd_keystore_add(int argc, char **argv) +{ + union ble_store_key key; + union ble_store_value value; + int obj_type; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = cmd_keystore_parse_keydata(argc, argv, &key, &obj_type); + + if (rc) { + return rc; + } + + rc = cmd_keystore_parse_valuedata(argc, argv, obj_type, &key, &value); + + if (rc) { + return rc; + } + + switch(obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + rc = ble_store_write_peer_sec(&value.sec); + break; + case BLE_STORE_OBJ_TYPE_OUR_SEC: + rc = ble_store_write_our_sec(&value.sec); + break; + case BLE_STORE_OBJ_TYPE_CCCD: + rc = ble_store_write_cccd(&value.cccd); + break; + default: + rc = ble_store_write(obj_type, &value); + } + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param keystore_add_params[] = { + {"type", "entry type, usage: =<msec|ssec|cccd>"}, + {"addr_type", "usage: =<public|random>"}, + {"addr", "usage: =<XX:XX:XX:XX:XX:XX>"}, + {"ediv", "usage: =<UINT16>"}, + {"rand", "usage: =<UINT64>"}, + {"ltk", "usage: =<XX:XX:...>, len=16 octets"}, + {"irk", "usage: =<XX:XX:...>, len=16 octets"}, + {"csrk", "usage: =<XX:XX:...>, len=16 octets"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help keystore_add_help = { + .summary = "add data to keystore", + .usage = NULL, + .params = keystore_add_params, +}; +#endif + +/***************************************************************************** + * keystore-del * + *****************************************************************************/ + +static int +cmd_keystore_del(int argc, char **argv) +{ + union ble_store_key key; + int obj_type; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = cmd_keystore_parse_keydata(argc, argv, &key, &obj_type); + + if (rc) { + return rc; + } + rc = ble_store_delete(obj_type, &key); + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param keystore_del_params[] = { + {"type", "entry type, usage: =<msec|ssec|cccd>"}, + {"addr_type", "usage: =<public|random>"}, + {"addr", "usage: =<XX:XX:XX:XX:XX:XX>"}, + {"ediv", "usage: =<UINT16>"}, + {"rand", "usage: =<UINT64>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help keystore_del_help = { + .summary = "remove data from keystore", + .usage = NULL, + .params = keystore_del_params, +}; +#endif + +/***************************************************************************** + * keystore-show * + *****************************************************************************/ + +static int +cmd_keystore_iterator(int obj_type, + union ble_store_value *val, + void *cookie) { + + switch (obj_type) { + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_OUR_SEC: + console_printf("Key: "); + if (ble_addr_cmp(&val->sec.peer_addr, BLE_ADDR_ANY) == 0) { + console_printf("ediv=%u ", val->sec.ediv); + console_printf("ediv=%llu ", val->sec.rand_num); + } else { + console_printf("addr_type=%u ", val->sec.peer_addr.type); + print_addr(val->sec.peer_addr.val); + } + console_printf("\n"); + + if (val->sec.ltk_present) { + console_printf(" LTK: "); + print_bytes(val->sec.ltk, 16); + console_printf("\n"); + } + if (val->sec.irk_present) { + console_printf(" IRK: "); + print_bytes(val->sec.irk, 16); + console_printf("\n"); + } + if (val->sec.csrk_present) { + console_printf(" CSRK: "); + print_bytes(val->sec.csrk, 16); + console_printf("\n"); + } + break; + case BLE_STORE_OBJ_TYPE_CCCD: + console_printf("Key: "); + console_printf("addr_type=%u ", val->cccd.peer_addr.type); + print_addr(val->cccd.peer_addr.val); + console_printf("\n"); + + console_printf(" char_val_handle: %d\n", val->cccd.chr_val_handle); + console_printf(" flags: 0x%02x\n", val->cccd.flags); + console_printf(" changed: %d\n", val->cccd.value_changed); + break; + } + return 0; +} + +static int +cmd_keystore_show(int argc, char **argv) +{ + int type; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + type = parse_arg_kv("type", cmd_keystore_entry_type, &rc); + if (rc != 0) { + console_printf("invalid 'type' parameter\n"); + return rc; + } + + ble_store_iterate(type, &cmd_keystore_iterator, NULL); + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param keystore_show_params[] = { + {"type", "entry type, usage: =<msec|ssec|cccd>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help keystore_show_help = { + .summary = "show data in keystore", + .usage = NULL, + .params = keystore_show_params, +}; +#endif + +#if NIMBLE_BLE_SM +/***************************************************************************** + * $show-oob-sc * + *****************************************************************************/ + +extern struct ble_sm_sc_oob_data oob_data_local; +extern struct ble_sm_sc_oob_data oob_data_remote; + +static int +cmd_show_oob_sc(int argc, char **argv) +{ + console_printf("Local OOB Data: r="); + print_bytes(oob_data_local.r, 16); + console_printf(" c="); + print_bytes(oob_data_local.c, 16); + console_printf("\n"); + + console_printf("Remote OOB Data: r="); + print_bytes(oob_data_remote.r, 16); + console_printf(" c="); + print_bytes(oob_data_remote.c, 16); + console_printf("\n"); + + return 0; +} + +/***************************************************************************** + * $auth-passkey * + *****************************************************************************/ + +static int +cmd_auth_passkey(int argc, char **argv) +{ + uint16_t conn_handle; + struct ble_sm_io pk; + char *yesno; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + pk.action = parse_arg_uint16("action", &rc); + if (rc != 0) { + console_printf("invalid 'action' parameter\n"); + return rc; + } + + switch (pk.action) { + case BLE_SM_IOACT_INPUT: + case BLE_SM_IOACT_DISP: + /* passkey is 6 digit number */ + pk.passkey = parse_arg_long_bounds("key", 0, 999999, &rc); + if (rc != 0) { + console_printf("invalid 'key' parameter\n"); + return rc; + } + break; + + case BLE_SM_IOACT_OOB: + rc = parse_arg_byte_stream_exact_length("oob", pk.oob, 16); + if (rc != 0) { + console_printf("invalid 'oob' parameter\n"); + return rc; + } + break; + + case BLE_SM_IOACT_NUMCMP: + yesno = parse_arg_extract("yesno"); + if (yesno == NULL) { + console_printf("invalid 'yesno' parameter\n"); + return EINVAL; + } + + switch (yesno[0]) { + case 'y': + case 'Y': + pk.numcmp_accept = 1; + break; + case 'n': + case 'N': + pk.numcmp_accept = 0; + break; + + default: + console_printf("invalid 'yesno' parameter\n"); + return EINVAL; + } + break; + + case BLE_SM_IOACT_OOB_SC: + rc = parse_arg_byte_stream_exact_length("r", oob_data_remote.r, 16); + if (rc != 0 && rc != ENOENT) { + console_printf("invalid 'r' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream_exact_length("c", oob_data_remote.c, 16); + if (rc != 0 && rc != ENOENT) { + console_printf("invalid 'c' parameter\n"); + return rc; + } + pk.oob_sc_data.local = &oob_data_local; + if (ble_hs_cfg.sm_oob_data_flag) { + pk.oob_sc_data.remote = &oob_data_remote; + } else { + pk.oob_sc_data.remote = NULL; + } + break; + + default: + console_printf("invalid passkey action action=%d\n", pk.action); + return EINVAL; + } + + rc = ble_sm_inject_io(conn_handle, &pk); + if (rc != 0) { + console_printf("error providing passkey; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param auth_passkey_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"action", "auth action type, usage: =<UINT16>"}, + {"key", "usage: =[0-999999]"}, + {"oob", "usage: =[XX:XX...], len=16 octets"}, + {"yesno", "usage: =[string]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help auth_passkey_help = { + .summary = "set authorization passkey options", + .usage = NULL, + .params = auth_passkey_params, +}; +#endif + +/***************************************************************************** + * $security-pair * + *****************************************************************************/ + +static int +cmd_security_pair(int argc, char **argv) +{ + uint16_t conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = btshell_sec_pair(conn_handle); + if (rc != 0) { + console_printf("error initiating pairing; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param security_pair_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help security_pair_help = { + .summary = "start pairing procedure for connection", + .usage = NULL, + .params = security_pair_params, +}; +#endif + +/***************************************************************************** + * $security-unpair * + *****************************************************************************/ + +static int +cmd_security_unpair(int argc, char **argv) +{ + ble_addr_t peer; + int rc; + int oldest; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = parse_arg_bool_dflt("oldest", 0, &oldest); + if (rc != 0) { + console_printf("invalid 'oldest' parameter\n"); + return rc; + } + + if (oldest) { + rc = ble_gap_unpair_oldest_peer(); + console_printf("Unpair oldest status: 0x%02x\n", rc); + return 0; + } + + rc = parse_dev_addr("peer_", cmd_peer_addr_types, &peer); + if (rc != 0) { + console_printf("invalid 'peer_addr' parameter\n"); + return rc; + } + + rc = ble_gap_unpair(&peer); + if (rc != 0) { + console_printf("error unpairing; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param security_unpair_params[] = { + {"oldest", "usage: =[true|false], default: false"}, + {"peer_addr_type", "usage: =[public|random|public_id|random_id], default: public"}, + {"peer_addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help security_unpair_help = { + .summary = "unpair a peer device", + .usage = NULL, + .params = security_unpair_params, +}; +#endif + +/***************************************************************************** + * $security-start * + *****************************************************************************/ + +static int +cmd_security_start(int argc, char **argv) +{ + uint16_t conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = btshell_sec_start(conn_handle); + if (rc != 0) { + console_printf("error starting security; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param security_start_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help security_start_help = { + .summary = "start security procedure for connection", + .usage = NULL, + .params = security_start_params, +}; +#endif + +/***************************************************************************** + * $security-encryption * + *****************************************************************************/ + +static int +cmd_security_encryption(int argc, char **argv) +{ + uint16_t conn_handle; + uint16_t ediv; + uint64_t rand_val; + uint8_t ltk[16]; + uint8_t key_size; + int rc; + int auth; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + ediv = parse_arg_uint16("ediv", &rc); + if (rc == ENOENT) { + rc = btshell_sec_restart(conn_handle, 0, NULL, 0, 0, 0); + } else { + rand_val = parse_arg_uint64("rand", &rc); + if (rc != 0) { + console_printf("invalid 'rand' parameter\n"); + return rc; + } + + auth = parse_arg_bool("auth", &rc); + if (rc != 0) { + console_printf("invalid 'auth' parameter\n"); + return rc; + } + + key_size = parse_arg_uint8("key_size", &rc); + if (rc != 0) { + console_printf("invalid 'key_size' parameter\n"); + return rc; + } + + rc = parse_arg_byte_stream_exact_length("ltk", ltk, 16); + if (rc != 0) { + console_printf("invalid 'ltk' parameter\n"); + return rc; + } + + rc = btshell_sec_restart(conn_handle, key_size, + ltk, ediv, rand_val, auth); + } + + if (rc != 0) { + console_printf("error initiating encryption; rc=%d\n", rc); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param security_encryption_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"ediv", "usage: =[UINT16]"}, + {"rand", "usage: =[UINT64]"}, + {"auth", "usage: =[0-1]"}, + {"ltk", "usage: =[XX:XX...], len=16 octets"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help security_encryption_help = { + .summary = "start encryption procedure for connection", + .usage = NULL, + .params = security_encryption_params, +}; +#endif + +/***************************************************************************** + * $security-set-data * + *****************************************************************************/ + +static int +cmd_security_set_data(int argc, char **argv) +{ + uint8_t tmp; + int good; + int rc; + + good = 0; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + tmp = parse_arg_bool("oob_flag", &rc); + if (rc == 0) { + ble_hs_cfg.sm_oob_data_flag = tmp; + good++; + } else if (rc != ENOENT) { + console_printf("invalid 'oob_flag' parameter\n"); + return rc; + } + + tmp = parse_arg_bool("mitm_flag", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_mitm = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'mitm_flag' parameter\n"); + return rc; + } + + tmp = parse_arg_uint8("io_capabilities", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_io_cap = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'io_capabilities' parameter\n"); + return rc; + } + + tmp = parse_arg_uint8("our_key_dist", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_our_key_dist = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'our_key_dist' parameter\n"); + return rc; + } + + tmp = parse_arg_uint8("their_key_dist", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_their_key_dist = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'their_key_dist' parameter\n"); + return rc; + } + + tmp = parse_arg_bool("bonding", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_bonding = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'bonding' parameter\n"); + return rc; + } + + tmp = parse_arg_bool("sc", &rc); + if (rc == 0) { + good++; + ble_hs_cfg.sm_sc = tmp; + } else if (rc != ENOENT) { + console_printf("invalid 'sc' parameter\n"); + return rc; + } + + if (!good) { + console_printf("Error: no valid settings specified\n"); + return -1; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param security_set_data_params[] = { + {"oob_flag", "usage: =[0-1]"}, + {"mitm_flag", "usage: =[0-1]"}, + {"io_capabilities", "usage: =[UINT8]"}, + {"our_key_dist", "usage: =[UINT8]"}, + {"their_key_dist", "usage: =[UINT8]"}, + {"bonding", "usage: =[0-1]"}, + {"sc", "usage: =[0-1]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help security_set_data_help = { + .summary = "set security data", + .usage = NULL, + .params = security_set_data_params, +}; +#endif +#endif + +/***************************************************************************** + * $test-tx * + * * + * Command to transmit 'num' packets of size 'len' at rate 'r' to + * handle 'h' Note that length must be <= 251. The rate is in msecs. + * + *****************************************************************************/ + +static int +cmd_test_tx(int argc, char **argv) +{ + int rc; + uint16_t conn; + uint16_t len; + uint16_t rate; + uint16_t num; + uint8_t stop; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + stop = parse_arg_uint8_dflt("stop", 0, &rc); + if (rc != 0) { + console_printf("invalid 'stop' parameter\n"); + return rc; + } + + if (stop) { + btshell_tx_stop(); + return 0; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + len = parse_arg_uint16("length", &rc); + if (rc != 0) { + console_printf("invalid 'length' parameter\n"); + return rc; + } + if ((len > 251) || (len < 4)) { + console_printf("error: len must be between 4 and 251, inclusive"); + } + + rate = parse_arg_uint16_dflt("rate", 1, &rc); + if (rc != 0) { + console_printf("invalid 'rate' parameter\n"); + return rc; + } + + num = parse_arg_uint16_dflt("num", 1, &rc); + if (rc != 0) { + console_printf("invalid 'num' parameter\n"); + return rc; + } + + rc = btshell_tx_start(conn, len, rate, num); + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param test_tx_params[] = { + {"conn", "handle to tx to, usage: =<UINT16>"}, + {"length", "size of packet, usage: =<UINT16>"}, + {"rate", "rate of tx, usage: =<UINT16>, default=1"}, + {"num", "number of packets, usage: =<UINT16>, default=1"}, + {"stop", "stop sending, usage: 1 to stop, default 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help test_tx_help = { + .summary = "test packet transmission", + .usage = NULL, + .params = test_tx_params, +}; +#endif + +/***************************************************************************** + * $phy-set * + *****************************************************************************/ + +static int +cmd_phy_set(int argc, char **argv) +{ + uint16_t conn; + uint8_t tx_phys_mask; + uint8_t rx_phys_mask; + uint16_t phy_opts; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + tx_phys_mask = parse_arg_uint8("tx_phys_mask", &rc); + if (rc != 0) { + console_printf("invalid 'tx_phys_mask' parameter\n"); + return rc; + } + + rx_phys_mask = parse_arg_uint8("rx_phys_mask", &rc); + if (rc != 0) { + console_printf("invalid 'rx_phys_mask' parameter\n"); + return rc; + } + + phy_opts = parse_arg_uint16("phy_opts", &rc); + if (rc != 0) { + console_printf("invalid 'phy_opts' parameter\n"); + return rc; + } + + return ble_gap_set_prefered_le_phy(conn, tx_phys_mask, rx_phys_mask, + phy_opts); +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param phy_set_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"tx_phys_mask", "usage: =<UINT8>"}, + {"rx_phys_mask", "usage: =<UINT8>"}, + {"phy_opts", "usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help phy_set_help = { + .summary = "set preferred PHYs", + .usage = NULL, + .params = phy_set_params, +}; +#endif + +/***************************************************************************** + * $phy-set-default * + *****************************************************************************/ + +static int +cmd_phy_set_default(int argc, char **argv) +{ + uint8_t tx_phys_mask; + uint8_t rx_phys_mask; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + tx_phys_mask = parse_arg_uint8("tx_phys_mask", &rc); + if (rc != 0) { + console_printf("invalid 'tx_phys_mask' parameter\n"); + return rc; + } + + rx_phys_mask = parse_arg_uint8("rx_phys_mask", &rc); + if (rc != 0) { + console_printf("invalid 'rx_phys_mask' parameter\n"); + return rc; + } + + return ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask); +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param phy_set_default_params[] = { + {"tx_phys_mask", "usage: =<UINT8>"}, + {"rx_phys_mask", "usage: =<UINT8>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help phy_set_default_help = { + .summary = "set preferred default PHYs", + .usage = NULL, + .params = phy_set_default_params, +}; +#endif + +/***************************************************************************** + * $phy-read * + *****************************************************************************/ + +static int +cmd_phy_read(int argc, char **argv) +{ + uint16_t conn = 0; + uint8_t tx_phy; + uint8_t rx_phy; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = ble_gap_read_le_phy(conn, &tx_phy, &rx_phy); + if (rc != 0) { + console_printf("Could not read PHY error: %d\n", rc); + return rc; + } + + console_printf("TX_PHY: %d\n", tx_phy); + console_printf("RX_PHY: %d\n", tx_phy); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param phy_read_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help phy_read_help = { + .summary = "read PHYs", + .usage = NULL, + .params = phy_read_params, +}; +#endif + +/***************************************************************************** + * $host-enable * + *****************************************************************************/ + +static int +cmd_host_enable(int argc, char **argv) +{ + int rc; + + rc = gatt_svr_init(); + assert(rc == 0); + + ble_hs_sched_start(); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_cmd_help host_enable_help = { + .summary = "start the NimBLE host", + .usage = NULL, + .params = NULL, +}; +#endif + +/***************************************************************************** + * $host-disable * + *****************************************************************************/ + +static void +on_stop(int status, void *arg) +{ + if (status == 0) { + console_printf("host stopped\n"); + } else { + console_printf("host failed to stop; rc=%d\n", status); + } +} + +static int +cmd_host_disable(int argc, char **argv) +{ + static struct ble_hs_stop_listener listener; + int rc; + + rc = ble_hs_stop(&listener, on_stop, NULL); + if (rc) { + return rc; + } + + ble_gatts_reset(); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_cmd_help host_disable_help = { + .summary = "stop the NimBLE host", + .usage = NULL, + .params = NULL, +}; + +/***************************************************************************** + * $gatt-discover * + *****************************************************************************/ + +static const struct shell_param gatt_discover_characteristic_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"uuid", "discover by uuid, usage: =[UUID]"}, + {"start", "start handle, usage: =<UINT16>"}, + {"end", "end handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_discover_characteristic_help = { + .summary = "perform characteristic discovery procedure", + .usage = NULL, + .params = gatt_discover_characteristic_params, +}; + +static const struct shell_param gatt_discover_descriptor_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"start", "start handle, usage: =<UINT16>"}, + {"end", "end handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_discover_descriptor_help = { + .summary = "perform descriptor discovery procedure", + .usage = NULL, + .params = gatt_discover_descriptor_params, +}; + +static const struct shell_param gatt_discover_service_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"uuid", "discover by uuid, usage: =[UUID]"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_discover_service_help = { + .summary = "perform service discovery procedure", + .usage = NULL, + .params = gatt_discover_service_params, +}; + +static const struct shell_param gatt_discover_full_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_discover_full_help = { + .summary = "perform full discovery procedure", + .usage = NULL, + .params = gatt_discover_full_params, +}; + +/***************************************************************************** + * $gatt-exchange-mtu * + *****************************************************************************/ + +static const struct shell_param gatt_exchange_mtu_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_exchange_mtu_help = { + .summary = "perform mtu exchange procedure", + .usage = NULL, + .params = gatt_exchange_mtu_params, +}; + +/***************************************************************************** + * $gatt-find-included-services * + *****************************************************************************/ + +static const struct shell_param gatt_find_included_services_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"start", "start handle, usage: =<UINT16>"}, + {"end", "end handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_find_included_services_help = { + .summary = "perform find included services procedure", + .usage = NULL, + .params = gatt_find_included_services_params, +}; + +/***************************************************************************** + * $gatt-notify * + *****************************************************************************/ + +static const struct shell_param gatt_notify_params[] = { + {"attr", "attribute handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_notify_help = { + .summary = "notify about attribute value changed", + .usage = NULL, + .params = gatt_notify_params, +}; + +/***************************************************************************** + * $gatt-read * + *****************************************************************************/ + +static const struct shell_param gatt_read_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"long", "is read long, usage: =[0-1], default=0"}, + {"attr", "attribute handle, usage: =<UINT16>"}, + {"offset", "offset value, usage: =<UINT16>"}, + {"uuid", "read by uuid, usage: =[UUID]"}, + {"start", "start handle, usage: =<UINT16>"}, + {"end", "end handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_read_help = { + .summary = "perform gatt read procedure", + .usage = NULL, + .params = gatt_read_params, +}; + +/***************************************************************************** + * $gatt-service-changed * + *****************************************************************************/ + +static const struct shell_param gatt_service_changed_params[] = { + {"start", "start handle, usage: =<UINT16>"}, + {"end", "end handle, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_service_changed_help = { + .summary = "send service changed indication", + .usage = NULL, + .params = gatt_service_changed_params, +}; + +/***************************************************************************** + * $gatt-service-visibility * + *****************************************************************************/ + +static const struct shell_param gatt_service_visibility_params[] = { + {"handle", "usage: =<UINT16>"}, + {"visibility", "usage: =<0-1>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_service_visibility_help = { + .summary = "change service visibility", + .usage = NULL, + .params = gatt_service_visibility_params, +}; + +/***************************************************************************** + * $gatt-show * + *****************************************************************************/ + +static const struct shell_param gatt_show_params[] = { + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_show_help = { + .summary = "show discovered gatt database", + .usage = NULL, + .params = gatt_show_params, +}; + +static const struct shell_cmd_help gatt_show_local_help = { + .summary = "show local gatt database", + .usage = NULL, + .params = gatt_show_params, +}; + +static const struct shell_cmd_help gatt_show_addr_help = { + .summary = "show device address", + .usage = NULL, + .params = gatt_show_params, +}; + +static const struct shell_cmd_help gatt_show_conn_help = { + .summary = "show connections information", + .usage = NULL, + .params = gatt_show_params, +}; + +/***************************************************************************** + * $gatt-write * + *****************************************************************************/ + +static const struct shell_param gatt_write_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"no_rsp", "write without response, usage: =[0-1], default=0"}, + {"long", "is write long, usage: =[0-1], default=0"}, + {"attr", "attribute handle, usage: =<UINT16>"}, + {"offset", "attribute handle, usage: =<UINT16>"}, + {"value", "usage: =<octets>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help gatt_write_help = { + .summary = "perform gatt write procedure", + .usage = NULL, + .params = gatt_write_params, +}; +#endif + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) +#if MYNEWT_VAL(SHELL_CMD_HELP) +/***************************************************************************** + * $l2cap-update * + *****************************************************************************/ + +static const struct shell_param l2cap_update_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"interval_min", "usage: =[0-UINT16_MAX], default: 30"}, + {"interval_max", "usage: =[0-UINT16_MAX], default: 50"}, + {"latency", "usage: =[UINT16], default: 0"}, + {"timeout", "usage: =[UINT16], default: 0x0100"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_update_help = { + .summary = "update l2cap parameters for connection", + .usage = NULL, + .params = l2cap_update_params, +}; + +/***************************************************************************** + * $l2cap-create-server * + *****************************************************************************/ + +static const struct shell_param l2cap_create_server_params[] = { + {"psm", "usage: =<UINT16>"}, + {"mtu", "usage: =<UINT16> not more than BTSHELL_COC_MTU, default BTSHELL_COC_MTU"}, + {"error", "usage: used for PTS testing:"}, + {"", "0 - always accept"}, + {"", "1 - reject with insufficient authentication"}, + {"", "2 - reject with insufficient authorization"}, + {"", "3 - reject with insufficient key size"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_create_server_help = { + .summary = "create l2cap server", + .usage = NULL, + .params = l2cap_create_server_params, +}; + +/***************************************************************************** + * $l2cap-connect * + *****************************************************************************/ + +static const struct shell_param l2cap_connect_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"psm", "usage: =<UINT16>"}, + {"num", "usage: number of connection created in a row: [1-5]"}, + {"mtu", "usage: =<UINT16> not more than BTSHELL_COC_MTU, default BTSHELL_COC_MTU"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_connect_help = { + .summary = "perform l2cap connect procedure", + .usage = NULL, + .params = l2cap_connect_params, +}; + +/***************************************************************************** + * $l2cap-disconnect * + *****************************************************************************/ + +static const struct shell_param l2cap_disconnect_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"idx", "usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_disconnect_help = { + .summary = "perform l2cap disconnect procedure", + .usage = "use gatt-show-coc to get the parameters", + .params = l2cap_disconnect_params, +}; + +/***************************************************************************** + * $l2cap-reconfig * + *****************************************************************************/ + +static const struct shell_param l2cap_reconfig_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"mtu", "new mtu, usage: =<UINT16>, default: 0 (no change)"}, + {"idxs", "list of channel indexes, usage: idxs=1,3"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_reconfig_help = { + .summary = "perform l2cap reconfigure procedure", + .usage = "use gatt-show-coc to get the parameters", + .params = l2cap_reconfig_params, +}; + +/***************************************************************************** + * $l2cap-send * + *****************************************************************************/ + +static const struct shell_param l2cap_send_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"idx", "usage: =<UINT16>"}, + {"bytes", "number of bytes to send, usage: =<UINT16>"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_send_help = { + .summary = "perform l2cap send procedure", + .usage = "use l2cap-show-coc to get the parameters", + .params = l2cap_send_params, +}; + +/***************************************************************************** + * $l2cap-show-coc * + *****************************************************************************/ + +static const struct shell_param l2cap_show_coc_params[] = { + {NULL, NULL} +}; + +static const struct shell_cmd_help l2cap_show_coc_help = { + .summary = "show coc information", + .usage = NULL, + .params = l2cap_show_coc_params, +}; + +#endif +#endif + +#if MYNEWT_VAL(BLE_PERIODIC_ADV) +static int +cmd_periodic_configure(int argc, char **argv) +{ + struct ble_gap_periodic_adv_params params = {0}; + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + memset(¶ms, 0, sizeof(params)); + + params.include_tx_power = parse_arg_bool_dflt("include_tx_power", 0, &rc); + if (rc != 0) { + console_printf("invalid 'include_tx_power' parameter\n"); + return rc; + } + + params.itvl_min = parse_arg_time_dflt("interval_min", 1250, 0, &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + params.itvl_max = parse_arg_time_dflt("interval_max", 1250, params.itvl_min, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_configure(instance, ¶ms); + if (rc) { + console_printf("failed to configure periodic advertising\n"); + return rc; + } + + console_printf("Instance %u configured for periodic advertising\n", + instance); + + return 0; +} + +static int +cmd_periodic_set_adv_data(int argc, char **argv) +{ + return cmd_set_adv_data_or_scan_rsp(argc, argv, false, true); +} + +static int +cmd_periodic_start(int argc, char **argv) +{ + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + rc = ble_gap_periodic_adv_start(instance); + if (rc) { + console_printf("failed to start periodic advertising\n"); + return rc; + } + + return 0; +} + +static int +cmd_periodic_stop(int argc, char **argv) +{ + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0 || instance >= BLE_ADV_INSTANCES) { + console_printf("invalid instance\n"); + return rc; + } + + rc = ble_gap_periodic_adv_stop(instance); + if (rc) { + console_printf("failed to stop periodic advertising\n"); + return rc; + } + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param periodic_configure_params[] = { + {"instance", "default: 0"}, + {"interval_min", "usage: =[0-UINT32_MAX], default: 0"}, + {"interval_max", "usage: =[0-UINT32_MAX], default: interval_min"}, + {"tx_power", "include TX power, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help periodic_configure_help = { + .summary = "configure periodic advertising for instance", + .usage = NULL, + .params = periodic_configure_params, +}; + +static const struct shell_param periodic_start_params[] = { + {"instance", "default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help periodic_start_help = { + .summary = "start periodic advertising for instance", + .usage = NULL, + .params = periodic_start_params, +}; + +static const struct shell_param periodic_stop_params[] = { + {"instance", "default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help periodic_stop_help = { + .summary = "stop periodic advertising for instance", + .usage = NULL, + .params = periodic_stop_params, +}; +#endif + +static int +cmd_sync_create(int argc, char **argv) +{ + struct ble_gap_periodic_sync_params params = { 0 }; + ble_addr_t addr; + ble_addr_t *addr_param = &addr; + uint8_t sid; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + if (argc > 1 && strcmp(argv[1], "cancel") == 0) { + rc = ble_gap_periodic_adv_sync_create_cancel(); + if (rc != 0) { + console_printf("Sync create cancel fail: %d\n", rc); + return rc; + } + + return 0; + } + + rc = parse_dev_addr("peer_", cmd_addr_type, &addr); + if (rc == ENOENT) { + /* With no "peer_addr" specified we'll use periodic list */ + addr_param = NULL; + } else if (rc != 0) { + console_printf("invalid 'addr' parameter\n"); + return rc; + } + + sid = parse_arg_uint8_dflt("sid", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sid' parameter\n"); + return rc; + } + + params.skip = parse_arg_uint16_dflt("skip", 0, &rc); + if (rc != 0) { + console_printf("invalid 'skip' parameter\n"); + return rc; + } + + params.sync_timeout = parse_arg_time_dflt("sync_timeout", 10000, 2000, &rc); + if (rc != 0) { + console_printf("invalid 'sync_timeout' parameter\n"); + return rc; + } + + params.reports_disabled = parse_arg_bool_dflt("reports_disabled", false, &rc); + if (rc != 0) { + console_printf("invalid 'reports_disabled' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_create(addr_param, sid, ¶ms, + btshell_gap_event, NULL); + if (rc) { + console_printf("Failed to create sync (%d)\n", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_create_params[] = { + {"cancel", "cancel periodic sync establishment procedure"}, + {"peer_addr_type", "usage: =[public|random], default: public"}, + {"peer_addr", "usage: =[XX:XX:XX:XX:XX:XX]"}, + {"sid", "usage: =[UINT8], default: 0"}, + {"skip", "usage: =[0-0x01F3], default: 0x0000"}, + {"sync_timeout", "usage: =[0x000A-0x4000], default: 0x07D0"}, + {"reports_disabled", "disable reports, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_create_help = { + .summary = "start/stop periodic sync procedure with specific parameters", + .usage = NULL, + .params = sync_create_params, +}; +#endif + +#if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER) +static int +cmd_sync_transfer(int argc, char **argv) +{ + uint16_t service_data; + uint16_t conn_handle; + uint16_t sync_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + sync_handle = parse_arg_uint16_dflt("sync_handle", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sync_handle' parameter\n"); + return rc; + } + + service_data = parse_arg_uint16_dflt("service_data", 0, &rc); + if (rc != 0) { + console_printf("invalid 'service_data' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_transfer(sync_handle, conn_handle, + service_data); + if (rc) { + console_printf("Failed to transfer sync (%d)\n", rc); + } + + return rc; +} + +static int +cmd_sync_reporting(int argc, char **argv) +{ + uint16_t sync_handle; + bool enable; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + sync_handle = parse_arg_uint16_dflt("sync_handle", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sync_handle' parameter\n"); + return rc; + } + + enable = parse_arg_bool_dflt("enabled", 0, &rc); + if (rc != 0) { + console_printf("invalid 'enabled' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_reporting(sync_handle, enable); + if (rc) { + console_printf("Failed to %s reporting (%d)\n", + enable ? "enable" : "disable", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_transfer_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"sync_handle", "sync handle, usage: =[UINT16], default: 0"}, + {"service_data", "service data, usage: =[UINT16], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_transfer_help = { + .summary = "start periodic sync transfer procedure with specific parameters", + .usage = NULL, + .params = sync_transfer_params, +}; + +static const struct shell_param sync_reporting_params[] = { + {"sync_handle", "sync handle, usage: =[UINT16], default: 0"}, + {"enabled", "toggle reporting, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_reporting_help = { + .summary = "configure periodic advertising sync reporting", + .usage = NULL, + .params = sync_reporting_params, +}; +#endif + +static int +cmd_sync_transfer_set_info(int argc, char **argv) +{ + uint16_t service_data; + uint16_t conn_handle; + uint8_t instance; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + instance = parse_arg_uint8_dflt("instance", 0, &rc); + if (rc != 0) { + console_printf("invalid 'instance' parameter\n"); + return rc; + } + + service_data = parse_arg_uint16_dflt("service_data", 0, &rc); + if (rc != 0) { + console_printf("invalid 'service_data' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_set_info(instance, conn_handle, + service_data); + if (rc) { + console_printf("Failed to transfer sync (%d)\n", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_transfer_set_info_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"instance", "advertising instance, usage: =[UINT8], default: 0"}, + {"service_data", "service data, usage: =[UINT16], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_transfer_set_info_help = { + .summary = "start periodic sync transfer procedure with specific parameters", + .usage = NULL, + .params = sync_transfer_set_info_params, +}; +#endif + +static int +cmd_sync_transfer_receive(int argc, char **argv) +{ + struct ble_gap_periodic_sync_params params = { 0 }; + uint16_t conn_handle; + bool disable; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + disable = parse_arg_bool_dflt("disable", false, &rc); + if (rc != 0) { + console_printf("invalid 'disable' parameter\n"); + return rc; + } + + if (disable) { + rc = ble_gap_periodic_adv_sync_receive(conn_handle, NULL, NULL, NULL); + if (rc) { + console_printf("Failed to disable sync transfer reception (%d)\n", rc); + } + + return rc; + } + + params.skip = parse_arg_uint16_dflt("skip", 0, &rc); + if (rc != 0) { + console_printf("invalid 'skip' parameter\n"); + return rc; + } + + params.sync_timeout = parse_arg_time_dflt("sync_timeout", 10000, 10, &rc); + if (rc != 0) { + console_printf("invalid 'sync_timeout' parameter\n"); + return rc; + } + + params.reports_disabled = parse_arg_bool_dflt("reports_disabled", false, &rc); + if (rc != 0) { + console_printf("invalid 'reports_disabled' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_receive(conn_handle, ¶ms, btshell_gap_event, + NULL); + if (rc) { + console_printf("Failed to enable sync transfer reception (%d)\n", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_transfer_receive_params[] = { + {"conn", "connection handle, usage: =<UINT16>"}, + {"disable", "disable transfer reception, usage: =[0-1], default: 0"}, + {"skip", "usage: =[0-0x01F3], default: 0x0000"}, + {"sync_timeout", "usage: =[0x000A-0x4000], default: 0x000A"}, + {"reports_disabled", "disable reports, usage: =[0-1], default: 0"}, + {NULL, NULL} +}; +#endif + +static const struct shell_cmd_help sync_transfer_receive_help = { + .summary = "start/stop periodic sync reception with specific parameters", + .usage = NULL, + .params = sync_transfer_receive_params, +}; +#endif + +static int +cmd_sync_terminate(int argc, char **argv) +{ + uint16_t sync_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + sync_handle = parse_arg_uint16_dflt("sync_handle", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sync_handle' parameter\n"); + return rc; + } + + rc = ble_gap_periodic_adv_sync_terminate(sync_handle); + if (rc) { + console_printf("Failed to terminate sync (%d)\n", rc); + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_terminate_params[] = { + {"sync_handle", "usage: =[UINT16], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_terminate_help = { + .summary = "terminate periodic sync", + .usage = NULL, + .params = sync_terminate_params, +}; +#endif + +static int +cmd_sync_stats(int argc, char **argv) +{ + uint16_t sync_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + sync_handle = parse_arg_uint16_dflt("sync_handle", 0, &rc); + if (rc != 0) { + console_printf("invalid 'sync_handle' parameter\n"); + return rc; + } + + btshell_sync_stats(sync_handle); + + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param sync_stats_params[] = { + {"sync_handle", "usage: =[UINT16], default: 0"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help sync_stats_help = { + .summary = "show sync stats", + .usage = NULL, + .params = sync_stats_params, +}; +#endif +#endif + +static const struct shell_cmd btshell_commands[] = { +#if MYNEWT_VAL(BLE_EXT_ADV) + { + .sc_cmd = "advertise-configure", + .sc_cmd_func = cmd_advertise_configure, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_configure_help, +#endif + }, + { + .sc_cmd = "advertise-set-addr", + .sc_cmd_func = cmd_advertise_set_addr, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_set_addr_help, +#endif + }, + { + .sc_cmd = "advertise-set-adv-data", + .sc_cmd_func = cmd_set_adv_data, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_adv_data_help, +#endif + }, + { + .sc_cmd = "advertise-set-scan-rsp", + .sc_cmd_func = cmd_set_scan_rsp, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_scan_rsp_help, +#endif + }, + { + .sc_cmd = "advertise-start", + .sc_cmd_func = cmd_advertise_start, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_start_help, +#endif + }, + { + .sc_cmd = "advertise-stop", + .sc_cmd_func = cmd_advertise_stop, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_stop_help, +#endif + }, + { + .sc_cmd = "advertise-remove", + .sc_cmd_func = cmd_advertise_remove, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_remove_help, +#endif + }, +#else + { + .sc_cmd = "advertise", + .sc_cmd_func = cmd_advertise, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &advertise_help, +#endif + }, +#endif + { + .sc_cmd = "connect", + .sc_cmd_func = cmd_connect, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &connect_help, +#endif + }, + { + .sc_cmd = "disconnect", + .sc_cmd_func = cmd_disconnect, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &disconnect_help, +#endif + }, + { + .sc_cmd = "show-addr", + .sc_cmd_func = cmd_show_addr, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_show_addr_help, +#endif + }, + { + .sc_cmd = "show-conn", + .sc_cmd_func = cmd_show_conn, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_show_conn_help, +#endif + }, + { + .sc_cmd = "set-scan-opts", + .sc_cmd_func = cmd_set_scan_opts, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_scan_opts_help, +#endif + }, + { + .sc_cmd = "scan", + .sc_cmd_func = cmd_scan, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &scan_help, +#endif + }, + { + .sc_cmd = "set", + .sc_cmd_func = cmd_set, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_help, +#endif + }, +#if !MYNEWT_VAL(BLE_EXT_ADV) + { + .sc_cmd = "set-adv-data", + .sc_cmd_func = cmd_set_adv_data, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_adv_data_help, +#endif + }, + { + .sc_cmd = "set-scan-rsp", + .sc_cmd_func = cmd_set_scan_rsp, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_scan_rsp_help, +#endif + }, +#endif + { + .sc_cmd = "set-priv-mode", + .sc_cmd_func = cmd_set_priv_mode, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_priv_mode_help, +#endif + }, + { + .sc_cmd = "white-list", + .sc_cmd_func = cmd_white_list, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &white_list_help, +#endif + }, + { + .sc_cmd = "conn-rssi", + .sc_cmd_func = cmd_conn_rssi, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &conn_rssi_help, +#endif + }, + { + .sc_cmd = "conn-update-params", + .sc_cmd_func = cmd_conn_update_params, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &conn_update_params_help, +#endif + }, + { + .sc_cmd = "conn-datalen", + .sc_cmd_func = cmd_conn_datalen, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &conn_datalen_help, +#endif + }, + { + .sc_cmd = "gatt-discover-characteristic", + .sc_cmd_func = cmd_gatt_discover_characteristic, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_discover_characteristic_help, +#endif + }, + { + .sc_cmd = "gatt-discover-descriptor", + .sc_cmd_func = cmd_gatt_discover_descriptor, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_discover_descriptor_help, +#endif + }, + { + .sc_cmd = "gatt-discover-service", + .sc_cmd_func = cmd_gatt_discover_service, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_discover_service_help, +#endif + }, + { + .sc_cmd = "gatt-discover-full", + .sc_cmd_func = cmd_gatt_discover_full, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_discover_full_help, +#endif + }, + { + .sc_cmd = "gatt-find-included-services", + .sc_cmd_func = cmd_gatt_find_included_services, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_find_included_services_help, +#endif + }, + { + .sc_cmd = "gatt-exchange-mtu", + .sc_cmd_func = cmd_gatt_exchange_mtu, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_exchange_mtu_help, +#endif + }, + { + .sc_cmd = "gatt-read", + .sc_cmd_func = cmd_gatt_read, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_read_help, +#endif + }, + { + .sc_cmd = "gatt-notify", + .sc_cmd_func = cmd_gatt_notify, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_notify_help, +#endif + }, + { + .sc_cmd = "gatt-service-changed", + .sc_cmd_func = cmd_gatt_service_changed, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_service_changed_help, +#endif + }, + { + .sc_cmd = "gatt-service-visibility", + .sc_cmd_func = cmd_gatt_service_visibility, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_service_visibility_help, +#endif + }, + { + .sc_cmd = "gatt-show", + .sc_cmd_func = cmd_gatt_show, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_show_help, +#endif + }, + { + .sc_cmd = "gatt-show-local", + .sc_cmd_func = cmd_gatt_show_local, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_show_local_help, +#endif + }, + { + .sc_cmd = "gatt-write", + .sc_cmd_func = cmd_gatt_write, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &gatt_write_help, +#endif + }, +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) + { + .sc_cmd = "l2cap-update", + .sc_cmd_func = cmd_l2cap_update, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_update_help, +#endif + }, + { + .sc_cmd = "l2cap-create-server", + .sc_cmd_func = cmd_l2cap_create_server, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_create_server_help, +#endif + }, + { + .sc_cmd = "l2cap-connect", + .sc_cmd_func = cmd_l2cap_connect, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_connect_help, +#endif + }, + { + .sc_cmd = "l2cap-reconfig", + .sc_cmd_func = cmd_l2cap_reconfig, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_reconfig_help, +#endif + }, + { + .sc_cmd = "l2cap-disconnect", + .sc_cmd_func = cmd_l2cap_disconnect, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_disconnect_help, +#endif + }, + { + .sc_cmd = "l2cap-send", + .sc_cmd_func = cmd_l2cap_send, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_send_help, +#endif + }, + { + .sc_cmd = "l2cap-show-coc", + .sc_cmd_func = cmd_l2cap_show_coc, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &l2cap_show_coc_help, +#endif + }, +#endif + { + .sc_cmd = "keystore-add", + .sc_cmd_func = cmd_keystore_add, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &keystore_add_help, +#endif + }, + { + .sc_cmd = "keystore-del", + .sc_cmd_func = cmd_keystore_del, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &keystore_del_help, +#endif + }, + { + .sc_cmd = "keystore-show", + .sc_cmd_func = cmd_keystore_show, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &keystore_show_help, +#endif + }, +#if NIMBLE_BLE_SM + { + .sc_cmd = "show-oob-sc", + .sc_cmd_func = cmd_show_oob_sc, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = NULL, +#endif + }, + { + .sc_cmd = "auth-passkey", + .sc_cmd_func = cmd_auth_passkey, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &auth_passkey_help, +#endif + }, + { + .sc_cmd = "security-pair", + .sc_cmd_func = cmd_security_pair, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &security_pair_help, +#endif + }, + { + .sc_cmd = "security-unpair", + .sc_cmd_func = cmd_security_unpair, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &security_unpair_help, +#endif + }, + { + .sc_cmd = "security-start", + .sc_cmd_func = cmd_security_start, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &security_start_help, +#endif + }, + { + .sc_cmd = "security-encryption", + .sc_cmd_func = cmd_security_encryption, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &security_encryption_help, +#endif + }, + { + .sc_cmd = "security-set-data", + .sc_cmd_func = cmd_security_set_data, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &security_set_data_help, +#endif + }, +#endif + { + .sc_cmd = "test-tx", + .sc_cmd_func = cmd_test_tx, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &test_tx_help, +#endif + }, + { + .sc_cmd = "phy-set", + .sc_cmd_func = cmd_phy_set, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &phy_set_help, +#endif + }, + { + .sc_cmd = "phy-set-default", + .sc_cmd_func = cmd_phy_set_default, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &phy_set_default_help, +#endif + }, + { + .sc_cmd = "phy-read", + .sc_cmd_func = cmd_phy_read, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &phy_read_help, +#endif + }, + { + .sc_cmd = "host-enable", + .sc_cmd_func = cmd_host_enable, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &host_enable_help, +#endif + }, + { + .sc_cmd = "host-disable", + .sc_cmd_func = cmd_host_disable, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &host_disable_help, +#endif + }, +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + { + .sc_cmd = "periodic-configure", + .sc_cmd_func = cmd_periodic_configure, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &periodic_configure_help, +#endif + }, + { + .sc_cmd = "periodic-set-adv-data", + .sc_cmd_func = cmd_periodic_set_adv_data, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &set_adv_data_help, +#endif + }, + { + .sc_cmd = "periodic-start", + .sc_cmd_func = cmd_periodic_start, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &periodic_start_help, +#endif + }, + { + .sc_cmd = "periodic-stop", + .sc_cmd_func = cmd_periodic_stop, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &periodic_stop_help, +#endif + }, + { + .sc_cmd = "sync-create", + .sc_cmd_func = cmd_sync_create, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_create_help, +#endif + }, + { + .sc_cmd = "sync-terminate", + .sc_cmd_func = cmd_sync_terminate, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_terminate_help, +#endif + }, + { + .sc_cmd = "sync-stats", + .sc_cmd_func = cmd_sync_stats, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_stats_help, +#endif + }, +#if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER) + { + .sc_cmd = "sync-transfer", + .sc_cmd_func = cmd_sync_transfer, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_transfer_help, +#endif + }, + { + .sc_cmd = "sync-transfer-set-info", + .sc_cmd_func = cmd_sync_transfer_set_info, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_transfer_set_info_help, +#endif + }, + { + .sc_cmd = "sync-transfer-receive", + .sc_cmd_func = cmd_sync_transfer_receive, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_transfer_receive_help, +#endif + }, + { + .sc_cmd = "sync-reporting", + .sc_cmd_func = cmd_sync_reporting, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &sync_reporting_help, +#endif + }, +#endif +#endif + { 0 }, +}; + + +void +cmd_init(void) +{ + shell_register(BTSHELL_MODULE, btshell_commands); + shell_register_default_module(BTSHELL_MODULE); +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd.h b/src/libs/mynewt-nimble/apps/btshell/src/cmd.h new file mode 100644 index 00000000..63bd50d1 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd.h @@ -0,0 +1,68 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef CMD_H +#define CMD_H + +#include <inttypes.h> +#include "host/ble_uuid.h" + +struct kv_pair { + char *key; + int val; +}; + +uint32_t parse_arg_time_dflt(char *name, int step, uint32_t dflt, int *out_status); +const struct kv_pair *parse_kv_find(const struct kv_pair *kvs, char *name); +int parse_arg_find_idx(const char *key); +char *parse_arg_extract(const char *key); +long parse_arg_long_bounds(char *name, long min, long max, int *out_status); +long parse_arg_long_bounds_dflt(char *name, long min, long max, + long dflt, int *out_status); +uint64_t parse_arg_uint64_bounds(char *name, uint64_t min, + uint64_t max, int *out_status); +long parse_arg_long(char *name, int *staus); +uint8_t parse_arg_bool(char *name, int *status); +uint8_t parse_arg_bool_dflt(char *name, uint8_t dflt, int *out_status); +uint8_t parse_arg_uint8(char *name, int *status); +uint8_t parse_arg_uint8_dflt(char *name, uint8_t dflt, int *out_status); +uint16_t parse_arg_uint16(char *name, int *status); +uint16_t parse_arg_uint16_dflt(char *name, uint16_t dflt, int *out_status); +uint32_t parse_arg_uint32(char *name, int *out_status); +uint32_t parse_arg_uint32_dflt(char *name, uint32_t dflt, int *out_status); +uint64_t parse_arg_uint64(char *name, int *out_status); +int parse_arg_kv(char *name, const struct kv_pair *kvs, int *out_status); +int parse_arg_kv_dflt(char *name, const struct kv_pair *kvs, int def_val, + int *out_status); +int parse_arg_byte_stream(char *name, int max_len, uint8_t *dst, int *out_len); +int parse_arg_uint8_list_with_separator(char *name, char *separator, int max_len, + uint8_t *dst, int *out_len); +int parse_arg_byte_stream_exact_length(char *name, uint8_t *dst, int len); +int parse_arg_mac(char *name, uint8_t *dst); +int parse_arg_addr(char *name, ble_addr_t *addr); +int parse_arg_uuid(char *name, ble_uuid_any_t *uuid); +int parse_arg_all(int argc, char **argv); +int parse_eddystone_url(char *full_url, uint8_t *out_scheme, char *out_body, + uint8_t *out_body_len, uint8_t *out_suffix); +int cmd_parse_conn_start_end(uint16_t *out_conn, uint16_t *out_start, + uint16_t *out_end); + +void cmd_init(void); + +#endif diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.c b/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.c new file mode 100644 index 00000000..ba3799e5 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.c @@ -0,0 +1,587 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <inttypes.h> +#include <errno.h> + +#include "bsp/bsp.h" +#include "host/ble_hs_mbuf.h" +#include "host/ble_gap.h" +#include "services/gatt/ble_svc_gatt.h" +#include "console/console.h" +#include "btshell.h" +#include "cmd.h" +#include "cmd_gatt.h" + +#define CMD_BUF_SZ 256 +static bssnz_t uint8_t cmd_buf[CMD_BUF_SZ]; + +/***************************************************************************** + * $gatt-discover * + *****************************************************************************/ + +int +cmd_gatt_discover_characteristic(int argc, char **argv) +{ + uint16_t start_handle; + uint16_t conn_handle; + uint16_t end_handle; + ble_uuid_any_t uuid; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle); + if (rc != 0) { + console_printf("invalid 'conn start end' parameter\n"); + return rc; + } + + rc = parse_arg_uuid("uuid", &uuid); + if (rc == 0) { + rc = btshell_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, + &uuid.u); + } else if (rc == ENOENT) { + rc = btshell_disc_all_chrs(conn_handle, start_handle, end_handle); + } else { + console_printf("invalid 'uuid' parameter\n"); + return rc; + } + if (rc != 0) { + console_printf("error discovering characteristics; rc=%d\n", rc); + return rc; + } + + return 0; +} + +int +cmd_gatt_discover_descriptor(int argc, char **argv) +{ + uint16_t start_handle; + uint16_t conn_handle; + uint16_t end_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle); + if (rc != 0) { + console_printf("invalid 'conn start end' parameter\n"); + return rc; + } + + rc = btshell_disc_all_dscs(conn_handle, start_handle, end_handle); + if (rc != 0) { + console_printf("error discovering descriptors; rc=%d\n", rc); + return rc; + } + + return 0; +} + +int +cmd_gatt_discover_service(int argc, char **argv) +{ + ble_uuid_any_t uuid; + int conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = parse_arg_uuid("uuid", &uuid); + if (rc == 0) { + rc = btshell_disc_svc_by_uuid(conn_handle, &uuid.u); + } else if (rc == ENOENT) { + rc = btshell_disc_svcs(conn_handle); + } else { + console_printf("invalid 'uuid' parameter\n"); + return rc; + } + + if (rc != 0) { + console_printf("error discovering services; rc=%d\n", rc); + return rc; + } + + return 0; +} + +int +cmd_gatt_discover_full(int argc, char **argv) +{ + int conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = btshell_disc_full(conn_handle); + if (rc != 0) { + console_printf("error discovering all; rc=%d\n", rc); + return rc; + } + + return 0; +} + +/***************************************************************************** + * $gatt-exchange-mtu * + *****************************************************************************/ + +int +cmd_gatt_exchange_mtu(int argc, char **argv) +{ + uint16_t conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + rc = btshell_exchange_mtu(conn_handle); + if (rc != 0) { + console_printf("error exchanging mtu; rc=%d\n", rc); + return rc; + } + + return 0; +} + +/***************************************************************************** + * $gatt-notify * + *****************************************************************************/ + +int +cmd_gatt_notify(int argc, char **argv) +{ + uint16_t attr_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + attr_handle = parse_arg_uint16("attr", &rc); + if (rc != 0) { + console_printf("invalid 'attr' parameter\n"); + return rc; + } + + btshell_notify(attr_handle); + + return 0; +} + +/***************************************************************************** + * $gatt-read * + *****************************************************************************/ + +#define CMD_READ_MAX_ATTRS 8 + +int +cmd_gatt_read(int argc, char **argv) +{ + static uint16_t attr_handles[CMD_READ_MAX_ATTRS]; + uint16_t conn_handle; + uint16_t start; + uint16_t end; + uint16_t offset; + ble_uuid_any_t uuid; + uint8_t num_attr_handles; + int is_uuid; + int is_long; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + is_long = parse_arg_long("long", &rc); + if (rc == ENOENT) { + is_long = 0; + } else if (rc != 0) { + console_printf("invalid 'long' parameter\n"); + return rc; + } + + for (num_attr_handles = 0; + num_attr_handles < CMD_READ_MAX_ATTRS; + num_attr_handles++) { + + attr_handles[num_attr_handles] = parse_arg_uint16("attr", &rc); + if (rc == ENOENT) { + break; + } else if (rc != 0) { + console_printf("invalid 'attr' parameter\n"); + return rc; + } + } + + rc = parse_arg_uuid("uuid", &uuid); + if (rc == ENOENT) { + is_uuid = 0; + } else if (rc == 0) { + is_uuid = 1; + } else { + console_printf("invalid 'uuid' parameter\n"); + return rc; + } + + start = parse_arg_uint16("start", &rc); + if (rc == ENOENT) { + start = 0; + } else if (rc != 0) { + console_printf("invalid 'start' parameter\n"); + return rc; + } + + end = parse_arg_uint16("end", &rc); + if (rc == ENOENT) { + end = 0; + } else if (rc != 0) { + console_printf("invalid 'end' parameter\n"); + return rc; + } + + offset = parse_arg_uint16("offset", &rc); + if (rc == ENOENT) { + offset = 0; + } else if (rc != 0) { + console_printf("invalid 'offset' parameter\n"); + return rc; + } + + if (num_attr_handles == 1) { + if (is_long) { + rc = btshell_read_long(conn_handle, attr_handles[0], offset); + } else { + rc = btshell_read(conn_handle, attr_handles[0]); + } + } else if (num_attr_handles > 1) { + rc = btshell_read_mult(conn_handle, attr_handles, num_attr_handles); + } else if (is_uuid) { + if (start == 0 || end == 0) { + rc = EINVAL; + } else { + rc = btshell_read_by_uuid(conn_handle, start, end, &uuid.u); + } + } else { + rc = EINVAL; + } + + if (rc != 0) { + console_printf("error reading characteristic; rc=%d\n", rc); + return rc; + } + + return 0; +} + + +/***************************************************************************** + * $gatt-service-changed * + *****************************************************************************/ + +int +cmd_gatt_service_changed(int argc, char **argv) +{ + uint16_t start; + uint16_t end; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + start = parse_arg_uint16("start", &rc); + if (rc != 0) { + console_printf("invalid 'start' parameter\n"); + return rc; + } + + end = parse_arg_uint16("end", &rc); + if (rc != 0) { + console_printf("invalid 'end' parameter\n"); + return rc; + } + + ble_svc_gatt_changed(start, end); + + return 0; +} + +/***************************************************************************** + * $gatt-service-visibility * + *****************************************************************************/ + +int +cmd_gatt_service_visibility(int argc, char **argv) +{ + uint16_t handle; + bool vis; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + handle = parse_arg_uint16("handle", &rc); + if (rc != 0) { + console_printf("invalid 'handle' parameter\n"); + return rc; + } + + vis = parse_arg_bool("visibility", &rc); + if (rc != 0) { + console_printf("invalid 'visibility' parameter\n"); + return rc; + } + + ble_gatts_svc_set_visibility(handle, vis); + + return 0; +} + +/***************************************************************************** + * $gatt-find-included-services * + *****************************************************************************/ + +int +cmd_gatt_find_included_services(int argc, char **argv) +{ + uint16_t start_handle; + uint16_t conn_handle; + uint16_t end_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle); + if (rc != 0) { + console_printf("invalid 'conn start end' parameter\n"); + return rc; + } + + rc = btshell_find_inc_svcs(conn_handle, start_handle, end_handle); + if (rc != 0) { + console_printf("error finding included services; rc=%d\n", rc); + return rc; + } + + return 0; +} + +/***************************************************************************** + * $gatt-show * + *****************************************************************************/ + +int +cmd_gatt_show(int argc, char **argv) +{ + struct btshell_conn *conn; + struct btshell_svc *svc; + int i; + + for (i = 0; i < btshell_num_conns; i++) { + conn = btshell_conns + i; + + console_printf("CONNECTION: handle=%d\n", conn->handle); + + SLIST_FOREACH(svc, &conn->svcs, next) { + print_svc(svc); + } + } + + return 0; +} + +int +cmd_gatt_show_local(int argc, char **argv) +{ + gatt_svr_print_svcs(); + return 0; +} + +/***************************************************************************** + * $gatt-write * + *****************************************************************************/ + +int +cmd_gatt_write(int argc, char **argv) +{ + struct ble_gatt_attr attrs[MYNEWT_VAL(BLE_GATT_WRITE_MAX_ATTRS)] = { { 0 } }; + uint16_t attr_handle; + uint16_t conn_handle; + uint16_t offset; + int total_attr_len; + int num_attrs; + int attr_len; + int is_long; + int no_rsp; + int rc; + int i; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + no_rsp = parse_arg_bool_dflt("no_rsp", 0, &rc); + if (rc != 0) { + console_printf("invalid 'no_rsp' parameter\n"); + return rc; + } + + is_long = parse_arg_bool_dflt("long", 0, &rc); + if (rc != 0) { + console_printf("invalid 'long' parameter\n"); + return rc; + } + + total_attr_len = 0; + num_attrs = 0; + while (1) { + attr_handle = parse_arg_uint16("attr", &rc); + if (rc == ENOENT) { + break; + } else if (rc != 0) { + rc = -rc; + console_printf("invalid 'attr' parameter\n"); + goto done; + } + + rc = parse_arg_byte_stream("value", sizeof cmd_buf - total_attr_len, + cmd_buf + total_attr_len, &attr_len); + if (rc == ENOENT) { + break; + } else if (rc != 0) { + console_printf("invalid 'value' parameter\n"); + goto done; + } + + offset = parse_arg_uint16("offset", &rc); + if (rc == ENOENT) { + offset = 0; + } else if (rc != 0) { + console_printf("invalid 'offset' parameter\n"); + return rc; + } + + if (num_attrs >= sizeof attrs / sizeof attrs[0]) { + rc = -EINVAL; + goto done; + } + + attrs[num_attrs].handle = attr_handle; + attrs[num_attrs].offset = offset; + attrs[num_attrs].om = ble_hs_mbuf_from_flat(cmd_buf + total_attr_len, + attr_len); + if (attrs[num_attrs].om == NULL) { + goto done; + } + + total_attr_len += attr_len; + num_attrs++; + } + + if (no_rsp) { + if (num_attrs != 1) { + rc = -EINVAL; + goto done; + } + rc = btshell_write_no_rsp(conn_handle, attrs[0].handle, attrs[0].om); + attrs[0].om = NULL; + } else if (is_long) { + if (num_attrs != 1) { + rc = -EINVAL; + goto done; + } + rc = btshell_write_long(conn_handle, attrs[0].handle, + attrs[0].offset, attrs[0].om); + attrs[0].om = NULL; + } else if (num_attrs > 1) { + rc = btshell_write_reliable(conn_handle, attrs, num_attrs); + } else if (num_attrs == 1) { + rc = btshell_write(conn_handle, attrs[0].handle, attrs[0].om); + attrs[0].om = NULL; + } else { + rc = -EINVAL; + } + +done: + for (i = 0; i < sizeof attrs / sizeof attrs[0]; i++) { + os_mbuf_free_chain(attrs[i].om); + } + + if (rc != 0) { + console_printf("error writing characteristic; rc=%d\n", rc); + } + + return rc; +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.h b/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.h new file mode 100644 index 00000000..70536d03 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd_gatt.h @@ -0,0 +1,39 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef CMD_GATT_H +#define CMD_GATT_H + +#include "cmd.h" + +int cmd_gatt_discover_characteristic(int argc, char **argv); +int cmd_gatt_discover_descriptor(int argc, char **argv); +int cmd_gatt_discover_service(int argc, char **argv); +int cmd_gatt_discover_full(int argc, char **argv); +int cmd_gatt_find_included_services(int argc, char **argv); +int cmd_gatt_exchange_mtu(int argc, char **argv); +int cmd_gatt_notify(int argc, char **argv); +int cmd_gatt_read(int argc, char **argv); +int cmd_gatt_service_changed(int argc, char **argv); +int cmd_gatt_service_visibility(int argc, char **argv); +int cmd_gatt_show(int argc, char **argv); +int cmd_gatt_show_local(int argc, char **argv); +int cmd_gatt_write(int argc, char **argv); + +#endif diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.c b/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.c new file mode 100644 index 00000000..e74e3bf3 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.c @@ -0,0 +1,325 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <inttypes.h> +#include <errno.h> + +#include "syscfg/syscfg.h" +#include "host/ble_gap.h" +#include "host/ble_l2cap.h" +#include "console/console.h" +#include "btshell.h" +#include "cmd.h" +#include "cmd_l2cap.h" + + +/***************************************************************************** + * $l2cap-update * + *****************************************************************************/ + +int +cmd_l2cap_update(int argc, char **argv) +{ + struct ble_l2cap_sig_update_params params; + uint16_t conn_handle; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn_handle = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + params.itvl_min = parse_arg_uint16_dflt("interval_min", + BLE_GAP_INITIAL_CONN_ITVL_MIN, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_min' parameter\n"); + return rc; + } + + params.itvl_max = parse_arg_uint16_dflt("interval_max", + BLE_GAP_INITIAL_CONN_ITVL_MAX, + &rc); + if (rc != 0) { + console_printf("invalid 'interval_max' parameter\n"); + return rc; + } + + params.slave_latency = parse_arg_uint16_dflt("latency", 0, &rc); + if (rc != 0) { + console_printf("invalid 'latency' parameter\n"); + return rc; + } + + params.timeout_multiplier = parse_arg_uint16_dflt("timeout", 0x0100, &rc); + if (rc != 0) { + console_printf("invalid 'timeout' parameter\n"); + return rc; + } + + rc = btshell_l2cap_update(conn_handle, ¶ms); + if (rc != 0) { + console_printf("error txing l2cap update; rc=%d\n", rc); + return rc; + } + + return 0; +} + +/***************************************************************************** + * $l2cap-create-server * + *****************************************************************************/ + +int +cmd_l2cap_create_server(int argc, char **argv) +{ + uint16_t psm = 0; + uint16_t mtu; + int error; + int accept_response = 0; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + psm = parse_arg_uint16("psm", &rc); + if (rc != 0) { + console_printf("invalid 'psm' parameter\n"); + return rc; + } + + error = parse_arg_uint32_dflt("error", 0, &rc); + if (rc != 0) { + console_printf("invalid 'error' parameter\n"); + return rc; + } + + mtu = parse_arg_uint16_dflt("mtu", 0, &rc); + if (rc != 0) { + console_printf("invalid 'mtu' parameter\n"); + return rc; + } + + switch (error) { + case 1: + accept_response = BLE_HS_EAUTHEN; + break; + case 2: + accept_response = BLE_HS_EAUTHOR; + break; + case 3: + accept_response = BLE_HS_EENCRYPT_KEY_SZ; + break; + } + + rc = btshell_l2cap_create_srv(psm, mtu, accept_response); + if (rc) { + console_printf("Server create error: 0x%02x\n", rc); + return rc; + } + + console_printf("Server created successfully\n"); + return 0; +} + +/***************************************************************************** + * $l2cap-connect * + *****************************************************************************/ + +int +cmd_l2cap_connect(int argc, char **argv) +{ + uint16_t conn = 0; + uint16_t psm = 0; + uint16_t mtu; + uint8_t num; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + psm = parse_arg_uint16("psm", &rc); + if (rc != 0) { + console_printf("invalid 'psm' parameter\n"); + return rc; + } + + mtu = parse_arg_uint16_dflt("mtu", 0, &rc); + if (rc != 0) { + console_printf("invalid 'mtu' parameter\n"); + return rc; + } + + num = parse_arg_uint8_dflt("num", 1, &rc); + if (rc != 0) { + console_printf("invalid 'num' parameter\n"); + return rc; + } + + return btshell_l2cap_connect(conn, psm, mtu, num); +} + +/***************************************************************************** + * $l2cap-disconnect * + *****************************************************************************/ + +int +cmd_l2cap_disconnect(int argc, char **argv) +{ + uint16_t conn; + uint16_t idx; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + idx = parse_arg_uint16("idx", &rc); + if (rc != 0) { + console_printf("invalid 'idx' parameter\n"); + return rc; + } + + return btshell_l2cap_disconnect(conn, idx); +} + +/***************************************************************************** + * $l2cap-send * + *****************************************************************************/ + +int +cmd_l2cap_send(int argc, char **argv) +{ + uint16_t conn; + uint16_t idx; + uint16_t bytes; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + idx = parse_arg_uint16("idx", &rc); + if (rc != 0) { + console_printf("invalid 'idx' parameter\n"); + return rc; + } + + bytes = parse_arg_uint16("bytes", &rc); + if (rc != 0) { + console_printf("invalid 'bytes' parameter\n"); + return rc; + } + + return btshell_l2cap_send(conn, idx, bytes); +} + +int +cmd_l2cap_show_coc(int argc, char **argv) +{ + struct btshell_conn *conn = NULL; + struct btshell_l2cap_coc *coc; + int i, j; + + for (i = 0; i < btshell_num_conns; i++) { + conn = btshell_conns + i; + + if (SLIST_EMPTY(&conn->coc_list)) { + continue; + } + + console_printf("conn_handle: 0x%04x\n", conn->handle); + j = 0; + SLIST_FOREACH(coc, &conn->coc_list, next) { + console_printf(" idx: %i, chan pointer = %p\n", j++, coc->chan); + } + } + + return 0; +} + +int +cmd_l2cap_reconfig(int argc, char **argv) +{ +#if MYNEWT_VAL(BLE_L2CAP_ENHANCED_COC) + uint16_t conn; + uint16_t mtu; + uint8_t idxs[5]; + int num; + int rc; + + rc = parse_arg_all(argc - 1, argv + 1); + if (rc != 0) { + return rc; + } + + conn = parse_arg_uint16("conn", &rc); + if (rc != 0) { + console_printf("invalid 'conn' parameter\n"); + return rc; + } + + mtu = parse_arg_uint16_dflt("mtu", 0,&rc); + if (rc != 0) { + console_printf("invalid 'mtu' parameter\n"); + return rc; + } + + rc = parse_arg_uint8_list_with_separator("idxs", ",", 5, idxs, &num); + if (rc != 0) { + console_printf("invalid 'idxs' parameter\n"); + return rc; + } + + return btshell_l2cap_reconfig(conn, mtu, num, idxs); +#else + console_printf("To enable this features set BLE_L2CAP_ENHANCED_COC\n"); + return ENOTSUP; +#endif +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.h b/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.h new file mode 100644 index 00000000..d366fe26 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/cmd_l2cap.h @@ -0,0 +1,33 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef CMD_L2CAP_H +#define CMD_L2CAP_H + +#include "cmd.h" + +int cmd_l2cap_update(int argc, char **argv); +int cmd_l2cap_create_server(int argc, char **argv); +int cmd_l2cap_connect(int argc, char **argv); +int cmd_l2cap_disconnect(int argc, char **argv); +int cmd_l2cap_send(int argc, char **argv); +int cmd_l2cap_show_coc(int argc, char **argv); +int cmd_l2cap_reconfig(int argc, char **argv); + +#endif diff --git a/src/libs/mynewt-nimble/apps/btshell/src/gatt_svr.c b/src/libs/mynewt-nimble/apps/btshell/src/gatt_svr.c new file mode 100644 index 00000000..99d44d05 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/gatt_svr.c @@ -0,0 +1,638 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <assert.h> +#include <string.h> +#include "bsp/bsp.h" +#include "console/console.h" +#include "host/ble_hs.h" +#include "host/ble_uuid.h" +#include "host/ble_gatt.h" +#include "btshell.h" + +/* 0000xxxx-8c26-476f-89a7-a108033a69c7 */ +#define PTS_UUID_DECLARE(uuid16) \ + ((const ble_uuid_t *) (&(ble_uuid128_t) BLE_UUID128_INIT( \ + 0xc7, 0x69, 0x3a, 0x03, 0x08, 0xa1, 0xa7, 0x89, \ + 0x6f, 0x47, 0x26, 0x8c, uuid16, uuid16 >> 8, 0x00, 0x00 \ + ))) + +#define PTS_SVC 0x0001 +#define PTS_CHR_READ 0x0002 +#define PTS_CHR_WRITE 0x0003 +#define PTS_CHR_RELIABLE_WRITE 0x0004 +#define PTS_CHR_WRITE_NO_RSP 0x0005 +#define PTS_CHR_READ_WRITE 0x0006 +#define PTS_CHR_READ_WRITE_ENC 0x0007 +#define PTS_CHR_READ_WRITE_AUTHEN 0x0008 +#define PTS_DSC_READ 0x0009 +#define PTS_DSC_WRITE 0x000a +#define PTS_DSC_READ_WRITE 0x000b +#define PTS_DSC_READ_WRITE_ENC 0x000c +#define PTS_DSC_READ_WRITE_AUTHEN 0x000d + +#define PTS_LONG_SVC 0x0011 +#define PTS_LONG_CHR_READ 0x0012 +#define PTS_LONG_CHR_WRITE 0x0013 +#define PTS_LONG_CHR_RELIABLE_WRITE 0x0014 +#define PTS_LONG_CHR_READ_WRITE 0x0015 +#define PTS_LONG_CHR_READ_WRITE_ALT 0x0016 +#define PTS_LONG_CHR_READ_WRITE_ENC 0x0017 +#define PTS_LONG_CHR_READ_WRITE_AUTHEN 0x0018 +#define PTS_LONG_DSC_READ 0x0019 +#define PTS_LONG_DSC_WRITE 0x001a +#define PTS_LONG_DSC_READ_WRITE 0x001b +#define PTS_LONG_DSC_READ_WRITE_ENC 0x001c +#define PTS_LONG_DSC_READ_WRITE_AUTHEN 0x001d + +#define PTS_INC_SVC 0x001e +#define PTS_CHR_READ_WRITE_ALT 0x001f + +/** + * The vendor specific security test service consists of two characteristics: + * o random-number-generator: generates a random 32-bit number each time + * it is read. This characteristic can only be read over an encrypted + * connection. + * o static-value: a single-byte characteristic that can always be read, + * but can only be written over an encrypted connection. + */ + +/* 59462f12-9543-9999-12c8-58b459a2712d */ +static const ble_uuid128_t gatt_svr_svc_sec_test_uuid = + BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, + 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59); + +/* 5c3a659e-897e-45e1-b016-007107c96df6 */ +static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid = + BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0, + 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c); + +/* 5c3a659e-897e-45e1-b016-007107c96df7 */ +static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid = + BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0, + 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c); + +/* 5c3a659e-897e-45e1-b016-007107c96df8 */ +static const ble_uuid128_t gatt_svr_chr_sec_test_static_auth_uuid = + BLE_UUID128_INIT(0xf8, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0, + 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c); + +static uint8_t gatt_svr_sec_test_static_val; + +static uint8_t gatt_svr_pts_static_val; +static uint8_t gatt_svr_pts_static_long_val[30]; +static uint8_t gatt_svr_pts_static_long_val_alt[30]; + +static int +gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg); + +static int +gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg); + +static int +gatt_svr_long_access_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg); + +static const struct ble_gatt_svc_def gatt_svr_svcs[] = { + { + /*** Service: PTS test. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = PTS_UUID_DECLARE(PTS_SVC), + .characteristics = (struct ble_gatt_chr_def[]) { { + .uuid = PTS_UUID_DECLARE(PTS_CHR_READ), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_WRITE), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_RELIABLE_WRITE), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_RELIABLE_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_WRITE_NO_RSP), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE_ENC), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC | + BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC, + .min_key_size = 16, + }, { + .uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE_AUTHEN), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN | + BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_AUTHEN, + + .descriptors = (struct ble_gatt_dsc_def[]){ { + .uuid = PTS_UUID_DECLARE(PTS_DSC_READ), + .access_cb = gatt_svr_access_test, + .att_flags = BLE_ATT_F_READ, + }, { + .uuid = PTS_UUID_DECLARE(PTS_DSC_WRITE), + .access_cb = gatt_svr_access_test, + .att_flags = BLE_ATT_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_DSC_READ_WRITE), + .access_cb = gatt_svr_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_DSC_READ_WRITE_ENC), + .access_cb = gatt_svr_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC | + BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, + .min_key_size = 16, + }, { + .uuid = PTS_UUID_DECLARE(PTS_DSC_READ_WRITE_AUTHEN), + .access_cb = gatt_svr_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_AUTHEN | + BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_AUTHEN, + }, { + 0, /* No more descriptors in this characteristic. */ + } } + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + /*** Service: PTS long test. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = PTS_UUID_DECLARE(PTS_LONG_SVC), + .characteristics = (struct ble_gatt_chr_def[]) { { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_READ, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_WRITE), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_RELIABLE_WRITE), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_RELIABLE_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_ALT), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_ENC), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC | + BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC, + .min_key_size = 16, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_AUTHEN), + .access_cb = gatt_svr_long_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN | + BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_AUTHEN, + + .descriptors = (struct ble_gatt_dsc_def[]){ { + .uuid = PTS_UUID_DECLARE(PTS_LONG_DSC_READ), + .access_cb = gatt_svr_long_access_test, + .att_flags = BLE_ATT_F_READ, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_DSC_WRITE), + .access_cb = gatt_svr_long_access_test, + .att_flags = BLE_ATT_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_DSC_READ_WRITE), + .access_cb = gatt_svr_long_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_WRITE, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_DSC_READ_WRITE_ENC), + .access_cb = gatt_svr_long_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC | + BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, + .min_key_size = 16, + }, { + .uuid = PTS_UUID_DECLARE(PTS_LONG_DSC_READ_WRITE_AUTHEN), + .access_cb = gatt_svr_long_access_test, + .att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_AUTHEN | + BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_AUTHEN, + }, { + 0, /* No more descriptors in this characteristic. */ + } } + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + /*** Service: Security test. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = &gatt_svr_svc_sec_test_uuid.u, + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Random number generator. */ + .uuid = &gatt_svr_chr_sec_test_rand_uuid.u, + .access_cb = gatt_svr_chr_access_sec_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC, + }, { + /*** Characteristic: Static value. */ + .uuid = &gatt_svr_chr_sec_test_static_uuid.u, + .access_cb = gatt_svr_chr_access_sec_test, + .flags = BLE_GATT_CHR_F_READ | + BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC, + }, { + /*** Characteristic: Static value. */ + .uuid = &gatt_svr_chr_sec_test_static_auth_uuid.u, + .access_cb = gatt_svr_chr_access_sec_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_AUTHEN, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +static const struct ble_gatt_svc_def *inc_svcs[] = { + &gatt_svr_svcs[0], + NULL, +}; + +static const struct ble_gatt_svc_def gatt_svr_inc_svcs[] = { + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = PTS_UUID_DECLARE(PTS_INC_SVC), + .includes = inc_svcs, + .characteristics = (struct ble_gatt_chr_def[]) {{ + .uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE_ALT), + .access_cb = gatt_svr_access_test, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, + }, { + 0, /* No more characteristics */ + }, }, + }, + + { + 0, /* No more services. */ + }, +}; + +static int +gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, + void *dst, uint16_t *len) +{ + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(om); + if (om_len < min_len || om_len > max_len) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +static int +gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + const ble_uuid_t *uuid; + int rand_num; + int rc; + + uuid = ctxt->chr->uuid; + + /* Determine which characteristic is being accessed by examining its + * 128-bit UUID. + */ + + if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_rand_uuid.u) == 0) { + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + + /* Respond with a 32-bit random number. */ + rand_num = rand(); + rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0 || + ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_auth_uuid.u) == 0) { + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val, + sizeof gatt_svr_sec_test_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case BLE_GATT_ACCESS_OP_WRITE_CHR: + rc = gatt_svr_chr_write(ctxt->om, + sizeof gatt_svr_sec_test_static_val, + sizeof gatt_svr_sec_test_static_val, + &gatt_svr_sec_test_static_val, NULL); + return rc; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + } + + /* Unknown characteristic; the nimble stack should not have called this + * function. + */ + assert(0); + return BLE_ATT_ERR_UNLIKELY; +} + +/* This method is used for PTS testing only, to extract 16 bit value + * from 128 bit vendor specific UUID. + */ +static uint16_t +extract_uuid16_from_pts_uuid128(const ble_uuid_t *uuid) +{ + const uint8_t *u8ptr; + uint16_t uuid16; + + u8ptr = BLE_UUID128(uuid)->value; + uuid16 = u8ptr[12]; + uuid16 |= (uint16_t)u8ptr[13] << 8; + return uuid16; +} + +static int +gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + uint16_t uuid16; + int rc; + + uuid16 = extract_uuid16_from_pts_uuid128(ctxt->chr->uuid); + assert(uuid16 != 0); + + switch (uuid16) { + case PTS_CHR_READ: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val, + sizeof gatt_svr_pts_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case PTS_CHR_WRITE: + case PTS_CHR_RELIABLE_WRITE: + case PTS_CHR_WRITE_NO_RSP: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_val, + &gatt_svr_pts_static_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val, + sizeof gatt_svr_pts_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + assert(0); + break; + case PTS_CHR_READ_WRITE: + case PTS_CHR_READ_WRITE_ENC: + case PTS_CHR_READ_WRITE_AUTHEN: + case PTS_CHR_READ_WRITE_ALT: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_val, + &gatt_svr_pts_static_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val, + sizeof gatt_svr_pts_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + assert(0); + break; + case PTS_DSC_READ: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC); + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val, + sizeof gatt_svr_pts_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case PTS_DSC_WRITE: + assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC); + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_val, + &gatt_svr_pts_static_val, NULL); + return rc; + + case PTS_DSC_READ_WRITE: + case PTS_DSC_READ_WRITE_ENC: + case PTS_DSC_READ_WRITE_AUTHEN: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_val, + &gatt_svr_pts_static_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val, + sizeof gatt_svr_pts_static_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + assert(0); + break; + default: + assert(0); + break; + } + + return BLE_ATT_ERR_UNLIKELY; +} + +static int +gatt_svr_long_access_test(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + uint16_t uuid16; + int rc; + + uuid16 = extract_uuid16_from_pts_uuid128(ctxt->chr->uuid); + assert(uuid16 != 0); + + switch (uuid16) { + case PTS_LONG_CHR_READ: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val, + sizeof gatt_svr_pts_static_long_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case PTS_LONG_CHR_WRITE: + case PTS_LONG_CHR_RELIABLE_WRITE: + assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR); + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val, + &gatt_svr_pts_static_long_val, NULL); + return rc; + + case PTS_LONG_CHR_READ_WRITE: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val, + &gatt_svr_pts_static_long_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val, + sizeof gatt_svr_pts_static_long_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + case PTS_LONG_CHR_READ_WRITE_ALT: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val_alt, + &gatt_svr_pts_static_long_val_alt, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val_alt, + sizeof gatt_svr_pts_static_long_val_alt); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + case PTS_LONG_CHR_READ_WRITE_ENC: + case PTS_LONG_CHR_READ_WRITE_AUTHEN: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val, + &gatt_svr_pts_static_long_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val, + sizeof gatt_svr_pts_static_long_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + case PTS_LONG_DSC_READ: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC); + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val, + sizeof gatt_svr_pts_static_long_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case PTS_LONG_DSC_WRITE: + assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC); + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val, + &gatt_svr_pts_static_long_val, NULL); + return rc; + + case PTS_LONG_DSC_READ_WRITE: + case PTS_LONG_DSC_READ_WRITE_ENC: + case PTS_LONG_DSC_READ_WRITE_AUTHEN: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) { + rc = gatt_svr_chr_write(ctxt->om,0, + sizeof gatt_svr_pts_static_long_val, + &gatt_svr_pts_static_long_val, NULL); + return rc; + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) { + rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_long_val, + sizeof gatt_svr_pts_static_long_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + +void +gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) +{ + char buf[BLE_UUID_STR_LEN]; + + switch (ctxt->op) { + case BLE_GATT_REGISTER_OP_SVC: + MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n", + ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf), + ctxt->svc.handle); + break; + + case BLE_GATT_REGISTER_OP_CHR: + MODLOG_DFLT(DEBUG, "registering characteristic %s with " + "def_handle=%d val_handle=%d\n", + ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf), + ctxt->chr.def_handle, + ctxt->chr.val_handle); + break; + + case BLE_GATT_REGISTER_OP_DSC: + MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n", + ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf), + ctxt->dsc.handle); + break; + + default: + assert(0); + break; + } +} + +void +gatt_svr_print_svcs(void) +{ + ble_gatts_show_local(); +} + +int +gatt_svr_init(void) +{ + int rc; + + rc = ble_gatts_count_cfg(gatt_svr_svcs); + if (rc != 0) { + return rc; + } + + rc = ble_gatts_add_svcs(gatt_svr_svcs); + if (rc != 0) { + return rc; + } + + rc = ble_gatts_count_cfg(gatt_svr_inc_svcs); + if (rc != 0) { + return rc; + } + + rc = ble_gatts_add_svcs(gatt_svr_inc_svcs); + if (rc != 0) { + return rc; + } + + return 0; +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/main.c b/src/libs/mynewt-nimble/apps/btshell/src/main.c new file mode 100644 index 00000000..b7031836 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/main.c @@ -0,0 +1,2630 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include "os/mynewt.h" +#include "bsp/bsp.h" +#include "log/log.h" +#include "stats/stats.h" +#include "bsp/bsp.h" +#include "hal/hal_gpio.h" +#include "console/console.h" +#include "btshell.h" +#include "cmd.h" + +/* BLE */ +#include "nimble/ble.h" +#include "nimble/nimble_opt.h" +#include "nimble/ble_hci_trans.h" +#include "host/ble_hs.h" +#include "host/ble_hs_adv.h" +#include "host/ble_uuid.h" +#include "host/ble_att.h" +#include "host/ble_gap.h" +#include "host/ble_gatt.h" +#include "host/ble_store.h" +#include "host/ble_sm.h" + +/* Mandatory services. */ +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" + +/* XXX: An app should not include private headers from a library. The btshell + * app uses some of nimble's internal details for logging. + */ +#include "../src/ble_hs_conn_priv.h" +#include "../src/ble_hs_atomic_priv.h" +#include "../src/ble_hs_priv.h" + +#if MYNEWT_VAL(BLE_ROLE_CENTRAL) +#define BTSHELL_MAX_SVCS 32 +#define BTSHELL_MAX_CHRS 64 +#define BTSHELL_MAX_DSCS 64 +#else +#define BTSHELL_MAX_SVCS 1 +#define BTSHELL_MAX_CHRS 1 +#define BTSHELL_MAX_DSCS 1 +#endif + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) +#define BTSHELL_COC_MTU (256) +/* We use same pool for incoming and outgoing sdu */ +#define BTSHELL_COC_BUF_COUNT (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)) + +#define INT_TO_PTR(x) (void *)((intptr_t)(x)) +#define PTR_TO_INT(x) (int) ((intptr_t)(x)) +#endif + +bssnz_t struct btshell_conn btshell_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)]; +int btshell_num_conns; + +static os_membuf_t btshell_svc_mem[ + OS_MEMPOOL_SIZE(BTSHELL_MAX_SVCS, sizeof(struct btshell_svc)) +]; +static struct os_mempool btshell_svc_pool; + +static os_membuf_t btshell_chr_mem[ + OS_MEMPOOL_SIZE(BTSHELL_MAX_CHRS, sizeof(struct btshell_chr)) +]; +static struct os_mempool btshell_chr_pool; + +static os_membuf_t btshell_dsc_mem[ + OS_MEMPOOL_SIZE(BTSHELL_MAX_DSCS, sizeof(struct btshell_dsc)) +]; +static struct os_mempool btshell_dsc_pool; + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) +static os_membuf_t btshell_coc_conn_mem[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM), + sizeof(struct btshell_l2cap_coc)) +]; +static struct os_mempool btshell_coc_conn_pool; + +static os_membuf_t btshell_sdu_coc_mem[ + OS_MEMPOOL_SIZE(BTSHELL_COC_BUF_COUNT, BTSHELL_COC_MTU) +]; +struct os_mbuf_pool sdu_os_mbuf_pool; +static struct os_mempool sdu_coc_mbuf_mempool; +#endif + +static struct os_callout btshell_tx_timer; +struct btshell_tx_data_s +{ + uint16_t tx_num; + uint16_t tx_num_requested; + uint16_t tx_rate; + uint16_t tx_conn_handle; + uint16_t tx_len; + struct ble_hs_conn *conn; +}; +static struct btshell_tx_data_s btshell_tx_data; +int btshell_full_disc_prev_chr_val; + +struct ble_sm_sc_oob_data oob_data_local; +struct ble_sm_sc_oob_data oob_data_remote; + +#define XSTR(s) STR(s) +#ifndef STR +#define STR(s) #s +#endif + + +#ifdef DEVICE_NAME +#define BTSHELL_AUTO_DEVICE_NAME XSTR(DEVICE_NAME) +#else +#define BTSHELL_AUTO_DEVICE_NAME "" +#endif + +#if MYNEWT_VAL(BLE_EXT_ADV) +struct { + bool restart; + uint16_t conn_handle; +} ext_adv_restart[BLE_ADV_INSTANCES]; +#endif + +static struct { + bool restart; + uint8_t own_addr_type; + ble_addr_t direct_addr; + int32_t duration_ms; + struct ble_gap_adv_params params; +} adv_params; + +static void +btshell_print_error(char *msg, uint16_t conn_handle, + const struct ble_gatt_error *error) +{ + if (msg == NULL) { + msg = "ERROR"; + } + + console_printf("%s: conn_handle=%d status=%d att_handle=%d\n", + msg, conn_handle, error->status, error->att_handle); +} + +static void +btshell_print_adv_fields(const struct ble_hs_adv_fields *fields) +{ + const uint8_t *u8p; + int i; + + if (fields->flags != 0) { + console_printf(" flags=0x%02x:\n", fields->flags); + + if (!(fields->flags & BLE_HS_ADV_F_DISC_LTD) && + !(fields->flags & BLE_HS_ADV_F_DISC_GEN)) { + console_printf(" Non-discoverable mode\n"); + } + + if (fields->flags & BLE_HS_ADV_F_DISC_LTD) { + console_printf(" Limited discoverable mode\n"); + } + + if (fields->flags & BLE_HS_ADV_F_DISC_GEN) { + console_printf(" General discoverable mode\n"); + } + + if (fields->flags & BLE_HS_ADV_F_BREDR_UNSUP) { + console_printf(" BR/EDR not supported\n"); + } + } + + if (fields->uuids16 != NULL) { + console_printf(" uuids16(%scomplete)=", + fields->uuids16_is_complete ? "" : "in"); + for (i = 0; i < fields->num_uuids16; i++) { + print_uuid(&fields->uuids16[i].u); + console_printf(" "); + } + console_printf("\n"); + } + + if (fields->uuids32 != NULL) { + console_printf(" uuids32(%scomplete)=", + fields->uuids32_is_complete ? "" : "in"); + for (i = 0; i < fields->num_uuids32; i++) { + print_uuid(&fields->uuids32[i].u); + console_printf(" "); + } + console_printf("\n"); + } + + if (fields->uuids128 != NULL) { + console_printf(" uuids128(%scomplete)=", + fields->uuids128_is_complete ? "" : "in"); + for (i = 0; i < fields->num_uuids128; i++) { + print_uuid(&fields->uuids128[i].u); + console_printf(" "); + } + console_printf("\n"); + } + + if (fields->name != NULL) { + console_printf(" name(%scomplete)=", + fields->name_is_complete ? "" : "in"); + console_write((char *)fields->name, fields->name_len); + console_printf("\n"); + } + + if (fields->tx_pwr_lvl_is_present) { + console_printf(" tx_pwr_lvl=%d\n", fields->tx_pwr_lvl); + } + + if (fields->slave_itvl_range != NULL) { + console_printf(" slave_itvl_range="); + print_bytes(fields->slave_itvl_range, + BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); + console_printf("\n"); + } + + if (fields->svc_data_uuid16 != NULL) { + console_printf(" svc_data_uuid16="); + print_bytes(fields->svc_data_uuid16, + fields->svc_data_uuid16_len); + console_printf("\n"); + } + + if (fields->public_tgt_addr != NULL) { + console_printf(" public_tgt_addr="); + u8p = fields->public_tgt_addr; + for (i = 0; i < fields->num_public_tgt_addrs; i++) { + print_addr(u8p); + u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN; + } + console_printf("\n"); + } + + if (fields->appearance_is_present) { + console_printf(" appearance=0x%04x\n", fields->appearance); + } + + if (fields->adv_itvl_is_present) { + console_printf(" adv_itvl=0x%04x\n", fields->adv_itvl); + } + + if (fields->svc_data_uuid32 != NULL) { + console_printf(" svc_data_uuid32="); + print_bytes(fields->svc_data_uuid32, + fields->svc_data_uuid32_len); + console_printf("\n"); + } + + if (fields->svc_data_uuid128 != NULL) { + console_printf(" svc_data_uuid128="); + print_bytes(fields->svc_data_uuid128, + fields->svc_data_uuid128_len); + console_printf("\n"); + } + + if (fields->uri != NULL) { + console_printf(" uri="); + print_bytes(fields->uri, fields->uri_len); + console_printf("\n"); + } + + if (fields->mfg_data != NULL) { + console_printf(" mfg_data="); + print_bytes(fields->mfg_data, fields->mfg_data_len); + console_printf("\n"); + } +} + +static int +btshell_conn_find_idx(uint16_t handle) +{ + int i; + + for (i = 0; i < btshell_num_conns; i++) { + if (btshell_conns[i].handle == handle) { + return i; + } + } + + return -1; +} + +static struct btshell_conn * +btshell_conn_find(uint16_t handle) +{ + int idx; + + idx = btshell_conn_find_idx(handle); + if (idx == -1) { + return NULL; + } else { + return btshell_conns + idx; + } +} + +static struct btshell_svc * +btshell_svc_find_prev(struct btshell_conn *conn, uint16_t svc_start_handle) +{ + struct btshell_svc *prev; + struct btshell_svc *svc; + + prev = NULL; + SLIST_FOREACH(svc, &conn->svcs, next) { + if (svc->svc.start_handle >= svc_start_handle) { + break; + } + + prev = svc; + } + + return prev; +} + +static struct btshell_svc * +btshell_svc_find(struct btshell_conn *conn, uint16_t svc_start_handle, + struct btshell_svc **out_prev) +{ + struct btshell_svc *prev; + struct btshell_svc *svc; + + prev = btshell_svc_find_prev(conn, svc_start_handle); + if (prev == NULL) { + svc = SLIST_FIRST(&conn->svcs); + } else { + svc = SLIST_NEXT(prev, next); + } + + if (svc != NULL && svc->svc.start_handle != svc_start_handle) { + svc = NULL; + } + + if (out_prev != NULL) { + *out_prev = prev; + } + return svc; +} + +static struct btshell_svc * +btshell_svc_find_range(struct btshell_conn *conn, uint16_t attr_handle) +{ + struct btshell_svc *svc; + + SLIST_FOREACH(svc, &conn->svcs, next) { + if (svc->svc.start_handle <= attr_handle && + svc->svc.end_handle >= attr_handle) { + + return svc; + } + } + + return NULL; +} + +static void +btshell_chr_delete(struct btshell_chr *chr) +{ + struct btshell_dsc *dsc; + + while ((dsc = SLIST_FIRST(&chr->dscs)) != NULL) { + SLIST_REMOVE_HEAD(&chr->dscs, next); + os_memblock_put(&btshell_dsc_pool, dsc); + } + + os_memblock_put(&btshell_chr_pool, chr); +} + +static void +btshell_svc_delete(struct btshell_svc *svc) +{ + struct btshell_chr *chr; + + while ((chr = SLIST_FIRST(&svc->chrs)) != NULL) { + SLIST_REMOVE_HEAD(&svc->chrs, next); + btshell_chr_delete(chr); + } + + os_memblock_put(&btshell_svc_pool, svc); +} + +static struct btshell_svc * +btshell_svc_add(uint16_t conn_handle, const struct ble_gatt_svc *gatt_svc) +{ + struct btshell_conn *conn; + struct btshell_svc *prev; + struct btshell_svc *svc; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + MODLOG_DFLT(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; " + "HANDLE=%d\n", + conn_handle); + return NULL; + } + + svc = btshell_svc_find(conn, gatt_svc->start_handle, &prev); + if (svc != NULL) { + /* Service already discovered. */ + return svc; + } + + svc = os_memblock_get(&btshell_svc_pool); + if (svc == NULL) { + MODLOG_DFLT(DEBUG, "OOM WHILE DISCOVERING SERVICE\n"); + return NULL; + } + memset(svc, 0, sizeof *svc); + + svc->svc = *gatt_svc; + SLIST_INIT(&svc->chrs); + + if (prev == NULL) { + SLIST_INSERT_HEAD(&conn->svcs, svc, next); + } else { + SLIST_INSERT_AFTER(prev, svc, next); + } + + return svc; +} + +static struct btshell_chr * +btshell_chr_find_prev(const struct btshell_svc *svc, uint16_t chr_val_handle) +{ + struct btshell_chr *prev; + struct btshell_chr *chr; + + prev = NULL; + SLIST_FOREACH(chr, &svc->chrs, next) { + if (chr->chr.val_handle >= chr_val_handle) { + break; + } + + prev = chr; + } + + return prev; +} + +static struct btshell_chr * +btshell_chr_find(const struct btshell_svc *svc, uint16_t chr_val_handle, + struct btshell_chr **out_prev) +{ + struct btshell_chr *prev; + struct btshell_chr *chr; + + prev = btshell_chr_find_prev(svc, chr_val_handle); + if (prev == NULL) { + chr = SLIST_FIRST(&svc->chrs); + } else { + chr = SLIST_NEXT(prev, next); + } + + if (chr != NULL && chr->chr.val_handle != chr_val_handle) { + chr = NULL; + } + + if (out_prev != NULL) { + *out_prev = prev; + } + return chr; +} + +static struct btshell_chr * +btshell_chr_add(uint16_t conn_handle, uint16_t svc_start_handle, + const struct ble_gatt_chr *gatt_chr) +{ + struct btshell_conn *conn; + struct btshell_chr *prev; + struct btshell_chr *chr; + struct btshell_svc *svc; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + MODLOG_DFLT(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; " + "HANDLE=%d\n", + conn_handle); + return NULL; + } + + svc = btshell_svc_find(conn, svc_start_handle, NULL); + if (svc == NULL) { + MODLOG_DFLT(DEBUG, "CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n", + conn_handle); + return NULL; + } + + chr = btshell_chr_find(svc, gatt_chr->val_handle, &prev); + if (chr != NULL) { + /* Characteristic already discovered. */ + return chr; + } + + chr = os_memblock_get(&btshell_chr_pool); + if (chr == NULL) { + MODLOG_DFLT(DEBUG, "OOM WHILE DISCOVERING CHARACTERISTIC\n"); + return NULL; + } + memset(chr, 0, sizeof *chr); + + chr->chr = *gatt_chr; + + if (prev == NULL) { + SLIST_INSERT_HEAD(&svc->chrs, chr, next); + } else { + SLIST_NEXT(prev, next) = chr; + } + + return chr; +} + +static struct btshell_dsc * +btshell_dsc_find_prev(const struct btshell_chr *chr, uint16_t dsc_handle) +{ + struct btshell_dsc *prev; + struct btshell_dsc *dsc; + + prev = NULL; + SLIST_FOREACH(dsc, &chr->dscs, next) { + if (dsc->dsc.handle >= dsc_handle) { + break; + } + + prev = dsc; + } + + return prev; +} + +static struct btshell_dsc * +btshell_dsc_find(const struct btshell_chr *chr, uint16_t dsc_handle, + struct btshell_dsc **out_prev) +{ + struct btshell_dsc *prev; + struct btshell_dsc *dsc; + + prev = btshell_dsc_find_prev(chr, dsc_handle); + if (prev == NULL) { + dsc = SLIST_FIRST(&chr->dscs); + } else { + dsc = SLIST_NEXT(prev, next); + } + + if (dsc != NULL && dsc->dsc.handle != dsc_handle) { + dsc = NULL; + } + + if (out_prev != NULL) { + *out_prev = prev; + } + return dsc; +} + +static struct btshell_dsc * +btshell_dsc_add(uint16_t conn_handle, uint16_t chr_val_handle, + const struct ble_gatt_dsc *gatt_dsc) +{ + struct btshell_conn *conn; + struct btshell_dsc *prev; + struct btshell_dsc *dsc; + struct btshell_svc *svc; + struct btshell_chr *chr; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + MODLOG_DFLT(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; " + "HANDLE=%d\n", + conn_handle); + return NULL; + } + + svc = btshell_svc_find_range(conn, chr_val_handle); + if (svc == NULL) { + MODLOG_DFLT(DEBUG, "CAN'T FIND SERVICE FOR DISCOVERED DSC; HANDLE=%d\n", + conn_handle); + return NULL; + } + + chr = btshell_chr_find(svc, chr_val_handle, NULL); + if (chr == NULL) { + MODLOG_DFLT(DEBUG, "CAN'T FIND CHARACTERISTIC FOR DISCOVERED DSC; " + "HANDLE=%d\n", + conn_handle); + return NULL; + } + + dsc = btshell_dsc_find(chr, gatt_dsc->handle, &prev); + if (dsc != NULL) { + /* Descriptor already discovered. */ + return dsc; + } + + dsc = os_memblock_get(&btshell_dsc_pool); + if (dsc == NULL) { + console_printf("OOM WHILE DISCOVERING DESCRIPTOR\n"); + return NULL; + } + memset(dsc, 0, sizeof *dsc); + + dsc->dsc = *gatt_dsc; + + if (prev == NULL) { + SLIST_INSERT_HEAD(&chr->dscs, dsc, next); + } else { + SLIST_NEXT(prev, next) = dsc; + } + + return dsc; +} + +static struct btshell_conn * +btshell_conn_add(struct ble_gap_conn_desc *desc) +{ + struct btshell_conn *conn; + + assert(btshell_num_conns < MYNEWT_VAL(BLE_MAX_CONNECTIONS)); + + conn = btshell_conns + btshell_num_conns; + btshell_num_conns++; + + conn->handle = desc->conn_handle; + SLIST_INIT(&conn->svcs); + SLIST_INIT(&conn->coc_list); + + return conn; +} + +static void +btshell_conn_delete_idx(int idx) +{ + struct btshell_conn *conn; + struct btshell_svc *svc; + + assert(idx >= 0 && idx < btshell_num_conns); + + conn = btshell_conns + idx; + while ((svc = SLIST_FIRST(&conn->svcs)) != NULL) { + SLIST_REMOVE_HEAD(&conn->svcs, next); + btshell_svc_delete(svc); + } + + /* This '#if' is not strictly necessary. It is here to prevent a spurious + * warning from being reported. + */ +#if MYNEWT_VAL(BLE_MAX_CONNECTIONS) > 1 + int i; + for (i = idx + 1; i < btshell_num_conns; i++) { + btshell_conns[i - 1] = btshell_conns[i]; + } +#endif + + btshell_num_conns--; +} + +static int +btshell_on_mtu(uint16_t conn_handle, const struct ble_gatt_error *error, + uint16_t mtu, void *arg) +{ + switch (error->status) { + case 0: + console_printf("mtu exchange complete: conn_handle=%d mtu=%d\n", + conn_handle, mtu); + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static void +btshell_full_disc_complete(int rc) +{ + console_printf("full discovery complete; rc=%d\n", rc); + btshell_full_disc_prev_chr_val = 0; +} + +static void +btshell_disc_full_dscs(uint16_t conn_handle) +{ + struct btshell_conn *conn; + struct btshell_chr *chr; + struct btshell_svc *svc; + int rc; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + MODLOG_DFLT(DEBUG, "Failed to discover descriptors for conn=%d; " + "not connected\n", conn_handle); + btshell_full_disc_complete(BLE_HS_ENOTCONN); + return; + } + + SLIST_FOREACH(svc, &conn->svcs, next) { + SLIST_FOREACH(chr, &svc->chrs, next) { + if (!chr_is_empty(svc, chr) && + SLIST_EMPTY(&chr->dscs) && + btshell_full_disc_prev_chr_val <= chr->chr.def_handle) { + + rc = btshell_disc_all_dscs(conn_handle, + chr->chr.val_handle, + chr_end_handle(svc, chr)); + if (rc != 0) { + btshell_full_disc_complete(rc); + } + + btshell_full_disc_prev_chr_val = chr->chr.val_handle; + return; + } + } + } + + /* All descriptors discovered. */ + btshell_full_disc_complete(0); +} + +static void +btshell_disc_full_chrs(uint16_t conn_handle) +{ + struct btshell_conn *conn; + struct btshell_svc *svc; + int rc; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + MODLOG_DFLT(DEBUG, "Failed to discover characteristics for conn=%d; " + "not connected\n", conn_handle); + btshell_full_disc_complete(BLE_HS_ENOTCONN); + return; + } + + SLIST_FOREACH(svc, &conn->svcs, next) { + if (!svc->discovered) { + rc = btshell_disc_all_chrs_in_svc(conn_handle, svc); + if (rc != 0) { + btshell_full_disc_complete(rc); + } + return; + } + } + + /* All characteristics discovered. */ + btshell_disc_full_dscs(conn_handle); +} + +static int +btshell_on_disc_s(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) +{ + switch (error->status) { + case 0: + btshell_svc_add(conn_handle, service); + break; + + case BLE_HS_EDONE: + console_printf("service discovery successful\n"); + if (btshell_full_disc_prev_chr_val > 0) { + btshell_disc_full_chrs(conn_handle); + } + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_disc_c(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) +{ + intptr_t svc_start_handle; + + svc_start_handle = (intptr_t)arg; + + switch (error->status) { + case 0: + btshell_chr_add(conn_handle, svc_start_handle, chr); + break; + + case BLE_HS_EDONE: + console_printf("characteristic discovery successful\n"); + if (btshell_full_disc_prev_chr_val > 0) { + btshell_disc_full_chrs(conn_handle); + } + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_disc_c_in_s(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) +{ + struct btshell_svc *svc = arg; + + switch (error->status) { + case 0: + btshell_chr_add(conn_handle, svc->svc.start_handle, chr); + break; + + case BLE_HS_EDONE: + svc->discovered = true; + console_printf("characteristic discovery successful\n"); + if (btshell_full_disc_prev_chr_val > 0) { + btshell_disc_full_chrs(conn_handle); + } + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_disc_d(uint16_t conn_handle, const struct ble_gatt_error *error, + uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, + void *arg) +{ + switch (error->status) { + case 0: + btshell_dsc_add(conn_handle, chr_val_handle, dsc); + break; + + case BLE_HS_EDONE: + console_printf("descriptor discovery successful\n"); + if (btshell_full_disc_prev_chr_val > 0) { + btshell_disc_full_dscs(conn_handle); + } + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_read(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) +{ + switch (error->status) { + case 0: + console_printf("characteristic read; conn_handle=%d " + "attr_handle=%d len=%d value=", conn_handle, + attr->handle, OS_MBUF_PKTLEN(attr->om)); + print_mbuf(attr->om); + console_printf("\n"); + break; + + case BLE_HS_EDONE: + console_printf("characteristic read complete\n"); + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_write(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) +{ + switch (error->status) { + case 0: + console_printf("characteristic write complete; conn_handle=%d " + "attr_handle=%d\n", conn_handle, attr->handle); + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static int +btshell_on_write_reliable(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attrs, uint8_t num_attrs, + void *arg) +{ + int i; + + switch (error->status) { + case 0: + console_printf("characteristic write reliable complete; " + "conn_handle=%d", conn_handle); + + for (i = 0; i < num_attrs; i++) { + console_printf(" attr_handle=%d len=%d value=", attrs[i].handle, + OS_MBUF_PKTLEN(attrs[i].om)); + print_mbuf(attrs[i].om); + } + console_printf("\n"); + break; + + default: + btshell_print_error(NULL, conn_handle, error); + break; + } + + return 0; +} + +static void +btshell_decode_adv_data(const uint8_t *adv_data, uint8_t adv_data_len, void *arg) +{ + struct btshell_scan_opts *scan_opts = arg; + struct ble_hs_adv_fields fields; + + console_printf(" data_length=%d data=", adv_data_len); + + if (scan_opts) { + adv_data_len = min(adv_data_len, scan_opts->limit); + } + + print_bytes(adv_data, adv_data_len); + + console_printf(" fields:\n"); + ble_hs_adv_parse_fields(&fields, adv_data, adv_data_len); + btshell_print_adv_fields(&fields); +} + +#if MYNEWT_VAL(BLE_EXT_ADV) +static void +btshell_decode_event_type(struct ble_gap_ext_disc_desc *desc, void *arg) +{ + struct btshell_scan_opts *scan_opts = arg; + uint8_t directed = 0; + + if (desc->props & BLE_HCI_ADV_LEGACY_MASK) { + if (scan_opts && scan_opts->ignore_legacy) { + return; + } + + console_printf("Legacy PDU type %d", desc->legacy_event_type); + if (desc->legacy_event_type == BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) { + directed = 1; + } + goto common_data; + } else { + if (scan_opts && scan_opts->periodic_only && desc->periodic_adv_itvl == 0) { + return; + } + } + + console_printf("Extended adv: "); + if (desc->props & BLE_HCI_ADV_CONN_MASK) { + console_printf("'conn' "); + } + if (desc->props & BLE_HCI_ADV_SCAN_MASK) { + console_printf("'scan' "); + } + if (desc->props & BLE_HCI_ADV_DIRECT_MASK) { + console_printf("'dir' "); + directed = 1; + } + + if (desc->props & BLE_HCI_ADV_SCAN_RSP_MASK) { + console_printf("'scan rsp' "); + } + + switch(desc->data_status) { + case BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE: + console_printf("complete"); + break; + case BLE_GAP_EXT_ADV_DATA_STATUS_INCOMPLETE: + console_printf("incomplete"); + break; + case BLE_GAP_EXT_ADV_DATA_STATUS_TRUNCATED: + console_printf("truncated"); + break; + default: + console_printf("reserved %d", desc->data_status); + break; + } + +common_data: + console_printf(" rssi=%d txpower=%d, pphy=%d, sphy=%d, sid=%d," + " periodic_adv_itvl=%u, addr_type=%d addr=", + desc->rssi, desc->tx_power, desc->prim_phy, desc->sec_phy, + desc->sid, desc->periodic_adv_itvl, desc->addr.type); + print_addr(desc->addr.val); + if (directed) { + console_printf(" init_addr_type=%d inita=", desc->direct_addr.type); + print_addr(desc->direct_addr.val); + } + + console_printf("\n"); + + if(!desc->length_data) { + return; + } + + btshell_decode_adv_data(desc->data, desc->length_data, arg); +} +#endif + +static int +btshell_restart_adv(struct ble_gap_event *event) +{ + int rc = 0; +#if MYNEWT_VAL(BLE_EXT_ADV) + uint8_t i; +#endif + + if (event->type != BLE_GAP_EVENT_DISCONNECT) { + return -1; + } + +#if MYNEWT_VAL(BLE_EXT_ADV) + for (i = 0; i < BLE_ADV_INSTANCES; ++i) { + if (ext_adv_restart[i].restart && + (ext_adv_restart[i].conn_handle == + event->disconnect.conn.conn_handle)) { + rc = ble_gap_ext_adv_start(i, 0, 0); + break; + } + } +#else + if (!adv_params.restart) { + return 0; + } + + rc = ble_gap_adv_start(adv_params.own_addr_type, &adv_params.direct_addr, + adv_params.duration_ms, &adv_params.params, + btshell_gap_event, NULL); +#endif + + return rc; +} + +#if MYNEWT_VAL(BLE_PERIODIC_ADV) +struct psync { + bool established; + unsigned int complete; + unsigned int truncated; + size_t off; + bool changed; + uint8_t data[1650]; /* TODO make this configurable */ +}; + +static struct psync g_periodic_data[MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)]; + +void +btshell_sync_stats(uint16_t handle) +{ + struct psync *psync; + + if (handle >= MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)) { + return; + } + + psync = &g_periodic_data[handle]; + if (!psync->established) { + console_printf("Sync not established\n"); + return; + } + + console_printf("completed=%u truncated=%u\n", + psync->complete, psync->truncated); +} + +static void +handle_periodic_report(struct ble_gap_event *event) +{ + struct psync *psync; + uint16_t handle = event->periodic_report.sync_handle; + + if (handle >= MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)) { + return; + } + + psync = &g_periodic_data[handle]; + + if (psync->changed || + memcmp(psync->data + psync->off, event->periodic_report.data, + event->periodic_report.data_length)) { + /* first fragment with changed data */ + if (!psync->changed) { + console_printf("Sync data changed, completed=%u, truncated=%u\n", + psync->complete, psync->truncated); + } + + psync->changed = true; + + console_printf("Sync report handle=%u status=", handle); + switch(event->periodic_report.data_status) { + case BLE_HCI_PERIODIC_DATA_STATUS_COMPLETE: + console_printf("complete"); + break; + case BLE_HCI_PERIODIC_DATA_STATUS_INCOMPLETE: + console_printf("incomplete"); + break; + case BLE_HCI_PERIODIC_DATA_STATUS_TRUNCATED: + console_printf("truncated"); + break; + default: + console_printf("reserved 0x%x", event->periodic_report.data_status); + break; + } + + btshell_decode_adv_data(event->periodic_report.data, + event->periodic_report.data_length, NULL); + + psync->complete = 0; + psync->truncated = 0; + } + + /* cache data */ + memcpy(psync->data + psync->off, event->periodic_report.data, + event->periodic_report.data_length); + + switch(event->periodic_report.data_status) { + case BLE_HCI_PERIODIC_DATA_STATUS_INCOMPLETE: + psync->off += event->periodic_report.data_length; + break; + case BLE_HCI_PERIODIC_DATA_STATUS_COMPLETE: + psync->complete++; + psync->off = 0; + psync->changed = false; + break; + case BLE_HCI_PERIODIC_DATA_STATUS_TRUNCATED: + psync->truncated++; + psync->off = 0; + psync->changed = false; + break; + default: + break; + } +} +#endif + +int +btshell_gap_event(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int conn_idx; + int rc; +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + struct psync *psync; +#endif + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + console_printf("connection %s; status=%d ", + event->connect.status == 0 ? "established" : "failed", + event->connect.status); + + if (event->connect.status == 0) { + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + print_conn_desc(&desc); + btshell_conn_add(&desc); + } + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + console_printf("disconnect; reason=%d ", event->disconnect.reason); + print_conn_desc(&event->disconnect.conn); + + conn_idx = btshell_conn_find_idx(event->disconnect.conn.conn_handle); + if (conn_idx != -1) { + btshell_conn_delete_idx(conn_idx); + } + + return btshell_restart_adv(event); +#if MYNEWT_VAL(BLE_EXT_ADV) + case BLE_GAP_EVENT_EXT_DISC: + btshell_decode_event_type(&event->ext_disc, arg); + return 0; +#endif + case BLE_GAP_EVENT_DISC: + console_printf("received advertisement; event_type=%d rssi=%d " + "addr_type=%d addr=", event->disc.event_type, + event->disc.rssi, event->disc.addr.type); + print_addr(event->disc.addr.val); + + /* + * There is no adv data to print in case of connectable + * directed advertising + */ + if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) { + console_printf("\nConnectable directed advertising event\n"); + return 0; + } + + btshell_decode_adv_data(event->disc.data, event->disc.length_data, arg); + + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE: + console_printf("connection updated; status=%d ", + event->conn_update.status); + rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc); + assert(rc == 0); + print_conn_desc(&desc); + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE_REQ: + console_printf("connection update request\n"); + *event->conn_update_req.self_params = + *event->conn_update_req.peer_params; + return 0; + + case BLE_GAP_EVENT_PASSKEY_ACTION: + console_printf("passkey action event; action=%d", + event->passkey.params.action); + if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) { + console_printf(" numcmp=%lu", + (unsigned long)event->passkey.params.numcmp); + } + console_printf("\n"); + return 0; + + + case BLE_GAP_EVENT_DISC_COMPLETE: + console_printf("discovery complete; reason=%d\n", + event->disc_complete.reason); + return 0; + + case BLE_GAP_EVENT_ADV_COMPLETE: +#if MYNEWT_VAL(BLE_EXT_ADV) + console_printf("advertise complete; reason=%d, instance=%u, handle=%d\n", + event->adv_complete.reason, event->adv_complete.instance, + event->adv_complete.conn_handle); + + ext_adv_restart[event->adv_complete.instance].conn_handle = + event->adv_complete.conn_handle; +#else + console_printf("advertise complete; reason=%d\n", + event->adv_complete.reason); +#endif + return 0; + + case BLE_GAP_EVENT_ENC_CHANGE: + console_printf("encryption change event; status=%d ", + event->enc_change.status); + rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc); + assert(rc == 0); + print_conn_desc(&desc); + return 0; + + case BLE_GAP_EVENT_NOTIFY_RX: + console_printf("notification rx event; attr_handle=%d indication=%d " + "len=%d data=", + event->notify_rx.attr_handle, + event->notify_rx.indication, + OS_MBUF_PKTLEN(event->notify_rx.om)); + + print_mbuf(event->notify_rx.om); + console_printf("\n"); + return 0; + + case BLE_GAP_EVENT_NOTIFY_TX: + console_printf("notification tx event; status=%d attr_handle=%d " + "indication=%d\n", + event->notify_tx.status, + event->notify_tx.attr_handle, + event->notify_tx.indication); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + console_printf("subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=%d\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate, + event->subscribe.cur_indicate); + return 0; + + case BLE_GAP_EVENT_MTU: + console_printf("mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + + case BLE_GAP_EVENT_IDENTITY_RESOLVED: + console_printf("identity resolved "); + rc = ble_gap_conn_find(event->identity_resolved.conn_handle, &desc); + assert(rc == 0); + print_conn_desc(&desc); + return 0; + + case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: + console_printf("PHY update complete; status=%d, conn_handle=%d " + " tx_phy=%d, rx_phy=%d\n", + event->phy_updated.status, + event->phy_updated.conn_handle, + event->phy_updated.tx_phy, + event->phy_updated.rx_phy); + return 0; + + case BLE_GAP_EVENT_REPEAT_PAIRING: + /* We already have a bond with the peer, but it is attempting to + * establish a new secure link. This app sacrifices security for + * convenience: just throw away the old bond and accept the new link. + */ + + /* Delete the old bond. */ + rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + assert(rc == 0); + ble_store_util_delete_peer(&desc.peer_id_addr); + + /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should + * continue with the pairing operation. + */ + return BLE_GAP_REPEAT_PAIRING_RETRY; +#if MYNEWT_VAL(BLE_PERIODIC_ADV) + case BLE_GAP_EVENT_PERIODIC_SYNC: + if (event->periodic_sync.status) { + console_printf("Periodic Sync Establishment Failed; status=%u\n", + event->periodic_sync.status); + } else { + console_printf("Periodic Sync Established; sync_handle=%u sid=%u " + "phy=%u adv_interval=%u ca=%u addr_type=%u addr=", + event->periodic_sync.sync_handle, + event->periodic_sync.sid, event->periodic_sync.adv_phy, + event->periodic_sync.per_adv_ival, + event->periodic_sync.adv_clk_accuracy, + event->periodic_sync.adv_addr.type); + print_addr(event->periodic_sync.adv_addr.val); + console_printf("\n"); + + /* TODO non-NimBLE controllers may not start handles from 0 */ + if (event->periodic_sync.sync_handle >= MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)) { + console_printf("Unable to prepare cache for sync data\n"); + } else { + psync = &g_periodic_data[event->periodic_sync.sync_handle]; + memset(psync, 0, sizeof(*psync)); + psync->changed = true; + psync->established = true; + } + } + return 0; + case BLE_GAP_EVENT_PERIODIC_SYNC_LOST: + /* TODO non-NimBLE controllers may not start handles from 0 */ + if (event->periodic_sync_lost.sync_handle >= MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)) { + console_printf("Periodic Sync Lost; sync_handle=%d reason=%d\n", + event->periodic_sync_lost.sync_handle, + event->periodic_sync_lost.reason); + } else { + psync = &g_periodic_data[event->periodic_sync_lost.sync_handle]; + + console_printf("Periodic Sync Lost; sync_handle=%d reason=%d completed=%u truncated=%u\n", + event->periodic_sync_lost.sync_handle, + event->periodic_sync_lost.reason, + psync->complete, psync->truncated); + + memset(psync, 0, sizeof(*psync)); + } + return 0; + case BLE_GAP_EVENT_PERIODIC_REPORT: + handle_periodic_report(event); + return 0; +#if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER) + case BLE_GAP_EVENT_PERIODIC_TRANSFER: + console_printf("Periodic Sync Transfer Received on conn=%u; status=%u," + " sync_handle=%u sid=%u phy=%u adv_interval=%u ca=%u " + "addr_type=%u addr=", + event->periodic_transfer.conn_handle, + event->periodic_transfer.status, + event->periodic_transfer.sync_handle, + event->periodic_transfer.sid, + event->periodic_transfer.adv_phy, + event->periodic_transfer.per_adv_itvl, + event->periodic_transfer.adv_clk_accuracy, + event->periodic_transfer.adv_addr.type); + print_addr(event->periodic_transfer.adv_addr.val); + console_printf("\n"); + + if (!event->periodic_transfer.status) { + /* TODO non-NimBLE controllers may not start handles from 0 */ + if (event->periodic_transfer.sync_handle >= MYNEWT_VAL(BLE_MAX_PERIODIC_SYNCS)) { + console_printf("Unable to prepare cache for sync data\n"); + } else { + psync = &g_periodic_data[event->periodic_transfer.sync_handle]; + memset(psync, 0, sizeof(*psync)); + psync->changed = true; + psync->established = true; + } + } + return 0; +#endif +#endif + default: + return 0; + } +} + +static void +btshell_on_l2cap_update(uint16_t conn_handle, int status, void *arg) +{ + console_printf("l2cap update complete; conn_handle=%d status=%d\n", + conn_handle, status); +} + +static void +btshell_tx_timer_cb(struct os_event *ev) +{ + uint8_t i; + uint8_t len; + int32_t timeout; + struct ble_l2cap_hdr l2cap_hdr; + struct os_mbuf *om; + + if ((btshell_tx_data.tx_num == 0) || (btshell_tx_data.tx_len == 0)) { + return; + } + + console_printf("Sending %d/%d len: %d\n", + btshell_tx_data.tx_num_requested - btshell_tx_data.tx_num + 1, + btshell_tx_data.tx_num_requested, btshell_tx_data.tx_len); + + len = btshell_tx_data.tx_len; + + om = NULL; + if (os_msys_num_free() >= 4) { + om = os_msys_get_pkthdr(len + BLE_L2CAP_HDR_SZ, BLE_HCI_DATA_HDR_SZ); + } + + if (om) { + /* + * NOTE: CID is 0xffff so it is not confused with valid l2cap channel. + * The rest of the data gets filled with incrementing pattern starting + * from 0. + */ + put_le16(&l2cap_hdr.len, len); + put_le16(&l2cap_hdr.cid, 0xffff); + + os_mbuf_append(om, (void *)&l2cap_hdr, BLE_L2CAP_HDR_SZ); + + for (i = 0; i < len; ++i) { + os_mbuf_append(om, (void *)&i, 1); + } + + ble_hs_lock(); + ble_hs_hci_acl_tx_now(btshell_tx_data.conn, &om); + ble_hs_unlock(); + + --btshell_tx_data.tx_num; + } + + if (btshell_tx_data.tx_num) { + timeout = (int32_t)btshell_tx_data.tx_rate; + timeout = (timeout * OS_TICKS_PER_SEC) / 1000; + os_callout_reset(&btshell_tx_timer, timeout); + } +} + +int +btshell_exchange_mtu(uint16_t conn_handle) +{ + int rc; + + rc = ble_gattc_exchange_mtu(conn_handle, btshell_on_mtu, NULL); + return rc; +} + +int +btshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle) +{ + intptr_t svc_start_handle; + int rc; + + svc_start_handle = start_handle; + rc = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle, + btshell_on_disc_c, (void *)svc_start_handle); + return rc; +} + +int +btshell_disc_all_chrs_in_svc(uint16_t conn_handle, struct btshell_svc *svc) +{ + int rc; + + rc = ble_gattc_disc_all_chrs(conn_handle, svc->svc.start_handle, + svc->svc.end_handle, btshell_on_disc_c_in_s, + svc); + return rc; +} + +int +btshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid) +{ + intptr_t svc_start_handle; + int rc; + + svc_start_handle = start_handle; + rc = ble_gattc_disc_chrs_by_uuid(conn_handle, start_handle, end_handle, + uuid, btshell_on_disc_c, + (void *)svc_start_handle); + return rc; +} + +int +btshell_disc_svcs(uint16_t conn_handle) +{ + int rc; + + rc = ble_gattc_disc_all_svcs(conn_handle, btshell_on_disc_s, NULL); + return rc; +} + +int +btshell_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid) +{ + int rc; + + rc = ble_gattc_disc_svc_by_uuid(conn_handle, uuid, + btshell_on_disc_s, NULL); + return rc; +} + +int +btshell_disc_all_dscs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle) +{ + int rc; + + rc = ble_gattc_disc_all_dscs(conn_handle, start_handle, end_handle, + btshell_on_disc_d, NULL); + return rc; +} + +int +btshell_disc_full(uint16_t conn_handle) +{ + struct btshell_conn *conn; + struct btshell_svc *svc; + + /* Undiscover everything first. */ + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + return BLE_HS_ENOTCONN; + } + + while ((svc = SLIST_FIRST(&conn->svcs)) != NULL) { + SLIST_REMOVE_HEAD(&conn->svcs, next); + btshell_svc_delete(svc); + } + + btshell_full_disc_prev_chr_val = 1; + btshell_disc_svcs(conn_handle); + + return 0; +} + +int +btshell_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle) +{ + int rc; + + rc = ble_gattc_find_inc_svcs(conn_handle, start_handle, end_handle, + btshell_on_disc_s, NULL); + return rc; +} + +int +btshell_read(uint16_t conn_handle, uint16_t attr_handle) +{ + struct os_mbuf *om; + int rc; + + if (conn_handle == BLE_HS_CONN_HANDLE_NONE) { + rc = ble_att_svr_read_local(attr_handle, &om); + if (rc == 0) { + console_printf("read local; attr_handle=%d len=%d value=", + attr_handle, OS_MBUF_PKTLEN(om)); + print_mbuf(om); + console_printf("\n"); + + os_mbuf_free_chain(om); + } + } else { + rc = ble_gattc_read(conn_handle, attr_handle, btshell_on_read, NULL); + } + return rc; +} + +int +btshell_read_long(uint16_t conn_handle, uint16_t attr_handle, uint16_t offset) +{ + int rc; + + rc = ble_gattc_read_long(conn_handle, attr_handle, offset, + btshell_on_read, NULL); + return rc; +} + +int +btshell_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid) +{ + int rc; + + rc = ble_gattc_read_by_uuid(conn_handle, start_handle, end_handle, uuid, + btshell_on_read, NULL); + return rc; +} + +int +btshell_read_mult(uint16_t conn_handle, uint16_t *attr_handles, + int num_attr_handles) +{ + int rc; + + rc = ble_gattc_read_mult(conn_handle, attr_handles, num_attr_handles, + btshell_on_read, NULL); + return rc; +} + +int +btshell_write(uint16_t conn_handle, uint16_t attr_handle, struct os_mbuf *om) +{ + int rc; + + if (conn_handle == BLE_HS_CONN_HANDLE_NONE) { + rc = ble_att_svr_write_local(attr_handle, om); + } else { + rc = ble_gattc_write(conn_handle, attr_handle, om, + btshell_on_write, NULL); + } + + return rc; +} + +int +btshell_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, + struct os_mbuf *om) +{ + int rc; + + rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, om); + + return rc; +} + +int +btshell_write_long(uint16_t conn_handle, uint16_t attr_handle, + uint16_t offset, struct os_mbuf *om) +{ + int rc; + + rc = ble_gattc_write_long(conn_handle, attr_handle, offset, + om, btshell_on_write, NULL); + return rc; +} + +int +btshell_write_reliable(uint16_t conn_handle, + struct ble_gatt_attr *attrs, + int num_attrs) +{ + int rc; + + rc = ble_gattc_write_reliable(conn_handle, attrs, num_attrs, + btshell_on_write_reliable, NULL); + return rc; +} + +#if MYNEWT_VAL(BLE_EXT_ADV) +int +btshell_ext_adv_configure(uint8_t instance, + const struct ble_gap_ext_adv_params *params, + int8_t *selected_tx_power) +{ + return ble_gap_ext_adv_configure(instance, params, selected_tx_power, + btshell_gap_event, NULL); +} + +int +btshell_ext_adv_start(uint8_t instance, int duration, + int max_events, bool restart) +{ + int rc; + + /* Advertising restart doesn't make sense + * with limited duration or events + */ + if (restart && (duration == 0) && (max_events == 0)) { + ext_adv_restart[instance].restart = restart; + } + + rc = ble_gap_ext_adv_start(instance, duration, max_events); + + return rc; +} + +int +btshell_ext_adv_stop(uint8_t instance) +{ + int rc; + + ext_adv_restart[instance].restart = false; + + rc = ble_gap_ext_adv_stop(instance); + + return rc; +} +#endif + +int +btshell_adv_stop(void) +{ + int rc; + + adv_params.restart = false; + + rc = ble_gap_adv_stop(); + return rc; +} + +int +btshell_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr, + int32_t duration_ms, const struct ble_gap_adv_params *params, + bool restart) +{ + int rc; + + if (restart) { + adv_params.restart = restart; + adv_params.own_addr_type = own_addr_type; + adv_params.duration_ms = duration_ms; + + if (direct_addr) { + memcpy(&adv_params.direct_addr, direct_addr, sizeof(adv_params.direct_addr)); + } + + if (params) { + memcpy(&adv_params.params, params, sizeof(adv_params.params)); + } + } + + rc = ble_gap_adv_start(own_addr_type, direct_addr, duration_ms, params, + btshell_gap_event, NULL); + return rc; +} + +int +btshell_conn_initiate(uint8_t own_addr_type, const ble_addr_t *peer_addr, + int32_t duration_ms, struct ble_gap_conn_params *params) +{ + int rc; + + rc = ble_gap_connect(own_addr_type, peer_addr, duration_ms, params, + btshell_gap_event, NULL); + + return rc; +} + +int +btshell_ext_conn_initiate(uint8_t own_addr_type, const ble_addr_t *peer_addr, + int32_t duration_ms, + struct ble_gap_conn_params *phy_1m_params, + struct ble_gap_conn_params *phy_2m_params, + struct ble_gap_conn_params *phy_coded_params) +{ +#if !MYNEWT_VAL(BLE_EXT_ADV) + console_printf("BLE extended advertising not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + int rc; + uint8_t phy_mask = 0; + + if (phy_1m_params) { + phy_mask |= BLE_GAP_LE_PHY_1M_MASK; + } + + if (phy_2m_params) { + phy_mask |= BLE_GAP_LE_PHY_2M_MASK; + } + + if (phy_coded_params) { + phy_mask |= BLE_GAP_LE_PHY_CODED_MASK; + } + + rc = ble_gap_ext_connect(own_addr_type, peer_addr, duration_ms, phy_mask, + phy_1m_params, phy_2m_params, phy_coded_params, + btshell_gap_event, NULL); + + return rc; +#endif +} + +int +btshell_conn_cancel(void) +{ + int rc; + + rc = ble_gap_conn_cancel(); + return rc; +} + +int +btshell_term_conn(uint16_t conn_handle, uint8_t reason) +{ + int rc; + + rc = ble_gap_terminate(conn_handle, reason); + return rc; +} + +int +btshell_wl_set(ble_addr_t *addrs, int addrs_count) +{ + int rc; + + rc = ble_gap_wl_set(addrs, addrs_count); + return rc; +} + +int +btshell_scan(uint8_t own_addr_type, int32_t duration_ms, + const struct ble_gap_disc_params *disc_params, void *cb_args) +{ + int rc; + + rc = ble_gap_disc(own_addr_type, duration_ms, disc_params, + btshell_gap_event, cb_args); + return rc; +} + +int +btshell_ext_scan(uint8_t own_addr_type, uint16_t duration, uint16_t period, + uint8_t filter_duplicates, uint8_t filter_policy, + uint8_t limited, + const struct ble_gap_ext_disc_params *uncoded_params, + const struct ble_gap_ext_disc_params *coded_params, + void *cb_args) +{ +#if !MYNEWT_VAL(BLE_EXT_ADV) + console_printf("BLE extended advertising not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + int rc; + + rc = ble_gap_ext_disc(own_addr_type, duration, period, filter_duplicates, + filter_policy, limited, uncoded_params, coded_params, + btshell_gap_event, cb_args); + return rc; +#endif +} + +int +btshell_scan_cancel(void) +{ + int rc; + + rc = ble_gap_disc_cancel(); + return rc; +} + +int +btshell_update_conn(uint16_t conn_handle, struct ble_gap_upd_params *params) +{ + int rc; + + rc = ble_gap_update_params(conn_handle, params); + return rc; +} + +void +btshell_notify(uint16_t attr_handle) +{ + ble_gatts_chr_updated(attr_handle); +} + +int +btshell_datalen(uint16_t conn_handle, uint16_t tx_octets, uint16_t tx_time) +{ + int rc; + + rc = ble_hs_hci_util_set_data_len(conn_handle, tx_octets, tx_time); + return rc; +} + +int +btshell_l2cap_update(uint16_t conn_handle, + struct ble_l2cap_sig_update_params *params) +{ + int rc; + + rc = ble_l2cap_sig_update(conn_handle, params, btshell_on_l2cap_update, + NULL); + return rc; +} + +int +btshell_sec_pair(uint16_t conn_handle) +{ +#if !NIMBLE_BLE_SM + return BLE_HS_ENOTSUP; +#endif + + int rc; + + rc = ble_gap_pair_initiate(conn_handle); + return rc; +} + +int +btshell_sec_unpair(ble_addr_t *peer_addr) +{ +#if !NIMBLE_BLE_SM + return BLE_HS_ENOTSUP; +#endif + + int rc; + + rc = ble_gap_unpair(peer_addr); + return rc; +} + +int +btshell_sec_start(uint16_t conn_handle) +{ +#if !NIMBLE_BLE_SM + return BLE_HS_ENOTSUP; +#endif + + int rc; + + rc = ble_gap_security_initiate(conn_handle); + return rc; +} + +int +btshell_sec_restart(uint16_t conn_handle, + uint8_t key_size, + uint8_t *ltk, + uint16_t ediv, + uint64_t rand_val, + int auth) +{ +#if !NIMBLE_BLE_SM + return BLE_HS_ENOTSUP; +#endif + + struct ble_store_value_sec value_sec; + struct ble_store_key_sec key_sec; + struct ble_gap_conn_desc desc; + ble_hs_conn_flags_t conn_flags; + int rc; + + if (ltk == NULL) { + /* The user is requesting a store lookup. */ + rc = ble_gap_conn_find(conn_handle, &desc); + if (rc != 0) { + return rc; + } + + memset(&key_sec, 0, sizeof key_sec); + key_sec.peer_addr = desc.peer_id_addr; + + rc = ble_hs_atomic_conn_flags(conn_handle, &conn_flags); + if (rc != 0) { + return rc; + } + if (conn_flags & BLE_HS_CONN_F_MASTER) { + rc = ble_store_read_peer_sec(&key_sec, &value_sec); + } else { + rc = ble_store_read_our_sec(&key_sec, &value_sec); + } + if (rc != 0) { + return rc; + } + + ltk = value_sec.ltk; + key_size = value_sec.key_size; + ediv = value_sec.ediv; + rand_val = value_sec.rand_num; + auth = value_sec.authenticated; + } + + rc = ble_gap_encryption_initiate(conn_handle, key_size, ltk, + ediv, rand_val, auth); + return rc; +} + +/** + * Called to start transmitting 'num' packets at rate 'rate' of size 'size' + * to connection handle 'handle' + * + * @param handle + * @param len + * @param rate + * @param num + * + * @return int + */ +int +btshell_tx_start(uint16_t conn_handle, uint16_t len, uint16_t rate, uint16_t num) +{ + /* Cannot be currently in a session */ + if (num == 0) { + return 0; + } + + /* Do not allow start if already in progress */ + if (btshell_tx_data.tx_num != 0) { + return -1; + } + + /* XXX: for now, must have contiguous mbuf space */ + if ((len + 4) > MYNEWT_VAL_MSYS_1_BLOCK_SIZE) { + return -2; + } + + btshell_tx_data.tx_num = num; + btshell_tx_data.tx_num_requested = num; + btshell_tx_data.tx_rate = rate; + btshell_tx_data.tx_len = len; + btshell_tx_data.tx_conn_handle = conn_handle; + + ble_hs_lock(); + btshell_tx_data.conn = ble_hs_conn_find(conn_handle); + ble_hs_unlock(); + + if (!btshell_tx_data.conn) { + console_printf("Could not find ble_hs_conn for handle: %d\n", + conn_handle); + return -1; + } + + os_callout_reset(&btshell_tx_timer, 0); + + return 0; +} + +void +btshell_tx_stop(void) +{ + os_callout_stop(&btshell_tx_timer); + btshell_tx_data.tx_num = 0; +} + +int +btshell_rssi(uint16_t conn_handle, int8_t *out_rssi) +{ + int rc; + + rc = ble_gap_conn_rssi(conn_handle, out_rssi); + if (rc != 0) { + return rc; + } + + return 0; +} + +static void +btshell_on_reset(int reason) +{ + console_printf("Error: Resetting state; reason=%d\n", reason); +} + +static void +btshell_on_sync(void) +{ +#if MYNEWT_VAL(BLE_SM_SC) + int rc; + + rc = ble_sm_sc_oob_generate_data(&oob_data_local); + if (rc) { + console_printf("Error: generating oob data; reason=%d\n", rc); + return; + } +#endif + + console_printf("Host and controller synced\n"); +} + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0 + +static int +btshell_l2cap_coc_add(uint16_t conn_handle, struct ble_l2cap_chan *chan) +{ + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + struct btshell_l2cap_coc *prev, *cur; + + conn = btshell_conn_find(conn_handle); + assert(conn != NULL); + + coc = os_memblock_get(&btshell_coc_conn_pool); + if (!coc) { + return ENOMEM; + } + + coc->chan = chan; + + prev = NULL; + SLIST_FOREACH(cur, &conn->coc_list, next) { + prev = cur; + } + + if (prev == NULL) { + SLIST_INSERT_HEAD(&conn->coc_list, coc, next); + } else { + SLIST_INSERT_AFTER(prev, coc, next); + } + + return 0; +} + +static void +btshell_l2cap_coc_remove(uint16_t conn_handle, struct ble_l2cap_chan *chan) +{ + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + struct btshell_l2cap_coc *cur; + + conn = btshell_conn_find(conn_handle); + assert(conn != NULL); + + coc = NULL; + SLIST_FOREACH(cur, &conn->coc_list, next) { + if (cur->chan == chan) { + coc = cur; + break; + } + } + + if (!coc) { + return; + } + + SLIST_REMOVE(&conn->coc_list, coc, btshell_l2cap_coc, next); + os_memblock_put(&btshell_coc_conn_pool, coc); +} + +static void +btshell_l2cap_coc_recv(struct ble_l2cap_chan *chan, struct os_mbuf *sdu) +{ + console_printf("LE CoC SDU received, chan: 0x%08lx, data len %d\n", + (uint32_t) chan, OS_MBUF_PKTLEN(sdu)); + + os_mbuf_free_chain(sdu); + sdu = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); + assert(sdu != NULL); + + if (ble_l2cap_recv_ready(chan, sdu) != 0) { + assert(0); + } +} + +static int +btshell_l2cap_coc_accept(uint16_t conn_handle, uint16_t peer_mtu, + struct ble_l2cap_chan *chan) +{ + struct os_mbuf *sdu_rx; + + console_printf("LE CoC accepting, chan: 0x%08lx, peer_mtu %d\n", + (uint32_t) chan, peer_mtu); + + sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); + if (!sdu_rx) { + return BLE_HS_ENOMEM; + } + + return ble_l2cap_recv_ready(chan, sdu_rx); +} + +static void +btshell_l2cap_coc_unstalled(uint16_t conn_handle, struct ble_l2cap_chan *chan) +{ + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + struct btshell_l2cap_coc *cur; + + conn = btshell_conn_find(conn_handle); + assert(conn != NULL); + + coc = NULL; + SLIST_FOREACH(cur, &conn->coc_list, next) { + if (cur->chan == chan) { + coc = cur; + break; + } + } + + if (!coc) { + return; + } + + coc->stalled = false; +} + +static int +btshell_l2cap_event(struct ble_l2cap_event *event, void *arg) +{ + int accept_response; + struct ble_l2cap_chan_info chan_info; + + switch(event->type) { + case BLE_L2CAP_EVENT_COC_CONNECTED: + if (event->connect.status) { + console_printf("LE COC error: %d\n", event->connect.status); + return 0; + } + + if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) { + assert(0); + } + + console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x, scid: 0x%04x, " + "dcid: 0x%04x, our_mps: %d, our_mtu: %d, peer_mps: %d, peer_mtu: %d\n", + event->connect.conn_handle, event->connect.chan, + chan_info.psm, chan_info.scid, chan_info.dcid, + chan_info.our_l2cap_mtu, chan_info.our_coc_mtu, chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu); + + btshell_l2cap_coc_add(event->connect.conn_handle, + event->connect.chan); + + return 0; + case BLE_L2CAP_EVENT_COC_DISCONNECTED: + console_printf("LE CoC disconnected, chan: %p\n", + event->disconnect.chan); + + btshell_l2cap_coc_remove(event->disconnect.conn_handle, + event->disconnect.chan); + return 0; + case BLE_L2CAP_EVENT_COC_ACCEPT: + accept_response = PTR_TO_INT(arg); + if (accept_response) { + return accept_response; + } + + return btshell_l2cap_coc_accept(event->accept.conn_handle, + event->accept.peer_sdu_size, + event->accept.chan); + + case BLE_L2CAP_EVENT_COC_DATA_RECEIVED: + btshell_l2cap_coc_recv(event->receive.chan, event->receive.sdu_rx); + return 0; + case BLE_L2CAP_EVENT_COC_RECONFIG_COMPLETED: + + if (ble_l2cap_get_chan_info(event->reconfigured.chan, &chan_info)) { + assert(0); + } + + console_printf("LE CoC reconfigure completed status 0x%02x," \ + "chan: %p\n", + event->reconfigured.status, + event->reconfigured.chan); + + if (event->reconfigured.status == 0) { + console_printf("\t our_mps: %d our_mtu %d\n", chan_info.our_l2cap_mtu, chan_info.our_coc_mtu); + } + return 0; + case BLE_L2CAP_EVENT_COC_PEER_RECONFIGURED: + + if (ble_l2cap_get_chan_info(event->reconfigured.chan, &chan_info)) { + assert(0); + } + + console_printf("LE CoC peer reconfigured status 0x%02x," \ + "chan: %p\n", + event->reconfigured.status, + event->reconfigured.chan); + + if (event->reconfigured.status == 0) { + console_printf("\t peer_mps: %d peer_mtu %d\n", chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu); + } + + return 0; + case BLE_L2CAP_EVENT_COC_TX_UNSTALLED: + console_printf("L2CAP CoC channel %p unstalled, last sdu sent with err=0x%02x\n", + event->tx_unstalled.chan, event->tx_unstalled.status); + btshell_l2cap_coc_unstalled(event->tx_unstalled.conn_handle, event->tx_unstalled.chan); + return 0; + default: + return 0; + } +} +#endif + +int +btshell_l2cap_create_srv(uint16_t psm, uint16_t mtu, int accept_response) +{ +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) == 0 + console_printf("BLE L2CAP LE COC not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + + if (mtu == 0 || mtu > BTSHELL_COC_MTU) { + mtu = BTSHELL_COC_MTU; + } + + return ble_l2cap_create_server(psm, mtu, btshell_l2cap_event, + INT_TO_PTR(accept_response)); +#endif +} + +int +btshell_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu, uint8_t num) +{ +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) == 0 + console_printf("BLE L2CAP LE COC not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + + struct os_mbuf *sdu_rx[num]; + int i; + + if (mtu == 0 || mtu > BTSHELL_COC_MTU) { + mtu = BTSHELL_COC_MTU; + } + + console_printf("L2CAP CoC MTU: %d, max available %d\n", mtu, BTSHELL_COC_MTU); + + for (i = 0; i < num; i++) { + sdu_rx[i] = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); + assert(sdu_rx != NULL); + } + + if (num == 1) { + return ble_l2cap_connect(conn_handle, psm, mtu, sdu_rx[0], + btshell_l2cap_event, NULL); + } + + return ble_l2cap_enhanced_connect(conn_handle, psm, mtu, + num, sdu_rx,btshell_l2cap_event, NULL); +#endif +} + +int +btshell_l2cap_disconnect(uint16_t conn_handle, uint16_t idx) +{ +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) == 0 + console_printf("BLE L2CAP LE COC not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + int i; + int rc = 0; + + conn = btshell_conn_find(conn_handle); + assert(conn != NULL); + + i = 0; + SLIST_FOREACH(coc, &conn->coc_list, next) { + if (i == idx) { + break; + } + i++; + } + assert(coc != NULL); + + rc = ble_l2cap_disconnect(coc->chan); + if (rc) { + console_printf("Could not disconnect channel rc=%d\n", rc); + } + + return rc; +#endif +} + +int +btshell_l2cap_reconfig(uint16_t conn_handle, uint16_t mtu, + uint8_t num, uint8_t idxs[]) +{ + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + struct ble_l2cap_chan * chans[5] = {0}; + int i, j; + int cnt; + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + console_printf("conn=%d does not exist\n", conn_handle); + return 0; + } + + i = 0; + j = 0; + cnt = 0; + SLIST_FOREACH(coc, &conn->coc_list, next) { + for (i = 0; i < num; i++) { + if (idxs[i] == j) { + chans[cnt] = coc->chan; + cnt++; + break; + } + } + j++; + } + + if (cnt != num) { + console_printf("Missing coc? (%d!=%d)\n", num, cnt); + return BLE_HS_EINVAL; + } + + return ble_l2cap_reconfig(chans, cnt, mtu); +} + +int +btshell_l2cap_send(uint16_t conn_handle, uint16_t idx, uint16_t bytes) +{ +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) == 0 + console_printf("BLE L2CAP LE COC not supported."); + console_printf(" Configure nimble host to enable it\n"); + return 0; +#else + + struct btshell_conn *conn; + struct btshell_l2cap_coc *coc; + struct os_mbuf *sdu_tx; + uint8_t b[] = {0x00, 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88, 0x99}; + int i; + int rc; + + console_printf("conn=%d, idx=%d, bytes=%d\n", conn_handle, idx, bytes); + + conn = btshell_conn_find(conn_handle); + if (conn == NULL) { + console_printf("conn=%d does not exist\n", conn_handle); + return 0; + } + + i = 0; + SLIST_FOREACH(coc, &conn->coc_list, next) { + if (i == idx) { + break; + } + i++; + } + if (coc == NULL) { + console_printf("Are you sure your channel exist?\n"); + return 0; + } + + if (coc->stalled) { + console_printf("Channel is stalled, wait ...\n"); + return 0; + } + + sdu_tx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0); + if (sdu_tx == NULL) { + console_printf("No memory in the test sdu pool\n"); + return 0; + } + + /* For the testing purpose we fill up buffer with known data, easy + * to validate on other side. In this loop we add as many full chunks as we + * can + */ + for (i = 0; i < bytes / sizeof(b); i++) { + rc = os_mbuf_append(sdu_tx, b, sizeof(b)); + if (rc) { + console_printf("Cannot append data %i !\n", i); + os_mbuf_free_chain(sdu_tx); + return rc; + } + } + + /* Here we add the rest < sizeof(b) */ + rc = os_mbuf_append(sdu_tx, b, bytes - (sizeof(b) * i)); + if (rc) { + console_printf("Cannot append data %i !\n", i); + os_mbuf_free_chain(sdu_tx); + return rc; + } + + rc = ble_l2cap_send(coc->chan, sdu_tx); + if (rc) { + if (rc == BLE_HS_ESTALLED) { + console_printf("CoC module is stalled with data. Wait for unstalled \n"); + coc->stalled = true; + } else { + console_printf("Could not send data rc=%d\n", rc); + } + os_mbuf_free_chain(sdu_tx); + } + + return rc; + +#endif +} + +static void +btshell_init_ext_adv_restart(void) +{ +#if MYNEWT_VAL(BLE_EXT_ADV) + int i; + + for (i = 0; i < BLE_ADV_INSTANCES; ++i) { + ext_adv_restart[i].conn_handle = BLE_HS_CONN_HANDLE_NONE; + } +#endif +} + +/** + * main + * + * The main task for the project. This function initializes the packages, + * then starts serving events from default event queue. + * + * @return int NOTE: this function should never return! + */ +int +main(int argc, char **argv) +{ + int rc; + +#ifdef ARCH_sim + mcu_sim_parse_args(argc, argv); +#endif + + /* Initialize OS */ + sysinit(); + + /* Initialize some application specific memory pools. */ + rc = os_mempool_init(&btshell_svc_pool, BTSHELL_MAX_SVCS, + sizeof (struct btshell_svc), btshell_svc_mem, + "btshell_svc_pool"); + assert(rc == 0); + + rc = os_mempool_init(&btshell_chr_pool, BTSHELL_MAX_CHRS, + sizeof (struct btshell_chr), btshell_chr_mem, + "btshell_chr_pool"); + assert(rc == 0); + + rc = os_mempool_init(&btshell_dsc_pool, BTSHELL_MAX_DSCS, + sizeof (struct btshell_dsc), btshell_dsc_mem, + "btshell_dsc_pool"); + assert(rc == 0); + +#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0 + /* For testing we want to support all the available channels */ + rc = os_mempool_init(&sdu_coc_mbuf_mempool, BTSHELL_COC_BUF_COUNT, + BTSHELL_COC_MTU, btshell_sdu_coc_mem, + "btshell_coc_sdu_pool"); + assert(rc == 0); + + rc = os_mbuf_pool_init(&sdu_os_mbuf_pool, &sdu_coc_mbuf_mempool, + BTSHELL_COC_MTU, BTSHELL_COC_BUF_COUNT); + assert(rc == 0); + + rc = os_mempool_init(&btshell_coc_conn_pool, + MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM), + sizeof (struct btshell_l2cap_coc), btshell_coc_conn_mem, + "btshell_coc_conn_pool"); + assert(rc == 0); +#endif + + /* Initialize the NimBLE host configuration. */ + ble_hs_cfg.reset_cb = btshell_on_reset; + ble_hs_cfg.sync_cb = btshell_on_sync; + ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; + ble_hs_cfg.store_status_cb = ble_store_util_status_rr; + + rc = gatt_svr_init(); + assert(rc == 0); + + cmd_init(); + + /* Set the default device name. */ + rc = ble_svc_gap_device_name_set("nimble-btshell"); + assert(rc == 0); + + /* Create a callout (timer). This callout is used by the "tx" btshell + * command to repeatedly send packets of sequential data bytes. + */ + os_callout_init(&btshell_tx_timer, os_eventq_dflt_get(), + btshell_tx_timer_cb, NULL); + + btshell_init_ext_adv_restart(); + + while (1) { + os_eventq_run(os_eventq_dflt_get()); + } + /* os start should never return. If it does, this should be an error */ + assert(0); + + return 0; +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/misc.c b/src/libs/mynewt-nimble/apps/btshell/src/misc.c new file mode 100644 index 00000000..e100eb79 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/misc.c @@ -0,0 +1,163 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include "console/console.h" +#include "host/ble_uuid.h" +#include "host/ble_gap.h" + +#include "btshell.h" + +/** + * Utility function to log an array of bytes. + */ +void +print_bytes(const uint8_t *bytes, int len) +{ + int i; + + for (i = 0; i < len; i++) { + console_printf("%s0x%02x", i != 0 ? ":" : "", bytes[i]); + } +} + +void +print_mbuf(const struct os_mbuf *om) +{ + int colon; + + colon = 0; + while (om != NULL) { + if (colon) { + console_printf(":"); + } else { + colon = 1; + } + print_bytes(om->om_data, om->om_len); + om = SLIST_NEXT(om, om_next); + } +} + +void +print_addr(const void *addr) +{ + const uint8_t *u8p; + + u8p = addr; + console_printf("%02x:%02x:%02x:%02x:%02x:%02x", + u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); +} + +void +print_uuid(const ble_uuid_t *uuid) +{ + char buf[BLE_UUID_STR_LEN]; + + ble_uuid_to_str(uuid, buf); + + console_printf("%s", buf); +} + +int +svc_is_empty(const struct btshell_svc *svc) +{ + return svc->svc.end_handle <= svc->svc.start_handle; +} + +uint16_t +chr_end_handle(const struct btshell_svc *svc, const struct btshell_chr *chr) +{ + const struct btshell_chr *next_chr; + + next_chr = SLIST_NEXT(chr, next); + if (next_chr != NULL) { + return next_chr->chr.def_handle - 1; + } else { + return svc->svc.end_handle; + } +} + +int +chr_is_empty(const struct btshell_svc *svc, const struct btshell_chr *chr) +{ + return chr_end_handle(svc, chr) <= chr->chr.val_handle; +} + +void +print_conn_desc(const struct ble_gap_conn_desc *desc) +{ + console_printf("handle=%d our_ota_addr_type=%d our_ota_addr=", + desc->conn_handle, desc->our_ota_addr.type); + print_addr(desc->our_ota_addr.val); + console_printf(" our_id_addr_type=%d our_id_addr=", + desc->our_id_addr.type); + print_addr(desc->our_id_addr.val); + console_printf(" peer_ota_addr_type=%d peer_ota_addr=", + desc->peer_ota_addr.type); + print_addr(desc->peer_ota_addr.val); + console_printf(" peer_id_addr_type=%d peer_id_addr=", + desc->peer_id_addr.type); + print_addr(desc->peer_id_addr.val); + console_printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d" + " key_size=%d encrypted=%d authenticated=%d bonded=%d\n", + desc->conn_itvl, desc->conn_latency, + desc->supervision_timeout, + desc->sec_state.key_size, + desc->sec_state.encrypted, + desc->sec_state.authenticated, + desc->sec_state.bonded); +} + +static void +print_dsc(struct btshell_dsc *dsc) +{ + console_printf(" dsc_handle=%d uuid=", dsc->dsc.handle); + print_uuid(&dsc->dsc.uuid.u); + console_printf("\n"); +} + +static void +print_chr(struct btshell_chr *chr) +{ + struct btshell_dsc *dsc; + + console_printf(" def_handle=%d val_handle=%d properties=0x%02x " + "uuid=", chr->chr.def_handle, chr->chr.val_handle, + chr->chr.properties); + print_uuid(&chr->chr.uuid.u); + console_printf("\n"); + + SLIST_FOREACH(dsc, &chr->dscs, next) { + print_dsc(dsc); + } +} + +void +print_svc(struct btshell_svc *svc) +{ + struct btshell_chr *chr; + + console_printf(" start=%d end=%d uuid=", svc->svc.start_handle, + svc->svc.end_handle); + print_uuid(&svc->svc.uuid.u); + console_printf("\n"); + + SLIST_FOREACH(chr, &svc->chrs, next) { + print_chr(chr); + } +} diff --git a/src/libs/mynewt-nimble/apps/btshell/src/parse.c b/src/libs/mynewt-nimble/apps/btshell/src/parse.c new file mode 100644 index 00000000..d8018c5c --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/src/parse.c @@ -0,0 +1,734 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <stdarg.h> +#include <errno.h> +#include <assert.h> +#include <ctype.h> +#include "console/console.h" +#include "host/ble_hs.h" +#include "host/ble_uuid.h" +#include "host/ble_eddystone.h" +#include "cmd.h" +#include "btshell.h" + +#define CMD_MAX_ARGS 16 + +static char *cmd_args[CMD_MAX_ARGS][2]; +static int cmd_num_args; + +int +parse_arg_find_idx(const char *key) +{ + int i; + + for (i = 0; i < cmd_num_args; i++) { + if (strcmp(cmd_args[i][0], key) == 0) { + return i; + } + } + + return -1; +} + +char * +parse_arg_peek(const char *key) +{ + int i; + + for (i = 0; i < cmd_num_args; i++) { + if (strcmp(cmd_args[i][0], key) == 0) { + return cmd_args[i][1]; + } + } + + return NULL; +} + +char * +parse_arg_extract(const char *key) +{ + int i; + + for (i = 0; i < cmd_num_args; i++) { + if (strcmp(cmd_args[i][0], key) == 0) { + /* Erase parameter. */ + cmd_args[i][0][0] = '\0'; + + return cmd_args[i][1]; + } + } + + return NULL; +} + +/** + * Determines which number base to use when parsing the specified numeric + * string. This just avoids base '0' so that numbers don't get interpreted as + * octal. + */ +static int +parse_arg_long_base(char *sval) +{ + if (sval[0] == '0' && sval[1] == 'x') { + return 0; + } else { + return 10; + } +} + +long +parse_long_bounds(char *sval, long min, long max, int *out_status) +{ + char *endptr; + long lval; + + lval = strtol(sval, &endptr, parse_arg_long_base(sval)); + if (sval[0] != '\0' && *endptr == '\0' && + lval >= min && lval <= max) { + + *out_status = 0; + return lval; + } + + *out_status = EINVAL; + return 0; +} + +long +parse_arg_long_bounds_peek(char *name, long min, long max, int *out_status) +{ + char *sval; + + sval = parse_arg_peek(name); + if (sval == NULL) { + *out_status = ENOENT; + return 0; + } + return parse_long_bounds(sval, min, max, out_status); +} + +long +parse_arg_long_bounds(char *name, long min, long max, int *out_status) +{ + char *sval; + + sval = parse_arg_extract(name); + if (sval == NULL) { + *out_status = ENOENT; + return 0; + } + return parse_long_bounds(sval, min, max, out_status); +} + +long +parse_arg_long_bounds_dflt(char *name, long min, long max, + long dflt, int *out_status) +{ + long val; + int rc; + + val = parse_arg_long_bounds(name, min, max, &rc); + if (rc == ENOENT) { + rc = 0; + val = dflt; + } + + *out_status = rc; + + return val; +} + +uint64_t +parse_arg_uint64_bounds(char *name, uint64_t min, uint64_t max, int *out_status) +{ + char *endptr; + char *sval; + uint64_t lval; + + sval = parse_arg_extract(name); + if (sval == NULL) { + *out_status = ENOENT; + return 0; + } + + lval = strtoull(sval, &endptr, parse_arg_long_base(sval)); + if (sval[0] != '\0' && *endptr == '\0' && + lval >= min && lval <= max) { + + *out_status = 0; + return lval; + } + + *out_status = EINVAL; + return 0; +} + +long +parse_arg_long(char *name, int *out_status) +{ + return parse_arg_long_bounds(name, LONG_MIN, LONG_MAX, out_status); +} + +uint8_t +parse_arg_bool(char *name, int *out_status) +{ + return parse_arg_long_bounds(name, 0, 1, out_status); +} + +uint8_t +parse_arg_bool_dflt(char *name, uint8_t dflt, int *out_status) +{ + return parse_arg_long_bounds_dflt(name, 0, 1, dflt, out_status); +} + +uint8_t +parse_arg_uint8(char *name, int *out_status) +{ + return parse_arg_long_bounds(name, 0, UINT8_MAX, out_status); +} + +uint16_t +parse_arg_uint16(char *name, int *out_status) +{ + return parse_arg_long_bounds(name, 0, UINT16_MAX, out_status); +} + +uint16_t +parse_arg_uint16_peek(char *name, int *out_status) +{ + return parse_arg_long_bounds_peek(name, 0, UINT16_MAX, out_status); +} + +uint32_t +parse_arg_uint32(char *name, int *out_status) +{ + return parse_arg_uint64_bounds(name, 0, UINT32_MAX, out_status); +} + +uint64_t +parse_arg_uint64(char *name, int *out_status) +{ + return parse_arg_uint64_bounds(name, 0, UINT64_MAX, out_status); +} + +uint8_t +parse_arg_uint8_dflt(char *name, uint8_t dflt, int *out_status) +{ + uint8_t val; + int rc; + + val = parse_arg_uint8(name, &rc); + if (rc == ENOENT) { + val = dflt; + rc = 0; + } + + *out_status = rc; + return val; +} + +uint16_t +parse_arg_uint16_dflt(char *name, uint16_t dflt, int *out_status) +{ + uint16_t val; + int rc; + + val = parse_arg_uint16(name, &rc); + if (rc == ENOENT) { + val = dflt; + rc = 0; + } + + *out_status = rc; + return val; +} + +uint32_t +parse_arg_uint32_dflt(char *name, uint32_t dflt, int *out_status) +{ + uint32_t val; + int rc; + + val = parse_arg_uint32(name, &rc); + if (rc == ENOENT) { + val = dflt; + rc = 0; + } + + *out_status = rc; + return val; +} + +static uint32_t +parse_time_unit_mult(const char *str) +{ + if (!strcasecmp(str, "us")) { + return 1; + } else if (!strcasecmp(str, "ms")) { + return 1000; + } else if (!strcasecmp(str, "s")) { + return 1000000; + } + + return 0; +} + +static uint32_t +parse_time_us(const char *str, int *out_status) +{ + uint32_t val = 0; + uint32_t val_div = 1; + uint32_t val_mult = 1; + uint32_t val_us; + + while (isdigit(*str)) { + val *= 10; + val += *str - '0'; + str++; + } + + if (*str == '.') { + str++; + while (isdigit(*str)) { + val *= 10; + val += *str - '0'; + val_div *= 10; + str++; + } + } + + val_mult = parse_time_unit_mult(str); + if (val_mult == 0) { + *out_status = EINVAL; + return 0; + } + + if (val_mult > val_div) { + val_us = val * (val_mult / val_div); + } else { + val_us = val * (val_div / val_mult); + } + + *out_status = 0; + + return val_us; +} + +uint32_t +parse_arg_time_dflt(char *name, int step_us, uint32_t dflt, int *out_status) +{ + const char *arg; + uint32_t val; + int rc; + + arg = parse_arg_peek(name); + if (!arg) { + *out_status = 0; + return dflt; + } + + val = parse_time_us(arg, &rc); + if (rc) { + val = parse_arg_uint32(name, &rc); + if (rc == ENOENT) { + *out_status = 0; + return dflt; + } + } else { + val /= step_us; + parse_arg_extract(name); + } + + *out_status = rc; + return val; +} + +const struct kv_pair * +parse_kv_find(const struct kv_pair *kvs, char *name) +{ + const struct kv_pair *kv; + int i; + + for (i = 0; kvs[i].key != NULL; i++) { + kv = kvs + i; + if (strcmp(name, kv->key) == 0) { + return kv; + } + } + + return NULL; +} + +int +parse_arg_kv(char *name, const struct kv_pair *kvs, int *out_status) +{ + const struct kv_pair *kv; + char *sval; + + sval = parse_arg_extract(name); + if (sval == NULL) { + *out_status = ENOENT; + return -1; + } + + kv = parse_kv_find(kvs, sval); + if (kv == NULL) { + *out_status = EINVAL; + return -1; + } + + *out_status = 0; + return kv->val; +} + +int +parse_arg_kv_dflt(char *name, const struct kv_pair *kvs, int def_val, + int *out_status) +{ + int val; + int rc; + + val = parse_arg_kv(name, kvs, &rc); + if (rc == ENOENT) { + rc = 0; + val = def_val; + } + + *out_status = rc; + + return val; +} + + +static int +parse_arg_byte_stream_delim(char *sval, char *delims, int max_len, + uint8_t *dst, int *out_len) +{ + unsigned long ul; + char *endptr; + char *token; + int i; + + i = 0; + for (token = strtok(sval, delims); + token != NULL; + token = strtok(NULL, delims)) { + + if (i >= max_len) { + return EINVAL; + } + + ul = strtoul(token, &endptr, 16); + if (sval[0] == '\0' || *endptr != '\0' || ul > UINT8_MAX) { + return -1; + } + + dst[i] = ul; + i++; + } + + *out_len = i; + + return 0; +} + +int +parse_arg_byte_stream(char *name, int max_len, uint8_t *dst, int *out_len) +{ + char *sval; + + sval = parse_arg_extract(name); + if (sval == NULL) { + return ENOENT; + } + + return parse_arg_byte_stream_delim(sval, ":-", max_len, dst, out_len); +} + +int +parse_arg_uint8_list_with_separator(char *name, char *separator, int max_len, + uint8_t *dst, int *out_len) +{ + char *sval; + + sval = parse_arg_extract(name); + if (sval == NULL) { + return ENOENT; + } + + return parse_arg_byte_stream_delim(sval, separator, max_len, dst, out_len); +} + +int +parse_arg_byte_stream_exact_length(char *name, uint8_t *dst, int len) +{ + int actual_len; + int rc; + + rc = parse_arg_byte_stream(name, len, dst, &actual_len); + if (rc != 0) { + return rc; + } + + if (actual_len != len) { + return EINVAL; + } + + return 0; +} + +static void +parse_reverse_bytes(uint8_t *bytes, int len) +{ + uint8_t tmp; + int i; + + for (i = 0; i < len / 2; i++) { + tmp = bytes[i]; + bytes[i] = bytes[len - i - 1]; + bytes[len - i - 1] = tmp; + } +} + +int +parse_arg_mac(char *name, uint8_t *dst) +{ + int rc; + + rc = parse_arg_byte_stream_exact_length(name, dst, 6); + if (rc != 0) { + return rc; + } + + parse_reverse_bytes(dst, 6); + + return 0; +} + +int +parse_arg_addr(char *name, ble_addr_t *addr) +{ + char *arg; + size_t len; + uint8_t addr_type; + bool addr_type_found; + int rc; + + arg = parse_arg_peek(name); + if (!arg) { + return ENOENT; + } + + len = strlen(arg); + if (len < 2) { + return EINVAL; + } + + addr_type_found = false; + if ((arg[len - 2] == ':') || (arg[len - 2] == '-')) { + if (tolower(arg[len - 1]) == 'p') { + addr_type = BLE_ADDR_PUBLIC; + addr_type_found = true; + } else if (tolower(arg[len - 1]) == 'r') { + addr_type = BLE_ADDR_RANDOM; + addr_type_found = true; + } + + if (addr_type_found) { + arg[len - 2] = '\0'; + } +} + + rc = parse_arg_mac(name, addr->val); + if (rc != 0) { + return rc; + } + + if (addr_type_found) { + addr->type = addr_type; + } else { + rc = EAGAIN; + } + + return rc; +} + +int +parse_arg_uuid(char *str, ble_uuid_any_t *uuid) +{ + uint16_t uuid16; + uint8_t val[16]; + int len; + int rc; + + uuid16 = parse_arg_uint16_peek(str, &rc); + switch (rc) { + case ENOENT: + parse_arg_extract(str); + return ENOENT; + + case 0: + len = 2; + val[0] = uuid16; + val[1] = uuid16 >> 8; + parse_arg_extract(str); + break; + + default: + len = 16; + rc = parse_arg_byte_stream_exact_length(str, val, 16); + if (rc != 0) { + return EINVAL; + } + parse_reverse_bytes(val, 16); + break; + } + + rc = ble_uuid_init_from_buf(uuid, val, len); + if (rc != 0) { + return EINVAL; + } else { + return 0; + } +} + +int +parse_arg_all(int argc, char **argv) +{ + char *key; + char *val; + int i; + + cmd_num_args = 0; + + for (i = 0; i < argc; i++) { + key = strtok(argv[i], "="); + val = strtok(NULL, "="); + + if (key != NULL && val != NULL) { + if (strlen(key) == 0) { + console_printf("Error: invalid argument: %s\n", argv[i]); + return -1; + } + + if (cmd_num_args >= CMD_MAX_ARGS) { + console_printf("Error: too many arguments"); + return -1; + } + + cmd_args[cmd_num_args][0] = key; + cmd_args[cmd_num_args][1] = val; + cmd_num_args++; + } + } + + return 0; +} + +int +parse_eddystone_url(char *full_url, uint8_t *out_scheme, char *out_body, + uint8_t *out_body_len, uint8_t *out_suffix) +{ + static const struct { + char *s; + uint8_t scheme; + } schemes[] = { + { "http://www.", BLE_EDDYSTONE_URL_SCHEME_HTTP_WWW }, + { "https://www.", BLE_EDDYSTONE_URL_SCHEME_HTTPS_WWW }, + { "http://", BLE_EDDYSTONE_URL_SCHEME_HTTP }, + { "https://", BLE_EDDYSTONE_URL_SCHEME_HTTPS }, + }; + + static const struct { + char *s; + uint8_t code; + } suffixes[] = { + { ".com/", BLE_EDDYSTONE_URL_SUFFIX_COM_SLASH }, + { ".org/", BLE_EDDYSTONE_URL_SUFFIX_ORG_SLASH }, + { ".edu/", BLE_EDDYSTONE_URL_SUFFIX_EDU_SLASH }, + { ".net/", BLE_EDDYSTONE_URL_SUFFIX_NET_SLASH }, + { ".info/", BLE_EDDYSTONE_URL_SUFFIX_INFO_SLASH }, + { ".biz/", BLE_EDDYSTONE_URL_SUFFIX_BIZ_SLASH }, + { ".gov/", BLE_EDDYSTONE_URL_SUFFIX_GOV_SLASH }, + { ".com", BLE_EDDYSTONE_URL_SUFFIX_COM }, + { ".org", BLE_EDDYSTONE_URL_SUFFIX_ORG }, + { ".edu", BLE_EDDYSTONE_URL_SUFFIX_EDU }, + { ".net", BLE_EDDYSTONE_URL_SUFFIX_NET }, + { ".info", BLE_EDDYSTONE_URL_SUFFIX_INFO }, + { ".biz", BLE_EDDYSTONE_URL_SUFFIX_BIZ }, + { ".gov", BLE_EDDYSTONE_URL_SUFFIX_GOV }, + }; + + char *prefix; + char *suffix; + int full_url_len; + int prefix_len; + int suffix_len; + int suffix_idx; + int rc; + int i; + + full_url_len = strlen(full_url); + + rc = BLE_HS_EINVAL; + for (i = 0; i < sizeof schemes / sizeof schemes[0]; i++) { + prefix = schemes[i].s; + prefix_len = strlen(schemes[i].s); + + if (full_url_len >= prefix_len && + memcmp(full_url, prefix, prefix_len) == 0) { + + *out_scheme = i; + rc = 0; + break; + } + } + if (rc != 0) { + return rc; + } + + rc = BLE_HS_EINVAL; + for (i = 0; i < sizeof suffixes / sizeof suffixes[0]; i++) { + suffix = suffixes[i].s; + suffix_len = strlen(suffixes[i].s); + + suffix_idx = full_url_len - suffix_len; + if (suffix_idx >= prefix_len && + memcmp(full_url + suffix_idx, suffix, suffix_len) == 0) { + + *out_suffix = i; + rc = 0; + break; + } + } + if (rc != 0) { + *out_suffix = BLE_EDDYSTONE_URL_SUFFIX_NONE; + *out_body_len = full_url_len - prefix_len; + } else { + *out_body_len = full_url_len - prefix_len - suffix_len; + } + + memcpy(out_body, full_url + prefix_len, *out_body_len); + + return 0; +} diff --git a/src/libs/mynewt-nimble/apps/btshell/syscfg.yml b/src/libs/mynewt-nimble/apps/btshell/syscfg.yml new file mode 100644 index 00000000..9ebf9d89 --- /dev/null +++ b/src/libs/mynewt-nimble/apps/btshell/syscfg.yml @@ -0,0 +1,42 @@ +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# + +syscfg.defs: + BTSHELL_ANS: + description: Include support for the alert notification service. + value: 1 + +syscfg.vals: + # Enable the shell task. + SHELL_TASK: 1 + + # Set log level to info (disable debug logging). + LOG_LEVEL: 1 + + # Disable security manager (pairing and bonding). + BLE_SM_LEGACY: 0 + BLE_SM_SC: 0 + + # Default task settings + OS_MAIN_STACK_SIZE: 512 + + # SMP is not supported in this app, so disable smp-over-shell. + SHELL_MGMT: 0 + +syscfg.vals.BLE_MESH: + MSYS_1_BLOCK_COUNT: 16 |