9d1f0520f8c8e676ae5f1f2ab938f3a7a2724c60
[profile/ivi/tel-plugin-imc.git] / src / s_modem.c
1 /*\r
2  * tel-plugin-imc\r
3  *\r
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.\r
5  *\r
6  * Contact: Hayoon Ko <hayoon.ko@samsung.com>\r
7  *\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  */\r
20 \r
21 #include <stdio.h>\r
22 #include <stdlib.h>\r
23 #include <string.h>\r
24 #include <unistd.h>\r
25 \r
26 #include <glib.h>\r
27 \r
28 #include <tcore.h>\r
29 #include <hal.h>\r
30 #include <core_object.h>\r
31 #include <plugin.h>\r
32 #include <queue.h>\r
33 #include <co_modem.h>\r
34 #include <storage.h>\r
35 #include <server.h>\r
36 #include <at.h>\r
37 #include <mux.h>\r
38 \r
39 #include "s_common.h"\r
40 #include "s_modem.h"\r
41 \r
42 \r
43 #define ID_RESERVED_AT 0x0229\r
44 \r
45 #define MAX_VERSION_LEN 32\r
46 #define TAPI_MISC_ME_SN_LEN_MAX                         32\r
47 #define TAPI_MISC_PRODUCT_CODE_LEN_MAX          32\r
48 #define TAPI_MISC_MODEL_ID_LEN_MAX                      17\r
49 #define TAPI_MISC_PRL_ERI_VER_LEN_MAX           17\r
50 \r
51 #define CPAS_RES_READY          0\r
52 #define CPAS_RES_UNAVAIL                1\r
53 #define CPAS_RES_UNKNOWN                2\r
54 #define CPAS_RES_RINGING                3\r
55 #define CPAS_RES_CALL_PROGRESS  4\r
56 #define CPAS_RES_ASLEEP           5\r
57 #define AT_VER_LEN 20\r
58 \r
59 \r
60 enum cp_state {\r
61         CP_STATE_OFFLINE,\r
62         CP_STATE_CRASH_RESET,\r
63         CP_STATE_CRASH_EXIT,\r
64         CP_STATE_BOOTING,\r
65         CP_STATE_ONLINE,\r
66         CP_STATE_NV_REBUILDING,\r
67         CP_STATE_LOADER_DONE,\r
68 };\r
69 \r
70 typedef enum {\r
71         TAPI_MISC_ME_IMEI = 0x00, /**< 0x00: IMEI, GSM/UMTS device */\r
72         TAPI_MISC_ME_ESN = 0x01, /**< 0x01: ESN(Electronic Serial Number), It`s essentially run out. CDMA device */\r
73         TAPI_MISC_ME_MEID = 0x02, /**< 0x02: MEID, This value can have hexa decimal digits. CDMA device */\r
74         TAPI_MISC_ME_MAX = 0xff /**< 0xff: reserved */\r
75 } TelMiscSNIndexType_t;\r
76 \r
77 typedef struct {\r
78         TelMiscSNIndexType_t sn_index; /**< serial number index */\r
79         int sn_len; /**< Length */\r
80         unsigned char szNumber[TAPI_MISC_ME_SN_LEN_MAX]; /**< Number */\r
81 } TelMiscSNInformation;\r
82 \r
83 /**\r
84  * Mobile Equipment Version Information\r
85  */\r
86 typedef struct {\r
87         unsigned char ver_mask; /**< version mask  - 0x01:SW_ver, 0x02:HW_ver, 0x04:RF_CAL_date, 0x08:Product_code, 0x10:Model_ID, 0x20:PRL, 0x04:ERI, 0xff:all */\r
88         unsigned char szSwVersion[MAX_VERSION_LEN]; /**< Software version, null termination */\r
89         unsigned char szHwVersion[MAX_VERSION_LEN]; /**< Hardware version, null termination */\r
90         unsigned char szRfCalDate[MAX_VERSION_LEN]; /**< Calculation Date, null termination */\r
91         unsigned char szProductCode[TAPI_MISC_PRODUCT_CODE_LEN_MAX]; /**< product code, null termination */\r
92         unsigned char szModelId[TAPI_MISC_MODEL_ID_LEN_MAX]; /**< model id (only for CDMA), null termination */\r
93         unsigned char prl_nam_num; /**< number of PRL NAM fields */\r
94         unsigned char szPrlVersion[TAPI_MISC_PRL_ERI_VER_LEN_MAX * 3];/**< prl version (only for CDMA), null termination */\r
95         unsigned char eri_nam_num; /**< number of PRL NAM fields */\r
96         unsigned char szEriVersion[TAPI_MISC_PRL_ERI_VER_LEN_MAX * 3];/**< eri version (only for CDMA), null termination */\r
97 } TelMiscVersionInformation;\r
98 \r
99         \r
100 void prepare_and_send_pending_request(TcorePlugin *plugin, char *co_name, const char *at_cmd, const char* prefix, enum tcore_at_command_type at_cmd_type, TcorePendingResponseCallback callback);\r
101 static void on_confirmation_modem_message_send(TcorePending *p, gboolean result, void *user_data); // from Kernel\r
102 void on_response_bootup_subscription(TcorePending *p, int data_len, const void *data, void *user_data);\r
103 void on_response_last_bootup_subscription(TcorePending *p, int data_len, const void *data, void *user_data);\r
104 static void on_timeout_modem_poweron(TcorePending *p, void *user_data);\r
105 static void on_response_enable_proactive_command(TcorePending *p, int data_len, const void *data, void *user_data);\r
106 \r
107 static void on_timeout_modem_poweron(TcorePending *p, void *user_data)\r
108 {\r
109         unsigned int data_len = 0;\r
110         char data[] = "AT+CPAS";                        \r
111         dbg("TIMEOUT for 1st AT Command !!!!! NO Response for initial AT command. Resending it");       \r
112         data_len = sizeof(data);\r
113         \r
114         /* Retransmit 1st AT command directly via HAL, don't disturb pending queue. */\r
115         /* HAL was passed as user_data, re-use it */\r
116         if (user_data) \r
117         {\r
118                 tcore_hal_send_data(user_data, data_len, (void *)data); \r
119         }\r
120 }\r
121 \r
122 static void on_confirmation_modem_message_send(TcorePending *p, gboolean result, void *user_data)\r
123 {\r
124         dbg("on_confirmation_modem_message_send - msg out from queue.\n");\r
125 \r
126         if (result == FALSE) {\r
127                 /* Fail */\r
128                 dbg("SEND FAIL");\r
129         }\r
130         else {\r
131                 dbg("SEND OK");\r
132         }\r
133 }\r
134 \r
135 static void on_response_enable_proactive_command(TcorePending *p, int data_len, const void *data, void *user_data)\r
136 {\r
137         const TcoreATResponse *resp = data;\r
138         \r
139         if(resp->success > 0){\r
140                 dbg("RESPONSE OK proactive command enabled");\r
141                 \r
142         }\r
143         else    {\r
144                 dbg("RESPONSE NOK proactive command disabled");\r
145         }\r
146 }\r
147 \r
148 void prepare_and_send_pending_request(TcorePlugin *plugin, char *co_name, const char *at_cmd, const char* prefix, enum tcore_at_command_type at_cmd_type, TcorePendingResponseCallback callback)\r
149 {\r
150         TcoreATRequest *req = NULL;\r
151         TcoreHal *hal = NULL;\r
152         CoreObject *o= NULL;\r
153         TcorePending *pending = NULL;\r
154         TReturn ret;\r
155         \r
156         o = tcore_plugin_ref_core_object(plugin, co_name);\r
157         hal = tcore_object_get_hal(o);\r
158         dbg("hal: %p", hal);\r
159         \r
160         pending = tcore_pending_new(o, 0);\r
161         if(!pending)\r
162                 dbg("Pending is NULL");\r
163         req = tcore_at_request_new(at_cmd, prefix, at_cmd_type);\r
164 \r
165         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
166 \r
167         tcore_pending_set_request_data(pending, 0, req);\r
168         tcore_pending_set_response_callback(pending, callback, NULL);\r
169         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
170         tcore_pending_link_user_request(pending, NULL); //set user request to NULL - this is intenal request\r
171         ret = tcore_hal_send_request(hal, pending);\r
172         return;\r
173 }\r
174 \r
175 void on_response_bootup_subscription(TcorePending *p, int data_len, const void *data, void *user_data)\r
176 {\r
177         const TcoreATResponse *resp = data;\r
178         dbg("enry of on_response_bootup_subscription() - response comes\n");\r
179 \r
180         if(resp->success){\r
181                 dbg("result OK");       \r
182         }\r
183         else{\r
184                 dbg("result ERROR");\r
185         }\r
186 }\r
187 \r
188 void on_response_last_bootup_subscription(TcorePending *p, int data_len, const void *data, void *user_data)\r
189 {\r
190         const TcoreATResponse *resp = data;\r
191         dbg("enry of on_response_last_bootup_subscription() - final response comes\n");\r
192         if(resp->success){\r
193                 dbg("SEND OK"); \r
194         }\r
195         else{\r
196                 dbg("SEND FAIL");\r
197         }\r
198         dbg("Response for AT+CLIP. Boot-up configration completed for IMC modem. Bring CP to online based on Flightmode status\n");\r
199         on_event_modem_power(NULL, NULL, tcore_pending_ref_plugin(p));\r
200 }\r
201 \r
202 static void on_response_power_off(TcorePending *p, int data_len, const void *data, void *user_data)\r
203 {\r
204         CoreObject *o = 0;\r
205         TcoreHal *h = 0;\r
206         o = tcore_pending_ref_core_object(p);\r
207         h = tcore_object_get_hal(o);\r
208 \r
209         dbg("modem power off");\r
210 \r
211         tcore_hal_set_power_state(h, FALSE);\r
212 }\r
213 \r
214 static void on_response_set_flight_mode(TcorePending *p, int data_len, const void *data, void *user_data)\r
215 {\r
216         CoreObject *o = user_data;\r
217         UserRequest *ur = NULL;\r
218         const TcoreATResponse *ATresp = data;\r
219         GSList *tokens=NULL;\r
220         const char *line = NULL;\r
221         struct tresp_modem_set_flightmode res = {0};\r
222         int response = 0;\r
223         struct tnoti_modem_flight_mode modem_flight_mode = {0};\r
224 \r
225         o = tcore_pending_ref_core_object(p);\r
226 \r
227         if(ATresp->success > 0){\r
228                 dbg("RESPONSE OK - flight mode operation finished");\r
229                 res.result = TCORE_RETURN_SUCCESS;\r
230         }\r
231         else    {\r
232                 dbg("RESPONSE NOK");\r
233                 line = (const char*)ATresp->final_response;\r
234                 tokens = tcore_at_tok_new(line);\r
235 \r
236                 if (g_slist_length(tokens) < 1) {\r
237                         dbg("err cause not specified or string corrupted");\r
238                         res.result = TCORE_RETURN_3GPP_ERROR;\r
239                 }\r
240                 else    {\r
241                         response = atoi(g_slist_nth_data(tokens, 0));\r
242                         /* TODO: CMEE error mapping is required. */\r
243                         res.result = TCORE_RETURN_3GPP_ERROR;\r
244                 }\r
245         }\r
246 \r
247         ur = tcore_pending_ref_user_request(p);\r
248         if(NULL == ur){\r
249                 dbg("No user request. Internal request created during boot-up sequence");\r
250                 \r
251                 if(ATresp->success > 0){\r
252                         modem_flight_mode.enable = tcore_modem_get_flight_mode_state(o);\r
253                         dbg("sucess case - Sending Flight Mode Notification (%d) to Telephony Server",modem_flight_mode.enable);\r
254                         \r
255                         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_MODEM_FLIGHT_MODE,\r
256                                         sizeof(struct tnoti_modem_flight_mode), &modem_flight_mode);\r
257                 }\r
258         }\r
259         else{\r
260                 dbg("Sending response for Flight mode operation");\r
261                 tcore_user_request_send_response(ur, TRESP_MODEM_SET_FLIGHTMODE, sizeof(struct tresp_modem_set_flightmode), &res);\r
262         }\r
263         \r
264         tcore_at_tok_free(tokens);\r
265 }\r
266 \r
267 static void on_response_imei(TcorePending *p, int data_len, const void *data, void *user_data)\r
268 {\r
269         const TcoreATResponse* resp = data;\r
270         TcorePlugin *plugin = NULL;\r
271         struct tresp_modem_get_imei res;\r
272         TelMiscSNInformation *imei_property = NULL;\r
273         UserRequest* ur = NULL;\r
274         GSList *tokens=NULL;\r
275         const char *line;\r
276         int response = 0;\r
277 \r
278         memset(&res, 0, sizeof(struct tresp_modem_get_imei));\r
279 \r
280         if(resp->success > 0)\r
281         {\r
282                 dbg("RESPONSE OK");\r
283                 if(resp->lines) {\r
284                         line = (const char*)resp->lines->data;\r
285                         tokens = tcore_at_tok_new(line);\r
286                         if (g_slist_length(tokens) != 1) {\r
287                                 msg("invalid message");\r
288                                 goto OUT;\r
289                         }\r
290                 }\r
291                 res.result = TCORE_RETURN_SUCCESS;\r
292                 strncpy(res.imei, g_slist_nth_data(tokens, 0), 16);\r
293 \r
294                 dbg("imei = [%s]", res.imei);\r
295 \r
296                 plugin = tcore_pending_ref_plugin(p);\r
297                 imei_property = tcore_plugin_ref_property(plugin, "IMEI");\r
298                 if (imei_property)\r
299                 {\r
300                         imei_property->sn_index = TAPI_MISC_ME_IMEI;\r
301                         imei_property->sn_len = strlen(res.imei);\r
302                         memcpy(imei_property->szNumber, res.imei, imei_property->sn_len);\r
303                 }\r
304         }\r
305         else\r
306         {\r
307                 dbg("RESPONSE NOK");\r
308                 if(resp->lines) {\r
309                         line = (const char*)resp->lines->data;\r
310                         tokens = tcore_at_tok_new(line);\r
311                 }\r
312 \r
313                 \r
314                 if (g_slist_length(tokens) < 1) {\r
315                         dbg("err cause not specified or string corrupted");\r
316                         res.result = TCORE_RETURN_3GPP_ERROR;\r
317                 }\r
318                 else\r
319                 {\r
320                         response = atoi(g_slist_nth_data(tokens, 0));\r
321                         /* TODO: CMEE error mapping is required. */\r
322                         res.result = TCORE_RETURN_3GPP_ERROR;\r
323                 }\r
324         }\r
325 \r
326         ur = tcore_pending_ref_user_request(p); \r
327         tcore_user_request_send_response(ur, TRESP_MODEM_GET_IMEI, sizeof(struct tresp_modem_get_imei), &res);  \r
328 \r
329 OUT:\r
330         if(tokens != NULL)\r
331                 tcore_at_tok_free(tokens);\r
332 \r
333         return;\r
334 }\r
335 \r
336 static void on_response_version(TcorePending *p, int data_len, const void *data, void *user_data)\r
337 {\r
338         const TcoreATResponse *resp = data;\r
339         TcorePlugin *plugin = NULL;\r
340         struct tresp_modem_get_version res = {0};\r
341         TelMiscVersionInformation *vi_property = NULL;\r
342         TelMiscVersionInformation *vi = NULL;\r
343         UserRequest *ur = NULL;\r
344         GSList* tokens = NULL;\r
345         const char* line=NULL;\r
346         char *swver= NULL;\r
347         char *hwver=NULL; \r
348         char *caldate=NULL;\r
349         char *pcode=NULL;\r
350         char *id=NULL;\r
351 \r
352         int response = 0;\r
353 \r
354         if(resp->success > 0)\r
355         {\r
356                 dbg("RESPONSE OK");\r
357                 if(resp->lines) {\r
358                         line = (const char*)resp->lines->data;\r
359                         tokens = tcore_at_tok_new(line);\r
360                         if (g_slist_length(tokens) != 5) {\r
361                                 msg("invalid message");\r
362                                 goto OUT;\r
363                         }\r
364                 }\r
365 \r
366                 swver = g_slist_nth_data(tokens, 0);\r
367                 hwver = g_slist_nth_data(tokens, 1);\r
368                 caldate = g_slist_nth_data(tokens, 2);\r
369                 pcode = g_slist_nth_data(tokens, 3);\r
370                 id = g_slist_nth_data(tokens, 4);\r
371 \r
372                 dbg("version: sw=[%s], hw=[%s], rf_cal=[%s], product_code=[%s], model_id=[%s]", swver, hwver, caldate, pcode, id);\r
373 \r
374                 vi = calloc(sizeof(TelMiscVersionInformation), 1);\r
375                 if(NULL!=swver)\r
376                         memcpy(vi->szSwVersion, swver, strlen(swver));\r
377                 if(NULL!=hwver)\r
378                         memcpy(vi->szHwVersion, hwver, strlen(hwver));\r
379                 if(NULL!=caldate)\r
380                         memcpy(vi->szRfCalDate, caldate, strlen(caldate));\r
381                 if(NULL!=pcode)\r
382                         memcpy(vi->szProductCode, pcode,strlen(pcode));\r
383                 if(NULL!=id)\r
384                         memcpy(vi->szModelId, id, strlen(id));\r
385 \r
386                 memset(&res, 0, sizeof(struct tresp_modem_get_version));\r
387 \r
388                 if(NULL!=swver)         \r
389                         snprintf(res.software, (AT_VER_LEN >strlen(swver) ?strlen(swver):AT_VER_LEN), "%s", swver);\r
390                 if(NULL!=hwver)\r
391                         snprintf(res.hardware, (AT_VER_LEN >strlen(hwver) ?strlen(hwver):AT_VER_LEN), "%s", hwver);\r
392 \r
393                 plugin = tcore_pending_ref_plugin(p);\r
394                 vi_property = tcore_plugin_ref_property(plugin, "VERSION");\r
395                 memcpy(vi_property, vi, sizeof(TelMiscVersionInformation));\r
396                 free(vi);\r
397         }\r
398         else\r
399         {\r
400                 dbg("RESPONSE NOK");\r
401                 if(resp->lines) {\r
402                         line = (const char*)resp->lines->data;\r
403                         tokens = tcore_at_tok_new(line);\r
404                 }\r
405 \r
406                 memset(&res, 0, sizeof(struct tresp_modem_get_version));\r
407 \r
408 \r
409                 if (g_slist_length(tokens) < 1) {\r
410                         dbg("err cause not specified or string corrupted");\r
411                     res.result = TCORE_RETURN_3GPP_ERROR;\r
412                 }\r
413                 else\r
414                 {\r
415                         response = atoi(g_slist_nth_data(tokens, 0));\r
416                         /* TODO: CMEE error mapping is required. */\r
417                 res.result = TCORE_RETURN_3GPP_ERROR;\r
418                 }\r
419         }\r
420 \r
421         ur = tcore_pending_ref_user_request(p);\r
422         tcore_user_request_send_response(ur, TRESP_MODEM_GET_VERSION, sizeof(struct tresp_modem_get_version), &res);\r
423 \r
424 OUT:\r
425         if(tokens != NULL)\r
426                 tcore_at_tok_free(tokens);\r
427 \r
428         return; \r
429 }\r
430 \r
431 static gboolean on_event_bootup_sim_status(CoreObject *o, const void *event_info, void *user_data)\r
432 {\r
433         GSList *tok = NULL;\r
434         GSList *lines = NULL;\r
435         int value = -1;\r
436         char* line = NULL;\r
437 \r
438         lines = (GSList*)event_info;\r
439         if (1 != g_slist_length(lines)) {\r
440                 dbg("unsolicited msg but multiple line");\r
441             goto OUT;\r
442         }\r
443         line  = (char*)(lines->data);\r
444         dbg("on_bootup_event_sim_status notification : %s", line);\r
445 \r
446         tok = tcore_at_tok_new(line);\r
447         value = atoi(g_slist_nth_data(tok, 0));\r
448 \r
449         if(7 == value){\r
450                 dbg("SIM ready. request COPS & remove callback");\r
451                 dbg("power on done set for proactive command receiving mode");\r
452                 prepare_and_send_pending_request(tcore_object_ref_plugin(o), "sat", "AT+CFUN=6", NULL, TCORE_AT_NO_RESULT, on_response_enable_proactive_command);\r
453                 prepare_and_send_pending_request(tcore_object_ref_plugin(o), "umts_network", "AT+COPS=0", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
454                 return FALSE;\r
455         }\r
456 \r
457 OUT:\r
458         if (tok != NULL)\r
459                 tcore_at_tok_free(tok);\r
460         \r
461         return TRUE;\r
462 }\r
463 \r
464 \r
465 \r
466 gboolean on_event_modem_power(TcoreAT *at, const char *line, TcorePlugin *p)\r
467 {\r
468         CoreObject *o = NULL;\r
469         struct treq_modem_set_flightmode flight_mode_set = {0};\r
470         struct tnoti_modem_power modem_power = {0};\r
471         TcoreHal *h = NULL;\r
472         Storage *strg = NULL; \r
473         \r
474         o = tcore_plugin_ref_core_object(p, "modem");\r
475         \r
476         strg = tcore_server_find_storage(tcore_plugin_ref_server(p), "vconf");\r
477         flight_mode_set.enable = tcore_storage_get_bool(strg, STORAGE_KEY_SETAPPL_FLIGHT_MODE_BOOL);\r
478 \r
479         h = tcore_object_get_hal(o);\r
480         tcore_hal_set_power_state(h, TRUE);\r
481         \r
482         /* Set Flight mode as per AP settings */\r
483         if (flight_mode_set.enable)\r
484         { /* Radio Off */\r
485                 prepare_and_send_pending_request(p, "modem", "AT+CFUN=4", NULL, TCORE_AT_NO_RESULT, on_response_set_flight_mode);\r
486                 tcore_modem_set_flight_mode_state(o, TRUE);\r
487         }\r
488         else\r
489         {/* Radio On */\r
490                 prepare_and_send_pending_request(p, "modem", "AT+CFUN=1", NULL, TCORE_AT_NO_RESULT, on_response_set_flight_mode);\r
491                 tcore_modem_set_flight_mode_state(o, FALSE);\r
492         }\r
493                 \r
494         /* Get IMEI */\r
495         prepare_and_send_pending_request(p, "modem", "AT+CGSN", NULL, TCORE_AT_NUMERIC, on_response_imei);\r
496 \r
497         /* Get Version Number  */\r
498         prepare_and_send_pending_request(p, "modem", "AT+CGMR", NULL, TCORE_AT_SINGLELINE, on_response_version);\r
499         \r
500         tcore_modem_set_powered(o, TRUE);\r
501 \r
502         modem_power.state = MODEM_STATE_ONLINE;\r
503 \r
504         tcore_server_send_notification(tcore_plugin_ref_server(tcore_object_ref_plugin(o)), o, TNOTI_MODEM_POWER,\r
505                         sizeof(struct tnoti_modem_power), &modem_power);\r
506 \r
507         return TRUE;\r
508 }\r
509 \r
510 static void _modem_subscribe_events(TcorePlugin *plugin)\r
511 {\r
512         dbg("Entry");\r
513         \r
514         /* XCALLSTAT subscription */\r
515         prepare_and_send_pending_request(plugin, "call", "at+xcallstat=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
516 \r
517         /* XSIMSTATE subscription */\r
518         prepare_and_send_pending_request(plugin, "sim", "at+xsimstate=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
519 \r
520                 prepare_and_send_pending_request(plugin, "umts_sms", "at+xsimstate=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
521                 prepare_and_send_pending_request(plugin, "modem", "at+xsimstate=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
522 \r
523         /* CREG subscription */\r
524         prepare_and_send_pending_request(plugin, "umts_network", "at+creg=2", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
525 \r
526         /* CGREG subscription */\r
527         prepare_and_send_pending_request(plugin, "umts_network", "at+cgreg=2", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
528 \r
529         /* Allow automatic time Zone updation via NITZ */\r
530         prepare_and_send_pending_request(plugin, "umts_network", "at+ctzu=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
531 \r
532         /* TZ, time & daylight changing event reporting subscription */\r
533         prepare_and_send_pending_request(plugin, "umts_network", "at+ctzr=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
534         \r
535         /* XMER subscription */\r
536         prepare_and_send_pending_request(plugin, "umts_network", "at+xmer=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
537         \r
538         /* CGEREP subscription */\r
539         prepare_and_send_pending_request(plugin, "umts_ps", "at+cgerep=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
540 \r
541         /* XDATASTAT subscription */\r
542         prepare_and_send_pending_request(plugin, "umts_ps", "at+xdatastat=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
543 \r
544         /* CMGF subscription */\r
545         prepare_and_send_pending_request(plugin, "umts_sms", "at+cmgf=0", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
546 \r
547         /* CSSN subscription */\r
548         prepare_and_send_pending_request(plugin, "call", "at+cssn=1,1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
549 \r
550         /* CUSD subscription */\r
551         prepare_and_send_pending_request(plugin, "call", "at+cusd=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
552 \r
553         /* XDNS subscription */\r
554         prepare_and_send_pending_request(plugin, "umts_ps", "at+xdns=1,1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
555 \r
556         /* CLIP subscription */\r
557         prepare_and_send_pending_request(plugin, "call", "at+clip=1", NULL, TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
558 \r
559         /*CMEE subscription*/\r
560         prepare_and_send_pending_request(plugin,"umts_ps","at+cmee=2",NULL,TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
561 \r
562         /*incoming sms and cb subscription*/\r
563         prepare_and_send_pending_request(plugin,"umts_sms","at+cnmi=1,2,2,2,0",NULL,TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
564 \r
565         /*message service subscription*/\r
566         prepare_and_send_pending_request(plugin,"umts_sms","at+csms=1",NULL,TCORE_AT_NO_RESULT, on_response_bootup_subscription);\r
567 \r
568         /* text/pdu mode subscription*/\r
569         prepare_and_send_pending_request(plugin,"umts_sms","at+cmgf=0",NULL,TCORE_AT_NO_RESULT, on_response_last_bootup_subscription);\r
570 \r
571         dbg("Exit");\r
572         return;\r
573 }\r
574 \r
575 \r
576 static void on_response_setupmux(TcorePending *p, int data_len, const void *data, void *user_data)\r
577 {\r
578         TcorePlugin *plugin = NULL;\r
579         TcoreHal *hal = NULL;\r
580         TReturn ret;\r
581         dbg("Entry");\r
582         \r
583         /* IMC Plugin dereferenced from pending request */\r
584         plugin = tcore_pending_ref_plugin(p);\r
585         \r
586         /* Actual HAL - like svnet(2) */\r
587         hal = (TcoreHal *)user_data;\r
588 \r
589         /* Initialize CMUX */\r
590         ret = tcore_cmux_init(plugin, hal);\r
591         if(TCORE_RETURN_SUCCESS == ret) {\r
592                 dbg("Successfully initialized CMUX");\r
593         }\r
594         else {\r
595                 err("Failed to initialize CMUX");\r
596         }\r
597         \r
598         dbg("Exit");\r
599         return;\r
600 }\r
601 \r
602 \r
603 \r
604 static void setup_mux(CoreObject *o)\r
605 {\r
606         TcoreHal* hal = NULL;\r
607         TcorePending *pending = NULL;\r
608         dbg("Entered");\r
609 \r
610         /* HAL has type itself,\r
611          * e.g.) TCORE_HAL_MODE_AT\r
612          */\r
613         hal = tcore_object_get_hal(o);\r
614 \r
615         pending = tcore_at_pending_new(o, "AT+CMUX=0,0,,1509,10,3,30,,", "+CMUX", TCORE_AT_NO_RESULT, on_response_setupmux, hal);\r
616 \r
617         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
618 \r
619         /* Send callback */\r
620         tcore_hal_send_request(hal, pending);\r
621 \r
622         dbg("Exit");\r
623         return;\r
624 }\r
625 \r
626 static gboolean on_event_mux_channel_up(CoreObject *o, const void *event_info, void *user_data)\r
627 {\r
628         TcorePlugin *plugin = NULL;\r
629         dbg("Entry");\r
630 \r
631         plugin = (TcorePlugin *)user_data;\r
632         _modem_subscribe_events(plugin);\r
633         dbg("Exit");\r
634         return TRUE;\r
635 }\r
636 \r
637         \r
638 static void on_response_enable_logging(TcorePending *p, int data_len, const void *data, void *user_data)\r
639 {\r
640         const TcoreATResponse *resp = data;\r
641         TcorePlugin *plugin = NULL;\r
642         \r
643         plugin = tcore_pending_ref_plugin(p);\r
644         \r
645         /* DELETE ME: only for DEBUG */\r
646         if (!resp)\r
647                 dbg("no data");\r
648 \r
649         dbg("response...(result = %d, final_response = '%s')", resp->success, resp->final_response);\r
650 \r
651         if(resp->success){\r
652                 dbg("RESPONSE OK");\r
653                 dbg("Enabling CP logging is success !!!\n");\r
654 \r
655         }else {\r
656                 dbg("RESPONSE NOK");\r
657                 dbg("Enabling CP logging is failed !!!\n");\r
658         }\r
659 \r
660                 dbg("Calling setup_mux");\r
661                 setup_mux(tcore_pending_ref_core_object(p));\r
662 \r
663 \r
664         dbg("Exit");\r
665         return;\r
666 \r
667 }\r
668 \r
669 static void _send_enable_logging_command(CoreObject *o)\r
670 {\r
671         TcoreATRequest *req = NULL;\r
672         TcoreHal *hal = NULL;\r
673         TcorePending *pending = NULL;\r
674         TReturn ret = 0;\r
675         \r
676         /* DLELTE ME */\r
677         dbg("Send Trace enabling command for CP logging. \n");\r
678 \r
679         if(!o){\r
680                 dbg("Co-object is Null !!\n");\r
681                 //goto error;\r
682         }\r
683 \r
684         hal = tcore_object_get_hal(o);\r
685         pending = tcore_pending_new(o, 0);\r
686         req = tcore_at_request_new("at+xsystrace=1,\"digrf=1;bb_sw=1;3g_sw=1\",\"digrf=0x84\",\"oct=4\";+xsystrace=11;+trace=1", NULL, TCORE_AT_NO_RESULT);\r
687 \r
688         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
689 \r
690         tcore_pending_set_request_data(pending, 0, req);\r
691         tcore_pending_set_response_callback(pending, on_response_enable_logging, hal);\r
692         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
693 \r
694         ret = tcore_hal_send_request(hal, pending);\r
695         if(ret != TCORE_RETURN_SUCCESS)\r
696                 dbg("tcore_hal_send_request fail !!! (hal: 0x%x, pending: 0x%x)\n", hal, pending);\r
697 \r
698 }\r
699 \r
700 static void on_response_poweron(TcorePending *p, int data_len, const void *data, void *user_data)\r
701 {\r
702         const TcoreATResponse *resp = data;\r
703         GSList*tokens=NULL;\r
704         const char *line=NULL;\r
705         gboolean bpoweron = FALSE;\r
706         int response = 0;\r
707                 \r
708         if(resp->success){\r
709                 dbg("RESPONSE OK");\r
710                 /* Parse AT Response */\r
711                 if (resp->lines) {\r
712                         line = (const char *)resp->lines->data;\r
713                         tokens = tcore_at_tok_new(line);\r
714                         if (g_slist_length(tokens) != 1) {\r
715                                 dbg("invalid message");\r
716                                 bpoweron = FALSE;\r
717                                 goto error;\r
718                         }\r
719                 }\r
720 \r
721                 response = atoi(g_slist_nth_data(tokens, 0));\r
722 \r
723                 dbg("CPAS: response %d",response);\r
724 \r
725                 switch(response)\r
726                 {\r
727                         case CPAS_RES_READY:\r
728                         case CPAS_RES_RINGING:\r
729                         case CPAS_RES_CALL_PROGRESS:\r
730                         case CPAS_RES_ASLEEP:\r
731                                 bpoweron = TRUE;\r
732                         break;\r
733 \r
734                         case CPAS_RES_UNAVAIL:\r
735                         case CPAS_RES_UNKNOWN:\r
736                         default:\r
737                                 dbg("value is unvail/unknown - but CP responded - proceed poweron");\r
738                                 //bpoweron = FALSE;\r
739                                 bpoweron = TRUE;\r
740                         break;\r
741                 }\r
742         }else{\r
743                 dbg("CPAS: RESPONSE NOK");\r
744                 bpoweron = FALSE;\r
745         }\r
746 \r
747 error:\r
748         /* DELE ME: AT request & response are freed after AT processing in HAL.\r
749          * ref.) _emit_pending_response (libtcore/src/at.c)\r
750          */\r
751          if(tokens != NULL)\r
752                 tcore_at_tok_free(tokens);\r
753 \r
754         if(bpoweron == TRUE){\r
755                 dbg("Power on NOTI received, (pending: 0x%x, co: 0x%x)\n", p, tcore_pending_ref_core_object(p));\r
756 \r
757                 _send_enable_logging_command(tcore_pending_ref_core_object(p));\r
758         }\r
759         else{\r
760                 dbg("CP is not ready, let us send CPAS once again");\r
761                 s_modem_send_poweron(tcore_object_ref_plugin(tcore_pending_ref_core_object(p)));\r
762         }\r
763         return;\r
764 }\r
765 \r
766 static TReturn power_off(CoreObject *o, UserRequest *ur)\r
767 {\r
768         TcoreHal *hal = NULL;\r
769         TcoreATRequest *req = NULL;\r
770         TcorePending *pending = NULL;\r
771 \r
772         hal = tcore_object_get_hal(o);\r
773         pending = tcore_pending_new(o, 0);\r
774 \r
775         req = tcore_at_request_new("AT+CFUN=0", NULL, TCORE_AT_NO_RESULT);\r
776 \r
777         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
778 \r
779         tcore_pending_set_request_data(pending, 0, req);\r
780         tcore_pending_set_response_callback(pending, on_response_power_off, hal);\r
781         tcore_pending_link_user_request(pending, ur);\r
782         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
783 \r
784         tcore_hal_send_request(hal, pending);\r
785 \r
786         return TCORE_RETURN_SUCCESS;\r
787 }\r
788 \r
789 static TReturn get_imei(CoreObject *o, UserRequest *ur)\r
790 {\r
791         TcoreHal* hal;\r
792         TcoreATRequest *req;\r
793         TcorePending *pending = NULL;\r
794 \r
795         hal = tcore_object_get_hal(o);\r
796         pending = tcore_pending_new(o, 0);\r
797 \r
798         req = tcore_at_request_new("AT+CGSN", NULL, TCORE_AT_NUMERIC);\r
799 \r
800         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
801 \r
802         tcore_pending_set_request_data(pending, 0, req);\r
803         tcore_pending_set_response_callback(pending, on_response_imei, hal);\r
804         tcore_pending_link_user_request(pending, ur);\r
805         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
806 \r
807         tcore_hal_send_request(hal, pending);\r
808 \r
809         return TCORE_RETURN_SUCCESS;\r
810 }\r
811 \r
812 \r
813 static TReturn get_version(CoreObject *o, UserRequest *ur)\r
814 {\r
815         TcoreHal* hal;\r
816         TcoreATRequest *req;\r
817         TcorePending *pending = NULL;\r
818 \r
819         hal = tcore_object_get_hal(o);\r
820         pending = tcore_pending_new(o, 0);\r
821 \r
822         req = tcore_at_request_new("AT+CGMR", NULL, TCORE_AT_SINGLELINE);\r
823 \r
824         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
825 \r
826         tcore_pending_set_request_data(pending, 0, req);\r
827         tcore_pending_set_response_callback(pending, on_response_version, hal);\r
828         tcore_pending_link_user_request(pending, ur);\r
829         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
830 \r
831         tcore_hal_send_request(hal, pending);\r
832 \r
833         return TCORE_RETURN_SUCCESS;\r
834 }\r
835 \r
836 static TReturn set_flight_mode(CoreObject *o, UserRequest *ur)\r
837 {\r
838         TcoreHal *hal = NULL;\r
839         TcoreATRequest *req = NULL;\r
840         TcorePending *pending = NULL;\r
841         const struct treq_modem_set_flightmode *req_data = NULL;\r
842         char *cmd_str = NULL;\r
843 \r
844         hal = tcore_object_get_hal(o);\r
845         pending = tcore_pending_new(o, 0);\r
846 \r
847         req_data = tcore_user_request_ref_data(ur, NULL);\r
848 \r
849         if (req_data->enable) {\r
850                 dbg("Flight mode on/n");\r
851                 cmd_str = g_strdup("AT+CFUN=4");\r
852         }\r
853         else {\r
854                 dbg("Flight mode off/n");\r
855                 cmd_str = g_strdup("AT+CFUN=1");\r
856         }\r
857 \r
858         req = tcore_at_request_new((const char*)cmd_str, NULL, TCORE_AT_NO_RESULT);\r
859 \r
860         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
861 \r
862         tcore_pending_set_request_data(pending, 0, req);\r
863         tcore_pending_set_response_callback(pending, on_response_set_flight_mode, hal);\r
864         tcore_pending_link_user_request(pending, ur);\r
865         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
866 \r
867         tcore_hal_send_request(hal, pending);\r
868 \r
869         return TCORE_RETURN_SUCCESS;\r
870 }\r
871 \r
872 \r
873 static struct tcore_modem_operations modem_ops =\r
874 {\r
875         .power_on = NULL,\r
876         .power_off = power_off,\r
877         .power_reset = NULL,\r
878         .set_flight_mode = set_flight_mode,\r
879         .get_imei = get_imei,\r
880         .get_version = get_version,\r
881         .get_sn = NULL,\r
882         .dun_pin_ctrl = NULL,\r
883 };\r
884 \r
885 gboolean s_modem_init(TcorePlugin *p, TcoreHal *h)\r
886 {\r
887         CoreObject *o = NULL;\r
888         GQueue *work_queue = NULL;\r
889         TelMiscVersionInformation *vi_property = NULL;\r
890         TelMiscSNInformation *imei_property = NULL;\r
891         TelMiscSNInformation *sn_property = NULL;\r
892 \r
893         o = tcore_modem_new(p, "modem", &modem_ops, h);\r
894         if (!o)\r
895                 return FALSE;\r
896 \r
897         work_queue = g_queue_new();\r
898         tcore_object_link_user_data(o, work_queue);\r
899 \r
900         vi_property = calloc(sizeof(TelMiscVersionInformation), 1);\r
901         tcore_plugin_link_property(p, "VERSION", vi_property);\r
902 \r
903         imei_property = calloc(sizeof(TelMiscSNInformation), 1);\r
904         tcore_plugin_link_property(p, "IMEI", imei_property);\r
905 \r
906         sn_property = calloc(sizeof(TelMiscSNInformation), 1);\r
907         tcore_plugin_link_property(p, "SN", sn_property);\r
908 \r
909                 dbg("Registerind for CMUX-UP event");\r
910                 tcore_object_add_callback(o, "CMUX-UP", on_event_mux_channel_up, p);\r
911 \r
912 \r
913         dbg("Registering for +XSIM event");\r
914         tcore_object_add_callback(o, "+XSIM", on_event_bootup_sim_status, NULL);\r
915 \r
916         return TRUE;\r
917 }\r
918 \r
919 void s_modem_exit(TcorePlugin *p)\r
920 {\r
921         CoreObject *o = NULL;\r
922         GQueue *work_queue = NULL;\r
923         TelMiscVersionInformation *vi_property = NULL;\r
924         TelMiscSNInformation *imei_property = NULL;\r
925         TelMiscSNInformation *sn_property = NULL;\r
926 \r
927         if (!p)\r
928                 return;\r
929 \r
930         o = tcore_plugin_ref_core_object(p, "modem");\r
931 \r
932         work_queue = tcore_object_ref_user_data(o);\r
933         g_queue_free(work_queue);\r
934 \r
935         vi_property = tcore_plugin_ref_property(p, "VERSION");\r
936         if (vi_property)\r
937                 free(vi_property);\r
938 \r
939         imei_property = tcore_plugin_ref_property(p, "IMEI");\r
940         if (imei_property)\r
941                 free(imei_property);\r
942 \r
943         sn_property = tcore_plugin_ref_property(p, "SN");\r
944         if (sn_property)\r
945                 free(sn_property);\r
946 \r
947         tcore_modem_free(o);\r
948         return;\r
949 }\r
950 \r
951 gboolean s_modem_send_poweron(TcorePlugin *p)\r
952 {\r
953         TcoreHal* hal;\r
954         TcoreATRequest *req;\r
955         TcorePending *pending = NULL;\r
956         CoreObject *o;\r
957 \r
958         o = tcore_plugin_ref_core_object(p, "modem");\r
959         hal = tcore_object_get_hal(o);\r
960 \r
961         pending = tcore_pending_new(o, 0);\r
962 \r
963         req = tcore_at_request_new("AT+CPAS", "+CPAS", TCORE_AT_SINGLELINE);\r
964 \r
965         dbg("cmd : %s, prefix(if any) :%s, cmd_len : %d", req->cmd, req->prefix, strlen(req->cmd));\r
966 \r
967         tcore_pending_set_timeout(pending, 10);\r
968         tcore_pending_set_priority(pending, TCORE_PENDING_PRIORITY_DEFAULT);    \r
969         tcore_pending_set_timeout_callback(pending, on_timeout_modem_poweron, hal);\r
970         \r
971         tcore_pending_set_request_data(pending, 0, req);\r
972         tcore_pending_set_response_callback(pending, on_response_poweron, hal);\r
973         tcore_pending_set_send_callback(pending, on_confirmation_modem_message_send, NULL);\r
974 \r
975         tcore_hal_send_request(hal, pending);\r
976 \r
977         return TRUE;\r
978 }\r