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 #include "napi_mms.h"
16 
17 #include "ability.h"
18 #include "napi_base_context.h"
19 #include "napi_mms_pdu.h"
20 #include "napi_mms_pdu_helper.h"
21 #include "sms_constants_utils.h"
22 #include "telephony_permission.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 namespace {
27 const std::string mmsTypeKey = "mmsType";
28 const std::string attachmentKey = "attachment";
29 static const int32_t DEFAULT_REF_COUNT = 1;
30 } // namespace
31 
SetPropertyArray(napi_env env,napi_value object,const std::string & name,MmsAttachmentContext & context)32 static void SetPropertyArray(napi_env env, napi_value object, const std::string &name, MmsAttachmentContext &context)
33 {
34     napi_value array = nullptr;
35     napi_create_array(env, &array);
36     for (uint32_t i = 0; i < context.inBuffLen; i++) {
37         napi_value element = nullptr;
38         napi_create_int32(env, context.inBuff[i], &element);
39         napi_set_element(env, array, i, element);
40     }
41     napi_set_named_property(env, object, name.c_str(), array);
42 }
43 
WrapDecodeMmsStatus(int32_t status)44 int32_t WrapDecodeMmsStatus(int32_t status)
45 {
46     switch (status) {
47         case MmsMsgType::MMS_MSGTYPE_SEND_REQ: {
48             return MessageType::TYPE_MMS_SEND_REQ;
49         }
50         case MmsMsgType::MMS_MSGTYPE_SEND_CONF: {
51             return MessageType::TYPE_MMS_SEND_CONF;
52         }
53         case MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND: {
54             return MessageType::TYPE_MMS_NOTIFICATION_IND;
55         }
56         case MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND: {
57             return MessageType::TYPE_MMS_RESP_IND;
58         }
59         case MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF: {
60             return MessageType::TYPE_MMS_RETRIEVE_CONF;
61         }
62         case MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND: {
63             return MessageType::TYPE_MMS_ACKNOWLEDGE_IND;
64         }
65         case MmsMsgType::MMS_MSGTYPE_DELIVERY_IND: {
66             return MessageType::TYPE_MMS_DELIVERY_IND;
67         }
68         case MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND: {
69             return MessageType::TYPE_MMS_READ_ORIG_IND;
70         }
71         case MmsMsgType::MMS_MSGTYPE_READ_REC_IND: {
72             return MessageType::TYPE_MMS_READ_REC_IND;
73         }
74         default: {
75             return MESSAGE_UNKNOWN_STATUS;
76         }
77     }
78 }
79 
WrapEncodeMmsStatus(int32_t status)80 int32_t WrapEncodeMmsStatus(int32_t status)
81 {
82     switch (status) {
83         case MessageType::TYPE_MMS_SEND_REQ: {
84             return MmsMsgType::MMS_MSGTYPE_SEND_REQ;
85         }
86         case MessageType::TYPE_MMS_SEND_CONF: {
87             return MmsMsgType::MMS_MSGTYPE_SEND_CONF;
88         }
89         case MessageType::TYPE_MMS_NOTIFICATION_IND: {
90             return MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND;
91         }
92         case MessageType::TYPE_MMS_RESP_IND: {
93             return MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND;
94         }
95         case MessageType::TYPE_MMS_RETRIEVE_CONF: {
96             return MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF;
97         }
98         case MessageType::TYPE_MMS_ACKNOWLEDGE_IND: {
99             return MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND;
100         }
101         case MessageType::TYPE_MMS_DELIVERY_IND: {
102             return MmsMsgType::MMS_MSGTYPE_DELIVERY_IND;
103         }
104         case MessageType::TYPE_MMS_READ_ORIG_IND: {
105             return MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND;
106         }
107         case MessageType::TYPE_MMS_READ_REC_IND: {
108             return MmsMsgType::MMS_MSGTYPE_READ_REC_IND;
109         }
110         default: {
111             return MESSAGE_UNKNOWN_STATUS;
112         }
113     }
114 }
115 
parseDispositionValue(int32_t value)116 std::string parseDispositionValue(int32_t value)
117 {
118     switch (value) {
119         case FROM_DATA:
120             return "from-data";
121         case ATTACHMENT:
122             return "attachment";
123         case INLINE:
124             return "inline";
125         default:
126             TELEPHONY_LOGE("Invalid contentDisposition value");
127             return "";
128     }
129 }
130 
formatDispositionValue(const std::string & value)131 int32_t formatDispositionValue(const std::string &value)
132 {
133     if (std::string("from-data") == value) {
134         return FROM_DATA;
135     } else if (std::string("attachment") == value) {
136         return ATTACHMENT;
137     } else {
138         return INLINE;
139     }
140 }
141 
GetMmsSendConf(MmsMsg mmsMsg,MmsSendConfContext & asyncContext)142 void GetMmsSendConf(MmsMsg mmsMsg, MmsSendConfContext &asyncContext)
143 {
144     TELEPHONY_LOGI("napi_mms GetMmsSendConf start");
145     asyncContext.responseState = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_RESPONSE_STATUS);
146     asyncContext.transactionId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_TRANSACTION_ID);
147     asyncContext.version = static_cast<uint16_t>(mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_MMS_VERSION));
148     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
149     TELEPHONY_LOGI("napi_mms GetMmsSendConf end");
150 }
151 
GetMmsSendReq(MmsMsg mmsMsg,MmsSendReqContext & asyncContext)152 void GetMmsSendReq(MmsMsg mmsMsg, MmsSendReqContext &asyncContext)
153 {
154     TELEPHONY_LOGI("napi_mms GetMmsSendConf end");
155     asyncContext.from = mmsMsg.GetMmsFrom();
156     mmsMsg.GetMmsTo(asyncContext.to);
157     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
158     asyncContext.version = mmsMsg.GetMmsVersion();
159     asyncContext.date = mmsMsg.GetMmsDate();
160     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_CC, asyncContext.cc);
161     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_BCC, asyncContext.bcc);
162     asyncContext.subject = mmsMsg.GetMmsSubject();
163     asyncContext.messageClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS);
164     asyncContext.expiry = mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY);
165     asyncContext.priority = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY);
166     asyncContext.senderVisibility = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_SENDER_VISIBILITY);
167     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
168     asyncContext.readReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT);
169     asyncContext.contentType = mmsMsg.GetHeaderContentType();
170     TELEPHONY_LOGI("napi_mms GetMmsSendReq end");
171 }
172 
GetMmsNotificationInd(MmsMsg mmsMsg,MmsNotificationIndContext & asyncContext)173 void GetMmsNotificationInd(MmsMsg mmsMsg, MmsNotificationIndContext &asyncContext)
174 {
175     TELEPHONY_LOGI("napi_mms GetMmsNotificationInd start");
176     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
177     asyncContext.messageClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS);
178     asyncContext.messageSize = mmsMsg.GetHeaderLongValue(MmsFieldCode::MMS_MESSAGE_SIZE);
179     asyncContext.expiry = mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY);
180     asyncContext.version = mmsMsg.GetMmsVersion();
181     asyncContext.from = mmsMsg.GetMmsFrom();
182     asyncContext.subject = mmsMsg.GetMmsSubject();
183     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
184     asyncContext.contentLocation = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_CONTENT_LOCATION);
185     asyncContext.contentClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_CONTENT_CLASS);
186 }
187 
GetMmsRespInd(MmsMsg mmsMsg,MmsRespIndContext & asyncContext)188 void GetMmsRespInd(MmsMsg mmsMsg, MmsRespIndContext &asyncContext)
189 {
190     TELEPHONY_LOGI("napi_mms GetMmsRespInd start");
191     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
192     asyncContext.status = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_STATUS);
193     asyncContext.version = mmsMsg.GetMmsVersion();
194     asyncContext.reportAllowed = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED);
195     TELEPHONY_LOGI("napi_mms GetMmsRespInd end");
196 }
197 
GetMmsRetrieveConf(MmsMsg mmsMsg,MmsRetrieveConfContext & asyncContext)198 void GetMmsRetrieveConf(MmsMsg mmsMsg, MmsRetrieveConfContext &asyncContext)
199 {
200     TELEPHONY_LOGI("napi_mms GetMmsRetrieveConf start");
201     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
202     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
203     asyncContext.date = mmsMsg.GetMmsDate();
204     asyncContext.version = mmsMsg.GetMmsVersion();
205     mmsMsg.GetMmsTo(asyncContext.to);
206     asyncContext.from = mmsMsg.GetMmsFrom();
207     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_CC, asyncContext.cc);
208     asyncContext.subject = mmsMsg.GetMmsSubject();
209     asyncContext.priority = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY);
210     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
211     asyncContext.readReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT);
212     asyncContext.retrieveStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_RETRIEVE_STATUS);
213     asyncContext.retrieveText = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_RETRIEVE_TEXT);
214     asyncContext.contentType = mmsMsg.GetHeaderContentType();
215     TELEPHONY_LOGI("napi_mms GetMmsRetrieveConf end");
216 }
217 
GetMmsAcknowledgeInd(MmsMsg mmsMsg,MmsAcknowledgeIndContext & asyncContext)218 void GetMmsAcknowledgeInd(MmsMsg mmsMsg, MmsAcknowledgeIndContext &asyncContext)
219 {
220     TELEPHONY_LOGI("napi_mms GetMmsAcknowledgeInd start");
221     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
222     asyncContext.version = mmsMsg.GetMmsVersion();
223     asyncContext.reportAllowed = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED);
224     TELEPHONY_LOGI("napi_mms GetMmsAcknowledgeInd end");
225 }
226 
GetMmsDeliveryInd(MmsMsg mmsMsg,MmsDeliveryIndContext & asyncContext)227 void GetMmsDeliveryInd(MmsMsg mmsMsg, MmsDeliveryIndContext &asyncContext)
228 {
229     TELEPHONY_LOGI("napi_mms GetMmsDeliveryInd start");
230     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
231     asyncContext.date = mmsMsg.GetMmsDate();
232     std::vector<MmsAddress> toAddress;
233     bool result = mmsMsg.GetMmsTo(toAddress);
234     if (result) {
235         asyncContext.to.assign(toAddress.begin(), toAddress.end());
236     }
237     asyncContext.status = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_STATUS);
238     asyncContext.version = mmsMsg.GetMmsVersion();
239 
240     TELEPHONY_LOGI("napi_mms GetMmsDeliveryInd end");
241 }
242 
GetMmsReadOrigInd(MmsMsg mmsMsg,MmsReadOrigIndContext & asyncContext)243 void GetMmsReadOrigInd(MmsMsg mmsMsg, MmsReadOrigIndContext &asyncContext)
244 {
245     TELEPHONY_LOGI("napi_mms GetMmsReadOrigInd start");
246     asyncContext.version = mmsMsg.GetMmsVersion();
247     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
248     mmsMsg.GetMmsTo(asyncContext.to);
249     asyncContext.from = mmsMsg.GetMmsFrom();
250     asyncContext.date = mmsMsg.GetMmsDate();
251     asyncContext.readStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS);
252 
253     TELEPHONY_LOGI("napi_mms GetMmsReadOrigInd end");
254 }
255 
GetMmsReadRecInd(MmsMsg mmsMsg,MmsReadRecIndContext & asyncContext)256 void GetMmsReadRecInd(MmsMsg mmsMsg, MmsReadRecIndContext &asyncContext)
257 {
258     TELEPHONY_LOGI("napi_mms GetMmsReadRecInd start");
259     asyncContext.version = mmsMsg.GetMmsVersion();
260     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
261     mmsMsg.GetMmsTo(asyncContext.to);
262     asyncContext.from = mmsMsg.GetMmsFrom();
263     asyncContext.date = mmsMsg.GetMmsDate();
264     asyncContext.readStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS);
265 
266     TELEPHONY_LOGI("napi_mms GetMmsReadRecInd end");
267 }
268 
getAttachmentByDecodeMms(MmsMsg & mmsMsg,DecodeMmsContext & context)269 void getAttachmentByDecodeMms(MmsMsg &mmsMsg, DecodeMmsContext &context)
270 {
271     std::vector<MmsAttachment> attachment;
272     mmsMsg.GetAllAttachment(attachment);
273     if (attachment.empty()) {
274         return;
275     }
276     for (auto it : attachment) {
277         MmsAttachmentContext attachmentContext;
278         attachmentContext.path = it.GetAttachmentFilePath();
279         attachmentContext.fileName = it.GetFileName();
280         attachmentContext.contentId = it.GetContentId();
281         attachmentContext.contentLocation = it.GetContentLocation();
282         attachmentContext.contentDisposition = it.GetContentDisposition();
283         attachmentContext.contentTransferEncoding = it.GetContentTransferEncoding();
284         attachmentContext.contentType = it.GetContentType();
285         attachmentContext.isSmil = it.IsSmilFile();
286         attachmentContext.charset = static_cast<int32_t>(it.GetCharSet());
287         std::unique_ptr<char[]> buffer = nullptr;
288         buffer = it.GetDataBuffer(attachmentContext.inBuffLen);
289         attachmentContext.inBuff = std::move(buffer);
290         context.attachment.push_back(std::move(attachmentContext));
291     }
292 }
293 
NativeDecodeMms(napi_env env,void * data)294 void NativeDecodeMms(napi_env env, void *data)
295 {
296     if (data == nullptr) {
297         TELEPHONY_LOGE("napi_mms data nullptr");
298         return;
299     }
300     auto context = static_cast<DecodeMmsContext *>(data);
301     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
302         TELEPHONY_LOGE("Non-system applications use system APIs!");
303         context->errorCode = TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
304         return;
305     }
306     MmsMsg mmsMsg;
307     bool mmsResult = false;
308     if (context->messageMatchResult == TEXT_MESSAGE_PARAMETER_MATCH) {
309         mmsResult = mmsMsg.DecodeMsg(context->textFilePath);
310     } else if (context->messageMatchResult == RAW_DATA_MESSAGE_PARAMETER_MATCH) {
311         mmsResult = mmsMsg.DecodeMsg(std::move(context->inBuffer), context->inLen);
312     }
313     if (!mmsResult) {
314         TELEPHONY_LOGE("napi_mms DecodeMsg error!");
315         context->errorCode = TELEPHONY_ERR_FAIL;
316         return;
317     }
318     mmsMsg.DumpMms();
319     int32_t messageType = WrapDecodeMmsStatus(static_cast<int32_t>(mmsMsg.GetMmsMessageType()));
320     context->messageType = messageType;
321     if (messageType == MessageType::TYPE_MMS_SEND_CONF) {
322         GetMmsSendConf(mmsMsg, context->sendConf);
323     } else if (messageType == MessageType::TYPE_MMS_SEND_REQ) {
324         GetMmsSendReq(mmsMsg, context->sendReq);
325     } else if (messageType == MessageType::TYPE_MMS_NOTIFICATION_IND) {
326         GetMmsNotificationInd(mmsMsg, context->notificationInd);
327     } else if (messageType == MessageType::TYPE_MMS_RESP_IND) {
328         GetMmsRespInd(mmsMsg, context->respInd);
329     } else if (messageType == MessageType::TYPE_MMS_RETRIEVE_CONF) {
330         GetMmsRetrieveConf(mmsMsg, context->retrieveConf);
331     } else if (messageType == MessageType::TYPE_MMS_ACKNOWLEDGE_IND) {
332         GetMmsAcknowledgeInd(mmsMsg, context->acknowledgeInd);
333     } else if (messageType == MessageType::TYPE_MMS_DELIVERY_IND) {
334         GetMmsDeliveryInd(mmsMsg, context->deliveryInd);
335     } else if (messageType == MessageType::TYPE_MMS_READ_ORIG_IND) {
336         GetMmsReadOrigInd(mmsMsg, context->readOrigInd);
337     } else if (messageType == MessageType::TYPE_MMS_READ_REC_IND) {
338         GetMmsReadRecInd(mmsMsg, context->readRecInd);
339     }
340     getAttachmentByDecodeMms(mmsMsg, *context);
341     context->errorCode = TELEPHONY_ERR_SUCCESS;
342     context->resolved = true;
343     TELEPHONY_LOGI("napi_mms NativeDecodeMms finish");
344 }
345 
CreateAttachmentValue(napi_env env,MmsAttachmentContext & context)346 napi_value CreateAttachmentValue(napi_env env, MmsAttachmentContext &context)
347 {
348     TELEPHONY_LOGI("napi_mms CreateAttachmentValue  start");
349     napi_value attachment = nullptr;
350     napi_create_object(env, &attachment);
351     NapiUtil::SetPropertyStringUtf8(env, attachment, "path", context.path);
352     NapiUtil::SetPropertyStringUtf8(env, attachment, "fileName", context.fileName);
353     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentId", context.contentId);
354     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentLocation", context.contentLocation);
355     NapiUtil::SetPropertyInt32(
356         env, attachment, "contentDisposition", formatDispositionValue(context.contentDisposition));
357     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentTransferEncoding", context.contentTransferEncoding);
358     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentType", context.contentType);
359     NapiUtil::SetPropertyBoolean(env, attachment, "isSmil", context.isSmil);
360     NapiUtil::SetPropertyInt32(env, attachment, "charset", context.charset);
361     SetPropertyArray(env, attachment, "inBuff", context);
362     return attachment;
363 }
364 
ParseAddress(napi_env env,napi_value outValue,const std::string & name,MmsAddress mmsAddress)365 void ParseAddress(napi_env env, napi_value outValue, const std::string &name, MmsAddress mmsAddress)
366 {
367     napi_value addressObj = nullptr;
368     napi_create_object(env, &addressObj);
369     NapiUtil::SetPropertyStringUtf8(env, addressObj, "address", mmsAddress.GetAddressString());
370     NapiUtil::SetPropertyInt32(env, addressObj, "charset", static_cast<int32_t>(mmsAddress.GetAddressCharset()));
371     napi_set_named_property(env, outValue, name.c_str(), addressObj);
372 }
373 
ParseAddressArr(napi_env env,napi_value outValue,const std::string & name,std::vector<MmsAddress> addressArr)374 void ParseAddressArr(napi_env env, napi_value outValue, const std::string &name, std::vector<MmsAddress> addressArr)
375 {
376     napi_value toArr = nullptr;
377     napi_create_array(env, &toArr);
378     for (size_t i = 0; i < addressArr.size(); i++) {
379         napi_value addressObj = nullptr;
380         napi_create_object(env, &addressObj);
381         NapiUtil::SetPropertyStringUtf8(env, addressObj, "address", addressArr[i].GetAddressString());
382         NapiUtil::SetPropertyInt32(env, addressObj, "charset", static_cast<int32_t>(addressArr[i].GetAddressCharset()));
383         napi_set_element(env, toArr, i, addressObj);
384     }
385     napi_set_named_property(env, outValue, name.c_str(), toArr);
386 }
387 
ParseSendReqValue(napi_env env,napi_value object,MmsSendReqContext & sendReqContext)388 void ParseSendReqValue(napi_env env, napi_value object, MmsSendReqContext &sendReqContext)
389 {
390     TELEPHONY_LOGI("napi_mms  ParseSendReqValue start");
391     napi_value sendReqObj = nullptr;
392     napi_create_object(env, &sendReqObj);
393     ParseAddress(env, sendReqObj, "from", sendReqContext.from);
394     ParseAddressArr(env, sendReqObj, "to", sendReqContext.to);
395     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "transactionId", sendReqContext.transactionId);
396     NapiUtil::SetPropertyInt32(env, sendReqObj, "version", sendReqContext.version);
397     NapiUtil::SetPropertyInt64(env, sendReqObj, "date", sendReqContext.date);
398     ParseAddressArr(env, sendReqObj, "cc", sendReqContext.cc);
399     ParseAddressArr(env, sendReqObj, "bcc", sendReqContext.bcc);
400     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "subject", sendReqContext.subject);
401     NapiUtil::SetPropertyInt32(env, sendReqObj, "messageClass", static_cast<int32_t>(sendReqContext.messageClass));
402     NapiUtil::SetPropertyInt32(env, sendReqObj, "expiry", sendReqContext.expiry);
403     NapiUtil::SetPropertyInt32(env, sendReqObj, "priority", static_cast<int32_t>(sendReqContext.priority));
404     NapiUtil::SetPropertyInt32(
405         env, sendReqObj, "senderVisibility", static_cast<int32_t>(sendReqContext.senderVisibility));
406     NapiUtil::SetPropertyInt32(env, sendReqObj, "deliveryReport", static_cast<int32_t>(sendReqContext.deliveryReport));
407     NapiUtil::SetPropertyInt32(env, sendReqObj, "readReport", static_cast<int32_t>(sendReqContext.readReport));
408     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "contentType", sendReqContext.contentType);
409     napi_set_named_property(env, object, mmsTypeKey.c_str(), sendReqObj);
410     TELEPHONY_LOGI("napi_mms  ParseSendReqValue end");
411 }
412 
ParseSendConfValue(napi_env env,napi_value object,MmsSendConfContext & sendConfContext)413 void ParseSendConfValue(napi_env env, napi_value object, MmsSendConfContext &sendConfContext)
414 {
415     TELEPHONY_LOGI("napi_mms  ParseSendConfValue start");
416     napi_value sendConfObj = nullptr;
417     napi_create_object(env, &sendConfObj);
418     NapiUtil::SetPropertyInt32(env, sendConfObj, "responseState", static_cast<int32_t>(sendConfContext.responseState));
419     NapiUtil::SetPropertyStringUtf8(env, sendConfObj, "transactionId", sendConfContext.transactionId);
420     NapiUtil::SetPropertyInt32(env, sendConfObj, "version", sendConfContext.version);
421     NapiUtil::SetPropertyStringUtf8(env, sendConfObj, "messageId", sendConfContext.messageId);
422     napi_set_named_property(env, object, mmsTypeKey.c_str(), sendConfObj);
423     TELEPHONY_LOGI("napi_mms  ParseSendConfValue end");
424 }
425 
ParseNotificationIndValue(napi_env env,napi_value object,MmsNotificationIndContext & notificationContext)426 void ParseNotificationIndValue(napi_env env, napi_value object, MmsNotificationIndContext &notificationContext)
427 {
428     TELEPHONY_LOGI("napi_mms  ParseNotificationIndValue start");
429     napi_value notificationObj = nullptr;
430     napi_create_object(env, &notificationObj);
431     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "transactionId", notificationContext.transactionId);
432     NapiUtil::SetPropertyInt32(
433         env, notificationObj, "messageClass", static_cast<int32_t>(notificationContext.messageClass));
434     NapiUtil::SetPropertyInt64(env, notificationObj, "messageSize", notificationContext.messageSize);
435     NapiUtil::SetPropertyInt32(env, notificationObj, "expiry", notificationContext.expiry);
436     NapiUtil::SetPropertyInt32(env, notificationObj, "version", notificationContext.version);
437     ParseAddress(env, notificationObj, "from", notificationContext.from);
438     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "subject", notificationContext.subject);
439     NapiUtil::SetPropertyInt32(
440         env, notificationObj, "deliveryReport", static_cast<int32_t>(notificationContext.deliveryReport));
441     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "contentLocation", notificationContext.contentLocation);
442     NapiUtil::SetPropertyInt32(
443         env, notificationObj, "contentClass", static_cast<int32_t>(notificationContext.contentClass));
444     napi_set_named_property(env, object, mmsTypeKey.c_str(), notificationObj);
445 }
446 
ParseRespIndValue(napi_env env,napi_value object,MmsRespIndContext & respIndContext)447 void ParseRespIndValue(napi_env env, napi_value object, MmsRespIndContext &respIndContext)
448 {
449     TELEPHONY_LOGI("napi_mms  ParseRespIndValue start");
450     napi_value respIndObj = nullptr;
451     napi_create_object(env, &respIndObj);
452     NapiUtil::SetPropertyStringUtf8(env, respIndObj, "transactionId", respIndContext.transactionId);
453     NapiUtil::SetPropertyInt32(env, respIndObj, "status", static_cast<int32_t>(respIndContext.status));
454     NapiUtil::SetPropertyInt32(env, respIndObj, "version", static_cast<int32_t>(respIndContext.version));
455     NapiUtil::SetPropertyInt32(env, respIndObj, "reportAllowed", static_cast<int32_t>(respIndContext.reportAllowed));
456     napi_set_named_property(env, object, mmsTypeKey.c_str(), respIndObj);
457     TELEPHONY_LOGI("napi_mms  ParseRespIndValue end");
458 }
459 
ParseRetrieveConfValue(napi_env env,napi_value object,MmsRetrieveConfContext & retrieveConfContext)460 void ParseRetrieveConfValue(napi_env env, napi_value object, MmsRetrieveConfContext &retrieveConfContext)
461 {
462     TELEPHONY_LOGI("napi_mms  ParseRetrieveConfValue start");
463     napi_value retrieveConfObj = nullptr;
464     napi_create_object(env, &retrieveConfObj);
465     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "transactionId", retrieveConfContext.transactionId);
466     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "messageId", retrieveConfContext.messageId);
467     NapiUtil::SetPropertyInt64(env, retrieveConfObj, "date", retrieveConfContext.date);
468     NapiUtil::SetPropertyInt32(env, retrieveConfObj, "version", retrieveConfContext.version);
469     ParseAddressArr(env, retrieveConfObj, "to", retrieveConfContext.to);
470     ParseAddress(env, retrieveConfObj, "from", retrieveConfContext.from);
471     ParseAddressArr(env, retrieveConfObj, "cc", retrieveConfContext.cc);
472     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "subject", retrieveConfContext.subject);
473     NapiUtil::SetPropertyInt32(env, retrieveConfObj, "priority", static_cast<int32_t>(retrieveConfContext.priority));
474     NapiUtil::SetPropertyInt32(
475         env, retrieveConfObj, "deliveryReport", static_cast<int32_t>(retrieveConfContext.deliveryReport));
476     NapiUtil::SetPropertyInt32(
477         env, retrieveConfObj, "readReport", static_cast<int32_t>(retrieveConfContext.readReport));
478     NapiUtil::SetPropertyInt32(
479         env, retrieveConfObj, "retrieveStatus", static_cast<int32_t>(retrieveConfContext.retrieveStatus));
480     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "retrieveText", retrieveConfContext.retrieveText);
481     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "contentType", retrieveConfContext.contentType);
482     napi_set_named_property(env, object, mmsTypeKey.c_str(), retrieveConfObj);
483     TELEPHONY_LOGI("napi_mms  ParseRetrieveConfValue end");
484 }
485 
ParseAcknowledgeIndValue(napi_env env,napi_value object,MmsAcknowledgeIndContext & acknowledgeIndContext)486 void ParseAcknowledgeIndValue(napi_env env, napi_value object, MmsAcknowledgeIndContext &acknowledgeIndContext)
487 {
488     TELEPHONY_LOGI("napi_mms  ParseAcknowledgeIndValue start");
489     napi_value acknowledgeIndObj = nullptr;
490     napi_create_object(env, &acknowledgeIndObj);
491     NapiUtil::SetPropertyStringUtf8(env, acknowledgeIndObj, "transactionId", acknowledgeIndContext.transactionId);
492     NapiUtil::SetPropertyInt32(env, acknowledgeIndObj, "version", acknowledgeIndContext.version);
493     NapiUtil::SetPropertyInt32(
494         env, acknowledgeIndObj, "reportAllowed", static_cast<int32_t>(acknowledgeIndContext.reportAllowed));
495     napi_set_named_property(env, object, mmsTypeKey.c_str(), acknowledgeIndObj);
496     TELEPHONY_LOGI("napi_mms  ParseAcknowledgeIndValue end");
497 }
498 
ParseDeliveryIndValue(napi_env env,napi_value object,MmsDeliveryIndContext & deliveryIndContext)499 void ParseDeliveryIndValue(napi_env env, napi_value object, MmsDeliveryIndContext &deliveryIndContext)
500 {
501     TELEPHONY_LOGI("napi_mms  ParseDeliveryIndValue start");
502     napi_value deliveryIndObj = nullptr;
503     napi_create_object(env, &deliveryIndObj);
504     NapiUtil::SetPropertyStringUtf8(env, deliveryIndObj, "messageId", deliveryIndContext.messageId);
505     NapiUtil::SetPropertyInt64(env, deliveryIndObj, "date", deliveryIndContext.date);
506     ParseAddressArr(env, deliveryIndObj, "to", deliveryIndContext.to);
507     NapiUtil::SetPropertyInt32(env, deliveryIndObj, "status", static_cast<int32_t>(deliveryIndContext.status));
508     NapiUtil::SetPropertyInt32(env, deliveryIndObj, "version", deliveryIndContext.version);
509     napi_set_named_property(env, object, mmsTypeKey.c_str(), deliveryIndObj);
510     TELEPHONY_LOGI("napi_mms  ParseDeliveryIndValue end");
511 }
512 
ParseReadOrigIndValue(napi_env env,napi_value object,MmsReadOrigIndContext & readOrigIndContext)513 void ParseReadOrigIndValue(napi_env env, napi_value object, MmsReadOrigIndContext &readOrigIndContext)
514 {
515     TELEPHONY_LOGI("napi_mms  ParseReadOrigIndValue start");
516     napi_value readOrigIndObj = nullptr;
517     napi_create_object(env, &readOrigIndObj);
518     NapiUtil::SetPropertyInt32(env, readOrigIndObj, "version", readOrigIndContext.version);
519     NapiUtil::SetPropertyStringUtf8(env, readOrigIndObj, "messageId", readOrigIndContext.messageId);
520     ParseAddressArr(env, readOrigIndObj, "to", readOrigIndContext.to);
521     ParseAddress(env, readOrigIndObj, "from", readOrigIndContext.from);
522     NapiUtil::SetPropertyInt64(env, readOrigIndObj, "date", readOrigIndContext.date);
523     NapiUtil::SetPropertyInt32(env, readOrigIndObj, "readStatus", static_cast<int32_t>(readOrigIndContext.readStatus));
524     napi_set_named_property(env, object, mmsTypeKey.c_str(), readOrigIndObj);
525     TELEPHONY_LOGI("napi_mms  ParseReadOrigIndValue end");
526 }
527 
ParseReadRecIndValue(napi_env env,napi_value object,MmsReadRecIndContext & readRecIndContext)528 void ParseReadRecIndValue(napi_env env, napi_value object, MmsReadRecIndContext &readRecIndContext)
529 {
530     TELEPHONY_LOGI("napi_mms  ParseReadRecIndValue start");
531     napi_value readRecIndObj = nullptr;
532     napi_create_object(env, &readRecIndObj);
533     NapiUtil::SetPropertyInt32(env, readRecIndObj, "version", readRecIndContext.version);
534     NapiUtil::SetPropertyStringUtf8(env, readRecIndObj, "messageId", readRecIndContext.messageId);
535     ParseAddressArr(env, readRecIndObj, "to", readRecIndContext.to);
536     ParseAddress(env, readRecIndObj, "from", readRecIndContext.from);
537     NapiUtil::SetPropertyInt64(env, readRecIndObj, "date", readRecIndContext.date);
538     NapiUtil::SetPropertyInt32(env, readRecIndObj, "readStatus", static_cast<int32_t>(readRecIndContext.readStatus));
539     napi_set_named_property(env, object, mmsTypeKey.c_str(), readRecIndObj);
540     TELEPHONY_LOGI("napi_mms  ParseReadRecIndValue end");
541 }
542 
CreateDecodeMmsValue(napi_env env,DecodeMmsContext & asyncContext)543 napi_value CreateDecodeMmsValue(napi_env env, DecodeMmsContext &asyncContext)
544 {
545     TELEPHONY_LOGI("napi_mms  CreateDecodeMmsValue start");
546     napi_value object = nullptr;
547     napi_value attachmentArr = nullptr;
548     napi_create_object(env, &object);
549     napi_create_array(env, &attachmentArr);
550     NapiUtil::SetPropertyInt32(env, object, "messageType", static_cast<int32_t>(asyncContext.messageType));
551     if (asyncContext.attachment.size() > 0) {
552         int i = 0;
553         for (std::vector<MmsAttachmentContext>::iterator it = asyncContext.attachment.begin();
554              it != asyncContext.attachment.end(); ++it) {
555             napi_value attachNapi = CreateAttachmentValue(env, *it);
556             napi_set_element(env, attachmentArr, i, attachNapi);
557             i++;
558         }
559         napi_set_named_property(env, object, attachmentKey.c_str(), attachmentArr);
560     }
561     int32_t messageType = asyncContext.messageType;
562     if (messageType == MessageType::TYPE_MMS_SEND_REQ) {
563         ParseSendReqValue(env, object, asyncContext.sendReq);
564     } else if (messageType == MessageType::TYPE_MMS_SEND_CONF) {
565         ParseSendConfValue(env, object, asyncContext.sendConf);
566     } else if (messageType == MessageType::TYPE_MMS_NOTIFICATION_IND) {
567         ParseNotificationIndValue(env, object, asyncContext.notificationInd);
568     } else if (messageType == MessageType::TYPE_MMS_RESP_IND) {
569         ParseRespIndValue(env, object, asyncContext.respInd);
570     } else if (messageType == MessageType::TYPE_MMS_RETRIEVE_CONF) {
571         ParseRetrieveConfValue(env, object, asyncContext.retrieveConf);
572     } else if (messageType == MessageType::TYPE_MMS_ACKNOWLEDGE_IND) {
573         ParseAcknowledgeIndValue(env, object, asyncContext.acknowledgeInd);
574     } else if (messageType == MessageType::TYPE_MMS_DELIVERY_IND) {
575         ParseDeliveryIndValue(env, object, asyncContext.deliveryInd);
576     } else if (messageType == MessageType::TYPE_MMS_READ_ORIG_IND) {
577         ParseReadOrigIndValue(env, object, asyncContext.readOrigInd);
578     } else if (messageType == MessageType::TYPE_MMS_READ_REC_IND) {
579         ParseReadRecIndValue(env, object, asyncContext.readRecInd);
580     }
581     return object;
582 }
583 
DecodeMmsCallback(napi_env env,napi_status status,void * data)584 void DecodeMmsCallback(napi_env env, napi_status status, void *data)
585 {
586     TELEPHONY_LOGI("napi_mms DecodeMmsCallback start");
587     if (data == nullptr) {
588         TELEPHONY_LOGE("data nullptr");
589         return;
590     }
591     auto decodeMmsContext = static_cast<DecodeMmsContext *>(data);
592     napi_value callbackValue = nullptr;
593 
594     if (status == napi_ok) {
595         if (decodeMmsContext->resolved) {
596             callbackValue = CreateDecodeMmsValue(env, *decodeMmsContext);
597         } else {
598             JsError error = NapiUtil::ConverErrorMessageForJs(decodeMmsContext->errorCode);
599             callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
600         }
601     } else {
602         callbackValue =
603             NapiUtil::CreateErrorMessage(env, "decode mms error,cause napi_status = " + std::to_string(status));
604     }
605     NapiUtil::Handle2ValueCallback(env, decodeMmsContext, callbackValue);
606 }
607 
ParseDecodeMmsParam(napi_env env,napi_value object,DecodeMmsContext & context)608 void ParseDecodeMmsParam(napi_env env, napi_value object, DecodeMmsContext &context)
609 {
610     TELEPHONY_LOGI("napi_mms ParseDecodeMmsParam start");
611     if (context.messageMatchResult == TEXT_MESSAGE_PARAMETER_MATCH) {
612         TELEPHONY_LOGI("napi_mms  messageMatchResult == TEXT");
613         char contentChars[MAX_TEXT_SHORT_MESSAGE_LENGTH] = { 0 };
614         size_t contentLength = 0;
615         napi_get_value_string_utf8(env, object, contentChars, MAX_TEXT_SHORT_MESSAGE_LENGTH, &contentLength);
616         context.textFilePath = std::string(contentChars, 0, contentLength);
617     } else if (context.messageMatchResult == RAW_DATA_MESSAGE_PARAMETER_MATCH) {
618         TELEPHONY_LOGI("napi_mms  messageMatchResult  == RAW_DATA");
619         napi_value elementValue = nullptr;
620         int32_t element = 0;
621         uint32_t arrayLength = 0;
622         napi_get_array_length(env, object, &arrayLength);
623         if (arrayLength > MMS_PDU_MAX_SIZE) {
624             TELEPHONY_LOGE("arrayLength over size error");
625             return;
626         }
627         context.inLen = arrayLength;
628         TELEPHONY_LOGI("napi_mms ParseDecodeMmsParam arrayLength = %{public}d", arrayLength);
629         context.inBuffer = std::make_unique<char[]>(arrayLength);
630         if (context.inBuffer == nullptr) {
631             TELEPHONY_LOGE("make unique error");
632             return;
633         }
634         for (uint32_t i = 0; i < arrayLength; i++) {
635             napi_get_element(env, object, i, &elementValue);
636             napi_get_value_int32(env, elementValue, &element);
637             context.inBuffer[i] = (char)element;
638         }
639     }
640 }
641 
GetMatchDecodeMmsResult(napi_env env,const napi_value parameters[],size_t parameterCount)642 int32_t GetMatchDecodeMmsResult(napi_env env, const napi_value parameters[], size_t parameterCount)
643 {
644     TELEPHONY_LOGI("napi_mms GetMatchDecodeMmsResult start");
645     int32_t paramsTypeMatched = MESSAGE_PARAMETER_NOT_MATCH;
646     switch (parameterCount) {
647         case ONE_PARAMETER:
648             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object }) ||
649                                 NapiUtil::MatchParameters(env, parameters, { napi_string });
650             break;
651         case TWO_PARAMETERS:
652             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object, napi_function }) ||
653                                 NapiUtil::MatchParameters(env, parameters, { napi_string, napi_function });
654             break;
655         default:
656             return MESSAGE_PARAMETER_NOT_MATCH;
657     }
658     if (!paramsTypeMatched) {
659         return MESSAGE_PARAMETER_NOT_MATCH;
660     }
661 
662     bool filePathIsStr = NapiUtil::MatchValueType(env, parameters[0], napi_string);
663     bool filePathIsObj = NapiUtil::MatchValueType(env, parameters[0], napi_object);
664     bool filePathIsArray = false;
665     if (filePathIsObj) {
666         napi_is_array(env, parameters[0], &filePathIsArray);
667     }
668     if (filePathIsStr) {
669         return TEXT_MESSAGE_PARAMETER_MATCH;
670     } else if (filePathIsArray) {
671         return RAW_DATA_MESSAGE_PARAMETER_MATCH;
672     } else {
673         return MESSAGE_PARAMETER_NOT_MATCH;
674     }
675 }
676 
DecodeMms(napi_env env,napi_callback_info info)677 napi_value NapiMms::DecodeMms(napi_env env, napi_callback_info info)
678 {
679     TELEPHONY_LOGI("napi_mms DecodeMms start");
680     napi_value result = nullptr;
681     size_t parameterCount = TWO_PARAMETERS;
682     napi_value parameters[TWO_PARAMETERS] = { 0 };
683     napi_value thisVar = nullptr;
684     void *data = nullptr;
685     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
686     int32_t messageMatchResult = GetMatchDecodeMmsResult(env, parameters, parameterCount);
687     if (messageMatchResult == MESSAGE_PARAMETER_NOT_MATCH) {
688         TELEPHONY_LOGE("DecodeMms parameter matching failed.");
689         NapiUtil::ThrowParameterError(env);
690         return nullptr;
691     }
692     auto context = std::make_unique<DecodeMmsContext>().release();
693     if (context == nullptr) {
694         TELEPHONY_LOGE("DecodeMms DecodeMmsContext is nullptr.");
695         NapiUtil::ThrowParameterError(env);
696         return nullptr;
697     }
698     context->messageMatchResult = messageMatchResult;
699     ParseDecodeMmsParam(env, parameters[0], *context);
700     if (parameterCount == TWO_PARAMETERS) {
701         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
702     }
703     result = NapiUtil::HandleAsyncWork(env, context, "DecodeMms", NativeDecodeMms, DecodeMmsCallback);
704     return result;
705 }
706 
MatchEncodeMms(napi_env env,const napi_value parameters[],size_t parameterCount)707 bool MatchEncodeMms(napi_env env, const napi_value parameters[], size_t parameterCount)
708 {
709     TELEPHONY_LOGI("napi_mms MatchEncodeMms start");
710     bool paramsTypeMatched = false;
711     switch (parameterCount) {
712         case ONE_PARAMETER:
713             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object });
714             break;
715         case TWO_PARAMETERS:
716             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object, napi_function });
717             break;
718         default:
719             return false;
720     }
721     if (!paramsTypeMatched) {
722         TELEPHONY_LOGE("encodeMms parameter not match");
723         return false;
724     }
725     if (NapiUtil::HasNamedProperty(env, parameters[0], "attachment")) {
726         return NapiUtil::MatchObjectProperty(env, parameters[0],
727             {
728                 { "messageType", napi_number },
729                 { "mmsType", napi_object },
730                 { "attachment", napi_object },
731             });
732     } else {
733         return NapiUtil::MatchObjectProperty(env, parameters[0],
734             {
735                 { "messageType", napi_number },
736                 { "mmsType", napi_object },
737             });
738     }
739     return false;
740 }
741 
GetNapiBooleanValue(napi_env env,napi_value napiValue,std::string name,bool defValue=false)742 bool GetNapiBooleanValue(napi_env env, napi_value napiValue, std::string name, bool defValue = false)
743 {
744     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
745     if (value != nullptr) {
746         bool result = defValue;
747         napi_get_value_bool(env, value, &result);
748         return result;
749     } else {
750         return defValue;
751     }
752 }
753 
GetNapiStringValue(napi_env env,napi_value napiValue,std::string name,std::string defValue="")754 std::string GetNapiStringValue(napi_env env, napi_value napiValue, std::string name, std::string defValue = "")
755 {
756     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
757     if (value != nullptr) {
758         return NapiUtil::GetStringFromValue(env, value);
759     } else {
760         return defValue;
761     }
762 }
763 
GetNapiInt32Value(napi_env env,napi_value napiValue,std::string name,int32_t defValue=0)764 int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, std::string name, int32_t defValue = 0)
765 {
766     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
767     if (value != nullptr) {
768         int32_t intValue = 0;
769         napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
770         if (getIntStatus == napi_ok) {
771             return intValue;
772         }
773     }
774     return defValue;
775 }
776 
GetNapiInt64Value(napi_env env,napi_value napiValue,std::string name,int64_t defValue=0)777 int64_t GetNapiInt64Value(napi_env env, napi_value napiValue, std::string name, int64_t defValue = 0)
778 {
779     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
780     if (value != nullptr) {
781         int64_t intValue = 0;
782         napi_status getIntStatus = napi_get_value_int64(env, value, &intValue);
783         if (getIntStatus == napi_ok) {
784             return intValue;
785         }
786     }
787     return defValue;
788 }
789 
GetNapiUint32Value(napi_env env,napi_value napiValue,std::string name,uint32_t defValue=0)790 uint32_t GetNapiUint32Value(napi_env env, napi_value napiValue, std::string name, uint32_t defValue = 0)
791 {
792     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
793     if (value != nullptr) {
794         uint32_t uint32Value = 0;
795         napi_status status = napi_get_value_uint32(env, value, &uint32Value);
796         if (status == napi_ok) {
797             return uint32Value;
798         }
799     }
800     return defValue;
801 }
802 
GetNapiUint8Value(napi_env env,napi_value napiValue,const std::string & name,uint8_t defValue=0)803 uint8_t GetNapiUint8Value(napi_env env, napi_value napiValue, const std::string &name, uint8_t defValue = 0)
804 {
805     return uint8_t(GetNapiInt32Value(env, napiValue, name, defValue));
806 }
807 
formatMmsCharSet(int32_t charsetInt)808 MmsCharSets formatMmsCharSet(int32_t charsetInt)
809 {
810     switch (charsetInt) {
811         case static_cast<int32_t>(MmsCharSets::BIG5):
812             return MmsCharSets::BIG5;
813         case static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2):
814             return MmsCharSets::ISO_10646_UCS_2;
815         case static_cast<int32_t>(MmsCharSets::ISO_8859_1):
816             return MmsCharSets::ISO_8859_1;
817         case static_cast<int32_t>(MmsCharSets::ISO_8859_2):
818             return MmsCharSets::ISO_8859_2;
819         case static_cast<int32_t>(MmsCharSets::ISO_8859_3):
820             return MmsCharSets::ISO_8859_3;
821         case static_cast<int32_t>(MmsCharSets::ISO_8859_4):
822             return MmsCharSets::ISO_8859_4;
823         case static_cast<int32_t>(MmsCharSets::ISO_8859_5):
824             return MmsCharSets::ISO_8859_5;
825         case static_cast<int32_t>(MmsCharSets::ISO_8859_6):
826             return MmsCharSets::ISO_8859_6;
827         case static_cast<int32_t>(MmsCharSets::ISO_8859_7):
828             return MmsCharSets::ISO_8859_7;
829         case static_cast<int32_t>(MmsCharSets::ISO_8859_8):
830             return MmsCharSets::ISO_8859_8;
831         case static_cast<int32_t>(MmsCharSets::ISO_8859_9):
832             return MmsCharSets::ISO_8859_9;
833         case static_cast<int32_t>(MmsCharSets::SHIFT_JIS):
834             return MmsCharSets::SHIFT_JIS;
835         case static_cast<int32_t>(MmsCharSets::US_ASCII):
836             return MmsCharSets::US_ASCII;
837         default:
838             return MmsCharSets::UTF_8;
839     }
840 }
841 
ReadMmsAddress(napi_env env,napi_value value)842 MmsAddress ReadMmsAddress(napi_env env, napi_value value)
843 {
844     std::string address = GetNapiStringValue(env, value, "address");
845     int32_t charset = GetNapiInt32Value(env, value, "charset");
846     MmsAddress mmsAddress(address, formatMmsCharSet(charset));
847     return mmsAddress;
848 }
849 
ReadMmsAddress(napi_env env,napi_value napiValue,std::string name,std::vector<MmsAddress> & array)850 void ReadMmsAddress(napi_env env, napi_value napiValue, std::string name, std::vector<MmsAddress> &array)
851 {
852     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
853     if (value != nullptr) {
854         uint32_t arrayLength = 0;
855         napi_get_array_length(env, value, &arrayLength);
856         napi_value elementValue = nullptr;
857         for (uint32_t i = 0; i < arrayLength; i++) {
858             napi_get_element(env, value, i, &elementValue);
859             MmsAddress eachMmsAddress = ReadMmsAddress(env, elementValue);
860             array.push_back(eachMmsAddress);
861         }
862     }
863 }
864 
HasNamedProperty(napi_env env,napi_value napiValue,std::vector<std::string> checkName)865 bool HasNamedProperty(napi_env env, napi_value napiValue, std::vector<std::string> checkName)
866 {
867     for (std::string item : checkName) {
868         if (!NapiUtil::HasNamedProperty(env, napiValue, item)) {
869             TELEPHONY_LOGE("Missed param with %{public}s", item.c_str());
870             return false;
871         }
872     }
873     return true;
874 }
875 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsSendReqContext & sendReq)876 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsSendReqContext &sendReq)
877 {
878     TELEPHONY_LOGI("mms_napi ReadEncodeMmsType start");
879     if (!HasNamedProperty(env, napiValue, { "from", "transactionId", "contentType" })) {
880         return false;
881     }
882     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
883     sendReq.from = ReadMmsAddress(env, from);
884     if (sendReq.from.GetAddressString().empty()) {
885         return false;
886     }
887     ReadMmsAddress(env, napiValue, "to", sendReq.to);
888     sendReq.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
889     sendReq.version = GetNapiInt32Value(env, napiValue, "version");
890     sendReq.date = GetNapiInt64Value(env, napiValue, "date");
891     ReadMmsAddress(env, napiValue, "cc", sendReq.cc);
892     ReadMmsAddress(env, napiValue, "bcc", sendReq.bcc);
893     sendReq.subject = GetNapiStringValue(env, napiValue, "subject");
894     sendReq.messageClass = GetNapiUint8Value(env, napiValue, "messageClass");
895     sendReq.expiry = GetNapiInt32Value(env, napiValue, "expiry");
896     sendReq.priority = GetNapiUint8Value(env, napiValue, "priority");
897     sendReq.senderVisibility = GetNapiUint8Value(env, napiValue, "senderVisibility");
898     sendReq.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
899     sendReq.readReport = GetNapiUint8Value(env, napiValue, "readReport");
900     sendReq.contentType = GetNapiStringValue(env, napiValue, "contentType");
901     TELEPHONY_LOGI("mms_napi ReadEncodeMmsType end");
902     return true;
903 }
904 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsSendConfContext & sendConf)905 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsSendConfContext &sendConf)
906 {
907     if (!HasNamedProperty(env, napiValue, { "responseState", "transactionId" })) {
908         return false;
909     }
910     sendConf.responseState = GetNapiUint8Value(env, napiValue, "responseState");
911     sendConf.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
912     sendConf.version = GetNapiInt32Value(env, napiValue, "version");
913     sendConf.messageId = GetNapiStringValue(env, napiValue, "messageId");
914     return true;
915 }
916 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsNotificationIndContext & notificationInd)917 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsNotificationIndContext &notificationInd)
918 {
919     std::vector<std::string> checkName = { "transactionId", "messageClass", "messageSize", "expiry",
920         "contentLocation" };
921     if (!HasNamedProperty(env, napiValue, checkName)) {
922         return false;
923     }
924     notificationInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
925     notificationInd.messageClass = GetNapiUint8Value(env, napiValue, "messageClass");
926     notificationInd.messageSize = GetNapiInt64Value(env, napiValue, "messageSize");
927     notificationInd.expiry = GetNapiInt32Value(env, napiValue, "expiry");
928     notificationInd.version = static_cast<uint16_t>(GetNapiInt32Value(env, napiValue, "version"));
929     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
930     notificationInd.from = ReadMmsAddress(env, from);
931     notificationInd.subject = GetNapiStringValue(env, napiValue, "subject");
932     notificationInd.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
933     notificationInd.contentLocation = GetNapiStringValue(env, napiValue, "contentLocation");
934     notificationInd.contentClass = GetNapiInt32Value(env, napiValue, "contentClass");
935     notificationInd.charset = GetNapiUint32Value(env, napiValue, "charset", static_cast<uint32_t>(MmsCharSets::UTF_8));
936     return true;
937 }
938 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsRespIndContext & respInd)939 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsRespIndContext &respInd)
940 {
941     if (!HasNamedProperty(env, napiValue, { "transactionId", "status" })) {
942         return false;
943     }
944     respInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
945     respInd.status = GetNapiUint8Value(env, napiValue, "status");
946     respInd.version = GetNapiInt32Value(env, napiValue, "version");
947     respInd.reportAllowed = GetNapiUint8Value(env, napiValue, "reportAllowed");
948     TELEPHONY_LOGI("respInd.reportAllowed = %{public}d", respInd.reportAllowed);
949     return true;
950 }
951 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsRetrieveConfContext & retrieveConf)952 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsRetrieveConfContext &retrieveConf)
953 {
954     if (!HasNamedProperty(env, napiValue, { "transactionId", "messageId", "date", "contentType" })) {
955         return false;
956     }
957     retrieveConf.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
958     retrieveConf.messageId = GetNapiStringValue(env, napiValue, "messageId");
959     retrieveConf.date = GetNapiInt64Value(env, napiValue, "date");
960     retrieveConf.version = (uint16_t)GetNapiInt32Value(env, napiValue, "version");
961     ReadMmsAddress(env, napiValue, "to", retrieveConf.to);
962     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
963     retrieveConf.from = ReadMmsAddress(env, from);
964     ReadMmsAddress(env, napiValue, "cc", retrieveConf.cc);
965     retrieveConf.subject = GetNapiStringValue(env, napiValue, "subject");
966     retrieveConf.priority = GetNapiUint8Value(env, napiValue, "priority");
967     retrieveConf.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
968     retrieveConf.readReport = GetNapiUint8Value(env, napiValue, "readReport");
969     retrieveConf.retrieveStatus = GetNapiUint8Value(env, napiValue, "retrieveStatus");
970     retrieveConf.retrieveText = GetNapiStringValue(env, napiValue, "retrieveText");
971     retrieveConf.contentType = GetNapiStringValue(env, napiValue, "contentType");
972     return true;
973 }
974 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsAcknowledgeIndContext & acknowledgeInd)975 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsAcknowledgeIndContext &acknowledgeInd)
976 {
977     if (!HasNamedProperty(env, napiValue, { "transactionId" })) {
978         return false;
979     }
980     acknowledgeInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
981     acknowledgeInd.version = GetNapiInt32Value(env, napiValue, "version");
982     acknowledgeInd.reportAllowed = GetNapiUint8Value(env, napiValue, "reportAllowed");
983     return true;
984 }
985 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsDeliveryIndContext & deliveryInd)986 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsDeliveryIndContext &deliveryInd)
987 {
988     if (!HasNamedProperty(env, napiValue, { "messageId", "date", "to", "status" })) {
989         return false;
990     }
991     deliveryInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
992     deliveryInd.date = GetNapiInt64Value(env, napiValue, "date");
993     ReadMmsAddress(env, napiValue, "to", deliveryInd.to);
994     deliveryInd.status = GetNapiUint8Value(env, napiValue, "status");
995     deliveryInd.version = GetNapiInt32Value(env, napiValue, "version");
996     return true;
997 }
998 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsReadOrigIndContext & readOrigInd)999 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsReadOrigIndContext &readOrigInd)
1000 {
1001     if (!HasNamedProperty(env, napiValue, { "version", "messageId", "to", "from", "date", "readStatus" })) {
1002         return false;
1003     }
1004     readOrigInd.version = GetNapiInt32Value(env, napiValue, "version");
1005     readOrigInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
1006     ReadMmsAddress(env, napiValue, "to", readOrigInd.to);
1007     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
1008     readOrigInd.from = ReadMmsAddress(env, from);
1009     readOrigInd.date = GetNapiInt64Value(env, napiValue, "date");
1010     readOrigInd.readStatus = GetNapiUint8Value(env, napiValue, "readStatus");
1011     return true;
1012 }
1013 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsReadRecIndContext & readRecInd)1014 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsReadRecIndContext &readRecInd)
1015 {
1016     if (!HasNamedProperty(env, napiValue, { "version", "messageId", "to", "from", "date", "readStatus" })) {
1017         return false;
1018     }
1019     readRecInd.version = GetNapiInt32Value(env, napiValue, "version");
1020     readRecInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
1021     ReadMmsAddress(env, napiValue, "to", readRecInd.to);
1022     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
1023     readRecInd.from = ReadMmsAddress(env, from);
1024     readRecInd.date = GetNapiInt64Value(env, napiValue, "date");
1025     readRecInd.readStatus = GetNapiUint8Value(env, napiValue, "readStatus");
1026     TELEPHONY_LOGI("context->readRecInd.readStatus = %{public}d", readRecInd.readStatus);
1027     return true;
1028 }
1029 
BuildMmsAttachment(napi_env env,napi_value value)1030 MmsAttachmentContext BuildMmsAttachment(napi_env env, napi_value value)
1031 {
1032     MmsAttachmentContext attachmentContext;
1033     attachmentContext.path = GetNapiStringValue(env, value, "path");
1034     attachmentContext.fileName = GetNapiStringValue(env, value, "fileName");
1035     attachmentContext.contentId = GetNapiStringValue(env, value, "contentId");
1036     attachmentContext.contentLocation = GetNapiStringValue(env, value, "contentLocation");
1037     attachmentContext.contentDisposition = parseDispositionValue(GetNapiInt32Value(env, value, "contentDisposition"));
1038     attachmentContext.contentTransferEncoding = GetNapiStringValue(env, value, "contentTransferEncoding");
1039     attachmentContext.contentType = GetNapiStringValue(env, value, "contentType");
1040     attachmentContext.isSmil = GetNapiBooleanValue(env, value, "isSmil");
1041     napi_value inBuffValue = NapiUtil::GetNamedProperty(env, value, "inBuff");
1042     if (inBuffValue != nullptr) {
1043         uint32_t arrayLength = 0;
1044         int32_t elementInt = 0;
1045         napi_get_array_length(env, inBuffValue, &arrayLength);
1046         if (arrayLength > MMS_PDU_MAX_SIZE) {
1047             TELEPHONY_LOGE("arrayLength over size error");
1048             return attachmentContext;
1049         }
1050         attachmentContext.inBuffLen = arrayLength;
1051         attachmentContext.inBuff = std::make_unique<char[]>(arrayLength);
1052         if (attachmentContext.inBuff == nullptr) {
1053             TELEPHONY_LOGE("make unique error");
1054             return attachmentContext;
1055         }
1056         napi_value elementValue = nullptr;
1057         for (uint32_t i = 0; i < arrayLength; i++) {
1058             napi_get_element(env, inBuffValue, i, &elementValue);
1059             napi_get_value_int32(env, elementValue, &elementInt);
1060             attachmentContext.inBuff[i] = static_cast<char>(elementInt);
1061         }
1062     }
1063     attachmentContext.charset = GetNapiInt32Value(env, value, "charset", static_cast<int32_t>(MmsCharSets::UTF_8));
1064     return attachmentContext;
1065 }
1066 
ReadEncodeMmsAttachment(napi_env env,napi_value value,std::vector<MmsAttachmentContext> & attachment)1067 bool ReadEncodeMmsAttachment(napi_env env, napi_value value, std::vector<MmsAttachmentContext> &attachment)
1068 {
1069     uint32_t arrayLength = 0;
1070     napi_get_array_length(env, value, &arrayLength);
1071     napi_value elementValue = nullptr;
1072     for (uint32_t i = 0; i < arrayLength; i++) {
1073         napi_get_element(env, value, i, &elementValue);
1074         MmsAttachmentContext mmsAttachmentContext = BuildMmsAttachment(env, elementValue);
1075         attachment.push_back(std::move(mmsAttachmentContext));
1076     }
1077     return true;
1078 }
1079 
EncodeMmsType(napi_env env,napi_value mmsTypeValue,EncodeMmsContext & context)1080 bool EncodeMmsType(napi_env env, napi_value mmsTypeValue, EncodeMmsContext &context)
1081 {
1082     bool result = false;
1083     switch (context.messageType) {
1084         case TYPE_MMS_SEND_REQ:
1085             result = ReadEncodeMmsType(env, mmsTypeValue, context.sendReq);
1086             break;
1087         case TYPE_MMS_SEND_CONF:
1088             result = ReadEncodeMmsType(env, mmsTypeValue, context.sendConf);
1089             break;
1090         case TYPE_MMS_NOTIFICATION_IND:
1091             result = ReadEncodeMmsType(env, mmsTypeValue, context.notificationInd);
1092             break;
1093         case TYPE_MMS_RESP_IND:
1094             result = ReadEncodeMmsType(env, mmsTypeValue, context.respInd);
1095             break;
1096         case TYPE_MMS_RETRIEVE_CONF:
1097             result = ReadEncodeMmsType(env, mmsTypeValue, context.retrieveConf);
1098             break;
1099         case TYPE_MMS_ACKNOWLEDGE_IND:
1100             result = ReadEncodeMmsType(env, mmsTypeValue, context.acknowledgeInd);
1101             break;
1102         case TYPE_MMS_DELIVERY_IND:
1103             result = ReadEncodeMmsType(env, mmsTypeValue, context.deliveryInd);
1104             break;
1105         case TYPE_MMS_READ_ORIG_IND:
1106             result = ReadEncodeMmsType(env, mmsTypeValue, context.readOrigInd);
1107             break;
1108         case TYPE_MMS_READ_REC_IND:
1109             result = ReadEncodeMmsType(env, mmsTypeValue, context.readRecInd);
1110             break;
1111         default:
1112             TELEPHONY_LOGE("napi_mms EncodeMms param messageType is incorrect");
1113             break;
1114     }
1115     return result;
1116 }
1117 
ParseEncodeMmsParam(napi_env env,napi_value object,EncodeMmsContext & context)1118 bool ParseEncodeMmsParam(napi_env env, napi_value object, EncodeMmsContext &context)
1119 {
1120     TELEPHONY_LOGI("mms_napi ParseEncodeMmsParam start");
1121     napi_value messageTypeValue = NapiUtil::GetNamedProperty(env, object, "messageType");
1122     if (messageTypeValue == nullptr) {
1123         TELEPHONY_LOGE("messageTypeValue == nullptr");
1124         return false;
1125     }
1126     napi_get_value_int32(env, messageTypeValue, &context.messageType);
1127 
1128     napi_value mmsTypeValue = NapiUtil::GetNamedProperty(env, object, "mmsType");
1129     if (mmsTypeValue == nullptr) {
1130         TELEPHONY_LOGE("mmsTypeValue == nullptr");
1131         return false;
1132     }
1133     bool result = EncodeMmsType(env, mmsTypeValue, context);
1134     if (NapiUtil::HasNamedProperty(env, object, "attachment")) {
1135         napi_value napiAttachment = NapiUtil::GetNamedProperty(env, object, "attachment");
1136         if (napiAttachment != nullptr) {
1137             result = ReadEncodeMmsAttachment(env, napiAttachment, context.attachment);
1138         }
1139     }
1140     return result;
1141 }
1142 
SetAttachmentToCore(MmsMsg & mmsMsg,std::vector<MmsAttachmentContext> & attachment)1143 bool SetAttachmentToCore(MmsMsg &mmsMsg, std::vector<MmsAttachmentContext> &attachment)
1144 {
1145     if (attachment.size() > 0) {
1146         int i = 0;
1147         for (auto it = attachment.begin(); it != attachment.end(); it++) {
1148             MmsAttachment itAttachment;
1149             if (it->path.size() > 0) {
1150                 itAttachment.SetAttachmentFilePath(it->path, it->isSmil);
1151             }
1152             itAttachment.SetIsSmilFile(it->isSmil);
1153             if (it->fileName.size() > 0) {
1154                 itAttachment.SetFileName(it->fileName);
1155             }
1156             if (it->contentId.size() > 0) {
1157                 itAttachment.SetContentId(it->contentId);
1158             }
1159             if (it->contentLocation.size() > 0) {
1160                 itAttachment.SetContentLocation(it->contentLocation);
1161             }
1162             if (it->contentDisposition.size() > 0) {
1163                 itAttachment.SetContentDisposition(it->contentDisposition);
1164             }
1165             if (it->contentTransferEncoding.size() > 0) {
1166                 itAttachment.SetContentTransferEncoding(it->contentTransferEncoding);
1167             }
1168             if (it->contentType.size() > 0) {
1169                 itAttachment.SetContentType(it->contentType);
1170             }
1171             if (it->charset != DEFAULT_ERROR) {
1172                 itAttachment.SetCharSet(it->charset);
1173             }
1174             if (it->inBuffLen > 0) {
1175                 itAttachment.SetDataBuffer(std::move(it->inBuff), it->inBuffLen);
1176             }
1177             if (!mmsMsg.AddAttachment(itAttachment)) {
1178                 TELEPHONY_LOGE("attachment file error");
1179                 return false;
1180             }
1181             i++;
1182         }
1183     }
1184     return true;
1185 }
1186 
setSendReqToCore(MmsMsg & mmsMsg,MmsSendReqContext & context)1187 void setSendReqToCore(MmsMsg &mmsMsg, MmsSendReqContext &context)
1188 {
1189     mmsMsg.SetMmsFrom(context.from);
1190     if (context.to.size() > 0) {
1191         mmsMsg.SetMmsTo(context.to);
1192     }
1193     if (context.transactionId.size() > 0) {
1194         mmsMsg.SetMmsTransactionId(context.transactionId);
1195     }
1196     if (context.version > 0) {
1197         mmsMsg.SetMmsVersion(context.version);
1198     }
1199     if (context.date > 0) {
1200         mmsMsg.SetMmsDate(context.date);
1201     }
1202     if (context.cc.size() > 0) {
1203         for (MmsAddress address : context.cc) {
1204             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_CC, address);
1205         }
1206     }
1207     if (context.bcc.size() > 0) {
1208         for (MmsAddress address : context.bcc) {
1209             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_BCC, address);
1210         }
1211     }
1212     if (context.subject.size() > 0) {
1213         mmsMsg.SetMmsSubject(context.subject);
1214     }
1215     if (context.messageClass > 0) {
1216         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS, context.messageClass);
1217     }
1218     if (context.expiry > 0) {
1219         mmsMsg.SetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY, context.expiry);
1220     }
1221     if (context.priority > 0) {
1222         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY, context.priority);
1223     }
1224     if (context.senderVisibility > 0) {
1225         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_SENDER_VISIBILITY, context.senderVisibility);
1226     }
1227     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1228     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT, context.readReport);
1229     mmsMsg.SetHeaderContentType(context.contentType);
1230 }
1231 
setSendConfToCore(MmsMsg & mmsMsg,MmsSendConfContext & context)1232 void setSendConfToCore(MmsMsg &mmsMsg, MmsSendConfContext &context)
1233 {
1234     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_RESPONSE_STATUS, context.responseState);
1235     mmsMsg.SetMmsTransactionId(context.transactionId);
1236     mmsMsg.SetMmsVersion(context.version);
1237     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1238 }
1239 
setNotificationIndToCore(MmsMsg & mmsMsg,MmsNotificationIndContext & context)1240 void setNotificationIndToCore(MmsMsg &mmsMsg, MmsNotificationIndContext &context)
1241 {
1242     mmsMsg.SetMmsTransactionId(context.transactionId);
1243     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS, context.messageClass);
1244     mmsMsg.SetHeaderLongValue(MmsFieldCode::MMS_MESSAGE_SIZE, context.messageSize);
1245     mmsMsg.SetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY, context.expiry);
1246     mmsMsg.SetMmsVersion(context.version);
1247     mmsMsg.SetMmsFrom(context.from);
1248     mmsMsg.SetMmsSubject(context.subject);
1249     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1250     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_CONTENT_LOCATION, context.contentLocation);
1251     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_CONTENT_CLASS, context.contentClass);
1252 }
1253 
setRespIndToCore(MmsMsg & mmsMsg,MmsRespIndContext & context)1254 void setRespIndToCore(MmsMsg &mmsMsg, MmsRespIndContext &context)
1255 {
1256     mmsMsg.SetMmsTransactionId(context.transactionId);
1257     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_STATUS, context.status);
1258     mmsMsg.SetMmsVersion(context.version);
1259     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED, context.reportAllowed);
1260 }
1261 
setRetrieveConfToCore(MmsMsg & mmsMsg,MmsRetrieveConfContext & context)1262 void setRetrieveConfToCore(MmsMsg &mmsMsg, MmsRetrieveConfContext &context)
1263 {
1264     mmsMsg.SetMmsTransactionId(context.transactionId);
1265     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1266     mmsMsg.SetMmsDate(context.date);
1267     mmsMsg.SetMmsVersion(context.version);
1268     mmsMsg.SetMmsTo(context.to);
1269     mmsMsg.SetMmsFrom(context.from);
1270     if (context.cc.size() > 0) {
1271         for (MmsAddress address : context.cc) {
1272             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_CC, address);
1273         }
1274     }
1275     mmsMsg.SetMmsSubject(context.subject);
1276     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY, context.priority);
1277     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1278     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT, context.readReport);
1279     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_RETRIEVE_STATUS, context.retrieveStatus);
1280     if (!context.retrieveText.empty()) {
1281         mmsMsg.SetHeaderEncodedStringValue(
1282             MmsFieldCode::MMS_RETRIEVE_TEXT, context.retrieveText, (uint32_t)MmsCharSets::UTF_8);
1283     }
1284     mmsMsg.SetHeaderContentType(context.contentType);
1285 }
1286 
setAcknowledgeIndToCore(MmsMsg & mmsMsg,MmsAcknowledgeIndContext & context)1287 void setAcknowledgeIndToCore(MmsMsg &mmsMsg, MmsAcknowledgeIndContext &context)
1288 {
1289     mmsMsg.SetMmsTransactionId(context.transactionId);
1290     mmsMsg.SetMmsVersion(context.version);
1291     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED, context.reportAllowed);
1292 }
1293 
setDeliveryIndToCore(MmsMsg & mmsMsg,MmsDeliveryIndContext & context)1294 void setDeliveryIndToCore(MmsMsg &mmsMsg, MmsDeliveryIndContext &context)
1295 {
1296     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1297     mmsMsg.SetMmsDate(context.date);
1298     mmsMsg.SetMmsTo(context.to);
1299     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_STATUS, context.status);
1300     mmsMsg.SetMmsVersion(context.version);
1301 }
setReadOrigIndToCore(MmsMsg & mmsMsg,MmsReadOrigIndContext & context)1302 void setReadOrigIndToCore(MmsMsg &mmsMsg, MmsReadOrigIndContext &context)
1303 {
1304     mmsMsg.SetMmsVersion(context.version);
1305     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1306     mmsMsg.SetMmsTo(context.to);
1307     mmsMsg.SetMmsFrom(context.from);
1308     if (context.date != 0) {
1309         mmsMsg.SetMmsDate(context.date);
1310     }
1311     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS, context.readStatus);
1312 }
1313 
setReadRecIndToCore(MmsMsg & mmsMsg,MmsReadRecIndContext & context)1314 void setReadRecIndToCore(MmsMsg &mmsMsg, MmsReadRecIndContext &context)
1315 {
1316     mmsMsg.SetMmsVersion(context.version);
1317     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1318     mmsMsg.SetMmsTo(context.to);
1319     mmsMsg.SetMmsFrom(context.from);
1320     if (context.date != 0) {
1321         mmsMsg.SetMmsDate(context.date);
1322     }
1323     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS, context.readStatus);
1324 }
1325 
SetRequestToCore(MmsMsg & mmsMsg,EncodeMmsContext * context)1326 void SetRequestToCore(MmsMsg &mmsMsg, EncodeMmsContext *context)
1327 {
1328     if (context == nullptr) {
1329         TELEPHONY_LOGE("context is nullptr");
1330         return;
1331     }
1332     switch (context->messageType) {
1333         case MessageType::TYPE_MMS_SEND_REQ:
1334             setSendReqToCore(mmsMsg, context->sendReq);
1335             break;
1336         case MessageType::TYPE_MMS_SEND_CONF:
1337             setSendConfToCore(mmsMsg, context->sendConf);
1338             break;
1339         case MessageType::TYPE_MMS_NOTIFICATION_IND:
1340             setNotificationIndToCore(mmsMsg, context->notificationInd);
1341             break;
1342         case MessageType::TYPE_MMS_RESP_IND:
1343             setRespIndToCore(mmsMsg, context->respInd);
1344             break;
1345         case MessageType::TYPE_MMS_RETRIEVE_CONF:
1346             setRetrieveConfToCore(mmsMsg, context->retrieveConf);
1347             break;
1348         case MessageType::TYPE_MMS_ACKNOWLEDGE_IND:
1349             setAcknowledgeIndToCore(mmsMsg, context->acknowledgeInd);
1350             break;
1351         case MessageType::TYPE_MMS_DELIVERY_IND:
1352             setDeliveryIndToCore(mmsMsg, context->deliveryInd);
1353             break;
1354         case MessageType::TYPE_MMS_READ_ORIG_IND:
1355             setReadOrigIndToCore(mmsMsg, context->readOrigInd);
1356             break;
1357         case MessageType::TYPE_MMS_READ_REC_IND:
1358             setReadRecIndToCore(mmsMsg, context->readRecInd);
1359             break;
1360         default:
1361             break;
1362     }
1363 }
1364 
NativeEncodeMms(napi_env env,void * data)1365 void NativeEncodeMms(napi_env env, void *data)
1366 {
1367     if (data == nullptr) {
1368         TELEPHONY_LOGE("NativeEncodeMms data is nullptr");
1369         NapiUtil::ThrowParameterError(env);
1370         return;
1371     }
1372 
1373     EncodeMmsContext *context = static_cast<EncodeMmsContext *>(data);
1374     if (context == nullptr) {
1375         TELEPHONY_LOGE("context is nullptr");
1376         return;
1377     }
1378     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
1379         TELEPHONY_LOGE("Non-system applications use system APIs!");
1380         context->errorCode = TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
1381         return;
1382     }
1383     MmsMsg mmsMsg;
1384     mmsMsg.SetMmsMessageType(static_cast<uint8_t>(WrapEncodeMmsStatus(context->messageType)));
1385     if (!SetAttachmentToCore(mmsMsg, context->attachment)) {
1386         context->errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
1387         context->resolved = false;
1388         return;
1389     }
1390     SetRequestToCore(mmsMsg, context);
1391     auto encodeResult = mmsMsg.EncodeMsg(context->bufferLen);
1392     if (encodeResult != nullptr) {
1393         context->outBuffer = std::move(encodeResult);
1394         context->errorCode = TELEPHONY_ERR_SUCCESS;
1395         context->resolved = true;
1396     } else {
1397         context->errorCode = TELEPHONY_ERR_FAIL;
1398         context->resolved = false;
1399     }
1400     TELEPHONY_LOGD("napi_mms NativeEncodeMms length:%{private}d", context->bufferLen);
1401 }
1402 
EncodeMmsCallback(napi_env env,napi_status status,void * data)1403 void EncodeMmsCallback(napi_env env, napi_status status, void *data)
1404 {
1405     auto context = static_cast<EncodeMmsContext *>(data);
1406 
1407     napi_value callbackValue = nullptr;
1408     if (context->resolved) {
1409         napi_create_array(env, &callbackValue);
1410         for (uint32_t i = 0; i < context->bufferLen; i++) {
1411             napi_value itemValue = nullptr;
1412             int32_t element = context->outBuffer[i];
1413             napi_create_int32(env, element, &itemValue);
1414             napi_set_element(env, callbackValue, i, itemValue);
1415         }
1416     } else {
1417         JsError error = NapiUtil::ConverErrorMessageForJs(context->errorCode);
1418         callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
1419     }
1420     NapiUtil::Handle2ValueCallback(env, context, callbackValue);
1421 }
1422 
EncodeMms(napi_env env,napi_callback_info info)1423 napi_value NapiMms::EncodeMms(napi_env env, napi_callback_info info)
1424 {
1425     TELEPHONY_LOGI("napi_mms EncodeMms start");
1426     napi_value result = nullptr;
1427     size_t parameterCount = TWO_PARAMETERS;
1428     napi_value parameters[TWO_PARAMETERS] = { 0 };
1429     napi_value thisVar = nullptr;
1430     void *data = nullptr;
1431     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
1432     if (!MatchEncodeMms(env, parameters, parameterCount)) {
1433         TELEPHONY_LOGE("EncodeMms parameter matching failed.");
1434         NapiUtil::ThrowParameterError(env);
1435         return nullptr;
1436     }
1437     auto context = std::make_unique<EncodeMmsContext>().release();
1438     if (context == nullptr) {
1439         TELEPHONY_LOGE("EncodeMms EncodeMmsContext is nullptr.");
1440         NapiUtil::ThrowParameterError(env);
1441         return nullptr;
1442     }
1443 
1444     if (!ParseEncodeMmsParam(env, parameters[0], *context)) {
1445         free(context);
1446         context = nullptr;
1447         NapiUtil::ThrowParameterError(env);
1448         return nullptr;
1449     }
1450     if (parameterCount == TWO_PARAMETERS) {
1451         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
1452     }
1453 
1454     result = NapiUtil::HandleAsyncWork(env, context, "EncodeMms", NativeEncodeMms, EncodeMmsCallback);
1455     TELEPHONY_LOGI("napi_mms EncodeMms end");
1456     return result;
1457 }
1458 
InitEnumMmsCharSets(napi_env env,napi_value exports)1459 napi_value NapiMms::InitEnumMmsCharSets(napi_env env, napi_value exports)
1460 {
1461     napi_property_descriptor desc[] = {
1462         DECLARE_NAPI_STATIC_PROPERTY("BIG5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::BIG5))),
1463         DECLARE_NAPI_STATIC_PROPERTY(
1464             "ISO_10646_UCS_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2))),
1465         DECLARE_NAPI_STATIC_PROPERTY(
1466             "ISO_8859_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_1))),
1467         DECLARE_NAPI_STATIC_PROPERTY(
1468             "ISO_8859_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_2))),
1469         DECLARE_NAPI_STATIC_PROPERTY(
1470             "ISO_8859_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_3))),
1471         DECLARE_NAPI_STATIC_PROPERTY(
1472             "ISO_8859_4", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_4))),
1473         DECLARE_NAPI_STATIC_PROPERTY(
1474             "ISO_8859_5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_5))),
1475         DECLARE_NAPI_STATIC_PROPERTY(
1476             "ISO_8859_6", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_6))),
1477         DECLARE_NAPI_STATIC_PROPERTY(
1478             "ISO_8859_7", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_7))),
1479         DECLARE_NAPI_STATIC_PROPERTY(
1480             "ISO_8859_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_8))),
1481         DECLARE_NAPI_STATIC_PROPERTY(
1482             "ISO_8859_9", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_9))),
1483         DECLARE_NAPI_STATIC_PROPERTY(
1484             "SHIFT_JIS", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::SHIFT_JIS))),
1485         DECLARE_NAPI_STATIC_PROPERTY(
1486             "US_ASCII", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::US_ASCII))),
1487         DECLARE_NAPI_STATIC_PROPERTY("UTF_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::UTF_8))),
1488     };
1489     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1490     return exports;
1491 }
1492 
CreateEnumConstructor(napi_env env,napi_callback_info info)1493 static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
1494 {
1495     napi_value thisArg = nullptr;
1496     void *data = nullptr;
1497     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
1498     napi_value global = nullptr;
1499     napi_get_global(env, &global);
1500     return thisArg;
1501 }
1502 
InitSupportEnumMmsCharSets(napi_env env,napi_value exports)1503 napi_value NapiMms::InitSupportEnumMmsCharSets(napi_env env, napi_value exports)
1504 {
1505     napi_property_descriptor desc[] = {
1506         DECLARE_NAPI_STATIC_PROPERTY("BIG5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::BIG5))),
1507         DECLARE_NAPI_STATIC_PROPERTY(
1508             "ISO_10646_UCS_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2))),
1509         DECLARE_NAPI_STATIC_PROPERTY(
1510             "ISO_8859_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_1))),
1511         DECLARE_NAPI_STATIC_PROPERTY(
1512             "ISO_8859_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_2))),
1513         DECLARE_NAPI_STATIC_PROPERTY(
1514             "ISO_8859_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_3))),
1515         DECLARE_NAPI_STATIC_PROPERTY(
1516             "ISO_8859_4", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_4))),
1517         DECLARE_NAPI_STATIC_PROPERTY(
1518             "ISO_8859_5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_5))),
1519         DECLARE_NAPI_STATIC_PROPERTY(
1520             "ISO_8859_6", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_6))),
1521         DECLARE_NAPI_STATIC_PROPERTY(
1522             "ISO_8859_7", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_7))),
1523         DECLARE_NAPI_STATIC_PROPERTY(
1524             "ISO_8859_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_8))),
1525         DECLARE_NAPI_STATIC_PROPERTY(
1526             "ISO_8859_9", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_9))),
1527         DECLARE_NAPI_STATIC_PROPERTY(
1528             "SHIFT_JIS", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::SHIFT_JIS))),
1529         DECLARE_NAPI_STATIC_PROPERTY(
1530             "US_ASCII", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::US_ASCII))),
1531         DECLARE_NAPI_STATIC_PROPERTY("UTF_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::UTF_8))),
1532     };
1533     napi_value result = nullptr;
1534     napi_define_class(env, "MmsCharSets", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1535         sizeof(desc) / sizeof(*desc), desc, &result);
1536     napi_set_named_property(env, exports, "MmsCharSets", result);
1537     return exports;
1538 }
1539 
InitEnumMessageType(napi_env env,napi_value exports)1540 napi_value NapiMms::InitEnumMessageType(napi_env env, napi_value exports)
1541 {
1542     napi_property_descriptor desc[] = {
1543         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_REQ",
1544             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_REQ))),
1545         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_CONF",
1546             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_CONF))),
1547         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_NOTIFICATION_IND",
1548             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND))),
1549         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RESP_IND",
1550             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND))),
1551         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RETRIEVE_CONF",
1552             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF))),
1553         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_ACKNOWLEDGE_IND",
1554             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND))),
1555         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_DELIVERY_IND",
1556             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_DELIVERY_IND))),
1557         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_REC_IND",
1558             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_REC_IND))),
1559         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_ORIG_IND",
1560             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND))),
1561     };
1562     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1563     return exports;
1564 }
1565 
InitSupportEnumMessageType(napi_env env,napi_value exports)1566 napi_value NapiMms::InitSupportEnumMessageType(napi_env env, napi_value exports)
1567 {
1568     napi_property_descriptor desc[] = {
1569         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_REQ",
1570             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_REQ))),
1571         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_CONF",
1572             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_CONF))),
1573         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_NOTIFICATION_IND",
1574             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND))),
1575         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RESP_IND",
1576             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND))),
1577         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RETRIEVE_CONF",
1578             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF))),
1579         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_ACKNOWLEDGE_IND",
1580             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND))),
1581         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_DELIVERY_IND",
1582             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_DELIVERY_IND))),
1583         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_REC_IND",
1584             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_REC_IND))),
1585         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_ORIG_IND",
1586             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND))),
1587     };
1588     napi_value result = nullptr;
1589     napi_define_class(env, "MessageType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1590         sizeof(desc) / sizeof(*desc), desc, &result);
1591     napi_set_named_property(env, exports, "MessageType", result);
1592     return exports;
1593 }
1594 
InitEnumPriorityType(napi_env env,napi_value exports)1595 napi_value NapiMms::InitEnumPriorityType(napi_env env, napi_value exports)
1596 {
1597     napi_property_descriptor desc[] = {
1598         DECLARE_NAPI_STATIC_PROPERTY(
1599             "MMS_LOW", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_LOW))),
1600         DECLARE_NAPI_STATIC_PROPERTY(
1601             "MMS_NORMAL", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_NORMAL))),
1602         DECLARE_NAPI_STATIC_PROPERTY(
1603             "MMS_HIGH", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_HIGH))),
1604     };
1605     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1606     return exports;
1607 }
1608 
InitSupportEnumPriorityType(napi_env env,napi_value exports)1609 napi_value NapiMms::InitSupportEnumPriorityType(napi_env env, napi_value exports)
1610 {
1611     napi_property_descriptor desc[] = {
1612         DECLARE_NAPI_STATIC_PROPERTY(
1613             "MMS_LOW", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_LOW))),
1614         DECLARE_NAPI_STATIC_PROPERTY(
1615             "MMS_NORMAL", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_NORMAL))),
1616         DECLARE_NAPI_STATIC_PROPERTY(
1617             "MMS_HIGH", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_HIGH))),
1618     };
1619     napi_value result = nullptr;
1620     napi_define_class(env, "MmsPriorityType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1621         sizeof(desc) / sizeof(*desc), desc, &result);
1622     napi_set_named_property(env, exports, "MmsPriorityType", result);
1623     return exports;
1624 }
1625 
InitEnumVersionType(napi_env env,napi_value exports)1626 napi_value NapiMms::InitEnumVersionType(napi_env env, napi_value exports)
1627 {
1628     napi_property_descriptor desc[] = {
1629         DECLARE_NAPI_STATIC_PROPERTY(
1630             "MMS_VERSION_1_0", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_0))),
1631         DECLARE_NAPI_STATIC_PROPERTY(
1632             "MMS_VERSION_1_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_1))),
1633         DECLARE_NAPI_STATIC_PROPERTY(
1634             "MMS_VERSION_1_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_2))),
1635         DECLARE_NAPI_STATIC_PROPERTY(
1636             "MMS_VERSION_1_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_3))),
1637     };
1638     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1639     return exports;
1640 }
1641 
InitSupportEnumVersionType(napi_env env,napi_value exports)1642 napi_value NapiMms::InitSupportEnumVersionType(napi_env env, napi_value exports)
1643 {
1644     napi_property_descriptor desc[] = {
1645         DECLARE_NAPI_STATIC_PROPERTY(
1646             "MMS_VERSION_1_0", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_0))),
1647         DECLARE_NAPI_STATIC_PROPERTY(
1648             "MMS_VERSION_1_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_1))),
1649         DECLARE_NAPI_STATIC_PROPERTY(
1650             "MMS_VERSION_1_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_2))),
1651         DECLARE_NAPI_STATIC_PROPERTY(
1652             "MMS_VERSION_1_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_3))),
1653     };
1654     napi_value result = nullptr;
1655     napi_define_class(env, "MmsVersionType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1656         sizeof(desc) / sizeof(*desc), desc, &result);
1657     napi_set_named_property(env, exports, "MmsVersionType", result);
1658     return exports;
1659 }
1660 
InitEnumDispositionType(napi_env env,napi_value exports)1661 napi_value NapiMms::InitEnumDispositionType(napi_env env, napi_value exports)
1662 {
1663     napi_property_descriptor desc[] = {
1664         DECLARE_NAPI_STATIC_PROPERTY(
1665             "FROM_DATA", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::FROM_DATA))),
1666         DECLARE_NAPI_STATIC_PROPERTY(
1667             "ATTACHMENT", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::ATTACHMENT))),
1668         DECLARE_NAPI_STATIC_PROPERTY(
1669             "INLINE", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::INLINE))),
1670     };
1671     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1672     return exports;
1673 }
1674 
InitSupportEnumDispositionType(napi_env env,napi_value exports)1675 napi_value NapiMms::InitSupportEnumDispositionType(napi_env env, napi_value exports)
1676 {
1677     napi_property_descriptor desc[] = {
1678         DECLARE_NAPI_STATIC_PROPERTY(
1679             "FROM_DATA", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::FROM_DATA))),
1680         DECLARE_NAPI_STATIC_PROPERTY(
1681             "ATTACHMENT", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::ATTACHMENT))),
1682         DECLARE_NAPI_STATIC_PROPERTY(
1683             "INLINE", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::INLINE))),
1684     };
1685     napi_value result = nullptr;
1686     napi_define_class(env, "DispositionType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1687         sizeof(desc) / sizeof(*desc), desc, &result);
1688     napi_set_named_property(env, exports, "DispositionType", result);
1689     return exports;
1690 }
1691 
InitEnumReportAllowedType(napi_env env,napi_value exports)1692 napi_value NapiMms::InitEnumReportAllowedType(napi_env env, napi_value exports)
1693 {
1694     napi_property_descriptor desc[] = {
1695         DECLARE_NAPI_STATIC_PROPERTY(
1696             "MMS_YES", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_YES))),
1697         DECLARE_NAPI_STATIC_PROPERTY(
1698             "MMS_NO", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_NO))),
1699     };
1700     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1701     return exports;
1702 }
1703 
InitSupportEnumReportAllowedType(napi_env env,napi_value exports)1704 napi_value NapiMms::InitSupportEnumReportAllowedType(napi_env env, napi_value exports)
1705 {
1706     napi_property_descriptor desc[] = {
1707         DECLARE_NAPI_STATIC_PROPERTY(
1708             "MMS_YES", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_YES))),
1709         DECLARE_NAPI_STATIC_PROPERTY(
1710             "MMS_NO", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_NO))),
1711     };
1712     napi_value result = nullptr;
1713     napi_define_class(env, "ReportType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1714         sizeof(desc) / sizeof(*desc), desc, &result);
1715     napi_set_named_property(env, exports, "ReportType", result);
1716     return exports;
1717 }
1718 } // namespace Telephony
1719 } // namespace OHOS
1720