| 1 | /**************************************************************************** |
|---|
| 2 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 3 | This file is part of the OSSIE __COMP_NAME__. |
|---|
| 4 | OSSIE __COMP_NAME__ is free software; you can redistribute it and/or modify |
|---|
| 5 | it under the terms of the GNU General Public License as published by |
|---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | "(at your option) any later version |
|---|
| 8 | OSSIE __COMP_NAME__ is distributed in the hope that it will be useful, |
|---|
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | GNU General Public License for more details. |
|---|
| 12 | You should have received a copy of the GNU General Public License |
|---|
| 13 | along with OSSIE __COMP_NAME__; if not, write to the Free Software |
|---|
| 14 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 15 | ****************************************************************************/ |
|---|
| 16 | |
|---|
| 17 | #include <string> |
|---|
| 18 | #include <iostream> |
|---|
| 19 | #include "voiceToPacket.h" |
|---|
| 20 | |
|---|
| 21 | #include <cstdio> |
|---|
| 22 | |
|---|
| 23 | voiceToPacket_i::voiceToPacket_i(const char *uuid, omni_condition *condition) : |
|---|
| 24 | Resource_impl(uuid), component_running(condition) |
|---|
| 25 | { |
|---|
| 26 | dataIn_0 = new standardInterfaces_i::complexShort_p("samplesIn"); |
|---|
| 27 | packetHeaderDataOut = new standardInterfaces_i::realChar_u("headerOut"); |
|---|
| 28 | packetPayloadDataOut = new standardInterfaces_i::realChar_u("payloadOut"); |
|---|
| 29 | start(); |
|---|
| 30 | |
|---|
| 31 | header_len = 14; |
|---|
| 32 | verbose = true; |
|---|
| 33 | packet_delay = 1000; |
|---|
| 34 | |
|---|
| 35 | packet_id = 0; |
|---|
| 36 | |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | voiceToPacket_i::~voiceToPacket_i(void) |
|---|
| 40 | { |
|---|
| 41 | delete dataIn_0; |
|---|
| 42 | delete packetHeaderDataOut; |
|---|
| 43 | delete packetPayloadDataOut; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | // Static function for omni thread |
|---|
| 47 | void voiceToPacket_i::Run( void * data ) |
|---|
| 48 | { |
|---|
| 49 | ((voiceToPacket_i*)data)->ProcessData(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | CORBA::Object_ptr voiceToPacket_i::getPort( const char* portName ) throw ( |
|---|
| 53 | CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 54 | { |
|---|
| 55 | DEBUG(3, voiceToPacket, "getPort() invoked with " << portName) |
|---|
| 56 | |
|---|
| 57 | CORBA::Object_var p; |
|---|
| 58 | |
|---|
| 59 | p = dataIn_0->getPort(portName); |
|---|
| 60 | |
|---|
| 61 | if (!CORBA::is_nil(p)) |
|---|
| 62 | return p._retn(); |
|---|
| 63 | |
|---|
| 64 | p = packetHeaderDataOut->getPort(portName); |
|---|
| 65 | |
|---|
| 66 | if (!CORBA::is_nil(p)) |
|---|
| 67 | return p._retn(); |
|---|
| 68 | |
|---|
| 69 | p = packetPayloadDataOut->getPort(portName); |
|---|
| 70 | |
|---|
| 71 | if (!CORBA::is_nil(p)) |
|---|
| 72 | return p._retn(); |
|---|
| 73 | |
|---|
| 74 | /*exception*/ |
|---|
| 75 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void voiceToPacket_i::start() throw (CORBA::SystemException, |
|---|
| 79 | CF::Resource::StartError) |
|---|
| 80 | { |
|---|
| 81 | DEBUG(3, voiceToPacket, "start() invoked") |
|---|
| 82 | omni_mutex_lock l(processing_mutex); |
|---|
| 83 | if( false == thread_started ) |
|---|
| 84 | { |
|---|
| 85 | thread_started = true; |
|---|
| 86 | // Create the thread for the writer's processing function |
|---|
| 87 | processing_thread = new omni_thread(Run, (void *) this); |
|---|
| 88 | |
|---|
| 89 | // Start the thread containing the writer's processing function |
|---|
| 90 | processing_thread->start(); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void voiceToPacket_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 95 | { |
|---|
| 96 | DEBUG(3, voiceToPacket, "stop() invoked") |
|---|
| 97 | omni_mutex_lock l(processing_mutex); |
|---|
| 98 | thread_started = false; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void voiceToPacket_i::releaseObject() throw (CORBA::SystemException, |
|---|
| 102 | CF::LifeCycle::ReleaseError) |
|---|
| 103 | { |
|---|
| 104 | DEBUG(3, voiceToPacket, "releaseObject() invoked") |
|---|
| 105 | |
|---|
| 106 | component_running->signal(); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void voiceToPacket_i::initialize() throw (CF::LifeCycle::InitializeError, |
|---|
| 110 | CORBA::SystemException) |
|---|
| 111 | { |
|---|
| 112 | DEBUG(3, voiceToPacket, "initialize() invoked") |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void voiceToPacket_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 116 | { |
|---|
| 117 | if( configProperties.length() == 0 ) |
|---|
| 118 | { |
|---|
| 119 | configProperties.length( propertySet.length() ); |
|---|
| 120 | for( unsigned int i = 0; i < propertySet.length(); i++ ) |
|---|
| 121 | { |
|---|
| 122 | configProperties[i].id = CORBA::string_dup( propertySet[i].id ); |
|---|
| 123 | configProperties[i].value = propertySet[i].value; |
|---|
| 124 | } |
|---|
| 125 | return; |
|---|
| 126 | } else { |
|---|
| 127 | for( unsigned int i = 0; i < configProperties.length(); i++ ) { |
|---|
| 128 | for( unsigned int j = 0; j < propertySet.length(); j++ ) { |
|---|
| 129 | if( strcmp(configProperties[i].id, propertySet[j].id) == 0 ) { |
|---|
| 130 | configProperties[i].value = propertySet[j].value; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | } // end if-else |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | void voiceToPacket_i::configure(const CF::Properties& props) |
|---|
| 138 | throw (CORBA::SystemException, |
|---|
| 139 | CF::PropertySet::InvalidConfiguration, |
|---|
| 140 | CF::PropertySet::PartialConfiguration) |
|---|
| 141 | { |
|---|
| 142 | DEBUG(3, voiceToPacket, "configure() invoked") |
|---|
| 143 | |
|---|
| 144 | static int init = 0; |
|---|
| 145 | if( init == 0 ) { |
|---|
| 146 | if( props.length() == 0 ) { |
|---|
| 147 | std::cout << "configure called with invalid props.length - " << props.length() << std::endl; |
|---|
| 148 | return; |
|---|
| 149 | } |
|---|
| 150 | propertySet.length(props.length()); |
|---|
| 151 | for( unsigned int j=0; j < props.length(); j++ ) { |
|---|
| 152 | propertySet[j].id = CORBA::string_dup(props[j].id); |
|---|
| 153 | propertySet[j].value = props[j].value; |
|---|
| 154 | } |
|---|
| 155 | init = 1; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | std::cout << "props length : " << props.length() << std::endl; |
|---|
| 159 | |
|---|
| 160 | for ( unsigned int i = 0; i <props.length(); i++) |
|---|
| 161 | { |
|---|
| 162 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 163 | |
|---|
| 164 | if (strcmp(props[i].id, "DCE:bce93bb6-d3de-11e0-94b5-001aa0b89dbf") == 0) |
|---|
| 165 | { |
|---|
| 166 | CORBA::Short simple_temp; |
|---|
| 167 | props[i].value >>= simple_temp; |
|---|
| 168 | simple_0_value = simple_temp; |
|---|
| 169 | for( unsigned int k = 0; k < propertySet.length(); k++ ) { |
|---|
| 170 | if( strcmp(propertySet[k].id, props[i].id) == 0 ) { |
|---|
| 171 | propertySet[k].value = props[i].value; |
|---|
| 172 | break; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | if (strcmp(props[i].id, "DCE:cfa68aec-d3de-11e0-b0db-001aa0b89dbf") == 0) |
|---|
| 178 | { |
|---|
| 179 | CORBA::Short simple_temp; |
|---|
| 180 | props[i].value >>= simple_temp; |
|---|
| 181 | simple_1_value = simple_temp; |
|---|
| 182 | for( unsigned int k = 0; k < propertySet.length(); k++ ) { |
|---|
| 183 | if( strcmp(propertySet[k].id, props[i].id) == 0 ) { |
|---|
| 184 | propertySet[k].value = props[i].value; |
|---|
| 185 | break; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | if (strcmp(props[i].id, "DCE:df88e05e-d3de-11e0-aeb7-001aa0b89dbf") == 0) |
|---|
| 191 | { |
|---|
| 192 | CORBA::Short simple_temp; |
|---|
| 193 | props[i].value >>= simple_temp; |
|---|
| 194 | simple_2_value = simple_temp; |
|---|
| 195 | for( unsigned int k = 0; k < propertySet.length(); k++ ) { |
|---|
| 196 | if( strcmp(propertySet[k].id, props[i].id) == 0 ) { |
|---|
| 197 | propertySet[k].value = props[i].value; |
|---|
| 198 | break; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (strcmp(props[i].id, "DCE:ff76e0d2-d3de-11e0-9af0-001aa0b89dbf") == 0) |
|---|
| 204 | { |
|---|
| 205 | CORBA::Boolean simple_temp; |
|---|
| 206 | props[i].value >>= simple_temp; |
|---|
| 207 | simple_3_value = simple_temp; |
|---|
| 208 | for( unsigned int k = 0; k < propertySet.length(); k++ ) { |
|---|
| 209 | if( strcmp(propertySet[k].id, props[i].id) == 0 ) { |
|---|
| 210 | propertySet[k].value = props[i].value; |
|---|
| 211 | break; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | void voiceToPacket_i::ProcessData() |
|---|
| 220 | { |
|---|
| 221 | DEBUG(3, voiceToPacket, "ProcessData() invoked") |
|---|
| 222 | |
|---|
| 223 | PortTypes::CharSequence headerData; |
|---|
| 224 | PortTypes::CharSequence payloadData; |
|---|
| 225 | |
|---|
| 226 | PortTypes::ShortSequence *I_in_0(NULL), *Q_in_0(NULL); |
|---|
| 227 | |
|---|
| 228 | while(continue_processing()) |
|---|
| 229 | { |
|---|
| 230 | dataIn_0->getData(I_in_0, Q_in_0); |
|---|
| 231 | |
|---|
| 232 | payload_len = I_in_0->length(); |
|---|
| 233 | |
|---|
| 234 | headerData.length(header_len); |
|---|
| 235 | payloadData.length(payload_len); |
|---|
| 236 | |
|---|
| 237 | // generate header data |
|---|
| 238 | unsigned int i; |
|---|
| 239 | for (i = 0; i<header_len; i++) |
|---|
| 240 | { |
|---|
| 241 | headerData[i] = (i == 0) ? packet_id & 0xff : 0; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | // take voice samples, put into payload |
|---|
| 245 | for (i = 0; i<payload_len; i++) |
|---|
| 246 | { |
|---|
| 247 | payloadData[i] = (*I_in_0)[i] & 0xff; |
|---|
| 248 | //printf("%d",payloadData[i]); |
|---|
| 249 | |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | dataIn_0->bufferEmptied(); |
|---|
| 254 | packetHeaderDataOut->pushPacket(headerData); |
|---|
| 255 | packetPayloadDataOut->pushPacket(payloadData); |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | bool voiceToPacket_i::continue_processing() |
|---|
| 260 | { |
|---|
| 261 | omni_mutex_lock l(processing_mutex); |
|---|
| 262 | return thread_started; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|