/// <summary> /// Create beacon message /// </summary> /// <param name="msgBytes"></param> /// <param name="etx">Estimated distance from Base</param> /// <param name="beaconNum">Beacon sequence counter</param> /// <returns>Size of message</returns> public static int CreateBeacon(byte[] msgBytes, byte etx, ushort beaconNum, byte ewmarnp, ushort parent) { var idx = 0; msgBytes[idx] = (byte)MessageIds.Beacon; idx += sizeof(byte); //Debug.Print("\tIdx: " + idx); msgBytes[idx] = etx; idx += sizeof(byte); //Debug.Print("\tIdx: " + idx); var beaconAdj = (ushort)Math.Min(beaconNum, ushort.MaxValue); BitConverter.InsertValueIntoArray(msgBytes, idx, beaconAdj); idx += sizeof(ushort); msgBytes[idx] = ewmarnp; idx += sizeof(byte); BitConverter.InsertValueIntoArray(msgBytes, idx, parent); idx += sizeof(ushort); return(idx); }
public static void SendPacket(byte[] msgBytes, out ClassificationType classificationType, out ushort TCPNumber, out ushort originator, out byte TTL, /*out ushort pathLength,*/ out ushort payloadLength) { var idx = 1; // Start at 1 since we've already checked the message ID classificationType = (ClassificationType)msgBytes[idx]; idx += 1; TCPNumber = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); originator = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); TTL = msgBytes[idx]; idx += 1; /* * pathLength = BitConverter.ToUInt16(msgBytes, idx); * idx += sizeof(ushort); * * ushort[] path = new ushort[pathLength]; * int i; * for(i = 0; i < pathLength; i = i + 1){ * path[i] = BitConverter.ToUInt16(msgBytes, idx); * idx += sizeof(ushort); * } */ payloadLength = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); //return path; }
/// <summary> /// Compose a detection message /// </summary> /// <param name="msgBytes"></param> /// <param name="originator"></param> /// <param name="classificatonType"></param> /// <param name="detectionNum"></param> /// <returns></returns> public static int Detection(byte[] msgBytes, ushort originator, ClassificationType classificatonType, int detectionNum, byte TTL) { var idx = 0; // Detection message type msgBytes[idx] = (byte)MessageIds.Detect; idx++; // Classification msgBytes[idx] = (byte)classificatonType; idx++; // Detection message number var detectNum = (ushort)Math.Min(detectionNum, ushort.MaxValue); BitConverter.InsertValueIntoArray(msgBytes, idx, detectNum); idx += sizeof(ushort); // Message originator BitConverter.InsertValueIntoArray(msgBytes, idx, originator); idx += sizeof(ushort); // TTL msgBytes[idx] = (byte)TTL; idx++; // Check if message is the right size if (idx != DetectionMessageSize) { throw new Exception("Compose Detection: message composed is " + idx + " bytes; should be " + DetectionMessageSize); } return(idx); }
/// <summary> /// Compose Heartbeat message /// </summary> /// <param name="msgBytes"></param> /// <param name="originatorId"></param> /// <param name="heartbeatNumber"></param> /// <param name="nodeType"></param> /// <param name="parent"></param> /// <returns>Message size</returns> /// </summary> public static int Heartbeat(byte[] msgBytes, ushort originatorId, ushort heartbeatNumber, SystemGlobal.NodeTypes nodeType, ushort parent, byte TTL) { var idx = 0; msgBytes[idx] = (byte)MessageIds.Heartbeat; idx++; BitConverter.InsertValueIntoArray(msgBytes, idx, originatorId); idx += sizeof(ushort); BitConverter.InsertValueIntoArray(msgBytes, idx, heartbeatNumber); idx += sizeof(ushort); msgBytes[idx] = (byte)nodeType; idx += sizeof(byte); BitConverter.InsertValueIntoArray(msgBytes, idx, parent); idx += sizeof(ushort); // Adding TTL msgBytes[idx] = TTL; // RoutingGlobal.Infinity - 1, hardcoded for now idx += sizeof(byte); // Check if message is the right size if (idx != HeartbeatMessageSize) { throw new Exception("Compose Heartbeat: message composed is " + idx + " bytes; should be " + HeartbeatMessageSize); } return(idx); }
internal static int CreateStatusQueryMessage(byte[] msgBytes, ushort beaconNum) { var idx = 0; msgBytes[idx] = (byte)MessageIds.StatusQuery; idx += sizeof(byte); var beaconAdj = (ushort)Math.Min(beaconNum, ushort.MaxValue); BitConverter.InsertValueIntoArray(msgBytes, idx, beaconAdj); idx += sizeof(ushort); return(idx); }
public static int RecievePacket(byte[] msgBytes, ushort originator, ushort destination, ClassificationType classificatonType, int rcvNumber, byte TTL, /*ushort pathLength, ushort[] path,*/ ushort payloadLength) { var idx = 0; // Detection message type msgBytes[idx] = (byte)MessageIds.Recieve; idx++; // Classification msgBytes[idx] = (byte)classificatonType; idx++; // Detection message number var rcvNum = (ushort)Math.Min(rcvNumber, ushort.MaxValue); BitConverter.InsertValueIntoArray(msgBytes, idx, rcvNum); idx += sizeof(ushort); // Message originator BitConverter.InsertValueIntoArray(msgBytes, idx, originator); idx += sizeof(ushort); // Message destination BitConverter.InsertValueIntoArray(msgBytes, idx, destination); idx += sizeof(ushort); // TTL msgBytes[idx] = (byte)TTL; idx++; /* * // Path Length * BitConverter.InsertValueIntoArray(msgBytes, idx, pathLength); * idx += sizeof(ushort); * * // Add Path * int i; * for (i = 0; i < pathLength; i = i + 1) * { * * BitConverter.InsertValueIntoArray(msgBytes, idx, path[i]); * idx += sizeof(ushort); * } */ // Payload Length BitConverter.InsertValueIntoArray(msgBytes, idx, payloadLength); idx += sizeof(ushort); return(idx); }
internal static void ParseStatusResponseMessage(byte[] msgBytes, out Color color, out byte etx, out ushort beaconNum, out byte pathEWRNP_B2N) { var idx = 1; color = (Color)msgBytes[idx]; idx += sizeof(byte); etx = msgBytes[idx]; idx += sizeof(byte); beaconNum = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); pathEWRNP_B2N = msgBytes[idx]; idx += sizeof(byte); }
/// <summary> /// Parse detection message /// </summary> /// <param name="msgBytes"></param> /// <param name="classificationType"></param> /// <param name="detectionNum"></param> /// <param name="originator"></param> public static void Detection(byte[] msgBytes, out ClassificationType classificationType, out ushort detectionNum, out ushort originator, out byte TTL) { var idx = 1; // Start at 1 since we've already checked the message ID classificationType = (ClassificationType)msgBytes[idx]; idx += 1; detectionNum = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); originator = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); TTL = msgBytes[idx]; idx += 1; }
public static void RecievePacket(byte[] msgBytes, out ClassificationType classificationType, out ushort sndNumber, out ushort originator, out ushort destination, out byte TTL, /*out ushort pathLength, out ushort cur_node,*/ out ushort payloadLength) { var idx = 1; // Start at 1 since we've already checked the message ID classificationType = (ClassificationType)msgBytes[idx]; idx += 1; sndNumber = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); originator = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); destination = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); TTL = msgBytes[idx]; idx += 1; /* * pathLength = BitConverter.ToUInt16(msgBytes, idx); * idx += sizeof(ushort); * * ushort[] path_minus_self = new ushort[pathLength - 1]; * int i; * for (i = 0; i < pathLength - 1; i = i + 1) * { * path_minus_self[i] = BitConverter.ToUInt16(msgBytes, idx); * idx += sizeof(ushort); * } * if (pathLength > 1) * { * cur_node = BitConverter.ToUInt16(msgBytes, idx); * } * else * { * cur_node = 0; * } */ idx += sizeof(ushort); payloadLength = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); //return path_minus_self; }
internal static int CreateStatusResponseMessage(byte[] msgBytes, ushort beaconNum) { var idx = 0; msgBytes[idx] = (byte)MessageIds.StatusResponse; idx += sizeof(byte); msgBytes[idx] = (byte)RoutingGlobal._color; idx += sizeof(byte); msgBytes[idx] = RoutingGlobal.BestEtx; idx += sizeof(byte); var beaconAdj = (ushort)Math.Min(beaconNum, ushort.MaxValue); BitConverter.InsertValueIntoArray(msgBytes, idx, beaconAdj); idx += sizeof(ushort); msgBytes[idx] = RoutingGlobal.GetPathEWRNP(); idx = +sizeof(byte); return(idx); }
/// <summary> /// Parse heartbeat message /// </summary> /// <param name="msgBytes"></param> /// <param name="originatorId"></param> /// <param name="heartbeatNumber"></param> /// <param name="nodeType"></param> /// <param name="parent"></param> public static int HeartBeat(byte[] msgBytes, out ushort originatorId, out ushort heartbeatNumber, out SystemGlobal.NodeTypes nodeType, out ushort parent, out byte TTL) { var idx = 1; // Ignore message type originatorId = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); heartbeatNumber = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); nodeType = (SystemGlobal.NodeTypes)msgBytes[idx]; idx += sizeof(byte); parent = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); // Added TTL parsing TTL = msgBytes[idx]; idx += sizeof(byte); return(idx); }
internal static void ParseStatusQueryMessage(byte[] msgBytes, out ushort beaconNum) { var idx = 1; beaconNum = BitConverter.ToUInt16(msgBytes, idx); }
/// <summary> /// Parse Advanced Heartbeat message /// </summary> /// <param name="msgBytes"></param> /// <param name="originatorId"></param> /// <param name="heartbeatNumber"></param> /// <param name="nodeType"></param> /// <param name="parent"></param> /// <param name="bestetx"></param> /// <param name="neighbors"></param> /// <param name="nbrStatus"></param> /// <param name="numSamplesRec"></param> /// <param name="numSyncSent"></param> /// <param name="avgRSSI"></param> /// <param name="ewrnp"></param> /// <returns>Message size</returns> /// </summary> /*CAUTION: Any change in the advanced heartbeat structure should be accounted for in variables * NetManagerGlobal.AdvHeartbeatFixedSize and NetManagerGlobal.EachNeighborInfoSize */ public static int HeartBeat(byte[] msgBytes, out ushort originatorID, out ushort heartbeatNumber, out SystemGlobal.NodeTypes nodeType, out ushort parent, out byte bestetx, out byte num_nbrs, out ushort[] neighbors, out byte[] nbrStatus, out ushort[] numSamplesRec, out ushort[] numSyncSent, out byte[] avgRSSI, out byte[] ewrnp, out byte[] isAvailableForUpperLayers, out byte TTL) { var idx = 1; // Ignore message type originatorID = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); heartbeatNumber = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); nodeType = (SystemGlobal.NodeTypes)msgBytes[idx]; idx += sizeof(byte); parent = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); bestetx = msgBytes[idx]; idx += sizeof(byte); num_nbrs = msgBytes[idx]; idx += sizeof(byte); byte num_nbrs_reported = (byte)((msgBytes.Length - AdvHeartbeatFixedSize) / EachNeighborInfoSize); neighbors = new ushort[num_nbrs_reported]; nbrStatus = new byte[num_nbrs_reported]; numSamplesRec = new ushort[num_nbrs_reported]; numSyncSent = new ushort[num_nbrs_reported]; avgRSSI = new byte[num_nbrs_reported]; ewrnp = new byte[num_nbrs_reported]; isAvailableForUpperLayers = new byte[num_nbrs_reported]; for (int i = 0; i < neighbors.Length; i++) { neighbors[i] = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { nbrStatus[i] = msgBytes[idx]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { numSamplesRec[i] = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { numSyncSent[i] = BitConverter.ToUInt16(msgBytes, idx); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { avgRSSI[i] = msgBytes[idx]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { ewrnp[i] = msgBytes[idx]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { isAvailableForUpperLayers[i] = msgBytes[idx]; idx += sizeof(byte); } // Added TTL parsing TTL = msgBytes[idx]; idx += sizeof(byte); return(idx); }
/// <summary> /// Compose Advanced Heartbeat message /// </summary> /// <param name="msgBytes"></param> /// <param name="originatorId"></param> /// <param name="heartbeatNumber"></param> /// <param name="nodeType"></param> /// <param name="parent"></param> /// <param name="bestetx"></param> /// <param name="neighbors"></param> /// <param name="nbrStatus"></param> /// <param name="numSamplesRec"></param> /// <param name="numSyncSent"></param> /// <param name="avgRSSI"></param> /// <param name="ewrnp"></param> /// <returns>Message size</returns> /// </summary> /*CAUTION: Any change in the advanced heartbeat structure should be accounted for in variables * NetManagerGlobal.AdvHeartbeatFixedSize and NetManagerGlobal.EachNeighborInfoSize */ public static int Heartbeat(byte[] msgBytes, ushort originatorId, ushort heartbeatNumber, SystemGlobal.NodeTypes nodeType, ushort parent, byte bestetx, ushort[] neighbors, byte[] nbrStatus, ushort[] numSamplesRec, ushort[] numSyncSent, byte[] avgRSSI, byte[] ewrnp, byte[] isAvailableForUpperLayers, byte TTL) { var idx = 0; msgBytes[idx] = (byte)MessageIds.NeighborInfo; idx++; BitConverter.InsertValueIntoArray(msgBytes, idx, originatorId); idx += sizeof(ushort); BitConverter.InsertValueIntoArray(msgBytes, idx, heartbeatNumber); idx += sizeof(ushort); msgBytes[idx] = (byte)nodeType; idx += sizeof(byte); BitConverter.InsertValueIntoArray(msgBytes, idx, parent); idx += sizeof(ushort); msgBytes[idx] = bestetx; idx += sizeof(byte); msgBytes[idx] = (byte)neighbors.Length; idx += sizeof(byte); for (int i = 0; i < neighbors.Length; i++) { BitConverter.InsertValueIntoArray(msgBytes, idx, neighbors[i]); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { msgBytes[idx] = nbrStatus[i]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { BitConverter.InsertValueIntoArray(msgBytes, idx, numSamplesRec[i]); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { BitConverter.InsertValueIntoArray(msgBytes, idx, numSyncSent[i]); idx += sizeof(ushort); } for (int i = 0; i < neighbors.Length; i++) { msgBytes[idx] = avgRSSI[i]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { msgBytes[idx] = ewrnp[i]; idx += sizeof(byte); } for (int i = 0; i < neighbors.Length; i++) { msgBytes[idx] = isAvailableForUpperLayers[i]; idx += sizeof(byte); } // Adding TTL msgBytes[idx] = TTL; idx += sizeof(byte); return(idx); }