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