Exemple #1
0
 private void ProcessUdpPacket(KickDeviceInfo device, UdpPacket request)
 {
     switch (request.Command)
       {
     case 0x01:
       var response = new UdpPacket(Address.All, 0x83, null);
       RegisterUdpPacketToSend(response);
       break;
     case 0x06:
       device.EV = request.Data[0];
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x81:
       var rgbBytes = request.Data.Take(3).ToArray();
       var nameBytes = request.Data.Skip(3).TakeWhile(b => b > 0x0).ToArray();
       device.Name = Encoding.ASCII.GetString(nameBytes);
       device.Color = new RGBColor(rgbBytes[0], rgbBytes[1], rgbBytes[2]);
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x83:
       device.EmitterTemperature = Convert.ToSByte(request.Data[0]);
       device.Power = request.Data[1];
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x87:
       device.FirmwareVersion = new Version(request.Data[0], request.Data[1]);
       device.HardwareVersion = new Version(request.Data[2], request.Data[3]);
       device.SerialNumber = request.Data.Skip(4).Take(4).ToHexString()
     + "-" + request.Data.Skip(8).Take(4).ToHexString()
     + "-" + request.Data.Skip(12).Take(4).ToHexString()
     + "-" + request.Data.Skip(16).Take(4).ToHexString();
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x89:
       device.EmitterTemperature = Convert.ToSByte(request.Data[0]);
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x90:
       device.Power = request.Data[0];
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     case 0x91:
       device.EV = request.Data[0];
       device.Color = new RGBColor(request.Data[9], request.Data[10], request.Data[11]);
       device.FirmwareVersion = new Version(request.Data[12], request.Data[13]);
       device.HardwareVersion = new Version(request.Data[14], request.Data[15]);
       device.Name = Encoding.ASCII.GetString(request.Data.Skip(16).TakeWhile(b => b > 0x0).ToArray());
       if (OnKickDeviceChanged != null) OnKickDeviceChanged(device);
       break;
     default:
       break;
       }
 }
Exemple #2
0
        private void DataReceiveRunner()
        {
            while (m_Connected && m_UdpClient != null)
              {
            if (!m_ReaderWriterLock.TryEnterReadLock(100))
              continue;

            try
            {
              //Format: <marker><address><length><command><data>
              IPEndPoint endpoint = null;
              byte[] data;

              try
              {
            data = m_UdpClient.Receive(ref endpoint);
              }
              catch
              {
            continue;
              }

              if (data == null || data.Length == 0)
            continue;

              var marker = Encoding.ASCII.GetString(data, 0, 2);
              var kickAddress = new byte[] { data[2], data[3], data[4] };

              if (!marker.StartsWith("R"))
            continue;

              // Ignore all non-slave communication
              if (marker != "R$")
            continue;

              var newDevice = false;
                    KickDeviceInfo device;
              lock (m_Devices)
              {
            if (!m_Devices.Any(d => d.Address.SequenceEqual(kickAddress)))
            {
              newDevice = true;
                            m_Devices.Add(new KickDeviceInfo { Address = kickAddress });
            }

            device = m_Devices.Single(d => d.Address.SequenceEqual(kickAddress));
              }

                    if (newDevice && OnNewKickDevice != null)
                        OnNewKickDevice(device);

              var request = new UdpPacket(data.Skip(1).Take(3).ToArray(), data.Skip(7).Take(1).ElementAt(0), data.Length > 8 ? data.Skip(8).ToArray() : null);
              ProcessUdpPacket(device, request);
              DebugWriteLine("IN>  " + data.Take(7).ToHexString() + "   <" + data.Skip(7).Take(1).ToHexString() + "> " + (data.Length > 8 ? data.Skip(8).ToHexString() : ""));
            }
            catch (Exception err)
            {
              DebugWriteLine("ERR> DataReceive: " + err.Message);
            }
            finally
            {
              m_ReaderWriterLock.ExitReadLock();
            }
              }
        }
Exemple #3
0
        private void DataBroadcast(UdpPacket packet)
        {
            try
              {
            if (m_Connected)
            {
              if (packet.Data == null)
            packet.Data = new byte[0];

              var group = packet.Address == Address.All ? byte.MaxValue : byte.MinValue;
              // <marker 16b><address 16b><length 16b><command 8b><data ?b>
              var headerBytes = new byte[] { (byte)'R', (byte)'L', group, packet.Address[0], packet.Address[1], packet.Address[2], 0x00, (byte)(packet.Data.Length + 1), packet.Command };
              var allBytes = new List<byte>(headerBytes);

              if (packet.Data.Length > 0)
            allBytes.AddRange(packet.Data);

              var data = allBytes.ToArray();
              DebugWriteLine("OUT> " + data.Take(8).ToHexString() + " <" + data.Skip(8).Take(1).ToHexString() + "> " +   (data.Length > 9 ? data.Skip(9).ToHexString() : ""));

              m_UdpClient.Send(data, data.Length, new IPEndPoint(m_Broadcast, 8080));

              if (packet.Command == 0x88)
            m_DisconnectCommandSentEvent.Set();
            }
              }
              catch (Exception err)
              {
            DebugWriteLine("ERR> DataBroadcast: " + err.Message);
              }
        }
Exemple #4
0
        private void DataBroadcastRunner()
        {
            while (m_UdpClient != null && m_Connected)
              {
            if (!m_ReaderWriterLock.TryEnterReadLock(100))
              continue;

            try
            {
              if (m_SendPacket == null)
              {
            Thread.Sleep(TransmitInterval);
            continue;
              }

              UdpPacket packet = null;

              lock (m_SetSendPacketLock)
              {
            if (m_SendPacket != null)
            {
              packet = m_SendPacket;
              m_SendPacket = null;
            }
              }

              if (packet != null)
              {
              Thread.Sleep(TransmitInterval);
              DataBroadcast(packet);
              }
            }
            catch (Exception e)
            {
              DebugWriteLine("ERR> TransmitData: " + e.Message);
            }
            finally
            {
              m_ReaderWriterLock.ExitReadLock();
            }
              }
        }
Exemple #5
0
        public void RegisterUdpPacketToSend(UdpPacket packet)
        {
            if (!m_Transmitting)
            return;

              lock (m_SetSendPacketLock)
              {
            m_SendPacket = packet;
              }
        }