9a898a3db02edb034082d71ff90f678a79dcfbd7
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupsyncaction / musicplayer.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 mpEntityHandler(const std::shared_ptr< OCResourceRequest > request)
31 {
32     cout << "mpEntityHandler:\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::InitFlag)
42         {
43             cout << "\trequestFlag : Init\n";
44
45             // entity handler to perform resource initialization operations
46         }
47         else if (requestFlag == RequestHandlerFlag::RequestFlag)
48         {
49             cout << "\trequestFlag : Request\n";
50
51             OCRepresentation rp = request->getResourceRepresentation();
52
53             // If the request type is GET
54             if (requestType == "GET")
55             {
56                 cout << "\t\trequestType : GET\n";
57             }
58             else if (requestType == "PUT")
59             {
60                 cout << "\t\trequestType : PUT\n";
61
62                 action = rp.getValue< std::string >("play");
63                 cout << "\t\t\tplay : " << action << endl;
64             }
65             else if (requestType == "POST")
66             {
67                 cout << "\t\trequestType : POST\n";
68             }
69             else if (requestType == "DELETE")
70             {
71                 cout << "\t\trequestType : DELETE\n";
72             }
73         }
74         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
75         {
76             cout << "\trequestFlag : Observer\n";
77         }
78     }
79     else
80     {
81         cout << "Request invalid" << endl;
82     }
83
84     return OC_EH_OK;
85 }
86
87 int main(int argc, char* argv[])
88 {
89     // Create PlatformConfig object
90     PlatformConfig cfg
91     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
92
93     OCPlatform::Configure(cfg);
94
95     int selectedMenu;
96     OCStackResult result;
97     OCResourceHandle mpResourceHandle = NULL;
98
99     try
100     {
101         while (true)
102         {
103             // some operations
104             cout << "(1) CREATE MUSIC PLAYER | (2) DELETE MUSIC PLAYER" << endl;
105
106             std::cin >> selectedMenu;
107
108             if (selectedMenu == 1)
109             {
110                 if (mpResourceHandle)
111                 {
112                     cout << "Music player resource is registered already." << endl;
113                     continue;
114                 }
115
116                 std::string resourceURi = "/core/musicplayer";
117                 std::string resourceTypeName = "core.musicplayer";
118                 std::string resourceInterface = DEFAULT_INTERFACE;
119
120                 result = OCPlatform::registerResource(mpResourceHandle, resourceURi,
121                         resourceTypeName, resourceInterface, mpEntityHandler,
122                         OC_DISCOVERABLE | OC_OBSERVABLE);
123                 if (OC_STACK_OK == result)
124                 {
125                     cout << "To register music player resource was successful\n";
126                 }
127                 else
128                 {
129                     cout << "To register music player resource was unsuccessful\n";
130                 }
131             }
132             else if (selectedMenu == 2)
133             {
134                 if (NULL == mpResourceHandle)
135                 {
136                     cout
137                             << "Error! No resource to unregister. Register resource first!"
138                             << endl;
139                     continue;
140                 }
141
142                 result = OCPlatform::unregisterResource(mpResourceHandle);
143                 if (OC_STACK_OK == result)
144                 {
145                     cout << "To unregister music player resource was successful\n";
146                 }
147                 else
148                 {
149                     cout << "To unregister music player resource was unsuccessful\n";
150                 }
151
152                 mpResourceHandle = NULL;
153             }
154         }
155     }
156     catch (OCException& e)
157     {
158         //log(e.what());
159     }
160
161     return 0;
162 }
163