4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
8 * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9 * Nikita Kalyazin <n.kalyazin@samsung.com>
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 * - Samsung RnD Institute Russia
38 #include "transfer_thread.h"
42 static void transfer_thread_cleanup(void *arg)
45 fd_pipe = (int *) arg;
52 static void *transfer_thread(void *arg)
59 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
63 //set cleanup function
64 pthread_cleanup_push(transfer_thread_cleanup, (void *)fd_pipe);
66 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
68 LOGI("transfer thread started\n");
71 nrd = splice(manager.buf_fd, NULL,
75 if (errno == EAGAIN) {
76 LOGI("No more data to read\n");
79 LOGE("Cannot splice read: %s\n", strerror(errno));
83 nwr = splice(fd_pipe[0], NULL,
84 manager.host.data_socket, NULL,
88 LOGE("Cannot splice write: %s\n", strerror(errno));
92 LOGW("nrd - nwr = %d\n", nrd - nwr);
97 pthread_cleanup_pop(1);
99 LOGI("transfer thread finished. return\n");
108 if (manager.host.data_socket == -1) {
109 LOGW("won't start transfer thread: data socket isn't open\n");
113 if (manager.transfer_thread != -1) { // already started
114 LOGW("transfer already running\n");
118 saved_flags = fcntl(manager.buf_fd, F_GETFL);
119 fcntl(manager.buf_fd, F_SETFL, saved_flags & ~O_NONBLOCK);
121 if(pthread_create(&(manager.transfer_thread),
126 LOGE("Failed to create transfer thread\n");
138 if (manager.transfer_thread == -1) {
139 LOGI("transfer thread not running\n");
142 LOGI("stopping transfer\n");
145 saved_flags = fcntl(manager.buf_fd, F_GETFL);
146 fcntl(manager.buf_fd, F_SETFL, saved_flags | O_NONBLOCK);
149 LOGI("joining thread...\n");
150 ret = pthread_join(manager.transfer_thread, NULL);
152 LOGW("pthread_join: unknown error %d\n", ret);
154 manager.transfer_thread = -1;
156 LOGI("transfer thread stoped\n");