Stealthrt

Onkyo eISCP

Mar 7th, 2024
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.26 KB | Source Code | 0 0
  1. package com.example.onkyorc;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.os.StrictMode;
  6. import android.view.View;
  7.  
  8. import java.io.DataInputStream;
  9. import java.io.DataOutputStream;
  10. import java.io.EOFException;
  11. import java.io.FilterOutputStream;
  12. import java.io.IOException;
  13. import java.io.OutputStream;
  14. import java.net.Socket;
  15. import java.net.UnknownHostException;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.     }
  23.  
  24.     public void buttonClicked(View view) {
  25.         MainActivity instance = new MainActivity();
  26.  
  27.         if (view.getId() == R.id.btnPWR) {
  28.             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  29.             StrictMode.setThreadPolicy(policy);
  30.  
  31.             //instance.queryCommand("PWRQSTN",true);
  32.             instance.sendCommand("PWR01", true);
  33.         }
  34.     }
  35.  
  36.     private static final String DEFAULT_EISCP_IP = "192.168.1.33";
  37.     private static final int DEFAULT_EISCP_PORT = 60128;
  38.     private static Socket eiscpSocket_ = null;
  39.     private static DataOutputStream out_ = null;
  40.     private static DataInputStream in_ = null;
  41.     private static boolean connected_ = false;
  42.  
  43.     public boolean connectSocket() {
  44.         return connectSocket(DEFAULT_EISCP_IP, DEFAULT_EISCP_PORT);
  45.     }
  46.  
  47.     public boolean connectSocket(String ip, int eiscpPort) {
  48.         if (ip == null || ip.equals("")) ip = DEFAULT_EISCP_IP;
  49.         if (eiscpPort == 0) eiscpPort = DEFAULT_EISCP_PORT;
  50.  
  51.         try {
  52.             //1. creating a socket to connect to the server
  53.             eiscpSocket_ = new Socket(ip, eiscpPort);
  54.             System.out.println("Connected to " + ip + " on port " + eiscpPort);
  55.  
  56.             //2. get Input and Output streams
  57.             out_ = new DataOutputStream(eiscpSocket_.getOutputStream());
  58.             System.out.println("out_Init");
  59.             out_.flush();
  60.  
  61.             System.out.println("inInit");
  62.             connected_ = true;
  63.         } catch (UnknownHostException unknownHost) {
  64.             System.err.println("You are trying to connect to an unknown host!");
  65.         } catch (IOException ioException) {
  66.             ioException.printStackTrace();
  67.         }
  68.  
  69.         return connected_;
  70.     }
  71.  
  72.     public boolean closeSocket() {
  73.         //4: Closing connection
  74.         try {
  75.             if (in_ != null) in_.close();
  76.             if (out_ != null) out_.close();
  77.             if (eiscpSocket_ != null) eiscpSocket_.close();
  78.  
  79.             System.out.println("closed connections");
  80.             connected_ = false;
  81.         } catch (IOException ioException) {
  82.             ioException.printStackTrace();
  83.         }
  84.  
  85.         return connected_;
  86.     }
  87.  
  88.     public static String convertStringToHex(String str, boolean dumpOut) {
  89.         char[] chars = str.toCharArray();
  90.         String out_put = "";
  91.  
  92.         if (dumpOut) System.out.println("Ascii: " + str);
  93.         if (dumpOut) System.out.print("Hex: ");
  94.  
  95.         StringBuffer hex = new StringBuffer();
  96.  
  97.         for (int i = 0; i < chars.length; i++) {
  98.             out_put = Integer.toHexString((int) chars[i]);
  99.             if (out_put.length() == 1) hex.append("0");
  100.             hex.append(out_put);
  101.  
  102.             if (dumpOut)
  103.                 System.out.print("0x" + (out_put.length() == 1 ? "0" : "") + out_put + " ");
  104.         }
  105.  
  106.         if (dumpOut) System.out.println("");
  107.  
  108.         return hex.toString();
  109.     }
  110.  
  111.     public StringBuilder getEiscpMessage(String command) {
  112.         StringBuilder sb = new StringBuilder();
  113.         int eiscpDataSize = 2 + command.length() + 1; // this is the eISCP data size
  114.  
  115.         sb.append("ISCP");
  116.  
  117.         // the following are all in HEX representing one char
  118.         // 4 char Big Endian Header
  119.         sb.append((char) 0x00);
  120.         sb.append((char) 0x00);
  121.         sb.append((char) 0x00);
  122.         sb.append((char) 0x10);
  123.  
  124.         // 4 char  Big Endian data size
  125.         sb.append((char) ((eiscpDataSize >> 24) & 0xFF));
  126.         sb.append((char) ((eiscpDataSize >> 16) & 0xFF));
  127.         sb.append((char) ((eiscpDataSize >> 8) & 0xFF));
  128.         sb.append((char) (eiscpDataSize & 0xFF));
  129.  
  130.         // eiscp_version = "01";
  131.         sb.append((char) 0x01);
  132.  
  133.         // 3 chars reserved = "00"+"00"+"00";
  134.         sb.append((char) 0x00);
  135.         sb.append((char) 0x00);
  136.         sb.append((char) 0x00);
  137.  
  138.         //  eISCP data
  139.         // Start Character
  140.         sb.append("!");
  141.  
  142.         // eISCP data - unittype char '1' is receiver
  143.         sb.append("1");
  144.  
  145.         // eISCP data - 3 char command and param    ie PWR01
  146.         sb.append(command);
  147.  
  148.         // msg end - EOF
  149.         //sb.append((char) Integer.parseInt("0D", 16));
  150.         sb.append((char) 0x0D);
  151.  
  152.         System.out.println("eISCP data size: " + eiscpDataSize + "(0x" + Integer.toHexString(eiscpDataSize) + ") chars");
  153.         System.out.println("eISCP msg size: " + sb.length() + "(0x" + Integer.toHexString(sb.length()) + ") chars");
  154.  
  155.         return sb;
  156.     }
  157.  
  158.     public void sendCommand(String command, boolean closeSocket) {
  159.         StringBuilder sb = getEiscpMessage(command);
  160.  
  161.         if (connectSocket()) {
  162.             try {
  163.                 System.out.println("sending " + sb.length() + " chars: ");
  164.                 convertStringToHex(sb.toString(), true);
  165.  
  166.                 out_ = new DataOutputStream(new LoggingOutputStream(eiscpSocket_.getOutputStream()));
  167.                 out_.writeBytes(sb.toString());
  168.                 out_.flush();
  169.                 System.out.println("sent!");
  170.             } catch (IOException ioException) {
  171.                 ioException.printStackTrace();
  172.             }
  173.         }
  174.  
  175.         if (closeSocket) closeSocket();
  176.     }
  177.  
  178.     public class LoggingOutputStream extends FilterOutputStream {
  179.         private long count;
  180.  
  181.         public LoggingOutputStream(OutputStream out) { super(out); }
  182.  
  183.         @Override
  184.         public void write(int b) throws IOException {
  185.             System.out.printf("writing byte %3d: %2h (%s)%n", count, b, (char) b);
  186.             count++;
  187.             super.write(b);
  188.         }
  189.     }
  190.  
  191.     public String queryCommand(String command, boolean closeSocket) {
  192.         String retVal = "";
  193.  
  194.         sendCommand(command, false);
  195.  
  196.         // now listen for the response
  197.         if (connected_) {
  198.             try {
  199.                 in_ = new DataInputStream(eiscpSocket_.getInputStream());
  200.                 byte[] responseBytes = new byte[32];
  201.                 int numBytesReceived = 0;
  202.  
  203.                 numBytesReceived = in_.read(responseBytes);
  204.                 retVal = new String(responseBytes);
  205.                 System.out.println("received " + numBytesReceived + " bytes: \"" + retVal + "\"");
  206.  
  207.             } catch (EOFException eofException) {
  208.                 System.out.println("received: \"" + retVal + "\"");
  209.             } catch (IOException ioException) {
  210.                 ioException.printStackTrace();
  211.             }
  212.         } else
  213.             System.out.println(" Not Connected to Receive ");
  214.  
  215.         if (closeSocket) closeSocket();
  216.  
  217.         return retVal;
  218.     }
  219.  
  220.     public static void main(String[] args) { } // main
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment