1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAP_MSE_RESOURCE_H 17 #define MAP_MSE_RESOURCE_H 18 19 #include <cstdint> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 #include "data_access.h" 24 #include "dispatcher.h" 25 #include "log.h" 26 #include "map_mse_bmsg.h" 27 #include "map_mse_event.h" 28 #include "map_mse_folder.h" 29 #include "map_mse_instance.h" 30 #include "map_mse_params.h" 31 #include "stub/map_service.h" 32 33 namespace OHOS { 34 namespace bluetooth { 35 struct MseMessageElement { 36 // Messages-Listing Format Version 1.0 37 std::string handle_ = ""; 38 std::string subject_ = ""; 39 std::string dateTime_ = ""; 40 std::string senderName_ = ""; 41 std::string senderAddressing_ = ""; 42 std::string replytoAddressing_ = ""; 43 std::string recipientName_ = ""; 44 std::string recipientAddressing_ = ""; 45 MessageType type = MessageType::NONE; 46 int size_ = -1; 47 std::string text_ = ""; 48 std::string receptionStatus_ = ""; 49 int attachmentSize_ = -1; 50 std::string priority_ = ""; 51 bool read_ = false; 52 std::string sent_ = ""; 53 std::string protected_ = ""; 54 // Messages-Listing Format Version 1.1 55 std::string deliveryStatus_ = ""; 56 std::string conversationId_ = ""; 57 std::string conversationName_ = ""; 58 std::string direction_ = ""; 59 std::string attachmentMimeTypes_ = ""; 60 bool msgTypeAppParamSet_ = false; 61 }; 62 struct MseParticipant { 63 std::string uci_ = ""; 64 std::string name_ = ""; 65 std::string displayName_ = ""; 66 int presenceAvailability_ = -1; 67 std::string presenceText_ = ""; 68 int priority_ = -1; 69 std::string lastActy_ = ""; 70 int chatState_ = -1; 71 std::string btUid_ = ""; 72 }; 73 74 struct MseConvoLstElement { 75 std::string convoId_ = ""; 76 std::string name_ = ""; 77 std::string lastActivity_ = ""; 78 int readStatus_ = -1; 79 std::string versionCounter_ = ""; 80 std::string summary_ = ""; 81 std::vector<MseParticipant> contacts_ {}; 82 }; 83 class MapMseResource { 84 public: 85 explicit MapMseResource(MapMseMnscli &mnsClient, utility::Dispatcher &dispatcher, MapMseInstance &instance); 86 virtual ~MapMseResource(); 87 std::string GetBodyXml(const std::string &folderName, const MapMseParams &appParameter, 88 const std::string &version, uint16_t &listSize, uint8_t &unRead); 89 void GetListingSize( 90 const std::string &folderName, const MapMseParams &appParameter, uint16_t &listSize, uint8_t &unRead); 91 std::string GetMessage(const std::string handle, const MapMseParams &appParameter, const std::string &version); 92 std::string GetCvslistBodyXml(const MapMseParams &appParameter, uint16_t &size); 93 int GetConvoLstCount(const MapMseParams &appParameter); 94 void SetNotificationRegistration(const RawAddress &remoteAddr, const int status); 95 void SetNotificationFilter(const RawAddress &remoteAddr, uint32_t filter); 96 void SetRemoteFeatureMask(uint32_t features); 97 static bool SetMessageStatusDeleted( 98 std::string handle, MessageType type, std::string path, uint8_t status); 99 static bool SetMessageStatusRead(std::string handle, MessageType type, std::string path, uint8_t status); 100 static bool SetExtendedData(std::string handle, std::string data); 101 void SentOrForwardedMessage(MapMseBmsg &bmessage, MapMseParams &appParameter, const std::string &handle); 102 std::string SentConversationMessage( 103 MapMseBmsg &bmessage, MapMseParams &appParameter, const std::string &folderName); 104 std::string SentNewMessage(MapMseBmsg &bmessage, std::string &folderName, MapMseParams &appParameter); 105 106 private: 107 static const std::string XML_TAG_CONVO_LISTING; 108 static const std::string XML_ATT_ENCODING; 109 static const std::string XML_ATT_VERSION; 110 static const std::string XML_TAG_CONVERSATION; 111 static const std::string XML_ATT_LAST_ACTIVITY; 112 static const std::string XML_ATT_NAME; 113 static const std::string XML_ATT_ID; 114 static const std::string XML_ATT_READ; 115 static const std::string XML_ATT_VERSION_COUNTER; 116 static const std::string XML_ATT_SUMMARY; 117 static const std::string XML_TAG_PARTICIPANT; 118 static const std::string XML_ATT_UCI; 119 static const std::string XML_ATT_DISPLAY_NAME; 120 static const std::string XML_ATT_CHAT_STATE; 121 static const std::string XML_ATT_PRIORITY; 122 static const std::string XML_ATT_PRESENCE_TEXT; 123 static const std::string XML_ATT_PRESENCE_AVAILABILITY; 124 static const std::string XML_ATT_X_BT_UID; 125 static const uint32_t SUBJECT_MASK = 1; 126 static const uint32_t DATETIME_MASK = 1 << 1; 127 static const uint32_t SENDER_NAME_MASK = 1 << 2; 128 static const uint32_t SENDER_ADDRESSING_MASK = 1 << 3; 129 static const uint32_t RECIPIENT_NAME_MASK = 1 << 4; 130 static const uint32_t RECIPIENT_ADDRESSING_MASK = 1 << 5; 131 static const uint32_t TYPE_MASK = 1 << 6; 132 static const uint32_t SIZE_MASK = 1 << 7; 133 static const uint32_t RECEPTION_STATUS_MASK = 1 << 8; 134 static const uint32_t TEXT_MASK = 1 << 9; 135 static const uint32_t ATTACHMENT_SIZE_MASK = 1 << 10; 136 static const uint32_t PRIORITY_MASK = 1 << 11; 137 static const uint32_t READ_MASK = 1 << 12; 138 static const uint32_t SENT_MASK = 1 << 13; 139 static const uint32_t PROTECTED_MASK = 1 << 14; 140 static const uint32_t REPLYTO_ADDRESSING = 1 << 15; 141 static const uint32_t DELIVERY_STATUS_MASK = 1 << 16; 142 static const uint32_t CONVERSATION_ID_MASK = 1 << 17; 143 static const uint32_t CONVERSATION_NAME_MASK = 1 << 18; 144 static const uint32_t DIRECTION_MASK = 1 << 19; 145 static const uint32_t ATTACHMENT_NIME_MASK = 1 << 20; 146 static const uint8_t MASK_MSG_SMS_GSM = 0x01; 147 static const uint8_t MASK_MSG_SMS_CDMA = 0x02; 148 static const uint8_t MASK_MSG_EMAIL = 0x04; 149 static const uint8_t MASK_MSG_MMS = 0x08; 150 static const uint8_t MASK_MSG_IM = 0x10; 151 static const uint32_t CONVO_LISTING_NAME_MASK = 1; 152 static const uint32_t CONVO_LAST_ACTIVITY_MASK = 1 << 1; 153 static const uint32_t CONVO_READ_STATUS_MASK = 1 << 2; 154 static const uint32_t CONVO_VERSION_COUNTER_MASK = 1 << 3; 155 static const uint32_t CONVO_SUMMARY_MASK = 1 << 4; 156 static const uint32_t PARTI_MASK = 1 << 5; 157 static const uint32_t PARTI_UCI_MASK = 1 << 6; 158 static const uint32_t PARTI_DISPLAY_NAME_MASK = 1 << 7; 159 static const uint32_t PARTI_CHAT_STATE_MASK = 1 << 8; 160 static const uint32_t PARTI_LAST_ACTIVITY_MASK = 1 << 9; 161 static const uint32_t PARTI_X_BT_UID_MASK = 1 << 10; 162 static const uint32_t PARTI_NAME_MASK = 1 << 11; 163 static const uint32_t PARTI_PRESENCE_AVAIL_MASK = 1 << 12; 164 static const uint32_t PARTI_PRESENCE_TEXT_MASK = 1 << 13; 165 static const uint32_t PARTI_PRIORITY_MASK = 1 << 14; 166 static const std::string TYPE_NEW; 167 static const std::string TYPE_DELETE; 168 static const std::string TYPE_SHIFT; 169 static const std::string TYPE_SENDING_SUCCESS; 170 static const std::string TYPE_SENDING_FAILURE; 171 static const std::string TYPE_DELIVERY_SUCCESS; 172 static const std::string TYPE_DELIVERY_FAILURE; 173 static const std::string TYPE_MEMORY_FULL; 174 static const std::string TYPE_MEMORY_AVAILABLE; 175 static const std::string TYPE_READ_STATUS; 176 static const std::string TYPE_CONVERSATION; 177 static const std::string TYPE_PRESENCE; 178 static const std::string TYPE_CHAT_STATE; 179 static const std::string TYPE_EXTENDED_DATA; 180 static const std::string TYPE_REMOVED; 181 static const uint32_t FILTER_MASK_NEW_MESSAGE = 1; 182 static const uint32_t FILTER_MASK_MESSAGE_DELETED = 1 << 1; 183 static const uint32_t FILTER_MASK_MESSAGE_SHIFT = 1 << 2; 184 static const uint32_t FILTER_MASK_SENDING_SUCCESS = 1 << 3; 185 static const uint32_t FILTER_MASK_SENDING_FAILURE = 1 << 4; 186 static const uint32_t FILTER_MASK_DELIVERY_SUCCESS = 1 << 5; 187 static const uint32_t FILTER_MASK_DELIVERY_FAILURE = 1 << 6; 188 static const uint32_t FILTER_MASK_MEMORY_FULL = 1 << 7; 189 static const uint32_t FILTER_MASK_MEMORY_AVAILABLE = 1 << 8; 190 static const uint32_t FILTER_MASK_READ_STATUS_CHANGED = 1 << 9; 191 static const uint32_t FILTER_MASK_CONVERSATION_CHANGED = 1 << 10; 192 static const uint32_t FILTER_MASK_PARTICIPANT_PRESENCE_CHANGED = 1 << 11; 193 static const uint32_t FILTER_MASK_PARTICIPANT_CHATSTATE_CHANGED = 1 << 12; 194 static const uint32_t FILTER_MASK_EXTENDED_DATA_CHANGED = 1 << 13; 195 static const uint32_t FILTER_MASK_MESSAGE_REMOVED = 1 << 14; 196 std::string GetMsglistSql(const MapMseParams &appParameter, const std::string &folderName); 197 static void AddWhereSql(const MapMseParams &appParameter, std::string &sql); 198 static void SetContacts(IDataStatement &ids, const MapMseParams &appParameter, int &index); 199 static std::vector<int> GetMsgTypeParam(uint8_t mask); 200 void SetMsglistParam(IDataStatement &ids, const MapMseParams &appParameter, const std::string &folderName); 201 std::vector<std::unique_ptr<MseMessageElement>> GetMessageListContent( 202 const std::string &folderName, const MapMseParams &appParameter); 203 static std::vector<MapMseVcard> GetVCardData( 204 std::unique_ptr<DataAccess> &dataAccess, const std::string handle, const int isOriginator = 0); 205 static std::string GetCvslistSql(const MapMseParams &appParameter); 206 static void SetCvslistParam(IDataStatement &ids, const MapMseParams &appParameter); 207 static std::string GetParticipantSql(const MapMseParams &appParameter); 208 static void SetParticipantParam(IDataStatement &ids, const MapMseParams &appParameter, 209 const std::string &convoId); 210 std::vector<MseParticipant> GetParticipantContent( 211 IDataStatement &ids, const MapMseParams &appParameter, const std::string &convoId); 212 std::vector<std::unique_ptr<MseConvoLstElement>> GetConvoLstContent(const MapMseParams &appParameter); 213 void AddVer10Node(xmlNodePtr &node, MseMessageElement &msg, uint8_t &unRead, long paraMask); 214 static std::string ChangeType(MessageType type); 215 std::string GetPrivateBodyXml(std::vector<std::unique_ptr<MseMessageElement>> &messageList, 216 const std::string &version, uint8_t &unRead, long paraMask); 217 static bool CheckParameterMask(uint32_t mask, std::string para, long paraMask, bool required = false); 218 void ParticipantsToXml(xmlNodePtr &node, std::vector<MseParticipant> &contacts, long paraMask); 219 void SendEventReport(const std::string &addr, const std::string &event, const std::string &type); 220 bool CheckEventFilterMask(const std::string &addr, const uint32_t bit); 221 MapMseMnscli *mnsClient_ = nullptr; 222 utility::Dispatcher &dispatcher_; 223 MapMseInstance &instance_; 224 uint8_t masId_ = 0; 225 std::atomic_uint8_t reportVersion_ = 0x0; 226 std::unordered_map<std::string, uint32_t> eventFilterMaskMap_ {}; 227 static std::string GetDateTime(); 228 std::string UpdateData(MapMseBmsg &bmessage, MapMseParams &appParameter, const std::string &handle); 229 static int UpdateMessage( 230 std::unique_ptr<DataAccess> &dataAccess, const MapMseBmsg &bmessage, const std::string &handle); 231 int UpdateMessageList(std::unique_ptr<DataAccess> &dataAccess, const std::string &handle); 232 std::string DataInsert(MapMseBmsg &bmessage, MapMseParams &appParameter, const std::string &folderName); 233 int InsertMessageList(std::unique_ptr<DataAccess> &dataAccess, MapMseParams &appParameter, 234 const MapMseBmsg &bmessage, const std::string &handle, const std::string &folderName); 235 static void GetNameAddress(MessageType type, std::vector<MapMseVcard> vcard, 236 std::string &name, std::string &sender); 237 int InsertMessage(std::unique_ptr<DataAccess> &dataAccess, MapMseParams &appParameter, 238 const MapMseBmsg &bmessage, const std::string &handle); 239 uint64_t GetMessageHandle(MessageType type); 240 static std::string GetMessageNameByType(MessageType type); 241 void NotifySendMessage(const MapMseBmsg &bmessage, const MapMseParams &appParameter); 242 std::string InsertConvData(MapMseBmsg &bmessage, MapMseParams &appParameter, const std::string &folderName); 243 static int UpdateConvMessage( 244 std::unique_ptr<DataAccess> &dataAccess, const std::string &conversationId, const std::string &handle); 245 static int UpdateConvMessageList( 246 std::unique_ptr<DataAccess> &dataAccess, const std::string &conversationId, const std::string &handle); 247 static int InsertVCard( 248 std::unique_ptr<DataAccess> &dataAccess, const MapMseVcard &vcard, const int isOriginator, 249 const std::string &handle); 250 uint32_t remoteFeatures_ = 0x0; 251 void SendEvent(const std::string &addr, const stub::EventInfo &event); 252 void SendEventNative(const std::string &addr, const stub::EventInfo &event); 253 uint32_t GetRemoteFeatureMask(const std::string &addr); 254 void SendNewMessageEvent(const std::string &addr, const stub::EventInfo &event); 255 void SendMessageShiftEvent(const std::string &addr, const stub::EventInfo &event); 256 void SendMessageRemovedEvent(const std::string &addr, const stub::EventInfo &event); 257 void SendingFailureEvent(const std::string &addr, const stub::EventInfo &event); 258 void SendReadStatusChangedEvent(const std::string &addr, const stub::EventInfo &event); 259 void SendConversationChangedEvent(const std::string &addr, const stub::EventInfo &event); 260 void SendParticipantPresenceChangedEvent(const std::string &addr, const stub::EventInfo &event); 261 void SendParticipantChatStateChangedEvent(const std::string &addr, const stub::EventInfo &event); 262 void SendMessageExtendedDataChangedEvent(const std::string &addr, const stub::EventInfo &event); 263 void SendMemoryFullEvent(const std::string &addr, const stub::EventInfo &event); 264 void SendMemoryAvailableEvent(const std::string &addr, const stub::EventInfo &event); 265 void SendMessageEvent(const std::string type, const std::string &addr, const stub::EventInfo &event); 266 void UpdateDbIdCounter(void); 267 void UpdateVerIdCounter(void); 268 void UpdateConvoVerIdCounter(void); 269 class EventObserver : public stub::MapServiceObserver { 270 public: 271 explicit EventObserver(MapMseResource &observer); 272 virtual ~EventObserver() = default; 273 274 private: 275 void OnNotifyingEvent(const std::string &addr, stub::EventInfo &event) override; 276 void OnUpdateDbIdCounter(void) override; 277 void OnUpdateVerIdCounter(void) override; 278 void OnUpdateConvoVerIdCounter(void) override; 279 MapMseResource &observer_; 280 }; 281 std::unordered_map<int, std::unique_ptr<EventObserver>> observerMap_ {}; 282 BT_DISALLOW_COPY_AND_ASSIGN(MapMseResource); 283 }; 284 } // namespace bluetooth 285 } // namespace OHOS 286 287 #endif // MAP_MSE_RESOURCE_H 288