1 /* Copyright (c) 2015-2017, 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 
32 #include <cinttypes>
33 #include <string>
34 #include <list>
35 #include <map>
36 #include <new>
37 #include <vector>
38 
39 #include <MsgTask.h>
40 #include <DataItemId.h>
41 #include <IOsObserver.h>
42 #include <platform_lib_log_util.h>
43 
44 namespace loc_core
45 {
46 /******************************************************************************
47  SystemStatusOsObserver
48 ******************************************************************************/
49 using namespace std;
50 
51 // Forward Declarations
52 class IDataItemCore;
53 template<typename CT, typename DIT> class IClientIndex;
54 template<typename CT, typename DIT> class IDataItemIndex;
55 
56 struct SystemContext {
57     IDataItemSubscription* mSubscriptionObj;
58     IFrameworkActionReq* mFrameworkActionReqObj;
59     const MsgTask* mMsgTask;
60 
SystemContextSystemContext61     inline SystemContext() :
62         mSubscriptionObj(NULL),
63         mFrameworkActionReqObj(NULL),
64         mMsgTask(NULL) {}
65 };
66 
67 typedef map<IDataItemObserver*, list<DataItemId>> ObserverReqCache;
68 
69 // Clients wanting to get data from OS/Framework would need to
70 // subscribe with OSObserver using IDataItemSubscription interface.
71 // Such clients would need to implement IDataItemObserver interface
72 // to receive data when it becomes available.
73 class SystemStatusOsObserver : public IOsObserver {
74 
75 public:
76     // ctor
77     SystemStatusOsObserver(const MsgTask* msgTask);
78 
79     // dtor
80     ~SystemStatusOsObserver();
81 
82     // To set the subscription object
83     virtual void setSubscriptionObj(IDataItemSubscription* subscriptionObj);
84 
85     // To set the framework action request object
setFrameworkActionReqObj(IFrameworkActionReq * frameworkActionReqObj)86     inline void setFrameworkActionReqObj(IFrameworkActionReq* frameworkActionReqObj) {
87         mContext.mFrameworkActionReqObj = frameworkActionReqObj;
88     }
89 
90     // IDataItemSubscription Overrides
91     virtual void subscribe(const list<DataItemId>& l, IDataItemObserver* client);
92     virtual void updateSubscription(const list<DataItemId>& l, IDataItemObserver* client);
93     virtual void requestData(const list<DataItemId>& l, IDataItemObserver* client);
94     virtual void unsubscribe(const list<DataItemId>& l, IDataItemObserver* client);
95     virtual void unsubscribeAll(IDataItemObserver* client);
96 
97     // IDataItemObserver Overrides
98     virtual void notify(const list<IDataItemCore*>& dlist);
getName(string & name)99     inline virtual void getName(string& name) {
100         name = mAddress;
101     }
102 
103     // IFrameworkActionReq Overrides
104     virtual void turnOn(DataItemId dit, int timeOut = 0);
105     virtual void turnOff(DataItemId dit);
106 
107 private:
108     SystemContext                                    mContext;
109     const string                                     mAddress;
110     IClientIndex<IDataItemObserver*, DataItemId>*    mClientIndex;
111     IDataItemIndex<IDataItemObserver*, DataItemId>*  mDataItemIndex;
112     map<DataItemId, IDataItemCore*>                  mDataItemCache;
113     map<DataItemId, int>                             mActiveRequestCount;
114 
115     // Cache the subscribe and requestData till subscription obj is obtained
116     ObserverReqCache mSubscribeReqCache;
117     ObserverReqCache mReqDataCache;
118     void cacheObserverRequest(ObserverReqCache& reqCache,
119             const list<DataItemId>& l, IDataItemObserver* client);
120 
121     // Helpers
122     void sendFirstResponse(const list<DataItemId>& l, IDataItemObserver* to);
123     void sendCachedDataItems(const list<DataItemId>& l, IDataItemObserver* to);
124     void updateCache(IDataItemCore* d, bool& dataItemUpdated);
logMe(const list<DataItemId> & l)125     inline void logMe(const list<DataItemId>& l) {
126         for (auto id : l) {
127             LOC_LOGD("DataItem %d", id);
128         }
129     }
130 };
131 
132 } // namespace loc_core
133 
134 #endif //__SYSTEM_STATUS__
135 
136