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