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 OHOS_SIM_DIALLING_NUMBERS_HANDLER_H 17 #define OHOS_SIM_DIALLING_NUMBERS_HANDLER_H 18 19 #include <unordered_map> 20 #include <thread> 21 22 #include "event_handler.h" 23 #include "event_runner.h" 24 #include "dialling_numbers_info.h" 25 #include "icc_file_controller.h" 26 #include "sim_data_type.h" 27 #include "sim_number_decode.h" 28 #include "sim_utils.h" 29 30 namespace OHOS { 31 namespace Telephony { 32 enum { 33 MSG_SIM_OBTAIN_ADN_DONE = 1, 34 MSG_SIM_OBTAIN_ALL_ADN_DONE = 2, 35 MSG_SIM_OBTAIN_LINEAR_FILE_SIZE_DONE = 3, 36 MSG_SIM_RENEW_ADN_DONE = 4 37 }; 38 39 enum { 40 // 3gpp ts51.011 10.5.1 41 BCD_NUMBER_BYTES = 0, 42 TON_NPI_NUMBER = 1, 43 DIALING_NUMBERS_BEGIN = 2, 44 DIALING_NUMBERS_END = 11, 45 MORE_FILE_ID = 12, 46 EXTRA_FILE_ID = 13 47 }; 48 49 enum { 50 MORE_FILE_TYPE_DATA = 0x02, 51 MORE_FILE_FLAG = 0x03, 52 MAX_MORE_PARTY_LENGTH = 0xa 53 }; 54 55 struct DiallingNumberLoadRequest { 56 public: DiallingNumberLoadRequestDiallingNumberLoadRequest57 DiallingNumberLoadRequest(int serialId, int fileId, int exId, int indexNum, const std::string &pin2Str, 58 const AppExecFwk::InnerEvent::Pointer &pointer) : elementaryFileId(fileId), extFileId(exId), 59 pin2(pin2Str), index(indexNum), loadId(serialId) 60 { 61 callPointer = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(pointer)); 62 } SetResultDiallingNumberLoadRequest63 void SetResult(std::shared_ptr<void> result) 64 { 65 this->result = result; 66 } GetResultDiallingNumberLoadRequest67 std::shared_ptr<void> GetResult() 68 { 69 return this->result; 70 } 71 SetExceptionDiallingNumberLoadRequest72 void SetException(std::shared_ptr<void> exception) 73 { 74 this->exception = exception; 75 } 76 GetExceptionDiallingNumberLoadRequest77 std::shared_ptr<void> GetException() 78 { 79 return this->exception; 80 } 81 SetElementaryFileIdDiallingNumberLoadRequest82 void SetElementaryFileId(int id) 83 { 84 this->elementaryFileId = id; 85 } 86 GetElementaryFileIdDiallingNumberLoadRequest87 int GetElementaryFileId() 88 { 89 return this->elementaryFileId; 90 } 91 SetExEFDiallingNumberLoadRequest92 void SetExEF(int ef) 93 { 94 this->extFileId = ef; 95 } 96 GetExEFDiallingNumberLoadRequest97 int GetExEF() 98 { 99 return this->extFileId; 100 } 101 SetIndexDiallingNumberLoadRequest102 void SetIndex(int index) 103 { 104 this->index = index; 105 } 106 GetIndexDiallingNumberLoadRequest107 int GetIndex() 108 { 109 return this->index; 110 } 111 SetPin2DiallingNumberLoadRequest112 void SetPin2(std::string pin2Code) 113 { 114 this->pin2 = pin2Code; 115 } 116 GetPin2DiallingNumberLoadRequest117 std::string GetPin2() 118 { 119 return this->pin2; 120 } 121 SetIsDeleteDiallingNumberLoadRequest122 void SetIsDelete(bool del) 123 { 124 this->isDelete = del; 125 } 126 GetIsDeleteDiallingNumberLoadRequest127 bool GetIsDelete() 128 { 129 return this->isDelete; 130 } 131 SetLoadIdDiallingNumberLoadRequest132 void SetLoadId(int id) 133 { 134 this->loadId = id; 135 } 136 GetLoadIdDiallingNumberLoadRequest137 int GetLoadId() 138 { 139 return this->loadId; 140 } GetCallerDiallingNumberLoadRequest141 AppExecFwk::InnerEvent::Pointer &GetCaller() 142 { 143 return callPointer; 144 } HasCountDiallingNumberLoadRequest145 bool HasCount() 146 { 147 return moreFileToGet != 0; 148 } InitCountDiallingNumberLoadRequest149 void InitCount() 150 { 151 moreFileToGet = INIT_COUNT; 152 } CountUpDiallingNumberLoadRequest153 void CountUp() 154 { 155 ++moreFileToGet; 156 } CountDownDiallingNumberLoadRequest157 void CountDown() 158 { 159 --moreFileToGet; 160 } ClearCountDiallingNumberLoadRequest161 void ClearCount() 162 { 163 moreFileToGet = 0; 164 } 165 private: 166 int elementaryFileId = 0; 167 int extFileId = 0; 168 int moreFileToGet = 0; 169 std::string pin2 = ""; 170 int index = 0; 171 int loadId = 0; 172 bool isDelete = false; 173 std::shared_ptr<void> result = nullptr; 174 std::shared_ptr<void> exception = nullptr; 175 AppExecFwk::InnerEvent::Pointer &callPointer = nullptr_; 176 AppExecFwk::InnerEvent::Pointer nullptr_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr); 177 const int INIT_COUNT = 1; 178 }; 179 struct DiallingNumberUpdateInfor { 180 std::shared_ptr<DiallingNumbersInfo> diallingNumber = nullptr; 181 int index = 0; 182 int fileId = 0; 183 int extFile = 0; 184 std::string pin2 = ""; 185 bool isDel = false; 186 }; 187 class IccDiallingNumbersHandler : public AppExecFwk::EventHandler { 188 public: 189 IccDiallingNumbersHandler( 190 const std::shared_ptr<AppExecFwk::EventRunner> &runner, std::shared_ptr<IccFileController> fh); 191 ~IccDiallingNumbersHandler(); 192 void GetDiallingNumbers(int ef, int extensionEF, int recordNumber, AppExecFwk::InnerEvent::Pointer &response); 193 void GetAllDiallingNumbers(int ef, int extensionEF, AppExecFwk::InnerEvent::Pointer &response); 194 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); 195 void UpdateDiallingNumbers(const DiallingNumberUpdateInfor &infor, AppExecFwk::InnerEvent::Pointer &response); GetNextSerialId()196 static int GetNextSerialId() 197 { 198 return nextSerialId_++; 199 } 200 static std::atomic_int nextSerialId_; 201 static std::unordered_map<int, std::shared_ptr<DiallingNumberLoadRequest>> requestMap_; 202 static std::shared_ptr<DiallingNumberLoadRequest> FindLoadRequest(int serial); 203 static void ClearLoadRequest(int serial); 204 static std::shared_ptr<DiallingNumberLoadRequest> CreateLoadRequest(int fileId, 205 int exId, int indexNum, const std::string &pin2Str, const AppExecFwk::InnerEvent::Pointer &result); 206 void UpdateFileController(const std::shared_ptr<IccFileController> &fileController); 207 208 protected: 209 std::shared_ptr<IccFileController> fileController_; 210 std::string GetFilePath(int elementaryFileId); 211 // 3GPP TS 51.011 V4.1.0 section 10.7 files of gsm 212 const std::string MASTER_FILE_SIM = "3F00"; 213 const std::string DEDICATED_FILE_TELECOM = "7F10"; 214 215 private: 216 using ProcessFunc = void (IccDiallingNumbersHandler::*)(const AppExecFwk::InnerEvent::Pointer &event, int &id); 217 std::map<int, ProcessFunc> memberFuncMap_; 218 AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, int loadId); 219 AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, std::shared_ptr<void> pobj, int loadId); 220 void ProcessDiallingNumberAllLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 221 void ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 222 void ProcessLinearSizeDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 223 void ProcessUpdateRecordDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 224 bool SendBackResult(int loadId); 225 void FetchDiallingNumberContent( 226 const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, const std::string &recordData); 227 std::shared_ptr<unsigned char> CreateSavingSequence( 228 const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int dataLength); 229 std::shared_ptr<unsigned char> CreateNameSequence(const std::u16string &name, int &seqLength); 230 std::shared_ptr<HRilRadioResponseInfo> MakeExceptionResult(int code); 231 void FillNumberFiledForDiallingNumber( 232 std::shared_ptr<unsigned char> diallingNumber, const std::string &number, int dataLength); 233 bool FormatNameAndNumber(std::shared_ptr<DiallingNumbersInfo> &diallingNumber, bool isDel); 234 void SendUpdateCommand(const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int length, 235 const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, int loadId); 236 void InitFuncMap(); 237 const int RECORD_LENGTH = 28; 238 const int LENGTH_RATE = 2; 239 const int INVALID_LENGTH = 49; 240 const static int32_t PRE_BYTES_NUM = 14; 241 const static int32_t MAX_NUMBER_SIZE_BYTES = 11; 242 const static int32_t EXT_FILE_BITYES_NUM = 13; 243 }; 244 } // namespace Telephony 245 } // namespace OHOS 246 247 #endif // OHOS_SIM_DIALLING_NUMBERS_HANDLER_H 248