Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupsyncaction / speaker.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 <string>
22 #include <cstdlib>
23 #include <pthread.h>
24 #include "OCPlatform.h"
25 #include "OCApi.h"
26 #include "ThingsManager.h"
27
28 using namespace OC;
29
30 OCEntityHandlerResult speakerEntityHandler(const std::shared_ptr< OCResourceRequest > request)
31 {
32     cout << "speakerEntityHandler:\n";
33
34     if (request)
35     {
36         // Get the request type and request flag
37         std::string requestType = request->getRequestType();
38         int requestFlag = request->getRequestHandlerFlag();
39         std::string action;
40
41         if (requestFlag == RequestHandlerFlag::RequestFlag)
42         {
43             cout << "\trequestFlag : Request\n";
44
45             OCRepresentation rp = request->getResourceRepresentation();
46
47             // If the request type is GET
48             if (requestType == "GET")
49             {
50                 cout << "\t\trequestType : GET\n";
51             }
52             else if (requestType == "PUT")
53             {
54                 cout << "\t\trequestType : PUT\n";
55
56                 action = rp.getValue< std::string >("volume");
57                 cout << "\t\t\tvolume : " << action << endl;
58             }
59             else if (requestType == "POST")
60             {
61                 cout << "\t\trequestType : POST\n";
62             }
63             else if (requestType == "DELETE")
64             {
65                 cout << "\t\trequestType : DELETE\n";
66             }
67         }
68         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
69         {
70             cout << "\trequestFlag : Observer\n";
71         }
72     }
73     else
74     {
75         cout << "Request invalid" << endl;
76     }
77
78     return OC_EH_OK;
79 }
80
81 int main(int argc, char* argv[])
82 {
83
84     // Create PlatformConfig object
85     PlatformConfig cfg
86     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
87
88     OCPlatform::Configure(cfg);
89
90     OCPlatform::startPresence(30);
91
92     int selectedMenu;
93     OCStackResult result;
94     OCResourceHandle speakerResourceHandle = NULL;
95
96     try
97     {
98         while (true)
99         {
100             // some operations
101             cout << "(1) CREATE SPEAKER | (2) DELETE SPEAKER" << endl;
102
103             std::cin >> selectedMenu;
104
105             if (selectedMenu == 1)
106             {
107                 if (speakerResourceHandle)
108                 {
109                     cout << "Speaker resource is registered already." << endl;
110                     continue;
111                 }
112
113                 std::string resourceURi = "/core/speaker";
114                 std::string resourceTypeName = "core.speaker";
115
116                 std::string resourceInterface = DEFAULT_INTERFACE;
117
118                 result = OCPlatform::registerResource(speakerResourceHandle, resourceURi,
119                         resourceTypeName, resourceInterface, speakerEntityHandler,
120                         OC_DISCOVERABLE | OC_OBSERVABLE);
121                 if (OC_STACK_OK == result)
122                 {
123                     cout << "To register speaker resource was successful\n";
124                 }
125                 else
126                 {
127                     cout << "To register speaker resource was unsuccessful\n";
128                 }
129             }
130             else if (selectedMenu == 2)
131             {
132                 if (NULL == speakerResourceHandle)
133                 {
134                     cout
135                             << "Error! No resource to unregister. Register resource first!"
136                             << endl;
137                     continue;
138                 }
139
140                 result = OCPlatform::unregisterResource(speakerResourceHandle);
141                 if (OC_STACK_OK == result)
142                 {
143                     cout << "To unregister speaker resource was successful\n";
144                 }
145                 else
146                 {
147                     cout << "To unregister speaker resource was unsuccessful\n";
148                 }
149
150                 speakerResourceHandle = NULL;
151             }
152         }
153     }
154     catch (OCException& e)
155     {
156          std::cout << "Exception: " << e.what() << std::endl;
157     }
158
159     return 0;
160 }
161