示例#1
0
ERL_NIF_TERM x_pool_lookup(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    uint64_t id;
    char pool_name[MAX_NAME_LEN];
    if (!enif_get_uint64(env, argv[0], &id) ||
        !enif_get_string(env, argv[1], pool_name, MAX_NAME_LEN, ERL_NIF_LATIN1))
    {
        return enif_make_badarg(env);
    }

    rados_t cluster = map_cluster_get(id);
    if (cluster == NULL)
    {
        return enif_make_badarg(env);
    }

    int64_t err = rados_pool_lookup(cluster, pool_name);
    if (err < 0) 
    {
        return make_error_tuple(env, -err);
    }

    return enif_make_tuple2(env,
                            enif_make_atom(env, "ok"),
                            enif_make_int64(env, err));  // Pool ID
}
示例#2
0
ERL_NIF_TERM x_pool_create_for_user(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    uint64_t id;
    char pool_name[MAX_NAME_LEN];
    uint64_t uid;
    if (!enif_get_uint64(env, argv[0], &id) ||
        !enif_get_string(env, argv[1], pool_name, MAX_NAME_LEN, ERL_NIF_LATIN1) ||
        !enif_get_uint64(env, argv[2], &uid))
    {
        return enif_make_badarg(env);
    }

    rados_t cluster = map_cluster_get(id);
    if (cluster == NULL)
    {
        return enif_make_badarg(env);
    }

    int err = rados_pool_create_with_auid(cluster, pool_name, uid);
    if (err < 0) 
    {
        return make_error_tuple(env, -err);
    }
    return enif_make_atom(env, "ok");
}
示例#3
0
//key, incr, ttl, timestamp
static ERL_NIF_TERM update(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {

    int ttl=0, timestamp=0, incr=0, next=0;
    CHECK(enif_get_string(env, argv[0], keybuff, KEY_MAX_LEN, ERL_NIF_LATIN1));
    CHECK(enif_get_int(env, argv[1], &incr));
    CHECK(enif_get_int(env, argv[2], &ttl));
    CHECK(enif_get_int(env, argv[3], &timestamp));

    metronome_item * item = find_in_hash(keybuff, timestamp);
    item->ttl = ttl;

    if(item->timestamp + ttl > timestamp) {
        item->value += incr;
        next = item->timestamp + ttl - timestamp;
    } else {
        item->value = incr;
        item->timestamp = timestamp;
        next = ttl;
    }

    //
    return enif_make_tuple3(env,
                            enif_make_atom(env, "ok"),
                            enif_make_int(env, item->value),
                            enif_make_int(env, next)
                           );
}
示例#4
0
int decode_command_call_into_process(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], process_t **ptr)
{
    // Instantiate a new process
    if (pm_new_process(ptr))
        return -1;

    process_t *process = *ptr;
    const ERL_NIF_TERM* big_tuple;
    int arity = 2;
    // Get the outer tuple
    if(!enif_get_tuple(env, argv[0], &arity, &big_tuple)) return -1;

    // The first command is a string
    char command[MAX_BUFFER_SZ], key[MAX_BUFFER_SZ], value[MAX_BUFFER_SZ];
    memset(&command, '\0', sizeof(command));

    // Get the command
    if (enif_get_string(env, big_tuple[0], command, sizeof(command), ERL_NIF_LATIN1) < 0) return -1;
    pm_malloc_and_set_attribute(&process->command, command);

    // The second element of the tuple is a list of options
    const ERL_NIF_TERM* tuple;
    ERL_NIF_TERM head, tail, list = big_tuple[1];

    // int enif_get_tuple(ErlNifEnv* env, ERL_NIF_TERM term, int* arity, const ERL_NIF_TERM** array)
    while(enif_get_list_cell(env, list, &head, &tail)) {
        // Get the tuple
        if(!enif_get_tuple(env, head, &arity, &tuple)) return -1;
        // First element is an atom
        if (!enif_get_atom(env, tuple[0], key, sizeof(key))) return -2;
        if (enif_get_string(env, tuple[1], value, sizeof(value), ERL_NIF_LATIN1) < 0) return -3;
        if (!strcmp(key, "do_before")) {
            // Do before
            pm_malloc_and_set_attribute(&process->before, value);
        } else if (!strcmp(key, "do_after")) {
            pm_malloc_and_set_attribute(&process->after, value);
        } else if (!strcmp(key, "cd")) {
            pm_malloc_and_set_attribute(&process->cd, value);
        } else if (!strcmp(key, "env")) {
            pm_add_env(&process, value);
        } else if (!strcmp(key, "nice")) {
            process->nice = atoi(value);
        }
        list = tail;
    }
    return 0;
}
static ERL_NIF_TERM _test(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
	UNUSED(argc);

	char result[MAXBUFLEN];
	char path[MAXBUFLEN];

	enif_get_string(env, argv[0], path, 1024, ERL_NIF_LATIN1);
	return enif_make_string(env, result, ERL_NIF_LATIN1);
}
示例#6
0
static ERL_NIF_TERM
geef_object_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char path[MAXBUFLEN];
  char sha[MAXBUFLEN];
  (void)memset(&path, '\0', sizeof(path));
  (void)memset(&sha, '\0', sizeof(sha));

  if (enif_get_string(env, argv[0], path, sizeof(path), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);
  if (enif_get_string(env, argv[1], sha, sizeof(sha), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);

  git_odb *odb;
  git_odb_open(&odb, path);

  git_oid oid;
  git_oid_fromstr(&oid, sha);

  int error;
  git_odb_object *obj;

  error = git_odb_read(&obj, odb, &oid);
  if (error)
  {
    git_odb_free(odb);
    return enif_make_tuple2(env,
        enif_make_atom(env, "error"),
        enif_make_string(env, "object does not exist", ERL_NIF_LATIN1));
  }

  size_t len;
  len = git_odb_object_size(obj);

  ErlNifBinary ibin;
  enif_alloc_binary(len, &ibin);
  memcpy(ibin.data, git_odb_object_data(obj), len);

  git_odb_free(odb);

  return enif_make_binary(env, &ibin);
}
示例#7
0
static ERL_NIF_TERM nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
    int option;
    int facility;
    if ( (enif_get_string(env, argv[0], (char *)ident, sizeof(ident), ERL_NIF_LATIN1) < 1) ||
         !enif_get_int(env, argv[1], &option) ||
         !enif_get_int(env, argv[2], &facility)) {
        return enif_make_badarg(env);
    }
    openlog(ident, option, facility);
    return enif_make_atom(env, "ok");
}
示例#8
0
static ERL_NIF_TERM nif_write(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
    char message[MAXBUFLEN];
    int log_level;
    if (!enif_get_int(env, argv[0], &log_level)) {
        return enif_make_badarg(env);
    }
    if (enif_get_string(env, argv[1], message, sizeof(message), ERL_NIF_LATIN1) < 1) {
        return enif_make_badarg(env);
    }
    syslog(log_level, "%s", message);
    return enif_make_atom(env, "ok");
}
示例#9
0
static ERL_NIF_TERM hashpw(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    char pw[1024];
    char salt[1024];
    char *ret = NULL;

    (void)memset(&pw, '\0', sizeof(pw));
    (void)memset(&salt, '\0', sizeof(salt));

    if (enif_get_string(env, argv[0], pw, sizeof(pw), ERL_NIF_LATIN1) < 1)
        return enif_make_badarg(env);

    if (enif_get_string(env, argv[1], salt, sizeof(salt), ERL_NIF_LATIN1) < 1)
        return enif_make_badarg(env);

    if (NULL == (ret = bcrypt(pw, salt)) || 0 == strcmp(ret, ":")) {
        return enif_make_badarg(env);
    }

    return enif_make_string(env, ret, ERL_NIF_LATIN1);
}
示例#10
0
static int x509_parse_subject(ErlNifEnv* env, ERL_NIF_TERM subject_tuple, int *num_subject_entries, x509_subject_entry **subject_entries){
  int num_subject_tuple;
  unsigned num_subject_terms;
  ERL_NIF_TERM head, tail;
  int pair_arity;
  char *name;
  char *value;
  char *subject_string = NULL;
  const ERL_NIF_TERM* pair;
  int idx;
  x509_subject_entry* se;
  unsigned value_len;
  const ERL_NIF_TERM* subject_terms;

  *subject_entries = NULL;
  *num_subject_entries = 0;

  /* make sure this is a tuple with first term 'subject' */
  if(!enif_get_tuple(env, subject_tuple, &num_subject_tuple, &subject_terms) ||
     !atom_to_string(env, subject_terms[0], &subject_string) ||
     strncmp(subject_string, SUBJECT_STR, subject_strlen)) {
    if(subject_string) free(subject_string);
    return 0;
  }
  free(subject_string);

  /* create room for the x509_subject_entry structs */
  if(!enif_get_list_length(env, subject_terms[1], &num_subject_terms) || 
     (NULL == (se = (x509_subject_entry*)malloc(num_subject_terms * sizeof(x509_subject_entry))))) return 0;

  /* get the first entry and prime the pump for walking the rest */
  if(!enif_get_list_cell(env, subject_terms[1], &head, &tail) || 
     !enif_get_tuple(env, head, &pair_arity, &pair) ||
     pair_arity!=2) { return 0; }

  for(idx=0; idx<num_subject_terms; idx++){
    atom_to_string(env, pair[0], &name);

    enif_get_list_length(env, pair[1], &value_len);
    value = (char*)malloc(value_len+1);
    enif_get_string(env, pair[1], value, value_len+1, ERL_NIF_LATIN1);
    (se+idx)->name = name;
    (se+idx)->value = value;

    if(!enif_get_list_cell(env, tail, &head, &tail) || 
       !enif_get_tuple(env, head, &pair_arity, &pair) ||
       pair_arity!=2) { break; }
  }

  *num_subject_entries = num_subject_terms;
  *subject_entries = se;
  return 1;
}
示例#11
0
文件: lgeo_ogr.c 项目: namjae/lgeo
/* OGRDataSourceH CPL_DLL OGROpen(const char *, int, OGRSFDriverH *)
    CPL_WARN_UNUSED_RESULT;

{ok, DataSource} = lgeo_ogr:open("test/polygon.shp").
{ok, DataSource} = lgeo_ogr:open("test/polygon.shp", 1).

*/
static ERL_NIF_TERM
open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int update = 0; // read-only (default)
    OGRDataSourceH datasource;
    ERL_NIF_TERM eterm;
    /*
    OGRSFDriverH  *pahDriver;
    ERL_NIF_TERM eterm1, eterm2;
    */

    unsigned len;
    if (argc > 0 && !enif_get_list_length(env, argv[0], &len)) {
        return enif_make_badarg(env);
    }
    char * filename = enif_alloc(sizeof(char)*(len+1));

    if(!enif_get_string(env, argv[0], filename, len+1, ERL_NIF_LATIN1)) {
        return enif_make_badarg(env);
    }

    if (argc == 2 && !enif_get_int(env, argv[1], &update)) {
        return enif_make_badarg(env);
    }

    datasource = OGROpen(filename, update, NULL);
    //datasource = OGROpen(filename, upadate, pahDriver);

    enif_free(filename);
    if(datasource == NULL) {
        return enif_make_atom(env, "undefined");
    }

    OGRDataSourceH **hDS = \
        enif_alloc_resource(OGR_DS_RESOURCE, sizeof(OGRDataSourceH*));
    *hDS = datasource;

    /*
    OGRSFDriverH **hDriver = \
        enif_alloc_resource(OGR_D_RESOURCE, sizeof(OGRSFDriverH*));
    *hDriver = *pahDriver;
    */

    eterm = enif_make_resource(env, hDS);
    enif_release_resource(hDS);
    return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm);
    /*
    eterm2 = enif_make_resource(env, hDriver);
    enif_release_resource(hDriver);
    return enif_make_tuple2(env, eterm1, eterm1);
    */
}
示例#12
0
// convert an erlang term to a python object
// return None if the type can't be converted
static PyObject* pynerl_term_to_obj(ErlNifEnv* env, ERL_NIF_TERM term) {
	int vint;
	Py_ssize_t arity, i;
	long int vlong;
	double vdouble;
	char buff[BUFF_SIZE];
	PyObject* obj;
	ERL_NIF_TERM list, head, tail;
	const ERL_NIF_TERM *terms;

	// TODO: add more types
	if (enif_get_long(env, term, &vlong)) {
		obj = PyLong_FromLong(vlong);
	}
	else if (enif_get_double(env, term, &vdouble)) {
		obj = PyFloat_FromDouble(vlong);
	}
	else if (enif_is_empty_list(env, term)) {
		obj = PyList_New(0);
	}
	else if (enif_get_tuple(env, term, &vint, &terms)) {
		arity = vint;
		obj = PyTuple_New(arity);

		for (i = 0; i < arity; i++) {
			PyTuple_SetItem(obj, i, pynerl_term_to_obj(env, terms[(int)i]));
		}
	}
	else if (enif_is_identical(term, enif_make_atom(env, "true"))) {
		obj = Py_True;
	}
	else if (enif_is_identical(term, enif_make_atom(env, "false"))) {
		obj = Py_False;
	}
	else if (enif_get_string(env, term, buff, BUFF_SIZE, ERL_NIF_LATIN1)) {
		obj = PyUnicode_FromString(buff);
	}
	else if (enif_get_list_cell(env, term, &head, &tail)) {
		obj = PyList_New(0);
		list = term;

		while (enif_get_list_cell(env, list, &head, &tail)) {
			PyList_Append(obj, pynerl_term_to_obj(env, head));
			list = tail;
		}
	}
	else {
		obj = Py_None;
	}

	return obj;
}
示例#13
0
static ERL_NIF_TERM string_to_bin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary obin;
    unsigned size;
    int n;
    if (!enif_get_int(env,argv[1],(int*)&size) 
	|| !enif_alloc_binary(size,&obin)) {
	return enif_make_badarg(env);
    }
    n = enif_get_string(env, argv[0], (char*)obin.data, size, ERL_NIF_LATIN1);
    return enif_make_tuple(env, 2, enif_make_int(env,n),
			   enif_make_binary(env,&obin));
}
示例#14
0
static ERL_NIF_TERM helloworld (ErlNifEnv* env, int argc,
                                          const ERL_NIF_TERM argv[])
{
    unsigned int length = 0;
    enif_get_list_length(env, argv[0], &length);

    char *name = (char *)enif_alloc(++length);
    enif_get_string(env, argv[0], name, length, ERL_NIF_LATIN1);
    printf("Hello, %s!\n", name);
    enif_free(name);
    
    return enif_make_atom(env, "ok");
}
示例#15
0
static ERL_NIF_TERM exec_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  unsigned path_length, args_count, arg_length;
  ERL_NIF_TERM head, tail;
  int i = 0;

  enif_get_list_length(env, argv[0], &path_length);
  enif_get_list_length(env, argv[1], &args_count);

  char* exec_argv[args_count + 2];
  char path[path_length + 1];

  if (!enif_get_string(env, argv[0], path, path_length + 1, ERL_NIF_LATIN1) || !enif_is_list(env, argv[1])) {
    return enif_make_badarg(env);
  }

  tail = argv[1];
  while(enif_get_list_cell(env, tail, &head, &tail) != 0) {
    enif_get_list_length(env, head, &arg_length);

    char* arg = (char*) malloc(sizeof(char) * (arg_length + 1));

    if (!enif_get_string(env, head, arg, arg_length + 1, ERL_NIF_LATIN1)) {
      return enif_make_badarg(env);
    }

    exec_argv[i + 1] = arg;

    i++;
  }

  exec_argv[0] = path;
  exec_argv[args_count + 1] = NULL;

  execv(path, exec_argv);

  return enif_make_atom(env, "ok");
}
示例#16
0
static ERL_NIF_TERM bf_expand(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
	ErlNifBinary state;
	char key[1024];
	char salt[1024];
	uint8_t key_len;
	unsigned long key_len_arg;
	uint8_t salt_len;

	if (argc != 4 || !enif_inspect_binary(env, argv[0], &state))
		return enif_make_badarg(env);
	if (!enif_get_string(env, argv[1], key, sizeof(key), ERL_NIF_LATIN1) ||
			!enif_get_ulong(env, argv[2], &key_len_arg) ||
			!enif_get_string(env, argv[3], salt, sizeof(salt), ERL_NIF_LATIN1))
		return enif_make_badarg(env);
	key_len = key_len_arg;
	salt_len = BCRYPT_MAXSALT;

	Blowfish_expand0state((blf_ctx *) state.data, (uint8_t *) key, key_len);
	Blowfish_expand0state((blf_ctx *) state.data, (uint8_t *) salt, salt_len);

	return enif_make_binary(env, &state);
}
示例#17
0
static ERL_NIF_TERM pynerl_call(ErlNifEnv* env, int arc, const ERL_NIF_TERM argv[]) {
	Py_Initialize();

	int size;
	char buff[BUFF_SIZE];
	PyObject *pModName, *pModule, *pFunc, *pArgs, *pArgList, *pValue;
	ERL_NIF_TERM eResult;

	pModName = pynerl_term_to_obj(env, argv[0]);

    pModule = PyImport_Import(pModName);
    Py_DECREF(pModName);

    if (pModule != NULL) {
		// TODO: error checking
		enif_get_string(env, argv[1], buff, BUFF_SIZE, ERL_NIF_LATIN1);
		pFunc = PyObject_GetAttrString(pModule, buff);

        if (pFunc && PyCallable_Check(pFunc)) {
			pArgList = pynerl_term_to_obj(env, argv[2]);

            pArgs = PyList_AsTuple(pArgList);
			Py_DECREF(pArgList);
			pValue = PyObject_CallObject(pFunc, pArgs);
			Py_DECREF(pArgs);

			if (pValue != NULL) {
				eResult = pynerl_obj_to_term(env, pValue);
				Py_DECREF(pValue);
			}
			else {
				eResult = pynerl_make_error(env, "exception", "Exception while calling function");
			}

			Py_DECREF(pFunc);
		}
		else {
			eResult = pynerl_make_error(env, "nofun", "No function in module");
		}

		Py_DECREF(pModule);
    }
	else {
		eResult = pynerl_make_error(env, "nomodule", "Module doesn't exist");
    }

	Py_Finalize();

	return eResult;
}
示例#18
0
文件: cb.c 项目: muut/cberl
void *cb_connect_args(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    connect_args_t* args = (connect_args_t*)enif_alloc(sizeof(connect_args_t));

    unsigned arg_length;
    if (!enif_get_list_length(env, argv[0], &arg_length)) goto error0;
    args->host = (char *) malloc(arg_length + 1);
    if (!enif_get_string(env, argv[0], args->host, arg_length + 1, ERL_NIF_LATIN1)) goto error1;

    if (!enif_get_list_length(env, argv[1], &arg_length)) goto error1;
    args->user = (char *) malloc(arg_length + 1);
    if (!enif_get_string(env, argv[1], args->user, arg_length + 1, ERL_NIF_LATIN1)) goto error2;

    if (!enif_get_list_length(env, argv[2], &arg_length)) goto error2;
    args->pass = (char *) malloc(arg_length + 1);
    if (!enif_get_string(env, argv[2], args->pass, arg_length + 1, ERL_NIF_LATIN1)) goto error3;

    if (!enif_get_list_length(env, argv[3], &arg_length)) goto error3;
    args->bucket = (char *) malloc(arg_length + 1);
    if (!enif_get_string(env, argv[3], args->bucket, arg_length + 1, ERL_NIF_LATIN1)) goto error4;

    return (void*)args;

    error4:
    free(args->bucket);
    error3:
    free(args->pass);
    error2:
    free(args->user);
    error1:
    free(args->host);
    error0:
    enif_free(args);

    return NULL;
}
示例#19
0
    int
notify_hints_type(ErlNifEnv *env, NotifyNotification *notify, int arity, ERL_NIF_TERM key, ERL_NIF_TERM value)
{
    char s_key[1024] = {0};

    char s_value[1024] = {0};
    int i_value = 0;
    double d_value = 0;

    const ERL_NIF_TERM *byte = NULL;
    char s_byte[256] = {0};
    int len = 0;


    if (!enif_get_string(env, key, s_key, sizeof(s_key), ERL_NIF_LATIN1))
        return -1;

    if (enif_get_int(env, value, &i_value))
        notify_notification_set_hint_int32(notify, s_key, (value == atom_undefined ? 0 : i_value));
    else if (enif_get_double(env, value, &d_value))
        notify_notification_set_hint_double(notify, s_key, (value == atom_undefined ? 0 : d_value));
    else if (enif_get_tuple(env, value, &len, &byte)) {
        if ( (len != 2) ||
                !enif_get_atom(env, byte[0], s_byte, sizeof(s_byte), ERL_NIF_LATIN1) || 
                (strcmp(s_byte, "byte") != 0) ||
                !enif_get_int(env, byte[1], &i_value))
            return -1;
        notify_notification_set_hint_byte(notify, s_key, (value == atom_undefined ? 0 : (u_int8_t)i_value));
    }
    else if ((arity == 0) || enif_get_string(env, value, s_value, sizeof(s_value), ERL_NIF_LATIN1))
        notify_notification_set_hint_string(notify, s_key, (value == atom_undefined ? "" : s_value));
    else
        return -1;

    return 0;
}
示例#20
0
文件: syslog.c 项目: argv0/syslog
static ERL_NIF_TERM slog(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{

  int priority = LOG_DEBUG;
  enif_get_int(env, argv[0], &priority);

  unsigned int length = 0;
  enif_get_list_length(env, argv[1], &length);

  char *buf = (char *)enif_alloc(++length);
  enif_get_string(env,argv[1], buf, length, ERL_NIF_LATIN1);
  syslog(priority,"%s",buf);
  enif_free(buf);

  return enif_make_atom(env, ENIF_OK);
}
示例#21
0
文件: wpi.c 项目: alepharchives/wpi
static ERL_NIF_TERM
serial_puts_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int handle, str_len;
    if (!enif_get_int(env, argv[0], &handle) ||
        !enif_get_int(env, argv[1], &str_len))
    {
        return enif_make_badarg(env);
    }
    char str[str_len+1];
    if (!enif_get_string(env, argv[2], str, sizeof(str), ERL_NIF_LATIN1))
    {
        return enif_make_badarg(env);
    }
    serialPuts(handle, str);
    return atom_ok;
}
示例#22
0
static ERL_NIF_TERM
geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char sha[MAXBUFLEN];
  (void)memset(&sha, '\0', sizeof(sha));

  if (enif_get_string(env, argv[0], sha, sizeof(sha), ERL_NIF_LATIN1) < 1)
      return enif_make_badarg(env);

  git_oid oid;
  git_oid_fromstr(&oid, sha);

  ErlNifBinary ibin;
  enif_alloc_binary(20, &ibin);
  memcpy(ibin.data, (&oid)->id, 20);

  return enif_make_binary(env, &ibin);
}
示例#23
0
文件: wpi.c 项目: alepharchives/wpi
// serial
static ERL_NIF_TERM
serial_open_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int str_len, baud, handle;
    if (!enif_get_int(env, argv[0], &baud) ||
        !enif_get_int(env, argv[1], &str_len))
    {
        return enif_make_badarg(env);
    }
    char device[str_len+1];
    if (!enif_get_string(env, argv[2], device, sizeof(device), ERL_NIF_LATIN1))
    {
        return enif_make_badarg(env);
    }

    handle = serialOpen(device, baud);
    return enif_make_int(env, handle);
}
示例#24
0
static ERL_NIF_TERM dirty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int n;
    char s[10];
    ErlNifBinary b;
    if (have_dirty_schedulers()) {
	assert(ERL_NIF_THR_DIRTY_CPU_SCHEDULER == enif_thread_type()
	       || ERL_NIF_THR_DIRTY_IO_SCHEDULER == enif_thread_type());
    }
    assert(argc == 3);
    enif_get_int(env, argv[0], &n);
    enif_get_string(env, argv[1], s, sizeof s, ERL_NIF_LATIN1);
    enif_inspect_binary(env, argv[2], &b);
    return enif_make_tuple3(env,
			    enif_make_int(env, n),
			    enif_make_string(env, s, ERL_NIF_LATIN1),
			    enif_make_binary(env, &b));
}
示例#25
0
    static int
load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
{
    char buf[1024] = {0};

    if (enif_get_string(env, load_info, buf, sizeof(buf), ERL_NIF_LATIN1) < 1)
        return -1;

    if (!notify_init(buf))
        return -1;

    atom_ok = enif_make_atom(env, "ok");
    atom_error = enif_make_atom(env, "error");
    atom_nomem = enif_make_atom(env, "enomem");
    atom_undefined = enif_make_atom(env, "undefined");

    return 0;
}
示例#26
0
static ERL_NIF_TERM
nif_scheduler_sendFrameworkMessage(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]){

    ErlNifBinary executorId_binary;
    ErlNifBinary slaveId_binary;
    char data[MAXBUFLEN];

    state_ptr state = (state_ptr) enif_priv_data(env);
    
    if(state->initilised == 0 ) 
    {
        return enif_make_tuple2(env, 
            enif_make_atom(env, "state_error"), 
            enif_make_atom(env, "scheduler_not_inited"));
    }
    if (!enif_inspect_binary(env, argv[0], &executorId_binary)) 
    {
                return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "executor_id"));
    }
    if (!enif_inspect_binary(env, argv[1], &slaveId_binary)) 
    {
                return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "slave_id"));
    }
    //REVIEW : buffer length
    if(!enif_get_string(env, argv[2], data , MAXBUFLEN, ERL_NIF_LATIN1 ))
    {
        return enif_make_tuple3(env, 
                    enif_make_atom(env, "argument_error"), 
                    enif_make_atom(env, "invalid_or_corrupted_parameter"),
                    enif_make_atom(env, "data"));
    }

    SchedulerDriverStatus status = scheduler_sendFrameworkMessage( state->scheduler_state , 
                                                                        &executorId_binary, 
                                                                        &slaveId_binary, 
                                                                        data);
    return get_return_value_from_status(env, status);
}
示例#27
0
文件: spidev.c 项目: nygge/erl-spidev
static ERL_NIF_TERM open_1(ErlNifEnv *env, int argc,
                                     const ERL_NIF_TERM argv[])
{
    char *device;
    unsigned int length;
    uint8_t tmp8;
    uint32_t tmp32;
    
    state.env = env;
    state.fd = -1;
    state.mode = 0;
    state.bits_per_word = 0;
    state.max_speed_hz = 0;
    if (argc != 1 || !enif_is_list(env, argv[0]))
        return enif_make_badarg(env);
    if (!enif_get_list_length(env, argv[0], &length))
        return enif_make_badarg(env);
    device = (char *) enif_alloc(length + 1);
    enif_get_string(env, argv[0], device, length + 1, ERL_NIF_LATIN1);
    if ((state.fd = open(device, O_RDWR, 0)) == -1) {
        return enif_make_badarg(env);
    }
    if (ioctl(state.fd, SPI_IOC_RD_MODE, &tmp8) == -1) {
        close(state.fd);
        enif_free(device);
        return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "read mode", ERL_NIF_LATIN1));
    }
    state.mode = tmp8;
    if (ioctl(state.fd, SPI_IOC_RD_BITS_PER_WORD, &tmp8) == -1) {
        close(state.fd);
        enif_free(device);
        return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "read bits per word", ERL_NIF_LATIN1));
    }
    state.bits_per_word = tmp8;
    if (ioctl(state.fd, SPI_IOC_RD_MAX_SPEED_HZ, &tmp32) == -1) {
        close(state.fd);
        enif_free(device);
        return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "read max speed hz", ERL_NIF_LATIN1));
    }
    state.max_speed_hz = tmp32;
    enif_free(device);
    
    return enif_make_atom(env, "ok");
}
示例#28
0
static ERL_NIF_TERM gdal_nif_tile_to_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    gdal_tile_handle* ti;
    if (!enif_get_resource(env, argv[0], gdal_tile_RESOURCE, (void**)&ti)) {
        return enif_make_badarg(env);
    }

    ErlNifBinary tilefilenameBin;
    if (!enif_inspect_iolist_as_binary(env, argv[1], &tilefilenameBin) || (tilefilenameBin.size >= FILENAME_LEN)) {
        return make_error_msg(env, "filename error, maybe too long");
    }

    char rasterFormatCode[16] = "";
    if (enif_get_string(env, argv[2], rasterFormatCode, 16, ERL_NIF_LATIN1) <= 0) {
        return enif_make_badarg(env);
    }

    char tilefilename[FILENAME_LEN] = "";
    memcpy(tilefilename, tilefilenameBin.data, tilefilenameBin.size);
    DEBUG("passed tilefilename: %s\r\n", tilefilename);

    GDALDriverH hOutDriver = GDALGetDriverByName(rasterFormatCode);
    if ( ! ti->options_resampling || (strcmp("antialias", ti->options_resampling) != 0) ) {
        char vsimemFileName[128] = "";
        sprintf(vsimemFileName, "/vsimem/%s", tilefilename);
        GDALDatasetH tileBinDataset = GDALCreateCopy(hOutDriver,
                                                     vsimemFileName, ti->dstile, 
                                                     FALSE, NULL, NULL, NULL);
        vsi_l_offset binDataLength;
        int bUnlinkAndSeize = FALSE;
        GByte* binData = VSIGetMemFileBuffer(vsimemFileName, &binDataLength, bUnlinkAndSeize);
        DEBUG("vsimem: %s, bin len: %d\r\n", vsimemFileName, binDataLength);

        ERL_NIF_TERM binTerm;
        unsigned char* buf = enif_make_new_binary(env, binDataLength, &binTerm);
        memcpy(buf, binData, binDataLength);
 //       CPLFree(binData);

        GDALClose(tileBinDataset);
        return enif_make_tuple2(env, ATOM_OK, binTerm);
    }

    return make_error_msg(env, "wrong resampling");
}
示例#29
0
文件: elua.c 项目: indie21/elua
static ERL_NIF_TERM
dofile(ErlNifEnv *env, lua_State *L, const ERL_NIF_TERM arg)
{
    char buff_str[STACK_STRING_BUFF];
    int size = enif_get_string(env, arg, buff_str, STACK_STRING_BUFF, ERL_NIF_LATIN1);
    if(size <= 0) {
        return make_error_tuple(env, "invalid_filename");
    }

    if(luaL_dofile(L, buff_str) !=LUA_OK) {
        const char *error = lua_tostring(L, -1);
        ERL_NIF_TERM error_tuple = make_error_tuple(env, error);
        lua_pop(L,1);
        return error_tuple;
    }

    // printf("do file well\n");
    return atom_ok;
}
示例#30
0
static ERL_NIF_TERM call_dirty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int n;
    char s[10];
    ErlNifBinary b;
    assert(ERL_NIF_THR_NORMAL_SCHEDULER == enif_thread_type());
    if (argc != 3)
	return enif_make_badarg(env);
    if (have_dirty_schedulers()) {
	if (enif_get_int(env, argv[0], &n) &&
	    enif_get_string(env, argv[1], s, sizeof s, ERL_NIF_LATIN1) &&
	    enif_inspect_binary(env, argv[2], &b))
	    return enif_schedule_nif(env, "call_dirty_nif", ERL_NIF_DIRTY_JOB_CPU_BOUND, dirty_nif, argc, argv);
	else
	    return enif_make_badarg(env);
    } else {
	return dirty_nif(env, argc, argv);
    }
}