123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #ifndef EMULATEHOST64_SOURCE
- #define EMULATEHOST64_SOURCE
- #include <unistd.h>
- #include <memory.h>
- #include "../plc/plc.h"
- #include "../ether/channel.h"
- #include "../tools/error.h"
- #include "../tools/flags.h"
- #include "../tools/files.h"
- #include "../nvm/nvm.h"
- #include "../pib/pib.h"
- signed EmulateHost64 (struct plc * plc)
- {
- struct channel * channel = (struct channel *)(plc->channel);
- struct message * message = (struct message *)(plc->message);
- static char const * actions [] =
- {
- "start device",
- "store firmware",
- "store parameters",
- "update host",
- "config memory",
- "restore defaults",
- "unknown"
- };
- #ifndef __GNUC__
- #pragma pack (push,1)
- #endif
- struct __packed vs_host_action_ind
- {
- struct ethernet_hdr ethernet;
- struct qualcomm_hdr qualcomm;
- uint8_t MACTION;
- uint8_t MAJOR_VERSION;
- uint8_t MINOR_VERSION;
- }
- * indicate = (struct vs_host_action_ind *) (message);
- #if 0
- struct __packed vs_host_action_rsp
- {
- struct ethernet_hdr ethernet;
- struct qualcomm_hdr qualcomm;
- uint8_t MSTATUS;
- }
- * response = (struct vs_host_action_rsp *) (message);
- #endif
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- char const * NVM = plc->NVM.name;
- char const * PIB = plc->PIB.name;
- signed status;
- signed action;
- Request (plc, "Waiting for Host Action");
- while (1)
- {
- status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
- if (status < 0)
- {
- break;
- }
- if (status > 0)
- {
- printf ("\n");
- if (indicate->MACTION < (sizeof (actions) / sizeof (char const *)))
- {
- Confirm (plc, "Host Action Request is (%d) %s.", indicate->MACTION, actions [indicate->MACTION]);
- }
- else
- {
- error (0, ENOTSUP, "Host Action 0x%0X", indicate->MACTION);
- continue;
- }
- action = indicate->MACTION;
- memcpy (channel->peer, indicate->ethernet.OSA, sizeof (channel->peer));
- if (HostActionResponse (plc))
- {
- return (-1);
- }
- if (action == 0x00)
- {
- if (BootDevice1 (plc))
- {
- return (-1);
- }
- if (_anyset (plc->flags, PLC_FLASH_DEVICE))
- {
- if (WriteNVM (plc))
- {
- return (-1);
- }
- if (WritePIB (plc))
- {
- return (-1);
- }
- if (FlashNVM (plc))
- {
- return (-1);
- }
- }
- continue;
- }
- if (action == 0x01)
- {
- close (plc->NVM.file);
- if (ReadFirmware1 (plc))
- {
- return (-1);
- }
- if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->NVM.name);
- }
- if (ResetDevice (plc))
- {
- return (-1);
- }
- continue;
- }
- if (action == 0x02)
- {
- close (plc->PIB.file);
- if (ReadParameters1 (plc))
- {
- return (-1);
- }
- if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->PIB.name);
- }
- if (ResetDevice (plc))
- {
- return (-1);
- }
- continue;
- }
- if (action == 0x03)
- {
- close (plc->PIB.file);
- if (ReadParameters1 (plc))
- {
- return (-1);
- }
- if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->PIB.name);
- }
- close (plc->NVM.file);
- if (ReadFirmware1 (plc))
- {
- return (-1);
- }
- if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->NVM.name);
- }
- if (ResetDevice (plc))
- {
- return (-1);
- }
- continue;
- }
- if (action == 0x04)
- {
- if (InitDevice1 (plc))
- {
- return (-1);
- }
- continue;
- }
- if (action == 0x05)
- {
- close (plc->NVM.file);
- if ((plc->NVM.file = open (plc->NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->NVM.name);
- }
- close (plc->PIB.file);
- if ((plc->PIB.file = open (plc->PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->PIB.name);
- }
- if (ResetDevice (plc))
- {
- return (-1);
- }
- continue;
- }
- if (action == 0x06)
- {
- close (plc->PIB.file);
- if (ReadParameters1 (plc))
- {
- return (-1);
- }
- if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
- {
- error (1, errno, "%s", plc->PIB.name);
- }
- continue;
- }
- error (0, ENOSYS, "Host Action 0x%02X", action);
- }
- }
- return (0);
- }
- #endif
|