}
} else if (ctx->protocol == COAP_PROTO_TCP || ctx->protocol == COAP_PROTO_TLS) {
/* TODO: do nothing? */
+ debug("message_handler : do nothing\n");
} else {
/* should not enter here */
}
code = (unsigned short)coap_get_code(received, transport);
+ debug("message_handler : code %d, sent %p\n", code, sent);
+
/* output the received data, if any */
if (code == COAP_RESPONSE_CODE(205)) {
/* set obs timer if we have successfully subscribed a resource */
- info("message_handler : code %d, sent %p\n", code, sent);
-
if (sent && coap_check_option2(received, COAP_OPTION_SUBSCRIPTION, &opt_iter, transport)) {
printf("message_handler : observation relationship established, set timeout to %d\n", obs_seconds);
set_timeout(&obs_wait, obs_seconds);
}
} else { /* no 2.05 */
+ debug("message_handler : response class %d\n", COAP_RESPONSE_CLASS(code));
/* check if an error was signaled and output payload if so */
if (COAP_RESPONSE_CLASS(code) >= 4) {
fprintf(stderr, "%d.%02d", (code >> 5), code & 0x1F);
}
/* finally send new request, if needed */
- if (pdu && coap_send(ctx, remote, pdu) == COAP_INVALID_TID) {
- debug("message_handler: error sending response");
+ if (pdu != NULL) {
+ if (coap_send(ctx, remote, pdu) == COAP_INVALID_TID) {
+ debug("message_handler: error sending response");
+ }
+ } else {
+ debug("message_handler: pdu is NULL\n");
}
coap_delete_pdu(pdu);
case COAP_PROTO_TCP:
case COAP_PROTO_TLS:
tid = coap_send(ctx, &dst, pdu);
-
- if (tid == COAP_INVALID_TID) {
- coap_delete_pdu(pdu);
- }
+ coap_delete_pdu(pdu);
break;
default :
/* should not enter here */
if (g_operating_status == SERVER_RUNNING) {
if (quit) {
printf("coap-server : set quit flag %d\n", quit);
- /* To prevent memory leakage */
- if (addr_str != NULL)
+ while (g_operating_status != SERVER_STOPPED) {
+ usleep(100000);
+ }
+ printf("coap-server : server process has been stopped\n");
+ if (addr_str) {
free(addr_str);
+ }
} else {
printf("coap-server : another libcoap-server is running\n");
}
+
return 0;
}
coap_pdu_t *response;
coap_tid_t result = COAP_INVALID_TID;
- if (request) {
- /* TODO : Considering TCP Case */
- response = coap_pdu_init(type, 0, request->transport_hdr->udp.id, sizeof(coap_pdu_t));
- if (response) {
- result = coap_send(context, dst, response);
- coap_delete_pdu(response);
+ coap_transport_t transport;
+
+ switch (context->protocol) {
+ case COAP_PROTO_UDP:
+ case COAP_PROTO_DTLS:
+ transport = COAP_UDP;
+ if (request) {
+ response = coap_pdu_init(type, 0, request->transport_hdr->udp.id, sizeof(coap_pdu_t));
+ if (response) {
+ result = coap_send(context, dst, response);
+ coap_delete_pdu(response);
+ }
}
+ break;
+ case COAP_PROTO_TCP:
+ case COAP_PROTO_TLS:
+ transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)request)[0] >> 4);
+ result = COAP_TCP_TID;
+ break;
+ default:
+ break;
}
+
return result;
}
}
/* Now create the response and fill with options and payload data. */
- /* TODO : Considering TCP Case */
if (protocol == COAP_PROTO_UDP || protocol == COAP_PROTO_DTLS) {
response = coap_pdu_init(type, code, request->transport_hdr->udp.id, COAP_MAX_PDU_SIZE);
} else if (protocol == COAP_PROTO_TCP || protocol == COAP_PROTO_TLS) {
context->response_handler(context, &rcvd->remote, sent ? sent->pdu : NULL, rcvd->pdu, rcvd->id);
} else {
/* send ACK if rcvd is confirmable (i.e. a separate response) */
- coap_send_ack(context, &rcvd->remote, rcvd->pdu);
+ if (context->protocol == COAP_PROTO_UDP || context->protocol == COAP_PROTO_DTLS) {
+ coap_send_ack(context, &rcvd->remote, rcvd->pdu);
+ } else {
+ debug("handle_response : do nothing\n");
+ /* TODO: CoAP over TCP doesn't have ACK message */
+ }
}
}
warn("coap_check_notify : failed to create TCP response\n");
} else {
tid = coap_send(context, &obs->subscriber, tcp_resp);
- if (COAP_INVALID_TID == tid) {
- coap_delete_pdu(tcp_resp);
+ if (tid == COAP_INVALID_TID) {
+ debug("coap_check_notify: coap_send failed\n");
}
+ coap_delete_pdu(tcp_resp);
}
break;
default: