Skip to content

Instantly share code, notes, and snippets.

@msantos
Created September 22, 2011 15:27
Show Gist options
  • Save msantos/1235059 to your computer and use it in GitHub Desktop.
Save msantos/1235059 to your computer and use it in GitHub Desktop.
"Low level" serial interface
/* Copyright (c) 2011, Michael Santos <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "erl_nif.h"
#include "erl_driver.h"
#include "serctl.h"
static ERL_NIF_TERM error_tuple(ErlNifEnv *env, int errnum);
static ERL_NIF_TERM atom_ok;
static ERL_NIF_TERM atom_error;
static ERL_NIF_TERM atom_eagain;
static int
load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{
atom_ok = enif_make_atom(env, "ok");
atom_error = enif_make_atom(env, "error");
atom_eagain = enif_make_atom(env, "eagain");
return (0);
}
static void
unload(ErlNifEnv* env, void *priv_data)
{
}
static ERL_NIF_TERM
nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
char buf[MAXPATHLEN];
int fd = -1;
int flags = 0;
if (enif_get_string(env, argv[0], buf, sizeof(buf), ERL_NIF_LATIN1) < 1)
return (-1);
fd = open(buf, O_RDWR);
if (fd < 0)
return error_tuple(env, errno);
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
return error_tuple(env, errno);
if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
return error_tuple(env, errno);
return enif_make_tuple2(env,
atom_ok,
enif_make_int(env, fd));
}
static ERL_NIF_TERM
nif_close(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int fd = -1;
if (!enif_get_int(env, argv[0], &fd))
return enif_make_badarg(env);
if (close(fd) < 0)
return error_tuple(env, errno);
return atom_ok;
}
static ERL_NIF_TERM
nif_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int fd = -1;
unsigned long len = 0;
ErlNifBinary buf = {0};
ssize_t bufsz = 0;
int err = 0;
if (!enif_get_int(env, argv[0], &fd))
return enif_make_badarg(env);
if (!enif_get_ulong(env, argv[1], &len))
return enif_make_badarg(env);
if (!enif_alloc_binary(len, &buf))
return error_tuple(env, ENOMEM);
if ( (bufsz = read(fd, buf.data, buf.size)) == -1) {
err = errno;
enif_release_binary(&buf);
return error_tuple(env, err);
}
if (bufsz < buf.size && !enif_realloc_binary(&buf, bufsz)) {
enif_release_binary(&buf);
return error_tuple(env, ENOMEM);
}
return enif_make_tuple2(env, atom_ok, enif_make_binary(env, &buf));
}
static ERL_NIF_TERM
nif_write(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int fd = -1;
ErlNifBinary buf = {0};
if (!enif_get_int(env, argv[0], &fd))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[1], &buf))
return enif_make_badarg(env);
if (write(fd, buf.data, buf.size) == -1)
return error_tuple(env, errno);
return atom_ok;
}
static ERL_NIF_TERM
nif_tcgetattr(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int fd = -1;
ErlNifBinary buf = {0};
int err = 0;
if (!enif_get_int(env, argv[0], &fd))
return enif_make_badarg(env);
if (!enif_alloc_binary(sizeof(struct termios), &buf))
return error_tuple(env, ENOMEM);
if (tcgetattr(fd, (struct termios *)buf.data) < 0) {
err = errno;
enif_release_binary(&buf);
return error_tuple(env, err);
}
return enif_make_tuple2(env,
atom_ok,
enif_make_binary(env, &buf));
}
static ERL_NIF_TERM
nif_tcsetattr(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int fd = -1;
int opt = 0;
ErlNifBinary buf = {0};
if (!enif_get_int(env, argv[0], &fd))
return enif_make_badarg(env);
if (!enif_get_int(env, argv[1], &opt))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[2], &buf) || (buf.size != sizeof(struct termios))
return enif_make_badarg(env);
if (tcsetattr(fd, opt, (struct termios *)buf.data) < 0)
return error_tuple(env, errno);
return atom_ok;
}
static ERL_NIF_TERM
nif_cfsetispeed(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary buf = {0};
unsigned int speed = 0;
if (!enif_get_uint(env, argv[0], &speed))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[1], &buf) || (buf.size != sizeof(struct termios))
return enif_make_badarg(env);
if (cfsetispeed((struct termios *)buf.data, speed) < 0)
return error_tuple(env, errno);
return atom_ok;
}
static ERL_NIF_TERM
nif_cfsetospeed(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary buf = {0};
unsigned int speed = 0;
if (!enif_get_uint(env, argv[0], &speed))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[1], &buf) || (buf.size != sizeof(struct termios))
return enif_make_badarg(env);
if (cfsetospeed((struct termios *)buf.data, speed) < 0)
return error_tuple(env, errno);
return atom_ok;
}
static ERL_NIF_TERM
error_tuple(ErlNifEnv *env, int errnum)
{
return enif_make_tuple2(env, atom_error,
enif_make_atom(env, erl_errno_id(errnum)));
}
static ErlNifFunc nif_funcs[] = {
{"open", 1, nif_open},
{"close", 1, nif_close},
{"read", 2, nif_read},
{"write", 2, nif_write},
{"tcgetattr", 1, nif_tcgetattr},
{"tcsetattr", 3, nif_tcsetattr},
{"cfsetispeed", 2, nif_cfsetispeed},
{"cfsetospeed", 2, nif_cfsetospeed}
};
ERL_NIF_INIT(serctl, nif_funcs, load, NULL, NULL, unload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment