smcp_status_t smcp_observable_trigger(smcp_observable_t context, uint8_t key, uint8_t flags) { smcp_status_t ret = SMCP_STATUS_OK; int8_t i; #if SMCP_EMBEDDED smcp_t const interface = smcp_get_current_instance(); #else smcp_t const interface = context->interface; if(!interface) goto bail; #endif if(!context->first_observer) goto bail; for(i = context->first_observer-1; i >= 0; i = observer_table[i].next - 1) { if( (observer_table[i].key != SMCP_OBSERVABLE_BROADCAST_KEY) && (key != SMCP_OBSERVABLE_BROADCAST_KEY) && (observer_table[i].key != key) ) { continue; } observer_table[i].seq++; if(observer_table[i].transaction.active) { smcp_transaction_tickle(interface, &observer_table[i].transaction); } else { smcp_transaction_init( &observer_table[i].transaction, 0, // Flags (void*)&retry_sending_event, (void*)&event_response_handler, (void*)&observer_table[i] ); ret = smcp_transaction_begin( interface, &observer_table[i].transaction, SHOULD_CONFIRM_EVENT_FOR_OBSERVER(&observer_table[i])?SMCP_OBSERVER_CON_EVENT_EXPIRATION:SMCP_OBSERVER_NON_EVENT_EXPIRATION ); } } bail: return ret; }
static smcp_status_t response_test_handler(int statuscode, void* context) { test_data_s * const test_data = context; smcp_t interface = smcp_get_current_instance(); bool has_observe_option = false; smcp_status_t status = 0; if(!test_data->finished) { if (statuscode < 0) { test_data->finished = true; if (statuscode != SMCP_STATUS_TRANSACTION_INVALIDATED) { test_data->error = statuscode; } } else { const uint8_t* value; coap_size_t value_len; coap_option_key_t key; test_data->msg_id = smcp_inbound_get_packet()->msg_id; if (smcp_inbound_is_dupe()) { test_data->inbound_dupe_packets++; } else { test_data->inbound_packets++; } while ((key=smcp_inbound_next_option(&value, &value_len))!=COAP_OPTION_INVALID) { if (key == COAP_OPTION_BLOCK1) { test_data->block1_option = 0; test_data->has_block1_option = 1; test_data->block1_option = coap_decode_uint32(value,(uint8_t)value_len); if ( statuscode == COAP_RESULT_231_CONTINUE) { smcp_transaction_new_msg_id(interface, test_data->transaction, smcp_get_next_msg_id(interface)); smcp_transaction_tickle(interface, test_data->transaction); } } if (key == COAP_OPTION_BLOCK2) { test_data->block2_option = 0; test_data->has_block2_option = 1; test_data->block2_option = coap_decode_uint32(value,(uint8_t)value_len); if (!(test_data->block2_option & (1<<3))) { test_data->finished = true; } } if (key == COAP_OPTION_OBSERVE) { if (test_data->inbound_packets > 2) { test_data->finished = true; } has_observe_option = true; printf("\tobs-response: \"%s\"\n", smcp_inbound_get_content_ptr()); } } if ( !has_observe_option && !test_data->has_block2_option && statuscode != COAP_RESULT_231_CONTINUE ) { test_data->finished = true; } test_data->inbound_content_len += smcp_inbound_get_content_len(); test_data->inbound_code = (coap_code_t)statuscode; if (test_data->has_block2_option) { strncat(test_data->response, smcp_inbound_get_content_ptr(), smcp_inbound_get_content_len()); } else { strncpy(test_data->response, smcp_inbound_get_content_ptr(), smcp_inbound_get_content_len()); } } } return status; }