2 * tel-plugin-at_standard
4 * Copyright (c) 2012 Intel Corporation. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
29 #include <core_object.h>
34 #include <co_context.h>
38 enum pdp_context_state {
43 static TReturn set_pdp_context(CoreObject *co_ps, CoreObject *ps_context,
44 enum pdp_context_state state);
46 static void on_confirmation_ps_message_send(TcorePending *p, gboolean result,
49 dbg("msg out from queue");
51 dbg("Sending %s", (result == TRUE) ? "OK" : "FAIL");
54 static void notify_context_status_changed(CoreObject *co_ps, unsigned int cid,
55 enum ps_data_call_status status)
57 struct tnoti_ps_call_status data_resp = {0};
58 TcorePlugin *p = tcore_object_ref_plugin(co_ps);
59 Server *server = tcore_plugin_ref_server(p);
63 data_resp.context_id = cid;
64 data_resp.state = status;
66 tcore_server_send_notification(server, co_ps, TNOTI_PS_CALL_STATUS,
67 sizeof(struct tnoti_ps_call_status),
73 static void on_response_set_pdp_context_deactivate(TcorePending *p,
78 CoreObject *co_ps = tcore_pending_ref_core_object(p);
79 TcoreHal *h = tcore_object_get_hal(co_ps);
80 const TcoreATResponse *resp = data;
81 CoreObject *ps_context = user_data;
82 unsigned int cid = tcore_context_get_id(ps_context);
87 * AT+CGACT = 0 is returning NO CARRIER or an error. Just test if the
88 * response contains NO CARRIER else decode CME error.
90 if (resp->lines != NULL) {
93 line = (const char *)resp->lines->data;
94 if (g_strcmp0(line, "NO CARRIER") != 0) {
95 /* TODO: Decode CME error */
97 err("Context %d has not been deactivated", cid);
104 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
106 if (tcore_hal_setup_netif(h, co_ps, NULL, NULL, cid,
107 FALSE) != TCORE_RETURN_SUCCESS)
108 err("Failed to disable network interface");
114 static void on_setup_pdp(CoreObject *co_ps, int result, const char *netif_name,
117 CoreObject *ps_context = user_data;
118 struct tnoti_ps_call_status data_status = {0};
124 /* Deactivate PDP context */
125 tcore_ps_deactivate_context(co_ps, ps_context, NULL);
129 dbg("devname = [%s]", netif_name);
131 tcore_context_set_ipv4_devname(ps_context, netif_name);
133 server = tcore_plugin_ref_server(tcore_object_ref_plugin(co_ps));
135 data_status.context_id = tcore_context_get_id(ps_context);
136 data_status.state = PS_DATA_CALL_CONNECTED;
138 tcore_server_send_notification(server, co_ps,
139 TNOTI_PS_CALL_STATUS,
140 sizeof(struct tnoti_ps_call_status),
146 static void on_response_get_pdp_address(TcorePending *p, int data_len,
147 const void *data, void *user_data)
149 CoreObject *co_ps = tcore_pending_ref_core_object(p);
150 CoreObject *ps_context = user_data;
151 TcoreHal *h = tcore_object_get_hal(co_ps);
152 GSList *tokens = NULL;
153 const TcoreATResponse *resp = data;
156 char *real_pdp_address;
157 int cid = tcore_context_get_id(ps_context);
161 if (resp->final_response == NULL) {
168 if (resp->lines == NULL) {
169 err("Invalid response line");
173 line = (const char *)resp->lines->data;
174 tokens = tcore_at_tok_new(line);
175 if (g_slist_length(tokens) < 2) {
176 err("Invalid message");
180 dbg("Line: %s", line);
182 /* Skip CID & read directly IP address */
183 pdp_address = g_slist_nth_data(tokens, 1);
184 real_pdp_address = tcore_at_tok_extract(pdp_address);
186 tcore_context_set_ipv4_addr(ps_context, real_pdp_address);
188 dbg("PDP address: %s", real_pdp_address);
190 g_free(real_pdp_address);
192 dbg("Adding default DNS");
194 tcore_context_set_ipv4_dns(ps_context, "8.8.8.8", "8.8.4.4");
196 if (tcore_hal_setup_netif(h, co_ps, on_setup_pdp, ps_context, cid,
197 TRUE) == TCORE_RETURN_SUCCESS)
201 err("Failed to get PDP address deactivating context...");
202 set_pdp_context(co_ps, ps_context, DEACTIVATE);
205 tcore_at_tok_free(tokens);
209 static void get_pdp_address(CoreObject *co_ps, CoreObject *ps_context)
212 unsigned int cid = tcore_context_get_id(ps_context);
216 cmd_str = g_strdup_printf("AT+CGPADDR=%d", cid);
218 if (tcore_prepare_and_send_at_request(co_ps, cmd_str, NULL,
219 TCORE_AT_NO_RESULT, NULL,
220 on_response_get_pdp_address,
222 on_confirmation_ps_message_send, NULL)
223 != TCORE_RETURN_SUCCESS) {
224 err("Failed to prepare and send AT request");
225 set_pdp_context(co_ps, ps_context, DEACTIVATE);
233 static void on_response_set_pdp_context_activate(TcorePending *p, int data_len,
237 CoreObject *co_ps = tcore_pending_ref_core_object(p);
238 const TcoreATResponse *resp = data;
239 CoreObject *ps_context = user_data;
245 get_pdp_address(co_ps, ps_context);
247 unsigned int cid = tcore_context_get_id(ps_context);
249 /* TODO: Manage CME errors */
251 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
257 static TReturn set_pdp_context(CoreObject *co_ps, CoreObject *ps_context,
258 enum pdp_context_state state)
261 unsigned int cid = tcore_context_get_id(ps_context);
262 int ret = TCORE_RETURN_SUCCESS;
263 TcorePendingResponseCallback cb = NULL;
264 gboolean activate = (state == ACTIVATE);
268 dbg("CID %d %s", cid, activate ? "activation" : "deactivation");
270 if (activate == TRUE)
271 cb = on_response_set_pdp_context_activate;
273 cb = on_response_set_pdp_context_deactivate;
275 cmd_str = g_strdup_printf("AT+CGACT=%d,%d", activate ? 1 : 0, cid);
277 if (tcore_prepare_and_send_at_request(co_ps, cmd_str, NULL,
278 TCORE_AT_NO_RESULT, NULL,
280 on_confirmation_ps_message_send, NULL)
281 != TCORE_RETURN_SUCCESS) {
282 err("Failed to prepare and send AT request");
283 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
284 ret = TCORE_RETURN_FAILURE;
294 static void on_response_define_pdp_context(TcorePending *p, int data_len,
298 const TcoreATResponse *resp = data;
299 CoreObject *ps_context = user_data;
300 unsigned int cid = tcore_context_get_id(ps_context);
301 CoreObject *co_ps = tcore_pending_ref_core_object(p);
307 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_CTX_DEFINED);
310 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
316 static TReturn define_pdp_context(CoreObject *co_ps, CoreObject *ps_context,
320 char *cmd_str = NULL;
321 char *pdp_type_str = NULL;
323 enum co_context_type pdp_type;
324 enum co_context_d_comp d_comp;
325 enum co_context_h_comp h_comp;
326 int ret = TCORE_RETURN_FAILURE;
330 pdp_type = tcore_context_get_type(ps_context);
331 cid = tcore_context_get_id(ps_context);
334 case CONTEXT_TYPE_X25:
335 dbg("CONTEXT_TYPE_X25");
336 pdp_type_str = g_strdup("X.25");
339 case CONTEXT_TYPE_IP:
340 dbg("CONTEXT_TYPE_IP");
341 pdp_type_str = g_strdup("IP");
344 case CONTEXT_TYPE_PPP:
345 dbg("CONTEXT_TYPE_PPP");
346 pdp_type_str = g_strdup("PPP");
349 case CONTEXT_TYPE_IPV6:
350 dbg("CONTEXT_TYPE_IPV6");
351 pdp_type_str = g_strdup("IPV6");
355 /* PDP Type not supported supported */
356 err("Unsupported PDP type %d ", pdp_type);
361 d_comp = tcore_context_get_data_compression(ps_context);
362 h_comp = tcore_context_get_header_compression(ps_context);
363 apn = tcore_context_get_apn(ps_context);
365 cmd_str = g_strdup_printf("AT+CGDCONT=%d,\"%s\",\"%s\",,%d,%d", cid,
366 pdp_type_str, apn, d_comp, h_comp);
368 ret = tcore_prepare_and_send_at_request(co_ps, cmd_str, NULL,
369 TCORE_AT_NO_RESULT, NULL,
370 on_response_define_pdp_context,
372 on_confirmation_ps_message_send, NULL);
374 g_free(pdp_type_str);
378 if (ret == TCORE_RETURN_SUCCESS)
381 err("Failed to prepare and send AT request");
384 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
393 static gboolean on_cgev_notification(CoreObject *co_ps, const void *data,
396 GSList *tokens = NULL;
397 GSList *lines = (GSList *)data;
398 const char *line = lines->data;
408 dbg("Lines->data :%s", line);
410 tokens = tcore_at_tok_new(line);
411 if (g_slist_length(tokens) != 3)
414 noti_data = g_slist_nth_data(tokens, 0);
416 /* Only care about NW context deactivation */
417 if (g_str_has_prefix(noti_data, "NW DEACT") == FALSE)
420 noti_data = g_slist_nth_data(tokens, 1);
421 dbg("PDP Address: %s", noti_data);
423 noti_data = g_slist_nth_data(tokens, 2);
424 cid = (unsigned int)atoi(noti_data);
425 dbg("Context %d deactivated", cid);
427 notify_context_status_changed(co_ps, cid, PS_DATA_CALL_NOT_CONNECTED);
429 hal = tcore_object_get_hal(co_ps);
430 if (tcore_hal_setup_netif(hal, co_ps, NULL, NULL, cid,
431 FALSE) != TCORE_RETURN_SUCCESS)
432 err("Failed to disable network interface");
435 tcore_at_tok_free(tokens);
442 static TReturn activate_ps_context(CoreObject *co_ps, CoreObject *ps_context,
445 return set_pdp_context(co_ps, ps_context, ACTIVATE);
448 static TReturn deactivate_ps_context(CoreObject *co_ps, CoreObject *ps_context,
451 return set_pdp_context(co_ps, ps_context, DEACTIVATE);
454 static struct tcore_ps_operations ps_ops = {
455 .define_context = define_pdp_context,
456 .activate_context = activate_ps_context,
457 .deactivate_context = deactivate_ps_context
460 gboolean at_ps_init(TcorePlugin *cp)
462 CoreObject *co = NULL;
465 co = tcore_ps_new(cp, &ps_ops, NULL);
469 tcore_object_add_callback(co, "+CGEV", on_cgev_notification, NULL);
471 server = tcore_plugin_ref_server(cp);
472 tcore_server_add_template_object(server, co);
477 void at_ps_exit(TcorePlugin *cp)
484 co = tcore_plugin_ref_core_object(cp, CORE_OBJECT_TYPE_PS);
488 tcore_object_free(co);