| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2006 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE DataComparator. |
|---|
| 6 | |
|---|
| 7 | OSSIE DataComparator 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 DataComparator 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 DataComparator; 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 <fstream> |
|---|
| 27 | #include "DataComparator.h" |
|---|
| 28 | |
|---|
| 29 | #define LOGGING |
|---|
| 30 | |
|---|
| 31 | #ifdef LOGGING |
|---|
| 32 | #define LOG_SIZE_MAX 32 |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #define BIT0 0 |
|---|
| 36 | #define BIT1 1 |
|---|
| 37 | |
|---|
| 38 | const char DataComparator_i::packet_tmp[400] = |
|---|
| 39 | { |
|---|
| 40 | 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
|---|
| 41 | 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, |
|---|
| 42 | 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 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|
| 57 | 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, |
|---|
| 58 | 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, |
|---|
| 59 | 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0 |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | DataComparator_i::DataComparator_i(const char *uuid, omni_condition *condition) : Resource_impl(uuid), component_running(condition) |
|---|
| 64 | { |
|---|
| 65 | dataIn_0 = new standardInterfaces_i::realChar_p("data_bits_in"); |
|---|
| 66 | dataIn_1 = new standardInterfaces_i::realChar_p("control_bits_in"); |
|---|
| 67 | |
|---|
| 68 | // initialze algorithm variables |
|---|
| 69 | packet_length = 400; |
|---|
| 70 | packet = NULL; |
|---|
| 71 | control_data_length = 15; |
|---|
| 72 | control_data = new signed char[control_data_length]; |
|---|
| 73 | |
|---|
| 74 | //Create the thread for the writer's processing function |
|---|
| 75 | processing_thread = new omni_thread(run, (void *) this); |
|---|
| 76 | |
|---|
| 77 | //Start the thread containing the writer's processing function |
|---|
| 78 | processing_thread->start(); |
|---|
| 79 | |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | DataComparator_i::~DataComparator_i(void) |
|---|
| 83 | { |
|---|
| 84 | delete dataIn_0; |
|---|
| 85 | delete dataIn_1; |
|---|
| 86 | if ( packet != NULL ) |
|---|
| 87 | delete [] packet; |
|---|
| 88 | if ( control_data != NULL ) |
|---|
| 89 | delete [] control_data; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void DataComparator_i::run( void * data ) |
|---|
| 93 | { |
|---|
| 94 | ((DataComparator_i*) data)->run_loop(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | CORBA::Object_ptr DataComparator_i::getPort( const char* portName ) throw (CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 98 | { |
|---|
| 99 | DEBUG(5, DataComparator, "getPort() invoked with : " << portName) |
|---|
| 100 | |
|---|
| 101 | CORBA::Object_var p; |
|---|
| 102 | |
|---|
| 103 | p = dataIn_0->getPort(portName); |
|---|
| 104 | |
|---|
| 105 | if (!CORBA::is_nil(p)) |
|---|
| 106 | return p._retn(); |
|---|
| 107 | |
|---|
| 108 | p = dataIn_1->getPort(portName); |
|---|
| 109 | |
|---|
| 110 | if (!CORBA::is_nil(p)) |
|---|
| 111 | return p._retn(); |
|---|
| 112 | |
|---|
| 113 | /*exception*/ |
|---|
| 114 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void DataComparator_i::start() throw (CORBA::SystemException, CF::Resource::StartError) |
|---|
| 118 | { |
|---|
| 119 | DEBUG(5, DataComparator, "start() invoked") |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void DataComparator_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 123 | { |
|---|
| 124 | DEBUG(5, DataComparator, "stop() invoked") |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | void DataComparator_i::releaseObject() throw (CORBA::SystemException, CF::LifeCycle::ReleaseError) |
|---|
| 128 | { |
|---|
| 129 | DEBUG(5, DataComparator, "releaseObject() invoked") |
|---|
| 130 | |
|---|
| 131 | component_running->signal(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | void DataComparator_i::initialize() throw (CF::LifeCycle::InitializeError, CORBA::SystemException) |
|---|
| 135 | { |
|---|
| 136 | DEBUG(5, DataComparator, "initialize() invoked") |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void DataComparator_i::configure(const CF::Properties& props) throw (CORBA::SystemException, CF::PropertySet::InvalidConfiguration, CF::PropertySet::PartialConfiguration) |
|---|
| 140 | { |
|---|
| 141 | DEBUG(5, DataComparator, "configure() invoked") |
|---|
| 142 | |
|---|
| 143 | DEBUG(5, DataComparator, "props length : " << props.length()) |
|---|
| 144 | |
|---|
| 145 | for (unsigned int i = 0; i <props.length(); i++) |
|---|
| 146 | { |
|---|
| 147 | DEBUG(5, DataComparator, "Property id : " << props[i].id) |
|---|
| 148 | |
|---|
| 149 | if (strcmp(props[i].id, "DCE:111132a5-3d23-4632-be1e-b00403b03fe2")==0) |
|---|
| 150 | // packet_length |
|---|
| 151 | { |
|---|
| 152 | CORBA::Long simple_temp; |
|---|
| 153 | props[i].value >>= simple_temp; |
|---|
| 154 | simple_0_value = simple_temp; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void DataComparator_i::run_loop() |
|---|
| 161 | { |
|---|
| 162 | DEBUG(5, DataComparator, "run_loop() thread started") |
|---|
| 163 | |
|---|
| 164 | #ifdef LOGGING |
|---|
| 165 | unsigned int log_size_counter(0); |
|---|
| 166 | std::ofstream packet_log; |
|---|
| 167 | packet_log.open("packet_log.dat"); |
|---|
| 168 | #endif |
|---|
| 169 | |
|---|
| 170 | unsigned int i, N, num_errors; |
|---|
| 171 | unsigned int packet_id(0), prev_packet_id(0); |
|---|
| 172 | signed int packet_index_diff(0); |
|---|
| 173 | unsigned int num_dropped_packets(0); |
|---|
| 174 | unsigned int num_packets_to_observe(100); |
|---|
| 175 | unsigned int num_received_packets(0); |
|---|
| 176 | unsigned int M(control_data_length); |
|---|
| 177 | |
|---|
| 178 | bool first_packet(true); |
|---|
| 179 | |
|---|
| 180 | packet_length = 400; |
|---|
| 181 | if ( packet != NULL ) |
|---|
| 182 | { |
|---|
| 183 | delete [] packet; |
|---|
| 184 | packet = new signed char[packet_length]; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | PortTypes::CharSequence * data_bits_in(NULL); |
|---|
| 188 | PortTypes::CharSequence * control_bits_in(NULL); |
|---|
| 189 | |
|---|
| 190 | while( true ) |
|---|
| 191 | { |
|---|
| 192 | data_bits_in = NULL; |
|---|
| 193 | dataIn_0->getData(data_bits_in); |
|---|
| 194 | N = data_bits_in->length(); |
|---|
| 195 | DEBUG(7, DataComparator, "received " << N << " data samples") |
|---|
| 196 | |
|---|
| 197 | dataIn_1->getData(control_bits_in); |
|---|
| 198 | M = control_bits_in->length(); |
|---|
| 199 | DEBUG(7, DataComparator, "received " << M << " control samples") |
|---|
| 200 | |
|---|
| 201 | if ( N != packet_length ) |
|---|
| 202 | { |
|---|
| 203 | DEBUG(1, DataComparator, "WARNING: data packet sizes do not match") |
|---|
| 204 | } |
|---|
| 205 | if ( M != control_data_length ) |
|---|
| 206 | { |
|---|
| 207 | DEBUG(1, DataComparator, "WARNING: control packet sizes do not match") |
|---|
| 208 | } |
|---|
| 209 | else |
|---|
| 210 | { |
|---|
| 211 | // compare control data |
|---|
| 212 | if ( true ) |
|---|
| 213 | { |
|---|
| 214 | packet_id = 0; |
|---|
| 215 | // std::cout << "DataComparator: control: "; |
|---|
| 216 | /* |
|---|
| 217 | for (i=0; i<M; i++) |
|---|
| 218 | std::cout << " " << (int) (*control_bits_in)[i]; |
|---|
| 219 | std::cout << std::endl; |
|---|
| 220 | */ |
|---|
| 221 | |
|---|
| 222 | // control data represent a 5-bit packet ID, viz |
|---|
| 223 | // packet_id = [b4 b3 b2 b1 b0] |
|---|
| 224 | // repeated 3 timesl; take best of 3 bits |
|---|
| 225 | |
|---|
| 226 | packet_id |= ( ( |
|---|
| 227 | (*control_bits_in)[0] + |
|---|
| 228 | (*control_bits_in)[5] + |
|---|
| 229 | (*control_bits_in)[10] |
|---|
| 230 | ) >= 2 ) ? 0x0001 : 0x0000; |
|---|
| 231 | packet_id <<= 1; |
|---|
| 232 | |
|---|
| 233 | packet_id |= ( ( |
|---|
| 234 | (*control_bits_in)[1] + |
|---|
| 235 | (*control_bits_in)[6] + |
|---|
| 236 | (*control_bits_in)[11] |
|---|
| 237 | ) >= 2 ) ? 0x0001 : 0x0000; |
|---|
| 238 | packet_id <<= 1; |
|---|
| 239 | |
|---|
| 240 | packet_id |= ( ( |
|---|
| 241 | (*control_bits_in)[2] + |
|---|
| 242 | (*control_bits_in)[7] + |
|---|
| 243 | (*control_bits_in)[12] |
|---|
| 244 | ) >= 2 ) ? 0x0001 : 0x0000; |
|---|
| 245 | packet_id <<= 1; |
|---|
| 246 | |
|---|
| 247 | packet_id |= ( ( |
|---|
| 248 | (*control_bits_in)[3] + |
|---|
| 249 | (*control_bits_in)[8] + |
|---|
| 250 | (*control_bits_in)[13] |
|---|
| 251 | ) >= 2 ) ? 0x0001 : 0x0000; |
|---|
| 252 | packet_id <<= 1; |
|---|
| 253 | |
|---|
| 254 | packet_id |= ( ( |
|---|
| 255 | (*control_bits_in)[4] + |
|---|
| 256 | (*control_bits_in)[9] + |
|---|
| 257 | (*control_bits_in)[14] |
|---|
| 258 | ) > 2 ) ? 0x0001 : 0x0000; |
|---|
| 259 | |
|---|
| 260 | DEBUG(5, DataComparator, "packet id : " << packet_id); |
|---|
| 261 | |
|---|
| 262 | if ( first_packet ) |
|---|
| 263 | first_packet = false; |
|---|
| 264 | else |
|---|
| 265 | { |
|---|
| 266 | // determine if packets have been dropped |
|---|
| 267 | if ( packet_id == 0 ) |
|---|
| 268 | packet_index_diff = 31 - prev_packet_id; |
|---|
| 269 | else |
|---|
| 270 | packet_index_diff = packet_id - prev_packet_id - 1; |
|---|
| 271 | |
|---|
| 272 | while (packet_index_diff<0) |
|---|
| 273 | packet_index_diff += 31; |
|---|
| 274 | |
|---|
| 275 | num_dropped_packets += (unsigned int) packet_index_diff; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | num_received_packets++; |
|---|
| 279 | prev_packet_id = packet_id; |
|---|
| 280 | |
|---|
| 281 | } |
|---|
| 282 | num_errors = 0; |
|---|
| 283 | |
|---|
| 284 | // compare data values |
|---|
| 285 | for (i=0; i<N; i++) |
|---|
| 286 | num_errors += ( (signed char) (*data_bits_in)[i]==packet_tmp[i]) |
|---|
| 287 | ? 0 : 1; |
|---|
| 288 | |
|---|
| 289 | DEBUG(2, DataComparator, "data errors : " << num_errors << " / " << N) |
|---|
| 290 | |
|---|
| 291 | if ( (num_received_packets+num_dropped_packets) >= num_packets_to_observe ) |
|---|
| 292 | { |
|---|
| 293 | DEBUG(1, DataComparator, "------------------ dropped packets : " << num_dropped_packets << " / " << num_received_packets + num_dropped_packets) |
|---|
| 294 | num_received_packets = 0; |
|---|
| 295 | num_dropped_packets = 0; |
|---|
| 296 | } |
|---|
| 297 | #ifdef LOGGING |
|---|
| 298 | if ( log_size_counter < LOG_SIZE_MAX ) |
|---|
| 299 | { |
|---|
| 300 | packet_log << packet_id << " " << num_errors << std::endl; |
|---|
| 301 | log_size_counter++; |
|---|
| 302 | } |
|---|
| 303 | #endif |
|---|
| 304 | } |
|---|
| 305 | dataIn_0->bufferEmptied(); |
|---|
| 306 | dataIn_1->bufferEmptied(); |
|---|
| 307 | } |
|---|
| 308 | #ifdef LOGGING |
|---|
| 309 | packet_log.close(); |
|---|
| 310 | #endif |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|