POK
/home/jaouen/pok_official/pok/trunk/libpok/middleware/buffersend.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/event.h>
00025 #include <core/time.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_send (const pok_buffer_id_t              id, 
00033                            const void*                        data, 
00034                            const pok_port_size_t              len, 
00035                            const uint64_t                     timeout)
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    if (len <= 0)
00055    {
00056       return POK_ERRNO_EINVAL;
00057    }
00058 
00059    if (len > (pok_buffers[id].size * pok_buffers[id].msgsize))
00060    {
00061       return POK_ERRNO_EINVAL;
00062    }
00063 
00064    pok_event_lock (pok_buffers[id].lock);
00065 
00066    while (pok_buffers[id].full)
00067    {
00068       if (timeout == 0)
00069       {
00070          pok_event_unlock (pok_buffers[id].lock);
00071          return POK_ERRNO_FULL;
00072       }
00073       else
00074       {
00075          ret = pok_event_wait (pok_buffers[id].lock, timeout);
00076          if (ret != POK_ERRNO_OK)
00077          {
00078             pok_event_unlock (pok_buffers[id].lock);
00079             return ret;
00080          }
00081       }
00082    }
00083 
00084    memcpy (&pok_buffers_data[pok_buffers[id].index + pok_buffers[id].off_e], data, len);
00085    pok_buffers[id].off_e = (pok_buffers[id].off_e + len ) % pok_buffers[id].size;
00086 
00087    if (pok_buffers[id].off_e == pok_buffers[id].off_b)
00088    {
00089       pok_buffers[id].full = TRUE;
00090    }
00091 
00092    pok_buffers[id].empty = FALSE;
00093 
00094    pok_event_unlock (pok_buffers[id].lock);
00095 
00096    pok_event_broadcast (pok_buffers[id].lock);
00097 
00098    return POK_ERRNO_OK;
00099 }
00100 
00101 #endif /* POK_NEEDS_BUFFERS */
00102 #endif