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