PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 23package org.mobicents.protocols.ss7.mtp.util;
 24
 25/**
 26 * Small class to define methods of use to o
 27 * 
 28 * @author baranowb
 29 * 
 30 */
 31public class MTPUtility {
 32
 33	/**
 34	 * Extract routing information from source[], it expects that source is
 35	 * properly encoded routing label atleast(5bytes long, same as dest). It
 36	 * copies data to <b>dest</b> and swamp <i>DPC</i> with <i>OPC</i>.
 37	 * 
 38	 * @param source
 39	 * @param dest
 40	 */
 41	public static void copyBackRouteHeader(byte[] source, byte[] dest) {
 42		int thisPointCode = getFromSif_DPC(source, 1);
 43		int remotePointCode = getFromSif_OPC(source, 1);
 44		int sls = getFromSif_SLS(source, 1);
 45		int si = getFromSif_SI(source);
 46		int ssi = getFromSif_SSI(source);
 47		writeRoutingLabel(dest, si, ssi, sls, remotePointCode, thisPointCode);
 48	}
 49
 50	/**
 51	 * Retrieves DPC from SIF. SIF starts from byte 1 in MSU. For MSU
 52	 * [SIO,DPC,OPC,SLS,Data], call would look as following: int dpc =
 53	 * getFromSif_DPC(MSU,1);
 54	 * 
 55	 * @param sif
 56	 *            - byte[] - either SIF or MSU
 57	 * @param shift
 58	 *            - shift in passed byte[]. For MSU its 1. For SIF part of MSU
 59	 *            it will be 0;
 60	 * @return
 61	 */
 62	public static final int getFromSif_DPC(byte[] sif, int shift) {
 63		int dpc = (sif[0 + shift] & 0xff | ((sif[1 + shift] & 0x3f) << 8));
 64		return dpc;
 65	}
 66
 67	/**
 68	 * Retrieves OPC from SIF. SIF starts from byte 1 in MSU. For MSU
 69	 * [SIO,DPC,OPC,SLS,Data], call would look as following: int opc =
 70	 * getFromSif_OPC(MSU,1);
 71	 * 
 72	 * @param sif
 73	 *            - byte[] - either SIF or MSU
 74	 * @param shift
 75	 *            - shift in passed byte[]. For MSU its 1. For SIF part of MSU
 76	 *            it will be 0;
 77	 * @return
 78	 */
 79	public static final int getFromSif_OPC(byte[] sif, int shift) {
 80		int opc = ((sif[1 + shift] & 0xC0) >> 6) | ((sif[2 + shift] & 0xff) << 2) | ((sif[3 + shift] & 0x0f) << 10);
 81		return opc;
 82	}
 83
 84	/**
 85	 * Retrieves SLS from SIF. SIF starts from byte 1 in MSU. For MSU
 86	 * [SIO,DPC,OPC,SLS,Data], call would look as following: int opc =
 87	 * getFromSif_SLS(MSU,1);
 88	 * 
 89	 * @param sif
 90	 *            - byte[] - either SIF or MSU
 91	 * @param shift
 92	 *            - shift in passed byte[]. For MSU its 1. For SIF part of MSU
 93	 *            it will be 0;
 94	 * @return
 95	 */
 96	public static final int getFromSif_SLS(byte[] sif, int shift) {
 97		int sls = (sif[3 + shift] & 0xf0) >>> 4;
 98		return sls;
 99	}
100
101	public static final int getFromSif_SI(byte[] data) {
102
103		int serviceIndicator = data[0] & 0x0f;
104		return serviceIndicator;
105	}
106
107	public static final int getFromSif_SSI(byte[] data) {
108		// see Q.704.14.2
109		int subserviceIndicator = (data[0] >> 4) & 0x0F;
110		return subserviceIndicator;
111	}
112
113	/**
114	 * Encodes routing label into passed byte[]. It has to be at least 5 bytes
115	 * long!
116	 * 
117	 * @param destination
118	 * @param si
119	 * @param ssi
120	 * @param sls
121	 * @param dpc
122	 * @param opc
123	 */
124	public static void writeRoutingLabel(byte[] destination, int si, int ssi, int sls, int dpc, int opc) {
125		// see Q.704.14.2
126		destination[0] = (byte) (((ssi & 0x0F) << 4) | (si & 0x0F));
127		destination[1] = (byte) dpc;
128		destination[2] = (byte) (((dpc >> 8) & 0x3F) | ((opc & 0x03) << 6));
129		destination[3] = (byte) (opc >> 2);
130		destination[4] = (byte) (((opc >> 10) & 0x0F) | ((sls & 0x0F) << 4));
131		// sif[4] = (byte) (((opc>> 10) & 0x0F) | ((0 & 0x0F) << 4));
132	}
133
134}