#pragma once
#include "protocol_spec.hpp"
#include "mcp2521.hpp"
#include "sm.hpp"

#include "freertos/FreeRTOS.h"



typedef void (*GobotRPCHandler_t)(void *);
typedef void (*GobotRPCFrameHandler)(void *, RPCFrame *);

class GobotRPCReciver {
protected:
    MCP2521 * can_interface;
    GobotRPCStateMashine state_mashine;

    GobotRPCHandler_t onDoneFrameHandler;
    void * argOnDoneFrameHandler;

public:
    GobotRPCReciver(MCP2521 * can_interface);
    ~GobotRPCReciver();

    void onRX();

    void registerOnDoneFrameHandler(GobotRPCHandler_t handler, void * arg);
};

void onRX0_GoboRPC(void *arg);

class GoboRPCTransiver {
private:
    MCP2521 * can_interface;
    GobotRPCReciver rx;

    GobotRPCHandler_t onRequestHandler;
    GobotRPCHandler_t onResponseHandler;
    GobotRPCHandler_t onResponseErrorHandler;

    void * argOnRequestHandler;
    void * argOnResponseHandler;
    void * argOnResponseErrorHandler;



public:
    GoboRPCTransiver(MCP2521 * can_interface);
    ~GoboRPCTransiver();

    void send(RPCFrame * frame);

    void registerOnRequestHandler(GobotRPCHandler_t handler, void * arg);
    void registerOnResponseHandler(GobotRPCHandler_t handler, void * arg);
    void registerOnResponseErrorHandler(GobotRPCHandler_t handler, void * arg);
};