2897af7a0326d88f3b267d82107602132897efef
[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     try
102     {
103         registerLocalResources();
104     }
105     catch (std::runtime_error e)
106     {
107         std::cout << "Caught OCException [Code: " << e.what() << std::endl;
108     }
109
110     while (1)
111     {
112         sleep(2);
113
114         if (g_curResource_t == NULL || g_curResource_l == NULL)
115         {
116             continue;
117         }
118         printHelp();
119
120         in = 0;
121         std::cin >> in;
122
123         if (std::cin.fail())
124         {
125             std::cin.clear();
126             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
127             std::cout << "Invalid input type, please try again" << std::endl;
128             continue;
129         }
130
131         try
132         {
133             switch ((int)in)
134             {
135                 case 1:
136                     OCRDDiscover(biasFactorCB);
137                     break;
138                 case 2:
139                     OCRDPublish(rdAddress, rdPort, 2, g_curResource_t, g_curResource_l);
140                     break;
141                 case 3:
142                     break;
143                 default:
144                     std::cout << "Invalid input, please try again" << std::endl;
145                     break;
146             }
147         }
148         catch (OCException e)
149         {
150             std::cout << "Caught OCException [Code: " << e.code() << " Reason: " << e.reason() << std::endl;
151         }
152     }
153     return 0;
154 }