ext_tcp_socket_io_errors_t ext_tcp_utils_tcp_socket_read(kaa_fd_t fd, char *buffer, size_t buffer_size, size_t *bytes_read) { KAA_RETURN_IF_NIL(buffer, KAA_TCP_SOCK_IO_ERROR); if (fd != access_point_test_info.fd) { return KAA_TCP_SOCK_IO_ERROR; } if (access_point_test_info.socket_connecting_error_scenario) { *bytes_read = 0; return KAA_TCP_SOCK_IO_ERROR; } else if (!access_point_test_info.connack_read) { memcpy(buffer,CONNACK,sizeof(CONNACK)); *bytes_read = sizeof(CONNACK); access_point_test_info.connack_read = true; } else if (!access_point_test_info.kaasync_read) { memcpy(buffer,KAASYNC_BOOTSTRAP,sizeof(KAASYNC_BOOTSTRAP)); *bytes_read = sizeof(KAASYNC_BOOTSTRAP); access_point_test_info.kaasync_read = true; } return KAA_TCP_SOCK_IO_OK; }
kaa_error_t kaa_buffer_reallocate_space(kaa_buffer_t *buffer_p, size_t size) { KAA_RETURN_IF_NIL(buffer_p, KAA_ERR_BADPARAM); size_t locked_space = buffer_p->current - buffer_p->begin; size_t free_space = buffer_p->end - buffer_p->current; size_t total_space = buffer_p->end - buffer_p->begin; size_t new_buffer_size = size - free_space + locked_space; char *ptr; if (total_space >= new_buffer_size) return KAA_ERR_BADPARAM; ptr = KAA_REALLOC(buffer_p->begin, new_buffer_size); if (ptr) { buffer_p->begin = ptr; buffer_p->end = buffer_p->begin + new_buffer_size; buffer_p->current = buffer_p->begin + locked_space; return KAA_ERR_NONE; } return KAA_ERR_NOMEM; }
static kaa_error_t kaa_context_destroy(kaa_context_t *context) { KAA_RETURN_IF_NIL(context, KAA_ERR_BADPARAM); kaa_user_manager_destroy(context->user_manager); #ifndef KAA_DISABLE_FEATURE_EVENTS kaa_event_manager_destroy(context->event_manager); #endif kaa_profile_manager_destroy(context->profile_manager); kaa_bootstrap_manager_destroy(context->bootstrap_manager); kaa_channel_manager_destroy(context->channel_manager); kaa_status_destroy(context->status->status_instance); KAA_FREE(context->status); #ifndef KAA_DISABLE_FEATURE_LOGGING kaa_log_collector_destroy(context->log_collector); #endif #ifndef KAA_DISABLE_FEATURE_CONFIGURATION kaa_configuration_manager_destroy(context->configuration_manager); #endif kaa_platform_protocol_destroy(context->platfrom_protocol); KAA_FREE(context); return KAA_ERR_NONE; }
kaa_error_t kaa_client_process_channel_disconnected(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; esp8266_error_t esp_error = ESP8266_ERR_NONE; char *hostname = NULL; size_t hostname_size = 0; uint16_t port = 0; error_code = kaa_tcp_channel_get_access_point(&kaa_client->channel, &hostname, &hostname_size, &port); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel get access point failed"); kaa_client_channel_error(kaa_client); } if (hostname && hostname_size && port) { esp_error = esp8266_connect_tcp(kaa_client->controler, hostname, hostname_size, port, &kaa_client->channel_fd); if (esp_error) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, KAA_ERR_NONE, "ESP8266 connect tcp failed, code: %d", esp_error); kaa_client_channel_error(kaa_client); //Need to reinitialize WiFi connection and ESP kaa_client->connection_state = KAA_CLIENT_ESP8266_STATE_UNINITED; } else { KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(0x%08X) connected to port %d, fd=%d", kaa_client->channel_id, port, kaa_client->channel_fd); kaa_client->channel_state = KAA_CLIENT_CHANNEL_STATE_CONNECTED; error_code = kaa_tcp_channel_connected(&kaa_client->channel); } } return error_code; }
kaa_error_t kaa_init(kaa_context_t **kaa_context_p) { KAA_RETURN_IF_NIL(kaa_context_p, KAA_ERR_BADPARAM); // Initialize logger kaa_logger_t *logger = NULL; kaa_error_t error = kaa_log_create(&logger, KAA_MAX_LOG_MESSAGE_LENGTH, KAA_MAX_LOG_LEVEL, NULL); // TODO: make log destination configurable if (error) { return error; } KAA_LOG_INFO(logger, KAA_ERR_NONE, "Kaa SDK version %s, commit hash %s", KAA_BUILD_VERSION, KAA_BUILD_COMMIT_HASH); // Initialize general Kaa context error = kaa_context_create(kaa_context_p, logger); if (error) { KAA_LOG_FATAL(logger, error, "Failed to create Kaa context"); *kaa_context_p = NULL; return error; } // Initialize endpoint identity uint8_t *sha1 = NULL; size_t sha1_size = 0; error = kaa_init_keys(); if (error) { KAA_LOG_ERROR(logger, error, "Failed to initialize keys"); return error; } ext_get_sha1_public(&sha1, &sha1_size); ext_copy_sha_hash((*kaa_context_p)->status->status_instance->endpoint_public_key_hash, sha1); return kaa_status_set_updated((*kaa_context_p)->status->status_instance, true); }
kaa_error_t kaa_stop(kaa_context_t *kaa_context) { KAA_RETURN_IF_NIL(kaa_context, KAA_ERR_BADPARAM); return kaa_status_save(kaa_context->status->status_instance); }
kaa_error_t kaa_client_start(kaa_client_t *kaa_client , external_process_fn external_process , void *external_process_context , kaa_time_t max_delay) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; esp8266_error_t esp_error = ESP8266_ERR_NONE; kaa_client->external_process_fn = external_process; kaa_client->external_process_context = external_process_context; kaa_client->external_process_max_delay = max_delay; kaa_client->external_process_last_call = get_sys_milis(); KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Starting Kaa client..."); const kaa_configuration_root_receiver_t config_receiver = { kaa_client, configuration_update }; error_code = kaa_configuration_manager_set_root_receiver(kaa_client->kaa_context->configuration_manager, &config_receiver); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error registering Configuration root receiver"); return error_code; } const kaa_root_configuration_t *config = kaa_configuration_manager_get_configuration(kaa_client->kaa_context->configuration_manager); if (config) { configuration_update(kaa_client, config); } uint8 *buffer = NULL; size_t buffer_size = 0; int send_c = 0; int tmp; while (kaa_client->operate) { if ((get_sys_milis() - kaa_client->external_process_last_call) >= kaa_client->external_process_max_delay) { if (kaa_client->external_process_fn) { kaa_client->external_process_fn(kaa_client->external_process_context); } kaa_client->external_process_last_call = get_sys_milis(); } if (kaa_client->connection_state == KAA_CLIENT_WIFI_STATE_CONNECTED) { esp_error = esp8266_process(kaa_client->controler, kaa_client->external_process_max_delay); if (esp_error) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, KAA_ERR_NONE, "ESP8266 process failed code: %d", esp_error); kaa_client_esp8266_error(kaa_client); } //Check Kaa channel is ready to transmit something if (kaa_client->channel_id > 0) { if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_NOT_CONNECTED) { error_code = kaa_client_process_channel_disconnected(kaa_client); } else if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_CONNECTED) { error_code = kaa_client_process_channel_connected(kaa_client); } } else { //No initialized channels if (kaa_client->boostrap_complete) { KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(0x%08X) Boostrap complete, reinitializing to Operations ...", kaa_client->channel_id); kaa_client->boostrap_complete = false; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_OPERATIONS); } else { KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(0x%08X) Operations error, reinitializing to Bootstrap ...", kaa_client->channel_id); kaa_client->boostrap_complete = true; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP); } } } else { kaa_client_state_process(kaa_client); } } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client Stopped"); return error_code; }
size_t ext_log_storage_get_records_count(const void *context) { KAA_RETURN_IF_NIL(context, 0); return ((test_log_storage_context_t *)context)->record_count; }
kaa_error_t kaa_client_process_channel_connected(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); fd_set read_fds, write_fds, except_fds; struct timeval select_tv = { get_poll_timeout(kaa_client), 0 }; int channel_fd = 0; FD_ZERO(&read_fds); FD_ZERO(&write_fds); FD_ZERO(&except_fds); kaa_error_t error_code = kaa_tcp_channel_get_descriptor(&kaa_client->channel, &channel_fd); if(error_code) KAA_LOG_ERROR(kaa_client->context->logger, error_code, "No descriptor provided!"); if (kaa_tcp_channel_is_ready(&kaa_client->channel, FD_READ)) FD_SET(channel_fd, &read_fds); if (kaa_tcp_channel_is_ready(&kaa_client->channel, FD_WRITE)) FD_SET(channel_fd, &write_fds); int poll_result = select(channel_fd + 1, &read_fds, &write_fds, NULL, &select_tv); if (poll_result == 0) { error_code = kaa_tcp_channel_check_keepalive(&kaa_client->channel); } else if (poll_result > 0) { if (channel_fd >= 0) { if (FD_ISSET(channel_fd, &read_fds)) { KAA_LOG_DEBUG(kaa_client->context->logger, KAA_ERR_NONE, "Processing IN event for the client socket %d", channel_fd); error_code = kaa_tcp_channel_process_event(&kaa_client->channel, FD_READ); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to process IN event for the client socket %d", channel_fd); } } if (FD_ISSET(channel_fd, &write_fds)) { KAA_LOG_DEBUG(kaa_client->context->logger, KAA_ERR_NONE, "Processing OUT event for the client socket %d", channel_fd); error_code = kaa_tcp_channel_process_event(&kaa_client->channel, FD_WRITE); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to process OUT event for the client socket %d", channel_fd); } } } } else { KAA_LOG_ERROR(kaa_client->context->logger, KAA_ERR_BAD_STATE, "Failed to poll descriptors"); error_code = KAA_ERR_BAD_STATE; } if (kaa_client->channel_socket_closed) { KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] connection terminated", kaa_client->channel_id); kaa_client->channel_state = KAA_CLIENT_CHANNEL_STATE_NOT_CONNECTED; if (error_code != KAA_ERR_EVENT_NOT_ATTACHED) { kaa_client_deinit_channel(kaa_client); } } return error_code; }
kaa_error_t kaa_client_create(kaa_client_t **kaa_client, kaa_client_props_t *props) { KAA_RETURN_IF_NIL2(kaa_client, props, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; error_code = kaa_init_security_stuff(); KAA_RETURN_IF_ERR(error_code); kaa_client_t *self = sndc_mem_calloc(1,sizeof(kaa_client_t)); KAA_RETURN_IF_NIL(self,KAA_ERR_NOMEM); self->thread_name = sndc_mem_strdup("Kaa-Client-Thread"); self->operate = true; self->max_update_time = props->max_update_time; print_mem_stat(self); sndc_printf("Initializing Kaa SDK...\n"); sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND); error_code = kaa_init(&self->kaa_context); if (error_code) { sndc_printf("Error during Kaa context creation %d\n", error_code); sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND); goto error; } KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa framework initialized."); print_mem_stat(self); error_code = kaa_log_collector_init(self); if (error_code) { sndc_printf("Failed to init Kaa log collector %d\n", error_code); sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND); goto error; } KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa log collector initialized."); *kaa_client = self; /* initialize semaphore */ sndc_sem_init(&self->start_semophore, 0); sndc_sem_init(&self->logging_semophore, 0); int status = sndc_thrd_create( self->thread_name, thread_run_fn, (uintptr_t)self, SNDC_THRD_PRIORITY_DEFAULT, THREAD_STACKSIZE_DMR_START); //Defined 4K-8 bytes of stack switch (status) { case 0: break; case -EINVAL: error_code = KAA_ERR_BADPARAM; break; case -ENOMEM: error_code = KAA_ERR_NOMEM; break; default: error_code = KAA_ERR_BADDATA; break; } return error_code; error: sndc_printf("Kaa initialization failed. error_code %d\n", error_code); sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND); kaa_client_destroy(self); return error_code; }
kaatcp_error_t kaatcp_fill_disconnect_message(kaatcp_disconnect_reason_t return_code, kaatcp_disconnect_t *message) { KAA_RETURN_IF_NIL(message, KAATCP_ERR_BAD_PARAM); message->reason = return_code; return KAATCP_ERR_NONE; }
static kaatcp_error_t kaatcp_parser_message_done(kaatcp_parser_t *parser) { KAA_RETURN_IF_NIL(parser, KAATCP_ERR_BAD_PARAM); switch (parser->message_type) { case KAATCP_MESSAGE_CONNACK: if (parser->handlers.connack_handler) { kaatcp_connack_t connack = { *(parser->payload + 1) }; parser->handlers.connack_handler(parser->handlers.handlers_context, connack); } break; case KAATCP_MESSAGE_DISCONNECT: if (parser->handlers.disconnect_handler) { kaatcp_disconnect_t disconnect = { *(parser->payload + 1) }; parser->handlers.disconnect_handler(parser->handlers.handlers_context, disconnect); } break; case KAATCP_MESSAGE_PINGRESP: if (parser->handlers.pingresp_handler) { parser->handlers.pingresp_handler(parser->handlers.handlers_context); } break; case KAATCP_MESSAGE_KAASYNC: { kaatcp_kaasync_header_t sync_header; const char *cursor = parser->payload; sync_header.protocol_name_length = KAA_NTOHS(*((uint16_t *) cursor)); if (sync_header.protocol_name_length > KAATCP_PROTOCOL_NAME_MAX_SIZE) { return KAATCP_ERR_INVALID_PROTOCOL; } cursor += sizeof(uint16_t); if (memcmp(cursor, KAA_TCP_NAME, KAA_TCP_NAME_LENGTH)) { return KAATCP_ERR_INVALID_PROTOCOL; } memcpy(sync_header.protocol_name, cursor, sync_header.protocol_name_length); sync_header.protocol_name[sync_header.protocol_name_length] = '\0'; cursor += sync_header.protocol_name_length; sync_header.protocol_version = *(cursor++); if (sync_header.protocol_version != PROTOCOL_VERSION) { return KAATCP_ERR_INVALID_PROTOCOL; } uint16_t msg_id; memcpy(&msg_id, cursor, sizeof(uint16_t)); cursor += sizeof(uint16_t); sync_header.message_id = KAA_NTOHS(msg_id); sync_header.flags = *(cursor++); if ((sync_header.flags & KAA_SYNC_SYNC_BIT) && parser->handlers.kaasync_handler) { kaatcp_kaasync_t *kaasync = (kaatcp_kaasync_t *) KAA_MALLOC(sizeof(kaatcp_kaasync_t)); KAA_RETURN_IF_NIL(kaasync, KAATCP_ERR_NOMEM); kaasync->sync_header = sync_header; kaasync->sync_request_size = parser->message_length - KAA_SYNC_HEADER_LENGTH; if (kaasync->sync_request_size) { kaasync->sync_request = (char *) KAA_MALLOC(kaasync->sync_request_size); if (!kaasync->sync_request) { KAA_FREE(kaasync); return KAATCP_ERR_NOMEM; } memcpy(kaasync->sync_request, cursor, kaasync->sync_request_size); } else { kaasync->sync_request = NULL; } parser->handlers.kaasync_handler(parser->handlers.handlers_context, kaasync); } break; } default: break; } return kaatcp_parser_reset(parser); }
static kaa_error_t kaa_context_create(kaa_context_t **context_p, kaa_logger_t *logger) { KAA_RETURN_IF_NIL2(context_p, logger, KAA_ERR_BADPARAM); *context_p = (kaa_context_t *) KAA_MALLOC(sizeof(kaa_context_t)); KAA_RETURN_IF_NIL(*context_p, KAA_ERR_NOMEM); (*context_p)->logger = logger; kaa_error_t error = KAA_ERR_NONE; (*context_p)->status = (kaa_status_holder_t *) KAA_MALLOC(sizeof(kaa_status_holder_t)); if (!(*context_p)->status) error = KAA_ERR_NOMEM; if (!error) error = kaa_status_create(&((*context_p)->status->status_instance)); if (!error) error = kaa_platform_protocol_create(&((*context_p)->platfrom_protocol), *context_p, (*context_p)->status->status_instance); if (!error) error = kaa_channel_manager_create(&((*context_p)->channel_manager), (*context_p)); if (!error) error = kaa_bootstrap_manager_create(&((*context_p)->bootstrap_manager), (*context_p)->channel_manager, (*context_p)->logger); if (!error) error = kaa_profile_manager_create(&((*context_p)->profile_manager), (*context_p)->status->status_instance, (*context_p)->channel_manager, (*context_p)->logger); #ifndef KAA_DISABLE_FEATURE_EVENTS if (!error) error = kaa_event_manager_create(&((*context_p)->event_manager), (*context_p)->status->status_instance, (*context_p)->channel_manager, (*context_p)->logger); #else (*context_p)->event_manager = NULL; #endif #ifndef KAA_DISABLE_FEATURE_LOGGING if (!error) error = kaa_log_collector_create(&((*context_p)->log_collector), (*context_p)->status->status_instance, (*context_p)->channel_manager, (*context_p)->logger); #else (*context_p)->log_collector = NULL; #endif #ifndef KAA_DISABLE_FEATURE_CONFIGURATION if (!error) error = kaa_configuration_manager_create(&((*context_p)->configuration_manager), (*context_p)->channel_manager, (*context_p)->status->status_instance, (*context_p)->logger); #else (*context_p)->configuration_manager = NULL; #endif if (!error) error = kaa_user_manager_create(&((*context_p)->user_manager), (*context_p)->status->status_instance, (*context_p)->channel_manager, (*context_p)->logger); if (error) { kaa_context_destroy(*context_p); *context_p = NULL; } return error; }
static kaa_error_t kaa_context_create(kaa_context_t **context_p, kaa_logger_t *logger) { KAA_RETURN_IF_NIL2(context_p, logger, KAA_ERR_BADPARAM); kaa_context_t *context = KAA_MALLOC(sizeof(*context)); KAA_RETURN_IF_NIL(context, KAA_ERR_NOMEM); context->logger = logger; kaa_error_t error = KAA_ERR_NONE; context->status = KAA_MALLOC(sizeof(*context->status)); if (!context->status) error = KAA_ERR_NOMEM; if (!error) error = kaa_status_create(&context->status->status_instance); if (!error) error = kaa_platform_protocol_create(&context->platform_protocol, context, context->status->status_instance); if (!error) error = kaa_channel_manager_create(&context->channel_manager, context); if (!error) error = kaa_bootstrap_manager_create(&context->bootstrap_manager, context); if (!error) error = kaa_profile_manager_create(&context->profile_manager, context->status->status_instance, context->channel_manager, context->logger); if (!error) error = kaa_failover_strategy_create(&context->failover_strategy, logger); #ifndef KAA_DISABLE_FEATURE_EVENTS if (!error) error = kaa_event_manager_create(&context->event_manager, context->status->status_instance, context->channel_manager, context->logger); #else context->event_manager = NULL; #endif #ifndef KAA_DISABLE_FEATURE_LOGGING if (!error) error = kaa_log_collector_create(&context->log_collector, context->status->status_instance, context->channel_manager, context->logger); #else context->log_collector = NULL; #endif #ifndef KAA_DISABLE_FEATURE_CONFIGURATION if (!error) error = kaa_configuration_manager_create(&context->configuration_manager, context->channel_manager, context->status->status_instance, context->logger); #else context->configuration_manager = NULL; #endif #ifndef KAA_DISABLE_FEATURE_NOTIFICATION if (!error) error = kaa_notification_manager_create(&context->notification_manager, context->status->status_instance, context->channel_manager, context->logger); #else context->notification_manager = NULL; #endif if (!error) error = kaa_user_manager_create(&context->user_manager, context->status->status_instance, context->channel_manager, context->logger); if (error) { kaa_context_destroy(context); *context_p = NULL; } else { *context_p = context; } return error; }
static kaa_error_t remove_request(kaa_log_collector_t *self, uint16_t bucket_id) { KAA_RETURN_IF_NIL(self, KAA_ERR_BADPARAM); kaa_list_remove_first(self->timeouts, &find_by_bucket_id, &bucket_id, NULL); return KAA_ERR_NONE; }
bool kaa_process_failover(kaa_context_t *kaa_context) { KAA_RETURN_IF_NIL(kaa_context, false); return kaa_bootstrap_manager_process_failover(kaa_context->bootstrap_manager); }
kaa_error_t kaa_context_set_status_registered(kaa_context_t *kaa_context, bool is_registered) { KAA_RETURN_IF_NIL(kaa_context, KAA_ERR_BADPARAM); return kaa_status_set_registered(kaa_context->status->status_instance, is_registered); }
kaa_error_t kaa_client_stop(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_client->operate = false; return KAA_ERR_NONE; }
kaa_error_t kaa_buffer_reset(kaa_buffer_t *buffer_p) { KAA_RETURN_IF_NIL(buffer_p, KAA_ERR_BADPARAM); buffer_p->current = buffer_p->begin; return KAA_ERR_NONE; }
kaa_error_t configuration_update(void *context, const kaa_root_configuration_t *configuration) { KAA_RETURN_IF_NIL(context, KAA_ERR_BADPARAM); KAA_LOG_INFO(((kaa_client_t *)context)->kaa_context->logger, KAA_ERR_NONE, "New configuration received"); return KAA_ERR_NONE; }
kaa_context_t *kaa_client_get_context(kaa_client_t *client) { KAA_RETURN_IF_NIL(client, NULL); return client->context; }
kaa_error_t kaa_client_process_channel_connected(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; esp8266_error_t esp_error = ESP8266_ERR_NONE; error_code = kaa_tcp_channel_check_keepalive(&kaa_client->channel); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel error check keepalive"); } int tmp = (get_sys_milis() / kaa_client->blink_timeout) % 2; if (kaa_client->blink_prev != (uint8)tmp) { kaa_client->blink_prev = (uint8)tmp; if (kaa_client->blink_prev) { ledOn(); if (kaa_client->boostrap_complete) { KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Process Bootstrap channel, state connected"); } else { KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Process Operation channel, state connected"); } } else ledOff(); } uint8 * buffer = NULL; size_t buffer_size = 0; error_code = kaa_tcp_channel_get_buffer_for_send(&kaa_client->channel, &buffer, &buffer_size); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel get buffer for send failed"); kaa_client_channel_error(kaa_client); } if (buffer_size > 0) { esp_error = esp8266_send_tcp(kaa_client->controler, kaa_client->channel_fd, buffer, buffer_size); if (esp_error) { if (esp_error != ESP8266_ERR_COMMAND_BUSY) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, KAA_ERR_NONE, "ESP8266 send tcp failed, code: %d", esp_error); kaa_client_channel_error(kaa_client); } } else { KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(0x%08X) sending %lu bytes", kaa_client->channel_id, buffer_size); error_code = kaa_tcp_channel_free_send_buffer(&kaa_client->channel, buffer_size); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel error free buffer"); } } } if (kaa_tcp_channel_connection_is_ready_to_terminate(&kaa_client->channel)) { esp_error = esp8266_disconnect_tcp(kaa_client->controler, kaa_client->channel_fd); if (esp_error) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, KAA_ERR_NONE, "ESP8266 send tcp failed, code: %d", esp_error); } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(0x%08X) connection terminated", kaa_client->channel_id); error_code = kaa_client_channel_error(kaa_client); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel error"); } } return error_code; }
kaa_error_t kaa_client_start(kaa_client_t *kaa_client, external_process_fn external_process, void *external_process_context, kaa_time_t max_delay) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = kaa_check_readiness(kaa_client->context); if (error_code != KAA_ERR_NONE) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Cannot start Kaa client: Kaa context is not fully initialized"); return error_code; } kaa_client->external_process = external_process; kaa_client->external_process_context = external_process_context; kaa_client->external_process_max_delay = max_delay; kaa_client->external_process_last_call = KAA_TIME(); KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Starting Kaa client..."); while (kaa_client->operate) { if (kaa_client->external_process) { if (KAA_TIME() - kaa_client->external_process_last_call >= (kaa_time_t)kaa_client->external_process_max_delay) { kaa_client->external_process(kaa_client->external_process_context); kaa_client->external_process_last_call = KAA_TIME(); } } if (kaa_process_failover(kaa_client->context)) { kaa_client->bootstrap_complete = false; } else { if(kaa_client->channel_id>0) { if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_NOT_CONNECTED) { error_code = kaa_client_process_channel_disconnected(kaa_client); } else if (kaa_client->channel_state == KAA_CLIENT_CHANNEL_STATE_CONNECTED) { error_code = kaa_client_process_channel_connected(kaa_client); if (error_code == KAA_ERR_TIMEOUT) kaa_client_deinit_channel(kaa_client); } } else { //No initialized channels if (kaa_client->bootstrap_complete) { KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] Boostrap complete, reinitializing to Operations ...", kaa_client->channel_id); kaa_client->bootstrap_complete = false; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_OPERATIONS); if (error_code == KAA_ERR_BAD_STATE) { kaa_client_deinit_channel(kaa_client); kaa_client->bootstrap_complete = false; } } else { KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] Operations error, reinitializing to Bootstrap ...", kaa_client->channel_id); kaa_client->bootstrap_complete = true; kaa_client_deinit_channel(kaa_client); kaa_client_init_channel(kaa_client, KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP); } } } #ifndef KAA_DISABLE_FEATURE_LOGGING ext_log_upload_timeout(kaa_client->context->log_collector); #endif } KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Kaa client stopped"); return error_code; }
size_t ext_log_storage_get_total_size(const void *context) { KAA_RETURN_IF_NIL(context, 0); return ((test_log_storage_context_t *)context)->total_size; }