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