root/ossiedev/branches/jgaeddert/0.8.0/components/src/PacketDecoder.cpp @ 10639

Revision 10639, 7.2 KB (checked in by jgaeddert, 2 years ago)

updating components to work with liquid-dsp HEAD

Line 
1/****************************************************************************
2
3Copyright 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
19PacketDecoder_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    check     = CRC_16;
27    fec_inner = FEC_NONE;
28    fec_outer = FEC_HAMMING128;
29    dec_msg_len = 0;
30    enc_msg_len = packetizer_compute_enc_msg_len(dec_msg_len,check,fec_inner,fec_outer);
31    p = packetizer_create(dec_msg_len,check,fec_inner,fec_outer);
32}
33
34PacketDecoder_i::~PacketDecoder_i(void)
35{
36    delete portCodedDataIn;
37    delete portDecodedDataOut;
38}
39
40// Static function for omni thread
41void PacketDecoder_i::Run( void * data )
42{
43    ((PacketDecoder_i*)data)->ProcessData();
44}
45
46CORBA::Object_ptr PacketDecoder_i::getPort( const char* portName ) throw (
47    CORBA::SystemException, CF::PortSupplier::UnknownPort)
48{
49    DEBUG(3, PacketDecoder, "getPort() invoked with " << portName)
50
51    CORBA::Object_var p;
52
53    p = portCodedDataIn->getPort(portName);
54
55    if (!CORBA::is_nil(p))
56        return p._retn();
57
58    p = portDecodedDataOut->getPort(portName);
59
60    if (!CORBA::is_nil(p))
61        return p._retn();
62
63    /*exception*/
64    throw CF::PortSupplier::UnknownPort();
65}
66
67void PacketDecoder_i::start() throw (CORBA::SystemException,
68    CF::Resource::StartError)
69{
70    DEBUG(3, PacketDecoder, "start() invoked")
71    omni_mutex_lock  l(processing_mutex);
72    if( false == thread_started )
73    {
74        thread_started = true;
75        // Create the thread for the writer's processing function
76        processing_thread = new omni_thread(Run, (void *) this);
77
78        // Start the thread containing the writer's processing function
79        processing_thread->start();
80    }
81}
82
83void PacketDecoder_i::stop() throw (CORBA::SystemException, CF::Resource::StopError)
84{
85    DEBUG(3, PacketDecoder, "stop() invoked")
86    omni_mutex_lock l(processing_mutex);
87    thread_started = false;
88}
89
90void PacketDecoder_i::releaseObject() throw (CORBA::SystemException,
91    CF::LifeCycle::ReleaseError)
92{
93    DEBUG(3, PacketDecoder, "releaseObject() invoked")
94
95    component_running->signal();
96}
97
98void PacketDecoder_i::initialize() throw (CF::LifeCycle::InitializeError,
99    CORBA::SystemException)
100{
101    DEBUG(3, PacketDecoder, "initialize() invoked")
102}
103
104void PacketDecoder_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties)
105{
106    if( configProperties.length() == 0 )
107    {
108        configProperties.length( propertySet.length() );
109        for( unsigned int i = 0; i < propertySet.length(); i++ )
110        {
111            configProperties[i].id = CORBA::string_dup( propertySet[i].id );
112            configProperties[i].value = propertySet[i].value;
113        }
114        return;
115    } else {
116        for( unsigned int i = 0; i < configProperties.length(); i++ ) {
117            for( unsigned int j = 0; j < propertySet.length(); j++ ) {
118                if( strcmp(configProperties[i].id, propertySet[i].id) == 0 ) {
119                    configProperties[i].value = propertySet[i].value;
120                }
121            }
122        }
123    } // end if-else
124}
125
126void PacketDecoder_i::configure(const CF::Properties& props)
127throw (CORBA::SystemException,
128    CF::PropertySet::InvalidConfiguration,
129    CF::PropertySet::PartialConfiguration)
130{
131    DEBUG(3, PacketDecoder, "configure() invoked")
132
133    static int init = 0;
134    if( init == 0 ) {
135        if( props.length() == 0 ) {
136            std::cout << "configure called with invalid props.length - " << props.length() << std::endl;
137            return;
138        }
139        propertySet.length(props.length());
140        for( unsigned int j=0; j < props.length(); j++ ) {
141            propertySet[j].id = CORBA::string_dup(props[j].id);
142            propertySet[j].value = props[j].value;
143        }
144        init = 1;
145    }
146
147    std::cout << "props length : " << props.length() << std::endl;
148
149    for ( unsigned int i = 0; i <props.length(); i++)
150    {
151        std::cout << "Property id : " << props[i].id << std::endl;
152
153        if (strcmp(props[i].id, "DCE:659f6164-1017-11df-baab-001aa089d644") == 0)
154        {
155        }
156    }
157}
158
159void PacketDecoder_i::ProcessData()
160{
161    DEBUG(3, PacketDecoder, "ProcessData() invoked")
162
163    PortTypes::CharSequence decodedDataOut;
164
165
166    PortTypes::CharSequence *encodedDataIn(NULL);
167    CORBA::UShort encodedDataIn_length;
168
169    while(continue_processing())
170    {
171        portCodedDataIn->getData(encodedDataIn);
172
173        encodedDataIn_length = encodedDataIn->length();
174
175        // check input length and re-create packetizer (if necessary)
176        if (enc_msg_len != encodedDataIn_length) {
177            // re-compute message lengths
178            enc_msg_len = encodedDataIn_length;
179
180            dec_msg_len = packetizer_compute_dec_msg_len(enc_msg_len,
181                                                         check,
182                                                         fec_inner,
183                                                         fec_outer);
184
185            // re-create packetizer object
186            p = packetizer_recreate(p,dec_msg_len,check,fec_inner,fec_outer);
187
188            // validate that encoded message lengths match
189            unsigned int n = packetizer_get_enc_msg_len(p);
190
191            if (n != enc_msg_len) {
192                fprintf(stderr,"error: PacketDecoder cannot decode packet with this configuration");
193                fprintf(stderr,"    (received %u encoded bytes, expected %u)\n", enc_msg_len, n);
194                fprintf(stderr,"    check       : %10s\n", crc_scheme_str[check][0]);
195                fprintf(stderr,"    fec_inner   : %10s\n", fec_scheme_str[fec_inner][0]);
196                fprintf(stderr,"    fec_outer   : %10s\n", fec_scheme_str[fec_outer][0]);
197                fprintf(stderr,"    dec msg len : %4u\n", dec_msg_len);
198                fprintf(stderr,"    enc msg len : %4u\n", enc_msg_len);
199                throw(0);
200            }
201
202        }
203
204        // create buffers (not efficient to do every time)
205        // TODO : allocate buffers internally, realloc only when necessary
206        unsigned char dec_msg[dec_msg_len];
207        unsigned char enc_msg[enc_msg_len];
208
209        // copy input to buffer
210        for (unsigned int i=0; i<enc_msg_len; i++)
211            enc_msg[i] = (*encodedDataIn)[i];
212        portCodedDataIn->bufferEmptied();
213
214        // decode the packet
215        packetizer_decode(p, enc_msg, dec_msg);
216
217        // copy result to output buffer
218        decodedDataOut.length(dec_msg_len);
219        for (unsigned int i=0; i<dec_msg_len; i++)
220            decodedDataOut[i] = dec_msg[i];
221
222        // push decoded data packet
223        portDecodedDataOut->pushPacket(decodedDataOut);
224    }
225}
226
227bool PacketDecoder_i::continue_processing()
228{
229    omni_mutex_lock l(processing_mutex);
230    return thread_started;
231}
232
233
Note: See TracBrowser for help on using the browser.