コード例 #1
0
ファイル: iwmxsdk.c プロジェクト: aelarabawy/NetworkManager
/*
 * Initialize the WiMAX API, register with it, setup callbacks
 *
 */
static int iwmx_sdk_setup(struct wmxsdk *wmxsdk)
{
	int result, status;

	WIMAX_API_RET r;

	char errstr[512];
	UINT32 errstr_size = sizeof(errstr);

	result = -ENFILE;

	/* device_id initialized by iwmx_sdk_dev_add */

	r = WiMaxDeviceOpen(&wmxsdk->device_id);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot open device: %d (%s)", r, errstr);
		goto error_wimaxdeviceopen;
	}

	/*
	 * We scan in auto mode (in the background)
	 *
	 * Otherwise is messy -- if we have NetworkManager triggering a scan
	 * when we call iwmx_nm_scan() -> iwmx_sdk_scan(), most of the
	 * times that causes a race condition when the UI asks for a
	 * scan right before displaying the network menu. As there is
	 * no way to cancel an ongoing scan before connecting, we are
	 * stuck. So we do auto bg and have iwmx_sdk_scan() just return
	 * the current network list.
	 */
	r = SetConnectionMode(&wmxsdk->device_id,
			      WIMAX_API_CONNECTION_AUTO_SCAN_MANUAL_CONNECT);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot set connectin mode to manual: %d (%s)", r, errstr);
		goto error_connection_mode;
	}

	r = SubscribeControlPowerManagement(&wmxsdk->device_id,
					    __iwmx_sdk_rf_state_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to radio change events: %u (%s)", r, errstr);
		result = -EIO;
		goto error_subscribe_rf_state;
	}

	r = SubscribeDeviceStatusChange(&wmxsdk->device_id,
					__iwmx_sdk_state_change_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to state chaneg events: %d (%s)", r, errstr);
		goto error_subscribe_state_change;
	}

	r = SubscribeNetworkSearchWideScanEx(&wmxsdk->device_id,
					     __iwmx_sdk_wide_scan_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to wide scan events: %d (%s)", r, errstr);
		goto error_subscribe_wide_scan;
	}
	r = SubscribeNetworkSearchEx(&wmxsdk->device_id, __iwmx_sdk_scan_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to scan events: %d (%s)", r, errstr);
		goto error_subscribe_scan;
	}

	r = SubscribeConnectToNetwork(&wmxsdk->device_id,
				      __iwmx_sdk_connect_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to connect events: %d (%s)", r, errstr);
		goto error_subscribe_connect;
	}

	r = SubscribeDisconnectToNetwork(&wmxsdk->device_id,
					 __iwmx_sdk_disconnect_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to disconnect events: %d (%s)", r, errstr);
		goto error_subscribe_disconnect;
	}

	r = SubscribeMediaStatusUpdate(&wmxsdk->device_id, __iwmx_sdk_media_status_update_cb);
	if (r != WIMAX_API_RET_SUCCESS) {
		GetErrorString(&wmxsdk->device_id, r, errstr, &errstr_size);
		nm_log_err(LOGD_WIMAX, "wmxsdk: Cannot subscribe to media status events: %d (%s)", r, errstr);
		goto error_subscribe_media_status;
	}

	status = iwmx_sdk_get_device_status(wmxsdk);
	if ((int) status < 0)
		status = WIMAX_API_DEVICE_STATUS_UnInitialized;

	g_mutex_lock(&wmxsdk->status_mutex);
	wmxsdk->status = status;
	g_mutex_unlock(&wmxsdk->status_mutex);

	_schedule_state_change(wmxsdk,
	                       status,
	                       WIMAX_API_DEVICE_STATUS_UnInitialized,
	                       WIMAX_API_STATUS_REASON_Normal,
	                       WIMAX_API_DEVICE_CONNECTION_PROGRESS_Ranging);

	return 0;

	UnsubscribeMediaStatusUpdate(&wmxsdk->device_id);
error_subscribe_media_status:
	UnsubscribeDisconnectToNetwork(&wmxsdk->device_id);
error_subscribe_disconnect:
	UnsubscribeConnectToNetwork(&wmxsdk->device_id);
error_subscribe_connect:
	UnsubscribeNetworkSearchEx(&wmxsdk->device_id);
error_subscribe_scan:
	UnsubscribeNetworkSearchWideScanEx(&wmxsdk->device_id);
error_subscribe_wide_scan:
	UnsubscribeDeviceStatusChange(&wmxsdk->device_id);
error_subscribe_state_change:
	UnsubscribeControlPowerManagement(&wmxsdk->device_id);
error_subscribe_rf_state:
error_connection_mode:
	WiMaxDeviceClose(&wmxsdk->device_id);
error_wimaxdeviceopen:
	return result;
}
コード例 #2
0
ファイル: wimaxcu_scan_op.c プロジェクト: tony2001/wimax-ns
int wimaxcu_get_network_list_ex(WIMAX_API_DEVICE_ID_P p_device_id, CMD_ARGS scan_mode)
{
    int ret = 0;
    WIMAX_API_RET wmxStatus;
    int time_out = 0;
    WIMAX_API_CONNECTION_MODE connectMode;
    WIMAX_API_DEVICE_STATUS DeviceStatus;
    WIMAX_API_CONNECTION_PROGRESS_INFO ConnectionProgressInfo;
    WIMAX_API_NSP_INFO_EX_P pNspInfo;

    UINT32 numOfNSPs = 20;
    int i = 0;

    // initialize the variable as No Networks found
    g_noNetworksFound = 1;
    g_searchProgress = 0;
    // get the device status
    wmxStatus =
        GetDeviceStatus(p_device_id, &DeviceStatus,
                        &ConnectionProgressInfo);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }

    switch (DeviceStatus) {
    case WIMAX_API_DEVICE_STATUS_UnInitialized: /**<  Device is uninitialized */
        printf("ERROR: Device not Initialized\n");
        return 2;
    case WIMAX_API_DEVICE_STATUS_RF_OFF_HW_SW:  /**<  Device RF Off(both H/W and S/W) */
        printf
        ("WARNING: HW and SW Radios are OFF.\nPlease turn ON the HW and SW Radios to perform a scan.\n");
        return 2;
    case WIMAX_API_DEVICE_STATUS_RF_OFF_HW:     /**<  Device RF Off(via H/W switch) */
        printf
        ("WARNING: HW Radio is OFF.\nPlease turn ON the HW Radio to perform a scan.\n");
        return 2;
    case WIMAX_API_DEVICE_STATUS_RF_OFF_SW:     /**<  Device RF Off(via S/W switch) */
        printf
        ("WARNING: SW Radio is OFF.\nPlease turn ON the SW Radio to perform a scan.\n");
        return 2;
    case WIMAX_API_DEVICE_STATUS_Ready:	     /**<  Device is ready */
    case WIMAX_API_DEVICE_STATUS_Scanning:	     /**<  Device is scanning */
        break;
    case WIMAX_API_DEVICE_STATUS_Connecting:    /**<  Connection in progress */
        printf("WARNING: Connection is in progress\n");
        return 2;
    case WIMAX_API_DEVICE_STATUS_Data_Connected:	/**<  Layer 2 connected */
        printf
        ("WARNING: Connection already established!\nPlease disconnect, before attempting to scan.\n");
        return 2;
    default:
        printf("ERROR: Device status Unknown.\n");
        return 2;
    }


    wmxStatus = SubscribeRfTaken(p_device_id, &ind_rf_taken_cb);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }
    wmxStatus = GetConnectionMode(p_device_id, &connectMode);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }

    if (connectMode == WIMAX_API_CONNECTION_MANUAL_SCAN_MANUAL_CONNECT
            && DeviceStatus == WIMAX_API_DEVICE_STATUS_Scanning) {
        printf
        ("WARNING: Scanning is in progress\nPlease wait for the current scan to complete.\n");
        return 2;
    }

    if (scan_mode == CMD_SCAN_ARG_PREFERRED) {

        wmxStatus =
            SubscribeNetworkSearchEx(p_device_id,
                                     &ind_network_search_cb_ex);
        if (WIMAX_API_RET_SUCCESS != wmxStatus) {
            PrintWmxStatus(wmxStatus);
            return 2;
        }
        /* In Manual mode always scan do not read the cache as we don't know when cache was updated */
        if (connectMode !=
                WIMAX_API_CONNECTION_MANUAL_SCAN_MANUAL_CONNECT) {
            pNspInfo =
                (WIMAX_API_NSP_INFO_EX_P) malloc(MAX_LEN *
                                                 sizeof
                                                 (WIMAX_API_NSP_INFO_EX));
            memset(pNspInfo, 0,
                   sizeof(WIMAX_API_NSP_INFO_EX) * MAX_LEN);
            wmxStatus =
                GetNetworkListEx(p_device_id, pNspInfo, &numOfNSPs);
            if (WIMAX_API_RET_SUCCESS != wmxStatus) {
                PrintWmxStatus(wmxStatus);
                /*
                   If the Device is already scanning, it won't allow one more AppSrv will reject it
                   with the Operation Falied error
                 */
                free(pNspInfo);
                return 1;
            }

            if (numOfNSPs != 0) {
                for (i = 0; i < numOfNSPs; i++) {
                    printf("\nNetwork found.\n");
                    PrintNSPInfoEx(&pNspInfo[i]);
                    if (wimaxcu_is_network_activated_ex
                            (p_device_id, &pNspInfo[i]))
                        printf("\tActivated\n");
                    else
                        printf("\tNot Activated\n");
                }
                return 0;
            }
        }

        wmxStatus = CmdNetworkSearch(p_device_id);

        if (WIMAX_API_RET_SUCCESS != wmxStatus) {
            PrintWmxStatus(wmxStatus);
            if ((DeviceStatus == WIMAX_API_DEVICE_STATUS_Scanning)
                    && (wmxStatus == WIMAX_API_RET_FAILED)) {
                printf
                ("WARNING: Scanning is in progress\nPlease wait for the current scan to complete.\n");
            }
            return 1;
        }

        printf("Scanning %2d%% Done ", g_searchProgress);
        fflush(stdout);
        do {
            if (wmxcu_sem_timeout(&g_semConnectionUtility, 5 * 1000)
                    == 1) {
                time_out++;
                //if (pthread_mutex_trylock(&g_console_owner_mutex) == 0) {
                printf("\r");
                printf("Scanning %2d%% Done ",
                       g_searchProgress);
                // printf("Scanning %2d% Done [", g_searchProgress);

                for (i = 0; i <= time_out; i++) {
                    printf("=");
                }
                printf("-");
//                                      for(i = 0; i<=SCAN_TIMEOUT_IN_10SEC_ITREATIONS - time_out; i++)
//                                      {
//                                              printf(" ");
//                                      }
//                                      printf("]");
                fflush(stdout);
                //pthread_mutex_unlock(&g_console_owner_mutex);
                //}
                wmxStatus =
                    GetDeviceStatus(p_device_id, &DeviceStatus,
                                    &ConnectionProgressInfo);
                if (WIMAX_API_RET_SUCCESS != wmxStatus) {
                    PrintWmxStatus(wmxStatus);
                    return 2;
                }

                if (DeviceStatus == WIMAX_API_DEVICE_STATUS_Ready) {
                    if (g_noNetworksFound == 1)
                        printf("\nNo networks found.\n");
                    break;
                }

                if (time_out > SCAN_TIMEOUT_IN_10SEC_ITREATIONS) {
                    if (g_noNetworksFound == 1) {
                        printf
                        ("\nNo networks found.\n");
                    } else {
                        printf
                        ("\nScan Operation timeout.\n");
                        // As Scan operation timed out
                        ret = 1;
                    }
                    break;
                }
            } else {
                break;
            }
        } while (1);

    } else if (scan_mode == CMD_SCAN_ARG_WIDE) {

        wmxStatus = SubscribeNetworkSearchWideScanEx(p_device_id,
                    &ind_network_search_wide_scan_cb_ex);
        if (WIMAX_API_RET_SUCCESS != wmxStatus) {
            PrintWmxStatus(wmxStatus);
            return 2;
        }
        // Display the warning message:
        printf("WARNING: Wide scan may take upto 2 minutes... \n");
        wmxStatus = CmdNetworkSearchWideScan(p_device_id);
        if (WIMAX_API_RET_SUCCESS != wmxStatus) {
            PrintWmxStatus(wmxStatus);
            /*
               If the Device is already scanning, it won't allow one more AppSrv will reject it
               with the Operation Falied wrror
             */
            if ((DeviceStatus == WIMAX_API_DEVICE_STATUS_Scanning)
                    && (wmxStatus == WIMAX_API_RET_FAILED)) {
                printf
                ("WARNING: Scanning is in progress\nPlease wait for the current scan to complete.\n ");

            }
            return 1;
        }

        if (wmxcu_sem_timeout(&g_semConnectionUtility, 120 * 1000) == 1)
            printf("No networks found.\n");
    }
    wmxStatus = UnsubscribeRfTaken(p_device_id);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }
    wmxStatus = UnsubscribeNetworkSearchEx(p_device_id);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }
    wmxStatus = UnsubscribeNetworkSearchWideScanEx(p_device_id);
    if (WIMAX_API_RET_SUCCESS != wmxStatus) {
        PrintWmxStatus(wmxStatus);
        return 2;
    }
    return ret;
}