/// <summary> /// Encode beacon into frame /// </summary> /// <param name="frame">target frame</param> /// <returns>True on success</returns> public bool WriteToFrame(Frame frame) { int len = Length(); if (frame == null || frame.LengthDataAvail < len || frame.LengthDataUsed > 0) return false; if (payload != null) len -= payload.LengthDataUsed; frame.AllocBack(len); int offset = 0; // 7.2.2.1.2 Superframe Specification field int i; i = (beaconOrder & 0xf) | ((superframeOrder & 0xf) << 4) | ((finalCapSlot & 0xf) << 8) | ((batteryLifeExtension & 1) << 12) | ((panCoordinator & 1) << 14) | ((associationPermit & 1) << 15); frame.Write(offset, (UInt16)i); offset += 2; // 7.2.2.1.3 GTS Specification field if (gtsLength > 7) return false; // assert i = (gtsLength & 7) | ((gtsPermit & 1) << 7); frame.Write(offset, (Byte)i); offset += 1; // 7.2.2.1.4 GTS Directions field if (gtsLength > 0) { i = (gtsDirectionsMask & 0x7F); // 7 bits frame.Write(offset, (Byte)i); offset += 1; } // 7.2.2.1.5 GTS List field for (int k = 0; k < gtsLength; k++) { frame.Write(offset, gtsDescriptor[k].deviceShortAddr); offset += 2; i = (gtsDescriptor[k].gtsStartingSlot & 0xF) | ((gtsDescriptor[k].gtsLength & 0xF) << 4); frame.Write(offset, (Byte)i); offset++; } // 7.2.2.1.6 Pending Address Specification field i = (pendingShort & 7) | ((pendingExt & 7) << 4); frame.Write(offset, (Byte)i); offset++; // 7.2.2.1.7 Address List field for (int k = 0; k < pendingShort; k++) { frame.Write(offset, shortAddrPending[k]); offset += 2; } for (int k = 0; k < pendingExt; k++) { frame.Write(offset, extAddrPending[k]); offset += 8; } // 7.2.2.1.8 Beacon Payload field if (payload != null) frame.WriteToBack(payload); return true; }