SystemStatusOsObserver.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2015-2017, 2020 The Linux Foundation. All rights reserved.
  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. #ifndef __SYSTEM_STATUS_OSOBSERVER__
  30. #define __SYSTEM_STATUS_OSOBSERVER__
  31. #include <cinttypes>
  32. #include <string>
  33. #include <map>
  34. #include <new>
  35. #include <vector>
  36. #include <MsgTask.h>
  37. #include <DataItemId.h>
  38. #include <IOsObserver.h>
  39. #include <loc_pla.h>
  40. #include <log_util.h>
  41. #include <gps_extended.h>
  42. #include <LocUnorderedSetMap.h>
  43. namespace loc_core
  44. {
  45. /******************************************************************************
  46. SystemStatusOsObserver
  47. ******************************************************************************/
  48. using namespace std;
  49. using namespace loc_util;
  50. // Forward Declarations
  51. class IDataItemCore;
  52. class SystemStatus;
  53. class SystemStatusOsObserver;
  54. typedef LocUnorderedSetMap<IDataItemObserver*, DataItemId> ClientToDataItems;
  55. typedef LocUnorderedSetMap<DataItemId, IDataItemObserver*> DataItemToClients;
  56. typedef unordered_map<DataItemId, IDataItemCore*> DataItemIdToCore;
  57. typedef unordered_map<DataItemId, int> DataItemIdToInt;
  58. #ifdef USE_GLIB
  59. // Cache details of backhaul client requests
  60. typedef std::map<string, BackhaulContext> ClientBackhaulReqCache;
  61. #endif
  62. struct ObserverContext {
  63. IDataItemSubscription* mSubscriptionObj;
  64. IFrameworkActionReq* mFrameworkActionReqObj;
  65. const MsgTask* mMsgTask;
  66. SystemStatusOsObserver* mSSObserver;
  67. inline ObserverContext(const MsgTask* msgTask, SystemStatusOsObserver* observer) :
  68. mSubscriptionObj(NULL), mFrameworkActionReqObj(NULL),
  69. mMsgTask(msgTask), mSSObserver(observer) {}
  70. };
  71. // Clients wanting to get data from OS/Framework would need to
  72. // subscribe with OSObserver using IDataItemSubscription interface.
  73. // Such clients would need to implement IDataItemObserver interface
  74. // to receive data when it becomes available.
  75. class SystemStatusOsObserver : public IOsObserver {
  76. public:
  77. // ctor
  78. inline SystemStatusOsObserver(SystemStatus* systemstatus, const MsgTask* msgTask) :
  79. mSystemStatus(systemstatus), mContext(msgTask, this),
  80. mAddress("SystemStatusOsObserver"),
  81. mClientToDataItems(MAX_DATA_ITEM_ID), mDataItemToClients(MAX_DATA_ITEM_ID) {}
  82. // dtor
  83. ~SystemStatusOsObserver();
  84. template <typename CINT, typename COUT>
  85. static COUT containerTransfer(CINT& s);
  86. template <typename CINT, typename COUT>
  87. inline static COUT containerTransfer(CINT&& s) {
  88. return containerTransfer<CINT, COUT>(s);
  89. }
  90. // To set the subscription object
  91. virtual void setSubscriptionObj(IDataItemSubscription* subscriptionObj);
  92. // To set the framework action request object
  93. inline void setFrameworkActionReqObj(IFrameworkActionReq* frameworkActionReqObj) {
  94. mContext.mFrameworkActionReqObj = frameworkActionReqObj;
  95. #ifdef USE_GLIB
  96. uint32_t numBackHaulClients = mBackHaulConnReqCache.size();
  97. if (numBackHaulClients > 0) {
  98. // For each client, invoke connectbackhaul.
  99. for (auto clientContext : mBackHaulConnReqCache) {
  100. LOC_LOGd("Invoke connectBackhaul for client: %s Sub: %d Apn: %s IpType: %d",
  101. clientContext.second.clientName.c_str(), clientContext.second.prefSub,
  102. clientContext.second.prefApn.c_str(), clientContext.second.prefIpType);
  103. BackhaulContext ctx = { clientContext.second.clientName,
  104. clientContext.second.prefSub,
  105. clientContext.second.prefApn,
  106. clientContext.second.prefIpType };
  107. connectBackhaul(ctx);
  108. }
  109. // Clear the set
  110. mBackHaulConnReqCache.clear();
  111. }
  112. #endif
  113. }
  114. // IDataItemSubscription Overrides
  115. inline virtual void subscribe(const unordered_set<DataItemId>& l, IDataItemObserver* client) override {
  116. subscribe(l, client, false);
  117. }
  118. virtual void updateSubscription(const unordered_set<DataItemId>& l, IDataItemObserver* client) override;
  119. inline virtual void requestData(const unordered_set<DataItemId>& l, IDataItemObserver* client) override {
  120. subscribe(l, client, true);
  121. }
  122. virtual void unsubscribe(const unordered_set<DataItemId>& l, IDataItemObserver* client) override;
  123. virtual void unsubscribeAll(IDataItemObserver* client) override;
  124. // IDataItemObserver Overrides
  125. virtual void notify(const unordered_set<IDataItemCore*>& dlist) override;
  126. inline virtual void getName(string& name) override {
  127. name = mAddress;
  128. }
  129. // IFrameworkActionReq Overrides
  130. virtual void turnOn(DataItemId dit, int timeOut = 0) override;
  131. virtual void turnOff(DataItemId dit) override;
  132. #ifdef USE_GLIB
  133. virtual bool connectBackhaul(const BackhaulContext& ctx) override;
  134. virtual bool disconnectBackhaul(const BackhaulContext& ctx) override;
  135. #endif
  136. private:
  137. SystemStatus* mSystemStatus;
  138. ObserverContext mContext;
  139. const string mAddress;
  140. ClientToDataItems mClientToDataItems;
  141. DataItemToClients mDataItemToClients;
  142. DataItemIdToCore mDataItemCache;
  143. DataItemIdToInt mActiveRequestCount;
  144. #ifdef USE_GLIB
  145. // Cache the framework action request for connect/disconnect
  146. ClientBackhaulReqCache mBackHaulConnReqCache;
  147. #endif
  148. void subscribe(const unordered_set<DataItemId>& l, IDataItemObserver* client, bool toRequestData);
  149. // Helpers
  150. void sendCachedDataItems(const unordered_set<DataItemId>& s, IDataItemObserver* to);
  151. bool updateCache(IDataItemCore* d);
  152. inline void logMe(const unordered_set<DataItemId>& l) {
  153. IF_LOC_LOGD {
  154. for (auto id : l) {
  155. LOC_LOGD("DataItem %d", id);
  156. }
  157. }
  158. }
  159. };
  160. } // namespace loc_core
  161. #endif //__SYSTEM_STATUS__