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