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