root/ossiedev/trunk/components/SymbolSyncPoly-metadata/src/FrameSynchronizer_metadata.cpp @ 8023

Revision 8023, 15.4 KB (checked in by hvolos, 5 years ago)

FrameSyncrhonizer? new property ids generated

  • Property svn:eol-style set to native
Line 
1/****************************************************************************
2
3Copyright 2007 Virginia Polytechnic Institute and State University
4
5This file is part of the OSSIE FrameSynchronizer_metadata.
6
7OSSIE FrameSynchronizer_metadata is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12OSSIE FrameSynchronizer_metadata is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OSSIE FrameSynchronizer_metadata; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21****************************************************************************/
22
23
24#include <string>
25#include <iostream>
26#include <fstream>
27#include "FrameSynchronizer_metadata.h"
28
29#undef LOGGING
30
31FrameSynchronizer_metadata_i::FrameSynchronizer_metadata_i(const char *uuid, omni_condition *condition) :
32    Resource_impl(uuid), component_running(condition)
33{
34    dataIn_0 = new standardInterfaces_i::complexShort_p("IF_in");
35    dataOut_0 = new standardInterfaces_i::complexShort_u("symbols_out");
36
37
38    //Create the thread for the writer's processing function
39    processing_thread = new omni_thread(Run, (void *) this);
40
41    //Start the thread containing the writer's processing function
42    processing_thread->start();
43
44    isConfigured_pulseShape = false;
45    isConfigured_k          = false;
46    isConfigured_m          = false;
47    isConfigured_beta       = false;
48    isConfigured_Npfb       = false;
49}
50
51FrameSynchronizer_metadata_i::~FrameSynchronizer_metadata_i(void)
52{   
53    delete dataIn_0;
54    delete dataOut_0;
55}
56
57// Static function for omni thread
58void FrameSynchronizer_metadata_i::Run( void * data )
59{
60    ((FrameSynchronizer_metadata_i*)data)->ProcessData();
61}
62
63CORBA::Object_ptr FrameSynchronizer_metadata_i::getPort( const char* portName ) throw (
64    CORBA::SystemException, CF::PortSupplier::UnknownPort)
65{
66    DEBUG(3, FrameSynchronizer_metadata, "getPort() invoked with " << portName)
67   
68    CORBA::Object_var p;
69
70    p = dataIn_0->getPort(portName);
71
72    if (!CORBA::is_nil(p))
73        return p._retn();
74
75    p = dataOut_0->getPort(portName);
76
77    if (!CORBA::is_nil(p))
78        return p._retn();
79
80    /*exception*/
81    throw CF::PortSupplier::UnknownPort();
82}
83
84void FrameSynchronizer_metadata_i::start() throw (CORBA::SystemException,
85    CF::Resource::StartError)
86{
87    DEBUG(3, FrameSynchronizer_metadata, "start() invoked")
88}
89
90void FrameSynchronizer_metadata_i::stop() throw (CORBA::SystemException, CF::Resource::StopError)
91
92    DEBUG(3, FrameSynchronizer_metadata, "stop() invoked")
93}
94
95void FrameSynchronizer_metadata_i::releaseObject() throw (CORBA::SystemException,
96    CF::LifeCycle::ReleaseError)
97{
98    DEBUG(3, FrameSynchronizer_metadata, "releaseObject() invoked")
99   
100    component_running->signal();
101}
102
103void FrameSynchronizer_metadata_i::initialize() throw (CF::LifeCycle::InitializeError,
104    CORBA::SystemException)
105{
106    DEBUG(3, FrameSynchronizer_metadata, "initialize() invoked")
107}
108
109void FrameSynchronizer_metadata_i::configure(const CF::Properties& props)
110throw (CORBA::SystemException,
111    CF::PropertySet::InvalidConfiguration,
112    CF::PropertySet::PartialConfiguration)
113{
114    DEBUG(3, FrameSynchronizer_metadata, "configure() invoked")
115   
116    std::cout << "props length : " << props.length() << std::endl;
117
118    for (unsigned int i = 0; i <props.length(); i++) {
119        std::cout << "Property id : " << props[i].id << std::endl;
120
121        if (strcmp(props[i].id, "DCE:48576cfb-849b-4856-8b53-b3ab215df53c") == 0) {
122            // pulse shape
123            const char * prop_str;
124            props[i].value >>= prop_str;
125
126            // Set appropriate pulse shape
127            if ( strcmp(prop_str, "rrcos") == 0 ) {
128                _pulseShape = "rrcos";
129            } else {
130                // unknown pulse shape
131                std::cerr << "ERROR: FrameSynchronizer_metadata::configure() unknown pulse shape "
132                          << prop_str << std::endl;
133                throw CF::PropertySet::InvalidConfiguration();
134            }
135            isConfigured_pulseShape = true;
136
137        } else if (strcmp(props[i].id, "DCE:3bc8ae91-fa37-4905-aeeb-dff58870b76e") == 0) {
138            // k : samples per symbol
139            CORBA::UShort simple_temp;
140            props[i].value >>= simple_temp;
141            _k = simple_temp;
142            isConfigured_k = true;
143        } else if (strcmp(props[i].id, "DCE:c70a36df-a580-4e1e-90d6-05a1b713edd4") == 0) {
144            // m : symbol delay
145            CORBA::UShort simple_temp;
146            props[i].value >>= simple_temp;
147            _m = simple_temp;
148            isConfigured_m = true;
149        } else if (strcmp(props[i].id, "DCE:c233bee5-9696-4b37-b01e-3a910f77afe6") == 0) {
150            // beta : excess bandwidth factor
151            CORBA::Float simple_temp;
152            props[i].value >>= simple_temp;
153            _beta = simple_temp;
154            isConfigured_beta = true;
155        } else if (strcmp(props[i].id, "DCE:0b80d4ae-b108-4e6c-8755-dc10d8f46f3b") == 0) {
156            // Npfb : number of filters in bank
157            CORBA::UShort simple_temp;
158            props[i].value >>= simple_temp;
159            _Npfb = simple_temp;
160            isConfigured_Npfb = true;
161        } else {
162            // unknown property
163            std::cerr << "ERROR: FrameSynchronizer_metadata::configure() unknown property \""
164               << props[i].id << "\"" << std::endl;
165            throw CF::PropertySet::InvalidConfiguration();
166        }
167    }
168
169    // Once all properties have been configured, create the filter bank
170    if ( isConfigured_pulseShape    &&
171         isConfigured_k             &&
172         isConfigured_m             &&
173         isConfigured_beta          &&
174         isConfigured_Npfb )
175    {
176        DEBUG(5, FrameSynchronizer_metadata, "configuring filter bank for " <<
177              _pulseShape << ", k=" << _k << ", m=" << _m << ", beta=" << _beta << ", Npfb=" << _Npfb)
178        ConfigureFilterBank(_pulseShape, _k, _m, _beta, _Npfb);
179    }
180}
181
182void FrameSynchronizer_metadata_i::SetMetaDataModulationScheme()
183{
184    switch (mod_scheme) {
185    case SigProc::BPSK:
186        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::PSK;
187        metadata->modulation_scheme.M = 2;
188        break;
189    case SigProc::QPSK:
190        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::PSK;
191        metadata->modulation_scheme.M = 4;
192        break;
193    case SigProc::PSK8:
194        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::PSK;
195        metadata->modulation_scheme.M = 8;
196        break;
197    case SigProc::QAM16:
198        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::QAM;
199        metadata->modulation_scheme.M = 16;
200        break;
201    case SigProc::PAM4:
202        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::PAM;
203        metadata->modulation_scheme.M = 4;
204        break;
205    default:
206        std::cout << "  ::::  WARNING: FrameSynchronizer_metadata: unknown modulation scheme  ::::" << std::endl;
207        metadata->modulation_scheme.scheme = standardInterfaces::ModulationScheme::UNKNOWN;
208        break;
209    }
210}
211
212void FrameSynchronizer_metadata_i::ProcessData()
213{
214    DEBUG(3, FrameSynchronizer_metadata, "ProcessData() invoked")
215
216#ifdef LOGGING
217    std::ofstream input_log;
218    input_log.open("fs_input_log.dat");
219
220    std::ofstream output_log;
221    output_log.open("fs_output_log.dat");
222#endif
223
224
225
226    PortTypes::ShortSequence I_out_0, Q_out_0;
227
228    PortTypes::ShortSequence *I_in_0(NULL), *Q_in_0(NULL);
229    unsigned int I_in_0_length, Q_in_0_length, N_in(0), N_out(0);
230    unsigned int buf_len(1024);
231    short * I_in = new short[buf_len];
232    short * Q_in = new short[buf_len];
233    short * I_out = new short[buf_len];
234    short * Q_out = new short[buf_len];
235
236    bool extracted;
237    unsigned int nr;        // num samples read
238    unsigned int nw;        // num samples written
239    unsigned int nr_total;  // total samples read
240    unsigned int nw_total;  // total samples written
241
242    float rssi_threshold_dB(3.0f);
243    unsigned int rssi_pass_num(3);
244    unsigned int rssi_counter(0);
245
246    while( true ) {
247        DEBUG(7, FrameSynchronizer_metadata, "iterating loop...")
248
249        // Get data from port
250        dataIn_0->getData(I_in_0, Q_in_0, metadata);
251
252        DEBUG(7, FrameSynchronizer_metadata, "Got " << I_in_0->length() << " samples")
253
254        DEBUG(7, FrameSync, "rx signal strength: "<< (float) (metadata->signal_strength) << "dB");
255
256        if (metadata->signal_strength < rssi_threshold_dB) {
257            rssi_counter++;
258            if (rssi_counter > rssi_pass_num) {
259                dataIn_0->bufferEmptied();
260                continue;
261            }
262        } else {
263            rssi_counter = 0;
264        }
265
266        // Adjust sampling frequency in metadata
267        metadata->sampling_frequency /= float(k);
268
269        // Read input data length
270        I_in_0_length = I_in_0->length();
271        Q_in_0_length = Q_in_0->length();
272
273        if ( I_in_0_length != Q_in_0_length ) {
274            // Not sure how to handle multiple-length inputs
275            std::cerr << "ERROR: FrameSynchronizer_metadata_i::ProcessData()" << std::endl
276                      << "  => Not sure how to handle multiple-length inputs" << std::endl;
277            throw 0;
278        } else {
279            N_in = I_in_0_length;
280        }
281
282        /// \todo Initialize I_in and Q_in from CORBA sequence instead
283        /// of copying values in loop
284        DEBUG(7, FrameSynchronizer_metadata, "Copying data from CORBA sequence...")
285        /// \todo This is incredibly inefficient; need to manage memory better
286        if (N_in > buf_len) {
287            delete [] I_in;
288            delete [] Q_in;
289            delete [] I_out;
290            delete [] Q_out;
291            buf_len = N_in;
292
293            I_in = new short[buf_len];
294            Q_in = new short[buf_len];
295            I_out = new short[buf_len];
296            Q_out = new short[buf_len];
297            N_out = N_in;
298        }
299
300        // copy data to input buffer
301        for (unsigned int ii=0; ii<N_in; ii++) {
302            I_in[ii] = (*I_in_0)[ii];
303            Q_in[ii] = (*Q_in_0)[ii];
304
305            I_out[ii] = 0;
306            Q_out[ii] = 0;
307        }
308
309#ifdef LOGGING
310        for (unsigned int ii=0; ii<I_in_0_length; ii++)
311            input_log << I_in[ii] << "  " << Q_in[ii] << std::endl;
312#endif
313
314#if 0
315        printf("running...\n");
316        N_out = buf_len;
317        // Main signal processing algorithm
318        DEBUG(7, FrameSynchronizer_metadata, "Attempting to synchronize and decimate...")
319        SynchronizeAndDecimate(
320            I_in, Q_in, N_in,
321            I_out, Q_out, N_out);
322
323        if ( N_out == 0 ) {
324            // release input buffer
325            dataIn_0->bufferEmptied();
326            continue;
327        }
328
329        DEBUG(7, FrameSynchronizer_metadata, "Setting output length (" << N_out << ")...")
330        I_out_0.length(N_out);
331        Q_out_0.length(N_out);
332
333        /// \todo Initialize I_out_0 and Q_out_0 from array instead
334        /// of copying values in loop
335        DEBUG(7, FrameSynchronizer_metadata, "Copying data to CORBA sequence...")
336        for (unsigned int ii=0; ii<N_out; ii++) {
337            I_out_0[ii] = I_out[ii];
338            Q_out_0[ii] = Q_out[ii];
339        }
340
341        // release input buffer
342        dataIn_0->bufferEmptied();
343
344        SetMetaDataModulationScheme();
345        DEBUG(7, FrameSynchronizer_metadata, "pushing " << I_out_0.length() << " samples")
346        dataOut_0->pushPacket(I_out_0, Q_out_0, *metadata);
347
348        continue;
349#endif
350
351
352        nr=0;           // num samples read
353        nw=0;           // num samples written
354        nr_total=0;     // total samples read
355        nw_total=0;     // total samples written
356
357        while ( nr_total < N_in ) {
358            switch (operationalMode) {
359            case EXTRACT_PN_FRAME_SYNC_CODE:
360                DEBUG(5, FrameSynchronizer_metadata, "trying to extract frame sync code...");
361                extracted = FindFrameHeader(
362                    &I_in[nr_total],  &Q_in[nr_total],  N_in-nr_total,  nr,
363                    &I_out[nw_total], &Q_out[nw_total], N_out-nw_total, nw);
364                nr_total += nr;
365                //nw_total += nw; // uncomment to allow preamble data to be pushed
366
367                if (!extracted)
368                    break;
369
370            case EXTRACT_CONTROL_CODES:
371                DEBUG(5, FrameSynchornizer, "trying to extract frame header...");
372                extracted = ExtractFrameHeader(
373                    &I_in[nr_total],  &Q_in[nr_total],  N_in-nr_total,  nr,
374                    &I_out[nw_total], &Q_out[nw_total], N_out-nw_total, nw);
375                nr_total += nr;
376                //nw_total += nw; // uncomment to allow control codes to be pushed
377
378                if (!extracted)
379                    break;
380
381            case DECODE_FRAME_HEADER:
382                DEBUG(5, FrameSynchronizer_metadata, "trying to decode frame header...");
383                DecodeFrameHeader();
384                SetMetaDataModulationScheme();
385
386            case EXTRACT_FRAME:
387                DEBUG(5, FrameSynchronizer_metadata, "trying to extract frame symbols...");
388                extracted = ExtractFrameSymbols(
389                    &I_in[nr_total],  &Q_in[nr_total],  N_in-nr_total,  nr,
390                    &I_out[nw_total], &Q_out[nw_total], N_out-nw_total, nw);
391                nr_total += nr;
392                nw_total += nw;
393
394                break;
395            case EXTRACT_EOM_CODE:
396                // set to BPSK
397                SetMetaDataModulationScheme();
398                DEBUG(5, FrameSynchronizer_metadata, "trying to extract EOM code...");
399                extracted = ExtractFrameEOM(
400                    &I_in[nr_total],  &Q_in[nr_total],  N_in-nr_total,  nr,
401                    &I_out[nw_total], &Q_out[nw_total], N_out-nw_total, nw);
402                nr_total += nr;
403                nw_total += nw;
404
405                break;
406            default:
407                std::cerr << "ERROR! FrameSynchronizer_metadata: unknown operational mode!" << std::endl;
408                throw 0;
409            }
410       
411            //usleep(2000000);
412            // push data to output buffer
413            if ( nw_total == 0 )
414                continue;
415
416            DEBUG(7, FrameSynchronizer_metadata, "Setting output length (" << nw_total << ")...")
417            I_out_0.length(nw_total);
418            Q_out_0.length(nw_total);
419
420            /// \todo Initialize I_out_0 and Q_out_0 from array instead
421            /// of copying values in loop
422            DEBUG(7, FrameSynchronizer_metadata, "Copying data to CORBA sequence...")
423            for (unsigned int ii=0; ii<nw_total; ii++) {
424                I_out_0[ii] = I_out[ii];
425                Q_out_0[ii] = Q_out[ii];
426            }
427
428            DEBUG(5, FrameSynchronizer_metadata, "pushing " << I_out_0.length() << " samples")
429            dataOut_0->pushPacket(I_out_0, Q_out_0, *metadata);
430        }
431
432        // release input buffer
433        dataIn_0->bufferEmptied();
434
435
436    }
437
438    /// \todo This is incredibly inefficient; need to manage memory better
439    DEBUG(7, FrameSynchronizer_metadata, "Deleting temp buffers...")
440    delete [] I_in;
441    delete [] Q_in;
442
443    delete [] I_out;
444    delete [] Q_out;
445
446#ifdef LOGGING
447    input_log.close();
448    output_log.close();
449#endif
450
451}
452
453
Note: See TracBrowser for help on using the browser.