parent
ed85e6ae49
commit
8e30b52b66
@ -1,57 +0,0 @@ |
||||
package fr.mobdev.lpcprog; |
||||
|
||||
import android.hardware.usb.UsbDevice; |
||||
import android.hardware.usb.UsbDeviceConnection; |
||||
import android.os.Build; |
||||
import android.support.v7.widget.RecyclerView; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.TextView; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class DeviceAdapter extends RecyclerView.Adapter<DeviceHolder> { |
||||
|
||||
private List<UsbDevice> devices; |
||||
public DeviceAdapter(List<UsbDevice> devices){ |
||||
this.devices = devices; |
||||
} |
||||
|
||||
@Override |
||||
public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_item,parent,false); |
||||
return new DeviceHolder(view); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(DeviceHolder holder, int position) { |
||||
holder.setupDevice(devices.get(position)); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return devices.size(); |
||||
} |
||||
} |
||||
|
||||
class DeviceHolder extends RecyclerView.ViewHolder { |
||||
|
||||
|
||||
public DeviceHolder(View itemView) { |
||||
super(itemView); |
||||
} |
||||
|
||||
public void setupDevice(UsbDevice device){ |
||||
if(Build.VERSION.SDK_INT >= 21) { |
||||
TextView name = (TextView) itemView.findViewById(R.id.device_name); |
||||
name.setText("Name: " + device.getProductName()); |
||||
} |
||||
TextView vendor = (TextView) itemView.findViewById(R.id.device_vendor_id); |
||||
vendor.setText("Vendor Id: "+device.getVendorId()); |
||||
TextView product = (TextView) itemView.findViewById(R.id.device_id); |
||||
product.setText("Product id: " + device.getProductId()); |
||||
|
||||
} |
||||
|
||||
} |
@ -1,227 +0,0 @@ |
||||
package fr.mobdev.lpcprog; |
||||
|
||||
import android.hardware.usb.UsbDevice; |
||||
import android.hardware.usb.UsbDeviceConnection; |
||||
import android.provider.Settings; |
||||
|
||||
import com.felhr.usbserial.UsbSerialDevice; |
||||
import com.felhr.usbserial.UsbSerialInterface; |
||||
|
||||
public class IspManager { |
||||
|
||||
//defines
|
||||
private static final int REP_BUF = 100; |
||||
private static final byte[] SYNC_START = "?".getBytes(); |
||||
private static final byte[] SYNC = "Synchronized\r\n".getBytes(); |
||||
private static final byte[] SYNC_OK = "OK\r\n".getBytes(); |
||||
private static final byte[] DATA_BLOCK = "OK\r\n".getBytes(); |
||||
private static final byte[] DATA_BLOCK_RESEND = "RESEND\r\n".getBytes(); |
||||
private static final byte[] SYNC_ECHO_OFF = "A 0\r\n".getBytes(); |
||||
private static final byte[] READ_UID = "N\r\n".getBytes(); |
||||
private static final byte[] READ_PART_ID = "J\r\n".getBytes(); |
||||
private static final byte[] READ_BOOT_VERSION = "K\r\n".getBytes(); |
||||
|
||||
//members
|
||||
private static IspManager instance = null; |
||||
private UsbSerialDevice serialPort = null; |
||||
|
||||
private IspManager(){ |
||||
|
||||
} |
||||
|
||||
public static IspManager getInstance(){ |
||||
if(instance == null) |
||||
instance = new IspManager(); |
||||
return instance; |
||||
} |
||||
|
||||
public boolean setupDevice(UsbDevice device, UsbDeviceConnection connection, int baudRate, int data, int stops, int parity, int flow){ |
||||
serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection); |
||||
if(serialPort.syncOpen()) { |
||||
serialPort.setBaudRate(baudRate); |
||||
serialPort.setDataBits(data); |
||||
serialPort.setStopBits(stops); |
||||
serialPort.setParity(parity); |
||||
serialPort.setFlowControl(flow); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public boolean synchronize(int crystal_freq){ |
||||
if(serialPort == null) |
||||
return false; |
||||
byte[] buf; |
||||
if(serialPort.syncWrite(SYNC_START,500) != SYNC_START.length){ |
||||
System.out.println("Start"); |
||||
return false; |
||||
} |
||||
|
||||
buf = new byte[SYNC.length]; |
||||
serialPort.syncRead(buf,500); |
||||
String result = new String(buf); |
||||
String expected = new String(SYNC); |
||||
if(result.compareTo(expected) != 0) { |
||||
printbuffer(buf); |
||||
System.out.println("Start answer "+result); |
||||
return false; |
||||
} |
||||
if(serialPort.syncWrite(SYNC,500) != SYNC.length) { |
||||
System.out.println("SYNC write"); |
||||
return false; |
||||
} |
||||
clearBuffer(SYNC.length); |
||||
buf = new byte[SYNC_OK.length]; |
||||
serialPort.syncRead(buf,500); |
||||
result = new String(buf); |
||||
expected = new String(SYNC_OK); |
||||
if(result.compareTo(expected) != 0) { |
||||
printbuffer(buf); |
||||
System.out.println("SYNC OK "+result); |
||||
return false; |
||||
} |
||||
|
||||
String freq = String.valueOf(crystal_freq)+"\r\n"; |
||||
if(serialPort.syncWrite(freq.getBytes(),500) != freq.length()) { |
||||
System.out.println("freq write"); |
||||
return false; |
||||
} |
||||
|
||||
clearBuffer(freq.length()); |
||||
buf = new byte[SYNC_OK.length]; |
||||
serialPort.syncRead(buf,500); |
||||
result = new String(buf); |
||||
expected = new String(SYNC_OK); |
||||
if(result.compareTo(expected) != 0) { |
||||
System.out.println("freq answer "+result); |
||||
printbuffer(buf); |
||||
return false; |
||||
} |
||||
|
||||
if(serialPort.syncWrite(SYNC_ECHO_OFF,500) != SYNC_ECHO_OFF.length) { |
||||
System.out.println("Sync Echo Off write"); |
||||
return false; |
||||
} |
||||
|
||||
clearBuffer(SYNC_ECHO_OFF.length); |
||||
buf = new byte[3]; |
||||
serialPort.syncRead(buf,500); |
||||
|
||||
return true; |
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
private void printbuffer(byte[] buffer){ |
||||
for(int i = 0; i < buffer.length; i++){ |
||||
System.out.println(String.format("%02x",buffer[i])); |
||||
} |
||||
} |
||||
|
||||
private void clearBuffer(int size){ |
||||
if(serialPort == null) |
||||
return; |
||||
|
||||
byte[] buf = new byte[size]; |
||||
int cpt=0; |
||||
do{ |
||||
int nb = serialPort.syncRead(buf,500); |
||||
System.out.println(String.format("%02x",buf[0])); |
||||
break;/* |
||||
if(nb < 0){ |
||||
cpt++; |
||||
if(cpt >=100) { |
||||
System.out.println("timeout"); |
||||
break; |
||||
} |
||||
} else if( nb == 0){ |
||||
System.out.println("nb = 0"); |
||||
return; |
||||
}*/ |
||||
}while(buf[0] == '\r' && buf[0] != '\n'); |
||||
|
||||
if(buf[0] == '\r') { |
||||
serialPort.syncRead(buf, 5); |
||||
System.out.println(String.format("%02x", buf[0])); |
||||
} |
||||
System.out.println("end"); |
||||
} |
||||
|
||||
public String[] readUid(){ |
||||
if(serialPort == null) |
||||
return null; |
||||
byte[] buf = new byte[REP_BUF]; |
||||
int ret = sendCommand(READ_UID,null); |
||||
if(ret != 0){ |
||||
System.out.println("Send Command "+ret); |
||||
return null; |
||||
} |
||||
int size = serialPort.syncRead(buf,1); |
||||
if(size<=0){ |
||||
System.out.println("Send size "+size); |
||||
return null; |
||||
} |
||||
String[] uids = new String[4]; |
||||
for(int i = 0; i < 4; i++){ |
||||
uids[0] = Long.toHexString(byteArrayToLong(buf,8*i+i*2)); |
||||
} |
||||
System.out.println("uids ok"); |
||||
return uids; |
||||
} |
||||
|
||||
public String readPartId(){ |
||||
return null; |
||||
} |
||||
|
||||
public String[] readBootVersion(){ |
||||
return null; |
||||
} |
||||
|
||||
public int sendCommand(byte[] cmd, String[] args){ |
||||
byte[] buf = new byte[3]; |
||||
if(serialPort.syncWrite(cmd,15) != cmd.length) |
||||
return -5; |
||||
int len = serialPort.syncRead(buf, 15); |
||||
System.out.println("Len "+len); |
||||
for(int i = 0; i < 3; i++){ |
||||
System.out.println(String.format("%02x", buf[i])); |
||||
} |
||||
if(len <= 0) |
||||
return -4; |
||||
int ret = parseRetCode(buf); |
||||
return ret; |
||||
} |
||||
|
||||
private int parseRetCode(byte[] data){ |
||||
String str = new String(data); |
||||
str = str.replace("\r\n",""); |
||||
|
||||
int ret = Integer.parseInt(str); |
||||
//int ret = byteArrayToInt(data,0);
|
||||
return ret; |
||||
} |
||||
|
||||
private int byteArrayToInt(byte[] array, int offset){ |
||||
int value = 0; |
||||
value |= (array[offset] & 0xff) << 24; |
||||
value |= (array[offset+1] & 0xff) << 16; |
||||
value |= (array[offset+2] & 0xff) << 8; |
||||
value |= (array[offset+3] & 0xff); |
||||
return value; |
||||
} |
||||
|
||||
private long byteArrayToLong(byte[] array, int offset){ |
||||
long value = 0; |
||||
value |= (array[offset] & 0xff) << 56; |
||||
value |= (array[offset+1] & 0xff) << 48; |
||||
value |= (array[offset+2] & 0xff) << 40; |
||||
value |= (array[offset+3] & 0xff) << 32; |
||||
value |= (array[offset+4] & 0xff) << 24; |
||||
value |= (array[offset+5] & 0xff) << 16; |
||||
value |= (array[offset+6] & 0xff) << 8; |
||||
value |= (array[offset+7] & 0xff); |
||||
return value; |
||||
} |
||||
} |
||||
|
@ -1,80 +0,0 @@ |
||||
package fr.mobdev.lpcprog; |
||||
|
||||
import android.app.Activity; |
||||
import android.app.PendingIntent; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.content.IntentFilter; |
||||
import android.hardware.usb.UsbDevice; |
||||
import android.hardware.usb.UsbDeviceConnection; |
||||
import android.hardware.usb.UsbManager; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class UsbComm { |
||||
private static final String ACTION_USB_PERMISSION = "fr.mobdev.usb_perm"; |
||||
private UsbManager manager = null; |
||||
private UsbDeviceConnection connection = null; |
||||
private static int val = 0; |
||||
private UsbDevice device = null; |
||||
|
||||
public UsbComm(Context context){ |
||||
manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); |
||||
} |
||||
|
||||
public boolean searchForDevice(){ |
||||
HashMap<String, UsbDevice> usbDevices = manager.getDeviceList(); |
||||
if(!usbDevices.isEmpty()) { |
||||
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { |
||||
UsbDevice device = entry.getValue(); |
||||
int deviceVID = device.getVendorId(); |
||||
int devicePID = device.getProductId(); |
||||
if (deviceVID != 0x1d6b && devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public List<UsbDevice> getDevices(){ |
||||
List<UsbDevice> devices = new ArrayList<>(); |
||||
HashMap<String, UsbDevice> usbDevices = manager.getDeviceList(); |
||||
if(!usbDevices.isEmpty()) { |
||||
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { |
||||
UsbDevice device = entry.getValue(); |
||||
int deviceVID = device.getVendorId(); |
||||
int devicePID = device.getProductId(); |
||||
if (deviceVID != 0x1d6b && devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) { |
||||
devices.add(device); |
||||
} |
||||
} |
||||
} |
||||
return devices; |
||||
} |
||||
|
||||
public boolean askForPermission(UsbDevice device, Activity activity){ |
||||
if(!manager.hasPermission(device)) { |
||||
PendingIntent intent = PendingIntent.getBroadcast(activity, 0, new Intent(ACTION_USB_PERMISSION), 0); |
||||
manager.requestPermission(device, intent); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public UsbDeviceConnection openConnection(UsbDevice device, Activity activity){ |
||||
if(!manager.hasPermission(device)) { |
||||
askForPermission(device, activity); |
||||
return null; |
||||
} |
||||
if(connection == null || this.device != device) { |
||||
connection = manager.openDevice(device); |
||||
this.device = device; |
||||
} |
||||
return connection; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue