summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Hirt <daniel.hirt@samsung.com>2015-09-21 19:32:25 +0300
committerDaniel Hirt <daniel.hirt@samsung.com>2015-09-21 19:32:25 +0300
commit84f35c8d087aaa6d4566a4d4da4af8ced374f19f (patch)
tree4622c3ab0550fc09a24617c1110355292dc3653c
parent27f70ecf157afe2227dfb3fe4d6a7b05c460d273 (diff)
Add version 0.1 which is file-push baseddebugd-0.1
-rw-r--r--src/Makefile4
-rw-r--r--src/client.c236
-rw-r--r--src/debugd.c286
3 files changed, 332 insertions, 194 deletions
diff --git a/src/Makefile b/src/Makefile
index 94a83f4..f89f01b 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -5,10 +5,10 @@ targets = client debugd
5all: $(targets) 5all: $(targets)
6 6
7client: client.c 7client: client.c
8 $(CC) -o client client.c `pkg-config --cflags --libs efl ecore ecore-ipc` 8 $(CC) -o client client.c `pkg-config --cflags --libs efl ecore ecore-ipc` -Wall
9 9
10debugd: debugd.c 10debugd: debugd.c
11 $(CC) -o debugd debugd.c `pkg-config --cflags --libs efl ecore ecore-file ecore-ipc` 11 $(CC) -o debugd debugd.c `pkg-config --cflags --libs efl ecore ecore-ipc ecore-file` -Wall
12 12
13clean: 13clean:
14 rm client debugd 14 rm client debugd
diff --git a/src/client.c b/src/client.c
index a9b01f8..694afed 100644
--- a/src/client.c
+++ b/src/client.c
@@ -7,29 +7,19 @@
7#include <Ecore.h> 7#include <Ecore.h>
8/* We will create a socket in a specific port defined by TCP_SERVER_PORT */ 8/* We will create a socket in a specific port defined by TCP_SERVER_PORT */
9 9
10#define BUFFER_SIZE 1024 10#define BUFFER_SIZE 1000000
11 11
12Ecore_Exe *g_child = NULL; 12Ecore_Exe *g_child = NULL;
13char *g_bridge_file = NULL;
14Eina_Bool test_mode = EINA_FALSE; 13Eina_Bool test_mode = EINA_FALSE;
14char *g_bridge_file = NULL;
15 15
16static Eina_Bool 16//static int _auto_remaining_tests = 10000;
17_send_message_test(char *msg) 17static int _auto_remaining_tests = 10;
18{ 18static int _auto_buf_size;
19 if (!ecore_exe_send(g_child, msg, strlen(msg))) 19static char _auto_buf[BUFFER_SIZE];
20 {
21 fprintf(stderr, "Could not send message to child\n");
22 return EINA_FALSE;
23 }
24 else
25 {
26 fprintf(stderr, "Father: I've sent this message: %s\n", msg);
27 return EINA_TRUE;
28 }
29}
30 20
31static Eina_Bool 21static Eina_Bool
32_send_message(char *msg) 22_send_data(char *msg, int size)
33{ 23{
34 char tmp[1024]; 24 char tmp[1024];
35 char msg2[1024]; 25 char msg2[1024];
@@ -41,12 +31,14 @@ _send_message(char *msg)
41 31
42 msg2[0] = seq++; 32 msg2[0] = seq++;
43 33
44 strcpy(msg2 + 1, msg); 34 memcpy(msg2 + 1, msg, size);
45 35
46 FILE *f = fopen("msg", "w+"); 36 FILE *f = fopen("msg", "w+");
47 fprintf(f, "%s\n", msg2); 37 fwrite(msg2, sizeof(char), size, f);
38 fflush(f);
48 fclose(f); 39 fclose(f);
49 40
41 printf("Exec (size=%d): %s\n", size, tmp);
50 ecore_exe_run(tmp, NULL); 42 ecore_exe_run(tmp, NULL);
51 43
52 return EINA_TRUE; 44 return EINA_TRUE;
@@ -64,15 +56,99 @@ _fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
64 } 56 }
65 57
66 fprintf(stdout, "Father: User entered: %s\n", msg); 58 fprintf(stdout, "Father: User entered: %s\n", msg);
67 if (test_mode) 59 _send_data(msg, strlen(msg));
60 return ECORE_CALLBACK_RENEW;
61}
62
63static void
64_pattern_auto_generate()
65{
66 int i;
67 int *magic = (int *)_auto_buf, *size = (int *)(_auto_buf + sizeof(*magic));
68 char *msg = _auto_buf + sizeof(*magic) + sizeof(*size);
69 if (!_auto_remaining_tests--) exit(0);
70 do
68 { 71 {
69 _send_message_test(msg); 72 _auto_buf_size = rand() % 32000;
73 } while(!_auto_buf_size);
74 _auto_buf_size = 200;
75 *magic = 0xDEADBEEF;
76 *size = _auto_buf_size;
77 printf("Generating pattern: size %d\n", _auto_buf_size);
78 for (i = 0; i < _auto_buf_size; i++)
79 {
80 msg[i] = rand() % 256;
70 } 81 }
71 else 82 _send_data(_auto_buf, _auto_buf_size + sizeof(*magic) + sizeof(*size));
83}
84
85static Eina_Bool
86_msg_from_child_auto_handler(void *fdata EINA_UNUSED, int type EINA_UNUSED,
87 void *event)
88{
89 printf("Message from child\n");
90 Ecore_Exe_Event_Data *cdata = event;
91 if (!g_bridge_file)
72 { 92 {
73 _send_message(msg); 93 printf("Initializing bridge file (size=%d)\n", cdata->size);
94 char msg[1024];
95 strncpy(msg, cdata->data, cdata->size);
96 msg[cdata->size] = 0;
97 g_bridge_file = strdup(msg);
98 printf("Destination file set to %s\n", g_bridge_file);
99 return ECORE_CALLBACK_DONE;
74 } 100 }
75 return ECORE_CALLBACK_RENEW; 101
102 static char msg[BUFFER_SIZE];
103 static int cur = 0, remaining = 0;
104 int *magic, *size;
105 printf(" -- Received from bridge: %d bytes\n", cdata->size);
106 if (remaining <= 0)
107 {
108 printf(" ---- Doing magic checks\n");
109 magic = (int *)cdata->data;
110 size = (int *)(cdata->data + sizeof(*magic));
111
112 if (*magic != 0xDEADBEEF)
113 {
114 int off;
115 char str[cdata->size + 1];
116 printf("Bad magic number %X. Data size: %d\n", *magic, cdata->size);
117 strncpy(str, cdata->data, cdata->size);
118 str[cdata->size] = 0;
119 printf(" -- Maybe it's a string: %s\n", str);
120 for (off = 0; off < cdata->size; off++)
121 {
122 char c = ((char *)cdata->data)[off];
123 if (off) printf(", ");
124 printf("%c", c);
125 }
126 printf("\n");
127 return ECORE_CALLBACK_DONE;
128 }
129 remaining = *size + sizeof(*magic) + sizeof(*size);
130 }
131 remaining -= cdata->size;
132 memcpy(msg + cur, cdata->data, cdata->size);
133 cur += cdata->size;
134
135 if (remaining <= 0)
136 {
137 cur = 0;
138 magic = (int *)msg;
139 size = (int *)(msg + sizeof(*magic));
140 if (*magic != 0xDEADBEEF) printf("Bad magic number %X\n", *magic);
141 else if (_auto_buf_size != *size)
142 printf("Bad size (%d-%d) for received buffer\n", _auto_buf_size, *size);
143 else if (!memcmp(_auto_buf, msg+sizeof(*magic)+sizeof(*size), _auto_buf_size))
144 printf("Bad contents for received buffer\n");
145 else
146 {
147 printf("Received buffer ok\n");
148 _pattern_auto_generate();
149 }
150 }
151 return ECORE_CALLBACK_DONE;
76} 152}
77 153
78static Eina_Bool 154static Eina_Bool
@@ -81,33 +157,60 @@ _msg_from_child_handler(void *data EINA_UNUSED, int type EINA_UNUSED,
81{ 157{
82 Ecore_Exe_Event_Data *cdata = event; 158 Ecore_Exe_Event_Data *cdata = event;
83 char msg[BUFFER_SIZE]; 159 char msg[BUFFER_SIZE];
84 strncpy(msg, cdata->data, cdata->size); 160
161 memcpy(msg, cdata->data, cdata->size);
85 msg[cdata->size] = 0; 162 msg[cdata->size] = 0;
86 fprintf(stdout, "Father: my child sent me: %s\n", msg); 163 fprintf(stdout, "Father: my child sent me (size=%d): %s\n", cdata->size, msg);
87 164
88 /* First message initializes the file path to push */ 165 return ECORE_CALLBACK_DONE;
89 if (!g_bridge_file) 166}
167
168static Eina_Bool
169_su_timer(void *data EINA_UNUSED)
170{
171 Ecore_Exe *exe = data;
172 char *cmd = "su\n";
173 if (exe && !test_mode)
90 { 174 {
91 int len = strlen(msg); 175 /* Once we have the shell, we will run the bridge start command.
92 msg[len - 1] = 0; /* removing the newline */ 176 * This is a workaround because 'sdb shell [command]' does
93 g_bridge_file = strdup(msg); 177 * '/bin/sh -' */
94 fprintf(stdout, "Dest file set to %s\n", g_bridge_file); 178 printf("Executing bridge command on sdb shell\n");
179 if (!ecore_exe_send(exe, cmd, strlen(cmd)))
180 {
181 printf("Failed sending cmd to sdb exe\n");
182
183 }
95 } 184 }
96 185 return ECORE_CALLBACK_CANCEL;
97 return ECORE_CALLBACK_DONE;
98} 186}
99 187
100static int count = 0; 188static Eina_Bool
189_bridge_timer(void *data)
190{
191 char *cmd = "/home/developer/debugd bridge\n";
192 Ecore_Exe *exe = data;
193 if (exe && !test_mode)
194 {
195 /* Once we have the shell, we will run the bridge start command.
196 * This is a workaround because 'sdb shell [command]' does
197 * '/bin/sh -' */
198 printf("Executing bridge command on sdb shell\n");
199 if (!ecore_exe_send(exe, cmd, strlen(cmd)))
200 {
201 printf("Failed sending cmd to sdb exe\n");
202
203 }
204 }
205 return ECORE_CALLBACK_CANCEL;
206}
101 207
102static Ecore_Exe * 208static Ecore_Exe *
103_init_bridge(void) 209_init_bridge(void)
104{ 210{
105 /* Listen to port and after connect start receiving. 211 Ecore_Exe *exe;
106 * Recieved messages should contain ASCII text. Send back UPPERCASE 212 const char *cmd_bridge = "export PATH=$PATH:~/tizen-sdk/tools; sdb shell /home/developer/debugd bridge";
107 * conversion of these messages */ 213 const char *cmd_test = "./debugd bridge";
108
109 const char *cmd_bridge = "sdb shell /home/developer/debugd bridge";
110 const char *cmd_test = "./debugd bridge test";
111 214
112 const char *cmd; 215 const char *cmd;
113 const char *profile = getenv("DBG_PROFILE"); 216 const char *profile = getenv("DBG_PROFILE");
@@ -115,21 +218,24 @@ _init_bridge(void)
115 if (profile && !strcmp(profile, "test")) 218 if (profile && !strcmp(profile, "test"))
116 { 219 {
117 printf ("Starting test profile\n"); 220 printf ("Starting test profile\n");
118 cmd = cmd_test; 221 cmd_bridge = cmd_test;
119 test_mode = EINA_TRUE; 222 test_mode = EINA_TRUE;
120 } 223 }
121 else
122 {
123 cmd = cmd_bridge;
124 }
125 224
126 printf ("Starting bridge with command: %s\n", cmd); 225 printf ("Starting bridge with command: %s\n", cmd_bridge);
127 226
128 return ecore_exe_pipe_run(cmd, 227 exe = ecore_exe_pipe_run(cmd_bridge,
129 ECORE_EXE_PIPE_WRITE | 228 ECORE_EXE_PIPE_WRITE |
130 ECORE_EXE_PIPE_READ_LINE_BUFFERED | 229 ECORE_EXE_PIPE_READ,
131 ECORE_EXE_PIPE_READ, 230 NULL);
132 NULL); 231 return exe;
232}
233
234static Eina_Bool
235_on_timer(void *data EINA_UNUSED)
236{
237 _pattern_auto_generate();
238 return ECORE_CALLBACK_CANCEL;
133} 239}
134 240
135int main (int argc, char **argv) 241int main (int argc, char **argv)
@@ -137,25 +243,31 @@ int main (int argc, char **argv)
137 if (!ecore_init()) 243 if (!ecore_init())
138 goto exit; 244 goto exit;
139 245
140 Ecore_Exe *bridge = _init_bridge(); 246 g_child = _init_bridge();
141 247
142 if (!bridge) 248 if (!g_child)
143 { 249 {
144 fprintf(stderr, "Could not initialize bridge\n"); 250 fprintf(stderr, "Could not initialize bridge\n");
145 goto error; 251 goto error;
146 } 252 }
147 253
148 g_child = bridge; 254 if (argc > 1 && !strcmp(argv[1], "auto"))
149 255 {
150 ecore_main_fd_handler_add(STDIN_FILENO, 256 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_auto_handler, NULL);
151 ECORE_FD_READ, 257 ecore_timer_add(1.0, _on_timer, NULL);
152 _fd_handler_cb, 258 }
153 NULL, NULL, NULL); 259 else
154 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL); 260 {
261 ecore_main_fd_handler_add(STDIN_FILENO,
262 ECORE_FD_READ,
263 _fd_handler_cb,
264 NULL, NULL, NULL);
265 ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
266 }
155 267
156 ecore_main_loop_begin(); 268 ecore_main_loop_begin();
157 269
158 ecore_exe_free(bridge); /* Does not affect the child process */ 270 ecore_exe_free(g_child); /* Does not affect the child process */
159 ecore_shutdown(); 271 ecore_shutdown();
160 272
161 return EXIT_SUCCESS; 273 return EXIT_SUCCESS;
diff --git a/src/debugd.c b/src/debugd.c
index 2431222..b0470eb 100644
--- a/src/debugd.c
+++ b/src/debugd.c
@@ -2,10 +2,11 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include <unistd.h> 3#include <unistd.h>
4#include <Ecore.h> 4#include <Ecore.h>
5#include <Ecore_File.h>
5#include <Ecore_Ipc.h> 6#include <Ecore_Ipc.h>
6#include <ctype.h> 7#include <ctype.h>
7 8
8#define BUFFER_SIZE 1024 9#define BUFFER_SIZE 1000000
9////////////////////////////////////////////////////////////////////////// 10//////////////////////////////////////////////////////////////////////////
10/////////////////// Daemon (Server) ////////////////////////////////////// 11/////////////////// Daemon (Server) //////////////////////////////////////
11///////////////////////////////////////////////////////////////////////// 12/////////////////////////////////////////////////////////////////////////
@@ -16,60 +17,67 @@ static Ecore_Event_Handler *hnd_add = NULL;
16static Ecore_Event_Handler *hnd_del = NULL; 17static Ecore_Event_Handler *hnd_del = NULL;
17static Ecore_Event_Handler *hnd_data = NULL; 18static Ecore_Event_Handler *hnd_data = NULL;
18 19
19static Eina_Bool
20_cb_client_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
21{
22 return ECORE_CALLBACK_DONE;
23}
24
25static Eina_Bool
26_cb_client_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
27{
28 return ECORE_CALLBACK_DONE;
29}
30
31#define IPC_HEAD(_type) \ 20#define IPC_HEAD(_type) \
32 Ecore_Ipc_Event_Client_##_type *e = event; \ 21 Ecore_Ipc_Event_Client_##_type *e = event; \
33 if (ecore_ipc_client_server_get(e->client) != ipc) \ 22 if (ecore_ipc_client_server_get(e->client) != ipc) \
34 return ECORE_CALLBACK_PASS_ON 23 return ECORE_CALLBACK_PASS_ON
35 24
36static Eina_Bool 25static Eina_Bool
37_cb_client_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 26_daemon_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
38{ 27{
39 IPC_HEAD(Data); 28 IPC_HEAD(Add);
40 29 printf("Client %p connected\n", e->client);
41 char tmp[1024];
42 char *c;
43 30
44 strncpy(tmp, e->data, e->size); 31 return ECORE_CALLBACK_DONE;
45 32}
46#if 1
47 for (c = tmp; *c != 0; c++)
48 *c = toupper(toupper(*c));
49#endif
50 33
51 if (e->major == 1) 34static Eina_Bool
52 { 35_daemon_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
53 ecore_ipc_client_send(e->client, 1, 0, 0, 0, 0, tmp, e->size); 36{
54 } 37 IPC_HEAD(Del);
38 printf("Client deleted\n");
39 ecore_ipc_client_del(e->client);
55 return ECORE_CALLBACK_DONE; 40 return ECORE_CALLBACK_DONE;
56} 41}
57 42
58////// Server ////////////////////
59static void 43static void
60_broadcast(Ecore_Ipc_Server *svr, int major, int minor, void *data, int size) 44_broadcast(Ecore_Ipc_Server *svr, int major, int minor, void *data, int size,
45 Ecore_Ipc_Client *ignore)
61{ 46{
62 Eina_List *ipc_clients = ecore_ipc_server_clients_get(svr); 47 Eina_List *ipc_clients = ecore_ipc_server_clients_get(svr);
63 Eina_List *l; 48 Eina_List *l;
64 Ecore_Ipc_Client *cl; 49 Ecore_Ipc_Client *cl;
65 50
51 printf("Broadcasting\n");
52 if (eina_list_count(ipc_clients) == 0)
53 {
54 printf(" -- But there are no clients\n");
55 }
66 EINA_LIST_FOREACH(ipc_clients, l, cl) 56 EINA_LIST_FOREACH(ipc_clients, l, cl)
67 { 57 {
68 ecore_ipc_client_send(cl, major, minor, 0, 0, 0, data, size); 58 if (cl != ignore)
59 {
60 printf(" -- Sending to client %p (size: %d)\n", cl, size);
61 ecore_ipc_client_send(cl, major, minor, 0, 0, 0, data, size);
62 }
69 } 63 }
70} 64}
71 65
72static Eina_Bool 66static Eina_Bool
67_daemon_data_from_bridge(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
68{
69 printf("Hello world\n");
70 IPC_HEAD(Data);
71 int *magic = (int *)e->data;
72 printf("Received Data from client: size %d magic %X\n", e->size, *magic);
73
74 _broadcast(ipc, 1, 0, e->data, e->size, e->client);
75
76 return ECORE_CALLBACK_DONE;
77}
78
79////// Server ////////////////////
80static Eina_Bool
73_server_shutdown(void) 81_server_shutdown(void)
74{ 82{
75 if (!ipc) return EINA_TRUE; 83 if (!ipc) return EINA_TRUE;
@@ -91,18 +99,18 @@ _server_init(void)
91{ 99{
92 if (ipc) return EINA_TRUE; 100 if (ipc) return EINA_TRUE;
93 101
94 ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_USER, "debugd", 0, NULL); 102 ipc = ecore_ipc_server_add(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
95 if (!ipc) 103 if (!ipc)
96 { 104 {
97 _server_shutdown(); 105 _server_shutdown();
98 return EINA_FALSE; 106 return EINA_FALSE;
99 } 107 }
100 hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, 108 hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD,
101 _cb_client_add, NULL); 109 _daemon_add, NULL);
102 hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, 110 hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL,
103 _cb_client_del, NULL); 111 _daemon_del, NULL);
104 hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, 112 hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA,
105 _cb_client_data, NULL); 113 _daemon_data_from_bridge, NULL);
106 return EINA_TRUE; 114 return EINA_TRUE;
107} 115}
108 116
@@ -110,33 +118,23 @@ typedef enum
110{ 118{
111 DAEMON_MODE_NORMAL, 119 DAEMON_MODE_NORMAL,
112 DAEMON_MODE_TEST, 120 DAEMON_MODE_TEST,
113 DAEMON_MODE_BRIDGE_NORMAL, 121 DAEMON_MODE_BRIDGE
114 DAEMON_MODE_BRIDGE_TEST
115} Daemon_Mode; 122} Daemon_Mode;
116 123
117Eina_Bool 124Eina_Bool
118_daemon(Daemon_Mode mode) 125_daemon(void)
119{ 126{
120#if 1
121 if (!_server_init()) 127 if (!_server_init())
122 { 128 {
123 return EINA_FALSE; 129 return EINA_FALSE;
124 } 130 }
125#endif
126 ecore_main_loop_begin(); 131 ecore_main_loop_begin();
127#if 1
128 _server_shutdown(); 132 _server_shutdown();
129#endif
130 133
131 return EINA_TRUE; 134 return EINA_TRUE;
132} 135}
133 136
134//////////////////////////////////////////////////////////////////////////
135/////////////////// Bridge (Client) //////////////////////////////////////
136/////////////////////////////////////////////////////////////////////////
137
138/////// Bridge-Daemon Comm. ///////// 137/////// Bridge-Daemon Comm. /////////
139#include <Ecore_File.h>
140static Ecore_Ipc_Server *srv_ipc = NULL; 138static Ecore_Ipc_Server *srv_ipc = NULL;
141/* Server-to-client cb's */ 139/* Server-to-client cb's */
142static Ecore_Event_Handler *svr_hnd_add = NULL; 140static Ecore_Event_Handler *svr_hnd_add = NULL;
@@ -145,12 +143,6 @@ static Ecore_Event_Handler *svr_hnd_data = NULL;
145 143
146const char *debug_prefix = "."; 144const char *debug_prefix = ".";
147 145
148/* Send a message from bridge to daemon */
149static void
150_bridge_send(char *msg, int size)
151{
152 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, msg, size);
153}
154 146
155static Eina_Bool 147static Eina_Bool
156_cb_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 148_cb_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
@@ -170,17 +162,19 @@ _cb_server_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
170 return ECORE_CALLBACK_PASS_ON 162 return ECORE_CALLBACK_PASS_ON
171 163
172static Eina_Bool 164static Eina_Bool
173_cb_server_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) 165_bridge_data_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
174{ 166{
175 SVR_IPC_HEAD(Data); 167 SVR_IPC_HEAD(Data);
176 char tmp[1024];
177
178 strncpy(tmp, e->data, e->size);
179 tmp[e->size] = 0;
180 168
181 if (e->major == 1) // registration 169 if (e->major == 1) // registration
182 { 170 {
183 fprintf(stdout, "Server responded: %s\n", tmp); 171 int nb = fwrite(e->data, e->size, 1, stdout);
172 fflush(stdout);
173
174 FILE *sent = fopen("/home/developer/sent", "w+");
175 fprintf(sent, "item size: %d, written items: %d\n", e->size, nb);
176 fflush(sent);
177 fclose(sent);
184 } 178 }
185 return ECORE_CALLBACK_DONE; 179 return ECORE_CALLBACK_DONE;
186} 180}
@@ -197,24 +191,25 @@ _ipc_launch(void)
197 { 191 {
198 num++; 192 num++;
199 usleep(1000); 193 usleep(1000);
200 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_USER, "debugd", 0, NULL); 194 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
201 } 195 }
202 196
203 if (!srv_ipc) 197 if (!srv_ipc)
204 { 198 {
205 perror("Launched server but could not connect after 500 tries\n"); 199 //fprintf(stderr, "Launched server but could not connect after 500 tries\n");
206 } 200 }
207} 201}
208 202
209static Eina_Bool 203static Eina_Bool
210debug_daemon_init(void) 204_bridge_init(void)
211{ 205{
212 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_USER, "debugd", 0, NULL); 206 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
213 207
214 if (!srv_ipc) _ipc_launch(); 208 if (!srv_ipc) _ipc_launch();
215 209
216 if (!srv_ipc) 210 if (!srv_ipc)
217 { 211 {
212 while (1) {}
218 ecore_ipc_shutdown(); 213 ecore_ipc_shutdown();
219 return EINA_FALSE; 214 return EINA_FALSE;
220 } 215 }
@@ -223,16 +218,42 @@ debug_daemon_init(void)
223 svr_hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL, 218 svr_hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL,
224 _cb_server_del, NULL); 219 _cb_server_del, NULL);
225 svr_hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA, 220 svr_hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA,
226 _cb_server_data, NULL); 221 _bridge_data_from_daemon, NULL);
227 222
228 return EINA_TRUE;; 223 return EINA_TRUE;;
229} 224}
230 225
231/////// Bridge-CLI Comm ///////// 226/////// Bridge-CLI Comm /////////
227static Eina_Bool
228_fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
229{
230 char msg[BUFFER_SIZE], *tmp;
231 int *magic = (int *)msg, *size = (int *)(msg + sizeof(*magic));
232 int len;
233
234 fread(magic, sizeof(*magic), 1, stdin);
235 if (*magic != 0xDEADBEEF) return ECORE_CALLBACK_RENEW;
236 fread(size, sizeof(*size), 1, stdin);
237 len = *size;
238 //fprintf(stderr, "Bridge: have to receive from client %d+8 bytes\n", *size);
239 tmp = msg + sizeof(*magic) + sizeof(*size);
240 while(len > 0)
241 {
242 int nb = fread(tmp, 1, len, stdin);
243 if (nb <= 0)
244 {
245 //fprintf(stderr, "fread returns bad result\n");
246 return ECORE_CALLBACK_RENEW;
247 }
248 len -= nb;
249 tmp += nb;
250 }
251 //fprintf(stderr, "Bridge: received from client %d+8 bytes\n", *size);
252 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, msg, *size + sizeof(*magic) + sizeof(*size));
253 return ECORE_CALLBACK_RENEW;
254}
232 255
233///////// SDB MODE /////////////
234const char *tmp_dir = "/home/developer/tmp"; 256const char *tmp_dir = "/home/developer/tmp";
235int tmpcount = 0;
236 257
237static char * 258static char *
238_check_strip_cmd(char* cmd) 259_check_strip_cmd(char* cmd)
@@ -250,29 +271,32 @@ _check_strip_cmd(char* cmd)
250} 271}
251 272
252static void 273static void
274_process_data(char *data, int size)
275{
276 //fprintf(stderr, "Bridge: received from client %d+8 bytes\n", *size);
277 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, data, size);
278}
279
280static void
253_tmp_file_monitor_cb(void *data, Ecore_File_Monitor *em, 281_tmp_file_monitor_cb(void *data, Ecore_File_Monitor *em,
254 Ecore_File_Event event, const char *path) 282 Ecore_File_Event event, const char *path)
255{ 283{
256 if (event == ECORE_FILE_EVENT_MODIFIED) 284 char cmd[BUFFER_SIZE];
257 { 285 char *cmd2;
258 char line[BUFFER_SIZE]; 286 FILE *f = fopen(path, "r");
259 char arg1[256], arg2[256];
260 char *cmd;
261 FILE *f = fopen(path, "r");
262 287
263 fgets(line, BUFFER_SIZE, f); 288 int nm = fread(cmd, sizeof(char), BUFFER_SIZE, f);
264 fclose(f); 289 cmd2 = _check_strip_cmd(cmd);
265 cmd = _check_strip_cmd(line);
266 290
267 if (cmd) 291 if (cmd2)
268 { 292 {
269 _bridge_send(cmd, strlen(cmd)); 293 _process_data(cmd2, nm); /* minus the seq counter */
270 }
271 } 294 }
295 fclose(f);
272} 296}
273 297
274static Eina_Bool 298static Eina_Bool
275_bridge_sdb(void) 299_bridge(void)
276{ 300{
277 char tmp[1024]; 301 char tmp[1024];
278 FILE *f; 302 FILE *f;
@@ -288,64 +312,76 @@ _bridge_sdb(void)
288 sprintf(tmp, "%s/bridge-%d", tmp_dir, getpid()); 312 sprintf(tmp, "%s/bridge-%d", tmp_dir, getpid());
289 313
290 f = fopen(tmp, "w+"); 314 f = fopen(tmp, "w+");
291 fprintf(stdout, "%s\n", tmp);
292 fclose(f); 315 fclose(f);
316 /* Sending monitored file that is pushed from client */
317 fprintf(stdout, "%s", tmp);
318 fflush(stdout);
293 319
294 fm = ecore_file_monitor_add(tmp, _tmp_file_monitor_cb, NULL); 320 fm = ecore_file_monitor_add(tmp, _tmp_file_monitor_cb, NULL);
295 321
322 /* Initialize debug daemon */
323 //fprintf(stderr, "This is the bridge process..\n");
324 if (!_bridge_init())
325 {
326 //fprintf(stderr, "Exiting...\n");
327 return EINA_FALSE;
328 }
329
330 ecore_main_fd_handler_add(STDIN_FILENO,
331 ECORE_FD_READ,
332 _fd_handler_cb,
333 NULL, NULL, NULL);
296 ecore_main_loop_begin(); 334 ecore_main_loop_begin();
297 ecore_file_shutdown();
298 335
299 return EINA_TRUE; 336 return EINA_TRUE;
300} 337}
301 338
302////////// TEST MODE /////////////////
303static Eina_Bool 339static Eina_Bool
304_fd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED) 340_test_data_from_daemon(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
305{ 341{
306 char msg[BUFFER_SIZE]; 342 SVR_IPC_HEAD(Data);
307 char *c;
308 343
309 if (!fgets(msg, BUFFER_SIZE, stdin)) 344 int *magic = (int *)e->data;
345 printf("Received Message: size %d magic %X\n", e->size, *magic);
346 if (e->major == 1)
310 { 347 {
311 fprintf(stderr, "nothing\n"); 348 printf("Test client sending data\n");
312 return ECORE_CALLBACK_RENEW; 349 ecore_ipc_server_send(srv_ipc, 1, 0, 0, 0, 0, e->data, e->size);
313 } 350 }
314#if 0 351 return ECORE_CALLBACK_DONE;
315 for (c = msg; *c != 0; c++)
316 *c = toupper(toupper(*c));
317
318 fprintf(stdout, "%s\n", msg);
319#endif
320 _bridge_send(msg, strlen(msg));
321 return ECORE_CALLBACK_RENEW;
322} 352}
323 353
324static Eina_Bool 354static Eina_Bool
325_bridge_test(void) 355_test_client_init(void)
326{ 356{
327 ecore_main_fd_handler_add(STDIN_FILENO, 357 srv_ipc = ecore_ipc_server_connect(ECORE_IPC_LOCAL_SYSTEM, "debugd", 0, NULL);
328 ECORE_FD_READ, 358
329 _fd_handler_cb, 359 if (!srv_ipc)
330 NULL, NULL, NULL); 360 {
331 ecore_main_loop_begin(); 361 ecore_ipc_shutdown();
332 return EINA_TRUE; 362 return EINA_FALSE;
363 }
364 svr_hnd_add = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD,
365 _cb_server_add, NULL);
366 svr_hnd_del = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL,
367 _cb_server_del, NULL);
368 svr_hnd_data = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA,
369 _test_data_from_daemon, NULL);
370
371 return EINA_TRUE;;
333} 372}
334 373
335static Eina_Bool 374static Eina_Bool
336_bridge(Daemon_Mode mode) 375_test(void)
337{ 376{
377 printf("Test mode\n");
338 /* Initialize debug daemon */ 378 /* Initialize debug daemon */
339 if (!debug_daemon_init()) 379 if (!_test_client_init())
340 { 380 {
341 perror("Exiting...\n"); 381 printf("Could not connect to server\n");
342 return EINA_FALSE; 382 return EINA_FALSE;
343 } 383 }
344 384 ecore_main_loop_begin();
345 if (mode == DAEMON_MODE_BRIDGE_TEST)
346 _bridge_test();
347 else //BRIDGE_NORMAL
348 _bridge_sdb();
349 385
350 return EINA_TRUE; 386 return EINA_TRUE;
351} 387}
@@ -354,13 +390,11 @@ _bridge(Daemon_Mode mode)
354///////////////// Main ///////////////////////////////////// 390///////////////// Main /////////////////////////////////////
355//////////////////////////////////////////////////////////// 391////////////////////////////////////////////////////////////
356 392
357/* All modes require ipc comm. */
358int main (int argc, char **argv) 393int main (int argc, char **argv)
359{ 394{
360 Daemon_Mode mode; 395 Daemon_Mode mode;
361 Eina_Bool ret;
362 Eina_Bool test = EINA_FALSE;
363 if (!ecore_init()) goto exit; 396 if (!ecore_init()) goto exit;
397 /* All modes require ipc comm. */
364 if (!ecore_ipc_init()) 398 if (!ecore_ipc_init())
365 { 399 {
366 ecore_shutdown(); 400 ecore_shutdown();
@@ -372,7 +406,7 @@ int main (int argc, char **argv)
372 { 406 {
373 if (!strcmp(argv[1], "bridge")) 407 if (!strcmp(argv[1], "bridge"))
374 { 408 {
375 mode = DAEMON_MODE_BRIDGE_NORMAL; 409 mode = DAEMON_MODE_BRIDGE;
376 } 410 }
377 else if (!strcmp(argv[1], "test")) 411 else if (!strcmp(argv[1], "test"))
378 { 412 {
@@ -380,21 +414,13 @@ int main (int argc, char **argv)
380 } 414 }
381 415
382 } 416 }
383 if ((argc == 3) && !strcmp(argv[2], "test"))
384 {
385 if (mode == DAEMON_MODE_BRIDGE_NORMAL)
386 {
387 mode = DAEMON_MODE_BRIDGE_TEST;
388 }
389 else
390 {
391 mode = DAEMON_MODE_TEST;
392 }
393 }
394 417
395 if (mode > DAEMON_MODE_TEST) 418 if (mode == DAEMON_MODE_BRIDGE)
396 _bridge(mode); 419 _bridge();
397 else _daemon(mode); 420 else if (mode == DAEMON_MODE_TEST)
421 _test();
422 else
423 _daemon();
398 424
399 ecore_shutdown(); 425 ecore_shutdown();
400 426