17ffa9c8f804b086bb7a740cb4871bfa8de4b7c8
[platform/core/telephony/tel-plugin-imc.git] / src / s_network.c
1 /*
2  * tel-plugin-imc
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Harish Bishnoi <hbishnoi@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
25 #include <glib.h>
26
27 #include <tcore.h>
28 #include <hal.h>
29 #include <core_object.h>
30 #include <plugin.h>
31 #include <user_request.h>
32 #include <queue.h>
33 #include <co_network.h>
34 #include <co_ps.h>
35 #include <server.h>
36 #include <storage.h>
37 #include <util.h>
38 #include <at.h>
39
40 #include "s_common.h"
41 #include "s_network.h"
42
43 #define AT_CREG_STAT_NOT_REG    0 /* not registered, MT is not currently searching a new operator to register to */
44 #define AT_CREG_STAT_REG_HOME   1 /* registered, home network */
45 #define AT_CREG_STAT_SEARCHING  2 /* not registered, but MT is currently searching a new operator to register to */
46 #define AT_CREG_STAT_REG_DENIED 3 /* registration denied */
47 #define AT_CREG_STAT_UNKNOWN    4 /* unknown */
48 #define AT_CREG_STAT_REG_ROAM   5 /* registered, roaming */
49
50 #define AT_COPS_MODE_AUTOMATIC  0 /* automatic (<oper> field is ignored) */
51 #define AT_COPS_MODE_MANUAL 1 /* manual (<oper> field shall be present, and <AcT> optionally) */
52 #define AT_COPS_MODE_DEREGISTER 2 /* deregister from network */
53 #define AT_COPS_MODE_SET_ONLY   3 /* set only <format> */
54 #define AT_COPS_MODE_MANUAL_AUTOMATIC 4 /*automatic - manual*/
55
56 #define AT_COPS_FORMAT_LONG_ALPHANUMERIC    0 /* long format alphanumeric <oper> */
57 #define AT_COPS_FORMAT_SHORT_ALPHANUMERIC   1 /* short format alphanumeric <oper> */
58 #define AT_COPS_FORMAT_NUMERIC          2 /* numeric <oper> */
59
60 #define AT_COPS_ACT_GSM         0   /* GSM */
61 #define AT_COPS_ACT_GSM_COMPACT     1   /* GSM Compact */
62 #define AT_COPS_ACT_UTRAN       2   /* UTRAN */
63 #define AT_COPS_ACT_GSM_EGPRS       3   /* GSM w/EGPRS */
64 #define AT_COPS_ACT_UTRAN_HSDPA     4   /* UTRAN w/HSDPA */
65 #define AT_COPS_ACT_UTRAN_HSUPA     5   /* UTRAN w/HSUPA */
66 #define AT_COPS_ACT_UTRAN_HSDPA_HSUPA   6   /* UTRAN w/HSDPA and HSUPA */
67 #define AT_COPS_ACT_E_UTRAN     7   /* E-UTRAN */
68
69 #define AT_GSM_XBANDSEL_AUTOMATIC 0
70 #define AT_GSM_XBANDSEL_1800 1800
71 #define AT_GSM_XBANDSEL_1900 1900
72 #define AT_GSM_XBANDSEL_900 900
73 #define AT_GSM_XBANDSEL_850 850
74 #define AT_GSM_XBANDSEL_450 450
75 #define AT_GSM_XBANDSEL_480 480
76 #define AT_GSM_XBANDSEL_750 750
77 #define AT_GSM_XBANDSEL_380 380
78 #define AT_GSM_XBANDSEL_410 410
79
80 #define AT_XRAT_GSM 0
81 #define AT_XRAT_DUAL 1
82 #define AT_XRAT_UMTS 2
83
84 #define MAX_NETWORKS_PREF_PLMN_SUPPORT 150
85 #define MAX_NETWORKS_MANUAL_SEARCH_SUPPORT 20
86
87 static unsigned int lookup_tbl_net_status[] = {
88         [AT_CREG_STAT_NOT_REG] = NETWORK_SERVICE_DOMAIN_STATUS_NO,
89         [AT_CREG_STAT_REG_HOME] = NETWORK_SERVICE_DOMAIN_STATUS_FULL,
90         [AT_CREG_STAT_SEARCHING] = NETWORK_SERVICE_DOMAIN_STATUS_SEARCH,
91         [AT_CREG_STAT_REG_DENIED] = NETWORK_SERVICE_DOMAIN_STATUS_EMERGENCY,
92         [AT_CREG_STAT_UNKNOWN] = NETWORK_SERVICE_DOMAIN_STATUS_NO,
93         [AT_CREG_STAT_REG_ROAM] = NETWORK_SERVICE_DOMAIN_STATUS_FULL,
94 };
95
96 static unsigned int lookup_tbl_access_technology[] = {
97         [AT_COPS_ACT_GSM] = NETWORK_ACT_GSM,
98         [AT_COPS_ACT_GSM_COMPACT] = NETWORK_ACT_GSM,
99         [AT_COPS_ACT_UTRAN] = NETWORK_ACT_UTRAN,
100         [AT_COPS_ACT_GSM_EGPRS] = NETWORK_ACT_EGPRS,
101         [AT_COPS_ACT_UTRAN_HSDPA] = NETWORK_ACT_UTRAN,
102         [AT_COPS_ACT_UTRAN_HSUPA] = NETWORK_ACT_UTRAN,
103         [AT_COPS_ACT_UTRAN_HSDPA_HSUPA] = NETWORK_ACT_UTRAN,
104         [AT_COPS_ACT_E_UTRAN] = NETWORK_ACT_GSM_UTRAN,
105 };
106
107 static gboolean get_serving_network(CoreObject *o, UserRequest *ur);
108
109
110 static void on_confirmation_network_message_send(TcorePending *p, gboolean result, void *user_data)
111 {
112         dbg("on_confirmation_modem_message_send - msg out from queue.\n");
113
114         if (result == FALSE) {
115                 /* Fail */
116                 dbg("SEND FAIL");
117         } else {
118                 dbg("SEND OK");
119         }
120 }
121
122 static void nwk_prepare_and_send_pending_request(CoreObject *co, const char *at_cmd, const char *prefix, enum tcore_at_command_type at_cmd_type, UserRequest *ur, TcorePendingResponseCallback callback)
123 {
124         TcoreATRequest *req = NULL;
125         TcoreHal *hal;
126         TcorePending *pending = NULL;
127         TReturn ret;
128
129         hal = tcore_object_get_hal(co);
130
131         pending = tcore_pending_new(co, 0);
132         req = tcore_at_request_new(at_cmd, prefix, at_cmd_type);
133
134         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));
135
136         tcore_pending_set_request_data(pending, 0, req);
137         tcore_pending_set_response_callback(pending, callback, req->cmd);
138         tcore_pending_link_user_request(pending, ur);
139         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
140
141         ret = tcore_hal_send_request(hal, pending);
142         return;
143 }
144
145
146 static void _insert_mcc_mnc_oper_list(TcorePlugin *p, CoreObject *o)
147 {
148         Server *s;
149         Storage *strg;
150         void *handle;
151         char query[255] = { 0, };
152         GHashTableIter iter;
153         gpointer key, value;
154         GHashTable *result = NULL, *row = NULL;
155         struct tcore_network_operator_info *noi = NULL;
156         int count = 0;
157
158         s = tcore_plugin_ref_server(p);
159         strg = tcore_server_find_storage(s, "database");
160
161         handle = tcore_storage_create_handle(strg, "/opt/dbspace/.mcc_mnc_oper_list.db");
162         if (!handle) {
163                 dbg("fail to create database handle");
164                 return;
165         }
166
167         snprintf(query, 255, "select country, mcc, mnc, oper from mcc_mnc_oper_list");
168
169         result = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
170                                                                    (GDestroyNotify) g_hash_table_destroy);
171
172         tcore_storage_read_query_database(strg, handle, query, NULL, result, 4);
173
174         g_hash_table_iter_init(&iter, result);
175         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
176                 row = value;
177
178                 noi = calloc(sizeof(struct tcore_network_operator_info), 1);
179
180                 snprintf(noi->mcc, 4, "%s", (char *) g_hash_table_lookup(row, "1"));
181                 snprintf(noi->mnc, 4, "%s", (char *) g_hash_table_lookup(row, "2"));
182                 snprintf(noi->name, 41, "%s", (char *) g_hash_table_lookup(row, "3"));
183                 snprintf(noi->country, 4, "%s", (char *) g_hash_table_lookup(row, "0"));
184
185                 tcore_network_operator_info_add(o, noi);
186
187                 count++;
188         }
189
190         dbg("count = %d", count);
191
192         g_hash_table_destroy(result);
193
194         tcore_storage_remove_handle(strg, handle);
195 }
196
197 static enum telephony_network_service_type _get_service_type(enum telephony_network_service_type prev_type,
198                                                                                                                          int domain, int act, int cs_status, int ps_status)
199 {
200         enum telephony_network_service_type ret;
201
202         ret = prev_type;
203
204         switch (act) {
205         case NETWORK_ACT_UNKNOWN:
206                 ret = NETWORK_SERVICE_TYPE_UNKNOWN;
207                 break;
208
209         case NETWORK_ACT_GSM:
210                 if (prev_type == NETWORK_SERVICE_TYPE_2_5G_EDGE && domain == NETWORK_SERVICE_DOMAIN_CS)
211                         ret = NETWORK_SERVICE_TYPE_2_5G_EDGE;
212                 else
213                         ret = NETWORK_SERVICE_TYPE_2G;
214                 break;
215
216         case NETWORK_ACT_EGPRS:
217                 return NETWORK_SERVICE_TYPE_2_5G_EDGE;
218                 break;
219
220         case NETWORK_ACT_UMTS:
221                 ret = NETWORK_SERVICE_TYPE_3G;
222                 break;
223         }
224
225         if (cs_status == NETWORK_SERVICE_DOMAIN_STATUS_NO && ps_status == NETWORK_SERVICE_DOMAIN_STATUS_NO) {
226                 ret = NETWORK_SERVICE_TYPE_NO_SERVICE;
227         } else if (cs_status == NETWORK_SERVICE_DOMAIN_STATUS_SEARCH || ps_status == NETWORK_SERVICE_DOMAIN_STATUS_SEARCH) {
228                 if (cs_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL || ps_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL) {
229                         /* no change */
230                 } else {
231                         ret = NETWORK_SERVICE_TYPE_SEARCH;
232                 }
233         } else if (cs_status == NETWORK_SERVICE_DOMAIN_STATUS_EMERGENCY || ps_status == NETWORK_SERVICE_DOMAIN_STATUS_EMERGENCY) {
234                 if (cs_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL || ps_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL) {
235                         /* no change */
236                 } else {
237                         ret = NETWORK_SERVICE_TYPE_EMERGENCY;
238                 }
239         }
240
241         return ret;
242 }
243
244 static void _ps_set(TcorePlugin *p, int status)
245 {
246         CoreObject *co_ps;
247
248         co_ps = tcore_plugin_ref_core_object(p, CORE_OBJECT_TYPE_PS);
249         if (co_ps == NULL) {
250                 err("No PS Core Object on plugin");
251                 return;
252         }
253
254         if (status == NETWORK_SERVICE_DOMAIN_STATUS_FULL)
255                 tcore_ps_set_online(co_ps, TRUE);
256         else
257                 tcore_ps_set_online(co_ps, FALSE);
258 }
259
260 static void on_timeout_search_network(TcorePending *p, void *user_data)
261 {
262         UserRequest *ur;
263         struct tresp_network_search resp;
264
265         dbg("TIMEOUT !!!!! pending=%p", p);
266
267         memset(&resp, 0, sizeof(struct tresp_network_search));
268
269         resp.result = TCORE_RETURN_FAILURE;
270         resp.list_count = 0;
271
272         ur = tcore_pending_ref_user_request(p);
273         if (ur) {
274                 tcore_user_request_send_response(ur, TRESP_NETWORK_SEARCH, sizeof(struct tresp_network_search), &resp);
275         }
276 }
277
278 static void on_response_set_plmn_selection_mode(TcorePending *p, int data_len, const void *data, void *user_data)
279 {
280         UserRequest *ur;
281         const TcoreATResponse *atResp = data;
282         // GSList *tokens = NULL;
283         // char * line = NULL;
284         struct tresp_network_set_plmn_selection_mode resp = {0};
285
286         if (atResp->success > 0) {
287                 dbg("RESPONSE OK");
288                 resp.result = TCORE_RETURN_SUCCESS;
289         } else {
290                 dbg("RESPONSE NOK");
291                 resp.result = TCORE_RETURN_FAILURE;
292         }
293
294         ur = tcore_pending_ref_user_request(p);
295         if (ur) {
296                 tcore_user_request_send_response(ur, TRESP_NETWORK_SET_PLMN_SELECTION_MODE, sizeof(struct tresp_network_set_plmn_selection_mode), &resp);
297         }
298 }
299
300 static void on_response_get_plmn_selection_mode(TcorePending *p, int data_len, const void *data, void *user_data)
301 {
302         UserRequest *ur;
303         struct tresp_network_get_plmn_selection_mode resp = {0};
304         const TcoreATResponse *atResp = data;
305         GSList *tokens = NULL;
306         char *line = NULL;
307         int mode = 0;
308
309         resp.result = TCORE_RETURN_FAILURE;
310
311         if (atResp->success > 0) {
312                 dbg("RESPONSE OK");
313                 /* Format of output
314                 +COPS: <mode>[,<format>,<oper>[,< AcT>]]
315                 */
316
317                 if (atResp->lines) {
318                         line = (char *) atResp->lines->data;
319                         tokens = tcore_at_tok_new(line);
320                         if (g_slist_length(tokens) < 1) {
321                                 msg("invalid message");
322                                 goto OUT;
323                         }
324                         mode = atoi(tcore_at_tok_nth(tokens, 0));
325                         dbg("mode = %d", mode);
326
327                         switch (mode) {
328                         case AT_COPS_MODE_AUTOMATIC:
329                                 resp.mode = NETWORK_SELECT_MODE_AUTOMATIC;
330                                 break;
331
332                         case AT_COPS_MODE_MANUAL:
333                         case AT_COPS_MODE_MANUAL_AUTOMATIC:
334                                 resp.mode = NETWORK_SELECT_MODE_MANUAL;
335                                 break;
336
337                         case AT_COPS_MODE_DEREGISTER:
338                         case AT_COPS_MODE_SET_ONLY:
339                                 resp.result = TCORE_RETURN_FAILURE;
340                                 goto OUT;
341                         }
342                         resp.result = TCORE_RETURN_SUCCESS;
343                 }
344         } else {
345                 dbg("RESPONSE NOK");
346                 resp.result = TCORE_RETURN_FAILURE;
347         }
348
349 OUT:
350         ur = tcore_pending_ref_user_request(p);
351         if (ur) {
352                 tcore_user_request_send_response(ur, TRESP_NETWORK_GET_PLMN_SELECTION_MODE, sizeof(struct tresp_network_get_plmn_selection_mode), &resp);
353         }
354
355         if (tokens != NULL)
356                 tcore_at_tok_free(tokens);
357
358         return;
359 }
360
361 static void on_response_search_network(TcorePending *p, int data_len, const void *data, void *user_data)
362 {
363         UserRequest *ur;
364         struct tresp_network_search resp;
365         int i = 0;
366         char *line = NULL;
367         const TcoreATResponse *atResp = data;
368         GSList *tokens = NULL;
369         GSList *network_token = NULL;
370         int AcT = 0;
371         char *temp_plmn_info = NULL;
372         char *alpha_name = NULL;
373         char *pResp = NULL;
374         int num_network_avail = 0;
375
376         memset(&resp, 0, sizeof(struct tresp_network_search));
377         resp.result = TCORE_RETURN_FAILURE;
378         resp.list_count = 0;
379
380         if (atResp->success > 0) {
381                 dbg("RESPONSE OK");
382                 if (atResp->lines) {
383                         line = (char *) atResp->lines->data;
384                         tokens = tcore_at_tok_new(line);
385                         num_network_avail = g_slist_length(tokens);
386                         dbg(" length of tokens is %d\n", num_network_avail);
387                         if (num_network_avail < 1) {
388                                 msg("invalid message");
389                                 goto OUT;
390                         }
391                 }
392
393                 resp.result = TCORE_RETURN_SUCCESS;
394                 /*
395                  *      +COPS: [list of supported (<stat>,long alphanumeric <oper>,short alphanumeric <oper>,numeric <oper>[,<AcT>])s]
396                  *             [,,(list of supported <mode>s),(list of supported <format>s)]
397                 */
398
399                 for (i = 0; ((i < num_network_avail) && (i < MAX_NETWORKS_MANUAL_SEARCH_SUPPORT)); i++) {
400                         network_token = tcore_at_tok_new(g_slist_nth_data(tokens, i));
401
402                         pResp = (tcore_at_tok_nth(network_token, 0));
403                         if (pResp != NULL) {
404                                 dbg("status : %s", pResp);
405                                 resp.list[i].status = (enum telephony_network_plmn_status) atoi(pResp);
406                         }
407
408                         if ((pResp = tcore_at_tok_nth(network_token, 1))) {     /* Long Alpha name */
409                                 dbg("Long Alpha name : %s", pResp);
410
411                                 if (strlen(pResp) > 0) {
412                                         alpha_name = tcore_at_tok_extract((const char *)pResp);
413                                         strncpy(resp.list[i].name, alpha_name, 39);
414                                         resp.list[i].name[39] = '\0';
415                                 }
416                         } else if ((pResp = tcore_at_tok_nth(network_token, 2))) {
417                                 dbg("Short Alpha name : %s", pResp);
418                                 /* Short Alpha name */
419
420                                 if (strlen(pResp) > 0) {
421                                         alpha_name = tcore_at_tok_extract((const char *)pResp);
422                                         strncpy(resp.list[i].name, alpha_name, 39);
423                                         resp.list[i].name[39] = '\0';
424                                 }
425                         }
426
427                         /* PLMN ID */
428                         pResp = tcore_at_tok_nth(network_token, 3);
429                         if (pResp != NULL) {
430                                 dbg("PLMN ID : %s", pResp);
431                                 temp_plmn_info = tcore_at_tok_extract((const char *)pResp);
432                                 strncpy(resp.list[i].plmn, temp_plmn_info, 6);
433                                 resp.list[i].plmn[6] = '\0';
434                         }
435
436                         /* Parse Access Technology */
437                         if ((pResp = tcore_at_tok_nth(tokens, 4))) {
438                                 if (strlen(pResp) > 0) {
439                                         AcT = atoi(pResp);
440
441                                         if (0 == AcT)
442                                                 resp.list[i].act = NETWORK_ACT_GSM;
443                                         else if (2 == AcT)
444                                                 resp.list[i].act = NETWORK_ACT_UMTS;
445                                 }
446                         }
447
448                         dbg("Operator [%d] :: stat = %d, Name =%s, plmnId = %s, AcT=%d\n", resp.list_count, resp.list[i].status, resp.list[i].name, resp.list[i].plmn, resp.list[i].act);
449                         resp.list_count++;
450
451                         tcore_at_tok_free(network_token);
452                         g_free(alpha_name);
453                         g_free(temp_plmn_info);
454                 }
455         } else {
456                 dbg("RESPONSE NOK");
457                 resp.result = TCORE_RETURN_FAILURE;
458         }
459
460 OUT:
461         ur = tcore_pending_ref_user_request(p);
462         if (ur) {
463                 tcore_user_request_send_response(ur, TRESP_NETWORK_SEARCH, sizeof(struct tresp_network_search), &resp);
464         }
465
466         /* Free tokens */
467         tcore_at_tok_free(tokens);
468 }
469
470 static void on_response_set_umts_band(TcorePending *p, int data_len, const void *data, void *user_data)
471 {
472         const TcoreATResponse *atResp = data;
473
474         dbg("On Response Set UMTS Band");
475
476         if (atResp->success > 0) {
477                 dbg("Response OK");
478         } else {
479                 dbg("Response NOK");
480         }
481
482         dbg("Wait for response of XRAT before sending final band setting response to AP");
483         return;
484 }
485
486
487 static void on_response_set_gsm_band(TcorePending *p, int data_len, const void *data, void *user_data)
488 {
489         const TcoreATResponse *atResp = data;
490
491         dbg("On Response Set GSM Band");
492         if (atResp->success > 0) {
493                 dbg("Response OK");
494         } else {
495                 dbg("Response NOK");
496         }
497
498         dbg("Wait for response of XRAT before sending final band setting response to AP");
499         return;
500 }
501
502 static void on_response_get_umts_band(TcorePending *p, int data_len, const void *data, void *user_data)
503 {
504         const TcoreATResponse *atResp = data;
505         GSList *tokens = NULL;
506         const char *line = NULL;
507         int total_umts_bands = 0;
508         int i = 0;
509         char *band_token = NULL;
510         char umts_band[20] = {0};
511         char umts_band_1 = 0;
512         char umts_band_2 = 0;
513         char umts_band_5 = 0;
514         UserRequest *ur = NULL;
515         struct tresp_network_get_band resp = {0};
516
517         dbg("Entry on_response_get_umts_band");
518
519         resp.mode = NETWORK_BAND_MODE_PREFERRED;
520         resp.result = TCORE_RETURN_SUCCESS;
521
522         if (atResp->success > 0) {
523                 dbg("RESPONSE OK");
524                 if (atResp->lines) {
525                         line = (char *) atResp->lines->data;
526                         tokens = tcore_at_tok_new(line);
527                         total_umts_bands = g_slist_length(tokens);
528                         dbg("Total UMTS bands enabled are : %d\n", total_umts_bands);
529                         if (total_umts_bands < 1) {
530                                 goto OUT;
531                         }
532                 }
533         } else {
534                 dbg("RESPONSE NOK");
535                 goto OUT;
536         }
537
538         for (i = 0; i < total_umts_bands; i++) {
539                 band_token = tcore_at_tok_nth(tokens, i);
540
541                 if (band_token == NULL)
542                         continue;
543
544                 memset(umts_band, 0x00, sizeof(umts_band));
545
546                 if (atoi(band_token) == 0) { /* 0 means UMTS automatic */
547                         umts_band_1 = umts_band_2 = umts_band_5 = TRUE;
548                         break;
549                 }
550
551                 /* Strip off starting quotes & ending quotes */
552                 strncpy(umts_band, band_token + 1, strlen(band_token) - 2);
553
554                 if (!strcmp(umts_band, "UMTS_BAND_I")) {
555                         umts_band_1 = TRUE;
556                 } else if (!strcmp(umts_band, "UMTS_BAND_II")) {
557                         umts_band_2 = TRUE;
558                 } else if (!strcmp(umts_band, "UMTS_BAND_II")) {
559                         umts_band_5 = TRUE;
560                 } else {
561                         /* Telephony is not interest */
562                         dbg("Telephony is not interested in %s band", umts_band);
563                 }
564         }
565
566 OUT:
567         if ((umts_band_1) && (umts_band_2) && (umts_band_5)) {
568                 resp.band = NETWORK_BAND_TYPE_WCDMA;
569         } else if (umts_band_1) {
570                 resp.band = NETWORK_BAND_TYPE_WCDMA2100;
571         } else if (umts_band_2) {
572                 resp.band = NETWORK_BAND_TYPE_WCDMA1900;
573         } else if (umts_band_5) {
574                 resp.band = NETWORK_BAND_TYPE_WCDMA850;
575         } else {
576                 resp.result = TCORE_RETURN_FAILURE;
577         }
578
579         dbg("Final resp.band sent to TS = %d", resp.band);
580
581         ur = tcore_pending_ref_user_request(p);
582         if (ur) {
583                 tcore_user_request_send_response(ur, TRESP_NETWORK_GET_BAND, sizeof(struct tresp_network_get_band), &resp);
584         }
585
586         if (tokens != NULL)
587                 tcore_at_tok_free(tokens);
588
589         dbg("Exit on_response_get_umts_band");
590         return;
591 }
592
593 static void on_response_get_gsm_band(TcorePending *p, int data_len, const void *data, void *user_data)
594 {
595         struct tresp_network_get_band resp = {0};
596         const TcoreATResponse *atResp = data;
597         GSList *tokens = NULL;
598         int total_gsm_bands = 0;
599         const char *line = NULL;
600         int i = 0;
601         char *band_token = NULL;
602         UserRequest *ur = NULL;
603         int gsm_850 = 0;
604         int gsm_900 = 0;
605         int gsm_1800 = 0;
606         int gsm_1900 = 0;
607
608         dbg("Entry on_response_get_gsm_band");
609
610         resp.mode = NETWORK_BAND_MODE_PREFERRED;
611         resp.result = TCORE_RETURN_SUCCESS;
612
613         if (atResp->success > 0) {
614                 dbg("RESPONSE OK");
615                 if (atResp->lines) {
616                         line = (char *) atResp->lines->data;
617                         tokens = tcore_at_tok_new(line);
618                         total_gsm_bands = g_slist_length(tokens);
619                         dbg("Total GSM bands enabled are : %d\n", total_gsm_bands);
620                         if (total_gsm_bands < 1)
621                                 goto OUT;
622                 }
623         }
624
625         for (i = 0; i < total_gsm_bands; i++) {
626                 band_token = tcore_at_tok_nth(tokens, i);
627
628                 if (band_token == NULL)
629                         continue;
630
631                 if (atoi(band_token) == 0) { /* 0 means GSM automatic */
632                         gsm_850 = gsm_900 = gsm_1800 = gsm_1900 = TRUE;
633                         break;
634                 }
635
636                 switch (atoi(band_token)) {
637                 case AT_GSM_XBANDSEL_850:
638                         gsm_850 = TRUE;
639                         break;
640
641                 case AT_GSM_XBANDSEL_900:
642                         gsm_900 = TRUE;
643                         break;
644
645                 case AT_GSM_XBANDSEL_1800:
646                         gsm_1800 = TRUE;
647                         break;
648
649                 case AT_GSM_XBANDSEL_1900:
650                         gsm_1900 = TRUE;
651                         break;
652
653                 default:
654                         break;
655                 }
656         }
657
658 OUT:
659
660         if (gsm_850 && gsm_900 && gsm_1800 && gsm_1900) {
661                 resp.band = NETWORK_BAND_TYPE_GSM;
662         } else if (gsm_850 && gsm_1900) {
663                 resp.band = NETWORK_BAND_TYPE_GSM_850_1900;
664         } else if (gsm_900 && gsm_1800) {
665                 resp.band = NETWORK_BAND_TYPE_GSM_900_1800;
666         } else if (gsm_1900) {
667                 resp.band = NETWORK_BAND_TYPE_GSM1900;
668         } else if (gsm_850) {
669                 resp.band = NETWORK_BAND_TYPE_GSM850;
670         } else if (gsm_1800) {
671                 resp.band = NETWORK_BAND_TYPE_GSM1800;
672         } else if (gsm_900) {
673                 resp.band = NETWORK_BAND_TYPE_GSM900;
674         } else {
675                 resp.result = TCORE_RETURN_FAILURE;
676         }
677
678         dbg("Final resp.band sent to TS = %d", resp.band);
679
680         ur = tcore_pending_ref_user_request(p);
681         if (ur) {
682                 tcore_user_request_send_response(ur, TRESP_NETWORK_GET_BAND, sizeof(struct tresp_network_get_band), &resp);
683         }
684
685         if (tokens != NULL)
686                 tcore_at_tok_free(tokens);
687
688         dbg("Exit on_response_get_gsm_band");
689         return;
690 }
691
692
693 static void on_response_get_xrat(TcorePending *p, int data_len, const void *data, void *user_data)
694 {
695         TcoreHal *h = NULL;
696         UserRequest *ur = NULL;
697
698         TcoreATRequest *atreq;
699         char *cmd_str = NULL;
700         UserRequest *dup_ur = NULL;
701         const TcoreATResponse *atResp = data;
702         const char *line = NULL;
703         char *pResp = NULL;
704         GSList *tokens = NULL;
705         TcorePending *pending = NULL;
706         CoreObject *o = NULL;
707         int cp_xrat = 0;
708         struct tresp_network_get_band resp = {0};
709
710         dbg("Enter on_response_get_xrat !!");
711
712         resp.mode = NETWORK_BAND_MODE_PREFERRED;
713
714         ur = tcore_pending_ref_user_request(p);
715         h = tcore_object_get_hal(tcore_pending_ref_core_object(p));
716         o = tcore_pending_ref_core_object(p);
717
718         if (atResp->success > 0) {
719                 dbg("RESPONSE OK");
720                 if (atResp->lines) {
721                         line = (char *) atResp->lines->data;
722                         tokens = tcore_at_tok_new(line);
723                         if (g_slist_length(tokens) < 1) {
724                                 msg("invalid message");
725                                 goto OUT;
726                         }
727                 }
728
729                 if ((pResp = tcore_at_tok_nth(tokens, 0))) {
730                         cp_xrat = atoi(pResp);
731
732                         if ((cp_xrat == AT_XRAT_DUAL)) {   /* mode is Dual, send reply to Telephony */
733                                 resp.result = TCORE_RETURN_SUCCESS;
734                                 resp.band = NETWORK_BAND_TYPE_ANY;
735
736                                 ur = tcore_pending_ref_user_request(p);
737                                 if (ur) {
738                                         tcore_user_request_send_response(ur, TRESP_NETWORK_GET_BAND, sizeof(struct tresp_network_get_band), &resp);
739                                 }
740                                 goto OUT;
741                         } else if ((cp_xrat == AT_XRAT_UMTS)) {
742                                 /* Get UMTS Band Information */
743                                 dup_ur = tcore_user_request_ref(ur); /* duplicate user request for AT+XUBANDSEL */
744                                 cmd_str = g_strdup_printf("AT+XUBANDSEL?");
745                                 atreq = tcore_at_request_new(cmd_str, "+XUBANDSEL", TCORE_AT_SINGLELINE);
746                                 pending = tcore_pending_new(o, 0);
747                                 tcore_pending_set_request_data(pending, 0, atreq);
748                                 tcore_pending_set_response_callback(pending, on_response_get_umts_band, NULL);
749                                 tcore_pending_link_user_request(pending, ur);
750                                 tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
751                                 tcore_hal_send_request(h, pending);
752                                 g_free(cmd_str);
753                         } else if ((cp_xrat == AT_XRAT_UMTS)) {
754                                 /* Get GSM Band Information */
755                                 dup_ur = tcore_user_request_ref(ur); /* duplicate user request for AT+XBANDSEL */
756                                 cmd_str = g_strdup_printf("AT+XBANDSEL?");
757                                 atreq = tcore_at_request_new(cmd_str, "+XBANDSEL", TCORE_AT_SINGLELINE);
758                                 pending = tcore_pending_new(o, 0);
759                                 tcore_pending_set_request_data(pending, 0, atreq);
760                                 tcore_pending_set_response_callback(pending, on_response_get_gsm_band, NULL);
761                                 tcore_pending_link_user_request(pending, dup_ur);
762                                 tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
763                                 tcore_hal_send_request(h, pending);
764                                 g_free(cmd_str);
765                         }
766                 }
767         } else {
768                 dbg("RESPONSE NOK");
769
770                 resp.result = TCORE_RETURN_FAILURE;
771                 resp.band = NETWORK_BAND_TYPE_ANY;
772
773                 ur = tcore_pending_ref_user_request(p);
774                 if (ur) {
775                         tcore_user_request_send_response(ur, TRESP_NETWORK_GET_BAND, sizeof(struct tresp_network_get_band), &resp);
776                 }
777         }
778 OUT:
779
780         if (tokens != NULL)
781                 tcore_at_tok_free(tokens);
782
783         dbg("Exit on_response_get_xrat !!");
784
785         return;
786 }
787
788
789 static void on_response_set_xrat(TcorePending *p, int data_len, const void *data, void *user_data)
790 {
791         UserRequest *ur = NULL;
792         struct tresp_network_set_band resp = {0};
793         const TcoreATResponse *atResp = data;
794
795         dbg("On Response Set XRAT");
796
797         if (atResp->success > 0) {
798                 dbg("Response OK");
799                 resp.result = TCORE_RETURN_SUCCESS;
800         } else {
801                 dbg("Response NOK");
802                 resp.result = TCORE_RETURN_FAILURE;
803         }
804
805         ur = tcore_pending_ref_user_request(p);
806         if (ur) {
807                 tcore_user_request_send_response(ur, TRESP_NETWORK_SET_BAND, sizeof(struct tresp_network_set_band), &resp);
808         }
809
810         return;
811 }
812
813 static void on_response_set_preferred_plmn(TcorePending *p, int data_len, const void *data, void *user_data)
814 {
815         UserRequest *ur = NULL;
816         struct tresp_network_set_preferred_plmn resp = {0};
817         const TcoreATResponse *atResp = data;
818
819         dbg("ENTER on_response_set_preferred_plmn");
820
821         if (atResp->success > 0) {
822                 dbg("Response OK");
823                 resp.result = TCORE_RETURN_SUCCESS;
824         } else {
825                 dbg("Response NOK");
826                 resp.result = TCORE_RETURN_FAILURE;
827         }
828
829         ur = tcore_pending_ref_user_request(p);
830         if (ur) {
831                 tcore_user_request_send_response(ur, TRESP_NETWORK_SET_PREFERRED_PLMN, sizeof(struct tresp_network_set_preferred_plmn), &resp);
832         }
833
834         dbg("Exit on_response_set_preferred_plmn");
835         return;
836 }
837
838 static void on_response_get_nitz_name(TcorePending *p, int data_len, const void *data, void *user_data)
839 {
840         const TcoreATResponse *atResp = data;
841         GSList *tokens = NULL;
842         const char *line = NULL;
843         CoreObject *o = NULL;
844         struct tnoti_network_identity noti;
845         int nol = 0;
846         int count = 0;
847         int net_name_type = 0;
848         char *pResp = NULL;
849         char *net_name = NULL;
850
851         dbg("Entry on_response_get_nitz_name (+XCOPS)");
852         o = tcore_pending_ref_core_object(p);
853         if (atResp->success > 0) {
854                 dbg("RESPONSE OK");
855
856                 if (atResp->lines) {
857                         nol = g_slist_length(atResp->lines);
858                         if (nol > 3) {
859                                 msg("invalid message");
860                                 goto OUT;
861                         }
862
863                         memset(&noti, 0, sizeof(struct tnoti_network_identity));
864
865                         for (count = 0; count < nol; count++) {
866                                 // parse each line
867                                 line = g_slist_nth_data(atResp->lines, count);
868                                 tokens = tcore_at_tok_new(line);
869                                 dbg("line %d start---------------", count);
870
871                                 if ((pResp = tcore_at_tok_nth(tokens, 0))) {
872                                         net_name_type = atoi(pResp);
873                                         dbg("Net name type  : %d", net_name_type);
874
875                                         switch (net_name_type) {
876                                         case 0: /* plmn_id (mcc, mnc) */
877                                                 if ((pResp = tcore_at_tok_nth(tokens, 1))) {
878                                                         if (strlen(pResp) > 0) {
879                                                                 net_name = tcore_at_tok_extract((const char *)pResp);
880                                                                 strncpy(noti.plmn, net_name, 6);
881                                                                 noti.plmn[6] = '\0';
882                                                         }
883                                                 }
884                                                 break;
885
886                                         case 5: /* Short NITZ name*/
887                                                 if ((pResp = tcore_at_tok_nth(tokens, 1))) {
888                                                         if (strlen(pResp) > 0) {
889                                                                 net_name = tcore_at_tok_extract((const char *)pResp);
890                                                                 strncpy(noti.short_name, net_name, 16);
891                                                                 noti.short_name[16] = '\0';
892                                                         }
893                                                 }
894                                                 break;
895
896                                         case 6: /* Full NITZ name */
897                                                 if ((pResp = tcore_at_tok_nth(tokens, 1))) {
898                                                         if (strlen(pResp) > 0) {
899                                                                 net_name = tcore_at_tok_extract((const char *)pResp);
900                                                                 strncpy(noti.full_name, net_name, 32);
901                                                                 noti.full_name[32] = '\0';
902                                                         }
903                                                 }
904                                                 break;
905
906                                         default:
907                                                 break;
908                                         }
909
910                                         g_free(net_name);
911                                 }
912
913                                 tcore_at_tok_free(tokens);
914                         }
915
916                         dbg("plmn <%s> short NITZ name<%s> full NITZ name<%s>",
917                                 noti.plmn, noti.short_name, noti.full_name);
918                         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_IDENTITY,
919                                                                                    sizeof(struct tnoti_network_identity), &noti);
920                 }
921         } else {
922                 dbg("RESPONSE NOK");
923         }
924
925 OUT:
926         dbg("Exit on_response_get_nitz_name");
927 }
928
929 static void on_response_get_preferred_plmn(TcorePending *p, int data_len, const void *data, void *user_data)
930 {
931         UserRequest *ur;
932         int i = 0;
933         char *line = NULL;
934         const TcoreATResponse *atResp = data;
935         GSList *tokens = NULL;
936         char *pResp = NULL;
937         int plmn_format = 0;
938
939         struct tresp_network_get_preferred_plmn resp = {0};
940         int total_lines = 0;
941         int GSM_AcT2 = 0, GSM_Compact_AcT2 = 0, UTRAN_AcT2 = 0;
942
943         dbg("Entry");
944
945         if (atResp->success > 0) {
946                 dbg("RESPONSE OK");
947                 if (atResp->lines) {
948                         total_lines = g_slist_length(atResp->lines);
949                         dbg("Total number of network present in Preferred PLMN list is %d\n", total_lines);
950
951                         if (total_lines < 1) {
952                                 msg("invalid message");
953                                 goto OUT;
954                         }
955
956                         if (total_lines >= MAX_NETWORKS_PREF_PLMN_SUPPORT)
957                                 total_lines = MAX_NETWORKS_PREF_PLMN_SUPPORT;
958
959 /*
960 +CPOL: <index1>,<format>,<oper1>[,<GSM_AcT1>,<GSM_Compact_AcT1>,<UTRAN_AcT1>,<E-UTRAN_AcT1>] [<CR><LF>
961 +CPOL: <index2>,<format>,<oper2>[,<GSM_AcT2>,<GSM_Compact_AcT2>,<UTRAN_AcT2>,<E-UTRAN_AcT2>]
962 */
963                         resp.result = TCORE_RETURN_SUCCESS;
964
965                         for (i = 0; i < total_lines; i++) {
966                                 /* Take each line response at a time & parse it */
967                                 line = tcore_at_tok_nth(atResp->lines, i);
968                                 tokens = tcore_at_tok_new(line);
969
970                                 /* <index2>,<format>,<oper2>[,<GSM_AcT2>,<GSM_Compact_AcT2>,<UTRAN_AcT2>,<E-UTRAN_AcT2>] */
971
972                                 /* EF Index */
973                                 if ((pResp = tcore_at_tok_nth(tokens, 0))) {
974                                         dbg("Index : %s", pResp);
975                                         resp.list[i].index = atoi(pResp);
976                                 }
977                                 /* Format */
978                                 if ((pResp = tcore_at_tok_nth(tokens, 1))) {
979                                         dbg("format : %s", pResp);
980                                         plmn_format = atoi(pResp);
981                                 }
982
983                                 /* Operator PLMN ID */
984                                 if ((pResp = tcore_at_tok_nth(tokens, 2))) {
985                                         dbg("plmn ID : %s", pResp);
986
987                                         if (strlen(pResp) > 0) {
988                                                 char *oper;
989
990                                                 oper = tcore_at_tok_extract((const char *)pResp);
991                                                 dbg("operator <%s>", oper);
992
993                                                 // Get only PLMN ID
994                                                 if (plmn_format == 2) {
995                                                         strncpy(resp.list[i].plmn, oper, 6);
996                                                         resp.list[i].plmn[6] = '\0';
997                                                 }
998
999                                                 g_free (oper);
1000                                         }
1001                                 }
1002
1003                                 if ((pResp = tcore_at_tok_nth(tokens, 3))) {
1004                                         dbg("GSM_AcT2  : %s", pResp);
1005                                         GSM_AcT2 = atoi(pResp);
1006                                 }
1007
1008                                 if ((pResp = tcore_at_tok_nth(tokens, 4))) {
1009                                         dbg("GSM_Compact AcT2  : %s", pResp);
1010                                         GSM_Compact_AcT2 = atoi(pResp);
1011                                 }
1012
1013                                 if ((pResp = tcore_at_tok_nth(tokens, 5))) {
1014                                         dbg("UTRAN_AcT2  : %s", pResp);
1015                                         UTRAN_AcT2 = atoi(pResp);
1016                                 }
1017
1018                                 if (UTRAN_AcT2 && (GSM_AcT2 || GSM_Compact_AcT2))
1019                                         resp.list[i].act = NETWORK_ACT_GSM_UTRAN;
1020                                 else if (UTRAN_AcT2)
1021                                         resp.list[i].act = NETWORK_ACT_UMTS;
1022                                 else if (GSM_AcT2 || GSM_Compact_AcT2)
1023                                         resp.list[i].act = NETWORK_ACT_GPRS;
1024
1025                                 (resp.list_count)++;
1026
1027                                 tcore_at_tok_free(tokens);
1028                         }
1029                 }
1030         } else {
1031                 dbg("RESPONSE NOT OK");
1032                 // TODO: CMEE error mapping is required.
1033                 resp.result = TCORE_RETURN_FAILURE;
1034         }
1035
1036 OUT:
1037         ur = tcore_pending_ref_user_request(p);
1038         if (ur) {
1039                 tcore_user_request_send_response(ur, TRESP_NETWORK_GET_PREFERRED_PLMN, sizeof(struct tresp_network_get_preferred_plmn), &resp);
1040         }
1041         dbg("Exit");
1042         return;
1043 }
1044
1045 static void on_response_get_serving_network(TcorePending *p, int data_len, const void *data, void *user_data)
1046 {
1047         const TcoreATResponse *resp = data;
1048         UserRequest *ur;
1049         struct tresp_network_get_serving_network Tresp = {0};
1050         char plmn[7] = {0};
1051         char *long_plmn_name = NULL;
1052         char *short_plmn_name = NULL;
1053         char *plmn_id = NULL;
1054         CoreObject *o;
1055         GSList *tokens = NULL;
1056         const char *line;
1057         int network_mode = -1;
1058         int plmn_format = -1;
1059         int AcT = -1;
1060         struct tnoti_network_identity noti;
1061         char *pResp = NULL;
1062         int nol, count = 0;
1063
1064         o = tcore_pending_ref_core_object(p);
1065
1066         if (resp->success <= 0) {
1067                 dbg("RESPONSE NOK");
1068
1069                 ur = tcore_pending_ref_user_request(p);
1070                 if (ur) {
1071                         Tresp.result = TCORE_RETURN_FAILURE;
1072                         tcore_user_request_send_response(ur, TRESP_NETWORK_GET_SERVING_NETWORK, sizeof(struct tresp_network_get_serving_network), &Tresp);
1073                 }
1074
1075                 return;
1076         } else {
1077                 dbg("RESPONSE OK");
1078                 nol = g_slist_length(resp->lines);
1079                 dbg("nol : %d", nol);
1080
1081                 for (count = 0; count < nol; count++) {
1082                         // parse each line
1083                         line = g_slist_nth_data(resp->lines, count);
1084                         tokens = tcore_at_tok_new(line);
1085                         dbg("line %d start---------------", count);
1086                         // mode
1087                         if ((pResp = tcore_at_tok_nth(tokens, 0))) {
1088                                 dbg("mode  : %s", pResp);
1089                                 network_mode = atoi(pResp);
1090                         }
1091
1092                         // format (optional)
1093                         if ((pResp = tcore_at_tok_nth(tokens, 1))) {
1094                                 dbg("format  : %s", pResp);
1095                                 if (strlen(pResp) > 0)
1096                                         plmn_format = atoi(pResp);
1097                         }
1098
1099                         // plmn
1100                         switch (plmn_format) {
1101                         case AT_COPS_FORMAT_LONG_ALPHANUMERIC:
1102                                 if ((pResp = tcore_at_tok_nth(tokens, 2))) {
1103                                         dbg("long PLMN  : %s", pResp);
1104                                         if (strlen(pResp) > 0) {
1105                                                 long_plmn_name = tcore_at_tok_extract((const char *)pResp);
1106
1107                                                 // set network name into po
1108                                                 tcore_network_set_network_name(o, TCORE_NETWORK_NAME_TYPE_FULL, long_plmn_name);
1109                                         }
1110                                 }
1111                                 break;
1112
1113                         case AT_COPS_FORMAT_SHORT_ALPHANUMERIC:
1114                                 if ((pResp = tcore_at_tok_nth(tokens, 2))) {
1115                                         dbg("short PLMN  : %s", pResp);
1116                                         if (strlen(pResp) > 0) {
1117                                                 short_plmn_name = tcore_at_tok_extract((const char *)pResp);
1118
1119                                                 // set network name into po
1120                                                 tcore_network_set_network_name(o, TCORE_NETWORK_NAME_TYPE_SHORT, short_plmn_name);
1121                                         }
1122                                 }
1123                                 break;
1124
1125                         case AT_COPS_FORMAT_NUMERIC:
1126                                 if ((pResp = tcore_at_tok_nth(tokens, 2))) {
1127                                         dbg("numeric : %s", pResp);
1128                                         if (strlen(pResp) > 0) {
1129                                                 plmn_id = tcore_at_tok_extract((const char *)pResp);
1130
1131                                                 // set plmn id into po
1132                                                 tcore_network_set_plmn(o, plmn_id);
1133                                         }
1134                                 }
1135                                 break;
1136
1137                         default:
1138                                 break;
1139                         }
1140
1141                         // act
1142                         if ((pResp = tcore_at_tok_nth(tokens, 3))) {
1143                                 dbg("AcT  : %s", pResp);
1144                                 if (strlen(pResp) > 0) {
1145                                         AcT = atoi(pResp);
1146                                         tcore_network_set_access_technology(o, lookup_tbl_access_technology[AcT]);
1147                                 }
1148                         }
1149
1150                         tcore_at_tok_free(tokens);
1151                 }
1152
1153                 memcpy(Tresp.plmn, plmn, 7);
1154                 tcore_network_get_access_technology(o, &(Tresp.act));
1155                 tcore_network_get_lac(o, &(Tresp.gsm.lac));
1156
1157                 ur = tcore_pending_ref_user_request(p);
1158                 if (ur) {
1159                         Tresp.result = TCORE_RETURN_SUCCESS;
1160                         tcore_user_request_send_response(ur, TRESP_NETWORK_GET_SERVING_NETWORK, sizeof(struct tresp_network_get_serving_network), &Tresp);
1161                 } else {
1162                         /* Network change noti */
1163                         struct tnoti_network_change network_change;
1164
1165                         memset(&network_change, 0, sizeof(struct tnoti_network_change));
1166                         memcpy(network_change.plmn, plmn, 7);
1167                         tcore_network_get_access_technology(o, &(network_change.act));
1168                         tcore_network_get_lac(o, &(network_change.gsm.lac));
1169
1170                         tcore_server_send_notification(tcore_plugin_ref_server(tcore_pending_ref_plugin(p)), tcore_pending_ref_core_object(p),
1171                                                                                 TNOTI_NETWORK_CHANGE, sizeof(struct tnoti_network_change), &network_change);
1172                         dbg("dbg.. network_change.plmn  : %s", network_change.plmn);
1173                         dbg("dbg.. network_change.act  : %d", network_change.act);
1174                         dbg("dbg.. network_change.gsm.lac  : %d", network_change.gsm.lac);
1175
1176                         if ((AT_COPS_MODE_DEREGISTER != network_mode) &&
1177                                 (AT_COPS_MODE_SET_ONLY != network_mode)) {
1178                                 /*Network identity noti*/
1179                                 memset(&noti, 0x0, sizeof(struct tnoti_network_change));
1180                                 if (long_plmn_name)
1181                                         memcpy(noti.full_name, long_plmn_name, MIN(32, strlen(long_plmn_name)));
1182                                 if (short_plmn_name)
1183                                         memcpy(noti.short_name, short_plmn_name, MIN(16, strlen(short_plmn_name)));
1184                                 if (plmn_id)
1185                                         memcpy(noti.plmn, plmn_id, strlen(plmn_id)); // plmn_id length is necessarily <= 6
1186
1187                                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)),
1188                                                                 o, TNOTI_NETWORK_IDENTITY, sizeof(struct tnoti_network_identity), &noti);
1189                                 dbg("dbg.. noti.short_name  : %s", noti.short_name);
1190                                 dbg("dbg.. noti.full_name  : %s", noti.full_name);
1191                                 dbg("dbg.. noti.plmn  : %s", noti.plmn);
1192                         }
1193                 }
1194
1195                 g_free(long_plmn_name);
1196                 g_free(short_plmn_name);
1197                 g_free(plmn_id);
1198         }
1199         return;
1200 }
1201
1202 static gboolean on_event_ps_network_regist(CoreObject *o, const void *data, void *user_data)
1203 {
1204         struct tnoti_network_registration_status regist_status;
1205         enum telephony_network_service_domain_status cs_status;
1206         enum telephony_network_service_domain_status ps_status;
1207         enum telephony_network_service_type service_type;
1208         enum telephony_network_access_technology act = NETWORK_ACT_UNKNOWN;
1209         struct tnoti_network_location_cellinfo net_lac_cell_info = {0};
1210         struct tnoti_ps_protocol_status noti = {0};
1211         unsigned char svc_domain = NETWORK_SERVICE_DOMAIN_PS;
1212         int stat = 0, AcT = 0;
1213         unsigned int lac = 0xffff, ci = 0xffff;
1214         unsigned int rac = 0xffff;
1215         GSList *tokens = NULL;
1216         char *pResp;
1217         char *line = NULL;
1218         GSList *lines = NULL;
1219
1220         lines = (GSList *) data;
1221         if (1 != g_slist_length(lines)) {
1222                 dbg("unsolicited msg but multiple line");
1223                 goto OUT;
1224         }
1225         line = (char *) (lines->data);
1226         dbg("+CGREG NOTI RECEIVED");
1227
1228 /*
1229 +CREG: <stat> [[,<lac>,<ci>[AcT]]
1230
1231 Possible values of <stat> can be
1232 0 Not registered, ME is not currently searching a new operator to register to
1233 1 Registered, home network
1234 2 Not registered, but ME is currently searching a new operator to register
1235 3 Registration denied
1236 4 Unknown
1237 5 Registered, in roaming
1238
1239 <lac>
1240 string type; two byte location area code in hexadecimal format (e.g. 00C3)
1241
1242 <ci>
1243 string type; four byte cell ID in hexadecimal format (e.g. 0000A13F)
1244
1245 <ACT>
1246 0 GSM
1247 2 UTRAN
1248 3 GSM w/EGPRS
1249 4 UTRAN w/HSDPA
1250 5 UTRAN w/HSUPA
1251 6 UTRAN w/HSDPA and HSUPA
1252 Note: <Act> is supporting from R7 and above Protocol Stack.
1253
1254 <rac>: is R7 and above feature, string type; one byte routing area code in hexadecimal format.
1255 */
1256         if (line != NULL) {
1257                 tokens = tcore_at_tok_new(line);
1258                 if (g_slist_length(tokens) < 1) {
1259                         msg("invalid message");
1260                         goto OUT;
1261                 }
1262
1263                 if (!(pResp = g_slist_nth_data(tokens, 0))) {
1264                         dbg("No  STAT in +CGREG");
1265                         goto OUT;
1266                 } else {
1267                         stat = atoi(pResp);
1268                         if ((pResp = g_slist_nth_data(tokens, 1)))
1269                                 lac = atoi(pResp);
1270
1271                         if ((pResp = g_slist_nth_data(tokens, 2)))
1272                                 ci = atoi(pResp);
1273                         else
1274                                 dbg("No ci in +CGREG");
1275
1276                         if ((pResp = g_slist_nth_data(tokens, 3)))
1277                                 AcT = atoi(pResp);
1278                         else
1279                                 dbg("No AcT in +CGREG");
1280
1281                         if ((pResp = g_slist_nth_data(tokens, 4)))
1282                                 rac = atoi(pResp);
1283                         else
1284                                 dbg("No rac in +CGREG");
1285                 }
1286
1287
1288                 dbg("stat=%d, lac=0x%lx, ci=0x%lx, Act=%d, rac = 0x%x", stat, lac, ci, AcT, rac);
1289
1290                 ps_status = lookup_tbl_net_status[stat];
1291
1292                 tcore_network_set_service_status(o, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_PACKET, ps_status);
1293                 _ps_set(tcore_object_ref_plugin(o), ps_status);
1294
1295                 tcore_network_get_service_status(o, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_CIRCUIT, &cs_status);
1296
1297                 act = lookup_tbl_access_technology[AcT];
1298                 tcore_network_set_access_technology(o, act);
1299
1300                 if (stat == AT_CREG_STAT_REG_ROAM)
1301                         tcore_network_set_roaming_state(o, TRUE);
1302                 else
1303                         tcore_network_set_roaming_state(o, FALSE);
1304
1305                 tcore_network_get_service_type(o, &service_type);
1306                 dbg("prev_service_type = 0x%x", service_type);
1307                 service_type = _get_service_type(service_type, svc_domain, act, cs_status, ps_status);
1308                 dbg("new_service_type = 0x%x", service_type);
1309                 tcore_network_set_service_type(o, service_type);
1310
1311                 tcore_network_set_lac(o, lac);
1312                 tcore_network_set_cell_id(o, ci);
1313                 tcore_network_set_rac(o, rac);
1314
1315                 net_lac_cell_info.lac = lac;
1316                 net_lac_cell_info.cell_id = ci;
1317
1318                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_LOCATION_CELLINFO,
1319                                                                            sizeof(struct tnoti_network_location_cellinfo), &net_lac_cell_info);
1320
1321                 regist_status.cs_domain_status = cs_status;
1322                 regist_status.ps_domain_status = ps_status;
1323                 regist_status.service_type = service_type;
1324                 regist_status.roaming_status = tcore_network_get_roaming_state(o);
1325
1326                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o,
1327                                                                            TNOTI_NETWORK_REGISTRATION_STATUS, sizeof(regist_status), &regist_status);
1328 #if 0
1329                 if (service_type == NETWORK_SERVICE_TYPE_HSDPA)
1330                         noti.status = TELEPHONY_HSDPA_ON;
1331                 else
1332                         noti.status = TELEPHONY_HSDPA_OFF;
1333 #else
1334                 switch(AcT){
1335                         case AT_COPS_ACT_GSM:/*Fall Through*/
1336                         case AT_COPS_ACT_GSM_COMPACT:/*Fall Through*/
1337                         case AT_COPS_ACT_UTRAN:/*Fall Through*/
1338                         case AT_COPS_ACT_GSM_EGPRS:/*Fall Through*/
1339                         case AT_COPS_ACT_E_UTRAN:
1340                         {
1341                                 dbg("Not required for Protocol Status Notification");
1342                                 goto OUT;
1343                         }
1344                         case AT_COPS_ACT_UTRAN_HSDPA:
1345                         {
1346                                 dbg("HSDPA");
1347                                 noti.status = TELEPHONY_HSDPA_ON;
1348                                 break;
1349                         }
1350                         case AT_COPS_ACT_UTRAN_HSUPA:
1351                         {
1352                                 dbg("HSUPA");
1353                                 noti.status = TELEPHONY_HSUPA_ON;
1354                                 break;
1355                         }
1356                         case AT_COPS_ACT_UTRAN_HSDPA_HSUPA:
1357                         {
1358                                 dbg("HSPA");
1359                                 noti.status = TELEPHONY_HSPA_ON;
1360                                 break;
1361                         }
1362                         default:
1363                         {
1364                                 dbg("Ignore");
1365                                 goto OUT;
1366                         }
1367                 }
1368 #endif
1369                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_PS_PROTOCOL_STATUS,
1370                                                                            sizeof(struct tnoti_ps_protocol_status), &noti);
1371
1372                 /* Get PLMN ID needed to application */
1373                 // get_serving_network(o, NULL);
1374         } else {
1375                 dbg("Response NOK");
1376         }
1377
1378 OUT:
1379         if (NULL != tokens)
1380                 tcore_at_tok_free(tokens);
1381         return TRUE;
1382 }
1383
1384 static gboolean on_event_cs_network_regist(CoreObject *o, const void *event_info, void *user_data)
1385 {
1386         GSList *lines = NULL;
1387         char *line = NULL;
1388         struct tnoti_network_registration_status regist_status;
1389         enum telephony_network_service_domain_status cs_status;
1390         enum telephony_network_service_domain_status ps_status;
1391         enum telephony_network_service_type service_type;
1392         enum telephony_network_access_technology act = NETWORK_ACT_UNKNOWN;
1393         struct tnoti_network_location_cellinfo net_lac_cell_info = {0};
1394
1395
1396         unsigned char svc_domain = NETWORK_SERVICE_DOMAIN_CS;
1397         int stat = 0, AcT = 0;
1398         unsigned int lac = 0xffff, ci = 0xffff;
1399         GSList *tokens = NULL;
1400         char *pResp;
1401
1402         lines = (GSList *) event_info;
1403         if (1 != g_slist_length(lines)) {
1404                 dbg("unsolicited msg but multiple line");
1405                 goto OUT;
1406         }
1407         line = (char *) (lines->data);
1408
1409         dbg("+CREG NOTI RECEIVED");
1410
1411 /*
1412 +CREG: <stat> [[,<lac>,<ci>[AcT]]
1413
1414 Possible values of <stat> can be
1415 0 Not registered, ME is not currently searching a new operator to register to
1416 1 Registered, home network
1417 2 Not registered, but ME is currently searching a new operator to register
1418 3 Registration denied
1419 4 Unknown
1420 5 Registered, in roaming
1421
1422 <lac>
1423 string type; two byte location area code in hexadecimal format (e.g. 00C3)
1424
1425 <ci>
1426 string type; four byte cell ID in hexadecimal format (e.g. 0000A13F)
1427
1428 <ACT>
1429 0 GSM
1430 2 UTRAN
1431 3 GSM w/EGPRS
1432 4 UTRAN w/HSDPA
1433 5 UTRAN w/HSUPA
1434 6 UTRAN w/HSDPA and HSUPA
1435 Note: <Act> is supporting from R7 and above Protocol Stack.
1436 */
1437         if (line != NULL) {
1438                 tokens = tcore_at_tok_new(line);
1439                 if (g_slist_length(tokens) < 1) {
1440                         msg("invalid message");
1441                         goto OUT;
1442                 }
1443
1444                 if (!(pResp = g_slist_nth_data(tokens, 0))) {
1445                         dbg("No  STAT in +CREG");
1446                         goto OUT;
1447                 } else {
1448                         stat = atoi(pResp);
1449                         if ((pResp = g_slist_nth_data(tokens, 1)))
1450                                 lac = atoi(pResp);
1451
1452                         if ((pResp = g_slist_nth_data(tokens, 2)))
1453                                 ci = atoi(pResp);
1454                         else
1455                                 dbg("No ci in +CREG");
1456
1457                         if ((pResp = g_slist_nth_data(tokens, 3)))
1458                                 AcT = atoi(pResp);
1459                         else
1460                                 dbg("No AcT in +CREG");
1461                 }
1462
1463
1464                 dbg("stat=%d, lac=0x%lx, ci=0x%lx, Act=%d", stat, lac, ci, AcT);
1465
1466                 cs_status = lookup_tbl_net_status[stat];
1467                 tcore_network_set_service_status(o, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_CIRCUIT, cs_status);
1468
1469                 // tcore_network_get_service_status(o, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_CIRCUIT, &cs_status);
1470                 tcore_network_get_service_status(o, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_PACKET, &ps_status);
1471
1472                 act = lookup_tbl_access_technology[AcT];
1473                 tcore_network_set_access_technology(o, act);
1474
1475                 if (stat == AT_CREG_STAT_REG_ROAM)
1476                         tcore_network_set_roaming_state(o, TRUE);
1477                 else
1478                         tcore_network_set_roaming_state(o, FALSE);
1479
1480                 tcore_network_get_service_type(o, &service_type);
1481                 dbg("prev_service_type = 0x%x", service_type);
1482                 service_type = _get_service_type(service_type, svc_domain, act, cs_status, ps_status);
1483                 dbg("new_service_type = 0x%x", service_type);
1484                 tcore_network_set_service_type(o, service_type);
1485
1486                 tcore_network_set_lac(o, lac);
1487                 tcore_network_set_cell_id(o, ci);
1488
1489                 net_lac_cell_info.lac = lac;
1490                 net_lac_cell_info.cell_id = ci;
1491
1492                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_LOCATION_CELLINFO,
1493                                                                            sizeof(struct tnoti_network_location_cellinfo), &net_lac_cell_info);
1494
1495                 regist_status.cs_domain_status = cs_status;
1496                 regist_status.ps_domain_status = ps_status;
1497                 regist_status.service_type = service_type;
1498                 regist_status.roaming_status = tcore_network_get_roaming_state(o);
1499
1500                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o,
1501                                                                            TNOTI_NETWORK_REGISTRATION_STATUS, sizeof(struct tnoti_network_registration_status), &regist_status);
1502
1503                 /* Get PLMN ID needed to application */
1504                 if ((NETWORK_SERVICE_DOMAIN_STATUS_FULL == cs_status) ||
1505                         NETWORK_SERVICE_DOMAIN_STATUS_FULL == ps_status)
1506                         get_serving_network(o, NULL);
1507         } else {
1508                 dbg("Response NOK");
1509         }
1510
1511 OUT:
1512         if (NULL != tokens)
1513                 tcore_at_tok_free(tokens);
1514         return TRUE;
1515 }
1516
1517 static gboolean on_event_network_icon_info(CoreObject *o, const void *event_info, void *user_data)
1518 {
1519         struct tnoti_network_icon_info net_icon_info = {0};
1520         char *line = NULL;
1521         char *rssiToken = NULL;
1522         char *batteryToken = NULL;
1523         GSList *tokens = NULL;
1524         GSList *lines = NULL;
1525
1526         lines = (GSList *) event_info;
1527         if (1 != g_slist_length(lines)) {
1528                 dbg("unsolicited msg but multiple line");
1529                 goto OUT;
1530         }
1531         line = (char *) (lines->data);
1532         dbg("+XCIEV Network Icon Info Noti Recieve");
1533         memset(&net_icon_info, 0, sizeof(struct tnoti_network_icon_info));
1534
1535         if (line != NULL) {
1536                 dbg("Response OK");
1537
1538                 tokens = tcore_at_tok_new(line);
1539                 if (g_slist_length(tokens) != 2) {
1540                         msg("invalid message");
1541                         goto OUT;
1542                 }
1543
1544                 rssiToken = (char *) g_slist_nth_data(tokens, 0);
1545
1546                 if (strlen(rssiToken) > 0) {
1547                         net_icon_info.type = NETWORK_ICON_INFO_RSSI;
1548                         net_icon_info.rssi = atoi(g_slist_nth_data(tokens, 0));
1549                         dbg("rssi level : %d", net_icon_info.rssi);
1550                 }
1551
1552                 batteryToken = (char *) g_slist_nth_data(tokens, 1);
1553                 if (strlen(batteryToken) > 0) {
1554                         net_icon_info.type = NETWORK_ICON_INFO_BATTERY;
1555                         net_icon_info.battery = atoi(g_slist_nth_data(tokens, 1));
1556                         dbg("battery level : %d", net_icon_info.battery);
1557                 }
1558
1559                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_ICON_INFO,
1560                                                                            sizeof(struct tnoti_network_icon_info), &net_icon_info);
1561         } else {
1562                 dbg("Response NOK");
1563         }
1564
1565
1566 OUT:
1567         if (NULL != tokens)
1568                 tcore_at_tok_free(tokens);
1569
1570         return TRUE;
1571 }
1572
1573 static gboolean on_event_network_ctzv_time_info(CoreObject *o, const void *event_info, void *user_data)
1574 {
1575         struct tnoti_network_timeinfo net_time_info = {0};
1576         char *line = NULL;
1577         GSList *tokens = NULL;
1578         char *time = NULL;
1579         char *time_zone = NULL;
1580         GSList *lines = NULL;
1581         char ptime_param[20] = {0};
1582         UserRequest *ur = NULL;
1583         dbg("Enter : on_event_network_ctzv_time_info");
1584
1585         lines = (GSList *) event_info;
1586         if (1 != g_slist_length(lines)) {
1587                 dbg("unsolicited msg but multiple line");
1588                 goto OUT;
1589         }
1590         line = (char *) (lines->data);
1591
1592 /*
1593 +CTZV: <tz>,<time>
1594 <tz> integer value indicating the time zone (e.g. -22 or +34)
1595 <time> string type value; format is yy/MM/dd,hh:mms, wherein characters indicates year, month, day, hour,
1596 minutes, seconds.*/
1597
1598         dbg("Network time info (+CTZV) recieved");
1599
1600         if (line != NULL) {
1601                 dbg("Response OK");
1602                 dbg("noti line is %s", line);
1603
1604                 tokens = tcore_at_tok_new(line);
1605
1606                 if (g_slist_length(tokens) < 2) {
1607                         msg("invalid message");
1608                         goto OUT;
1609                 }
1610
1611                 if ((time_zone = g_slist_nth_data(tokens, 0))) {
1612                         net_time_info.gmtoff = atoi(time_zone) * 15; /* TZ in minutes */
1613                 }
1614
1615                 if (tcore_network_get_plmn(o) != NULL)
1616                         strcpy(net_time_info.plmn, tcore_network_get_plmn(o));
1617
1618                 if ((time = g_slist_nth_data(tokens, 1)) && (strlen(time) > 18)) {
1619                         strncpy(ptime_param, time + 1, 2); /* skip past initial quote (") */
1620                         net_time_info.year = atoi(ptime_param);
1621
1622                         strncpy(ptime_param, time + 4, 2); /* skip slash (/) after year param */
1623                         net_time_info.month = atoi(ptime_param);
1624
1625                         strncpy(ptime_param, time + 7, 2); /* skip past slash (/) after month param */
1626                         net_time_info.day = atoi(ptime_param);
1627
1628                         strncpy(ptime_param, time + 10, 2); /* skip past comma (,) after day param */
1629                         net_time_info.hour = atoi(ptime_param);
1630
1631                         strncpy(ptime_param, time + 13, 2); /* skip past colon (:) after hour param */
1632                         net_time_info.minute = atoi(ptime_param);
1633
1634                         strncpy(ptime_param, time + 16, 2); /* skip past colon (:) after minute param */
1635                         net_time_info.second = atoi(ptime_param);
1636                 }
1637                 tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_NETWORK_TIMEINFO, sizeof(struct tnoti_network_timeinfo), &net_time_info);
1638
1639                 dbg("new pending(AT+XOPS=0/5/6 for Nitz PLMN name)");
1640
1641                 /* Get NITZ name and plmn_id via AT+XCOPS = 0/5/6 */
1642                 nwk_prepare_and_send_pending_request(o, "AT+XCOPS=0;+XCOPS=5;+XCOPS=6", "+XCOPS", TCORE_AT_MULTILINE, ur, on_response_get_nitz_name);
1643         } else {
1644                 dbg("line is  NULL");
1645         }
1646
1647 OUT:
1648         if (NULL != tokens)
1649                 tcore_at_tok_free(tokens);
1650
1651         dbg("Exit: on_event_network_ctzv_time_info");
1652         return TRUE;
1653 }
1654
1655 static void on_sim_resp_hook_get_netname(UserRequest *ur, enum tcore_response_command command, unsigned int data_len,
1656                                                                                  const void *data, void *user_data)
1657 {
1658         const struct tresp_sim_read *resp = data;
1659         CoreObject *o = user_data;
1660
1661         if (command == TRESP_SIM_GET_SPN) {
1662                 dbg("OK SPN GETTING!!");
1663                 dbg("resp->result = 0x%x", resp->result);
1664                 dbg("resp->data.spn.display_condition = 0x%x", resp->data.spn.display_condition);
1665                 dbg("resp->data.spn.spn = [%s]", resp->data.spn.spn);
1666
1667                 tcore_network_set_network_name(o, TCORE_NETWORK_NAME_TYPE_SPN, (const char *) resp->data.spn.spn);
1668
1669                 /**
1670                  * display condition
1671                  *  bit[0]: 0 = display of registered PLMN name not required when registered PLMN is either HPLMN or a PLMN in the service provider PLMN list
1672                  *          1 = display of registered PLMN name required when registered PLMN is either HPLMN or a PLMN in the service provider PLMN list
1673                  *  bit[1]: 0 = display of the service provider name is required when registered PLMN is neither HPLMN nor a PLMN in the service provider PLMN list
1674                  *          1 = display of the service provider name is not required when registered PLMN is neither HPLMN nor a PLMN in the service provider PLMN list
1675                  */
1676                 if (resp->data.spn.display_condition & 0x01) {
1677                         tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_NETWORK);
1678                 }
1679                 if ((resp->data.spn.display_condition & 0x02) == 0) {
1680                         tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_SPN);
1681                 }
1682                 if ((resp->data.spn.display_condition & 0x03) == 0x01) {
1683                         tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_ANY);
1684                 }
1685
1686                 // fallback in case no SPN name is provided
1687                 if (resp->data.spn.spn[0] == '\0')
1688                         tcore_network_set_network_name_priority(o, TCORE_NETWORK_NAME_PRIORITY_NETWORK);
1689         }
1690 }
1691
1692 static enum tcore_hook_return on_hook_sim_init(Server *s, CoreObject *source, enum tcore_notification_command command,
1693                                                                                            unsigned int data_len, void *data, void *user_data)
1694 {
1695         const struct tnoti_sim_status *sim = data;
1696         UserRequest *ur = NULL;
1697
1698         if (sim->sim_status == SIM_STATUS_INIT_COMPLETED) {
1699                 ur = tcore_user_request_new(NULL, NULL);
1700                 tcore_user_request_set_command(ur, TREQ_SIM_GET_SPN);
1701                 tcore_user_request_set_response_hook(ur, on_sim_resp_hook_get_netname, user_data);
1702                 tcore_object_dispatch_request(source, ur);
1703         }
1704
1705         return TCORE_HOOK_RETURN_CONTINUE;
1706 }
1707
1708 static TReturn search_network(CoreObject *o, UserRequest *ur)
1709 {
1710         TcoreHal *h = NULL;
1711         TcorePending *pending = NULL;
1712         TcoreATRequest *atreq = NULL;
1713         char *cmd_str = NULL;
1714         dbg("search_network - ENTER!!");
1715
1716         if (!o || !ur)
1717                 return TCORE_RETURN_EINVAL;
1718
1719         h = tcore_object_get_hal(o);
1720         if(FALSE == tcore_hal_get_power_state(h)){
1721                 dbg("cp not ready/n");
1722                 return TCORE_RETURN_ENOSYS;
1723         }
1724
1725         pending = tcore_pending_new(o, 0);
1726
1727         cmd_str = g_strdup_printf("AT+COPS=?");
1728         atreq = tcore_at_request_new(cmd_str, "+COPS", TCORE_AT_SINGLELINE);
1729
1730         tcore_pending_set_request_data(pending, 0, atreq);
1731         tcore_pending_set_timeout(pending, 60);
1732         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
1733         tcore_pending_set_response_callback(pending, on_response_search_network, NULL);
1734         tcore_pending_set_timeout_callback(pending, on_timeout_search_network, NULL);
1735         tcore_pending_link_user_request(pending, ur);
1736         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
1737
1738         tcore_hal_send_request(h, pending);
1739         g_free(cmd_str);
1740         return TCORE_RETURN_SUCCESS;
1741 }
1742
1743 static TReturn set_plmn_selection_mode(CoreObject *o, UserRequest *ur)
1744 {
1745         TcoreHal *h = NULL;
1746         TcorePending *pending = NULL;
1747         TcoreATRequest *atreq;
1748         char *cmd_str = NULL;
1749         int format = 0; /* default value for long alphanumeric */
1750         int mode = 0;
1751         char plmn[7] = {0};
1752         int act = 0;
1753
1754         const struct treq_network_set_plmn_selection_mode *req_data = NULL;
1755
1756
1757         dbg("set_plmn_selection_mode - ENTER!!");
1758
1759         if (!o || !ur)
1760                 return TCORE_RETURN_EINVAL;
1761
1762         req_data = tcore_user_request_ref_data(ur, NULL);
1763         h = tcore_object_get_hal(o);
1764         if(FALSE == tcore_hal_get_power_state(h)){
1765                 dbg("cp not ready/n");
1766                 return TCORE_RETURN_ENOSYS;
1767         }
1768         pending = tcore_pending_new(o, 0);
1769
1770         // Command Format - AT+COPS=[<mode>[,<format>[,<oper>[,< AcT>]]]]
1771         /* oper parameter format
1772             - 0 <oper> format presentations are set to long alphanumeric. If Network name not available it displays combination of Mcc and MNC in string format.
1773             - 1 <oper> format presentation is set to short alphanumeric.
1774             - 2 <oper> format presentations set to numeric.
1775         */
1776
1777         if ((req_data->act == NETWORK_ACT_GSM) || (req_data->act == NETWORK_ACT_EGPRS))
1778                 act = 0;
1779         else
1780                 act = 2;
1781
1782         switch (req_data->mode) {
1783         case NETWORK_SELECT_MODE_MANUAL:
1784         {
1785                 mode = AT_COPS_MODE_MANUAL;
1786                 format = AT_COPS_FORMAT_NUMERIC;
1787
1788                 memset(plmn, 0, 7);
1789                 memcpy(plmn, req_data->plmn, 6);
1790
1791                 if (strlen(req_data->plmn) == 6) {
1792                         if (plmn[5] == '#')
1793                                 plmn[5] = 0;
1794                 }
1795
1796                 cmd_str = g_strdup_printf("AT+COPS=%d,%d,\"%s\",%d", mode, format, plmn, act);
1797         }
1798         break;
1799
1800         case NETWORK_SELECT_MODE_AUTOMATIC:
1801         default:
1802                 cmd_str = g_strdup("AT+COPS=0");
1803                 break;
1804         }
1805
1806
1807         atreq = tcore_at_request_new(cmd_str, "+COPS", TCORE_AT_NO_RESULT);
1808
1809         tcore_pending_set_request_data(pending, 0, atreq);
1810         tcore_pending_set_response_callback(pending, on_response_set_plmn_selection_mode, NULL);
1811         tcore_pending_link_user_request(pending, ur);
1812         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
1813
1814         tcore_hal_send_request(h, pending);
1815         g_free(cmd_str);
1816         return TCORE_RETURN_SUCCESS;
1817 }
1818
1819 static TReturn get_plmn_selection_mode(CoreObject *o, UserRequest *ur)
1820 {
1821         TcoreHal *h = NULL;
1822         TcorePending *pending = NULL;
1823         TcoreATRequest *atreq;
1824         char *cmd_str = NULL;
1825
1826         dbg("get_plmn_selection_mode - ENTER!!");
1827
1828         if (!o || !ur)
1829                 return TCORE_RETURN_EINVAL;
1830
1831         h = tcore_object_get_hal(o);
1832         if(FALSE == tcore_hal_get_power_state(h)){
1833                 dbg("cp not ready/n");
1834                 return TCORE_RETURN_ENOSYS;
1835         }
1836         pending = tcore_pending_new(o, 0);
1837
1838         cmd_str = g_strdup_printf("AT+COPS?");
1839         atreq = tcore_at_request_new(cmd_str, "+COPS", TCORE_AT_SINGLELINE);
1840
1841         tcore_pending_set_request_data(pending, 0, atreq);
1842         tcore_pending_set_response_callback(pending, on_response_get_plmn_selection_mode, NULL);
1843         tcore_pending_link_user_request(pending, ur);
1844         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
1845
1846         tcore_hal_send_request(h, pending);
1847         g_free(cmd_str);
1848         return TCORE_RETURN_SUCCESS;
1849 }
1850
1851
1852 static TReturn set_band(CoreObject *o, UserRequest *ur)
1853 {
1854         TcoreHal *h = NULL;
1855         TcorePending *pending = NULL;
1856         TcorePending *pending_gsm = NULL;
1857         TcorePending *pending_umts = NULL;
1858         TcoreATRequest *atreq;
1859         char *cmd_str = NULL;
1860         const struct treq_network_set_band *req_data;
1861         gboolean set_gsm_band = 0;
1862         gboolean set_umts_band = 0;
1863         int gsm_band = 255;
1864         int gsm_band2 = 255;
1865         char *umts_band = NULL;
1866         UserRequest *dup_ur_gsm = NULL;
1867         UserRequest *dup_ur_umts = NULL;
1868
1869         dbg("set_band - ENTER!!");
1870
1871         if (!o || !ur)
1872                 return TCORE_RETURN_EINVAL;
1873
1874         req_data = tcore_user_request_ref_data(ur, NULL);
1875         h = tcore_object_get_hal(o);
1876         if(FALSE == tcore_hal_get_power_state(h)){
1877                 dbg("cp not ready/n");
1878                 return TCORE_RETURN_ENOSYS;
1879         }
1880
1881         dbg("set_band - called with band = %d", req_data->band);
1882
1883         switch (req_data->band) {
1884         case NETWORK_BAND_TYPE_GSM850:
1885                 gsm_band = AT_GSM_XBANDSEL_850;
1886                 set_gsm_band = TRUE;
1887                 break;
1888
1889         case NETWORK_BAND_TYPE_GSM_900_1800:
1890                 gsm_band = AT_GSM_XBANDSEL_900;
1891                 gsm_band2 = AT_GSM_XBANDSEL_1800;
1892                 set_gsm_band = TRUE;
1893                 break;
1894
1895         case NETWORK_BAND_TYPE_GSM1900:
1896                 gsm_band = AT_GSM_XBANDSEL_1900;
1897                 set_gsm_band = TRUE;
1898                 break;
1899
1900         case NETWORK_BAND_TYPE_GSM1800:
1901                 gsm_band = AT_GSM_XBANDSEL_1800;
1902                 set_gsm_band = TRUE;
1903                 break;
1904
1905         case NETWORK_BAND_TYPE_GSM_850_1900:
1906                 gsm_band = AT_GSM_XBANDSEL_850;
1907                 gsm_band2 = AT_GSM_XBANDSEL_1900;
1908                 set_gsm_band = TRUE;
1909                 break;
1910
1911         case NETWORK_BAND_TYPE_ANY:
1912                 gsm_band = AT_GSM_XBANDSEL_AUTOMATIC;
1913                 set_umts_band = TRUE;
1914                 set_gsm_band = TRUE;
1915                 break;
1916
1917         case NETWORK_BAND_TYPE_WCDMA:
1918                 set_umts_band = TRUE;
1919                 break;
1920
1921         case NETWORK_BAND_TYPE_WCDMA2100:
1922                 umts_band = "UMTS_BAND_I";
1923                 set_umts_band = TRUE;
1924                 break;
1925
1926         case NETWORK_BAND_TYPE_WCDMA1900:
1927                 umts_band = "UMTS_BAND_II";
1928                 set_umts_band = TRUE;
1929                 break;
1930
1931         case NETWORK_BAND_TYPE_WCDMA850:
1932                 umts_band = "UMTS_BAND_V";
1933                 set_umts_band = TRUE;
1934                 break;
1935
1936         default:
1937                 break;
1938         }
1939
1940         dbg("set_band > set_umts_band = %d, set_gsm_band = %d", set_umts_band, set_gsm_band);
1941
1942         if (set_umts_band == TRUE) {
1943                 if ((req_data->band == NETWORK_BAND_TYPE_WCDMA) || (req_data->band == NETWORK_BAND_TYPE_ANY))
1944                         cmd_str = g_strdup_printf("AT+XUBANDSEL=0");
1945                 else
1946                         cmd_str = g_strdup_printf("AT+XUBANDSEL=%s", umts_band);
1947
1948                 atreq = tcore_at_request_new(cmd_str, "+XUBANDSEL", TCORE_AT_NO_RESULT);
1949                 pending_umts = tcore_pending_new(o, 0);
1950
1951                 tcore_pending_set_request_data(pending_umts, 0, atreq);
1952                 tcore_pending_set_priority(pending_umts, TCORE_PENDING_PRIORITY_DEFAULT);
1953                 tcore_pending_set_response_callback(pending_umts, on_response_set_umts_band, NULL);
1954
1955                 /* duplicate user request for UMTS Band setting AT command for same UR */
1956                 dup_ur_umts = tcore_user_request_ref(ur);
1957                 tcore_pending_link_user_request(pending_umts, dup_ur_umts);
1958                 tcore_pending_set_send_callback(pending_umts, on_confirmation_network_message_send, NULL);
1959
1960                 tcore_hal_send_request(h, pending_umts);
1961                 g_free(cmd_str);
1962         }
1963
1964         if (set_gsm_band == TRUE) {
1965                 dbg("Entered set_gsm_band");
1966                 if (gsm_band2 == 255)
1967                         cmd_str = g_strdup_printf("AT+XBANDSEL=%d", gsm_band);
1968                 else
1969                         cmd_str = g_strdup_printf("AT+XBANDSEL=%d,%d", gsm_band, gsm_band2);
1970
1971                 dbg("Command string: %s", cmd_str);
1972                 atreq = tcore_at_request_new(cmd_str, "+XBANDSEL", TCORE_AT_NO_RESULT);
1973                 pending_gsm = tcore_pending_new(o, 0);
1974
1975                 tcore_pending_set_request_data(pending_gsm, 0, atreq);
1976                 tcore_pending_set_priority(pending_gsm, TCORE_PENDING_PRIORITY_DEFAULT);
1977                 tcore_pending_set_response_callback(pending_gsm, on_response_set_gsm_band, NULL);
1978
1979                 /* duplicate user request for GSM Band setting AT command for same UR */
1980                 dup_ur_gsm = tcore_user_request_ref(ur);
1981                 tcore_pending_link_user_request(pending_gsm, dup_ur_gsm);
1982                 tcore_pending_set_send_callback(pending_gsm, on_confirmation_network_message_send, NULL);
1983
1984                 tcore_hal_send_request(h, pending_gsm);
1985                 g_free(cmd_str);
1986         }
1987
1988         /* Lock device to specific RAT as requested by application */
1989 /*
1990 AT+XRAT=<Act>[,<PreferredAct>]
1991 <AcT> indicates the radio access technology and may be
1992 0 GSM single mode
1993 1 GSM / UMTS Dual mode
1994 2 UTRAN (UMTS)
1995 */
1996         if ((set_umts_band == TRUE) && (set_gsm_band == TRUE)) {
1997                 cmd_str = g_strdup_printf("AT+XRAT=%d", AT_XRAT_DUAL);
1998         } else if (set_umts_band == TRUE) {
1999                 cmd_str = g_strdup_printf("AT+XRAT=%d", AT_XRAT_UMTS);
2000         } else {
2001                 cmd_str = g_strdup_printf("AT+XRAT=%d", AT_XRAT_GSM);
2002         }
2003         atreq = tcore_at_request_new(cmd_str, "+XRAT", TCORE_AT_NO_RESULT);
2004         pending = tcore_pending_new(o, 0);
2005
2006         tcore_pending_set_request_data(pending, 0, atreq);
2007         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);
2008         tcore_pending_set_response_callback(pending, on_response_set_xrat, NULL);
2009         tcore_pending_link_user_request(pending, ur);
2010         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
2011
2012         tcore_hal_send_request(h, pending);
2013         g_free(cmd_str);
2014         return TCORE_RETURN_SUCCESS;
2015 }
2016
2017 static TReturn get_band(CoreObject *o, UserRequest *ur)
2018 {
2019         TcoreHal *h = NULL;
2020         TcorePending *pending = NULL;
2021         TcoreATRequest *atreq;
2022         char *cmd_str = NULL;
2023         dbg("get_band - ENTER!!");
2024
2025         if (!o || !ur)
2026                 return TCORE_RETURN_EINVAL;
2027
2028         h = tcore_object_get_hal(o);
2029         if(FALSE == tcore_hal_get_power_state(h)){
2030                 dbg("cp not ready/n");
2031                 return TCORE_RETURN_ENOSYS;
2032         }
2033
2034         /* Get RAT Information Information. Based on RAT read response, we will get specific RAT bands only */
2035         cmd_str = g_strdup_printf("AT+XRAT?");
2036         atreq = tcore_at_request_new(cmd_str, "+XRAT", TCORE_AT_SINGLELINE);
2037         pending = tcore_pending_new(o, 0);
2038         tcore_pending_set_request_data(pending, 0, atreq);
2039         tcore_pending_set_response_callback(pending, on_response_get_xrat, NULL);
2040         tcore_pending_link_user_request(pending, ur);
2041         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
2042         tcore_hal_send_request(h, pending);
2043
2044         g_free(cmd_str);
2045         return TCORE_RETURN_SUCCESS;
2046 }
2047
2048 static TReturn set_preferred_plmn(CoreObject *o, UserRequest *ur)
2049 {
2050         TcoreHal *h = NULL;
2051         TcorePlugin *p = NULL;
2052         TcorePending *pending = NULL;
2053         TcoreATRequest *atreq = NULL;
2054         struct treq_network_set_preferred_plmn *req_data = NULL;
2055         char *cmd_str = NULL;
2056         int format = 2; /* Alway use numeric format, as application gives data in this default format */
2057         int gsm_act = 0;
2058         int gsm_compact_act = 0;
2059         int utran_act = 0;
2060
2061         if (!o || !ur)
2062                 return TCORE_RETURN_EINVAL;
2063
2064         p = tcore_object_ref_plugin(o);
2065         h = tcore_object_get_hal(o);
2066         if(FALSE == tcore_hal_get_power_state(h)){
2067                 dbg("cp not ready/n");
2068                 return TCORE_RETURN_ENOSYS;
2069         }
2070
2071         req_data = (struct treq_network_set_preferred_plmn *) tcore_user_request_ref_data(ur, NULL);
2072         pending = tcore_pending_new(o, 0);
2073
2074         dbg("Entry set_preferred_plmn");
2075 /*
2076 AT+CPOL=
2077 [<index>][,<format>[,<oper>[,<GSM_AcT>,
2078 <GSM_Compact_AcT>,<UTRAN_AcT>]]]
2079  */
2080
2081         if ((req_data->act == NETWORK_ACT_GSM) || (req_data->act == NETWORK_ACT_GPRS) || (req_data->act == NETWORK_ACT_EGPRS))
2082                 gsm_act = TRUE;
2083         else if ((req_data->act == NETWORK_ACT_UMTS) || (req_data->act == NETWORK_ACT_UTRAN))
2084                 utran_act = TRUE;
2085         else if (req_data->act == NETWORK_ACT_GSM_UTRAN)
2086                 gsm_act = utran_act = TRUE;
2087
2088         if (strlen(req_data->plmn) > 6) {
2089                 req_data->plmn[6] = '\0';
2090         } else if (strlen(req_data->plmn) == 6) {
2091                 if (req_data->plmn[5] == '#') {
2092                         req_data->plmn[5] = '\0';
2093                 }
2094         }
2095         cmd_str = g_strdup_printf("AT+CPOL=%d,%d,\"%s\",%d,%d,%d", req_data->index + 1, format, req_data->plmn, gsm_act, gsm_compact_act, utran_act);
2096
2097         dbg("cmd_str - %s", cmd_str);
2098         atreq = tcore_at_request_new(cmd_str, "+CPOL", TCORE_AT_NO_RESULT);
2099
2100         tcore_pending_set_request_data(pending, 0, atreq);
2101         tcore_pending_set_response_callback(pending, on_response_set_preferred_plmn, NULL);
2102         tcore_pending_link_user_request(pending, ur);
2103         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
2104
2105         tcore_hal_send_request(h, pending);
2106
2107         g_free(cmd_str);
2108
2109         dbg("Exit set_preferred_plmn");
2110
2111         return TCORE_RETURN_SUCCESS;
2112 }
2113
2114 static TReturn get_preferred_plmn(CoreObject *o, UserRequest *ur)
2115 {
2116         TcoreHal *h = NULL;
2117         TcorePending *pending = NULL;
2118         TcoreATRequest *atreq = NULL;
2119
2120         char *cmd_str = NULL;
2121
2122         dbg("get_preferred_plmn - ENTER!!");
2123
2124         if (!o || !ur)
2125                 return TCORE_RETURN_EINVAL;
2126
2127         h = tcore_object_get_hal(o);
2128         if(FALSE == tcore_hal_get_power_state(h)){
2129                 dbg("cp not ready/n");
2130                 return TCORE_RETURN_ENOSYS;
2131         }
2132
2133         pending = tcore_pending_new(o, 0);
2134
2135         cmd_str = g_strdup_printf("AT+CPOL=,2;+CPOL?");
2136         atreq = tcore_at_request_new(cmd_str, "+CPOL", TCORE_AT_MULTILINE);
2137
2138         tcore_pending_set_request_data(pending, 0, atreq);
2139         tcore_pending_set_response_callback(pending, on_response_get_preferred_plmn, NULL);
2140         tcore_pending_link_user_request(pending, ur);
2141         tcore_pending_set_send_callback(pending, on_confirmation_network_message_send, NULL);
2142
2143         tcore_hal_send_request(h, pending);
2144
2145         g_free(cmd_str);
2146
2147         dbg("get_preferred_plmn - EXIT!!");
2148
2149         return TCORE_RETURN_SUCCESS;
2150 }
2151
2152 static TReturn get_serving_network(CoreObject *o, UserRequest *ur)
2153 {
2154         dbg("get_serving_network - ENTER!!");
2155
2156         if (!o)
2157                 return TCORE_RETURN_EINVAL;
2158
2159         if(FALSE == tcore_hal_get_power_state(tcore_object_get_hal(o))){
2160                 dbg("cp not ready/n");
2161                 return TCORE_RETURN_ENOSYS;
2162         }
2163
2164         dbg("new pending(AT+COPS?)");
2165
2166         nwk_prepare_and_send_pending_request(o, "AT+COPS=3,2;+COPS?;+COPS=3,0;+COPS?", "+COPS", TCORE_AT_MULTILINE, ur, on_response_get_serving_network);
2167         return TCORE_RETURN_SUCCESS;
2168 }
2169
2170 static struct tcore_network_operations network_ops = {
2171         .search = search_network,
2172         .set_plmn_selection_mode = set_plmn_selection_mode,
2173         .get_plmn_selection_mode = get_plmn_selection_mode,
2174         .set_service_domain = NULL,
2175         .get_service_domain = NULL,
2176         .set_band = set_band,
2177         .get_band = get_band,
2178         .set_preferred_plmn = set_preferred_plmn,
2179         .get_preferred_plmn = get_preferred_plmn,
2180         .set_order = NULL,
2181         .get_order = NULL,
2182         .set_power_on_attach = NULL,
2183         .get_power_on_attach = NULL,
2184         .set_cancel_manual_search = NULL,
2185         .get_serving_network = get_serving_network,
2186 };
2187
2188 gboolean s_network_init(TcorePlugin *cp, CoreObject *co_network)
2189 {
2190         dbg("Enter");
2191
2192         tcore_network_override_ops(co_network, &network_ops);
2193
2194         tcore_object_override_callback(co_network, "+CREG", on_event_cs_network_regist, NULL);
2195         tcore_object_override_callback(co_network, "+CGREG", on_event_ps_network_regist, NULL);
2196         tcore_object_override_callback(co_network, "+XCIEV", on_event_network_icon_info, NULL);
2197
2198         /* +CTZV: <tz>,<time> */
2199         tcore_object_override_callback(co_network, "+CTZV", on_event_network_ctzv_time_info, NULL);
2200
2201         tcore_server_add_notification_hook(tcore_plugin_ref_server(cp), TNOTI_SIM_STATUS, on_hook_sim_init, co_network);
2202
2203         _insert_mcc_mnc_oper_list(cp, co_network);
2204
2205         dbg("Exit");
2206
2207         return TRUE;
2208 }
2209
2210 void s_network_exit(TcorePlugin *cp, CoreObject *co_network)
2211 {
2212         dbg("Exit");
2213 }