zdcache.lst
来自「CC2430上开关控制灯和温度传感器采集两个例子」· LST 代码 · 共 1,345 行 · 第 1/5 页
LST
1,345 行
132 static uint16 cacheFindAddr[FIND_RSP_MAX];
133 static byte EPArr[CACHE_EP_MAX];
134 static byte EPCnt;
135 static byte cacheCnt; // Count of responding cache devices.
136 static byte cacheRsp; // Results of last discovery cache request.
137 #endif
138
139
140
141 #if ( CACHE_DEV_MAX > 0 )
142 /*********************************************************************
143 * @fn processDiscoveryStoreReq
144 *
145 * @brief Process a Discovery_store_req
146 *
147 * @return none
148 */
149 static byte processDiscoveryStoreReq( byte *data )
150 {
151 uint16 aoi = BUILD_UINT16( data[0], data[1] );
152 byte rtrn = ZDP_INSUFFICIENT_SPACE;
153 byte idx;
154
155 data += 2;
156 // First purge any outdated cache with this network address or IEEE.
157 purgeAddr( aoi );
158 purgeIEEE( data );
159
160 // If an invalid addr cannot be found, the cache arrays are full.
161 if ( (idx = getIdx( INVALID_NODE_ADDR )) != CACHE_DEV_MAX )
162 {
163 osal_cpyExtAddr( ExtAddr[idx], data );
164 data += Z_EXTADDR_LEN;
165
166 if ( (*data++ == sizeof( NodeDescriptorFormat_t )) &&
167 (*data++ == sizeof( NodePowerDescriptorFormat_t )) &&
168 (*data++ <= CACHE_EP_MAX) &&
169 (*data < CACHE_EP_MAX) )
170 {
171 rtrn = ZDP_SUCCESS;
172 NwkAddr[idx] = aoi;
173 Expiry[idx] = 0; //TBD - what value & how to keep alive?
174 EPCnt[idx] = 0;
175 }
176 }
177
178 return rtrn;
179 }
180
181 /*********************************************************************
182 * @fn processSimpleDescStoreReq
183 *
184 * @brief Process a Simple_Desc_store_req
185 *
186 * @return none
187 */
188 static byte processSimpleDescStoreReq( byte *data, byte idx )
189 {
190 byte rtrn = ZDP_INSUFFICIENT_SPACE;
191 SimpleDescriptionFormat_t desc;
192
193 // Skip first byte == total length of descriptor.
194 if (ZDO_ParseSimpleDescBuf( data+1, &desc )) {
195 // malloc failed -- we can't do this...
196 return ZDP_NOT_PERMITTED;
197 }
198
199 if ( (desc.AppNumInClusters <= CACHE_CR_MAX) &&
200 (desc.AppNumOutClusters <= CACHE_CR_MAX) )
201 {
202 byte epIdx = getIdxEP( idx, desc.EndPoint );
203
204 if ( epIdx == CACHE_EP_MAX )
205 {
206 rtrn = ZDP_NOT_PERMITTED;
207 }
208 else
209 {
210 SimpleDescriptionFormat_t *pDesc = &(SimpDesc[idx][epIdx]);
211
212 osal_memcpy( pDesc->pAppInClusterList, desc.pAppInClusterList,
213 desc.AppNumInClusters*sizeof(uint16) );
214 osal_memcpy( pDesc->pAppOutClusterList, desc.pAppOutClusterList,
215 desc.AppNumOutClusters*sizeof(uint16) );
216
217 pDesc->EndPoint = desc.EndPoint;
218 pDesc->AppProfId = desc.AppProfId;
219 pDesc->AppDeviceId = desc.AppDeviceId;
220 pDesc->AppDevVer = desc.AppDevVer;
221 pDesc->Reserved = desc.Reserved;
222 pDesc->AppNumInClusters = desc.AppNumInClusters;
223 pDesc->AppNumOutClusters = desc.AppNumOutClusters;
224
225 rtrn = ZDP_SUCCESS;
226 }
227 }
228
229 // free up the malloc'ed cluster lists
230 if (desc.AppNumInClusters) {
231 osal_mem_free(desc.pAppInClusterList);
232 }
233 if (desc.AppNumOutClusters) {
234 osal_mem_free(desc.pAppOutClusterList);
235 }
236
237 return rtrn;
238 }
239
240 /*********************************************************************
241 * @fn processFindNodeCacheReq
242 *
243 * @brief Process a Find_node_cache_req
244 *
245 * @return none
246 */
247 static void processFindNodeCacheReq( byte *data )
248 {
249 uint16 aoi = BUILD_UINT16( data[0], data[1] );
250 byte idx = getIdx( aoi );
251
252 if ( idx == CACHE_DEV_MAX )
253 {
254 idx = getIdxExt ( data+2 );
255 }
256
257 if ( idx != CACHE_DEV_MAX )
258 {
259 byte buf[2+2+Z_EXTADDR_LEN];
260
261 buf[0] = LO_UINT16( ZDAppNwkAddr.addr.shortAddr );
262 buf[1] = HI_UINT16( ZDAppNwkAddr.addr.shortAddr );
263 buf[2] = LO_UINT16( NwkAddr[idx] );
264 buf[3] = HI_UINT16( NwkAddr[idx] );
265 osal_cpyExtAddr( buf+4, ExtAddr[idx] );
266
267 SendMsg( Find_node_cache_rsp, 2+2+Z_EXTADDR_LEN, buf );
268 }
269 }
270
271 /*********************************************************************
272 * @fn processMgmtCacheReq
273 *
274 * @brief Process a Mgmt_cache_req
275 *
276 * @return none
277 */
278 static void processMgmtCacheReq( byte idx )
279 {
280 const byte max =
281 (((CACHE_DEV_MAX-idx) * (2 + Z_EXTADDR_LEN) + 1) < MAX_PKT_LEN) ?
282 ((CACHE_DEV_MAX-idx) * (2 + Z_EXTADDR_LEN) + 1) : MAX_PKT_LEN;
283 byte *buf = osal_mem_alloc( max );
284 byte status = ZDP_INSUFFICIENT_SPACE;
285 byte cnt = 1;
286
287 if ( buf == NULL )
288 {
289 buf = &status;
290 }
291 else
292 {
293 byte *ptr = buf;
294 *ptr++ = ZDP_SUCCESS;
295 *ptr++ = getCnt();
296 *ptr++ = idx;
297 *ptr++ = 0;
298
299 for ( idx = getNth( idx ); idx < CACHE_DEV_MAX; idx++ )
300 {
301 if ( cnt >= (max - (2 + Z_EXTADDR_LEN)) )
302 {
303 break;
304 }
305
306 if ( NwkAddr[idx] != INVALID_NODE_ADDR )
307 {
308 ptr = osal_cpyExtAddr( ptr, ExtAddr[idx] );
309 *ptr++ = LO_UINT16( NwkAddr[idx] );
310 *ptr++ = HI_UINT16( NwkAddr[idx] );
311 cnt += (2 + Z_EXTADDR_LEN);
312 buf[3]++;
313 }
314 }
315 }
316
317 SendMsg( Mgmt_Cache_rsp, cnt, buf );
318
319 if ( buf != NULL )
320 {
321 osal_mem_free( buf );
322 }
323 }
324
325 /*********************************************************************
326 * @fn getCnt
327 *
328 * @return The count of valid cache entries.
329 *
330 */
331 static byte getCnt( void )
332 {
333 byte cnt = 0;
334 byte idx;
335
336 for ( idx = 0; idx < CACHE_DEV_MAX; idx++ )
337 {
338 if ( NwkAddr[idx] != INVALID_NODE_ADDR )
339 {
340 cnt++;
341 }
342 }
343
344 return cnt;
345 }
346
347 /*********************************************************************
348 * @fn getIdx
349 *
350 * @brief Find the idx into the Discovery Cache Arrays corresponding to
351 * the given short address.
352 *
353 * @param uint16 - a valid 16 bit short address.
354 *
355 * @return If address found, return the valid index, else CACHE_DEV_MAX.
356 *
357 */
358 static byte getIdx( uint16 addr )
359 {
360 byte idx;
361
362 for ( idx = 0; idx < CACHE_DEV_MAX; idx++ )
363 {
364 if ( addr == NwkAddr[idx] )
365 {
366 break;
367 }
368 }
369
370 return idx;
371 }
372
373 /*********************************************************************
374 * @fn getNth
375 *
376 * @param byte - the Nth idx sought.
377 *
378 * @return If N or more entries exist, return the index of the Nth entry,
379 * else CACHE_DEV_MAX.
380 *
381 */
382 static byte getNth( byte Nth )
383 {
384 byte cnt = 0;
385 byte idx;
386
387 for ( idx = 0; idx < CACHE_DEV_MAX; idx++ )
388 {
389 if ( NwkAddr[idx] != INVALID_NODE_ADDR )
390 {
391 if ( cnt++ >= Nth )
392 {
393 break;
394 }
395 }
396 }
397
398 return idx;
399 }
400
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?