embox
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
21static int java_pipe[2];22
23static struct mutex java_global_mutex;24
25javacall_result javacall_event_send(unsigned char* binaryBuffer,26int binaryBufferLen) {27mutex_lock(&java_global_mutex);28{29/* blocking writing */30write(java_pipe[1], &binaryBufferLen, 4);31write(java_pipe[1], binaryBuffer, binaryBufferLen);32}33mutex_unlock(&java_global_mutex);34return JAVACALL_OK;35}
36
37javacall_result javacall_event_receive(38long timeToWaitInMillisec, /* XXX unused*/39/*OUT*/ unsigned char* binaryBuffer,40int binaryBufferMaxLen,41/*OUT*/ int* outEventLen) {42size_t actual_size;43struct timeval timeout;44fd_set readfds;45javacall_result res = JAVACALL_OK;46int fd_cnt = 0;47
48FD_ZERO(&readfds);49FD_SET(java_pipe[0], &readfds);50
51timeout.tv_sec = timeToWaitInMillisec / 1000;52timeout.tv_usec = (timeToWaitInMillisec % 1000) * 1000;53
54fd_cnt = select(java_pipe[0] + 1, &readfds, NULL, NULL, &timeout);55
56if (fd_cnt != 1) {57res = JAVACALL_FAIL;58goto out;59}60
61read(java_pipe[0], &actual_size, 4);62if (actual_size > binaryBufferMaxLen) {63res = JAVACALL_OUT_OF_MEMORY;64actual_size = binaryBufferMaxLen;65}66*outEventLen = read(java_pipe[0], binaryBuffer, actual_size);67
68out:69return res;70}
71
72javacall_result javacall_create_event_queue_lock(void) {73return JAVACALL_OK;74}
75
76javacall_result javacall_wait_and_lock_event_queue(void) {77mutex_lock(&java_global_mutex);78return JAVACALL_OK;79}
80
81javacall_result javacall_unlock_event_queue(void) {82mutex_unlock(&java_global_mutex);83return JAVACALL_OK;84}
85
86javacall_result javacall_destroy_event_queue_lock(void) {87return JAVACALL_OK;88}
89
90javacall_result javacall_events_init(void) {91if (-1 == pipe(java_pipe)) {92return JAVACALL_FAIL;93}94mutex_init(&java_global_mutex);95return JAVACALL_OK;96}
97