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 NAPI_SMS_H
17 #define NAPI_SMS_H
18 
19 #include <codecvt>
20 #include <cstring>
21 #include <locale>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "base_context.h"
27 #include "i_sms_service_interface.h"
28 #include "napi/native_api.h"
29 #include "napi/native_node_api.h"
30 #include "napi_util.h"
31 #include "short_message.h"
32 #include "sms_service_manager_client.h"
33 #include "telephony_log_wrapper.h"
34 #include "telephony_types.h"
35 
36 namespace OHOS {
37 namespace Telephony {
38 const int32_t DEFAULT_ERROR = -1;
39 const int32_t MESSAGE_UNKNOWN_STATUS = -1;
40 const int32_t MESSAGE_PARAMETER_NOT_MATCH = 0;
41 const int32_t TEXT_MESSAGE_PARAMETER_MATCH = 1;
42 const int32_t RAW_DATA_MESSAGE_PARAMETER_MATCH = 2;
43 constexpr int32_t MAX_TEXT_SHORT_MESSAGE_LENGTH = 4096;
44 constexpr size_t BUFF_LENGTH = 1001;
45 constexpr int32_t PROPERTY_NAME_SIZE = 1001;
46 constexpr int32_t NORMAL_STRING_SIZE = 1001;
47 constexpr int32_t NONE_PARAMETER = 0;
48 constexpr int32_t ONE_PARAMETER = 1;
49 constexpr int32_t TWO_PARAMETERS = 2;
50 constexpr int32_t THREE_PARAMETERS = 3;
51 constexpr int32_t FOUR_PARAMETERS = 4;
52 constexpr int32_t PARAMETERS_INDEX_TWO = 2;
53 constexpr int32_t INVALID_PORT = -1;
54 constexpr int32_t MIN_PORT = 0;
55 constexpr int32_t MAX_PORT = 0xffff;
56 
57 enum class ShortMessageClass {
58     /** Indicates an unknown type. */
59     UNKNOWN,
60     /** Indicates an instant message, which is displayed immediately after being received. */
61     INSTANT_MESSAGE,
62     /** Indicates an SMS message that can be stored on the device or SIM card based on the storage status. */
63     OPTIONAL_MESSAGE,
64     /** Indicates an SMS message containing SIM card information, which is to be stored in a SIM card. */
65     SIM_MESSAGE,
66     /** Indicates an SMS message to be forwarded to another device. */
67     FORWARD_MESSAGE
68 };
69 
70 enum SendSmsResult {
71     /**
72      * Indicates that the SMS message is successfully sent.
73      */
74     SEND_SMS_SUCCESS = 0,
75 
76     /**
77      * Indicates that sending the SMS message fails due to an unknown reason.
78      */
79     SEND_SMS_FAILURE_UNKNOWN = 1,
80 
81     /**
82      * Indicates that sending the SMS fails because the modem is powered off.
83      */
84     SEND_SMS_FAILURE_RADIO_OFF = 2,
85 
86     /**
87      * Indicates that sending the SMS message fails because the network is unavailable
88      * or does not support sending or reception of SMS messages.
89      */
90     SEND_SMS_FAILURE_SERVICE_UNAVAILABLE = 3
91 };
92 
93 enum RanType {
94     TYPE_GSM = 1,
95     TYPE_CDMA = 2,
96 };
97 
98 struct SendMessageContext : BaseContext {
99     int32_t slotId = DEFAULT_SIM_SLOT_ID;
100     std::u16string destinationHost = u"";
101     std::u16string serviceCenter = u"";
102     std::u16string textContent = u"";
103     std::vector<uint8_t> rawDataContent;
104     napi_ref thisVarRef = nullptr;
105     napi_async_work work = nullptr;
106     napi_ref sendCallbackRef = nullptr;
107     napi_ref deliveryCallbackRef = nullptr;
108     int32_t messageType = MESSAGE_PARAMETER_NOT_MATCH;
109     uint16_t destinationPort = 0;
110 };
111 
112 struct CreateMessageContext : BaseContext {
113     std::vector<unsigned char> pdu {};
114     std::string specification = "";
115     ShortMessage *shortMessage = nullptr;
116 };
117 
118 struct SetDefaultSmsSlotIdContext : BaseContext {
119     int32_t slotId = DEFAULT_SIM_SLOT_ID;
120     bool setResult = false;
121 };
122 
123 struct IsImsSmsSupportedContext : BaseContext {
124     int32_t slotId = DEFAULT_SIM_SLOT_ID;
125     bool setResult = false;
126 };
127 
128 struct GetDefaultSmsSlotIdContext : BaseContext {
129     int32_t defaultSmsSlotId = DEFAULT_SIM_SLOT_ID;
130 };
131 
132 struct GetDefaultSmsSimIdContext : BaseContext {
133     int32_t defaultSmsSimId = DEFAULT_SIM_SLOT_ID;
134 };
135 
136 struct SetSmscAddrContext : BaseContext {
137     int32_t slotId = DEFAULT_SIM_SLOT_ID;
138     std::string smscAddr = "";
139     bool setResult = false;
140 };
141 
142 struct GetSmscAddrContext : BaseContext {
143     int32_t slotId = DEFAULT_SIM_SLOT_ID;
144     std::string smscAddr = "";
145 };
146 
147 struct AddSimMessageContext : BaseContext {
148     int32_t slotId = DEFAULT_SIM_SLOT_ID;
149     std::string smsc = "";
150     std::string pdu = "";
151     int32_t status = MESSAGE_UNKNOWN_STATUS;
152     bool addResult = false;
153 };
154 
155 struct DelSimMessageContext : BaseContext {
156     int32_t slotId = DEFAULT_SIM_SLOT_ID;
157     int32_t msgIndex = DEFAULT_ERROR;
158     bool deleteResult = false;
159 };
160 
161 struct UpdateSimMessageContext : BaseContext {
162     int32_t slotId = DEFAULT_SIM_SLOT_ID;
163     int32_t msgIndex = DEFAULT_ERROR;
164     bool updateResult = false;
165     int32_t newStatus = MESSAGE_UNKNOWN_STATUS;
166     std::string pdu = "";
167     std::string smsc = "";
168 };
169 
170 struct GetAllSimMessagesContext : BaseContext {
171     int32_t slotId = DEFAULT_SIM_SLOT_ID;
172     std::vector<ShortMessage> messageArray;
173 };
174 
175 struct CBConfigContext : BaseContext {
176     int32_t slotId = DEFAULT_SIM_SLOT_ID;
177     bool enable = false;
178     int32_t startMessageId = DEFAULT_ERROR;
179     int32_t endMessageId = DEFAULT_ERROR;
180     int32_t ranType = DEFAULT_ERROR;
181     bool setResult = false;
182 };
183 
184 struct SplitMessageContext : BaseContext {
185     int32_t slotId = DEFAULT_SIM_SLOT_ID;
186     std::string content = "";
187     std::vector<std::u16string> messageArray;
188 };
189 
190 struct GetSmsSegmentsInfoContext : BaseContext {
191     int32_t slotId = DEFAULT_SIM_SLOT_ID;
192     std::string content = "";
193     bool force7BitCode = false;
194     int32_t splitCount = 0;
195     int32_t encodeCount = 0;
196     int32_t encodeCountRemaining = 0;
197     ISmsServiceInterface::SmsSegmentsInfo::SmsSegmentCodeScheme scheme;
198 };
199 
200 template<typename T>
201 struct SingleValueContext : BaseContext {
202     T value;
203 };
204 } // namespace Telephony
205 } // namespace OHOS
206 #endif // NAPI_SMS_H