Skip to content

Commit

Permalink
Backup and restore PDM records.
Browse files Browse the repository at this point in the history
Backup code 0x0B00 with response 0x8B00;
Restore code  0x0B01 with response 0x8B01;
Before restore we need stop BDB and ZPS routines - code 0x0B02.
Serial buffer 256 bytes, so pdm parts need to be smaller, and sending small chunks. First 4 words (2 bytes * 4) are: PDM address, Data size, Full record data size, Offset from start PDM record.
  • Loading branch information
devbis committed Aug 8, 2022
1 parent 990c799 commit 60a8221
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ModuleRadio/Firmware/src/ZiGate/Source/Common/app_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ typedef enum
typedef struct {
enum {
FACTORY_NEW = 0,
NOT_FACTORY_NEW = 0xff
NOT_FACTORY_NEW = 0xff,
PDM_UPDATE = 0x80
}eState;
teNODE_STATES eNodeState;
uint8 u8DeviceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ typedef enum
E_SL_MSG_AHI_SET_TX_POWER_RSP = 0x8806,
E_SL_MSG_AHI_GET_TX_POWER = 0x0807,
E_SL_MSG_AHI_GET_TX_POWER_RSP = 0x8807,

E_SL_MSG_DUMP_PDM_RECORD = 0x0B00,
E_SL_MSG_DUMP_PDM_RECORD_RESPONSE = 0x8B00,
E_SL_MSG_RESTORE_PDM_RECORD_REQUEST = 0x0B01,
E_SL_MSG_RESTORE_PDM_RECORD_RESPONSE = 0x8B01,
E_SL_MSG_RESTORE_PDM_MODE = 0x0B02,
} teSL_MsgType;
typedef enum
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ PRIVATE void APP_vUpdateReportableChange( tuZCL_AttributeReportable *puAttribute
#ifdef PDM_DEBUG
PRIVATE void APP_CustomPDMDebug( void );
#endif
PRIVATE void dump_PDM(uint8 *pu8LinkRxBuffer, uint16 u16PacketLength);
PRIVATE void restore_PDM(uint8 *pu8LinkRxBuffer, uint16 u16PacketLength);

/****************************************************************************/
/*** Exported Variables ***/
/****************************************************************************/
Expand Down Expand Up @@ -439,7 +442,10 @@ PUBLIC void APP_vProcessIncomingSerialCommands ( uint8 u8RxByte )
vSL_WriteMessage ( E_SL_MSG_VERSION_LIST,
sizeof ( uint32 ),
( uint8* ) &u32Version,
0);
0 );
vLog_Printf ( TRACE_APP, LOG_DEBUG, "\nRaw: %d\n", sZllState.u8RawMode );
vLog_Printf ( TRACE_APP, LOG_DEBUG, "\nChannel: %d\n", sZllState.u8MyChannel );
vLog_Printf ( TRACE_APP, LOG_DEBUG, "\nMyAddr: %04x\n", sZllState.u16MyAddr );
return;
}
break;
Expand Down Expand Up @@ -2614,9 +2620,29 @@ PUBLIC void APP_vProcessIncomingSerialCommands ( uint8 u8RxByte )


#endif
default:
case E_SL_MSG_DUMP_PDM_RECORD:
{
dump_PDM(au8LinkRxBuffer, u16PacketLength);
}
break;

case E_SL_MSG_RESTORE_PDM_RECORD_REQUEST:
{
restore_PDM(au8LinkRxBuffer, u16PacketLength);
}
break;

case E_SL_MSG_RESTORE_PDM_MODE:
{
sZllState.eState = PDM_UPDATE;
PDM_eSaveRecordData(PDM_ID_APP_ZLL_CMSSION, &sZllState, sizeof(sZllState));
bResetIssued = TRUE;
ZTIMER_eStart( u8IdTimer, ZTIMER_TIME_MSEC ( 1 ) );
}
break;
default:
u8Status = E_SL_MSG_STATUS_UNHANDLED_COMMAND;
break;
break;
}
}
//PDM Messages
Expand Down Expand Up @@ -2648,6 +2674,110 @@ PUBLIC void APP_vProcessIncomingSerialCommands ( uint8 u8RxByte )

}

PRIVATE void dump_PDM( uint8 *pu8LinkRxBuffer,
uint16 u16PacketLength )
{
uint16 i;
uint16 offset=0;
uint16 found;
uint16 dataLength;
uint16 u16DataBytesRead;
uint16 hits=0;
uint16 fullsize=2;
uint16 pdm_addresses[17] = { 0x0010, 0xf000, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104, 0xf105, 0xf106, 0x0001, 0x0002, 0x0003 };
uint8 tmp[0x800];
uint16 addr = ZNC_RTN_U16(pu8LinkRxBuffer, 0);

if(u16PacketLength>0 || addr == 0xffff)
{
if( PDM_bDoesDataExist(addr, &dataLength)!=0) {
PDM_eReadDataFromRecord ( addr, &tmp[8], dataLength, &u16DataBytesRead );
//PDM address
tmp[0] = addr >> 8;
tmp[1] = addr & 0xff;
//Data size
tmp[2] = dataLength >> 8;
tmp[3] = dataLength & 0xff;
//PDM data size
tmp[4] = dataLength >> 8;
tmp[5] = dataLength & 0xff;
//PDM data offset
tmp[6]=0;
tmp[7]=0;
vLog_Printf ( TRACE_APP, LOG_INFO, "\n**Dump %04x [%04x] %04x** ", addr, dataLength, u16DataBytesRead );
vSL_WriteMessage ( E_SL_MSG_DUMP_PDM_RECORD_RESPONSE, dataLength+8, tmp, 0 );
}
else
{
vLog_Printf ( TRACE_APP, LOG_INFO, "\n**Not found %04x [%04x]** ", addr, dataLength );
vSL_WriteMessage ( E_SL_MSG_DUMP_PDM_RECORD_RESPONSE, 0, pu8LinkRxBuffer, 0 );
}
}
else
{
for (i=0; i<17; i++)
{
if( PDM_bDoesDataExist( pdm_addresses[i], &dataLength ) != 0 )
{
PDM_eReadDataFromRecord ( pdm_addresses[i], &tmp[8], dataLength, &u16DataBytesRead );
//PDM address
tmp[0] = pdm_addresses[i] >> 8;
tmp[1] = pdm_addresses[i] & 0xff;
//Data size
tmp[2] = dataLength >> 8;
tmp[3] = dataLength & 0xff;
//PDM data size
tmp[4] = dataLength >> 8;
tmp[5] = dataLength & 0xff;
//PDM data offset
tmp[6]=0;
tmp[7]=0;
vLog_Printf ( TRACE_APP, LOG_INFO, "\n**Dump %04x [%04x] %04x** ", pdm_addresses[i], dataLength, u16DataBytesRead );
vSL_WriteMessage ( E_SL_MSG_DUMP_PDM_RECORD_RESPONSE, dataLength+8, tmp, 0 );
}
}
}
}

PRIVATE void restore_PDM(uint8 *pu8LinkRxBuffer, uint16 u16PacketLength)
{
uint16 i;
uint16 dataLength;
uint16 u16DataBytesRead;
uint16 blockstart = 0;
uint8 tmp[0x800];
uint16 addr = ZNC_RTN_U16 ( pu8LinkRxBuffer, blockstart + 0 );//PDM address
uint16 size = ZNC_RTN_U16 ( pu8LinkRxBuffer, blockstart + 2 );//data size
uint16 fullsize = ZNC_RTN_U16 ( pu8LinkRxBuffer, blockstart + 4 );//PDM size
uint16 offset = ZNC_RTN_U16 ( pu8LinkRxBuffer, blockstart + 6 );//data offset

vLog_Printf ( TRUE, LOG_INFO, "\n**Restore data addr: %04x size: %04x [%04x] %04x**\n", addr, size, fullsize, offset);

if( PDM_bDoesDataExist( addr, &dataLength ) )
{
PDM_eReadDataFromRecord ( addr, &tmp, dataLength, &u16DataBytesRead );
for( i=0; i<size; i++ )
{
tmp[i+offset] = pu8LinkRxBuffer[i+blockstart+8];
}
if( size+offset>dataLength )
{
dataLength=size+offset;
}

PDM_eSaveRecordData( addr, &tmp, dataLength );
}
else
{
PDM_eSaveRecordData( addr, &pu8LinkRxBuffer[blockstart + 8], size );
}

vLog_Printf ( TRUE, LOG_INFO, "\n**Restored %04x [%04x] pkt[%04x]**\n", addr, size, u16PacketLength );

tmp[0]=size;

vSL_WriteMessage ( E_SL_MSG_RESTORE_PDM_RECORD_RESPONSE, 2, tmp, 0 );
}
/****************************************************************************
*
* NAME: APP_eZclBasicResetToFactoryDefaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ PUBLIC void vAppMain(void)
}
//bAHI_FlashInit(141, NULL);
vInitialiseApp ();
//app_vFormatAndSendUpdateLists ( );
if(sZllState.eState != PDM_UPDATE) {
app_vFormatAndSendUpdateLists ( );


if (sZllState.eNodeState == E_RUNNING)
{
/* Declared within if statement. If it is declared at the top
Expand Down Expand Up @@ -408,9 +408,17 @@ PUBLIC void vAppMain(void)
( uint8* ) &sZllState.eNodeState,
0);
}

ZTIMER_eStart ( u8TickTimer, ZCL_TICK_TIME );


DBG_vPrintf( TRACE_APPSTART, "APP: Entering APP_vMainLoop()\n");
}else{
vSL_WriteMessage( E_SL_MSG_NODE_FACTORY_NEW_RESTART,
1,
( uint8* ) &sZllState.eNodeState,
0 );
}
APP_vMainLoop();

}
Expand Down Expand Up @@ -558,6 +566,9 @@ PRIVATE void vInitialiseApp ( void )
vLog_Printf ( TRACE_EXC,LOG_DEBUG, "PDM: test %d\n", sEndpointTable.u8NumRecords );
#endif

if(sZllState.eState != PDM_UPDATE) {
ZPS_u32MacSetTxBuffers ( 5 );

if ( sZllState.eNodeState == E_RUNNING )
{
u8DeviceType = ( sZllState.u8DeviceType >= 2 ) ? 1 : sZllState.u8DeviceType;
Expand Down Expand Up @@ -603,6 +614,7 @@ PRIVATE void vInitialiseApp ( void )
vAppInitOTA();
#endif
}
}
}


Expand All @@ -623,9 +635,12 @@ PUBLIC void APP_vMainLoop(void)
while (TRUE)
{

if(sZllState.eState != PDM_UPDATE) {
zps_taskZPS ( );
bdb_taskBDB ( );

APP_vHandleAppEvents ( );
}
APP_vProcessRxData ( );
ZTIMER_vTask ( );

Expand Down

0 comments on commit 60a8221

Please sign in to comment.