コード例 #1
0
ファイル: main.c プロジェクト: xueliu/nRF51822
static void coap_endpoints_init(void)
{
    uint32_t err_code;

    static coap_resource_t root;
    err_code = coap_resource_create(&root, "/");
    APP_ERROR_CHECK(err_code);

    static coap_resource_t well_known;
    err_code = coap_resource_create(&well_known, ".well-known");
    APP_ERROR_CHECK(err_code);
    err_code = coap_resource_child_add(&root, &well_known);
    APP_ERROR_CHECK(err_code);

    static coap_resource_t core;
    err_code = coap_resource_create(&core, "core");
    APP_ERROR_CHECK(err_code);

    core.permission = COAP_PERM_GET;
    core.callback   = well_known_core_callback;

    err_code = coap_resource_child_add(&well_known, &core);
    APP_ERROR_CHECK(err_code);

    static coap_resource_t lights;
    err_code = coap_resource_create(&lights, m_lights_name);
    APP_ERROR_CHECK(err_code);

    err_code = coap_resource_child_add(&root, &lights);
    APP_ERROR_CHECK(err_code);

    err_code = coap_resource_create(&m_led3, m_led3_name);
    APP_ERROR_CHECK(err_code);

    m_led3.permission      = (COAP_PERM_GET | COAP_PERM_PUT | COAP_PERM_OBSERVE);
    m_led3.callback        = led3_callback;
    m_led3.ct_support_mask = COAP_CT_MASK_APP_JSON | COAP_CT_MASK_PLAIN_TEXT;
    m_led3.max_age         = 15;

    err_code = coap_resource_child_add(&lights, &m_led3);
    APP_ERROR_CHECK(err_code);

    uint16_t size = sizeof(m_well_known_core);
    err_code = coap_resource_well_known_generate(m_well_known_core, &size);
    APP_ERROR_CHECK(err_code);
}
コード例 #2
0
ファイル: coap_server.c プロジェクト: AleLudovici/CoAPXY
int main() {
	coap_server_t *server;
    fd_set lset, lsetcopy;
    int rc, max;
    coap_resource_t *res;
    struct timeval timeout;



    /* Create CoAP server */
    if (!coap_resource_create("test",  "Test", "", "", 0, 0, handler_resource, &res))
    	return 1;
    if (!coap_server_create(8005,0, &server))
    	return 1;

    coap_server_resource_add(server, res);

    max = server->context->socket;
    FD_ZERO(&lset);
    FD_SET(server->context->socket, &lset);

    lsetcopy = lset;

    while (1) {
        lset = lsetcopy;
        timeout.tv_sec = 2;
        timeout.tv_usec = 0;
        do {
            rc = select(max + 1, &lset, NULL, NULL, &timeout);
        } while(rc < 0 && errno == EINTR);

        if(rc == 0) {
//        	coap_pdu_t *pdu;
//
//        	coap_pdu_create(&pdu);
//
//        	pdu->hdr.type = COAP_MESSAGE_CON;
//        	pdu->hdr.code = COAP_RESPONSE_205;
//        	sprintf(pdu->payload, "%s", "HOLA MUNDO");
//        	pdu->payload_len = strlen("HOLA MUNDO");
//
//        	coap_server_notify_observers(server, res, pdu);
//
//        	coap_pdu_delete(pdu);

            continue;
        }
        if (FD_ISSET(server->context->socket,&lset)) {
        	coap_server_read(server);
        	coap_server_dispatch(server);
        } else {
        	coap_server_clean_pending(server);
        }
    }
    return 0;
}