1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 #include <anna/diameter/codec/tme/Avp.hpp>
39 #include <anna/diameter/codec/tme/Engine.hpp>
41 #include <anna/diameter/helpers/defines.hpp>
42 #include <anna/diameter/stack/Format.hpp>
43 #include <anna/core/functions.hpp>
45 #include <anna/xml/xml.hpp>
48 using namespace anna::diameter::codec::tme;
52 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------- Avp::Avp()
54 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------- Avp::Avp()
62 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------ Avp::~Avp()
71 //------------------------------------------------------------------------------
77 //------------------------------------------------------------------------------
78 //------------------------------------------------------------- Avp::getEngine()
79 //------------------------------------------------------------------------------
80 anna::diameter::codec::Engine * Avp::getEngine() const throw(anna::RuntimeException) {
81 return a_engine ? a_engine : (a_engine = (anna::diameter::codec::Engine *)anna::functions::component <Engine> (ANNA_FILE_LOCATION));
85 //------------------------------------------------------------------------------
86 //---------------------------------------------------- Avp::initializeByFormat()
87 //------------------------------------------------------------------------------
88 void Avp::initializeByFormat() throw() {
95 //------------------------------------------------------------------------------
96 //--------------------------------------------------------- Avp::clearByFormat()
97 //------------------------------------------------------------------------------
98 void Avp::clearByFormat() throw() {
100 delete a_ISDNAddress;
105 //------------------------------------------------------------------------------
106 //---------------------------------------------------- Avp::allocationByFormat()
107 //------------------------------------------------------------------------------
108 void Avp::allocationByFormat(const stack::Format *stackFormat) throw() {
109 if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber = new ISDNNumber();
110 else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress = new ISDNAddress();
111 else if(stackFormat->getName() == "Unsigned16") a_Unsigned16 = new Unsigned16();
115 //------------------------------------------------------------------------------
116 //----------------------------------------------------- Avp::getLengthByFormat()
117 //------------------------------------------------------------------------------
118 U24 Avp::getLengthByFormat(const stack::Format *stackFormat) const throw() {
121 if(stackFormat->getName() == "ISDNNumber") result += a_ISDNNumber->getSize();
122 else if(stackFormat->getName() == "ISDNAddress") result += a_ISDNAddress->getSize();
123 else if(stackFormat->getName() == "Unsigned16") result += a_Unsigned16->getSize();
129 //------------------------------------------------------------------------------
130 //------------------------------------------------ Avp::decodeDataPartByFormat()
131 //------------------------------------------------------------------------------
132 void Avp::decodeDataPartByFormat(const char * buffer, int size, const stack::Format *stackFormat) throw(anna::RuntimeException) {
133 if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->decode(buffer, size);
134 else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->decode(buffer, size);
135 else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->decode(buffer, size);
139 //------------------------------------------------------------------------------
140 //---------------------------------------------------------- Avp::codeByFormat()
141 //------------------------------------------------------------------------------
142 void Avp::codeByFormat(char* dataPart, const stack::Format *stackFormat) const throw(anna::RuntimeException) {
145 if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->code(dataPart, dataBytes);
146 else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->code(dataPart, dataBytes);
147 else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->code(dataPart, dataBytes);
151 //------------------------------------------------------------------------------
152 //---------------------------------------------------- Avp::getXMLdataByFormat()
153 //------------------------------------------------------------------------------
154 std::string Avp::getXMLdataByFormat(bool & isHex, const stack::Format *stackFormat) const throw() {
157 if(stackFormat->getName() == "ISDNNumber") {
159 return a_ISDNNumber->asHexString();
160 } else if(stackFormat->getName() == "ISDNAddress") {
162 return a_ISDNAddress->asHexString();
163 } else if(stackFormat->getName() == "Unsigned16") return a_Unsigned16->asPrintableString();
169 //------------------------------------------------------------------------------
170 //------------------------------------------------------- Avp::fromXMLByFormat()
171 //------------------------------------------------------------------------------
172 void Avp::fromXMLByFormat(const anna::xml::Attribute* data, const anna::xml::Attribute* hexData, const stack::Format *stackFormat) throw(anna::RuntimeException) {
173 if(stackFormat->getName() == "ISDNNumber") {
174 if(data) a_ISDNNumber->fromPrintableString(data->getValue().c_str());
175 else if(hexData) a_ISDNNumber->fromHexString(hexData->getValue());
176 } else if(stackFormat->getName() == "ISDNAddress") {
177 if(data) a_ISDNAddress->fromPrintableString(data->getValue().c_str());
178 else if(hexData) a_ISDNAddress->fromHexString(hexData->getValue());
179 } else if(stackFormat->getName() == "Unsigned16") {
180 if(data) a_Unsigned16->fromPrintableString(data->getValue().c_str());
181 else if(hexData) a_Unsigned16->fromHexString(hexData->getValue());