Merge branch 'tizen_qemu_2.0' into tizen
[sdk/emulator/qemu.git] / tizen / src / ecs / ecs_tethering.c
1 /*
2  * Emulator Control Server - Device Tethering Handler
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact:
7  *  KiTae Kim       <kt920.kim@samsung.com>
8  *  JiHye Kim       <jihye1128.kim@samsung.com>
9  *  YeongKyoon Lee  <yeongkyoon.lee@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  *
28  */
29
30 #include "ui/console.h"
31
32 #include "ecs.h"
33 #include "ecs_tethering.h"
34 #include "../tethering/app_tethering.h"
35 #include "../hw/maru_virtio_touchscreen.h"
36 #include "../debug_ch.h"
37
38 MULTI_DEBUG_CHANNEL(tizen, ecs_tethering);
39
40 #define MSG_BUF_SIZE  255
41 #define MSG_LEN_SIZE    4
42
43 #if 0
44 // ecs <-> ecp messages
45 #define ECS_TETHERING_MSG_CATEGORY                      "tethering"
46
47 #define ECS_TETHERING_MSG_GROUP_ECP                     1
48 // #define TETHERING_MSG_GROUP_USB
49 // #define TETHERING_MSG_GROUP_WIFI
50
51 #define ECS_TETHERING_MSG_ACTION_CONNECT                1
52 #define ECS_TETHERING_MSG_ACTION_DISCONNECT             2
53 #define ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS      3
54 #define ECS_TETHERING_MSG_ACTION_SENSOR_STATUS          4
55 #define ECS_TETHERING_MSG_ACTION_TOUCH_STATUS           5
56 #endif
57
58 // static bool send_tethering_ntf(const char *data, const int len);
59 static bool send_tethering_ntf(const char *data);
60 static void send_tethering_status_ntf(type_group group, type_action action);
61
62 static int tethering_port = 0;
63
64 void send_tethering_sensor_status_ecp(void)
65 {
66     INFO(">> send tethering_event_status to ecp\n");
67     send_tethering_status_ntf(ECS_TETHERING_MSG_GROUP_ECP,
68             ECS_TETHERING_MSG_ACTION_SENSOR_STATUS);
69 }
70
71 void send_tethering_touch_status_ecp(void)
72 {
73     send_tethering_status_ntf(ECS_TETHERING_MSG_GROUP_ECP,
74             ECS_TETHERING_MSG_ACTION_TOUCH_STATUS);
75 }
76
77 void send_tethering_connection_status_ecp(void)
78 {
79     INFO(">> send tethering_connection_status to ecp\n");
80     send_tethering_status_ntf(ECS_TETHERING_MSG_GROUP_ECP,
81             ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS);
82 }
83
84 static void send_tethering_port_ecp(void)
85 {
86     type_length length;
87     type_group group = ECS_TETHERING_MSG_GROUP_ECP;
88     type_action action = ECS_TETHERING_MSG_ACTION_CONNECT;
89     uint8_t *msg = NULL;
90     gchar data[12];
91
92     msg = g_malloc(MSG_BUF_SIZE);
93     if (!msg) {
94         return;
95     }
96
97     TRACE(">> send port_num: %d\n", tethering_port);
98
99     g_snprintf(data, sizeof(data) - 1, "%d", tethering_port);
100     length = strlen(data);
101
102     memcpy(msg, ECS_TETHERING_MSG_CATEGORY, 10);
103     memcpy(msg + 10, &length, sizeof(unsigned short));
104     memcpy(msg + 12, &group, sizeof(unsigned char));
105     memcpy(msg + 13, &action, sizeof(unsigned char));
106     memcpy(msg + 14, data, length);
107
108     TRACE(">> send tethering_ntf to ecp. action=%d, group=%d, data=%s\n",
109         action, group, data);
110
111 //    send_tethering_ntf((const char *)msg, MSG_BUF_SIZE);
112     send_tethering_ntf((const char *)msg);
113
114     if (msg) {
115         g_free(msg);
116     }
117 }
118
119 static void send_tethering_status_ntf(type_group group, type_action action)
120 {
121     type_length length = 1;
122     int status = 0;
123     uint8_t *msg = NULL;
124     gchar data[2];
125
126     switch (action) {
127     case ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS:
128         status = get_tethering_connection_status();
129         if (status == CONNECTED) {
130             send_tethering_port_ecp();
131         }
132         break;
133     case ECS_TETHERING_MSG_ACTION_SENSOR_STATUS:
134         status = get_tethering_sensor_status();
135         break;
136     case ECS_TETHERING_MSG_ACTION_TOUCH_STATUS:
137         status = get_tethering_multitouch_status();
138         break;
139     default:
140         break;
141     }
142
143     msg = g_malloc(MSG_BUF_SIZE);
144     if (!msg) {
145         return;
146     }
147
148     g_snprintf(data, sizeof(data), "%d", status);
149
150     memcpy(msg, ECS_TETHERING_MSG_CATEGORY, 10);
151     memcpy(msg + 10, &length, sizeof(unsigned short));
152     memcpy(msg + 12, &group, sizeof(unsigned char));
153     memcpy(msg + 13, &action, sizeof(unsigned char));
154     memcpy(msg + 14, data, 1);
155
156     TRACE(">> send tethering_ntf to ecp. action=%d, group=%d, data=%s\n",
157         action, group, data);
158
159 //    send_tethering_ntf((const char *)msg, MSG_BUF_SIZE);
160     send_tethering_ntf((const char *)msg);
161
162     if (msg) {
163         g_free(msg);
164     }
165 }
166
167 // static bool send_tethering_ntf(const char *data, const int len)
168 static bool send_tethering_ntf(const char *data)
169 {
170     type_length length = 0;
171     type_group group = 0;
172     type_action action = 0;
173
174     const int catsize = 10;
175     char cat[catsize + 1];
176     memset(cat, 0, catsize + 1);
177
178     read_val_str(data, cat, catsize);
179     read_val_short(data + catsize, &length);
180     read_val_char(data + catsize + 2, &group);
181     read_val_char(data + catsize + 2 + 1, &action);
182
183     const char* ijdata = (data + catsize + 2 + 1 + 1);
184
185     TRACE("<< header cat = %s, length = %d, action=%d, group=%d\n", cat, length,action, group);
186
187     ECS__Master master = ECS__MASTER__INIT;
188     ECS__TetheringNtf ntf = ECS__TETHERING_NTF__INIT;
189
190     ntf.category = (char*) g_malloc(catsize + 1);
191     strncpy(ntf.category, cat, 10);
192
193     ntf.length = length;
194     ntf.group = group;
195     ntf.action = action;
196
197     if (length > 0) {
198         ntf.has_data = 1;
199
200         ntf.data.data = g_malloc(length);
201         ntf.data.len = length;
202         memcpy(ntf.data.data, ijdata, length);
203
204         TRACE("data = %s, length = %hu\n", ijdata, length);
205     }
206
207     master.type = ECS__MASTER__TYPE__TETHERING_NTF;
208     master.tethering_ntf = &ntf;
209
210     send_to_ecp(&master);
211
212     if (ntf.data.data && ntf.data.len > 0) {
213         g_free(ntf.data.data);
214     }
215
216     if (ntf.category) {
217         g_free(ntf.category);
218     }
219
220     return true;
221 }
222
223 void send_tethering_sensor_data(const char *data, int len)
224 {
225     set_injector_data(data);
226 }
227
228 void send_tethering_touch_data(int x, int y, int index, int status)
229 {
230     virtio_touchscreen_event(x, y, index, status);
231 }
232
233 // handle tethering_req message
234 bool msgproc_tethering_req(ECS_Client* ccli, ECS__TetheringReq* msg)
235 {
236     gchar cmd[10] = {0};
237
238     g_strlcpy(cmd, msg->category, sizeof(cmd));
239     type_length length = (type_length) msg->length;
240     type_group group = (type_group) (msg->group & 0xff);
241     type_action action = (type_action) (msg->action & 0xff);
242
243     TRACE(">> header = cmd = %s, length = %d, action=%d, group=%d\n",
244             cmd, length, action, group);
245
246     if (group == ECS_TETHERING_MSG_GROUP_ECP) {
247         switch(action) {
248         case ECS_TETHERING_MSG_ACTION_CONNECT:
249         {
250             if (msg->data.data && msg->data.len > 0) {
251                 const gchar *data = (const gchar *)msg->data.data;
252                 gint port = 0;
253
254                 port = g_ascii_strtoull(data, NULL, 10);
255
256                 TRACE(">> MSG_ACTION_CONNECT\n");
257                 TRACE(">> len = %zd, data\" %s\"\n", strlen(data), data);
258
259                 connect_tethering_app(port);
260                 tethering_port = port;
261
262                 TRACE(">> port_num: %d, %d\n", port, tethering_port);
263             }
264         }
265             break;
266         case ECS_TETHERING_MSG_ACTION_DISCONNECT:
267             INFO(">> MSG_ACTION_DISCONNECT\n");
268             disconnect_tethering_app();
269             tethering_port = 0;
270             break;
271         case ECS_TETHERING_MSG_ACTION_CONNECTION_STATUS:
272         case ECS_TETHERING_MSG_ACTION_SENSOR_STATUS:
273         case ECS_TETHERING_MSG_ACTION_TOUCH_STATUS:
274             TRACE(">> get_status_action\n");
275             send_tethering_status_ntf(group, action);
276             break;
277         default:
278             break;
279         }
280     }
281
282     return true;
283 }