1
+ #include " DRFZigbee.h"
2
+
3
+ void DRFZigbee::sendData (uint8_t cmd, const std::initializer_list<uint8_t > args)
4
+ {
5
+ byteArray sendArray;
6
+ sendArray += cmd;
7
+ sendArray += args.size ();
8
+ sendArray += byteArray (args.begin (),args.size ());
9
+
10
+
11
+ uint8_t sum = 0 ;
12
+ for (int i = 0 ; i < sendArray.size (); i++) sum += sendArray.at (i);
13
+ sendArray += sum;
14
+ #ifdef ZIGBEE_DEBUG
15
+ Serial.print (" [SEND]" );
16
+ for (int i = 0 ; i < sendArray.size (); i++)
17
+ {
18
+ Serial.printf (" %02X " , sendArray.at (i));
19
+ }
20
+ Serial.println (" " );
21
+ #endif
22
+ _uartp->write (sendArray.dataptr (),sendArray.size ());
23
+ }
24
+
25
+ int DRFZigbee::sendCMDAndWaitRevice (uint8_t cmd, const std::initializer_list<uint8_t > args, byteArray *reviceArray, size_t timeout)
26
+ {
27
+ byteArray sendArray (args.begin (),args.size ());
28
+ return sendCMDAndWaitRevice (cmd,sendArray,reviceArray,timeout);
29
+ }
30
+
31
+ int DRFZigbee::sendCMDAndWaitRevice (uint8_t cmd, byteArray &array, byteArray *reviceArray, size_t timeout)
32
+ {
33
+ byteArray sendArray;
34
+ sendArray += cmd;
35
+ sendArray += array.size ();
36
+ sendArray += array;
37
+
38
+ uint8_t sum = 0x00 ;
39
+ for (int i = 0 ; i < sendArray.size (); i++) sum += sendArray.at (i);
40
+ sendArray += sum;
41
+
42
+ #ifdef ZIGBEE_DEBUG
43
+ Serial.print (" [SEND]" );sendArray.printfArray <HardwareSerial>(&Serial);
44
+ #endif
45
+ _uartp->flush ();
46
+ _uartp->write (sendArray.dataptr (),sendArray.size ());
47
+
48
+ uint8_t reviceBuff[256 ];
49
+
50
+ _uartp->setTimeout (timeout);
51
+
52
+ if (!_uartp->find (0xfa )){ return lastErrorcode = kTimeoutError ; }
53
+
54
+ size_t length = _uartp->available ();
55
+ _uartp->readBytes (reviceBuff,length);
56
+
57
+ #ifdef ZIGBEE_DEBUG
58
+ Serial.print (" [REVC]" );byteArray (reviceBuff,length).printfArray <HardwareSerial>(&Serial);
59
+ #endif
60
+
61
+ if ( reviceBuff[0 ] != ( length - 3 )){ return lastErrorcode = kLengthError ; }
62
+
63
+ uint8_t checkSum = 0xfa ;
64
+ for (int i = 0 ; i < length - 1 ; i++) checkSum += reviceBuff[i];
65
+
66
+ if ( checkSum != reviceBuff[length - 1 ]){ return lastErrorcode = kCheckSumError ; }
67
+
68
+ if ( reviceArray != nullptr ) *reviceArray = byteArray (&reviceBuff[1 ],length - 2 );
69
+
70
+ return kReviceOK ;
71
+ }
72
+
73
+ int DRFZigbee::sendDataP2P (uint8_t mode,uint16_t addr,uint8_t *dataptr,size_t length)
74
+ {
75
+ byteArray sendArray (dataptr,length);
76
+ return sendDataP2P (mode,addr,sendArray);
77
+ }
78
+
79
+ int DRFZigbee::sendDataP2P (uint8_t mode,uint16_t addr,byteArray &array)
80
+ {
81
+ byteArray sendArray;
82
+ sendArray += mode;
83
+ sendArray += array.size ();
84
+ sendArray += addr >> 8 ;
85
+ sendArray += addr & 0x00ff ;
86
+ sendArray += array;
87
+
88
+ #ifdef ZIGBEE_DEBUG
89
+ Serial.print (" [SEND]" );sendArray.printfArray <HardwareSerial>(&Serial);
90
+ #endif
91
+ _uartp->flush ();
92
+ _uartp->write (sendArray.dataptr (),sendArray.size ());
93
+
94
+ return 0 ;
95
+ }
96
+ int DRFZigbee::sendDataP2P (uint8_t mode,uint16_t addr,const std::initializer_list<uint8_t > args)
97
+ {
98
+ byteArray sendArray (args.begin (),args.size ());
99
+ sendDataP2P (mode,addr,sendArray);
100
+ return 0 ;
101
+ }
102
+
103
+ int DRFZigbee::getNetworksTopology ()
104
+ {
105
+ uint8_t senddata[] = {0xfc ,0x04 ,0x0a ,0x22 ,0x33 ,0x44 ,0xa3 };
106
+ byteArray sendArray (senddata,7 );
107
+ #ifdef ZIGBEE_DEBUG
108
+ Serial.print (" [SEND]" );sendArray.printfArray <HardwareSerial>(&Serial);
109
+ #endif
110
+ _uartp->flush ();
111
+ _uartp->write (sendArray.dataptr (),sendArray.size ());
112
+
113
+ uint8_t reviceBuff[256 ];
114
+
115
+ _uartp->setTimeout (5000 );
116
+
117
+ if (!_uartp->find (0xed )){ Serial.print (" [TIMEOUT]\r\n " ); return lastErrorcode = kTimeoutError ;}
118
+
119
+ size_t length = _uartp->available ();
120
+ _uartp->readBytes (reviceBuff,length);
121
+
122
+ #ifdef ZIGBEE_DEBUG
123
+ Serial.print (" [REVC]" );byteArray (reviceBuff,length).printfArray <HardwareSerial>(&Serial);
124
+ #endif
125
+ byteArray array (reviceBuff,length);
126
+ int pos = array.indexof (0xed );
127
+ Serial.printf (" pos:%d\r\n " ,pos);
128
+ // for( int i = 0; i < )
129
+ return kReviceOK ;
130
+ }
131
+
132
+ int DRFZigbee::linkMoudle ()
133
+ {
134
+ return sendCMDAndWaitRevice (0xfc ,ZIGBEE_CMD_LINKMODULE);
135
+ }
136
+
137
+ int DRFZigbee::rebootModule ()
138
+ {
139
+ return sendCMDAndWaitRevice (0xfc ,{0x06 ,0x44 ,0x54 ,0x4b ,0xaa ,0xbb });
140
+ }
141
+
142
+ int DRFZigbee::readModuleparm (zigbee_arg_t *parm)
143
+ {
144
+ byteArray array;
145
+ if ( sendCMDAndWaitRevice (0xfc ,ZIGBEE_CMD_READPARM,&array) != DRFZigbee::kReviceOK ) return lastErrorcode;
146
+ if (( array.at (0 ) != 0x0A )||( array.at (1 ) != 0x0E )) return kPramFormatError ;
147
+ array = array.mid (2 );
148
+ #ifdef ZIGBEE_DEBUG
149
+ array.printfArray <HardwareSerial>(&Serial);
150
+ #endif
151
+ if ( parm == nullptr ) return kPointerisnullptr ;
152
+ memcpy (parm->Wave ,array.dataptr (),sizeof (DRFZigbee::zigbee_arg));
153
+ return kReviceOK ;
154
+ }
155
+
156
+ int DRFZigbee::setModuleparm (zigbee_arg_t &parm)
157
+ {
158
+ byteArray sendArray;
159
+ sendArray += 0x07 ;
160
+ sendArray += byteArray (&parm.Wave [0 ],16 );
161
+ sendArray += byteArray (&parm.Wave [24 ],16 );
162
+ sendArray += byteArray (&parm.Wave [42 ],6 );
163
+
164
+ sendArray.printfArray <HardwareSerial>(&Serial);
165
+
166
+ return sendCMDAndWaitRevice (0xfc ,sendArray);
167
+ }
168
+
169
+ int8_t DRFZigbee::getModuleRSSI (nodeRSSI_t *nodeRSSIPtr)
170
+ {
171
+ byteArray reviceArray;
172
+ if ( sendCMDAndWaitRevice (0xfc ,ZIGBEE_CMD_GETNODERSSI,&reviceArray,5000 ) != kReviceOK ) return -1 ;
173
+
174
+ if ( reviceArray.at (0 ) != 0x0a ) return -1 ;
175
+
176
+ if ( nodeRSSIPtr != nullptr )
177
+ {
178
+ nodeRSSIPtr->routerLevel = reviceArray.at (1 );
179
+ nodeRSSIPtr->shortAddr = ( reviceArray.at (2 ) << 8 ) | reviceArray.at (3 );
180
+ nodeRSSIPtr->rssi = reviceArray.at (4 );
181
+ }
182
+
183
+ return int8_t (reviceArray.at (4 ));
184
+ }
185
+
186
+ int DRFZigbee::reviceData (reviceData_t *revice,uint8_t type,size_t timeout)
187
+ {
188
+ uint8_t reviceBuff[300 ];
189
+
190
+ _uartp->setTimeout (timeout);
191
+
192
+ if (!_uartp->find (type)){ lastErrorcode = kTimeoutError ; return kTimeoutError ; }
193
+
194
+ size_t length = _uartp->available ();
195
+ _uartp->readBytes (reviceBuff,length);
196
+
197
+ revice->length = reviceBuff[0 ];
198
+ revice->addr = (reviceBuff[1 ] << 8 ) + reviceBuff[2 ];
199
+ revice->array = new byteArray (&reviceBuff[3 ],revice->length );
200
+ revice->fromAddr = (reviceBuff[3 + revice->length ] << 8 ) + reviceBuff[4 + revice->length ];
201
+
202
+ return kReviceOK ;
203
+ }
0 commit comments