/*                                              -*- mode:C++; fill-column:100 -*-
  SFBCodeFlow.h - Support for the SFB Code Flow protocol
  Copyright (C) 2008 The Regents of the University of New Mexico.  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  USA
*/

/**
  \file SFBCodeFlow.h The SFB Code Flow protocol allows SFB's to program each other.
  \author David H. Ackley.  
  \date (C) 2008 All rights reserved.
  \lgpl
 */
#ifndef SFBCODEFLOW_H_
#define SFBCODEFLOW_H_

#include "SFBTypes.h"
#include "SFBConstants.h"
#include "SFBAssert.h"
#include "SFBProvenance.h"
#include "SFBSerial.h"

extern u8 sectorOfAddress(u32 address) ;

extern u32 sectorStart(u8 sectorNumber) ;

extern u32 sectorSize(u8 sectorNumber) ;

extern u32 sectorEnd(u8 sectorNumber) ;

bool plausibleProvenance(SFBProvenance * p, uptr startAddr, uptr endAddr) ;

enum { CHECKSUM_IGNORE, CHECKSUM_CHECK, CHECKSUM_GENERATE };

bool validProvenance(SFBProvenance * p, uptr startAddr, uptr endAddr, int checksumMode=CHECKSUM_CHECK) ;

SFBProvenance * findProvenance(uptr startAddr, uptr endAddr, int checksumMode=CHECKSUM_CHECK) ;

extern void programServerDispatcher(u8 * packet) ; // Handler for 'P' packets

extern void programServerInit(u8 face) ; // Initializer for 'P' packets

extern void programServerSetSketch(const volatile SFBProvenance * theSketch, u32 startAddress, uptr addressOffset, u8 pflags = 0) ;

extern void printPhSketchInfo(u8 toFace, const volatile SFBProvenance * theSketch) ;

extern bool announcePhSketchInfoIfAny(u8 toFace) ;

enum CodeFlowStatus {
  CODE_FLOW_INIT,
  CODE_FLOW_IDLE,
  CODE_FLOW_ACTIVE,
  CODE_FLOW_SUCCEEDED
};
typedef void (*CodeFlowStatusCallback)(u8 face, CodeFlowStatus status) ;

extern void setCodeFlowStatusCallback(CodeFlowStatusCallback ptr) ;

extern int wantPhPacketCodeUpdate(u8 * packet) __attribute__ ((weak));

extern void initProgramServerSketch() __attribute__ ((weak)); /* Defer to real init call in SFBProvenanceData.h, if it got compiled in.. */

#endif /* SFBCODEFLOW_H_ */
