You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/02/12 04:49:36 UTC
[1/2] incubator-mynewt-larva git commit: Remove bleshell;
bletiny renders it obsolete
Repository: incubator-mynewt-larva
Updated Branches:
refs/heads/master 4adc88c7b -> 2e19f75dc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/parse.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/parse.c b/project/bleshell/src/parse.c
deleted file mode 100644
index 6930be2..0000000
--- a/project/bleshell/src/parse.c
+++ /dev/null
@@ -1,447 +0,0 @@
-/**
- * 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 "console/console.h"
-#include "host/ble_uuid.h"
-#include "bleshell_priv.h"
-
-#define CMD_MAX_ARGS 16
-
-static char *cmd_args[CMD_MAX_ARGS][2];
-static int cmd_num_args;
-
-void
-bleshell_printf(const char *fmt, ...)
-{
- va_list args;
-
- if (!console_is_midline) {
- console_printf("(BLESHELL) ");
- }
-
- va_start(args, fmt);
- console_vprintf(fmt, args);
- va_end(args);
-}
-
-void
-print_addr(void *addr)
-{
- uint8_t *u8p;
-
- u8p = addr;
- bleshell_printf("%02x:%02x:%02x:%02x:%02x:%02x",
- u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
-}
-
-void
-print_uuid(void *uuid128)
-{
- uint16_t uuid16;
- uint8_t *u8p;
-
- uuid16 = ble_uuid_128_to_16(uuid128);
- if (uuid16 != 0) {
- bleshell_printf("0x%04x", uuid16);
- return;
- }
-
- u8p = uuid128;
-
- /* 00001101-0000-1000-8000-00805f9b34fb */
- bleshell_printf("%02x%02x%02x%02x-", u8p[15], u8p[14], u8p[13], u8p[12]);
- bleshell_printf("%02x%02x-%02x%02x-", u8p[11], u8p[10], u8p[9], u8p[8]);
- bleshell_printf("%02x%02x%02x%02x%02x%02x%02x%02x",
- u8p[7], u8p[6], u8p[5], u8p[4],
- u8p[3], u8p[2], u8p[1], u8p[0]);
-}
-
-int
-parse_err_too_few_args(char *cmd_name)
-{
- bleshell_printf("Error: too few arguments for command \"%s\"\n", cmd_name);
- return -1;
-}
-
-const struct cmd_entry *
-parse_cmd_find(const struct cmd_entry *cmds, char *name)
-{
- const struct cmd_entry *cmd;
- int i;
-
- for (i = 0; cmds[i].name != NULL; i++) {
- cmd = cmds + i;
- if (strcmp(name, cmd->name) == 0) {
- return cmd;
- }
- }
-
- return NULL;
-}
-
-struct kv_pair *
-parse_kv_find(struct kv_pair *kvs, char *name)
-{
- 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;
-}
-
-char *
-parse_arg_find(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;
-}
-
-long
-parse_arg_long_bounds(char *name, long min, long max, int *out_status)
-{
- char *endptr;
- char *sval;
- long lval;
-
- sval = parse_arg_find(name);
- if (sval == NULL) {
- *out_status = ENOENT;
- return 0;
- }
-
- lval = strtol(sval, &endptr, 0);
- 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);
-}
-
-uint16_t
-parse_arg_uint16(char *name, int *out_status)
-{
- return parse_arg_long_bounds(name, 0, UINT16_MAX, out_status);
-}
-
-uint32_t
-parse_arg_uint32(char *name, int *out_status)
-{
- return parse_arg_long_bounds(name, 0, UINT32_MAX, out_status);
-}
-
-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;
-}
-
-int
-parse_arg_kv(char *name, struct kv_pair *kvs)
-{
- struct kv_pair *kv;
- char *sval;
-
- sval = parse_arg_find(name);
- if (sval == NULL) {
- return ENOENT;
- }
-
- kv = parse_kv_find(kvs, sval);
- if (kv == NULL) {
- return EINVAL;
- }
-
- return kv->val;
-}
-
-static int
-parse_arg_byte_stream_no_delim(char *sval, int max_len, uint8_t *dst,
- int *out_len)
-{
- unsigned long ul;
- char *endptr;
- char buf[3];
- int i;
-
- buf[2] = '\0';
- i = 0;
- while (1) {
- if (sval[i * 2] == '\0') {
- *out_len = i;
- return 0;
- }
-
- if (i >= max_len) {
- return EINVAL;
- }
-
- buf[0] = sval[i * 2 + 0];
- buf[1] = sval[i * 2 + 1];
-
- ul = strtoul(buf, &endptr, 16);
- if (*sval == '\0' || *endptr != '\0') {
- return EINVAL;
- }
-
- assert(ul <= UINT8_MAX);
- dst[i] = ul;
-
- i++;
- }
-}
-
-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)
-{
- int total_len;
- char *sval;
-
- sval = parse_arg_find(name);
- if (sval == NULL) {
- return ENOENT;
- }
-
- total_len = strlen(sval);
- if (strcspn(sval, ":-") == total_len) {
- return parse_arg_byte_stream_no_delim(sval, max_len, dst, out_len);
- } else {
- return parse_arg_byte_stream_delim(sval, ":-", 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, 6, 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_uuid(char *str, uint8_t *dst_uuid128)
-{
- uint16_t uuid16;
- char *tok;
- int rc;
-
- uuid16 = parse_arg_uint16(str, &rc);
- switch (rc) {
- case ENOENT:
- return ENOENT;
-
- case 0:
- rc = ble_uuid_16_to_128(uuid16, dst_uuid128);
- if (rc != 0) {
- return EINVAL;
- } else {
- return 0;
- }
-
- default:
- /* e7add801-b042-4876-aae1112855353cc1 */
- if (strlen(str) == 35) {
- tok = strtok(str, "-");
- if (tok == NULL) {
- return EINVAL;
- }
- rc = parse_arg_byte_stream_exact_length(tok, dst_uuid128 + 0, 4);
- if (rc != 0) {
- return rc;
- }
-
- tok = strtok(NULL, "-");
- if (tok == NULL) {
- return EINVAL;
- }
- rc = parse_arg_byte_stream_exact_length(tok, dst_uuid128 + 4, 2);
- if (rc != 0) {
- return rc;
- }
-
- tok = strtok(NULL, "-");
- if (tok == NULL) {
- return EINVAL;
- }
- rc = parse_arg_byte_stream_exact_length(tok, dst_uuid128 + 6, 2);
- if (rc != 0) {
- return rc;
- }
-
- tok = strtok(NULL, "-");
- if (tok == NULL) {
- return EINVAL;
- }
- rc = parse_arg_byte_stream_exact_length(tok, dst_uuid128 + 8, 8);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
- }
-
- rc = parse_arg_byte_stream_exact_length(str, dst_uuid128, 16);
- return rc;
- }
-}
-
-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) {
- bleshell_printf("Error: invalid argument: %s\n", argv[i]);
- return -1;
- }
-
- if (cmd_num_args >= CMD_MAX_ARGS) {
- bleshell_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;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/periph.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/periph.c b/project/bleshell/src/periph.c
deleted file mode 100644
index db08255..0000000
--- a/project/bleshell/src/periph.c
+++ /dev/null
@@ -1,365 +0,0 @@
-/**
- * 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 "bleshell_priv.h"
-
-#define PERIPH_SVC_NEWTMGR_UUID 0x7713
-#define PERIPH_CHR_NM_REQRSP_UUID 0x5562
-
-#define CHR_F_FULL_ACCESS (BLE_GATT_CHR_F_READ | \
- BLE_GATT_CHR_F_WRITE_NO_RSP | \
- BLE_GATT_CHR_F_WRITE | \
- BLE_GATT_CHR_F_NOTIFY | \
- BLE_GATT_CHR_F_INDICATE)
-
-#define PERIPH_SVC_ALERT_UUID 0x1811
-#define PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47
-#define PERIPH_CHR_NEW_ALERT 0x2A46
-#define PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48
-#define PERIPH_CHR_UNR_ALERT_STAT_UUID 0x2A45
-#define PERIPH_CHR_ALERT_NOT_CTRL_PT 0x2A44
-
-static int
-periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg);
-static int
-periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg);
-static int
-periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg);
-
-static const struct ble_gatt_svc_def periph_svcs[] = {
- [0] = {
- /*** Service: GAP. */
- .type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid128 = BLE_UUID16(BLE_GAP_SVC_UUID16),
- .characteristics = (struct ble_gatt_chr_def[]) { {
- /*** Characteristic: Device Name. */
- .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_DEVICE_NAME),
- .access_cb = periph_chr_access_gap,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- /*** Characteristic: Appearance. */
- .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_APPEARANCE),
- .access_cb = periph_chr_access_gap,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- /*** Characteristic: Peripheral Privacy Flag. */
- .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG),
- .access_cb = periph_chr_access_gap,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- /*** Characteristic: Reconnection Address. */
- .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_RECONNECT_ADDR),
- .access_cb = periph_chr_access_gap,
- .flags = BLE_GATT_CHR_F_WRITE,
- }, {
- /*** Characteristic: Peripheral Preferred Connection Parameters. */
- .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS),
- .access_cb = periph_chr_access_gap,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- 0, /* No more characteristics in this service. */
- } },
- },
-
- [1] = {
- /*** Service: GATT */
- .type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid128 = BLE_UUID16(BLE_GATT_SVC_UUID16),
- .characteristics = (struct ble_gatt_chr_def[]) { {
- .uuid128 = BLE_UUID16(BLE_GATT_CHR_SERVICE_CHANGED_UUID16),
- .access_cb = periph_chr_access_gatt,
- .flags = BLE_GATT_CHR_F_INDICATE,
- }, {
- 0, /* No more characteristics in this service. */
- } },
- },
-
- [2] = {
- /*** Alert Notification Service. */
- .type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid128 = BLE_UUID16(PERIPH_SVC_ALERT_UUID),
- .characteristics = (struct ble_gatt_chr_def[]) { {
- .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID),
- .access_cb = periph_chr_access_alert,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- .uuid128 = BLE_UUID16(PERIPH_CHR_NEW_ALERT),
- .access_cb = periph_chr_access_alert,
- .flags = BLE_GATT_CHR_F_NOTIFY,
- }, {
- .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID),
- .access_cb = periph_chr_access_alert,
- .flags = BLE_GATT_CHR_F_READ,
- }, {
- .uuid128 = BLE_UUID16(PERIPH_CHR_UNR_ALERT_STAT_UUID),
- .access_cb = periph_chr_access_alert,
- .flags = BLE_GATT_CHR_F_NOTIFY,
- }, {
- .uuid128 = BLE_UUID16(PERIPH_CHR_ALERT_NOT_CTRL_PT),
- .access_cb = periph_chr_access_alert,
- .flags = BLE_GATT_CHR_F_WRITE,
- }, {
- 0, /* No more characteristics in this service. */
- } },
- },
-
- [3] = {
- /*** Newtmgr Service. */
- .type = BLE_GATT_SVC_TYPE_PRIMARY,
- .uuid128 = BLE_UUID16(PERIPH_SVC_NEWTMGR_UUID),
- .characteristics = (struct ble_gatt_chr_def[]) { {
- .uuid128 = BLE_UUID16(PERIPH_CHR_NM_REQRSP_UUID),
- .access_cb = nm_chr_access,
- .arg = &nm_ble_transport,
- .flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
- }, {
- 0, /* No more characteristics in this service. */
- } },
- },
-
- {
- 0, /* No more services. */
- },
-};
-
-static int
-periph_chr_write(uint8_t op, union ble_gatt_access_ctxt *ctxt,
- uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len)
-{
- assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
- if (ctxt->chr_access.len < min_len ||
- ctxt->chr_access.len > max_len) {
- return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
- }
- memcpy(dst, ctxt->chr_access.data, ctxt->chr_access.len);
- if (len != NULL) {
- *len = ctxt->chr_access.len;
- }
-
- return 0;
-}
-
-static int
-periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg)
-{
- uint16_t uuid16;
-
- uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
- assert(uuid16 != 0);
-
- switch (uuid16) {
- case BLE_GAP_CHR_UUID16_DEVICE_NAME:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)bleshell_device_name;
- ctxt->chr_access.len = strlen(bleshell_device_name);
- break;
-
- case BLE_GAP_CHR_UUID16_APPEARANCE:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)&bleshell_appearance;
- ctxt->chr_access.len = sizeof bleshell_appearance;
- break;
-
- case BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)&bleshell_privacy_flag;
- ctxt->chr_access.len = sizeof bleshell_privacy_flag;
- break;
-
- case BLE_GAP_CHR_UUID16_RECONNECT_ADDR:
- assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
- if (ctxt->chr_access.len != sizeof bleshell_reconnect_addr) {
- return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
- }
- memcpy(bleshell_reconnect_addr, ctxt->chr_access.data,
- sizeof bleshell_reconnect_addr);
- break;
-
- case BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)&bleshell_pref_conn_params;
- ctxt->chr_access.len = sizeof bleshell_pref_conn_params;
- break;
-
- default:
- assert(0);
- break;
- }
-
- return 0;
-}
-
-static int
-periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg)
-{
- uint16_t uuid16;
-
- uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
- assert(uuid16 != 0);
-
- switch (uuid16) {
- case BLE_GATT_CHR_SERVICE_CHANGED_UUID16:
- if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
- if (ctxt->chr_access.len != sizeof bleshell_gatt_service_changed) {
- return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
- }
- memcpy(bleshell_gatt_service_changed, ctxt->chr_access.data,
- sizeof bleshell_gatt_service_changed);
- } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
- ctxt->chr_access.data = (void *)&bleshell_gatt_service_changed;
- ctxt->chr_access.len = sizeof bleshell_gatt_service_changed;
- }
- break;
-
- default:
- assert(0);
- break;
- }
-
- return 0;
-}
-
-#define PERIPH_NEW_ALERT_VAL_MAX_LEN 64
-
-static const uint8_t periph_new_alert_cat = 0x01; /* Simple alert. */
-static uint8_t periph_new_alert_val[PERIPH_NEW_ALERT_VAL_MAX_LEN];
-static uint16_t periph_new_alert_val_len;
-static const uint8_t periph_unr_alert_cat = 0x01; /* Simple alert. */
-static uint16_t periph_unr_alert_stat;
-static uint16_t periph_alert_not_ctrl_pt;
-
-static int
-periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
- union ble_gatt_access_ctxt *ctxt, void *arg)
-{
- uint16_t uuid16;
- int rc;
-
- uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
- assert(uuid16 != 0);
-
- switch (uuid16) {
- case PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)&periph_new_alert_cat;
- ctxt->chr_access.len = sizeof periph_new_alert_cat;
- return 0;
-
- case PERIPH_CHR_NEW_ALERT:
- if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
- rc = periph_chr_write(op, ctxt, 0, sizeof periph_new_alert_val,
- periph_new_alert_val,
- &periph_new_alert_val_len);
- return rc;
- } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
- ctxt->chr_access.data = (void *)&periph_new_alert_val;
- ctxt->chr_access.len = sizeof periph_new_alert_val;
- return 0;
- }
-
- case PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID:
- assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
- ctxt->chr_access.data = (void *)&periph_unr_alert_cat;
- ctxt->chr_access.len = sizeof periph_unr_alert_cat;
- return 0;
-
- case PERIPH_CHR_UNR_ALERT_STAT_UUID:
- if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
- rc = periph_chr_write(op, ctxt, 2, 2, &periph_unr_alert_stat, NULL);
- } else {
- ctxt->chr_access.data = (void *)&periph_unr_alert_stat;
- ctxt->chr_access.len = sizeof periph_unr_alert_stat;
- rc = 0;
- }
- return rc;
-
- case PERIPH_CHR_ALERT_NOT_CTRL_PT:
- if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
- rc = periph_chr_write(op, ctxt, 2, 2, &periph_alert_not_ctrl_pt,
- NULL);
- } else {
- rc = BLE_ATT_ERR_UNLIKELY;
- }
- return rc;
-
- default:
- assert(0);
- return BLE_ATT_ERR_UNLIKELY;
- }
-}
-
-static void
-periph_register_cb(uint8_t op, union ble_gatt_register_ctxt *ctxt, void *arg)
-{
- uint16_t uuid16;
-
- switch (op) {
- case BLE_GATT_REGISTER_OP_SVC:
- uuid16 = ble_uuid_128_to_16(ctxt->svc_reg.svc->uuid128);
- assert(uuid16 != 0);
- bleshell_printf("registered service 0x%04x with handle=%d\n",
- uuid16, ctxt->svc_reg.handle);
- break;
-
- case BLE_GATT_REGISTER_OP_CHR:
- uuid16 = ble_uuid_128_to_16(ctxt->chr_reg.chr->uuid128);
- assert(uuid16 != 0);
- bleshell_printf("registering characteristic 0x%04x with def_handle=%d "
- "val_handle=%d\n",
- uuid16, ctxt->chr_reg.def_handle,
- ctxt->chr_reg.val_handle);
-
- if (uuid16 == PERIPH_CHR_NM_REQRSP_UUID) {
- nm_attr_val_handle = ctxt->chr_reg.val_handle;
- }
- break;
-
- case BLE_GATT_REGISTER_OP_DSC:
- uuid16 = ble_uuid_128_to_16(ctxt->dsc_reg.dsc->uuid128);
- assert(uuid16 != 0);
- bleshell_printf("registering descriptor 0x%04x with handle=%d "
- "chr_handle=%d\n",
- uuid16, ctxt->dsc_reg.dsc_handle,
- ctxt->dsc_reg.chr_def_handle);
- break;
-
- default:
- assert(0);
- break;
- }
-}
-
-void
-periph_init(void)
-{
- int rc;
-
- rc = ble_gatts_register_svcs(periph_svcs, periph_register_cb, NULL);
- assert(rc == 0);
-}
[2/2] incubator-mynewt-larva git commit: Remove bleshell;
bletiny renders it obsolete
Posted by cc...@apache.org.
Remove bleshell; bletiny renders it obsolete
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/2e19f75d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/2e19f75d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/2e19f75d
Branch: refs/heads/master
Commit: 2e19f75dc834282f06e15eea053de38f075bde42
Parents: 4adc88c
Author: Christopher Collins <cc...@gmail.com>
Authored: Thu Feb 11 19:49:06 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Thu Feb 11 19:49:06 2016 -0800
----------------------------------------------------------------------
project/bleshell/bleshell.yml | 28 -
project/bleshell/src/bleshell_priv.h | 164 ----
project/bleshell/src/cmd.c | 1455 -----------------------------
project/bleshell/src/main.c | 1356 ---------------------------
project/bleshell/src/nm.c | 139 ---
project/bleshell/src/parse.c | 447 ---------
project/bleshell/src/periph.c | 365 --------
7 files changed, 3954 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/bleshell.yml
----------------------------------------------------------------------
diff --git a/project/bleshell/bleshell.yml b/project/bleshell/bleshell.yml
deleted file mode 100644
index 6950ab5..0000000
--- a/project/bleshell/bleshell.yml
+++ /dev/null
@@ -1,28 +0,0 @@
-#
-# 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.
-#
-
-project.name: bleshell
-project.pkgs:
- - libs/os
- - net/nimble/controller
- - net/nimble/host
- - libs/console/full
- - libs/shell
- - libs/baselibc
- - libs/newtmgr
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/bleshell_priv.h
----------------------------------------------------------------------
diff --git a/project/bleshell/src/bleshell_priv.h b/project/bleshell/src/bleshell_priv.h
deleted file mode 100644
index c169acf..0000000
--- a/project/bleshell/src/bleshell_priv.h
+++ /dev/null
@@ -1,164 +0,0 @@
-/**
- * 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_BLESHELL_PRIV_
-#define H_BLESHELL_PRIV_
-
-#include <inttypes.h>
-#include "os/queue.h"
-
-#include "newtmgr/newtmgr.h"
-#include "host/ble_gatt.h"
-struct ble_gap_white_entry;
-struct ble_hs_adv_fields;
-struct ble_gap_upd_params;
-struct ble_gap_crt_params;
-struct hci_adv_params;
-struct ble_l2cap_sig_update_req;
-struct ble_l2cap_sig_update_params;
-
-#define BLESHELL_MAX_CONNS 8
-
-typedef int cmd_fn(int argc, char **argv);
-struct cmd_entry {
- char *name;
- cmd_fn *cb;
-};
-
-struct kv_pair {
- char *key;
- int val;
-};
-
-struct bleshell_dsc {
- SLIST_ENTRY(bleshell_dsc) next;
- struct ble_gatt_dsc dsc;
-};
-SLIST_HEAD(bleshell_dsc_list, bleshell_dsc);
-
-struct bleshell_chr {
- SLIST_ENTRY(bleshell_chr) next;
- struct ble_gatt_chr chr;
-
- struct bleshell_dsc_list dscs;
-};
-SLIST_HEAD(bleshell_chr_list, bleshell_chr);
-
-struct bleshell_svc {
- SLIST_ENTRY(bleshell_svc) next;
- struct ble_gatt_service svc;
-
- struct bleshell_chr_list chrs;
-};
-
-SLIST_HEAD(bleshell_svc_list, bleshell_svc);
-
-struct bleshell_conn {
- uint16_t handle;
- uint8_t addr_type;
- uint8_t addr[6];
-
- struct bleshell_svc_list svcs;
-};
-
-extern struct bleshell_conn bleshell_conns[BLESHELL_MAX_CONNS];
-extern int bleshell_num_conns;
-
-extern const char *bleshell_device_name;
-extern const uint16_t bleshell_appearance;
-extern const uint8_t bleshell_privacy_flag;
-extern uint8_t bleshell_reconnect_addr[6];
-extern uint8_t bleshell_pref_conn_params[8];
-extern uint8_t bleshell_gatt_service_changed[4];
-
-extern struct nmgr_transport nm_ble_transport;
-extern uint16_t nm_attr_val_handle;
-
-void bleshell_printf(const char *fmt, ...);
-void print_addr(void *addr);
-void print_uuid(void *uuid128);
-const struct cmd_entry *parse_cmd_find(const struct cmd_entry *cmds,
- char *name);
-struct kv_pair *parse_kv_find(struct kv_pair *kvs, char *name);
-char *parse_arg_find(char *key);
-long parse_arg_long_bounds(char *name, long min, long max, int *out_status);
-long parse_arg_long(char *name, int *staus);
-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);
-int parse_arg_kv(char *name, struct kv_pair *kvs);
-int parse_arg_byte_stream(char *name, 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_uuid(char *name, uint8_t *dst_uuid128);
-int parse_err_too_few_args(char *cmd_name);
-int parse_arg_all(int argc, char **argv);
-int cmd_init(void);
-void periph_init(void);
-int nm_chr_access(uint16_t conn_handle, uint16_t attr_handle,
- uint8_t op, union ble_gatt_access_ctxt *ctxt,
- void *arg);
-int nm_rx_rsp(uint8_t *attr_val, uint16_t attr_len);
-void nm_init(void);
-void bleshell_lock(void);
-void bleshell_unlock(void);
-int bleshell_exchange_mtu(uint16_t conn_handle);
-int bleshell_disc_svcs(uint16_t conn_handle);
-int bleshell_disc_svc_by_uuid(uint16_t conn_handle, uint8_t *uuid128);
-int bleshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle);
-int bleshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle, uint8_t *uuid128);
-int bleshell_disc_all_dscs(uint16_t conn_handle, uint16_t chr_val_handle,
- uint16_t chr_end_handle);
-int bleshell_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle);
-int bleshell_read(uint16_t conn_handle, uint16_t attr_handle);
-int bleshell_read_long(uint16_t conn_handle, uint16_t attr_handle);
-int bleshell_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle, uint8_t *uuid128);
-int bleshell_read_mult(uint16_t conn_handle, uint16_t *attr_handles,
- int num_attr_handles);
-int bleshell_write(uint16_t conn_handle, uint16_t attr_handle, void *value,
- uint16_t value_len);
-int bleshell_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
- void *value, uint16_t value_len);
-int bleshell_write_long(uint16_t conn_handle, uint16_t attr_handle,
- void *value, uint16_t value_len);
-int bleshell_write_reliable(uint16_t conn_handle, struct ble_gatt_attr *attrs,
- int num_attrs);
-int bleshell_adv_start(int disc, int conn, uint8_t *peer_addr, int addr_type,
- struct hci_adv_params *params);
-int bleshell_adv_stop(void);
-int bleshell_conn_initiate(int addr_type, uint8_t *peer_addr,
- struct ble_gap_crt_params *params);
-int bleshell_conn_cancel(void);
-int bleshell_term_conn(uint16_t conn_handle);
-int bleshell_wl_set(struct ble_gap_white_entry *white_list,
- int white_list_count);
-int bleshell_scan(uint32_t dur_ms, uint8_t disc_mode, uint8_t scan_type,
- uint8_t filter_policy);
-int bleshell_set_adv_data(struct ble_hs_adv_fields *adv_fields);
-int bleshell_update_conn(uint16_t conn_handle,
- struct ble_gap_upd_params *params);
-void bleshell_chrup(uint16_t attr_handle);
-int bleshell_l2cap_update(uint16_t conn_handle,
- struct ble_l2cap_sig_update_params *params);
-
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/cmd.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/cmd.c b/project/bleshell/src/cmd.c
deleted file mode 100644
index 0b96e15..0000000
--- a/project/bleshell/src/cmd.c
+++ /dev/null
@@ -1,1455 +0,0 @@
-/**
- * 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 <errno.h>
-#include <string.h>
-#include "bsp/bsp.h"
-#include "console/console.h"
-#include "shell/shell.h"
-
-#include "nimble/ble.h"
-#include "nimble/hci_common.h"
-#include "host/ble_gap.h"
-#include "host/ble_hs_adv.h"
-#include "../src/ble_l2cap_priv.h"
-
-#include "bleshell_priv.h"
-
-#define CMD_BUF_SZ 1024
-
-static struct shell_cmd cmd_b;
-
-static bssnz_t uint8_t cmd_buf[CMD_BUF_SZ];
-
-/*****************************************************************************
- * $misc *
- *****************************************************************************/
-
-static int
-cmd_exec(const struct cmd_entry *cmds, int argc, char **argv)
-{
- const struct cmd_entry *cmd;
- int rc;
-
- if (argc <= 1) {
- return parse_err_too_few_args(argv[0]);
- }
-
- cmd = parse_cmd_find(cmds, argv[1]);
- if (cmd == NULL) {
- bleshell_printf("Error: unknown %s command: %s\n", argv[0], argv[1]);
- return -1;
- }
-
- rc = cmd->cb(argc - 1, argv + 1);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-static void
-cmd_print_dsc(struct bleshell_dsc *dsc)
-{
- bleshell_printf(" dsc_handle=%d uuid=", dsc->dsc.handle);
- print_uuid(dsc->dsc.uuid128);
- bleshell_printf("\n");
-}
-
-static void
-cmd_print_chr(struct bleshell_chr *chr)
-{
- struct bleshell_dsc *dsc;
-
- bleshell_printf(" def_handle=%d val_handle=%d properties=0x%02x "
- "uuid=",
- chr->chr.decl_handle, chr->chr.value_handle,
- chr->chr.properties);
- print_uuid(chr->chr.uuid128);
- bleshell_printf("\n");
-
- SLIST_FOREACH(dsc, &chr->dscs, next) {
- cmd_print_dsc(dsc);
- }
-}
-
-static void
-cmd_print_svc(struct bleshell_svc *svc, int print_chrs)
-{
- struct bleshell_chr *chr;
-
- bleshell_printf(" start=%d end=%d uuid=", svc->svc.start_handle,
- svc->svc.end_handle);
- print_uuid(svc->svc.uuid128);
- bleshell_printf("\n");
-
- if (print_chrs) {
- SLIST_FOREACH(chr, &svc->chrs, next) {
- cmd_print_chr(chr);
- }
- }
-}
-
-static 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;
-}
-
-/*****************************************************************************
- * $advertise *
- *****************************************************************************/
-
-static 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 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 struct kv_pair cmd_adv_addr_types[] = {
- { "public", BLE_ADDR_TYPE_PUBLIC },
- { "random", BLE_ADDR_TYPE_RANDOM },
- { NULL }
-};
-
-static 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 },
-};
-
-static int
-cmd_adv(int argc, char **argv)
-{
- struct hci_adv_params params = {
- .adv_itvl_min = 0,
- .adv_itvl_max = 0,
- .adv_type = BLE_HCI_ADV_TYPE_ADV_IND,
- .own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC,
- .peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC,
- .adv_channel_map = BLE_HCI_ADV_CHANMASK_DEF,
- .adv_filter_policy = BLE_HCI_ADV_FILT_DEF,
- };
- uint8_t peer_addr[6];
- uint8_t u8;
- int addr_type;
- int conn;
- int disc;
- int rc;
-
- if (argc > 1 && strcmp(argv[1], "stop") == 0) {
- rc = bleshell_adv_stop();
- if (rc != 0) {
- bleshell_printf("advertise stop fail: %d\n", rc);
- return rc;
- }
-
- return 0;
- }
-
- conn = parse_arg_kv("conn", cmd_adv_conn_modes);
- if (conn == -1) {
- bleshell_printf("invalid 'conn' parameter\n");
- return -1;
- }
-
- disc = parse_arg_kv("disc", cmd_adv_disc_modes);
- if (disc == -1) {
- bleshell_printf("missing 'disc' parameter\n");
- return -1;
- }
-
- if (conn == BLE_GAP_CONN_MODE_DIR) {
- addr_type = parse_arg_kv("addr_type", cmd_adv_addr_types);
- if (addr_type == -1) {
- return -1;
- }
-
- rc = parse_arg_mac("addr", peer_addr);
- if (rc != 0) {
- return rc;
- }
- } else {
- addr_type = 0;
- memset(peer_addr, 0, sizeof peer_addr);
- }
-
- u8 = parse_arg_long_bounds("chan_map", 0, 0xff, &rc);
- if (rc == 0) {
- params.adv_channel_map = u8;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- if (parse_arg_find("filt") != NULL) {
- params.adv_filter_policy = parse_arg_kv("filt", cmd_adv_filt_types);
- if (params.adv_filter_policy == -1) {
- return EINVAL;
- }
- }
-
- rc = bleshell_adv_start(disc, conn, peer_addr, addr_type, ¶ms);
- if (rc != 0) {
- bleshell_printf("advertise fail: %d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $connect *
- *****************************************************************************/
-
-static struct kv_pair cmd_conn_addr_types[] = {
- { "public", BLE_HCI_CONN_PEER_ADDR_PUBLIC },
- { "random", BLE_HCI_CONN_PEER_ADDR_RANDOM },
- { "public_ident", BLE_HCI_CONN_PEER_ADDR_PUBLIC_IDENT },
- { "random_ident", BLE_HCI_CONN_PEER_ADDR_RANDOM_IDENT },
- { "wl", BLE_GAP_ADDR_TYPE_WL },
- { NULL }
-};
-
-static int
-cmd_conn(int argc, char **argv)
-{
- struct ble_gap_crt_params params;
- uint8_t peer_addr[6];
- int addr_type;
- int rc;
-
- if (argc > 1 && strcmp(argv[1], "cancel") == 0) {
- rc = bleshell_conn_cancel();
- if (rc != 0) {
- bleshell_printf("connection cancel fail: %d\n", rc);
- return rc;
- }
-
- return 0;
- }
-
- addr_type = parse_arg_kv("addr_type", cmd_conn_addr_types);
- if (addr_type == -1) {
- return -1;
- }
-
- if (addr_type != BLE_GAP_ADDR_TYPE_WL) {
- rc = parse_arg_mac("addr", peer_addr);
- if (rc != 0) {
- return rc;
- }
- } else {
- memset(peer_addr, 0, sizeof peer_addr);
- }
-
- params.scan_itvl = parse_arg_uint16_dflt("scan_itvl", 0x0010, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.scan_window = parse_arg_uint16_dflt("scan_window", 0x0010, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_min = parse_arg_uint16_dflt(
- "itvl_min", BLE_GAP_INITIAL_CONN_ITVL_MIN, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_max = parse_arg_uint16_dflt(
- "itvl_max", BLE_GAP_INITIAL_CONN_ITVL_MAX, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.latency = parse_arg_uint16_dflt("latency", 0, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.supervision_timeout = parse_arg_uint16_dflt("timeout", 0x0100, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.min_ce_len = parse_arg_uint16_dflt("min_ce_len", 0x0010, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.max_ce_len = parse_arg_uint16_dflt("max_ce_len", 0x0300, &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_conn_initiate(addr_type, peer_addr, ¶ms);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $connect *
- *****************************************************************************/
-
-static int
-cmd_chrup(int argc, char **argv)
-{
- uint16_t attr_handle;
- int rc;
-
- attr_handle = parse_arg_long("attr", &rc);
- if (rc != 0) {
- return rc;
- }
-
- bleshell_chrup(attr_handle);
-
- return 0;
-}
-
-/*****************************************************************************
- * $discover *
- *****************************************************************************/
-
-static int
-cmd_disc_chr(int argc, char **argv)
-{
- uint16_t start_handle;
- uint16_t conn_handle;
- uint16_t end_handle;
- uint8_t uuid128[16];
- int rc;
-
- rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle);
- if (rc != 0) {
- return rc;
- }
-
- rc = parse_arg_uuid("uuid", uuid128);
- if (rc == 0) {
- rc = bleshell_disc_chrs_by_uuid(conn_handle, start_handle, end_handle,
- uuid128);
- } else if (rc == ENOENT) {
- rc = bleshell_disc_all_chrs(conn_handle, start_handle, end_handle);
- } else {
- return rc;
- }
- if (rc != 0) {
- bleshell_printf("error discovering characteristics; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static int
-cmd_disc_dsc(int argc, char **argv)
-{
- uint16_t start_handle;
- uint16_t conn_handle;
- uint16_t end_handle;
- int rc;
-
- rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_disc_all_dscs(conn_handle, start_handle, end_handle);
- if (rc != 0) {
- bleshell_printf("error discovering descriptors; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static int
-cmd_disc_svc(int argc, char **argv)
-{
- uint8_t uuid128[16];
- int conn_handle;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = parse_arg_uuid("uuid", uuid128);
- if (rc == 0) {
- rc = bleshell_disc_svc_by_uuid(conn_handle, uuid128);
- } else if (rc == ENOENT) {
- rc = bleshell_disc_svcs(conn_handle);
- } else {
- return rc;
- }
-
- if (rc != 0) {
- bleshell_printf("error discovering services; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static struct cmd_entry cmd_disc_entries[] = {
- { "chr", cmd_disc_chr },
- { "dsc", cmd_disc_dsc },
- { "svc", cmd_disc_svc },
- { NULL, NULL }
-};
-
-static int
-cmd_disc(int argc, char **argv)
-{
- int rc;
-
- rc = cmd_exec(cmd_disc_entries, argc, argv);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $find *
- *****************************************************************************/
-
-static int
-cmd_find_inc_svcs(int argc, char **argv)
-{
- uint16_t start_handle;
- uint16_t conn_handle;
- uint16_t end_handle;
- int rc;
-
- rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_find_inc_svcs(conn_handle, start_handle, end_handle);
- if (rc != 0) {
- bleshell_printf("error finding included services; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static const struct cmd_entry cmd_find_entries[] = {
- { "inc_svcs", cmd_find_inc_svcs },
- { NULL, NULL }
-};
-
-static int
-cmd_find(int argc, char **argv)
-{
- int rc;
-
- rc = cmd_exec(cmd_find_entries, argc, argv);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $l2cap *
- *****************************************************************************/
-
-static int
-cmd_l2cap_update(int argc, char **argv)
-{
- struct ble_l2cap_sig_update_params params;
- uint16_t conn_handle;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_min = parse_arg_uint16_dflt(
- "itvl_min", BLE_GAP_INITIAL_CONN_ITVL_MIN, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_max = parse_arg_uint16_dflt(
- "itvl_max", BLE_GAP_INITIAL_CONN_ITVL_MAX, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.slave_latency = parse_arg_uint16_dflt("latency", 0, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.timeout_multiplier = parse_arg_uint16_dflt("timeout", 0x0100, &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_l2cap_update(conn_handle, ¶ms);
- if (rc != 0) {
- bleshell_printf("error txing l2cap update; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static const struct cmd_entry cmd_l2cap_entries[] = {
- { "update", cmd_l2cap_update },
- { NULL, NULL }
-};
-
-static int
-cmd_l2cap(int argc, char **argv)
-{
- int rc;
-
- rc = cmd_exec(cmd_l2cap_entries, argc, argv);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $mtu *
- *****************************************************************************/
-
-static int
-cmd_mtu(int argc, char **argv)
-{
- uint16_t conn_handle;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_exchange_mtu(conn_handle);
- if (rc != 0) {
- bleshell_printf("error exchanging mtu; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $read *
- *****************************************************************************/
-
-#define CMD_READ_MAX_ATTRS 8
-
-static int
-cmd_read(int argc, char **argv)
-{
- static uint16_t attr_handles[CMD_READ_MAX_ATTRS];
- uint16_t conn_handle;
- uint16_t start;
- uint16_t end;
- uint8_t uuid128[16];
- uint8_t num_attr_handles;
- int is_uuid;
- int is_long;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- is_long = parse_arg_long("long", &rc);
- if (rc == ENOENT) {
- is_long = 0;
- } else if (rc != 0) {
- 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) {
- return rc;
- }
- }
-
- rc = parse_arg_uuid("uuid", uuid128);
- if (rc == ENOENT) {
- is_uuid = 0;
- } else if (rc == 0) {
- is_uuid = 1;
- } else {
- return rc;
- }
-
- start = parse_arg_uint16("start", &rc);
- if (rc == ENOENT) {
- start = 0;
- } else if (rc != 0) {
- return rc;
- }
-
- end = parse_arg_uint16("end", &rc);
- if (rc == ENOENT) {
- end = 0;
- } else if (rc != 0) {
- return rc;
- }
-
- if (num_attr_handles == 1) {
- if (is_long) {
- rc = bleshell_read_long(conn_handle, attr_handles[0]);
- } else {
- rc = bleshell_read(conn_handle, attr_handles[0]);
- }
- } else if (num_attr_handles > 1) {
- rc = bleshell_read_mult(conn_handle, attr_handles, num_attr_handles);
- } else if (is_uuid) {
- if (start == 0 || end == 0) {
- rc = EINVAL;
- } else {
- rc = bleshell_read_by_uuid(conn_handle, start, end, uuid128);
- }
- } else {
- rc = EINVAL;
- }
-
- if (rc != 0) {
- bleshell_printf("error reading characteristic; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $scan *
- *****************************************************************************/
-
-static struct kv_pair cmd_scan_disc_modes[] = {
- { "ltd", BLE_GAP_DISC_MODE_LTD },
- { "gen", BLE_GAP_DISC_MODE_GEN },
- { NULL }
-};
-
-static struct kv_pair cmd_scan_types[] = {
- { "passive", BLE_HCI_SCAN_TYPE_PASSIVE },
- { "active", BLE_HCI_SCAN_TYPE_ACTIVE },
- { NULL }
-};
-
-static 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 int
-cmd_scan(int argc, char **argv)
-{
- uint32_t dur;
- int disc;
- int type;
- int filt;
- int rc;
-
- dur = parse_arg_uint16("dur", &rc);
- if (rc != 0) {
- return rc;
- }
-
- disc = parse_arg_kv("disc", cmd_scan_disc_modes);
- if (disc == -1) {
- return EINVAL;
- }
-
- type = parse_arg_kv("type", cmd_scan_types);
- if (type == -1) {
- return EINVAL;
- }
-
- filt = parse_arg_kv("filt", cmd_scan_filt_policies);
- if (disc == -1) {
- return EINVAL;
- }
-
- rc = bleshell_scan(dur, disc, type, filt);
- if (rc != 0) {
- bleshell_printf("error scanning; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $show *
- *****************************************************************************/
-
-static int
-cmd_show_addr(int argc, char **argv)
-{
- bleshell_lock();
-
- bleshell_printf("myaddr=");
- print_addr(g_dev_addr);
- bleshell_printf("\n");
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-cmd_show_chr(int argc, char **argv)
-{
- struct bleshell_conn *conn;
- struct bleshell_svc *svc;
- int i;
-
- bleshell_lock();
-
- for (i = 0; i < bleshell_num_conns; i++) {
- conn = bleshell_conns + i;
-
- bleshell_printf("CONNECTION: handle=%d addr=", conn->handle);
- print_addr(conn->addr);
- bleshell_printf("\n");
-
- SLIST_FOREACH(svc, &conn->svcs, next) {
- cmd_print_svc(svc, 1);
- }
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-cmd_show_conn(int argc, char **argv)
-{
- struct bleshell_conn *conn;
- int i;
-
- bleshell_lock();
-
- for (i = 0; i < bleshell_num_conns; i++) {
- conn = bleshell_conns + i;
-
- bleshell_printf("handle=%d addr=", conn->handle);
- print_addr(conn->addr);
- bleshell_printf(" addr_type=%d\n", conn->addr_type);
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-cmd_show_svc(int argc, char **argv)
-{
- struct bleshell_conn *conn;
- struct bleshell_svc *svc;
- int i;
-
- bleshell_lock();
-
- for (i = 0; i < bleshell_num_conns; i++) {
- conn = bleshell_conns + i;
-
- bleshell_printf("CONNECTION: handle=%d addr=", conn->handle);
- print_addr(conn->addr);
- bleshell_printf("\n");
-
- SLIST_FOREACH(svc, &conn->svcs, next) {
- cmd_print_svc(svc, 0);
- }
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static struct cmd_entry cmd_show_entries[] = {
- { "addr", cmd_show_addr },
- { "chr", cmd_show_chr },
- { "conn", cmd_show_conn },
- { "svc", cmd_show_svc },
- { NULL, NULL }
-};
-
-static int
-cmd_show(int argc, char **argv)
-{
- int rc;
-
- rc = cmd_exec(cmd_show_entries, argc, argv);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $set *
- *****************************************************************************/
-
-#define CMD_ADV_DATA_MAX_UUIDS16 8
-#define CMD_ADV_DATA_MAX_UUIDS32 8
-#define CMD_ADV_DATA_MAX_UUIDS128 8
-#define CMD_ADV_DATA_MAX_PUBLIC_TGT_ADDRS 8
-#define CMD_ADV_DATA_SVC_DATA_UUID16_MAX_LEN 32
-#define CMD_ADV_DATA_SVC_DATA_UUID32_MAX_LEN 32
-#define CMD_ADV_DATA_SVC_DATA_UUID128_MAX_LEN 32
-#define CMD_ADV_DATA_URI_MAX_LEN 32
-#define CMD_ADV_DATA_MFG_DATA_MAX_LEN 32
-
-static int
-cmd_set_adv_data(void)
-{
- static bssnz_t uint16_t uuids16[CMD_ADV_DATA_MAX_UUIDS16];
- static bssnz_t uint32_t uuids32[CMD_ADV_DATA_MAX_UUIDS32];
- static bssnz_t uint8_t uuids128[CMD_ADV_DATA_MAX_UUIDS128][16];
- 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 device_class[BLE_HS_ADV_DEVICE_CLASS_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 le_addr[BLE_HS_ADV_LE_ADDR_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];
- 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;
-
- memset(&adv_fields, 0, sizeof adv_fields);
-
- while (1) {
- uuid16 = parse_arg_uint16("uuid16", &rc);
- if (rc == 0) {
- if (adv_fields.num_uuids16 >= CMD_ADV_DATA_MAX_UUIDS16) {
- return EINVAL;
- }
- uuids16[adv_fields.num_uuids16] = uuid16;
- adv_fields.num_uuids16++;
- } else if (rc == ENOENT) {
- break;
- } else {
- return rc;
- }
- }
- if (adv_fields.num_uuids16 > 0) {
- adv_fields.uuids16 = uuids16;
- }
-
- tmp = parse_arg_long("uuids16_is_complete", &rc);
- if (rc == 0) {
- adv_fields.uuids16_is_complete = !!tmp;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- while (1) {
- uuid32 = parse_arg_uint32("uuid32", &rc);
- if (rc == 0) {
- if (adv_fields.num_uuids32 >= CMD_ADV_DATA_MAX_UUIDS32) {
- return EINVAL;
- }
- uuids32[adv_fields.num_uuids32] = uuid32;
- adv_fields.num_uuids32++;
- } else if (rc == ENOENT) {
- break;
- } else {
- return rc;
- }
- }
- if (adv_fields.num_uuids32 > 0) {
- adv_fields.uuids32 = uuids32;
- }
-
- tmp = parse_arg_long("uuids32_is_complete", &rc);
- if (rc == 0) {
- adv_fields.uuids32_is_complete = !!tmp;
- } else if (rc != ENOENT) {
- 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) {
- return EINVAL;
- }
- memcpy(uuids128[adv_fields.num_uuids128], uuid128, 16);
- adv_fields.num_uuids128++;
- } else if (rc == ENOENT) {
- break;
- } else {
- return rc;
- }
- }
- if (adv_fields.num_uuids128 > 0) {
- adv_fields.uuids128 = uuids128;
- }
-
- tmp = parse_arg_long("uuids128_is_complete", &rc);
- if (rc == 0) {
- adv_fields.uuids128_is_complete = !!tmp;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- adv_fields.name = (uint8_t *)parse_arg_find("name");
- if (adv_fields.name != NULL) {
- adv_fields.name_len = strlen((char *)adv_fields.name);
- }
-
- tmp = parse_arg_long_bounds("tx_pwr_lvl", 0, 0xff, &rc);
- if (rc == 0) {
- adv_fields.tx_pwr_lvl = tmp;
- adv_fields.tx_pwr_lvl_is_present = 1;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- rc = parse_arg_byte_stream_exact_length("device_class", device_class,
- BLE_HS_ADV_DEVICE_CLASS_LEN);
- if (rc == 0) {
- adv_fields.device_class = device_class;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- rc = parse_arg_byte_stream_exact_length("slave_itvl_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) {
- return rc;
- }
-
- rc = parse_arg_byte_stream("svc_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) {
- return rc;
- }
-
- while (1) {
- rc = parse_arg_byte_stream_exact_length(
- "public_tgt_addr", 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) {
-
- 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 {
- 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) {
- return rc;
- }
-
- adv_fields.adv_itvl = parse_arg_uint16("adv_itvl", &rc);
- if (rc == 0) {
- adv_fields.adv_itvl_is_present = 1;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- rc = parse_arg_byte_stream_exact_length("le_addr", le_addr,
- BLE_HS_ADV_LE_ADDR_LEN);
- if (rc == 0) {
- adv_fields.le_addr = le_addr;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- adv_fields.le_role = parse_arg_long_bounds("le_role", 0, 0xff, &rc);
- if (rc == 0) {
- adv_fields.le_role_is_present = 1;
- } else if (rc != ENOENT) {
- return rc;
- }
-
- rc = parse_arg_byte_stream("svc_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) {
- return rc;
- }
-
- rc = parse_arg_byte_stream("svc_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) {
- 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) {
- 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) {
- return rc;
- }
-
- rc = bleshell_set_adv_data(&adv_fields);
- if (rc != 0) {
- bleshell_printf("error setting advertisement data; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-static int
-cmd_set(int argc, char **argv)
-{
- uint16_t mtu;
- uint8_t addr[6];
- int good;
- int rc;
-
- if (argc > 1 && strcmp(argv[1], "adv_data") == 0) {
- rc = cmd_set_adv_data();
- return rc;
- }
-
- good = 0;
-
- rc = parse_arg_mac("addr", addr);
- if (rc == 0) {
- good = 1;
- memcpy(g_dev_addr, addr, 6);
- } else if (rc != ENOENT) {
- return rc;
- }
-
- 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) {
- return rc;
- }
-
- if (!good) {
- bleshell_printf("Error: no valid settings specified\n");
- return -1;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $terminate *
- *****************************************************************************/
-
-static int
-cmd_term(int argc, char **argv)
-{
- uint16_t conn_handle;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_term_conn(conn_handle);
- if (rc != 0) {
- bleshell_printf("error terminating connection; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $update connection parameters *
- *****************************************************************************/
-
-static int
-cmd_update(int argc, char **argv)
-{
- struct ble_gap_upd_params params;
- uint16_t conn_handle;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_min = parse_arg_uint16_dflt(
- "itvl_min", BLE_GAP_INITIAL_CONN_ITVL_MIN, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.itvl_max = parse_arg_uint16_dflt(
- "itvl_max", BLE_GAP_INITIAL_CONN_ITVL_MAX, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.latency = parse_arg_uint16_dflt("latency", 0, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.supervision_timeout = parse_arg_uint16_dflt("timeout", 0x0100, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.min_ce_len = parse_arg_uint16_dflt("min_ce_len", 0x0010, &rc);
- if (rc != 0) {
- return rc;
- }
-
- params.max_ce_len = parse_arg_uint16_dflt("max_ce_len", 0x0300, &rc);
- if (rc != 0) {
- return rc;
- }
-
- rc = bleshell_update_conn(conn_handle, ¶ms);
- if (rc != 0) {
- bleshell_printf("error updating connection; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $white list *
- *****************************************************************************/
-
-static struct kv_pair cmd_wl_addr_types[] = {
- { "public", BLE_HCI_CONN_PEER_ADDR_PUBLIC },
- { "random", BLE_HCI_CONN_PEER_ADDR_RANDOM },
- { NULL }
-};
-
-#define CMD_WL_MAX_SZ 8
-
-static int
-cmd_wl(int argc, char **argv)
-{
- static struct ble_gap_white_entry white_list[CMD_WL_MAX_SZ];
- uint8_t addr_type;
- uint8_t addr[6];
- int wl_cnt;
- int rc;
-
- wl_cnt = 0;
- while (1) {
- if (wl_cnt >= CMD_WL_MAX_SZ) {
- return EINVAL;
- }
-
- rc = parse_arg_mac("addr", addr);
- if (rc == ENOENT) {
- break;
- } else if (rc != 0) {
- return rc;
- }
-
- addr_type = parse_arg_kv("addr_type", cmd_wl_addr_types);
- if (addr_type == -1) {
- return EINVAL;
- }
-
- memcpy(white_list[wl_cnt].addr, addr, 6);
- white_list[wl_cnt].addr_type = addr_type;
- wl_cnt++;
- }
-
- if (wl_cnt == 0) {
- return EINVAL;
- }
-
- bleshell_wl_set(white_list, wl_cnt);
-
- return 0;
-}
-
-/*****************************************************************************
- * $write *
- *****************************************************************************/
-
-#define CMD_WRITE_MAX_ATTRS 16
-
-static int
-cmd_write(int argc, char **argv)
-{
- static struct ble_gatt_attr attrs[CMD_WRITE_MAX_ATTRS];
- uint16_t attr_handle;
- uint16_t conn_handle;
- int total_attr_len;
- int num_attrs;
- int attr_len;
- int is_long;
- int no_rsp;
- int rc;
-
- conn_handle = parse_arg_uint16("conn", &rc);
- if (rc != 0) {
- return rc;
- }
-
- no_rsp = parse_arg_long("no_rsp", &rc);
- if (rc == ENOENT) {
- no_rsp = 0;
- } else if (rc != 0) {
- return rc;
- }
-
- is_long = parse_arg_long("long", &rc);
- if (rc == ENOENT) {
- is_long = 0;
- } else if (rc != 0) {
- return rc;
- }
-
- total_attr_len = 0;
- num_attrs = 0;
- while (1) {
- attr_handle = parse_arg_long("attr", &rc);
- if (rc == ENOENT) {
- break;
- } else if (rc != 0) {
- return rc;
- }
-
- 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) {
- return rc;
- }
-
- if (num_attrs >= CMD_WRITE_MAX_ATTRS) {
- return EINVAL;
- }
-
- attrs[num_attrs].handle = attr_handle;
- attrs[num_attrs].offset = 0;
- attrs[num_attrs].value_len = attr_len;
- attrs[num_attrs].value = cmd_buf + total_attr_len;
-
- total_attr_len += attr_len;
- num_attrs++;
- }
-
- if (no_rsp) {
- if (num_attrs != 1) {
- return EINVAL;
- }
- rc = bleshell_write_no_rsp(conn_handle, attrs[0].handle,
- attrs[0].value, attrs[0].value_len);
- } else if (is_long) {
- if (num_attrs != 1) {
- return EINVAL;
- }
- rc = bleshell_write_long(conn_handle, attrs[0].handle,
- attrs[0].value, attrs[0].value_len);
- } else if (num_attrs > 1) {
- rc = bleshell_write_reliable(conn_handle, attrs, num_attrs);
- } else if (num_attrs == 1) {
- rc = bleshell_write(conn_handle, attrs[0].handle,
- attrs[0].value, attrs[0].value_len);
- } else {
- return EINVAL;
- }
-
- if (rc != 0) {
- bleshell_printf("error writing characteristic; rc=%d\n", rc);
- return rc;
- }
-
- return 0;
-}
-
-/*****************************************************************************
- * $init *
- *****************************************************************************/
-
-static struct cmd_entry cmd_b_entries[] = {
- { "adv", cmd_adv },
- { "conn", cmd_conn },
- { "chrup", cmd_chrup },
- { "disc", cmd_disc },
- { "find", cmd_find },
- { "l2cap", cmd_l2cap },
- { "mtu", cmd_mtu },
- { "read", cmd_read },
- { "scan", cmd_scan },
- { "show", cmd_show },
- { "set", cmd_set },
- { "term", cmd_term },
- { "update", cmd_update },
- { "wl", cmd_wl },
- { "write", cmd_write },
- { NULL, NULL }
-};
-
-static int
-cmd_b_exec(int argc, char **argv)
-{
- int rc;
-
- rc = parse_arg_all(argc - 1, argv + 1);
- if (rc != 0) {
- goto done;
- }
-
- rc = cmd_exec(cmd_b_entries, argc, argv);
- if (rc != 0) {
- bleshell_printf("error\n");
- goto done;
- }
-
- rc = 0;
-
-done:
- return rc;
-}
-
-int
-cmd_init(void)
-{
- int rc;
-
- rc = shell_cmd_register(&cmd_b, "b", cmd_b_exec);
- if (rc != 0) {
- return rc;
- }
-
- return 0;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/main.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/main.c b/project/bleshell/src/main.c
deleted file mode 100755
index 0f15f5a..0000000
--- a/project/bleshell/src/main.c
+++ /dev/null
@@ -1,1356 +0,0 @@
-/**
- * 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 "bsp/bsp.h"
-#include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
-#include "hal/hal_cputime.h"
-#include "console/console.h"
-#include "shell/shell.h"
-#include "bleshell_priv.h"
-
-/* BLE */
-#include "nimble/ble.h"
-#include "host/host_hci.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 "controller/ble_ll.h"
-#include "../src/ble_hs_conn.h"
-
-#define BSWAP16(x) ((uint16_t)(((x) << 8) | (((x) & 0xff00) >> 8)))
-
-/* Task 1 */
-#define HOST_TASK_PRIO (1)
-
-#define SHELL_TASK_PRIO (3)
-#define SHELL_MAX_INPUT_LEN (1024)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(384))
-static bssnz_t os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
-#define NMGR_TASK_PRIO (4)
-#define NMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(384))
-static bssnz_t os_stack_t nmgr_stack[NMGR_TASK_STACK_SIZE];
-
-static struct os_mutex bleshell_mutex;
-
-/* For LED toggling */
-int g_led_pin;
-
-/* Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
-
-/* Our random address (in case we need it) */
-uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
-
-/* A buffer for host advertising data */
-uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
-uint8_t g_host_adv_len;
-
-static uint8_t bleshell_addr[6] = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00};
-
-/* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS (10)
-#define MBUF_BUF_SIZE OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-os_membuf_t default_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
-
-/* BLESHELL variables */
-#define BLESHELL_STACK_SIZE (128)
-#define BLESHELL_TASK_PRIO (HOST_TASK_PRIO + 1)
-
-#define BLESHELL_MAX_SVCS 32
-#define BLESHELL_MAX_CHRS 64
-#define BLESHELL_MAX_DSCS 64
-
-uint32_t g_next_os_time;
-int g_bleshell_state;
-struct os_eventq g_bleshell_evq;
-struct os_task bleshell_task;
-bssnz_t os_stack_t bleshell_stack[BLESHELL_STACK_SIZE];
-
-struct bleshell_conn ble_shell_conns[BLESHELL_MAX_CONNS];
-int bleshell_num_conns;
-
-void
-bletest_inc_adv_pkt_num(void) { }
-
-bssnz_t struct bleshell_conn bleshell_conns[BLESHELL_MAX_CONNS];
-int bleshell_num_conns;
-
-static void *bleshell_svc_mem;
-static struct os_mempool bleshell_svc_pool;
-
-static void *bleshell_chr_mem;
-static struct os_mempool bleshell_chr_pool;
-
-static void *bleshell_dsc_mem;
-static struct os_mempool bleshell_dsc_pool;
-
-const char *bleshell_device_name = "runtime-1";
-const uint16_t bleshell_appearance = BSWAP16(BLE_GAP_APPEARANCE_GEN_COMPUTER);
-const uint8_t bleshell_privacy_flag = 0;
-uint8_t bleshell_reconnect_addr[6];
-uint8_t bleshell_pref_conn_params[8];
-uint8_t bleshell_gatt_service_changed[4];
-
-void
-bleshell_lock(void)
-{
- int rc;
-
- rc = os_mutex_pend(&bleshell_mutex, 0xffffffff);
- assert(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-void
-bleshell_unlock(void)
-{
- int rc;
-
- rc = os_mutex_release(&bleshell_mutex);
- assert(rc == 0 || rc == OS_NOT_STARTED);
-}
-
-static void
-bleshell_print_error(char *msg, uint16_t conn_handle,
- struct ble_gatt_error *error)
-{
- bleshell_printf("%s: conn_handle=%d status=%d att_handle=%d\n",
- msg, conn_handle, error->status, error->att_handle);
-}
-
-static void
-bleshell_print_bytes(uint8_t *bytes, int len)
-{
- int i;
-
- for (i = 0; i < len; i++) {
- bleshell_printf("%s0x%02x", i != 0 ? ":" : "", bytes[i]);
- }
-}
-
-static void
-bleshell_print_conn_desc(struct ble_gap_conn_desc *desc)
-{
- bleshell_printf("handle=%d peer_addr_type=%d peer_addr=",
- desc->conn_handle, desc->peer_addr_type);
- bleshell_print_bytes(desc->peer_addr, 6);
- bleshell_printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d",
- desc->conn_itvl, desc->conn_latency,
- desc->supervision_timeout);
-}
-
-static void
-bleshell_print_adv_fields(struct ble_hs_adv_fields *fields)
-{
- uint32_t u32;
- uint16_t u16;
- uint8_t *u8p;
- int i;
-
- if (fields->flags_is_present) {
- bleshell_printf(" flags=0x%02x\n", fields->flags);
- }
-
- if (fields->uuids16 != NULL) {
- bleshell_printf(" uuids16(%scomplete)=",
- fields->uuids16_is_complete ? "" : "in");
- u8p = fields->uuids16;
- for (i = 0; i < fields->num_uuids16; i++) {
- memcpy(&u16, u8p + i * 2, 2);
- bleshell_printf("0x%04x ", u16);
- }
- bleshell_printf("\n");
- }
-
- if (fields->uuids32 != NULL) {
- bleshell_printf(" uuids32(%scomplete)=",
- fields->uuids32_is_complete ? "" : "in");
- u8p = fields->uuids32;
- for (i = 0; i < fields->num_uuids32; i++) {
- memcpy(&u32, u8p + i * 4, 4);
- bleshell_printf("0x%08x ", (unsigned)u32);
- }
- bleshell_printf("\n");
- }
-
- if (fields->uuids128 != NULL) {
- bleshell_printf(" uuids128(%scomplete)=",
- fields->uuids128_is_complete ? "" : "in");
- u8p = fields->uuids128;
- for (i = 0; i < fields->num_uuids128; i++) {
- print_uuid(u8p);
- bleshell_printf(" ");
- u8p += 16;
- }
- bleshell_printf("\n");
- }
-
- if (fields->name != NULL) {
- bleshell_printf(" name(%scomplete)=",
- fields->name_is_complete ? "" : "in");
- bleshell_printf("%*s\n", fields->name_len, fields->name);
- }
-
- if (fields->tx_pwr_lvl_is_present) {
- bleshell_printf(" tx_pwr_lvl=%d\n", fields->tx_pwr_lvl);
- }
-
- if (fields->device_class != NULL) {
- bleshell_printf(" device_class=");
- bleshell_print_bytes(fields->device_class,
- BLE_HS_ADV_DEVICE_CLASS_LEN);
- bleshell_printf("\n");
- }
-
- if (fields->slave_itvl_range != NULL) {
- bleshell_printf(" slave_itvl_range=");
- bleshell_print_bytes(fields->slave_itvl_range,
- BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
- bleshell_printf("\n");
- }
-
- if (fields->svc_data_uuid16 != NULL) {
- bleshell_printf(" svc_data_uuid16=");
- bleshell_print_bytes(fields->svc_data_uuid16,
- fields->svc_data_uuid16_len);
- bleshell_printf("\n");
- }
-
- if (fields->public_tgt_addr != NULL) {
- bleshell_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;
- }
- bleshell_printf("\n");
- }
-
- if (fields->appearance_is_present) {
- bleshell_printf(" appearance=0x%04x\n", fields->appearance);
- }
-
- if (fields->adv_itvl_is_present) {
- bleshell_printf(" adv_itvl=0x%04x\n", fields->adv_itvl);
- }
-
- if (fields->le_addr != NULL) {
- bleshell_printf(" le_addr=");
- bleshell_print_bytes(fields->le_addr, BLE_HS_ADV_LE_ADDR_LEN);
- bleshell_printf("\n");
- }
-
- if (fields->le_role_is_present) {
- bleshell_printf(" le_role=0x%02x\n", fields->le_role);
- }
-
- if (fields->svc_data_uuid32 != NULL) {
- bleshell_printf(" svc_data_uuid32=");
- bleshell_print_bytes(fields->svc_data_uuid32,
- fields->svc_data_uuid32_len);
- bleshell_printf("\n");
- }
-
- if (fields->svc_data_uuid128 != NULL) {
- bleshell_printf(" svc_data_uuid128=");
- bleshell_print_bytes(fields->svc_data_uuid128,
- fields->svc_data_uuid128_len);
- bleshell_printf("\n");
- }
-
- if (fields->uri != NULL) {
- bleshell_printf(" uri=");
- bleshell_print_bytes(fields->uri, fields->uri_len);
- bleshell_printf("\n");
- }
-
- if (fields->mfg_data != NULL) {
- bleshell_printf(" mfg_data=");
- bleshell_print_bytes(fields->mfg_data, fields->mfg_data_len);
- bleshell_printf("\n");
- }
-}
-
-static int
-bleshell_conn_find_idx(uint16_t handle)
-{
- int i;
-
- for (i = 0; i < bleshell_num_conns; i++) {
- if (bleshell_conns[i].handle == handle) {
- return i;
- }
- }
-
- return -1;
-}
-
-static struct bleshell_conn *
-bleshell_conn_find(uint16_t handle)
-{
- int idx;
-
- idx = bleshell_conn_find_idx(handle);
- if (idx == -1) {
- return NULL;
- } else {
- return bleshell_conns + idx;
- }
-}
-
-static struct bleshell_conn *
-bleshell_conn_add(struct ble_gap_conn_desc *desc)
-{
- struct bleshell_conn *conn;
-
- assert(bleshell_num_conns < BLESHELL_MAX_CONNS);
-
- conn = bleshell_conns + bleshell_num_conns;
- bleshell_num_conns++;
-
- conn->handle = desc->conn_handle;
- conn->addr_type = desc->peer_addr_type;
- memcpy(conn->addr, desc->peer_addr, 6);
- SLIST_INIT(&conn->svcs);
-
- return conn;
-}
-
-static void
-bleshell_conn_delete_idx(int idx)
-{
- struct bleshell_conn *conn;
- struct bleshell_svc *svc;
- int i;
-
- assert(idx >= 0 && idx < bleshell_num_conns);
-
- conn = bleshell_conns + idx;
- while ((svc = SLIST_FIRST(&conn->svcs)) != NULL) {
- SLIST_REMOVE_HEAD(&conn->svcs, next);
- os_memblock_put(&bleshell_svc_pool, svc);
- }
-
- bleshell_num_conns--;
- for (i = idx; i < bleshell_num_conns; i++) {
- bleshell_conns[i - 1] = bleshell_conns[i];
- }
-}
-
-static struct bleshell_svc *
-bleshell_svc_find_prev(struct bleshell_conn *conn, uint16_t svc_start_handle)
-{
- struct bleshell_svc *prev;
- struct bleshell_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 bleshell_svc *
-bleshell_svc_find(struct bleshell_conn *conn, uint16_t svc_start_handle,
- struct bleshell_svc **out_prev)
-{
- struct bleshell_svc *prev;
- struct bleshell_svc *svc;
-
- prev = bleshell_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 bleshell_svc *
-bleshell_svc_find_range(struct bleshell_conn *conn, uint16_t attr_handle)
-{
- struct bleshell_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 struct bleshell_svc *
-bleshell_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
-{
- struct bleshell_conn *conn;
- struct bleshell_svc *prev;
- struct bleshell_svc *svc;
-
- conn = bleshell_conn_find(conn_handle);
- if (conn == NULL) {
- bleshell_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
- conn_handle);
- return NULL;
- }
-
- svc = bleshell_svc_find(conn, gatt_svc->start_handle, &prev);
- if (svc != NULL) {
- /* Service already discovered. */
- return svc;
- }
-
- svc = os_memblock_get(&bleshell_svc_pool);
- if (svc == NULL) {
- bleshell_printf("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 bleshell_chr *
-bleshell_chr_find_prev(struct bleshell_svc *svc, uint16_t chr_def_handle)
-{
- struct bleshell_chr *prev;
- struct bleshell_chr *chr;
-
- prev = NULL;
- SLIST_FOREACH(chr, &svc->chrs, next) {
- if (chr->chr.decl_handle >= chr_def_handle) {
- break;
- }
-
- prev = chr;
- }
-
- return prev;
-}
-
-static struct bleshell_chr *
-bleshell_chr_find(struct bleshell_svc *svc, uint16_t chr_def_handle,
- struct bleshell_chr **out_prev)
-{
- struct bleshell_chr *prev;
- struct bleshell_chr *chr;
-
- prev = bleshell_chr_find_prev(svc, chr_def_handle);
- if (prev == NULL) {
- chr = SLIST_FIRST(&svc->chrs);
- } else {
- chr = SLIST_NEXT(prev, next);
- }
-
- if (chr != NULL && chr->chr.decl_handle != chr_def_handle) {
- chr = NULL;
- }
-
- if (out_prev != NULL) {
- *out_prev = prev;
- }
- return chr;
-}
-
-
-static struct bleshell_chr *
-bleshell_chr_add(uint16_t conn_handle, uint16_t svc_start_handle,
- struct ble_gatt_chr *gatt_chr)
-{
- struct bleshell_conn *conn;
- struct bleshell_chr *prev;
- struct bleshell_chr *chr;
- struct bleshell_svc *svc;
-
- conn = bleshell_conn_find(conn_handle);
- if (conn == NULL) {
- bleshell_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
- conn_handle);
- return NULL;
- }
-
- svc = bleshell_svc_find(conn, svc_start_handle, NULL);
- if (svc == NULL) {
- bleshell_printf("CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n",
- conn_handle);
- return NULL;
- }
-
- chr = bleshell_chr_find(svc, gatt_chr->decl_handle, &prev);
- if (chr != NULL) {
- /* Characteristic already discovered. */
- return chr;
- }
-
- chr = os_memblock_get(&bleshell_chr_pool);
- if (chr == NULL) {
- bleshell_printf("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 bleshell_dsc *
-bleshell_dsc_find_prev(struct bleshell_chr *chr, uint16_t dsc_handle)
-{
- struct bleshell_dsc *prev;
- struct bleshell_dsc *dsc;
-
- prev = NULL;
- SLIST_FOREACH(dsc, &chr->dscs, next) {
- if (dsc->dsc.handle >= dsc_handle) {
- break;
- }
-
- prev = dsc;
- }
-
- return prev;
-}
-
-static struct bleshell_dsc *
-bleshell_dsc_find(struct bleshell_chr *chr, uint16_t dsc_handle,
- struct bleshell_dsc **out_prev)
-{
- struct bleshell_dsc *prev;
- struct bleshell_dsc *dsc;
-
- prev = bleshell_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 bleshell_dsc *
-bleshell_dsc_add(uint16_t conn_handle, uint16_t chr_def_handle,
- struct ble_gatt_dsc *gatt_dsc)
-{
- struct bleshell_conn *conn;
- struct bleshell_dsc *prev;
- struct bleshell_dsc *dsc;
- struct bleshell_svc *svc;
- struct bleshell_chr *chr;
-
- conn = bleshell_conn_find(conn_handle);
- if (conn == NULL) {
- bleshell_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
- conn_handle);
- return NULL;
- }
-
- svc = bleshell_svc_find_range(conn, chr_def_handle);
- if (svc == NULL) {
- bleshell_printf("CAN'T FIND SERVICE FOR DISCOVERED DSC; HANDLE=%d\n",
- conn_handle);
- return NULL;
- }
-
- chr = bleshell_chr_find(svc, chr_def_handle, NULL);
- if (chr == NULL) {
- bleshell_printf("CAN'T FIND CHARACTERISTIC FOR DISCOVERED DSC; "
- "HANDLE=%d\n", conn_handle);
- return NULL;
- }
-
- dsc = bleshell_dsc_find(chr, gatt_dsc->handle, &prev);
- if (dsc != NULL) {
- /* Descriptor already discovered. */
- return dsc;
- }
-
- dsc = os_memblock_get(&bleshell_dsc_pool);
- if (dsc == NULL) {
- bleshell_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 int
-bleshell_on_mtu(uint16_t conn_handle, struct ble_gatt_error *error,
- uint16_t mtu, void *arg)
-{
- if (error != NULL) {
- bleshell_print_error("ERROR EXCHANGING MTU", conn_handle, error);
- } else {
- bleshell_printf("mtu exchange complete: conn_handle=%d mtu=%d\n",
- conn_handle, mtu);
- }
-
- return 0;
-}
-
-static int
-bleshell_on_disc_s(uint16_t conn_handle, struct ble_gatt_error *error,
- struct ble_gatt_service *service, void *arg)
-{
- bleshell_lock();
-
- if (error != NULL) {
- bleshell_print_error("ERROR DISCOVERING SERVICE", conn_handle, error);
- } else if (service != NULL) {
- bleshell_svc_add(conn_handle, service);
- } else {
- /* Service discovery complete. */
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-bleshell_on_disc_c(uint16_t conn_handle, struct ble_gatt_error *error,
- struct ble_gatt_chr *chr, void *arg)
-{
- intptr_t svc_start_handle;
-
- bleshell_lock();
-
- svc_start_handle = (intptr_t)arg;
-
- if (error != NULL) {
- bleshell_print_error("ERROR DISCOVERING CHARACTERISTIC", conn_handle,
- error);
- } else if (chr != NULL) {
- bleshell_chr_add(conn_handle, svc_start_handle, chr);
- } else {
- /* Service discovery complete. */
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-bleshell_on_disc_d(uint16_t conn_handle, struct ble_gatt_error *error,
- uint16_t chr_def_handle, struct ble_gatt_dsc *dsc,
- void *arg)
-{
- bleshell_lock();
-
- if (error != NULL) {
- bleshell_print_error("ERROR DISCOVERING DESCRIPTOR", conn_handle,
- error);
- } else if (dsc != NULL) {
- bleshell_dsc_add(conn_handle, chr_def_handle, dsc);
- } else {
- /* Descriptor discovery complete. */
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static int
-bleshell_on_read(uint16_t conn_handle, struct ble_gatt_error *error,
- struct ble_gatt_attr *attr, void *arg)
-{
- if (error != NULL) {
- bleshell_print_error("ERROR READING CHARACTERISTIC", conn_handle,
- error);
- } else if (attr != NULL) {
- bleshell_printf("characteristic read; conn_handle=%d "
- "attr_handle=%d len=%d value=", conn_handle,
- attr->handle, attr->value_len);
- bleshell_print_bytes(attr->value, attr->value_len);
- bleshell_printf("\n");
- } else {
- bleshell_printf("characteristic read complete\n");
- }
-
- return 0;
-}
-
-static int
-bleshell_on_read_mult(uint16_t conn_handle, struct ble_gatt_error *error,
- uint16_t *attr_handles, uint8_t num_attr_handles,
- uint8_t *attr_data, uint16_t attr_data_len, void *arg)
-{
- int i;
-
- if (error != NULL) {
- bleshell_print_error("ERROR READING CHARACTERISTICS", conn_handle,
- error);
- } else {
- bleshell_printf("multiple characteristic read complete; conn_handle=%d "
- "attr_handles=", conn_handle);
- for (i = 0; i < num_attr_handles; i++) {
- bleshell_printf("%s%d", i != 0 ? "," : "", attr_handles[i]);
- }
-
- bleshell_printf(" len=%d value=", attr_data_len);
- bleshell_print_bytes(attr_data, attr_data_len);
- bleshell_printf("\n");
- }
-
- return 0;
-
-}
-
-static int
-bleshell_on_write(uint16_t conn_handle, struct ble_gatt_error *error,
- struct ble_gatt_attr *attr, void *arg)
-{
- if (error != NULL) {
- bleshell_print_error("ERROR WRITING CHARACTERISTIC", conn_handle,
- error);
- } else {
- bleshell_printf("characteristic write complete; conn_handle=%d "
- "attr_handle=%d len=%d value=", conn_handle,
- attr->handle, attr->value_len);
- bleshell_print_bytes(attr->value, attr->value_len);
- bleshell_printf("\n");
- }
-
- return 0;
-}
-
-static int
-bleshell_on_write_reliable(uint16_t conn_handle, struct ble_gatt_error *error,
- struct ble_gatt_attr *attrs, uint8_t num_attrs,
- void *arg)
-{
- int i;
-
- if (error != NULL) {
- bleshell_print_error("ERROR WRITING CHARACTERISTICS RELIABLY",
- conn_handle, error);
- } else {
- bleshell_printf("characteristic write reliable complete; "
- "conn_handle=%d", conn_handle);
-
- for (i = 0; i < num_attrs; i++) {
- bleshell_printf(" attr_handle=%d len=%d value=", attrs[i].handle,
- attrs[i].value_len);
- bleshell_print_bytes(attrs[i].value, attrs[i].value_len);
- }
- bleshell_printf("\n");
- }
-
- return 0;
-}
-
-static int
-bleshell_on_notify(uint16_t conn_handle, uint16_t attr_handle,
- uint8_t *attr_val, uint16_t attr_len, void *arg)
-{
- bleshell_printf("received notification from conn_handle=%d attr=%d "
- "len=%d value=", conn_handle, attr_handle, attr_len);
-
- bleshell_print_bytes(attr_val, attr_len);
- bleshell_printf("\n");
-
- if (attr_handle == nm_attr_val_handle) {
- nm_rx_rsp(attr_val, attr_len);
- }
-
- return 0;
-}
-
-static int
-bleshell_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
- void *arg)
-{
- int conn_idx;
-
- bleshell_lock();
-
- switch (event) {
- case BLE_GAP_EVENT_CONN:
- bleshell_printf("connection complete; status=%d ", status);
- bleshell_print_conn_desc(ctxt->desc);
- bleshell_printf("\n");
-
- if (status == 0) {
- bleshell_conn_add(ctxt->desc);
- } else {
- if (ctxt->desc->conn_handle == BLE_HS_CONN_HANDLE_NONE) {
- if (status == BLE_HS_HCI_ERR(BLE_ERR_UNK_CONN_ID)) {
- bleshell_printf("connection procedure cancelled.\n");
- }
- } else {
- conn_idx = bleshell_conn_find_idx(ctxt->desc->conn_handle);
- if (conn_idx == -1) {
- bleshell_printf("UNKNOWN CONNECTION\n");
- } else {
- bleshell_conn_delete_idx(conn_idx);
- }
- }
- }
-
- break;
-
- case BLE_GAP_EVENT_CONN_UPDATED:
- bleshell_printf("connection updated; status=%d ", status);
- bleshell_print_conn_desc(ctxt->desc);
- bleshell_printf("\n");
- break;
-
- case BLE_GAP_EVENT_CONN_UPDATE_REQ:
- bleshell_printf("connection update request; status=%d ", status);
- *ctxt->self_params = *ctxt->peer_params;
- break;
- }
-
- bleshell_unlock();
-
- return 0;
-}
-
-static void
-bleshell_on_l2cap_update(int status, void *arg)
-{
- bleshell_printf("l2cap update complete; status=%d\n", status);
-}
-
-static void
-bleshell_on_wl_set(int status, void *arg)
-{
- bleshell_printf("white list set status=%d\n", status);
-}
-
-static void
-bleshell_on_scan(int event, int status, struct ble_gap_disc_desc *desc,
- void *arg)
-{
- switch (event) {
- case BLE_GAP_EVENT_DISC_SUCCESS:
- bleshell_printf("received advertisement; event_type=%d addr_type=%d "
- "addr=", desc->event_type, desc->addr_type);
- bleshell_print_bytes(desc->addr, 6);
- bleshell_printf(" length_data=%d rssi=%d data=", desc->length_data,
- desc->rssi);
- bleshell_print_bytes(desc->data, desc->length_data);
- bleshell_printf(" fields:\n");
- bleshell_print_adv_fields(desc->fields);
- bleshell_printf("\n");
- break;
-
- case BLE_GAP_EVENT_DISC_FINISHED:
- bleshell_printf("scanning finished; status=%d\n", status);
- break;
-
- default:
- assert(0);
- break;
- }
-}
-
-int
-bleshell_exchange_mtu(uint16_t conn_handle)
-{
- int rc;
-
- rc = ble_gattc_exchange_mtu(conn_handle, bleshell_on_mtu, NULL);
- return rc;
-}
-
-int
-bleshell_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,
- bleshell_on_disc_c, (void *)svc_start_handle);
- return rc;
-}
-
-int
-bleshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle, uint8_t *uuid128)
-{
- 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,
- uuid128, bleshell_on_disc_c,
- &svc_start_handle);
- return rc;
-}
-
-int
-bleshell_disc_svcs(uint16_t conn_handle)
-{
- int rc;
-
- rc = ble_gattc_disc_all_svcs(conn_handle, bleshell_on_disc_s, NULL);
- return rc;
-}
-
-int
-bleshell_disc_svc_by_uuid(uint16_t conn_handle, uint8_t *uuid128)
-{
- int rc;
-
- rc = ble_gattc_disc_svc_by_uuid(conn_handle, uuid128,
- bleshell_on_disc_s, NULL);
- return rc;
-}
-
-int
-bleshell_disc_all_dscs(uint16_t conn_handle, uint16_t chr_def_handle,
- uint16_t chr_end_handle)
-{
- int rc;
-
- rc = ble_gattc_disc_all_dscs(conn_handle, chr_def_handle, chr_end_handle,
- bleshell_on_disc_d, NULL);
- return rc;
-}
-
-int
-bleshell_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,
- bleshell_on_disc_s, NULL);
- return rc;
-}
-
-int
-bleshell_read(uint16_t conn_handle, uint16_t attr_handle)
-{
- int rc;
-
- rc = ble_gattc_read(conn_handle, attr_handle, bleshell_on_read, NULL);
- return rc;
-}
-
-int
-bleshell_read_long(uint16_t conn_handle, uint16_t attr_handle)
-{
- int rc;
-
- rc = ble_gattc_read_long(conn_handle, attr_handle, bleshell_on_read, NULL);
- return rc;
-}
-
-int
-bleshell_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
- uint16_t end_handle, uint8_t *uuid128)
-{
- int rc;
-
- rc = ble_gattc_read_by_uuid(conn_handle, start_handle, end_handle, uuid128,
- bleshell_on_read, NULL);
- return rc;
-}
-
-int
-bleshell_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,
- bleshell_on_read_mult, NULL);
- return rc;
-}
-
-int
-bleshell_write(uint16_t conn_handle, uint16_t attr_handle, void *value,
- uint16_t value_len)
-{
- int rc;
-
- if (conn_handle == BLE_HS_CONN_HANDLE_NONE) {
- rc = ble_att_svr_write_local(attr_handle, value, value_len);
- } else {
- rc = ble_gattc_write(conn_handle, attr_handle, value, value_len,
- bleshell_on_write, NULL);
- }
-
- return rc;
-}
-
-int
-bleshell_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, void *value,
- uint16_t value_len)
-{
- int rc;
-
- rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, value, value_len,
- bleshell_on_write, NULL);
- return rc;
-}
-
-int
-bleshell_write_long(uint16_t conn_handle, uint16_t attr_handle, void *value,
- uint16_t value_len)
-{
- int rc;
-
- rc = ble_gattc_write_long(conn_handle, attr_handle, value, value_len,
- bleshell_on_write, NULL);
- return rc;
-}
-
-int
-bleshell_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,
- bleshell_on_write_reliable, NULL);
- return rc;
-}
-
-int
-bleshell_adv_stop(void)
-{
- int rc;
-
- rc = ble_gap_adv_stop();
- return rc;
-}
-
-int
-bleshell_adv_start(int disc, int conn, uint8_t *peer_addr, int addr_type,
- struct hci_adv_params *params)
-{
- int rc;
-
- rc = ble_gap_adv_start(disc, conn, peer_addr, addr_type, params,
- bleshell_on_connect, NULL);
- return rc;
-}
-
-int
-bleshell_conn_initiate(int addr_type, uint8_t *peer_addr,
- struct ble_gap_crt_params *params)
-{
- int rc;
-
- rc = ble_gap_conn_initiate(addr_type, peer_addr, NULL, bleshell_on_connect,
- params);
- return rc;
-}
-
-int
-bleshell_conn_cancel(void)
-{
- int rc;
-
- rc = ble_gap_cancel();
- return rc;
-}
-
-int
-bleshell_term_conn(uint16_t conn_handle)
-{
- int rc;
-
- rc = ble_gap_terminate(conn_handle);
- return rc;
-}
-
-int
-bleshell_wl_set(struct ble_gap_white_entry *white_list, int white_list_count)
-{
- int rc;
-
- rc = ble_gap_wl_set(white_list, white_list_count, bleshell_on_wl_set,
- NULL);
- return rc;
-}
-
-int
-bleshell_scan(uint32_t dur_ms, uint8_t disc_mode, uint8_t scan_type,
- uint8_t filter_policy)
-{
- int rc;
-
- rc = ble_gap_disc(dur_ms, disc_mode, scan_type, filter_policy,
- bleshell_on_scan, NULL);
- return rc;
-}
-
-int
-bleshell_set_adv_data(struct ble_hs_adv_fields *adv_fields)
-{
- int rc;
-
- rc = ble_gap_adv_set_fields(adv_fields);
- return rc;
-}
-
-int
-bleshell_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
-bleshell_chrup(uint16_t attr_handle)
-{
- ble_gatts_chr_updated(attr_handle);
-}
-
-int
-bleshell_l2cap_update(uint16_t conn_handle,
- struct ble_l2cap_sig_update_params *params)
-{
- int rc;
-
- rc = ble_l2cap_sig_update(conn_handle, params, bleshell_on_l2cap_update,
- NULL);
- return rc;
-}
-
-/**
- * BLE test task
- *
- * @param arg
- */
-static void
-bleshell_task_handler(void *arg)
-{
- struct os_event *ev;
- struct os_callout_func *cf;
-
- periph_init();
-
- ble_att_set_notify_cb(bleshell_on_notify, NULL);
-
- /* Initialize eventq */
- os_eventq_init(&g_bleshell_evq);
-
- /* Init bleshell variables */
- g_bleshell_state = 0;
- g_next_os_time = os_time_get();
-
- while (1) {
- ev = os_eventq_get(&g_bleshell_evq);
- switch (ev->ev_type) {
- case OS_EVENT_T_TIMER:
- cf = (struct os_callout_func *)ev;
- assert(cf->cf_func);
- cf->cf_func(cf->cf_arg);
- break;
- default:
- assert(0);
- break;
- }
- }
-}
-
-/**
- * main
- *
- * The main function for the project. This function initializes the os, calls
- * init_tasks to initialize tasks (and possibly other objects), then starts the
- * OS. We should not return from os start.
- *
- * @return int NOTE: this function should never return!
- */
-int
-main(void)
-{
- int i;
- int rc;
- uint32_t seed;
-
- /* Initialize OS */
- os_init();
-
- /* Set cputime to count at 1 usec increments */
- rc = cputime_init(1000000);
- assert(rc == 0);
-
- /* Dummy device address */
- memcpy(g_dev_addr, bleshell_addr, 6);
-
- /*
- * Seed random number generator with least significant bytes of device
- * address.
- */
- seed = 0;
- for (i = 0; i < 4; ++i) {
- seed |= g_dev_addr[i];
- seed <<= 8;
- }
- srand(seed);
-
- /* Set the led pin as an output */
- g_led_pin = LED_BLINK_PIN;
- gpio_init_out(g_led_pin, 1);
-
- bleshell_svc_mem = malloc(
- OS_MEMPOOL_BYTES(BLESHELL_MAX_SVCS, sizeof (struct bleshell_svc)));
- assert(bleshell_svc_mem != NULL);
-
- rc = os_mempool_init(&bleshell_svc_pool, BLESHELL_MAX_SVCS,
- sizeof (struct bleshell_svc), bleshell_svc_mem,
- "bleshell_svc_pool");
- assert(rc == 0);
-
- bleshell_chr_mem = malloc(
- OS_MEMPOOL_BYTES(BLESHELL_MAX_CHRS, sizeof (struct bleshell_chr)));
- assert(bleshell_chr_mem != NULL);
-
- rc = os_mempool_init(&bleshell_chr_pool, BLESHELL_MAX_CHRS,
- sizeof (struct bleshell_chr), bleshell_chr_mem,
- "bleshell_chr_pool");
- assert(rc == 0);
-
- bleshell_dsc_mem = malloc(
- OS_MEMPOOL_BYTES(BLESHELL_MAX_DSCS, sizeof (struct bleshell_dsc)));
- assert(bleshell_dsc_mem != NULL);
-
- rc = os_mempool_init(&bleshell_dsc_pool, BLESHELL_MAX_DSCS,
- sizeof (struct bleshell_dsc), bleshell_dsc_mem,
- "bleshell_dsc_pool");
- assert(rc == 0);
-
- rc = os_mempool_init(&default_mbuf_mpool, MBUF_NUM_MBUFS,
- MBUF_MEMBLOCK_SIZE, default_mbuf_mpool_data,
- "default_mbuf_data");
- assert(rc == 0);
-
- rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
- MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
- assert(rc == 0);
-
- rc = os_msys_register(&default_mbuf_pool);
- assert(rc == 0);
-
- os_task_init(&bleshell_task, "bleshell", bleshell_task_handler,
- NULL, BLESHELL_TASK_PRIO, OS_WAIT_FOREVER,
- bleshell_stack, BLESHELL_STACK_SIZE);
-
- /* Initialize host HCI */
- rc = ble_hs_init(HOST_TASK_PRIO, NULL);
- assert(rc == 0);
-
- /* Initialize the BLE LL */
- ble_ll_init();
-
- rc = shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
- SHELL_MAX_INPUT_LEN);
- assert(rc == 0);
-
- /* Init the console */
- rc = console_init(shell_console_rx_cb);
- assert(rc == 0);
-
- rc = cmd_init();
- assert(rc == 0);
-
- nmgr_task_init(NMGR_TASK_PRIO, nmgr_stack, NMGR_TASK_STACK_SIZE);
- nm_init();
-
- /* Initialize the preferred parameters. */
- htole16(bleshell_pref_conn_params + 0, BLE_GAP_INITIAL_CONN_ITVL_MIN);
- htole16(bleshell_pref_conn_params + 2, BLE_GAP_INITIAL_CONN_ITVL_MAX);
- htole16(bleshell_pref_conn_params + 4, 0);
- htole16(bleshell_pref_conn_params + 6, BSWAP16(0x100));
-
- rc = os_mutex_init(&bleshell_mutex);
- assert(rc == 0);
-
- /* Start the OS */
- os_start();
-
- /* os start should never return. If it does, this should be an error */
- assert(0);
-
- return 0;
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/2e19f75d/project/bleshell/src/nm.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/nm.c b/project/bleshell/src/nm.c
deleted file mode 100644
index dcd5cbe..0000000
--- a/project/bleshell/src/nm.c
+++ /dev/null
@@ -1,139 +0,0 @@
-/**
- * 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 "bsp/bsp.h"
-#include "os/os.h"
-#include "console/console.h"
-#include "shell/shell.h"
-#include "newtmgr/newtmgr.h"
-#include "host/ble_hs.h"
-#include "bleshell_priv.h"
-
-struct nmgr_transport nm_ble_transport;
-uint16_t nm_attr_val_handle;
-
-static bssnz_t uint8_t nm_buf[512];
-
-int
-nm_chr_access(uint16_t conn_handle, uint16_t attr_handle,
- uint8_t op, union ble_gatt_access_ctxt *ctxt,
- void *arg)
-{
- struct nmgr_transport *nt;
- struct os_mbuf *om;
- int rc;
-
- assert(attr_handle == nm_attr_val_handle);
-
- om = NULL;
-
- if (op != BLE_GATT_ACCESS_OP_WRITE_CHR) {
- rc = BLE_ATT_ERR_WRITE_NOT_PERMITTED;
- goto err;
- }
-
- om = os_msys_get_pkthdr(ctxt->chr_access.len, 2);
- if (om == NULL) {
- rc = 1;
- goto err;
- }
-
- /* Put the connection handle in the request packet header. */
- memcpy(OS_MBUF_USRHDR(om), &conn_handle, sizeof conn_handle);
-
- rc = os_mbuf_append(om, ctxt->chr_access.data, ctxt->chr_access.len);
- if (rc != 0) {
- goto err;
- }
-
- nt = arg;
- rc = nmgr_rx_req(nt, om);
-
- return rc;
-
-err:
- os_mbuf_free_chain(om);
- return rc;
-}
-
-static int
-nm_ble_out(struct nmgr_transport *nt, struct os_mbuf *om)
-{
- struct ble_gatt_attr attr;
- uint16_t conn_handle;
- int rc;
-
- assert(OS_MBUF_USRHDR_LEN(om) == 2);
- memcpy(&conn_handle, OS_MBUF_USRHDR(om), 2);
-
- assert(OS_MBUF_PKTLEN(om) <= sizeof nm_buf);
- rc = os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), nm_buf);
- assert(rc == 0);
-
- attr.handle = nm_attr_val_handle;
- attr.offset = 0;
- attr.value_len = OS_MBUF_PKTLEN(om);
- attr.value = nm_buf;
-
- rc = ble_gattc_notify_custom(conn_handle, &attr);
- console_printf("nm_ble_out(); conn_handle = %d notify-rc=%d\n",
- conn_handle, rc);
-
- return rc;
-}
-
-int
-nm_rx_rsp(uint8_t *attr_val, uint16_t attr_len)
-{
- struct os_mbuf *om;
- int rc;
-
- om = os_msys_get_pkthdr(attr_len, 0);
- if (om == NULL) {
- rc = 1;
- goto err;
- }
-
- rc = os_mbuf_append(om, attr_val, attr_len);
- if (rc != 0) {
- goto err;
- }
-
- bleshell_printf("received nmgr rsp: ");
- rc = shell_nlip_output(om);
- if (rc != 0) {
- goto err;
- }
-
- return 0;
-
-err:
- os_mbuf_free_chain(om);
- return rc;
-}
-
-void
-nm_init(void)
-{
- int rc;
-
- rc = nmgr_transport_init(&nm_ble_transport, nm_ble_out);
- assert(rc == 0);
-}