9c29dec759c227d1be35783f9665463ea3c0e7c8
[platform/core/telephony/tel-plugin-imc.git] / src / imc_sat.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Chandan Swarup Patra <chandan.sp@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <tcore.h>
26 #include <hal.h>
27 #include <core_object.h>
28 #include <plugin.h>
29 #include <queue.h>
30 #include <server.h>
31 #include <co_sat.h>
32 #include <user_request.h>
33 #include <at.h>
34
35 #include "imc_common.h"
36 #include "imc_sat.h"
37 #define ENVELOPE_CMD_LEN        256
38
39 static TReturn imc_terminal_response(CoreObject *o, UserRequest *ur);
40 static void on_confirmation_sat_message_send(TcorePending *p, gboolean result, void *user_data);      // from Kernel
41
42 static void on_confirmation_sat_message_send(TcorePending *p, gboolean result, void *user_data)
43 {
44         dbg("on_confirmation_modem_message_send - msg out from queue.\n");
45
46         if (result == FALSE) {
47                 /* Fail */
48                 dbg("SEND FAIL");
49         } else {
50                 dbg("SEND OK");
51         }
52 }
53
54 static gboolean on_response_terminal_response_confirm(CoreObject *o, const void *event_info, void *user_data)
55 {
56         dbg("Function Entry");
57         return TRUE;
58         dbg("Function Exit");
59 }
60
61 static gboolean on_event_sat_proactive_command(CoreObject *o, const void *event_info, void *user_data)
62 {
63         struct tcore_sat_proactive_command decoded_data;
64         struct tnoti_sat_proactive_ind proactive_noti;
65         int len_proactive_cmd = 0;
66         GSList *lines = NULL;
67         GSList *tokens = NULL;
68         char *line = NULL;
69         char *hexData = NULL;
70         char *tmp = NULL;
71         char *recordData = NULL;
72
73         dbg("Function Entry");
74
75         memset(&proactive_noti, 0x00, sizeof(struct tnoti_sat_proactive_ind));
76         memset(&decoded_data, 0x00, sizeof(struct tcore_sat_proactive_command));
77         lines = (GSList *) event_info;
78         line = (char *) lines->data;
79         tokens = tcore_at_tok_new(line);
80         if (g_slist_length(tokens) != 1) {
81                 err("Invalid message");
82                 tcore_at_tok_free(tokens);
83                 return FALSE;
84         }
85
86         hexData = (char *)g_slist_nth_data(tokens, 0);
87         dbg("SAT data: [%s] SAT data length: [%d]", hexData, strlen(hexData));
88
89         tmp = util_removeQuotes(hexData);
90         recordData = util_hexStringToBytes(tmp);
91         dbg("recordData: %x", recordData);
92         g_free(tmp);
93         util_hex_dump("    ", strlen(hexData) / 2, recordData);
94         len_proactive_cmd = strlen(recordData);
95         dbg("len_proactive_cmd = %d", len_proactive_cmd);
96         tcore_sat_decode_proactive_command((unsigned char *) recordData, (strlen(hexData) / 2) - 1, &decoded_data);
97         g_free(recordData);
98
99         proactive_noti.cmd_number = decoded_data.cmd_num;
100         proactive_noti.cmd_type = decoded_data.cmd_type;
101
102         switch (decoded_data.cmd_type) {
103         case SAT_PROATV_CMD_DISPLAY_TEXT:
104                 dbg("decoded command is display text!!");
105                 memcpy(&proactive_noti.proactive_ind_data.display_text, &decoded_data.data.display_text, sizeof(struct tel_sat_display_text_tlv));
106                 break;
107
108         case SAT_PROATV_CMD_GET_INKEY:
109                 dbg("decoded command is get inkey!!");
110                 memcpy(&proactive_noti.proactive_ind_data.get_inkey, &decoded_data.data.get_inkey, sizeof(struct tel_sat_get_inkey_tlv));
111                 break;
112
113         case SAT_PROATV_CMD_GET_INPUT:
114                 dbg("decoded command is get input!!");
115                 memcpy(&proactive_noti.proactive_ind_data.get_input, &decoded_data.data.get_input, sizeof(struct tel_sat_get_input_tlv));
116                 break;
117
118         case SAT_PROATV_CMD_MORE_TIME:
119                 dbg("decoded command is more time!!");
120                 memcpy(&proactive_noti.proactive_ind_data.more_time, &decoded_data.data.more_time, sizeof(struct tel_sat_more_time_tlv));
121                 break;
122
123         case SAT_PROATV_CMD_PLAY_TONE:
124                 dbg("decoded command is play tone!!");
125                 memcpy(&proactive_noti.proactive_ind_data.play_tone, &decoded_data.data.play_tone, sizeof(struct tel_sat_play_tone_tlv));
126                 break;
127
128         case SAT_PROATV_CMD_SETUP_MENU:
129                 dbg("decoded command is SETUP MENU!!");
130                 memcpy(&proactive_noti.proactive_ind_data.setup_menu, &decoded_data.data.setup_menu, sizeof(struct tel_sat_setup_menu_tlv));
131                 break;
132
133         case SAT_PROATV_CMD_SELECT_ITEM:
134                 dbg("decoded command is select item!!");
135                 memcpy(&proactive_noti.proactive_ind_data.select_item, &decoded_data.data.select_item, sizeof(struct tel_sat_select_item_tlv));
136                 break;
137
138         case SAT_PROATV_CMD_SEND_SMS:
139                 dbg("decoded command is send sms!!");
140                 memcpy(&proactive_noti.proactive_ind_data.send_sms, &decoded_data.data.send_sms, sizeof(struct tel_sat_send_sms_tlv));
141                 break;
142
143         case SAT_PROATV_CMD_SEND_SS:
144                 dbg("decoded command is send ss!!");
145                 memcpy(&proactive_noti.proactive_ind_data.send_ss, &decoded_data.data.send_ss, sizeof(struct tel_sat_send_ss_tlv));
146                 break;
147
148         case SAT_PROATV_CMD_SEND_USSD:
149                 dbg("decoded command is send ussd!!");
150                 memcpy(&proactive_noti.proactive_ind_data.send_ussd, &decoded_data.data.send_ussd, sizeof(struct tel_sat_send_ussd_tlv));
151                 break;
152
153         case SAT_PROATV_CMD_SETUP_CALL:
154                 dbg("decoded command is setup call!!");
155                 memcpy(&proactive_noti.proactive_ind_data.setup_call, &decoded_data.data.setup_call, sizeof(struct tel_sat_setup_call_tlv));
156                 break;
157
158         case SAT_PROATV_CMD_REFRESH:
159                 dbg("decoded command is refresh");
160                 memcpy(&proactive_noti.proactive_ind_data.refresh, &decoded_data.data.refresh, sizeof(struct tel_sat_refresh_tlv));
161                 break;
162
163         case SAT_PROATV_CMD_PROVIDE_LOCAL_INFO:
164                 dbg("decoded command is provide local info");
165                 memcpy(&proactive_noti.proactive_ind_data.provide_local_info, &decoded_data.data.provide_local_info, sizeof(struct tel_sat_provide_local_info_tlv));
166                 break;
167
168         case SAT_PROATV_CMD_SETUP_EVENT_LIST:
169                 dbg("decoded command is setup event list!!");
170                 memcpy(&proactive_noti.proactive_ind_data.setup_event_list, &decoded_data.data.setup_event_list, sizeof(struct tel_sat_setup_event_list_tlv));
171                 // setup_event_rsp_get(o, &decoded_data.data.setup_event_list);
172                 break;
173
174         case SAT_PROATV_CMD_SETUP_IDLE_MODE_TEXT:
175                 dbg("decoded command is setup idle mode text");
176                 memcpy(&proactive_noti.proactive_ind_data.setup_idle_mode_text, &decoded_data.data.setup_idle_mode_text, sizeof(struct tel_sat_setup_idle_mode_text_tlv));
177                 break;
178
179         case SAT_PROATV_CMD_SEND_DTMF:
180                 dbg("decoded command is send dtmf");
181                 memcpy(&proactive_noti.proactive_ind_data.send_dtmf, &decoded_data.data.send_dtmf, sizeof(struct tel_sat_send_dtmf_tlv));
182                 break;
183
184         case SAT_PROATV_CMD_LANGUAGE_NOTIFICATION:
185                 dbg("decoded command is language notification");
186                 memcpy(&proactive_noti.proactive_ind_data.language_notification, &decoded_data.data.language_notification, sizeof(struct tel_sat_language_notification_tlv));
187                 break;
188
189         case SAT_PROATV_CMD_LAUNCH_BROWSER:
190                 dbg("decoded command is launch browser");
191                 memcpy(&proactive_noti.proactive_ind_data.launch_browser, &decoded_data.data.launch_browser, sizeof(struct tel_sat_launch_browser_tlv));
192                 break;
193
194         case SAT_PROATV_CMD_OPEN_CHANNEL:
195                 dbg("decoded command is open channel!!");
196                 memcpy(&proactive_noti.proactive_ind_data.open_channel, &decoded_data.data.open_channel, sizeof(struct tel_sat_open_channel_tlv));
197                 break;
198
199         case SAT_PROATV_CMD_CLOSE_CHANNEL:
200                 dbg("decoded command is close channel!!");
201                 memcpy(&proactive_noti.proactive_ind_data.close_channel, &decoded_data.data.close_channel, sizeof(struct tel_sat_close_channel_tlv));
202                 break;
203
204         case SAT_PROATV_CMD_RECEIVE_DATA:
205                 dbg("decoded command is receive data!!");
206                 memcpy(&proactive_noti.proactive_ind_data.receive_data, &decoded_data.data.receive_data, sizeof(struct tel_sat_receive_channel_tlv));
207                 break;
208
209         case SAT_PROATV_CMD_SEND_DATA:
210                 dbg("decoded command is send data!!");
211                 memcpy(&proactive_noti.proactive_ind_data.send_data, &decoded_data.data.send_data, sizeof(struct tel_sat_send_channel_tlv));
212                 break;
213
214         case SAT_PROATV_CMD_GET_CHANNEL_STATUS:
215                 dbg("decoded command is get channel status!!");
216                 memcpy(&proactive_noti.proactive_ind_data.get_channel_status, &decoded_data.data.get_channel_status, sizeof(struct tel_sat_get_channel_status_tlv));
217                 break;
218
219         default:
220                 dbg("wrong input");
221                 break;
222         }
223         if (decoded_data.cmd_type == SAT_PROATV_CMD_REFRESH) {
224                 /*Not supported*/
225                 dbg("Not suported Proactive command");
226                 return FALSE;
227         }
228         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SAT_PROACTIVE_CMD,
229                                                                    sizeof(struct tnoti_sat_proactive_ind), &proactive_noti);
230         tcore_at_tok_free(tokens);
231         dbg("Function Exit");
232         return TRUE;
233 }
234
235 static void on_response_envelop_cmd(TcorePending *p, int data_len, const void *data, void *user_data)
236 {
237         const TcoreATResponse *resp = data;
238         UserRequest *ur = NULL;
239         CoreObject *o = NULL;
240         const struct                treq_sat_envelop_cmd_data *req_data = NULL;
241         GSList *tokens = NULL;
242         struct                      tresp_sat_envelop_data res;
243         const char *line = NULL;
244         //const char *env_res = NULL;
245         int sw2 = -1;
246
247         ur = tcore_pending_ref_user_request(p);
248         req_data = tcore_user_request_ref_data(ur, NULL);
249         o = tcore_pending_ref_core_object(p);
250
251         if (!req_data) {
252                 dbg("request data is NULL");
253                 return;
254         }
255         memset(&res, 0, sizeof(struct tresp_sat_envelop_data));
256
257         res.sub_cmd = req_data->sub_cmd;
258
259         if (resp->success > 0) {
260                 dbg("RESPONSE OK");
261                 if (resp->lines) {
262                         line = (const char *) resp->lines->data;
263                         tokens = tcore_at_tok_new(line);
264                         if (g_slist_length(tokens) < 1) {
265                                 msg("invalid message");
266                                 tcore_at_tok_free(tokens);
267                                 return;
268                         }
269                 }
270                 //env_res = g_slist_nth_data(tokens, 0);
271                 res.result = 0x8000;
272                 res.envelop_resp = ENVELOPE_SUCCESS;
273                 dbg("RESPONSE OK 3");
274                 if (NULL != g_slist_nth_data(tokens, 1)) {
275                         sw2 = atoi(g_slist_nth_data(tokens, 1));
276                         dbg("RESPONSE OK 4");
277                         if (sw2 == 0) {
278                                 dbg("RESPONSE OK 5");
279                                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SAT_SESSION_END, 0, NULL);
280                         }
281                 }
282         } else {
283                 dbg("RESPONSE NOK");
284                 res.result = -1;
285                 res.envelop_resp = ENVELOPE_FAILED;
286         }
287
288         if (ur) {
289                 tcore_user_request_send_response(ur, TRESP_SAT_REQ_ENVELOPE, sizeof(struct tresp_sat_envelop_data), &res);
290         }
291         tcore_at_tok_free(tokens);
292         dbg(" Function exit");
293 }
294
295
296 static void on_response_terminal_response(TcorePending *p, int data_len, const void *data, void *user_data)
297 {
298         UserRequest *ur = NULL;
299         CoreObject *o = NULL;
300         const TcoreATResponse *resp = data;
301         gpointer tmp = NULL;
302
303         dbg("Function Entry");
304
305         if (resp->success > 0) {
306                 dbg("RESPONSE OK");
307                 dbg(" resp->success = %d", resp->success);
308                 ur = tcore_pending_ref_user_request(p);
309                 tmp = (gpointer) tcore_user_request_ref_communicator(ur);
310                 if (!ur || !tmp) {
311                         dbg("error - current ur is NULL");
312                         return;
313                 }
314
315                 o = tcore_pending_ref_core_object(p);
316                 if (!o)
317                         dbg("error - current sat core is NULL");
318                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SAT_SESSION_END, 0, NULL);
319         }
320         dbg("Function Exit");
321 }
322
323 static TReturn imc_envelope(CoreObject *o, UserRequest *ur)
324 {
325         TcoreHal *hal;
326         TcoreATRequest *req = NULL;
327         TcorePending *pending = NULL;
328         char *cmd_str = NULL;
329         const struct            treq_sat_envelop_cmd_data *req_data = NULL;
330         int envelope_cmd_len = 0;
331         char envelope_cmd[ENVELOPE_CMD_LEN];
332         int count = 0;
333         char envelope_cmdhex[ENVELOPE_CMD_LEN * 2];
334         char *pbuffer = NULL;
335
336         dbg("Function Entry");
337         memset(&envelope_cmdhex, 0x00, sizeof(envelope_cmdhex));
338         pbuffer = envelope_cmdhex;
339
340         hal = tcore_object_get_hal(o);
341         if(FALSE == tcore_hal_get_power_state(hal)){
342                 dbg("cp not ready/n");
343                 return TCORE_RETURN_ENOSYS;
344         }
345
346         pending = tcore_pending_new(o, 0);
347         req_data = tcore_user_request_ref_data(ur, NULL);
348         dbg("new pending sub cmd(%d)", req_data->sub_cmd);
349
350         envelope_cmd_len = tcore_sat_encode_envelop_cmd(req_data, (char *) envelope_cmd);
351
352         dbg("envelope_cmd_len %d", envelope_cmd_len);
353         if (envelope_cmd_len == 0) {
354                 return TCORE_RETURN_EINVAL;
355         }
356         for (count = 0; count < envelope_cmd_len; count++) {
357                 dbg("envelope_cmd %02x", (unsigned char)envelope_cmd[count]);
358                 sprintf(pbuffer, "%02x", (unsigned char)envelope_cmd[count]);
359                 pbuffer += 2;
360         }
361         dbg("pbuffer %s", envelope_cmdhex);
362         cmd_str = g_strdup_printf("AT+SATE=\"%s\"", envelope_cmdhex);
363         req = tcore_at_request_new(cmd_str, "+SATE:", TCORE_AT_SINGLELINE);
364         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
365
366         tcore_pending_set_request_data(pending, 0, req);
367         tcore_pending_set_response_callback(pending, on_response_envelop_cmd, hal);
368         tcore_pending_link_user_request(pending, ur);
369         tcore_pending_set_send_callback(pending, on_confirmation_sat_message_send, NULL);
370         tcore_hal_send_request(hal, pending);
371
372         g_free(cmd_str);
373         dbg("Function Exit");
374         return TCORE_RETURN_SUCCESS;
375 }
376
377 static TReturn imc_terminal_response(CoreObject *o, UserRequest *ur)
378 {
379         TcoreHal *hal = NULL;
380         TcoreATRequest *req = NULL;
381         TcorePending *pending = NULL;
382         char *cmd_str = NULL;
383         const struct            treq_sat_terminal_rsp_data *req_data = NULL;
384         int proactive_resp_len = 0;
385         char proactive_resp[ENVELOPE_CMD_LEN];
386         char proactive_resphex[ENVELOPE_CMD_LEN * 2];
387         int i = 0;
388         char *hexString = NULL;
389
390         dbg("Function Entry");
391         memset(&proactive_resphex, 0x00, sizeof(proactive_resphex));
392         hal = tcore_object_get_hal(o);
393         if(FALSE == tcore_hal_get_power_state(hal)){
394                 dbg("cp not ready/n");
395                 return TCORE_RETURN_ENOSYS;
396         }
397
398         pending = tcore_pending_new(o, 0);
399         req_data = tcore_user_request_ref_data(ur, NULL);
400
401         proactive_resp_len = tcore_sat_encode_terminal_response(req_data, (char *) proactive_resp);
402         dbg("proactive_resp %s", proactive_resp);
403         dbg("proactive_resp length %d", strlen(proactive_resp));
404         if (proactive_resp_len == 0) {
405                 tcore_pending_free(pending);
406                 return TCORE_RETURN_EINVAL;
407         }
408         hexString = calloc((proactive_resp_len * 2) + 1, 1);
409
410         for (i = 0; i < proactive_resp_len * 2; i += 2) {
411                 char value = 0;
412                 value = (proactive_resp[i / 2] & 0xf0) >> 4;
413                 if (value < 0xA)
414                         hexString[i] = ((proactive_resp[i / 2] & 0xf0) >> 4) + '0';
415                 else
416                         hexString[i] = ((proactive_resp[i / 2] & 0xf0) >> 4) + 'A' - 10;
417
418                 value = proactive_resp[i / 2] & 0x0f;
419                 if (value < 0xA)
420                         hexString[i + 1] = (proactive_resp[i / 2] & 0x0f) + '0';
421                 else
422                         hexString[i + 1] = (proactive_resp[i / 2] & 0x0f) + 'A' - 10;
423         }
424
425         dbg("hexString %s", hexString);
426         cmd_str = g_strdup_printf("AT+SATR=\"%s\"", hexString);
427
428         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_NO_RESULT);
429         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
430
431         tcore_pending_set_request_data(pending, 0, req);
432         tcore_pending_set_response_callback(pending, on_response_terminal_response, hal);
433         tcore_pending_link_user_request(pending, ur);
434         tcore_pending_set_send_callback(pending, on_confirmation_sat_message_send, NULL);
435         tcore_hal_send_request(hal, pending);
436
437         g_free(cmd_str);
438         g_free(hexString);
439         dbg("Function Exit");
440         return TCORE_RETURN_SUCCESS;
441 }
442
443 static struct tcore_sat_operations sat_ops = {
444         .envelope = imc_envelope,
445         .terminal_response = imc_terminal_response,
446 };
447
448 gboolean imc_sat_init(TcorePlugin *cp, CoreObject *co_sat)
449 {
450         dbg("Entry");
451
452         /* Set operations */
453         tcore_sat_set_ops(co_sat, &sat_ops);
454
455         tcore_object_add_callback(co_sat, "+SATI", on_event_sat_proactive_command, NULL);
456         tcore_object_add_callback(co_sat, "+SATN", on_event_sat_proactive_command, NULL);
457         tcore_object_add_callback(co_sat, "+SATF", on_response_terminal_response_confirm, NULL);
458
459         dbg("Exit");
460
461         return TRUE;
462 }
463
464 void imc_sat_exit(TcorePlugin *cp, CoreObject *co_sat)
465 {
466         dbg("Exit");
467 }