connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/pccsnative/PCCSConnection.java
changeset 1037 7261015324d2
parent 1025 0970d96e01ad
child 1046 a176b8031313
equal deleted inserted replaced
1036:b1909e47f4af 1037:7261015324d2
    27 
    27 
    28 import org.eclipse.cdt.utils.WindowsRegistry;
    28 import org.eclipse.cdt.utils.WindowsRegistry;
    29 import org.eclipse.core.runtime.CoreException;
    29 import org.eclipse.core.runtime.CoreException;
    30 import org.eclipse.core.runtime.IStatus;
    30 import org.eclipse.core.runtime.IStatus;
    31 import org.eclipse.core.runtime.Status;
    31 import org.eclipse.core.runtime.Status;
    32 import org.eclipse.core.runtime.preferences.InstanceScope;
    32 
    33 import org.eclipse.jface.dialogs.IDialogConstants;
    33 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    34 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
       
    35 import org.eclipse.swt.widgets.Display;
       
    36 import org.osgi.service.prefs.BackingStoreException;
       
    37 
       
    38 import com.nokia.carbide.remoteconnections.discovery.pccs.Activator;
    34 import com.nokia.carbide.remoteconnections.discovery.pccs.Activator;
    39 import com.nokia.carbide.remoteconnections.discovery.pccs.Messages;
    35 import com.nokia.carbide.remoteconnections.discovery.pccs.Messages;
    40 import com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative.IConnAPILibrary.IConnAPIDeviceCallback;
    36 import com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative.IConnAPILibrary.IConnAPIDeviceCallback;
    41 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
       
    42 
       
    43 import com.sun.jna.Pointer;
    37 import com.sun.jna.Pointer;
    44 import com.sun.jna.WString;
    38 import com.sun.jna.WString;
    45 import com.sun.jna.ptr.IntByReference;
    39 import com.sun.jna.ptr.IntByReference;
    46 import com.sun.jna.ptr.ShortByReference;
    40 import com.sun.jna.ptr.ShortByReference;
    47 
    41 
    48 public class PCCSConnection {
    42 public class PCCSConnection {
    49 
    43 
    50 	private static final String NOT_KNOWN = "not known"; //$NON-NLS-1$ // used for all string structure elements that come back from PCCS as null
    44 	private static final String NOT_KNOWN = "not known"; //$NON-NLS-1$ // used for all string structure elements that come back from PCCS as null
    51 	private boolean DEBUG = false;
    45 	private boolean DEBUG = false;
    52 	private String DONT_ASK_AGAIN_KEY = "DontAskAgain"; //$NON-NLS-1$
       
    53 	
    46 	
    54 	/**
       
    55 	 *
       
    56 	 */
       
    57 	public class DeviceNotificationCallback implements IConnAPIDeviceCallback {
    47 	public class DeviceNotificationCallback implements IConnAPIDeviceCallback {
    58 
    48 
    59 		/* (non-Javadoc)
    49 		/* (non-Javadoc)
    60 		 * @see com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative.IConnAPILibrary.IConnAPIDeviceCallback#invoke(int, com.sun.jna.ptr.ShortByReference)
    50 		 * @see com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative.IConnAPILibrary.IConnAPIDeviceCallback#invoke(int, com.sun.jna.ptr.ShortByReference)
    61 		 */
    51 		 */
   112 	private static final int DMAPI_VERSION = DMAPIDefinitions.DMAPI_VERSION_34;
   102 	private static final int DMAPI_VERSION = DMAPIDefinitions.DMAPI_VERSION_34;
   113 	
   103 	
   114 	private APIHANDLE dmHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   104 	private APIHANDLE dmHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   115 	private APIHANDLE mcHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   105 	private APIHANDLE mcHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   116 	private APIHANDLE upHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   106 	private APIHANDLE upHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   117 	private boolean dontAskAgain;
       
   118 	private boolean doSwitch;
       
   119 	
   107 	
   120 	private Collection<String> noSwitchConnections = new ArrayList<String>();
   108 	private Collection<String> noSwitchConnections = new ArrayList<String>();
   121 	
   109 	
   122 	private void storeDontAskAgain() {
       
   123 		Activator.getDefault().getPreferenceStore().setValue(DONT_ASK_AGAIN_KEY, true);
       
   124 		try {
       
   125 			new InstanceScope().getNode(Activator.PLUGIN_ID).flush();
       
   126 		} catch (BackingStoreException e) {
       
   127 			Activator.logError(e);
       
   128 		}
       
   129 	}
       
   130 	private boolean dontAskAgain() {
       
   131 		return Activator.getDefault().getPreferenceStore().getBoolean(DONT_ASK_AGAIN_KEY);
       
   132 	}
       
   133 
       
   134 	public PCCSConnection() {
   110 	public PCCSConnection() {
   135 	}
   111 	}
   136 
   112 
   137 	public void open() throws CoreException {
   113 	public void open() throws CoreException {
   138 		if (library == null) {
   114 		if (library == null) {
   350 		terminateDMAPI();
   326 		terminateDMAPI();
   351 	}
   327 	}
   352 	
   328 	
   353 	private void terminateDMAPI() {
   329 	private void terminateDMAPI() {
   354     	// Terminate Device management API
   330     	// Terminate Device management API
   355     	int dwResult = library.DMAPI_Terminate(null);
   331     	/*int dwResult =*/ library.DMAPI_Terminate(null);
   356 		if (DEBUG) System.out.println("terminateDMAPI"); //$NON-NLS-1$
   332 		if (DEBUG) System.out.println("terminateDMAPI"); //$NON-NLS-1$
   357 	}
   333 	}
   358 	private void stopDMNotifications(APIHANDLE handle) {
   334 	private void stopDMNotifications(APIHANDLE handle) {
   359 		int dwResult = PCCSErrors.CONA_OK;
   335 //		int dwResult = PCCSErrors.CONA_OK;
   360 		if (!APIHANDLE.INVALID_HANDLE_VALUE.equals(handle)) {
   336 		if (!APIHANDLE.INVALID_HANDLE_VALUE.equals(handle)) {
   361 			// unregister callback
   337 			// unregister callback
   362 			dwResult = library.CONARegisterNotifyCallback(handle, PCCSTypeDefinitions.API_UNREGISTER, pfnCallback);
   338 			/*dwResult =*/ library.CONARegisterNotifyCallback(handle, PCCSTypeDefinitions.API_UNREGISTER, pfnCallback);
   363 		}		
   339 		}		
   364 		if (DEBUG) System.out.println("stopDMNotifications"); //$NON-NLS-1$
   340 		if (DEBUG) System.out.println("stopDMNotifications"); //$NON-NLS-1$
   365 	}
   341 	}
   366 	/**
   342 	/**
   367 	 * Closes the DMAPI.
   343 	 * Closes the DMAPI.
   377 		}
   353 		}
   378 		return handle;
   354 		return handle;
   379 	}
   355 	}
   380 
   356 
   381 	private void closeMCAPI() {
   357 	private void closeMCAPI() {
   382 		int dwResult = PCCSErrors.CONA_OK;
   358 //		int dwResult = PCCSErrors.CONA_OK;
   383 		if (!APIHANDLE.INVALID_HANDLE_VALUE.equals(mcHandle)) {
   359 		if (!APIHANDLE.INVALID_HANDLE_VALUE.equals(mcHandle)) {
   384 			// close DM connection
   360 			// close DM connection
   385 			dwResult = library.CONACloseDM(mcHandle);
   361 			/*dwResult =*/ library.CONACloseDM(mcHandle);
   386 			mcHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   362 			mcHandle = APIHANDLE.INVALID_HANDLE_VALUE;
   387         	// Terminate Device management API
   363         	// Terminate Device management API
   388     		dwResult = library.MCAPI_Terminate(null);
   364     		/*dwResult =*/ library.MCAPI_Terminate(null);
   389 		}
   365 		}
   390 		if (DEBUG) System.out.println("closeMCAPI----\n"); //$NON-NLS-1$
   366 		if (DEBUG) System.out.println("closeMCAPI----\n"); //$NON-NLS-1$
   391 	}
   367 	}
   392 	
   368 	
   393 	private void closeUPAPI() {
   369 	private void closeUPAPI() {
   766 		if (personality.supportedPersonalities.containsKey(new Integer(UPAPIDefinitions.UPAPI_PERSONALITY_CODE_OVI_SUITE))) {
   742 		if (personality.supportedPersonalities.containsKey(new Integer(UPAPIDefinitions.UPAPI_PERSONALITY_CODE_OVI_SUITE))) {
   767 			goodCode = UPAPIDefinitions.UPAPI_PERSONALITY_CODE_OVI_SUITE;
   743 			goodCode = UPAPIDefinitions.UPAPI_PERSONALITY_CODE_OVI_SUITE;
   768 		}
   744 		}
   769 		String goodDesc = personality.supportedPersonalities.get(new Integer(goodCode));
   745 		String goodDesc = personality.supportedPersonalities.get(new Integer(goodCode));
   770 
   746 
   771 		if (dontAskAgain()) {
       
   772 			if (DEBUG) System.out.println("isGoodUSBPersonality: dont ask = true, return false"); //$NON-NLS-1$
       
   773 			String fmt = Messages.PCCSConnection_Bad_Personality_DontSwitch_Warning1 +
       
   774 			Messages.PCCSConnection_Bad_Personality_DontSwitch_Warning2;
       
   775 			String message = MessageFormat.format(fmt, device.friendlyName, personality.currentPersonalityDescription, goodDesc);
       
   776 			Activator.logMessage(message, IStatus.WARNING);
       
   777 			return false;
       
   778 		}
       
   779 
       
   780 		if (!noSwitchConnectionsContains(personality.deviceID))
   747 		if (!noSwitchConnectionsContains(personality.deviceID))
   781 			// ask to switch
   748 			// ask to switch
   782 			askToSwitchPersonality(device, personality, goodCode, goodDesc);
   749 			askToSwitchPersonality(device, personality, goodCode, goodDesc);
   783 
   750 
   784 		// was bad personality - if we switched, it will be good next notification
   751 		// was bad personality - if we switched, it will be good next notification
   810 	private void forgetNoSwitchConnections(String deviceID) {
   777 	private void forgetNoSwitchConnections(String deviceID) {
   811 		if (noSwitchConnections.isEmpty()) {
   778 		if (noSwitchConnections.isEmpty()) {
   812 			if (DEBUG) System.out.println("forgetNoSwitchConnections empty");
   779 			if (DEBUG) System.out.println("forgetNoSwitchConnections empty");
   813 			return;
   780 			return;
   814 		}
   781 		}
   815 		for (String id : new ArrayList<String>(noSwitchConnections)) {
   782 		if (DEBUG) System.out.println("forgetNoSwitchConnections: " + deviceID);
   816 			if (id.equals(deviceID)) {
   783 		noSwitchConnections.remove(deviceID);
   817 				if (DEBUG) System.out.println("forgetNoSwitchConnections: " + deviceID);
   784 	}
   818 				noSwitchConnections.remove(id);
   785 	
   819 			}
       
   820 		}
       
   821 	}
       
   822 	private void askToSwitchPersonality(final DeviceInfo device,
   786 	private void askToSwitchPersonality(final DeviceInfo device,
   823 			final DeviceUSBPersonalityInfo personality, final int goodCode, final String goodDesc) {
   787 			final DeviceUSBPersonalityInfo personality, final int goodCode, final String goodDesc) {
   824 		
   788 		
   825 		doSwitch = false;
   789 		// remember this device so we don't ask again unless it changes personality or is reconnected
   826 		Display.getDefault().syncExec(
   790 		rememberNoSwitchConnections(personality.deviceID);
   827 			new Runnable() {
   791 
   828 
   792 		String message = MessageFormat.format(
   829 				public void run() {
   793 				"''{0}'' is currently in {1} mode\nand must be in {2} mode to be usable in Carbide.",
   830 					// ask the user and switch
   794 				device.friendlyName,
   831 					String fmt = Messages.PCCSConnection_Switch_Message1 +
   795 				personality.currentPersonalityDescription,
   832 					Messages.PCCSConnection_Swtich_Message2 +
   796 				goodDesc);
   833 					Messages.PCCSConnection_Switch_Message3 +
   797 		IStatus status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, message);
   834 					Messages.PCCSConnection_Switch_Message4;
   798 		
   835 					String message = MessageFormat.format(fmt,
   799 		String prompt = MessageFormat.format("Switch to {0} mode now.", goodDesc);
   836 							device.friendlyName,
   800 		
   837 							personality.currentPersonalityDescription,
   801 		RemoteConnectionsActivator.getStatusDisplay().displayStatusWithAction(status, prompt, new Runnable() {
   838 							goodDesc);
   802 			public void run() {
   839 
   803 				WString pstrDeviceId = new WString(personality.deviceID);
   840 					MessageDialogWithToggle dlg = MessageDialogWithToggle.openYesNoQuestion(
   804 				int dwResult = library.UPAPI_SetPersonality(upHandle, pstrDeviceId, goodCode);
   841 							WorkbenchUtils.getSafeShell(), 
   805 				if (dwResult != PCCSErrors.CONA_OK) {
   842 							Messages.PCCSConnection_Switch_Message_Title, 
   806 					forgetNoSwitchConnections(personality.deviceID);
   843 							message, 
   807 					if (DEBUG)
   844 							Messages.PCCSConnection_DontAsk_CheckText, 
   808 						System.out.printf("UPAPI_SetPersonality failed: %x\n", dwResult); //$NON-NLS-1$
   845 							false, 
   809 				}
   846 							null, 
   810 			}
   847 							null);
   811 		});
   848 					dontAskAgain = dlg.getToggleState();
   812 
   849 					doSwitch = (dlg.getReturnCode() == IDialogConstants.YES_ID);
       
   850 					
       
   851 				}
       
   852 			}
       
   853 		);
       
   854 		if (doSwitch) {
       
   855 			WString pstrDeviceId = new WString(personality.deviceID);
       
   856 			int dwResult = library.UPAPI_SetPersonality(upHandle, pstrDeviceId, goodCode);
       
   857 			if (dwResult != PCCSErrors.CONA_OK) {
       
   858 				if (DEBUG)
       
   859 					System.out.printf("UPAPI_SetPersonality failed: %x\n", dwResult); //$NON-NLS-1$
       
   860 			}
       
   861 		} else {
       
   862 			// remember this device so we don't ask again unless it changes personality or is reconnected
       
   863 			rememberNoSwitchConnections(personality.deviceID);
       
   864 			// also log an info message to error log regarding this
       
   865 			String fmt = Messages.PCCSConnection_Switch_Message1 + Messages.PCCSConnection_Swtich_Message2;
       
   866 			String message = MessageFormat.format(fmt, 	device.friendlyName,
       
   867 							personality.currentPersonalityDescription,
       
   868 							goodDesc);
       
   869 			Activator.logMessage(message, IStatus.INFO);
       
   870 		}
       
   871 		// store don't ask again only if selected
       
   872 		if (dontAskAgain)
       
   873 			storeDontAskAgain();
       
   874 	}
   813 	}
   875 
   814 
   876 	/**
   815 	/**
   877 	 * Device is bad personality and user said 'no' to switch (we won't ask again for this device unless it is reconnected or now in a good mode)
   816 	 * Device is bad personality and user said 'no' to switch (we won't ask again for this device unless it is reconnected or now in a good mode)
   878 	 * 
   817 	 * 
  1015 		if (DEBUG) System.out.printf("current Desc found: %s\n", deviceInfo.currentPersonalityDescription); //$NON-NLS-1$
   954 		if (DEBUG) System.out.printf("current Desc found: %s\n", deviceInfo.currentPersonalityDescription); //$NON-NLS-1$
  1016 		return apiError;
   955 		return apiError;
  1017 	}
   956 	}
  1018 
   957 
  1019 
   958 
  1020 	private String getBTComPort(String address) {
   959 //	private String getBTComPort(String address) {
  1021 		// TODO Auto-generated method stub
   960 //		// TODO Auto-generated method stub
  1022 		return null;
   961 //		return null;
  1023 	}
   962 //	}
  1024 
   963 
  1025 	public String getUSBComPort(String address) {
   964 	public String getUSBComPort(String address) {
  1026 		if (address != null && address.length() > 0) {
   965 		if (address != null && address.length() > 0) {
  1027 			String id, key, subKey, parentIdPrefix = null, portName;
   966 			String id, key, subKey, parentIdPrefix = null, portName;
  1028 
   967