Skip to content

Commit

Permalink
Merge pull request #34 from chegewara/master
Browse files Browse the repository at this point in the history
Refactored ping event
  • Loading branch information
MrBuddyCasino committed Jun 28, 2018
2 parents eedf5e0 + e8fbf70 commit 07ef9e5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 112 deletions.
92 changes: 38 additions & 54 deletions components/alexa/alexa.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ struct alexa_session_struct_t
EventGroupHandle_t event_group;
alexa_stream_t *stream_directives;
alexa_stream_t *stream_events;
alexa_stream_t *stream_ping;
asio_registry_t *registry;
int last_time_activity;
};

static alexa_session_t *alexa_session;

const int AUTH_TOKEN_VALID_BIT = BIT(2);
const int DOWNCHAN_CONNECTED_BIT = BIT(3);
const int INITIAL_STATE_SENT_BIT = BIT(4);
//#define REFRESH_TOKEN "Atzr|IwEBIJxHw2Xg2PCgRYBoesycfa4ZjMN5AGSe8uRchSwdVskPXtt4Vl982jVz994u1fuDMLTR5oytHAm1J_KECqPrTK0l5AoBHSem8gjVrhDwslwMWic4B21heKtxZLGVNIFbHfxbbm7f3EV-XT7ruH5aCGB3kmp0Kif_XgvxjRFPsm8Y8s2jHhQB44YQ6N4aANU6qjyxlNd8beYildvv97luyoIFExX_B6Cb4CISlPqAcfml0iqWOzD_Q8F2PEn3Wc5_aLZHkngoSisMDPfoehXW9gTay0zrrP06okz6M--cZrtJykrIncs0aCO8smLP7uuNfY4uuwKraFo9YsXgtCi-2Bsvr-f5uVTr2j2BpTHrOzsz6OWlMIO1XQmYnDL9OIQZ3J-Dz2zbBFTpSq7j1x1nOdb8Ja7rkmO_HrM1CQnSEYjvhCsJrYvAv9dFJ-8MpNYKGTf0iZBbqo65fJ40dfMe5JWZEguPxmQvBWW2Rkn5z03dL1jqqCtRjk9IjBmdVwzpbyneeDt5D3oqOqHG0df9s7A01KcHubU62LG3e33FSzVNdg"
const int USER_ACTIVITY_PRESENCE_BIT = BIT(5);


/* Europe: alexa-eu / America: alexa-na */
static char *uri_directives = ALEXA_ENDPOINT "/v20160207/directives";
static char *uri_events = ALEXA_ENDPOINT "/v20160207/events";
static char *uri_ping = ALEXA_ENDPOINT "/ping";

#define TAG "alexa"

Expand Down Expand Up @@ -169,14 +168,6 @@ static int create_alexa_session(alexa_session_t **alexa_session_ptr)
alexa_session->stream_events->msg_id = 1;
alexa_session->stream_events->dialog_req_id = 1;

alexa_session->stream_ping = calloc(1, sizeof(alexa_stream_t));
alexa_session->stream_ping->stream_type = STREAM_PING;
alexa_session->stream_ping->alexa_session = alexa_session;
alexa_session->stream_ping->status = CONN_CLOSED;
alexa_session->stream_ping->stream_id = -1;
alexa_session->stream_ping->msg_id = 1;
alexa_session->stream_ping->dialog_req_id = 1;

return 0;
}

Expand Down Expand Up @@ -472,7 +463,6 @@ int open_downchannel(alexa_session_t *alexa_session)

alexa_session->stream_directives->http2_session = http2_session;
alexa_session->stream_events->http2_session = http2_session;
alexa_session->stream_ping->http2_session = http2_session;

/* start read write loop */
//ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
Expand Down Expand Up @@ -517,34 +507,6 @@ int alexa_send_event(alexa_session_t *alexa_session,
}


int alexa_send_ping(alexa_session_t *alexa_session,
nghttp2_data_source_read_callback read_callback)
{

ESP_LOGE(TAG, "PING");
// h2 will take ownership
nghttp2_data_provider data_provider_struct = {
.read_callback = read_callback,
.source.ptr = alexa_session->stream_ping
};

// add headers
char *auth_header = build_auth_header(alexa_session->auth_token);
// ESP_LOGI(TAG, "authorization length=%d value=%s", strlen(auth_header), auth_header);
nghttp2_nv hdrs[2] = {
MAKE_NV("authorization", auth_header, strlen(auth_header))};

/* create stream */
int ret = nghttp_new_stream(alexa_session->stream_directives->http2_session,
&alexa_session->stream_events->stream_id,
alexa_session->stream_events, uri_ping, "GET", hdrs, 1,
&data_provider_struct);

free(auth_header);

return ret;
}

void alexa_gpio_handler_task(gpio_handler_param_t *params)
{
xQueueHandle gpio_evt_queue = params->gpio_evt_queue;
Expand All @@ -564,13 +526,22 @@ void alexa_gpio_handler_task(gpio_handler_param_t *params)
vTaskDelete(NULL);
}

/*
* reset inactivity "timer" and ping "timer"
*/
void update_last_action_time()
{
xEventGroupSetBits(alexa_session->event_group, USER_ACTIVITY_PRESENCE_BIT);
}

void alexa_gpio_handler(gpio_num_t io_num, void *user_data)
{
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));

alexa_session_t *alexa_session = user_data;

update_last_action_time();

if(speech_recognizer_is_ready()) {
ui_queue_event(UI_RECOGNIZING_SPEECH);
speech_recognizer_start_capture(alexa_session);
Expand Down Expand Up @@ -628,22 +599,36 @@ asio_result_t on_wifi_connected_cb(asio_task_t *task, void *arg, void *user_data
return ASIO_OK;
}

#define TICKS_TO_DELAY 4 * 60 * 1000 // 1000 - is it 1000 ms or is it 1000 ticks???
void delayed_server_ping_task(void* p){
TickType_t last_wake_time;
TickType_t ticks_before_delay;

last_wake_time = xTaskGetTickCount();
ticks_before_delay = last_wake_time;
/* send ping every around 5 minutes, "timer" is reset on user activity (ie push button) */
TickType_t last_wake_time;
#define TICKS_TO_DELAY 5 * 60 * 999 // its 999 ticks if is setup 1000 ticks in menuconfig
asio_result_t delayed_server_ping_task(asio_task_t *task, void *arg, void *user_data)
{
EventGroupHandle_t event_group = arg;
alexa_session_t *alexa_session = user_data;

while(1){
// maybe it is over thinking to use vTaskDelayUntil()?
// probably vTaskDelay() would be enough
vTaskDelayUntil(&last_wake_time, TICKS_TO_DELAY );
event_send_ping(alexa_session);
if(!(xEventGroupGetBits(event_group) & USER_ACTIVITY_PRESENCE_BIT) && last_wake_time + TICKS_TO_DELAY <= xTaskGetTickCount())
{
nghttp2_session *session = alexa_session->stream_events->http2_session->h2_session;
nghttp2_submit_ping(session, NGHTTP2_FLAG_NONE, NULL);
last_wake_time = xTaskGetTickCount();
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
if((xTaskGetTickCount() - alexa_session->last_time_activity)/3600 > 0){
//todo send UserInactiveReport every 1 hour inactivity of user in seconds rounded to full hour
}
}
else if(xEventGroupGetBits(event_group) & USER_ACTIVITY_PRESENCE_BIT)
{
last_wake_time = xTaskGetTickCount();
alexa_session->last_time_activity = last_wake_time;
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
xEventGroupClearBits(event_group, USER_ACTIVITY_PRESENCE_BIT);
}
return ASIO_OK;
}



int alexa_init()
{
ESP_LOGI(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
Expand Down Expand Up @@ -674,8 +659,7 @@ int alexa_init()
/* send initial state when downchannel is connected */
asio_new_generic_task("send_initial_state", alexa_session->registry, on_downchan_connected_cb, alexa_session->event_group, alexa_session);

/* create task to send ping request to keep connection socket open */
xTaskCreatePinnedToCore(delayed_server_ping_task, "ping", 2048, NULL, 5, NULL, 0);
asio_new_generic_task("ping", alexa_session->registry, delayed_server_ping_task, alexa_session->event_group, alexa_session);
// run event loop
while(1) {
asio_registry_poll(alexa_session->registry);
Expand Down
56 changes: 0 additions & 56 deletions components/alexa/event_send_ping.c

This file was deleted.

2 changes: 1 addition & 1 deletion components/alexa/include/alexa.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void auth_token_refresh(alexa_session_t *alexa_session);

int alexa_send_event(alexa_session_t *alexa_session, nghttp2_data_source_read_callback read_callback);

int alexa_send_ping(alexa_session_t *alexa_session, nghttp2_data_source_read_callback read_callback);
void update_last_action_time();

extern const int AUTH_TOKEN_VALID_BIT;
extern const int DOWNCHAN_CONNECTED_BIT;
Expand Down
1 change: 0 additions & 1 deletion components/alexa/include/alexa_events_send.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@

int event_send_state(alexa_session_t *alexa_session);
int event_send_settings_updated(alexa_session_t *alexa_session);
int event_send_ping(alexa_session_t *alexa_session);

#endif /* _INCLUDE_ALEXA_EVENTS_SEND_H_ */

0 comments on commit 07ef9e5

Please sign in to comment.