blob: c0fbf2887ef15b4e3e10bc7cca8888e029f4d26b [file] [log] [blame]
package com.projectara.araepm;
import android.hardware.I2cManager;
import android.hardware.I2cTransaction;
import java.io.IOException;
public class L3Switch {
private static final int slaveAddress = 0x44;
private static String i2cBus;
private I2cManager i2c;
private static enum AttributeId {
T_PEERDEVICEID (0x4021),
T_PEERCPORTID (0x4022),
T_CONNECTIONSTATE (0x4020),
T_TRAFFICCLASS (0x4023),
T_PROTOCOLID (0x4024),
T_CPORTFLAGS (0x4025),
T_TXTOKENVALUE (0x4026),
T_RXTOKENVALUE (0x4027),
T_LOCALBUFFERSPACE(0x4028),
T_PEERBUFFERSPACE (0x4029),
T_CREDITSTOSEND (0x402A),
T_CPORTMODE (0x402B);
private int id;
private AttributeId(int id) {
this.id = id;
}
}
private static enum FunctionId {
C0_SETREQ (0x00),
C0_SETCNF (0x01),
C0_PEERSETREQ (0x02),
C0_PEERSETCNF (0x03),
C0_GETREQ (0x04),
C0_GETCNF (0x05),
C0_PEERGETREQ (0x06),
C0_PEERGETCNF (0x07),
C0_LUTSETREQ (0x08),
C0_LUTSETCNF (0x09),
C0_LUTGETREQ (0x0a),
C0_LUTGETCNF (0x0b),
C0_SWITCHATTRSETREQ (0x0c),
C0_SWITCHATTRSETCNF (0x0d),
C0_SWITCHATTRGETREQ (0x0e),
C0_SWITCHATTERGETCNF(0x0f);
private int id;
private FunctionId(int id) {
this.id = id;
}
}
private class SetRequest {
int portId;
FunctionId functionId;
AttributeId attrId;
int selectorIndex;
int attrVal;
public byte[] getBytes() {
byte[] rawBytes = new byte[10];
rawBytes[0] = (byte)(portId & 0xFF);
rawBytes[1] = (byte)(functionId.id & 0xFF);
rawBytes[2] = (byte)((attrId.id >> 8) & 0xFF); // msb
rawBytes[3] = (byte)(attrId.id & 0xFF); // lsb
rawBytes[4] = (byte)((selectorIndex >> 8) & 0xFF);
rawBytes[5] = (byte)((selectorIndex) & 0xFF);
rawBytes[6] = (byte)((attrVal >> 24) & 0xFF);
rawBytes[7] = (byte)((attrVal >> 16) & 0xFF);
rawBytes[8] = (byte)((attrVal >> 8) & 0xFF);
rawBytes[9] = (byte)((attrVal) & 0xFF);
return rawBytes;
}
}
public L3Switch(I2cManager i2c) {
this.i2c = i2c;
this.i2cBus = i2c.getI2cBuses()[0];
}
// api_write_switch_config
public void writeSwitchConfig(int port, boolean peer, AttributeId attrId,
int selectorIndex, int attrVal,
int expectedResultCode) {
I2cTransaction txn = null;
SetRequest setReq = new SetRequest();
setReq.portId = port;
setReq.functionId = peer ? FunctionId.C0_PEERSETREQ : FunctionId.C0_SETREQ;
setReq.attrId = attrId;
setReq.selectorIndex = selectorIndex;
setReq.attrVal = attrVal;
txn = I2cTransaction.newWrite(setReq.getBytes());
I2cTransaction[] results;
try {
results = i2c.performTransactions(i2cBus, slaveAddress, txn);
} catch (IOException e) {
return;
}
}
// api_write_switch_config, peer = 0 (false)
public void writeSwitchConfigLocal(int port, AttributeId attrId,
int selectorIndex, int attrVal,
int expectedResultCode) {
writeSwitchConfig(port, false, attrId, selectorIndex, attrVal,
expectedResultCode);
}
// api_write_switch_config, peer = 1 (true)
public void writeSwitchConfigPeer(int port, AttributeId attrId,
int selectorIndex, int attrVal,
int expectedResultCode) {
writeSwitchConfig(port, true, attrId, selectorIndex, attrVal,
expectedResultCode);
}
// // api_read_switch_config
// int readSwitchConfig(int port, boolean peer, AttributeId attrId,
// int selectorIndex,
// int expectedResultCode) {
// return 0;
// }
//
// // api_read_switch_config, peer = 0 (false)
// int readSwitchConfigLocal(int port, int attrId,
// int selectorIndex,
// int expectedResultCode) {
// return readSwitchConfig(port, false, attrId, selectorIndex,
// expectedResultCode);
// }
//
// // api_read_switch_config, peer = 1 (true)
// int readSwitchConfigPeer(int port, int attrId,
// int selectorIndex,
// int expectedResultCode) {
// return readSwitchConfig(port, true, attrId, selectorIndex,
// expectedResultCode);
// }
// api_write_switch_lut_config
public void writeSwitchLutConfig(int port, int lutAddr, int destPortId,
int destCPortId, int expectedResultCode) {
}
public void configureL3Device(int portA, int cPortA,
int cPortB, int deviceIdB) {
}
}