xref: /nrf52832-nimble/packages/NimBLE-latest/apps/blemesh_shell/src/main.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <assert.h>
21 #include "os/mynewt.h"
22 #include "mesh/mesh.h"
23 #include "console/console.h"
24 #include "hal/hal_system.h"
25 #include "hal/hal_gpio.h"
26 #include "bsp/bsp.h"
27 #include "shell/shell.h"
28 
29 /* BLE */
30 #include "nimble/ble.h"
31 #include "host/ble_hs.h"
32 #include "services/gap/ble_svc_gap.h"
33 #include "mesh/glue.h"
34 #include "mesh/testing.h"
35 
36 
net_recv_ev(uint8_t ttl,uint8_t ctl,uint16_t src,uint16_t dst,const void * payload,size_t payload_len)37 void net_recv_ev(uint8_t ttl, uint8_t ctl, uint16_t src, uint16_t dst,
38                  const void *payload, size_t payload_len)
39 {
40     console_printf("Received net packet: ttl 0x%02x ctl 0x%02x src 0x%04x "
41                    "dst 0x%04x " "payload_len %d\n", ttl, ctl, src, dst,
42                    payload_len);
43 }
44 
model_bound_cb(u16_t addr,struct bt_mesh_model * model,u16_t key_idx)45 static void model_bound_cb(u16_t addr, struct bt_mesh_model *model,
46                            u16_t key_idx)
47 {
48     console_printf("Model bound: remote addr 0x%04x key_idx 0x%04x model %p\n",
49                    addr, key_idx, model);
50 }
51 
model_unbound_cb(u16_t addr,struct bt_mesh_model * model,u16_t key_idx)52 static void model_unbound_cb(u16_t addr, struct bt_mesh_model *model,
53                              u16_t key_idx)
54 {
55     console_printf("Model unbound: remote addr 0x%04x key_idx 0x%04x "
56                    "model %p\n", addr, key_idx, model);
57 }
58 
invalid_bearer_cb(u8_t opcode)59 static void invalid_bearer_cb(u8_t opcode)
60 {
61     console_printf("Invalid bearer: opcode 0x%02x\n", opcode);
62 }
63 
incomp_timer_exp_cb(void)64 static void incomp_timer_exp_cb(void)
65 {
66     console_printf("Incomplete timer expired\n");
67 }
68 
69 static struct bt_test_cb bt_test_cb = {
70     .mesh_net_recv = net_recv_ev,
71     .mesh_model_bound = model_bound_cb,
72     .mesh_model_unbound = model_unbound_cb,
73     .mesh_prov_invalid_bearer = invalid_bearer_cb,
74     .mesh_trans_incomp_timer_exp = incomp_timer_exp_cb,
75 };
76 
77 static void
blemesh_on_reset(int reason)78 blemesh_on_reset(int reason)
79 {
80     BLE_HS_LOG(ERROR, "Resetting state; reason=%d\n", reason);
81 }
82 
83 static void
blemesh_on_sync(void)84 blemesh_on_sync(void)
85 {
86     console_printf("Bluetooth initialized\n");
87 
88     shell_register_default_module("mesh");
89 
90     if (IS_ENABLED(CONFIG_BT_TESTING)) {
91         bt_test_cb_register(&bt_test_cb);
92     }
93 }
94 
95 int
main(void)96 main(void)
97 {
98     /* Initialize OS */
99     sysinit();
100 
101     /* Initialize the NimBLE host configuration. */
102     ble_hs_cfg.reset_cb = blemesh_on_reset;
103     ble_hs_cfg.sync_cb = blemesh_on_sync;
104     ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
105 
106     hal_gpio_init_out(LED_2, 0);
107 
108     while (1) {
109         os_eventq_run(os_eventq_dflt_get());
110     }
111     return 0;
112 }
113