connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/pccsnative/PCCSConnection.java
changeset 870 cdb6d745cb05
parent 868 089762ea5b6d
child 873 0b6536bf05fd
equal deleted inserted replaced
868:089762ea5b6d 870:cdb6d745cb05
    14 * Description: 
    14 * Description: 
    15 *
    15 *
    16 */
    16 */
    17 package com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative;
    17 package com.nokia.carbide.remoteconnections.discovery.pccs.pccsnative;
    18 
    18 
       
    19 import java.nio.IntBuffer;
    19 import java.util.Collection;
    20 import java.util.Collection;
    20 import java.util.Iterator;
    21 import java.util.Iterator;
    21 import java.util.LinkedList;
    22 import java.util.LinkedList;
    22 
    23 
    23 import org.eclipse.cdt.utils.WindowsRegistry;
    24 import org.eclipse.cdt.utils.WindowsRegistry;
    97 		}
    98 		}
    98 	}
    99 	}
    99 
   100 
   100 	private static final Collection<DeviceEventListener> listeners = new LinkedList<DeviceEventListener>();
   101 	private static final Collection<DeviceEventListener> listeners = new LinkedList<DeviceEventListener>();
   101 	private IConnAPILibrary library;
   102 	private IConnAPILibrary library;
   102 	private LPDMHANDLE dmHandle = new LPDMHANDLE();
   103 	private LPAPIHANDLE dmHandle = new LPAPIHANDLE();
   103 	private DeviceNotificationCallback pfnCallback = new DeviceNotificationCallback();
   104 	private DeviceNotificationCallback pfnCallback = new DeviceNotificationCallback();
   104 	public static final int PCCS_NOT_FOUND = 1;
   105 	public static final int PCCS_NOT_FOUND = 1;
   105 	public static final int PCCS_WRONG_VERSION = 2;
   106 	public static final int PCCS_WRONG_VERSION = 2;
   106 	
   107 	
   107 	private LPMCHANDLE mcHandle = new LPMCHANDLE();
   108 	private LPAPIHANDLE mcHandle = new LPAPIHANDLE();
   108 	
   109 	
   109 	/**
   110 	/**
   110 	 * 
   111 	 * 
   111 	 */
   112 	 */
   112 	public PCCSConnection() {
   113 	public PCCSConnection() {
   115 	public void open() throws CoreException {
   116 	public void open() throws CoreException {
   116 		if (library == null) {
   117 		if (library == null) {
   117 			library = ConnAPILibrary.getInstance();
   118 			library = ConnAPILibrary.getInstance();
   118 		}
   119 		}
   119 		loadDMAPI();
   120 		loadDMAPI();
   120 //		loadMCAPI(); TODO: not tested yet
   121 		loadMCAPI(); //TODO: not tested yet
   121 	}
   122 	}
   122 	
   123 	
   123 	/**
   124 	/**
   124 	 * Initializes the Device Management API (DMAPI) for use.
   125 	 * Initializes the Device Management API (DMAPI) for use.
   125 	 * 
   126 	 * 
   149 //    		String msg = String.format("PCCS CFAPI_Initialize API returned error on initialization %x", dwResult);
   150 //    		String msg = String.format("PCCS CFAPI_Initialize API returned error on initialization %x", dwResult);
   150 //    		throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, dwResult, msg, null));
   151 //    		throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, dwResult, msg, null));
   151 //    	}
   152 //    	}
   152     	
   153     	
   153     	// open a DM handle
   154     	// open a DM handle
   154     	dmHandle.setValue(DMHANDLE.INVALID_HANDLE_VALUE);
   155     	dmHandle.setValue(APIHANDLE.INVALID_HANDLE_VALUE);
   155     	dwResult = library.CONAOpenDM(dmHandle);
   156     	dwResult = library.CONAOpenDM(dmHandle);
   156     	if (dwResult != PCCSErrors.CONA_OK) {
   157     	if (dwResult != PCCSErrors.CONA_OK) {
   157     		library.DMAPI_Terminate(null);
   158     		library.DMAPI_Terminate(null);
   158     		String msg = String.format(Messages.PCCSConnection_PCCS_CONAOpenDM_Error, dwResult);
   159     		String msg = String.format(Messages.PCCSConnection_PCCS_CONAOpenDM_Error, dwResult);
   159     		if (dwResult == PCCSErrors.ECONA_NOT_ENOUGH_MEMORY) {
   160     		if (dwResult == PCCSErrors.ECONA_NOT_ENOUGH_MEMORY) {
   166     	dwResult = library.CONARegisterNotifyCallback(dmHandle.getValue(), PCCSTypeDefinitions.API_REGISTER, pfnCallback);
   167     	dwResult = library.CONARegisterNotifyCallback(dmHandle.getValue(), PCCSTypeDefinitions.API_REGISTER, pfnCallback);
   167     	if (dwResult != PCCSErrors.CONA_OK) {
   168     	if (dwResult != PCCSErrors.CONA_OK) {
   168 //    		System.out.printf("CONAOpenDM returned: %x\n", dwResult);
   169 //    		System.out.printf("CONAOpenDM returned: %x\n", dwResult);
   169     		library.DMAPI_Terminate(null);
   170     		library.DMAPI_Terminate(null);
   170     		library.CONACloseDM(dmHandle.getValue());
   171     		library.CONACloseDM(dmHandle.getValue());
   171         	dmHandle.setValue(DMHANDLE.INVALID_HANDLE_VALUE);
   172         	dmHandle.setValue(APIHANDLE.INVALID_HANDLE_VALUE);
   172 
   173 
   173         	String msg = String.format(Messages.PCCSConnection_PCCS_CONARegisterNotifyCallback_Error, dwResult);
   174         	String msg = String.format(Messages.PCCSConnection_PCCS_CONARegisterNotifyCallback_Error, dwResult);
   174     		if (dwResult == PCCSErrors.ECONA_INVALID_POINTER) {
   175     		if (dwResult == PCCSErrors.ECONA_INVALID_POINTER) {
   175     			msg = Messages.PCCSConnection_PCCS_CONARegisterNotifyCallback_Pointer_Error;
   176     			msg = Messages.PCCSConnection_PCCS_CONARegisterNotifyCallback_Pointer_Error;
   176     		}
   177     		}
   208 
   209 
   209 	public void close() throws CoreException {
   210 	public void close() throws CoreException {
   210 		if (library == null)
   211 		if (library == null)
   211 			return;
   212 			return;
   212 		
   213 		
   213 //		closeMCAPI(); TODO: not tested yet
   214 		closeMCAPI(); //TODO: not tested yet
   214 		closeDMAPI();
   215 		closeDMAPI();
   215 	}
   216 	}
   216 	
   217 	
   217 	/**
   218 	/**
   218 	 * Closes the DMAPI.
   219 	 * Closes the DMAPI.
   219 	 * @throws CoreException
   220 	 * @throws CoreException
   220 	 */
   221 	 */
   221 	private void closeDMAPI() throws CoreException {
   222 	private void closeDMAPI() throws CoreException {
   222 		int dwResult = PCCSErrors.CONA_OK;
   223 		int dwResult = PCCSErrors.CONA_OK;
   223 		if (dmHandle.getValue() != DMHANDLE.INVALID_HANDLE_VALUE) {
   224 		if (dmHandle.getValue() != APIHANDLE.INVALID_HANDLE_VALUE) {
   224 			// unregister callback
   225 			// unregister callback
   225 			dwResult = library.CONARegisterNotifyCallback(dmHandle.getValue(), PCCSTypeDefinitions.API_UNREGISTER, pfnCallback);
   226 			dwResult = library.CONARegisterNotifyCallback(dmHandle.getValue(), PCCSTypeDefinitions.API_UNREGISTER, pfnCallback);
   226 			// close DM connection
   227 			// close DM connection
   227 			dwResult = library.CONACloseDM(dmHandle.getValue());
   228 			dwResult = library.CONACloseDM(dmHandle.getValue());
   228         	dmHandle.setValue(DMHANDLE.INVALID_HANDLE_VALUE);
   229         	dmHandle.setValue(APIHANDLE.INVALID_HANDLE_VALUE);
   229         	// Terminate Common Functions API
   230         	// Terminate Common Functions API
   230 //        	dwResult = library.CFAPI_Terminate(null); unnecessary
   231 //        	dwResult = library.CFAPI_Terminate(null); unnecessary
   231         	// Terminate Device management API
   232         	// Terminate Device management API
   232         	dwResult = library.DMAPI_Terminate(null);
   233         	dwResult = library.DMAPI_Terminate(null);
   233 		}
   234 		}
   234 	}
   235 	}
   235 
   236 
   236 	private void closeMCAPI() {
   237 	private void closeMCAPI() {
   237 		int dwResult = PCCSErrors.CONA_OK;
   238 		int dwResult = PCCSErrors.CONA_OK;
   238 		if (mcHandle.getValue() != MCHANDLE.INVALID_HANDLE_VALUE) {
   239 		if (mcHandle.getValue() != APIHANDLE.INVALID_HANDLE_VALUE) {
   239 			// close DM connection
   240 			// close DM connection
   240 			dwResult = library.CONACloseDM(mcHandle.getValue());
   241 			dwResult = library.CONACloseDM(mcHandle.getValue());
   241 			mcHandle.setValue(MCHANDLE.INVALID_HANDLE_VALUE);
   242 			mcHandle.setValue(APIHANDLE.INVALID_HANDLE_VALUE);
   242         	// Terminate Device management API
   243         	// Terminate Device management API
   243     		dwResult = library.MCAPI_Terminate(null);
   244     		dwResult = library.MCAPI_Terminate(null);
       
   245 	}
       
   246 	
       
   247 	public void getMediaList() {
       
   248 		if (mcHandle.getValue() != APIHANDLE.INVALID_HANDLE_VALUE) {
       
   249 			IntBuffer pdwCount = IntBuffer.allocate(1);
       
   250 			CONAPI_MEDIA pMedia = new CONAPI_MEDIA();
       
   251 			
       
   252 			CONAPI_MEDIA.ByReference[] ppMedia = null;
       
   253 			
       
   254 			int dwResult = library.CONAMMGetMedia(mcHandle.getValue(), pdwCount, ppMedia);
       
   255 			System.out.printf("dwResult = %x\tpdwCount = %d\n", dwResult, pdwCount.get());
   244 		}
   256 		}
   245 	}
   257 	}
   246 	/**
   258 	/**
   247 	 * @return null if no device found.
   259 	 * @return null if no device found.
   248 	 * @throws CoreException
   260 	 * @throws CoreException