public static void SerialCallback_base_node(byte[] readBytes) { if (readBytes.Length < 1) { return; } var readChars = System.Text.Encoding.UTF8.GetChars(readBytes); // Decode the input bytes as char using UTF8 string tempStr = new string(readChars); //Debug.Print("Base serial input: " + tempStr); // If 1, note that PC wants to get switch data if (tempStr.Length == 8 && tempStr.Substring(0, 7).Equals("fffffff")) { int rcvSize = AppGlobal.RecieveMessageSize; var rcvPayloadBytes = new byte[rcvSize]; ushort originator = AppGlobal.AppPipe.MACRadioObj.RadioAddress; AppGlobal.ClassificationType classificationType = AppGlobal.ClassificationType.Recieve; byte TTL = Byte.MaxValue; var headerSize = AppGlobal.MoteMessages.Compose.RecievePacket(rcvPayloadBytes, originator, _destination, classificationType, _packetNumber, _TTL, (ushort)_payloadLength); // add payload AppGlobal.MoteMessages.AddPayload.RecievePacket(rcvPayloadBytes, headerSize, readBytes, _payloadLength); #if DBG_VERBOSE Debug.Print("Sending Packet #" + _packetNumber + " to neighbor " + _destination); Debug.Print(" Classification: " + (char)classificationType); Debug.Print(" Originator: " + originator); Debug.Print(" path Length: " + _pathLength); Debug.Print(" payload Length: " + _payloadLength + "\n"); #endif try { #if DBG_VERBOSE Debug.Print("Send Successful"); #endif var status = RoutingGlobal.SendToChild(AppGlobal.AppPipe, _destination, rcvPayloadBytes, rcvSize); } catch (Exception ex) { Debug.Print("SerialComm exception for Detection message [" + rcvSize + "]\n" + ex); } //Debug.Print("I know something you don't+ debug print from mote "); //temComm.Write("helloToYouToo: from Mote \r\n"); //string tempNewStr = "fffffff" + newReturnNum; //temComm.Write(tempNewStr); //_tempTimer = new Timer(temp_timer, null, 0, 1 * 10000); //send readBytes to orignator who send them return; } }
public static void SerialCallback_client_node(byte[] readBytes) { if (readBytes.Length < 1) { return; } var readChars = System.Text.Encoding.UTF8.GetChars(readBytes); // Decode the input bytes as char using UTF8 string tempStr = new string(readChars); //Debug.Print("Client serial input: " + tempStr); // If 1, note that PC wants to get switch data if (readBytes.Length == 8 && tempStr.Substring(0, 7).Equals("fffffff")) { // byte payloadValue = (byte)_rand.Next(11); // Change both for different payload sizes // byte payloadValue = (byte)_rand.Next(11); // Change both for different payload sizes int payloadLength = readBytes.Length; int sendSize = AppGlobal.SendMessageSize; var routedMsg = new byte[sendSize]; ushort originator = AppGlobal.AppPipe.MACRadioObj.RadioAddress; AppGlobal.ClassificationType classificationType = AppGlobal.ClassificationType.Send; byte TTL = Byte.MaxValue; var headerSize = AppGlobal.MoteMessages.Compose.SendPacket(routedMsg, originator, classificationType, _sendMsgNum, TTL, (ushort)payloadLength); // add payload AppGlobal.MoteMessages.AddPayload.SendPacket(routedMsg, headerSize, readBytes, payloadLength); if (RoutingGlobal.IsParent) { #if DBG_VERBOSE Debug.Print("Sending Packet #" + _sendMsgNum + " to Parent"); Debug.Print(" Classification: " + (char)classificationType); Debug.Print(" Originator: " + originator); Debug.Print(" payload Length: " + payloadLength + "\n"); #endif var status = RoutingGlobal.SendToParent(AppGlobal.AppPipe, routedMsg, sendSize); if (status != 999) { Debug.Print("Send Successful"); _sendMsgNum++; RoutingGlobal.UpdateNumTriesInCurrentWindow_Parent(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for Parent " + RoutingGlobal.Parent + "; new value = " + RoutingGlobal.GetNumTriesInCurrentWindow_Parent()); #endif } else //Retry once { #if !DBG_LOGIC Debug.Print("Retrying packet"); #endif RoutingGlobal.CleanseCandidateTable(AppGlobal.AppPipe); Candidate tmpBest = CandidateTable.GetBestCandidate(false); AppGlobal.TempParent = tmpBest.GetMacID(); status = AppGlobal.SendToTempParent(AppGlobal.AppPipe, routedMsg, sendSize); if (status != 999) { Debug.Print("Send Successful"); _sendMsgNum++; tmpBest.UpdateNumTriesInCurrentWindow(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for TempParent " + AppGlobal.TempParent + "; new value = " + tmpBest.GetNumTriesInCurrentWindow()); #endif } } } //Debug.Print("I know something you don't+ debug print from mote "); //temComm.Write("helloToYouToo: from Mote \r\n"); //Random random = new Random(); //int newReturnNum = random.Next(9); //String tempNewStr = "fffffff" + newReturnNum; //temComm.Write(tempNewStr); //_tempTimer = new Timer(temp_timer, null, 0, 1 * 10000); //send readBytes to parent. return; } }
public static void SendDetectionMessage(AppGlobal.ClassificationType classification, int detectNum) { var msgBytes = new byte[AppGlobal.DetectionMessageSize]; var actualSize = AppGlobal.MoteMessages.Compose.Detection(msgBytes, AppGlobal.AppPipe.MACRadioObj.RadioAddress, classification, detectNum, RoutingGlobal.Infinity); #region Uncomment when not using scheduler #if DBG_VERBOSE //Debug.Print("\nAttempting send of detection message " + detectNum + " on pipe " + AppGlobal.AppPipe.PayloadType + " with classification " + (char)classification + ", size " + actualSize + " to parent " + RoutingGlobal.Parent); #elif DBG_SIMPLE Debug.Print("\nSending to " + RoutingGlobal.Parent); #endif #if DBG_VERBOSE var msgS = new System.Text.StringBuilder(); for (var i = 0; i < actualSize; i++) { msgS.Append(msgBytes[i]); msgS.Append(' '); } Debug.Print("\t" + msgS); #endif // If in a reset, do not forward TODO: Change this to "spray" if (RoutingGlobal._color == Color.Red) { #if DBG_VERBOSE || DBG_SIMPLE Debug.Print("\tIn a Reset wave... not forwarded"); #endif return; } // If parent is available, pass it on if (RoutingGlobal.IsParent) { Debug.Print("routed message len: " + msgBytes.Length); var status = RoutingGlobal.SendToParent(AppGlobal.AppPipe, msgBytes, actualSize); Debug.Print("status " + status); if (status != 999) { RoutingGlobal.UpdateNumTriesInCurrentWindow_Parent(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for Parent " + RoutingGlobal.Parent + "; new value = " + RoutingGlobal.GetNumTriesInCurrentWindow_Parent()); #endif } else //Retry once { #if !DBG_LOGIC Debug.Print("Retrying packet"); #endif RoutingGlobal.CleanseCandidateTable(AppGlobal.AppPipe); Candidate tmpBest = CandidateTable.GetBestCandidate(false); AppGlobal.TempParent = tmpBest.GetMacID(); status = AppGlobal.SendToTempParent(AppGlobal.AppPipe, msgBytes, actualSize); if (status != 999) { tmpBest.UpdateNumTriesInCurrentWindow(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for TempParent " + AppGlobal.TempParent + "; new value = " + tmpBest.GetNumTriesInCurrentWindow()); #endif } } } #endregion }
public static void SendPacketMessage(Object state) { Debug.Print("Timer Trigger"); _lcd.Write("Send"); ushort pathLength = 1; int payloadValue = 25000; // byte payloadValue = (byte)_rand.Next(11); // Change both for different payload sizes byte[] payload = BitConverter.GetBytes(payloadValue); int payloadLength = payload.Length; int sendSize = AppGlobal.SendMessageSize; var routedMsg = new byte[sendSize]; ushort originator = AppGlobal.AppPipe.MACRadioObj.RadioAddress; AppGlobal.ClassificationType classificationType = AppGlobal.ClassificationType.Send; byte TTL = Byte.MaxValue; ushort[] path = { originator }; var headerSize = AppGlobal.MoteMessages.Compose.SendPacket(routedMsg, originator, classificationType, _sendMsgNum, TTL, (ushort)payloadLength); // add payload AppGlobal.MoteMessages.AddPayload.SendPacket(routedMsg, headerSize, payload, payloadLength); #if DBG_SIMPLE Debug.Print("\nSending to " + RoutingGlobal.Parent); #endif // If in a reset, do not forward TODO: Change this to "spray" if (RoutingGlobal._color == Color.Red) { #if DBG_VERBOSE || DBG_SIMPLE Debug.Print("\tIn a Reset wave... not forwarded"); #endif return; } // If parent is available, pass it on if (RoutingGlobal.IsParent) { Debug.Print("routed message len: " + routedMsg.Length); var status = RoutingGlobal.SendToParent(AppGlobal.AppPipe, routedMsg, sendSize); Debug.Print("status " + status); if (status != 999) { _sendMsgNum++; RoutingGlobal.UpdateNumTriesInCurrentWindow_Parent(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for Parent " + RoutingGlobal.Parent + "; new value = " + RoutingGlobal.GetNumTriesInCurrentWindow_Parent()); #endif } else //Retry once { #if !DBG_LOGIC Debug.Print("Retrying packet"); #endif RoutingGlobal.CleanseCandidateTable(AppGlobal.AppPipe); Candidate tmpBest = CandidateTable.GetBestCandidate(false); AppGlobal.TempParent = tmpBest.GetMacID(); status = AppGlobal.SendToTempParent(AppGlobal.AppPipe, routedMsg, sendSize); if (status != 999) { _sendMsgNum++; tmpBest.UpdateNumTriesInCurrentWindow(1); #if !DBG_LOGIC Debug.Print("Updated numTriesInCurrentWindow for TempParent " + AppGlobal.TempParent + "; new value = " + tmpBest.GetNumTriesInCurrentWindow()); #endif } } } }