TIVI-1924: Initial commit of IVI settings daemon.
[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\":\"wifi\","
63       // "    \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
64       // "    \"name\":\"scan\","
65       // "    \"value\":null"
66       // "}";
67
68       // "{"
69       // "    \"type\":\"wifi\","
70       // "    \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
71       // "    \"name\":\"connect\","
72       // "    \"value\":{"
73       // "        \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\","
74       // "        \"security\":null,"
75       // "        \"passcode\":null}"
76       // "}";
77
78       // "{"
79       // "    \"type\": \"clock\","
80       // "    \"transactionid\": \"fnord\","
81       // "    \"name\": \"time_updates\","
82       // "    \"value\": \"auto\""
83       // "}";
84
85
86     // libwebsockets wants a sequence of octets (unsigned char *) rather
87     // than characters.
88     typedef std::vector<unsigned char> vector_type;
89
90     static vector_type::size_type const request_len =
91       sizeof(request) - 1;   // Don't include null terminator.
92
93     vector_type::size_type const buf_len =
94       LWS_SEND_BUFFER_PRE_PADDING
95       + request_len
96       + LWS_SEND_BUFFER_POST_PADDING;
97
98     vector_type buf(buf_len);
99     unsigned char * const payload =
100       buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
101
102     // Copy the string into the buffer after libwebsockets pre-padding.
103     std::copy(std::begin(request),
104               std::end(request) - 1, // Don't include null terminator.
105               payload);
106
107     int const count = libwebsocket_write(wsi,
108                                          payload,
109                                          request_len,
110                                          LWS_WRITE_TEXT);
111
112     std::cout << "WROTE " << count << " bytes of " << request_len << "\n";
113
114     if (count < 0 || count < static_cast<int>(request_len))
115       return -1;
116
117     return 0;
118   }
119
120   int
121   callback_settings_test(libwebsocket_context * context,
122                          libwebsocket * wsi,
123                          enum libwebsocket_callback_reasons reason,
124                          void * /* user */,
125                          void * in,
126                          size_t /* len */)
127   {
128     // For LWS_CALLBACK_*_POLL_FD cases.
129     // int   const fd     = static_cast<int>(reinterpret_cast<intptr_t>(in));
130     // short const events = static_cast<short>(len);
131
132     switch(reason) {
133     case LWS_CALLBACK_CLIENT_RECEIVE:
134       // Response has come in from Settings daemon.
135       std::cout << "Response: " << static_cast<char const *>(in) << std::endl;
136       break;
137     case LWS_CALLBACK_CLIENT_ESTABLISHED:
138       libwebsocket_callback_on_writable(context, wsi);
139       break;
140     case LWS_CALLBACK_CLIENT_WRITEABLE:
141       return send_request(context, wsi);
142     default:
143       break;
144     }
145
146     return 0;
147   }
148
149   struct ws_session_data_type
150   {
151   };
152
153   libwebsocket_protocols settings_protocols[] = {
154     {
155       "http-only",
156       callback_settings_test,
157       sizeof(ws_session_data_type),
158       0,
159       nullptr,
160       0
161     },
162     {
163       nullptr,
164       nullptr,
165       0,
166       0,
167       nullptr,
168       0
169     }
170   };
171 }
172
173 // ----------------------------------------------------------------------
174
175 /**
176  * settings_test program entry point.
177  */
178 int main()
179 {
180   lws_context_creation_info info;
181
182   info.port  = 0;
183   info.iface = "lo";
184
185   info.protocols  = settings_protocols;
186   info.extensions = libwebsocket_get_internal_extensions();
187
188   info.ssl_cert_filepath        = nullptr;
189   info.ssl_private_key_filepath = nullptr;
190   info.ssl_ca_filepath          = nullptr;
191   info.ssl_cipher_list          = nullptr;
192
193   info.gid = -1;
194   info.uid = -1;
195
196   info.options = 0;
197
198   info.user = nullptr;
199
200   info.ka_time     = 0;
201   info.ka_probes   = 0;
202   info.ka_interval = 0;
203
204   // lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
205
206   libwebsocket_context * const context =
207     libwebsocket_create_context(&info);
208   if (context == nullptr)
209     exit(EXIT_FAILURE);
210
211   static char const address[] = "localhost";
212   static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT;
213   static int ssl_connection = 0;  // Unencrypted Websocket connection.
214   static char const path[] = "/";
215   static char const host[] = "localhost";
216   static char const origin[] = "localhost";
217   static char const protocol[] = "http-only";
218   static int const ietf_version = -1; // Connect using latest
219                                       // supported protocol.
220
221   libwebsocket * const wsi =
222     libwebsocket_client_connect (context,
223                                  address,
224                                  port, ssl_connection,
225                                  path,
226                                  host,
227                                  origin,
228                                  protocol,
229                                  ietf_version);
230
231   if (wsi == nullptr) {
232     std::cerr << "Unable to connect to settings daemon.\n";
233     libwebsocket_context_destroy(context);
234     exit(EXIT_FAILURE);
235   }
236
237   int n = 0;  
238   while (n >= 0)
239     n = libwebsocket_service(context, 10);
240   
241   libwebsocket_context_destroy(context);
242   return 0;
243 }
244
245
246 // Local Variables:
247 // mode:c++
248 // c-basic-offset:2
249 // indent-tabs-mode: nil
250 // End: