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_SERVICE_H
17#define SMS_SERVICE_H
18
19#include <memory>
20
21#include "sms_interface_stub.h"
22#include "sms_state_handler.h"
23#include "system_ability.h"
24#include "system_ability_definition.h"
25
26namespace OHOS {
27namespace Telephony {
28enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
29
30class SmsService : public SystemAbility, public SmsInterfaceStub, public std::enable_shared_from_this<SmsService> {
31    DECLARE_DELAYED_SINGLETON(SmsService)
32    DECLARE_SYSTEM_ABILITY(SmsService) // necessary
33public:
34    void OnStart() override;
35    void OnStop() override;
36    int32_t Dump(std::int32_t fd, const std::vector<std::u16string> &args) override;
37    std::string GetBindTime();
38    void InsertSessionAndDetail(int32_t slotId, const std::string &telephone, const std::string &text,
39        uint16_t &dataBaseId);
40
41    /**
42     * Sends a text Type SMS message.
43     * @param slotId Indicates the card slot index number,
44     * ranging from {@code 0} to the maximum card slot index number supported by
45     * the device
46     * @param desAddr Indicates the destination address
47     * @param scAddr Indicates the sms center address
48     * @param text Indicates sms content
49     * @param sendCallback Indicates callback for send out
50     * @param deliverCallback Indicates callback for delivery to destination user
51     * @return Returns {@code 0} if send message success.
52     */
53    int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
54        const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback,
55        const sptr<IDeliveryShortMessageCallback> &deliveryCallback, bool isMmsApp = true) override;
56
57    /**
58     * Sends a text Type SMS message withot save to database.
59     * @param slotId Indicates the card slot index number,
60     * ranging from {@code 0} to the maximum card slot index number supported by
61     * the device
62     * @param desAddr Indicates the destination address
63     * @param scAddr Indicates the sms center address
64     * @param text Indicates sms content
65     * @param sendCallback Indicates callback for send out
66     * @param deliverCallback Indicates callback for delivery to destination user
67     * @return Returns {@code 0} if send message success.
68     */
69    int32_t SendMessageWithoutSave(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
70        const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback,
71        const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
72
73    /**
74     * Sends a data Type SMS message.
75     * @param slotId Indicates the card slot index number,
76     * ranging from {@code 0} to the maximum card slot index number supported by
77     * the device
78     * @param desAddr Indicates the destination address
79     * @param scAddr Indicates the sms center address
80     * @param port Indicates the port of data sms
81     * @param data Indicates the array of data sms
82     * @param dataLen Indicates the array length of data sms
83     * @param sendCallback Indicates callback for send out
84     * @param deliverCallback Indicates callback for delivery to destination user
85     * @return Returns {@code 0} if send message success.
86     */
87    int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr, uint16_t port,
88        const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
89        const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
90
91    /**
92     * Sets the address for the Short Message Service Center (SMSC) based on a
93     * specified slot ID
94     * @param slotId Indicates the card slot index number,
95     * ranging from {@code 0} to the maximum card slot index number supported by
96     * the device
97     * @param scAddr Indicates the sms center address
98     * @return Returns {@code 0} if set smsc success
99     */
100    int32_t SetSmscAddr(int32_t slotId, const std::u16string &scAddr) override;
101
102    /**
103     * Obtains the SMSC address based on a specified slot ID.
104     * @param slotId Indicates the card slot index number,
105     * ranging from {@code 0} to the maximum card slot index number supported by
106     * the device
107     * @param smscAddress [out]
108     * @return Returns {@code 0} if get smsc success.
109     */
110    int32_t GetSmscAddr(int32_t slotId, std::u16string &smscAddress) override;
111
112    /**
113     * Add a sms to sim card.
114     * @param slotId Indicates the card slot index number,
115     * ranging from {@code 0} to the maximum card slot index number supported by
116     * the device
117     * @param smsc Indicates the sms center address
118     * @param pdu Indicates the sms pdu data
119     * @param status Indicates the sms status, read or not
120     * @return Returns {@code true} if add sim success; returns {@code false}
121     */
122    int32_t AddSimMessage(
123        int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status) override;
124
125    /**
126     * Delete a sms in the sim card.
127     * @param slotId Indicates the card slot index number,
128     * ranging from {@code 0} to the maximum card slot index number supported by
129     * the device
130     * @param msgIndex Indicates the sim sms index in sim card
131     * @return Returns {@code true} if delete sim success; returns {@code false}
132     */
133    int32_t DelSimMessage(int32_t slotId, uint32_t msgIndex) override;
134
135    /**
136     * Update a sms in the sim card.
137     * @param slotId Indicates the card slot index number,
138     * ranging from {@code 0} to the maximum card slot index number supported by
139     * the device
140     * @param msgIndex Indicates the sim sms index in sim card
141     * @param newStatusIndicates the sms status, read or not
142     * @param pdu Indicates the sms pdu data
143     * @param smsc Indicates the sms center address
144     * @return Returns {@code 0} if update sim success
145     */
146    int32_t UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus, const std::u16string &pdu,
147        const std::u16string &smsc) override;
148
149    /**
150     * Get sim card all the sms.
151     * @param slotId Indicates the card slot index number,
152     * ranging from {@code 0} to the maximum card slot index number supported by
153     * the device
154     * @param message Indicates all SMS messages of sim card
155     * @return Returns {@code 0} if get all sim messages success
156     */
157    int32_t GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message) override;
158
159    /**
160     * Configure a cell broadcast in a certain band range.
161     * @param slotId [in]
162     * @param enable [in]
163     * @param fromMsgId [in]
164     * @param toMsgId [in]
165     * @param netType [in]
166     * @return Returns {@code 0} if set CB config success
167     */
168    int32_t SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType) override;
169
170    /**
171     * enable or disable IMS SMS.
172     * @param slotId Indicates the card slot index number,
173     * ranging from {@code 0} to the maximum card slot index number supported by
174     * the device
175     * @param enable Indicates enable or disable Ims sms
176     * ranging {@code 0} disable Ims sms {@code 1} enable Ims sms
177     * @return Returns {@code true} if enable or disable Ims Sms success; returns
178     * {@code false} otherwise
179     */
180    bool SetImsSmsConfig(int32_t slotId, int32_t enable) override;
181
182    /**
183     * Set the Default Sms Slot Id To SmsService
184     * @param slotId Indicates the card slot index number,
185     * ranging from {@code 0} to the maximum card slot index number supported by
186     * the device
187     * @return Returns {@code 0} if set default sms slot id success
188     */
189    int32_t SetDefaultSmsSlotId(int32_t slotId) override;
190
191    /**
192     * Get the Default Sms Slot Id From SmsService
193     * @return Returns default sms slot id
194     */
195    int32_t GetDefaultSmsSlotId() override;
196
197    /**
198     * @brief GetDefaultSmsSlotId
199     * Get the Default Sms Sim Id From SmsService
200     * @return int32_t
201     */
202    int32_t GetDefaultSmsSimId(int32_t &simId) override;
203
204    /**
205     * @brief SplitMessage
206     * calculate Sms Message Split Segment count
207     * @param Indicates input message
208     * @param splitMessage Indicates the split information
209     * @return Returns {@code 0} if split message success
210     */
211    int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage) override;
212
213    /**
214     * calculate the Sms Message Segments Info
215     * @param slotId Indicates the card slot index number,
216     * ranging from {@code 0} to the maximum card slot index number supported by
217     * the device
218     * @param message Indicates input message
219     * @param force7BitCode Indicates sms encode type, 7bit or not
220     * @param info Indicates output sms segment
221     * @return Returns {@code 0} if get sms segments info
222     */
223    int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode,
224        ISmsServiceInterface::SmsSegmentsInfo &info) override;
225
226    /**
227     * Check Sms Is supported Ims newtwork
228     * Hide this for inner system use
229     * @param slotId Indicates the card slot index number,
230     * ranging from {@code 0} to the maximum card slot index number supported by
231     * the device
232     * @param isSupported Whether ims SMS is supported
233     * @return Returns {@code 0} if successful
234     */
235    int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) override;
236
237    /**
238     * Get the Ims Short Message Format 3gpp/3gpp2
239     * Hide this for inner system use
240     * @return int32_t
241     */
242    int32_t GetImsShortMessageFormat(std::u16string &format) override;
243
244    /**
245     * Check whether it is supported Sms Capability
246     * @return Returns {@code true} if support sms; returns {@code false}
247     */
248    bool HasSmsCapability() override;
249
250    /**
251     * @brief GetServiceRunningState
252     * Get service running state
253     * @return ServiceRunningState
254     */
255    int32_t GetServiceRunningState();
256
257    /**
258     * @brief GetSpendTime
259     * Get service start spend time
260     * @return Spend time
261     */
262    int64_t GetSpendTime();
263
264    /**
265     * @brief GetEndTime
266     * Get service start finish time
267     * @return Spend time
268     */
269    int64_t GetEndTime();
270
271    /**
272     * create a short message
273     * @param pdu Indicates pdu code,
274     * @param specification Indicates 3gpp or 3gpp2
275     * @param ShortMessage Indicates a short message object
276     * @return Returns {@code 0} if CreateMessage success
277     */
278    int32_t CreateMessage(std::string pdu, std::string specification, ShortMessage &message) override;
279
280    /**
281     * mms base64 encode
282     * @param src Indicates source string,
283     * @param dest Indicates destination string
284     * @return Returns {@code true} if encode success; returns {@code false} otherwise
285     */
286    bool GetBase64Encode(std::string src, std::string &dest) override;
287
288    /**
289     * mms base64 decode
290     * @param src Indicates source string,
291     * @param dest Indicates destination string
292     * @return Returns {@code true} if decode success; returns {@code false} otherwise
293     */
294    bool GetBase64Decode(std::string src, std::string &dest) override;
295
296    /**
297     * Get Encode String
298     * @param encodeString Indicates output string,
299     * @param charset Indicates character set,
300     * @param valLength Indicates input string length,
301     * @param strEncodeString Indicates input string
302     * @return Returns {@code true} if decode success; returns {@code false} otherwise
303     */
304    bool GetEncodeStringFunc(
305        std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString) override;
306
307    /**
308     * Send a Mms.
309     * @param slotId Indicates the card slot index number,
310     * ranging from {@code 0} to the maximum card slot index number supported by
311     * the device
312     * @param mmsc Indicates service center of mms
313     * @param data Indicates mms pdu byte array
314     * @param ua Indicates mms user agent
315     * @param uaprof Indicates mms user agent profile
316     * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
317     */
318    int32_t SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua,
319        const std::u16string &uaprof, int64_t &time, bool isMmsApp = false) override;
320
321    /**
322     * Service after Send Mms.
323     * @param slotId Indicates the card slot index number,
324     * ranging from {@code 0} to the maximum card slot index number supported by
325     * the device
326     * @param time Indicates tiemstamp of hap call NAPI function
327     * @param dataBaseId id of mms in sqlite
328     * @param sessionBucket  object of DataShareValuesBucket to upate sqlite
329     * @param sendStatus status of mms sent
330     * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
331     */
332    void ServiceAfterSendMmsComplete(int32_t slotId, int64_t &time, uint16_t &dataBaseId,
333        DataShare::DataShareValuesBucket &sessionBucket, std::string  &sendStatus);
334
335    /**
336     * Download a Mms.
337     * @param slotId Indicates the card slot index number,
338     * ranging from {@code 0} to the maximum card slot index number supported by
339     * the device
340     * @param mmsc Indicates service center of mms
341     * @param data Indicates mms pdu byte array
342     * @param ua Indicates mms user agent
343     * @param uaprof Indicates mms user agent profile
344     * @return Returns {@code 0} if download mms success; returns {@code false} otherwise
345     */
346    int32_t DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data,
347        const std::u16string &ua, const std::u16string &uaprof) override;
348    int32_t OnRilAdapterHostDied(int32_t slotId);
349
350private:
351    bool Init();
352    void WaitCoreServiceToInit();
353    bool CheckSmsPermission(const sptr<ISendShortMessageCallback> &sendCallback);
354    bool ValidDestinationAddress(std::string desAddr);
355    void TrimSmscAddr(std::string &sca);
356    bool CheckSimMessageIndexValid(int32_t slotId, uint32_t msgIndex);
357    void InsertSmsMmsInfo(int32_t slotId, uint16_t sessionId, const std::string &number,
358        const std::string &text, uint16_t &dataBaseId);
359    bool InsertSession(bool isNewSession, uint16_t messageCount, const std::string &number, const std::string &text);
360    bool QuerySessionByTelephone(const std::string &telephone, uint16_t &sessionId, uint16_t &messageCount);
361    void UpdateSmsContact(const std::string &address);
362
363private:
364    int64_t bindTime_ = 0;
365    int64_t endTime_ = 0;
366    int64_t spendTime_ = 0;
367    bool registerToService_ = false;
368    static constexpr const char *SMS_MMS_INFO_MSG_STATE_SENDING = "1";
369    ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
370    std::shared_ptr<SmsStateHandler> smsStateHandler_;
371    const std::string SMS_MMS_INFO = "datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info";
372    const std::string SMS_SESSION = "datashare:///com.ohos.smsmmsability/sms_mms/session";
373};
374} // namespace Telephony
375} // namespace OHOS
376#endif