view src/lwwire-pingpong.c @ 12:f8226a33698d

Add PACKET extension
author Brett Gordon <beretta42@gmail.com>
date Fri, 18 Nov 2016 19:51:43 -0700
parents 36c4cda4b6c4
children
line wrap: on
line source

/*
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);
}