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