/* Main CLI processing thread * * Waits to receive a command buffer pointer from an input collector, and * then processes. Note that it must cleanup the buffer when done with it. * * Input collectors handle their own lexical analysis and must pass complete * command lines to CLI. */ static void cli_main(os_thread_arg_t data) { os_mutex_get(&cli_mutex, OS_WAIT_FOREVER); while (1) { int ret; char *msg; msg = NULL; ret = os_queue_recv(&cli.input_queue, &msg, RX_WAIT); if (ret != WM_SUCCESS) { if (ret == WM_E_BADF) { wmprintf("Error: CLI fatal queue error." "\r\n"); /* Special case fatal errors. Shouldn't happen. If it does * it means CLI is fatally corrupted, so end the thread. */ return; } /* A number of other non-fatal conditions can cause us to get here] * without a message to process, if so, just go back and wait. */ continue; } /* HALT message indicates that this thread will be deleted * shortly. Hence this function need to do necessary actions * required before getting deleted. * HALT message is not dynamically allocated, * hence msg doesn't need to be freed up in that case. */ if (msg != NULL) { if (strcmp(msg, HALT_MSG) == 0) break; ret = handle_input(msg); if (ret == 1) print_bad_command(msg); else if (ret == 2) wmprintf("syntax error\r\n"); wmprintf(PROMPT); /* done with it, clean up the message (we own it) */ cli_mem_free(&msg); } } os_mutex_put(&cli_mutex); os_thread_self_complete(NULL); }
/* 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); }