| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455 |
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #include <ctype.h>
- #include <errno.h>
- #include <unistd.h>
- #include <gfaserial.h>
- #include <gfamininetmst.h>
- #include <byteswap.h>
- #include "bl_commands.h"
- #include "gfabootlmast.h"
- #include "dbghlp.h"
- ////////////////////////////////////////////////////////////////////////////////////
- #define _dword_offset(m) (offsetof(GFA_APP_IMG_HEADER, m) / sizeof(uint32_t) - 2)
- #define _EXEC_CALLBACK(pfn, ...) if(pfn) (*pfn)(__FILE__, __LINE__, __VA_ARGS__)
- #define _BOOTLOADER_EXEC_WAIT_TIME 250
- #define _GFA_MAX_DUMP_DWORDS 16
- #define _MAX_SEND_DATA_BLOCK_SIZE 76
- #define _DEF_SEND_DATA_BLOCK_SIZE 64
- ////////////////////////////////////////////////////////////////////////////////////
- typedef struct _GFA_BLM
- {
- HGFAMINEMST hMst;
- PFN_BU_CMD_DOWNLOAD_STATUS pfnBuCmdDownloadStatus;
- PFN_BU_CMD_SEND_DATA_STATUS pfnBuCmdSendDataStatus;
- int nVerbosity;
- }GFA_BLM, *LPGFA_BLM;
- typedef const GFA_BLM *LPCGFA_BLM;
- ////////////////////////////////////////////////////////////////////////////////////
- HGFABLM GfaBlmOpen(LPCGFA_BLM_CFG_PARAMS pblmcfg)
- {
- if(pblmcfg)
- {
- HGFAMINEMST hMst = GfaMininetMasterOpen(&pblmcfg->mmcp);
- if(hMst)
- {
- LPGFA_BLM pBlm = malloc(sizeof(GFA_BLM));
- memset(pBlm, 0, sizeof(GFA_BLM));
- pBlm->hMst = hMst;
- pBlm->pfnBuCmdDownloadStatus = pblmcfg->pfnBuCmdDownloadStatus;
- pBlm->pfnBuCmdSendDataStatus = pblmcfg->pfnBuCmdSendDataStatus;
- return (HGFABLM)pBlm;
- }
- return NULL;
- }
- errno = EINVAL;
- return NULL;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- void GfaBlmClose(HGFABLM hBlm)
- {
- if(hBlm)
- {
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- GfaMininetMasterClose(pBlm->hMst);
- free(pBlm);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmResetSlaveIndex(HGFABLM hBlm, uint8_t nNodeAddr)
- {
- if(hBlm)
- {
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- return GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr);
- }
- errno = EINVAL;
- return -1;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- ssize_t GfaBlmBUCmdPollData(HGFABLM hBlm, uint8_t nNodeAddr, void *pData, size_t nCbData, uint32_t nTimeoutMS)
- {
- if(hBlm && pData && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
- {
- size_t s, nReceived = 0;
- struct timeval tvRX;
- ssize_t nLen, nRet = -1;
- uint8_t cmd[32];
- uint8_t txb[32];
- uint8_t rxb[512];
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- uint8_t *pszData = (uint8_t*)pData;
- tvRX.tv_sec = nTimeoutMS / 1000;
- tvRX.tv_usec = (nTimeoutMS % 1000) * 1000;
- GfaMininetMasterSaveTimeouts(pBlm->hMst);
- GfaMininetMasterSetTimeouts(pBlm->hMst, &tvRX, NULL);
- while(nReceived < nCbData)
- {
- s = GfaBlmBuildCmdDataPacket("BU", 0, NULL, 0, cmd, sizeof(cmd), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) <= 0)
- {
- nRet = -1;
- break;
- }
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) > 0)
- {
- uint8_t nIndex;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, pszData, nCbData - nReceived)) > 0)
- {
- pszData += nRet;
- nReceived += nRet;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- if(nTimeoutMS > 0)
- {
- if(nTimeoutMS >= 100)
- {
- usleep(100000);
- nTimeoutMS -= 100;
- // TRACE("\nACK ...\n");
- }
- else
- {
- usleep(nTimeoutMS * 1000);
- nTimeoutMS = 0;
- // TRACE("\nACK ...\n");
- }
- }
- else
- {
- errno = ETIMEDOUT;
- nReceived = 0;
- break;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- nReceived = 0;
- break;
- }
- else
- {
- errno = EPROTO;
- nReceived = 0;
- break;
- }
- }
- else
- {
- nReceived = 0;
- break;
- }
- }
- GfaMininetMasterRestoreTimeouts(pBlm->hMst);
- return nReceived;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdReset(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nTimeoutMS)
- {
- if(hBlm)
- {
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- uint8_t nIndex;
- size_t s;
- ssize_t nRet, nLen;
- uint8_t cmd = COMMAND_RESET;
- uint8_t cmddata[16];
- char txb[32], rxb[256], ack[2];
-
- if((nRet = GfaBlmBootloaderSetBaudrate(hBlm, nNodeAddr, 19200)) != 0)
- return -1;
- s = GfaBlmBuildCmdDataPacket("BU", 0, &cmd, 1, cmddata, sizeof(cmddata), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmddata, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
-
- if(NODE_IS_MULTICAST(nNodeAddr))
- return 0;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
- (nRet == MINET_SLAVE_RESPONSE_ACK))
- {
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
- return -1;
- }
- else
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 200)) != 2)
- return -1;
- }
- if( (ack[0] == 0) &&
- (ack[1] == COMMAND_ACK))
- {
- do
- {
- if(nTimeoutMS > _BOOTLOADER_EXEC_WAIT_TIME)
- {
- usleep(_BOOTLOADER_EXEC_WAIT_TIME * 1000);
- nTimeoutMS -= _BOOTLOADER_EXEC_WAIT_TIME;
- }
- else
- {
- usleep(nTimeoutMS * 1000);
- nTimeoutMS = 0;
- }
- if((nRet = GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr)) == 0)
- break;
- GfaMininetMasterPurgeDeviceRXBuffer(pBlm->hMst);
- }
- while(nTimeoutMS > 0);
- return nRet;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdPing(HGFABLM hBlm, uint8_t nNodeAddr)
- {
- if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- ssize_t nRet, nLen;
- uint8_t nCmd = COMMAND_PING;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- char txb[32], rxb[32], cmd[8], ack[2];
- size_t s = GfaBlmBuildCmdDataPacket("BU", 0, &nCmd, 1, cmd, sizeof(cmd), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) < nLen)
- return nRet;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
- (nRet == MINET_SLAVE_RESPONSE_ACK))
- {
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
- return -1;
- }
- else
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 200)) != 2)
- return -1;
- }
- if(ack[0] == 0)
- {
- return (ack[1] == COMMAND_ACK);
- }
- else
- {
- errno = EPROTO;
- return -1;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdGetStatus(HGFABLM hBlm, uint8_t nNodeAddr, uint8_t *pbStatus)
- {
- if(hBlm && pbStatus && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- ssize_t nRet, nLen;
- uint8_t nCmd = COMMAND_GET_STATUS;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- char txb[32], rxb[32], cmd[8], ack[3], stat[3];
- size_t s = GfaBlmBuildCmdDataPacket("BU", 0, &nCmd, 1, cmd, sizeof(cmd), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) < nLen)
- return nRet;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if( (nRet == MINET_SLAVE_RESPONSE_SUCCESS) ||
- (nRet == MINET_SLAVE_RESPONSE_ACK))
- {
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
- return -1;
- }
- else
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 200)) != 2)
- return -1;
- }
- if(ack[0] == 0)
- {
- if(ack[1] == COMMAND_ACK)
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, stat, 3, 200)) != 3)
- return -1;
- if((stat[0] == 3) && (stat[1] == stat[2]))
- {
- if(pbStatus)
- *pbStatus = stat[2];
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BU\xCC", 3, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
- if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
- return nRet;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
- return 0;
- }
- else
- {
- errno = EPROTO;
- return -1;
- }
- }
- else
- {
- errno = EPROTO;
- return -1;
- }
- }
- else
- {
- errno = EPROTO;
- return -1;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdDownload(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nFlashStartAddr, uint32_t nCbData, uint32_t nTimeoutMS)
- {
- if(hBlm && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
- {
- size_t s;
- ssize_t nRet, nLen;
- uint8_t nIndex, nStatus;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- uint32_t nAddr = bswap_32(nFlashStartAddr), nCount = bswap_32(nCbData);
- uint8_t txb[32], rxb[32], cmd[16], data[9], ack[2];
- data[0] = COMMAND_DOWNLOAD;
- struct timeval tv;
- memcpy(&data[1], &nAddr, sizeof(nAddr));
- memcpy(&data[5], &nCount, sizeof(nCount));
- tv.tv_sec = nTimeoutMS / 1000;
- tv.tv_usec = (nTimeoutMS % 1000) * 1000;
- s = GfaBlmBuildCmdDataPacket("BU", 0, data, sizeof(data), cmd, sizeof(cmd), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- GfaMininetMasterSaveTimeouts(pBlm->hMst);
- GfaMininetMasterSetTimeouts(pBlm->hMst, &tv, NULL);
-
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 1, 0);
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- GfaMininetMasterRestoreTimeouts(pBlm->hMst);
- return -1;
- }
-
- if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- GfaMininetMasterRestoreTimeouts(pBlm->hMst);
- return -1;
- }
-
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 2, 0);
- GfaMininetMasterRestoreTimeouts(pBlm->hMst);
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
- {
- if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- if(nStatus == COMMAND_RET_SUCCESS)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 3, 0);
- return 0;
- }
- }
- errno = EPROTO;
- return -1;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, nTimeoutMS)) != 2)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
- {
- if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- if(nStatus == COMMAND_RET_SUCCESS)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 3, 0);
- return 0;
- }
- }
- errno = EPROTO;
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- else
- {
- errno = EPROTO;
- _EXEC_CALLBACK(pBlm->pfnBuCmdDownloadStatus, nNodeAddr, nFlashStartAddr, nCbData, 0, errno);
- return -1;
- }
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdSendDataBlock(HGFABLM hBlm, uint8_t nNodeAddr, const void *pDataBlock, size_t nCbDataBlock)
- {
- if(hBlm && pDataBlock && nCbDataBlock && (nCbDataBlock <= 250) && !NODE_IS_MULTICAST(nNodeAddr))
- {
- ssize_t nRet;
- struct timeval tv;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- tv.tv_sec = 0;
- tv.tv_usec = 500000;
- GfaMininetMasterSaveTimeouts(pBlm->hMst);
- GfaMininetMasterSetTimeouts(pBlm->hMst, &tv, NULL);
-
- do
- {
- size_t s;
- ssize_t nLen;
- uint8_t nIndex, nStatus;
- uint8_t txb[512], rxb[32], cmd[256], data[256], ack[2];
- data[0] = COMMAND_SEND_DATA;
- memcpy(&data[1], pDataBlock, nCbDataBlock);
- s = GfaBlmBuildCmdDataPacket("BU", 0, data, nCbDataBlock + 1, cmd, sizeof(cmd), true);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- {
- nRet = -1;
- break;
- }
- if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
- {
- nRet = -1;
- break;
- }
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, ack, 2)) != 2)
- {
- nRet = -1;
- break;
- }
- if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
- {
- if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
- {
- nRet = -1;
- break;
- }
- if(nStatus != COMMAND_RET_SUCCESS)
- {
- errno = -nStatus;
- nRet = -1;
- break;
- }
- nRet = 0;
- break;
- }
- errno = EPROTO;
- nRet = -1;
- break;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, ack, 2, 5000)) != 2)
- {
- nRet = -1;
- break;
- }
- if((ack[0] == 0) && (ack[1] == COMMAND_ACK))
- {
- if((nRet = GfaBlmBUCmdGetStatus(hBlm, nNodeAddr, &nStatus)))
- {
- nRet = -1;
- break;
- }
- if(nStatus != COMMAND_RET_SUCCESS)
- {
- errno = -nStatus;
- nRet = -1;
- break;
- }
- nRet = 0;
- break;
- }
- errno = EPROTO;
- nRet = -1;
- break;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- nRet = -1;
- break;
- }
- }
- while(false);
-
- GfaMininetMasterRestoreTimeouts(pBlm->hMst);
- return nRet;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdSendData(HGFABLM hBlm, uint8_t nNodeAddr, const void *pData, size_t nCbData, size_t nCbBlock)
- {
- if(hBlm && pData && nCbData && !NODE_IS_MULTICAST(nNodeAddr))
- {
- int nRet;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- const uint8_t *pbData = (const uint8_t*)pData;
- uint32_t nSent = 0;
- if(!nCbBlock || nCbBlock < 4 || nCbBlock > _MAX_SEND_DATA_BLOCK_SIZE)
- nCbBlock = _DEF_SEND_DATA_BLOCK_SIZE;
- else
- nCbBlock &= ~0x03;
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 1, 0);
-
- while(nCbData >= nCbBlock)
- {
- if((nRet = GfaBlmBUCmdSendDataBlock(hBlm, nNodeAddr, pbData, nCbBlock)) != 0)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 0, errno);
- return -1;
- }
- nCbData -= nCbBlock;
- pbData += nCbBlock;
- nSent += nCbBlock;
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 2, 0);
- }
-
- if(nCbData)
- {
- if((nRet = GfaBlmBUCmdSendDataBlock(hBlm, nNodeAddr, pbData, nCbData)) != 0)
- {
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 0, errno);
- return -1;
- }
- nSent += nCbData;
- nCbData = 0;
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 2, 0);
- }
- _EXEC_CALLBACK(pBlm->pfnBuCmdSendDataStatus, nNodeAddr, nCbBlock, nSent, 3, 0);
- return 0;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBUCmdSendDataFile(HGFABLM hBlm, uint8_t nNodeAddr, const char *pszFilename, uint32_t nFlashStartAddr, size_t nCbBlock, uint32_t nTimeoutMS)
- {
- if(hBlm && pszFilename && !NODE_IS_MULTICAST(nNodeAddr))
- {
- int nRet = -1;
- ssize_t nFileLen;
- FILE *pf = NULL;
- uint8_t *pBuf = NULL;
-
- do
- {
- if(!(pf = fopen(pszFilename, "rb")))
- break;
- if(fseek(pf, 0, SEEK_END))
- break;
- if((nFileLen = ftell(pf)) < 0)
- break;
- if(fseek(pf, 0, SEEK_SET))
- break;
- if( (nFileLen > 0) &&
- (pBuf = (uint8_t*)malloc(nFileLen)) &&
- (fread(pBuf, 1, nFileLen, pf) == (size_t)nFileLen))
- {
- if((nRet = GfaBlmBUCmdDownload(hBlm, nNodeAddr, nFlashStartAddr, nFileLen, nTimeoutMS)) == 0)
- nRet = GfaBlmBUCmdSendData(hBlm, nNodeAddr, pBuf, nFileLen, nCbBlock);
- }
- }
- while(false);
-
- if(pBuf)
- free(pBuf);
- if(pf)
- fclose(pf);
- return nRet;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- GFA_BLM_EXEC_CONTEXT GfaBlmGetExecutionContext(HGFABLM hBlm, uint8_t nNodeAddr)
- {
- if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- ssize_t nRet, nLen;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- char txb[32], rxb[32];
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BU", 2, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return GfaBlmCtx_Err;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) <= 0)
- return GfaBlmCtx_Err;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- return GfaBlmCtx_Err;
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- return GfaBlmCtx_Boot;
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- return GfaBlmCtx_App;
- return GfaBlmCtx_Err;
- }
- errno = EINVAL;
- return GfaBlmCtx_Err;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBootloaderExecute(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t *pnImgCRC32, uint32_t nTimeoutMS)
- {
- if(hBlm && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- uint32_t nImgCRC32;
- ssize_t nRet, nLen;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- char txb[32], rxb[32];
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BE", 2, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return -1;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, &nImgCRC32, sizeof(nImgCRC32))) == 4)
- {
- if(pnImgCRC32)
- *pnImgCRC32 = bswap_32(nImgCRC32);
- do
- {
- if(nTimeoutMS > _BOOTLOADER_EXEC_WAIT_TIME)
- {
- usleep(_BOOTLOADER_EXEC_WAIT_TIME * 1000);
- nTimeoutMS -= _BOOTLOADER_EXEC_WAIT_TIME;
- }
- else
- {
- usleep(nTimeoutMS * 1000);
- nTimeoutMS = 0;
- }
- if((nRet = GfaMininetMasterResetSlaveIndex(pBlm->hMst, nNodeAddr)) == 0)
- break;
- GfaMininetMasterPurgeDeviceRXBuffer(pBlm->hMst);
- }
- while(nTimeoutMS > 0);
- return nRet;
- }
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBootloaderSetBaudrate(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nBaudrate)
- {
- if(hBlm)
- {
- uint8_t nIndex;
- size_t s;
- ssize_t nRet, nLen;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
-
- if(GfaMininetMasterIsValidBaudrate(pBlm->hMst, nBaudrate))
- {
- char txb[32], rxb[32], cmd[8];
- uint32_t nBr = bswap_32(nBaudrate);
- s = GfaBlmBuildCmdDataPacket("BB", 0, &nBr, sizeof(nBr), cmd, sizeof(cmd), false);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
-
- if(NODE_IS_MULTICAST(nNodeAddr))
- return 0;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- return GfaMininetMasterSetBaudrate(pBlm->hMst, nBaudrate);
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmBootloaderDump(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nAddress, uint32_t nCntDwords, void *pBuffer, size_t nCbBuffer)
- {
- if(hBlm && pBuffer && nCntDwords && !NODE_IS_MULTICAST(nNodeAddr))
- {
- size_t s;
- uint8_t nIndex;
- ssize_t nRet, nLen;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- uint32_t i, dmp[_GFA_MAX_DUMP_DWORDS];
- char txb[32], rxb[128], cmd[10];
- if(nCntDwords > _GFA_MAX_DUMP_DWORDS)
- nCntDwords = _GFA_MAX_DUMP_DWORDS;
- if((nCntDwords * sizeof(uint32_t)) > nCbBuffer)
- {
- errno = ENOMEM;
- return -1;
- }
- struct _MEM
- {
- uint32_t nAddr;
- uint32_t nCount;
- }mem = {bswap_32(nAddress), bswap_32(nCntDwords)};
- s = GfaBlmBuildCmdDataPacket("BD", 0, &mem, sizeof(mem), cmd, sizeof(cmd), false);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, dmp, sizeof(uint32_t) * nCntDwords)) != (ssize_t)(sizeof(uint32_t) * nCntDwords))
- return -1;
- for(i = 0; i < nCntDwords; ++i)
- {
- dmp[i] = bswap_32(dmp[i]);
- }
- memcpy(pBuffer, dmp, sizeof(uint32_t) * nCntDwords);
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, dmp, sizeof(uint32_t) * nCntDwords, 200)) != (ssize_t)(sizeof(uint32_t) * nCntDwords))
- return -1;
- for(i = 0; i < nCntDwords; ++i)
- {
- dmp[i] = bswap_32(dmp[i]);
- }
- memcpy(pBuffer, dmp, sizeof(uint32_t) * nCntDwords);
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- int GfaBlmGetImgInfo(HGFABLM hBlm, uint8_t nNodeAddr, uint32_t nDumpAddr, bool bCtxIsApp, LPGFA_IMG_INFO pii)
- {
- if(hBlm && pii && !NODE_IS_MULTICAST(nNodeAddr))
- {
- ssize_t nRet = 0;
- uint32_t i, j, nPfx0Addr = 0;
- int32_t nPfx0Index = -1, nPfx1Index = -1;
- uint32_t aDump[_GFA_MAX_DUMP_DWORDS];
- GFA_APP_IMG_HEADER aih;
- memset(&aih, 0, sizeof(aih));
- memset(pii, 0, sizeof(GFA_IMG_INFO));
- for(i = 0; i < 257; i += _GFA_MAX_DUMP_DWORDS)
- {
- if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDumpAddr, _GFA_MAX_DUMP_DWORDS, aDump, sizeof(aDump))) == 0)
- {
- for(j = 0; j < _GFA_MAX_DUMP_DWORDS; ++j, nDumpAddr += sizeof(uint32_t))
- {
- switch(aDump[j])
- {
- case GFA_APP_IMG_HEADER_PREFIX_0:
- nPfx0Addr = nDumpAddr;
- nPfx0Index = i + j;
- continue;
- case GFA_APP_IMG_HEADER_PREFIX_1:
- nPfx1Index = i + j;
- break;
- default:
- break;
- }
- if((nPfx0Index >= 0) && (nPfx1Index == (nPfx0Index + 1)))
- {
- uint32_t nDmpStart = nPfx0Addr + 2 * sizeof(uint32_t), *pDwDst = ((uint32_t*)&aih) + 2;
- uint32_t nCntDwAvail = 15 - j;
- if(bCtxIsApp)
- {
- uint32_t nDwOffsImgMaterialNum = _dword_offset(app.pszImgMaterialNum);
- uint32_t nDwOffsImgNameBuild = _dword_offset(app.pszImgNameBuild);
- uint32_t nDwOffsImgCRC32 = _dword_offset(nImgCRC32);
- size_t nSizeDst = (nDwOffsImgNameBuild + 1) * sizeof(uint32_t);
- uint32_t nCntDwToDmp, nCntDwToCpy;
- if(nCntDwAvail > nDwOffsImgNameBuild)
- {
- // have nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
- // need nothing
- nCntDwToCpy = nDwOffsImgNameBuild + 1;
- nCntDwToDmp = 0;
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- }
- else if(nCntDwAvail == nDwOffsImgNameBuild)
- {
- // have nImgLength, nImgCRC32, pszImgMaterialNum
- // need pszImgNameBuild
- nCntDwToCpy = nDwOffsImgNameBuild;
- nCntDwToDmp = 1;
- nDmpStart += nCntDwToCpy * sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nCntDwToCpy;
- nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
- }
- else if(nCntDwAvail > nDwOffsImgCRC32)
- {
- // have nImgLength, nImgCRC32
- // need pszImgMaterialNum, pszImgNameBuild
- nCntDwToCpy = 2;
- nCntDwToDmp = 2;
- nDmpStart += nDwOffsImgMaterialNum * sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nDwOffsImgMaterialNum;
- nSizeDst -= nDwOffsImgMaterialNum * sizeof(uint32_t);
- }
- else if(nCntDwAvail == nDwOffsImgCRC32)
- {
- // have nImgLength
- // need nImgCRC32, pszImgMaterialNum, pszImgNameBuild
- nCntDwToCpy = nDwOffsImgCRC32;
- nCntDwToDmp = 7; // 1 + 4 + 2
- nDmpStart += sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nCntDwToCpy;
- nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
- }
- else
- {
- // have nothing
- // need nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
- nCntDwToCpy = 0;
- nCntDwToDmp = nDwOffsImgNameBuild + 1; // 2 + 4 + 2
- }
- if(nCntDwToDmp)
- {
- if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDmpStart, nCntDwToDmp, pDwDst, nSizeDst)) != 0)
- return -1;
- }
- pii->nImgLength = aih.nImgLength;
- pii->nImgCRC32 = aih.nImgCRC32;
- if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, (uint32_t)aih.app.pszImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH / sizeof(uint32_t), pii->szImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH)) != 0)
- return -1;
- if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, (uint32_t)aih.app.pszImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t), pii->szImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH)) != 0)
- return -1;
- pii->szImgMaterialNum[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
- pii->szImgNameBuild[GFA_APP_MAX_IMG_NAME_BUILD_LENGTH - 1] = '\0';
- return 0;
- }
- else
- {
- uint32_t nDwOffsImgMaterialNum = _dword_offset(bl.szImgMaterialNum);
- uint32_t nDwOffsImgNameBuild = _dword_offset(bl.szImgNameBuild);
- uint32_t nDwOffsImgCRC32 = _dword_offset(nImgCRC32);
- size_t nSizeDst = nDwOffsImgNameBuild * sizeof(uint32_t) + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH;
- uint32_t nCntDwToDmp, nCntDwToCpy;
- if(nCntDwAvail >= (nDwOffsImgNameBuild + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t)))
- {
- // have nImgLength, nImgCRC32, szImgMaterialNum, szImgNameBuild
- // need nothing
- nCntDwToCpy = nDwOffsImgNameBuild + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH / sizeof(uint32_t);
- nCntDwToDmp = 0;
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- }
- else if(nCntDwAvail > nDwOffsImgMaterialNum)
- {
- // have nImgLength, nImgCRC32, szImgMaterialNum
- // need szImgNameBuild
- nCntDwToCpy = nCntDwAvail;
- nCntDwToDmp = nSizeDst / sizeof(uint32_t) - nCntDwToCpy; // (1...3) + 6
- nDmpStart += nCntDwToCpy * sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nCntDwToCpy;
- nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
- }
- else if(nCntDwAvail > nDwOffsImgCRC32)
- {
- // have nImgLength, nImgCRC32
- // need szImgMaterialNum, szImgNameBuild
- nCntDwToCpy = 2;
- nCntDwToDmp = (GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH + GFA_APP_MAX_IMG_NAME_BUILD_LENGTH) / sizeof(uint32_t); // 4 + 6
- nDmpStart += nDwOffsImgMaterialNum * sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nDwOffsImgMaterialNum;
- nSizeDst -= nDwOffsImgMaterialNum * sizeof(uint32_t);
- }
- else if(nCntDwAvail == nDwOffsImgCRC32)
- {
- // have nImgLength
- // need nImgCRC32, szImgMaterialNum, szImgNameBuild
- nCntDwToCpy = nDwOffsImgCRC32;
- nCntDwToDmp = nSizeDst / sizeof(uint32_t) - nCntDwToCpy; // 1 + 4 + 4 + 6
- nDmpStart += sizeof(uint32_t);
- memcpy(pDwDst, &aDump[j + 1], nCntDwToCpy * sizeof(uint32_t));
- pDwDst += nCntDwToCpy;
- nSizeDst -= nCntDwToCpy * sizeof(uint32_t);
- }
- else
- {
- // have nothing
- // need nImgLength, nImgCRC32, pszImgMaterialNum, pszImgNameBuild
- nCntDwToCpy = 0;
- nCntDwToDmp = nSizeDst / sizeof(uint32_t); // 2 + 4 + 4 + 6
- }
- if(nCntDwToDmp)
- {
- if((nRet = GfaBlmBootloaderDump(hBlm, nNodeAddr, nDmpStart, nCntDwToDmp, pDwDst, nSizeDst)) != 0)
- return -1;
- }
- pii->nImgLength = aih.nImgLength;
- pii->nImgCRC32 = aih.nImgCRC32;
- memcpy(pii->szImgMaterialNum, aih.bl.szImgMaterialNum, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
- memcpy(pii->szImgNameBuild, aih.bl.szImgNameBuild, GFA_APP_MAX_IMG_NAME_BUILD_LENGTH);
- pii->szImgMaterialNum[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
- pii->szImgNameBuild[GFA_APP_MAX_IMG_NAME_BUILD_LENGTH - 1] = '\0';
- return 0;
- }
- }
- }
- }
- else
- {
- break;
- }
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmGetInfoBD(HGFABLM hBlm, uint8_t nNodeAddr, LPGFA_BL_APP_IMG_INFO paii)
- {
- if(hBlm && paii && !NODE_IS_MULTICAST(nNodeAddr))
- {
- int nRet1, nRet2;
- if((nRet1 = GfaBlmGetImgInfo(hBlm, nNodeAddr, GFA_APP_BOOTLOADER_START_ADDRESS, false, &paii->bl)) < 0)
- {
- memset(&paii->bl, 0, sizeof(paii->bl));
- paii->bl.nImgLength = paii->bl.nImgCRC32 = 0xFFFFFFFF;
- }
- if((nRet2 = GfaBlmGetImgInfo(hBlm, nNodeAddr, GFA_APP_APPLICATION_START_ADDRESS, true, &paii->app)) < 0)
- {
- memset(&paii->app, 0, sizeof(paii->app));
- paii->app.nImgLength = paii->app.nImgCRC32 = 0xFFFFFFFF;
- }
- return nRet1 + nRet2;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmGetInfoBI(HGFABLM hBlm, uint8_t nNodeAddr, LPGFA_BL_APP_IMG_INFO paii)
- {
- if(hBlm && paii && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- ssize_t nRet, nLen;
- char txb[32], rxb[256];
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BI", 2, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, paii, sizeof(GFA_BL_APP_IMG_INFO))) == sizeof(GFA_BL_APP_IMG_INFO))
- {
- paii->bl.nImgLength = bswap_32(paii->bl.nImgLength);
- paii->bl.nImgCRC32 = bswap_32(paii->bl.nImgCRC32);
- paii->app.nImgLength = bswap_32(paii->app.nImgLength);
- paii->app.nImgCRC32 = bswap_32(paii->app.nImgCRC32);
- return 0;
- }
- errno = EPROTO;
- return -1;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- errno = EPROTO;
- return -1;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmReadMaterialAndSerialID(HGFABLM hBlm, uint8_t nNodeAddr, char *pszMaterial, size_t nCbMaterial, char *pszSerial, size_t nCbSerial)
- {
- if(hBlm && pszMaterial && (nCbMaterial >= GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH) && pszSerial && (nCbSerial >= GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH) && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- ssize_t nRet, nLen;
- char txb[32], rxb[64], data[32];
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, "BR", 2, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
- if((nLen = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nLen;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nLen, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- if((nRet = GfaMininetMasterGetDataFromSlaveFrame(rxb, nLen, data, sizeof(data))) != (GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH + GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH))
- return -1;
- memcpy(pszMaterial, data, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
- pszMaterial[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
- memcpy(pszSerial, &data[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH], GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH);
- pszSerial[GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH - 1] = '\0';
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- if((nRet = GfaBlmBUCmdPollData(hBlm, nNodeAddr, data, sizeof(data), 200)) != (ssize_t)sizeof(data))
- return -1;
- memcpy(pszMaterial, data, GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH);
- pszMaterial[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH - 1] = '\0';
- memcpy(pszSerial, &data[GFA_APP_MAX_IMG_MATERIAL_NUM_LENGTH], GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH);
- pszSerial[GFA_APP_MAX_IMG_SERIAL_NUM_LENGTH - 1] = '\0';
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmWriteMaterialAndSerialID(HGFABLM hBlm, uint8_t nNodeAddr, const char *pszMaterial, const char *pszSerial)
- {
- if(hBlm && pszMaterial && pszSerial && !NODE_IS_MULTICAST(nNodeAddr))
- {
- uint8_t nIndex;
- size_t s;
- ssize_t nRet, nLen;
- size_t nLenMaterial, nLenSerial;
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- struct _TS
- {
- char szMaterial[16];
- char szSerial[16];
- }ts;
- char txb[64], rxb[32], cmd[64];
- nLenMaterial = strlen(pszMaterial);
- nLenSerial = strlen(pszSerial);
- if((nLenMaterial > 15) || (nLenSerial > 15))
- return -1;
- memcpy(ts.szMaterial, pszMaterial, nLenMaterial);
- if(nLenMaterial < 15)
- memset(&ts.szMaterial[nLenMaterial], ' ', 15 - nLenMaterial);
- ts.szMaterial[15] = '\0';
- memcpy(ts.szSerial, pszSerial, nLenSerial);
- if(nLenSerial < 15)
- memset(&ts.szSerial[nLenSerial], ' ', 15 - nLenSerial);
- ts.szSerial[15] = '\0';
- s = GfaBlmBuildCmdDataPacket("BW", 0, &ts, sizeof(ts), cmd, sizeof(cmd), false);
- nLen = GfaMininetMasterBuildFrame(pBlm->hMst, nNodeAddr, 0, cmd, s, txb, sizeof(txb));
- if((nRet = GfaMininetMasterTransmitFrame(pBlm->hMst, txb, nLen)) != nLen)
- return nRet;
- if((nRet = GfaMininetMasterReceiveFrame(pBlm->hMst, rxb, sizeof(rxb), true)) < 0)
- return nRet;
- nRet = GfaMininetMasterEvaluateSlaveResponse(pBlm->hMst, nNodeAddr, rxb, nRet, true, &nIndex);
- if(nRet == MINET_SLAVE_RESPONSE_SUCCESS)
- {
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_ACK)
- {
- return 0;
- }
- else if(nRet == MINET_SLAVE_RESPONSE_INDEX_IS_STATUS_CODE)
- {
- errno = -(int)nIndex;
- return -1;
- }
- return -1;
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- HGFAMINEMST GfaBlmGetMininetMasterHandle(HGFABLM hBlm)
- {
- if(hBlm)
- {
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- return pBlm->hMst;
- }
- errno = EINVAL;
- return NULL;
- }
- /////////////////////////////////////////////////////////////////////////////
- int GfaBlmSetVerbosity(HGFABLM hBlm, int nVerbosity)
- {
- if(hBlm)
- {
- LPGFA_BLM pBlm = (LPGFA_BLM)hBlm;
- if(nVerbosity < 0)
- nVerbosity = 0;
- else if(nVerbosity > 4)
- nVerbosity = 4;
- pBlm->nVerbosity = nVerbosity;
- return GfaMininetMasterSetVerbosity(pBlm->hMst, pBlm->nVerbosity);
- }
- errno = EINVAL;
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////
- uint8_t GfaBlmDataCheckSum(const void *pData, size_t nCbData)
- {
- uint8_t chk = 0;
- const uint8_t *pbData = (const uint8_t*)pData;
- while(nCbData--) {
- chk += *pbData++;
- }
- return chk;
- }
- /////////////////////////////////////////////////////////////////////////////
- size_t GfaBlmBuildCmdDataPacket(const char *pszCmd, uint8_t tiCmd, const void *pCmdData, size_t nCbCmdData, void *pPacket, size_t nCbPacket, bool bAddLenAndCheck)
- {
- size_t nLen = strlen(pszCmd);
- uint8_t *pbPacket = (uint8_t*)pPacket;
- tiCmd = tiCmd; // not yet used
- if(nCbPacket < (nLen + nCbCmdData + ((pCmdData && nCbCmdData && bAddLenAndCheck) ? 2 : 0)))
- return 0;
- memcpy(pbPacket, pszCmd, nLen);
- pbPacket += nLen;
- if(pCmdData && nCbCmdData)
- {
- if(bAddLenAndCheck)
- {
- *pbPacket++ = nCbCmdData + 2;
- *pbPacket++ = GfaBlmDataCheckSum(pCmdData, nCbCmdData);
- nLen += 2;
- }
- memcpy(pbPacket, pCmdData, nCbCmdData);
- nLen += nCbCmdData;
- }
- return nLen;
- }
- const char* GfaBlmStrError(int nErrorCode)
- {
- switch(nErrorCode)
- {
- case -COMMAND_RET_UNKNOWN_CMD:
- return "Unknown Bootloader command";
- case -COMMAND_RET_INVALID_CMD:
- return "Invalid Bootloader command";
- case -COMMAND_RET_INVALID_ADR:
- return "Invalid Flash address";
- case -COMMAND_RET_FLASH_FAIL:
- return "Bootloader failed to erase flash";
- case -COMMAND_RET_CRC_FAIL:
- return "Invalid Image CRC32";
- default:
- return GfaMininetMasterStrError(nErrorCode);
- }
- }
|