Merge branch 'master' into easysetup
[platform/upstream/iotivity.git] / service / resource-container / examples / DiscomfortIndexSensorBundle / src / inputSensors / THSensorApp / src / ThingResourceServer.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 <ThingResourceServer.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 TemphumidResource::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     result = OCPlatform::bindTypeToResource(m_resourceHandle, m_resourceTypes[1]);
48
49     if (OC_STACK_OK != result)
50     {
51         cout << "Binding TypeName to Resource was unsuccessful\n";
52     }
53 }
54
55 OCResourceHandle TemphumidResource::getHandle()
56 {
57     return m_resourceHandle;
58 }
59
60 void TemphumidResource::setResourceRepresentation(OCRepresentation &rep)
61 {
62     int tempHumid;
63     int tempTemp;
64
65     rep.getValue("humidity", tempTemp);
66     rep.getValue("temperature", tempHumid);
67
68     m_humid = tempHumid;
69     m_temp = tempTemp;
70
71     cout << "\t\t\t" << "Received representation: " << endl;
72     cout << "\t\t\t\t" << "temp: " << m_humid << endl;
73     cout << "\t\t\t\t" << "humid: " << m_temp << endl;
74 }
75
76 OCRepresentation TemphumidResource::getResourceRepresentation()
77 {
78
79     // This representation is temporaily for soft-sensor-management - name, type, vale
80     m_resourceRep.setValue("temperature", std::to_string(m_temp));
81     m_resourceRep.setValue("humidity", std::to_string(m_humid));
82
83     return m_resourceRep;
84 }
85
86 // Create the instance of the TemphumidResource class
87 TemphumidResource g_myResource;
88
89 void *TestSensorVal(void *param)
90 {
91     (void)param;
92
93     g_myResource.m_temp = 27;
94     g_myResource.m_humid = 48;
95
96     // This function continuously monitors for the changes
97     while (1)
98     {
99         sleep(5);
100
101         if (g_Observation)
102         {
103             g_myResource.m_temp += 1;
104             g_myResource.m_humid -= 1;
105
106             cout << "\ntemp updated to : " << g_myResource.m_temp << endl;
107             cout << "\nhumid updated to : " << g_myResource.m_humid << endl;
108             cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
109                  << endl;
110
111             OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
112
113             if (OC_STACK_NO_OBSERVERS == result)
114             {
115                 cout << "No More observers, stopping notifications" << endl;
116                 g_Observation = 0;
117             }
118         }
119     }
120     return NULL;
121 }
122
123 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
124 {
125     cout << "\tIn Server CPP entity handler:\n";
126
127     auto response = std::make_shared<OC::OCResourceResponse>();
128
129     if (request)
130     {
131         // Get the request type and request flag
132         std::string requestType = request->getRequestType();
133         int requestFlag = request->getRequestHandlerFlag();
134
135         response->setRequestHandle(request->getRequestHandle());
136         response->setResourceHandle(request->getResourceHandle());
137
138         if (requestFlag & RequestHandlerFlag::RequestFlag)
139         {
140             cout << "\t\trequestFlag : Request\n";
141
142             // If the request type is GET
143             if (requestType == "GET")
144             {
145                 cout << "\t\t\trequestType : GET\n";
146
147                 // Check for query params (if any)
148                 // Process query params and do required operations ..
149
150                 // Get the representation of this resource at this point and send it as response
151                 OCRepresentation rep = g_myResource.getResourceRepresentation();
152
153                 if (response)
154                 {
155                     // TODO Error Code
156                     response->setErrorCode(200);
157                     response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
158                 }
159             }
160             else if (requestType == "PUT")
161             {
162                 // TODO PUT request operations
163             }
164             else if (requestType == "POST")
165             {
166                 // TODO POST request operations
167             }
168             else if (requestType == "DELETE")
169             {
170                 // TODO DELETE request operations
171             }
172         }
173
174         if (requestFlag & RequestHandlerFlag::ObserverFlag)
175         {
176             pthread_t threadId;
177
178             cout << "\t\trequestFlag : Observer\n";
179             g_Observation = 1;
180
181             static int startedThread = 0;
182
183             if (!startedThread)
184             {
185                 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
186                 startedThread = 1;
187             }
188         }
189     }
190     else
191     {
192         std::cout << "Request invalid" << std::endl;
193     }
194
195     return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
196 }
197
198 int main()
199 {
200     // Create PlatformConfig object
201     PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
202
203     try
204     {
205         OC::OCPlatform::Configure(cfg);
206
207         OC::OCPlatform::startPresence(60);
208
209         g_myResource.registerResource();
210
211         int input = 0;
212         cout << "Type any key to terminate" << endl;
213         cin >> input;
214
215         OC::OCPlatform::stopPresence();
216     }
217     catch (std::exception e)
218     {
219         cout << e.what() << endl;
220     }
221
222     return 0;
223 }