/** @internal @brief Function registered to #ZCL_TIME_ATTR_TIME attribute of Time Cluster and called to refresh the Time attribute (global zcl_time_time). See zcl_attribute_update_fn() for calling convention. */ _zcl_time_debug uint_fast8_t _zcl_time_time_get( const zcl_attribute_full_t FAR *attribute) { // zcl_attribute_update_fn API, but 'attribute' parameter's value is always // a pointer to 'zcl_time_time' variable. XBEE_UNUSED_PARAMETER( attribute); #if ZCL_TIME_EPOCH_DELTA > 0 if (! zcl_time_skew && xbee_seconds_timer() < ZCL_TIME_EPOCH_DELTA) { // we don't really know what time it is... zcl_time_time = ZCL_UTCTIME_INVALID; } else #endif { zcl_time_time = zcl_time_skew + (xbee_seconds_timer() - ZCL_TIME_EPOCH_DELTA); } #ifdef ZCL_TIME_VERBOSE printf( "%s: read clock & updated zcl_time_time to 0x%" PRIx32 "\n", __FUNCTION__, zcl_time_time); #endif return ZCL_STATUS_SUCCESS; }
/* START FUNCTION DESCRIPTION ******************************************** _wpan_endpoint_expire_conversations <wpan_aps.c> SYNTAX: void _wpan_endpoint_expire_conversations( wpan_ep_state_t FAR *state) DESCRIPTION: Walk an endpoint's conversation table and expire any conversations that have timed out. PARAMETER1: state - endpoint state (from endpoint table) **************************************************************************/ wpan_aps_debug void _wpan_endpoint_expire_conversations( wpan_ep_state_t FAR *state) { wpan_conversation_t FAR *conversation; uint_fast8_t i; uint16_t now; if (state == NULL) { return; } /* Notes regarding timeout calculation: We just store the lower 16 bits of the xbee_seconds_timer() value of when we should timeout. We can tell whether we're before or after that time by subtracting the target time from the curent time. If the result as a signed integer is >= 0, we have passed the selected timeout value. */ now = (uint16_t) xbee_seconds_timer(); conversation = state->conversations; for (i = WPAN_MAX_CONVERSATIONS; i; ++conversation, --i) { if (conversation->handler != NULL && conversation->timeout != 0 && (int16_t)(now - conversation->timeout) >= 0) { // send timeout to conversation's handler, ignore the response conversation->handler( conversation, NULL); wpan_conversation_delete( conversation); } } }
/** @internal @brief Function registered to #ZCL_TIME_ATTR_TIME attribute of Time Cluster and called to modify the attribute. Also called internally to update the zcl_time_skew global. @param[in] attribute ignored; assumed to point to zcl_time_time @param[in,out] rec if NULL, function was called internally to update zcl_time_skew only See zcl_attribute_write_fn() for calling convention. */ _zcl_time_debug int _zcl_time_time_set( const zcl_attribute_full_t FAR *attribute, zcl_attribute_write_rec_t *rec) { int16_t bytes_read = 0; // decode using standard method if (rec) { // if this device is a MASTER, Time is read-only if (zcl_time_timestatus & ZCL_TIME_STATUS_MASTER) { rec->status = ZCL_STATUS_READ_ONLY; rec->buffer += 4; return 4; } bytes_read = zcl_decode_attribute( &attribute->base, rec); if (! (rec->flags & ZCL_ATTR_WRITE_FLAG_ASSIGN)) { return bytes_read; } } zcl_time_skew = zcl_time_time - (xbee_seconds_timer() - ZCL_TIME_EPOCH_DELTA); #ifdef ZCL_TIME_VERBOSE printf( "%s: setting time to 0x%" PRIx32 "; skew is %" PRId32 " sec\n", __FUNCTION__, zcl_time_time, zcl_time_skew); #endif return bytes_read; }
/* START FUNCTION DESCRIPTION ******************************************** wpan_conversation_register <wpan_aps.c> SYNTAX: int wpan_conversation_register( wpan_ep_state_t FAR *state, wpan_response_fn handler, const void FAR *context, uint16_t timeout) DESCRIPTION: Add a conversation to the table of tracked conversations. PARAMETER1: state - endpoint state associated with sending endpoint PARAMETER2: handler - handler to call when responses come back, or NULL to increment and return the endpoint's transaction ID PARAMETER3: context - pointer stored in conversation table and passed to callback handler PARAMETER4: timeout - number of seconds before generating timeout, or 0 for none RETURNS: 0-255 - transaction ID to use in sent frame -EINVAL - state is invalid (NULL) -ENOSPC - table is full SEE ALSO: wpan_endpoint_next_trans **************************************************************************/ wpan_aps_debug int wpan_conversation_register( wpan_ep_state_t FAR *state, wpan_response_fn handler, const void FAR *context, uint16_t timeout) { wpan_conversation_t FAR *conversation; uint_fast8_t i; if (! state) { return -EINVAL; } if (! handler) { // caller just wants the next transaction ID return ++state->last_transaction; } conversation = state->conversations; for (i = WPAN_MAX_CONVERSATIONS; i; ++conversation, --i) { if (conversation->handler == NULL) { // cast away the const -- allow const and non-const in conversation conversation->context = (void FAR *)context; conversation->handler = handler; if (timeout != 0) { timeout += (uint16_t) xbee_seconds_timer(); if (timeout == 0) { // timeout of 0 is reserved for "never", so add an extra second timeout = 1; } } conversation->timeout = timeout; return (conversation->transaction_id = ++state->last_transaction); } } return -ENOSPC; }