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 "common/contextual_callback.h"
20 #include "l2cap/psm.h"
21 #include "os/handler.h"
22 #include "os/log.h"
23 
24 namespace bluetooth {
25 namespace l2cap {
26 namespace classic {
27 
28 namespace internal {
29 class DynamicChannelServiceManagerImpl;
30 }
31 
32 class DynamicChannelService {
33  public:
34   DynamicChannelService() = default;
35 
36   using OnUnregisteredCallback = common::ContextualOnceCallback<void()>;
37 
38   /**
39    * Unregister a service from L2CAP module. This operation cannot fail.
40    * All channels opened for this service will be closed.
41    *
42    * @param on_unregistered will be triggered when unregistration is complete
43    */
44   void Unregister(OnUnregisteredCallback on_unregistered);
45 
46   friend internal::DynamicChannelServiceManagerImpl;
47 
48   Psm GetPsm() const;
49 
50  protected:
DynamicChannelService(Psm psm,internal::DynamicChannelServiceManagerImpl * manager,os::Handler * handler)51   DynamicChannelService(Psm psm, internal::DynamicChannelServiceManagerImpl* manager, os::Handler* handler)
52       : psm_(psm), manager_(manager), l2cap_layer_handler_(handler) {
53     ASSERT(IsPsmValid(psm));
54     ASSERT(manager_ != nullptr);
55     ASSERT(l2cap_layer_handler_ != nullptr);
56   }
57 
58  private:
59   Psm psm_ = kDefaultPsm;
60   internal::DynamicChannelServiceManagerImpl* manager_ = nullptr;
61   os::Handler* l2cap_layer_handler_;
62   DISALLOW_COPY_AND_ASSIGN(DynamicChannelService);
63 };
64 
65 }  // namespace classic
66 }  // namespace l2cap
67 }  // namespace bluetooth
68