• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
21 #include "host/ble_hs.h"
22 #include "nimble/nimble_port.h"
23 #if NIMBLE_CFG_CONTROLLER
24 #include "controller/ble_ll.h"
25 #endif
26 #include "store/config/ble_store_config.h"
27 
28 static struct ble_npl_eventq g_eventq_dflt ;
29 static struct ble_hs_stop_listener stop_listener;
30 static struct ble_npl_sem ble_hs_stop_sem;
31 static struct ble_npl_event ble_hs_ev_stop;
32 
33 #define NIMBLE_PORT_DEINIT_EV_ARG (-1)
34 
nimble_port_init(void)35 void nimble_port_init(void)
36 {
37 #if NIMBLE_CFG_CONTROLLER
38     void ble_hci_ram_init(void);
39 #endif
40     /* Initialize default event queue */
41     ble_npl_eventq_init(&g_eventq_dflt);
42     os_mempool_reset();
43     os_msys_init();
44     ble_hs_init();
45     /* XXX Need to have template for store */
46 #if MYNEWT_VAL(BLE_STORE_CONFIG_PERSIST)
47     ble_store_config_init();
48 #else
49     ble_store_ram_init();
50 #endif
51 #if NIMBLE_CFG_CONTROLLER
52     hal_timer_init(5, NULL); // 5:bytes
53     os_cputime_init(32768); // 32768:bytes
54     ble_ll_init();
55     ble_hci_ram_init();
56 #endif
57 }
58 
nimble_port_deinit(void)59 void nimble_port_deinit(void)
60 {
61     /* Deinitialize default event queue */
62     ble_npl_eventq_deinit(&g_eventq_dflt);
63     ble_hs_deinit();
64     os_msys_deinit();
65 #if MYNEWT_VAL(BLE_STORE_CONFIG_PERSIST)
66     ble_store_config_deinit();
67 #endif
68 }
69 
nimble_port_run(void)70 void nimble_port_run(void)
71 {
72     while (1) {
73         struct ble_npl_event *ev = ble_npl_eventq_get(&g_eventq_dflt, BLE_NPL_TIME_FOREVER);
74         ble_npl_event_run(ev);
75         int arg = (int)ble_npl_event_get_arg(ev);
76         if (arg == NIMBLE_PORT_DEINIT_EV_ARG) {
77             ; // break;
78         }
79     }
80 }
81 
82 /**
83  * Called when the host stop procedure has completed.
84  */
ble_hs_stop_cb(int status,void * arg)85 static void ble_hs_stop_cb(int status, void *arg)
86 {
87     ble_npl_sem_release(&ble_hs_stop_sem);
88 }
89 
nimble_port_stop_cb(struct ble_npl_event * ev)90 static void nimble_port_stop_cb(struct ble_npl_event *ev)
91 {
92     ble_npl_sem_release(&ble_hs_stop_sem);
93 }
94 
nimble_port_stop(void)95 int nimble_port_stop(void)
96 {
97     int rc;
98     ble_npl_sem_init(&ble_hs_stop_sem, 0);
99     /* Initiate a host stop procedure. */
100     rc = ble_hs_stop(&stop_listener, ble_hs_stop_cb,
101                      NULL);
102     if (rc != 0) {
103         ble_npl_sem_deinit(&ble_hs_stop_sem);
104         return rc;
105     }
106 
107     /* Wait till the host stop procedure is complete */
108     ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER);
109     ble_npl_event_init(&ble_hs_ev_stop, nimble_port_stop_cb,
110                        (void *)NIMBLE_PORT_DEINIT_EV_ARG);
111     ble_npl_eventq_put(&g_eventq_dflt, &ble_hs_ev_stop);
112     /* Wait till the event is serviced */
113     ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER);
114     ble_npl_sem_deinit(&ble_hs_stop_sem);
115 
116     /* Adding shutdown cb function, inform application free resources */
117     if (ble_hs_cfg.shutdown_cb != NULL) {
118         ble_hs_cfg.shutdown_cb(0);
119     }
120 
121     return rc;
122 }
123 
nimble_port_get_dflt_eventq(void)124 struct ble_npl_eventq *nimble_port_get_dflt_eventq(void)
125 {
126     return &g_eventq_dflt;
127 }
128 
129 #if NIMBLE_CFG_CONTROLLER
nimble_port_ll_task_func(void * arg)130 void nimble_port_ll_task_func(void *arg)
131 {
132     extern void ble_ll_task(void *);
133     ble_ll_task(arg);
134 }
135 #endif