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 40 /** 41 * Sends a text Type SMS message. 42 * @param slotId Indicates the card slot index number, 43 * ranging from {@code 0} to the maximum card slot index number supported by 44 * the device 45 * @param desAddr Indicates the destination address 46 * @param scAddr Indicates the sms center address 47 * @param text Indicates sms content 48 * @param sendCallback Indicates callback for send out 49 * @param deliverCallback Indicates callback for delivery to destination user 50 * @return Returns {@code 0} if send message success. 51 */ 52 int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr, 53 const std::u16string text, const sptr<ISendShortMessageCallback> &sendCallback, 54 const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override; 55 56 /** 57 * Sends a data Type SMS message. 58 * @param slotId Indicates the card slot index number, 59 * ranging from {@code 0} to the maximum card slot index number supported by 60 * the device 61 * @param desAddr Indicates the destination address 62 * @param scAddr Indicates the sms center address 63 * @param port Indicates the port of data sms 64 * @param data Indicates the array of data sms 65 * @param dataLen Indicates the array length of data sms 66 * @param sendCallback Indicates callback for send out 67 * @param deliverCallback Indicates callback for delivery to destination user 68 * @return Returns {@code 0} if send message success. 69 */ 70 int32_t SendMessage(int32_t slotId, const std::u16string desAddr, const std::u16string scAddr, uint16_t port, 71 const uint8_t *data, uint16_t dataLen, const sptr<ISendShortMessageCallback> &sendCallback, 72 const sptr<IDeliveryShortMessageCallback> &deliveryCallback) override; 73 74 /** 75 * Sets the address for the Short Message Service Center (SMSC) based on a 76 * specified slot ID 77 * @param slotId Indicates the card slot index number, 78 * ranging from {@code 0} to the maximum card slot index number supported by 79 * the device 80 * @param scAddr Indicates the sms center address 81 * @return Returns {@code 0} if set smsc success 82 */ 83 int32_t SetSmscAddr(int32_t slotId, const std::u16string &scAddr) override; 84 85 /** 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 * Add a sms to sim card. 97 * @param slotId Indicates the card slot index number, 98 * ranging from {@code 0} to the maximum card slot index number supported by 99 * the device 100 * @param smsc Indicates the sms center address 101 * @param pdu Indicates the sms pdu data 102 * @param status Indicates the sms status, read or not 103 * @return Returns {@code true} if add sim success; returns {@code false} 104 */ 105 int32_t AddSimMessage( 106 int32_t slotId, const std::u16string &smsc, const std::u16string &pdu, SimMessageStatus status) override; 107 108 /** 109 * Delete a sms in the sim card. 110 * @param slotId Indicates the card slot index number, 111 * ranging from {@code 0} to the maximum card slot index number supported by 112 * the device 113 * @param msgIndex Indicates the sim sms index in sim card 114 * @return Returns {@code true} if delete sim success; returns {@code false} 115 */ 116 int32_t DelSimMessage(int32_t slotId, uint32_t msgIndex) override; 117 118 /** 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 * Get sim card all the sms. 134 * @param slotId Indicates the card slot index number, 135 * ranging from {@code 0} to the maximum card slot index number supported by 136 * the device 137 * @param message Indicates all SMS messages of sim card 138 * @return Returns {@code 0} if get all sim messages success 139 */ 140 int32_t GetAllSimMessages(int32_t slotId, std::vector<ShortMessage> &message) override; 141 142 /** 143 * Configure a cell broadcast in a certain band range. 144 * @param slotId [in] 145 * @param enable [in] 146 * @param fromMsgId [in] 147 * @param toMsgId [in] 148 * @param netType [in] 149 * @return Returns {@code 0} if set CB config success 150 */ 151 int32_t SetCBConfig(int32_t slotId, bool enable, uint32_t fromMsgId, uint32_t toMsgId, uint8_t netType) override; 152 153 /** 154 * enable or disable IMS SMS. 155 * @param slotId Indicates the card slot index number, 156 * ranging from {@code 0} to the maximum card slot index number supported by 157 * the device 158 * @param enable Indicates enable or disable Ims sms 159 * ranging {@code 0} disable Ims sms {@code 1} enable Ims sms 160 * @return Returns {@code true} if enable or disable Ims Sms success; returns 161 * {@code false} otherwise 162 */ 163 bool SetImsSmsConfig(int32_t slotId, int32_t enable) override; 164 165 /** 166 * Set the Default Sms Slot Id To SmsService 167 * @param slotId Indicates the card slot index number, 168 * ranging from {@code 0} to the maximum card slot index number supported by 169 * the device 170 * @return Returns {@code 0} if set default sms slot id success 171 */ 172 int32_t SetDefaultSmsSlotId(int32_t slotId) override; 173 174 /** 175 * Get the Default Sms Slot Id From SmsService 176 * @return Returns default sms slot id 177 */ 178 int32_t GetDefaultSmsSlotId() override; 179 180 /** 181 * @brief GetDefaultSmsSlotId 182 * Get the Default Sms Sim Id From SmsService 183 * @return int32_t 184 */ 185 int32_t GetDefaultSmsSimId(int32_t &simId) override; 186 187 /** 188 * @brief SplitMessage 189 * calculate Sms Message Split Segment count 190 * @param Indicates input message 191 * @param splitMessage Indicates the split information 192 * @return Returns {@code 0} if split message success 193 */ 194 int32_t SplitMessage(const std::u16string &message, std::vector<std::u16string> &splitMessage) override; 195 196 /** 197 * calculate the Sms Message Segments Info 198 * @param slotId Indicates the card slot index number, 199 * ranging from {@code 0} to the maximum card slot index number supported by 200 * the device 201 * @param message Indicates input message 202 * @param force7BitCode Indicates sms encode type, 7bit or not 203 * @param info Indicates output sms segment 204 * @return Returns {@code 0} if get sms segments info 205 */ 206 int32_t GetSmsSegmentsInfo(int32_t slotId, const std::u16string &message, bool force7BitCode, 207 ISmsServiceInterface::SmsSegmentsInfo &info) override; 208 209 /** 210 * Check Sms Is supported Ims newtwork 211 * Hide this for inner system use 212 * @param slotId Indicates the card slot index number, 213 * ranging from {@code 0} to the maximum card slot index number supported by 214 * the device 215 * @param isSupported Whether ims SMS is supported 216 * @return Returns {@code 0} if successful 217 */ 218 int32_t IsImsSmsSupported(int32_t slotId, bool &isSupported) override; 219 220 /** 221 * Get the Ims Short Message Format 3gpp/3gpp2 222 * Hide this for inner system use 223 * @return int32_t 224 */ 225 int32_t GetImsShortMessageFormat(std::u16string &format) override; 226 227 /** 228 * Check whether it is supported Sms Capability 229 * @return Returns {@code true} if support sms; returns {@code 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 /** 291 * Send 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 send mms success; returns {@code false} otherwise 300 */ 301 int32_t SendMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data, const std::u16string &ua, 302 const std::u16string &uaprof) override; 303 304 /** 305 * Download a Mms. 306 * @param slotId Indicates the card slot index number, 307 * ranging from {@code 0} to the maximum card slot index number supported by 308 * the device 309 * @param mmsc Indicates service center of mms 310 * @param data Indicates mms pdu byte array 311 * @param ua Indicates mms user agent 312 * @param uaprof Indicates mms user agent profile 313 * @return Returns {@code 0} if download mms success; returns {@code false} otherwise 314 */ 315 int32_t DownloadMms(int32_t slotId, const std::u16string &mmsc, const std::u16string &data, 316 const std::u16string &ua, const std::u16string &uaprof) override; 317 318 private: 319 bool Init(); 320 void WaitCoreServiceToInit(); 321 bool CheckSmsPermission(const sptr<ISendShortMessageCallback> &sendCallback); 322 bool ValidDestinationAddress(std::string desAddr); 323 void TrimSmscAddr(std::string &sca); 324 bool CheckSimMessageIndexValid(int32_t slotId, uint32_t msgIndex); 325 void InsertSmsMmsInfo(int32_t slotId, uint16_t sessionId, const std::string &number, const std::string &text); 326 bool InsertSession(bool isNewSession, uint16_t messageCount, const std::string &number, const std::string &text); 327 bool QuerySessionByTelephone(const std::string &telephone, uint16_t &sessionId, uint16_t &messageCount); 328 329 private: 330 int64_t bindTime_ = 0; 331 int64_t endTime_ = 0; 332 int64_t spendTime_ = 0; 333 bool registerToService_ = false; 334 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 335 std::shared_ptr<SmsStateHandler> smsStateHandler_; 336 const std::string SMS_MMS_INFO = "datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info"; 337 const std::string SMS_SESSION = "datashare:///com.ohos.smsmmsability/sms_mms/session"; 338 }; 339 } // namespace Telephony 340 } // namespace OHOS 341 #endif