• 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_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 
26 namespace OHOS {
27 namespace Telephony {
28 enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
29 
30 class SmsService : public SystemAbility, public SmsInterfaceStub, public std::enable_shared_from_this<SmsService> {
31     DECLARE_DELAYED_SINGLETON(SmsService)
32     DECLARE_SYSTEM_ABILITY(SmsService) // necessary
33 public:
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      * Configure cell broadcast list in some certain band range.
172      * @param slotId [in]
173      * @param messageIds [in]
174      * @param ranType [in]
175      * @return Returns {@code 0} if set CB config list success
176      */
177     int32_t SetCBConfigList(int32_t slotId, const std::vector<int32_t>& messageIds, int32_t ranType) override;
178 
179     /**
180      * enable or disable IMS SMS.
181      * @param slotId Indicates the card slot index number,
182      * ranging from {@code 0} to the maximum card slot index number supported by
183      * the device
184      * @param enable Indicates enable or disable Ims sms
185      * ranging {@code 0} disable Ims sms {@code 1} enable Ims sms
186      * @return Returns {@code true} if enable or disable Ims Sms success; returns
187      * {@code false} otherwise
188      */
189     bool SetImsSmsConfig(int32_t slotId, int32_t enable) override;
190 
191     /**
192      * Set the Default Sms Slot Id To SmsService
193      * @param slotId Indicates the card slot index number,
194      * ranging from {@code 0} to the maximum card slot index number supported by
195      * the device
196      * @return Returns {@code 0} if set default sms slot id success
197      */
198     int32_t SetDefaultSmsSlotId(int32_t slotId) override;
199 
200     /**
201      * Get the Default Sms Slot Id From SmsService
202      * @return Returns default sms slot id
203      */
204     int32_t GetDefaultSmsSlotId() override;
205 
206     /**
207      * @brief GetDefaultSmsSlotId
208      * Get the Default Sms Sim Id From SmsService
209      * @return int32_t
210      */
211     int32_t GetDefaultSmsSimId(int32_t &simId) override;
212 
213     /**
214      * @brief SplitMessage
215      * calculate Sms Message Split Segment count
216      * @param Indicates input message
217      * @param splitMessage Indicates the split information
218      * @return Returns {@code 0} if split message success
219      */
220     int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage) override;
221 
222     /**
223      * calculate the Sms Message Segments Info
224      * @param slotId Indicates the card slot index number,
225      * ranging from {@code 0} to the maximum card slot index number supported by
226      * the device
227      * @param message Indicates input message
228      * @param force7BitCode Indicates sms encode type, 7bit or not
229      * @param info Indicates output sms segment
230      * @return Returns {@code 0} if get sms segments info
231      */
232     int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode,
233         ISmsServiceInterface::SmsSegmentsInfo &info) override;
234 
235     /**
236      * Check Sms Is supported Ims newtwork
237      * Hide this for inner system use
238      * @param slotId Indicates the card slot index number,
239      * ranging from {@code 0} to the maximum card slot index number supported by
240      * the device
241      * @param isSupported Whether ims SMS is supported
242      * @return Returns {@code 0} if successful
243      */
244     int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) override;
245 
246     /**
247      * Get the Ims Short Message Format 3gpp/3gpp2
248      * Hide this for inner system use
249      * @return int32_t
250      */
251     int32_t GetImsShortMessageFormat(std::u16string &format) override;
252 
253     /**
254      * Check whether it is supported Sms Capability
255      * @return Returns {@code true} if support sms; returns {@code false}
256      */
257     bool HasSmsCapability() override;
258 
259     /**
260      * @brief GetServiceRunningState
261      * Get service running state
262      * @return ServiceRunningState
263      */
264     int32_t GetServiceRunningState();
265 
266     /**
267      * @brief GetSpendTime
268      * Get service start spend time
269      * @return Spend time
270      */
271     int64_t GetSpendTime();
272 
273     /**
274      * @brief GetEndTime
275      * Get service start finish time
276      * @return Spend time
277      */
278     int64_t GetEndTime();
279 
280     /**
281      * create a short message
282      * @param pdu Indicates pdu code,
283      * @param specification Indicates 3gpp or 3gpp2
284      * @param ShortMessage Indicates a short message object
285      * @return Returns {@code 0} if CreateMessage success
286      */
287     int32_t CreateMessage(std::string pdu, std::string specification, ShortMessage &message) override;
288 
289     /**
290      * mms base64 encode
291      * @param src Indicates source string,
292      * @param dest Indicates destination string
293      * @return Returns {@code true} if encode success; returns {@code false} otherwise
294      */
295     bool GetBase64Encode(std::string src, std::string &dest) override;
296 
297     /**
298      * mms base64 decode
299      * @param src Indicates source string,
300      * @param dest Indicates destination string
301      * @return Returns {@code true} if decode success; returns {@code false} otherwise
302      */
303     bool GetBase64Decode(std::string src, std::string &dest) override;
304 
305     /**
306      * Get Encode String
307      * @param encodeString Indicates output string,
308      * @param charset Indicates character set,
309      * @param valLength Indicates input string length,
310      * @param strEncodeString Indicates input string
311      * @return Returns {@code true} if decode success; returns {@code false} otherwise
312      */
313     bool GetEncodeStringFunc(
314         std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString) override;
315 
316     /**
317      * Send a Mms.
318      * @param slotId Indicates the card slot index number,
319      * ranging from {@code 0} to the maximum card slot index number supported by
320      * the device
321      * @param mmsc Indicates service center of mms
322      * @param data Indicates mms pdu byte array
323      * @param ua Indicates mms user agent
324      * @param uaprof Indicates mms user agent profile
325      * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
326      */
327     int32_t SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua,
328         const std::u16string &uaprof, int64_t &time, bool isMmsApp = false) override;
329 
330     /**
331      * Check send mms permissions.
332      * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
333      */
334     int32_t CheckMmsPermissions();
335 
336     /**
337      * Query send mms database id.
338      * @param slotId Indicates the card slot index number,
339      * ranging from {@code 0} to the maximum card slot index number supported by
340      * the device
341      * @param time Indicates tiemstamp of hap call NAPI function
342      * @return Returns id of mms in sqlite
343      */
344     uint16_t QueryMmsDatabaseId(int32_t slotId, int64_t time);
345 
346     /**
347      * Service after Send Mms.
348      * @param slotId Indicates the card slot index number,
349      * ranging from {@code 0} to the maximum card slot index number supported by
350      * the device
351      * @param time Indicates tiemstamp of hap call NAPI function
352      * @param dataBaseId id of mms in sqlite
353      * @param sessionBucket  object of DataShareValuesBucket to upate sqlite
354      * @param sendStatus status of mms sent
355      * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
356      */
357     void ServiceAfterSendMmsComplete(int32_t slotId, int64_t &time, uint16_t &dataBaseId,
358         DataShare::DataShareValuesBucket &sessionBucket, std::string  &sendStatus);
359 
360     /**
361      * Download a Mms.
362      * @param slotId Indicates the card slot index number,
363      * ranging from {@code 0} to the maximum card slot index number supported by
364      * the device
365      * @param mmsc Indicates service center of mms
366      * @param data Indicates mms pdu byte array
367      * @param ua Indicates mms user agent
368      * @param uaprof Indicates mms user agent profile
369      * @return Returns {@code 0} if download mms success; returns {@code false} otherwise
370      */
371     int32_t DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data,
372         const std::u16string &ua, const std::u16string &uaprof) override;
373     int32_t OnRilAdapterHostDied(int32_t slotId);
374 
375 private:
376     bool Init();
377     void WaitCoreServiceToInit();
378     bool CheckSmsPermission(const sptr<ISendShortMessageCallback> &sendCallback);
379     bool ValidDestinationAddress(std::string desAddr);
380     void TrimSmscAddr(std::string &sca);
381     bool CheckSimMessageIndexValid(int32_t slotId, uint32_t msgIndex);
382     void InsertSmsMmsInfo(int32_t slotId, uint16_t sessionId, const std::string &number,
383         const std::string &text, uint16_t &dataBaseId);
384     bool InsertSession(bool isNewSession, uint16_t messageCount, const std::string &number, const std::string &text);
385     bool QuerySessionByTelephone(const std::string &telephone, uint16_t &sessionId, uint16_t &messageCount);
386     void UpdateSmsContact(const std::string &address);
387     bool IsInfoMsg(const std::string &telephone);
388     void UpdatePredicatesByPhoneNum(DataShare::DataSharePredicates &predicates, const std::string &phoneNum);
389     bool IsValidCBRangeList(const std::vector<int32_t>& messageIds);
390 
391 private:
392     int64_t bindTime_ = 0;
393     int64_t endTime_ = 0;
394     int64_t spendTime_ = 0;
395     bool registerToService_ = false;
396     static constexpr const char *SMS_MMS_INFO_MSG_STATE_SENDING = "1";
397     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
398     std::shared_ptr<SmsStateHandler> smsStateHandler_;
399     const std::string SMS_MMS_INFO = "datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info";
400     const std::string SMS_SESSION = "datashare:///com.ohos.smsmmsability/sms_mms/session";
401 };
402 } // namespace Telephony
403 } // namespace OHOS
404 #endif