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