1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "map_mse_server.h"
17 #include <algorithm>
18 #include <atomic>
19 #include <codecvt>
20 #include <ctime>
21 #include <iomanip>
22 #include <locale>
23 #include <sstream>
24 #include "adapter_config.h"
25 #include "data_access.h"
26 #include "map_mse_instance.h"
27
28 namespace OHOS {
29 namespace bluetooth {
30 const uint8_t MapMseServer::MAS_TARGET[TARGET_SIZE] = {
31 0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, 0xdb, 0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
32 };
33 const std::string MapMseServer::HEADER_TYPE_GET_FOLDER_LISTING = "x-obex/folder-listing";
34 const std::string MapMseServer::HEADER_TYPE_GET_MESSAGE_LISTING = "x-bt/MAP-msg-listing";
35 const std::string MapMseServer::HEADER_TYPE_GET_CONVO_LISTING = "x-bt/MAP-convo-listing";
36 const std::string MapMseServer::HEADER_TYPE_MESSAGE = "x-bt/message";
37 const std::string MapMseServer::HEADER_TYPE_SET_MESSAGE_STATUS = "x-bt/messageStatus";
38 const std::string MapMseServer::HEADER_TYPE_SET_NOTIFICATION_REGISTRATION = "x-bt/MAP-NotificationRegistration";
39 const std::string MapMseServer::HEADER_TYPE_MESSAGE_UPDATE = "x-bt/MAP-messageUpdate";
40 const std::string MapMseServer::HEADER_TYPE_GET_MAS_INSTANCE_INFORMATION = "x-bt/MASInstanceInformation";
41 const std::string MapMseServer::HEADER_TYPE_OWNER_STATUS = "x-bt/ownerStatus";
42 const std::string MapMseServer::HEADER_TYPE_SET_NOTIFICATION_FILTER = "x-bt/MAP-notification-filter";
43
MapMseServer(MapMseResource & content,utility::Dispatcher & dispatcher,MseObserver & observer,MapMseInstance & instance,uint8_t rfcommNo,uint16_t l2capPsm,std::string serviceName,const MapAccountItem & accountItem,bool smsSupport)44 MapMseServer::MapMseServer(MapMseResource &content, utility::Dispatcher &dispatcher, MseObserver &observer,
45 MapMseInstance &instance, uint8_t rfcommNo, uint16_t l2capPsm, std::string serviceName,
46 const MapAccountItem &accountItem, bool smsSupport)
47 : content_(content),
48 dispatcher_(dispatcher),
49 observer_(observer),
50 instance_(instance),
51 rfcommNo_(rfcommNo),
52 goepL2capPsm_(l2capPsm),
53 serviceName_(serviceName),
54 accountItem_(accountItem),
55 smsSupport_(smsSupport)
56 {
57 MSE_LOG_INFO("%{public}s Create", __PRETTY_FUNCTION__);
58
59 mseObexServer_ = std::make_unique<MseObexServer>(*this);
60 CreateFolder();
61 supportedFeatureMask_ = MAP_SUPPORTED_FEATURES_DEFAULT;
62 connectId_ = ATOMIC_VAR_INIT();
63 }
64
CreateFolder(void)65 void MapMseServer::CreateFolder(void)
66 {
67 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
68
69 rootFolder_ = std::make_unique<MapMseFolder>("root");
70 MapMseFolder *telecom = rootFolder_->SetFolder("telecom");
71 MapMseFolder *msg = telecom->SetFolder("msg");
72 InitDefaultFolder(*msg);
73 if (smsSupport_) {
74 InitSmsMmsFolder(*msg);
75 }
76 if (MessageType::EMAIL == accountItem_.msgType_) {
77 InitEmailFolder(*msg);
78 } else if (MessageType::IM == accountItem_.msgType_) {
79 InitImFolder(*msg);
80 }
81 currentFolder_ = rootFolder_.get();
82 }
83
InitDefaultFolder(MapMseFolder & root)84 void MapMseServer::InitDefaultFolder(MapMseFolder &root)
85 {
86 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
87 root.SetFolder(MAP_MSE_FOLDER_NAME_INBOX);
88 root.SetFolder(MAP_MSE_FOLDER_NAME_SENT);
89 root.SetFolder(MAP_MSE_FOLDER_NAME_OUTBOX);
90 root.SetFolder(MAP_MSE_FOLDER_NAME_DELETED);
91 }
92
InitSmsMmsFolder(MapMseFolder & root)93 void MapMseServer::InitSmsMmsFolder(MapMseFolder &root)
94 {
95 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
96 root.SetSmsMmsFolder(MAP_MSE_FOLDER_NAME_INBOX);
97 root.SetSmsMmsFolder(MAP_MSE_FOLDER_NAME_SENT);
98 root.SetSmsMmsFolder(MAP_MSE_FOLDER_NAME_DRAFT);
99 root.SetSmsMmsFolder(MAP_MSE_FOLDER_NAME_OUTBOX);
100 root.SetSmsMmsFolder(MAP_MSE_FOLDER_NAME_DELETED);
101 }
102
InitEmailFolder(MapMseFolder & parentFolder,int depth)103 void MapMseServer::InitEmailFolder(MapMseFolder &parentFolder, int depth)
104 {
105 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
106
107 auto dataProvider = DataAccess::GetConnection(DEFAULT_MAP_MSE_DB_FILE);
108 if (!dataProvider) {
109 return;
110 }
111
112 std::unordered_map<int, std::string> data = GetEmailInfo(dataProvider, parentFolder);
113 for (auto &fd : data) {
114 MapMseFolder *ff = parentFolder.GetSubFolder(fd.second);
115 if (ff == nullptr) {
116 continue;
117 }
118 auto secondData = GetEmailInfo(dataProvider, *ff);
119 for (auto &sd : secondData) {
120 MapMseFolder *sf = parentFolder.GetSubFolder(sd.second);
121 if (sf == nullptr) {
122 continue;
123 }
124 GetEmailInfo(dataProvider, *sf);
125 }
126 }
127 }
128
GetEmailInfo(std::unique_ptr<DataAccess> & dataAccess,MapMseFolder & parent)129 std::unordered_map<int, std::string> MapMseServer::GetEmailInfo(
130 std::unique_ptr<DataAccess> &dataAccess, MapMseFolder &parent)
131 {
132 std::unordered_map<int, std::string> result {};
133 std::string sql = "select id, name from Folder where parent_id = ? ";
134 auto stmt = dataAccess->CreateStatement(sql);
135 stmt->SetParamInt(1, parent.GetFolderId());
136 auto dataResult = stmt->Query();
137 while (dataResult->Next()) {
138 int id = dataResult->GetInt(0);
139 std::string name = dataResult->GetString(1);
140 result[id] = name;
141 parent.SetEmailFolder(id, name);
142 }
143 return result;
144 }
145
InitImFolder(MapMseFolder & root)146 void MapMseServer::InitImFolder(MapMseFolder &root)
147 {
148 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
149
150 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_OTHER, MAP_MSE_FOLDER_NAME_OTHER);
151 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_INBOX, MAP_MSE_FOLDER_NAME_INBOX);
152 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_SENT, MAP_MSE_FOLDER_NAME_SENT);
153 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_DRAFT, MAP_MSE_FOLDER_NAME_DRAFT);
154 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_OUTBOX, MAP_MSE_FOLDER_NAME_OUTBOX);
155 root.SetImFolder(ImFolderId::MAP_MSE_FOLDER_ID_DELETED, MAP_MSE_FOLDER_NAME_DELETED);
156 }
157
~MapMseServer()158 MapMseServer::~MapMseServer()
159 {
160 MSE_LOG_INFO("%{public}s Delete", __PRETTY_FUNCTION__);
161
162 currentFolder_ = nullptr;
163 }
164
MseObexServer(MapMseServer & mseServer)165 MapMseServer::MseObexServer::MseObexServer(MapMseServer &mseServer) : mseServer_(&mseServer)
166 {
167 MSE_LOG_INFO("%{public}s Create", __PRETTY_FUNCTION__);
168 }
169
~MseObexServer()170 MapMseServer::MseObexServer::~MseObexServer()
171 {
172 MSE_LOG_INFO("%{public}s Delete", __PRETTY_FUNCTION__);
173 }
174
OnConnect(ObexServerSession & session,const ObexHeader & req)175 void MapMseServer::MseObexServer::OnConnect(ObexServerSession &session, const ObexHeader &req)
176 {
177 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
178
179 if (!req.GetItemTarget()) {
180 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE, true));
181 return;
182 }
183 if (req.GetItemTarget()->GetHeaderDataSize() != TARGET_SIZE) {
184 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE, true));
185 return;
186 }
187 if (req.GetItemAppParams()) {
188 if (req.GetItemAppParams()->GetTlvtriplet(MapMseParams::PARAM_MAP_SUPPORTED_FEATURES)) {
189 mseServer_->supportedFeatureMask_ =
190 req.GetItemAppParams()->GetTlvtriplet(MapMseParams::PARAM_MAP_SUPPORTED_FEATURES)->GetUint32();
191 }
192 }
193 std::unique_ptr<uint8_t[]> masTarget = req.GetItemTarget()->GetBytes();
194 for (int i = 0; i < TARGET_SIZE; i++) {
195 if (masTarget[i] != MAS_TARGET[i]) {
196 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE, true));
197 return;
198 }
199 }
200 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS, true);
201 mseServer_->connectId_++;
202 resHeader->AppendItemConnectionId(mseServer_->connectId_.load());
203 resHeader->AppendItemWho(MAS_TARGET, TARGET_SIZE);
204 session.SendResponse(*resHeader);
205 mseServer_->observer_.OnConnected(session);
206 }
207
OnTransportConnect(ObexIncomingConnect & incomingConnect)208 void MapMseServer::MseObexServer::OnTransportConnect(ObexIncomingConnect &incomingConnect)
209 {
210 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
211
212 mseServer_->observer_.OnIncomingConnect(incomingConnect);
213 }
214
OnTransportDisconnected(const std::string & btAddr)215 void MapMseServer::MseObexServer::OnTransportDisconnected(const std::string &btAddr)
216 {
217 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
218
219 RawAddress remoteAddr(btAddr);
220 mseServer_->observer_.OnTransportDisconnected(remoteAddr);
221 }
222
OnTransportError(const std::string & btAddr,int errCd,const std::string & msg)223 void MapMseServer::MseObexServer::OnTransportError(const std::string &btAddr, int errCd, const std::string &msg)
224 {
225 MSE_LOG_ERROR("%{public}s Enter", __PRETTY_FUNCTION__);
226 }
227
OnError(const int errCd,const std::string & msg)228 void MapMseServer::MseObexServer::OnError(const int errCd, const std::string &msg)
229 {
230 MSE_LOG_ERROR("%{public}s Enter", __PRETTY_FUNCTION__);
231 }
232
OnDisconnect(ObexServerSession & session,const ObexHeader & req)233 void MapMseServer::MseObexServer::OnDisconnect(ObexServerSession &session, const ObexHeader &req)
234 {
235 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
236
237 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS, false);
238 session.SendResponse(*resHeader);
239 mseServer_->observer_.OnDisconnect(session.GetRemoteAddr());
240 }
241
OnPut(ObexServerSession & session,const ObexHeader & req)242 void MapMseServer::MseObexServer::OnPut(ObexServerSession &session, const ObexHeader &req)
243 {
244 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
245 if (!req.GetItemType()) {
246 MSE_LOG_ERROR("Item Type of Obex header is error.");
247 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
248 return;
249 }
250 OnBusy(session, true);
251 auto msgType = req.GetItemType()->GetString();
252 if (HEADER_TYPE_MESSAGE == msgType) {
253 mseServer_->PushMessageResponse(session, req);
254 } else if (HEADER_TYPE_SET_NOTIFICATION_REGISTRATION == msgType) {
255 mseServer_->SetNotificationRegistration(session, req);
256 } else if (HEADER_TYPE_SET_NOTIFICATION_FILTER == msgType) {
257 mseServer_->SetNotificationFilter(session, req);
258 } else if (HEADER_TYPE_SET_MESSAGE_STATUS == msgType) {
259 if (!req.GetItemName()) {
260 MSE_LOG_ERROR("Item Name of Obex header is error.");
261 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
262 return;
263 }
264 mseServer_->SetMessageStatus(session, req);
265 } else if (HEADER_TYPE_MESSAGE_UPDATE == msgType) {
266 mseServer_->UpdateInbox(session, req);
267 } else if (HEADER_TYPE_OWNER_STATUS == msgType) {
268 mseServer_->SetOwnerStatus(session, req);
269 }
270 OnBusy(session, false);
271 }
272
OnGet(ObexServerSession & session,const ObexHeader & req)273 void MapMseServer::MseObexServer::OnGet(ObexServerSession &session, const ObexHeader &req)
274 {
275 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
276
277 if (!req.GetItemType()) {
278 MSE_LOG_ERROR("Item Type of Obex header is error.");
279 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
280 return;
281 }
282 OnBusy(session, true);
283 auto msgType = req.GetItemType()->GetString();
284 if (HEADER_TYPE_MESSAGE == msgType) {
285 mseServer_->GetMessageResponse(session, req);
286 } else if (HEADER_TYPE_GET_MESSAGE_LISTING == msgType) {
287 if (!req.GetItemName()) {
288 MSE_LOG_ERROR("Item Name of Obex header is error.");
289 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
290 OnBusy(session, false);
291 return;
292 }
293 mseServer_->GetMessageListing(session, req);
294 } else if (HEADER_TYPE_GET_CONVO_LISTING == msgType) {
295 mseServer_->GetConversationListing(session, req);
296 } else if (HEADER_TYPE_GET_FOLDER_LISTING == msgType) {
297 mseServer_->GetFolderListing(session, req);
298 } else if (HEADER_TYPE_OWNER_STATUS == msgType) {
299 mseServer_->GetOwnerStatus(session, req);
300 } else if (HEADER_TYPE_GET_MAS_INSTANCE_INFORMATION == msgType) {
301 mseServer_->GetMASInstanceInformation(session, req);
302 }
303 OnBusy(session, false);
304 }
305
OnAbort(ObexServerSession & session,const ObexHeader & req)306 void MapMseServer::MseObexServer::OnAbort(ObexServerSession &session, const ObexHeader &req)
307 {
308 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
309 }
310
OnBusy(ObexServerSession & session,bool isBusy) const311 void MapMseServer::MseObexServer::OnBusy(ObexServerSession &session, bool isBusy) const
312 {
313 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
314
315 const RawAddress &remoteAddr = session.GetRemoteAddr();
316 mseServer_->observer_.OnBusy(remoteAddr, isBusy);
317 }
318
OnSetPath(ObexServerSession & session,const ObexHeader & req)319 void MapMseServer::MseObexServer::OnSetPath(ObexServerSession &session, const ObexHeader &req)
320 {
321 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
322
323 std::string folderName;
324 if (req.GetItemName() != nullptr) {
325 if (req.GetItemName()->GetUnicodeText().size() != 0) {
326 folderName = mseServer_->ToUtf8String(req.GetItemName()->GetUnicodeText());
327 std::transform(folderName.begin(), folderName.end(), folderName.begin(), ::tolower);
328 }
329 }
330 uint8_t flages = *req.GetFieldFlags();
331 if ((flages & 0x02) != 0x02) {
332 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
333 return;
334 }
335 MSE_LOG_INFO("MapMseServer::MseObexServer::OnSetPath folderName : %{public}s flages : %u ", folderName.c_str(), flages);
336 MapMseFolder *tempFolder = nullptr;
337 if (folderName.size() == 0) {
338 if (flages == 0x03) {
339 tempFolder = mseServer_->currentFolder_->GetParent();
340 } else {
341 tempFolder = mseServer_->rootFolder_.get();
342 }
343 } else {
344 if (flages == 0x03) {
345 tempFolder = mseServer_->currentFolder_->GetParent();
346 if (tempFolder == nullptr) {
347 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_FOUND));
348 return;
349 }
350 }
351 tempFolder = mseServer_->currentFolder_->GetSubFolder(folderName);
352 }
353 if (tempFolder == nullptr) {
354 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_FOUND));
355 return;
356 }
357 mseServer_->currentFolder_ = tempFolder;
358 MSE_LOG_INFO("Current Folder : %{public}s ", mseServer_->currentFolder_->GetName().c_str());
359 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
360 session.SendResponse(*resHeader);
361 }
362
Enable(void)363 int MapMseServer::Enable(void)
364 {
365 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
366 LoadAdapterConfig();
367 ObexServerConfig obexSrvConfig = {
368 .useRfcomm_ = true,
369 .rfcommScn_ = rfcommNo_,
370 .rfcommMtu_ = static_cast<uint16_t>(rfcommMtu_),
371 #ifdef MAP_MSE_L2CAP_ENABLE
372 .useL2cap_ = true,
373 #else
374 .useL2cap_ = false,
375 #endif
376 .l2capPsm_ = goepL2capPsm_,
377 .l2capMtu_ = static_cast<uint16_t>(l2capMtu_),
378 .isSupportSrm_ = srmEnable_,
379 .isSupportReliableSession_ = false
380 };
381 obexServer_ = std::make_unique<ObexMpServer>(serviceName_, obexSrvConfig, *mseObexServer_, dispatcher_);
382 return !obexServer_->Startup() ? Result::SUCCEED : Result::FAIL;
383 }
384
Disable(void)385 void MapMseServer::Disable(void)
386 {
387 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
388
389 return obexServer_->Shutdown();
390 }
391
SetRemoteFeatureMask(uint32_t features)392 void MapMseServer::SetRemoteFeatureMask(uint32_t features)
393 {
394 if (supportedFeatureMask_ == MAP_SUPPORTED_FEATURES_DEFAULT) {
395 supportedFeatureMask_ = features;
396 } else {
397 supportedFeatureMask_ = supportedFeatureMask_ & features;
398 }
399 content_.SetRemoteFeatureMask(supportedFeatureMask_);
400 messageVersion_ = MAP_V10;
401 messageListingVersion_ = MAP_V10;
402 if ((supportedFeatureMask_ & MAP_FEATURE_MESSAGE_FORMAT_V11_BIT) > 0) {
403 messageVersion_ = MAP_V11;
404 }
405 if ((supportedFeatureMask_ & MAP_FEATURE_MESSAGE_LISTING_FORMAT_V11_BIT) > 0) {
406 messageListingVersion_ = MAP_V11;
407 }
408 }
409
CreateBMessage(ObexServerSession & session,const ObexHeader & req,const uint8_t charset,std::string & folderName)410 int MapMseServer::CreateBMessage(
411 ObexServerSession &session, const ObexHeader &req, const uint8_t charset, std::string &folderName)
412 {
413 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
414
415 if (currentFolder_ == nullptr || req.GetItemName() == nullptr) {
416 return RET_BAD_PARAM;
417 } else {
418 if (req.GetItemName()->GetUnicodeText().size() != 0) {
419 std::string subFolderName = ToUtf8String(req.GetItemName()->GetUnicodeText());
420 std::transform(subFolderName.begin(), subFolderName.end(), subFolderName.begin(), ::tolower);
421 folderName = currentFolder_->GetSubFolder(subFolderName)->GetName();
422 } else {
423 folderName = currentFolder_->GetName();
424 }
425 }
426 if (MAP_MSE_FOLDER_NAME_OUTBOX != folderName && MAP_MSE_FOLDER_NAME_DRAFT != folderName) {
427 return RET_BAD_STATUS;
428 }
429 auto body = req.GetExtendBodyObject();
430 uint8_t buf[READ_BODY_BUFF_SIZE];
431 size_t cnt = 0;
432 std::vector<uint8_t> results;
433 while ((cnt = body->Read(buf, READ_BODY_BUFF_SIZE)) > 0) {
434 results.insert(results.end(), buf, buf + cnt);
435 }
436 std::string data(results.begin(), results.end());
437 MSE_LOG_INFO("body data: %{public}s", data.c_str());
438 bMessage_ = std::make_unique<MapMseBmsg>();
439 bMessage_->SetVersion(messageVersion_);
440 if (bMessage_->Init(data, charset) != RET_NO_ERROR) {
441 return RET_BAD_STATUS;
442 }
443 if (MessageType::SMS_GSM == bMessage_->GetType() || MessageType::SMS_CDMA == bMessage_->GetType()) {
444 if (stub::PhoneType::SMS_GSM == stub::MapService::GetInstance()->GetSystemTelephonyService().phoneType) {
445 bMessage_->SetType(MessageType::SMS_GSM);
446 } else if (stub::PhoneType::SMS_CDMA ==
447 stub::MapService::GetInstance()->GetSystemTelephonyService().phoneType) {
448 bMessage_->SetType(MessageType::SMS_CDMA);
449 }
450 }
451 return RET_NO_ERROR;
452 }
453
PushMessageResponse(ObexServerSession & session,const ObexHeader & req)454 void MapMseServer::PushMessageResponse(ObexServerSession &session, const ObexHeader &req)
455 {
456 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
457
458 std::unique_ptr<MapMseParams> appParams = nullptr;
459 if (req.GetItemAppParams()) {
460 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
461 } else {
462 appParams = std::make_unique<MapMseParams>();
463 }
464 if (!appParams->charSet_) {
465 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
466 return;
467 }
468 std::string folderName = "";
469 auto ret = CreateBMessage(session, req, *appParams->charSet_, folderName);
470 if (ret == RET_BAD_PARAM) {
471 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
472 return;
473 } else if (ret == RET_BAD_STATUS) {
474 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::BAD_REQUEST));
475 return;
476 }
477 std::string handle;
478 if (appParams->messageHandle_ != "") {
479 if (((supportedFeatureMask_ & MAP_FEATURE_MESSAGE_FORWARDING_BIT) == 0) || !appParams->attachment_ ||
480 !appParams->modifyText_) {
481 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
482 return;
483 }
484 content_.SentOrForwardedMessage(*bMessage_, *appParams, appParams->messageHandle_);
485 handle = appParams->messageHandle_;
486 } else if (appParams->conversationId_ != "") {
487 handle = content_.SentConversationMessage(*bMessage_, *appParams, folderName);
488 } else {
489 handle = content_.SentNewMessage(*bMessage_, folderName, *appParams);
490 }
491 if (handle == "") {
492 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
493 return;
494 }
495 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
496 resHeader->AppendItemName(ToU16String(handle));
497 session.SendResponse(*resHeader);
498 }
499
GetMessageResponse(ObexServerSession & session,const ObexHeader & req)500 void MapMseServer::GetMessageResponse(ObexServerSession &session, const ObexHeader &req)
501 {
502 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
503
504 if (!req.GetItemName()) {
505 MSE_LOG_ERROR("Item Name of Obex header is error.");
506 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
507 return;
508 }
509 std::unique_ptr<MapMseParams> appParams = nullptr;
510 if (req.GetItemAppParams()) {
511 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
512 } else {
513 appParams = std::make_unique<MapMseParams>();
514 }
515 std::string name;
516 if (req.GetItemName()->GetUnicodeText().size() != 0) {
517 name = ToUtf8String(req.GetItemName()->GetUnicodeText());
518 } else {
519 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
520 return;
521 }
522 std::string body = content_.GetMessage(name, *appParams, messageVersion_);
523 auto bodyObj = std::make_shared<ObexArrayBodyObject>();
524 bodyObj->Write((uint8_t *)body.data(), body.size());
525 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
526 uint64_t handle = std::atol(name.c_str());
527 if ((MessageType::EMAIL == ParseHandle(handle) || MessageType::IM == ParseHandle(handle)) &&
528 !appParams->fractionRequest_) {
529 ObexTlvParamters obexAppPrarams;
530 uint8_t fractionDeliver = static_cast<uint8_t>(FractionDeliver::LAST);
531 TlvTriplet tlvItem(MapMseParams::PARAM_FRACTION_DELIVER, fractionDeliver);
532 obexAppPrarams.AppendTlvtriplet(tlvItem);
533 resHeader->AppendItemAppParams(obexAppPrarams);
534 }
535 session.SendGetResponse(req, *resHeader, bodyObj);
536 }
537
GetMessageListing(ObexServerSession & session,const ObexHeader & req)538 void MapMseServer::GetMessageListing(ObexServerSession &session, const ObexHeader &req)
539 {
540 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
541
542 std::unique_ptr<MapMseParams> appParams = nullptr;
543 MapMseFolder *folder = currentFolder_;
544 if (req.GetItemAppParams()) {
545 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
546 if (appParams->filterMessageHandle_.size() != 0 || appParams->conversationId_.size() != 0) {
547 folder = rootFolder_.get();
548 folder->SetIgnore(true);
549 } else {
550 std::string folderName;
551 if (req.GetItemName() && req.GetItemName()->GetUnicodeText().size() != 0) {
552 folderName = ToUtf8String(req.GetItemName()->GetUnicodeText());
553 MSE_LOG_INFO("folderName : %{public}s Enter", folderName.c_str());
554 std::transform(folderName.begin(), folderName.end(), folderName.begin(), ::tolower);
555 folder = currentFolder_->GetSubFolder(folderName);
556 } else {
557 folder = currentFolder_;
558 }
559 if (folder == nullptr) {
560 MSE_LOG_INFO("currentFolder_ is nullptr.");
561 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_FOUND));
562 return;
563 }
564 }
565 } else {
566 if (appParams == nullptr)
567 appParams = std::make_unique<MapMseParams>();
568 }
569 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
570 std::shared_ptr<ObexBodyObject> bodyObj = nullptr;
571 uint8_t unMessage = 0;
572 uint16_t listingSize = 0;
573 auto name = folder->IsIgnore() ? "" : folder->GetName();
574 if (appParams->maxListCount_ == 0x0) {
575 content_.GetListingSize(name, *appParams, listingSize, unMessage);
576 } else {
577 auto body = content_.GetBodyXml(name, *appParams, messageListingVersion_, listingSize, unMessage);
578 MSE_LOG_INFO("body : %{public}s ", body.c_str());
579 if (body != "") {
580 bodyObj = std::make_shared<ObexArrayBodyObject>();
581 bodyObj->Write((uint8_t *)body.data(), body.size());
582 }
583 }
584 ObexTlvParamters obexAppPrarams;
585 CreateOutputAppPrarams(obexAppPrarams, listingSize, unMessage);
586 resHeader->AppendItemAppParams(obexAppPrarams);
587 session.SendGetResponse(req, *resHeader, bodyObj);
588 }
589
CreateOutputAppPrarams(ObexTlvParamters & obexAppPrarams,const uint16_t & listSize,const uint8_t & unRead)590 void MapMseServer::CreateOutputAppPrarams(
591 ObexTlvParamters &obexAppPrarams, const uint16_t &listSize, const uint8_t &unRead)
592 {
593 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_LISTING_SIZE, listSize));
594 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_NEW_MESSAGE, unRead));
595 if ((supportedFeatureMask_ & MAP_FEATURE_DATABASE_INDENTIFIER_BIT) == MAP_FEATURE_DATABASE_INDENTIFIER_BIT) {
596 auto value = std::to_string(instance_.GetDatabaseIdentifier());
597 obexAppPrarams.AppendTlvtriplet(
598 TlvTriplet(MapMseParams::PARAM_DATABASE_IDENTIFIER, static_cast<uint8_t>(value.size() + 1),
599 reinterpret_cast<uint8_t *>(value.data())));
600 }
601 if ((supportedFeatureMask_ & MAP_FEATURE_FOLDER_VERSION_COUNTER_BIT) == MAP_FEATURE_FOLDER_VERSION_COUNTER_BIT) {
602 auto value = std::to_string(instance_.GetFolderVersionCounter());
603 obexAppPrarams.AppendTlvtriplet(TlvTriplet(
604 MapMseParams::PARAM_FOLDER_VERSION_COUNTER, static_cast<uint8_t>(value.size() + 1),
605 reinterpret_cast<uint8_t *>(value.data())));
606 }
607 std::string mseTime = GetMseTime();
608 obexAppPrarams.AppendTlvtriplet(
609 TlvTriplet(MapMseParams::PARAM_MSE_TIME, mseTime.size(),
610 reinterpret_cast<uint8_t *>(const_cast<char *>(mseTime.c_str()))));
611 }
612
GetConversationListing(ObexServerSession & session,const ObexHeader & req)613 void MapMseServer::GetConversationListing(ObexServerSession &session, const ObexHeader &req)
614 {
615 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
616
617 std::unique_ptr<MapMseParams> appParams = nullptr;
618 if (req.GetItemAppParams()) {
619 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
620 } else {
621 appParams = std::make_unique<MapMseParams>();
622 }
623 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
624 ObexTlvParamters obexAppPrarams;
625 uint16_t listingSize = 0;
626 std::shared_ptr<ObexBodyObject> bodyObj = nullptr;
627 if (appParams->maxListCount_ == 0x0) {
628 listingSize = content_.GetConvoLstCount(*appParams);
629 if ((supportedFeatureMask_ & MAP_FEATURE_CONVERSATION_VERSION_COUNTER_BIT) > 0) {
630 auto value = std::to_string(instance_.GetConversationVersionCounter());
631 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_CONVERSATION_LISTING_VERSION_COUNTER,
632 static_cast<uint8_t>(value.size() + 1),
633 reinterpret_cast<uint8_t *>(value.data())));
634 }
635 } else {
636 auto bodyData = content_.GetCvslistBodyXml(*appParams, listingSize);
637 MSE_LOG_INFO("Body : %{public}s ", bodyData.c_str());
638 bodyObj = std::make_shared<ObexArrayBodyObject>();
639 bodyObj->Write((uint8_t *)bodyData.data(), bodyData.size());
640 }
641 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_LISTING_SIZE, listingSize));
642 if ((supportedFeatureMask_ & MAP_FEATURE_DATABASE_INDENTIFIER_BIT) > 0) {
643 auto value = std::to_string(instance_.GetDatabaseIdentifier());
644 obexAppPrarams.AppendTlvtriplet(
645 TlvTriplet(MapMseParams::PARAM_DATABASE_IDENTIFIER, static_cast<uint8_t>(value.size() + 1),
646 reinterpret_cast<uint8_t *>(value.data())));
647 }
648 std::string mseTime = GetMseTime();
649 obexAppPrarams.AppendTlvtriplet(
650 TlvTriplet(MapMseParams::PARAM_MSE_TIME, static_cast<uint8_t>(mseTime.size()),
651 reinterpret_cast<uint8_t *>(const_cast<char *>(mseTime.c_str()))));
652 resHeader->AppendItemAppParams(obexAppPrarams);
653 session.SendGetResponse(req, *resHeader, bodyObj);
654 }
655
GetFolderListing(ObexServerSession & session,const ObexHeader & req)656 void MapMseServer::GetFolderListing(ObexServerSession &session, const ObexHeader &req)
657 {
658 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
659
660 std::unique_ptr<MapMseParams> appParams = nullptr;
661 if (req.GetItemAppParams()) {
662 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
663 } else {
664 appParams = std::make_unique<MapMseParams>();
665 }
666 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
667 ObexTlvParamters obexAppPrarams;
668 std::shared_ptr<ObexBodyObject> bodyObj = nullptr;
669 if (appParams->maxListCount_ == 0x00) {
670 uint16_t listingSize = currentFolder_->GetSubFolderSize();
671 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_LISTING_SIZE, listingSize));
672 // no body to seting
673 } else {
674 auto folderData = currentFolder_->GetData(appParams->listStartOffSet_, appParams->maxListCount_);
675 if (folderData.size() == 0) {
676 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
677 return;
678 }
679 bodyObj = std::make_shared<ObexArrayBodyObject>();
680 bodyObj->Write((uint8_t *)folderData.data(), folderData.size());
681 MSE_LOG_INFO("Body : %{public}s ", folderData.c_str());
682 }
683 resHeader->AppendItemAppParams(obexAppPrarams);
684 session.SendGetResponse(req, *resHeader, bodyObj);
685 }
686
GetOwnerStatus(const ObexServerSession & session,const ObexHeader & req)687 void MapMseServer::GetOwnerStatus(const ObexServerSession &session, const ObexHeader &req)
688 {
689 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
690
691 std::unique_ptr<MapMseParams> appParams = nullptr;
692 if (req.GetItemAppParams()) {
693 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
694 } else {
695 appParams = std::make_unique<MapMseParams>();
696 }
697 if (accountItem_.msgType_ == MessageType::IM) {
698 if (!CheckConversationId(appParams->conversationId_)) {
699 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_FOUND));
700 return;
701 }
702 auto result = stub::MapService::GetInstance()->GetOwnerStatus(appParams->conversationId_);
703 ObexTlvParamters obexAppPrarams;
704 uint8_t presenceAvlt = result.presenceAvailability;
705 std::string presenceText = result.presenceText;
706 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_PRESENCE_AVAILABILITY, presenceAvlt));
707 obexAppPrarams.AppendTlvtriplet(
708 TlvTriplet(MapMseParams::PARAM_PRESENCE_TEXT, presenceText.size(),
709 reinterpret_cast<uint8_t *>(const_cast<char *>(presenceText.c_str()))));
710 if (appParams->conversationId_.size() > 0) {
711 std::string lastActivity = result.lastActivity;
712 uint8_t chatState = result.chatState;
713 obexAppPrarams.AppendTlvtriplet(TlvTriplet(
714 MapMseParams::PARAM_LAST_ACTIVITY, static_cast<uint8_t>(lastActivity.size()),
715 reinterpret_cast<uint8_t *>(const_cast<char *>(lastActivity.c_str()))));
716 obexAppPrarams.AppendTlvtriplet(TlvTriplet(MapMseParams::PARAM_CHAT_STATE, chatState));
717 }
718 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
719 resHeader->AppendItemAppParams(obexAppPrarams);
720 session.SendResponse(*resHeader);
721 return;
722 }
723 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
724 }
725
GetMASInstanceInformation(ObexServerSession & session,const ObexHeader & req)726 void MapMseServer::GetMASInstanceInformation(ObexServerSession &session, const ObexHeader &req)
727 {
728 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
729
730 std::unique_ptr<MapMseParams> appParams = nullptr;
731 if (req.GetItemAppParams()) {
732 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
733 } else {
734 appParams = std::make_unique<MapMseParams>();
735 }
736 if (instance_.GetMasId() == *appParams->masInstanceId_) {
737 std::string masStr;
738 std::string ownerUci;
739 GetImEmailMasInfo(masStr, ownerUci);
740 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
741 if (masStr.size() > MAP_MAS_INSTANCE_INFORMATION_LENGTH) {
742 masStr = masStr.substr(0, MAP_MAS_INSTANCE_INFORMATION_LENGTH);
743 }
744 auto bodyObj = std::make_shared<ObexArrayBodyObject>();
745 bodyObj->Write((uint8_t *)masStr.data(), masStr.size());
746 ObexTlvParamters obexAppPrarams;
747 obexAppPrarams.AppendTlvtriplet(
748 TlvTriplet(MapMseParams::PARAM_OWNER_UCI, static_cast<uint8_t>(ownerUci.size()),
749 reinterpret_cast<uint8_t *>(ownerUci.data())));
750 resHeader->AppendItemAppParams(obexAppPrarams);
751 session.SendGetResponse(req, *resHeader, bodyObj);
752 return;
753 } else {
754 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::BAD_REQUEST));
755 return;
756 }
757 }
758
GetImEmailMasInfo(std::string & masStr,std::string & ownerUci)759 void MapMseServer::GetImEmailMasInfo(std::string &masStr, std::string &ownerUci)
760 {
761 if (accountItem_.id_ != "") {
762 if (MessageType::EMAIL == accountItem_.msgType_) {
763 if (accountItem_.name_ != "") {
764 masStr = accountItem_.name_;
765 } else {
766 masStr = EMAIL_NAME;
767 }
768 } else if (MessageType::IM == accountItem_.msgType_) {
769 masStr = accountItem_.GetUciFullName();
770 if (masStr == "") {
771 std::stringstream ss;
772 ss << std::setw(0x3) << static_cast<int>(instance_.GetMasId());
773 masStr = "un" + ss.str() + accountItem_.uci_;
774 }
775 }
776 ownerUci = accountItem_.GetUciFullName();
777 } else {
778 masStr.append(SMS_MMS_NAME + "_TEL_");
779 masStr.append(stub::MapService::GetInstance()->GetSystemTelephonyService().phoneNumber);
780 ownerUci = stub::MapService::GetInstance()->GetSystemTelephonyService().phoneNumber;
781 }
782 }
783
SetNotificationRegistration(const ObexServerSession & session,const ObexHeader & req)784 void MapMseServer::SetNotificationRegistration(const ObexServerSession &session, const ObexHeader &req)
785 {
786 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
787
788 std::unique_ptr<MapMseParams> appParams = nullptr;
789 if (req.GetItemAppParams()) {
790 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
791 } else {
792 appParams = std::make_unique<MapMseParams>();
793 }
794 if (!appParams->notificationStatus_) {
795 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
796 return;
797 }
798 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SUCCESS));
799 content_.SetNotificationRegistration(session.GetRemoteAddr(), *appParams->notificationStatus_);
800 }
801
SetNotificationFilter(const ObexServerSession & session,const ObexHeader & req)802 void MapMseServer::SetNotificationFilter(const ObexServerSession &session, const ObexHeader &req)
803 {
804 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
805
806 std::unique_ptr<MapMseParams> appParams = nullptr;
807 if (req.GetItemAppParams()) {
808 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
809 } else {
810 appParams = std::make_unique<MapMseParams>();
811 }
812 if (appParams->notificationFilterMask_ == -1) {
813 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
814 return;
815 }
816 auto const &remoteAddr = session.GetRemoteAddr();
817 content_.SetNotificationFilter(remoteAddr, appParams->notificationFilterMask_);
818 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
819 session.SendResponse(*resHeader);
820 }
821
SetMessageStatus(const ObexServerSession & session,const ObexHeader & req)822 void MapMseServer::SetMessageStatus(const ObexServerSession &session, const ObexHeader &req)
823 {
824 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
825
826 std::string name;
827 if (req.GetItemName()->GetUnicodeText().size() != 0) {
828 name = ToUtf8String(req.GetItemName()->GetUnicodeText());
829 } else {
830 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_ACCEPTABLE));
831 return;
832 }
833 std::unique_ptr<MapMseParams> appParams = nullptr;
834 if (req.GetItemAppParams()) {
835 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
836 } else {
837 appParams = std::make_unique<MapMseParams>();
838 }
839 if (appParams->extendedData_ != "") {
840 if (!content_.SetExtendedData(name, appParams->extendedData_)) {
841 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
842 return;
843 }
844 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SUCCESS));
845 return;
846 }
847 if (!appParams->statusIndicator_ || !appParams->statusValue_) {
848 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
849 return;
850 }
851 if (static_cast<uint8_t>(StatusIndicator::SET_EXTENDED_DATA) == *appParams->statusIndicator_ &&
852 appParams->extendedData_.size() == 0) {
853 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
854 return;
855 }
856 MessageType messageType = ParseHandle(std::atol(name.c_str()));
857 if (static_cast<uint8_t>(StatusIndicator::READ_STATUS) == *appParams->statusIndicator_) {
858 if (!content_.SetMessageStatusRead(
859 name, messageType, currentFolder_->GetFullPath(), *appParams->statusValue_)) {
860 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
861 return;
862 }
863 } else if (static_cast<uint8_t>(StatusIndicator::DELETED_STATUS) == *appParams->statusIndicator_) {
864 if (!content_.SetMessageStatusDeleted(
865 name, messageType, currentFolder_->GetFullPath(), *appParams->statusValue_)) {
866 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
867 return;
868 }
869 }
870 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SUCCESS));
871 }
872
UpdateInbox(const ObexServerSession & session,const ObexHeader & req)873 void MapMseServer::UpdateInbox(const ObexServerSession &session, const ObexHeader &req)
874 {
875 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
876
877 if (accountItem_.id_.size() != 0) {
878 std::string name = MAP_MSE_FOLDER_NAME_INBOX;
879 if (req.GetItemName() != nullptr) {
880 if (req.GetItemName()->GetUnicodeText().size() != 0) {
881 name = ToUtf8String(req.GetItemName()->GetUnicodeText());
882 }
883 }
884 MapMseFolder *inboxFolder = currentFolder_->GetFolderByName(name);
885 if (inboxFolder == nullptr) {
886 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_IMPLEMENTED));
887 return;
888 }
889 if (accountItem_.pAuthority_.size() != 0) {
890 if (stub::MapService::GetInstance()->UpdateFolder(
891 accountItem_.id_, std::to_string(inboxFolder->GetFolderId()), instance_.GetMasId())) {
892 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SUCCESS));
893 return;
894 }
895 }
896 }
897 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_IMPLEMENTED));
898 }
899
SetStatusParam(const MapMseParams & appParams,stub::OwnerStatusParam & statusParam)900 int MapMseServer::SetStatusParam(const MapMseParams &appParams, stub::OwnerStatusParam &statusParam)
901 {
902 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
903
904 if (appParams.conversationId_.size() == 0 && appParams.presenceAvailability_ == nullptr &&
905 appParams.presenceText_.size() == 0 && appParams.lastActivity_.size() == 0 && appParams.chatState_ == nullptr) {
906 return RET_BAD_STATUS;
907 } else {
908 if (appParams.conversationId_ != "") {
909 if (!CheckConversationId(appParams.conversationId_)) {
910 return RET_NO_SUPPORT;
911 }
912 statusParam.conversationId = appParams.conversationId_;
913 }
914 if (appParams.presenceAvailability_) {
915 statusParam.presenceAvailability = *appParams.presenceAvailability_;
916 }
917 if (appParams.lastActivity_ != "") {
918 statusParam.lastActivity = appParams.lastActivity_;
919 }
920 if (appParams.chatState_) {
921 statusParam.chatState = *appParams.chatState_;
922 }
923 }
924 return RET_NO_ERROR;
925 }
926
SetOwnerStatus(const ObexServerSession & session,const ObexHeader & req)927 void MapMseServer::SetOwnerStatus(const ObexServerSession &session, const ObexHeader &req)
928 {
929 MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
930
931 if (accountItem_.msgType_ == MessageType::IM) {
932 std::unique_ptr<MapMseParams> appParams = nullptr;
933 if (req.GetItemAppParams()) {
934 appParams = std::make_unique<MapMseParams>(*req.GetItemAppParams()->GetTlvParamters());
935 } else {
936 appParams = std::make_unique<MapMseParams>();
937 }
938 stub::OwnerStatusParam statusParam {};
939 auto ret = SetStatusParam(*appParams, statusParam);
940 if (ret == RET_BAD_STATUS) {
941 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::PRECONDITION_FAILED));
942 return;
943 } else if (ret == RET_NO_SUPPORT) {
944 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_FOUND));
945 return;
946 }
947
948 if (stub::MapService::GetInstance()->SetOwnerStatus(statusParam, instance_.GetMasId())) {
949 auto resHeader = ObexHeader::CreateResponse(ObexRspCode::SUCCESS);
950 session.SendResponse(*resHeader);
951 return;
952 } else {
953 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::NOT_IMPLEMENTED));
954 return;
955 }
956 }
957 session.SendResponse(*ObexHeader::CreateResponse(ObexRspCode::SERVICE_UNAVAILABLE));
958 }
959
ToUtf8String(std::u16string u16str)960 std::string MapMseServer::ToUtf8String(std::u16string u16str)
961 {
962 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(u16str);
963 }
964
ToU16String(std::string str)965 std::u16string MapMseServer::ToU16String(std::string str)
966 {
967 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(str);
968 }
969
GetMseTime(void)970 std::string MapMseServer::GetMseTime(void)
971 {
972 time_t currTime;
973 time(&currTime);
974 struct tm utctm;
975 gmtime_r(&currTime, &utctm);
976 char buff[16];
977 strftime(buff, sizeof(buff), "%Y%m%{public}dT%H%M%{public}s", &utctm);
978 return std::string(buff);
979 }
980
ParseHandle(uint64_t handle)981 MessageType MapMseServer::ParseHandle(uint64_t handle)
982 {
983 if ((handle & MESSAGE_HANDLE_MMS_MASK) != 0) {
984 return MessageType::MMS;
985 }
986 if ((handle & MESSAGE_HANDLE_EMAIL_MASK) != 0) {
987 return MessageType::EMAIL;
988 }
989 if ((handle & MESSAGE_HANDLE_SMS_GSM_MASK) != 0) {
990 return MessageType::SMS_GSM;
991 }
992 if ((handle & MESSAGE_HANDLE_SMS_CDMA_MASK) != 0) {
993 return MessageType::SMS_CDMA;
994 }
995 if ((handle & MESSAGE_HANDLE_IM_MASK) != 0) {
996 return MessageType::IM;
997 }
998 return MessageType::NONE;
999 }
1000
CheckConversationId(std::string id)1001 bool MapMseServer::CheckConversationId(std::string id)
1002 {
1003 auto dataProvider = DataAccess::GetConnection(DEFAULT_MAP_MSE_DB_FILE);
1004 std::string sql = "select id from Conversation where convo_id = ? ";
1005 auto stmt = dataProvider->CreateStatement(sql);
1006 stmt->SetParamString(1, id);
1007 auto dataResult = stmt->Query();
1008 return dataResult->Next();
1009 }
1010
LoadAdapterConfig()1011 void MapMseServer::LoadAdapterConfig()
1012 {
1013 IAdapterConfig *adpterConfig = AdapterConfig::GetInstance();
1014 if (!adpterConfig->GetValue(SECTION_MAP_MSE_SERVICE, PROPERTY_SRM_ENABLE, srmEnable_)) {
1015 MSE_LOG_INFO("Load config %{public}s failure", PROPERTY_SRM_ENABLE.c_str());
1016 srmEnable_ = true;
1017 }
1018 if (!adpterConfig->GetValue(SECTION_MAP_MSE_SERVICE, PROPERTY_RFCOMM_MTU, rfcommMtu_)) {
1019 MSE_LOG_INFO("Load config %{public}s failure", PROPERTY_RFCOMM_MTU.c_str());
1020 rfcommMtu_ = OBEX_DEFAULT_MTU;
1021 }
1022 if (!adpterConfig->GetValue(SECTION_MAP_MSE_SERVICE, PROPERTY_L2CAP_MTU, l2capMtu_)) {
1023 MSE_LOG_INFO("Load config %{public}s failure", PROPERTY_L2CAP_MTU.c_str());
1024 l2capMtu_ = OBEX_DEFAULT_MTU;
1025 }
1026 }
1027 } // namespace bluetooth
1028 } // namespace OHOS