Android Open Source - remoteyourcam-usb Command






From Project

Back to project page remoteyourcam-usb.

License

The source code is released under:

Apache License

If you think the Android project remoteyourcam-usb listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Nils Assbeck, Guersel Ayaz and Michael Zoech
 *//from   ww  w.j av a2s .  co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.remoteyourcam.usb.ptp.commands;

import java.nio.ByteBuffer;

import android.util.Log;

import com.remoteyourcam.usb.AppConfig;
import com.remoteyourcam.usb.ptp.PacketUtil;
import com.remoteyourcam.usb.ptp.PtpAction;
import com.remoteyourcam.usb.ptp.PtpCamera;
import com.remoteyourcam.usb.ptp.PtpConstants;
import com.remoteyourcam.usb.ptp.PtpConstants.Type;

/**
 * Base class for all PTP commands.
 */
public abstract class Command implements PtpAction {

    private static final String TAG = "Command";

    protected final PtpCamera camera;

    /**
     * Deriving classes have to set this field to true if they want to send data
     * from host to camera.
     */
    protected boolean hasDataToSend;

    /**
     * Received response code, should be handled in
     * {@link #exec(com.remoteyourcam.usb.ptp.PtpCamera.IO)}.
     */
    protected int responseCode;

    private boolean hasResponseReceived;

    public Command(PtpCamera camera) {
        this.camera = camera;
    }

    @Override
    public abstract void exec(PtpCamera.IO io);

    public abstract void encodeCommand(ByteBuffer b);

    /**
     * Derived classes should implement this method if they want to send data
     * from host to camera. The field {@code hasDataToSend} has to be set to
     * true for the sending to be done. The data to send must not be greater
     * than the USB max packet size, any size below 256 should be save.
     */
    public void encodeData(ByteBuffer b) {
    }

    /**
     * Derived classes should implement this method if they want to decode data
     * received in an data packet that has been sent by the camera. The
     * {@code ByteBuffer} already points to the first byte behind the
     * transaction id, i.e. the payload.
     */
    protected void decodeData(ByteBuffer b, int length) {
        if (AppConfig.LOG) {
            Log.w(TAG, "Received data packet but handler not implemented");
        }
    }

    /**
     * Override if any special response data has to be decoded. The
     * {@code ByteBuffer} already points to the first byte behind the
     * transaction id, i.e. the payload.
     */
    protected void decodeResponse(ByteBuffer b, int length) {
    }

    public boolean hasDataToSend() {
        return hasDataToSend;
    }

    public boolean hasResponseReceived() {
        return hasResponseReceived;
    }

    public int getResponseCode() {
        return responseCode;
    }

    public void receivedRead(ByteBuffer b) {
        int length = b.getInt();
        int type = b.getShort() & 0xFFFF;
        int code = b.getShort() & 0xFFFF;
        int tx = b.getInt();

        if (AppConfig.LOG) {
            Log.i(TAG,
                    String.format("Received %s packet for %s, length %d, code %s, tx %d",
                            PtpConstants.typeToString(type), getClass().getSimpleName(), length,
                            PtpConstants.codeToString(type, code), tx));
        }
        if (AppConfig.LOG_PACKETS) {
            PacketUtil.logHexdump(TAG, b.array(), length < 512 ? length : 512);
        }

        if (type == Type.Data) {
            decodeData(b, length);
        } else if (type == Type.Response) {
            hasResponseReceived = true;
            responseCode = code;
            decodeResponse(b, length);
        } else {
            // error
            hasResponseReceived = true;
        }
    }

    /**
     * Reset fields so this command may be requeued.
     */
    @Override
    public void reset() {
        responseCode = 0;
        hasResponseReceived = false;
    }

    protected void encodeCommand(ByteBuffer b, int code) {
        b.putInt(12);
        b.putShort((short) Type.Command);
        b.putShort((short) code);
        b.putInt(camera.nextTransactionId());
        if (AppConfig.LOG_PACKETS) {
            Log.i(TAG, "command packet for " + getClass().getSimpleName());
            PacketUtil.logHexdump(TAG, b.array(), 12);
        }
    }

    protected void encodeCommand(ByteBuffer b, int code, int p0) {
        b.putInt(16);
        b.putShort((short) Type.Command);
        b.putShort((short) code);
        b.putInt(camera.nextTransactionId());
        b.putInt(p0);
        if (AppConfig.LOG_PACKETS) {
            Log.i(TAG, "command packet for " + getClass().getSimpleName());
            PacketUtil.logHexdump(TAG, b.array(), 16);
        }
    }

    protected void encodeCommand(ByteBuffer b, int code, int p0, int p1) {
        b.putInt(20);
        b.putShort((short) Type.Command);
        b.putShort((short) code);
        b.putInt(camera.nextTransactionId());
        b.putInt(p0);
        b.putInt(p1);
        if (AppConfig.LOG_PACKETS) {
            Log.i(TAG, "command packet for " + getClass().getSimpleName());
            PacketUtil.logHexdump(TAG, b.array(), 20);
        }
    }

    protected void encodeCommand(ByteBuffer b, int code, int p0, int p1, int p2) {
        b.putInt(24);
        b.putShort((short) Type.Command);
        b.putShort((short) code);
        b.putInt(camera.nextTransactionId());
        b.putInt(p0);
        b.putInt(p1);
        b.putInt(p2);
        if (AppConfig.LOG_PACKETS) {
            Log.i(TAG, "command packet for " + getClass().getSimpleName());
            PacketUtil.logHexdump(TAG, b.array(), 24);
        }
    }
}




Java Source Code List

com.remoteyourcam.usb.AbstractRycApplication.java
com.remoteyourcam.usb.AppConfig.java
com.remoteyourcam.usb.AppSettings.java
com.remoteyourcam.usb.GestureDetector.java
com.remoteyourcam.usb.MainActivity.java
com.remoteyourcam.usb.PictureView.java
com.remoteyourcam.usb.PropertyAdapter.java
com.remoteyourcam.usb.PropertyData.java
com.remoteyourcam.usb.PropertyDisplayer.java
com.remoteyourcam.usb.PropertyToggle.java
com.remoteyourcam.usb.RycApplication.java
com.remoteyourcam.usb.activities.AppSettingsActivity.java
com.remoteyourcam.usb.ptp.Camera.java
com.remoteyourcam.usb.ptp.EosCamera.java
com.remoteyourcam.usb.ptp.EosConstants.java
com.remoteyourcam.usb.ptp.FocusPoint.java
com.remoteyourcam.usb.ptp.NikonCamera.java
com.remoteyourcam.usb.ptp.PacketUtil.java
com.remoteyourcam.usb.ptp.PtpAction.java
com.remoteyourcam.usb.ptp.PtpCamera.java
com.remoteyourcam.usb.ptp.PtpConstants.java
com.remoteyourcam.usb.ptp.PtpPropertyHelper.java
com.remoteyourcam.usb.ptp.PtpService.java
com.remoteyourcam.usb.ptp.PtpUsbConnection.java
com.remoteyourcam.usb.ptp.PtpUsbService.java
com.remoteyourcam.usb.ptp.WorkerNotifier.java
com.remoteyourcam.usb.ptp.commands.CloseSessionCommand.java
com.remoteyourcam.usb.ptp.commands.Command.java
com.remoteyourcam.usb.ptp.commands.GetDeviceInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetDevicePropDescCommand.java
com.remoteyourcam.usb.ptp.commands.GetDevicePropValueCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectHandlesCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageIdsCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageInfosAction.java
com.remoteyourcam.usb.ptp.commands.GetThumb.java
com.remoteyourcam.usb.ptp.commands.InitiateCaptureCommand.java
com.remoteyourcam.usb.ptp.commands.OpenSessionCommand.java
com.remoteyourcam.usb.ptp.commands.RetrieveAddedObjectInfoAction.java
com.remoteyourcam.usb.ptp.commands.RetrieveImageAction.java
com.remoteyourcam.usb.ptp.commands.RetrieveImageInfoAction.java
com.remoteyourcam.usb.ptp.commands.RetrievePictureAction.java
com.remoteyourcam.usb.ptp.commands.SetDevicePropValueCommand.java
com.remoteyourcam.usb.ptp.commands.SimpleCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosEventCheckCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosGetLiveViewPictureCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosOpenSessionAction.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetExtendedEventInfoCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetLiveViewAction.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetPcModeCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetPropertyCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosTakePictureCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonAfDriveCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonAfDriveDeviceReadyCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonCloseSessionAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonEventCheckCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetLiveViewImageAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetLiveViewImageCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetVendorPropCodesCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonOpenSessionAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonStartLiveViewAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonStopLiveViewAction.java
com.remoteyourcam.usb.ptp.model.DeviceInfo.java
com.remoteyourcam.usb.ptp.model.DevicePropDesc.java
com.remoteyourcam.usb.ptp.model.LiveViewData.java
com.remoteyourcam.usb.ptp.model.ObjectInfo.java
com.remoteyourcam.usb.ptp.model.StorageInfo.java
com.remoteyourcam.usb.util.DimenUtil.java
com.remoteyourcam.usb.util.NotificationIds.java
com.remoteyourcam.usb.util.PackageUtil.java
com.remoteyourcam.usb.view.AspectRatioImageView.java
com.remoteyourcam.usb.view.BaseFragment.java
com.remoteyourcam.usb.view.GalleryAdapter.java
com.remoteyourcam.usb.view.GalleryFragment.java
com.remoteyourcam.usb.view.PictureFragment.java
com.remoteyourcam.usb.view.SessionActivity.java
com.remoteyourcam.usb.view.SessionFragment.java
com.remoteyourcam.usb.view.SessionView.java
com.remoteyourcam.usb.view.StorageAdapter.java
com.remoteyourcam.usb.view.TabletSessionFragment.java
com.remoteyourcam.usb.view.ThumbnailAdapter.java
com.remoteyourcam.usb.view.WebViewDialogFragment.java