xref: /nrf52832-nimble/packages/NimBLE-latest/apps/blemesh_light/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 #include <assert.h>
20 #include "sysinit/sysinit.h"
21 #include "os/os.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 #include "mesh/model_srv.h"
36 #include "light_model.h"
37 
38 
model_bound_cb(u16_t addr,struct bt_mesh_model * model,u16_t key_idx)39 static void model_bound_cb(u16_t addr, struct bt_mesh_model *model,
40                            u16_t key_idx)
41 {
42     int rc;
43 
44     console_printf("Model bound: remote addr 0x%04x key_idx 0x%04x model %p\n",
45                    addr, key_idx, model);
46 
47     if (model->id != BT_MESH_MODEL_ID_GEN_LEVEL_SRV) {
48         return;
49     }
50 
51     /* Hack for demo purposes */
52     rc = bt_test_bind_app_key_to_model(model, key_idx,
53                                        BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV);
54 
55     if (rc) {
56         console_printf("Failed to bind light lightness srv model to app_key");
57     } else {
58         console_printf("Successfuly bound light lightness srv model to app_key");
59     }
60 }
61 
62 static struct bt_test_cb bt_test_cb = {
63     .mesh_model_bound = model_bound_cb,
64 };
65 
66 static void
blemesh_on_reset(int reason)67 blemesh_on_reset(int reason)
68 {
69     BLE_HS_LOG(ERROR, "Resetting state; reason=%d\n", reason);
70 }
71 
72 static struct bt_mesh_gen_onoff_srv_cb gen_onoff_srv_cb = {
73         .get = light_model_gen_onoff_get,
74         .set = light_model_gen_onoff_set,
75 };
76 static struct bt_mesh_gen_level_srv_cb gen_level_srv_cb = {
77         .get = light_model_gen_level_get,
78         .set = light_model_gen_level_set,
79 };
80 static struct bt_mesh_light_lightness_srv_cb light_lightness_srv_cb = {
81         .get = light_model_light_lightness_get,
82         .set = light_model_light_lightness_set,
83 };
84 
85 static void
blemesh_on_sync(void)86 blemesh_on_sync(void)
87 {
88     console_printf("Bluetooth initialized\n");
89 
90     shell_register_default_module("mesh");
91 
92     bt_test_cb_register(&bt_test_cb);
93 
94     light_model_init();
95     bt_mesh_set_gen_onoff_srv_cb(&gen_onoff_srv_cb);
96     bt_mesh_set_gen_level_srv_cb(&gen_level_srv_cb);
97     bt_mesh_set_light_lightness_srv_cb(&light_lightness_srv_cb);
98 
99     console_printf("Mesh initialized\n");
100 
101     if (IS_ENABLED(CONFIG_SETTINGS)) {
102         settings_load();
103     }
104 
105     if (bt_mesh_is_provisioned()) {
106         printk("Mesh network restored from flash\n");
107     }
108 
109     /* Hack for demo purposes */
110     bt_test_shell_init();
111 }
112 
113 int
main(void)114 main(void)
115 {
116     /* Initialize OS */
117     sysinit();
118 
119     /* Initialize the NimBLE host configuration. */
120     ble_hs_cfg.reset_cb = blemesh_on_reset;
121     ble_hs_cfg.sync_cb = blemesh_on_sync;
122     ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
123 
124     while (1) {
125         os_eventq_run(os_eventq_dflt_get());
126     }
127     return 0;
128 }
129