Example #1
0
static smcp_status_t
delete_response_handler(
	int			statuscode,
	void*		context
) {
	if (statuscode >= 0) {
		char* content = (char*)smcp_inbound_get_content_ptr();
		coap_size_t content_length = smcp_inbound_get_content_len();

		if(content_length>(smcp_inbound_get_packet_length()-4)) {
			fprintf(stderr, "INTERNAL ERROR: CONTENT_LENGTH LARGER THAN PACKET_LENGTH-4! (content_length=%u, packet_length=%u)\n",content_length,smcp_inbound_get_packet_length());
			gRet = ERRORCODE_UNKNOWN;
			goto bail;
		}

		if (delete_show_headers) {
			coap_dump_header(
				stdout,
				NULL,
				smcp_inbound_get_packet(),
				smcp_inbound_get_packet_length()
			);
		}

		if(!coap_verify_packet((void*)smcp_inbound_get_packet(), smcp_inbound_get_packet_length())) {
			fprintf(stderr, "INTERNAL ERROR: CALLBACK GIVEN INVALID PACKET!\n");
			gRet = ERRORCODE_UNKNOWN;
			goto bail;
		}


		if (statuscode != COAP_RESULT_202_DELETED) {
			fprintf(stderr, "delete: Result code = %d (%s)\n", statuscode,
					(statuscode < 0) ? smcp_status_to_cstr(
					statuscode) : coap_code_to_cstr(statuscode));
		}

		if(content && content_length) {
			char contentBuffer[500];

			content_length =
				((content_length > sizeof(contentBuffer) -
					2) ? sizeof(contentBuffer) - 2 : content_length);
			memcpy(contentBuffer, content, content_length);
			contentBuffer[content_length] = 0;

			if(content_length && (contentBuffer[content_length - 1] == '\n'))
				contentBuffer[--content_length] = 0;

			printf("%s\n", contentBuffer);
		}
	}

bail:
	if (gRet == ERRORCODE_INPROGRESS) {
		gRet = 0;
	}
	return SMCP_STATUS_OK;
}
static smcp_status_t
get_response_handler(int statuscode, void* context) {
    ThingMLCOAPContext * thingml_context = (ThingMLCOAPContext *) context;

    const char* content = (const char*)smcp_inbound_get_content_ptr();
    coap_size_t content_length = smcp_inbound_get_content_len();

    if(statuscode>=0) {
        if(content_length>(smcp_inbound_get_packet_length()-4)) {
            fprintf(stderr, "INTERNAL ERROR: CONTENT_LENGTH LARGER THAN PACKET_LENGTH-4! (content_length=%u, packet_length=%u)\n",content_length,smcp_inbound_get_packet_length());
            gRet = ERRORCODE_UNKNOWN;
            thingml_context->fn_onerror_callback(thingml_context->thing_instance, gRet);
            goto bail;
        }

        if((statuscode >= 0) && get_show_headers) {
            if(next_len != ((coap_size_t)(-1)))
                fprintf(stdout, "\n\n");
            coap_dump_header(
                stdout,
                NULL,
                smcp_inbound_get_packet(),
                smcp_inbound_get_packet_length()
            );
        }

        if(!coap_verify_packet((void*)smcp_inbound_get_packet(), smcp_inbound_get_packet_length())) {
            fprintf(stderr, "INTERNAL ERROR: CALLBACK GIVEN INVALID PACKET!\n");
            gRet = ERRORCODE_UNKNOWN;
            thingml_context->fn_onerror_callback(thingml_context->thing_instance, gRet);
            goto bail;
        }
    }

    if(statuscode == SMCP_STATUS_TRANSACTION_INVALIDATED) {
        gRet = 0;
    }

    if(observe_ignore_first) {
        observe_ignore_first = false;
        goto bail;
    }

    if(	((statuscode < COAP_RESULT_200) ||(statuscode >= COAP_RESULT_400))
            && (statuscode != SMCP_STATUS_TRANSACTION_INVALIDATED)
            && (statuscode != HTTP_TO_COAP_CODE(HTTP_RESULT_CODE_PARTIAL_CONTENT))
      ) {
        if(get_observe && statuscode == SMCP_STATUS_TIMEOUT) {
            gRet = 0;
        } else {
            gRet = (statuscode == SMCP_STATUS_TIMEOUT)?ERRORCODE_TIMEOUT:ERRORCODE_COAP_ERROR;
            fprintf(stderr, "get: Result code = %d (%s)\n", statuscode,
                    (statuscode < 0) ? smcp_status_to_cstr(
                        statuscode) : coap_code_to_cstr(statuscode));
            thingml_context->fn_onerror_callback(thingml_context->thing_instance, gRet);
        }
    }

    if((statuscode>0) && content && content_length) {
        coap_option_key_t key;
        const uint8_t* value;
        coap_size_t value_len;
        bool last_block = true;
        int32_t observe_value = -1;

        while((key=smcp_inbound_next_option(&value, &value_len))!=COAP_OPTION_INVALID) {

            if(key == COAP_OPTION_BLOCK2) {
                last_block = !(value[value_len-1]&(1<<3));
            } else if(key == COAP_OPTION_OBSERVE) {
                if(value_len)
                    observe_value = value[0];
                else observe_value = 0;
            }

        }

        thingml_context->fn_onmsgrcv_callback(thingml_context->thing_instance, content, content_length);

        last_observe_value = observe_value;
    }

    if(observe_once) {
        gRet = 0;
        goto bail;
    }

bail:
    return SMCP_STATUS_OK;
}