| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2010 by your_name_or_organization, all rights reserved. |
|---|
| 4 | |
|---|
| 5 | ****************************************************************************/ |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #include <cstdio> |
|---|
| 9 | #include <string> |
|---|
| 10 | #include <iostream> |
|---|
| 11 | |
|---|
| 12 | #include "config.h" |
|---|
| 13 | #include "PacketDecoder.h" |
|---|
| 14 | |
|---|
| 15 | // include main.cpp (must define COMPONENT_OBJECT) |
|---|
| 16 | #define COMPONENT_OBJECT PacketDecoder_i |
|---|
| 17 | #include "main.cpp" |
|---|
| 18 | |
|---|
| 19 | PacketDecoder_i::PacketDecoder_i(const char *uuid, omni_condition *condition) : |
|---|
| 20 | Resource_impl(uuid), component_running(condition) |
|---|
| 21 | { |
|---|
| 22 | portCodedDataIn = new standardInterfaces_i::realChar_p("CodedDataIn"); |
|---|
| 23 | portDecodedDataOut = new standardInterfaces_i::realChar_u("DecodedDataOut"); |
|---|
| 24 | |
|---|
| 25 | // create DSP packetizer object |
|---|
| 26 | fec_inner = FEC_NONE; |
|---|
| 27 | fec_outer = FEC_HAMMING74; |
|---|
| 28 | crc = CRC_NONE; |
|---|
| 29 | dec_msg_len = 0; |
|---|
| 30 | // enc_msg_len = packetizer_get_packet_length(dec_msg_len,fec_inner,fec_outer); |
|---|
| 31 | enc_msg_len = packetizer_compute_enc_msg_len(dec_msg_len, crc, fec_inner, fec_outer); |
|---|
| 32 | p = packetizer_create(dec_msg_len,crc,fec_inner,fec_outer); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | PacketDecoder_i::~PacketDecoder_i(void) |
|---|
| 36 | { |
|---|
| 37 | delete portCodedDataIn; |
|---|
| 38 | delete portDecodedDataOut; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | // Static function for omni thread |
|---|
| 42 | void PacketDecoder_i::Run( void * data ) |
|---|
| 43 | { |
|---|
| 44 | ((PacketDecoder_i*)data)->ProcessData(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | CORBA::Object_ptr PacketDecoder_i::getPort( const char* portName ) throw ( |
|---|
| 48 | CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 49 | { |
|---|
| 50 | DEBUG(3, PacketDecoder, "getPort() invoked with " << portName) |
|---|
| 51 | |
|---|
| 52 | CORBA::Object_var p; |
|---|
| 53 | |
|---|
| 54 | p = portCodedDataIn->getPort(portName); |
|---|
| 55 | |
|---|
| 56 | if (!CORBA::is_nil(p)) |
|---|
| 57 | return p._retn(); |
|---|
| 58 | |
|---|
| 59 | p = portDecodedDataOut->getPort(portName); |
|---|
| 60 | |
|---|
| 61 | if (!CORBA::is_nil(p)) |
|---|
| 62 | return p._retn(); |
|---|
| 63 | |
|---|
| 64 | /*exception*/ |
|---|
| 65 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void PacketDecoder_i::start() throw (CORBA::SystemException, |
|---|
| 69 | CF::Resource::StartError) |
|---|
| 70 | { |
|---|
| 71 | DEBUG(3, PacketDecoder, "start() invoked") |
|---|
| 72 | omni_mutex_lock l(processing_mutex); |
|---|
| 73 | if( false == thread_started ) |
|---|
| 74 | { |
|---|
| 75 | thread_started = true; |
|---|
| 76 | // Create the thread for the writer's processing function |
|---|
| 77 | processing_thread = new omni_thread(Run, (void *) this); |
|---|
| 78 | |
|---|
| 79 | // Start the thread containing the writer's processing function |
|---|
| 80 | processing_thread->start(); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void PacketDecoder_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 85 | { |
|---|
| 86 | DEBUG(3, PacketDecoder, "stop() invoked") |
|---|
| 87 | omni_mutex_lock l(processing_mutex); |
|---|
| 88 | thread_started = false; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void PacketDecoder_i::releaseObject() throw (CORBA::SystemException, |
|---|
| 92 | CF::LifeCycle::ReleaseError) |
|---|
| 93 | { |
|---|
| 94 | DEBUG(3, PacketDecoder, "releaseObject() invoked") |
|---|
| 95 | |
|---|
| 96 | component_running->signal(); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void PacketDecoder_i::initialize() throw (CF::LifeCycle::InitializeError, |
|---|
| 100 | CORBA::SystemException) |
|---|
| 101 | { |
|---|
| 102 | DEBUG(3, PacketDecoder, "initialize() invoked") |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void PacketDecoder_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 106 | { |
|---|
| 107 | if( configProperties.length() == 0 ) |
|---|
| 108 | { |
|---|
| 109 | configProperties.length( propertySet.length() ); |
|---|
| 110 | for( unsigned int i = 0; i < propertySet.length(); i++ ) |
|---|
| 111 | { |
|---|
| 112 | configProperties[i].id = CORBA::string_dup( propertySet[i].id ); |
|---|
| 113 | configProperties[i].value = propertySet[i].value; |
|---|
| 114 | } |
|---|
| 115 | return; |
|---|
| 116 | } else { |
|---|
| 117 | for( unsigned int i = 0; i < configProperties.length(); i++ ) { |
|---|
| 118 | for( unsigned int j = 0; j < propertySet.length(); j++ ) { |
|---|
| 119 | if( strcmp(configProperties[i].id, propertySet[i].id) == 0 ) { |
|---|
| 120 | configProperties[i].value = propertySet[i].value; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } // end if-else |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | void PacketDecoder_i::configure(const CF::Properties& props) |
|---|
| 128 | throw (CORBA::SystemException, |
|---|
| 129 | CF::PropertySet::InvalidConfiguration, |
|---|
| 130 | CF::PropertySet::PartialConfiguration) |
|---|
| 131 | { |
|---|
| 132 | DEBUG(3, PacketDecoder, "configure() invoked") |
|---|
| 133 | |
|---|
| 134 | static int init = 0; |
|---|
| 135 | if( init == 0 ) { |
|---|
| 136 | if( props.length() == 0 ) { |
|---|
| 137 | std::cout << "configure called with invalid props.length - " << props.length() << std::endl; |
|---|
| 138 | return; |
|---|
| 139 | } |
|---|
| 140 | propertySet.length(props.length()); |
|---|
| 141 | for( unsigned int j=0; j < props.length(); j++ ) { |
|---|
| 142 | propertySet[j].id = CORBA::string_dup(props[j].id); |
|---|
| 143 | propertySet[j].value = props[j].value; |
|---|
| 144 | } |
|---|
| 145 | init = 1; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | std::cout << "props length : " << props.length() << std::endl; |
|---|
| 149 | |
|---|
| 150 | for ( unsigned int i = 0; i <props.length(); i++) |
|---|
| 151 | { |
|---|
| 152 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 153 | |
|---|
| 154 | if (strcmp(props[i].id, "DCE:659f6164-1017-11df-baab-001aa089d644") == 0) |
|---|
| 155 | { |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void PacketDecoder_i::ProcessData() |
|---|
| 161 | { |
|---|
| 162 | DEBUG(3, PacketDecoder, "ProcessData() invoked") |
|---|
| 163 | |
|---|
| 164 | PortTypes::CharSequence decodedDataOut; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | PortTypes::CharSequence *encodedDataIn(NULL); |
|---|
| 168 | CORBA::UShort encodedDataIn_length; |
|---|
| 169 | |
|---|
| 170 | while(continue_processing()) |
|---|
| 171 | { |
|---|
| 172 | portCodedDataIn->getData(encodedDataIn); |
|---|
| 173 | |
|---|
| 174 | encodedDataIn_length = encodedDataIn->length(); |
|---|
| 175 | |
|---|
| 176 | // check input length and re-create packetizer (if necessary) |
|---|
| 177 | if (enc_msg_len != encodedDataIn_length) { |
|---|
| 178 | // re-compute message lengths |
|---|
| 179 | enc_msg_len = encodedDataIn_length; |
|---|
| 180 | |
|---|
| 181 | // initial estimate of dec_msg_len |
|---|
| 182 | float rate_inner = fec_get_rate(fec_inner); |
|---|
| 183 | float rate_outer = fec_get_rate(fec_outer); |
|---|
| 184 | dec_msg_len = (unsigned int)(0.8f * rate_inner * rate_outer * enc_msg_len); |
|---|
| 185 | unsigned int n; |
|---|
| 186 | do { |
|---|
| 187 | dec_msg_len++; |
|---|
| 188 | |
|---|
| 189 | n = packetizer_compute_enc_msg_len(dec_msg_len, crc, fec_inner, fec_outer); |
|---|
| 190 | } while (n < enc_msg_len); |
|---|
| 191 | |
|---|
| 192 | if (n != enc_msg_len) { |
|---|
| 193 | //std::cerr << "error: PacketDecoder cannot decode packet with this configuration" << std::endl; |
|---|
| 194 | fprintf(stderr,"error: PacketDecoder cannot decode packet with this configuration"); |
|---|
| 195 | fprintf(stderr," fec_inner : %2u (rate = %12.8f)\n", fec_inner, rate_inner); |
|---|
| 196 | fprintf(stderr," fec_outer : %2u (rate = %12.8f)\n", fec_outer, rate_outer); |
|---|
| 197 | fprintf(stderr," enc msg len : %4u\n", enc_msg_len); |
|---|
| 198 | throw(0); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | // destroy and re-create packetizer object |
|---|
| 202 | packetizer_destroy(p); |
|---|
| 203 | p = packetizer_create(dec_msg_len, |
|---|
| 204 | crc, |
|---|
| 205 | fec_inner, |
|---|
| 206 | fec_outer); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | // create buffers (not efficient to do every time) |
|---|
| 210 | unsigned char dec_msg[dec_msg_len]; |
|---|
| 211 | unsigned char enc_msg[enc_msg_len]; |
|---|
| 212 | |
|---|
| 213 | // copy input to buffer |
|---|
| 214 | for (unsigned int i=0; i<enc_msg_len; i++) |
|---|
| 215 | enc_msg[i] = (*encodedDataIn)[i]; |
|---|
| 216 | portCodedDataIn->bufferEmptied(); |
|---|
| 217 | |
|---|
| 218 | // decode the packet |
|---|
| 219 | packetizer_decode(p, enc_msg, dec_msg); |
|---|
| 220 | |
|---|
| 221 | // copy result to output buffer |
|---|
| 222 | decodedDataOut.length(dec_msg_len); |
|---|
| 223 | for (unsigned int i=0; i<dec_msg_len; i++) |
|---|
| 224 | decodedDataOut[i] = dec_msg[i]; |
|---|
| 225 | |
|---|
| 226 | portDecodedDataOut->pushPacket(decodedDataOut); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | bool PacketDecoder_i::continue_processing() |
|---|
| 231 | { |
|---|
| 232 | omni_mutex_lock l(processing_mutex); |
|---|
| 233 | return thread_started; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|