S7COMM.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  5. /**
  6. * Created by root on 06.07.15.
  7. */
  8. public class S7COMM implements Protocol {
  9. @Override
  10. public int getPort() {
  11. return 102;
  12. }
  13. @Override
  14. public boolean isClosed() {
  15. return false;
  16. }
  17. @Override
  18. public boolean isSecure() {
  19. return false;
  20. }
  21. //S7COMM Siemens Simatic Parameter codes
  22. public static String DIAGNOSTICS = "0x00";
  23. public static String CONNECT = "0x0e";
  24. public static String DATA = "0x0f";
  25. public static String READ = "0x04";
  26. public static String WRITE = "0x05";
  27. public static String REQUEST_DOWNLOAD="0x1a";
  28. public static String DOWNLOAD_BLOCK="0x1b";
  29. public static String END_DOWNLOAD="0x1c";
  30. public static String START_UPLOAD="0x1d";
  31. public static String UPLOAD="0x1e";
  32. public static String END_UPLOAD="0x1f";
  33. public static final int READ_COILS = 1;
  34. public static final int READ_INPUT_DISCRETES = 2;
  35. public static final int READ_HOLDING_REGISTERS=3;
  36. public static final int READ_INPUT_REGISTERS = 4;
  37. public static final int WRITE_COIL = 5;
  38. public static final int WRITE_SINGLE_REGISTER = 6;
  39. @Override
  40. public List<Packet> processMessage(Packet requestPacket) {
  41. List<Packet> responsePackets = new ArrayList<Packet>();
  42. byte[] request = null;
  43. if (requestPacket != null) {
  44. request = requestPacket.getBytes();
  45. //getRequestType(request);
  46. // responsePackets.add(requestPacket); // Response packets have to be studied yet
  47. responsePackets=processRequest(request,getRequestType(request));
  48. }
  49. return responsePackets;
  50. }
  51. private List<Packet> processRequest(byte[] request, int requestType) {
  52. List<Packet> responsePackets = new ArrayList<Packet>();
  53. return responsePackets;
  54. }
  55. @Override
  56. public TALK_FIRST whoTalksFirst() {
  57. return null;
  58. }
  59. @Override
  60. public String toString(){
  61. return "S7COMM";
  62. }
  63. private int getRequestType(byte[] request) {
  64. int requestType=request[7];
  65. if (requestType == 5) {
  66. requestType = WRITE_COIL;
  67. } else if (requestType == 1) {
  68. requestType = READ_COILS;
  69. } else if (requestType == 6) {
  70. requestType = WRITE_SINGLE_REGISTER;
  71. } else if (requestType == 4) {
  72. requestType = READ_INPUT_REGISTERS;
  73. }
  74. else if (requestType==2){
  75. requestType = READ_INPUT_DISCRETES;
  76. }
  77. else if (requestType==3){
  78. requestType = READ_HOLDING_REGISTERS;
  79. }
  80. System.out.println(requestType);
  81. return requestType;
  82. }
  83. }