POK
bufferreceive.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
17 #include <core/dependencies.h>
18 
19 #ifdef POK_NEEDS_MIDDLEWARE
20 #ifdef POK_NEEDS_BUFFERS
21 
22 #include <errno.h>
23 #include <types.h>
24 #include <core/time.h>
25 #include <core/event.h>
26 #include <libc/string.h>
27 #include <middleware/buffer.h>
28 
29 extern pok_buffer_t pok_buffers[POK_CONFIG_NB_BUFFERS];
30 extern char pok_buffers_data[1024];
31 
32 pok_ret_t pok_buffer_receive (const pok_buffer_id_t id,
33  const uint64_t timeout,
34  void* data,
35  pok_port_size_t* len)
36 {
37  pok_ret_t ret;
38 
39  if (id > POK_CONFIG_NB_BUFFERS)
40  {
41  return POK_ERRNO_EINVAL;
42  }
43 
44  if (pok_buffers[id].ready == FALSE)
45  {
46  return POK_ERRNO_EINVAL;
47  }
48 
49  if (data == NULL)
50  {
51  return POK_ERRNO_EINVAL;
52  }
53 
54  pok_event_lock (pok_buffers[id].lock);
55 
56  while (pok_buffers[id].empty == TRUE)
57  {
58  if (timeout == 0)
59  {
60  pok_event_unlock (pok_buffers[id].lock);
61  }
62  else
63  {
64  ret = pok_event_wait (pok_buffers[id].lock, timeout);
65  if (ret != POK_ERRNO_OK)
66  {
67  pok_event_unlock (pok_buffers[id].lock);
68  return ret;
69  }
70  }
71  }
72 
73  memcpy (data, &pok_buffers_data[pok_buffers[id].index + pok_buffers[id].off_b], pok_buffers[id].msgsize);
74  pok_buffers[id].off_b = (pok_buffers[id].off_b + pok_buffers[id].msgsize) % pok_buffers[id].size;
75  if (pok_buffers[id].off_b == pok_buffers[id].off_e)
76  {
77  pok_buffers[id].empty = TRUE;
78  }
79 
80  pok_buffers[id].full = FALSE;
81 
82  *len = pok_buffers[id].msgsize;
83 
84  pok_event_unlock (pok_buffers[id].lock);
85 
86  pok_event_broadcast (pok_buffers[id].lock);
87 
88  return POK_ERRNO_OK;
89 }
90 
91 #endif /* POK_NEEDS_BUFFERS */
92 #endif