public void DiffAt(Kowhai.kowhai_symbol_t[] symbolPath) { treeView1.BeginUpdate(); foreach (TreeNode childNode in treeView1.Nodes) _diffAt(symbolPath, new bool[symbolPath.Length], 0, childNode); treeView1.EndUpdate(); }
public static int Deserialize(string buffer, out Kowhai.kowhai_node_t[] descriptor, out byte[] data) { int bufferSize = 0x100; int result; do { byte[] scratch = new byte[bufferSize]; descriptor = new Kowhai.kowhai_node_t[bufferSize]; data = new byte[bufferSize]; int descriptorSize = bufferSize; int dataSize = bufferSize; GCHandle h = GCHandle.Alloc(descriptor, GCHandleType.Pinned); result = kowhai_deserialize(buffer, scratch, bufferSize, h.AddrOfPinnedObject(), ref descriptorSize, data, ref dataSize); h.Free(); if (result == Kowhai.STATUS_OK) { Array.Resize(ref descriptor, descriptorSize); Array.Resize(ref data, dataSize); } bufferSize *= 2; } while (result == Kowhai.STATUS_SCRATCH_TOO_SMALL || result == Kowhai.STATUS_TARGET_BUFFER_TOO_SMALL); return result; }
public static int Serialize(Kowhai.kowhai_node_t[] descriptor, byte[] data, out string target, Object getNameParam, GetSymbolName getName) { kowhai_get_symbol_name_t _getName = delegate(IntPtr param, UInt16 value) { return getName(getNameParam, value); }; byte[] targetBuf; int targetBufferSize = 0x1000; int result; Kowhai.kowhai_tree_t tree; GCHandle h = GCHandle.Alloc(descriptor, GCHandleType.Pinned); tree.desc = h.AddrOfPinnedObject(); GCHandle h2 = GCHandle.Alloc(data, GCHandleType.Pinned); tree.data = h2.AddrOfPinnedObject(); do { targetBufferSize *= 2; targetBuf = new byte[targetBufferSize]; result = kowhai_serialize(tree, targetBuf, ref targetBufferSize, IntPtr.Zero, _getName); } while (result == Kowhai.STATUS_TARGET_BUFFER_TOO_SMALL); h2.Free(); h.Free(); ASCIIEncoding enc = new ASCIIEncoding(); target = enc.GetString(targetBuf, 0, targetBufferSize); return result; }
public static int Create(byte[] protoPacket, int packetSize, ref kowhai_protocol_t protocol, Kowhai.kowhai_symbol_t[] symbols, out int bytesRequired) { protocol.payload.spec.data.symbols.count = (uint8_t)symbols.Length; GCHandle h = GCHandle.Alloc(symbols, GCHandleType.Pinned); protocol.payload.spec.data.symbols.array_ = h.AddrOfPinnedObject(); int result = Create(protoPacket, packetSize, ref protocol, out bytesRequired); h.Free(); return result; }
public static int Create(byte[] protoPacket, int packetSize, ref kowhai_protocol_t protocol, Kowhai.kowhai_symbol_t[] symbols, byte[] buffer, uint16_t offset, out int bytesRequired) { protocol.payload.spec.data.memory.size = (uint16_t)buffer.Length; protocol.payload.spec.data.memory.offset = offset; GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned); protocol.payload.buffer = h.AddrOfPinnedObject(); int result = Create(protoPacket, packetSize, ref protocol, symbols, out bytesRequired); h.Free(); return result; }
public static int Diff(Kowhai.Tree left, Kowhai.Tree right, Object onDiffParam, OnDiff onDiff) { int result; Kowhai.kowhai_tree_t l, r; GCHandle h1 = GCHandle.Alloc(left.Descriptor, GCHandleType.Pinned); l.desc = h1.AddrOfPinnedObject(); GCHandle h2 = GCHandle.Alloc(left.Data, GCHandleType.Pinned); l.data = h2.AddrOfPinnedObject(); GCHandle h3 = GCHandle.Alloc(right.Descriptor, GCHandleType.Pinned); r.desc = h3.AddrOfPinnedObject(); GCHandle h4 = GCHandle.Alloc(right.Data, GCHandleType.Pinned); r.data = h4.AddrOfPinnedObject(); kowhai_on_diff_t _onDiff = delegate(IntPtr param_, ref Kowhai.kowhai_node_t left_node, IntPtr left_data, ref Kowhai.kowhai_node_t right_node, IntPtr right_data, int index, int depth) { Kowhai.kowhai_symbol_t[] symbolPathLeft = null; int offsetLeft = 0; if (left_data.ToInt32() != 0) { result = _CreateSymbolPath(ref l, left_data, out symbolPathLeft); if (result != Kowhai.STATUS_OK) return result; offsetLeft = (int)(left_data.ToInt64() - l.data.ToInt64()); } Kowhai.kowhai_symbol_t[] symbolPathRight = null; int offsetRight = 0; if (right_data.ToInt32() != 0) { result = _CreateSymbolPath(ref r, right_data, out symbolPathRight); if (result != Kowhai.STATUS_OK) return result; offsetRight = (int)(right_data.ToInt64() - r.data.ToInt64()); } int size = 0; if (left_data.ToInt32() != 0) { size = Kowhai.kowhai_get_node_type_size(left_node.type); if (right_data.ToInt32() != 0) System.Diagnostics.Debug.Assert(left_node.type == right_node.type); } else if (right_data.ToInt32() != 0) size = Kowhai.kowhai_get_node_type_size(right_node.type); onDiff(onDiffParam, left, symbolPathLeft, offsetLeft, right, symbolPathRight, offsetRight, size); return Kowhai.STATUS_OK; }; result = kowhai_diff(ref l, ref r, IntPtr.Zero, _onDiff); h4.Free(); h3.Free(); h2.Free(); h1.Free(); return result; }
public static int CreateSymbolPath(Kowhai.Tree tree, IntPtr targetLocation, out Kowhai.kowhai_symbol_t[] symbolPath) { int result; Kowhai.kowhai_tree_t _tree; GCHandle hTreeDesc = GCHandle.Alloc(tree.Descriptor, GCHandleType.Pinned); _tree.desc = hTreeDesc.AddrOfPinnedObject(); GCHandle hTreeData = GCHandle.Alloc(tree.Data, GCHandleType.Pinned); _tree.data = hTreeData.AddrOfPinnedObject(); result = _CreateSymbolPath(ref _tree, targetLocation, out symbolPath); hTreeData.Free(); hTreeDesc.Free(); return result; }
public static int Diff(Kowhai.Tree left, Kowhai.Tree right, Object onDiffParam, OnDiff onDiffLeft, OnDiff onDiffRight) { int result; Kowhai.kowhai_tree_t l, r; GCHandle h1 = GCHandle.Alloc(left.Descriptor, GCHandleType.Pinned); l.desc = h1.AddrOfPinnedObject(); GCHandle h2 = GCHandle.Alloc(left.Data, GCHandleType.Pinned); l.data = h2.AddrOfPinnedObject(); GCHandle h3 = GCHandle.Alloc(right.Descriptor, GCHandleType.Pinned); r.desc = h3.AddrOfPinnedObject(); GCHandle h4 = GCHandle.Alloc(right.Data, GCHandleType.Pinned); r.data = h4.AddrOfPinnedObject(); kowhai_on_diff_t _onDiff = delegate(IntPtr param_, ref Kowhai.kowhai_node_t left_node, IntPtr left_data, ref Kowhai.kowhai_node_t right_node, IntPtr right_data, int index, int depth) { Kowhai.kowhai_symbol_t[] symbolPath; if (onDiffLeft != null && left_data.ToInt32() != 0) { result = _CreateSymbolPath(ref l, left_data, out symbolPath); if (result == Kowhai.STATUS_OK) onDiffLeft(onDiffParam, left, symbolPath); } if (onDiffRight != null && right_data.ToInt32() != 0) { result = _CreateSymbolPath(ref r, right_data, out symbolPath); if (result == Kowhai.STATUS_OK) onDiffRight(onDiffParam, right, symbolPath); } return Kowhai.STATUS_OK; }; result = kowhai_diff(ref l, ref r, IntPtr.Zero, _onDiff); h4.Free(); h3.Free(); h2.Free(); h1.Free(); return result; }
public static extern int kowhai_serialize(Kowhai.kowhai_tree_t tree, byte[] target_buffer, ref int target_size, IntPtr get_name_param, kowhai_get_symbol_name_t get_name);
public void UpdateDescriptor(Kowhai.kowhai_node_t[] descriptor, string[] symbols, KowhaiNodeInfo info) { this.descriptor = descriptor; this.symbols = symbols; treeView1.Nodes.Clear(); int index = 0; ushort offset = 0; _UpdateDescriptor(descriptor, ref index, ref offset, null, info, descriptor[0].type == Kowhai.BRANCH_U_START); if (treeView1.Nodes.Count > 0) treeView1.Nodes[0].Expand(); return; }
public void _diffAt(Kowhai.kowhai_symbol_t[] symbolPath, bool[] symbolPathMatchesArrayIndex, int symbolPathIndex, TreeNode node, int diffOffset, int diffSize) { KowhaiNodeInfo info = (KowhaiNodeInfo)node.Tag; bool isBranch = info.KowhaiNode.type == Kowhai.BRANCH_START || info.KowhaiNode.type == Kowhai.BRANCH_U_START; bool isBranchArrayItem = info.IsArrayItem && isBranch; bool isLeafArrayParent = node.Nodes.Count > 0 && !isBranch; // color treeview node red if match is found if (info.Offset >= diffOffset && info.Offset < diffOffset + diffSize) node.BackColor = Color.Red; // poplulate symbolPathMatchesArrayIndex to ensure we are on the correct branch of the treeview if (isBranchArrayItem) symbolPathMatchesArrayIndex[symbolPathIndex - 1] = info.ArrayIndex == symbolPath[symbolPathIndex - 1].parts.array_index; else { if (info.KowhaiNode.symbol != symbolPath[symbolPathIndex].parts.name) return; symbolPathMatchesArrayIndex[symbolPathIndex] = info.ArrayIndex == symbolPath[symbolPathIndex].parts.array_index; } // color treeview node orange is match in this branch if (!symbolPathMatchesArrayIndex.Take(symbolPathIndex).Contains(false) && node.BackColor != Color.Red) node.BackColor = Color.Yellow; // increment symbol path index and check next level of treeview if (!isBranchArrayItem && !isLeafArrayParent) symbolPathIndex++; foreach (TreeNode childNode in node.Nodes) _diffAt(symbolPath, symbolPathMatchesArrayIndex, symbolPathIndex, childNode, diffOffset, diffSize); }
public KowhaiNodeInfo(Kowhai.kowhai_node_t kowhaiNode, int nodeIndex, bool isArrayItem, ushort arrayIndex, ushort offset, KowhaiNodeInfo parent) { KowhaiNode = kowhaiNode; NodeIndex = nodeIndex; IsArrayItem = isArrayItem; ArrayIndex = arrayIndex; Offset = offset; Parent = parent; }
void _UpdateDescriptor(Kowhai.kowhai_node_t[] descriptor, ref int index, ref ushort offset, TreeNode node, KowhaiNodeInfo initialNodeInfo) { while (index < descriptor.Length) { Kowhai.kowhai_node_t descNode = descriptor[index]; switch (descNode.type) { case Kowhai.BRANCH: if (node == null) node = treeView1.Nodes.Add(GetNodeName(descNode, null)); else node = node.Nodes.Add(GetNodeName(descNode, null)); KowhaiNodeInfo parentInfo = null; if (node.Parent != null) parentInfo = (KowhaiNodeInfo)node.Parent.Tag; node.Tag = new KowhaiNodeInfo(descNode, index, false, 0, offset, parentInfo); if (descNode.count > 1) { int prevIndex = index; for (ushort i = 0; i < descNode.count; i++) { if (initialNodeInfo != null && initialNodeInfo.IsArrayItem && initialNodeInfo.ArrayIndex != i) continue; index = prevIndex; TreeNode arrayNode = node.Nodes.Add("#" + i.ToString()); arrayNode.Tag = new KowhaiNodeInfo(descNode, index, true, i, offset, parentInfo); index++; _UpdateDescriptor(descriptor, ref index, ref offset, arrayNode, null); } } else { index++; _UpdateDescriptor(descriptor, ref index, ref offset, node, null); } node = node.Parent; break; case Kowhai.BRANCH_END: return; default: TreeNode leaf; if (node == null) leaf = treeView1.Nodes.Add(GetNodeName(descNode, null)); else leaf = node.Nodes.Add(GetNodeName(descNode, null)); KowhaiNodeInfo parentNodeInfo = null; if (leaf.Parent != null) parentNodeInfo = (KowhaiNodeInfo)leaf.Parent.Tag; leaf.Tag = new KowhaiNodeInfo(descNode, index, false, 0, offset, parentNodeInfo); if (descNode.count > 1 && descNode.type != Kowhai.CHAR) { for (ushort i = 0; i < descNode.count; i++) { TreeNode child = leaf.Nodes.Add("#" + i.ToString()); child.Tag = new KowhaiNodeInfo(descNode, index, true, i, offset, parentNodeInfo); offset += (ushort)Kowhai.kowhai_get_node_type_size(descNode.type); } } else offset += (ushort)(Kowhai.kowhai_get_node_type_size(descNode.type) * descNode.count); break; } index++; } }
string GetNodeTagString(Kowhai.kowhai_node_t node) { if (node.tag > 0) return string.Format("({0})", node.tag); return ""; }
public static void CopyDescriptor(Kowhai.kowhai_node_t[] target, kowhai_protocol_payload_t payload) { GCHandle h = GCHandle.Alloc(target, GCHandleType.Pinned); CopyIntPtrs(new IntPtr(h.AddrOfPinnedObject().ToInt64() + payload.spec.descriptor.offset), payload.buffer, payload.spec.descriptor.size); h.Free(); }
public static extern int kowhai_create_symbol_path2(ref Kowhai.kowhai_tree_t tree, IntPtr target_location, IntPtr target, ref int target_size);
private static int _CreateSymbolPath(ref Kowhai.kowhai_tree_t tree, IntPtr targetLocation, out Kowhai.kowhai_symbol_t[] symbolPath) { int result, symbolPathLength = 5; do { symbolPath = new Kowhai.kowhai_symbol_t[symbolPathLength]; GCHandle h = GCHandle.Alloc(symbolPath, GCHandleType.Pinned); result = kowhai_create_symbol_path2(ref tree, targetLocation, h.AddrOfPinnedObject(), ref symbolPathLength); h.Free(); if (result == Kowhai.STATUS_OK) Array.Resize<Kowhai.kowhai_symbol_t>(ref symbolPath, symbolPathLength); symbolPathLength *= 2; } while (result == Kowhai.STATUS_TARGET_BUFFER_TOO_SMALL); return result; }
public static extern int kowhai_merge(ref Kowhai.kowhai_tree_t dst, ref Kowhai.kowhai_tree_t src);
void onSettingsDiff(Object param, Kowhai.Tree left, Kowhai.kowhai_symbol_t[] symbolPathLeft, int offsetLeft, Kowhai.Tree right, Kowhai.kowhai_symbol_t[] symbolPathRight, int offsetRight, int size) { if (symbolPathRight != null) kowhaiTreeScratch.DiffAt(symbolPathRight, offsetRight, size); }
public void UpdateTree(Kowhai.kowhai_node_t[] descriptor, string[] symbols, byte[] data, KowhaiTree.KowhaiNodeInfo info) { kowhaiTree1.UpdateDescriptor(descriptor, symbols, info); kowhaiTree1.UpdateData(data, 0); tbHex.Text = BytesToHexString(data); }
void onSettingsDiffRight(Object param, Kowhai.Tree tree, Kowhai.kowhai_symbol_t[] symbolPath) { kowhaiTreeScratch.DiffAt(symbolPath); }
public static int Merge(Kowhai.Tree destination, Kowhai.Tree source) { Kowhai.kowhai_tree_t dst, src; GCHandle h1 = GCHandle.Alloc(destination.Descriptor, GCHandleType.Pinned); dst.desc = h1.AddrOfPinnedObject(); GCHandle h2 = GCHandle.Alloc(destination.Data, GCHandleType.Pinned); dst.data = h2.AddrOfPinnedObject(); GCHandle h3 = GCHandle.Alloc(source.Descriptor, GCHandleType.Pinned); src.desc = h3.AddrOfPinnedObject(); GCHandle h4 = GCHandle.Alloc(source.Data, GCHandleType.Pinned); src.data = h4.AddrOfPinnedObject(); int result = kowhai_merge(ref dst, ref src); h4.Free(); h3.Free(); h2.Free(); h1.Free(); return result; }
public void UpdateDescriptor(Kowhai.kowhai_node_t[] descriptor, string[] symbols, KowhaiNodeInfo info) { this.descriptor = descriptor; this.symbols = symbols; treeView1.Nodes.Clear(); int index = 0; ushort offset = 0; _UpdateDescriptor(descriptor, ref index, ref offset, null, info); treeView1.ExpandAll(); return; }
public void _diffAt(Kowhai.kowhai_symbol_t[] symbolPath, bool[] symbolPathMatchesArrayIndex, int symbolPathIndex, TreeNode node) { KowhaiNodeInfo info = (KowhaiNodeInfo)node.Tag; bool isBranchArrayItem = info.IsArrayItem && info.KowhaiNode.type == Kowhai.BRANCH; bool isLeafArrayParent = node.Nodes.Count > 0 && info.KowhaiNode.type != Kowhai.BRANCH; // poplulate symbolPathMatchesArrayIndex to ensure we are on the correct branch of the treeview if (isBranchArrayItem) symbolPathMatchesArrayIndex[symbolPathIndex - 1] = info.ArrayIndex == symbolPath[symbolPathIndex - 1].parts.array_index; else { if (info.KowhaiNode.symbol != symbolPath[symbolPathIndex].parts.name) return; symbolPathMatchesArrayIndex[symbolPathIndex] = info.ArrayIndex == symbolPath[symbolPathIndex].parts.array_index; } // color treeview node red is match is found if (symbolPathIndex == symbolPath.Length - 1 && !symbolPathMatchesArrayIndex.Contains(false) && !isLeafArrayParent && !isBranchArrayItem) node.BackColor = Color.Red; // increment symbol path index and check next level of treeview if (!isBranchArrayItem && !isLeafArrayParent) symbolPathIndex++; foreach (TreeNode childNode in node.Nodes) _diffAt(symbolPath, symbolPathMatchesArrayIndex, symbolPathIndex, childNode); }
string GetNodeArrayString(Kowhai.kowhai_node_t node) { if (node.count > 1) return string.Format("[{0}]", node.count); return ""; }
public static extern int kowhai_create_symbol_path(ref Kowhai.kowhai_node_t[] descriptor, ref Kowhai.kowhai_node_t node, IntPtr target, ref int target_size);
string GetNodeName(Kowhai.kowhai_node_t node, KowhaiNodeInfo info) { if (node.type == Kowhai.BRANCH) return string.Format("{0}{1}{2}", symbols[node.symbol], GetNodeArrayString(node), GetNodeTagString(node)); if (info != null && info.IsArrayItem) return string.Format("#{0}{1} = {2}", info.ArrayIndex, GetNodeTagString(node), GetDataValue(info)); else if (node.type == Kowhai.CHAR) return string.Format("{0}{1}{2}: {3} = \"{4}\"", symbols[node.symbol], GetNodeArrayString(node), GetNodeTagString(node), GetDataTypeString(node.type), GetDataValue(info)); else if (node.count > 1) return string.Format("{0}{1}{2}: {3}", symbols[node.symbol], GetNodeArrayString(node), GetNodeTagString(node), GetDataTypeString(node.type)); else return string.Format("{0}{1}: {2} = {3}", symbols[node.symbol], GetNodeTagString(node), GetDataTypeString(node.type), GetDataValue(info)); }
public static extern int kowhai_diff(ref Kowhai.kowhai_tree_t left, ref Kowhai.kowhai_tree_t right, IntPtr on_diff_param, kowhai_on_diff_t on_diff);
public static int Parse(byte[] protoPacket, int packetSize, out kowhai_protocol_t protocol, out Kowhai.kowhai_symbol_t[] symbols) { GCHandle h = GCHandle.Alloc(protoPacket, GCHandleType.Pinned); int result = kowhai_protocol_parse(h.AddrOfPinnedObject(), packetSize, out protocol); h.Free(); if (result == Kowhai.STATUS_OK && !IsDescriptorCommand(protocol.header.command)) { symbols = new Kowhai.kowhai_symbol_t[protocol.payload.spec.data.symbols.count]; h = GCHandle.Alloc(symbols, GCHandleType.Pinned); CopyIntPtrs(h.AddrOfPinnedObject(), protocol.payload.spec.data.symbols.array_, Marshal.SizeOf(typeof(Kowhai.kowhai_symbol_t)) * symbols.Length); h.Free(); } else symbols = null; return result; }