embox

Форк
0
/
javacall_midp_events.c 
96 строк · 2.2 Кб
1
/**
2
 * @file
3
 * @brief
4
 *
5
 * @date 14.01.13
6
 * @author Alexander Kalmuk
7
 */
8

9
#include <javacall_events.h>
10
#include <sys/select.h>
11
#include <sys/time.h>
12
#include <unistd.h>
13
#include <kernel/thread/sync/mutex.h>
14

15
/**
16
 * Note:
17
 * We made send/recv operations throw pipe between kernel task and java task.
18
 * That means any another tasks could not send/recv events.
19
 * */
20

21
static int java_pipe[2];
22

23
static struct mutex java_global_mutex;
24

25
javacall_result javacall_event_send(unsigned char* binaryBuffer,
26
                                    int binaryBufferLen) {
27
	mutex_lock(&java_global_mutex);
28
	{
29
		/* blocking writing */
30
		write(java_pipe[1], &binaryBufferLen, 4);
31
		write(java_pipe[1], binaryBuffer, binaryBufferLen);
32
	}
33
	mutex_unlock(&java_global_mutex);
34
	return JAVACALL_OK;
35
}
36

37
javacall_result javacall_event_receive(
38
                            long                    timeToWaitInMillisec, /* XXX unused*/
39
                            /*OUT*/ unsigned char*  binaryBuffer,
40
                            int                     binaryBufferMaxLen,
41
                            /*OUT*/ int*            outEventLen) {
42
	size_t actual_size;
43
	struct timeval timeout;
44
	fd_set readfds;
45
	javacall_result res = JAVACALL_OK;
46
	int fd_cnt = 0;
47

48
	FD_ZERO(&readfds);
49
	FD_SET(java_pipe[0], &readfds);
50

51
	timeout.tv_sec = timeToWaitInMillisec / 1000;
52
	timeout.tv_usec = (timeToWaitInMillisec % 1000) * 1000;
53

54
	fd_cnt = select(java_pipe[0] + 1, &readfds, NULL, NULL, &timeout);
55

56
	if (fd_cnt != 1) {
57
		res = JAVACALL_FAIL;
58
		goto out;
59
	}
60

61
	read(java_pipe[0], &actual_size, 4);
62
	if (actual_size > binaryBufferMaxLen) {
63
		res = JAVACALL_OUT_OF_MEMORY;
64
		actual_size = binaryBufferMaxLen;
65
	}
66
	*outEventLen = read(java_pipe[0], binaryBuffer, actual_size);
67

68
out:
69
	return res;
70
}
71

72
javacall_result javacall_create_event_queue_lock(void) {
73
	return JAVACALL_OK;
74
}
75

76
javacall_result javacall_wait_and_lock_event_queue(void) {
77
	mutex_lock(&java_global_mutex);
78
	return JAVACALL_OK;
79
}
80

81
javacall_result javacall_unlock_event_queue(void) {
82
	mutex_unlock(&java_global_mutex);
83
	return JAVACALL_OK;
84
}
85

86
javacall_result javacall_destroy_event_queue_lock(void) {
87
	return JAVACALL_OK;
88
}
89

90
javacall_result javacall_events_init(void) {
91
	if (-1 == pipe(java_pipe)) {
92
		return JAVACALL_FAIL;
93
	}
94
	mutex_init(&java_global_mutex);
95
	return JAVACALL_OK;
96
}
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.