POK(kernelpart)
/home/jaouen/pok_official/pok/trunk/kernel/middleware/portcreate.c
Go to the documentation of this file.
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 #if defined (POK_NEEDS_PORTS_SAMPLING) || defined (POK_NEEDS_PORTS_QUEUEING)
00018 
00019 #include <errno.h>
00020 #include <types.h>
00021 #include <core/error.h>
00022 #include <core/partition.h>
00023 #include <middleware/port.h>
00024 #include <middleware/queue.h>
00025 #include <libc.h>
00026 
00027 extern uint8_t       pok_current_partition;
00028 extern uint8_t       pok_ports_parts_nb_ports[POK_CONFIG_NB_PARTITIONS];
00029 extern uint8_t*      pok_ports_parts_ports_identifiers[POK_CONFIG_NB_PARTITIONS];
00030 extern pok_port_t    pok_ports[POK_CONFIG_NB_PORTS];
00031 extern pok_queue_t   pok_queue;
00032 
00033 pok_ret_t pok_port_create (char* name,
00034                                                                                                          const pok_port_size_t size,
00035                                                                                                          const pok_port_direction_t direction,
00036                                                                                                          uint8_t kind, pok_port_id_t* id)
00037 {
00038          uint8_t   gid;
00039          pok_ret_t ret;
00040 
00041          ret = POK_ERRNO_OK;
00042 
00043 
00044          if (size > POK_PORT_MAX_SIZE)
00045          {
00046 #if (defined POK_NEEDS_PARTITIONS) && (defined POK_NEEDS_ERROR_HANDLING)
00047                         POK_ERROR_CURRENT_PARTITION(POK_ERROR_KIND_PARTITION_CONFIGURATION);
00048 #endif
00049                         return POK_ERRNO_PORT;
00050          }
00051 
00052          if (size > pok_queue.available_size)
00053          {
00054 #if (defined POK_NEEDS_PARTITIONS) && (defined POK_NEEDS_ERROR_HANDLING)
00055                         POK_ERROR_CURRENT_PARTITION(POK_ERROR_KIND_PARTITION_CONFIGURATION);
00056 #endif
00057                         return POK_ERRNO_PORT;
00058          }
00059 
00060          if ((direction != POK_PORT_DIRECTION_IN) &&
00061                          (direction != POK_PORT_DIRECTION_OUT))
00062          {
00063 #if (defined POK_NEEDS_PARTITIONS) && (defined POK_NEEDS_ERROR_HANDLING)
00064                         POK_ERROR_CURRENT_PARTITION(POK_ERROR_KIND_PARTITION_CONFIGURATION);
00065 #endif
00066                         return POK_ERRNO_PORT;
00067          }
00068 
00069          switch (kind)
00070          {
00071                         case POK_PORT_KIND_SAMPLING:
00072                                  {
00073 #ifdef POK_NEEDS_PORTS_SAMPLING
00074                                                 ret = pok_port_sampling_id (name, &gid);
00075 #endif
00076                                                 break;
00077                                  }
00078                         case POK_PORT_KIND_QUEUEING:
00079                                  {
00080 #ifdef POK_NEEDS_PORTS_QUEUEING
00081                                                 ret = pok_port_queueing_id (name, &gid);
00082 #endif
00083                                                 break;
00084                                  }
00085                         default:
00086                                  {
00087                                                 return POK_ERRNO_EINVAL;
00088                                                 break;
00089                                  }
00090          }
00091 
00092          if (ret != POK_ERRNO_OK)
00093          {
00094 #if (defined POK_NEEDS_PARTITIONS) && (defined POK_NEEDS_ERROR_HANDLING)
00095                         POK_ERROR_CURRENT_PARTITION(POK_ERROR_KIND_PARTITION_INIT);
00096 #endif
00097                         return ret;
00098          }
00099 
00100          if (! pok_own_port (POK_SCHED_CURRENT_PARTITION, gid))
00101          {
00102                         return POK_ERRNO_PORT;
00103          }
00104 
00105          /*
00106                 * Check if the port was already created
00107                 * If it's already created, we return ERRNO_EXISTS but indicate
00108                 * the right port-id. This should not be taken as an assumption
00109                 * but could help the developper when a partition is restarted.
00110                 */
00111          if (pok_ports[gid].ready == TRUE)
00112          {
00113                         *id = gid;
00114                         return POK_ERRNO_EXISTS;
00115          }
00116 
00117          pok_ports[gid].index      = pok_queue.size - pok_queue.available_size;
00118          pok_ports[gid].off_b      = 0;
00119          pok_ports[gid].off_e      = 0;
00120          pok_ports[gid].size       = size;
00121          pok_ports[gid].full       = FALSE;
00122          pok_ports[gid].partition  = pok_current_partition;
00123          pok_ports[gid].direction  = direction;
00124          pok_ports[gid].ready      = TRUE;
00125          pok_ports[gid].kind       = kind;
00126 
00127          pok_queue.available_size  = pok_queue.available_size - size;
00128 
00129          *id = gid;
00130 
00131          return POK_ERRNO_OK;
00132 }
00133 #endif