kaa_error_t kaa_client_init_channel(kaa_client_t *kaa_client, kaa_client_channel_type_t channel_type) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Initializing channel...."); switch (channel_type) { case KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP: error_code = kaa_tcp_channel_create(&kaa_client->channel, kaa_client->kaa_context->logger, BOOTSTRAP_SERVICE, BOOTSTRAP_SERVICE_COUNT); break; case KAA_CLIENT_CHANNEL_TYPE_OPERATIONS: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->kaa_context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); break; } if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error initializing channel %d", channel_type); return error_code; } error_code = kaa_tcp_channel_set_keepalive_timeout(&kaa_client->channel, 120); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error set keepalive"); } error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager, &kaa_client->channel, &kaa_client->channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error register channel %d as transport", channel_type); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(type=%d,id=%08X) initialized successfully" , channel_type, kaa_client->channel_id); 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"); } else if (hostname_size > 0){ char *n_hostname = strndup(hostname, hostname_size); KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(type=%d,id=%08X) destination %s:%d", channel_type , kaa_client->channel_id, n_hostname, port); free(n_hostname); } return error_code; }
kaa_error_t kaa_client_init_channel(kaa_client_t *kaa_client, kaa_client_channel_type_t channel_type) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->context->logger, KAA_ERR_NONE, "Initializing channel...."); switch (channel_type) { case KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->context->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); break; case KAA_CLIENT_CHANNEL_TYPE_OPERATIONS: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); break; } if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to create transport channel, type %d", channel_type); return error_code; } error_code = kaa_tcp_channel_set_socket_events_callback(&kaa_client->channel, &on_kaa_tcp_channel_event, kaa_client); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to set socket events callback, channel type %d", channel_type); return error_code; } error_code = kaa_channel_manager_add_transport_channel(kaa_client->context->channel_manager , &kaa_client->channel , &kaa_client->channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to add transport channel, type %d", channel_type); return error_code; } KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] initialized successfully (type %d)" , kaa_client->channel_id, channel_type); return error_code; }
/* * Test Check bootstrap channel sync call. * 1. Set access point, check connect and authorize sending CONNECT and receiving CONNACK * 2. Receive Bootstrap Kaa Sync message. * 3. Send disconnect, check sending. * 4. Check socket close. * 5. Call sync * 6. Check connect and authorize sending CONNECT and receiving CONNACK * 7. Receive Bootstrap Kaa Sync message. * 8. Send disconnect, check sending. * 9. Check socket close. */ void test_bootstrap_sync_success(void) { KAA_TRACE_IN(logger); kaa_error_t error_code; kaa_transport_channel_interface_t *channel = NULL; channel = KAA_CALLOC(1,sizeof(kaa_transport_channel_interface_t)); kaa_service_t bootstrap_services[] = {KAA_SERVICE_BOOTSTRAP}; error_code = kaa_tcp_channel_create(channel,logger,bootstrap_services,1); ASSERT_EQUAL(error_code, KAA_ERR_NONE); //Set access point and imitate start of connection to destination test_set_access_point(channel); //Check standard bootstrap flow (connect, authenticate, sync receive and disconnect) test_check_bootstrap_sync(channel); //Imitate sync call channel->sync_handler(channel->context, bootstrap_services, 1); //Check standard bootstrap flow (connect, authenticate, sync receive and disconnect) test_check_bootstrap_sync(channel); channel->destroy(channel->context); KAA_TRACE_OUT(logger); KAA_FREE(channel); }
kaa_error_t kaa_client_init_operations_channel(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Start operations channel initialization"); error_code = kaa_tcp_channel_create(&kaa_client->operations_channel , kaa_client->kaa_context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Operations channel initialization failed"); return error_code; } KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Initializing Kaa SDK Operations channel added to transport channel manager"); error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager , &kaa_client->operations_channel , &kaa_client->operations_channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error during Kaa operations channel setting as transport"); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Operations channel initialized successfully"); print_mem_stat(kaa_client); return error_code; }
/* * Test IO error during operation. * 1. Set access point, check connect and authorize sending CONNECT and receiving CONNACK * 2. Imitate IO error on read * 3. Close socket, check bootstrap manager access point failure notification */ void test_set_access_point_io_error(void) { KAA_TRACE_IN(logger); kaa_error_t error_code; kaa_transport_channel_interface_t *channel = NULL; channel = KAA_CALLOC(1,sizeof(kaa_transport_channel_interface_t)); kaa_service_t bootstrap_services[] = {KAA_SERVICE_BOOTSTRAP}; error_code = kaa_tcp_channel_create(channel,logger,bootstrap_services,1); ASSERT_EQUAL(error_code, KAA_ERR_NONE); //Set access point and imitate start of connection to destination test_set_access_point(channel); //Imitate WR event, and wait socket connect call, channel should start authorization, prepare //CONNECT message error_code = kaa_tcp_channel_process_event(channel,FD_WRITE); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(access_point_test_info.socket_connected, true); ASSERT_EQUAL(access_point_test_info.socket_connected_callback, true); ASSERT_EQUAL(access_point_test_info.fill_connect_message, true); ASSERT_EQUAL(access_point_test_info.request_connect, true); //Check correct RD,WR operation, in this point we waiting for RD,WR operations true CHECK_SOCKET_RW(channel,true,true); //Imitate socket ready for writing, and writing CONNECT message error_code = kaa_tcp_channel_process_event(channel,FD_WRITE); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(access_point_test_info.auth_packet_written, true); //Check correct RD,WR operation, in this point we waiting for RD operations true //and WR false, no pending services and empty buffer. CHECK_SOCKET_RW(channel,true,false); //Imitate socket ready for reading, and got IO error during read access_point_test_info.socket_connecting_error_scenario = true; error_code = kaa_tcp_channel_process_event(channel,FD_READ); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(access_point_test_info.connack_read, false); ASSERT_EQUAL(access_point_test_info.socket_disconnected_callback, true); ASSERT_EQUAL(access_point_test_info.socket_disconnected_closed, true); ASSERT_EQUAL(access_point_test_info.bootstrap_manager_on_access_point_failed, true); channel->destroy(channel->context); KAA_TRACE_OUT(logger); KAA_FREE(channel); }
/* * Initializes Kaa SDK. */ kaa_error_t kaa_sdk_init() { kaa_error_t error_code = kaa_init(&kaa_context_); if (error_code) { printf("Error during kaa context creation %d", error_code); return error_code; } error_code = kaa_log_collector_init(); if (error_code) { KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to init Kaa log collector %d", error_code); return error_code; } KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Adding transport channels"); error_code = kaa_tcp_channel_create(&operations_channel , kaa_context_->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); KAA_RETURN_IF_ERR(error_code); error_code = kaa_tcp_channel_create(&bootstrap_channel , kaa_context_->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); KAA_RETURN_IF_ERR(error_code); error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager , &bootstrap_channel , NULL); KAA_RETURN_IF_ERR(error_code); error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager , &operations_channel , NULL); KAA_RETURN_IF_ERR(error_code); KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Kaa SDK started"); return KAA_ERR_NONE; }
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_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client starting ..."); print_mem_stat(kaa_client); kaa_error_t error_code = KAA_ERR_NONE; error_code = kaa_tcp_channel_create(&kaa_client->bootstrap_channel , kaa_client->kaa_context->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error during Kaa bootstrap channel creation"); return error_code; } error_code = kaa_tcp_channel_set_socket_events_callback(&kaa_client->bootstrap_channel, on_kaa_tcp_channel_event, (void*)kaa_client); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error setting callback bootstrap channel"); return error_code; } KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client - bootstrap channel initialized"); print_mem_stat(kaa_client); error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager , &kaa_client->bootstrap_channel , &kaa_client->bootstrap_channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error setting bootstrap channel setting as transport"); return error_code; } //Push running thread sndc_sem_post(&kaa_client->start_semophore); KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client started"); return KAA_ERR_NONE; }
/* * Test create and destroy bootstrap channel */ void test_create_kaa_tcp_channel(void) { KAA_TRACE_IN(logger); kaa_error_t error_code; kaa_transport_channel_interface_t *channel = NULL; channel = KAA_CALLOC(1,sizeof(kaa_transport_channel_interface_t)); kaa_service_t bootstrap_services[] = {KAA_SERVICE_BOOTSTRAP}; error_code = kaa_tcp_channel_create(channel,logger,bootstrap_services,1); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_NOT_NULL(channel->context); ASSERT_NOT_NULL(channel->get_protocol_id); ASSERT_NOT_NULL(channel->get_supported_services); ASSERT_NOT_NULL(channel->init); ASSERT_NOT_NULL(channel->destroy); ASSERT_NOT_NULL(channel->set_access_point); ASSERT_NOT_NULL(channel->sync_handler); kaa_transport_protocol_id_t protocol_info; error_code = channel->get_protocol_id(channel->context,&protocol_info); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(protocol_info.id, 0x56c8ff92); ASSERT_EQUAL(protocol_info.version, 1); kaa_service_t *r_supported_services; size_t r_supported_service_count = 0; error_code = channel->get_supported_services(channel->context,&r_supported_services,&r_supported_service_count); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(r_supported_service_count, 1); ASSERT_EQUAL(r_supported_services[0], KAA_SERVICE_BOOTSTRAP); channel->destroy(channel->context); KAA_TRACE_OUT(logger); KAA_FREE(channel); }
/* * Test connecting error during set access point. */ void test_set_access_point_connecting_error(void) { KAA_TRACE_IN(logger); kaa_error_t error_code; kaa_transport_channel_interface_t *channel = NULL; channel = KAA_CALLOC(1,sizeof(kaa_transport_channel_interface_t)); kaa_service_t bootstrap_services[] = {KAA_SERVICE_BOOTSTRAP}; error_code = kaa_tcp_channel_create(channel,logger,bootstrap_services,1); ASSERT_EQUAL(error_code, KAA_ERR_NONE); //Set access point and imitate start of connection to destination test_set_access_point(channel); //Imitate WR event, and wait socket connect call and return socket connection error access_point_test_info.socket_connecting_error_scenario = true; error_code = kaa_tcp_channel_process_event(channel,FD_WRITE); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(access_point_test_info.socket_connected_callback, false); ASSERT_EQUAL(access_point_test_info.fill_connect_message, false); ASSERT_EQUAL(access_point_test_info.request_connect, false); ASSERT_EQUAL(access_point_test_info.socket_connecting_error_callback, true); ASSERT_EQUAL(access_point_test_info.socket_disconnected_closed, true); ASSERT_EQUAL(access_point_test_info.bootstrap_manager_on_access_point_failed, true); channel->destroy(channel->context); KAA_TRACE_OUT(logger); KAA_FREE(channel); }