Added Haltest for wifi-mesh-manager
[platform/core/connectivity/wifi-mesh-manager.git] / haltest / wmesh.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "wmesh.h"
24
25 Wmesh::Wmesh()
26 {
27         Create();
28 }
29
30 Wmesh::~Wmesh()
31 {
32         Destroy();
33 }
34 error_e Wmesh::Scan(void)
35 {
36         GVariant *message = NULL;
37         error_e error = ERROR_NONE;
38
39         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
40                 WIFI_MESH_MGR_PATH,
41                 WIFI_MESH_MGR_INTERFACE,
42                 WIFI_MESH_MGR_METHOD_SCAN,
43                 NULL,
44                 &error);
45
46         if (message == NULL) {
47                 GLOGD("Failed to invoke dbus method");
48                 return error;
49         }
50
51         GLOGD("Succeeded to perform scan");
52
53         return ERROR_NONE;
54 }
55 error_e Wmesh::SpecificScan(char *mesh_id, int channel)
56 {
57         GVariant *message = NULL;
58         error_e error = ERROR_NONE;
59
60         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
61                 WIFI_MESH_MGR_PATH,
62                 WIFI_MESH_MGR_INTERFACE,
63                 WIFI_MESH_MGR_METHOD_SPECIFIC_SCAN,
64                 g_variant_new("(si)", mesh_id, channel),
65                 &error);
66
67         if (message == NULL) {
68                 GLOGD("Failed to invoke dbus method");
69                 return error;
70         }
71
72         GLOGD("Succeeded to perform specific_scan");
73
74         return ERROR_NONE;
75 }
76 error_e Wmesh::CancelScan(void)
77 {
78         GVariant *message = NULL;
79         error_e error = ERROR_NONE;
80
81         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
82                 WIFI_MESH_MGR_PATH,
83                 WIFI_MESH_MGR_INTERFACE,
84                 WIFI_MESH_MGR_METHOD_CANCEL_SCAN,
85                 NULL,
86                 &error);
87
88         if (message == NULL) {
89                 GLOGD("Failed to invoke dbus method");
90                 return error;
91         }
92
93         GLOGD("Succeeded to perform cancel_scan");
94
95         return ERROR_NONE;
96 }
97
98 error_e Wmesh::GetFoundMeshNetworks(void)
99 {
100         GVariant *message = NULL;
101         error_e error = ERROR_NONE;
102
103         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
104                 WIFI_MESH_MGR_PATH,
105                 WIFI_MESH_MGR_INTERFACE,
106                 WIFI_MESH_MGR_METHOD_GET_FOUND_MESH_NETWORKS,
107                 NULL,
108                 &error);
109
110         if (message == NULL) {
111                 GLOGD("Failed to invoke dbus method");
112                 return error;
113         }
114
115         GLOGD("Succeeded to perform get_found_mesh_networks");
116
117         return ERROR_NONE;
118 }
119 error_e Wmesh::GetConnectedPeers(void)
120 {
121         GVariant *message = NULL;
122         error_e error = ERROR_NONE;
123
124         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
125                 WIFI_MESH_MGR_PATH,
126                 WIFI_MESH_MGR_INTERFACE,
127                 WIFI_MESH_MGR_METHOD_GET_CONNECTED_PEERS,
128                 NULL,
129                 &error);
130
131         if (message == NULL) {
132                 GLOGD("Failed to invoke dbus method");
133                 return error;
134         }
135
136         GLOGD("Succeeded to perform get_connceted_peers");
137
138         return ERROR_NONE;
139 }
140 error_e Wmesh::EnableMesh(void)
141 {
142         GVariant *message = NULL;
143         error_e error = ERROR_NONE;
144
145         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
146                 WIFI_MESH_MGR_PATH,
147                 WIFI_MESH_MGR_INTERFACE,
148                 WIFI_MESH_MGR_METHOD_ENABLE_MESH,
149                 NULL,
150                 &error);
151
152         if (message == NULL) {
153                 GLOGD("Failed to invoke dbus method");
154                 return error;
155         }
156
157         GLOGD("Succeeded to perform enable_mesh");
158
159         return ERROR_NONE;
160 }
161 error_e Wmesh::DisableMesh(void)
162 {
163         GVariant *message = NULL;
164         error_e error = ERROR_NONE;
165
166         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
167                 WIFI_MESH_MGR_PATH,
168                 WIFI_MESH_MGR_INTERFACE,
169                 WIFI_MESH_MGR_METHOD_DISABLE_MESH,
170                 NULL,
171                 &error);
172
173         if (message == NULL) {
174                 GLOGD("Failed to invoke dbus method");
175                 return error;
176         }
177
178         GLOGD("Succeeded to perform disable_mesh");
179
180         return ERROR_NONE;
181 }
182 error_e Wmesh::IsMeshEnabled(void)
183 {
184         GVariant *message = NULL;
185         error_e error = ERROR_NONE;
186
187         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
188                 WIFI_MESH_MGR_PATH,
189                 WIFI_MESH_MGR_INTERFACE,
190                 WIFI_MESH_MGR_METHOD_IS_MESH_ENABLED,
191                 NULL,
192                 &error);
193
194         if (message == NULL) {
195                 GLOGD("Failed to invoke dbus method");
196                 return error;
197         }
198
199         GLOGD("Succeeded to perform is_mesh_enabled");
200
201         return ERROR_NONE;
202 }
203 error_e Wmesh::IsJoined(void)
204 {
205         GVariant *message = NULL;
206         error_e error = ERROR_NONE;
207
208         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
209                 WIFI_MESH_MGR_PATH,
210                 WIFI_MESH_MGR_INTERFACE,
211                 WIFI_MESH_MGR_METHOD_IS_JOINED,
212                 NULL,
213                 &error);
214
215         if (message == NULL) {
216                 GLOGD("Failed to invoke dbus method");
217                 return error;
218         }
219
220         GLOGD("Succeeded to perform is_joined");
221
222         return ERROR_NONE;
223 }
224 error_e Wmesh::GetJoinedMeshNetwork(void)
225 {
226         GVariant *message = NULL;
227         error_e error = ERROR_NONE;
228
229         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
230                 WIFI_MESH_MGR_PATH,
231                 WIFI_MESH_MGR_INTERFACE,
232                 WIFI_MESH_MGR_METHOD_GET_JOINED_MESH_NETWORK,
233                 NULL,
234                 &error);
235
236         if (message == NULL) {
237                 GLOGD("Failed to invoke dbus method");
238                 return error;
239         }
240
241         GLOGD("Succeeded to perform get_joined_mesh_network");
242
243         return ERROR_NONE;
244 }
245 error_e Wmesh::SetGate(bool gate_announce, int hwmp_root_mode, bool stp)
246 {
247         GVariant *message = NULL;
248         error_e error = ERROR_NONE;
249
250         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
251                 WIFI_MESH_MGR_PATH,
252                 WIFI_MESH_MGR_INTERFACE,
253                 WIFI_MESH_MGR_METHOD_SET_GATE,
254                 g_variant_new("(bqq)", gate_announce, hwmp_root_mode, stp),
255                 &error);
256
257         if (message == NULL) {
258                 GLOGD("Failed to invoke dbus method");
259                 return error;
260         }
261
262         GLOGD("Succeeded to perform set_gate");
263
264         return ERROR_NONE;
265 }
266 error_e Wmesh::UnsetGate(void)
267 {
268         GVariant *message = NULL;
269         error_e error = ERROR_NONE;
270
271         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
272                 WIFI_MESH_MGR_PATH,
273                 WIFI_MESH_MGR_INTERFACE,
274                 WIFI_MESH_MGR_METHOD_UNSET_GATE,
275                 NULL,
276                 &error);
277
278         if (message == NULL) {
279                 GLOGD("Failed to invoke dbus method");
280                 return error;
281         }
282
283         GLOGD("Succeeded to perform unset_gate");
284
285         return ERROR_NONE;
286 }
287 error_e Wmesh::SetSoftap(char *ssid, char *passphrase, int channel,
288                 int visibility, int max_stations, int security)
289 {
290         GVariant *message = NULL;
291         error_e error = ERROR_NONE;
292         char buf[2] = {0, };
293
294         if (channel <= 13) {
295                 buf[0] = 'g';
296                 buf[1] = 0;
297         } else {
298                 buf[0] = 'a';
299                 buf[1] = 0;
300         }
301
302         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
303                 WIFI_MESH_MGR_PATH,
304                 WIFI_MESH_MGR_INTERFACE,
305                 WIFI_MESH_MGR_METHOD_SET_SOFTAP,
306                 g_variant_new("(sssiiii)", ssid, passphrase, buf,
307                         channel, visibility, max_stations, security),
308                 &error);
309
310         if (message == NULL) {
311                 GLOGD("Failed to invoke dbus method");
312                 return error;
313         }
314
315         GLOGD("Succeeded to perform set_softap");
316
317         return ERROR_NONE;
318 }
319 error_e Wmesh::GetSoftap(void)
320 {
321         GVariant *message = NULL;
322         error_e error = ERROR_NONE;
323
324         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
325                 WIFI_MESH_MGR_PATH,
326                 WIFI_MESH_MGR_INTERFACE,
327                 WIFI_MESH_MGR_METHOD_GET_SOFTAP,
328                 NULL,
329                 &error);
330
331         if (message == NULL) {
332                 GLOGD("Failed to invoke dbus method");
333                 return error;
334         }
335
336         GLOGD("Succeeded to perform get_softap");
337
338         return ERROR_NONE;
339 }
340 error_e Wmesh::EnableSoftap(void)
341 {
342         GVariant *message = NULL;
343         error_e error = ERROR_NONE;
344
345         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
346                 WIFI_MESH_MGR_PATH,
347                 WIFI_MESH_MGR_INTERFACE,
348                 WIFI_MESH_MGR_METHOD_ENABLE_SOFTAP,
349                 NULL,
350                 &error);
351
352         if (message == NULL) {
353                 GLOGD("Failed to invoke dbus method");
354                 return error;
355         }
356
357         GLOGD("Succeeded to perform enabe_softap");
358
359         return ERROR_NONE;
360 }
361 error_e Wmesh::DisableSoftap(void)
362 {
363         GVariant *message = NULL;
364         error_e error = ERROR_NONE;
365
366         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
367                 WIFI_MESH_MGR_PATH,
368                 WIFI_MESH_MGR_INTERFACE,
369                 WIFI_MESH_MGR_METHOD_DISABLE_SOFTAP,
370                 NULL,
371                 &error);
372
373         if (message == NULL) {
374                 GLOGD("Failed to invoke dbus method");
375                 return error;
376         }
377
378         GLOGD("Succeeded to perform disable_softap");
379
380         return ERROR_NONE;
381 }
382 error_e Wmesh::IsSoftapEnabled(void)
383 {
384         GVariant *message = NULL;
385         error_e error = ERROR_NONE;
386
387         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
388                 WIFI_MESH_MGR_PATH,
389                 WIFI_MESH_MGR_INTERFACE,
390                 WIFI_MESH_MGR_METHOD_IS_SOFTAP_ENABLED,
391                 NULL,
392                 &error);
393
394         if (message == NULL) {
395                 GLOGD("Failed to invoke dbus method");
396                 return error;
397         }
398
399         GLOGD("Succeeded to perform is_softap_enabled");
400
401         return ERROR_NONE;
402 }
403 error_e Wmesh::CreateMeshNetwork(char *mesh_id, int channel, int security,
404                 int pmf)
405 {
406         GVariant *message = NULL;
407         error_e error = ERROR_NONE;
408
409         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
410                 WIFI_MESH_MGR_PATH,
411                 WIFI_MESH_MGR_INTERFACE,
412                 WIFI_MESH_MGR_METHOD_CREATE_MESH_NETWORK,
413                 g_variant_new("(siii)", mesh_id, channel, security, pmf),
414                 &error);
415
416         if (message == NULL) {
417                 GLOGD("Failed to invoke dbus method");
418                 return error;
419         }
420
421         GLOGD("Succeeded to perform create_mesh_network");
422
423         return ERROR_NONE;
424 }
425 error_e Wmesh::ConnectMeshNetwork(char *mesh_id, int channel, int security,
426                 char *passphrase)
427 {
428         GVariant *message = NULL;
429         error_e error = ERROR_NONE;
430
431         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
432                 WIFI_MESH_MGR_PATH,
433                 WIFI_MESH_MGR_INTERFACE,
434                 WIFI_MESH_MGR_METHOD_CONNECT_MESH_NETWORK,
435                 g_variant_new("(siis)", mesh_id, channel, security, passphrase),
436                 &error);
437
438         if (message == NULL) {
439                 GLOGD("Failed to invoke dbus method");
440                 return error;
441         }
442
443         GLOGD("Succeeded to perform connect_mesh_network");
444
445         return ERROR_NONE;
446 }
447 error_e Wmesh::DisconnectMeshNetwork(char *mesh_id, int channel, int security)
448 {
449         GVariant *message = NULL;
450         error_e error = ERROR_NONE;
451
452         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
453                 WIFI_MESH_MGR_PATH,
454                 WIFI_MESH_MGR_INTERFACE,
455                 WIFI_MESH_MGR_METHOD_DISCONNECT_MESH_NETWORK,
456                 g_variant_new("(sii)", mesh_id, channel, security),
457                 &error);
458
459         if (message == NULL) {
460                 GLOGD("Failed to invoke dbus method");
461                 return error;
462         }
463
464         GLOGD("Succeeded to perform disconnect_mesh_network");
465
466         return ERROR_NONE;
467 }
468 error_e Wmesh::ForgetMeshNetwork(char *mesh_id, int channel, int security)
469 {
470         GVariant *message = NULL;
471         error_e error = ERROR_NONE;
472
473         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
474                 WIFI_MESH_MGR_PATH,
475                 WIFI_MESH_MGR_INTERFACE,
476                 WIFI_MESH_MGR_METHOD_FORGET_MESH_NETWORK,
477                 g_variant_new("(sii)", mesh_id, channel, security),
478                 &error);
479
480         if (message == NULL) {
481                 GLOGD("Failed to invoke dbus method");
482                 return error;
483         }
484
485         GLOGD("Succeeded to perform forget_mesh_network");
486
487         return ERROR_NONE;
488 }
489 error_e Wmesh::SetInterfaces(char *mesh, char *gate, char *softap)
490 {
491         GVariant *message = NULL;
492         error_e error = ERROR_NONE;
493
494         message = InvokeMethod(WIFI_MESH_MGR_SERVICE,
495                 WIFI_MESH_MGR_PATH,
496                 WIFI_MESH_MGR_INTERFACE,
497                 WIFI_MESH_MGR_METHOD_SET_INTERFACES,
498                 g_variant_new("(sss)", mesh, gate, softap),
499                 &error);
500
501         if (message == NULL) {
502                 GLOGD("Failed to invoke dbus method");
503                 return error;
504         }
505
506         GLOGD("Succeeded to perform set_interfaces");
507
508         return ERROR_NONE;
509 }
510 error_e Wmesh::GetStationInfo(int sta_type)
511 {
512         bool is_started;
513         GVariant *msg1 = NULL;
514         GVariant *msg2 = NULL;
515         error_e error = ERROR_NONE;
516
517         msg1 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
518                 WIFI_MESH_MGR_PATH,
519                 WIFI_MESH_MGR_INTERFACE,
520                 WIFI_MESH_MGR_METHOD_IS_MESH_ENABLED,
521                 NULL,
522                 &error);
523
524         if (msg1 == NULL) {
525                 GLOGD("Failed to invoke dbus method");
526                 return error;
527         }
528
529         g_variant_get(msg1, "(b)", &is_started);
530         g_variant_unref(msg1);
531         if (TRUE == is_started) {
532                 msg2 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
533                         WIFI_MESH_MGR_PATH,
534                         WIFI_MESH_MGR_INTERFACE,
535                         WIFI_MESH_MGR_METHOD_GET_STATION_INFO,
536                         g_variant_new("(i)", sta_type),
537                         &error);
538
539                 if (msg2 == NULL) {
540                         GLOGD("Failed to invoke dbus method");
541                         return error;
542                 } else {
543                         g_variant_unref(msg2);
544                 }
545
546                 GLOGD("Succeeded to perform get_station_info");
547         }
548
549         return ERROR_NONE;
550 }
551 error_e Wmesh::GetMpathInfo(void)
552 {
553         bool is_started;
554         GVariant *msg1 = NULL;
555         GVariant *msg2 = NULL;
556         error_e error = ERROR_NONE;
557
558         msg1 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
559                 WIFI_MESH_MGR_PATH,
560                 WIFI_MESH_MGR_INTERFACE,
561                 WIFI_MESH_MGR_METHOD_IS_MESH_ENABLED,
562                 NULL,
563                 &error);
564
565         if (msg1 == NULL) {
566                 GLOGD("Failed to invoke dbus method");
567                 return error;
568         }
569
570         g_variant_get(msg1, "(b)", &is_started);
571         g_variant_unref(msg1);
572         if (TRUE == is_started) {
573                 msg2 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
574                                 WIFI_MESH_MGR_PATH,
575                                 WIFI_MESH_MGR_INTERFACE,
576                                 WIFI_MESH_MGR_METHOD_GET_MPATH_INFO,
577                                 NULL,
578                                 &error);
579
580                 if (msg2 == NULL) {
581                         GLOGD("Failed to invoke dbus method");
582                         return error;
583                 } else {
584                         g_variant_unref(msg2);
585                 }
586
587                 GLOGD("Succeeded to perform get_mpath_info");
588         }
589
590         return ERROR_NONE;
591 }
592 error_e Wmesh::GetMeshconfInfo(void)
593 {
594         bool is_started;
595         GVariant *msg1 = NULL;
596         GVariant *msg2 = NULL;
597         error_e error = ERROR_NONE;
598
599         msg1 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
600                 WIFI_MESH_MGR_PATH,
601                 WIFI_MESH_MGR_INTERFACE,
602                 WIFI_MESH_MGR_METHOD_IS_MESH_ENABLED,
603                 NULL,
604                 &error);
605
606         if (msg1 == NULL) {
607                 GLOGD("Failed to invoke dbus method");
608                 return error;
609         }
610
611         g_variant_get(msg1, "(b)", &is_started);
612         g_variant_unref(msg1);
613         if (TRUE == is_started) {
614                 msg2 = InvokeMethod(WIFI_MESH_MGR_SERVICE,
615                         WIFI_MESH_MGR_PATH,
616                         WIFI_MESH_MGR_INTERFACE,
617                         WIFI_MESH_MGR_METHOD_GET_MESHCONF_INFO,
618                         NULL,
619                         &error);
620
621                 if (msg2 == NULL) {
622                         GLOGD("Failed to invoke dbus method");
623                         return error;
624                 } else {
625                         g_variant_unref(msg2);
626                 }
627
628                 GLOGD("Succeeded to perform get_meshconf_info");
629         }
630
631         return ERROR_NONE;
632 }
633