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 #include "gatt_service_base.h"
17 #include <random>
18 #include "gatt_connection_manager.h"
19 #include "interface_profile.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace bluetooth {
24 const uint8_t GattServiceBase::MAXIMUM_NUMBER_APPLICATION = 0xFF;
25
ConvertConnectionState(const std::string & state)26 int GattServiceBase::ConvertConnectionState(const std::string &state)
27 {
28 BTConnectState newState = BTConnectState::DISCONNECTED;
29 if (state.compare(GattConnectionManager::Device::STATE_CONNECTING) == 0) {
30 newState = BTConnectState::CONNECTING;
31 } else if (state.compare(GattConnectionManager::Device::STATE_CONNECTED) == 0) {
32 newState = BTConnectState::CONNECTED;
33 } else if (state.compare(GattConnectionManager::Device::STATE_DISCONNECTING) == 0) {
34 newState = BTConnectState::DISCONNECTING;
35 }
36
37 return static_cast<int>(newState);
38 }
39
BuildBuffer(const uint8_t * value,size_t length)40 Buffer *GattServiceBase::BuildBuffer(const uint8_t *value, size_t length)
41 {
42 Buffer *result = BufferMalloc(length);
43 if (result != nullptr) {
44 (void)memcpy_s(BufferPtr(result), length, value, length);
45 }
46
47 return result;
48 }
49
BuildBuffer(const uint8_t * value,size_t offset,size_t length)50 Buffer *GattServiceBase::BuildBuffer(const uint8_t *value, size_t offset, size_t length)
51 {
52 Buffer *result = BufferMalloc(length);
53 if (result != nullptr) {
54 (void)memcpy_s(BufferPtr(result), length, value + offset, length);
55 }
56
57 return result;
58 }
59
MoveToGattValue(std::unique_ptr<uint8_t[]> & value)60 GattValue GattServiceBase::MoveToGattValue(std::unique_ptr<uint8_t[]> &value)
61 {
62 std::shared_ptr<std::unique_ptr<uint8_t[]>> sharedPtr
63 = std::make_shared<std::unique_ptr<uint8_t[]>>(std::move(value));
64 return sharedPtr;
65 }
66
BuildGattValue(const uint8_t * value,size_t length)67 GattValue GattServiceBase::BuildGattValue(const uint8_t *value, size_t length)
68 {
69 std::shared_ptr<std::unique_ptr<uint8_t[]>> sharedPtr =
70 std::make_shared<std::unique_ptr<uint8_t[]>>(std::make_unique<uint8_t[]>(length));
71 (void)memcpy_s(sharedPtr->get(), length, value, length);
72 return sharedPtr;
73 }
74
GetApplicationId()75 int GattServiceBase::GetApplicationId()
76 {
77 // The random number is used for the application of GATT service internal identification registration,
78 // and does not involve security calculations.
79 std::mt19937 generator(std::chrono::system_clock::now().time_since_epoch().count());
80 std::uniform_int_distribution<int> distribution(0x01);
81
82 return distribution(generator);
83 }
84 } // namespace bluetooth
85 } // namespace OHOS
86