1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_UTILS_H 17 #define OHOS_SHARING_UTILS_H 18 19 #include <regex> 20 #include <string> 21 #include <cstring> 22 #include <vector> 23 #include <net/if.h> 24 #include <arpa/inet.h> 25 #include <sys/socket.h> 26 #include <sys/ioctl.h> 27 #include <unistd.h> 28 29 namespace OHOS { 30 namespace Sharing { 31 32 class RpcKeyParser { 33 public: 34 RpcKeyParser() = default; 35 GetRpcKey(std::string bundleName,std::string abilityName,std::string deviceId,std::string className)36 std::string GetRpcKey(std::string bundleName, std::string abilityName, std::string deviceId, std::string className) 37 38 { 39 bundleName_ = std::move(bundleName); 40 abilityName_ = std::move(abilityName); 41 deviceId_ = std::move(deviceId); 42 className_ = std::move(className); 43 return bundleName_ + "+" + abilityName_ + "+" + deviceId_ + "+" + className_; 44 } 45 ParseKey(std::string key)46 bool ParseKey(std::string key) 47 { 48 std::regex reg{"\\+"}; 49 std::vector<std::string> out{std::sregex_token_iterator(key.begin(), key.end(), reg, -1), 50 std::sregex_token_iterator()}; 51 if (out.size() != 4) { // 4: rpckey length 52 return false; 53 } 54 bundleName_ = std::move(out[0]); 55 abilityName_ = std::move(out[1]); 56 deviceId_ = std::move(out[2]); // 2: rpckey parse 57 className_ = std::move(out[3]); // 3: rpckey parse 58 return true; 59 } 60 GetBundleName()61 std::string GetBundleName() 62 { 63 return bundleName_; 64 } 65 GetAbilityName()66 std::string GetAbilityName() 67 { 68 return abilityName_; 69 } 70 GetDeviceId()71 std::string GetDeviceId() 72 { 73 return deviceId_; 74 } 75 GetClassName()76 std::string GetClassName() 77 { 78 return className_; 79 } 80 81 private: 82 std::string deviceId_; 83 std::string className_; 84 std::string bundleName_; 85 std::string abilityName_; 86 }; 87 88 unsigned long long GetThreadId(); 89 uint64_t GetCurrentMillisecond(); 90 91 std::string ChangeCase(const std::string &value, bool lowerCase); 92 std::string Trim(std::string &&s, const std::string &chars = " \r\n\t"); 93 std::string &Trim(std::string &s, const std::string &chars = " \r\n\t"); 94 95 std::vector<std::string> Split(const std::string &s, const char *delim); 96 std::vector<std::string> SplitOnce(const std::string &s, const char *delim); 97 98 uint16_t SwapEndian16(uint16_t value); 99 uint32_t SwapEndian32(uint32_t value); 100 uint64_t SwapEndian64(uint64_t value); 101 102 uint16_t LoadBE16(const uint8_t *p); 103 uint32_t LoadBE24(const uint8_t *p); 104 uint32_t LoadBE32(const uint8_t *p); 105 uint64_t LoadBE64(const uint8_t *p); 106 107 void SetBE24(void *p, uint32_t val); 108 void SetBE32(void *p, uint32_t val); 109 void SetLE32(void *p, uint32_t val); 110 111 std::string GetAnonyString(const std::string &value); 112 std::string GetLocalP2pAddress(const std::string &interface); 113 } // namespace Sharing 114 } // namespace OHOS 115 #endif