replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-container / examples / BMISensorBundle / src / inputSensors / WeightSensorApp / src / WeightSensorApp.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 <WeightSensorApp.h>
22
23 using namespace OC;
24 using namespace std;
25
26
27 int g_Observation = 0;
28
29 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
30
31 /*
32  * TempResourceFunctions
33  */
34
35 void WeightResource::registerResource()
36 {
37     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
38
39     // This will internally create and register the resource.
40     OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
41                            m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
42
43     if (OC_STACK_OK != result)
44     {
45         cout << "Resource creation was unsuccessful\n";
46     }
47 }
48
49 OCResourceHandle WeightResource::getHandle()
50 {
51     return m_resourceHandle;
52 }
53
54 void WeightResource::setResourceRepresentation(OCRepresentation &rep)
55 {
56     double tempWeight = 0.0;
57
58     rep.getValue("weight", tempWeight);
59
60     m_weight = tempWeight;
61
62     cout << "\t\t\t" << "Received representation: " << endl;
63     cout << "\t\t\t\t" << "Weight" << m_weight << endl;
64
65 }
66
67 OCRepresentation WeightResource::getResourceRepresentation()
68 {
69     m_resourceRep.setValue("weight", std::to_string(m_weight));
70
71     return m_resourceRep;
72 }
73
74 // Create the instance of the TemphumidResource class
75 WeightResource 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 nWeight = 0.0;
84
85     std::cout << "[WeightSensorAPP] ::" << __func__ << " is called."
86               << std::endl;
87
88     nWeight = 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                 nWeight += DIFF_VAL;
101                 g_myResource.m_weight = nWeight;
102             }
103             else
104             {
105                 nSleep_time = INTERVAL_FOR_CHECK;
106             }
107
108             bFlag = bFlag ^ true;
109
110             cout << "\n weight updated to : " << g_myResource.m_weight << endl;
111             cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
112                  << endl;
113
114             OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
115
116             if (OC_STACK_NO_OBSERVERS == result)
117             {
118                 cout << "No More observers, stopping notifications" << endl;
119                 g_Observation = 0;
120             }
121         }
122         else
123         {
124
125             nSleep_time = INTERVAL_FOR_CHECK ;
126         }
127
128     }
129     return NULL;
130 }
131
132 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
133 {
134     cout << "\tIn Server CPP entity handler:\n";
135
136     auto response = std::make_shared<OC::OCResourceResponse>();
137
138     if (request)
139     {
140         // Get the request type and request flag
141         std::string requestType = request->getRequestType();
142         int requestFlag = request->getRequestHandlerFlag();
143
144         response->setRequestHandle(request->getRequestHandle());
145         response->setResourceHandle(request->getResourceHandle());
146
147         if (requestFlag & RequestHandlerFlag::RequestFlag)
148         {
149             cout << "\t\trequestFlag : Request\n";
150
151             // If the request type is GET
152             if (requestType == "GET")
153             {
154                 cout << "\t\t\trequestType : GET\n";
155
156                 // Check for query params (if any)
157                 // Process query params and do required operations ..
158
159                 // Get the representation of this resource at this point and send it as response
160                 OCRepresentation rep = g_myResource.getResourceRepresentation();
161
162                 if (response)
163                 {
164                     // TODO Error Code
165                     response->setErrorCode(200);
166                     response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
167                 }
168             }
169             else if (requestType == "PUT")
170             {
171                 // TODO: PUT request operations
172             }
173             else if (requestType == "POST")
174             {
175                 // TODO: POST request operations
176             }
177             else if (requestType == "DELETE")
178             {
179                 // TODO: DELETE request operations
180             }
181         }
182
183         if (requestFlag & RequestHandlerFlag::ObserverFlag)
184         {
185             pthread_t threadId = {};
186
187             cout << "\t\trequestFlag : Observer\n";
188             g_Observation = 1;
189
190             static int startedThread = 0;
191
192             if (!startedThread)
193             {
194                 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
195                 startedThread = 1;
196             }
197         }
198     }
199     else
200     {
201         std::cout << "Request invalid" << std::endl;
202     }
203
204     return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
205 }
206
207 int main()
208 {
209     // Create PlatformConfig object
210     PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
211
212     try
213     {
214         OC::OCPlatform::Configure(cfg);
215
216         OC::OCPlatform::startPresence(60);
217
218         g_myResource.registerResource();
219
220         int input = 0;
221         cout << "Type any key to terminate" << endl;
222         cin >> input;
223
224         OC::OCPlatform::stopPresence();
225     }
226     catch (std::exception e)
227     {
228         cout << e.what() << endl;
229     }
230
231     return 0;
232 }
233