LOCAL void set_state(CONNECTION_STATE state) { mConnectionState = state; if(state != CS_DISCONNECT) { if(dhmem_isblock()) { mNeedRecover = 1; return; } } switch(state) { case CS_DISCONNECT: start_resolve_dh_server(); break; case CS_GETINFO: case CS_REGISTER: case CS_POLL: { const sint8 cr = espconn_connect(&mDHConnector); if(cr == ESPCONN_ISCONN) return; if(cr != ESPCONN_OK) { dhesperrors_espconn_result("Connector espconn_connect failed:", cr); arm_repeat_timer(RETRY_CONNECTION_INTERVAL_MS); } break; } default: dhdebug("ASSERT: set_state wrong state %d", mConnectionState); } }
int ICACHE_FLASH_ATTR dhsender_queue_take(HTTP_REQUEST *out, unsigned int *is_notification) { if(mQueueTakePos < 0) return 0; ETS_INTR_LOCK(); DHSENDER_QUEUE item; os_memcpy(&item, &mQueue[mQueueTakePos], sizeof(DHSENDER_QUEUE)); if(mQueueTakePos >= mQueueMaxSize - 1) mQueueTakePos = 0; else mQueueTakePos++; if(mQueueAddPos == mQueueTakePos) mQueueTakePos = -1; mQueueSize--; ETS_INTR_UNLOCK(); if(mQueueSize < MEM_RECOVER_THRESHOLD && dhmem_isblock()) dhmem_unblock(); char buf[HTTP_REQUEST_MIN_ALLOWED_PAYLOAD]; if(dhsender_data_to_json(buf, sizeof(buf), item.notification_type == RNT_NOTIFICATION_GPIO, item.data_type, &item.data, item.data_len, item.pin) < 0) { snprintf(buf, sizeof(buf), "Failed to convert data to json"); item.type = RT_RESPONCE_ERROR; } *is_notification = 0; switch(item.type) { case RT_RESPONCE_OK: case RT_RESPONCE_ERROR: dhrequest_create_update(out, item.id, (item.type == RT_RESPONCE_OK) ? STATUS_OK : STATUS_ERROR, buf); break; case RT_NOTIFICATION: *is_notification = 1; switch(item.notification_type) { case RNT_NOTIFICATION_GPIO: dhrequest_create_notification(out, "gpio/int", buf); break; case RNT_NOTIFICATION_ADC: dhrequest_create_notification(out, "adc/int", buf); break; case RNT_NOTIFICATION_UART: dhrequest_create_notification(out, "uart/int", buf); break; case RNT_NOTIFICATION_ONEWIRE: dhrequest_create_notification(out, "onewire/master/int", buf); break; default: dhdebug("ERROR: Unknown notification type of request %d", item.notification_type); return 0; } break; default: dhdebug("ERROR: Unknown type of request %d", item.type); return 0; } return 1; }