Modifying version number for building on tizen 3.0
[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::InitFlag)
131         {
132             cout << "\t\trequestFlag : Init\n";
133
134             // entity handler to perform resource initialization operations
135         }
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                 // PUT request operations
162             }
163             else if (requestType == "POST")
164             {
165                 // POST request operations
166             }
167             else if (requestType == "DELETE")
168             {
169                 // 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 }