Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-directory / samples / rd_publishingClient.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 #include <iostream>
21
22 #include "OCPlatform.h"
23 #include "OCApi.h"
24 #include "oic_string.h"
25
26 #include "rd_client.h"
27
28 using namespace OC;
29
30 OCResourceHandle g_curResource_t = NULL;
31 OCResourceHandle g_curResource_l = NULL;
32 char rdAddress[MAX_ADDR_STR_SIZE];
33 uint16_t rdPort;
34
35 void registerLocalResources()
36 {
37     std::string resourceURI_thermostat = "/a/thermostat";
38     std::string resourceTypeName_thermostat = "core.thermostat";
39     std::string resourceURI_light = "/a/light";
40     std::string resourceTypeName_light = "core.light";
41     std::string resourceInterface = DEFAULT_INTERFACE;
42     uint8_t resourceProperty = OC_DISCOVERABLE;
43
44     OCStackResult result = OCPlatform::registerResource(g_curResource_t,
45                            resourceURI_thermostat,
46                            resourceTypeName_thermostat,
47                            resourceInterface,
48                            NULL,
49                            resourceProperty);
50
51     if (OC_STACK_OK != result)
52     {
53         throw std::runtime_error(
54             std::string("Device Resource failed to start") + std::to_string(result));
55     }
56
57     result = OCPlatform::registerResource(g_curResource_l,
58                                           resourceURI_light,
59                                           resourceTypeName_light,
60                                           resourceInterface,
61                                           NULL,
62                                           resourceProperty);
63
64     if (OC_STACK_OK != result)
65     {
66         throw std::runtime_error(
67             std::string("Device Resource failed to start") + std::to_string(result));
68     }
69 }
70
71 void printHelp()
72 {
73     std::cout << std::endl;
74     std::cout << "********************************************" << std::endl;
75     std::cout << "*  method Type : 1 - Discover RD           *" << std::endl;
76     std::cout << "*  method Type : 2 - Publish               *" << std::endl;
77     std::cout << "*  method Type : 3 - Update                *" << std::endl;
78     std::cout << "*  method Type : 4 - Delete                *" << std::endl;
79     std::cout << "*  method Type : 5 - Status                *" << std::endl;
80     std::cout << "********************************************" << std::endl;
81     std::cout << std::endl;
82 }
83
84 int biasFactorCB(char addr[MAX_ADDR_STR_SIZE], uint16_t port)
85 {
86     OICStrcpy(rdAddress, MAX_ADDR_STR_SIZE, addr);
87     rdPort = port;
88     std::cout << "RD Address is : " <<  addr << ":" << port << std::endl;
89     return 0;
90 }
91
92 int main()
93 {
94     int in;
95     PlatformConfig cfg;
96
97     OCPlatform::Configure(cfg);
98
99     std::cout << "Created Platform..." << std::endl;
100
101     registerLocalResources();
102
103     while (1)
104     {
105         sleep(2);
106
107         if (g_curResource_t == NULL || g_curResource_l == NULL)
108         {
109             continue;
110         }
111         printHelp();
112
113         in = 0;
114         std::cin >> in;
115
116         if (std::cin.fail())
117         {
118             std::cin.clear();
119             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
120             std::cout << "Invalid input type, please try again" << std::endl;
121             continue;
122         }
123
124         try
125         {
126             switch ((int)in)
127             {
128                 case 1:
129                     OCRDDiscover(biasFactorCB);
130                     break;
131                 case 2:
132                     OCRDPublish(rdAddress, rdPort, 2, g_curResource_t, g_curResource_l);
133                     break;
134                 case 3:
135                     break;
136                 default:
137                     std::cout << "Invalid input, please try again" << std::endl;
138                     break;
139             }
140         }
141         catch (OCException e)
142         {
143             std::cout << "Caught OCException [Code: " << e.code() << " Reason: " << e.reason() << std::endl;
144         }
145     }
146     return 0;
147 }