| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2010 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE FlexframeGen. |
|---|
| 6 | |
|---|
| 7 | OSSIE FlexframeGen is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE FlexframeGen 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 |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE FlexframeGen; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <string> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include <stdio.h> |
|---|
| 27 | |
|---|
| 28 | #include "config.h" |
|---|
| 29 | #include "FlexframeGen.h" |
|---|
| 30 | |
|---|
| 31 | // include main.cpp (must define COMPONENT_OBJECT) |
|---|
| 32 | #define COMPONENT_OBJECT FlexframeGen_i |
|---|
| 33 | #include "main.cpp" |
|---|
| 34 | |
|---|
| 35 | FlexframeGen_i::FlexframeGen_i(const char *uuid, omni_condition *condition) : |
|---|
| 36 | Resource_impl(uuid), component_running(condition) |
|---|
| 37 | { |
|---|
| 38 | headerDataPortIn = new standardInterfaces_i::realChar_p("HeaderDataIn"); |
|---|
| 39 | payloadDataPortIn = new standardInterfaces_i::realChar_p("PayloadDataIn"); |
|---|
| 40 | frameDataPortOut = new standardInterfaces_i::complexFloat_u("FrameSamplesOut"); |
|---|
| 41 | |
|---|
| 42 | // create dsp objects |
|---|
| 43 | fgprops.rampup_len = 16; |
|---|
| 44 | fgprops.phasing_len = 64; |
|---|
| 45 | fgprops.payload_len = 256; |
|---|
| 46 | fgprops.mod_scheme = MOD_QAM; |
|---|
| 47 | fgprops.mod_bps = 4; |
|---|
| 48 | fgprops.rampdn_len = 16; |
|---|
| 49 | |
|---|
| 50 | fg = flexframegen_create(&fgprops); |
|---|
| 51 | |
|---|
| 52 | payload_len = 64; |
|---|
| 53 | payload = (unsigned char*) malloc(payload_len); |
|---|
| 54 | |
|---|
| 55 | frame_len = 1024; |
|---|
| 56 | frame = (std::complex<float>*) malloc(frame_len); |
|---|
| 57 | |
|---|
| 58 | // design rrc filter |
|---|
| 59 | unsigned int k=2; |
|---|
| 60 | unsigned int m=3; |
|---|
| 61 | float beta=0.7f; |
|---|
| 62 | float dt = 0; |
|---|
| 63 | |
|---|
| 64 | interp = interp_crcf_create_rrc(k,m,beta,dt); |
|---|
| 65 | |
|---|
| 66 | interp2 = resamp2_crcf_create(37,0,60); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | FlexframeGen_i::~FlexframeGen_i(void) |
|---|
| 70 | { |
|---|
| 71 | delete headerDataPortIn; |
|---|
| 72 | delete payloadDataPortIn; |
|---|
| 73 | delete frameDataPortOut; |
|---|
| 74 | |
|---|
| 75 | flexframegen_destroy(fg); |
|---|
| 76 | interp_crcf_destroy(interp); |
|---|
| 77 | resamp2_crcf_destroy(interp2); |
|---|
| 78 | |
|---|
| 79 | free(payload); |
|---|
| 80 | free(frame); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // Static function for omni thread |
|---|
| 84 | void FlexframeGen_i::Run( void * data ) |
|---|
| 85 | { |
|---|
| 86 | ((FlexframeGen_i*)data)->ProcessData(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | CORBA::Object_ptr FlexframeGen_i::getPort( const char* portName ) throw ( |
|---|
| 90 | CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 91 | { |
|---|
| 92 | DEBUG(3, FlexframeGen, "getPort() invoked with " << portName) |
|---|
| 93 | |
|---|
| 94 | CORBA::Object_var p; |
|---|
| 95 | |
|---|
| 96 | p = headerDataPortIn->getPort(portName); |
|---|
| 97 | |
|---|
| 98 | if (!CORBA::is_nil(p)) |
|---|
| 99 | return p._retn(); |
|---|
| 100 | |
|---|
| 101 | p = payloadDataPortIn->getPort(portName); |
|---|
| 102 | |
|---|
| 103 | if (!CORBA::is_nil(p)) |
|---|
| 104 | return p._retn(); |
|---|
| 105 | |
|---|
| 106 | p = frameDataPortOut->getPort(portName); |
|---|
| 107 | |
|---|
| 108 | if (!CORBA::is_nil(p)) |
|---|
| 109 | return p._retn(); |
|---|
| 110 | |
|---|
| 111 | /*exception*/ |
|---|
| 112 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void FlexframeGen_i::start() throw (CORBA::SystemException, |
|---|
| 116 | CF::Resource::StartError) |
|---|
| 117 | { |
|---|
| 118 | DEBUG(3, FlexframeGen, "start() invoked") |
|---|
| 119 | omni_mutex_lock l(processing_mutex); |
|---|
| 120 | if( false == thread_started ) |
|---|
| 121 | { |
|---|
| 122 | thread_started = true; |
|---|
| 123 | // Create the thread for the writer's processing function |
|---|
| 124 | processing_thread = new omni_thread(Run, (void *) this); |
|---|
| 125 | |
|---|
| 126 | // Start the thread containing the writer's processing function |
|---|
| 127 | processing_thread->start(); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void FlexframeGen_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 132 | { |
|---|
| 133 | DEBUG(3, FlexframeGen, "stop() invoked") |
|---|
| 134 | omni_mutex_lock l(processing_mutex); |
|---|
| 135 | thread_started = false; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void FlexframeGen_i::releaseObject() throw (CORBA::SystemException, |
|---|
| 139 | CF::LifeCycle::ReleaseError) |
|---|
| 140 | { |
|---|
| 141 | DEBUG(3, FlexframeGen, "releaseObject() invoked") |
|---|
| 142 | |
|---|
| 143 | component_running->signal(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void FlexframeGen_i::initialize() throw (CF::LifeCycle::InitializeError, |
|---|
| 147 | CORBA::SystemException) |
|---|
| 148 | { |
|---|
| 149 | DEBUG(3, FlexframeGen, "initialize() invoked") |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void FlexframeGen_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 153 | { |
|---|
| 154 | if( configProperties.length() == 0 ) |
|---|
| 155 | { |
|---|
| 156 | configProperties.length( propertySet.length() ); |
|---|
| 157 | for(unsigned int i = 0; i < propertySet.length(); i++ ) |
|---|
| 158 | { |
|---|
| 159 | configProperties[i].id = CORBA::string_dup( propertySet[i].id ); |
|---|
| 160 | configProperties[i].value = propertySet[i].value; |
|---|
| 161 | } |
|---|
| 162 | return; |
|---|
| 163 | } else { |
|---|
| 164 | for( unsigned int i = 0; i < configProperties.length(); i++ ) { |
|---|
| 165 | for( unsigned int j = 0; j < propertySet.length(); j++ ) { |
|---|
| 166 | if( strcmp(configProperties[i].id, propertySet[j].id) == 0 ) { |
|---|
| 167 | configProperties[i].value = propertySet[j].value; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } // end if-else |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void FlexframeGen_i::configure(const CF::Properties& props) |
|---|
| 175 | throw (CORBA::SystemException, |
|---|
| 176 | CF::PropertySet::InvalidConfiguration, |
|---|
| 177 | CF::PropertySet::PartialConfiguration) |
|---|
| 178 | { |
|---|
| 179 | DEBUG(3, FlexframeGen, "configure() invoked") |
|---|
| 180 | |
|---|
| 181 | // not sure what the heck this is here for... |
|---|
| 182 | static int init = 0; |
|---|
| 183 | if( init == 0 ) { |
|---|
| 184 | if( props.length() == 0 ) { |
|---|
| 185 | std::cout << "configure called with invalid props.length - " << props.length() << std::endl; |
|---|
| 186 | return; |
|---|
| 187 | } |
|---|
| 188 | propertySet.length(props.length()); |
|---|
| 189 | for( int j=0; j < props.length(); j++ ) { |
|---|
| 190 | propertySet[j].id = CORBA::string_dup(props[j].id); |
|---|
| 191 | propertySet[j].value = props[j].value; |
|---|
| 192 | } |
|---|
| 193 | init = 1; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | std::cout << "props length : " << props.length() << std::endl; |
|---|
| 197 | |
|---|
| 198 | unsigned int ms = fgprops.mod_scheme; |
|---|
| 199 | unsigned int bps = fgprops.mod_bps; |
|---|
| 200 | |
|---|
| 201 | for ( unsigned int i = 0; i <props.length(); i++) { |
|---|
| 202 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 203 | |
|---|
| 204 | if (strcmp(props[i].id, "DCE:5b852bf4-6d83-11df-80dc-001aa089d644") == 0) { |
|---|
| 205 | // verbose |
|---|
| 206 | CORBA::Boolean simple_temp; |
|---|
| 207 | props[i].value >>= simple_temp; |
|---|
| 208 | } else if (strcmp(props[i].id, "DCE:c1612e14-6d83-11df-8500-001aa089d644") == 0) { |
|---|
| 209 | // mod_scheme |
|---|
| 210 | |
|---|
| 211 | const char * prop_str; |
|---|
| 212 | unsigned int M; |
|---|
| 213 | props[i].value >>= prop_str; |
|---|
| 214 | if (strcmp(prop_str,"bpsk")==0) { |
|---|
| 215 | ms = MOD_BPSK; |
|---|
| 216 | M = 2; |
|---|
| 217 | } else if (strcmp(prop_str,"qpsk")==0) { |
|---|
| 218 | ms = MOD_QPSK; |
|---|
| 219 | M = 4; |
|---|
| 220 | } else if (strcmp(prop_str,"arb16opt")==0) { |
|---|
| 221 | ms = MOD_ARB16OPT; |
|---|
| 222 | M = 16; |
|---|
| 223 | } else if (strcmp(prop_str,"arb64vt")==0) { |
|---|
| 224 | ms = MOD_ARB64VT; |
|---|
| 225 | M = 64; |
|---|
| 226 | } else if (strncmp(prop_str,"psk",3)==0) { |
|---|
| 227 | ms = MOD_PSK; |
|---|
| 228 | M = atoi(prop_str+3); |
|---|
| 229 | } else if (strncmp(prop_str,"dpsk",4)==0) { |
|---|
| 230 | ms = MOD_DPSK; |
|---|
| 231 | M = atoi(prop_str+4); |
|---|
| 232 | } else if (strncmp(prop_str,"qam",3)==0) { |
|---|
| 233 | ms = MOD_QAM; |
|---|
| 234 | M = atoi(prop_str+3); |
|---|
| 235 | } else if (strncmp(prop_str,"apsk",4)==0) { |
|---|
| 236 | ms = MOD_APSK; |
|---|
| 237 | M = atoi(prop_str+4); |
|---|
| 238 | } else if (strncmp(prop_str,"ask",3)==0) { |
|---|
| 239 | ms = MOD_ASK; |
|---|
| 240 | M = atoi(prop_str+3); |
|---|
| 241 | } else { |
|---|
| 242 | std::cerr << "error: invalid mod scheme: " << prop_str << std::endl; |
|---|
| 243 | throw (0); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | // compute bits/symbol |
|---|
| 247 | switch (M) { |
|---|
| 248 | case 2: bps = 1; break; |
|---|
| 249 | case 4: bps = 2; break; |
|---|
| 250 | case 8: bps = 3; break; |
|---|
| 251 | case 16: bps = 4; break; |
|---|
| 252 | case 32: bps = 5; break; |
|---|
| 253 | case 64: bps = 6; break; |
|---|
| 254 | case 128: bps = 7; break; |
|---|
| 255 | case 256: bps = 8; break; |
|---|
| 256 | default: |
|---|
| 257 | std::cerr << "error: FlexFrameGen_i::configure(), invalid constellation size : " << M << std::endl; |
|---|
| 258 | std::cerr << " Set mod_scheme to one of: bpsk, qpsk, psk<2..256>, dpsk<2..256>, qam<4..256>, apsk<4..128>, ask<2..256>, arb16opt, arb64vt" << std::endl; |
|---|
| 259 | throw (0); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | } else if (strcmp(props[i].id, "DCE:d1ff6dee-6d83-11df-a93b-001aa089d644") == 0) { |
|---|
| 263 | // mod_depth |
|---|
| 264 | //CORBA::Short simple_temp; |
|---|
| 265 | //props[i].value >>= simple_temp; |
|---|
| 266 | |
|---|
| 267 | std::cerr << "warning: mod_depth property ignored" << std::endl; |
|---|
| 268 | } else if (strcmp(props[i].id, "DCE:1ca10d80-6d84-11df-b66a-001aa089d644") == 0) { |
|---|
| 269 | // phasing_length |
|---|
| 270 | CORBA::Short simple_temp; |
|---|
| 271 | props[i].value >>= simple_temp; |
|---|
| 272 | std::cerr << "warning: phasing_length property ignored" << std::endl; |
|---|
| 273 | } else if (strcmp(props[i].id, "DCE:29b6bcc2-6d84-11df-bfd7-001aa089d644") == 0) { |
|---|
| 274 | // ramp_length |
|---|
| 275 | CORBA::Short simple_temp; |
|---|
| 276 | props[i].value >>= simple_temp; |
|---|
| 277 | std::cerr << "warning: ramp_length property ignored" << std::endl; |
|---|
| 278 | } else { |
|---|
| 279 | std::cerr << "error: unknown property" << std::endl; |
|---|
| 280 | throw (0); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // re-configure frame generator |
|---|
| 286 | fgprops.mod_scheme = ms; |
|---|
| 287 | fgprops.mod_bps = bps; |
|---|
| 288 | flexframegen_setprops(fg,&fgprops); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void FlexframeGen_i::ProcessData() |
|---|
| 292 | { |
|---|
| 293 | DEBUG(3, FlexframeGen, "ProcessData() invoked") |
|---|
| 294 | |
|---|
| 295 | PortTypes::FloatSequence I_out, Q_out; |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | // header data |
|---|
| 299 | PortTypes::CharSequence *headerData(NULL); |
|---|
| 300 | CORBA::UShort headerData_length; |
|---|
| 301 | |
|---|
| 302 | // payload data |
|---|
| 303 | PortTypes::CharSequence *payloadData(NULL); |
|---|
| 304 | CORBA::UShort payloadData_length; |
|---|
| 305 | |
|---|
| 306 | unsigned int i; |
|---|
| 307 | |
|---|
| 308 | verbose = 1; |
|---|
| 309 | while(continue_processing()) { |
|---|
| 310 | // get data from header port |
|---|
| 311 | headerDataPortIn->getData(headerData); |
|---|
| 312 | headerData_length = headerData->length(); |
|---|
| 313 | |
|---|
| 314 | payloadDataPortIn->getData(payloadData); |
|---|
| 315 | payloadData_length = payloadData->length(); |
|---|
| 316 | |
|---|
| 317 | // validate lengths |
|---|
| 318 | if (headerData_length != 8) { |
|---|
| 319 | std::cerr << "error: FlexframeGen header length must be 8 bytes" << std::endl; |
|---|
| 320 | throw(0); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | // reallocate payload |
|---|
| 324 | payload_len = payloadData_length; |
|---|
| 325 | payload = (unsigned char*) realloc(payload, payload_len*sizeof(unsigned char)); |
|---|
| 326 | |
|---|
| 327 | // copy header, payload |
|---|
| 328 | for (i=0; i<8; i++) |
|---|
| 329 | header[i] = (*headerData)[i]; |
|---|
| 330 | for (i=0; i<payload_len; i++) |
|---|
| 331 | payload[i] = (*payloadData)[i]; |
|---|
| 332 | |
|---|
| 333 | // configure fgprops on input data size |
|---|
| 334 | fgprops.payload_len = payload_len; |
|---|
| 335 | flexframegen_setprops(fg,&fgprops); |
|---|
| 336 | |
|---|
| 337 | // compute frame length, reallocate memory |
|---|
| 338 | frame_len = flexframegen_getframelen(fg); |
|---|
| 339 | frame = (std::complex<float>*) realloc(frame, frame_len*sizeof(std::complex<float>)); |
|---|
| 340 | |
|---|
| 341 | I_out.length(4*frame_len); //must define length of output |
|---|
| 342 | Q_out.length(4*frame_len); //must define length of output |
|---|
| 343 | |
|---|
| 344 | // generate the frame |
|---|
| 345 | flexframegen_execute(fg,header,payload,frame); |
|---|
| 346 | |
|---|
| 347 | // run interpolator, store in output |
|---|
| 348 | std::complex<float> y[2]; |
|---|
| 349 | std::complex<float> z[4]; |
|---|
| 350 | for (i=0; i<frame_len; i++) { |
|---|
| 351 | // execute interpolator one sample at a time, storing in |
|---|
| 352 | // two-sample array y |
|---|
| 353 | interp_crcf_execute(interp, frame[i], y); |
|---|
| 354 | |
|---|
| 355 | #if 0 |
|---|
| 356 | // write first interpolated sample |
|---|
| 357 | I_out[*i+0] = y[0].real(); |
|---|
| 358 | Q_out[2*i+0] = y[0].imag(); |
|---|
| 359 | |
|---|
| 360 | // write second interpolated sample |
|---|
| 361 | I_out[2*i+1] = y[1].real(); |
|---|
| 362 | Q_out[2*i+1] = y[1].imag(); |
|---|
| 363 | #else |
|---|
| 364 | resamp2_crcf_interp_execute(interp2, y[0], &z[0]); |
|---|
| 365 | resamp2_crcf_interp_execute(interp2, y[1], &z[2]); |
|---|
| 366 | |
|---|
| 367 | unsigned int j; |
|---|
| 368 | for (j=0; j<4; j++) { |
|---|
| 369 | I_out[4*i+j] = z[j].real(); |
|---|
| 370 | Q_out[4*i+j] = z[j].imag(); |
|---|
| 371 | } |
|---|
| 372 | #endif |
|---|
| 373 | |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | // push output frame |
|---|
| 377 | frameDataPortOut->pushPacket(I_out, Q_out); |
|---|
| 378 | |
|---|
| 379 | // free input buffers |
|---|
| 380 | headerDataPortIn->bufferEmptied(); |
|---|
| 381 | payloadDataPortIn->bufferEmptied(); |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | bool FlexframeGen_i::continue_processing() |
|---|
| 386 | { |
|---|
| 387 | omni_mutex_lock l(processing_mutex); |
|---|
| 388 | return thread_started; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | |
|---|