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 #if MYNEWT
21
22 #include "os/mynewt.h"
23 #include "ble_hs_priv.h"
24
25 static struct ble_hs_stop_listener ble_hs_shutdown_stop_listener;
26
27 /**
28 * Called when the host stop procedure has completed.
29 */
30 static void
ble_hs_shutdown_stop_cb(int status,void * arg)31 ble_hs_shutdown_stop_cb(int status, void *arg)
32 {
33 SYSDOWN_ASSERT_ACTIVE();
34
35 /* Indicate to sysdown that the host is fully shut down. */
36 sysdown_release();
37 }
38
39 int
ble_hs_shutdown(int reason)40 ble_hs_shutdown(int reason)
41 {
42 int rc;
43
44 /* Ensure this function only gets called by sysdown. */
45 SYSDOWN_ASSERT_ACTIVE();
46
47 /* Initiate a host stop procedure. */
48 rc = ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb,
49 NULL);
50 switch (rc) {
51 case 0:
52 /* Stop initiated. Wait for result to be reported asynchronously. */
53 return SYSDOWN_IN_PROGRESS;
54
55 case BLE_HS_EBUSY:
56 /* Already stopping. Wait for result to be reported asynchronously. */
57 return SYSDOWN_IN_PROGRESS;
58
59 case BLE_HS_EALREADY:
60 /* Already stopped. Shutdown complete. */
61 return SYSDOWN_COMPLETE;
62
63 default:
64 BLE_HS_LOG(ERROR, "ble_hs_shutdown: failed to stop host; rc=%d\n", rc);
65 return SYSDOWN_COMPLETE;
66 }
67 }
68
69 #endif
70