1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GATT_SERVICE_BASE_H
17 #define GATT_SERVICE_BASE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <stddef.h>
23 #include "buffer.h"
24 #include "gatt_defines.h"
25 
26 namespace OHOS {
27 namespace bluetooth {
28 class GattServiceBase {
29 public:
30     static const uint8_t MAXIMUM_NUMBER_APPLICATION;
31 
GattServiceBase()32     GattServiceBase() : runningState_(false), mutexRunningState_()
33     {}
~GattServiceBase()34     virtual ~GattServiceBase()
35     {}
InRunningState()36     bool InRunningState()
37     {
38         std::lock_guard<std::mutex> lck(mutexRunningState_);
39         return runningState_;
40     }
Start()41     void Start()
42     {
43         std::lock_guard<std::mutex> lck(mutexRunningState_);
44         runningState_ = true;
45     }
Stop()46     void Stop()
47     {
48         std::lock_guard<std::mutex> lck(mutexRunningState_);
49         runningState_ = false;
50     }
51 
52     static int ConvertConnectionState(const std::string &state);
53 
54     static Buffer *BuildBuffer(const uint8_t *value, size_t length);
55     static Buffer *BuildBuffer(const uint8_t *value, size_t offset, size_t length);
56 
57     static GattValue MoveToGattValue(std::unique_ptr<uint8_t[]> &value);
58     static GattValue BuildGattValue(const uint8_t *value, size_t length);
59     static int GetApplicationId();
60 
61 private:
62     bool runningState_;
63     std::mutex mutexRunningState_;
64 };
65 }  // namespace bluetooth
66 }  // namespace OHOS
67 
68 #endif  // !GATT_SERVICE_BASE_H
69