1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <bluetooth/log.h>
20 
21 #include "common/contextual_callback.h"
22 #include "l2cap/psm.h"
23 #include "os/handler.h"
24 #include "os/log.h"
25 
26 namespace bluetooth {
27 namespace l2cap {
28 namespace classic {
29 
30 namespace internal {
31 class DynamicChannelServiceManagerImpl;
32 }
33 
34 class DynamicChannelService {
35  public:
36   DynamicChannelService() = default;
37   DynamicChannelService(const DynamicChannelService&) = delete;
38   DynamicChannelService& operator=(const DynamicChannelService&) = delete;
39 
40   using OnUnregisteredCallback = common::ContextualOnceCallback<void()>;
41 
42   /**
43    * Unregister a service from L2CAP module. This operation cannot fail.
44    * All channels opened for this service will be closed.
45    *
46    * @param on_unregistered will be triggered when unregistration is complete
47    */
48   void Unregister(OnUnregisteredCallback on_unregistered);
49 
50   friend internal::DynamicChannelServiceManagerImpl;
51 
52   Psm GetPsm() const;
53 
54  protected:
DynamicChannelService(Psm psm,internal::DynamicChannelServiceManagerImpl * manager,os::Handler * handler)55   DynamicChannelService(Psm psm, internal::DynamicChannelServiceManagerImpl* manager, os::Handler* handler)
56       : psm_(psm), manager_(manager), l2cap_layer_handler_(handler) {
57     log::assert_that(IsPsmValid(psm), "assert failed: IsPsmValid(psm)");
58     log::assert_that(manager_ != nullptr, "assert failed: manager_ != nullptr");
59     log::assert_that(
60         l2cap_layer_handler_ != nullptr, "assert failed: l2cap_layer_handler_ != nullptr");
61   }
62 
63  private:
64   Psm psm_ = kDefaultPsm;
65   internal::DynamicChannelServiceManagerImpl* manager_ = nullptr;
66   os::Handler* l2cap_layer_handler_;
67 };
68 
69 }  // namespace classic
70 }  // namespace l2cap
71 }  // namespace bluetooth
72