f3s_ram_open()

In the socket services open() function, we assign a name for the driver and then process any options. If no options are specified, a default size is assigned and the memory for the (virtual) flash is allocated.

/*
** File: f3s_ram_open.c
**
** Description:
**
** This file contains the open function for the ram library
**
*/
#include "f3s_ram.h"

int32_t f3s_ram_open(f3s_socket_t *socket,
                     uint32_t flags)
{
   static void *   memory;
   char           name[8];
   int                 fd;
   int               flag;

   /* check if not initialized */
   if (!memory)
   {
      /* get io privileges */
      ThreadCtl(_NTO_TCTL_IO, NULL);

      /* setup socket name */
      socket->name = "RAM (flash simulation)";

      /* check if there are socket options */
      if (f3s_socket_option(socket))
         socket->window_size = 1024 * 1024;

      /* check if array size was not chosen */
      if (!socket->array_size)
         socket->array_size = socket->window_size;

      /* check if array size was not specified */
      if (!socket->array_size) return (ENXIO);

      /* set shared memory name */
      sprintf(name, "/fs%X", socket->socket_index);

      /* open shared memory */
      fd = shm_open(name, O_CREAT | O_RDWR, 0777);
      
      if (fd < 0) return (errno);

      /* set size of shared memory */
      flag = ftruncate(fd, socket->array_size);
      
      if (flag)
      {
         close(fd);
         return (errno);
      }

      /* map physical address into memory */
      memory = mmap(NULL, socket->array_size,
                    PROT_READ | PROT_WRITE, 
                    MAP_SHARED, fd, socket->address);
				      
      if (!memory)
      {
         close(fd);
         return (errno);
      }

      /* copy socket handle */
      socket->socket_handle = (void *)fd;
   }

   /* set socket memory pointer to previously initialized
      value */
   socket->memory = memory;
   return (EOK);
}