comparison 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
comparison
equal deleted inserted replaced
9:a11b330771e0 10:36c4cda4b6c4
1 /*
2 PINGPONG extension for lwwire
3 */
4
5 #include <stdio.h>
6 #include "lwwire.h"
7
8 /*
9
10 NOTE: the __attribute__((used)) tags are there to prevent the functions
11 from being optimized out as would often be the case with functions declared
12 static. This could be avoided by removing the static declarations. However,
13 by declaring everything static except for the lwwire_register() function,
14 we avoid polluting the global namespace and possibly having conflicts with
15 other extensions.
16 */
17
18
19 // this is called to handle an operation for this extension
20 // it should return nonzero if the operation code (op) is not
21 // supported. Otherwise, it should implement the operation codes.
22 __attribute__((used)) static int pingpong_handler(int op)
23 {
24 unsigned char buf[1];
25
26 if (op > 1 || op < 0)
27 return -1;
28 buf[1] = 0x42;
29 lwwire_write(buf, 1);
30 return 0;
31 }
32
33 // this will be called when the extension is enabled
34 // return nonzero if enabling the extension cannot happen for some reason
35 __attribute__((used)) static int pingpong_enable(void)
36 {
37 return 0;
38 }
39
40 // this will be called when an extension is disabled
41 // if this extension cannot be disabled after it is enabled, it should return
42 // nonzero; otherwise, this MUST succeed
43 __attribute__((used)) static int pingpong_disable(void)
44 {
45 return 0;
46 }
47
48 // this call should do anything needed to disable the extension
49 // and anything special that might be needed on a protocol reset
50 // it will only be called if the extension is enabled when the
51 // reset occurs. Even if the extension isn't disablable, this call
52 // MUST disable it and reset things as if the extension was never
53 // enabled.
54 __attribute__((used)) static void pingpong_reset(void)
55 {
56 }
57
58 int lwwire_register(void)
59 {
60 fprintf(stderr, "Registering PINGPONG extension\n");
61 return lwwire_register_extension(0xdf, pingpong_handler, pingpong_enable, pingpong_disable, pingpong_reset);
62 }