00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00027 #include <string.h>
00028
00029 #include <gtk/gtk.h>
00030 #include <glib.h>
00031
00032 #include <liberdm/connectionMgrConstants.h>
00033
00034 #include "connectionMgrLog.h"
00035 #include "connectScreenData.h"
00036 #include "connectScreenProfiles.h"
00037 #include "pingThread.h"
00038 #include "connectPing.h"
00039 #include "connectionMgr.h"
00040
00041 static gboolean connect_ping_start (gpointer data);
00042 static void connect_ping_done (pingContext_t *ctxt, pingStatus_t result);
00043 static void on_ping_status_changed (GObject *object,
00044 GParamSpec *arg1,
00045 gpointer data);
00046
00047 static gboolean connect_ping_select_profile(pingContext_t *ctxt,
00048 int profileIndex);
00049 static void connect_ping_unselect_profile (pingContext_t *ctxt);
00050
00051 pingContext_t *connect_ping_ctxt_new(void)
00052 {
00053 pingContext_t *ctxt;
00054 GtkWidget *pingStatus;
00055
00056 CN_LOGPRINTF("entry");
00057
00058 ctxt = g_new0(pingContext_t, 1);
00059 g_assert(ctxt != NULL);
00060
00061
00062 connect_ping_ctxt_set_mode(ctxt, undefPingMode_e);
00063 connect_ping_ctxt_set_connect_type(ctxt, cmgrConnectUnknown_e);
00064 connect_ping_ctxt_set_done_callbacks(ctxt, NULL, NULL);
00065
00066 ctxt->selectedProfileIndex = -1;
00067 ctxt->pingingProfileIndexStart = -1;
00068 ctxt->pingingProfileIndex = -1;
00069
00070
00071 ctxt->pingParms = g_new0(ping_thread_parms, 1);
00072 g_assert(ctxt->pingParms != NULL);
00073
00074
00075 pingStatus = gtk_label_new ("ping-idle");
00076 g_signal_connect(pingStatus, "notify",
00077 G_CALLBACK (on_ping_status_changed), ctxt);
00078 ctxt->pingParms->pingStatus = pingStatus;
00079
00080
00081
00082 return ctxt;
00083 }
00084
00085 void connect_ping_ctxt_set_mode(pingContext_t* ctxt, pingMode_t mode)
00086 {
00087 if (ctxt && (mode >= 0) && (mode < undefPingMode_e))
00088 {
00089 ctxt->mode = mode;
00090 }
00091 }
00092
00093 void connect_ping_ctxt_set_connect_type(pingContext_t* ctxt,
00094 cmgrConnectType_t connectType)
00095 {
00096 if (ctxt && (connectType >= 0) && (connectType < cmgrConnectUndef_e))
00097 {
00098 ctxt->connectType = connectType;
00099 }
00100 }
00101
00102 void connect_ping_ctxt_set_get_initial_profile(pingContext_t* ctxt,
00103 get_initial_profile_t* get_initial_profile)
00104 {
00105 if (ctxt)
00106 {
00107 ctxt->get_initial_profile = get_initial_profile;
00108 }
00109 }
00110
00111 void connect_ping_ctxt_set_access_network_profiles_callbacks(pingContext_t *ctxt,
00112 get_n_network_profiles_t *get_n_network_profiles,
00113 get_network_profile_t *get_network_profile,
00114 select_next_regprofile_t *select_next_regprofile)
00115 {
00116 if (ctxt)
00117 {
00118 ctxt->get_n_network_profiles = get_n_network_profiles;
00119 ctxt->get_network_profile = get_network_profile;
00120 ctxt->select_next_regprofile = select_next_regprofile;
00121 }
00122 }
00123
00124 void connect_ping_ctxt_set_ui_callbacks(pingContext_t* ctxt,
00125 ui_update_status_t* ui_update_status,
00126 ui_display_settings_t* ui_display_settings,
00127 ui_select_profile_t* ui_select_profile,
00128 ui_unselect_all_profile_t* ui_unselect_all_profile)
00129 {
00130 if (ctxt)
00131 {
00132 ctxt->ui_update_status = ui_update_status;
00133 ctxt->ui_display_settings = ui_display_settings;
00134 ctxt->ui_select_profile = ui_select_profile;
00135 ctxt->ui_unselect_all_profile = ui_unselect_all_profile;
00136 }
00137 }
00138
00139 void connect_ping_ctxt_set_done_callbacks(pingContext_t* ctxt,
00140 on_connected_t* on_connected,
00141 on_failed_all_t* on_failed_all)
00142 {
00143 if (ctxt)
00144 {
00145 ctxt->on_connected = on_connected;
00146 ctxt->on_failed_all = on_failed_all;
00147 }
00148 }
00149
00150 static void free_network_pc_settings(ping_thread_parms *pingParms)
00151 {
00152 regNetworkProfile_t *settings;
00153 regPCProfile_t *pcshare;
00154
00155 if (pingParms)
00156 {
00157 settings = (regNetworkProfile_t*)pingParms->settings;
00158 erRegFreeNetworkProfile(settings);
00159 pingParms->settings = NULL;
00160
00161 pcshare = (regPCProfile_t*)pingParms->pcshare;
00162 erRegFreePCProfile(pcshare);
00163 pingParms->pcshare = NULL;
00164 }
00165 }
00166
00167 void connect_ping_ctxt_destory(pingContext_t* ctxt)
00168 {
00169 ping_thread_parms *pingParms;
00170
00171 if (ctxt)
00172 {
00173 if (ctxt->pingParms)
00174 {
00175 pingParms = ctxt->pingParms;
00176 free_network_pc_settings(pingParms);
00177
00178
00179
00180
00181
00182
00183
00184 g_free(pingParms);
00185 }
00186 g_free(ctxt);
00187 }
00188 }
00189
00191
00193 int connect_ping_get_pinging_profile_index_start(pingContext_t* ctxt)
00194 {
00195 g_return_val_if_fail(ctxt != NULL, -1);
00196
00197 CN_LOGPRINTF("entry profileIndex=%d", ctxt->pingingProfileIndexStart);
00198
00199 return ctxt->pingingProfileIndexStart;
00200 }
00201
00202 void connect_ping_set_pinging_profile_index_start(pingContext_t* ctxt,
00203 int profileIndex)
00204 {
00205 g_return_if_fail(ctxt != NULL);
00206
00207 CN_LOGPRINTF("entry[%d]", profileIndex);
00208
00209 ctxt->pingingProfileIndexStart = profileIndex;
00210 }
00211
00212 int connect_ping_get_pinging_profile_index(pingContext_t* ctxt)
00213 {
00214 g_return_val_if_fail(ctxt != NULL, -1);
00215
00216 CN_LOGPRINTF("entry profileIndex=%d", ctxt->pingingProfileIndex);
00217
00218 return ctxt->pingingProfileIndex;
00219 }
00220
00221 void connect_ping_set_pinging_profile_index(pingContext_t* ctxt,
00222 int profileIndex)
00223 {
00224 g_return_if_fail(ctxt != NULL);
00225
00226 CN_LOGPRINTF("entry profileIndex[%d]", profileIndex);
00227
00228 ctxt->pingingProfileIndex = profileIndex;
00229 }
00230
00231 int connect_ping_get_selected_profile_index(pingContext_t* ctxt)
00232 {
00233 g_return_val_if_fail(ctxt != NULL, -1);
00234
00235 CN_LOGPRINTF("entry profileIndex=%d", ctxt->selectedProfileIndex);
00236
00237 return ctxt->selectedProfileIndex;
00238 }
00239
00240 void connect_ping_set_selected_profile_index(pingContext_t* ctxt,
00241 int profileIndex)
00242 {
00243 g_return_if_fail(ctxt != NULL);
00244
00245 CN_LOGPRINTF("entry profileIndex[%d]", profileIndex);
00246
00247 ctxt->selectedProfileIndex = profileIndex;
00248 }
00249
00250 static void connect_ping_select_first_profile(pingContext_t* ctxt)
00251 {
00252 int nProfiles = 0;
00253
00254 CN_LOGPRINTF("entry");
00255
00256 g_return_if_fail(ctxt != NULL);
00257
00258 if (ctxt->get_n_network_profiles)
00259 {
00260 nProfiles = ctxt->get_n_network_profiles();
00261 }
00262
00263 if (nProfiles > 0)
00264 {
00265 connect_ping_select_profile(ctxt, 0);
00266 }
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 static gboolean connect_ping_select_next_profile(pingContext_t* ctxt)
00287 {
00288 CN_LOGPRINTF("entry");
00289
00290 gint profileIndex;
00291 guint nProfiles;
00292 gboolean selected;
00293
00294 g_return_val_if_fail(ctxt != NULL, FALSE);
00295
00296 selected = FALSE;
00297 profileIndex = connect_ping_get_selected_profile_index(ctxt);
00298 if (ctxt->get_n_network_profiles)
00299 {
00300 nProfiles = ctxt->get_n_network_profiles();
00301 }
00302 else
00303 {
00304 nProfiles = 0;
00305 }
00306 if ((profileIndex >= 0) && (profileIndex < nProfiles))
00307 {
00308 if ((profileIndex + 1) < nProfiles)
00309 {
00310 connect_ping_select_profile(ctxt, profileIndex+1);
00311 selected = TRUE;
00312 }
00313 }
00314
00315 return selected;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 static gboolean connect_ping_select_profile(pingContext_t* ctxt,
00354 int profileIndex)
00355 {
00356 CN_LOGPRINTF("entry");
00357
00358 guint nProfiles;
00359 gboolean selected;
00360
00361 g_return_val_if_fail(ctxt != NULL, FALSE);
00362
00363 selected = FALSE;
00364 if (ctxt->get_n_network_profiles)
00365 {
00366 nProfiles = ctxt->get_n_network_profiles();
00367 }
00368 else
00369 {
00370 nProfiles = 0;
00371 }
00372 if ((profileIndex >= 0) && (profileIndex < nProfiles))
00373 {
00374 connect_ping_set_selected_profile_index(ctxt, profileIndex);
00375 selected = TRUE;
00376 }
00377 return selected;
00378 }
00379
00380
00381 void connect_ping_select_initial_profile (pingContext_t* ctxt)
00382 {
00383 CN_LOGPRINTF ("entry");
00384
00385 int profileIndex = 0;
00386 cmgrConnectType_t connectType;
00387
00388 g_return_if_fail(ctxt != NULL);
00389
00390
00391 connectType = ctxt->connectType;
00392 if (connectType == cmgrConnectUnknown_e)
00393 {
00394 CN_WARNPRINTF("Unknown connect type[%d].", connectType);
00395
00396
00397 connect_ping_unselect_profile (ctxt);
00398 }
00399 else
00400 {
00401 if (ctxt->get_initial_profile)
00402 {
00403 profileIndex = ctxt->get_initial_profile();
00404 }
00405
00406 connect_ping_select_profile(ctxt, profileIndex);
00407 connect_ping_set_pinging_profile_index_start(ctxt, profileIndex);
00408
00409 if (ctxt->mode != backgroundPing_e && ctxt->ui_select_profile)
00410 {
00411 ctxt->ui_select_profile(profileIndex);
00412 }
00413 }
00414 }
00415
00416 static void connect_ping_unselect_profile (pingContext_t* ctxt)
00417 {
00418 CN_LOGPRINTF ("entry");
00419
00420 g_return_if_fail(ctxt != NULL);
00421
00422 connect_ping_set_selected_profile_index(ctxt, -1);
00423 }
00424
00425
00427
00429 void connect_ping(pingContext_t* ctxt, gboolean delay)
00430 {
00431 CN_LOGPRINTF ("entry delay[%d]", delay);
00432
00433 g_return_if_fail(ctxt != NULL);
00434
00435 if (ctxt->pingServer)
00436 {
00437 CN_WARNPRINTF("Please wait while pinging...");
00438 return;
00439 }
00440
00441 if (delay)
00442 {
00443 if (ctxt->pingTimeoutId > 0)
00444 {
00445 CN_LOGPRINTF("remove the old timeout function for pinging");
00446 g_source_remove(ctxt->pingTimeoutId);
00447 ctxt->pingTimeoutId = 0;
00448 }
00449
00450
00451 ctxt->pingTimeoutId = g_timeout_add(delay,
00452 connect_ping_start,
00453 (gpointer)ctxt);
00454 }
00455 else
00456 {
00457 connect_ping_start(ctxt);
00458 }
00459 }
00460
00461 gboolean connect_ping_in_pinging(pingContext_t * ctxt)
00462 {
00463 g_return_val_if_fail(ctxt, FALSE);
00464
00465 CN_LOGPRINTF("return [%d]", ctxt->pingServer);
00466 return ctxt->pingServer;
00467 }
00468
00469
00470 static gboolean connect_ping_start (gpointer data)
00471 {
00472 pingContext_t* ctxt;
00473 int profileIndex;
00474 gboolean ok;
00475 const networkProfile_t* settings = NULL;
00476 regNetworkProfile_t* regSettings = NULL;
00477 const regPCProfile_t *pcshare = NULL;
00478
00479 ctxt = (pingContext_t*)data;
00480 g_return_val_if_fail(ctxt != NULL, FALSE);
00481
00482
00483 profileIndex = connect_ping_get_selected_profile_index(ctxt);
00484 CN_LOGPRINTF ("entry: selected profile [%d]", profileIndex);
00485 g_return_val_if_fail (profileIndex != -1, FALSE);
00486
00487 if (ctxt->get_network_profile)
00488 {
00489 settings = ctxt->get_network_profile(profileIndex);
00490 }
00491 g_return_val_if_fail(settings != NULL, FALSE);
00492
00493
00494 free_network_pc_settings(ctxt->pingParms);
00495
00496
00497 if (settings->nRegProfiles > 0
00498 && settings->regSettingsList
00499 && settings->regIndexList
00500 && ((settings->nActiveIndex >= 0)
00501 && (settings->nActiveIndex < settings->nRegProfiles)))
00502 {
00503 regSettings = settings->regSettingsList[settings->nActiveIndex];
00504 ctxt->pingParms->settings = erRegDupNetworkProfile (regSettings);
00505 CN_LOGPRINTF("settings->regIndexList[%d]=%d",
00506 settings->nActiveIndex,
00507 settings->regIndexList[settings->nActiveIndex]);
00508 }
00509 else
00510 {
00511 CN_LOGPRINTF("settings->scanSettings->encryption=%d",
00512 settings->scanSettings->encryption);
00513 if (connect_data_get_ac_wlan()
00514 && settings->scanSettings
00515 && settings->scanSettings->encryption == encr_none_t)
00516 {
00517 CN_LOGPRINTF("here");
00518
00519 regSettings = network_spec_to_reg_network(settings->scanSettings);
00520 ctxt->pingParms->settings = erRegDupNetworkProfile(regSettings);
00521 erRegFreeNetworkProfile(regSettings);
00522 regSettings = NULL;
00523 }
00524 }
00525
00526
00527 ctxt->pingServer = TRUE;
00528 connect_ping_set_pinging_profile_index(ctxt, profileIndex);
00529
00530 if (ctxt->pingParms->settings)
00531 {
00532 if (ctxt->mode != backgroundPing_e && ctxt->ui_update_status)
00533 {
00534 ctxt->ui_update_status(profileIndex, pingConnecting_e);
00535 }
00536
00537 pcshare = common_data_get_pcsharedata(0);
00538 if (pcshare && ctxt->connectType == cmgrConnectPCShare_e)
00539 {
00540 ctxt->pingParms->pcshare = erRegDupPCProfile (pcshare);
00541 }
00542 else
00543 {
00544 ctxt->pingParms->pcshare = NULL;
00545 }
00546
00547
00548 pcshare = ctxt->pingParms->pcshare;
00549 CN_WARNPRINTF ("profile [%s] pcshare [%s] [%s]",
00550 ctxt->pingParms->settings->name,
00551 pcshare ? pcshare->pcname : "",
00552 pcshare ? pcshare->sharename : "");
00553 ok = pingThread_start(ctxt->pingParms);
00554 if (ok)
00555 {
00556 CN_LOGPRINTF("Succeeded to start ping thread");
00557 }
00558 else
00559 {
00560 CN_ERRORPRINTF ("Failed to start ping thread");
00561 }
00562 }
00563 else
00564 {
00565 CN_LOGPRINTF("The %dth profile is the one by scanning,"
00566 " but it has no corresponding profile in registry. "
00567 "Skipped it. It can be used by changing the settings "
00568 "or filling in the encryption key. ",
00569 profileIndex);
00570 if (settings->scanSettings
00571 && settings->scanSettings->encryption == encr_none_t)
00572 {
00573 connect_ping_done(ctxt, pingSkipped_e);
00574 }
00575 else
00576 {
00577 connect_ping_done(ctxt, pingNeedKey_e);
00578 }
00579 }
00580
00581 ctxt->pingTimeoutId = 0;
00582 return FALSE;
00583 }
00584
00585 gboolean connect_ping_freeze_ui(pingContext_t* ctxt)
00586 {
00587 g_return_val_if_fail(ctxt != NULL, FALSE);
00588
00589 CN_LOGPRINTF("return freeze = %d", ctxt->pingAborting);
00590 return ctxt->pingAborting;
00591 }
00592
00593 static gboolean delay_connect_ping_abort(gpointer data)
00594 {
00595 pingContext_t* ctxt = (pingContext_t*)data;
00596 g_return_val_if_fail(ctxt != NULL, FALSE);
00597
00598 gboolean ret = TRUE;
00599 gint profileIndex = connect_ping_get_pinging_profile_index(ctxt);
00600
00601
00602 if (!pingThread_stopped())
00603 {
00604 CN_LOGPRINTF("call me later...");
00605 }
00606 else
00607 {
00608 ctxt->pingAborting = FALSE;
00609
00610 if (ctxt->mode != backgroundPing_e && ctxt->ui_update_status)
00611 {
00612
00613 ctxt->ui_update_status(profileIndex, pingAborted_e);
00614 }
00615
00616
00617 ctxt->pingServer = FALSE;
00618 connect_ping_set_pinging_profile_index(ctxt, -1);
00619
00620 ret = FALSE;
00621 }
00622
00623 return ret;
00624 }
00625
00626
00627 void connect_ping_abort (pingContext_t* ctxt)
00628 {
00629 CN_LOGPRINTF ("entry");
00630
00631 g_return_if_fail(ctxt != NULL);
00632
00633 if (ctxt->pingServer)
00634 {
00635 CN_WARNPRINTF("Abort pinging...");
00636
00637 gint profileIndex = connect_ping_get_pinging_profile_index(ctxt);
00638
00639 if (ctxt->mode != backgroundPing_e && ctxt->ui_update_status)
00640 {
00641
00642 ctxt->ui_update_status(profileIndex, pingAborting_e);
00643 }
00644
00645
00646 pingThread_stop();
00647
00648
00649 ctxt->pingAborting = TRUE;
00650 g_timeout_add(200, delay_connect_ping_abort, (gpointer)ctxt);
00651 }
00652 }
00653
00654
00655 static void connect_ping_done (pingContext_t* ctxt, pingStatus_t result)
00656 {
00657 GtkWidget *pingStatus;
00658 int profileIndex, startIdx;
00659 gboolean same = TRUE;
00660 gboolean selected;
00661
00662 CN_LOGPRINTF ("entry");
00663
00664 g_return_if_fail(ctxt != NULL);
00665
00666 if (ctxt->pingServer)
00667 {
00668
00669 pingStatus = ctxt->pingParms->pingStatus;
00670 gtk_label_set_text(GTK_LABEL(pingStatus), "ping-idle");
00671
00672
00673 free_network_pc_settings(ctxt->pingParms);
00674
00675
00676 profileIndex = connect_ping_get_pinging_profile_index(ctxt);
00677 g_return_if_fail(profileIndex != -1);
00678
00679 if (ctxt->mode != backgroundPing_e && ctxt->ui_update_status)
00680 {
00681 ctxt->ui_update_status(profileIndex, result);
00682 }
00683
00684 if (result == pingSucceeded_e)
00685 {
00686 ctxt->pingServer = FALSE;
00687 connect_ping_set_pinging_profile_index(ctxt, -1);
00688
00689 if (ctxt->mode != backgroundPing_e
00690 && ctxt->ui_unselect_all_profile)
00691 {
00692 ctxt->ui_unselect_all_profile();
00693 }
00694
00695 if (ctxt->on_connected)
00696 {
00697 ctxt->on_connected(profileIndex);
00698 }
00699 }
00700 else
00701 {
00702 if (ctxt->select_next_regprofile
00703 && !ctxt->select_next_regprofile(profileIndex))
00704 {
00705
00706 selected = connect_ping_select_next_profile(ctxt);
00707 if (!selected)
00708 {
00709 connect_ping_select_first_profile(ctxt);
00710 }
00711 same = FALSE;
00712 }
00713
00714
00715 profileIndex = connect_ping_get_selected_profile_index(ctxt);
00716 CN_LOGPRINTF ("entry: selected profile [%d]", profileIndex);
00717
00718 if (ctxt->mode != backgroundPing_e)
00719 {
00720 if (same && ctxt->ui_display_settings)
00721 {
00722 ctxt->ui_display_settings(profileIndex, TRUE);
00723 }
00724
00725 if (ctxt->ui_select_profile)
00726 {
00727 ctxt->ui_select_profile(profileIndex);
00728 }
00729 }
00730
00731
00732
00733 ctxt->pingServer = FALSE;
00734 connect_ping_set_pinging_profile_index(ctxt, -1);
00735
00736 startIdx = connect_ping_get_pinging_profile_index_start(ctxt);
00737 if (same || (profileIndex != startIdx))
00738 {
00739 if (ctxt->mode == backgroundPing_e)
00740 {
00741 connect_ping(ctxt, FALSE);
00742 }
00743 else
00744 {
00745 connect_ping(ctxt, TRUE);
00746 }
00747 }
00748 else
00749 {
00750 CN_WARNPRINTF ("All profiles done, stop connecting");
00751 connect_ping_unselect_profile (ctxt);
00752
00753 if (ctxt->mode != backgroundPing_e
00754 && ctxt->ui_unselect_all_profile)
00755 {
00756 ctxt->ui_unselect_all_profile();
00757 }
00758
00759 if (ctxt->on_failed_all)
00760 {
00761 ctxt->on_failed_all();
00762 }
00763 }
00764 }
00765 }
00766 }
00767
00768
00769 static void on_ping_status_changed (GObject * object,
00770 GParamSpec * arg1,
00771 gpointer data)
00772 {
00773 CN_LOGPRINTF ("entry");
00774
00775 pingContext_t* ctxt = (pingContext_t*)data;
00776 g_return_if_fail(ctxt != NULL);
00777
00778 const char *name = arg1->name;
00779 g_return_if_fail (strcmp (name, "label") == 0);
00780
00781 const char *pingStatus;
00782 pingStatus = gtk_label_get_text (GTK_LABEL (object));
00783 CN_LOGPRINTF ("pingStatus [%s]\n", pingStatus);
00784
00785 if (strcmp(pingStatus, "ping-done-ok") == 0)
00786 {
00787 connect_ping_done(ctxt, pingSucceeded_e);
00788 }
00789 else if (strcmp(pingStatus, "ping-done-error") == 0)
00790 {
00791 connect_ping_done(ctxt, pingFailed_e);
00792 }
00793 else if (strcmp(pingStatus, "ping-pcshare-error") == 0)
00794 {
00795 connect_ping_done (ctxt, pingFailedPCShare_e);
00796 }
00797 else if (strcmp(pingStatus, "ping-network-error") == 0)
00798 {
00799 connect_ping_done(ctxt, pingFailedNetwork_e);
00800 }
00801 else if(strcmp (pingStatus, "ping-done-abort") == 0)
00802 {
00803 CN_LOGPRINTF("ping-done-abort");
00804 }
00805 else
00806 {
00807
00808 ;
00809 }
00810 }
00811
00812