private void handleCommonCommand(routedMessage msg, byte fromCon) { commandReader reader = msg.getReader(); reader.ReadByte(); byte cmd = reader.ReadByte(); switch (cmd) { case 5: //parameter count byte count = reader.ReadByte(); activeNode.parameterCount = count; if (count > 0) { routedMessage msgGetMeta = new routedMessage(activeNode.address, new byte[] { 0x0a, 0x6, 0x00 }); SendRoutedMessage(msgGetMeta, SERIALCON); } break; case 7: //parameter metadata byte pIdx = reader.ReadByte(); string pName = reader.ReadString(); byte pType = reader.ReadByte(); UInt32 pArrayLen = reader.Read7BitEncodedUInt32(); try { ccParameter property = new ccParameter(pIdx, pName, pType, pArrayLen, activeNode); activeNode.properties.Add(pName, property); //bind to property editor if (property.TypeIdx != ccParameter.CC_ENUMERATION_VALUES && property.TypeIdx != ccParameter.CC_VOID_FUNCTION) { Flobbster.Windows.Forms.PropertySpec p = new Flobbster.Windows.Forms.PropertySpec(pName, property.type, "Parameters"); if (property.TypeIdx == ccParameter.CC_ENUMERATION) { p.ConverterTypeName = "Serial.CC_EnumTypeConverter"; } nodeParameterList.Properties.Add(p); propertyGrid1.Refresh(); } if (property.TypeIdx == ccParameter.CC_VOID_FUNCTION) { Button cmdButton = new Button(); cmdButton.Text = pName; cmdButton.Tag = property; cmdButton.AutoSize = true; cmdButton.Click += new EventHandler(cmdButton_Click); commandPanel.Controls.Add(cmdButton); if (activeNode.parameterCount > activeNode.properties.Count) { //get next parameter routedMessage msgGetMeta = new routedMessage(activeNode.address, new byte[] { 0x0a, 0x6, (byte)activeNode.properties.Count }); SendRoutedMessage(msgGetMeta, SERIALCON); } } else { //get value //TODO decide what to do with big arrays - limit to first 32 elements for now routedMessage msgValReq = new routedMessage(activeNode.address, new byte[] { 0x03, pIdx, 0x00, (byte)(Math.Min(pArrayLen, 32)) }); SendRoutedMessage(msgValReq, SERIALCON); } } catch (Exception pe) { MessageBox.Show("get parameter error" + pe.Message); } break; } }
private void pcHandleRoutedMessage(byte[] buff, int offset, int len, byte fromCon) { routedMessage msg = new routedMessage(buff, offset, len); //see if packet has reached its destination if (msg.Route.isForMe()) //no to addresses { //see if anything is waiting for this response //TODO clear expired subscriptions foreach (var subscription in pendingResponses) { if (!subscription.expired && subscription.matches(msg)) { subscription.handleReply(msg); if (subscription.singleShot) { pendingResponses.Remove(subscription); } return; } } //TODO make everything use subscription and remove the following switch (msg.commandByte) { case 0x01: //enumerate response { if (treeWalker != null) { //clear timeout timer1.Stop(); timer1.Enabled = false; treeWalker.enumerateResponse(msg); routedMessage resp = treeWalker.getNextRequest(); if (resp != null) { timer1.Enabled = true; timer1.Start(); SendRoutedMessage(resp, 0); } else { } bindtree(pcNode); } break; } case 0x91: //code update initialized { if (rUpload != null) { commandReader reader = msg.getReader(); reader.ReadByte(); string moduleType = reader.ReadString(); string filename; if (moduleType == "JN5148") { if (activeNode.name == "TX") { filename = mus.tx5148binpath; } else { filename = mus.rx5148binpath; } } else { if (activeNode.name == "TX") { filename = mus.txbinpath; } else { filename = mus.rxbinpath; } } rUpload.setFile(filename); // Let the user see the progress ulProg.Show(); routedMessage reply = rUpload.sendNextCmd(msg); if (reply != null) { SendRoutedMessage(reply, fromCon); } } break; } case 0x93: //code chunk received { if (rUpload != null) { routedMessage reply = rUpload.sendNextCmd(msg); if (reply != null) { SendRoutedMessage(reply, fromCon); } } break; } case 0x95: // Upload failed { commandReader reader = msg.getReader(); reader.ReadByte(); string errorMsg; switch (reader.ReadByte()) { case 1: errorMsg = "CRC Error"; break; case 2: errorMsg = "Block Length Error"; break; case 3: errorMsg = "Low Battery"; break; default: errorMsg = "Unknown Error"; break; } MessageBox.Show("Upload Failed " + errorMsg); break; } case 0xff: //debug message { commandReader reader = msg.getReader(); reader.ReadByte(); SetText(reader.ReadString()); break; } case 0x0a: //common commands { handleCommonCommand(msg, fromCon); break; } case 0x04: //get parameter value response { commandReader reader = msg.getReader(); reader.ReadByte(); byte paramIdx = reader.ReadByte(); //depending on type read value activeNode.getParamByIdx((int)paramIdx).parseValue(msg); propertyGrid1.Refresh(); if (activeNode.parameterCount > activeNode.properties.Count) { //get next parameter routedMessage msgGetMeta = new routedMessage(activeNode.address, new byte[] { 0x0a, 0x6, (byte)activeNode.properties.Count }); SendRoutedMessage(msgGetMeta, SERIALCON); } break; } case 0x02: // Set parameter response { commandReader reader = msg.getReader(); reader.ReadByte(); // Skip Command code bool response = !reader.ReadBoolean(); Debug.WriteLine(activeNode.name + ": Parameter set: " + response.ToString()); break; } case 0xfe: //loop test { if (loopTest != null) { routedMessage lt = loopTest.sendNextCmd(msg); if (lt != null) { SendRoutedMessage(lt, SERIALCON); } else { MessageBox.Show("Loop back done. Length errors " + loopTest.lengtherrors + " content errors " + loopTest.contenterrors); } } break; } } } else { //todo relay using routedMessage class /* * //relay message * byte to = buff[offset + addrToIdx + 1]; * //replace to with from * buff[offset + addrToIdx + 1] = fromCon; * //move index of next to address * addrToIdx++; * buff[offset] = (byte)((buff[offset] & 0xf0) + addrToIdx); * //pass message on to connector defined by 'to' address * pcSendRoutedMessage(buff, offset, (byte)len, to); */ } }