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_MISC_MANAGER_H
17#define SMS_MISC_MANAGER_H
18
19#include <condition_variable>
20#include <list>
21#include <mutex>
22#include <string>
23#include <vector>
24
25#include "tel_ril_sms_parcel.h"
26#include "i_sms_service_interface.h"
27#include "tel_event_handler.h"
28
29namespace OHOS {
30namespace Telephony {
31class SmsMiscManager : public TelEventHandler {
32public:
33    enum {
34        SET_CB_CONFIG_FINISH = 0,
35        GET_CB_CONFIG_FINISH,
36        SET_SMSC_ADDR_FINISH,
37        GET_SMSC_ADDR_FINISH,
38    };
39    using gsmCBRangeInfo = struct RangeInfo {
40        RangeInfo(uint32_t fromId, uint32_t toId)
41        {
42            fromMsgId = fromId;
43            toMsgId = toId;
44        }
45        uint32_t fromMsgId = 0;
46        uint32_t toMsgId = 0;
47        bool operator<(const RangeInfo &other) const
48        {
49            return fromMsgId < other.fromMsgId || (fromMsgId == other.fromMsgId && toMsgId < other.toMsgId);
50        }
51        bool operator==(const RangeInfo &other) const
52        {
53            return fromMsgId == other.fromMsgId && toMsgId == other.toMsgId;
54        }
55    };
56    explicit SmsMiscManager(int32_t slotId);
57    virtual ~SmsMiscManager() {};
58    int32_t SetCBConfig(bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType);
59    std::list<gsmCBRangeInfo> GetRangeInfo() const;
60    virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
61    int32_t AddSimMessage(
62        const std::string &smsc, const std::string &pdu, ISmsServiceInterface::SimMessageStatus status);
63    int32_t DelSimMessage(uint32_t msgIndex);
64    int32_t UpdateSimMessage(uint32_t msgIndex, ISmsServiceInterface::SimMessageStatus newStatus,
65        const std::string &pdu, const std::string &smsc);
66    int32_t GetAllSimMessages(std::vector<ShortMessage> &message);
67    int32_t SetSmscAddr(const std::string &scAddr);
68    int32_t GetSmscAddr(std::u16string &smscAddress);
69    int32_t SetDefaultSmsSlotId(int32_t slotId);
70    int32_t GetDefaultSmsSlotId();
71    int32_t GetDefaultSmsSimId(int32_t &simId);
72
73private:
74    using infoData = struct info {
75        info(uint32_t fromMsgId, uint32_t toMsgId)
76        {
77            startPos = fromMsgId;
78            endPos = toMsgId;
79        }
80        bool isMerge = false;
81        uint32_t startPos = 0;
82        uint32_t endPos = 0;
83    };
84
85private:
86    bool OpenCBRange(uint32_t fromMsgId, uint32_t toMsgId);
87    bool CloseCBRange(uint32_t fromMsgId, uint32_t toMsgId);
88    void NotifyHasResponse();
89    bool IsEmpty() const;
90    std::string RangeListToString(const std::list<gsmCBRangeInfo> rangeList);
91    bool SendDataToRil(bool enable, std::list<gsmCBRangeInfo> list);
92    void SplitMsgId(uint32_t fromMsgId, uint32_t toMsgId, const std::list<gsmCBRangeInfo>::iterator &oldIter);
93    void UpdateCbRangList(std::shared_ptr<CBConfigInfo> res);
94    void SplitMids(std::string src, std::vector<std::string> &dest, const std::string delimiter);
95    bool SplitMidValue(std::string value, std::string &start, std::string &end, const std::string delimiter);
96    void GetModemCBRange();
97    void CombineCBRange();
98    void GetCBConfigFinish(const AppExecFwk::InnerEvent::Pointer &event);
99
100private:
101    std::list<int32_t> fairList_;
102    int32_t fairVar_ = -1;
103    int32_t conditonVar_ = 0;
104    bool isSuccess_ = false;
105    int32_t slotId_;
106    std::list<gsmCBRangeInfo> rangeList_;
107    std::condition_variable condVar_;
108    std::mutex mutex_;
109    std::mutex cbMutex_;
110    std::string smscAddr_;
111    std::string codeScheme_ { "0-255" };
112    std::list<gsmCBRangeInfo> mdRangeList_;
113    int32_t smsCapacityOfSim_ = 0;
114    static bool hasGotCbRange_;
115};
116} // namespace Telephony
117} // namespace OHOS
118#endif