7af9804260e9e657fae9229553e9a9808f2564ce
[profile/ivi/settings-daemon.git] / tests / settings_test.cpp
1 /**
2  * @file settings_test.cpp
3  *
4  * @brief Settings daemon test.
5  *
6  * @author Ossama Othman @<ossama.othman@@intel.com@>
7  *
8  * @copyright @par
9  * Copyright 2013 Intel Corporation All Rights Reserved.
10  * @par
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  * @par
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  * @par
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA  02110-1301  USA
25  *
26  * @note This header is internal.
27  */
28
29 #include "../lib/config.hpp"
30
31 #include <libwebsockets.h>
32
33 #include <iostream>
34 #include <vector>
35
36
37 namespace
38 {
39   // --------------------------------------------------------------------
40   // libwebsockets callbacks and related data.
41   // --------------------------------------------------------------------
42
43   int
44   send_request(libwebsocket_context * /* context */, libwebsocket * wsi)
45   {
46     static char const request[] =
47       // "{"
48       // "    \"type\": \"test_setting\","
49       // "    \"transactionid\": \"fnord\","
50       // "    \"name\": \"some_arbitrary_method\","
51       // "    \"value\": \"some_arbitrary_value\""
52       // "}";
53
54       //"{"
55       //"    \"type\": \"ethernet\","
56       //"    \"transactionid\": \"fnord\","
57       //"    \"name\": \"is_enabled\","
58       //"    \"value\": null"
59       //"}";
60
61       "{"
62       "    \"type\": \"connman::service\","
63       "    \"transactionid\": \"fnord\","
64       "    \"name\": \"autoconnect\","
65       "    \"value\": { \"path\": \"/net/connman/service/ethernet_eca86bf61ad2_cable\", \"enable\": true }"
66       "}";
67       // "{"
68       // "    \"type\":\"wifi\","
69       // "    \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
70       // "    \"name\":\"scan\","
71       // "    \"value\":null"
72       // "}";
73
74       // "{"
75       // "    \"type\":\"wifi\","
76       // "    \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
77       // "    \"name\":\"connect\","
78       // "    \"value\":{"
79       // "        \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\","
80       // "        \"security\":null,"
81       // "        \"passcode\":null}"
82       // "}";
83
84       // "{"
85       // "    \"type\": \"clock\","
86       // "    \"transactionid\": \"fnord\","
87       // "    \"name\": \"time_updates\","
88       // "    \"value\": \"auto\""
89       // "}";
90
91
92     // libwebsockets wants a sequence of octets (unsigned char *) rather
93     // than characters.
94     typedef std::vector<unsigned char> vector_type;
95
96     static vector_type::size_type const request_len =
97       sizeof(request) - 1;   // Don't include null terminator.
98
99     vector_type::size_type const buf_len =
100       LWS_SEND_BUFFER_PRE_PADDING
101       + request_len
102       + LWS_SEND_BUFFER_POST_PADDING;
103
104     vector_type buf(buf_len);
105     unsigned char * const payload =
106       buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
107
108     // Copy the string into the buffer after libwebsockets pre-padding.
109     std::copy(std::begin(request),
110               std::end(request) - 1, // Don't include null terminator.
111               payload);
112
113     int const count = libwebsocket_write(wsi,
114                                          payload,
115                                          request_len,
116                                          LWS_WRITE_TEXT);
117
118     std::cout << "WROTE " << count << " bytes of " << request_len << "\n";
119
120     if (count < 0 || count < static_cast<int>(request_len))
121       return -1;
122
123     return 0;
124   }
125
126   int
127   callback_settings_test(libwebsocket_context * context,
128                          libwebsocket * wsi,
129                          enum libwebsocket_callback_reasons reason,
130                          void * /* user */,
131                          void * in,
132                          size_t /* len */)
133   {
134     // For LWS_CALLBACK_*_POLL_FD cases.
135     // int   const fd     = static_cast<int>(reinterpret_cast<intptr_t>(in));
136     // short const events = static_cast<short>(len);
137
138     switch(reason) {
139     case LWS_CALLBACK_CLIENT_RECEIVE:
140       // Response has come in from Settings daemon.
141       std::cout << "Response: " << static_cast<char const *>(in) << std::endl;
142       break;
143     case LWS_CALLBACK_CLIENT_ESTABLISHED:
144       libwebsocket_callback_on_writable(context, wsi);
145       break;
146     case LWS_CALLBACK_CLIENT_WRITEABLE:
147       return send_request(context, wsi);
148     default:
149       break;
150     }
151
152     return 0;
153   }
154
155   struct ws_session_data_type
156   {
157   };
158
159   libwebsocket_protocols settings_protocols[] = {
160     {
161       "http-only",
162       callback_settings_test,
163       sizeof(ws_session_data_type),
164       0,
165       nullptr,
166       0
167     },
168     {
169       nullptr,
170       nullptr,
171       0,
172       0,
173       nullptr,
174       0
175     }
176   };
177 }
178
179 // ----------------------------------------------------------------------
180
181 /**
182  * settings_test program entry point.
183  */
184 int main()
185 {
186   lws_context_creation_info info;
187
188   info.port  = 0;
189   info.iface = "lo";
190
191   info.protocols  = settings_protocols;
192   info.extensions = libwebsocket_get_internal_extensions();
193
194   info.ssl_cert_filepath        = nullptr;
195   info.ssl_private_key_filepath = nullptr;
196   info.ssl_ca_filepath          = nullptr;
197   info.ssl_cipher_list          = nullptr;
198
199   info.gid = -1;
200   info.uid = -1;
201
202   info.options = 0;
203
204   info.user = nullptr;
205
206   info.ka_time     = 0;
207   info.ka_probes   = 0;
208   info.ka_interval = 0;
209
210   // lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
211
212   libwebsocket_context * const context =
213     libwebsocket_create_context(&info);
214   if (context == nullptr)
215     exit(EXIT_FAILURE);
216
217   static char const address[] = "localhost";
218   static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT;
219   static int ssl_connection = 0;  // Unencrypted Websocket connection.
220   static char const path[] = "/";
221   static char const host[] = "localhost";
222   static char const origin[] = "localhost";
223   static char const protocol[] = "http-only";
224   static int const ietf_version = -1; // Connect using latest
225                                       // supported protocol.
226
227   libwebsocket * const wsi =
228     libwebsocket_client_connect (context,
229                                  address,
230                                  port, ssl_connection,
231                                  path,
232                                  host,
233                                  origin,
234                                  protocol,
235                                  ietf_version);
236
237   if (wsi == nullptr) {
238     std::cerr << "Unable to connect to settings daemon.\n";
239     libwebsocket_context_destroy(context);
240     exit(EXIT_FAILURE);
241   }
242
243   int n = 0;  
244   while (n >= 0)
245     n = libwebsocket_service(context, 10);
246   
247   libwebsocket_context_destroy(context);
248   return 0;
249 }
250
251
252 // Local Variables:
253 // mode:c++
254 // c-basic-offset:2
255 // indent-tabs-mode: nil
256 // End: