-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
1581 lines (1294 loc) · 39.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* connman-ncurses
*
* Copyright (C) 2014 Eurogiciel. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <dbus/dbus.h>
#include <signal.h>
#include <assert.h>
#include "engine.h"
#include "loop.h"
#include "ncurses_utils.h"
#include "string_utils.h"
#include "json_utils.h"
#include "renderers.h"
#include "keys.h"
#include "popup.h"
#include "special_win.h"
/*
* This file is the glue between ncurses and the engine.
* Thus every key pressed and callback end up here.
*/
/*
* See renderers.c for details.
*/
extern int nb_items;
extern int nb_fields;
// See renderers.h for details.
extern struct context_info context;
/*
* See popup.c for details.
*/
extern FORM *popup_form;
extern FIELD **popup_fields;
extern struct popup_actions **popup_btn_action;
static void print_services_for_tech(void);
static void connect_to_service(void);
static void get_state(void);
static void print_home_page(void);
static void exec_refresh(void);
static void get_service_settings(void);
// Refres isn't automatic, this could be problematic in high wifi density areas:
// your cursor would move around endlessly. Thus, automatic refresh is disabled
// by default in the context CONTEXT_SERVICES. The variable here force the
// refresh, it's true on special occasions like the user just managed to connect
static bool allow_refresh = false;
// This is used to store requests from the agent.
static char **agent_request_input_fields;
/*
* Those are special windows, they are over anything until 'Esc'.
* It's handy for error reporting and help messages.
*/
WINDOW *win_error;
WINDOW *win_help;
// This table contain every default action possible for each CONTEXT_XXX
static struct {
void (*func)(); // What to do on action
void (*func_free)(); // Free elements for the current_context
void (*func_back)(); // What to do on 'Esc'
void (*func_refresh)(); // Refresh the current_context
} context_actions[] = {
{ print_services_for_tech, __renderers_free_home_page, print_home_page,
print_home_page }, // CONTEXT_HOME
{ NULL, __renderers_free_service_config, print_home_page,
print_services_for_tech }, // CONTEXT_SERVICE_CONFIG
{ connect_to_service, __renderers_free_services, print_home_page,
print_services_for_tech }, // CONTEXT_SERVICES
{ NULL, __renderers_free_service_config, print_services_for_tech,
get_service_settings }, // CONTEXT_SERVICE_CONFIG_STANDALONE
};
/*
* This method is mandatory, and might be of use somehow
* e.g quite handy for debugging operations
*/
void callback_ended(void)
{
return;
}
/*
* Free everything possible in context.serv and context.tech.
* Freed variables will be se to NULL.
*/
static void context_free_userptr_data()
{
if (context.serv) {
if (context.serv->dbus_name) {
free(context.serv->dbus_name);
context.serv->dbus_name = NULL;
}
if (context.serv->pretty_name) {
free(context.serv->pretty_name);
context.serv->pretty_name = NULL;
}
}
if (context.tech) {
if (context.tech->dbus_name) {
free(context.tech->dbus_name);
context.tech->dbus_name = NULL;
}
if (context.tech->pretty_name) {
free(context.tech->pretty_name);
context.tech->pretty_name = NULL;
}
}
}
/*
* Just a handy way to report a new error.
*/
static void report_error()
{
if (win_error)
return;
win_error = win_error_new(win_body_lines, COLS-2, 2, 1,
" Invalid argument/value.");
}
/*
* Create a help window matching the context.
*/
static void get_help_window()
{
char *msg;
if (win_help)
return;
switch (context.current_context) {
case CONTEXT_HOME:
msg = " This view list technologies,\n"
" * Press 'Return'/'Enter' for details on a technology\n"
" * Press 'd' to disconnect a technology\n"
" * Press 'p' to toggle a technology's power state\n"
" * Press 'o' to toggle the OfflineMode (power on/off all technologies)\n"
" * Press 'F5' to force refresh\n"
" * Press '^C' to quit";
break;
case CONTEXT_SERVICE_CONFIG:
case CONTEXT_SERVICE_CONFIG_STANDALONE:
msg = " This view list the connection/service settings. Use it wisely !\n"
" /!\\ WARNING: Setting IPv4/6 'Method' to 'off' can block future connections to this service.\n"
" You can modify underlined settings. Some settings require arrays of strings. Those have the following format: '[ \"string\", ... ]'.\n"
" 'AutoConnect and 'Method' fields have a set of possible values, to change the value use the space bar or the left/right arrow keys.\n\n"
" * 'Nameservers.Configuration', 'Timeservers.Configuration' and 'Domains.Configuration' must be an array of strings (IPs, domains...).\n"
" * 'Proxy.Configuration':\n"
"\t* 'URL' the url\n"
"\t* 'Servers' and 'Excludes' are arrays of strings\n"
" * Press 'F5' to force refresh, Press 'F7' to submit changes, Press '^C' to quit";
break;
case CONTEXT_SERVICES:
msg = " This view list services the technology can connect to.\n"
" 'f' at the start of the line means that this service has 'Favorite' = True\n"
" * Press 's' to configure the service\n"
" * Press 'r' to remove saved information on a service\n"
" * Press 'Return'/'Enter' to connect\n"
" * Press 'F5' to force a refresh\n"
" * Press 'F6' to force a rescan for the technology (some technologies don't support this)\n"
" * Press '^C' to quit";
break;
}
win_help = win_help_new(win_body_lines, COLS-2, 2, 1, msg);
}
/*
* Free the popup allocated memory.
* Note: this is different from popup.c popup_delete().
*/
static void popup_free(void)
{
int i = 0;
assert(popup_btn_action != NULL);
while (popup_btn_action[i]) {
free(popup_btn_action[i]->key);
free(popup_btn_action[i]);
popup_btn_action[i] = NULL;
i++;
}
free(popup_btn_action);
popup_delete();
exec_refresh();
}
/*
* Stop the main loop and free the allocated memory.
*/
static void stop_loop(int signum, siginfo_t *si, void *useless)
{
loop_quit();
if (context_actions[context.current_context].func_free)
context_actions[context.current_context].func_free();
if (popup_exists())
popup_free();
if (win_exists(win_error))
win_driver(&win_error, 27);
if (win_exists(win_help))
win_driver(&win_help, 27);
engine_terminate();
context_free_userptr_data();
if (context.serv)
free(context.serv);
if (context.tech)
free(context.tech);
context.serv = NULL;
context.tech = NULL;
}
/*
* Create windows at the right dimensions.
* If LINES and COLS are too small, we force the 24*80 screen.
*/
static void create_win(void)
{
if (LINES < 24)
LINES = 24;
if (COLS < 80)
COLS = 80;
win_body_lines = LINES - 5;
win_body = newwin(win_body_lines + 2, COLS, 1, 0);
assert(win_body != NULL);
box(win_body, 0, 0);
win_header = newwin(1, COLS, 0, 0);
win_footer = newwin(2, COLS, LINES-2, 0);
assert(win_footer != NULL && win_header != NULL);
redrawwin(win_header);
redrawwin(win_body);
redrawwin(win_footer);
// If we don't do that, ncurses keep sending 'Esc' key on every key
// pressed
keypad(win_body, TRUE);
}
/*
* Delete windows, as in delwin().
*/
static void delete_win(void)
{
delwin(win_header);
delwin(win_body);
delwin(win_footer);
}
/*
* Triggered on SIGWINCH signal.
* This function have a resize effect (but we really delete and create windows).
*/
static void resize(int signum, siginfo_t *si, void *useless)
{
sigset_t mask, orig_mask;
// Mask the SIGWINCH signal, this will allow quite fast resize but won't
// resist if you go nuclear on resize...
sigemptyset(&mask);
sigaddset(&mask, SIGWINCH);
assert(sigprocmask(SIG_BLOCK, &mask, &orig_mask) == 0);
loop_quit();
endwin();
clear();
refresh();
delete_win();
create_win();
get_state();
exec_refresh();
loop_run(true);
if (popup_exists())
popup_move((LINES-17)/2, (COLS-75)/2);
if (win_error)
win_resize(win_error, win_body_lines, COLS-2);
if (win_help)
win_resize(win_help, win_body_lines, COLS-2);
// Restore the normal behaviour of SIGWINCH
assert(sigprocmask(SIG_SETMASK, &orig_mask, NULL) == 0);
}
/*
* Refresh context.cursor_id, free memory for the current context and execute
* func_refresh() of the current context. The popup will be refreshed as well
* (if it exists).
*/
static void exec_refresh(void)
{
ITEM *item;
FIELD *tmp_field;
struct userptr_data *tmp;
if (nb_fields != 0) {
tmp_field = current_field(main_form);
assert(tmp_field != NULL);
tmp = field_userptr(tmp_field);
context.cursor_id = strdup(tmp->dbus_name);
} else if (nb_items != 0) {
// We use the dbus_name as a unique key
item = current_item(main_menu);
assert(item != NULL);
tmp = item_userptr(item);
context.cursor_id = strdup(tmp->dbus_name);
}
context_actions[context.current_context].func_free();
context_actions[context.current_context].func_refresh();
}
/*
* Reset cursor position before refresh.
* We use a value put in userptr by the renderers, this value is the "true"
* dbus_name for menus and a "key" for forms.
* The key on form make it work but it isn't a great solution.
*/
void repos_cursor(void)
{
int i, j;
struct userptr_data *tmp;
ITEM *item;
if (!context.cursor_id)
return;
if (nb_fields != 0) { // On forms
for (i = 0; i < nb_fields; i++) {
if (!(field_opts(main_fields[i]) & O_ACTIVE))
continue;
tmp = field_userptr(main_fields[i]);
if (strncmp(tmp->dbus_name, context.cursor_id, 256) == 0) {
// This trick print the page of the field and
// set the cursor on the correct field.
unpost_form(main_form);
main_form->curpage = main_fields[i]->page;
main_form->current = main_fields[i];
post_form(main_form);
break;
}
}
} else if (nb_items != 0) { // On menus
for (i = 0; i < nb_items; i++) {
item = main_items[i];
tmp = item_userptr(item);
if (strncmp(context.cursor_id, tmp->dbus_name, 256) == 0) {
for (j = 0; j < i; j++)
menu_driver(main_menu, REQ_NEXT_ITEM);
wrefresh(main_menu->usersub);
break;
}
}
}
free(context.cursor_id);
context.cursor_id = NULL;
wrefresh(win_body);
}
/*
* Free the current context and execute the appropriate action.
* context.serv or context.tech are also updated.
*/
static void exec_action(struct userptr_data *data)
{
if (!data) {
exec_refresh();
return;
}
switch (context.current_context) {
case CONTEXT_SERVICES:
context.serv->dbus_name = strdup(data->dbus_name);
if (data->pretty_name)
context.serv->pretty_name = strdup(data->pretty_name);
break;
case CONTEXT_HOME:
context.tech->dbus_name = strdup(data->dbus_name);
context.tech->pretty_name = strdup(data->pretty_name);
break;
default:
break;
}
context_actions[context.current_context].func_free();
context_actions[context.current_context].func();
}
/*
* Free the current context and execute func_back().
*/
static void exec_back(void)
{
if (context.current_context == CONTEXT_SERVICE_CONFIG_STANDALONE) {
free(context.serv->dbus_name);
context.serv->dbus_name = NULL;
} else
context_free_userptr_data();
context_actions[context.current_context].func_free();
context_actions[context.current_context].func_back();
}
/*
* Execute a renderer action based on the jobj command name.
* @param jobj See engine.c for more details.
*/
static void action_on_cmd_callback(struct json_object *jobj)
{
struct json_object *data, *tmp, *array;
const char *cmd_name;
json_object_object_get_ex(jobj, key_command_data, &data);
cmd_name = __json_get_command_str(jobj);
/* dispatch according to the command name */
if (strcmp(key_engine_get_home_page, cmd_name) == 0)
__renderers_home_page(data);
else if (strcmp(key_engine_get_services_from_tech, cmd_name) == 0)
__renderers_services(data);
else if (strcmp(key_engine_get_state, cmd_name) == 0)
__renderers_state(data);
else if (strcmp(key_engine_get_service, cmd_name) == 0) {
tmp = json_object_new_object();
array = json_object_new_array();
json_object_array_add(array, json_object_get(data));
json_object_object_add(tmp, key_services, array);
__renderers_services(tmp);
json_object_put(tmp);
} else
print_info_in_footer(true, "Unknown command called");
}
/*
* Execute a refresh or back action depending on the signal data.
* If the technology the user is currently looking at is removed, exec_back() is
* executed. Else, exec_refresh() is executed, see comments on allow_refresh for
* more details.
* @param jobj See the format in commands.c monitor_changed().
*/
static void action_on_signal(struct json_object *jobj)
{
struct json_object *sig_path, *sig_data, *sig_name;
json_bool tmp_bool;
bool is_tech_removed, is_current_tech, got_connected, got_removed;
const char *tmp_str;
char *dbus_short_name;
json_object_object_get_ex(jobj, key_command_path, &sig_path);
json_object_object_get_ex(jobj, key_command_data, &sig_data);
json_object_object_get_ex(jobj, key_signal, &sig_name);
is_tech_removed = strncmp(key_sig_tech_removed, json_object_get_string(sig_name), 50) == 0;
if (context.tech->dbus_name == NULL)
is_current_tech = false;
else
is_current_tech = strncmp(context.tech->dbus_name, json_object_to_json_string(sig_data), 256) == 0;
if (is_tech_removed && is_current_tech) {
exec_back();
return;
}
got_connected = false;
got_removed = false;
if (sig_data && json_object_get_type(sig_data) == json_type_array) {
tmp_str = json_object_get_string(json_object_array_get_idx(sig_data, 0));
tmp_bool = json_object_get_boolean(json_object_array_get_idx(sig_data, 1));
if (tmp_str && strcmp("Connected", tmp_str) == 0 && tmp_bool)
got_connected = true;
if (tmp_str && strcmp("Favorite", tmp_str) == 0 && !tmp_bool)
got_removed = true;
}
if (context.current_context == CONTEXT_SERVICE_CONFIG ||
context.current_context == CONTEXT_SERVICE_CONFIG_STANDALONE) {
if (context.serv->dbus_name == NULL)
return;
dbus_short_name = extract_dbus_short_name(context.serv->dbus_name);
if (strcmp(dbus_short_name, json_object_get_string(sig_path)) != 0) {
free(dbus_short_name);
return;
}
free(dbus_short_name);
}
// This is to prevent always changing wifi services in areas
// with a load of networks
if (context.current_context != CONTEXT_SERVICES ||
(allow_refresh || got_connected || got_removed)) {
exec_refresh();
allow_refresh = false;
}
}
/*
* The user pressed "OK" button on an agent request, all fields of the popup
* form are extracted and the agent response is sent. The popup will also be
* removed.
*/
static void popup_btn_action_ok(void)
{
struct json_object *cmd, *cmd_data;
int i;
char *label_str, *field_str;
char *label_str_clean, *field_str_clean;
cmd = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_agent_response));
cmd_data = json_object_new_object();
for (i = 0; i < popup_form->maxfield; i++) {
label_str = field_buffer(popup_fields[i], 0);
label_str_clean = strtok(label_str, " ");
if (!label_str_clean)
label_str_clean = NULL;
i++;
field_str = field_buffer(popup_fields[i], 0);
field_str_clean = strtok(field_str, " ");
if (!field_str_clean)
field_str_clean = "";
json_object_object_add(cmd_data, label_str_clean,
json_object_new_string(field_str_clean));
}
json_object_object_add(cmd, key_command_data, cmd_data);
if (engine_query(cmd) == -EINVAL)
report_error();
popup_free();
}
/*
* Response to the "Retry" agent error.
* @param retry (json_type_bool -> int) 0 or 1
*/
static void popup_btn_action_retry(int retry)
{
struct json_object *cmd;
cmd = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_agent_retry));
json_object_object_add(cmd, key_command_data,
json_object_new_boolean(retry));
if (engine_query(cmd) == -EINVAL)
report_error();
popup_free();
}
/*
* Respond TRUE to the "Retry" agent error.
*/
static void popup_btn_action_retry_ok(void)
{
popup_btn_action_retry(TRUE);
}
/*
* Respond FALSE to the "Retry" agent error.
*/
static void popup_btn_action_retry_no(void)
{
popup_btn_action_retry(FALSE);
}
/*
* Create the fields for the popup.
* @param serv_name the service name the agen request is about
* @param data the agent requested values, see connman/doc/agent.api for details
*/
static void agent_input_popup(const char *serv_name, struct json_object *data)
{
int i, len = 0;
char req_char;
char buf[60];
// Nom du champ, type, alternates, requirement
char *fmt = "%s (%s, %s): %c", *alt_str;
const char *value_str;
// Arguments as in connman/doc/agent-api.txt
struct json_object *type, *req, *alt, *value;
struct popup_actions *popup_btn_ok;
json_object_object_foreach(data, _key, _val) {
assert(_key && _val);
len++;
}
agent_request_input_fields = malloc(sizeof(char *) * (len * 2 + 1));
popup_btn_ok = malloc(sizeof(struct popup_actions));
popup_btn_ok->key = strdup("OK");
popup_btn_ok->func = popup_btn_action_ok;
popup_btn_action = malloc(sizeof(struct popup_actions *) * 2);
popup_btn_action[0] = popup_btn_ok;
popup_btn_action[1] = NULL;
i = 0;
json_object_object_foreach(data, key, val) {
json_object_object_get_ex(val, "Type", &type);
json_object_object_get_ex(val, "Requirement", &req);
json_object_object_get_ex(val, "Alternates", &alt);
json_object_object_get_ex(val, "Value", &value);
if (strncmp(json_object_get_string(req), "mandatory", 10) == 0)
req_char = '*';
else if (strncmp(json_object_get_string(req), "alternate", 10) == 0)
req_char = '~';
else
req_char = ' ';
if (!alt)
alt_str = "no alt";
else {
alt_str = (char *) json_object_get_string(alt);
alt_str += sizeof(char) * 2; // removes leading '[ '
alt_str[strlen(alt_str)-2] = '\0'; // removes trailing ' ]'
}
snprintf(buf, 60, fmt, key, json_object_get_string(type), alt_str, req_char);
buf[59] = '\0';
agent_request_input_fields[i] = strndup(buf, 60);
i++;
value_str = " ";
if (value)
value_str = json_object_get_string(value);
agent_request_input_fields[i] = strndup(value_str, 60);
i++;
}
agent_request_input_fields[len*2] = NULL;
}
/*
* Create the popup from an agent request.
* Note: only "Input Requested" is fully supported for now.
* @param jobj the agent request
*/
static void action_on_agent_msg(struct json_object *jobj)
{
struct json_object *request, *service, *data;
const char *request_str, *service_str, *fmt = "The network %s request"
" credentials";
char buf[150];
int i;
assert(!popup_exists());
json_object_object_get_ex(jobj, key_agent_msg, &request);
json_object_object_get_ex(jobj, key_agent_msg_data, &data);
json_object_object_get_ex(jobj, key_service, &service);
request_str = json_object_get_string(request);
service_str = json_object_get_string(service);
if (strncmp(key_agent_request_input, request_str, 15) == 0)
agent_input_popup(service_str, data);
else if (strncmp(key_agent_request_browser, request_str, 21) == 0)
print_info_in_footer(false, "The agent request you to"
" open the web page: %s",
json_object_get_string(jobj));
else {
print_info_in_footer(true,
"Not yet handled agent request:");
print_info_in_footer2(true,
json_object_get_string(jobj));
}
snprintf(buf, 150, fmt, context.serv->pretty_name);
buf[149] = '\0';
popup_new(18, 76, (LINES-17)/2, (COLS-75)/2,
agent_request_input_fields, buf);
assert(popup_exists());
popup_refresh();
for (i = 0; agent_request_input_fields[i]; i++)
free(agent_request_input_fields[i]);
free(agent_request_input_fields);
agent_request_input_fields = NULL;
}
/*
* Create a popup from an agent error message. Often asking to retry.
* @param jobj the agent error e.g.:
{
key_agent_error: "invalid-key",
key_service: "wifi_XXXXXXXXXXXX_YYYYYYYYYYYYYY_managed_psk",
key_agent_error_message: "Retry ?",
key_agent_error_callback: "report_error_return"
}
*/
static void action_on_agent_error(struct json_object *jobj)
{
char *fmt = "Agent error: %s on \"%s\", %s", buf[150];
const char *error_msg_str, *service_str, *msg_str;
struct json_object *tmp;
struct popup_actions *popup_btn_no, *popup_btn_yes;
assert(!popup_exists());
json_object_object_get_ex(jobj, key_agent_error, &tmp);
error_msg_str = json_object_get_string(tmp);
json_object_object_get_ex(jobj, key_service, &tmp);
service_str = json_object_get_string(tmp);
json_object_object_get_ex(jobj, key_agent_error_message, &tmp);
msg_str = json_object_get_string(tmp);
snprintf(buf, 150, fmt, error_msg_str, service_str, msg_str);
buf[149] = '\0';
popup_btn_action = malloc(sizeof(struct popup_actions *) * 3);
popup_btn_yes = malloc(sizeof(struct popup_actions));
popup_btn_yes->key = strdup("YES");
popup_btn_yes->func = popup_btn_action_retry_ok;
popup_btn_action[0] = popup_btn_yes;
popup_btn_no = malloc(sizeof(struct popup_actions));
popup_btn_no->key = strdup("NO");
popup_btn_no->func = popup_btn_action_retry_no;
popup_btn_action[1] = popup_btn_no;
popup_btn_action[2] = NULL;
popup_new(16, 76, (LINES-15)/2, (COLS-75)/2, NULL, buf);
assert(popup_exists());
popup_refresh();
}
/*
* Dispatch the callback data to the appropriate functions based on the presence
* of the attributes in jobj:
* - key_command
* - key_signal
* - key_agent_msg
* - key_agent_error
* - key_return_force_refresh
* @param status the status code of this callback, status < 0 is an error
* @param jobj see above
*/
static void main_callback(int status, struct json_object *jobj)
{
struct json_object *cmd_tmp, *signal, *agent_msg, *agent_error,
*return_user_data, *error;
const char *error_str;
json_object_object_get_ex(jobj, key_error, &error);
error_str = json_object_get_string(error);
if (status < 0) {
print_info_in_footer2(true, "Error (code %d) %s", -status,
error_str ? error_str : "[ no error message ]");
}
/* get the main object items */
json_object_object_get_ex(jobj, key_command, &cmd_tmp);
json_object_object_get_ex(jobj, key_signal, &signal);
json_object_object_get_ex(jobj, key_agent_msg, &agent_msg);
json_object_object_get_ex(jobj, key_agent_error, &agent_error);
json_object_object_get_ex(jobj, key_return_force_refresh, &return_user_data);
if (cmd_tmp)
action_on_cmd_callback(jobj);
else if (signal)
action_on_signal(jobj);
else if (agent_msg)
action_on_agent_msg(jobj);
else if (agent_error)
action_on_agent_error(jobj);
else if (return_user_data) {
allow_refresh = true;
exec_refresh();
} else {
int i = 0;
json_object_object_foreach(jobj, key, val) {
assert(key && val);
i++;
}
if (i != 0)
print_info_in_footer(true, "Unidentified call back: "
"status: %d, jobj: %s\n", status,
json_object_get_string(jobj));
}
// Release the memory of the json object now
json_object_put(jobj);
if (win_exists(win_error))
win_driver(&win_error, 0);
if (win_exists(win_help))
win_driver(&win_help, 0);
}
/*
* Asks for the home page.
*/
static void print_home_page(void)
{
struct json_object *cmd;
werase(win_footer);
cmd = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_get_home_page));
if (engine_query(cmd) == -EINVAL)
report_error();
}
/*
* Asks for the state of connman. Note that the state is a part of the home
* page. This particular function is useful to refresh win_header on resize.
*/
static void get_state(void)
{
struct json_object *cmd;
cmd = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_get_state));
if (engine_query(cmd) == -EINVAL)
report_error();
}
/*
* Asks for the services for the technology in context.tech->dbus_name.
*/
static void print_services_for_tech(void)
{
struct json_object *cmd, *tmp;
if (!context.tech || !context.tech->dbus_name)
return;
cmd = json_object_new_object();
tmp = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_get_services_from_tech));
json_object_object_add(tmp, key_technology,
json_object_new_string(context.tech->dbus_name));
json_object_object_add(cmd, key_command_data, tmp);
if (engine_query(cmd) == -EINVAL)
report_error();
}
/*
* Asks to connect to the service in context.serv->dbus_name.
*/
static void connect_to_service(void)
{
struct json_object *cmd, *tmp;
if (!context.serv || !context.serv->dbus_name)
return;
cmd = json_object_new_object();
tmp = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_connect));
json_object_object_add(tmp, key_service,
json_object_new_string(context.serv->dbus_name));
json_object_object_add(cmd, key_command_data, tmp);
if (engine_query(cmd) == -EINVAL)
report_error();
werase(win_body);
mvwprintw(win_body, 1, 2, "Connecting...");
box(win_body, 0, 0);
wrefresh(win_body);
}
/*
* Asks for deconnection of data->dbus_name.
* @param data the user pointer of a technology item
*/
static void disconnect_of_service(struct userptr_data *data)
{
struct json_object *cmd, *tmp;
if (!data || !data->dbus_name)
return;
cmd = json_object_new_object();
tmp = json_object_new_object();
json_object_object_add(cmd, key_command,
json_object_new_string(key_engine_disconnect));
json_object_object_add(tmp, key_technology,
json_object_new_string(data->dbus_name));
json_object_object_add(cmd, key_command_data, tmp);
if (engine_query(cmd) == -EINVAL)
report_error();
}
/*
* Asks to toggle the power state of the technology data->dbus_name.
* @param data the user pointer of a technology item
*/
static void toggle_power_tech(struct userptr_data *data)
{
struct json_object *cmd, *tmp;
if (!data || !data->dbus_name)
return;
cmd = json_object_new_object();