void test_set_callback_fail(CuTest* tc) { evrythng_handle_t h; CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngInitHandle(&h)); CuAssertIntEquals(tc, EVRYTHNG_SUCCESS, EvrythngSetLogCallback(h, 0)); EvrythngDestroyHandle(h); }
static void common_tcp_init_handle(evrythng_handle_t* h) { EvrythngInitHandle(h); EvrythngSetUrl(*h, MQTT_URL); EvrythngSetLogCallback(*h, log_callback); EvrythngSetConnectionCallbacks(*h, on_connection_lost, on_connection_restored); EvrythngSetKey(*h, DEVICE_API_KEY); }
static void evrythng_task(void* pvParameters) { cmd_opts* opts = (cmd_opts*)pvParameters; EvrythngInitHandle(&opts->evt_handle); EvrythngSetLogCallback(opts->evt_handle, log_callback); EvrythngSetConnectionCallbacks(opts->evt_handle, conlost_callback, conrestored_callback); EvrythngSetUrl(opts->evt_handle, opts->url); EvrythngSetKey(opts->evt_handle, opts->key); EvrythngSetThreadPriority(opts->evt_handle, 5); log("Connecting to %s", opts->url); while(EvrythngConnect(opts->evt_handle) != EVRYTHNG_SUCCESS) { log("Retrying"); platform_sleep(2000); } log("Evrythng client Connected"); if (opts->sub) { log("Subscribing to property %s", opts->prop); EvrythngSubThngProperty(opts->evt_handle, opts->thng, opts->prop, 1, print_property_callback); while (!stop) { platform_sleep(2000); } } else { while (!stop) { int value = rand() % 100; char msg[128]; sprintf(msg, "[{\"value\": %d}]", value); log("Publishing value %d to property %s", value, opts->prop); EvrythngPubThngProperty(opts->evt_handle, opts->thng, opts->prop, msg); platform_sleep(1000); } } EvrythngDisconnect(opts->evt_handle); EvrythngDestroyHandle(opts->evt_handle); vTaskEndScheduler(); }
/* This task configures Evrythng client and connects to the Evrythng cloud */ static void evrythng_task() { psm_handle_t handle; int rc; if ((rc = psm_open(&handle, "evrythng")) != 0) { wmprintf("psm_open failed with: %d (Is the module name registered?)\n\r", rc); goto exit; } char api_key[128]; if (psm_get(&handle, "api_key", api_key, 128) == 0) { wmprintf("api_key: %s\n\r", api_key); } else { wmprintf("api_key doesn't exist\n\r"); goto exit; } char thng_id_buf[64]; if (psm_get(&handle, "thng_id", thng_id_buf, 64) == 0) { if (thng_id != NULL) { os_mem_free(thng_id); } thng_id = (char*)os_mem_alloc((strlen(thng_id_buf)+1)*sizeof(char)); strcpy(thng_id, thng_id_buf); wmprintf("thng_id: %s\n\r", thng_id); } else { wmprintf("thng_id doesn't exist\n\r"); goto exit; } char url_buf[64]; if (psm_get(&handle, "url", url_buf, 64) == 0) { wmprintf("Evrythng URL: %s\n\r", url_buf); } else { wmprintf("Evrythng URL doesn't exist\n\r"); goto exit; } psm_close(&handle); EvrythngInitHandle(&evt_handle); EvrythngSetUrl(evt_handle, url_buf); EvrythngSetKey(evt_handle, api_key); EvrythngSetLogCallback(evt_handle, log_callback); EvrythngSetConnectionCallbacks(evt_handle, on_connection_lost, on_connection_restored); while (EvrythngConnect(evt_handle) != EVRYTHNG_SUCCESS) { wmprintf("Retry\n\r"); os_thread_sleep(os_msec_to_ticks(5000)); } wmprintf("Connected\n\r"); os_semaphore_create_counting(&button1_sem, "button1_sem", 1000, 0); os_semaphore_create_counting(&button2_sem, "button1_sem", 1000, 0); EvrythngSubThngAction(evt_handle, thng_id, "_led1", 0, action_led_callback); EvrythngSubThngAction(evt_handle, thng_id, "_led2", 0, action_led_callback); os_thread_create(&button1_thread, "button1_task", button_task, (void*)button_1, &button_stack, OS_PRIO_3); os_thread_create(&button2_thread, "button2_task", button_task, (void*)button_2, &button_stack, OS_PRIO_3); exit: os_thread_self_complete(0); }