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_MANAGER_CLIENT_H
17 #define SMS_SERVICE_MANAGER_CLIENT_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "i_sms_service_interface.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace Telephony {
28 class SmsServiceManagerClient : public std::enable_shared_from_this<SmsServiceManagerClient> {
29     DECLARE_DELAYED_SINGLETON(SmsServiceManagerClient)
30 public:
31     /**
32      * @brief Init the proxy of SmsService.
33      */
34     bool InitSmsServiceProxy();
35 
36     /**
37      * @brief Reset the proxy of SmsService.
38      */
39     void ResetSmsServiceProxy();
40 
41     /**
42      * @brief Set the Default Sms Slot Id To SmsService
43      *
44      * @param slotId [in], indicates the card slot index number,
45      * ranging from {@code 0} to the maximum card slot index number supported by the device.
46      * @return int32_t, returns {@code 0} if success.
47      */
48     int32_t SetDefaultSmsSlotId(int32_t slotId);
49 
50     /**
51      * @brief Get the Default Sms Slot Id From SmsService
52      *
53      * @return int32_t, returns {@code 0} if success.
54      */
55     int32_t GetDefaultSmsSlotId();
56 
57     /**
58      * @brief Get the Default Sms Sim Id From SmsService
59      *
60      * @param simId [out], indicates the sms sim index number.
61      * @return int32_t, returns {@code 0} if success.
62      */
63     int32_t GetDefaultSmsSimId(int32_t &simId);
64 
65     /**
66      * @brief Sends a text type SMS message.
67      *
68      * @param slotId [in], indicates the card slot index number,
69      * ranging from {@code 0} to the maximum card slot index number supported by the device.
70      * @param desAddr [in], indicates the destination address.
71      * @param scAddr [in], indicates the sms center address.
72      * @param text [in], indicates sms content.
73      * @param sendCallback [in], indicates callback for send out.
74      * @param deliverCallback [in], indicates callback for delivery to destination user.
75      * @return int32_t, returns {@code 0} if success.
76      */
77     int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
78         const std::u16string text, const sptr<ISendShortMessageCallback> &callback,
79         const sptr<IDeliveryShortMessageCallback> &deliveryCallback);
80 
81     /**
82      * @brief Sends a data type SMS message.
83      *
84      * @param slotId [in], indicates the card slot index number,
85      * ranging from {@code 0} to the maximum card slot index number supported by the device.
86      * @param desAddr [in], indicates the destination address.
87      * @param scAddr [in], indicates the sms center address.
88      * @param port [in], indicates the port of data sms.
89      * @param data [in], indicates the array of data sms.
90      * @param dataLen [in], indicates the array length of data sms.
91      * @param sendCallback [in], indicates callback for send out.
92      * @param deliverCallback [in], indicates callback for delivery to destination user.
93      * @return int32_t, returns {@code 0} if success.
94      */
95     int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr,
96         uint16_t port, const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &callback,
97         const sptr<IDeliveryShortMessageCallback> &deliveryCallback);
98     /**
99      * @brief Sets the address for the Short Message Service Center (SMSC) based on a specified slot ID.
100      *
101      * @param slotId [in], indicates the card slot index number,
102      * ranging from {@code 0} to the maximum card slot index number supported by the device.
103      * @param scAddr [in], indicates the sms center address.
104      * @return int32_t, returns {@code 0} if success.
105      */
106     int32_t SetScAddress(int32_t slotId, const std::u16string &scAddr);
107 
108     /**
109      * @brief Obtains the SMSC address based on a specified slot ID.
110      *
111      * @param slotId [in], indicates the card slot index number,
112      * ranging from {@code 0} to the maximum card slot index number supported by the device.
113      * @param smscAddress [out]
114      * @return int32_t, returns {@code 0} if success.
115      */
116     int32_t GetScAddress(int32_t slotId, std::u16string &smscAddress);
117 
118     /**
119      * @brief Add a sms to sim card.
120      *
121      * @param slotId [in], indicates the card slot index number,
122      * ranging from {@code 0} to the maximum card slot index number supported by the device.
123      * @param smsc [in], indicates the short message service center.
124      * @param pdu [in], indicates the protocol data unit of message.
125      * @param status [in], indicates the status of sim message.
126      * @return int32_t, returns {@code 0} if success.
127      */
128     int32_t AddSimMessage(int32_t slotId, const std::u16string &smsc, const std::u16string &pdu,
129         ISmsServiceInterface::SimMessageStatus status);
130 
131     /**
132      * @brief Delete a sms in the sim card.
133      *
134      * @param slotId [in], indicates the card slot index number,
135      * ranging from {@code 0} to the maximum card slot index number supported by the device.
136      * @param msgIndex [in], indicates the message index.
137      * @return int32_t, returns {@code 0} if success.
138      */
139     int32_t DelSimMessage(int32_t slotId, uint32_t msgIndex);
140 
141     /**
142      * @brief Update a sms in the sim card.
143      *
144      * @param slotId [in], indicates the card slot index number,
145      * ranging from {@code 0} to the maximum card slot index number supported by the device.
146      * @param msgIndex [in], indicates the message index.
147      * @param newStatus [in], indicates the new status of the sim message.
148      * @param pdu [in], indicates the protocol data unit of message.
149      * @param smsc [in], indicates the short message service center.
150      * @return int32_t, returns {@code 0} if success.
151      */
152     int32_t UpdateSimMessage(int32_t slotId, uint32_t msgIndex, ISmsServiceInterface::SimMessageStatus newStatus,
153         const std::u16string &pdu, const std::u16string &smsc);
154 
155     /**
156      * @brief Get sim card all the sms.
157      *
158      * @param slotId [in], indicates the card slot index number,
159      * ranging from {@code 0} to the maximum card slot index number supported by the device.
160      * @param message [out], indicates all SMS messages of sim card.
161      * @return int32_t, returns {@code 0} if success.
162      */
163     int32_t GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message);
164 
165     /**
166      * @brief Configure a cell broadcast in a certain band range.
167      *
168      * @param slotId [in], indicates the card slot index number,
169      * ranging from {@code 0} to the maximum card slot index number supported by the device.
170      * @param enable [in], indicates whether to enable cell broadcast.
171      * @param fromMsgId [in], indicates the start message ID.
172      * @param toMsgId [in], indicates the end message ID.
173      * @param netType [in], indicates the network type.
174      * @return int32_t, returns {@code 0} if success.
175      */
176     int32_t SetCBConfig(int32_t slotId, bool enable, uint32_t startMessageId, uint32_t endMessageId, uint8_t ranType);
177 
178     /**
179      * @brief SetImsSmsConfig enable or disable IMS SMS.
180      *
181      * @param slotId Indicates the card slot index number,
182      * ranging from {@code 0} to the maximum card slot index number supported by the device.
183      * @param enable Indicates enable or disable Ims sms
184      * ranging {@code 0} disable Ims sms {@code 1} enable Ims sms
185      * @return Returns {@code true} if enable or disable Ims Sms success; returns {@code false} otherwise.
186      */
187     bool SetImsSmsConfig(int32_t slotId, int32_t enable);
188 
189     /**
190      * @brief Calculate Sms Message Split Segment count
191      *
192      * @param message [in], indicates input message.
193      * @param splitMessage [out], indicates the split information.
194      * @return int32_t, returns {@code 0} if success.
195      */
196     int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage);
197 
198     /**
199      * @brief Calculate the Sms Message Segments Info
200      *
201      * @param slotId [in], indicates the card slot index number,
202      * ranging from {@code 0} to the maximum card slot index number supported by the device.
203      * @param message [in], indicates input message.
204      * @param force7BitCode [in], indicates sms encode type, 7bit or not.
205      * @param segInfo [out], indicates output sms segment.
206      * @return int32_t, returns {@code 0} if get sms segments info.
207      */
208     int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode,
209         ISmsServiceInterface::SmsSegmentsInfo &segInfo);
210 
211     /**
212      * @brief Check Sms Is supported Ims newtwork
213      *
214      * @param slotId Indicates the card slot index number, ranging from {@code 0} to the maximum card
215      * slot index number supported by the device.
216      * @param isSupported Whether ims SMS is supported.
217      * @return int32_t, returns {@code 0} if success.
218      */
219     int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported);
220 
221     /**
222      * @brief Get the Ims Short Message Format 3gpp/3gpp2
223      *
224      * @param format Ims short message format
225      * @return int32_t, returns {@code 0} if success.
226      */
227     int32_t GetImsShortMessageFormat(std::u16string &format);
228 
229     /**
230      * @brief Check whether it is supported Sms Capability
231      *
232      * @return {@code true} if UE has sms capability; returns {@code false} otherwise.
233      */
234     bool HasSmsCapability();
235 
236     /**
237      * @brief Create a short message
238      *
239      * @param pdu Indicates pdu code,
240      * @param specification Indicates 3gpp or 3gpp2
241      * @param message Indicates a short message object
242      * @return Returns {@code 0} if CreateMessage success
243      */
244     int32_t CreateMessage(std::string pdu, std::string specification, ShortMessage &message);
245 
246     /**
247      * @brief Mms base64 encode
248      *
249      * @param src Indicates source string,
250      * @param dest Indicates destination string
251      * @return Returns {@code true} if encode success; returns {@code false} otherwise
252      */
253     bool GetBase64Encode(std::string src, std::string &dest);
254 
255     /**
256      * @brief Mms base64 decode
257      *
258      * @param src Indicates source string,
259      * @param dest Indicates destination string
260      * @return Returns {@code true} if decode success; returns {@code false} otherwise
261      */
262     bool GetBase64Decode(std::string src, std::string &dest);
263 
264     /**
265      * @brief Get Encode String
266      *
267      * @param encodeString Indicates output string,
268      * @param charset Indicates character set,
269      * @param valLength Indicates input string length,
270      * @param strEncodeString Indicates input string
271      * @return Returns {@code true} if decode success; returns {@code false} otherwise
272      */
273     bool GetEncodeStringFunc(
274         std::string &encodeString, uint32_t charset, uint32_t valLength, std::string strEncodeString);
275 
276     /**
277      * Send a Mms.
278      * @param slotId Indicates the card slot index number,
279      * ranging from {@code 0} to the maximum card slot index number supported by
280      * the device
281      * @param mmsc Indicates service center of mms
282      * @param data Indicates mms pdu byte array
283      * @param ua Indicates mms user agent
284      * @param uaprof Indicates mms user agent profile
285      * @return Returns {@code 0} if send mms success; returns {@code false} otherwise
286      */
287     int32_t SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua,
288         const std::u16string &uaprof);
289 
290     /**
291      * Download a Mms.
292      * @param slotId Indicates the card slot index number,
293      * ranging from {@code 0} to the maximum card slot index number supported by
294      * the device
295      * @param mmsc Indicates service center of mms
296      * @param data Indicates mms pdu byte array
297      * @param ua Indicates mms user agent
298      * @param uaprof Indicates mms user agent profile
299      * @return Returns {@code 0} if download mms success; returns {@code false} otherwise
300      */
301     int32_t DownloadMms(int32_t slotId, const std::u16string &mmsc, std::u16string &data, const std::u16string &ua,
302         const std::u16string &uaprof);
303 
304 private:
305     std::mutex mutex_;
306     sptr<ISmsServiceInterface> smsServiceInterface_;
307     sptr<IRemoteObject::DeathRecipient> recipient_;
308 };
309 
310 /**
311  * @brief Enumerates the result of sending SMS.
312  */
313 enum class SmsSendResult {
314     /**
315      * Indicates that the SMS message is successfully sent.
316      */
317     SEND_SMS_SUCCESS = 0,
318     /**
319      * Indicates that sending the SMS message fails due to an unknown reason.
320      */
321     SEND_SMS_FAILURE_UNKNOWN = 1,
322     /**
323      * Indicates that sending the SMS fails because the modem is powered off.
324      */
325     SEND_SMS_FAILURE_RADIO_OFF = 2,
326     /**
327      * Indicates that sending the SMS message fails because the network is unavailable
328      * or does not support sending or reception of SMS messages.
329      */
330     SEND_SMS_FAILURE_SERVICE_UNAVAILABLE = 3
331 };
332 } // namespace Telephony
333 } // namespace OHOS
334 #endif // SMS_SERVICE_MANAGER_CLIENT_H
335