Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / examples / BMISensorBundle / src / inputSensors / HeightSensorApp / src / HeightSensorApp.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <HeightSensorApp.h>
22
23 using namespace OC;
24 using namespace std;
25
26 int g_Observation = 0;
27
28 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
29
30 /*
31  * TempResourceFunctions
32  */
33
34 void HeightResource::registerResource()
35 {
36     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
37
38     // This will internally create and register the resource.
39     OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
40                            m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
41
42     if (OC_STACK_OK != result)
43     {
44         cout << "Resource creation was unsuccessful\n";
45     }
46 }
47
48 OCResourceHandle HeightResource::getHandle()
49 {
50     return m_resourceHandle;
51 }
52
53 void HeightResource::setResourceRepresentation(OCRepresentation &rep)
54 {
55     double tempHeight;
56
57     rep.getValue("height", tempHeight);
58
59     m_height = tempHeight;
60
61     cout << "\t\t\t" << "Received representation: " << endl;
62
63     cout << "\t\t\t\t" << "height" << m_height << endl;
64
65 }
66
67 OCRepresentation HeightResource::getResourceRepresentation()
68 {
69     m_resourceRep.setValue("height", std::to_string(m_height));
70
71     return m_resourceRep;
72 }
73
74 // Create the instance of the TemphumidResource class
75 HeightResource g_myResource;
76
77 void *TestSensorVal(void *param)
78 {
79     (void)param;
80
81     bool bFlag = true;
82     int nSleep_time = INTERVAL_FOR_CHECK;
83     double nHeight;
84
85     std::cout << "[HeightSensorAPP] ::" << __func__ << " is called."
86               << std::endl;
87
88     nHeight = INIT_VAL;
89
90     // This function continuously monitors for the changes
91     while (1)
92     {
93         sleep(nSleep_time);
94
95         if (g_Observation)
96         {
97             if (bFlag)
98             {
99                 nSleep_time = INTERVAL_FOR_MEASUREMENT;
100                 nHeight += DIFF_VAL;
101                 g_myResource.m_height = nHeight;
102             }
103             else
104             {
105                 nSleep_time = INTERVAL_FOR_CHECK;
106             }
107
108             bFlag = bFlag ^ true;
109
110             cout << "\n height updated to : " << g_myResource.m_height << endl;
111             cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
112                  << endl;
113
114             OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
115
116
117             if (OC_STACK_NO_OBSERVERS == result)
118             {
119                 cout << "No More observers, stopping notifications" << endl;
120                 g_Observation = 0;
121             }
122         }
123         else
124         {
125             nSleep_time = INTERVAL_FOR_CHECK ;
126         }
127     }
128     return NULL;
129 }
130
131 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
132 {
133     cout << "\tIn Server CPP entity handler:\n";
134
135     auto response = std::make_shared<OC::OCResourceResponse>();
136
137     if (request)
138     {
139         // Get the request type and request flag
140         std::string requestType = request->getRequestType();
141         int requestFlag = request->getRequestHandlerFlag();
142
143         response->setRequestHandle(request->getRequestHandle());
144         response->setResourceHandle(request->getResourceHandle());
145
146         if (requestFlag & RequestHandlerFlag::RequestFlag)
147         {
148             cout << "\t\trequestFlag : Request\n";
149
150             // If the request type is GET
151             if (requestType == "GET")
152             {
153                 cout << "\t\t\trequestType : GET\n";
154
155                 // Check for query params (if any)
156                 // Process query params and do required operations ..
157
158                 // Get the representation of this resource at this point and send it as response
159                 OCRepresentation rep = g_myResource.getResourceRepresentation();
160
161                 if (response)
162                 {
163                     // TODO Error Code
164                     response->setErrorCode(200);
165                     response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
166                 }
167             }
168             else if (requestType == "PUT")
169             {
170                 // TODO: PUT request operations
171             }
172             else if (requestType == "POST")
173             {
174                 // TODO: POST request operations
175             }
176             else if (requestType == "DELETE")
177             {
178                 // TODO: DELETE request operations
179             }
180         }
181
182         if (requestFlag & RequestHandlerFlag::ObserverFlag)
183         {
184             pthread_t threadId;
185
186             cout << "\t\trequestFlag : Observer\n";
187             g_Observation = 1;
188
189             static int startedThread = 0;
190
191             if (!startedThread)
192             {
193                 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
194                 startedThread = 1;
195             }
196         }
197     }
198     else
199     {
200         std::cout << "Request invalid" << std::endl;
201     }
202
203     return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
204 }
205
206 int main()
207 {
208     // Create PlatformConfig object
209     PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
210
211     try
212     {
213         OC::OCPlatform::Configure(cfg);
214
215         OC::OCPlatform::startPresence(60);
216
217         g_myResource.registerResource();
218
219         int input = 0;
220         cout << "Type any key to terminate" << endl;
221         cin >> input;
222
223         OC::OCPlatform::stopPresence();
224     }
225     catch (std::exception e)
226     {
227         cout << e.what() << endl;
228     }
229
230     return 0;
231 }
232