omnet++/veins : some sumo/traci commands seem to be not implemented - simulator

I would like to use some sumo/traci commands that seem to be not implemented in omnet++/veins, such as :
traffic light commands setRedYellowGreenState(tlsID, state) for example
junction command
and other commands
What is the simplest way to be able to use these commands from omnet++/veins?
Thank you very much for the help.
Regards :)

You can find the existing client source code for interacting with SUMO in TraCICommandInterface.cc, for example to set a TL program:
TraCIBuffer buf = connection->query(CMD_SET_TL_VARIABLE, TraCIBuffer()
<< static_cast<uint8_t>(TL_PROGRAM)
<< trafficLightId
<< static_cast<uint8_t>(TYPE_STRING)
<< program
);
The corresponding server source code can be found in TraCIServerAPI_TLS.cpp, for example to set a TL program:
// variable & id
int variable = inputStorage.readUnsignedByte();
std::string id = inputStorage.readString();
// [...] case TL_PROGRAM:
server.readTypeCheckingString(inputStorage, subID)
vars.switchTo(tlsControl, subID);
By investigating how the server is prepared to interact with the client (and how the client is already interacting with the server) you should be able to extend the client according to your wishes.

Related

field 'info' must be a network type

I'm a beginner and I try to create a struct in nesc for a tinyos app, but i have this error that i don't know how to fix.
Any ideas?
The code of the struct is:
typedef nx_struct Message
{
nx_uint16_t ID
float info;
} messaget;
I think you're just missing a semicolon after ID. Also, the name you are giving your type is very close to message_t which is already used by TinyOS - I would recommend giving it a more descriptive name, like MyInformationMessage_t.
You missed a ; after nx_uint16_t ID. A sample message may go like this:
typedef nx_struct test_message
{
nx_uint16_t ID
float info;
} test_message_t;
If you want to learn more about how to use structs to define message formats and directly access messages. You may refer to section 3.5.3 Platform-independent types in TinyOS Programming
, a Book by David Gay and Philip A. Levis.

QAudioOutput in Qt5 is not producing any sound

I’m working under kubuntu 12.10 and developping an application into which i need to generate some sound into a QIODevice, then play it with QAudioOutput.
I’ve read all the litterature around speaking of how to properly do that, and I think to have done so.
So far I’ve done :
QVector <double> * soundData = SoundGenerator::getSound();
soundBuffer->open(QIODevice::ReadWrite);
QDataStream writeStream(soundBuffer);
foreach(double d, *soundData) {
char value = d * (2 << 7);
// qDebug() << "Value : " << (short int)value;
writeStream << value;
}
QAudioFormat format;
// Set up the format, eg.
format.setSampleRate(SoundGenerator::getAudioSampleRate());
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
audio = new QAudioOutput(format, this);
if (audio->error() != QAudio::NoError) {
qDebug() << "Problem playing sound";
}
connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(aboutToFinish(QAudio::State)));
I have also a call to
audio->start(soundBuffer)
—
from another slot
I do not have any error in the initialization of the QAudioOutput
And I have NO SOUND AT ALL (all other applications have sound, and I’m porting a Qt4 app to Qt5, in Qt4 everything is ok with Phonon)
The aboutToFinish slot is called at the beggining with ActiveState as state, and NoError when calling QAudioOutput::error, but it’s not called anymore, even if waiting far more than the expected generated sound duration.
The sound generation process is not to be put in question, it has been tested by writing wav files, and it works.
Moreover, I have built the multimedia example from Qt’s sources, when it comes to pure audio there is no output (for example in the sprectrum example), on another hand, video plays with the sound perfectly.
Is there any known issue concerning that ? Is that a bug ? Am I doing something wrong ?
Thanks in advance ;)
This does not work because you have set 8 bit sample size and signed integer format.
SOLUTION: You have to set the sample type to unsigned for 8-bit resolution:
format.setSampleType(QAudioFormat::UnsignedInt);
This is not a Qt bug. Why? The answer is that in the WAV spec', 8-bit samples are always unsigned, whereas 16-bit samples are always signed. Any other combination does not work.
So for 16-bit samples you would have to put:
format.setSampleType(QAudioFormat:SignedInt);
(IMHO the fact that Qt does not take care of handling these cases by forcing the correct format is a flaw but not a lack in functionnality).
You can learn more about this in the notes section of this page: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
And also the solution to this very similar question (same problem but with 16-bit): Qt QAudioOutput push mode
Try to add:
QEventLoop loop;
loop.exec();

QNetworkAccessManager: post http multipart from serial QIODevice

I'm trying to use QNetworkAccessManager to upload http multiparts to a dedicated server.
The multipart consists of a JSON part describing the data being uploaded.
The data is read from a serial QIODevice, which encrypts the data.
This is the code that creates the multipart request:
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart metaPart;
metaPart.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
metaPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"metadata\""));
metaPart.setBody(meta.toJson());
multiPart->append(metaPart);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(fileFormat));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
filePart.setBodyDevice(p_encDevice);
p_encDevice->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
QNetworkAccessManager netMgr;
QScopedPointer<QNetworkReply> reply( netMgr.post(request, multiPart) );
multiPart->setParent(reply.data()); // delete the multiPart with the reply
If the p_encDevice is an instance of QFile, that file gets uploaded just fine.
If the specialised encrypting QIODevice is used (serial device) then all of the data is read from my custom device. however QNetworkAccessManager::post() doesn't complete (hangs).
I read in the documentation of QHttpPart that:
if device is sequential (e.g. sockets, but not files),
QNetworkAccessManager::post() should be called after device has
emitted finished().
Unfortunately I don't know how do that.
Please advise.
EDIT:
QIODevice doesn't have finished() slot at all. What's more, reading from my custom IODevice doesn't happen at all if QNetworkAccessManager::post() is not called and therefore the device wouldn't be able to emit such an event. (Catch 22?)
EDIT 2:
It seems that QNAM does not work with sequential devices at all. See discussion on qt-project.
EDIT 3:
I managed to "fool" QNAM to make it think that it is reading from non-sequential devices, but seek and reset functions prevent seeking. This will work until QNAM will actually try to seek.
bool AesDevice::isSequential() const
{
return false;
}
bool AesDevice::reset()
{
if (this->pos() != 0) {
return false;
}
return QIODevice::reset();
}
bool AesDevice::seek(qint64 pos)
{
if (this->pos() != pos) {
return false;
}
return QIODevice::seek(pos);
}
You'll need to refactor your code quite a lot so that the variables you pass to post are available outside that function you've posted, then you'll need a new slot defined with the code for doing the post inside the implementation. Lastly you need to do connect(p_encDevice, SIGNAL(finished()), this, SLOT(yourSlot()) to glue it all together.
You're mostly there, you just need to refactor it out and add a new slot you can tie to the QIODevice::finished() signal.
I've had more success creating the http post data manually than with using QHttpPart and QHttpMultiPart. I know it's probably not what you want to hear, and it's a little messy, but it definitely works. In this example I am reading from a QFile, but you can call readAll() on any QIODevice. It also is worth noting, QIODevice::size() will help you check if all the data has been read.
QByteArray postData;
QFile *file=new QFile("/tmp/image.jpg");
if(!(file->open(QIODevice::ReadOnly))){
qDebug() << "Could not open file for reading: "<< file->fileName();
return;
}
//create a header that the server can recognize
postData.insert(0,"--AaB03x\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n");
postData.append(file->readAll());
postData.append("\r\n--AaB03x--\r\n");
//here you can add additional parameters that your server may need to parse the data at the end of the url
QString check(QString(POST_URL)+"?fn="+fn+"&md="+md);
QNetworkRequest req(QUrl(check.toLocal8Bit()));
req.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data; boundary=AaB03x");
QVariant l=postData.length();
req.setHeader(QNetworkRequest::ContentLengthHeader,l.toString());
file->close();
//free up memory
delete(file);
//post the data
reply=manager->post(req,postData);
//connect the reply object so we can track the progress of the upload
connect(reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(updateProgress(qint64,qint64)));
Then the server can access the data like this:
<?php
$filename=$_REQUEST['fn'];
$makedir=$_REQUEST['md'];
if($_FILES["attachment"]["type"]=="image/jpeg"){
if(!move_uploaded_file($_FILES["attachment"]["tmp_name"], "/directory/" . $filename)){
echo "File Error";
error_log("Uploaded File Error");
exit();
};
}else{
print("no file");
error_log("No File");
exit();
}
echo "Success.";
?>
I hope some of this code can help you.
I think the catch is that QNetworkAccessManager does not support chunked transfer encoding when uploading (POST, PUT) data. This means that QNAM must know in advance the length of the data it's going to upload, in order to send the Content-Length header. This implies:
either the data does not come from sequential devices, but from random-access devices, which would correctly report their total size through size();
or the data comes from a sequential device, but the device has already buffered all of it (this is the meaning of the note about finished()), and will report it (through bytesAvailable(), I suppose);
or the data comes from a sequential device which has not buffered all the data, which in turn means
either QNAM reads and buffers itself all the data coming from the device (by reading until EOF)
or the user manually set the Content-Length header for the request.
(About the last two points, see the docs for the QNetworkRequest::DoNotBufferUploadDataAttribute.)
So, QHttpMultiPart somehow shares these limitations, and it's likely that it's choking on case 3. Supposing that you cannot possibly buffer in memory all the data from your "encoder" QIODevice, is there any chance you might know the size of the encoded data in advance and set the content-length on the QHttpPart?
(As a last note, you shouldn't be using QScopedPointer. That will delete the QNR when the smart pointer falls out of scope, but you don't want to do that. You want to delete the QNR when it emits finished()).
From a separate discussion in qt-project and by inspecting the source code it seems that QNAM doesn't work with sequential at all. Both the documentation and code are wrong.

converting a windows shell IStream to std::ifstream/std::get_line

We have a lot of code written that makes use of the standard template library. I would like to integrate some of our apps into the windows shell, which should provide a better experience for our users.
One piece of integration involves a Shell Preview provider, the code is very straight forward, however, I’m stuck on the best way to implement something.
The shell is giving me, via my preview handler, an IStream object and I need to convert/adapt it to an std::ifstream object, primarily so that std::getline can get called further down the callstack.
I was wondering if there was a “standard” way of doing the adapting or do I need to role up my sleeves and code?
TIA.
Faffed around with this for a while:
std::stringstream buff;
BYTE ib[2048];
ULONG totread=0, read=0, sbuff = 2048;
HRESULT hr;
do {
hr = WinInputStream->Read(ib, sbuff, &read);
buff.write(ib, read);
totread+=read;
} while((sbuff == read) && SUCCEEDED(hr));
if(totread == 0) return false;
ifstream i;
TCHAR* ncbuff = const_cast<TCHAR*>(buff.str().c_str());
i.rdbuf()->pubsetbuf(ncbuff, buff.str().length());
But didn't like having to read it all into memory, for it to be processed again.
So I implemented my preview handler using IInitializeWithFile instead.

Blackberry getting notified when the internet connection is available.

is there any way to know whether the net is connected or not in a blackbery device .I have the following code but it was waiting till the network timed out.
int rc = connection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
Is there any other way.
The title of this question has a different meaning than the body of the question. Based on the title, you can be notified when the network starts by using the RadioStatusListener interface, which defines a networkStarted() function. You could then use the checks that coldice recommends to make sure that the current network supports data transfer.
RadioStatusListner JavaDocs
There are several API for getting network info:
RadioInfo.isDataServiceOperational();
CoverageInfo.isOutOfCoverage();
WLANInfo.getWLANState();

Resources