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 OBEX_UTILS_H
17 #define OBEX_UTILS_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include "btstack.h"
22 #include "obex_headers.h"
23 #include "packet.h"
24 
25 namespace OHOS {
26 namespace bluetooth {
27 class ObexUtils {
28 public:
29     static bool SysIsBigEndian();
30     static void DataReverse(uint8_t *data, const uint16_t &len, const uint8_t &unitLen);
31 
32     static void SetBufData16(uint8_t *bigEndData, uint16_t pos, const uint16_t &value);
33     static void SetBufData32(uint8_t *bigEndData, uint16_t pos, const uint32_t &value);
34     static void SetBufData64(uint8_t *bigEndData, uint16_t pos, const uint64_t &value);
35     static uint16_t GetBufData16(const uint8_t *bigEndData, uint16_t pos = 0);
36     static uint32_t GetBufData32(const uint8_t *bigEndData, uint16_t pos = 0);
37     static uint64_t GetBufData64(const uint8_t *bigEndData, uint16_t pos = 0);
38     static std::string ToDebugString(Packet &obexPacket);
39     static std::string ToDebugString(const uint8_t *v, const size_t &s, bool wrap = false);
40     static void ObexHeaderDebug(const ObexHeader &header);
41     static std::string BtAddr2String(const BtAddr &addr);
42     static std::vector<uint8_t> MakeNonce(const uint32_t &privateKey);
43     static std::vector<uint8_t> MakeRequestDigest(const uint8_t *nonce, int sz, const std::string &password);
44     static std::string UnicodeToUtf8(const std::u16string &str16);
45     static std::u16string Utf8ToUnicode(const std::string &strUtf8);
46 
47 private:
48     static const uint16_t DEBUG_MAX_COL_COUNT = 10;
49     static const uint16_t DEBUG_COL_ITEM_LEN = 2;
50     static const size_t DEBUG_MAX_DATA_LEN = 100;
51     static std::mutex g_mutex;
52     static int g_bigEndian;
53     ObexUtils() = default;
54     virtual ~ObexUtils() = default;
55     static void ObexHeaderItemDebug(const ObexOptionalHeader &hi);
56 };
57 }  // namespace bluetooth
58 }  // namespace OHOS
59 #endif  // OBEX_UTILS_H