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_RECEIVE_HANDLER_H
17 #define SMS_RECEIVE_HANDLER_H
18 
19 #include <vector>
20 
21 #include "tel_ril_sms_parcel.h"
22 #include "sms_base_message.h"
23 #include "sms_receive_indexer.h"
24 #include "sms_receive_reliability_handler.h"
25 #include "sms_wap_push_handler.h"
26 #include "tel_event_handler.h"
27 #include "datashare_helper.h"
28 #include "system_ability_definition.h"
29 
30 #ifdef ABILITY_POWER_SUPPORT
31 #include "power_mgr_client.h"
32 #include "power_mgr_errors.h"
33 #endif
34 
35 namespace OHOS {
36 namespace Telephony {
37 class SmsReceiveHandler : public TelEventHandler {
38 public:
39     explicit SmsReceiveHandler(int32_t slotId);
40     virtual ~SmsReceiveHandler();
41     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
42     bool IsDataShareReady();
43     void ApplyRunningLock();
44     void ReduceRunningLock();
45     void ReleaseRunningLock();
46 
47 protected:
48     virtual int32_t HandleSmsByType(const std::shared_ptr<SmsBaseMessage> smsBaseMessage) = 0;
49     virtual int32_t HandleAck(const std::shared_ptr<SmsBaseMessage> smsBaseMessage) = 0;
50     virtual void HandleRemainDataShare(const std::shared_ptr<SmsBaseMessage> smsBaseMessage) = 0;
51     virtual bool ReplySmsToSmsc(int result) = 0;
52     virtual void HandleMessageQueue();
53     virtual void HandleReconnectEvent();
54     virtual std::shared_ptr<SmsBaseMessage> TransformMessageInfo(const std::shared_ptr<SmsMessageInfo> info) = 0;
55     void CombineMessagePart(const std::shared_ptr<SmsReceiveIndexer> &smsIndexer);
56     bool IsRepeatedMessagePart(const std::shared_ptr<SmsReceiveIndexer> &smsIndexer);
57     bool AddMsgToDB(const std::shared_ptr<SmsReceiveIndexer> indexer);
58 
59 private:
60     SmsReceiveHandler(const SmsReceiveHandler &) = delete;
61     SmsReceiveHandler(const SmsReceiveHandler &&) = delete;
62     SmsReceiveHandler &operator=(const SmsReceiveHandler &) = delete;
63     SmsReceiveHandler &operator=(const SmsReceiveHandler &&) = delete;
64     void HandleReceivedSms(const std::shared_ptr<SmsBaseMessage> smsBaseMessage);
65     void HandleReceivedSmsWithoutDataShare(const std::shared_ptr<SmsBaseMessage> smsBaseMessage);
66     bool CombineMultiPageMessage(const std::shared_ptr<SmsReceiveIndexer> &indexer,
67         std::shared_ptr<std::vector<std::string>> pdus,
68         std::shared_ptr<SmsReceiveReliabilityHandler> reliabilityHandler);
69     void UpdateMultiPageMessage(
70         const std::shared_ptr<SmsReceiveIndexer> &indexer, std::shared_ptr<std::vector<std::string>> pdus);
71     void CreateRunningLockInner();
72     void HandleRunningLockTimeoutEvent(const AppExecFwk::InnerEvent::Pointer &event);
73 
74 protected:
75     int32_t slotId_ = -1;
76 
77 private:
78     static const uint32_t RUNNING_LOCK_TIMEOUT_EVENT_ID = 100000;
79     static const uint32_t DELAY_RELEASE_RUNNING_LOCK_EVENT_ID = 100001;
80     static const uint32_t RETRY_CONNECT_DATASHARE_EVENT_ID = 100002;
81 
82     static const int64_t RUNNING_LOCK_DEFAULT_TIMEOUT_MS = 60 * 1000; // 60s
83     static const int64_t DELAY_RELEASE_RUNNING_LOCK_TIMEOUT_MS = 5 * 1000; // 5s
84     static const int64_t DELAY_REDUCE_RUNNING_LOCK_TIMEOUT_MS = 8 * 1000; // 8s
85     static const int64_t DELAY_REDUCE_RUNNING_LOCK_SMS_QUEUE_TIMEOUT_MS = 20 * 1000; // 20s
86     static const int64_t DELAY_RETRY_CONNECT_DATASHARE_MS = 5 * 1000; // 5s
87     static const uint8_t RECONNECT_MAX_COUNT = 20;
88     static const int32_t E_OK = 0;
89 
90     std::mutex queueMutex_;
91     std::mutex datashareMutex_;
92     std::unique_ptr<SmsWapPushHandler> smsWapPushHandler_;
93 #ifdef ABILITY_POWER_SUPPORT
94     std::shared_ptr<PowerMgr::RunningLock> smsRunningLock_;
95 #endif
96     std::atomic_uint smsRunningLockCount_;
97     std::atomic_int smsLockSerialNum_;
98     std::mutex mutexRunningLock_;
99 };
100 } // namespace Telephony
101 } // namespace OHOS
102 #endif