iotivity 0.9.0
[platform/upstream/iotivity.git] / examples / OICMiddle / OICMiddle.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation.
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 //
22 // OICMiddle.cpp : OIC demo application for Minnowboard
23 //
24
25 #include <unistd.h>
26
27 #include "OICMiddle.h"
28 #include "WrapResource.h"
29 #include "Client.h"
30 #include "Server.h"
31 #include "LineInput.h"
32 #include "RestInput.h"
33
34 class Middle middle;    // one and only
35
36 Middle::Middle() :
37     m_appType(AT_None),
38     m_useLineInput(false),
39     m_useRestInput(false),
40     m_client(nullptr),
41     m_server(nullptr),
42     m_lineInput(nullptr),
43     m_restInput(nullptr)
44 {
45 }
46
47 void Middle::init()
48 {
49
50 }
51
52 void Middle::run(int argc, char* argv[])
53 {
54     parseCommandLineOptions(argc, argv);
55
56     startPlatform();
57
58     if (m_appType & AT_Client) {
59         m_client = new MiddleClient();
60         m_client->init();
61     }
62
63     m_lineInput = new LineInput(m_client);
64
65     if (m_appType & AT_Server) {
66         m_server = new MiddleServer();
67         m_server->init();
68     }
69     if (m_useRestInput) {
70         if (!m_server) {
71             m_server = new MiddleServer();
72             m_server->init();
73         }
74         m_restInput = new RestInput(m_lineInput);
75         m_restInput->init();
76     }
77     if (m_useLineInput) {
78         if (m_server) {
79             m_lineInput->setServer(m_server);
80         }
81         m_lineInput->run();
82     } else {
83         while (true)
84             sleep(1);
85     }
86 }
87
88 void Middle::startPlatform()
89 {
90     uint16_t port = 0;
91     //std::string ipaddr = INADDR_ANY;
92     std::string ipaddr = "0.0.0.0";
93
94     PlatformConfig cfg { ServiceType::InProc, ModeType::Both,
95                   ipaddr, port, QualityOfService::LowQos};
96
97     OC::OCPlatform::Configure(cfg);
98 }
99
100 void Middle::provideHelp()
101 {
102     static const char usage[] = "\nUsage:  IOCMiddle args\n"
103                 "    where args may include any of these:\n"
104                 "\t-client      Run OIC client\n"
105                 "\t-server      Run OIC server\n"
106                 "\t-both        Run OIC client and server\n"
107                 "\t-console     Run console line interpreter\n"
108                 "\t-rest        Run ReST server\n"
109                 "\t-hue addr    Enable Hue resources on bridge at addr\n"
110                 "\t-help        Show Usage again\n"
111                 "Any combination of the above is okay.\n\n";
112     cout << usage;
113 }
114
115 bool Middle::parseCommandLineOptions(int argc, char *argv[])
116 {
117     bool any = false;
118
119     for (int i = 1; i < argc; i++) {
120         if (argv[i] == string("-server")) {
121             middle.m_appType = AT_Server; any = true;
122         } else if (argv[i] == string("-client")) {
123             middle.m_appType = AT_Client; any = true;
124         } else if (argv[i] == string("-both")) {
125             middle.m_appType = AT_Both; any = true;
126         } else if (argv[i] == string("-console")) {
127             middle.m_useLineInput = true; any = true;
128         } else if (argv[i] == string("-rest")) {
129             middle.m_useRestInput = true; any = true;
130         } else if (argv[i] == string("-hue")) {
131             if (i + 1 < argc && argv[i + 1][0] != '-') {
132                 m_hueAddr = argv[++i];
133                 any = true;
134             }
135         } else if (argv[i] == string("-help")) {
136                 any = false;
137             break;
138         } else {
139             std::cerr << "Not enough or invalid arguments, please try again.\n";
140             exit(1);
141         }
142     }
143     if (!any)
144             provideHelp();
145     return true;
146 }
147
148 int main(int argc, char* argv[])
149 {
150     middle.run(argc, argv);
151     return 0;
152 }