1/*
2 * Copyright (c) 2024 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 * Description: common method for cast session
15 * Author: lijianzhao
16 * Create: 2022-01-19
17 */
18
19#ifndef UTILS_H
20#define UTILS_H
21
22#include <locale>
23#include <string>
24#include <vector>
25
26namespace OHOS {
27namespace CastEngine {
28namespace CastEngineService {
29#if !defined(DISALLOW_COPY_AND_ASSIGN)
30#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
31    TypeName(const TypeName &);            \
32    void operator = (const TypeName &)
33#endif
34
35#if !defined(DISALLOW_EVIL_CONSTRUCTORS)
36#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
37#endif
38
39#if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS)
40#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
41    TypeName();                                  \
42    DISALLOW_COPY_AND_ASSIGN(TypeName)
43#endif
44
45struct PacketData {
46    uint8_t *data;
47    int length;
48};
49
50struct ConstPacketData {
51    const uint8_t *data;
52    int length;
53};
54
55class Utils {
56public:
57    Utils() {}
58    ~Utils() {}
59    static void SplitString(const std::string &src, std::vector<std::string> &dest, const std::string &seperator);
60    static std::string &Trim(std::string &str);
61    static std::string &ToLower(std::string &str);
62    static bool StartWith(const std::string &mainStr, const std::string &subStr);
63    static int IntToByteArray(int num, int length, uint8_t *output);
64    static uint32_t ByteArrayToInt(const uint8_t *input, unsigned int length);
65    static int32_t StringToInt(const std::string &str);
66    static std::string GetWifiIp();
67    static bool IsArrayAllZero(const uint8_t *input, int length);
68
69    static bool Base64Encode(const std::string &source, std::string &encoded);
70    static bool Base64Decode(const std::string &encoded, std::string &decoded);
71
72private:
73    static const int BYTE_TO_BIT_OFFSET = 8;
74    static const uint8_t INT_TO_BYTE = 0xff;
75    static const std::string BASE64_CHARS;
76    static inline bool IsBase64(unsigned char c);
77    static const int DECIMALISM = 10;
78    static std::string ConvertIpv4Address(unsigned int addressIpv4);
79};
80
81inline constexpr char SANDBOX_PATH[] = "/data/service/el1/public/cast_engine_service";
82inline constexpr char PARAM_MEDIA_DUMP[] = "debug.cast.media.dump";
83inline constexpr char PARAM_VIDEO_CACHE[] = "debug.cast.video.cache";
84inline constexpr char UNIQUE_SCREEN[] = "debug.cast.unique.screen";
85inline constexpr char NOTIFY_DEVICE_FOUND[] = "debug.cast.device.found";
86} // namespace CastEngineService
87} // namespace CastEngine
88} // namespace OHOS
89
90#endif
91