connman client library is in, with tests and all.
[framework/uifw/edbus.git] / src / bin / e_dbus_connman_test.c
1 #include "E_Connman.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <errno.h>
6
7 static void
8 _elements_print(E_Connman_Element **elements, int count)
9 {
10    int i;
11    for (i = 0; i < count; i++)
12      {
13         printf("--- element %d:\n", i);
14         e_connman_element_print(stdout, elements[i]);
15      }
16    free(elements);
17    printf("END: all elements count = %d\n", count);
18 }
19
20 static int
21 _on_element_add(void *data, int type, void *info)
22 {
23    E_Connman_Element *element = info;
24    printf(">>> %s\n", element->path);
25    return 1;
26 }
27
28 static int
29 _on_element_del(void *data, int type, void *info)
30 {
31    E_Connman_Element *element = info;
32    printf("<<< %s\n", element->path);
33    return 1;
34 }
35
36 static int
37 _on_element_updated(void *data, int type, void *info)
38 {
39    E_Connman_Element *element = info;
40    printf("!!! %s\n", element->path);
41    e_connman_element_print(stderr, element);
42    return 1;
43 }
44
45 static int
46 _on_cmd_quit(char *cmd, char *args)
47 {
48    fputs("Bye!\n", stderr);
49    ecore_main_loop_quit();
50    return 0;
51 }
52
53 static int
54 _on_cmd_sync(char *cmd, char *args)
55 {
56    e_connman_manager_sync_elements();
57    return 1;
58 }
59
60 static char *
61 _tok(char *p)
62 {
63    p = strchr(p, ' ');
64    if (!p)
65      return NULL;
66
67    *p = '\0';
68    p++;
69    while (isspace(*p))
70      p++;
71    if (*p == '\0')
72      return NULL;
73
74    return p;
75 }
76
77 static int
78 _on_cmd_get_all(char *cmd, char *args)
79 {
80    E_Connman_Element **elements;
81    char *type;
82    unsigned int count;
83    bool ret;
84
85    if (!args)
86      type = NULL;
87    else
88      type = args;
89
90    if (type)
91      ret = e_connman_elements_get_all_type(type, &count, &elements);
92    else
93      ret = e_connman_elements_get_all(&count, &elements);
94
95    if (!ret)
96      fputs("ERROR: could not get elements\n", stderr);
97    else
98      {
99         printf("BEG: all elements type=%s count = %d\n", type, count);
100         _elements_print(elements, count);
101      }
102
103    return 1;
104 }
105
106 static E_Connman_Element *
107 _element_from_args(char *args, char **next_args)
108 {
109    E_Connman_Element *element;
110
111    if (!args)
112      {
113         fputs("ERROR: missing element path\n", stderr);
114         *next_args = NULL;
115         return NULL;
116      }
117
118    *next_args = _tok(args);
119    element = e_connman_element_get(args);
120    if (!element)
121      fprintf(stderr, "ERROR: no element called \"%s\".\n", args);
122
123    return element;
124 }
125
126 static int
127 _on_cmd_print(char *cmd, char *args)
128 {
129    char *next_args;
130    E_Connman_Element *element = _element_from_args(args, &next_args);
131    if (element)
132      e_connman_element_print(stdout, element);
133    return 1;
134 }
135
136 static int
137 _on_cmd_get_properties(char *cmd, char *args)
138 {
139    char *next_args;
140    E_Connman_Element *element = _element_from_args(args, &next_args);
141    if (element)
142      e_connman_element_properties_sync(element);
143    return 1;
144 }
145
146 static int
147 _on_cmd_property_set(char *cmd, char *args)
148 {
149    char *next_args, *name, *p;
150    E_Connman_Element *element = _element_from_args(args, &next_args);
151    void *value;
152    long vlong;
153    unsigned short vu16;
154    unsigned int vu32;
155    int type;
156
157    if (!element)
158      return 1;
159
160    if (!next_args)
161      {
162         fputs("ERROR: missing parameters name, type and value.\n", stderr);
163         return 1;
164      }
165
166    name = next_args;
167    p = _tok(name);
168    if (!p)
169      {
170         fputs("ERROR: missing parameters type and value.\n", stderr);
171         return 1;
172      }
173
174    next_args = _tok(p);
175    if (!next_args)
176      {
177         fputs("ERROR: missing parameter value.\n", stderr);
178         return 1;
179      }
180
181    type = p[0];
182    switch (type)
183      {
184       case DBUS_TYPE_BOOLEAN:
185          vlong = !!atol(next_args);
186          value = (void *)vlong;
187          fprintf(stderr, "DBG: boolean is: %ld\n", vlong);
188          break;
189       case DBUS_TYPE_UINT16:
190          vu16 = strtol(next_args, &p, 0);
191          if (p == next_args)
192            {
193               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
194               return 1;
195            }
196          value = &vu16;
197          fprintf(stderr, "DBG: u16 is: %hu\n", vu16);
198          break;
199       case DBUS_TYPE_UINT32:
200          vu32 = strtol(next_args, &p, 0);
201          if (p == next_args)
202            {
203               fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
204               return 1;
205            }
206          value = &vu32;
207          fprintf(stderr, "DBG: u16 is: %u\n", vu32);
208          break;
209       case DBUS_TYPE_STRING:
210       case DBUS_TYPE_OBJECT_PATH:
211          p = next_args + strlen(next_args);
212          if (p > next_args)
213            p--;
214          while (p > next_args && isspace(*p))
215            p--;
216          if (p <= next_args)
217            {
218               fprintf(stderr, "ERROR: invalid string \"%s\".\n", next_args);
219            }
220          p[1] = '\0';
221          value = next_args;
222          fprintf(stderr, "DBG: string is: \"%s\"\n", next_args);
223          break;
224       default:
225          fprintf(stderr, "ERROR: don't know how to parse type '%c' (%d)\n",
226                  type, type);
227          return 1;
228      }
229
230    fprintf(stderr, "set_property %s [%p] %s %c %p...\n",
231            args, element, name, type, value);
232    e_connman_element_property_set(element, name, type, value);
233    return 1;
234 }
235
236
237 /* Manager Commands */
238
239 static int
240 _on_cmd_manager_get(char *cmd, char *args)
241 {
242    E_Connman_Element *element;
243    element = e_connman_manager_get();
244    e_connman_element_print(stderr, element);
245    return 1;
246 }
247
248 static int
249 _on_cmd_manager_get_profiles(char *cmd, char *args)
250 {
251    unsigned int count;
252    E_Connman_Element **profiles;
253
254    if (!e_connman_manager_profiles_get(&count, &profiles))
255      {
256         fputs("ERROR: can't get profiles\n", stderr);
257         return 1;
258      }
259    printf("BEG: all manager profiles elements count = %d\n", count);
260    _elements_print(profiles, count);
261    return 1;
262 }
263
264 static int
265 _on_cmd_manager_get_devices(char *cmd, char *args)
266 {
267    unsigned int count;
268    E_Connman_Element **devices;
269
270    if (!e_connman_manager_devices_get(&count, &devices))
271      {
272         fputs("ERROR: can't get devices\n", stderr);
273         return 1;
274      }
275    printf("BEG: all manager devices elements count = %d\n", count);
276    _elements_print(devices, count);
277    return 1;
278 }
279
280 static int
281 _on_cmd_manager_get_connections(char *cmd, char *args)
282 {
283    unsigned int count;
284    E_Connman_Element **connections;
285
286    if (!e_connman_manager_connections_get(&count, &connections))
287      {
288         fputs("ERROR: can't get connections\n", stderr);
289         return 1;
290      }
291    printf("BEG: all manager connections elements count = %d\n", count);
292    _elements_print(connections, count);
293    return 1;
294 }
295
296 static int
297 _on_cmd_manager_register_agent(char *cmd, char *args)
298 {
299    char *path;
300
301    if (!args)
302      {
303         fputs("ERROR: missing the object path\n", stderr);
304         return 1;
305      }
306
307    path = args;
308    if (e_connman_manager_register_agent(path, NULL, NULL))
309      printf(":::Registering agent %s...\n", path);
310    else
311      fprintf(stderr, "ERROR: can't register agent %s\n", path);
312
313    return 1;
314 }
315
316 static int
317 _on_cmd_manager_unregister_agent(char *cmd, char *args)
318 {
319    char *path;
320
321    if (!args)
322      {
323         fputs("ERROR: missing the object path\n", stderr);
324         return 1;
325      }
326
327    path = args;
328    if (e_connman_manager_unregister_agent(path, NULL, NULL))
329      printf(":::Unregistering agent %s...\n", path);
330    else
331      fprintf(stderr, "ERROR: can't unregister agent %s\n", path);
332
333    return 1;
334 }
335
336 static int
337 _on_cmd_manager_get_state(char *cmd, char *args)
338 {
339    const char *state;
340    if (e_connman_manager_state_get(&state))
341      printf(":::Manager state = \"%s\"\n", state);
342    else
343      fputs("ERROR: can't get manager state\n", stderr);
344    return 1;
345 }
346
347 static int
348 _on_cmd_manager_get_policy(char *cmd, char *args)
349 {
350    const char *policy;
351    if (e_connman_manager_policy_get(&policy))
352      printf(":::Manager policy = \"%s\"\n", policy);
353    else
354      fputs("ERROR: can't get manager policy\n", stderr);
355    return 1;
356 }
357
358 static int
359 _on_cmd_manager_set_policy(char *cmd, char *args)
360 {
361    char *policy;
362    if (!args)
363      {
364         fputs("ERROR: missing the policy value\n", stderr);
365         return 1;
366      }
367    _tok(args);
368    policy = args;
369    if (e_connman_manager_policy_set(policy, NULL, NULL))
370      printf(":::Manager policy set to \"%s\"\n", policy);
371    else
372      fputs("ERROR: can't set manager policy\n", stderr);
373    return 1;
374 }
375
376 static int
377 _on_cmd_manager_get_offline_mode(char *cmd, char *args)
378 {
379    bool offline;
380    if (e_connman_manager_offline_mode_get(&offline))
381      printf(":::Manager Offline Mode = %hhu\n", offline);
382    else
383      fputs("ERROR: can't get manager offline mode\n", stderr);
384    return 1;
385 }
386
387 static int
388 _on_cmd_manager_set_offline_mode(char *cmd, char *args)
389 {
390    bool offline;
391    if (!args)
392      {
393         fputs("ERROR: missing the offline mode value\n", stderr);
394         return 1;
395      }
396    _tok(args);
397    offline = !!atol(args);
398    if (e_connman_manager_offline_mode_set(offline, NULL, NULL))
399      printf(":::Manager Offline Mode set to %hhu\n", offline);
400    else
401      fputs("ERROR: can't set manager offline mode\n", stderr);
402    return 1;
403 }
404
405 /* Device Commands */
406
407 static int
408 _on_cmd_device_create_network(char *cmd, char *args)
409 {
410    char *path;
411    E_Connman_Element *e;
412
413    if (!args)
414      {
415         fputs("ERROR: missing the device path\n", stderr);
416         return 1;
417      }
418    _tok(args);
419    path = args;
420    e = e_connman_device_get(path);
421    if (e_connman_device_network_create(e, NULL, NULL))
422      printf(":::Creating Network %s...\n", path);
423    else
424      fputs("ERROR: can't create network\n", stderr);
425    return 1;
426 }
427
428 static int
429 _on_cmd_device_remove_network(char *cmd, char *args)
430 {
431    char *path, *device_path;
432    E_Connman_Element *e;
433
434    if (!args)
435      {
436         fputs("ERROR: missing the device path\n", stderr);
437         return 1;
438      }
439    device_path = args;
440    path = _tok(args);
441
442    if (!path)
443      {
444         fputs("ERROR: missing the object network\n", stderr);
445         return 1;
446      }
447    _tok(path);
448
449    e = e_connman_device_get(device_path);
450    if (e_connman_device_network_remove(e, path, NULL, NULL))
451      printf(":::Removing Network %s...\n", path);
452    else
453      fputs("ERROR: can't remove network\n", stderr);
454    return 1;
455 }
456
457 static int
458 _on_cmd_device_propose_scan(char *cmd, char *args)
459 {
460    char *path;
461    E_Connman_Element *e;
462
463    if (!args)
464      {
465         fputs("ERROR: missing the device path\n", stderr);
466         return 1;
467      }
468    _tok(args);
469    path = args;
470
471    e = e_connman_device_get(path);
472    if (e_connman_device_propose_scan(e, NULL, NULL))
473      printf(":::Proposing scan %s...\n", path);
474    else
475      fputs("ERROR: can't propose scan\n", stderr);
476    return 1;
477 }
478
479 static int
480 _on_cmd_device_get_name(char *cmd, char *args)
481 {
482    const char *name, *path;
483    E_Connman_Element *e;
484
485    if (!args)
486      {
487         fputs("ERROR: missing the device path\n", stderr);
488         return 1;
489      }
490    _tok(args);
491    path = args;
492
493    e = e_connman_device_get(path);
494    if (e_connman_device_name_get(e, &name))
495      printf(":::Device %s Name = \"%s\"\n", path, name);
496    else
497      fputs("ERROR: can't get device name\n", stderr);
498    return 1;
499 }
500
501 static int
502 _on_cmd_device_get_type(char *cmd, char *args)
503 {
504    const char *type, *path;
505    E_Connman_Element *e;
506
507    if (!args)
508      {
509         fputs("ERROR: missing the device path\n", stderr);
510         return 1;
511      }
512    _tok(args);
513    path = args;
514
515    e = e_connman_device_get(path);
516    if (e_connman_device_type_get(e, &type))
517      printf(":::Device %s Type = \"%s\"\n", path, type);
518    else
519      fputs("ERROR: can't get device type\n", stderr);
520    return 1;
521 }
522
523 static int
524 _on_cmd_device_get_interface(char *cmd, char *args)
525 {
526    const char *interface, *path;
527    E_Connman_Element *e;
528
529    if (!args)
530      {
531         fputs("ERROR: missing the device path\n", stderr);
532         return 1;
533      }
534    _tok(args);
535    path = args;
536
537    e = e_connman_device_get(path);
538    if (e_connman_device_interface_get(e, &interface))
539      printf(":::Device %s Interface = \"%s\"\n", path, interface);
540    else
541      fputs("ERROR: can't get device interface\n", stderr);
542    return 1;
543 }
544
545 static int
546 _on_cmd_device_get_policy(char *cmd, char *args)
547 {
548    const char *policy, *path;
549    E_Connman_Element *e;
550
551    if (!args)
552      {
553         fputs("ERROR: missing the device path\n", stderr);
554         return 1;
555      }
556    _tok(args);
557    path = args;
558
559    e = e_connman_device_get(path);
560    if (e_connman_device_policy_get(e, &policy))
561      printf(":::Device %s Policy = \"%s\"\n", path, policy);
562    else
563      fputs("ERROR: can't get device policy\n", stderr);
564    return 1;
565 }
566
567 static int
568 _on_cmd_device_set_policy(char *cmd, char *args)
569 {
570    char *policy;
571    const char *device_path;
572    E_Connman_Element *e;
573
574    if (!args)
575      {
576         fputs("ERROR: missing the device path\n", stderr);
577         return 1;
578      }
579    device_path = args;
580    policy = _tok(args);
581
582    if (!policy)
583      {
584         fputs("ERROR: missing the policy value\n", stderr);
585         return 1;
586      }
587    _tok(policy);
588
589    e = e_connman_device_get(device_path);
590    if (e_connman_device_policy_set(e, policy, NULL, NULL))
591      printf(":::Device %s policy set to \"%s\"\n", device_path, policy);
592    else
593      fputs("ERROR: can't set device policy\n", stderr);
594    return 1;
595 }
596
597 static int
598 _on_cmd_device_get_priority(char *cmd, char *args)
599 {
600    char *path;
601    unsigned char priority;
602    E_Connman_Element *e;
603
604    if (!args)
605      {
606         fputs("ERROR: missing the device path\n", stderr);
607         return 1;
608      }
609    _tok(args);
610    path = args;
611
612    e = e_connman_device_get(path);
613    if (e_connman_device_priority_get(e, &priority))
614      printf(":::Device %s Priority = %#02hhx (%d)\n", path, priority, priority);
615    else
616      fputs("ERROR: can't get device priority\n", stderr);
617    return 1;
618 }
619
620 static int
621 _on_cmd_device_set_priority(char *cmd, char *args)
622 {
623    char *next_args, *device_path, *p;
624    unsigned char priority;
625    E_Connman_Element *e;
626
627    if (!args)
628      {
629         fputs("ERROR: missing the device path\n", stderr);
630         return 1;
631      }
632    device_path = args;
633    next_args = _tok(args);
634    if (!next_args)
635      {
636         fputs("ERROR: missing the priority value\n", stderr);
637         return 1;
638      }
639    _tok(next_args);
640    priority = strtol(next_args, &p, 0);
641    if (p == next_args)
642      {
643         fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
644         return 1;
645      }
646
647    e = e_connman_device_get(device_path);
648    if (e_connman_device_priority_set(e, priority, NULL, NULL))
649      printf(":::Device %s priority set to %d\n", device_path, priority);
650    else
651      fputs("ERROR: can't set device priority\n", stderr);
652    return 1;
653 }
654
655 static int
656 _on_cmd_device_get_powered(char *cmd, char *args)
657 {
658    char *path;
659    bool powered;
660    E_Connman_Element *e;
661
662    if (!args)
663      {
664         fputs("ERROR: missing the device path\n", stderr);
665         return 1;
666      }
667    _tok(args);
668    path = args;
669
670    e = e_connman_device_get(path);
671    if (e_connman_device_powered_get(e, &powered))
672      printf(":::Device %s Powered = %hhu\n", path, powered);
673    else
674      fputs("ERROR: can't get device powered\n", stderr);
675    return 1;
676 }
677
678 static int
679 _on_cmd_device_set_powered(char *cmd, char *args)
680 {
681    char *device_path, *next_args;
682    bool powered;
683    E_Connman_Element *e;
684
685    if (!args)
686      {
687         fputs("ERROR: missing the device path\n", stderr);
688         return 1;
689      }
690    device_path = args;
691    next_args = _tok(args);
692    if (!next_args)
693      {
694         fputs("ERROR: missing the powered value\n", stderr);
695         return 1;
696      }
697    powered = !!atol(next_args);
698
699    e = e_connman_device_get(device_path);
700    if (e_connman_device_powered_set(e, powered, NULL, NULL))
701      printf(":::Device %s powered set to %hhu\n", device_path, powered);
702    else
703      fputs("ERROR: can't set device powered\n", stderr);
704    return 1;
705 }
706
707 static int
708 _on_cmd_device_get_scan_interval(char *cmd, char *args)
709 {
710    char *path;
711    unsigned short scan_interval;
712    E_Connman_Element *e;
713
714    if (!args)
715      {
716         fputs("ERROR: missing the device path\n", stderr);
717         return 1;
718      }
719    _tok(args);
720    path = args;
721
722    e = e_connman_device_get(path);
723    if (e_connman_device_scan_interval_get(e, &scan_interval))
724      printf(":::Device %s ScanInterval = %hu\n", path, scan_interval);
725    else
726      fputs("ERROR: can't get device scan interval\n", stderr);
727    return 1;
728 }
729
730 static int
731 _on_cmd_device_set_scan_interval(char *cmd, char *args)
732 {
733    char *device_path, *next_args, *p;
734    unsigned short scan_interval;
735    E_Connman_Element *e;
736
737    if (!args)
738      {
739         fputs("ERROR: missing the device path\n", stderr);
740         return 1;
741      }
742    device_path = args;
743    next_args = _tok(args);
744    if (!next_args)
745      {
746         fputs("ERROR: missing the scan interval value\n", stderr);
747         return 1;
748      }
749    scan_interval = strtol(next_args, &p, 0);
750    if (p == next_args)
751      {
752         fprintf(stderr, "ERROR: invalid number \"%s\".\n", next_args);
753         return 1;
754      }
755
756    e = e_connman_device_get(device_path);
757    if (e_connman_device_scan_interval_set(e, scan_interval, NULL, NULL))
758      printf(":::Device %s scan interval set to %hu\n", device_path, scan_interval);
759    else
760      fputs("ERROR: can't set device scan interval\n", stderr);
761    return 1;
762 }
763
764 static int
765 _on_cmd_device_get_scanning(char *cmd, char *args)
766 {
767    char *path;
768    bool scanning;
769    E_Connman_Element *e;
770
771    if (!args)
772      {
773         fputs("ERROR: missing the device path\n", stderr);
774         return 1;
775      }
776    _tok(args);
777    path = args;
778
779    e = e_connman_device_get(path);
780    if (e_connman_device_scanning_get(e, &scanning))
781      printf(":::Device %s Scanning = %hhu\n", path, scanning);
782    else
783      fputs("ERROR: can't get device scanning\n", stderr);
784    return 1;
785 }
786
787 static int
788 _on_cmd_device_get_networks(char *cmd, char *args)
789 {
790    E_Connman_Element **networks;
791    unsigned int count;
792    char *path;
793    E_Connman_Element *e;
794
795    if (!args)
796      {
797         fputs("ERROR: missing the device path\n", stderr);
798         return 1;
799      }
800    _tok(args);
801    path = args;
802
803    e = e_connman_device_get(path);
804    if (!e_connman_device_networks_get(e, &count, &networks))
805      {
806         fputs("ERROR: can't get networks\n", stderr);
807         return 1;
808      }
809
810    printf("BEG: all device network elements count = %d\n", count);
811    _elements_print(networks, count);
812    return 1;
813 }
814
815 /* Profile Commands */
816
817 static int
818 _on_cmd_profile_get_name(char *cmd, char *args)
819 {
820    const char *name, *path;
821    E_Connman_Element *e;
822
823    if (!args)
824      {
825         fputs("ERROR: missing the profile path\n", stderr);
826         return 1;
827      }
828    _tok(args);
829    path = args;
830
831    e = e_connman_profile_get(path);
832    if (e_connman_profile_name_get(e, &name))
833      printf(":::Profile %s Name = \"%s\"\n", path, name);
834    else
835      fputs("ERROR: can't get profile name\n", stderr);
836    return 1;
837 }
838
839 /* Connection Commands */
840
841 static int
842 _on_cmd_connection_get_type(char *cmd, char *args)
843 {
844    const char *type, *path;
845    E_Connman_Element *e;
846
847    if (!args)
848      {
849         fputs("ERROR: missing the connection path\n", stderr);
850         return 1;
851      }
852    _tok(args);
853    path = args;
854
855    e = e_connman_connection_get(path);
856    if (e_connman_connection_type_get(e, &type))
857      printf(":::Connection %s Type = \"%s\"\n", path, type);
858    else
859      fputs("ERROR: can't get connection type\n", stderr);
860    return 1;
861 }
862
863 static int
864 _on_cmd_connection_get_interface(char *cmd, char *args)
865 {
866    const char *interface, *path;
867    E_Connman_Element *e;
868
869    if (!args)
870      {
871         fputs("ERROR: missing the connection path\n", stderr);
872         return 1;
873      }
874    _tok(args);
875    path = args;
876
877    e = e_connman_connection_get(path);
878    if (e_connman_connection_interface_get(e, &interface))
879      printf(":::Connection %s Interface = \"%s\"\n", path, interface);
880    else
881      fputs("ERROR: can't get connection type\n", stderr);
882    return 1;
883 }
884
885 static int
886 _on_cmd_connection_get_device(char *cmd, char *args)
887 {
888    E_Connman_Element *e, *device;
889    char *path;
890
891    if (!args)
892      {
893         fputs("ERROR: missing the connection path\n", stderr);
894         return 1;
895      }
896    _tok(args);
897    path = args;
898
899    e = e_connman_connection_get(path);
900    if (!e_connman_connection_device_get(e, &device))
901      fputs("ERROR: can't get connection device\n", stderr);
902    else
903      e_connman_element_print(stderr, device);
904    return 1;
905 }
906
907 static int
908 _on_cmd_connection_get_network(char *cmd, char *args)
909 {
910    E_Connman_Element *e, *network;
911    char *path;
912
913    if (!args)
914      {
915         fputs("ERROR: missing the connection path\n", stderr);
916         return 1;
917      }
918    _tok(args);
919    path = args;
920
921    e = e_connman_connection_get(path);
922    if (!e_connman_connection_network_get(e, &network))
923      fputs("ERROR: can't get connection network\n", stderr);
924    else
925      e_connman_element_print(stderr, network);
926    return 1;
927 }
928
929 static int
930 _on_cmd_connection_get_strength(char *cmd, char *args)
931 {
932    char *path;
933    unsigned char strength;
934    E_Connman_Element *e;
935
936    if (!args)
937      {
938         fputs("ERROR: missing the connection path\n", stderr);
939         return 1;
940      }
941    _tok(args);
942    path = args;
943
944    e = e_connman_connection_get(path);
945    if (e_connman_connection_strength_get(e, &strength))
946      printf(":::Connection %s Strength = %#02hhx (%d)\n", path, strength, strength);
947    else
948      fputs("ERROR: can't get connection strength\n", stderr);
949    return 1;
950 }
951
952 static int
953 _on_cmd_connection_get_default(char *cmd, char *args)
954 {
955    char *path;
956    bool connection_default;
957    E_Connman_Element *e;
958
959    if (!args)
960      {
961         fputs("ERROR: missing the connection path\n", stderr);
962         return 1;
963      }
964    _tok(args);
965    path = args;
966
967    e = e_connman_connection_get(path);
968    if (e_connman_connection_default_get(e, &connection_default))
969      printf(":::Connection %s Default = %hhu\n", path, connection_default);
970    else
971      fputs("ERROR: can't get connection default\n", stderr);
972    return 1;
973 }
974
975 static int
976 _on_cmd_connection_get_ipv4_method(char *cmd, char *args)
977 {
978    const char *method, *path;
979    E_Connman_Element *e;
980
981    if (!args)
982      {
983         fputs("ERROR: missing the connection path\n", stderr);
984         return 1;
985      }
986    _tok(args);
987    path = args;
988
989    e = e_connman_connection_get(path);
990    if (e_connman_connection_ipv4_method_get(e, &method))
991      printf(":::Connection %s IPv4 Method = \"%s\"\n", path, method);
992    else
993      fputs("ERROR: can't get connection ipv4 method\n", stderr);
994    return 1;
995 }
996
997 static int
998 _on_cmd_connection_get_ipv4_address(char *cmd, char *args)
999 {
1000    const char *address, *path;
1001    E_Connman_Element *e;
1002
1003    if (!args)
1004      {
1005         fputs("ERROR: missing the connection path\n", stderr);
1006         return 1;
1007      }
1008    _tok(args);
1009    path = args;
1010
1011    e = e_connman_connection_get(path);
1012    if (e_connman_connection_ipv4_address_get(e, &address))
1013      printf(":::Connection %s IPv4 Address = \"%s\"\n", path, address);
1014    else
1015      fputs("ERROR: can't get connection ipv4 address\n", stderr);
1016    return 1;
1017 }
1018
1019 /* Network Commands */
1020
1021 static int
1022 _on_cmd_network_connect(char *cmd, char *args)
1023 {
1024    char *path;
1025    E_Connman_Element *e;
1026
1027    if (!args)
1028      {
1029         fputs("ERROR: missing the network path\n", stderr);
1030         return 1;
1031      }
1032    _tok(args);
1033    path = args;
1034
1035    e = e_connman_network_get(path);
1036    if (e_connman_network_connect(e, NULL, NULL))
1037      printf(":::Connecting to Network %s...\n", path);
1038    else
1039      fputs("ERROR: can't connect to network\n", stderr);
1040    return 1;
1041 }
1042
1043 static int
1044 _on_cmd_network_disconnect(char *cmd, char *args)
1045 {
1046    char *path;
1047    E_Connman_Element *e;
1048
1049    if (!args)
1050      {
1051         fputs("ERROR: missing the network path\n", stderr);
1052         return 1;
1053      }
1054    _tok(args);
1055    path = args;
1056
1057    e = e_connman_network_get(path);
1058    if (e_connman_network_disconnect(e, NULL, NULL))
1059      printf(":::Disconnecting Network %s...\n", path);
1060    else
1061      fputs("ERROR: can't disconnect network\n", stderr);
1062    return 1;
1063 }
1064
1065 static int
1066 _on_cmd_network_get_name(char *cmd, char *args)
1067 {
1068    const char *name, *path;
1069    E_Connman_Element *e;
1070
1071    if (!args)
1072      {
1073         fputs("ERROR: missing the network path\n", stderr);
1074         return 1;
1075      }
1076    _tok(args);
1077    path = args;
1078
1079    e = e_connman_network_get(path);
1080    if (e_connman_network_name_get(e, &name))
1081      printf(":::Network %s Name = \"%s\"\n", path, name);
1082    else
1083      fputs("ERROR: can't get network name\n", stderr);
1084    return 1;
1085 }
1086
1087 static int
1088 _on_cmd_network_get_available(char *cmd, char *args)
1089 {
1090    const char *path;
1091    bool available;
1092    E_Connman_Element *e;
1093
1094    if (!args)
1095      {
1096         fputs("ERROR: missing the network path\n", stderr);
1097         return 1;
1098      }
1099    _tok(args);
1100    path = args;
1101
1102    e = e_connman_network_get(path);
1103    if (e_connman_network_available_get(e, &available))
1104      printf(":::Network %s Available = %hhu\n", path, available);
1105    else
1106      fputs("ERROR: can't get network available\n", stderr);
1107    return 1;
1108 }
1109
1110 static int
1111 _on_cmd_network_get_connected(char *cmd, char *args)
1112 {
1113    char *path;
1114    bool connected;
1115    E_Connman_Element *e;
1116
1117    if (!args)
1118      {
1119         fputs("ERROR: missing the network path\n", stderr);
1120         return 1;
1121      }
1122    _tok(args);
1123    path = args;
1124
1125    e = e_connman_network_get(path);
1126    if (e_connman_network_connected_get(e, &connected))
1127      printf(":::Network %s Connected = %hhu\n", path, connected);
1128    else
1129      fputs("ERROR: can't get network connected\n", stderr);
1130    return 1;
1131 }
1132
1133 static int
1134 _on_cmd_network_get_remember(char *cmd, char *args)
1135 {
1136    char *path;
1137    bool remember;
1138    E_Connman_Element *e;
1139
1140    if (!args)
1141      {
1142         fputs("ERROR: missing the network path\n", stderr);
1143         return 1;
1144      }
1145    _tok(args);
1146    path = args;
1147
1148    e = e_connman_network_get(path);
1149    if (e_connman_network_remember_get(e, &remember))
1150      printf(":::Network %s Remember = %hhu\n", path, remember);
1151    else
1152      fputs("ERROR: can't get network remember\n", stderr);
1153    return 1;
1154 }
1155
1156 static int
1157 _on_cmd_network_set_remember(char *cmd, char *args)
1158 {
1159    char *network_path, *next_args;
1160    bool remember;
1161    E_Connman_Element *e;
1162
1163    if (!args)
1164      {
1165         fputs("ERROR: missing the network path\n", stderr);
1166         return 1;
1167      }
1168    network_path = args;
1169    next_args = _tok(args);
1170    if (!next_args)
1171      {
1172         fputs("ERROR: missing the remember value\n", stderr);
1173         return 1;
1174      }
1175    _tok(next_args);
1176    remember = !!atol(next_args);
1177
1178    e = e_connman_network_get(network_path);
1179    if (e_connman_network_remember_set(e, remember, NULL, NULL))
1180      printf(":::Network %s remember set to %d\n", network_path, remember);
1181    else
1182      fputs("ERROR: can't set network remember\n", stderr);
1183    return 1;
1184 }
1185
1186 static int
1187 _on_cmd_network_get_strength(char *cmd, char *args)
1188 {
1189    char *path;
1190    unsigned char strength;
1191    E_Connman_Element *e;
1192
1193    if (!args)
1194      {
1195         fputs("ERROR: missing the network path\n", stderr);
1196         return 1;
1197      }
1198    _tok(args);
1199    path = args;
1200
1201    e = e_connman_network_get(path);
1202    if (e_connman_network_strength_get(e, &strength))
1203      printf(":::Network %s Strength = %#02hhx (%d)\n", path, strength, strength);
1204    else
1205      fputs("ERROR: can't get network strength\n", stderr);
1206    return 1;
1207 }
1208
1209 static int
1210 _on_cmd_network_get_device(char *cmd, char *args)
1211 {
1212    E_Connman_Element *e, *device;
1213    char *path;
1214
1215    if (!args)
1216      {
1217         fputs("ERROR: missing the network path\n", stderr);
1218         return 1;
1219      }
1220    _tok(args);
1221    path = args;
1222
1223    e = e_connman_network_get(path);
1224    if (!e_connman_network_device_get(e, &device))
1225      fputs("ERROR: can't get network device\n", stderr);
1226    else
1227      e_connman_element_print(stderr, device);
1228    return 1;
1229 }
1230
1231 static int
1232 _on_cmd_network_get_wifi_ssid(char *cmd, char *args)
1233 {
1234    unsigned char *bytes;
1235    char *path;
1236    unsigned int i, count;
1237    E_Connman_Element *e;
1238
1239    if (!args)
1240      {
1241         fputs("ERROR: missing the network path\n", stderr);
1242         return 1;
1243      }
1244    _tok(args);
1245    path = args;
1246
1247    e = e_connman_network_get(path);
1248    if (e_connman_network_wifi_ssid_get(e, &count, &bytes))
1249      {
1250         printf(":::Network %s Wifi SSID = ", path);
1251         for (i = 0; i < count; i++)
1252           printf("%#02hhx (\"%c\"), ", bytes[i], bytes[i]);
1253         printf("\n");
1254      }
1255    else
1256      fputs("ERROR: can't get network wifi ssid\n", stderr);
1257    return 1;
1258 }
1259
1260 static int
1261 _on_cmd_network_get_wifi_mode(char *cmd, char *args)
1262 {
1263    const char *wifi_mode, *path;
1264    E_Connman_Element *e;
1265
1266    if (!args)
1267      {
1268         fputs("ERROR: missing the network path\n", stderr);
1269         return 1;
1270      }
1271    _tok(args);
1272    path = args;
1273
1274    e = e_connman_network_get(path);
1275    if (e_connman_network_wifi_mode_get(e, &wifi_mode))
1276      printf(":::Network %s Wifi Mode = \"%s\"\n", path, wifi_mode);
1277    else
1278      fputs("ERROR: can't get network wifi mode\n", stderr);
1279    return 1;
1280 }
1281
1282 static int
1283 _on_cmd_network_set_wifi_mode(char *cmd, char *args)
1284 {
1285    char *wifi_mode;
1286    const char *network_path;
1287    E_Connman_Element *e;
1288
1289    if (!args)
1290      {
1291         fputs("ERROR: missing the network path\n", stderr);
1292         return 1;
1293      }
1294    network_path = args;
1295    wifi_mode = _tok(args);
1296
1297    if (!wifi_mode)
1298      {
1299         fputs("ERROR: missing the wifi mode value\n", stderr);
1300         return 1;
1301      }
1302    _tok(wifi_mode);
1303
1304    e = e_connman_network_get(network_path);
1305    if (e_connman_network_wifi_mode_set(e, wifi_mode, NULL, NULL))
1306      printf(":::Network %s wifi mode set to \"%s\"\n", network_path, wifi_mode);
1307    else
1308      fputs("ERROR: can't set network wifi mode\n", stderr);
1309    return 1;
1310 }
1311
1312 static int
1313 _on_cmd_network_get_wifi_security(char *cmd, char *args)
1314 {
1315    const char *wifi_security, *path;
1316    E_Connman_Element *e;
1317
1318    if (!args)
1319      {
1320         fputs("ERROR: missing the network path\n", stderr);
1321         return 1;
1322      }
1323    _tok(args);
1324    path = args;
1325
1326    e = e_connman_network_get(path);
1327    if (e_connman_network_wifi_security_get(e, &wifi_security))
1328      printf(":::Network %s Wifi Security = \"%s\"\n", path, wifi_security);
1329    else
1330      fputs("ERROR: can't get network wifi security\n", stderr);
1331    return 1;
1332 }
1333
1334 static int
1335 _on_cmd_network_set_wifi_security(char *cmd, char *args)
1336 {
1337    char *wifi_security, *network_path;
1338    E_Connman_Element *e;
1339
1340    if (!args)
1341      {
1342         fputs("ERROR: missing the network path\n", stderr);
1343         return 1;
1344      }
1345    network_path = args;
1346    wifi_security = _tok(args);
1347
1348    if (!wifi_security)
1349      {
1350         fputs("ERROR: missing the wifi security value\n", stderr);
1351         return 1;
1352      }
1353    _tok(wifi_security);
1354
1355    e = e_connman_network_get(network_path);
1356    if (e_connman_network_wifi_security_set(e, wifi_security, NULL, NULL))
1357      printf(":::Network %s wifi security set to \"%s\"\n", network_path, wifi_security);
1358    else
1359      fputs("ERROR: can't set network wifi security\n", stderr);
1360    return 1;
1361 }
1362
1363 static int
1364 _on_cmd_network_get_wifi_passphrase(char *cmd, char *args)
1365 {
1366    const char *wifi_passphrase, *path;
1367    E_Connman_Element *e;
1368
1369    if (!args)
1370      {
1371         fputs("ERROR: missing the network path\n", stderr);
1372         return 1;
1373      }
1374    _tok(args);
1375    path = args;
1376
1377    e = e_connman_network_get(path);
1378    if (e_connman_network_wifi_passphrase_get(e, &wifi_passphrase))
1379      printf(":::Network %s Wifi Passphrase = \"%s\"\n", path, wifi_passphrase);
1380    else
1381      fputs("ERROR: can't get network wifi passphrase\n", stderr);
1382    return 1;
1383 }
1384
1385 static int
1386 _on_cmd_network_set_wifi_passphrase(char *cmd, char *args)
1387 {
1388    char *wifi_passphrase, *network_path;
1389    E_Connman_Element *e;
1390
1391    if (!args)
1392      {
1393         fputs("ERROR: missing the network path\n", stderr);
1394         return 1;
1395      }
1396    network_path = args;
1397    wifi_passphrase = _tok(args);
1398
1399    if (!wifi_passphrase)
1400      {
1401         fputs("ERROR: missing the wifi passphrase value\n", stderr);
1402         return 1;
1403      }
1404    _tok(wifi_passphrase);
1405
1406    e = e_connman_network_get(network_path);
1407    if (e_connman_network_wifi_passphrase_set(e, wifi_passphrase, NULL, NULL))
1408      printf(":::Network %s wifi passphrase set to \"%s\"\n", network_path, wifi_passphrase);
1409    else
1410      fputs("ERROR: can't set network wifi passphrase\n", stderr);
1411    return 1;
1412 }
1413
1414
1415 static int
1416 _on_input(void *data, Ecore_Fd_Handler *fd_handler)
1417 {
1418    char buf[256];
1419    char *cmd, *args;
1420    const struct {
1421       const char *cmd;
1422       int (*cb)(char *cmd, char *args);
1423    } *itr, maps[] = {
1424      {"quit", _on_cmd_quit},
1425      {"sync", _on_cmd_sync},
1426      {"get_all", _on_cmd_get_all},
1427      {"print", _on_cmd_print},
1428      {"get_properties", _on_cmd_get_properties},
1429      {"set_property", _on_cmd_property_set},
1430      {"manager_get", _on_cmd_manager_get},
1431      {"manager_get_profiles", _on_cmd_manager_get_profiles},
1432      {"manager_get_devices", _on_cmd_manager_get_devices},
1433      {"manager_get_connections", _on_cmd_manager_get_connections},
1434      {"manager_register_agent", _on_cmd_manager_register_agent},
1435      {"manager_unregister_agent", _on_cmd_manager_unregister_agent},
1436      {"manager_get_state", _on_cmd_manager_get_state},
1437      {"manager_get_policy", _on_cmd_manager_get_policy},
1438      {"manager_set_policy", _on_cmd_manager_set_policy},
1439      {"manager_get_offline_mode", _on_cmd_manager_get_offline_mode},
1440      {"manager_set_offline_mode", _on_cmd_manager_set_offline_mode},
1441      {"device_create_network", _on_cmd_device_create_network},
1442      {"device_remove_network", _on_cmd_device_remove_network},
1443      {"device_propose_scan", _on_cmd_device_propose_scan},
1444      {"device_get_name", _on_cmd_device_get_name},
1445      {"device_get_type", _on_cmd_device_get_type},
1446      {"device_get_interface", _on_cmd_device_get_interface},
1447      {"device_get_policy", _on_cmd_device_get_policy},
1448      {"device_set_policy", _on_cmd_device_set_policy},
1449      {"device_get_priority", _on_cmd_device_get_priority},
1450      {"device_set_priority", _on_cmd_device_set_priority},
1451      {"device_get_powered", _on_cmd_device_get_powered},
1452      {"device_set_powered", _on_cmd_device_set_powered},
1453      {"device_get_scan_interval", _on_cmd_device_get_scan_interval},
1454      {"device_set_scan_interval", _on_cmd_device_set_scan_interval},
1455      {"device_get_scanning", _on_cmd_device_get_scanning},
1456      {"device_get_networks", _on_cmd_device_get_networks},
1457      {"profile_get_name", _on_cmd_profile_get_name},
1458      {"connection_get_type", _on_cmd_connection_get_type},
1459      {"connection_get_interface", _on_cmd_connection_get_interface},
1460      {"connection_get_strength", _on_cmd_connection_get_strength},
1461      {"connection_get_default", _on_cmd_connection_get_default},
1462      {"connection_get_device", _on_cmd_connection_get_device},
1463      {"connection_get_network", _on_cmd_connection_get_network},
1464      {"connection_get_ipv4_method", _on_cmd_connection_get_ipv4_method},
1465      {"connection_get_ipv4_address", _on_cmd_connection_get_ipv4_address},
1466      {"network_connect", _on_cmd_network_connect},
1467      {"network_disconnect", _on_cmd_network_disconnect},
1468      {"network_get_name", _on_cmd_network_get_name},
1469      {"network_get_available", _on_cmd_network_get_available},
1470      {"network_get_connected", _on_cmd_network_get_connected},
1471      {"network_get_remember", _on_cmd_network_get_remember},
1472      {"network_set_remember", _on_cmd_network_set_remember},
1473      {"network_get_strength", _on_cmd_network_get_strength},
1474      {"network_get_device", _on_cmd_network_get_device},
1475      {"network_get_wifi_ssid", _on_cmd_network_get_wifi_ssid},
1476      {"network_get_wifi_mode", _on_cmd_network_get_wifi_mode},
1477      {"network_set_wifi_mode", _on_cmd_network_set_wifi_mode},
1478      {"network_get_wifi_security", _on_cmd_network_get_wifi_security},
1479      {"network_set_wifi_security", _on_cmd_network_set_wifi_security},
1480      {"network_get_wifi_passphrase", _on_cmd_network_get_wifi_passphrase},
1481      {"network_set_wifi_passphrase", _on_cmd_network_set_wifi_passphrase},
1482      {NULL, NULL}
1483    };
1484
1485    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
1486      {
1487         fputs("ERROR: reading from stdin, exit\n", stderr);
1488         return 0;
1489      }
1490
1491    if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1492      {
1493         fputs("ERROR: nothing to read?\n", stderr);
1494         return 0;
1495      }
1496
1497    if (!fgets(buf, sizeof(buf), stdin))
1498      {
1499         fprintf(stderr, "ERROR: could not read command: %s\n", strerror(errno));
1500         ecore_main_loop_quit();
1501         return 0;
1502      }
1503
1504    cmd = buf;
1505    while (isspace(*cmd))
1506      cmd++;
1507
1508    args = strchr(cmd, ' ');
1509    if (args)
1510      {
1511         char *p;
1512
1513         *args = '\0';
1514         args++;
1515
1516         while (isspace(*args))
1517           args++;
1518
1519         p = args + strlen(args) - 1;
1520         if (*p == '\n')
1521           *p = '\0';
1522      }
1523    else
1524      {
1525         char *p;
1526
1527         p = cmd + strlen(cmd) - 1;
1528         if (*p == '\n')
1529           *p = '\0';
1530      }
1531
1532    if (strcmp(cmd, "help") == 0)
1533      {
1534         if (args)
1535           {
1536              printf("Commands with '%s' in the name:\n", args);
1537              for (itr = maps; itr->cmd != NULL; itr++)
1538                if (strstr(itr->cmd, args))
1539                  printf("\t%s\n", itr->cmd);
1540           }
1541         else
1542           {
1543              fputs("Commands:\n", stdout);
1544              for (itr = maps; itr->cmd != NULL; itr++)
1545                printf("\t%s\n", itr->cmd);
1546           }
1547         fputc('\n', stdout);
1548         return 1;
1549      }
1550
1551    for (itr = maps; itr->cmd != NULL; itr++)
1552      if (strcmp(itr->cmd, cmd) == 0)
1553        return itr->cb(cmd, args);
1554
1555    printf("unknown command \"%s\", args=%s\n", cmd, args);
1556    return 1;
1557 }
1558
1559 int
1560 main(int argc, char *argv[])
1561 {
1562    E_DBus_Connection *c;
1563
1564    ecore_init();
1565    e_dbus_init();
1566    eina_init();
1567
1568    c = e_dbus_bus_get(DBUS_BUS_SYSTEM);
1569    if (!c) {
1570       printf("ERROR: can't connect to system session\n");
1571       return -1;
1572    }
1573
1574    e_connman_system_init(c);
1575    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_ADD, _on_element_add, NULL);
1576    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_DEL, _on_element_del, NULL);
1577    ecore_event_handler_add(E_CONNMAN_EVENT_ELEMENT_UPDATED,
1578                            _on_element_updated, NULL);
1579
1580    ecore_main_fd_handler_add
1581      (0, ECORE_FD_READ | ECORE_FD_ERROR, _on_input, NULL, NULL, NULL);
1582
1583    ecore_main_loop_begin();
1584
1585    e_connman_system_shutdown();
1586
1587    e_dbus_connection_close(c);
1588    eina_shutdown();
1589    e_dbus_shutdown();
1590    ecore_shutdown();
1591
1592    fputs("DBG: clean exit.\n", stderr);
1593
1594    return 0;
1595 }