FileDocCategorySizeDatePackage
DeviceEmul.javaAPI DocphoneME MR2 API (J2ME)18266Wed May 02 18:00:30 BST 2007com.sun.midp.jsr82emul

DeviceEmul

public class DeviceEmul extends EmulationClient implements EmulUnit
Emulates a Bluetooth device.

Fields Summary
private static int
DEFAULT_AC
Initial access code.
private Inquiry
curInquiry
Keeps current inquiry if any.
byte[]
address
Bluetooth address.
private DeviceState
deviceState
Device state i.e. discoverable mode and device class.
private Object
serverTransaction
Lock for emulation server communications that require response.
private static DeviceEmul
localDeviceEmul
Device emulation for the local device.
static final int
UPDATE_CLASS
Request code for updating service classes.
static final int
UPDATE_ACCESS
Request code for updating access code.
static final int
START_INQUIRY
Request code for updating starting inquiry.
static final int
CANCEL_INQUIRY
Request code for cancelling inquiry.
static final int
INIT_DEVICE
Request code for initing devie.
Constructors Summary
public DeviceEmul()
Constructs an emulation instance and retrieves addres for it from emulation server.


                            
      
        try {
            connect();

            messenger.sendBytes(toServer, Messenger.REGISTER_DEVICE, 
                    getLocalIpBytes());
            messenger.receive(fromServer);

            if (messenger.getCode() != Messenger.REGISTERED) {
                throw new IOException("Error communicating emulation server");
            }
            
            address = messenger.getBytes();
            int cod = initDevice(address, DEFAULT_AC);
            deviceState = new DeviceState(cod, DEFAULT_AC);
            updateState();
            
            Log.log("DeviceEmul: my address is " +
                    BluetoothUtils.getAddressString(address));
        } catch (IOException e) {
            throw new EmulationException(
                "Error initializing local device emulation");
        }
    
Methods Summary
private synchronized voidcancelInquiry()
Cancels current inquiry.

        if (curInquiry != null) {
            curInquiry.cancel();
        }
    
private native voiddeviceDiscovered(byte[] addr, int cod)
Notifies on device discovery.

param
addr Bluetooth address of device discovered
param
cod class of device discovered

private native voidfinalize()
Saves device information in persistent storage for future use.

public byte[]getAddress()
Returns address of this device.

return
Bluetooth address

        return address;
    
public static synchronized com.sun.midp.jsr82emul.DeviceEmulgetLocalDeviceEmul()
Returns instance of this class for local device emulation.

return
the device emulation object for the local device

        if (localDeviceEmul == null) {
            localDeviceEmul = new DeviceEmul();
        }
        return localDeviceEmul;
    
private byte[]getLocalIpBytes()
Retrieves IP address.

return
host computer IP address as byte array.

        String ip = EmulationClient.getLocalIP();
        byte[] res = null;
        
        if (ip != null && ip.length() > 0) {
            int from = 0;
            int to = 0;
            byte[] parsed = new byte[4];
            
            try {
                for (int i = 0; i < 4; i++) {
                    to = ip.indexOf('.", from);
                    if (to < 0) {
                        to = ip.length();
                    }
                    parsed[i] = (byte)Integer.parseInt(ip.substring(from, to));
                    from = to + 1;
                }
                
                res = parsed;
            } catch (NumberFormatException e) {
                // res == null idenitifies retrieving failure
            }
        }
        
        if (res == null) {
            res = new byte[] {127, 0, 0, 1};
        }
        return res;
    
private native intinitDevice(byte[] addr, int ac)
Initializes local device parameters in shared emulation storage.

param
addr Bluetoth address bytes retrieved form emulation server
param
ac initial access code
return
initial class of device with service classes that are possibly saved after previous usage of device with the same address

private native voidinquiryCompleted(boolean success)
Notifies on inquiry completion.

param
success true if completed successfully, false otherwize

public voidprocess(BytePack request)
Processes the request.

param
request Packeted request

    
                
        
        switch (request.extract()) {
        case UPDATE_CLASS:
            Log.log("Processing UPDATE_CLASS");
            deviceState.setServiceClasses(request.extractInt());
            updateState();
            break;
        case UPDATE_ACCESS:
            Log.log("Processing UPDATE_ACCESS");
            deviceState.setDiscoverable(request.extractInt());
            updateState();
            break;
        case START_INQUIRY:
            Log.log("Processing START_INQUIRY");
            startInquiry(request.extractInt());
            break;
        case CANCEL_INQUIRY:
            Log.log("Processing CANCEL_INQUIRY");
            cancelInquiry();
            break;
        case INIT_DEVICE:
            Log.log("Processing INIT_DEVICE");
            // Nothing to do: the request has already caused 
            // construction of all the objects required.
            break;
        }
    
voidregisterService(ServiceConnectionData serviceData)
Registers service at emulation server.

param
serviceData combined service connection info
throws
IOException if connection to emulation server failed.


        synchronized (serverTransaction) {
            messenger.sendBytes(toServer, Messenger.REGISTER_SERVICE,
                serviceData.toByteArray(ServiceConnectionData.SERVER_DATA));
        }
    
private synchronized voidstartInquiry(int accessCode)
Starts inquiry.

param
accessCode access code of desired devices

        curInquiry = new Inquiry(accessCode);
    
voidunregisterService(int serverSocketPort)
Unregisters service at emulation server.

param
serverSocketPort socket port desired service accepted connections at

        try {
            messenger.sendInt(toServer, Messenger.UNREGISTER_SERVICE,
                serverSocketPort);
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "Unregistering service failed");
            }
        }
    
private voidupdateState()
Sends device state update to emulation server.

        try {
            messenger.sendInt(toServer,
                Messenger.UPDATE_DEVICE_STATE, deviceState.toInt());
        } catch (IOException e) {
            throw new EmulationException(e.getMessage());
        }