Exemplo n.º 1
0
grn_rc
grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
{
#ifdef HAVE_CLOCK_GETTIME
  struct timespec t;
  if (clock_gettime(CLOCK_REALTIME, &t)) {
    SERR("clock_gettime");
  } else {
    tv->tv_sec = t.tv_sec;
    tv->tv_nsec = t.tv_nsec;
  }
  return ctx->rc;
#else /* HAVE_CLOCK_GETTIME */
# ifdef WIN32
  time_t t;
  struct _timeb tb;
  time(&t);
  _ftime(&tb);
  tv->tv_sec = t;
  tv->tv_nsec = tb.millitm * (GRN_TIME_NSEC_PER_SEC / 1000);
  return GRN_SUCCESS;
# else /* WIN32 */
  struct timeval t;
  if (gettimeofday(&t, NULL)) {
    SERR("gettimeofday");
  } else {
    tv->tv_sec = t.tv_sec;
    tv->tv_nsec = GRN_TIME_USEC_TO_NSEC(t.tv_usec);
  }
  return ctx->rc;
# endif /* WIN32 */
#endif /* HAVE_CLOCK_GETTIME */
}
Exemplo n.º 2
0
int32_t led_flash_rer_wait_pupil_contract(
  rer_cfg_t *rer,
  module_sensor_params_t *led_module_params)
{
  if (rer->status == RER_WAIT_PUPIL_CONTRACT) {
    /* Wait before to start the main flash after RER sequence */
    long int  delay;
    struct timeval ts_now, ts, ts_diff;

    ts = rer->last_rer_flash_ts;

    gettimeofday(&ts_now, NULL);
    timersub(&ts_now, &ts, &ts_diff);
    delay = (ts_diff.tv_sec * 1000000) + ts_diff.tv_usec;
    delay = (long int)(RER_PUPIL_CONTRACT_TIME * 1000) - delay;

    if (delay > 0) {
      /* Wait until Pupil contraction time RER_PUPIL_CONTRACT_TIME */
      if (delay <= (RER_PUPIL_CONTRACT_TIME * 1000)) {
        SLOW("Wait %ld us to reach RER_PUPIL_CONTRACT_TIME\n", delay);
        usleep(delay);
      } else {
        /* Error - delay can not be bigger than RER_PUPIL_CONTRACT_TIME */
        SERR("Error - RER delay out of range %d us\n", delay);
      }
    } else {
      /* Delay is bigger than the requested RER_PUPIL_CONTRACT_TIME */
      SERR("RER Over delay %ld us (total delay %ld us)\n",
        -(long int)delay,
        (long int)(RER_PUPIL_CONTRACT_TIME * 1000) - delay);
    }
    rer->status = RER_DONE;
  }
  return SENSOR_SUCCESS;
}
Exemplo n.º 3
0
grn_id
grn_plugin_open(grn_ctx *ctx, const char *filename)
{
    grn_id id;
    grn_dl dl;
    grn_plugin **plugin = NULL;

    CRITICAL_SECTION_ENTER(grn_plugins_lock);
    if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, PATHLEN(filename),
                           (void **)&plugin))) {
        (*plugin)->refcount++;
        goto exit;
    }

    if ((dl = grn_dl_open(filename))) {
        if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, PATHLEN(filename),
                               (void **)&plugin, NULL))) {
            *plugin = GRN_GMALLOCN(grn_plugin, 1);
            if (*plugin) {
                if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) {
                    GRN_GFREE(*plugin);
                    *plugin = NULL;
                }
            }
            if (!*plugin) {
                grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL);
                if (grn_dl_close(dl)) {
                    /* Now, __FILE__ set in plugin is invalid. */
                    ctx->errline = 0;
                    ctx->errfile = NULL;
                } else {
                    const char *label;
                    label = grn_dl_close_error_label();
                    SERR(label);
                }
                id = GRN_ID_NIL;
            } else {
                (*plugin)->refcount = 1;
            }
        } else {
            if (!grn_dl_close(dl)) {
                const char *label;
                label = grn_dl_close_error_label();
                SERR(label);
            }
        }
    } else {
        const char *label;
        label = grn_dl_open_error_label();
        SERR(label);
    }

exit:
    CRITICAL_SECTION_LEAVE(grn_plugins_lock);

    return id;
}
Exemplo n.º 4
0
Arquivo: com.c Projeto: iwaim/groonga
grn_rc
grn_com_event_init(grn_ctx *ctx, grn_com_event *ev, int max_nevents, int data_size)
{
    ev->max_nevents = max_nevents;
    if ((ev->hash = grn_hash_create(ctx, NULL, sizeof(grn_sock), data_size, 0))) {
        MUTEX_INIT(ev->mutex);
        COND_INIT(ev->cond);
        GRN_COM_QUEUE_INIT(&ev->recv_old);
        ev->msg_handler = NULL;
        memset(&(ev->curr_edge_id), 0, sizeof(grn_com_addr));
        ev->acceptor = NULL;
        ev->opaque = NULL;
#ifndef USE_SELECT
# ifdef USE_EPOLL
        if ((ev->events = GRN_MALLOC(sizeof(struct epoll_event) * max_nevents))) {
            if ((ev->epfd = epoll_create(max_nevents)) != -1) {
                goto exit;
            } else {
                SERR("epoll_create");
            }
            GRN_FREE(ev->events);
        }
# else /* USE_EPOLL */
#  ifdef USE_KQUEUE
        if ((ev->events = GRN_MALLOC(sizeof(struct kevent) * max_nevents))) {
            if ((ev->kqfd = kqueue()) != -1) {
                goto exit;
            } else {
                SERR("kqueue");
            }
            GRN_FREE(ev->events);
        }
#  else /* USE_KQUEUE */
        if ((ev->events = GRN_MALLOC(sizeof(struct pollfd) * max_nevents))) {
            goto exit;
        }
#  endif /* USE_KQUEUE*/
# endif /* USE_EPOLL */
        grn_hash_close(ctx, ev->hash);
        ev->hash = NULL;
        ev->events = NULL;
#else /* USE_SELECT */
        goto exit;
#endif /* USE_SELECT */
    }
exit :
    return ctx->rc;
}
Exemplo n.º 5
0
Arquivo: HTTPd.c Projeto: Youx/craftd
static
void
cd_JSONRequest (struct evhttp_request* request, CDServer* server)
{
    struct evbuffer* buffer = evhttp_request_get_input_buffer(request);
    char*            text   = CD_alloc(evbuffer_get_length(buffer) + 1);

    evbuffer_remove(buffer, text, evbuffer_get_length(buffer));

    json_error_t error;
    json_t*      input  = json_loads(text, 0, &error);
    json_t*      output = json_object();

    printf("%s\n", text);

    if (evhttp_request_get_command(request) != EVHTTP_REQ_POST) {
        goto error;
    }

    if (input == NULL) {
        SERR(server, "RPC.JSON: error on line %d: %s", error.line, error.text);

        goto error;
    }

    CD_EventDispatch(server, "RPC.JSON", input, output);

done: {
        char*            outString = json_dumps(output, JSON_INDENT(2));
        struct evbuffer* outBuffer = evbuffer_new();

        evbuffer_add_printf(outBuffer, "%s", outString);

        evhttp_send_reply(request, HTTP_OK, "OK", outBuffer);

        evbuffer_free(outBuffer);
        free(outString);
        json_delete(output);
        json_delete(input);
        CD_free(text);

        return;
    }

error: {
        evhttp_send_error(request, HTTP_INTERNAL, "Internal server error");

        CD_free(text);

        if (input) {
            json_delete(input);
        }

        if (output) {
            json_delete(output);
        }

        return;
    }
}
Exemplo n.º 6
0
grn_rc
grn_plugin_close(grn_ctx *ctx, grn_id id)
{
  grn_rc rc;
  grn_plugin *plugin;

  if (id == GRN_ID_NIL) {
    return GRN_INVALID_ARGUMENT;
  }

  CRITICAL_SECTION_ENTER(grn_plugins_lock);
  if (!grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin)) {
    rc = GRN_INVALID_ARGUMENT;
    goto exit;
  }
  if (--plugin->refcount) {
    rc = GRN_SUCCESS;
    goto exit;
  }
  if (plugin->dl) {
    grn_plugin_call_fin(ctx, id);
    if (!grn_dl_close(plugin->dl)) {
      const char *label;
      label = grn_dl_close_error_label();
      SERR("%s", label);
    }
  }
  GRN_GFREE(plugin);
  rc = grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL);

exit:
  CRITICAL_SECTION_LEAVE(grn_plugins_lock);

  return rc;
}
Exemplo n.º 7
0
void *
grn_plugin_sym(grn_ctx *ctx, grn_id id, const char *symbol)
{
  grn_plugin *plugin;
  grn_dl_symbol func;

  if (id == GRN_ID_NIL) {
    return NULL;
  }

  CRITICAL_SECTION_ENTER(grn_plugins_lock);
  if (!grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin)) {
    func = NULL;
    goto exit;
  }
  grn_dl_clear_error();
  if (!(func = grn_dl_sym(plugin->dl, symbol))) {
    const char *label;
    label = grn_dl_sym_error_label();
    SERR("%s", label);
  }

exit:
  CRITICAL_SECTION_LEAVE(grn_plugins_lock);

  return func;
}
Exemplo n.º 8
0
static int read_from_sock(server_t *srv) {
  selene_t *s = srv->s;
  int err;
  ssize_t rv = 0;
  do {
    char buf[8096];

    setnonblocking(srv->sock);

    rv = read(srv->sock, &buf[0], sizeof(buf));

    if (rv == -1) {
      err = errno;
      if (err != EAGAIN) {
        srv->read_err = err;
        break;
      }
    }

    if (rv == 0) {
      break;
    }

    if (rv > 0) {
      SERR(selene_io_in_enc_bytes(s, buf, rv));
    }
  } while (rv > 0);

  return 0;
}
Exemplo n.º 9
0
int32_t led_flash_rer_get_chromatix(
  rer_cfg_t *rer,
  red_eye_reduction_type *rer_chromatix)
{
  int led_flash_enable;
  red_eye_reduction_type *rer_cfg = (rer_cfg_t *)rer->cfg;

  if ((rer_cfg == NULL) ||
      (rer_chromatix == NULL)) {
    // Check input
    SERR("Red Eye Reduction process Skip ->\n \
          rer_cfg           = 0x%08x \n \
          rer_chromatix     = 0x%08x \n",
          (unsigned int)rer_cfg,
          (unsigned int)rer_chromatix);
    return SENSOR_FAILURE;
  }

  // Save red_eye_reduction_led_flash_enable state
  led_flash_enable = rer_cfg->red_eye_reduction_led_flash_enable;
  memcpy(rer_cfg, rer_chromatix, sizeof(red_eye_reduction_type));
  // Restore red_eye_reduction_led_flash_enable state
  rer_cfg->red_eye_reduction_led_flash_enable = led_flash_enable;

  return SENSOR_SUCCESS;
}
Exemplo n.º 10
0
static
void
cdsurvivalproxy_ProxyErrorCallback (struct bufferevent* event, short error, CDClient* client)
{
	assert(client);
	CDServer* server = client->server;
	assert(server);
	
	if (error & BEV_EVENT_CONNECTED) {
		SLOG(client->server, LOG_INFO, "proxy connected :D");
	}
	
	if (!((error & BEV_EVENT_EOF) || (error & BEV_EVENT_ERROR) || (error & BEV_EVENT_TIMEOUT))) {
		//not an error
		return;
	}
	
	if (error & BEV_EVENT_ERROR) {
		SLOG(client->server, LOG_INFO, "libevent: ip %s - %s", client->ip, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
	} else if (error & BEV_EVENT_TIMEOUT) {
		SERR(client->server, "A bufferevent timeout?");
	} else if (error & BEV_EVENT_EOF) {
		SLOG(client->server, LOG_INFO, "remote EOF");
	}
	
	CD_ServerKick(client->server, client, CD_CreateStringFromCString("remote connection died"));
}
Exemplo n.º 11
0
static grn_bool
grn_time_t_to_tm(grn_ctx *ctx, const time_t time, struct tm *tm)
{
  grn_bool success;
  const char *function_name;
#ifdef HAVE__LOCALTIME64_S
  function_name = "localtime_s";
  success = (localtime_s(tm, &time) == 0);
#else /* HAVE__LOCALTIME64_S */
# ifdef HAVE_LOCALTIME_R
  function_name = "localtime_r";
  success = (localtime_r(&time, tm) != NULL);
# else /* HAVE_LOCALTIME_R */
  function_name = "localtime";
  {
    struct tm *local_tm;
    local_tm = localtime(&time);
    if (local_tm) {
      success = GRN_TRUE;
      memcpy(tm, local_tm, sizeof(struct tm));
    } else {
      success = GRN_FALSE;
    }
  }
# endif /* HAVE_LOCALTIME_R */
#endif /* HAVE__LOCALTIME64_S */
  if (!success) {
    SERR("%s: failed to convert time_t to struct tm: <%" GRN_FMT_INT64D ">",
         function_name,
         (int64_t)time);
  }
  return success;
}
Exemplo n.º 12
0
Arquivo: mrb.c Projeto: XLPE/groonga
mrb_value
grn_mrb_load(grn_ctx *ctx, const char *path)
{
  grn_mrb_data *data = &(ctx->impl->mrb);
  mrb_state *mrb = data->state;
  char expanded_path[PATH_MAX];
  FILE *file;
  mrb_value result;
  struct mrb_parser_state *parser;

  if (!mrb) {
    return mrb_nil_value();
  }

  if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) {
    return mrb_nil_value();
  }

  file = grn_fopen(expanded_path, "r");
  if (!file) {
    mrb_value exception;
    SERR("fopen: failed to open mruby script file: <%s>",
         expanded_path);
    exception = mrb_exc_new(mrb, E_LOAD_ERROR,
                            ctx->errbuf, strlen(ctx->errbuf));
    mrb->exc = mrb_obj_ptr(exception);
    return mrb_nil_value();
  }

  {
    char current_base_directory[PATH_MAX];
    char *last_directory;

    grn_strcpy(current_base_directory, PATH_MAX, data->base_directory);
    grn_strcpy(data->base_directory, PATH_MAX, expanded_path);
    last_directory = strrchr(data->base_directory, '/');
    if (last_directory) {
      last_directory[0] = '\0';
    }

    parser = mrb_parser_new(mrb);
    mrb_parser_set_filename(parser, expanded_path);
    parser->s = parser->send = NULL;
    parser->f = file;
    mrb_parser_parse(parser, NULL);
    fclose(file);

    {
      struct RProc *proc;
      proc = mrb_generate_code(mrb, parser);
      result = mrb_toplevel_run(mrb, proc);
    }
    mrb_parser_free(parser);

    grn_strcpy(data->base_directory, PATH_MAX, current_base_directory);
  }

  return result;
}
Exemplo n.º 13
0
Arquivo: com.c Projeto: iwaim/groonga
grn_rc
grn_com_event_add(grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com)
{
    grn_com *c;
    /* todo : expand events */
    if (!ev || *ev->hash->n_entries == ev->max_nevents) {
        if (ev) {
            GRN_LOG(ctx, GRN_LOG_ERROR, "too many connections (%d)", ev->max_nevents);
        }
        return GRN_INVALID_ARGUMENT;
    }
#ifdef USE_EPOLL
    {
        struct epoll_event e;
        memset(&e, 0, sizeof(struct epoll_event));
        e.data.fd = (fd);
        e.events = (uint32_t) events;
        if (epoll_ctl(ev->epfd, EPOLL_CTL_ADD, (fd), &e) == -1) {
            SERR("epoll_ctl");
            return ctx->rc;
        }
    }
#endif /* USE_EPOLL*/
#ifdef USE_KQUEUE
    {
        struct kevent e;
        /* todo: udata should have fd */
        EV_SET(&e, (fd), events, EV_ADD, 0, 0, NULL);
        if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) {
            SERR("kevent");
            return ctx->rc;
        }
    }
#endif /* USE_KQUEUE */
    {
        if (grn_hash_add(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c, NULL)) {
            c->ev = ev;
            c->fd = fd;
            c->events = events;
            if (com) {
                *com = c;
            }
        }
    }
    return ctx->rc;
}
Exemplo n.º 14
0
Arquivo: com.c Projeto: iwaim/groonga
grn_rc
grn_com_event_mod(grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com)
{
    grn_com *c;
    if (!ev) {
        return GRN_INVALID_ARGUMENT;
    }
    if (grn_hash_get(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c)) {
        if (c->fd != fd) {
            GRN_LOG(ctx, GRN_LOG_ERROR,
                    "grn_com_event_mod fd unmatch "
                    "%" GRN_FMT_SOCKET " != %" GRN_FMT_SOCKET,
                    c->fd, fd);
            return GRN_OBJECT_CORRUPT;
        }
        if (com) {
            *com = c;
        }
        if (c->events != events) {
#ifdef USE_EPOLL
            struct epoll_event e;
            memset(&e, 0, sizeof(struct epoll_event));
            e.data.fd = (fd);
            e.events = (uint32_t) events;
            if (epoll_ctl(ev->epfd, EPOLL_CTL_MOD, (fd), &e) == -1) {
                SERR("epoll_ctl");
                return ctx->rc;
            }
#endif /* USE_EPOLL*/
#ifdef USE_KQUEUE
            // experimental
            struct kevent e[2];
            EV_SET(&e[0], (fd), GRN_COM_POLLIN|GRN_COM_POLLOUT, EV_DELETE, 0, 0, NULL);
            EV_SET(&e[1], (fd), events, EV_ADD, 0, 0, NULL);
            if (kevent(ev->kqfd, e, 2, NULL, 0, NULL) == -1) {
                SERR("kevent");
                return ctx->rc;
            }
#endif /* USE_KQUEUE */
            c->events = events;
        }
        return GRN_SUCCESS;
    }
    return GRN_INVALID_ARGUMENT;
}
Exemplo n.º 15
0
int32_t led_flash_sub_module_init(sensor_func_tbl_t *func_tbl)
{
  if (!func_tbl) {
    SERR("failed");
    return SENSOR_FAILURE;
  }
  func_tbl->open = led_flash_open;
  func_tbl->process = led_flash_process;
  func_tbl->close = led_flash_close;
  return SENSOR_SUCCESS;
}
Exemplo n.º 16
0
Arquivo: com.c Projeto: iwaim/groonga
grn_rc
grn_com_event_del(grn_ctx *ctx, grn_com_event *ev, grn_sock fd)
{
    if (!ev) {
        return GRN_INVALID_ARGUMENT;
    }
    {
        grn_com *c;
        grn_id id = grn_hash_get(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c);
        if (id) {
#ifdef USE_EPOLL
            if (!c->closed) {
                struct epoll_event e;
                memset(&e, 0, sizeof(struct epoll_event));
                e.data.fd = fd;
                e.events = c->events;
                if (epoll_ctl(ev->epfd, EPOLL_CTL_DEL, fd, &e) == -1) {
                    SERR("epoll_ctl");
                    return ctx->rc;
                }
            }
#endif /* USE_EPOLL*/
#ifdef USE_KQUEUE
            struct kevent e;
            EV_SET(&e, (fd), c->events, EV_DELETE, 0, 0, NULL);
            if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) {
                SERR("kevent");
                return ctx->rc;
            }
#endif /* USE_KQUEUE */
            return grn_hash_delete_by_id(ctx, ev->hash, id, NULL);
        } else {
            GRN_LOG(ctx, GRN_LOG_ERROR,
                    "%04x| fd(%" GRN_FMT_SOCKET ") not found in ev(%p)",
                    getpid(), fd, ev);
            return GRN_INVALID_ARGUMENT;
        }
    }
}
Exemplo n.º 17
0
int32_t led_flash_get_current(
  awb_update_t *awb_update,
  awb_dual_led_settings_t *dual_led_setting)
{
  if (!awb_update || !dual_led_setting) {
    SERR("failed");
    return SENSOR_FAILURE;
  }

  memcpy(dual_led_setting, &(awb_update->dual_led_setting),
    sizeof(awb_dual_led_settings_t));

  return SENSOR_SUCCESS;
}
Exemplo n.º 18
0
void *
grn_plugin_sym(grn_ctx *ctx, grn_id id, const char *symbol)
{
  grn_plugin *plugin;
  grn_dl_symbol func;

  if (!grn_hash_get_value(ctx, grn_plugins, id, &plugin)) {
    return NULL;
  }
  grn_dl_clear_error;
  if (!(func = grn_dl_sym(plugin->dl, symbol))) {
    const char *label;
    label = grn_dl_sym_error_label;
    SERR(label);
  }
  return func;
}
Exemplo n.º 19
0
grn_rc
grn_plugin_close(grn_ctx *ctx, grn_id id)
{
  grn_plugin *plugin;

  if (!grn_hash_get_value(ctx, grn_plugins, id, &plugin)) {
    return GRN_INVALID_ARGUMENT;
  }
  if (--plugin->refcount) { return GRN_SUCCESS; }
  grn_plugin_call_fin(ctx, id);
  if (!grn_dl_close(plugin->dl)) {
    const char *label;
    label = grn_dl_close_error_label;
    SERR(label);
  }
  GRN_GFREE(plugin);
  return grn_hash_delete_by_id(ctx, grn_plugins, id, NULL);
}
Exemplo n.º 20
0
grn_rc
grn_com_init(void)
{
#ifdef WIN32
  WSADATA wd;
  if (WSAStartup(MAKEWORD(2, 0), &wd) != 0) {
    grn_ctx *ctx = &grn_gctx;
    SOERR("WSAStartup");
  }
#else /* WIN32 */
#ifndef USE_MSG_NOSIGNAL
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
    grn_ctx *ctx = &grn_gctx;
    SERR("signal");
  }
#endif /* USE_MSG_NOSIGNAL */
#endif /* WIN32 */
  return grn_gctx.rc;
}
Exemplo n.º 21
0
Arquivo: ctx.c Projeto: ikdttr/groonga
grn_rc
grn_timeval2str(grn_ctx *ctx, grn_timeval *tv, char *buf)
{
  struct tm *ltm;
#ifdef WIN32
  time_t tvsec = (time_t) tv->tv_sec;
  ltm = localtime(&tvsec);
#else /* WIN32 */
  struct tm tm;
  time_t t = tv->tv_sec;
  ltm = localtime_r(&t, &tm);
#endif /* WIN32 */
  if (!ltm) { SERR("localtime"); }
  snprintf(buf, GRN_TIMEVAL_STR_SIZE - 1, GRN_TIMEVAL_STR_FORMAT,
           ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday,
           ltm->tm_hour, ltm->tm_min, ltm->tm_sec, (int) tv->tv_usec);
  buf[GRN_TIMEVAL_STR_SIZE - 1] = '\0';
  return ctx->rc;
}
Exemplo n.º 22
0
Arquivo: ctx.c Projeto: ikdttr/groonga
grn_rc
grn_timeval_now(grn_ctx *ctx, grn_timeval *tv)
{
#ifdef WIN32
  time_t t;
  struct _timeb tb;
  time(&t);
  _ftime(&tb);
  tv->tv_sec = (int32_t) t;
  tv->tv_usec = tb.millitm * 1000;
  return GRN_SUCCESS;
#else /* WIN32 */
  struct timeval t;
  if (gettimeofday(&t, NULL)) {
    SERR("gettimeofday");
  } else {
    tv->tv_sec = (int32_t) t.tv_sec;
    tv->tv_usec = t.tv_usec;
  }
  return ctx->rc;
#endif /* WIN32 */
}
Exemplo n.º 23
0
static int32_t led_flash_close(void *led_flash_ctrl)
{
  int32_t rc = SENSOR_SUCCESS;
  sensor_led_flash_data_t *ctrl = (sensor_led_flash_data_t *)led_flash_ctrl;
  struct msm_camera_led_cfg_t cfg;

  SLOW("Enter");
  cfg.cfgtype = MSM_CAMERA_LED_RELEASE;
  rc = ioctl(ctrl->fd, VIDIOC_MSM_FLASH_LED_DATA_CFG, &cfg);
  if (rc < 0) {
    SERR("VIDIOC_MSM_FLASH_LED_DATA_CFG failed %s",
      strerror(errno));
  }

  /* close subdev */
  close(ctrl->fd);

  if (ctrl->rer != NULL) {
    if (ctrl->rer->cfg != NULL) {
      /* Free rer->cfg */
      free(ctrl->rer->cfg);
      ctrl->rer->cfg = NULL;
    }
    /* Free rer */
    free(ctrl->rer);
    ctrl->rer = NULL;
  }

  if (ctrl->dual_led_setting != NULL) {
    /* Free dual_led_setting */
    free(ctrl->dual_led_setting);
    ctrl->dual_led_setting = NULL;
  }

  free(ctrl);
  SLOW("Exit");
  return rc;
}
Exemplo n.º 24
0
int main(int argc, char *argv[]) {
  const char *host = SELENE_SERVER_DEFAULT_HOST;
  int port = SELENE_SERVER_DEFAULT_PORT;
  const char *cert_path = SELENE_SERVER_DEFAULT_CERT_PATH;
  const char *key_path = NULL;
  selene_conf_t *conf = NULL;
  const char *cert = NULL;
  const char *pkey = NULL;
  int rv = 0;
  int i;

  for (i = 1; i < argc; i++) {
    /* TODO: s_server compat */
    if (!strcmp("-host", argv[i]) && argc > i + 1) {
      host = argv[i + 1];
      i++;
    } else if (!strcmp("-port", argv[i]) && argc > i + 1) {
      port = atoi(argv[i + 1]);
      i++;
    } else if (!strcmp("-listen", argv[i]) && argc > i + 1) {
      char *p;
      host = argv[i + 1];
      if ((p = strstr(host, ":")) == NULL) {
        fprintf(stderr, "no port found\n");
        exit(EXIT_FAILURE);
      }
      *(p++) = '\0';
      port = atoi(p);
      i++;
    } else {
      fprintf(stderr, "Invalid args\n");
      usage();
      exit(EXIT_FAILURE);
    }
  }

  if (host == NULL) {
    fprintf(stderr, "-host must be set\n");
    exit(EXIT_FAILURE);
  }

  if (port <= 0) {
    fprintf(stderr, "-port must be set\n");
    exit(EXIT_FAILURE);
  }

  if (key_path == NULL) {
    /* assume its a pem encoded cert + key in one */
    key_path = cert_path;
  }

  SERR(selene_conf_create(&conf));

  SERR(selene_conf_use_reasonable_defaults(conf));

  cert = load_cert(cert_path);
  pkey = load_cert(key_path);
  SERR(selene_conf_cert_chain_add(conf, cert, pkey));

  rv = listen_to(conf, host, port, stdin);

  selene_conf_destroy(conf);

  if (cert) {
    free((void *)cert);
  }

  if (pkey) {
    free((void *)pkey);
  }

  return rv;
}
Exemplo n.º 25
0
static int listen_to(selene_conf_t *conf, const char *host, int port,
                     FILE *fp) {
  fd_set readers;
  int rv = 0;
  server_t server;
  char buf[8096];
  char port_str[16];
  char *p = NULL;
  struct addrinfo hints, *res, *res0;
  selene_error_t *err = NULL;
  char ip_buf[INET6_ADDRSTRLEN];
  char *ip_str = NULL;

  memset(&server, 0, sizeof(server));

  snprintf(port_str, sizeof(port_str), "%i", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
  rv = getaddrinfo(host, port_str, &hints, &res0);
  if (rv != 0) {
    fprintf(stderr, "TCP getaddrinfo(%s:%d) failed: (%d) %s\n", host, port, rv,
            gai_strerror(rv));
    exit(EXIT_FAILURE);
  }

  server.sock = -1;
  for (res = res0; res; res = res->ai_next) {

    server.listen_sock =
        socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (server.listen_sock < 0) {
      continue;
    }

    ip_str = addr2str(res->ai_addr, &ip_buf[0], sizeof(ip_buf));

    fprintf(stderr, "TCP bind(%s:%d)\n", ip_str, port);

    rv = bind(server.listen_sock, res->ai_addr, res->ai_addrlen);
    if (rv != 0) {
      close(server.listen_sock);
      server.listen_sock = -1;
      continue;
    }

    rv = listen(server.listen_sock, 10);
    if (rv != 0) {
      close(server.listen_sock);
      server.listen_sock = -1;
      continue;
    }

    break;
  }

  freeaddrinfo(res0);

  if (server.listen_sock == -1) {
    fprintf(stderr, "TCP bind(%s:%d) failed\n", host, port);
    exit(EXIT_FAILURE);
  }

  server.sock = -1;

  while (server.write_err == 0) {
    FD_ZERO(&readers);

    FD_SET(server.listen_sock, &readers);
    if (server.sock != -1) {
      FD_SET(server.sock, &readers);
    }
    FD_SET(fileno(fp), &readers);

    rv = select(FD_SETSIZE, &readers, NULL, NULL, NULL);

    if (rv > 0) {
      if (FD_ISSET(fileno(fp), &readers)) {
        p = fgets(buf, sizeof(buf), fp);

        if (p == NULL) {
          break;
        }

        if (server.sock != -1) {
          SERR(selene_io_in_clear_bytes(server.s, p, strlen(p)));
        }
      } else if (FD_ISSET(server.listen_sock, &readers)) {
        /* TODO: multiple client support */
        if (server.sock == -1) {
          server.sock = accept(server.listen_sock, NULL, 0);

          err = selene_server_create(conf, &server.s);
          if (err != SELENE_SUCCESS) {
            fprintf(stderr,
                    "Failed to create client instance: (%d) %s [%s:%d]\n",
                    err->err, err->msg, err->file, err->line);
            exit(EXIT_FAILURE);
          }

          selene_subscribe(server.s, SELENE_EVENT_LOG_MSG, have_logline, NULL);

          SERR(selene_subscribe(server.s, SELENE_EVENT_IO_OUT_ENC, want_pull,
                                &server));

          SERR(selene_subscribe(server.s, SELENE_EVENT_IO_OUT_CLEAR,
                                have_cleartext, &server));

          SERR(selene_start(server.s));
        }
      } else if (server.sock != -1 && FD_ISSET(server.sock, &readers)) {
        read_from_sock(&server);
      }
    }
  }

  if (server.write_err != 0) {
    /* TODO: client ip */
    fprintf(stderr, "TCP write from %s:%d failed: (%d) %s\n", host, port,
            server.write_err, strerror(server.write_err));
    exit(EXIT_FAILURE);
  }

  if (server.read_err != 0) {
    /* TODO: just disconnect client, keep listening */
    fprintf(stderr, "TCP read on %s:%d failed: (%d) %s\n", host, port,
            server.read_err, strerror(server.read_err));
    exit(EXIT_FAILURE);
  }

  if (server.s) {
    selene_destroy(server.s);
    server.s = NULL;
  }

  return 0;
}
Exemplo n.º 26
0
int32_t led_flash_rer_sequence_process(
  rer_cfg_t *rer,
  module_sensor_params_t *led_module_params)
{
  int32_t rc = SENSOR_SUCCESS;
  red_eye_reduction_type *rer_cfg = (rer_cfg_t *)rer->cfg;
  int led_flash_enable;
  int preflash_cycles;
  int LED_pulse_duration_ms;
  int interval_pulese_ms;
  int LED_current_mA;

  if ((led_module_params == NULL) ||
      (rer_cfg == NULL)) {
    // Check input
    SERR("Red Eye Reduction process Skip ->\n \
        led_module_params = 0x%08x \n \
        rer_cfg           = 0x%08x \n",
        (unsigned int)led_module_params,
        (unsigned int)rer_cfg
    );
    return SENSOR_FAILURE;
  }

  // Get chromatix RER data from rer_cfg
  led_flash_enable = rer_cfg->red_eye_reduction_led_flash_enable;
  preflash_cycles  = rer_cfg->number_of_preflash_cycles;
  LED_pulse_duration_ms = rer_cfg->preflash_LED_pulse_duration;
  interval_pulese_ms = rer_cfg->preflash_interval_between_pulese;
  LED_current_mA = rer_cfg->preflash_LED_current;

  rer->status = RER_START;

  if (led_flash_enable == 1) {
    // Red eye procedure is Enabled
    int rc = SENSOR_SUCCESS;
    int sequence_time = ((preflash_cycles
        * (LED_pulse_duration_ms + interval_pulese_ms))
        + RER_PUPIL_CONTRACT_TIME);

    // Check Red Eye Tuning parameters
    rc += input_check(preflash_cycles, PREFLASH_CYCLES_MIN, PREFLASH_CYCLES_MAX);
    rc += input_check(LED_pulse_duration_ms, LED_ON_MS_MIN, LED_ON_MS_MAX);
    rc += input_check(interval_pulese_ms, LED_OFF_MS_MIN, LED_OFF_MS_MAX);
    rc += input_check(sequence_time, RER_DURATION_MS_MIN, RER_DURATION_MS_MAX);

    if (rc < 0) {
      SERR("Error: RER parameters out of range \n");
      rer->status = RER_DONE;
      return SENSOR_FAILURE;
    }

    // RER procedure
    while (preflash_cycles) {
      led_module_params->func_tbl.process(
        led_module_params->sub_module_private,
        LED_FLASH_SET_RER_PULSE_FLASH , &LED_current_mA);

      usleep(LED_pulse_duration_ms*1000);

      led_module_params->func_tbl.process(
        led_module_params->sub_module_private,
        LED_FLASH_SET_OFF , NULL);

      if (preflash_cycles <= 1) {
        /* Last flash pulse */
        rer->status = RER_WAIT_PUPIL_CONTRACT;
        gettimeofday(&rer->last_rer_flash_ts, NULL);
        break;
      } else {
        /* Generate interval between the pulses */
        usleep(interval_pulese_ms*1000);
      }
      preflash_cycles--;
    }
  }

  if (rer->status == RER_START) {
    rer->status = RER_DONE;
  }

  return SENSOR_SUCCESS;
}
Exemplo n.º 27
0
boolean sensor_init_probe(module_sensor_ctrl_t *module_ctrl)
{
    int32_t                     rc = 0, dev_fd = 0, sd_fd = 0;
    uint32_t                    i = 0;
    struct media_device_info    mdev_info;
    int32_t                     num_media_devices = 0;
    char                        dev_name[32];
    char                        subdev_name[32];
    struct sensor_init_cfg_data cfg;
    boolean                     ret = TRUE;

    while (1) {
        int32_t num_entities = 1;
        snprintf(dev_name, sizeof(dev_name), "/dev/media%d", num_media_devices);
        dev_fd = open(dev_name, O_RDWR | O_NONBLOCK);
        if (dev_fd < 0) {
            SLOW("Done enumerating media devices");
            break;
        }
        num_media_devices++;
        rc = ioctl(dev_fd, MEDIA_IOC_DEVICE_INFO, &mdev_info);
        if (rc < 0) {
            SLOW("Done enumerating media devices");
            close(dev_fd);
            break;
        }

        if (strncmp(mdev_info.model, "msm_config", sizeof(mdev_info.model) != 0)) {
            close(dev_fd);
            continue;
        }

        while (1) {
            struct media_entity_desc entity;
            memset(&entity, 0, sizeof(entity));
            entity.id = num_entities++;
            SLOW("entity id %d", entity.id);
            rc = ioctl(dev_fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
            if (rc < 0) {
                SLOW("Done enumerating media entities");
                rc = 0;
                break;
            }
            SLOW("entity name %s type %d group id %d",
                 entity.name, entity.type, entity.group_id);
            if (entity.type == MEDIA_ENT_T_V4L2_SUBDEV &&
                    entity.group_id == MSM_CAMERA_SUBDEV_SENSOR_INIT) {
                snprintf(subdev_name, sizeof(dev_name), "/dev/%s", entity.name);
                break;
            }
        }
        close(dev_fd);
    }

    /* Open sensor_init subdev */
    sd_fd = open(subdev_name, O_RDWR);
    if (sd_fd < 0) {
        SHIGH("Open sensor_init subdev failed");
        return FALSE;
    }

    /* Open sensor libraries and get init information */
    for (i = 0; i < ARRAY_SIZE(sensor_libs); i++) {
        ret = sensor_probe(sd_fd, sensor_libs[i]);
        if (ret == FALSE) {
            SERR("failed: to load %s", sensor_libs[i]);
        }
    }

    cfg.cfgtype = CFG_SINIT_PROBE_DONE;
    if (ioctl(sd_fd, VIDIOC_MSM_SENSOR_INIT_CFG, &cfg) < 0) {
        SERR("failed");
        ret = FALSE;
    }
    close(sd_fd);

    return TRUE;
}
Exemplo n.º 28
0
grn_id
grn_plugin_open(grn_ctx *ctx, const char *filename)
{
  grn_id id = GRN_ID_NIL;
  grn_dl dl;
  grn_plugin **plugin = NULL;
  size_t filename_size;

  filename_size = GRN_PLUGIN_KEY_SIZE(filename);

  CRITICAL_SECTION_ENTER(grn_plugins_lock);
  if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, filename_size,
                         (void **)&plugin))) {
    (*plugin)->refcount++;
    goto exit;
  }

#ifdef GRN_WITH_MRUBY
  {
    const char *mrb_suffix;
    mrb_suffix = grn_plugin_get_ruby_suffix();
    if (filename_size > strlen(mrb_suffix) &&
      strcmp(filename + (strlen(filename) - strlen(mrb_suffix)),
             mrb_suffix) == 0) {
      id = grn_plugin_open_mrb(ctx, filename, filename_size);
      goto exit;
    }
  }
#endif /* GRN_WITH_MRUBY */

  if ((dl = grn_dl_open(filename))) {
    if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, filename_size,
                           (void **)&plugin, NULL))) {
      *plugin = GRN_GMALLOCN(grn_plugin, 1);
      if (*plugin) {
        grn_memcpy((*plugin)->path, filename, filename_size);
        if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) {
          GRN_GFREE(*plugin);
          *plugin = NULL;
        }
      }
      if (!*plugin) {
        grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL);
        if (grn_dl_close(dl)) {
          /* Now, __FILE__ set in plugin is invalid. */
          ctx->errline = 0;
          ctx->errfile = NULL;
        } else {
          const char *label;
          label = grn_dl_close_error_label();
          SERR("%s", label);
        }
        id = GRN_ID_NIL;
      } else {
        (*plugin)->refcount = 1;
      }
    } else {
      if (!grn_dl_close(dl)) {
        const char *label;
        label = grn_dl_close_error_label();
        SERR("%s", label);
      }
    }
  } else {
    const char *label;
    label = grn_dl_open_error_label();
    SERR("%s", label);
  }

exit:
  CRITICAL_SECTION_LEAVE(grn_plugins_lock);

  return id;
}
/** mesh_rolloff_V4_UpScaleOTPMesh_9x7to10x13:
 *    @MeshIn: Input Mesh  9x7
 *    @MeshOut: Output Mesh   13x10
 *    @width: Sensor width
 *    @height: Sensor height
 *    @dh: Horizontal offset of OTP grid from top-left corner of image
 *    @dv: Vertical offset of OTP grid from top-left corner of image
 *
 *  This function resamples the 9x7 OTP data into the rolloff 13x10 grid
 *  This function assumed that 9x7 OTP data is unifrom
 *  This function is called by sonyimx135_format_lensshading
 *
 * Return:
 * void
 **/
void mesh_rolloff_V4_UpScaleOTPMesh_9x7to10x13(float *MeshIn,  float *MeshOut,
      int width, int height, int Dh, int Dv)
{
  float cxm, cx0, cx1, cx2, cym, cy0, cy1, cy2;
  float am, a0, a1, a2, bm, b0, b1, b2;
  float tx , ty;
  int ix, iy;
  int i, j;

  /* Initialize the roll-off mesh grid */
  int level,scale, w, h, sgh, sgv, gh, gv, dh, dv;
  int sign = 0;
  int MESH_H = 12; /* the rolloff block number (horizontal). */
  int MESH_V = 9;  /* the rolloff block number (vertical). */
  int Nx = 9;    /* the OTP grid number (horizontal) */
  int Ny = 7;    /* the OTP grid number (vertical) */
  int Gh, Gv;
  float *Extend_Mesh;

  interp_grid_optimization(width, height, &scale, &dh, &dv, &sgh, &sgv);
  gh = sgh * scale;
  gv = sgv * scale;

  /* outer extend the mesh data 1 block by keeping the same slope */
  Dh = Dh/2; /* convert into the size in per-channel image */
  Dv = Dv/2;
  Gh = (width/2-2*Dh)/(Nx-1);  /* OTP block size (horizontal) */
  Gv = (height/2-2*Dv)/(Ny-1);  /* OTP block size (vertical) */

  Extend_Mesh = (float*) malloc( sizeof(float) * (Nx+2)*(Ny+2));
  if (Extend_Mesh == NULL) {
    SERR("%s: malloc failed",__func__);
    return;
  }
  for (i=1; i<Ny+1; i++)
    for (j=1; j<Nx+1; j++)
      Extend_Mesh[i*(Nx+2)+j] = MeshIn[(i-1)*Nx+j-1];


  Extend_Mesh[0*(Nx+2)+0] = Extend_Mesh[1*(Nx+2)+1]*2- Extend_Mesh[2*(Nx+2)+2];
  Extend_Mesh[(Ny+1)*(Nx+2)+0] =
    Extend_Mesh[(Ny)*(Nx+2)+1]*2- Extend_Mesh[(Ny-1)*(Nx+2)+2];
  Extend_Mesh[(Ny+1)*(Nx+2)+Nx+1] =
    Extend_Mesh[(Ny)*(Nx+2)+Nx]*2- Extend_Mesh[(Ny-1)*(Nx+2)+Nx-1];
  Extend_Mesh[0*(Nx+2)+Nx+1] =
    Extend_Mesh[1*(Nx+2)+Nx]*2- Extend_Mesh[2*(Nx+2)+Nx-1];

  for (i=1; i<Ny+1; i++){
    Extend_Mesh[i*(Nx+2)+0] = Extend_Mesh[i*(Nx+2)+1]*2 -
      Extend_Mesh[i*(Nx+2)+2];
    Extend_Mesh[i*(Nx+2)+Nx+1] = Extend_Mesh[i*(Nx+2)+Nx]*2 -
      Extend_Mesh[i*(Nx+2)+Nx-1];
  }

  for (j=1; j<Nx+1; j++){
    Extend_Mesh[0*(Nx+2)+j] = Extend_Mesh[1*(Nx+2)+j]*2 -
      Extend_Mesh[2*(Nx+2)+j];
    Extend_Mesh[(Ny+1)*(Nx+2)+j] = Extend_Mesh[(Ny)*(Nx+2)+j]*2 -
      Extend_Mesh[(Ny-1)*(Nx+2)+j];
  }

  /*  resample Extended Mesh data onto the roll-off mesh grid */
  for (i = 0; i < (MESH_V + 1); i++) {
    for (j = 0; j < (MESH_H + 1); j++) {
      tx =  (float)((double)(j*gh-dh -Dh + Gh)/ (double)(Gh));
      ix = floor(tx);
      tx -= (double)ix;

      ty =  (float)((double)(i*gv-dv -Dv + Gv)/(double)(Gv));
      iy = floor(ty);
      ty -= (double)iy;

      if (i == 0 || j == 0  || i == MESH_V|| j == MESH_H){
      /* for boundary points, use bilinear interpolation */
          b1 = (1 - tx)* Extend_Mesh[iy    *(Nx+2) + ix] +
            tx* Extend_Mesh[iy    *(Nx+2) + ix+1];
          b2 = (1 - tx)* Extend_Mesh[(iy+1)*(Nx+2) + ix] +
            tx* Extend_Mesh[(iy+1)*(Nx+2) + ix+1];
          MeshOut[(i * (MESH_H + 1)) + j] = (float)((1 - ty)*b1 + ty*b2);
      } else { /* for nonboundary points, use bicubic interpolation */
              /*get x direction coeff and y direction coeff*/
          CUBIC_F(tx, cxm, cx0, cx1, cx2);
          CUBIC_F(ty, cym, cy0, cy1, cy2);

          am = Extend_Mesh[(iy-1) *(Nx+2)+ (ix-1)];
          a0 = Extend_Mesh[(iy-1) *(Nx+2)+ (ix  )];
          a1 = Extend_Mesh[(iy-1) *(Nx+2)+ (ix+1)];
          a2 = Extend_Mesh[(iy-1) *(Nx+2)+ (ix+2)];
          bm = ((cxm * am) + (cx0 * a0) + (cx1 * a1) + (cx2 * a2));

          am = Extend_Mesh[(iy  ) *(Nx+2)+ (ix-1)];
          a0 = Extend_Mesh[(iy  ) *(Nx+2)+ (ix  )];
          a1 = Extend_Mesh[(iy  ) *(Nx+2)+ (ix+1)];
          a2 = Extend_Mesh[(iy  ) *(Nx+2)+ (ix+2)];
          b0 = ((cxm * am) + (cx0 * a0) + (cx1 * a1) + (cx2 * a2));

          am = Extend_Mesh[(iy+1) *(Nx+2)+ (ix-1)];
          a0 = Extend_Mesh[(iy+1) *(Nx+2)+ (ix  )];
          a1 = Extend_Mesh[(iy+1) *(Nx+2)+ (ix+1)];
          a2 = Extend_Mesh[(iy+1) *(Nx+2)+ (ix+2)];
          b1 = ((cxm * am) + (cx0 * a0) + (cx1 * a1) + (cx2 * a2));

          am = Extend_Mesh[(iy+2) *(Nx+2)+ (ix-1)];
          a0 = Extend_Mesh[(iy+2) *(Nx+2)+ (ix  )];
          a1 = Extend_Mesh[(iy+2) *(Nx+2)+ (ix+1)];
          a2 = Extend_Mesh[(iy+2) *(Nx+2)+ (ix+2)];
          b2 = ((cxm * am) + (cx0 * a0) + (cx1 * a1) + (cx2 * a2));

          MeshOut[(i * (MESH_H + 1)) + j] =  (float)
            ((cym * bm) + (cy0 * b0) + (cy1 * b1) + (cy2 * b2));
      } /* else */
    }/*for (j = 0; j < (MESH_H + 1); j++) */
  }/* for (i = 0; i < (MESH_V + 1); i++) */

  /* free memory */
  free(Extend_Mesh);
}
Exemplo n.º 30
0
static int32_t led_flash_open(void **led_flash_ctrl, const char *subdev_name)
{
  int32_t rc = SENSOR_SUCCESS;
  sensor_led_flash_data_t *ctrl = NULL;
  struct msm_camera_led_cfg_t cfg;
  char subdev_string[32];

  SLOW("Enter");
  if (!led_flash_ctrl || !subdev_name) {
    SERR("failed sctrl %p subdev name %p",
      led_flash_ctrl, subdev_name);
    return SENSOR_ERROR_INVAL;
  }
  ctrl = malloc(sizeof(sensor_led_flash_data_t));
  if (!ctrl) {
    SERR("failed");
    return SENSOR_FAILURE;
  }
  memset(ctrl, 0, sizeof(sensor_led_flash_data_t));

  snprintf(subdev_string, sizeof(subdev_string), "/dev/%s", subdev_name);
  SLOW("sd name %s", subdev_string);
  /* Open subdev */
  ctrl->fd = open(subdev_string, O_RDWR);
  if (ctrl->fd < 0) {
    SERR("failed");
    rc = SENSOR_FAILURE;
    goto ERROR1;
  }

  ctrl->rer = malloc(sizeof(rer_cfg_t));
  if (!ctrl->rer) {
    SERR("failed");
    rc = SENSOR_FAILURE;
    goto ERROR1;
  }
  memset(ctrl->rer, 0, sizeof(rer_cfg_t));

  ctrl->rer->cfg = malloc(sizeof(red_eye_reduction_type));
  if (!ctrl->rer->cfg) {
    SERR("failed");
    rc = SENSOR_FAILURE;
    goto ERROR2;
  }
  memset(ctrl->rer->cfg, 0, sizeof(red_eye_reduction_type));

  ctrl->dual_led_setting = malloc(sizeof(awb_dual_led_settings_t));
  if (!ctrl->dual_led_setting) {
    SERR("failed");
    rc = SENSOR_FAILURE;
    goto ERROR3;
  }
  memset(ctrl->dual_led_setting, 0, sizeof(awb_dual_led_settings_t));

  cfg.cfgtype = MSM_CAMERA_LED_INIT;
  rc = ioctl(ctrl->fd, VIDIOC_MSM_FLASH_LED_DATA_CFG, &cfg);
  if (rc < 0) {
    SERR("VIDIOC_MSM_FLASH_LED_DATA_CFG failed %s", strerror(errno));
    goto ERROR4;
  }

  *led_flash_ctrl = (void *)ctrl;
  SLOW("Exit");
  return rc;

ERROR4:
  free(ctrl->dual_led_setting);
ERROR3:
  free(ctrl->rer->cfg);
ERROR2:
  free(ctrl->rer);
ERROR1:
  free(ctrl);
  return rc;
}