I am using the newly released INET 4.0 framework for OMNET++ and I would like to obtain the received signal strength value in a wireless host (of type AdhocHost). How may I do that?
In INET 4.0.0 the packet received by a module contains several tags. Between others there is SignalPowerInd tag. According to SignalTag.msg:
This indication specifies the average analog signal power that was detected during receiving the packet.
It may be present on a packet from the phyiscal layer to the application.
This tag is present in packet processing by a wireless MAC layer, for example:
And packet received by application layer contains SignalPowerInd too:
One can obtain the value of `SignalPowerInd` from received radio packet in any layer using standard API. For example, to obtain it in `UdpBasicApp` one should add in `UdpBasicApp.cc`:
#include "inet/physicallayer/common/packetlevel/SignalTag_m.h"
// ...
void UdpBasicApp::socketDataArrived(UdpSocket *socket, Packet *packet) {
if (packet->findTag<SignalPowerInd>() != nullptr) {
auto signalPowerInd = packet->getTag<SignalPowerInd>();
auto rxPower = signalPowerInd->getPower().get();
EV_INFO << "RX power= " << rxPower << "W" << endl;
}
// process incoming packet
processPacket(packet);
}
Related
I am building an ethernet simulation project to send and receive UDP packages to an external device (let's call it A).
I am supposed to simulate multiple devices, some of them send UDP packages (let's call them B) and some receive UDP packages (let's call them C), B and C are on two different VLANs with two different IDs.
I used an external ETH/Adapter for B and C, which both are connected to a switch alongside the main device A (which can see both the VLANs). then I configured the two eth/adp on windows by setting the "VLAN and Priority" to Enabled and Set VLAN ID with the correct ID for each B and C, finally, I set static IPs for each one of them.
I then used QT to create the simulation project, The Receiving parts are perfect Device A is transmitting UDP packages to Multicast and I join with VLAN C on the Multicast and start reading these frames.
The problem is with sending, I am able to send the frames correctly however the 4 bytes that define the Priority, DEI, and ID are missing (which means device A is not receiving and dumping these frames)
You can see in the below screenshot, on the right the healthy packages that are accepted by device A and on the left the simulated frames that are not accepted
Comaprison between accepted and unaccepted packages
Here is the Code I use to bind and Join Multicast
socket_1 = new QUdpSocket(this);
qDebug() << "Binding UDP Socket ..." ;
bool bind_res = socket_1->bind(QHostAddress("192.168.11.4"), 51011 , QUdpSocket::ShareAddress);
if(!bind_res)
{
qDebug() << "Faild to bind with Error: " +socket_1->errorString() ;
QApplication::quit();
}
bool join_res = socket_1->joinMulticastGroup(interface->GRP_IP,interface->Qinterface);
if(!join_res)
{
qDebug() << "Failed to join with error: "+ socket_1->errorString() ;
QApplication::quit();
}
connect(socket_1, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
qDebug() << "UDP Socket initialized successfully ..." ;
and here is the function to send (interface->GRP_IP is the Multicast IP)
void UDPSocket_VLAN11::sendUDP_1(QByteArray data)
{
qint64 res = socket_1->writeDatagram(data, interface->GRP_IP, 50011);
qDebug() << " --- Sending UDP Packet ---";
qDebug() << "Sending to: " << interface->GRP_IP;
qDebug() << "Sending port: " << port;
qDebug() << "Sending Size: " << data.size();
qDebug() << "Sending: " << data.toHex().toLower();
qDebug() << "Sending Result: " << res;
}
Can someone please point how to set these values weather it's in the configuration of the VLAN or the socket in QT ?
So yes, as #Zac67 mentioned, the main issue was that the eth/usb adapters weren't supporting this protocol and I had a choice of either keep looking for the right adapters or, as I finally did, to change the HW setup and get ride of the adapters and instead I used the native NIC ethernet port on the machine and configured it using Hyper-V to simulate the VLAN
In OMNET++ with INET framework, I want to find out how many packets are received from each sending node.I found the below code. Can anyone tell me what is function of "it->second++" command in below code?
std::map<L3Address, uint64_t> recPkt;
auto it = recPkt.find(senderAddr);
if (it == recPkt.end())
recPkt[senderAddr] = 1;
else
it->second++;
Also, can anyone suggest how to display the number of received packets per node.
it is an iterator to an element of std::map. Iterator is something like a pointer. it points to a pair: <L3Address, uint64_t>. Probably the address of sender is the first element of this pair, and the second one is the number of received packets.
The first element of this pair may be obtained using it->first while the second via it->second.
Operation recPkt.find(senderAddr) checks whether recPkt contains an entry with the address senderAddr:
if not, it points to recPkt.end(), then a new entry is created and 1 is set as value (because first packet has been just received),
if entry for senderAddr already exists, the second value of this element (counter) is incremented using it->second++
To show current value of received packets to internal log window one may use:
for (auto it : recPkt) {
EV << "From address " << it.first << " received " << it.second << " packets." << std::endl;
}
However, the better way is to write these values to statistics. The best place for it is a finish() method of your module:
void YourModule::finish() {
// ..
for (auto it : recPkt) {
std::string name = "Received packet from ";
name += it.first.str(); // address
recordScalar(name, it.second);
}
}
Reference: C++ Reference, std::map
EDIT
One more thing. The declaration of recPkt i.e. line:
std::map<L3Address, uint64_t> recPkt;
must be in your class. recPkt cannot be declared just before use.
My compound module is multiple layers as show in the attached figure.
Here Layer2 has a cPacketQueue buffer and I want the Layer1 module to directly insert packets into this cPacketQueue of Layer2. Layer1 and Layer2 gates are connected unidirecttionally as show in the figure.
Layer1Gate --> Layer2Gate
UPDATED:
Layer 1 creates Packets with different priorities (0-7) and injects to 8 different cPacketQueues in Layer2 named as priorityBuffers[i], (i is the index).
The Layer2 then sends self messages in intervals of 10ns to poll all these buffers in each iteration and send the packets.
This is all I am doing now. It works fine. But I know 10ns polling is definitely not an efficient way to do this and achieve QoS. So requesting for a better alternative.
I suggest adding a ControlInfo object with priority to every packet from Layer1, send the packet using send() command, then checking ControlInfo of received packet in Layer2, and insert the packet into a specific queue.
Firstly, one should define a class for ControlInfo, for example in common.h:
// common.h
class PriorityControlInfo : public cObject {
public:
int priority;
};
Then in C++ code of Layer1 simple module:
#include "common.h"
// ...
// in the method where packet is created
cPacket * packet = new cPacket();
PriorityControlInfo * info = new PriorityControlInfo();
info->priority = 2; // 2 is desired queue number
packet->setControlInfo(info);
send (packet, "out");
And finally in Layer2:
#include "common.h"
// ...
void Layer2::handleMessage(cMessage *msg) {
cPacket *packet = dynamic_cast<cPacket *>(msg);
if (packet) {
cObject * ci = packet->removeControlInfo();
if (ci) {
PriorityControlInfo * info = check_and_cast<PriorityControlInfo*>(ci);
int queue = info->priority;
EV << "Received packet to " << static_cast<int> (queue) << " queue.\n";
priorityBuffers[queue].insert(packet);
EV << priorityBuffers[queue].info() << endl;
}
}
}
According to using of self messages: I do not understand clearly what is your intention.
Does Layer2 should send a packet immediately after receiving it? If yes why do you use a buffer? In that situation instead of inserting a packet to a buffer, Layer2 should just send it to the Layer3.
Does Layer2 should do something else after receiving a packet and inserting it in a buffer? If yes, just call this action (function) in the above handleMessage().
In the both above variants there is no need to use self messages.
I suppose I actually have two separate questions, but I think that they are related enough to include them both. The context is a Linux USB device driver (not userspace).
After transmitting a request URB, how do I receive the response once my complete callback is called?
How can I use interrupt URBs for single request/response pairs, and not as actual continuous interrupt polling (as they are intended)?
So for some background, I'm working on a driver for the Microchip MCP2210 a USB-to-SPI Protocol Converter with GPIO (USB 2.0, datasheet here). This device advertises as generic HID and exposes two interrupt endpoints (an in and an out) as well as it's control endpoint.
I am starting from a working, (but alpha-quality) demo driver written by somebody else and kindly shared with the community. However, this is a HID driver and the mechanism it uses to communicate with the device is very expensive! (sending a 64 byte message requires allocating a 6k HID report struct, and allocation is sometimes performed in the context of an interrupt, requiring GFP_ATOMIC!). We'll be accessing this from an embedded low-memory device.
I'm new to USB drivers and still pretty green with Linux device drivers in general. However, I'm trying to convert this to a plain-jane USB driver (not HID) so I can use the less expensive interrupt URBs for my communications. Here is my code for transmitting my request. For the sake of (attempted) brevity, I'm not including the definition of my structs, etc, but please let me know if you need more of my code. dev->cur_cmd is where I'm keeping the current command I'm processing.
/* use a local for brevity */
cmd = dev->cur_cmd;
if (cmd->state == MCP2210_CMD_STATE_NEW) {
usb_fill_int_urb(dev->int_out_urb,
dev->udev,
usb_sndintpipe(dev->udev, dev->int_out_ep->desc.bEndpointAddress),
&dev->out_buffer,
sizeof(dev->out_buffer), /* always 64 bytes */
cmd->type->complete,
cmd,
dev->int_out_ep->desc.bInterval);
ret = usb_submit_urb(dev->int_out_urb, GFP_KERNEL);
if (ret) {
/* snipped: handle error */
}
cmd->state = MCP2210_CMD_STATE_XMITED;
}
And here is my complete fn:
/* note that by "ctrl" I mean a control command, not the control endpoint */
static void ctrl_complete(struct urb *)
{
struct mcp2210_device *dev = urb->context;
struct mcp2210_command *cmd = dev->cur_cmd;
int ret;
if (unlikely(!cmd || !cmd->dev)) {
printk(KERN_ERR "mcp2210: ctrl_complete called w/o valid cmd "
"or dev\n");
return;
}
switch (cmd->state) {
/* Time to rx the response */
case MCP2210_CMD_STATE_XMITED:
/* FIXME: I think that I need to check the response URB's
* status to find out if it was even transmitted or not */
usb_fill_int_urb(dev->int_in_urb,
dev->udev,
usb_sndintpipe(dev->udev, dev->int_in_ep->desc
.bEndpointAddress),
&dev->in_buffer,
sizeof(dev->in_buffer),
cmd->type->complete,
dev,
dev->int_in_ep->desc.bInterval);
ret = usb_submit_urb(dev->int_in_urb, GFP_KERNEL);
if (ret) {
dev_err(&dev->udev->dev,
"while attempting to rx response, "
"usb_submit_urb returned %d\n", ret);
free_cur_cmd(dev);
return;
}
cmd->state = MCP2210_CMD_STATE_RXED;
return;
/* got response, now process it */
case MCP2210_CMD_STATE_RXED:
process_response(cmd);
default:
dev_err(&dev->udev->dev, "ctrl_complete called with unexpected state: %d", cmd->state);
free_cur_cmd(dev);
};
}
So am I at least close here? Secondly, both dev->int_out_ep->desc.bInterval and dev->int_in_ep->desc.bInterval are equal to 1, will this keep sending my request every 125 microseconds? And if so, how do I say "ok, ty, now stop this interrupt". The MCP2210 offers only one configuration, one interface and that has just the two interrupt endpoints. (I know everything has the control interface, not sure where that fits into the picture though.)
Rather than spam this question with the lsusb -v, I'm going to pastebin it.
Typically, request/response communication works as follows:
Submit the response URB;
submit the request URB;
in the request completion handler, if the request was not actually sent, cancel the response URB and abort;
in the response completion handler, handle the response data.
All that asynchronous completion handler stuff is a big hassle if you have a single URB that is completed almost immediately; therefore, there is the helper function usb_interrupt_msg() which works synchronously.
URBs to be used for polling must be resubmitted (typically from the completion handler).
If you do not resubmit the URB, no polling happens.
I am using OpenCV in Visual Studio 2010 to track an object, and I am trying to send a value to the Arduino to rotate servos attached to the camera. I am using an Arduino Uno. I have completed the C++ code that tracks the object and determines which direction the camera needs to be rotated, but I am having trouble sending this data to the Arduino. I am currently trying to use an RS-232 cable for this. I am using a Type-B USB cable to program my Arduino and an RS-232 to try to send the data from Visual Studio to the Arduino. Here is my code for the Visual Studio serial communication:
int portspeed(int centerpix, int xmid)
{HANDLE hDevice = CreateFile(L"COM5",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
DCB lpTest;
GetCommState(hDevice,&lpTest);
lpTest.BaudRate = CBR_9600;
lpTest.ByteSize = 8;
lpTest.Parity = NOPARITY;
lpTest.StopBits = ONESTOPBIT;
SetCommState(hDevice,&lpTest);
DWORD btsIO;
if (centerpix<xmid)
{
char test[] = "2";
WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
cout << "Turn right " << test << endl;
}
else
{
char test[] = "3";
WriteFile(hDevice,test,strlen(test),&btsIO,NULL);
cout << "Turn left " << test << endl;
}
return 0;
}
On the Arduino code side I have this, which I am using to attempt to light two different LEDS to see if the program is able to correctly communicate which direction it needs to rotate:
int incomingByte = 0; // For incoming serial data
void setup()
{
Serial.begin(9600); // Opens serial port, sets data rate to 9600 bit/s
}
void loop()
{
// Send data only when you receive data:
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte==50) //if =2
analogWrite(9,100);
else
analogWrite(9,0);
if (incomingByte==51) //if =3
analogWrite(10,50);
else analogWrite(10,0);
delay(3000);
}
else
analogWrite(9,255);
}
My interpretation is that I need to start the C++ program (which continuously sends the data over the serial communication), and then attach the TX pin from the RS-232 into the RX pin (digital 0) on the Arduino. When I try to upload the program to the Arduino I am given an error,
avrdude: stk500_getsync(): not in sync: resp=0x00
This only occurs when I have a wire going into the RX pin, even if this wire is not connected to anything. I believe that this error occurs because the RX is looking for an input with a baud rate of 9600, but it still gives me this error when the C++ program is running and sending the data with a rate of 9600.
How can I send a value from a Visual Studio project doing real time image processing on a laptop to an Arduino via serial communication?
Speaking as someone with limited Win32 experience (more of a .NET guy, really), I think your problem may be in write buffering.
By default, writes to a file or port are buffered in memory. Perhaps the write is never getting sent to the port as you are never closing the file handle nor calling [FlushFileBuffers][3].
Try adding this prior to return 0;:
//After a time, sensitive write
FlushFileBuffers(hDevice);
//or, more properly for the end of the program.
CloseHandle(hDevice);