/// <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);
                }
Exemple #2
0
            /// <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);
            }
Exemple #3
0
                /// <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);
                }
Exemple #4
0
            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);
                }
Exemple #6
0
            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);
            }
Exemple #7
0
                /// <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);
                }