POK
/home/jaouen/pok_official/pok/trunk/libpok/middleware/bufferreceive.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Thu Jan 15 23:34:13 2009 
00015  */
00016 
00017 #include <core/dependencies.h>
00018 
00019 #ifdef POK_NEEDS_MIDDLEWARE
00020 #ifdef POK_NEEDS_BUFFERS
00021 
00022 #include <errno.h>
00023 #include <types.h>
00024 #include <core/time.h>
00025 #include <core/event.h>
00026 #include <libc/string.h>
00027 #include <middleware/buffer.h>
00028 
00029 extern pok_buffer_t    pok_buffers[POK_CONFIG_NB_BUFFERS];
00030 extern char            pok_buffers_data[1024];
00031 
00032 pok_ret_t pok_buffer_receive (const pok_buffer_id_t                id, 
00033                               const uint64_t                       timeout, 
00034                               void*                                data, 
00035                               pok_port_size_t*                     len)
00036 {
00037    pok_ret_t ret;
00038 
00039    if (id > POK_CONFIG_NB_BUFFERS)
00040    {
00041       return POK_ERRNO_EINVAL;
00042    }
00043 
00044    if (pok_buffers[id].ready == FALSE)
00045    {
00046       return POK_ERRNO_EINVAL;
00047    }
00048 
00049    if (data == NULL)
00050    {
00051       return POK_ERRNO_EINVAL;
00052    }
00053 
00054    pok_event_lock (pok_buffers[id].lock);
00055 
00056    while (pok_buffers[id].empty == TRUE)
00057    {
00058       if (timeout == 0)
00059       {
00060          pok_event_unlock (pok_buffers[id].lock);
00061       }
00062       else
00063       {
00064          ret = pok_event_wait (pok_buffers[id].lock, timeout);
00065          if (ret != POK_ERRNO_OK)
00066          {
00067             pok_event_unlock (pok_buffers[id].lock);
00068             return ret;
00069          }
00070       }
00071    }
00072 
00073    memcpy (data, &pok_buffers_data[pok_buffers[id].index + pok_buffers[id].off_b], pok_buffers[id].msgsize);
00074    pok_buffers[id].off_b = (pok_buffers[id].off_b + pok_buffers[id].msgsize) % pok_buffers[id].size;
00075    if (pok_buffers[id].off_b == pok_buffers[id].off_e)
00076    {
00077       pok_buffers[id].empty = TRUE;
00078    }
00079 
00080    pok_buffers[id].full = FALSE;
00081 
00082    *len = pok_buffers[id].msgsize;
00083 
00084    pok_event_unlock (pok_buffers[id].lock);
00085 
00086    pok_event_broadcast (pok_buffers[id].lock);
00087 
00088    return POK_ERRNO_OK;
00089 }
00090 
00091 #endif /* POK_NEEDS_BUFFERS */
00092 #endif