Since a Script comprises basically of C# (C sharp) code, it is very flexible. Let's assume that we would like the analyzer to send the MAC 802.15.4 payload of messages from device #1 over UDP to another application.
The user should define the structure of the connection string in the Preferences to be in the format of IP:port (for example 192.168.1.1:1000):
Figure 364 – Setting the communication port for Script access
The Script will then use this string at startup to open a UDP socket.
When processing a message the Script will check if the device number = 1, and if so, will build the UDP message and send it over UDP.
Example VI:
In the following example, the Script will divert relevant portions of selected messages to the UDP port.
//Script signing code, don't modify:5CE8D3587E186617
using System;
using log4net;
using Perytons.Analyzer.Core;
using Perytons.Analyzer.Core.Graphs;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
using Perytons.Analyzer.Core.Preferences;
// This Script show how to send some of the received messages to UPD
namespace Perytons.Analyzer.Core.Monitoring
{
public class SendToUDP : ScriptBase // don't remove this line
{
private UdpClient client;
private IPEndPoint endpoint;
public SendToUDP(GraphBuilder builder)
: base(builder) // don't remove this line
{
// open udp port
string connectionString = PreferencesHandler<UserPreferences>
.Instance.ConnectionString; // get connection string
from preferences
if (string.IsNullOrEmpty(connectionString))
{
Loggers.EventsLogger.Error("Connection string is empty");
return;
}
string[] parts = connectionString.Split(':');
int port;
if (!int.TryParse(parts[1], out port))
{
Loggers.EventsLogger.Info("Failed to open UDP connection, bad
IP/port address: " + connectionString + " (use 'IP':port
convention, for example 192.168.1.1:100 or
www.perytons.com:100)");
return;
}
client = new UdpClient();
try { client.Connect(parts[0], port); }
catch (Exception e)
{
Loggers.EventsLogger.Info("Couldn't open the destination port:
" + connectionString, e);
return;
}
}
// define udp message structure
[StructLat(LatKind.Sequential, Pack = 1)]
struct UDPMessage
{
public byte UW; // (0x055 indicates message start).
public uint MessageBumber;
public byte Rssi; // signal strength
public long Time; // start time in usec
public byte Len; // payload length in bytes
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] MacPayload;
}
// This method will be called on each message received, if
// this is a relevant message send it over the UDP
public override void DoScript(Packet packet)
{
packet.AddToTimeView();// add all messages to time view
if ((client != null) && (packet.Field("DeviceIndex").Exist))
{
if (packet.Field("DeviceIndex").Value == 16) //device index=16
{
// build UDP message
UDPMessage message = new UDPMessage();
message.UW = 0x055; // (0x055 indicates message start).
message.MessageBumber = (uint)packet.Field
("InfoMessageNum").Value;
message.Rssi = (byte)packet.Field
("InfoSignalStrength").Value;
message.Time = packet.Field("InfoStartTime").Value;
// read payload string, convert to array of bytes
string payload = packet.Field
("MacDataList").ValueText.Replace(" ", "");
// get the payload data without spaces
message.Len = (byte)(payload.Length / 2);
message.MacPayload = new byte[128];
int discarded;
byte[] b = HexEncoding.GetBytesReverse
(payload, out discarded);
b.CopyTo(message.MacPayload,0);
// Turn into bytes
byte[] data=Utils.StructToBytes<UDPMessage>(message);
// send message over UDP
client.Send(data, data.Length);
}
}
base.DoScript(packet); // don't remove this line
}
// close UDP port
public override void Cleanup()
{
if (client != null)
{
client.Close(); // close UDP port
}
base.Cleanup(); // don't remove this line
}
}
The following image shows the UDP packets built by the Script as these were captured by WireShark IP analyzer:
Figure 365 – WireShark example screen