From 6e182846ce82233e900d919afaab216181e627c2 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 19 Apr 2014 17:33:59 +0200 Subject: [PATCH 1/1] Start moving the implementation of the public part of the API to meshlink.c. For now it just contains stubs for all the public functions. --- examples/Makefile.am | 4 +- examples/chat.c | 14 ++++--- src/Makefile.am | 2 +- src/meshlink.c | 95 ++++++++++++++++++++++++++++++++++++++++++++ src/meshlink.h | 6 +-- 5 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 src/meshlink.c diff --git a/examples/Makefile.am b/examples/Makefile.am index ce4cb077..baa03715 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -5,5 +5,5 @@ AM_CPPFLAGS = -I../src meshlinkapp_SOURCES = meshlinkapp.c meshlinkapp_LDADD = ../src/libmeshlink.la -meshlinkapp_SOURCES = chat.c -meshlinkapp_LDADD = ../src/libmeshlink.la +chat_SOURCES = chat.c +chat_LDADD = ../src/libmeshlink.la diff --git a/examples/chat.c b/examples/chat.c index 78f9eb27..21ced7b0 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -3,18 +3,20 @@ #include #include "../src/meshlink.h" -static void log(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { +static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}; fprintf(stderr, "%s: %s\n", levelstr[level], text); } -static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len) { - if(!len || data[len - 1]) { +static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { + const char *msg = data; + + if(!len || msg[len - 1]) { fprintf(stderr, "Received invalid data from %s\n", source->name); return; } - printf("%s says: %s\n", source->name, data); + printf("%s says: %s\n", source->name, msg); } static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { @@ -154,8 +156,8 @@ int main(int argc, char *argv[]) { } meshlink_set_receive_cb(mesh, receive); - meshlink_set_node_status_cb(mesh, node_changed); - meshlink_set_log_cb(mesh, MESHLINK_INFO, log); + meshlink_set_node_status_cb(mesh, node_status); + meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); if(!meshlink_start(mesh)) { fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr); diff --git a/src/Makefile.am b/src/Makefile.am index 8f02c06d..021d6b8e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,7 +57,7 @@ sptps_speed_SOURCES = \ lib_LTLIBRARIES = libmeshlink.la libmeshlink_la_SOURCES = \ - libmeshlink.c libmeshlink.h \ + meshlink.c meshlink.h \ buffer.c buffer.h \ cipher.h \ conf.c conf.h \ diff --git a/src/meshlink.c b/src/meshlink.c new file mode 100644 index 00000000..86340f0e --- /dev/null +++ b/src/meshlink.c @@ -0,0 +1,95 @@ +/* + meshlink.c -- Implementation of the MeshLink API. + Copyright (C) 2014 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "system.h" + +#include "meshlink_internal.h" + +static const char *errstr[] = { + [MESHLINK_OK] = "No error", + [MESHLINK_ENOMEM] = "Out of memory", + [MESHLINK_ENOENT] = "No such node", +}; + +const char *meshlink_strerror(meshlink_errno_t errno) { + return errstr[errno]; +} + +meshlink_handle_t *meshlink_open(const char *confbase, const char *name) { + return NULL; +} + +bool meshlink_start(meshlink_handle_t *handle) { + return false; +} + +void meshlink_stop(meshlink_handle_t *handle) { +} + +void meshlink_close(meshlink_handle_t *handle) { +} + +void meshlink_set_receive_cb(meshlink_handle_t *handle, meshlink_receive_cb_t cb) { +} + +void meshlink_set_node_status_cb(meshlink_handle_t *handle, meshlink_node_status_cb_t cb) { +} + +void meshlink_set_log_cb(meshlink_handle_t *handle, meshlink_log_level_t level, meshlink_log_cb_t cb) { +} + +bool meshlink_send(meshlink_handle_t *handle, meshlink_node_t *destination, const void *data, unsigned int len) { + return false; +} + +meshlink_node_t *meshlink_get_node(meshlink_handle_t *handle, const char *name) { + return NULL; +} + +size_t meshlink_get_all_nodes(meshlink_handle_t *handle, meshlink_node_t **nodes, size_t nmemb) { + return 0; +} + +char *meshlink_sign(meshlink_handle_t *handle, const char *data, size_t len) { + return NULL; +} + +bool meshlink_verify(meshlink_handle_t *handle, meshlink_node_t *source, const char *data, size_t len, const char *signature) { + return false; +} + +char *meshlink_invite(meshlink_handle_t *handle, const char *name) { + return NULL; +} + +bool meshlink_join(meshlink_handle_t *handle, const char *invitation) { + return false; +} + +char *meshlink_export(meshlink_handle_t *handle) { + return NULL; +} + +bool meshlink_import(meshlink_handle_t *handle, const char *data) { + return false; +} + +void meshlink_blacklist(meshlink_handle_t *handle, meshlink_node_t *node) { +} + diff --git a/src/meshlink.h b/src/meshlink.h index dcafc3bc..178e92aa 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -118,7 +118,7 @@ typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *handle, meshlink_node_t * @param handle A handle which represents an instance of MeshLink. * @param cb A pointer to the function which will be called when another node sends data to the local node. */ -void meshlink_set_receive_cb(meshlink_handle_t *handle, meshlink_receive_cb_t cb); +extern void meshlink_set_receive_cb(meshlink_handle_t *handle, meshlink_receive_cb_t cb); /// A callback reporting node status changes. /** @param handle A handle which represents an instance of MeshLink. @@ -137,7 +137,7 @@ typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *handle, meshlink_no * @param handle A handle which represents an instance of MeshLink. * @param cb A pointer to the function which will be called when another node's status changes. */ -void meshlink_set_node_status_cb(meshlink_handle_t *handle, meshlink_node_status_cb_t cb); +extern void meshlink_set_node_status_cb(meshlink_handle_t *handle, meshlink_node_status_cb_t cb); /// Severity of log messages generated by MeshLink. typedef enum { @@ -166,7 +166,7 @@ typedef void (*meshlink_log_cb_t)(meshlink_handle_t *handle, meshlink_log_level_ * @param level An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback. * @param cb A pointer to the function which will be called when another node sends data to the local node. */ -void meshlink_set_log_cb(meshlink_handle_t *handle, meshlink_log_level_t level, meshlink_receive_cb_t cb); +extern void meshlink_set_log_cb(meshlink_handle_t *handle, meshlink_log_level_t level, meshlink_log_cb_t cb); /// Send data to another node. /** This functions sends one packet of data to another node in the mesh. -- 2.39.2