Remove unused OC_INIT_FLAG.
[platform/upstream/iotivity.git] / examples / OICMiddle / WrapResource.h
1 #ifndef WRAPRESOURCE_H
2 #define WRAPRESOURCE_H
3
4 //******************************************************************
5 //
6 // Copyright 2014 Intel Corporation.
7 //
8 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 //      http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23
24 #include <map>
25 #include <mutex>
26 #include <condition_variable>
27
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30
31 #include "OICMiddle.h"
32
33 using namespace OC;
34 using namespace std;
35
36 class WrapRequest;
37 class MiddleClient;
38
39 enum RequestType {
40     RT_Get,
41     RT_Put,
42     RT_Observe,
43 };
44
45 typedef std::shared_ptr<OCResource> ocresource_t;
46 typedef std::map<token_t, WrapRequest *> requestmap_t;
47 typedef std::function<void(const token_t token, const HeaderOptions&, const OCRepresentation&, const int, const int)> observecb_t;
48
49 class WrapResource
50 {
51 public:
52     WrapResource(string resourceID, ocresource_t resource);
53
54     token_t getResource();
55     token_t putResource(OCRepresentation& rep);
56     token_t observeResource(observecb_t& callback);
57     string getResourceID();
58     bool cancelObserve();
59     std::vector<std::string> getResourceTypes();
60     std::vector<std::string> getResourceInterfaces();
61     WrapRequest *waitResource(token_t token);
62     const stringmap_t& getFormats();
63
64     friend class WrapRequest;
65     friend class MiddleClient;
66
67 protected:
68     WrapRequest *newRequest(RequestType type);
69     void resourceCallback(WrapRequest *wreq);
70     void parseJSON(WrapRequest *wreq);
71     void findTypes();
72
73     string m_resourceID;
74     ocresource_t m_ocResource;
75     int m_listIndex;
76     int m_x;
77     bool m_repGetReady;
78     bool m_gettingRep;
79     mutex m_mutexMap;
80     mutex m_mutexGet;
81     observecb_t m_observeCB;
82     bool m_callbackRunning;
83     int m_requestToken;
84     requestmap_t m_requestMap;
85     WrapRequest *m_observeRequest;        // can only be one
86     stringmap_t m_typeMap;
87     vector<WrapRequest *> m_typeResults;
88     WrapRequest *m_typeRequest;
89 };
90
91 struct WrapRequest
92 {
93     WrapRequest(WrapResource *wres, RequestType type, token_t token);
94
95     friend class WrapResource;
96
97     HeaderOptions m_headerOptions;
98     OCRepresentation m_rep;
99     int m_eCode;
100     int m_sequenceNumber;
101     stringmap_t m_valueMap;
102     unsigned long m_touchTime;
103
104 protected:
105     WrapResource *m_parent;
106     RequestType m_type;
107     token_t m_token;
108     condition_variable m_cvGet;
109     bool m_forTypeOnly;
110     bool m_typeReady;
111
112     void getCB(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
113     void putCB(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
114     void observeCB(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode, const int sequenceNumber);
115
116     GetCallback m_getCB;
117     PutCallback m_putCB;
118     ObserveCallback m_obsCB;
119 };
120
121 #endif // WRAPRESOURCE_H