コード例 #1
0
        private int check_package(byte[] pdata, int startID, int len)
        {
            int  slen;
            byte fcs, sum;

            if (len < 5)
            {
                return(1);
            }
            slen = (pdata[startID + 1] + 5);
            if (slen > len)//package length error!
            {
                return(1);
            }
            fcs = pdata[startID + (slen - 1)];
            sum = ZigbeeCommon.GetXor(pdata, startID + 1, (slen - 2));
            if (fcs == sum)
            {
                return(0);
            }
            else
            {
                return(1);
            }
        }
コード例 #2
0
        private void NVMessageProc(byte[] data, int len)
        {
            switch (NVOperationType)
            {
            case ZigbeeCommon.NVReadNetWorkInfo:
                if (data[4] == 0x00)    //check success
                {
                    byte   channel = data[6];
                    UInt16 addr    = ZigbeeCommon.BtoU16(data, 7);
                    Array.Copy(data, 9, ExAddr, 0, 8);
                    UInt16 panID = ZigbeeCommon.BtoU16(data, 17);
                    Channel = channel;
                    PanID   = panID;
                    ZigbeeApi.myZtool.UptateNetWorkInfoTextBox(channel, panID);
                }
                break;

            case ZigbeeCommon.NVReadPreConfigKey:
                if (data[4] == 0x00)    //check success
                {
                    byte slen = data[5];
                    byte seq  = data[6];
                    Array.Copy(data, 7, NwkKey, 0, 16);
                    ZigbeeApi.myZtool.UpdatePreConfigKey(NwkKey);
                }
                break;
            }
        }
コード例 #3
0
 private void ShowNewDeviceInfo()
 {
     if (ShowDeviceInfoRuntime >= ZigbeeCommon.Tick)
     {
         ShowDeviceInfoRuntime -= ZigbeeCommon.Tick;
     }
     else
     {
         ShowDeviceInfoRuntime = 0;
     }
     if (ShowDeviceInfoRuntime == 0)
     {
         ShowDeviceInfoRuntime = ZigbeeCommon.ShowDeviceInfoUpdateTime;
         ZigbeeDevice.Device_t dev = ZigbeeApi.Device.GetNewDevieInfo();
         if (dev != null)
         {
             String str = "New Device:" + dev.addr.ToString("X4") + "\r\n";
             foreach (ZigbeeDevice.Point_t p in dev.PointList)
             {
                 if (p.inCLusterID != null && p.outClusterID != null)
                 {
                     str += "Point:" + p.point.ToString("X2") + "\r\n";
                     str += "device ID:" + p.devID.ToString("X4") + " ver:" + p.ver.ToString("X2") + "\r\n";
                     str += "Input  Cluster ID:" + ZigbeeCommon.U16toHexStr(p.inCLusterID, 0, p.inCLusterID.Length) + "\r\n";
                     str += "Output Cluster ID:" + ZigbeeCommon.U16toHexStr(p.outClusterID, 0, p.outClusterID.Length) + "\r\n";
                 }
             }
             str += "\r\n";
             myZtool.UpdateRecieveTextBox(str);
         }
     }
 }
コード例 #4
0
 public void ReqReadNV(UInt16 item, byte offset)
 {
     byte[] buf = new byte[3];
     ZigbeeCommon.U16toB(buf, 0, item);
     buf[2] = offset;
     Output.ReqSendCommand(0x21, 0x08, 0x00, buf);
 }
コード例 #5
0
        private void SendTestCommand()
        {
            if (textBox4.Text == "")
            {
                textBox4.Text = "FFFF";
            }
            if (textBox5.Text == "")
            {
                textBox5.Text = "FF";
            }
            if (textBox13.Text == "")
            {
                textBox13.Text = "0006";
            }
            if (textBox6.Text == "")
            {
                textBox6.Text = "01 01 00 00 00 00";
            }
            UInt16 addr  = (UInt16)Convert.ToInt16(textBox4.Text, 16);
            byte   point = Convert.ToByte(textBox5.Text, 16);
            UInt16 cID   = (UInt16)Convert.ToInt16(textBox13.Text, 16);

            byte[] data = ZigbeeCommon.HexStrtoB(textBox6.Text);
            ZigbeeApi.Instance.ReqSendCommand(addr, point, cID, data);
        }
コード例 #6
0
 public void ReqActivePoints(UInt16 addr)
 {
     byte[] buf = new byte[4];
     ZigbeeCommon.U16toB(buf, 0, addr);
     ZigbeeCommon.U16toB(buf, 2, addr);
     ZigbeeApi.Output.ReqSendCommand(0x25, 0x05, 0, buf);
 }
コード例 #7
0
        private void MessageInput(byte[] pkg)
        {
            byte cmd0 = pkg[2];
            byte cmd1 = pkg[3];

            switch (cmd0 & 0x0F)
            {
            case 0x01:    //system layer
                Output.Remove(0x21, cmd1, 0x00);
                if ((cmd1 == 0x08) || (cmd1 == 0x09))
                {
                    Device.InputMessage(pkg, pkg.Length);
                }
                break;

            case 0x05:    //zdo layer
                Output.Remove(0x25, (byte)(cmd1 & 0x7F), 0x00);
                if ((cmd0 & 0xF0) == 0x40)
                {
                    Device.InputMessage(pkg, pkg.Length);
                }
                break;

            case 0x09:    //app layer
                if ((cmd0 & 0xF0) == 0x40)
                {
                    UInt16 addr = ZigbeeCommon.BtoU16(pkg, 4);
                    Output.Remove(addr);
                    AppProc(pkg, pkg.Length);
                }
                break;
            }
        }
コード例 #8
0
        public void ReqSendCommand(byte cmd0, byte cmd1, byte seq, byte stry, byte[] data)
        {
            Send_t snd = new Send_t();

            snd.cmd0    = cmd0;
            snd.cmd1    = cmd1;
            snd.seq     = seq;
            snd.runtime = ZigbeeApi.myZtool.GetSendTimeout();
            snd.stry    = stry;
            snd.data    = new byte[data.Length];
            Array.Copy(data, snd.data, data.Length);
            if (SendList != null)
            {
                if (snd.cmd0 == 0x29)
                {
                    UInt16 addr  = ZigbeeCommon.BtoU16(data, 0);
                    byte   point = data[2];
                    UInt16 cID   = ZigbeeCommon.BtoU16(data, 5);
                    byte   dlen  = data[7];
                    byte[] buf   = new byte[dlen];
                    Array.Copy(data, 8, buf, 0, dlen);
                    ZigbeeApi.myZtool.UpdateTestCommandInfo(ZigbeeApi.Device.GetDeviceCount(), addr, point, cID, buf);

                    ZigbeeApi.Device.SendCount(addr);
                }
                SendList.Add(snd);
            }
        }
コード例 #9
0
 public void UpdateTestCommandInfo(int devNum, UInt16 addr, byte point, UInt16 cID, byte[] data)
 {
     textBox1.Text  = devNum.ToString();
     textBox4.Text  = addr.ToString("X4");
     textBox5.Text  = point.ToString("X2");
     textBox13.Text = cID.ToString("X4");
     textBox6.Text  = ZigbeeCommon.BtoHexStr(data, 0, data.Length);
 }
コード例 #10
0
 static void ReqDescribe(UInt16 addr, byte point)
 {
     byte[] buf = new byte[5];
     ZigbeeCommon.U16toB(buf, 0, addr);
     ZigbeeCommon.U16toB(buf, 2, addr);
     buf[4] = point;
     ZigbeeApi.Output.ReqSendCommand(0x25, 0x04, 0, buf);
 }
コード例 #11
0
        private void DeviceLeaveProc(byte[] data, int len)
        {
            UInt16 srcAddr;
            int    Index = 4;

            srcAddr = ZigbeeCommon.BtoU16(data, Index);
            Remove(srcAddr);
        }
コード例 #12
0
 public void ReqWriteNetWorkInfo(byte channel, UInt16 panID)
 {
     byte[] buf = new byte[13];
     buf[0] = channel;
     buf[1] = 0x00; //coordinator address
     buf[2] = 0x00; //coordinator address
     byte[] exaddr = Device.GetExAddr();
     Array.Copy(exaddr, 0, buf, 3, 8);
     ZigbeeCommon.U16toB(buf, 11, panID);
     ReqWriteNV(0x0021, 22, buf);
 }
コード例 #13
0
        private void NewJoinProc(byte[] data, int len)
        {
            UInt16 srcAddr;
            UInt16 devAddr;
            int    Index = 4;

            srcAddr = ZigbeeCommon.BtoU16(data, Index); Index += 2;
            devAddr = ZigbeeCommon.BtoU16(data, Index); Index += 2;
            byte[] exAddr = new byte[8];
            Array.Copy(data, Index, exAddr, 0, 8);
            AddDevice(devAddr, exAddr);
        }
コード例 #14
0
        public void MessageInput(byte[] data, int len)
        {
            int remain, temp;

            temp = fifo_counter + len;
            if (temp > RECEIVE_BUF_SIZE)
            {
                remain        = temp - RECEIVE_BUF_SIZE;
                fifo_counter -= remain;
                ZigbeeCommon.Memcpy(data_buf, remain, data_buf, 0, fifo_counter);
            }
            Array.Copy(data, 0, data_buf, fifo_counter, len);
            fifo_counter += len;
        }
コード例 #15
0
        public void ReqWriteNV(UInt16 item, byte offset, byte[] data)
        {
            byte slen = 4;

            if (data != null)
            {
                slen += (byte)data.Length;
            }
            byte[] buf = new byte[slen];
            ZigbeeCommon.U16toB(buf, 0, item);
            buf[2] = offset;
            buf[3] = (byte)data.Length;
            if (data != null)
            {
                Array.Copy(data, 0, buf, 4, data.Length);
            }
            Output.ReqSendCommand(0x21, 0x09, 0x00, buf);
        }
コード例 #16
0
 private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     if (sp.IsOpen == false)
     {
         MessageBox.Show("串口未打开!", "提示");
     }
     else
     {
         if (textBox14.Text != "")
         {
             byte[] key = ZigbeeCommon.HexStrtoB(textBox14.Text);
             if (key != null)
             {
                 ZigbeeApi.Instance.ReqWritePreConfigKey(key);
             }
         }
     }
 }
コード例 #17
0
        private int Send(byte cmd0, byte cmd1, byte[] data)
        {
            int slen = data.Length + 5;

            byte[] buf = new byte[slen];
            buf[0] = 0xFE;
            buf[1] = (byte)data.Length;
            buf[2] = cmd0;
            buf[3] = cmd1;
            Array.Copy(data, 0, buf, 4, data.Length);
            buf[slen - 1] = ZigbeeCommon.GetXor(buf, 1, (slen - 1));
            String str = "snd:" + ZigbeeCommon.BtoHexStr(buf, 0, buf.Length);

            ZigbeeApi.myZtool.UpdateSendTextBox(str);
            if (ZigbeeApi.Uart.IsOpen)
            {
                ZigbeeApi.Uart.Write(buf, 0, buf.Length);
            }
            return(0);
        }
コード例 #18
0
        public byte[] read_package()
        {
            int remain;
            int index = 0;
            int slen;

            while (index < fifo_counter)
            {
                remain = (fifo_counter - index);
                index  = find_sof(data_buf, index, remain);
                if (index >= 0)
                {
                    if (check_package(data_buf, index, remain) == 0)
                    {
                        slen = data_buf[index + 1] + 5;//total package length
                        byte[] buf = new byte[slen];
                        Array.Copy(data_buf, index, buf, 0, slen);
                        fifo_counter -= slen;
                        int clen = fifo_counter - index;
                        if ((clen >= RECEIVE_BUF_SIZE) || (clen < 0))
                        {
                            fifo_counter = 0;
                        }
                        else
                        {
                            ZigbeeCommon.Memcpy(data_buf, (index + slen), data_buf, index, clen);
                        }
                        return(buf);
                    }
                    else
                    {
                        index++;//move to next byte
                    }
                }
                else
                {
                    break;
                }
            }
            return(null);
        }
コード例 #19
0
 public void Poll()
 {
     try
     {
         if (SendList.Count > 0)
         {
             Send_t pkg = (Send_t)SendList[0];
             if (pkg.runtime >= ZigbeeCommon.Tick)
             {
                 pkg.runtime = pkg.runtime - ZigbeeCommon.Tick;
             }
             else
             {
                 pkg.runtime = 0;
             }
             if (pkg.runtime == 0)
             {
                 pkg.runtime = ZigbeeApi.myZtool.GetSendTimeout();
                 if (pkg.stry > 0)
                 {
                     Send(pkg.cmd0, pkg.cmd1, pkg.data);
                     pkg.stry--;
                     return;
                 }
                 else
                 {
                     if (pkg.cmd0 == 0x29)
                     {
                         UInt16 addr = ZigbeeCommon.BtoU16(pkg.data, 0);
                         ZigbeeApi.Device.LossCount(addr);
                     }
                     SendList.Remove(pkg);
                 }
             }
         }
     }
     catch (System.InvalidOperationException ex)
     {
         return;
     }
 }
コード例 #20
0
        public void ReqSendCommand(UInt16 addr, byte point, UInt16 cID, byte[] data)
        {
            int slen = 8;

            if (data != null)
            {
                slen += data.Length;
            }
            byte[] buf   = new byte[slen];
            int    index = 0;
            byte   seq   = ZigbeeCommon.GetSeq();

            ZigbeeCommon.U16toB(buf, 0, addr); index += 2;
            buf[index++] = point;                             //2
            buf[index++] = seq;                               //3
            buf[index++] = 0x00;                              //disable response 4
            ZigbeeCommon.U16toB(buf, index, cID); index += 2; //5
            buf[index++] = (byte)data.Length;                 //
            Array.Copy(data, 0, buf, index, data.Length);
            Output.ReqSendCommand(0x29, 0x06, seq, buf);
        }
コード例 #21
0
 public void Remove(UInt16 addr)
 {
     try
     {
         foreach (Send_t pkg in SendList)
         {
             if (pkg.cmd0 == 0x29)
             {
                 UInt16 daddr = ZigbeeCommon.BtoU16(pkg.data, 0);
                 if (daddr == addr)
                 {
                     SendList.Remove(pkg);
                 }
             }
         }
     }
     catch (System.InvalidOperationException ex)
     {
         return;
     }
 }
コード例 #22
0
        public void MessageInput(byte[] data, int len)
        {
            UInt16 srcAddr;
            int    startID = 0;
            int    index   = 4;//start of data

            srcAddr = ZigbeeCommon.BtoU16(data, index); index += 2;
            if (data[index++] == 0x00)//if succees
            {
                byte totalNum   = data[index++];
                byte startDevID = data[index++];
                byte curNum     = data[index++];
                for (int i = 0; i < curNum; i++)
                {
                    index += 16;
                    UInt16 DevAddr = ZigbeeCommon.BtoU16(data, index);
                    if (ZigbeeApi.Device.CheckExist(DevAddr) == false)
                    {
                        ZigbeeApi.Device.AddDevice(DevAddr, null);
                    }
                    index += 2;
                    byte devType = (byte)((data[index] & 0x03) + 1);
                    index += 3;
                    byte lqi = data[index++];
                    //if ((devType == 0x02))
                    //     ZigbeeApi.Instance.ReqAssoc(DevAddr, 0);
                    startID = AddDeviceAssoc(srcAddr, devType, DevAddr, lqi);
                }
                startID = startDevID + curNum;
                if (startID < totalNum)
                {
                    ZigbeeApi.Instance.ReqAssoc(srcAddr, (byte)startID);
                }
                else
                {
                    SetFinishedFlag(srcAddr, true);
                }
            }
        }
コード例 #23
0
        private void ReadDescribeResponse(byte[] data, int len)
        {
            int    Index   = 4;
            UInt16 srcAddr = ZigbeeCommon.BtoU16(data, Index); Index += 2;
            byte   state   = data[Index++];

            if (state == 0x00)//succeed
            {
                UInt16   devAddr   = ZigbeeCommon.BtoU16(data, Index); Index += 2;
                byte     dlen      = data[Index++];
                byte     point     = data[Index++];
                UInt16   profileID = ZigbeeCommon.BtoU16(data, Index); Index += 2;
                UInt16   deviceID  = ZigbeeCommon.BtoU16(data, Index); Index += 2;
                byte     ver       = data[Index++];
                byte     inNum     = data[Index++];
                UInt16[] IncID     = ConvertcID(data, Index, inNum); Index += (inNum * 2);
                byte     outNum    = data[Index++];
                UInt16[] OutcID    = ConvertcID(data, Index, outNum);
                AddDescribe(srcAddr, point, deviceID, ver, IncID, OutcID);
                NewDeviceList.Add(srcAddr);
            }
        }
コード例 #24
0
        public void ReqSendOnOff(UInt16 addr, byte point, byte val)
        {
            byte[] buf   = new byte[14];
            int    index = 0;
            byte   seq   = ZigbeeCommon.GetSeq();

            ZigbeeCommon.U16toB(buf, 0, addr); index += 2;
            buf[index++] = point;                                //2
            buf[index++] = seq;                                  //3
            buf[index++] = 0x00;                                 //disable response 4
            ZigbeeCommon.U16toB(buf, index, 0x0006); index += 2; //5
            buf[index++] = 6;                                    //

            buf[index++] = val;
            buf[index++] = 0x01;
            buf[index++] = 0x00;

            buf[index++] = 0x00;
            buf[index++] = 0x00;

            buf[index++] = 0x00;
            Output.ReqSendCommand(0x29, 0x06, seq, buf);
        }
コード例 #25
0
        private void AppProc(byte[] pkg, int len)
        {
            int  Index = 4;
            byte cmd0;
            byte cmd1;
            byte dlen;

            cmd0 = pkg[2];
            cmd1 = pkg[3];
            dlen = pkg[4];
            UInt16 addr = ZigbeeCommon.BtoU16(pkg, Index);

            if (Device.CheckExist(addr) == true)
            {
                string str = "app rec:";
                str += ZigbeeCommon.BtoHexStr(pkg, 0, pkg.Length);
                myZtool.UpdateRecieveTextBox(str);
                Device.ReceiveCount(addr);
            }
            else
            {
                Device.AddDevice(addr, null);
            }
        }
コード例 #26
0
        private void ReadPointResponse(byte[] data, int len)
        {
            UInt16 srcAddr;
            UInt16 devAddr;
            int    Index;
            byte   pNum;
            byte   state;

            Index   = 1 + 1 + 1 + 1;
            srcAddr = ZigbeeCommon.BtoU16(data, Index); Index += 2;
            state   = data[Index++];
            if (state == 0x00)//succeed
            {
                devAddr = ZigbeeCommon.BtoU16(data, Index); Index += 2;
                pNum    = data[Index]; Index += 1;
                byte[] points = new byte[pNum];
                Array.Copy(data, Index, points, 0, pNum);
                for (int i = 0; i < points.Length; i++)
                {
                    ReqDescribe(srcAddr, points[i]);
                }
                AddPoints(srcAddr, points);
            }
        }
コード例 #27
0
 public void UpdatePreConfigKey(byte[] key)
 {
     textBox14.Text = ZigbeeCommon.BtoHexStr(key, 0, key.Length);
 }