/protocols/ss7/mtp/mtp-impl/src/main/java/org/mobicents/protocols/ss7/mtp/util/MTPUtility.java

http://mobicents.googlecode.com/ · Java · 134 lines · 38 code · 11 blank · 85 comment · 0 complexity · 4dd3ec0c2f28d241177788818a3ea35d MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.ss7.mtp.util;
  23. /**
  24. * Small class to define methods of use to o
  25. *
  26. * @author baranowb
  27. *
  28. */
  29. public class MTPUtility {
  30. /**
  31. * Extract routing information from source[], it expects that source is
  32. * properly encoded routing label atleast(5bytes long, same as dest). It
  33. * copies data to <b>dest</b> and swamp <i>DPC</i> with <i>OPC</i>.
  34. *
  35. * @param source
  36. * @param dest
  37. */
  38. public static void copyBackRouteHeader(byte[] source, byte[] dest) {
  39. int thisPointCode = getFromSif_DPC(source, 1);
  40. int remotePointCode = getFromSif_OPC(source, 1);
  41. int sls = getFromSif_SLS(source, 1);
  42. int si = getFromSif_SI(source);
  43. int ssi = getFromSif_SSI(source);
  44. writeRoutingLabel(dest, si, ssi, sls, remotePointCode, thisPointCode);
  45. }
  46. /**
  47. * Retrieves DPC from SIF. SIF starts from byte 1 in MSU. For MSU
  48. * [SIO,DPC,OPC,SLS,Data], call would look as following: int dpc =
  49. * getFromSif_DPC(MSU,1);
  50. *
  51. * @param sif
  52. * - byte[] - either SIF or MSU
  53. * @param shift
  54. * - shift in passed byte[]. For MSU its 1. For SIF part of MSU
  55. * it will be 0;
  56. * @return
  57. */
  58. public static final int getFromSif_DPC(byte[] sif, int shift) {
  59. int dpc = (sif[0 + shift] & 0xff | ((sif[1 + shift] & 0x3f) << 8));
  60. return dpc;
  61. }
  62. /**
  63. * Retrieves OPC from SIF. SIF starts from byte 1 in MSU. For MSU
  64. * [SIO,DPC,OPC,SLS,Data], call would look as following: int opc =
  65. * getFromSif_OPC(MSU,1);
  66. *
  67. * @param sif
  68. * - byte[] - either SIF or MSU
  69. * @param shift
  70. * - shift in passed byte[]. For MSU its 1. For SIF part of MSU
  71. * it will be 0;
  72. * @return
  73. */
  74. public static final int getFromSif_OPC(byte[] sif, int shift) {
  75. int opc = ((sif[1 + shift] & 0xC0) >> 6) | ((sif[2 + shift] & 0xff) << 2) | ((sif[3 + shift] & 0x0f) << 10);
  76. return opc;
  77. }
  78. /**
  79. * Retrieves SLS from SIF. SIF starts from byte 1 in MSU. For MSU
  80. * [SIO,DPC,OPC,SLS,Data], call would look as following: int opc =
  81. * getFromSif_SLS(MSU,1);
  82. *
  83. * @param sif
  84. * - byte[] - either SIF or MSU
  85. * @param shift
  86. * - shift in passed byte[]. For MSU its 1. For SIF part of MSU
  87. * it will be 0;
  88. * @return
  89. */
  90. public static final int getFromSif_SLS(byte[] sif, int shift) {
  91. int sls = (sif[3 + shift] & 0xf0) >>> 4;
  92. return sls;
  93. }
  94. public static final int getFromSif_SI(byte[] data) {
  95. int serviceIndicator = data[0] & 0x0f;
  96. return serviceIndicator;
  97. }
  98. public static final int getFromSif_SSI(byte[] data) {
  99. // see Q.704.14.2
  100. int subserviceIndicator = (data[0] >> 4) & 0x0F;
  101. return subserviceIndicator;
  102. }
  103. /**
  104. * Encodes routing label into passed byte[]. It has to be at least 5 bytes
  105. * long!
  106. *
  107. * @param destination
  108. * @param si
  109. * @param ssi
  110. * @param sls
  111. * @param dpc
  112. * @param opc
  113. */
  114. public static void writeRoutingLabel(byte[] destination, int si, int ssi, int sls, int dpc, int opc) {
  115. // see Q.704.14.2
  116. destination[0] = (byte) (((ssi & 0x0F) << 4) | (si & 0x0F));
  117. destination[1] = (byte) dpc;
  118. destination[2] = (byte) (((dpc >> 8) & 0x3F) | ((opc & 0x03) << 6));
  119. destination[3] = (byte) (opc >> 2);
  120. destination[4] = (byte) (((opc >> 10) & 0x0F) | ((sls & 0x0F) << 4));
  121. // sif[4] = (byte) (((opc>> 10) & 0x0F) | ((0 & 0x0F) << 4));
  122. }
  123. }