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/07/25 20:19:42 UTC
[2/3] incubator-mynewt-core git commit: BLE Host - Don't require
2-phase GATT server init.
BLE Host - Don't require 2-phase GATT server init.
Prior to this change: the application had initialize each GATT service
in two steps:
1. (before ble_hs_init())- Account for cfg resource requirements.
2. (after ble_hs_init())- Register service.
Now: Each service definition array gets added to a global array of
pointers when step one is performed. When ble_hs_init() is called, all
queued service automatically get registered.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/fb6b4804
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/fb6b4804
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/fb6b4804
Branch: refs/heads/develop
Commit: fb6b480413e7f019f32e3bab3ad4836d7cbc92b6
Parents: 65e9a50
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Jul 25 13:09:08 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Jul 25 13:18:40 2016 -0700
----------------------------------------------------------------------
.../transport/ble/include/nmgrble/newtmgr_ble.h | 2 -
libs/newtmgr/transport/ble/src/newtmgr_ble.c | 19 ++---
net/nimble/host/include/host/ble_gatt.h | 1 +
net/nimble/host/include/host/ble_hs.h | 12 ++++
.../include/services/mandatory/ble_svc_gap.h | 1 -
.../include/services/mandatory/ble_svc_gatt.h | 1 -
.../host/services/mandatory/src/ble_svc_gap.c | 14 ++--
.../host/services/mandatory/src/ble_svc_gatt.c | 14 ++--
net/nimble/host/src/ble_gatts.c | 75 +++++++++++++++++---
9 files changed, 94 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h b/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
index 937b21d..a320eaa 100644
--- a/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
+++ b/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
@@ -24,7 +24,5 @@ int
nmgr_ble_proc_mq_evt(struct os_event *ev);
int
nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg);
-int
-nmgr_ble_svc_register(void);
#endif /* _NETMGR_H */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/libs/newtmgr/transport/ble/src/newtmgr_ble.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/src/newtmgr_ble.c b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
index 14fea3e..dbd9f12 100644
--- a/libs/newtmgr/transport/ble/src/newtmgr_ble.c
+++ b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
@@ -220,6 +220,11 @@ nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg)
goto err;
}
+ rc = ble_gatts_add_svcs(gatt_svr_svcs);
+ if (rc != 0) {
+ return rc;
+ }
+
app_evq = evq;
os_mqueue_init(&ble_nmgr_mq, &ble_nmgr_mq);
@@ -229,17 +234,3 @@ nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg)
err:
return rc;
}
-
-/**
- * Register nmgr service
- *
- * @return 0 on success; non-zero on failure
- */
-int
-nmgr_ble_svc_register(void) {
-
- int rc;
- rc = ble_gatts_register_svcs(gatt_svr_svcs, NULL, NULL);
- return rc;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 31a8427..8aab982 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -433,6 +433,7 @@ typedef void ble_gatt_register_fn(struct ble_gatt_register_ctxt *ctxt,
int ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
ble_gatt_register_fn *register_cb,
void *cb_arg);
+int ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs);
int ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs,
struct ble_gatt_resources *res);
int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index ec1b3d8..56fbc2b 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -134,6 +134,18 @@ struct ble_hs_cfg {
*/
uint16_t max_client_configs;
+ /**
+ * An optional callback that gets executed upon registration of each GATT
+ * resource (service, characteristic, or descriptor).
+ */
+ ble_gatt_register_fn *gatts_register_cb;
+
+ /**
+ * An optional argument that gets passed to the GATT registration
+ * callback.
+ */
+ void *gatts_register_arg;
+
/*** GATT client settings. */
/**
* The maximum number of concurrent GATT client procedures. When you
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
index 6076fdc..95d4226 100644
--- a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
+++ b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
@@ -34,7 +34,6 @@ struct ble_hs_cfg;
const char *ble_svc_gap_device_name(void);
int ble_svc_gap_device_name_set(const char *name);
-int ble_svc_gap_register(void);
int ble_svc_gap_init(struct ble_hs_cfg *cfg);
#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
index a0ff6da..d2438c2 100644
--- a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
+++ b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
@@ -5,7 +5,6 @@ struct ble_hs_cfg;
#define BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16 0x2a05
-int ble_svc_gatt_register(void);
int ble_svc_gatt_init(struct ble_hs_cfg *cfg);
#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/services/mandatory/src/ble_svc_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/src/ble_svc_gap.c b/net/nimble/host/services/mandatory/src/ble_svc_gap.c
index d5d50f3..d49396d 100644
--- a/net/nimble/host/services/mandatory/src/ble_svc_gap.c
+++ b/net/nimble/host/services/mandatory/src/ble_svc_gap.c
@@ -149,15 +149,6 @@ ble_svc_gap_device_name_set(const char *name)
}
int
-ble_svc_gap_register(void)
-{
- int rc;
-
- rc = ble_gatts_register_svcs(ble_svc_gap_defs, NULL, NULL);
- return rc;
-}
-
-int
ble_svc_gap_init(struct ble_hs_cfg *cfg)
{
int rc;
@@ -167,5 +158,10 @@ ble_svc_gap_init(struct ble_hs_cfg *cfg)
return rc;
}
+ rc = ble_gatts_add_svcs(ble_svc_gap_defs);
+ if (rc != 0) {
+ return rc;
+ }
+
return 0;
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/src/ble_svc_gatt.c b/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
index 704f849..0e8a139 100644
--- a/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
+++ b/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
@@ -72,15 +72,6 @@ ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
}
int
-ble_svc_gatt_register(void)
-{
- int rc;
-
- rc = ble_gatts_register_svcs(ble_svc_gatt_defs, NULL, NULL);
- return rc;
-}
-
-int
ble_svc_gatt_init(struct ble_hs_cfg *cfg)
{
int rc;
@@ -90,5 +81,10 @@ ble_svc_gatt_init(struct ble_hs_cfg *cfg)
return rc;
}
+ rc = ble_gatts_add_svcs(ble_svc_gatt_defs);
+ if (rc != 0) {
+ return rc;
+ }
+
return 0;
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fb6b4804/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 9c645a9..5456cd4 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -28,6 +28,9 @@
#define BLE_GATTS_INCLUDE_SZ 6
#define BLE_GATTS_CHR_MAX_SZ 19
+static const struct ble_gatt_svc_def **ble_gatts_svc_defs;
+static int ble_gatts_num_svc_defs;
+
struct ble_gatts_svc_entry {
const struct ble_gatt_svc_def *svc;
uint16_t handle; /* 0 means unregistered. */
@@ -1010,22 +1013,26 @@ ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
{
int total_registered;
int cur_registered;
+ int num_svcs;
+ int idx;
int rc;
int i;
for (i = 0; svcs[i].type != BLE_GATT_SVC_TYPE_END; i++) {
- ble_gatts_svc_entries[i].svc = svcs + i;
- ble_gatts_svc_entries[i].handle = 0;
- ble_gatts_svc_entries[i].end_group_handle = 0xffff;
- }
- if (i > ble_hs_cfg.max_services) {
- return BLE_HS_ENOMEM;
- }
+ idx = ble_gatts_num_svc_entries + i;
+ if (idx >= ble_hs_cfg.max_services) {
+ return BLE_HS_ENOMEM;
+ }
- ble_gatts_num_svc_entries = i;
+ ble_gatts_svc_entries[idx].svc = svcs + i;
+ ble_gatts_svc_entries[idx].handle = 0;
+ ble_gatts_svc_entries[idx].end_group_handle = 0xffff;
+ }
+ num_svcs = i;
+ ble_gatts_num_svc_entries += num_svcs;
total_registered = 0;
- while (total_registered < ble_gatts_num_svc_entries) {
+ while (total_registered < num_svcs) {
rc = ble_gatts_register_round(&cur_registered, cb, cb_arg);
if (rc != 0) {
return rc;
@@ -1834,6 +1841,36 @@ ble_gatts_find_dsc(const void *svc_uuid128, const void *chr_uuid128,
}
/**
+ * Queues a set of service definitions for registration. All services queued
+ * in this manner get registered when ble_hs_init() is called.
+ *
+ * @param svcs An array of service definitions to queue for
+ * registration. This array must be
+ * terminated with an entry whose 'type'
+ * equals 0.
+ *
+ * @return 0 on success;
+ * BLE_HS_ENOMEM on heap exhaustion.
+ */
+int
+ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs)
+{
+ void *p;
+
+ p = realloc(ble_gatts_svc_defs,
+ (ble_gatts_num_svc_defs + 1) * sizeof *ble_gatts_svc_defs);
+ if (p == NULL) {
+ return BLE_HS_ENOMEM;
+ }
+
+ ble_gatts_svc_defs = p;
+ ble_gatts_svc_defs[ble_gatts_num_svc_defs] = svcs;
+ ble_gatts_num_svc_defs++;
+
+ return 0;
+}
+
+/**
* Accumulates counts of each resource type required by the specified service
* definition array. This function is generally used to calculate some host
* configuration values prior to initialization. This function adds the counts
@@ -1964,6 +2001,14 @@ ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
}
static void
+ble_gatts_free_svc_defs(void)
+{
+ free(ble_gatts_svc_defs);
+ ble_gatts_svc_defs = NULL;
+ ble_gatts_num_svc_defs = 0;
+}
+
+static void
ble_gatts_free_mem(void)
{
free(ble_gatts_clt_cfg_mem);
@@ -1977,6 +2022,7 @@ int
ble_gatts_init(void)
{
int rc;
+ int i;
ble_gatts_free_mem();
ble_gatts_num_cfgable_chrs = 0;
@@ -2001,6 +2047,16 @@ ble_gatts_init(void)
}
}
+ for (i = 0; i < ble_gatts_num_svc_defs; i++) {
+ rc = ble_gatts_register_svcs(ble_gatts_svc_defs[i],
+ ble_hs_cfg.gatts_register_cb,
+ ble_hs_cfg.gatts_register_arg);
+ if (rc != 0) {
+ goto err;
+ }
+ }
+ ble_gatts_free_svc_defs();
+
rc = stats_init_and_reg(
STATS_HDR(ble_gatts_stats), STATS_SIZE_INIT_PARMS(ble_gatts_stats,
STATS_SIZE_32), STATS_NAME_INIT_PARMS(ble_gatts_stats), "ble_gatts");
@@ -2013,5 +2069,6 @@ ble_gatts_init(void)
err:
ble_gatts_free_mem();
+ ble_gatts_free_svc_defs();
return rc;
}