• 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 
39     /**
40      * Sends a text Type SMS message.
41      * @param slotId Indicates the card slot index number,
42      * ranging from {@code 0} to the maximum card slot index number supported by
43      * the device
44      * @param desAddr Indicates the destination address
45      * @param scAddr Indicates the sms center address
46      * @param text Indicates sms content
47      * @param sendCallback Indicates callback for send out
48      * @param deliverCallback Indicates callback for delivery to destination user
49      * @return Returns {@code 0} if send message success.
50      */
51     int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
52         const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback,
53         const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
54 
55     /**
56      * Sends a data Type SMS message.
57      * @param slotId Indicates the card slot index number,
58      * ranging from {@code 0} to the maximum card slot index number supported by
59      * the device
60      * @param desAddr Indicates the destination address
61      * @param scAddr Indicates the sms center address
62      * @param port Indicates the port of data sms
63      * @param data Indicates the array of data sms
64      * @param dataLen Indicates the array length of data sms
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 SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr, uint16_t port,
70         const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback,
71         const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override;
72 
73     /**
74      * Sets the address for the Short Message Service Center (SMSC) based on a
75      * specified slot ID
76      * @param slotId Indicates the card slot index number,
77      * ranging from {@code 0} to the maximum card slot index number supported by
78      * the device
79      * @param scAddr Indicates the sms center address
80      * @return Returns {@code 0} if set smsc success
81      */
82     int32_t SetSmscAddr(int32_t slotId, const std::u16string &scAddr) override;
83 
84     /**
85      * @brief GetSmscAddr
86      * Obtains the SMSC address based on a specified slot ID.
87      * @param slotId Indicates the card slot index number,
88      * ranging from {@code 0} to the maximum card slot index number supported by
89      * the device
90      * @param smscAddress [out]
91      * @return Returns {@code 0} if get smsc success.
92      */
93     int32_t GetSmscAddr(int32_t slotId, std::u16string &smscAddress) override;
94 
95     /**
96      * @brief AddSimMessage
97      * Add a sms to sim card.
98      * @param slotId [in]
99      * @param smsc [in]
100      * @param pdu [in]
101      * @param status [in]
102      * @return Returns {@code true} if add sim success; returns {@code false}
103 
104      */
105     int32_t AddSimMessage(
106         int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status) override;
107 
108     /**
109      * @brief DelSimMessage
110      * Delete a sms in the sim card.
111      * @param slotId [in]
112      * @param msgIndex [in]
113      * @return Returns {@code true} if delete sim success; returns {@code false}
114      */
115     int32_t DelSimMessage(int32_t slotId, uint32_t msgIndex) override;
116 
117     /**
118      * @brief UpdateSimMessage
119      * Update a sms in the sim card.
120      * @param slotId Indicates the card slot index number,
121      * ranging from {@code 0} to the maximum card slot index number supported by
122      * the device
123      * @param msgIndex Indicates the sim sms index in sim card
124      * @param newStatusIndicates the sms status, read or not
125      * @param pdu Indicates the sms pdu data
126      * @param smsc Indicates the sms center address
127      * @return Returns {@code 0} if update sim success
128      */
129     int32_t UpdateSimMessage(int32_t slotId, uint32_t msgIndex, SimMessageStatus newStatus, const std::u16string &pdu,
130         const std::u16string &smsc) override;
131 
132     /**
133      * @brief GetAllSimMessages
134      * Get sim card all the sms.
135      * @param slotId Indicates the card slot index number,
136      * ranging from {@code 0} to the maximum card slot index number supported by
137      * the device
138      * @param message Indicates all SMS messages of sim card
139      * @return Returns {@code 0} if get all sim messages success
140      */
141     int32_t GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message) override;
142 
143     /**
144      * @brief SetCBConfig
145      * Configure a cell broadcast in a certain band range.
146      * @param slotId [in]
147      * @param enable [in]
148      * @param fromMsgId [in]
149      * @param toMsgId [in]
150      * @param netType [in]
151      * @return Returns {@code 0} if set CB config success
152      */
153     int32_t SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType) override;
154 
155     /**
156      * @brief SetImsSmsConfig enable or disable IMS SMS.
157      * @param slotId Indicates the card slot index number,
158      * ranging from {@code 0} to the maximum card slot index number supported by the device.
159      * @param enable Indicates enable or disable Ims sms
160      * ranging {@code 0} disable Ims sms {@code 1} enable Ims sms
161      * @return Returns {@code true} if enable or disable Ims Sms success; returns {@code false} otherwise.
162      */
163     bool SetImsSmsConfig(int32_t slotId, int32_t enable) override;
164 
165     /**
166      * @brief SetDefaultSmsSlotId
167      * Set the Default Sms Slot Id To SmsService
168      * @param slotId Indicates the card slot index number,
169      * ranging from {@code 0} to the maximum card slot index number supported by
170      * the device
171      * @return Returns {@code 0} if set default sms slot id success
172      */
173     int32_t SetDefaultSmsSlotId(int32_t slotId) override;
174 
175     /**
176      * @brief GetDefaultSmsSlotId
177      * Get the Default Sms Slot Id From SmsService
178      * @return int32_t
179      */
180     int32_t GetDefaultSmsSlotId() override;
181 
182     /**
183      * @brief SplitMessage
184      * calculate Sms Message Split Segment count
185      * @param Indicates input message
186      * @param splitMessage Indicates the split information
187      * @return Returns {@code 0} if split message success
188      */
189     int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage) override;
190 
191     /**
192      * @brief GetSmsSegmentsInfo
193      * calculate the Sms Message Segments Info
194      * @param slotId Indicates the card slot index number,
195      * ranging from {@code 0} to the maximum card slot index number supported by
196      * the device
197      * @param message Indicates input message
198      * @param force7BitCode Indicates sms encode type, 7bit or not
199      * @param info Indicates output sms segment
200      * @return Returns {@code 0} if get sms segments info
201      */
202     int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode,
203         ISmsServiceInterface::SmsSegmentsInfo &info) override;
204 
205     /**
206      * @brief IsImsSmsSupported
207      * Check Sms Is supported Ims newtwork
208      * Hide this for inner system use
209      * @param slotId Indicates the card slot index number,
210      * ranging from {@code 0} to the maximum card slot index number supported by
211      * the device
212      * @param isSupported Whether ims SMS is supported
213      * @return Returns {@code 0} if successful
214      */
215     int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) override;
216 
217     /**
218      * @brief GetImsShortMessageFormat
219      * Get the Ims Short Message Format 3gpp/3gpp2
220      * Hide this for inner system use
221      * @return int32_t
222      */
223     int32_t GetImsShortMessageFormat(std::u16string &format) override;
224 
225     /**
226      * @brief HasSmsCapability
227      * Check whether it is supported Sms Capability
228      * @return true
229      * @return false
230      */
231     bool HasSmsCapability() override;
232 
233     /**
234      * @brief GetServiceRunningState
235      * Get service running state
236      * @return ServiceRunningState
237      */
238     int32_t GetServiceRunningState();
239 
240     /**
241      * @brief GetSpendTime
242      * Get service start spend time
243      * @return Spend time
244      */
245     int64_t GetSpendTime();
246 
247     /**
248      * @brief GetEndTime
249      * Get service start finish time
250      * @return Spend time
251      */
252     int64_t GetEndTime();
253 
254     /**
255      * create a short message
256      * @param pdu Indicates pdu code,
257      * @param specification Indicates 3gpp or 3gpp2
258      * @param ShortMessage Indicates a short message object
259      * @return Returns {@code 0} if CreateMessage success
260      */
261     int32_t CreateMessage(std::string pdu, std::string specification, ShortMessage &message) override;
262 
263     /**
264      * mms base64 encode
265      * @param src Indicates source string,
266      * @param dest Indicates destination string
267      * @return Returns {@code true} if encode success; returns {@code false} otherwise
268      */
269     bool GetBase64Encode(std::string src, std::string &dest) override;
270 
271     /**
272      * mms base64 decode
273      * @param src Indicates source string,
274      * @param dest Indicates destination string
275      * @return Returns {@code true} if decode success; returns {@code false} otherwise
276      */
277     bool GetBase64Decode(std::string src, std::string &dest) override;
278 
279     /**
280      * Get Encode String
281      * @param encodeString Indicates output string,
282      * @param charset Indicates character set,
283      * @param valLength Indicates input string length,
284      * @param strEncodeString Indicates input string
285      * @return Returns {@code true} if decode success; returns {@code false} otherwise
286      */
287     bool GetEncodeStringFunc(
288         std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString) override;
289 
290 private:
291     bool Init();
292     void WaitCoreServiceToInit();
293     bool NoPermissionOrParametersCheckFail(
294         int32_t slotId, const std::u16string desAddr, const sptr<ISendShortMessageCallback> &sendCallback);
295     bool ValidDestinationAddress(std::string desAddr);
296     void TrimSmscAddr(std::string &sca);
297 
298 private:
299     int64_t bindTime_ = 0;
300     int64_t endTime_ = 0;
301     int64_t spendTime_ = 0;
302     bool registerToService_ = false;
303     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
304     std::shared_ptr<SmsStateHandler> smsStateHandler_;
305 };
306 } // namespace Telephony
307 } // namespace OHOS
308 #endif