Fix call barring crash
[platform/core/telephony/tel-plugin-imc.git] / src / s_sap.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Ankit Jogi <ankit.jogi@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 <co_sap.h>
31 #include <co_sim.h>
32 #include <user_request.h>
33 #include <server.h>
34 #include <at.h>
35
36 #include "s_common.h"
37 #include "s_sap.h"
38
39
40 static void on_confirmation_sap_message_send(TcorePending *p, gboolean result, void *user_data)
41 {
42         dbg("on_confirmation_sap_message_send - msg out from queue.\n");
43
44         if (result == FALSE) {
45                 /* Fail */
46                 dbg("SEND FAIL");
47         } else {
48                 dbg("SEND OK");
49         }
50 }
51
52 static gboolean on_event_sap_status(CoreObject *o, const void *event_info, void *user_data)
53 {
54         struct tnoti_sap_status_changed noti;
55         GSList *tokens = NULL;
56         GSList *lines = NULL;
57         const char *line = NULL;
58         int status = 0;
59
60         dbg(" Function entry ");
61
62         lines = (GSList *) event_info;
63         if (1 != g_slist_length(lines)) {
64                 dbg("unsolicited msg but multiple line");
65                 return FALSE;
66         }
67         line = (char *) (lines->data);
68
69         tokens = tcore_at_tok_new(line);
70         if (g_slist_length(tokens) != 1) {
71                 msg("invalid message");
72                 tcore_at_tok_free(tokens);
73                 return FALSE;
74         }
75         status = atoi(g_slist_nth_data(tokens, 0));
76
77         switch(status){
78                 case 0:
79                         noti.status = SAP_CARD_STATUS_UNKNOWN;
80                         break;
81                 case 1:
82                         noti.status = SAP_CARD_STATUS_RESET;
83                         break;
84                 case 2:
85                         noti.status = SAP_CARD_STATUS_NOT_ACCESSIBLE;
86                         break;
87                 case 3:
88                         noti.status = SAP_CARD_STATUS_REMOVED;
89                         break;
90                 case 4:
91                         noti.status = SAP_CARD_STATUS_INSERTED;
92                         break;
93                 case 5:
94                         noti.status = SAP_CARD_STATUS_RECOVERED;
95                         break;
96                 default:
97                         noti.status = SAP_CARD_STATUS_NOT_ACCESSIBLE;
98                         break;
99         }
100
101         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SAP_STATUS,
102                         sizeof(struct tnoti_sap_status_changed), &noti);
103         return TRUE;
104 }
105
106 /*static void on_event_sap_disconnect(CoreObject *o, const void *event_info, void *user_data)
107 {
108         //ToDo - Indication not present
109
110         const ipc_sap_disconnect_noti_type *ipc = event_info;
111         struct tnoti_sap_disconnect noti;
112
113         dbg("NOTI RECEIVED");
114
115         noti.type = ipc->disconnect_type;
116         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_SAP_DISCONNECT,
117                                 sizeof(struct tnoti_sap_disconnect), &noti);
118 }*/
119
120 static void on_response_connect(TcorePending *p, int data_len, const void *data, void *user_data)
121 {
122         const TcoreATResponse *resp = data;
123         UserRequest *ur = NULL;
124         struct tresp_sap_req_connect res;
125         int *max_msg_size = (int *)user_data;
126
127         dbg(" Function entry ");
128
129         memset(&res, 0x00, sizeof(struct tresp_sap_req_connect));
130         ur = tcore_pending_ref_user_request(p);
131
132         if(resp->success > 0)
133         {
134                 dbg("RESPONSE OK");
135
136                 res.status = SAP_CONNECTION_STATUS_OK;
137                 res.max_msg_size = *max_msg_size;
138
139         }else{
140                 dbg("RESPONSE NOK");
141                 res.status = SAP_CONNECTION_STATUS_UNABLE_TO_ESTABLISH;
142                 res.max_msg_size = 0;
143         }
144
145         if (ur) {
146                 tcore_user_request_send_response(ur, TRESP_SAP_REQ_CONNECT, sizeof(struct tresp_sap_req_connect), &res);
147         }
148         dbg(" Function exit");
149 }
150
151 static void on_response_disconnect(TcorePending *p, int data_len, const void *data, void *user_data)
152 {
153         const TcoreATResponse *resp = data;
154         UserRequest *ur = NULL;
155         struct tresp_sap_req_disconnect res;
156
157         dbg(" Function entry ");
158         memset(&res, 0x00, sizeof(struct tresp_sap_req_disconnect));
159         ur = tcore_pending_ref_user_request(p);
160
161         if(resp->success > 0)
162         {
163                 dbg("RESPONSE OK");
164
165                 res.result = SAP_RESULT_CODE_OK;
166
167         }else{
168                 dbg("RESPONSE NOK");
169                 //ToDo - Error mapping
170         }
171
172         if (ur) {
173                 tcore_user_request_send_response(ur, TRESP_SAP_REQ_DISCONNECT, sizeof(struct tresp_sap_req_disconnect), &res);
174         }
175         dbg(" Function exit");
176 }
177
178 static void on_response_req_status(TcorePending *p, int data_len, const void *data, void *user_data)
179 {
180         const TcoreATResponse *resp = data;
181         UserRequest *ur = NULL;
182         struct tresp_sap_req_status res;
183
184         dbg(" Function entry ");
185
186         ur = tcore_pending_ref_user_request(p);
187
188         if(resp->success > 0)
189         {
190                 dbg("RESPONSE OK");
191                 //ToDo - No AT command present
192                 //res.status = NULL;
193
194         }else{
195                 dbg("RESPONSE NOK");
196                 //ToDo - Error mapping
197         }
198
199         if (ur) {
200                 tcore_user_request_send_response(ur, TRESP_SAP_REQ_STATUS, sizeof(struct tresp_sap_req_status), &res);
201         }
202         dbg(" Function exit");
203 }
204
205 static void on_response_set_transfort_protocol(TcorePending *p, int data_len, const void *data, void *user_data)
206 {
207         const TcoreATResponse *resp = data;
208         UserRequest *ur = NULL;
209         struct tresp_sap_set_protocol res;
210
211         dbg(" Function entry ");
212
213         ur = tcore_pending_ref_user_request(p);
214
215         if(resp->success > 0)
216         {
217                 dbg("RESPONSE OK");
218                 //ToDo - No AT command present
219                 //res.result = NULL;
220
221         }else{
222                 dbg("RESPONSE NOK");
223                 //ToDo - Error mapping
224         }
225
226         if (ur) {
227                 tcore_user_request_send_response(ur, TRESP_SAP_SET_PROTOCOL, sizeof(struct tresp_sap_set_protocol), &res);
228         }
229         dbg(" Function exit");
230 }
231
232 static void on_response_set_power(TcorePending *p, int data_len, const void *data, void *user_data)
233 {
234         const TcoreATResponse *resp = data;
235         UserRequest *ur = NULL;
236         struct tresp_sap_set_power res;
237         GSList *tokens=NULL;
238         const char *line;
239         int sap_status = -1;
240
241         dbg(" Function entry ");
242
243         ur = tcore_pending_ref_user_request(p);
244
245         if(resp->success > 0)
246         {
247                 dbg("RESPONSE OK");
248                 if(resp->lines) {
249                         line = (const char*)resp->lines->data;
250                         tokens = tcore_at_tok_new(line);
251                         if (g_slist_length(tokens) < 1) {
252                                 msg("invalid message");
253                                 tcore_at_tok_free(tokens);
254                                 return;
255                         }
256                 }
257                 sap_status = atoi(g_slist_nth_data(tokens, 0));
258
259                 switch(sap_status){
260                         case 0:
261                                 res.result = SAP_RESULT_CODE_OK;
262                                 break;
263                         case 1:
264                                 res.result = SAP_RESULT_CODE_NO_REASON;
265                                 break;
266                         case 2:
267                                 res.result = SAP_RESULT_CODE_CARD_NOT_ACCESSIBLE;
268                                 break;
269                         case 3:
270                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_OFF;
271                                 break;
272                         case 4:
273                                 res.result = SAP_RESULT_CODE_CARD_REMOVED;
274                                 break;
275                         case 5:
276                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_ON;
277                                 break;
278                         case 6:
279                                 res.result = SAP_RESULT_CODE_DATA_NOT_AVAILABLE;
280                                 break;
281                         case 7:
282                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
283                                 break;
284                         default:
285                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
286                                 break;
287                 }
288
289         }else{
290                 dbg("RESPONSE NOK");
291                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
292         }
293
294         if (ur) {
295                 tcore_user_request_send_response(ur, TRESP_SAP_SET_POWER, sizeof(struct tresp_sap_set_power), &res);
296         }
297         tcore_at_tok_free(tokens);
298         dbg(" Function exit");
299 }
300
301 static void on_response_get_atr(TcorePending *p, int data_len, const void *data, void *user_data)
302 {
303         const TcoreATResponse *resp = data;
304         UserRequest *ur = NULL;
305         struct tresp_sap_req_atr res;
306         GSList *tokens=NULL;
307         const char *line;
308         int sap_status = -1;
309         char *atr_data = NULL;
310
311         dbg(" Function entry ");
312
313         ur = tcore_pending_ref_user_request(p);
314
315         if(resp->success > 0)
316         {
317                 dbg("RESPONSE OK");
318
319                 if(resp->lines) {
320                         line = (const char*)resp->lines->data;
321                         tokens = tcore_at_tok_new(line);
322                         if (g_slist_length(tokens) < 1) {
323                                 msg("invalid message");
324                                 tcore_at_tok_free(tokens);
325                                 return;
326                         }
327                 }
328                 sap_status = atoi(g_slist_nth_data(tokens, 0));
329                 atr_data = (char *) g_slist_nth_data(tokens, 1);
330
331                 res.atr_length = strlen(atr_data);
332                 if( res.atr_length > 256 ) {
333                         dbg(" Memory overflow handling");
334                         return;
335                 }
336                 memcpy(res.atr, atr_data, res.atr_length);
337
338                 switch(sap_status){
339                         case 0:
340                                 res.result = SAP_RESULT_CODE_OK;
341                                 break;
342                         case 1:
343                                 res.result = SAP_RESULT_CODE_NO_REASON;
344                                 break;
345                         case 2:
346                                 res.result = SAP_RESULT_CODE_CARD_NOT_ACCESSIBLE;
347                                 break;
348                         case 3:
349                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_OFF;
350                                 break;
351                         case 4:
352                                 res.result = SAP_RESULT_CODE_CARD_REMOVED;
353                                 break;
354                         case 5:
355                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_ON;
356                                 break;
357                         case 6:
358                                 res.result = SAP_RESULT_CODE_DATA_NOT_AVAILABLE;
359                                 break;
360                         case 7:
361                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
362                                 break;
363                         default:
364                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
365                                 break;
366                 }
367
368         }else{
369                 dbg("RESPONSE NOK");
370                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
371         }
372
373         if (ur) {
374                 tcore_user_request_send_response(ur, TRESP_SAP_REQ_ATR, sizeof(struct tresp_sap_req_atr), &res);
375         }
376         dbg(" Function exit");
377 }
378
379 static void on_response_transfer_apdu(TcorePending *p, int data_len, const void *data, void *user_data)
380 {
381         const TcoreATResponse *resp = data;
382         UserRequest *ur = NULL;
383         struct tresp_sap_transfer_apdu res;
384         GSList *tokens=NULL;
385         const char *line;
386         int sap_status = -1;
387         char *apdu_data = NULL;
388
389         dbg(" Function entry ");
390
391         ur = tcore_pending_ref_user_request(p);
392
393         if(resp->success > 0)
394         {
395                 dbg("RESPONSE OK");
396
397                 if(resp->lines) {
398                         line = (const char*)resp->lines->data;
399                         tokens = tcore_at_tok_new(line);
400                         if (g_slist_length(tokens) < 1) {
401                                 msg("invalid message");
402                                 tcore_at_tok_free(tokens);
403                                 return;
404                         }
405                 }
406                 sap_status = atoi(g_slist_nth_data(tokens, 0));
407                 apdu_data = (char *) g_slist_nth_data(tokens, 1);
408
409                 res.resp_apdu_length = strlen(apdu_data);
410                 if( res.resp_apdu_length > 256 ) {
411                         dbg(" Memory overflow handling");
412                         return;
413                 }
414                 memcpy(res.resp_adpdu, apdu_data, res.resp_apdu_length);
415
416                 switch(sap_status){
417                         case 0:
418                                 res.result = SAP_RESULT_CODE_OK;
419                                 break;
420                         case 1:
421                                 res.result = SAP_RESULT_CODE_NO_REASON;
422                                 break;
423                         case 2:
424                                 res.result = SAP_RESULT_CODE_CARD_NOT_ACCESSIBLE;
425                                 break;
426                         case 3:
427                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_OFF;
428                                 break;
429                         case 4:
430                                 res.result = SAP_RESULT_CODE_CARD_REMOVED;
431                                 break;
432                         case 5:
433                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_ON;
434                                 break;
435                         case 6:
436                                 res.result = SAP_RESULT_CODE_DATA_NOT_AVAILABLE;
437                                 break;
438                         case 7:
439                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
440                                 break;
441                         default:
442                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
443                                 break;
444                 }
445
446         }else{
447                 dbg("RESPONSE NOK");
448                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
449         }
450
451         if (ur) {
452                 tcore_user_request_send_response(ur, TRESP_SAP_TRANSFER_APDU, sizeof(struct tresp_sap_transfer_apdu), &res);
453         }
454         dbg(" Function exit");
455 }
456
457 static void on_response_get_cardreader_status(TcorePending *p, int data_len, const void *data, void *user_data)
458 {
459         const TcoreATResponse *resp = data;
460         UserRequest *ur = NULL;
461         struct tresp_sap_req_cardreaderstatus res;
462         GSList *tokens=NULL;
463         const char *line;
464         int sap_status = -1;
465         char *card_reader_status = NULL;
466
467         dbg(" Function entry ");
468
469         ur = tcore_pending_ref_user_request(p);
470
471         if(resp->success > 0)
472         {
473                 dbg("RESPONSE OK");
474
475                 if(resp->lines) {
476                         line = (const char*)resp->lines->data;
477                         tokens = tcore_at_tok_new(line);
478                         if (g_slist_length(tokens) < 1) {
479                                 msg("invalid message");
480                                 tcore_at_tok_free(tokens);
481                                 return;
482                         }
483                 }
484                 sap_status = atoi(g_slist_nth_data(tokens, 0));
485                 card_reader_status = (char *) g_slist_nth_data(tokens, 1);
486
487                 res.reader_status = *card_reader_status;
488
489                 switch(sap_status){
490                         case 0:
491                                 res.result = SAP_RESULT_CODE_OK;
492                                 break;
493                         case 1:
494                                 res.result = SAP_RESULT_CODE_NO_REASON;
495                                 break;
496                         case 2:
497                                 res.result = SAP_RESULT_CODE_CARD_NOT_ACCESSIBLE;
498                                 break;
499                         case 3:
500                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_OFF;
501                                 break;
502                         case 4:
503                                 res.result = SAP_RESULT_CODE_CARD_REMOVED;
504                                 break;
505                         case 5:
506                                 res.result = SAP_RESULT_CODE_CARD_ALREADY_POWER_ON;
507                                 break;
508                         case 6:
509                                 res.result = SAP_RESULT_CODE_DATA_NOT_AVAILABLE;
510                                 break;
511                         case 7:
512                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
513                                 break;
514                         default:
515                                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
516                                 break;
517                 }
518
519         }else{
520                 dbg("RESPONSE NOK");
521                 res.result = SAP_RESULT_CODE_NOT_SUPPORT;
522         }
523
524         if (ur) {
525                 tcore_user_request_send_response(ur, TRESP_SAP_REQ_CARDREADERSTATUS, sizeof(struct tresp_sap_req_cardreaderstatus), &res);
526         }
527         dbg(" Function exit");
528 }
529
530 static  TReturn s_connect(CoreObject *o, UserRequest *ur)
531 {
532         TcoreHal *hal;
533         TcoreATRequest *req;
534         TcorePending *pending = NULL;
535         char *cmd_str = NULL;
536         const struct treq_sap_req_connect *req_data;
537         int *usr_data = NULL;
538
539         dbg(" Function entry");
540         if (!o || !ur)
541                 return TCORE_RETURN_EINVAL;
542         hal = tcore_object_get_hal(o);
543         if(FALSE == tcore_hal_get_power_state(hal)){
544                 dbg("cp not ready/n");
545                 return TCORE_RETURN_ENOSYS;
546         }
547         req_data = tcore_user_request_ref_data(ur, NULL);
548         usr_data = (int*)malloc(sizeof(int));
549         *usr_data = req_data->max_msg_size;
550         cmd_str = g_strdup_printf("AT+XBCON=0,0,0");
551
552         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
553
554         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
555
556         tcore_pending_set_request_data(pending, 0, req);
557         tcore_pending_set_response_callback(pending, on_response_connect, usr_data);
558         tcore_pending_link_user_request(pending, ur);
559         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
560
561         tcore_hal_send_request(hal, pending);
562
563         free(cmd_str);
564         dbg(" Function exit");
565         return TCORE_RETURN_SUCCESS;
566 }
567
568 static  TReturn s_disconnect(CoreObject *o, UserRequest *ur)
569 {
570         TcoreHal *hal;
571         TcoreATRequest *req;
572         TcorePending *pending = NULL;
573         char *cmd_str = NULL;
574         const struct treq_sap_req_disconnect *req_data;
575
576         dbg(" Function entry");
577         if (!o || !ur)
578                 return TCORE_RETURN_EINVAL;
579         hal = tcore_object_get_hal(o);
580         if(FALSE == tcore_hal_get_power_state(hal)){
581                 dbg("cp not ready/n");
582                 return TCORE_RETURN_ENOSYS;
583         }
584
585         req_data = tcore_user_request_ref_data(ur, NULL);
586
587         cmd_str = g_strdup_printf("AT+ XBDISC");
588
589         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
590
591         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
592
593         tcore_pending_set_request_data(pending, 0, req);
594         tcore_pending_set_response_callback(pending, on_response_disconnect, NULL);
595         tcore_pending_link_user_request(pending, ur);
596         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
597
598         tcore_hal_send_request(hal, pending);
599
600         free(cmd_str);
601         dbg(" Function exit");
602         return TCORE_RETURN_SUCCESS;
603 }
604
605 static TReturn s_req_status(CoreObject *o, UserRequest *ur)
606 {
607         TcoreHal *hal;
608         TcoreATRequest *req;
609         TcorePending *pending = NULL;
610         char *cmd_str = NULL;
611         const struct treq_sap_req_status *req_data;
612
613         dbg(" Function entry");
614         if (!o || !ur)
615                 return TCORE_RETURN_EINVAL;
616         hal = tcore_object_get_hal(o);
617         if(FALSE == tcore_hal_get_power_state(hal)){
618                 dbg("cp not ready/n");
619                 return TCORE_RETURN_ENOSYS;
620         }
621
622         req_data = tcore_user_request_ref_data(ur, NULL);
623
624         //cmd_str = g_strdup_printf("");//ToDo - No AT command present.
625
626         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
627
628         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
629
630         tcore_pending_set_request_data(pending, 0, req);
631         tcore_pending_set_response_callback(pending, on_response_req_status, NULL);
632         tcore_pending_link_user_request(pending, ur);
633         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
634
635         tcore_hal_send_request(hal, pending);
636
637         free(cmd_str);
638         dbg(" Function exit");
639         return TCORE_RETURN_SUCCESS;
640 }
641
642 static TReturn s_set_transport_protocol(CoreObject *o, UserRequest *ur)
643 {
644         TcoreHal *hal;
645         TcoreATRequest *req;
646         TcorePending *pending = NULL;
647         char *cmd_str = NULL;
648         const struct treq_sap_set_protocol *req_data;
649
650         dbg(" Function entry");
651         if (!o || !ur)
652                 return TCORE_RETURN_EINVAL;
653         hal = tcore_object_get_hal(o);
654         if(FALSE == tcore_hal_get_power_state(hal)){
655                 dbg("cp not ready/n");
656                 return TCORE_RETURN_ENOSYS;
657         }
658
659         req_data = tcore_user_request_ref_data(ur, NULL);
660
661         //cmd_str = g_strdup_printf("");//ToDo - No AT command present.
662
663         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
664
665         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
666
667         tcore_pending_set_request_data(pending, 0, req);
668         tcore_pending_set_response_callback(pending, on_response_set_transfort_protocol, NULL);
669         tcore_pending_link_user_request(pending, ur);
670         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
671
672         tcore_hal_send_request(hal, pending);
673
674         free(cmd_str);
675         dbg(" Function exit");
676         return TCORE_RETURN_SUCCESS;
677 }
678
679 static  TReturn s_set_power(CoreObject *o, UserRequest *ur)
680 {
681         TcoreHal *hal;
682         TcoreATRequest *req;
683         TcorePending *pending = NULL;
684         char *cmd_str = NULL;
685         const struct treq_sap_set_power *req_data;
686         int action = -1;
687
688         dbg(" Function entry");
689         if (!o || !ur)
690                 return TCORE_RETURN_EINVAL;
691         hal = tcore_object_get_hal(o);
692         if(FALSE == tcore_hal_get_power_state(hal)){
693                 dbg("cp not ready/n");
694                 return TCORE_RETURN_ENOSYS;
695         }
696
697         req_data = tcore_user_request_ref_data(ur, NULL);
698
699         if(req_data->mode == SAP_POWER_ON) {
700                 action = 0;
701         } else if ( req_data->mode == SAP_POWER_OFF ) {
702                 action = 1;
703         } else if ( req_data->mode == SAP_POWER_RESET ) {
704                 action = 2;
705         } else {
706                 action = -1;;
707         }
708
709         cmd_str = g_strdup_printf("AT+ XBPWR=%d", action);
710
711         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
712
713         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
714
715         tcore_pending_set_request_data(pending, 0, req);
716         tcore_pending_set_response_callback(pending, on_response_set_power, NULL);
717         tcore_pending_link_user_request(pending, ur);
718         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
719
720         tcore_hal_send_request(hal, pending);
721
722         free(cmd_str);
723         dbg(" Function exit");
724         return TCORE_RETURN_SUCCESS;
725 }
726
727 static  TReturn s_get_atr(CoreObject *o, UserRequest *ur)
728 {
729         TcoreHal *hal;
730         TcoreATRequest *req;
731         TcorePending *pending = NULL;
732         char *cmd_str = NULL;
733         const struct treq_sap_req_atr *req_data;
734
735         dbg(" Function entry");
736         if (!o || !ur)
737                 return TCORE_RETURN_EINVAL;
738         hal = tcore_object_get_hal(o);
739         if(FALSE == tcore_hal_get_power_state(hal)){
740                 dbg("cp not ready/n");
741                 return TCORE_RETURN_ENOSYS;
742         }
743
744         req_data = tcore_user_request_ref_data(ur, NULL);
745
746         cmd_str = g_strdup_printf("AT+ XBATR");
747
748         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
749
750         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
751
752         tcore_pending_set_request_data(pending, 0, req);
753         tcore_pending_set_response_callback(pending, on_response_get_atr, NULL);
754         tcore_pending_link_user_request(pending, ur);
755         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
756
757         tcore_hal_send_request(hal, pending);
758
759         free(cmd_str);
760         dbg(" Function exit");
761         return TCORE_RETURN_SUCCESS;
762 }
763
764 static  TReturn s_transfer_apdu(CoreObject *o, UserRequest *ur)
765 {
766         TcoreHal *hal;
767         TcoreATRequest *req;
768         TcorePending *pending = NULL;
769         char *cmd_str = NULL;
770         const struct treq_sap_transfer_apdu *req_data;
771
772         dbg(" Function entry");
773         if (!o || !ur)
774                 return TCORE_RETURN_EINVAL;
775         hal = tcore_object_get_hal(o);
776         if(FALSE == tcore_hal_get_power_state(hal)){
777                 dbg("cp not ready/n");
778                 return TCORE_RETURN_ENOSYS;
779         }
780
781         req_data = tcore_user_request_ref_data(ur, NULL);
782
783         cmd_str = g_strdup_printf("AT+ XBAPDU=\"%s\"", req_data->apdu_data); //ToDo - Need to check passing input as a string.
784
785         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
786
787         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
788
789         tcore_pending_set_request_data(pending, 0, req);
790         tcore_pending_set_response_callback(pending, on_response_transfer_apdu, NULL);
791         tcore_pending_link_user_request(pending, ur);
792         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
793
794         tcore_hal_send_request(hal, pending);
795
796         free(cmd_str);
797         dbg(" Function exit");
798         return TCORE_RETURN_SUCCESS;
799 }
800
801 static  TReturn s_get_cardreader_status(CoreObject *o, UserRequest *ur)
802 {
803         TcoreHal *hal;
804         TcoreATRequest *req;
805         TcorePending *pending = NULL;
806         char *cmd_str = NULL;
807         const struct treq_sap_req_cardreaderstatus *req_data;
808
809         dbg(" Function entry");
810         if (!o || !ur)
811                 return TCORE_RETURN_EINVAL;
812         hal = tcore_object_get_hal(o);
813         if(FALSE == tcore_hal_get_power_state(hal)){
814                 dbg("cp not ready/n");
815                 return TCORE_RETURN_ENOSYS;
816         }
817
818         req_data = tcore_user_request_ref_data(ur, NULL);
819
820         cmd_str = g_strdup_printf("AT+ XBCRDSTAT");
821
822         req = tcore_at_request_new(cmd_str, NULL, TCORE_AT_SINGLELINE);
823
824         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
825
826         tcore_pending_set_request_data(pending, 0, req);
827         tcore_pending_set_response_callback(pending, on_response_get_cardreader_status, NULL);
828         tcore_pending_link_user_request(pending, ur);
829         tcore_pending_set_send_callback(pending, on_confirmation_sap_message_send, NULL);
830
831         tcore_hal_send_request(hal, pending);
832
833         free(cmd_str);
834         dbg(" Function exit");
835         return TCORE_RETURN_SUCCESS;
836 }
837
838 static struct tcore_sap_operations sap_ops =
839 {
840         .connect = s_connect,
841         .disconnect = s_disconnect,
842         .req_status = s_req_status,
843         .set_transport_protocol = s_set_transport_protocol,
844         .set_power = s_set_power,
845         .get_atr = s_get_atr,
846         .transfer_apdu = s_transfer_apdu,
847         .get_cardreader_status = s_get_cardreader_status,
848 };
849
850
851 gboolean s_sap_init(TcorePlugin *cp, CoreObject *co_sap)
852 {
853         dbg("Entry");
854
855         tcore_sap_override_ops(co_sap, &sap_ops);
856
857         tcore_object_override_callback(co_sap,"+XBCSTAT", on_event_sap_status, NULL);
858
859         dbg("Exit");
860
861         return TRUE;
862 }
863
864 void s_sap_exit(TcorePlugin *cp, CoreObject *co_sap)
865 {
866         dbg("Exit");
867 }