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 <optional> 22 #include <queue> 23 #include <unordered_map> 24 25 #include "common_event_manager.h" 26 #include "gsm_sms_message.h" 27 #include "tel_ril_sms_parcel.h" 28 #include "i_sms_service_interface.h" 29 #include "network_state.h" 30 #include "sms_persist_helper.h" 31 #include "sms_send_indexer.h" 32 #include "tel_event_handler.h" 33 #include "want.h" 34 #include "common_event_support.h" 35 #include "sms_broadcast_subscriber_receiver.h" 36 #include "sms_wap_push_handler.h" 37 38 namespace OHOS { 39 namespace Telephony { 40 class SmsSender : public TelEventHandler { 41 public: 42 virtual ~SmsSender(); 43 SmsSender(int32_t slotId, std::function<void(std::shared_ptr<SmsSendIndexer>)> &sendRetryFun); 44 45 virtual void TextBasedSmsDelivery(const std::string &desAddr, const std::string &scAddr, 46 const std::string &text, const sptr<ISendShortMessageCallback> &sendCallback, 47 const sptr<IDeliveryShortMessageCallback> &deliveryCallback, uint16_t dataBaseId, bool isMmsApp = true) = 0; 48 49 virtual void DataBasedSmsDelivery(const std::string &desAddr, const std::string &scAddr, int32_t port, 50 const uint8_t *data, uint32_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback, 51 const sptr<IDeliveryShortMessageCallback> &deliveryCallback) = 0; 52 53 virtual void SendSmsToRil(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 54 void HandleMessageResponse(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 55 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 56 virtual void Init() = 0; 57 virtual void ResendTextDelivery(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 58 virtual void ResendDataDelivery(const std::shared_ptr<SmsSendIndexer> &smsIndexer) = 0; 59 virtual int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) = 0; 60 virtual void StatusReportSetImsSms(const AppExecFwk::InnerEvent::Pointer &event) = 0; 61 virtual void StatusReportGetImsSms(const AppExecFwk::InnerEvent::Pointer &event) = 0; 62 virtual void RegisterImsHandler() = 0; 63 64 static void SendResultCallBack( 65 const std::shared_ptr<SmsSendIndexer> &indexer, ISendShortMessageCallback::SmsSendResult result); 66 static void SendResultCallBack( 67 const sptr<ISendShortMessageCallback> &sendCallback, ISendShortMessageCallback::SmsSendResult result); 68 void SetNetworkState(bool isImsNetDomain, int32_t voiceServiceState); 69 std::optional<int32_t> GetNetworkId(); 70 void SetNetworkId(std::optional<int32_t> &id); 71 void SyncSwitchISmsResponse(); 72 bool SetImsSmsConfig(int32_t slotId, int32_t enable); 73 void OnRilAdapterHostDied(); 74 void DataBasedSmsDeliverySplitPage(GsmSmsMessage &gsmSmsMessage, std::vector<struct SplitInfo> cellsInfos, 75 std::shared_ptr<struct SmsTpdu> tpdu, uint8_t msgRef8bit, const std::string &desAddr, const std::string &scAddr, 76 int32_t port, const sptr<ISendShortMessageCallback> &sendCallback, 77 const sptr<IDeliveryShortMessageCallback> &deliveryCallback); 78 void DataBasedSmsDeliveryPacketSplitPage(GsmSmsMessage &gsmSmsMessage, std::shared_ptr<struct SmsTpdu> tpdu, 79 uint8_t msgRef8bit, uint32_t indexData, int32_t port, const std::string &scAddr, 80 const sptr<ISendShortMessageCallback> &sendCallback, 81 const sptr<IDeliveryShortMessageCallback> &deliveryCallback, std::shared_ptr<SmsSendIndexer> indexer, 82 std::vector<struct SplitInfo> cellsInfos); 83 void DataBasedSmsDeliverySendSplitPage(std::shared_ptr<struct EncodeInfo> encodeInfo, 84 const sptr<ISendShortMessageCallback> &sendCallback, std::shared_ptr<SmsSendIndexer> indexer, 85 uint8_t msgRef8bit, uint32_t totalPage); 86 void SendCallbackExceptionCase(const sptr<ISendShortMessageCallback> &sendCallback, std::string str); 87 static SmsCodingNationalType GetSmsCodingNationalType(int slotId); 88 89 public: 90 bool resIsSmsReady_ = false; 91 int32_t imsSmsCfg_ = IMS_SMS_ENABLE; 92 std::mutex ctx_; 93 std::condition_variable cv_; 94 95 protected: 96 void SendCacheMapTimeoutCheck(); 97 bool SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> &sendCallback); 98 bool SendCacheMapAddItem(int64_t id, const std::shared_ptr<SmsSendIndexer> &smsIndexer); 99 bool SendCacheMapEraseItem(int64_t id); 100 std::shared_ptr<SmsSendIndexer> FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer &event); 101 uint8_t GetMsgRef8Bit(); 102 int64_t GetMsgRef64Bit(); 103 virtual void StatusReportAnalysis(const AppExecFwk::InnerEvent::Pointer &event) = 0; 104 void SendMessageSucceed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 105 void SendMessageFailed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 106 bool CheckForce7BitEncodeType(); 107 void CharArrayToString(const uint8_t *data, uint32_t dataLen, std::string &dataStr); 108 109 protected: 110 int32_t slotId_ = -1; 111 std::list<std::shared_ptr<SmsSendIndexer>> reportList_; 112 bool isImsNetDomain_ = false; 113 bool enableImsSmsOnceWhenImsReg_ = true; 114 int32_t voiceServiceState_ = static_cast<int32_t>(RegServiceState::REG_STATE_UNKNOWN); 115 int32_t lastSmsDomain_ = CS_DOMAIN; 116 static constexpr uint8_t MAX_SEND_RETRIES = 3; 117 static constexpr uint8_t INITIAL_COUNT = 0; 118 static constexpr int32_t IMS_SMS_ENABLE = 1; 119 static constexpr int32_t IMS_SMS_DISABLE = 0; 120 static constexpr int32_t CS_DOMAIN = 0; 121 static constexpr int32_t IMS_DOMAIN = 1; 122 static constexpr int32_t WAIT_TIME_SECOND = 1; 123 124 private: 125 static constexpr uint16_t EXPIRED_TIME = 60 * 3; 126 static constexpr uint16_t DELAY_MAX_TIME_MSCE = 2000; 127 static constexpr uint8_t MSG_QUEUE_LIMIT = 25; 128 static constexpr uint8_t MAX_REPORT_LIST_LIMIT = 25; 129 static constexpr const char *KEY_SMS_CODING_NATIONAL_INT = "sms_coding_national_int"; 130 131 SmsSender(const SmsSender &) = delete; 132 SmsSender(const SmsSender &&) = delete; 133 SmsSender &operator=(const SmsSender &) = delete; 134 SmsSender &operator=(const SmsSender &&) = delete; 135 136 void HandleResend(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 137 void UpdateUnSentCellCount(uint8_t refId); 138 139 private: 140 std::function<void(std::shared_ptr<SmsSendIndexer>)> sendRetryFun_; 141 static std::unordered_map<int64_t, std::shared_ptr<SmsSendIndexer>> sendCacheMap_; 142 std::mutex sendCacheMapMutex_; 143 uint8_t msgRef8bit_ = 0; 144 static int64_t msgRef64bit_; 145 std::optional<int32_t> networkId_ = std::nullopt; 146 }; 147 } // namespace Telephony 148 } // namespace OHOS 149 #endif 150