| 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 | #undef 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, num_total_errors(0); |
|---|
| 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 | DEBUG(1, DataComparator, "WARNING: data packet sizes do not match") |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | if ( M != 1 ) { |
|---|
| 206 | DEBUG(1, DataComparator, "WARNING: control packet sizes do not match") |
|---|
| 207 | } |
|---|
| 208 | packet_id = (*control_bits_in)[0]; |
|---|
| 209 | // packet_id=1; |
|---|
| 210 | DEBUG(5, DataComparator, "packet id : " << packet_id); |
|---|
| 211 | |
|---|
| 212 | if ( first_packet ) |
|---|
| 213 | first_packet = false; |
|---|
| 214 | else |
|---|
| 215 | { |
|---|
| 216 | // determine if packets have been dropped |
|---|
| 217 | if ( packet_id == 0 ) |
|---|
| 218 | packet_index_diff = 127 - prev_packet_id; |
|---|
| 219 | else |
|---|
| 220 | packet_index_diff = packet_id - prev_packet_id - 1; |
|---|
| 221 | |
|---|
| 222 | while (packet_index_diff<0) |
|---|
| 223 | packet_index_diff += 127; |
|---|
| 224 | |
|---|
| 225 | num_dropped_packets += (unsigned int) packet_index_diff; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | num_received_packets++; |
|---|
| 229 | prev_packet_id = packet_id; |
|---|
| 230 | |
|---|
| 231 | num_errors = 0; |
|---|
| 232 | |
|---|
| 233 | // compare data values |
|---|
| 234 | for (i=0; i<N; i++) { |
|---|
| 235 | if ( (signed char) (*data_bits_in)[i] != packet_tmp[i] ) |
|---|
| 236 | num_errors += 1; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | DEBUG(2, DataComparator, "data errors [" << packet_id << "] : " << num_errors << " / " << N) |
|---|
| 240 | num_total_errors += num_errors; |
|---|
| 241 | |
|---|
| 242 | if ( (num_received_packets+num_dropped_packets) >= num_packets_to_observe ) { |
|---|
| 243 | DEBUG(1, DataComparator, "------------------ dropped packets : " << num_dropped_packets << " / " << num_received_packets + num_dropped_packets) |
|---|
| 244 | DEBUG(1, DataComparator, "------------------ error rate : " << float(num_total_errors)/float(num_received_packets*4) << " %") |
|---|
| 245 | num_received_packets = 0; |
|---|
| 246 | num_dropped_packets = 0; |
|---|
| 247 | num_total_errors = 0; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // Release input buffers |
|---|
| 251 | dataIn_0->bufferEmptied(); |
|---|
| 252 | dataIn_1->bufferEmptied(); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | #ifdef LOGGING |
|---|
| 256 | if ( log_size_counter < LOG_SIZE_MAX ) |
|---|
| 257 | { |
|---|
| 258 | packet_log << packet_id << " " << num_errors << std::endl; |
|---|
| 259 | log_size_counter++; |
|---|
| 260 | } |
|---|
| 261 | #endif |
|---|
| 262 | } |
|---|
| 263 | #ifdef LOGGING |
|---|
| 264 | packet_log.close(); |
|---|
| 265 | #endif |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|