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 SMS_SENDER_H 17 #define SMS_SENDER_H 18 19 #include <functional> 20 #include <list> 21 #include <memory> 22 #include <queue> 23 #include <string> 24 #include <unordered_map> 25 #include <optional> 26 27 #include "event_handler.h" 28 #include "event_runner.h" 29 30 #include "hril_sms_parcel.h" 31 #include "i_sms_service_interface.h" 32 #include "sms_send_indexer.h" 33 #include "sms_persist_helper.h" 34 #include "network_state.h" 35 36 namespace OHOS { 37 namespace Telephony { 38 class SmsSender : public AppExecFwk::EventHandler { 39 public: 40 virtual ~SmsSender(); 41 SmsSender(const std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId, 42 std::function<void(std::shared_ptr<SmsSendIndexer>)> &sendRetryFun); 43 44 virtual void TextBasedSmsDelivery(const std::string &desAddr, const std::string &scAddr, 45 const std::string &text, const sptr<ISendShortMessageCallback> &sendCallback, 46 const sptr<IDeliveryShortMessageCallback> &deliveryCallback) = 0; 47 48 virtual void DataBasedSmsDelivery(const std::string &desAddr, const std::string &scAddr, int32_t port, 49 const uint8_t *data, uint32_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback, 50 const sptr<IDeliveryShortMessageCallback> &deliveryCallback) = 0; 51 52 virtual void SendSmsToRil(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 53 void HandleMessageResponse(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 54 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 55 virtual void Init() = 0; 56 virtual void ResendTextDelivery(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 57 virtual void ResendDataDelivery(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 58 virtual int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) = 0; 59 virtual void StatusReportSetImsSms(const AppExecFwk::InnerEvent::Pointer &event) = 0; 60 virtual void StatusReportGetImsSms(const AppExecFwk::InnerEvent::Pointer &event) = 0; 61 virtual void RegisterImsHandler() = 0; 62 63 static void SendResultCallBack( 64 const std::shared_ptr<SmsSendIndexer> &indexer, ISendShortMessageCallback::SmsSendResult result); 65 static void SendResultCallBack( 66 const sptr<ISendShortMessageCallback> &sendCallback, ISendShortMessageCallback::SmsSendResult result); 67 void SetNetworkState(bool isImsNetDomain, int32_t voiceServiceState); 68 std::optional<int32_t> GetNetworkId(); 69 void SetNetworkId(std::optional<int32_t> &id); 70 void SyncSwitchISmsResponse(); 71 bool SetImsSmsConfig(int32_t slotId, int32_t enable); 72 73 public: 74 bool resIsSmsReady_ = false; 75 int32_t imsSmsCfg_ = IMS_SMS_ENABLE; 76 std::mutex ctx_; 77 std::condition_variable cv_; 78 79 protected: 80 void SendCacheMapTimeoutCheck(); 81 bool SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> &sendCallback); 82 bool SendCacheMapAddItem(int64_t id, const std::shared_ptr<SmsSendIndexer> &smsIndexer); 83 bool SendCacheMapEraseItem(int64_t id); 84 std::shared_ptr<SmsSendIndexer> FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer &event); 85 uint8_t GetMsgRef8Bit(); 86 int64_t GetMsgRef64Bit(); 87 virtual void StatusReportAnalysis(const AppExecFwk::InnerEvent::Pointer &event) = 0; 88 void SendMessageSucceed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 89 void SendMessageFailed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 90 bool CheckForce7BitEncodeType(); 91 92 int32_t slotId_ = -1; 93 std::list<std::shared_ptr<SmsSendIndexer>> reportList_; 94 bool isImsNetDomain_ = false; 95 bool enableImsSmsOnceWhenImsReg_ = true; 96 int32_t voiceServiceState_ = static_cast<int32_t>(RegServiceState::REG_STATE_UNKNOWN); 97 int32_t lastSmsDomain_ = CS_DOMAIN; 98 static constexpr uint8_t MAX_SEND_RETRIES = 3; 99 static constexpr uint8_t INITIAL_COUNT = 0; 100 static constexpr int32_t IMS_SMS_ENABLE = 1; 101 static constexpr int32_t IMS_SMS_DISABLE = 0; 102 static constexpr int32_t CS_DOMAIN = 0; 103 static constexpr int32_t IMS_DOMAIN = 1; 104 static constexpr int32_t WAIT_TIME_SECOND = 1; 105 106 private: 107 static constexpr uint16_t EXPIRED_TIME = 60 * 3; 108 static constexpr uint16_t DELAY_MAX_TIME_MSCE = 2000; 109 static constexpr uint8_t MSG_QUEUE_LIMIT = 25; 110 static constexpr uint8_t MAX_REPORT_LIST_LIMIT = 25; 111 112 SmsSender(const SmsSender &) = delete; 113 SmsSender(const SmsSender &&) = delete; 114 SmsSender &operator=(const SmsSender &) = delete; 115 SmsSender &operator=(const SmsSender &&) = delete; 116 117 void HandleResend(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 118 std::function<void(std::shared_ptr<SmsSendIndexer>)> sendRetryFun_; 119 std::unordered_map<int64_t, std::shared_ptr<SmsSendIndexer>> sendCacheMap_; 120 std::mutex sendCacheMapMutex_; 121 uint8_t msgRef8bit_ = 0; 122 int64_t msgRef64bit_ = 0; 123 std::optional<int32_t> networkId_ = std::nullopt; 124 }; 125 } // namespace Telephony 126 } // namespace OHOS 127 #endif