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