diff src/lwwire-pingpong.c @ 10:36c4cda4b6c4

Add extension support with the PINGPONG extension Add extension support. There are two ways to add extensions: 1. as a shared object which will be loaded with ext=<filename> as a parameter to lwwire. See the lwwire_pingpong.c file for details. 2. By doing basically the same thing as a shared object but linking it into the main binary and calling lwwire_register_extension() appropriately.
author William Astle <lost@l-w.ca>
date Sat, 30 Jul 2016 13:16:39 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lwwire-pingpong.c	Sat Jul 30 13:16:39 2016 -0600
@@ -0,0 +1,62 @@
+/*
+PINGPONG extension for lwwire
+*/
+
+#include <stdio.h>
+#include "lwwire.h"
+
+/*
+
+NOTE: the __attribute__((used)) tags are there to prevent the functions
+from being optimized out as would often be the case with functions declared
+static. This could be avoided by removing the static declarations. However,
+by declaring everything static except for the lwwire_register() function,
+we avoid polluting the global namespace and possibly having conflicts with
+other extensions.
+*/
+
+
+// this is called to handle an operation for this extension
+// it should return nonzero if the operation code (op) is not
+// supported. Otherwise, it should implement the operation codes.
+__attribute__((used)) static int pingpong_handler(int op)
+{
+    unsigned char buf[1];
+    
+    if (op > 1 || op < 0)
+        return -1;
+    buf[1] = 0x42;
+    lwwire_write(buf, 1);
+    return 0;
+}
+
+// this will be called when the extension is enabled
+// return nonzero if enabling the extension cannot happen for some reason
+__attribute__((used)) static int pingpong_enable(void)
+{
+    return 0;
+}
+
+// this will be called when an extension is disabled
+// if this extension cannot be disabled after it is enabled, it should return
+// nonzero; otherwise, this MUST succeed
+__attribute__((used)) static int pingpong_disable(void)
+{
+    return 0;
+}
+
+// this call should do anything needed to disable the extension
+// and anything special that might be needed on a protocol reset
+// it will only be called if the extension is enabled when the
+// reset occurs. Even if the extension isn't disablable, this call
+// MUST disable it and reset things as if the extension was never
+// enabled.
+__attribute__((used)) static void pingpong_reset(void)
+{
+}
+
+int lwwire_register(void)
+{
+    fprintf(stderr, "Registering PINGPONG extension\n");
+    return lwwire_register_extension(0xdf, pingpong_handler, pingpong_enable, pingpong_disable, pingpong_reset);
+}