| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2006 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE AutomaticGainControl. |
|---|
| 6 | |
|---|
| 7 | OSSIE AutomaticGainControl 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 AutomaticGainControl 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 AutomaticGainControl; 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 "AutomaticGainControl.h" |
|---|
| 27 | |
|---|
| 28 | AutomaticGainControl_i::AutomaticGainControl_i(const char *uuid, omni_condition *condition) : Resource_impl(uuid), component_running(condition) |
|---|
| 29 | { |
|---|
| 30 | dataOut_0 = new standardInterfaces_i::complexShort_u("data_out"); |
|---|
| 31 | dataIn_0 = new standardInterfaces_i::complexShort_p("data_in"); |
|---|
| 32 | |
|---|
| 33 | //Create the thread for the writer's processing function |
|---|
| 34 | processing_thread = new omni_thread(run, (void *) this); |
|---|
| 35 | |
|---|
| 36 | //Start the thread containing the writer's processing function |
|---|
| 37 | processing_thread->start(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | AutomaticGainControl_i::~AutomaticGainControl_i(void) |
|---|
| 41 | { |
|---|
| 42 | delete dataOut_0; |
|---|
| 43 | delete dataIn_0; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void AutomaticGainControl_i::run(void * data) |
|---|
| 47 | { |
|---|
| 48 | ((AutomaticGainControl_i*) data)->run_loop(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | CORBA::Object_ptr AutomaticGainControl_i::getPort( const char* portName ) throw (CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 52 | { |
|---|
| 53 | DEBUG(3, AutomaticGainControl, "getPort() invoked with : " << portName) |
|---|
| 54 | CORBA::Object_var p; |
|---|
| 55 | |
|---|
| 56 | p = dataOut_0->getPort(portName); |
|---|
| 57 | if (!CORBA::is_nil(p)) |
|---|
| 58 | return p._retn(); |
|---|
| 59 | |
|---|
| 60 | p = dataIn_0->getPort(portName); |
|---|
| 61 | if (!CORBA::is_nil(p)) |
|---|
| 62 | return p._retn(); |
|---|
| 63 | /*exception*/ |
|---|
| 64 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void AutomaticGainControl_i::start() throw (CORBA::SystemException, CF::Resource::StartError) |
|---|
| 68 | { |
|---|
| 69 | DEBUG(3, AutomaticGainControl, "start invoked()") |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void AutomaticGainControl_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 73 | { |
|---|
| 74 | DEBUG(3, AutomaticGainControl, "stop invoked()") |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void AutomaticGainControl_i::releaseObject() throw (CORBA::SystemException, CF::LifeCycle::ReleaseError) |
|---|
| 78 | { |
|---|
| 79 | DEBUG(3, AutomaticGainControl, "releaseObject invoked()") |
|---|
| 80 | |
|---|
| 81 | component_running->signal(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void AutomaticGainControl_i::initialize() throw (CF::LifeCycle::InitializeError, CORBA::SystemException) |
|---|
| 85 | { |
|---|
| 86 | DEBUG(3, AutomaticGainControl, "initialize invoked()") |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | void AutomaticGainControl_i::query(CF::Properties & configProperties) |
|---|
| 90 | throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 91 | { |
|---|
| 92 | if (configProperties.length () == 0) |
|---|
| 93 | { |
|---|
| 94 | configProperties.length (propertySet.length ()); |
|---|
| 95 | for (unsigned int i = 0; i < propertySet.length (); i++) |
|---|
| 96 | { |
|---|
| 97 | configProperties[i].id = CORBA::string_dup (propertySet[i].id); |
|---|
| 98 | configProperties[i].value = propertySet[i].value; |
|---|
| 99 | } |
|---|
| 100 | return ; |
|---|
| 101 | } |
|---|
| 102 | else { |
|---|
| 103 | for (unsigned int i = 0; i < configProperties.length(); i++) { |
|---|
| 104 | for (unsigned int j=0; j < propertySet.length(); j++) { |
|---|
| 105 | if ( strcmp(configProperties[i].id, propertySet[i].id) == 0 ){ |
|---|
| 106 | configProperties[i].value = propertySet[i].value; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void AutomaticGainControl_i::configure(const CF::Properties& props) throw (CORBA::SystemException, CF::PropertySet::InvalidConfiguration, CF::PropertySet::PartialConfiguration) |
|---|
| 114 | { |
|---|
| 115 | static int init = 0; |
|---|
| 116 | |
|---|
| 117 | CORBA::Float simple_temp; |
|---|
| 118 | |
|---|
| 119 | DEBUG(3, AutomaticGainControl, "configure() invoked") |
|---|
| 120 | |
|---|
| 121 | DEBUG(3, AutomaticGainControl, "props length : " << props.length()) |
|---|
| 122 | |
|---|
| 123 | if (init == 0){ |
|---|
| 124 | std::cout << "AutomaticGainControl :initial configure call .." << std::endl; |
|---|
| 125 | propertySet.length(props.length()); |
|---|
| 126 | for (unsigned int i=0; i < props.length(); i++) { |
|---|
| 127 | std::cout << "Property Id : " << props[i].id << std::endl; |
|---|
| 128 | propertySet[i].id = CORBA::string_dup(props[i].id); |
|---|
| 129 | propertySet[i].value = props[i].value; |
|---|
| 130 | } |
|---|
| 131 | init = 1; |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | else { |
|---|
| 135 | for (unsigned int i = 0; i <props.length(); i++) |
|---|
| 136 | { |
|---|
| 137 | DEBUG(3, AutomaticGainControl, "Property id : " << props[i].id) |
|---|
| 138 | |
|---|
| 139 | if (strcmp(props[i].id, "DCE:aaf97fa0-d184-4d88-9954-3a1334c73d6d") == 0) { |
|---|
| 140 | // energy_lo |
|---|
| 141 | props[i].value >>= simple_temp; |
|---|
| 142 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 143 | energy_lo = simple_temp; |
|---|
| 144 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 145 | // Update value of this property in propertySet also |
|---|
| 146 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 147 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 148 | propertySet[i].value = props[i].value; |
|---|
| 149 | break; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | DEBUG(3, AutomaticGainControl, "prop (energy_lo): " << simple_temp) |
|---|
| 154 | |
|---|
| 155 | } else if (strcmp(props[i].id, "DCE:346e17c9-6678-483a-bffb-1909c64bddc0") == 0) { |
|---|
| 156 | // energy_hi |
|---|
| 157 | props[i].value >>= simple_temp; |
|---|
| 158 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 159 | energy_hi = simple_temp; |
|---|
| 160 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 161 | // Update value of this property in propertySet also |
|---|
| 162 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 163 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 164 | propertySet[i].value = props[i].value; |
|---|
| 165 | break; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | DEBUG(3, AutomaticGainControl, "prop (energy_hi): " << simple_temp) |
|---|
| 170 | |
|---|
| 171 | } else if (strcmp(props[i].id, "DCE:4608b943-4fe2-49df-91fb-afa287b609d4") == 0) { |
|---|
| 172 | // k_attack |
|---|
| 173 | props[i].value >>= simple_temp; |
|---|
| 174 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 175 | k_attack = simple_temp; |
|---|
| 176 | |
|---|
| 177 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 178 | // Update value of this property in propertySet also |
|---|
| 179 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 180 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 181 | propertySet[i].value = props[i].value; |
|---|
| 182 | break; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | DEBUG(3, AutomaticGainControl, "prop (k_attack): " << simple_temp) |
|---|
| 187 | |
|---|
| 188 | } else if (strcmp(props[i].id, "DCE:491ec3de-ed45-48af-a6fc-ca2d6465e136") == 0) { |
|---|
| 189 | // k_release |
|---|
| 190 | props[i].value >>= simple_temp; |
|---|
| 191 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 192 | k_release = simple_temp; |
|---|
| 193 | |
|---|
| 194 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 195 | // Update value of this property in propertySet also |
|---|
| 196 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 197 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 198 | propertySet[i].value = props[i].value; |
|---|
| 199 | break; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | DEBUG(3, AutomaticGainControl, "prop (k_release): " << simple_temp) |
|---|
| 204 | |
|---|
| 205 | } else if (strcmp(props[i].id, "DCE:312f63fe-709a-4217-933b-c584c8d6a9bb") == 0) { |
|---|
| 206 | // g_min |
|---|
| 207 | props[i].value >>= simple_temp; |
|---|
| 208 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 209 | g_min = simple_temp; |
|---|
| 210 | |
|---|
| 211 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 212 | // Update value of this property in propertySet also |
|---|
| 213 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 214 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 215 | propertySet[i].value = props[i].value; |
|---|
| 216 | break; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | DEBUG(3, AutomaticGainControl, "prop (g_min): " << simple_temp) |
|---|
| 221 | |
|---|
| 222 | } else if (strcmp(props[i].id, "DCE:8357ee0d-2417-46d9-8475-2e5778d797e4") == 0) { |
|---|
| 223 | // g_max |
|---|
| 224 | props[i].value >>= simple_temp; |
|---|
| 225 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 226 | g_max = simple_temp; |
|---|
| 227 | |
|---|
| 228 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 229 | // Update value of this property in propertySet also |
|---|
| 230 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 231 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 232 | propertySet[i].value = props[i].value; |
|---|
| 233 | break; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | DEBUG(3, AutomaticGainControl, "prop (g_max): " << simple_temp) |
|---|
| 238 | |
|---|
| 239 | } else if (strcmp(props[i].id, "DCE:b9b72ec8-d0bd-4060-b356-dcc6b0809e65") == 0) { |
|---|
| 240 | // rssi_pass |
|---|
| 241 | props[i].value >>= simple_temp; |
|---|
| 242 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 243 | rssi_pass = simple_temp; |
|---|
| 244 | |
|---|
| 245 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 246 | // Update value of this property in propertySet also |
|---|
| 247 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 248 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 249 | propertySet[i].value = props[i].value; |
|---|
| 250 | break; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | DEBUG(3, AutomaticGainControl, "prop (rssi_pass): " << simple_temp) |
|---|
| 255 | |
|---|
| 256 | } else { |
|---|
| 257 | // unknown property |
|---|
| 258 | std::cerr << "ERROR: SymbolSyncPoly::configure() unknown property \"" |
|---|
| 259 | << props[i].id << "\"" << std::endl; |
|---|
| 260 | throw CF::PropertySet::InvalidConfiguration(); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | void AutomaticGainControl_i::run_loop() |
|---|
| 269 | { |
|---|
| 270 | DEBUG(3, AutomaticGainControl, "run_loop() thread started") |
|---|
| 271 | |
|---|
| 272 | PortTypes::ShortSequence I_out, Q_out; |
|---|
| 273 | |
|---|
| 274 | PortTypes::ShortSequence *I_in(NULL), *Q_in(NULL); |
|---|
| 275 | //CORBA::UShort I_in_length, Q_in_length; |
|---|
| 276 | |
|---|
| 277 | unsigned int i, N; |
|---|
| 278 | short I, Q; |
|---|
| 279 | |
|---|
| 280 | SigProc::AutomaticGainControl * agc; |
|---|
| 281 | |
|---|
| 282 | agc = new SigProc::AutomaticGainControl(); |
|---|
| 283 | agc->SetValues( |
|---|
| 284 | energy_lo, |
|---|
| 285 | energy_hi, |
|---|
| 286 | k_attack, |
|---|
| 287 | k_release, |
|---|
| 288 | g_min, |
|---|
| 289 | g_max |
|---|
| 290 | ); |
|---|
| 291 | |
|---|
| 292 | while( true ) |
|---|
| 293 | { |
|---|
| 294 | /// get input data |
|---|
| 295 | dataIn_0->getData(I_in, Q_in); |
|---|
| 296 | |
|---|
| 297 | // overwrite agc values with properties |
|---|
| 298 | // lock mutex for asynchronous configure() invocation |
|---|
| 299 | { |
|---|
| 300 | omni_mutex_lock oml(accessPrivateData); |
|---|
| 301 | agc->SetValues( |
|---|
| 302 | energy_lo, |
|---|
| 303 | energy_hi, |
|---|
| 304 | k_attack, |
|---|
| 305 | k_release, |
|---|
| 306 | g_min, |
|---|
| 307 | g_max |
|---|
| 308 | ); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | N = I_in->length(); |
|---|
| 312 | |
|---|
| 313 | I_out.length(N); // define output length |
|---|
| 314 | Q_out.length(N); // define output length |
|---|
| 315 | |
|---|
| 316 | /// apply gain to input signal |
|---|
| 317 | for (i=0; i<N; i++) |
|---|
| 318 | { |
|---|
| 319 | I = (*I_in)[i]; |
|---|
| 320 | Q = (*Q_in)[i]; |
|---|
| 321 | agc->ApplyGain(I, Q); |
|---|
| 322 | I_out[i] = I; |
|---|
| 323 | Q_out[i] = Q; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | float gain(0.0f), energy(0.0f); |
|---|
| 327 | agc->GetStatus(gain, energy); |
|---|
| 328 | DEBUG(3, AutomaticGainControl, "gain: " << gain << ", energy: " << energy) |
|---|
| 329 | |
|---|
| 330 | // empty input buffer |
|---|
| 331 | dataIn_0->bufferEmptied(); |
|---|
| 332 | |
|---|
| 333 | // Push data if energy exceeds RSSI threshold |
|---|
| 334 | if ( (energy/gain) > rssi_pass) { |
|---|
| 335 | dataOut_0->pushPacket(I_out, Q_out); |
|---|
| 336 | rssi_pass_packet_counter = 4; |
|---|
| 337 | } else if ( rssi_pass_packet_counter > 0 ) { |
|---|
| 338 | dataOut_0->pushPacket(I_out, Q_out); |
|---|
| 339 | rssi_pass_packet_counter--; |
|---|
| 340 | } else { |
|---|
| 341 | // energy level too low; do nothing |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | |
|---|