Skip to content

Commit 940ee93

Browse files
maxikriexiaoxiang781216
authored andcommittedMar 26, 2025
examples: add blecent and bleprph similar to mynewt-nimble apps
These example apps create a BLE central and peripheral based on mynewt-nimBLE and work in companion. The apps are mostly based on their equivalents, which can be found at https://github.com/apache/mynewt-nimble/tree/master/apps Signed-off-by: Max Kriegleder <max.kriegleder@gmail.com>
1 parent 7b65b2b commit 940ee93

16 files changed

+3015
-0
lines changed
 
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ##############################################################################
2+
# apps/examples/nimble_blecent/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_EXAMPLES_NIMBLE_BLECENT)
22+
nuttx_add_application(
23+
NAME
24+
nimble
25+
SRCS
26+
${CMAKE_CURRENT_LIST_DIR}/nimble_blecent_main.c
27+
${CMAKE_CURRENT_LIST_DIR}/misc.c
28+
${CMAKE_CURRENT_LIST_DIR}/peer.c
29+
DEPENDS
30+
nimble)
31+
endif()

‎examples/nimble_blecent/Kconfig

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_NIMBLE_BLECENT
7+
tristate "NimBLE peripheral"
8+
default n
9+
---help---
10+
Enable the nimble peripheral
11+
12+
if EXAMPLES_NIMBLE_BLECENT
13+
14+
config EXAMPLES_NIMBLE_BLECENT_PROGNAME
15+
string "Program name"
16+
default "nimble"
17+
---help---
18+
This is the name of the program that will be used when the NSH ELF
19+
program is installed.
20+
21+
config EXAMPLES_NIMBLE_BLECENT_PRIORITY
22+
int "NimBLE task priority"
23+
default 100
24+
25+
config EXAMPLES_NIMBLE_BLECENT_STACKSIZE
26+
int "NimBLE stack size"
27+
default DEFAULT_TASK_STACKSIZE
28+
29+
endif # EXAMPLES_NIMBLE_BLECENT
30+

‎examples/nimble_blecent/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/nimble_blecent/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_EXAMPLES_NIMBLE_BLECENT),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/nimble
23+
endif

‎examples/nimble_blecent/Makefile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
############################################################################
2+
# apps/examples/nimble_blecent/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
# NimBLE built-in application info
24+
25+
PROGNAME = $(CONFIG_EXAMPLES_NIMBLE_BLECENT_PROGNAME)
26+
PRIORITY = $(CONFIG_EXAMPLES_NIMBLE_BLECENT_PRIORITY)
27+
STACKSIZE = $(CONFIG_EXAMPLES_NIMBLE_BLECENT_STACKSIZE)
28+
MODULE = $(CONFIG_EXAMPLES_NIMBLE_BLECENT)
29+
30+
# NimBLE Central Example
31+
32+
CSRCS += misc.c peer.c
33+
MAINSRC = nimble_blecent_main.c
34+
35+
include $(APPDIR)/wireless/bluetooth/nimble/Makefile.nimble
36+
37+
include $(APPDIR)/Application.mk
38+

‎examples/nimble_blecent/blecent.h

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/****************************************************************************
2+
* apps/examples/nimble_blecent/blecent.h
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
#ifndef H_BLECENT_
22+
#define H_BLECENT_
23+
24+
/****************************************************************************
25+
* Included Files
26+
****************************************************************************/
27+
28+
#include <stdio.h>
29+
30+
#ifdef __cplusplus
31+
extern "C"
32+
{
33+
#endif
34+
35+
struct ble_hs_adv_fields;
36+
struct ble_gap_conn_desc;
37+
struct ble_hs_cfg;
38+
union ble_store_value;
39+
union ble_store_key;
40+
41+
#define BLECENT_SVC_ALERT_UUID 0x1811
42+
#define BLECENT_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47
43+
#define BLECENT_CHR_NEW_ALERT 0x2A46
44+
#define BLECENT_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48
45+
#define BLECENT_CHR_UNR_ALERT_STAT_UUID 0x2A45
46+
#define BLECENT_CHR_ALERT_NOT_CTRL_PT 0x2A44
47+
48+
/* Misc. */
49+
50+
void print_bytes(const uint8_t *bytes, int len);
51+
void print_mbuf(const struct os_mbuf *om);
52+
char *addr_str(const void *addr);
53+
void print_uuid(const ble_uuid_t *uuid);
54+
void print_conn_desc(const struct ble_gap_conn_desc *desc);
55+
void print_adv_fields(const struct ble_hs_adv_fields *fields);
56+
57+
/* Peer. */
58+
59+
struct peer_dsc
60+
{
61+
SLIST_ENTRY(peer_dsc) next;
62+
struct ble_gatt_dsc dsc;
63+
};
64+
SLIST_HEAD(peer_dsc_list, peer_dsc);
65+
66+
struct peer_chr
67+
{
68+
SLIST_ENTRY(peer_chr) next;
69+
struct ble_gatt_chr chr;
70+
71+
struct peer_dsc_list dscs;
72+
};
73+
SLIST_HEAD(peer_chr_list, peer_chr);
74+
75+
struct peer_svc
76+
{
77+
SLIST_ENTRY(peer_svc) next;
78+
struct ble_gatt_svc svc;
79+
80+
struct peer_chr_list chrs;
81+
};
82+
SLIST_HEAD(peer_svc_list, peer_svc);
83+
84+
struct peer;
85+
typedef void peer_disc_fn(const struct peer *peer, int status, void *arg);
86+
87+
struct peer
88+
{
89+
SLIST_ENTRY(peer) next;
90+
91+
uint16_t conn_handle;
92+
93+
/* List of discovered GATT services. */
94+
95+
struct peer_svc_list svcs;
96+
97+
/* Keeps track of where we are in the service discovery process. */
98+
99+
uint16_t disc_prev_chr_val;
100+
struct peer_svc *cur_svc;
101+
102+
/* Callback that gets executed when service discovery completes. */
103+
104+
peer_disc_fn *disc_cb;
105+
void *disc_cb_arg;
106+
};
107+
108+
int peer_disc_all(uint16_t conn_handle, peer_disc_fn *disc_cb,
109+
void *disc_cb_arg);
110+
const struct peer_dsc *
111+
peer_dsc_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
112+
const ble_uuid_t *chr_uuid, const ble_uuid_t *dsc_uuid);
113+
const struct peer_chr *
114+
peer_chr_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
115+
const ble_uuid_t *chr_uuid);
116+
const struct peer_svc *
117+
peer_svc_find_uuid(const struct peer *peer, const ble_uuid_t *uuid);
118+
int peer_delete(uint16_t conn_handle);
119+
int peer_add(uint16_t conn_handle);
120+
int peer_init(int max_peers, int max_svcs, int max_chrs, int max_dscs);
121+
122+
#ifdef __cplusplus
123+
}
124+
#endif
125+
126+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.