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