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 59 static void SendResultCallBack( 60 const std::shared_ptr<SmsSendIndexer> &indexer, ISendShortMessageCallback::SmsSendResult result); 61 static void SendResultCallBack( 62 const sptr<ISendShortMessageCallback> &sendCallback, ISendShortMessageCallback::SmsSendResult result); 63 void SetNetworkState(bool isImsNetDomain, int32_t voiceServiceState); 64 std::optional<int32_t> GetNetworkId(); 65 void SetNetworkId(std::optional<int32_t> &id); 66 67 protected: 68 void SendCacheMapTimeoutCheck(); 69 bool SendCacheMapLimitCheck(const sptr<ISendShortMessageCallback> &sendCallback); 70 bool SendCacheMapAddItem(int64_t id, const std::shared_ptr<SmsSendIndexer> &smsIndexer); 71 bool SendCacheMapEraseItem(int64_t id); 72 std::shared_ptr<SmsSendIndexer> FindCacheMapAndTransform(const AppExecFwk::InnerEvent::Pointer &event); 73 uint8_t GetMsgRef8Bit(); 74 int64_t GetMsgRef64Bit(); 75 virtual void StatusReportAnalysis(const AppExecFwk::InnerEvent::Pointer &event) = 0; 76 void SendMessageSucceed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 77 void SendMessageFailed(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 78 bool CheckForce7BitEncodeType(); 79 80 int32_t slotId_ = -1; 81 std::list<std::shared_ptr<SmsSendIndexer>> reportList_; 82 bool isImsNetDomain_ = false; 83 int32_t voiceServiceState_ = static_cast<int32_t>(RegServiceState::REG_STATE_UNKNOWN); 84 85 private: 86 static constexpr uint16_t EXPIRED_TIME = 60 * 3; 87 static constexpr uint16_t DELAY_MAX_TIME_MSCE = 2000; 88 static constexpr uint8_t MSG_QUEUE_LIMIT = 25; 89 static constexpr uint8_t MAX_REPORT_LIST_LIMIT = 25; 90 static constexpr uint8_t MAX_SEND_RETRIES = 3; 91 92 SmsSender(const SmsSender &) = delete; 93 SmsSender(const SmsSender &&) = delete; 94 SmsSender &operator=(const SmsSender &) = delete; 95 SmsSender &operator=(const SmsSender &&) = delete; 96 97 void HandleResend(const std::shared_ptr<SmsSendIndexer> &smsIndexer); 98 std::function<void(std::shared_ptr<SmsSendIndexer>)> sendRetryFun_; 99 std::unordered_map<int64_t, std::shared_ptr<SmsSendIndexer>> sendCacheMap_; 100 std::mutex sendCacheMapMutex_; 101 uint8_t msgRef8bit_ = 0; 102 int64_t msgRef64bit_ = 0; 103 std::optional<int32_t> networkId_ = std::nullopt; 104 }; 105 } // namespace Telephony 106 } // namespace OHOS 107 #endif