1 /* 2 * Copyright (c) 2025-2026 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 NRUNSOLICITEDMSGPARSER_H 17 #define NRUNSOLICITEDMSGPARSER_H 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 #include <any> 23 #include <memory> 24 #include <unordered_map> 25 #include <fstream> 26 #include <filesystem> 27 #include <iostream> 28 #include <utility> 29 #include <mutex> 30 #include "hwnetworkslicemanager.h" 31 #include "urspconfig.h" 32 #include "networksliceutil.h" 33 34 namespace OHOS { 35 namespace NetManagerStandard { 36 static constexpr int PLMN_LEN = 3; 37 static constexpr int RADIX = 10; 38 static constexpr short RESULT_LEN = 5; 39 static constexpr int CONVERT_INT_AND_BYTE = 0x000000ff; 40 static constexpr int CONVERT_INT_AND_SHORT = 0x0000ffff; 41 static constexpr int ARRAY_INDEX_0 = 0; 42 static constexpr int ARRAY_INDEX_1 = 1; 43 static constexpr int ARRAY_INDEX_2 = 2; 44 static constexpr uint8_t CAUSE_PROTOCOL_ERROR = 0x6F; 45 46 class PolicyInstruction { 47 public: 48 bool hasUrspType; 49 std::vector<UrspRule> urspRules; 50 to_json()51 nlohmann::json to_json() const 52 { 53 nlohmann::json policyinstructionJson = nlohmann::json::object({ 54 {"hasUrspType", hasUrspType}, 55 }); 56 for (const auto& urspRule : urspRules) { 57 policyinstructionJson["urspRules"].push_back(urspRule.to_json()); 58 } 59 return policyinstructionJson; 60 } 61 from_json(const nlohmann::json & jsonPolicyInstruction)62 void from_json(const nlohmann::json& jsonPolicyInstruction) 63 { 64 if (jsonPolicyInstruction.find("hasUrspType") != jsonPolicyInstruction.end()) { 65 this->hasUrspType = jsonPolicyInstruction.at("hasUrspType").get<bool>(); 66 } 67 68 if (jsonPolicyInstruction.find("urspRules") != jsonPolicyInstruction.end()) { 69 const nlohmann::json& urspRulesJson = jsonPolicyInstruction.at("urspRules"); 70 this->urspRules.clear(); 71 for (const auto& ruleJson : urspRulesJson) { 72 UrspRule rule; 73 rule.from_json(ruleJson); 74 this->urspRules.push_back(rule); 75 } 76 } 77 } 78 }; 79 80 class UePolicy { 81 public: 82 std::vector<uint8_t> plmn = std::vector<uint8_t>(PLMN_LEN); 83 /** 84 * instruction order and upsc HashMap 85 */ 86 std::unordered_map<short, short> instructionOrderMap; 87 /** 88 * upsc and PolicyInstruction HashMap 89 */ 90 std::unordered_map<short, PolicyInstruction> policyInstructionMap; 91 to_json()92 nlohmann::json to_json() const 93 { 94 nlohmann::json uePolicyJson = nlohmann::json::object({ 95 {"plmn", plmn}, 96 {"instructionOrderMap", nlohmann::json::object()} 97 }); 98 for (const auto& item : instructionOrderMap) { 99 uePolicyJson["instructionOrderMap"][std::to_string(item.first)] = item.second; 100 } 101 nlohmann::json policyInstructionMapJson; 102 for (const auto& item : policyInstructionMap) { 103 NETMGR_EXT_LOG_I("uePolicyJson policyInstructionMap key = %{public}d", item.first); 104 policyInstructionMapJson[std::to_string(item.first)] = item.second.to_json(); 105 } 106 uePolicyJson["policyInstructionMap"] = policyInstructionMapJson; 107 return uePolicyJson; 108 } 109 from_json(const nlohmann::json & jsonPolicy)110 void from_json(const nlohmann::json& jsonPolicy) 111 { 112 if (jsonPolicy.find("plmn") != jsonPolicy.end()) { 113 const nlohmann::json& plmnJson = jsonPolicy.at("plmn"); 114 this->plmn.resize(plmnJson.size()); 115 NETMGR_EXT_LOG_I("UePolicy plmn size = %{public}d", (int)plmnJson.size()); 116 for (size_t i = 0; i < plmnJson.size(); ++i) { 117 NETMGR_EXT_LOG_I("UePolicy plmn[%{public}d] = %{public}d", (int)i, (int)plmnJson[i]); 118 this->plmn[i] = plmnJson[i]; 119 } 120 } 121 122 if (jsonPolicy.find("instructionOrderMap") != jsonPolicy.end()) { 123 const nlohmann::json& instructionOrderMapJson = jsonPolicy.at("instructionOrderMap"); 124 this->instructionOrderMap.clear(); 125 for (const auto& item : instructionOrderMapJson.items()) { 126 short key = std::stoi(item.key()); 127 short value = item.value(); 128 this->instructionOrderMap[key] = value; 129 NETMGR_EXT_LOG_I("UePolicy instructionOrderMap = [%{public}d, %{public}d]", 130 key, value); 131 } 132 } 133 134 if (jsonPolicy.find("policyInstructionMap") != jsonPolicy.end()) { 135 const nlohmann::json& policyInstructionMapJson = jsonPolicy.at("policyInstructionMap"); 136 this->policyInstructionMap.clear(); 137 for (const auto& item : policyInstructionMapJson.items()) { 138 short key = std::stoi(item.key()); 139 NETMGR_EXT_LOG_I("UePolicy policyInstructionMap = %{public}d", key); 140 PolicyInstruction value; 141 value.from_json(item.value()); 142 this->policyInstructionMap[key] = value; 143 } 144 } 145 } 146 }; 147 148 class MultipleBuffer { 149 public: 150 uint8_t segmentNum; 151 int totalBufferLen; 152 std::unordered_map<uint8_t, std::vector<uint8_t>> bufferSegmentMap; 153 }; 154 155 class UePolicyResult { 156 public: 157 short upsc; 158 short failedInstructionOrder; 159 uint8_t cause; 160 }; 161 162 class UePolicyReject { 163 public: 164 std::vector<uint8_t> plmn; 165 std::vector<UePolicyResult> uePolicyResult; 166 }; 167 168 class UePolicyRejectMsg { 169 public: 170 short totalLen; 171 std::vector<UePolicyReject> uePolicyRejects; 172 }; 173 174 class NrUnsolicitedMsgParser { 175 public: 176 static NrUnsolicitedMsgParser& GetInstance(); 177 ~NrUnsolicitedMsgParser() = default; 178 std::string GetHplmn(); 179 private: 180 NrUnsolicitedMsgParser(); 181 std::vector<std::string> mEhplmns; 182 }; 183 184 } // namespace NetManagerStandard 185 } // namespace OHOS 186 187 188 #endif // NRUNSOLICITEDMSGPARSER_H 189