The (alpha of the) official Exosite library, CoAP version.
NOTE: This mirror repository may be out of date, check the main repo for changes. If there are any remind me to update this mirror.
This is an unstable alpha of the Official Exosite library, there are known issues with the port to this platform. You probably shouldn't use this library yet if you just want to get things done.
This version uses CoAP for the application protocol.
Diff: src/exosite.c
- Revision:
- 24:cfbd25fc62c4
- Parent:
- 23:e94087cd483d
- Child:
- 27:786cdb9c3605
--- a/src/exosite.c Tue Jan 06 11:22:43 2015 -0600 +++ b/src/exosite.c Tue Jan 06 13:02:02 2015 -0600 @@ -295,6 +295,7 @@ { uint8_t buf[MINIMUM_DATAGRAM_SIZE]; coap_pdu pdu; + coap_option opt; coap_payload payload; int i; @@ -311,7 +312,6 @@ if (coap_get_type(&pdu) == CT_CON || coap_get_type(&pdu) == CT_NON) { if (op[i].state == EXO_REQUEST_SUBSCRIBED && coap_get_token(&pdu) == op[i].token) { if (coap_get_code(&pdu) == CC_CONTENT) { - coap_option opt; uint32_t new_seq = 0; opt = coap_get_option_by_num(&pdu, CON_OBSERVE, 0); for (int j = 0; j < opt.len; j++) { @@ -334,6 +334,16 @@ } else { op[i].state = EXO_REQUEST_SUB_ACK; } + + opt = coap_get_option_by_num(&pdu, CON_MAX_AGE, 0); + uint8_t max_age = 120; // default, 2 minutes, see RFC4787 Sec 4.3 + if (opt.num !=0 && opt.len == 1){ // if has Max-Age option, use it + max_age = opt.val[0]; + } + + // Set timeout between Max-Age to Max-Age + ACK_RANDOM_FACTOR (CoAP Defined) + op[i].timeout = exopal_get_time() + (max_age * 1000000) + + (((uint64_t)rand() % 1500000)); } } else if (coap_get_code_class(&pdu) != 2) { op[i].state = EXO_REQUEST_ERROR; @@ -369,7 +379,16 @@ memcpy(op[i].value, payload.val, payload.len); op[i].value[payload.len] = 0; op[i].state = EXO_REQUEST_SUCCESS; - op[i].timeout = exopal_get_time() + 120000000 + (rand() % 15 * 100000); + + opt = coap_get_option_by_num(&pdu, CON_MAX_AGE, 0); + uint8_t max_age = 120; // default, 2 minutes, see RFC4787 Sec 4.3 + if (opt.num !=0 && opt.len == 1){ // if has Max-Age option, use it + max_age = opt.val[0]; + } + + // Set timeout between Max-Age to Max-Age + ACK_RANDOM_FACTOR (CoAP Defined) + op[i].timeout = exopal_get_time() + (max_age * 1000000) + + (((uint64_t)rand() % 1500000)); } break; case EXO_ACTIVATE: @@ -502,9 +521,6 @@ op[i].state = EXO_REQUEST_SUBSCRIBED; else if (op[i].state == EXO_REQUEST_SUB_ACK_NEW) op[i].state = EXO_REQUEST_SUCCESS; - - // TODO: this should get set to Max-Age value, hard coding 120 s - op[i].timeout = exopal_get_time() + 120000000 + (rand() % 15 * 100000); } break; default: