Yingjie Wang | 355dcda | 2020-01-17 10:10:18 +0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved. |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of The Linux Foundation, nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | */ |
| 29 | #define LOG_TAG "LocSvc_SystemStatusOsObserver" |
| 30 | |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 31 | #include <algorithm> |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 32 | #include <SystemStatus.h> |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 33 | #include <SystemStatusOsObserver.h> |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 34 | #include <IDataItemCore.h> |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 35 | #include <DataItemsFactoryProxy.h> |
| 36 | |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 37 | namespace loc_core |
| 38 | { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 39 | template <typename CINT, typename COUT> |
| 40 | COUT SystemStatusOsObserver::containerTransfer(CINT& inContainer) { |
| 41 | COUT outContainer(0); |
| 42 | for (auto item : inContainer) { |
| 43 | outContainer.insert(outContainer.begin(), item); |
| 44 | } |
| 45 | return outContainer; |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 46 | } |
| 47 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 48 | SystemStatusOsObserver::~SystemStatusOsObserver() { |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 49 | // Close data-item library handle |
| 50 | DataItemsFactoryProxy::closeDataItemLibraryHandle(); |
| 51 | |
| 52 | // Destroy cache |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 53 | for (auto each : mDataItemCache) { |
| 54 | if (nullptr != each.second) { |
| 55 | delete each.second; |
| 56 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 57 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 58 | |
| 59 | mDataItemCache.clear(); |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 60 | } |
| 61 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 62 | void SystemStatusOsObserver::setSubscriptionObj(IDataItemSubscription* subscriptionObj) |
| 63 | { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 64 | struct SetSubsObj : public LocMsg { |
| 65 | ObserverContext& mContext; |
| 66 | IDataItemSubscription* mSubsObj; |
| 67 | inline SetSubsObj(ObserverContext& context, IDataItemSubscription* subscriptionObj) : |
| 68 | mContext(context), mSubsObj(subscriptionObj) {} |
| 69 | void proc() const { |
| 70 | mContext.mSubscriptionObj = mSubsObj; |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 71 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 72 | if (!mContext.mSSObserver->mDataItemToClients.empty()) { |
| 73 | list<DataItemId> dis( |
| 74 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 75 | mContext.mSSObserver->mDataItemToClients.getKeys())); |
| 76 | mContext.mSubscriptionObj->subscribe(dis, mContext.mSSObserver); |
| 77 | mContext.mSubscriptionObj->requestData(dis, mContext.mSSObserver); |
| 78 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 79 | } |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | if (nullptr == subscriptionObj) { |
| 83 | LOC_LOGw("subscriptionObj is NULL"); |
| 84 | } else { |
| 85 | mContext.mMsgTask->sendMsg(new SetSubsObj(mContext, subscriptionObj)); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 86 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /****************************************************************************** |
| 90 | IDataItemSubscription Overrides |
| 91 | ******************************************************************************/ |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 92 | void SystemStatusOsObserver::subscribe(const list<DataItemId>& l, IDataItemObserver* client, |
| 93 | bool toRequestData) |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 94 | { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 95 | struct HandleSubscribeReq : public LocMsg { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 96 | inline HandleSubscribeReq(SystemStatusOsObserver* parent, |
| 97 | list<DataItemId>& l, IDataItemObserver* client, bool requestData) : |
| 98 | mParent(parent), mClient(client), |
| 99 | mDataItemSet(containerTransfer<list<DataItemId>, unordered_set<DataItemId>>(l)), |
Harikrishnan Hariharan | 06d7e5b | 2018-11-16 17:58:35 +0530 | [diff] [blame] | 100 | diItemlist(l), |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 101 | mToRequestData(requestData) {} |
| 102 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 103 | void proc() const { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 104 | unordered_set<DataItemId> dataItemsToSubscribe(0); |
| 105 | mParent->mDataItemToClients.add(mDataItemSet, {mClient}, &dataItemsToSubscribe); |
| 106 | mParent->mClientToDataItems.add(mClient, mDataItemSet); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 107 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 108 | mParent->sendCachedDataItems(mDataItemSet, mClient); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 109 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 110 | // Send subscription set to framework |
Harikrishnan Hariharan | 06d7e5b | 2018-11-16 17:58:35 +0530 | [diff] [blame] | 111 | if (nullptr != mParent->mContext.mSubscriptionObj) { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 112 | if (mToRequestData) { |
Harikrishnan Hariharan | 06d7e5b | 2018-11-16 17:58:35 +0530 | [diff] [blame] | 113 | LOC_LOGD("Request Data sent to framework for the following"); |
| 114 | mParent->mContext.mSubscriptionObj->requestData(diItemlist, mParent); |
| 115 | } else if (!dataItemsToSubscribe.empty()) { |
| 116 | LOC_LOGD("Subscribe Request sent to framework for the following"); |
| 117 | mParent->logMe(dataItemsToSubscribe); |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 118 | mParent->mContext.mSubscriptionObj->subscribe( |
| 119 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 120 | std::move(dataItemsToSubscribe)), |
| 121 | mParent); |
| 122 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 123 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 124 | } |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 125 | mutable SystemStatusOsObserver* mParent; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 126 | IDataItemObserver* mClient; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 127 | const unordered_set<DataItemId> mDataItemSet; |
Harikrishnan Hariharan | 06d7e5b | 2018-11-16 17:58:35 +0530 | [diff] [blame] | 128 | const list<DataItemId> diItemlist; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 129 | bool mToRequestData; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 130 | }; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 131 | |
| 132 | if (l.empty() || nullptr == client) { |
| 133 | LOC_LOGw("Data item set is empty or client is nullptr"); |
| 134 | } else { |
| 135 | mContext.mMsgTask->sendMsg( |
| 136 | new HandleSubscribeReq(this, (list<DataItemId>&)l, client, toRequestData)); |
| 137 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 138 | } |
| 139 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 140 | void SystemStatusOsObserver::updateSubscription( |
| 141 | const list<DataItemId>& l, IDataItemObserver* client) |
| 142 | { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 143 | struct HandleUpdateSubscriptionReq : public LocMsg { |
| 144 | HandleUpdateSubscriptionReq(SystemStatusOsObserver* parent, |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 145 | list<DataItemId>& l, IDataItemObserver* client) : |
| 146 | mParent(parent), mClient(client), |
| 147 | mDataItemSet(containerTransfer<list<DataItemId>, unordered_set<DataItemId>>(l)) {} |
| 148 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 149 | void proc() const { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 150 | unordered_set<DataItemId> dataItemsToSubscribe(0); |
| 151 | unordered_set<DataItemId> dataItemsToUnsubscribe(0); |
| 152 | unordered_set<IDataItemObserver*> clients({mClient}); |
| 153 | // below removes clients from all entries keyed with the return of the |
| 154 | // mClientToDataItems.update() call. If leaving an empty set of clients as the |
| 155 | // result, the entire entry will be removed. dataItemsToUnsubscribe will be |
| 156 | // populated to keep the keys of the removed entries. |
| 157 | mParent->mDataItemToClients.trimOrRemove( |
| 158 | // this call updates <IDataItemObserver*, DataItemId> map; removes |
| 159 | // the DataItemId's that are not new to the clietn from mDataItemSet; |
| 160 | // and returns a set of mDataItemSet's that are no longer used by client. |
| 161 | // This unused set of mDataItemSet's is passed to trimOrRemove method of |
| 162 | // <DataItemId, IDataItemObserver*> map to remove the client from the |
| 163 | // corresponding entries, and gets a set of the entries that are |
| 164 | // removed from the <DataItemId, IDataItemObserver*> map as a result. |
| 165 | mParent->mClientToDataItems.update(mClient, |
| 166 | (unordered_set<DataItemId>&)mDataItemSet), |
| 167 | clients, &dataItemsToUnsubscribe, nullptr); |
| 168 | // below adds mClient to <DataItemId, IDataItemObserver*> map, and populates |
| 169 | // new keys added to that map, which are DataItemIds to be subscribed. |
| 170 | mParent->mDataItemToClients.add(mDataItemSet, clients, &dataItemsToSubscribe); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 171 | |
| 172 | // Send First Response |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 173 | mParent->sendCachedDataItems(mDataItemSet, mClient); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 174 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 175 | if (nullptr != mParent->mContext.mSubscriptionObj) { |
| 176 | // Send subscription set to framework |
| 177 | if (!dataItemsToSubscribe.empty()) { |
| 178 | LOC_LOGD("Subscribe Request sent to framework for the following"); |
| 179 | mParent->logMe(dataItemsToSubscribe); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 180 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 181 | mParent->mContext.mSubscriptionObj->subscribe( |
| 182 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 183 | std::move(dataItemsToSubscribe)), |
| 184 | mParent); |
| 185 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 186 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 187 | // Send unsubscribe to framework |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 188 | if (!dataItemsToUnsubscribe.empty()) { |
| 189 | LOC_LOGD("Unsubscribe Request sent to framework for the following"); |
| 190 | mParent->logMe(dataItemsToUnsubscribe); |
| 191 | |
| 192 | mParent->mContext.mSubscriptionObj->unsubscribe( |
| 193 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 194 | std::move(dataItemsToUnsubscribe)), |
| 195 | mParent); |
| 196 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 197 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 198 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 199 | SystemStatusOsObserver* mParent; |
| 200 | IDataItemObserver* mClient; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 201 | unordered_set<DataItemId> mDataItemSet; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 202 | }; |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 203 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 204 | if (l.empty() || nullptr == client) { |
| 205 | LOC_LOGw("Data item set is empty or client is nullptr"); |
| 206 | } else { |
| 207 | mContext.mMsgTask->sendMsg( |
| 208 | new HandleUpdateSubscriptionReq(this, (list<DataItemId>&)l, client)); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 209 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 210 | } |
| 211 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 212 | void SystemStatusOsObserver::unsubscribe( |
| 213 | const list<DataItemId>& l, IDataItemObserver* client) |
| 214 | { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 215 | struct HandleUnsubscribeReq : public LocMsg { |
| 216 | HandleUnsubscribeReq(SystemStatusOsObserver* parent, |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 217 | list<DataItemId>& l, IDataItemObserver* client) : |
| 218 | mParent(parent), mClient(client), |
| 219 | mDataItemSet(containerTransfer<list<DataItemId>, unordered_set<DataItemId>>(l)) {} |
| 220 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 221 | void proc() const { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 222 | unordered_set<DataItemId> dataItemsUnusedByClient(0); |
| 223 | unordered_set<IDataItemObserver*> clientToRemove(0); |
| 224 | mParent->mClientToDataItems.trimOrRemove({mClient}, mDataItemSet, &clientToRemove, |
| 225 | &dataItemsUnusedByClient); |
| 226 | unordered_set<DataItemId> dataItemsToUnsubscribe(0); |
| 227 | mParent->mDataItemToClients.trimOrRemove(dataItemsUnusedByClient, {mClient}, |
| 228 | &dataItemsToUnsubscribe, nullptr); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 229 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 230 | if (nullptr != mParent->mContext.mSubscriptionObj && !dataItemsToUnsubscribe.empty()) { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 231 | LOC_LOGD("Unsubscribe Request sent to framework for the following data items"); |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 232 | mParent->logMe(dataItemsToUnsubscribe); |
| 233 | |
| 234 | // Send unsubscribe to framework |
| 235 | mParent->mContext.mSubscriptionObj->unsubscribe( |
| 236 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 237 | std::move(dataItemsToUnsubscribe)), |
| 238 | mParent); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 239 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 240 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 241 | SystemStatusOsObserver* mParent; |
| 242 | IDataItemObserver* mClient; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 243 | unordered_set<DataItemId> mDataItemSet; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 244 | }; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 245 | |
| 246 | if (l.empty() || nullptr == client) { |
| 247 | LOC_LOGw("Data item set is empty or client is nullptr"); |
| 248 | } else { |
| 249 | mContext.mMsgTask->sendMsg(new HandleUnsubscribeReq(this, (list<DataItemId>&)l, client)); |
| 250 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 251 | } |
| 252 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 253 | void SystemStatusOsObserver::unsubscribeAll(IDataItemObserver* client) |
| 254 | { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 255 | struct HandleUnsubscribeAllReq : public LocMsg { |
| 256 | HandleUnsubscribeAllReq(SystemStatusOsObserver* parent, |
| 257 | IDataItemObserver* client) : |
| 258 | mParent(parent), mClient(client) {} |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 259 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 260 | void proc() const { |
| 261 | unordered_set<DataItemId> diByClient = mParent->mClientToDataItems.getValSet(mClient); |
| 262 | if (!diByClient.empty()) { |
| 263 | unordered_set<DataItemId> dataItemsToUnsubscribe; |
| 264 | mParent->mClientToDataItems.remove(mClient); |
| 265 | mParent->mDataItemToClients.trimOrRemove(diByClient, {mClient}, |
| 266 | &dataItemsToUnsubscribe, nullptr); |
| 267 | |
| 268 | if (!dataItemsToUnsubscribe.empty() && |
| 269 | nullptr != mParent->mContext.mSubscriptionObj) { |
| 270 | |
| 271 | LOC_LOGD("Unsubscribe Request sent to framework for the following data items"); |
| 272 | mParent->logMe(dataItemsToUnsubscribe); |
| 273 | |
| 274 | // Send unsubscribe to framework |
| 275 | mParent->mContext.mSubscriptionObj->unsubscribe( |
| 276 | containerTransfer<unordered_set<DataItemId>, list<DataItemId>>( |
| 277 | std::move(dataItemsToUnsubscribe)), |
| 278 | mParent); |
| 279 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 280 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 281 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 282 | SystemStatusOsObserver* mParent; |
| 283 | IDataItemObserver* mClient; |
| 284 | }; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 285 | |
| 286 | if (nullptr == client) { |
| 287 | LOC_LOGw("Data item set is empty or client is nullptr"); |
| 288 | } else { |
| 289 | mContext.mMsgTask->sendMsg(new HandleUnsubscribeAllReq(this, client)); |
| 290 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /****************************************************************************** |
| 294 | IDataItemObserver Overrides |
| 295 | ******************************************************************************/ |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 296 | void SystemStatusOsObserver::notify(const list<IDataItemCore*>& dlist) |
| 297 | { |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 298 | struct HandleNotify : public LocMsg { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 299 | HandleNotify(SystemStatusOsObserver* parent, vector<IDataItemCore*>& v) : |
| 300 | mParent(parent), mDiVec(std::move(v)) {} |
| 301 | |
| 302 | inline virtual ~HandleNotify() { |
| 303 | for (auto item : mDiVec) { |
| 304 | delete item; |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 305 | } |
| 306 | } |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 307 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 308 | void proc() const { |
| 309 | // Update Cache with received data items and prepare |
| 310 | // list of data items to be sent. |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 311 | unordered_set<DataItemId> dataItemIdsToBeSent(0); |
| 312 | for (auto item : mDiVec) { |
| 313 | if (mParent->updateCache(item)) { |
| 314 | dataItemIdsToBeSent.insert(item->getId()); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | // Send data item to all subscribed clients |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 319 | unordered_set<IDataItemObserver*> clientSet(0); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 320 | for (auto each : dataItemIdsToBeSent) { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 321 | auto clients = mParent->mDataItemToClients.getValSetPtr(each); |
| 322 | if (nullptr != clients) { |
| 323 | clientSet.insert(clients->begin(), clients->end()); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 324 | } |
| 325 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 326 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 327 | for (auto client : clientSet) { |
| 328 | unordered_set<DataItemId> dataItemIdsForThisClient( |
| 329 | mParent->mClientToDataItems.getValSet(client)); |
Baili Feng | de454e2 | 2018-08-15 20:54:54 +0800 | [diff] [blame] | 330 | for (auto itr = dataItemIdsForThisClient.begin(); |
| 331 | itr != dataItemIdsForThisClient.end(); ) { |
| 332 | if (dataItemIdsToBeSent.find(*itr) == dataItemIdsToBeSent.end()) { |
| 333 | itr = dataItemIdsForThisClient.erase(itr); |
| 334 | } else { |
| 335 | itr++; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 338 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 339 | mParent->sendCachedDataItems(dataItemIdsForThisClient, client); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | SystemStatusOsObserver* mParent; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 343 | const vector<IDataItemCore*> mDiVec; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 344 | }; |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 345 | |
| 346 | if (!dlist.empty()) { |
| 347 | vector<IDataItemCore*> dataItemVec(dlist.size()); |
| 348 | |
| 349 | for (auto each : dlist) { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 350 | |
| 351 | IDataItemCore* di = DataItemsFactoryProxy::createNewDataItem(each->getId()); |
| 352 | if (nullptr == di) { |
| 353 | LOC_LOGw("Unable to create dataitem:%d", each->getId()); |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | // Copy contents into the newly created data item |
| 358 | di->copy(each); |
| 359 | |
| 360 | // add this dataitem if updated from last one |
| 361 | dataItemVec.push_back(di); |
Yingjie Wang | 355dcda | 2020-01-17 10:10:18 +0800 | [diff] [blame] | 362 | IF_LOC_LOGD { |
| 363 | string dv; |
| 364 | di->stringify(dv); |
| 365 | LOC_LOGd("notify: DataItem In Value:%s", dv.c_str()); |
| 366 | } |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (!dataItemVec.empty()) { |
| 370 | mContext.mMsgTask->sendMsg(new HandleNotify(this, dataItemVec)); |
| 371 | } |
| 372 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /****************************************************************************** |
| 376 | IFrameworkActionReq Overrides |
| 377 | ******************************************************************************/ |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 378 | void SystemStatusOsObserver::turnOn(DataItemId dit, int timeOut) |
| 379 | { |
| 380 | if (nullptr == mContext.mFrameworkActionReqObj) { |
| 381 | LOC_LOGE("%s:%d]: Framework action request object is NULL", __func__, __LINE__); |
| 382 | return; |
| 383 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 384 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 385 | // Check if data item exists in mActiveRequestCount |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 386 | DataItemIdToInt::iterator citer = mActiveRequestCount.find(dit); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 387 | if (citer == mActiveRequestCount.end()) { |
| 388 | // Data item not found in map |
| 389 | // Add reference count as 1 and add dataitem to map |
| 390 | pair<DataItemId, int> cpair(dit, 1); |
| 391 | mActiveRequestCount.insert(cpair); |
| 392 | LOC_LOGD("Sending turnOn request"); |
| 393 | |
| 394 | // Send action turn on to framework |
| 395 | struct HandleTurnOnMsg : public LocMsg { |
| 396 | HandleTurnOnMsg(IFrameworkActionReq* framework, |
| 397 | DataItemId dit, int timeOut) : |
| 398 | mFrameworkActionReqObj(framework), mDataItemId(dit), mTimeOut(timeOut) {} |
| 399 | virtual ~HandleTurnOnMsg() {} |
| 400 | void proc() const { |
| 401 | mFrameworkActionReqObj->turnOn(mDataItemId, mTimeOut); |
| 402 | } |
| 403 | IFrameworkActionReq* mFrameworkActionReqObj; |
| 404 | DataItemId mDataItemId; |
| 405 | int mTimeOut; |
| 406 | }; |
Harikrishnan Hariharan | 352ffcb | 2018-09-14 12:36:09 +0530 | [diff] [blame] | 407 | mContext.mMsgTask->sendMsg( |
| 408 | new (nothrow) HandleTurnOnMsg(mContext.mFrameworkActionReqObj, dit, timeOut)); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 409 | } |
| 410 | else { |
| 411 | // Found in map, update reference count |
| 412 | citer->second++; |
| 413 | LOC_LOGD("turnOn - Data item:%d Num_refs:%d", dit, citer->second); |
| 414 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 415 | } |
| 416 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 417 | void SystemStatusOsObserver::turnOff(DataItemId dit) |
| 418 | { |
| 419 | if (nullptr == mContext.mFrameworkActionReqObj) { |
| 420 | LOC_LOGE("%s:%d]: Framework action request object is NULL", __func__, __LINE__); |
| 421 | return; |
| 422 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 423 | |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 424 | // Check if data item exists in mActiveRequestCount |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 425 | DataItemIdToInt::iterator citer = mActiveRequestCount.find(dit); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 426 | if (citer != mActiveRequestCount.end()) { |
| 427 | // found |
| 428 | citer->second--; |
| 429 | LOC_LOGD("turnOff - Data item:%d Remaining:%d", dit, citer->second); |
| 430 | if(citer->second == 0) { |
| 431 | // if this was last reference, remove item from map and turn off module |
| 432 | mActiveRequestCount.erase(citer); |
| 433 | |
| 434 | // Send action turn off to framework |
| 435 | struct HandleTurnOffMsg : public LocMsg { |
| 436 | HandleTurnOffMsg(IFrameworkActionReq* framework, DataItemId dit) : |
| 437 | mFrameworkActionReqObj(framework), mDataItemId(dit) {} |
| 438 | virtual ~HandleTurnOffMsg() {} |
| 439 | void proc() const { |
| 440 | mFrameworkActionReqObj->turnOff(mDataItemId); |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 441 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 442 | IFrameworkActionReq* mFrameworkActionReqObj; |
| 443 | DataItemId mDataItemId; |
| 444 | }; |
| 445 | mContext.mMsgTask->sendMsg( |
| 446 | new (nothrow) HandleTurnOffMsg(mContext.mFrameworkActionReqObj, dit)); |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 447 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 448 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 449 | } |
| 450 | |
Harikrishnan Hariharan | f4dc944 | 2019-09-01 10:52:23 +0530 | [diff] [blame] | 451 | #ifdef USE_GLIB |
Harikrishnan Hariharan | f0d7fe8 | 2017-09-04 11:55:13 +0530 | [diff] [blame] | 452 | bool SystemStatusOsObserver::connectBackhaul() |
| 453 | { |
| 454 | bool result = false; |
| 455 | |
| 456 | if (mContext.mFrameworkActionReqObj != NULL) { |
| 457 | struct HandleConnectBackhaul : public LocMsg { |
| 458 | HandleConnectBackhaul(IFrameworkActionReq* fwkActReq) : |
| 459 | mFwkActionReqObj(fwkActReq) {} |
| 460 | virtual ~HandleConnectBackhaul() {} |
| 461 | void proc() const { |
| 462 | LOC_LOGD("HandleConnectBackhaul"); |
| 463 | mFwkActionReqObj->connectBackhaul(); |
| 464 | } |
| 465 | IFrameworkActionReq* mFwkActionReqObj; |
| 466 | }; |
| 467 | mContext.mMsgTask->sendMsg( |
| 468 | new (nothrow) HandleConnectBackhaul(mContext.mFrameworkActionReqObj)); |
| 469 | result = true; |
| 470 | } |
| 471 | else { |
| 472 | ++mBackHaulConnectReqCount; |
| 473 | LOC_LOGE("Framework action request object is NULL.Caching connect request: %d", |
| 474 | mBackHaulConnectReqCount); |
| 475 | result = false; |
| 476 | } |
| 477 | return result; |
| 478 | |
| 479 | } |
| 480 | |
| 481 | bool SystemStatusOsObserver::disconnectBackhaul() |
| 482 | { |
| 483 | bool result = false; |
| 484 | |
| 485 | if (mContext.mFrameworkActionReqObj != NULL) { |
| 486 | struct HandleDisconnectBackhaul : public LocMsg { |
| 487 | HandleDisconnectBackhaul(IFrameworkActionReq* fwkActReq) : |
| 488 | mFwkActionReqObj(fwkActReq) {} |
| 489 | virtual ~HandleDisconnectBackhaul() {} |
| 490 | void proc() const { |
| 491 | LOC_LOGD("HandleDisconnectBackhaul"); |
| 492 | mFwkActionReqObj->disconnectBackhaul(); |
| 493 | } |
| 494 | IFrameworkActionReq* mFwkActionReqObj; |
| 495 | }; |
| 496 | mContext.mMsgTask->sendMsg( |
| 497 | new (nothrow) HandleDisconnectBackhaul(mContext.mFrameworkActionReqObj)); |
| 498 | } |
| 499 | else { |
| 500 | if (mBackHaulConnectReqCount > 0) { |
| 501 | --mBackHaulConnectReqCount; |
| 502 | } |
| 503 | LOC_LOGE("Framework action request object is NULL.Caching disconnect request: %d", |
| 504 | mBackHaulConnectReqCount); |
| 505 | result = false; |
| 506 | } |
| 507 | return result; |
| 508 | } |
| 509 | #endif |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 510 | /****************************************************************************** |
| 511 | Helpers |
| 512 | ******************************************************************************/ |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 513 | void SystemStatusOsObserver::sendCachedDataItems( |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 514 | const unordered_set<DataItemId>& s, IDataItemObserver* to) |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 515 | { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 516 | if (nullptr == to) { |
| 517 | LOC_LOGv("client pointer is NULL."); |
| 518 | } else { |
| 519 | string clientName; |
| 520 | to->getName(clientName); |
| 521 | list<IDataItemCore*> dataItems(0); |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 522 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 523 | for (auto each : s) { |
| 524 | auto citer = mDataItemCache.find(each); |
| 525 | if (citer != mDataItemCache.end()) { |
| 526 | string dv; |
| 527 | citer->second->stringify(dv); |
| 528 | LOC_LOGI("DataItem: %s >> %s", dv.c_str(), clientName.c_str()); |
| 529 | dataItems.push_front(citer->second); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (dataItems.empty()) { |
| 534 | LOC_LOGv("No items to notify."); |
| 535 | } else { |
| 536 | to->notify(dataItems); |
| 537 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 538 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 539 | } |
| 540 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 541 | bool SystemStatusOsObserver::updateCache(IDataItemCore* d) |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 542 | { |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 543 | bool dataItemUpdated = false; |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 544 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 545 | // Request systemstatus to record this dataitem in its cache |
| 546 | // if the return is false, it means that SystemStatus is not |
| 547 | // handling it, so SystemStatusOsObserver also doesn't. |
| 548 | // So it has to be true to proceed. |
| 549 | if (nullptr != d && mSystemStatus->eventDataItemNotify(d)) { |
| 550 | auto citer = mDataItemCache.find(d->getId()); |
| 551 | if (citer == mDataItemCache.end()) { |
| 552 | // New data item; not found in cache |
| 553 | IDataItemCore* dataitem = DataItemsFactoryProxy::createNewDataItem(d->getId()); |
| 554 | if (nullptr != dataitem) { |
| 555 | // Copy the contents of the data item |
| 556 | dataitem->copy(d); |
| 557 | // Insert in mDataItemCache |
| 558 | mDataItemCache.insert(std::make_pair(d->getId(), dataitem)); |
| 559 | dataItemUpdated = true; |
| 560 | } |
| 561 | } else { |
| 562 | // Found in cache; Update cache if necessary |
| 563 | citer->second->copy(d, &dataItemUpdated); |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 564 | } |
| 565 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 566 | if (dataItemUpdated) { |
| 567 | LOC_LOGV("DataItem:%d updated:%d", d->getId(), dataItemUpdated); |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 568 | } |
Harikrishnan Hariharan | ff8b317 | 2017-08-11 17:36:10 +0530 | [diff] [blame] | 569 | } |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 570 | |
Kevin Tang | 36da980 | 2018-03-23 22:57:51 -0700 | [diff] [blame] | 571 | return dataItemUpdated; |
Harikrishnan Hariharan | ee2d665 | 2017-08-11 15:13:38 +0530 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | } // namespace loc_core |
| 575 | |