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 166 private: 167 int elementaryFileId = 0; 168 int extFileId = 0; 169 int moreFileToGet = 0; 170 std::string pin2 = ""; 171 int index = 0; 172 int loadId = 0; 173 bool isDelete = false; 174 std::shared_ptr<void> result = nullptr; 175 std::shared_ptr<void> exception = nullptr; 176 AppExecFwk::InnerEvent::Pointer &callPointer = nullptr_; 177 AppExecFwk::InnerEvent::Pointer nullptr_ = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr); 178 const int INIT_COUNT = 1; 179 }; 180 struct DiallingNumberUpdateInfor { 181 std::shared_ptr<DiallingNumbersInfo> diallingNumber = nullptr; 182 int index = 0; 183 int fileId = 0; 184 int extFile = 0; 185 std::string pin2 = ""; 186 bool isDel = false; 187 }; 188 class IccDiallingNumbersHandler : public AppExecFwk::EventHandler { 189 public: 190 IccDiallingNumbersHandler( 191 const std::shared_ptr<AppExecFwk::EventRunner> &runner, std::shared_ptr<IccFileController> fh); 192 ~IccDiallingNumbersHandler(); 193 void GetDiallingNumbers(int ef, int extensionEF, int recordNumber, AppExecFwk::InnerEvent::Pointer &response); 194 void GetAllDiallingNumbers(int ef, int extensionEF, AppExecFwk::InnerEvent::Pointer &response); 195 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); 196 void UpdateDiallingNumbers(const DiallingNumberUpdateInfor &infor, AppExecFwk::InnerEvent::Pointer &response); GetNextSerialId()197 static int GetNextSerialId() 198 { 199 return nextSerialId_++; 200 } 201 static std::atomic_int nextSerialId_; 202 static std::unordered_map<int, std::shared_ptr<DiallingNumberLoadRequest>> requestMap_; 203 static std::shared_ptr<DiallingNumberLoadRequest> FindLoadRequest(int serial); 204 static void ClearLoadRequest(int serial); 205 static std::shared_ptr<DiallingNumberLoadRequest> CreateLoadRequest(int fileId, 206 int exId, int indexNum, const std::string &pin2Str, const AppExecFwk::InnerEvent::Pointer &result); 207 void UpdateFileController(const std::shared_ptr<IccFileController> &fileController); 208 209 protected: 210 std::shared_ptr<IccFileController> fileController_; 211 std::string GetFilePath(int elementaryFileId); 212 // 3GPP TS 51.011 V4.1.0 section 10.7 files of gsm 213 const std::string MASTER_FILE_SIM = "3F00"; 214 const std::string DEDICATED_FILE_TELECOM = "7F10"; 215 216 private: 217 using ProcessFunc = void (IccDiallingNumbersHandler::*)(const AppExecFwk::InnerEvent::Pointer &event, int &id); 218 std::map<int, ProcessFunc> memberFuncMap_; 219 AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, int loadId); 220 AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, std::shared_ptr<void> pobj, int loadId); 221 void ProcessDiallingNumberAllLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 222 void ProcessDiallingNumber(const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, 223 const std::shared_ptr<MultiRecordResult> &object); 224 void ProcessDiallingNumberLoadDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 225 void ProcessLinearSizeDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 226 void ProcessUpdateRecordDone(const AppExecFwk::InnerEvent::Pointer &event, int &id); 227 bool SendBackResult(int loadId); 228 void FetchDiallingNumberContent( 229 const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, const std::string &recordData); 230 std::shared_ptr<unsigned char> CreateSavingSequence( 231 const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int dataLength); 232 std::shared_ptr<unsigned char> CreateNameSequence(const std::u16string &name, int &seqLength); 233 std::shared_ptr<HRilRadioResponseInfo> MakeExceptionResult(int code); 234 void FillNumberFiledForDiallingNumber( 235 std::shared_ptr<unsigned char> diallingNumber, const std::string &number, int dataLength); 236 bool FormatNameAndNumber(std::shared_ptr<DiallingNumbersInfo> &diallingNumber, bool isDel); 237 void SendUpdateCommand(const std::shared_ptr<DiallingNumbersInfo> &diallingNumber, int length, 238 const std::shared_ptr<DiallingNumberLoadRequest> &loadRequest, int loadId); 239 void InitFuncMap(); 240 const int RECORD_LENGTH = 28; 241 const int LENGTH_RATE = 2; 242 const int INVALID_LENGTH = 49; 243 const static int32_t PRE_BYTES_NUM = 14; 244 const static int32_t MAX_NUMBER_SIZE_BYTES = 11; 245 const static int32_t EXT_FILE_BITYES_NUM = 13; 246 }; 247 } // namespace Telephony 248 } // namespace OHOS 249 250 #endif // OHOS_SIM_DIALLING_NUMBERS_HANDLER_H 251