Counting the number of transmissions in TinyOS 2.x - tinyos

I'm trying to implement an application in NesC able to count the number of transmissions performed along the simulation, however I'm facing many difficulties. No approach that I tryed works. Could anyone help me? this is my application:
module FloodingC {
uses {
interface Boot;
interface SplitControl as AMControl;
interface Timer<TMilli> as MilliTimer;
interface Receive;
interface AMSend;
interface Packet;
interface AMPacket;
interface RootControl;
interface PacketAcknowledgements as PackAck;
}
}
implementation {
message_t packet;
bool locked = FALSE;
uint16_t flag;
event void Boot.booted() {
flag = 0;
call AMControl.start();
}
event void AMControl.startDone(error_t err) {
if(err == SUCCESS) {
if(TOS_NODE_ID == 1)
call RootControl.setRoot();
call MilliTimer.startOneShot(1024);
}
else {
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err) {
}
void sendMsg(){
floodingMsg_t* msg = (floodingMsg_t* ) call Packet.getPayload(&packet, sizeof(floodingMsg_t));
if(msg == NULL) {
return;
}
flag = 1;
msg->nodeid = TOS_NODE_ID;
msg->counter = 1;
call PackAck.requestAck(&packet);
if(call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(floodingMsg_t)) == SUCCESS) {
locked = TRUE;
}
}
event void MilliTimer.fired() {
if(locked) {
return;
}
else {
if (call RootControl.isRoot()){
sendMsg();
}
}
}
event void AMSend.sendDone(message_t *msg, error_t error){
if (call PackAck.wasAcked(msg) == SUCCESS){
locked = FALSE;
}
else{
sendMsg();
}
}
event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) {
floodingMsg_t* newMsg = (floodingMsg_t* )payload;
if (locked == TRUE) return msg;
if(flag == 0) {
flag = 1;
newMsg->nodeid = TOS_NODE_ID;
newMsg->counter++;
call AMSend.send(AM_BROADCAST_ADDR, msg, call Packet.maxPayloadLength());
}
return msg;
}
}
Thanks

You can count them using a BaseStation sniffer (included in tinyos), or adding a sequence number on your transmitted packet.

Related

Bluetooth Disconnection Takes Too Long

I can connect to my bluetooth device and when I want to disconnect it takes about 30 seconds to disconnect and for a few seconds it disconnects and automatically reconnects to the device in the background I don't want this to happen, how can I disconnect it literally?
I dispose service and device and pull to null but that doesn't work
This is Disconnect method :
public bool DisConnect()
{
try
{
IsScannerActiwe = false;
ButtonPressed = false;
IsConnected = false;
if (currentSelectedGattCharacteristic != null && currentSelectedService != null)
{
currentSelectedGattCharacteristic.Service?.Dispose();
currentSelectedService?.Dispose();
currentSelectedGattCharacteristic = null;
currentSelectedService = null;
if (bluetoothLeDevice != null)
{
bluetoothLeDevice.Dispose();
bluetoothLeDevice = null;
return true;
}
}
}
catch (System.Exception ex)
{
Trace.WriteLine("Exception Handled -> DisConnect: " + ex);
}
return false;
}
I dispose the device and the service and pull it to null and the function returns true but it does not break the connection and it is null for a few seconds and the service continues to run in the background automatically
I also tried another function for disconnection
public async Task<bool> ClearBluetoothLEDevice()
{
if (IsConnected)
{
IsScannerActiwe = false;
ButtonPressed = false;
IsConnected = false;
// Need to clear the CCCD from the remote device so we stop receiving notifications
var result = await currentSelectedGattCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None);
if (result != GattCommunicationStatus.Success)
{
return false;
}
else
{
if (currentSelectedGattCharacteristic != null)
{
currentSelectedGattCharacteristic.ValueChanged -= CurrentSelectedGattCharacteristic_ValueChanged;
IsConnected = false;
currentSelectedService.Dispose();
currentSelectedGattCharacteristic.Service.Dispose();
currentSelectedService?.Dispose(); //added code
currentSelectedService = null; //added code
bluetoothLeDevice?.Dispose();
bluetoothLeDevice = null;
currentSelectedGattCharacteristic = null;
return true;
}
}
}
return false;
}
I literally cannot disconnect from the device

Wso2 Stream Processor : Error occurred while processing eventByteBufferQueue

I have two nodes of wso2-am analytics server (2.6.0) which is Wso2 Stream processors. I see following error on passive node of cluster. The active node is fine and I don't see any error. Analytics result has no impact for users who is viewing data on API Publisher or Store. however there is an error in passive node.
please advise what is causing following issue..
2019-02-26 17:06:09,513] ERROR {org.wso2.carbon.stream.processor.core.ha.tcp.EventSyncServer} - Error occurred while processing eventByteBufferQueue null java.nio.BufferUnderflowException
Just meet the same issue, here is my problem and solution.
1) Using the WSO2 SP HA deployment.
2) When Event come in active node and according the source mapping of the streaming, some fields are NULL
3) Active Node would like sync this event to passive node
4) passive node pick up the event data from the 'eventByteBufferQueue' to meet the standby-take over mechanism
5) passive node cannot parse the data from active node and reports error exception.
the root cause is SP only support NULL String by default, when NULL with LONG, INTEGER.. the error occurred. but for me, Long fields have NULL is the normal case, you can change data type to string.
here is my solution:
org.wso2.carbon.stream.processor.core_2.0.478.jar
Add logic to support NULL
BinaryMessageConverterUtil.java for sending event data from active node
public final class BinaryMessageConverterUtil {
public static int getSize(Object data) {
if (data instanceof String) {
return 4 + ((String) data).length();
} else if (data instanceof Integer) {
return 4;
} else if (data instanceof Long) {
return 8;
} else if (data instanceof Float) {
return 4;
} else if (data instanceof Double) {
return 8;
} else if (data instanceof Boolean) {
return 1;
} else if (data == null) {
return 0;
}else {
//TODO
return 4;
}
}
public static EventDataMetaInfo getEventMetaInfo(Object data) {
int eventSize;
Attribute.Type attributeType;
if (data instanceof String) {
attributeType = Attribute.Type.STRING;
eventSize = 4 + ((String) data).length();
} else if (data instanceof Integer) {
attributeType = Attribute.Type.INT;
eventSize = 4;
} else if (data instanceof Long) {
attributeType = Attribute.Type.LONG;
eventSize = 8;
} else if (data instanceof Float) {
attributeType = Attribute.Type.FLOAT;
eventSize = 4;
} else if (data instanceof Double) {
attributeType = Attribute.Type.DOUBLE;
eventSize = 8;
} else if (data instanceof Boolean) {
attributeType = Attribute.Type.BOOL;
eventSize = 1;
} else if (data == null){
attributeType = Attribute.Type.OBJECT;
eventSize = 0; //'no content between the HA nodes for NULL fields'
} else {
//TODO
attributeType = Attribute.Type.OBJECT;
eventSize = 1;
}
return new EventDataMetaInfo(eventSize, attributeType);
}
public static void assignData(Object data, ByteBuffer eventDataBuffer) throws IOException {
if (data instanceof String) {
eventDataBuffer.putInt(((String) data).length());
eventDataBuffer.put((((String) data).getBytes(Charset.defaultCharset())));
} else if (data instanceof Integer) {
eventDataBuffer.putInt((Integer) data);
} else if (data instanceof Long) {
eventDataBuffer.putLong((Long) data);
} else if (data instanceof Float) {
eventDataBuffer.putFloat((Float) data);
} else if (data instanceof Double) {
eventDataBuffer.putDouble((Double) data);
} else if (data instanceof Boolean) {
eventDataBuffer.put((byte) (((Boolean) data) ? 1 : 0));
} else if (data == null){
//put nothing into he Buffer
} else {
eventDataBuffer.putInt(0);
}
}
public static String getString(ByteBuf byteBuf, int size) throws UnsupportedEncodingException {
byte[] bytes = new byte[size];
byteBuf.readBytes(bytes);
return new String(bytes, Charset.defaultCharset());
}
public static String getString(ByteBuffer byteBuf, int size) throws UnsupportedEncodingException {
byte[] bytes = new byte[size];
byteBuf.get(bytes);
return new String(bytes, Charset.defaultCharset());
}
}
SiddhiEventConverter.java for processing event data at passive node
static Object[] toObjectArray(ByteBuffer byteBuffer,
String[] attributeTypeOrder) throws UnsupportedEncodingException {
if (attributeTypeOrder != null) {
Object[] objects = new Object[attributeTypeOrder.length];
for (int i = 0; i < attributeTypeOrder.length; i++) {
switch (attributeTypeOrder[i]) {
case "INT":
objects[i] = byteBuffer.getInt();
break;
case "LONG":
objects[i] = byteBuffer.getLong();
break;
case "STRING":
int stringSize = byteBuffer.getInt();
if (stringSize == 0) {
objects[i] = null;
} else {
objects[i] = BinaryMessageConverterUtil.getString(byteBuffer, stringSize);
}
break;
case "DOUBLE":
objects[i] = byteBuffer.getDouble();
break;
case "FLOAT":
objects[i] = byteBuffer.getFloat();
break;
case "BOOL":
objects[i] = byteBuffer.get() == 1;
break;
case "OBJECT":
//for NULL fields
objects[i] = null;
break;
default:
// will not occur
}
}
return objects;
} else {
return null;
}
}

Desktop Computer - Detecting type of internet connection

Is there any way to detect if my computer is connected via cable or mobile network to the internet?
I tried to analyze the trace-route output but realized that it would need a very large and up-to-date database to detect the type of network the data packages take.
Have this two different types of network maybe some different attributes that are easily to detect?
You can use the NetworkListManager APIs to find if a network is connected to the internet. The example below does both that and checks to see if the network is free. You can just remove the Cost related code:
HRESULT GetFreeInternetInterface(_Out_ GUID* pInterfaceGuid)
{
bool fCoInitialized = false;
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
fCoInitialized = true;
CComPtr<INetworkListManager> pNetworkListManager;
hr = pNetworkListManager.CoCreateInstance(__uuidof(NetworkListManager));
if (SUCCEEDED(hr))
{
CComPtr<IEnumNetworkConnections> pNetworkConnections;
hr = pNetworkListManager->GetNetworkConnections(&pNetworkConnections);
bool fFound = false;
while (SUCCEEDED(hr))
{
CComPtr<INetworkConnection> pNetworkConnection;
hr = pNetworkConnections->Next(1, &pNetworkConnection, nullptr);
if (hr == S_OK)
{
CComPtr<INetworkConnectionCost> pNetworkConnectionCost;
hr = pNetworkConnection.QueryInterface(&pNetworkConnectionCost);
if (SUCCEEDED(hr))
{
NLM_CONNECTIVITY nlmConnectivity = NLM_CONNECTIVITY_DISCONNECTED;
DWORD dwCost = NLM_CONNECTION_COST_UNKNOWN;
if (SUCCEEDED(pNetworkConnection->GetConnectivity(&nlmConnectivity)) &&
(nlmConnectivity & (NLM_CONNECTIVITY_IPV4_INTERNET | NLM_CONNECTIVITY_IPV6_INTERNET)) != 0 &&
SUCCEEDED(pNetworkConnectionCost->GetCost(&dwCost)) &&
dwCost == NLM_CONNECTION_COST_UNRESTRICTED &&
SUCCEEDED(pNetworkConnection->GetAdapterId(pInterfaceGuid)))
{
fFound = true;
break;
}
}
}
else hr = E_NOINTERFACE;
}
if (SUCCEEDED(hr) && !fFound)
hr = E_NOINTERFACE;
}
}
if (fCoInitialized) CoUninitialize();
return hr;
}
Once you have the interface GUID and IP Helper APIs to get the interface type. Here is some code I had that looks for non-cellular networks. You can take it and modify it to do what you need:
BOOL GetAdapterAddresses(
_Out_ PIP_ADAPTER_ADDRESSES * ppIAA
)
{
*ppIAA = NULL;
DWORD len = 0;
DWORD flags = GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER;
if (GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len) != ERROR_BUFFER_OVERFLOW)
return FALSE;
PIP_ADAPTER_ADDRESSES pIAA = (PIP_ADAPTER_ADDRESSES)LocalAlloc(LPTR, len);
if (pIAA) {
GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pIAA, &len);
*ppIAA = pIAA;
return TRUE;
}
return FALSE;
}
bool IsNonCellularIfType(
_In_ IFTYPE IfType
)
{
return // Ignore Loopback and WWAN
IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
IfType != IF_TYPE_WWANPP &&
IfType != IF_TYPE_WWANPP2;
}
DWORD GetNonCellularIfIndex(
)
{
DWORD IfIndex = (DWORD)(-1);
PIP_ADAPTER_ADDRESSES pIAA;
if (GetAdapterAddresses(&pIAA)) {
PIP_ADAPTER_ADDRESSES pIAAList = pIAA;
while (pIAA) {
// Look for Non-Cellular interface
if (pIAA->OperStatus == IfOperStatusUp &&
IsNonCellularIfType(pIAA->IfType)) {
PIP_ADAPTER_UNICAST_ADDRESS_LH pUnicastAddr = pIAA->FirstUnicastAddress;
// Look through all unicast addresses for valid IPv4 or IPv6 addresses
while (pUnicastAddr) {
LPSOCKADDR pAddr = pUnicastAddr->Address.lpSockaddr;
if (pAddr->sa_family == AF_INET) {
if (!IN4_IS_ADDR_LINKLOCAL(&((SOCKADDR_IN *)pAddr)->sin_addr)) {
IfIndex = pIAA->IfIndex;
break;
}
}
else if (pAddr->sa_family == AF_INET6) {
if (!IN6_IS_ADDR_LINKLOCAL(&((SOCKADDR_IN6 *)pAddr)->sin6_addr)) {
IfIndex = pIAA->IfIndex;
break;
}
}
pUnicastAddr = pUnicastAddr->Next;
}
// No need to keep looking any more?
if (IfIndex != (DWORD)(-1))
break; // while (pIAA)
}
pIAA = pIAA->Next;
}
LocalFree(pIAAList);
}
return IfIndex;
}

SCAN_SDT_UNKNOWN - Motorola OPOS Scanner

I am receiving an odd error with my scanner integration. I am making use of an OPOS scanner in my program. When the program closes I disable, release and close the device, but no other application can use it after my program runs. Also if I restart no application can use it. Not even my application which causes the issue. I did find that if I do not claim the device the issue doesn't happen. I am currently trying to get a fresh copy of the DLL in case the release method is somehow corrupted? Any other ideas?
public bool InitBarcode(bool overrideLGBarcode)
{
Util.LogMessage("Initializing barcode scanner!");
if (_barcodeScanner == null)
{
Util.LogMessage("Barcode scanner was null. instantiating a new one");
_barcodeScanner = new OPOSScanner();
_barcodeScanner.AutoDisable = true;
_barcodeScanner.DataEvent += BarcodeDataEventHandler;
Util.LogMessage("Added event handler");
}
else
{
Util.LogMessage("Barcode scanner was not null");
}
if (_barcodeScanner.Open("STI_USBSCANNER") != 0)
{
Util.LogMessage("Barcode scanner \"STI_USBSCANNER\" could not be opened!");
return false;
}
else
{
Util.LogMessage("STI_USBSCANNER was opened");
}
int result = _barcodeScanner.ClaimDevice(-1);
Util.LogMessage("Claiming barcode scanner returned result: " + result);
_barcodeScanner.DecodeData = true;
_barcodeScanner.DeviceEnabled = true;
_barcodeScanner.DataEventEnabled = true;
return true;
}
public void CloseBarcode()
{
Util.LogMessage("Disabling, Releasing and Closing the barcode scanner!");
_barcodeScanner.DataEvent -= BarcodeDataEventHandler;
Util.LogMessage("Removed event handler");
_barcodeScanner.AutoDisable = false;
_barcodeScanner.DecodeData = false;
_barcodeScanner.DataEventEnabled = false;
_barcodeScanner.DeviceEnabled = false;
if (_barcodeScanner.DeviceEnabled != false)
{
Util.LogMessage("Barcode scanner could not be disabled!");
}
else
{
Util.LogMessage("Barcode scanner was disabled!");
}
int result = _barcodeScanner.ReleaseDevice();
Util.LogMessage("ReleseDevice() yielded result of: " + result);
if (result != 0)
{
Util.LogMessage("Barcode scanner could not be released!");
}
else
{
Util.LogMessage("Barcode scanner was released!");
}
if (_barcodeScanner.Close() != 0)
{
Util.LogMessage("Barcode scanner could not be closed!");
}
else
{
Util.LogMessage("Barcode scanner was closed!");
}
_barcodeScanner = null;
}

What is the purpose of the QAbstractButton::checkStateSet() method?

I'm writing my own 4 state button and I'm not quite sure what to put in the checkStateSet() method, if anything.
Here is what I've got so far:
SyncDirectionButton::SyncDirectionButton(QWidget *parent) :
QAbstractButton(parent)
{
setCheckable(true);
setToolTip(tr("Click to change the sync direction"));
_state = NoSync;
}
void SyncDirectionButton::paintEvent(QPaintEvent *e)
{
static QPixmapCache::Key noneKey;
static QPixmapCache::Key bothKey;
static QPixmapCache::Key leftKey;
static QPixmapCache::Key rightKey;
QPainter p(this);
QPixmap pix;
if (checkState() == SyncLeft) {
if (!QPixmapCache::find(leftKey, &pix)) {
pix.load(":/icons/sync-left.png");
leftKey = QPixmapCache::insert(pix);
}
} else if (checkState() == SyncBoth) {
if (!QPixmapCache::find(rightKey, &pix)) {
pix.load(":/icons/sync-right.png");
rightKey = QPixmapCache::insert(pix);
}
} else if (checkState() == SyncRight) {
if (!QPixmapCache::find(bothKey, &pix)) {
pix.load(":/icons/sync-both.png");
bothKey = QPixmapCache::insert(pix);
}
} else if (checkState() == NoSync) {
if (!QPixmapCache::find(noneKey, &pix)) {
pix.load(":/icons/application-exit.png");
noneKey = QPixmapCache::insert(pix);
}
}
p.drawPixmap(0,0,pix);
}
SyncDirectionButton::DirectionState SyncDirectionButton::checkState() const
{
return _state;
}
void SyncDirectionButton::setCheckState(DirectionState state)
{
setChecked(state != NoSync);
if (state != _state) {
_state = state;
}
}
QSize SyncDirectionButton::sizeHint() const
{
return QSize(180,90);
}
void SyncDirectionButton::checkStateSet()
{
}
void SyncDirectionButton::nextCheckState()
{
setCheckState((DirectionState)((checkState()+1)%4));
}
First, the QAbstractButton has 1 "unchecked" state and may have several "checked" states.
This method is called when check state is changed from "unchecked" to "checked". You have to set the inital "checked" state. It should be the first state in your 3 "checked" values,
Also your implementation nextCheckState() should call setChecked(false), when called on 3.rd checked value to return to "unchecked" state.
Betters see the code of QAbstractButton: http://www.koders.com/cpp/fid1779E80AD2DA4C93CA22AB575FAA092A9681AE7B.aspx?s=mdef%3Ainsert

Resources