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