summaryrefslogtreecommitdiff
path: root/src/bin/eeze/eeze_scanner_monitor/eeze_scanner_monitor.c
blob: c5a6a057cbd69f803185c45193d2fb95e6f0cea2 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define EFL_BETA_API_SUPPORT 1
#define EFL_EO_API_SUPPORT 1
#include <Eet.h>
#include <Ecore.h>
#include <Ecore_Con.h>
#include "../eeze_scanner/eeze_scanner.h"

static Eet_Data_Descriptor *edd;

static int retval = EXIT_SUCCESS;

static Eina_Bool
_eet_read(const void *eet_data, size_t size, void *user_data EINA_UNUSED)
{
   Eeze_Scanner_Event *ev = eet_data_descriptor_decode(edd, eet_data, size);

   if (!ev)
     {
        fprintf(stderr, "ERROR: could not decode event!\n");
        goto error;
     }

   switch (ev->type)
     {
      case EEZE_SCANNER_EVENT_TYPE_ADD:
         printf("ADD ");
         break;
      case EEZE_SCANNER_EVENT_TYPE_REMOVE:
         printf("DEL ");
         break;
      case EEZE_SCANNER_EVENT_TYPE_CHANGE:
         printf("CHG ");
         break;
      default:
         fprintf(stderr, "ERROR: unknown event type %d\n", ev->type);
         goto error;
     }

   if (ev->volume)
     printf("VOLUME ");

   printf("'%s'\n", ev->device);
   free(ev);

   return EINA_TRUE;

 error:
   retval = EXIT_FAILURE;
   ecore_main_loop_quit();
   return EINA_FALSE;
}

static Eina_Bool
_eet_write(const void *eet_data EINA_UNUSED, size_t size EINA_UNUSED, void *user_data EINA_UNUSED)
{
   fprintf(stderr, "ERROR: should not write data!\n");
   retval = EXIT_FAILURE;
   ecore_main_loop_quit();
   return EINA_FALSE;
}

static void
_on_data(void *data, const Efl_Event *event)
{
   Eet_Connection *ec = data;
   Eo *dialer = event->object;
   Eina_Slice slice;

   slice = efl_io_buffered_stream_slice_get(dialer);
   if (slice.len == 0) return;

   if (eet_connection_received(ec, slice.mem, slice.len) != 0)
     {
        fprintf(stderr, "ERROR: received invalid data\n");
        goto error;
     }

   efl_io_buffered_stream_discard(dialer, slice.len);

   return;

 error:
   retval = EXIT_FAILURE;
   ecore_main_loop_quit();
}

static void
_finished(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
   ecore_main_loop_quit();
}

static void
_error(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eo *dialer = event->object;
   Eina_Error *perr = event->info;

   fprintf(stderr, "ERROR: error communicating to %s: %s\n",
           efl_net_dialer_address_dial_get(dialer),
           eina_error_msg_get(*perr));
   retval = EXIT_FAILURE;
   ecore_main_loop_quit();
}

int
main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
   Eet_Data_Descriptor_Class eddc;
   Eo *loop;
   char *path;
   Eo *dialer;
   Eet_Connection *ec;
   Eina_Error err;

   ecore_app_no_system_modules();

   eina_init();
   ecore_init();
   eet_init();
   ecore_con_init();

   if (!eet_eina_stream_data_descriptor_class_set(&eddc, sizeof(eddc), "eeze_scanner_event", sizeof(Eeze_Scanner_Event)))
     {
        fprintf(stderr, "ERROR: could not create eet data descriptor!\n");
        retval = EXIT_FAILURE;
        goto error_edd;
     }
   edd = eet_data_descriptor_stream_new(&eddc);
   if (!edd)
     {
        fprintf(stderr, "ERROR: could not create eet data descriptor!\n");
        retval = EXIT_FAILURE;
        goto error_edd;
     }
#define DAT(MEMBER, TYPE) EET_DATA_DESCRIPTOR_ADD_BASIC(edd, Eeze_Scanner_Event, #MEMBER, MEMBER, EET_T_##TYPE)
   DAT(device, INLINED_STRING);
   DAT(type, UINT);
   DAT(volume, UCHAR);
#undef DAT

   path = ecore_con_local_path_new(EINA_TRUE, "eeze_scanner", 0);
   if (!path)
     {
        fprintf(stderr, "ERROR: could not get local communication path\n");
        retval = EXIT_FAILURE;
        goto error_path;
     }

   loop = ecore_main_loop_get();

#ifdef EFL_NET_DIALER_UNIX_CLASS
   dialer = efl_add(EFL_NET_DIALER_SIMPLE_CLASS, loop,
                    efl_net_dialer_simple_inner_class_set(efl_added, EFL_NET_DIALER_UNIX_CLASS));
#elif defined(EFL_NET_DIALER_WINDOWS_CLASS)
   dialer = efl_add(EFL_NET_DIALER_SIMPLE_CLASS, loop,
                    efl_net_dialer_simple_inner_class_set(efl_added, EFL_NET_DIALER_WINDOWS_CLASS));
#else
   fprintf(stderr, "ERROR: your platform doesn't support Efl.Net.Dialer.*\n");
#endif
   if (!dialer)
     {
        fprintf(stderr, "ERROR: could not create communication dialer\n");
        retval = EXIT_FAILURE;
        goto end;
     }

   ec = eet_connection_new(_eet_read, _eet_write, dialer);
   if (!ec)
     {
        fprintf(stderr, "ERROR: could not create Eet_Connection\n");
        retval = EXIT_FAILURE;
        goto end;
     }
   efl_event_callback_add(dialer, EFL_IO_BUFFERED_STREAM_EVENT_SLICE_CHANGED, _on_data, ec);


   efl_event_callback_add(dialer, EFL_IO_BUFFERED_STREAM_EVENT_ERROR, _error, NULL);
   efl_event_callback_add(dialer, EFL_IO_BUFFERED_STREAM_EVENT_READ_FINISHED, _finished, NULL);

   err = efl_net_dialer_dial(dialer, path);
   if (err)
     {
        fprintf(stderr, "ERROR: could not connect '%s': %s\n", path, eina_error_msg_get(err));
        retval = EXIT_FAILURE;
        goto end_dial;
     }

   ecore_main_loop_begin();

 end_dial:
   if (!efl_io_closer_closed_get(dialer))
     efl_io_closer_close(dialer);

   efl_event_callback_del(dialer, EFL_IO_BUFFERED_STREAM_EVENT_SLICE_CHANGED, _on_data, ec);
   eet_connection_close(ec, NULL);
   ec = NULL;

 end:
   efl_del(dialer);
   dialer = NULL;

   free(path);
   path = NULL;
 error_path:
   eet_data_descriptor_free(edd);
   edd = NULL;

 error_edd:
   ecore_con_shutdown();
   eet_shutdown();
   ecore_shutdown();
   eina_shutdown();
   return retval;
}