MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / doRemoteCommand

Function doRemoteCommand

xs/platforms/esp/xsPlatform.c:1025–1288  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1023}
1024
1025void doRemoteCommand(txMachine *the, uint8_t *cmd, uint32_t cmdLen)
1026{
1027 uint16_t resultID = 0;
1028 int16_t resultCode = 0;
1029 uint8_t cmdID;
1030 int baud = 0;
1031
1032 if (!cmdLen)
1033 return;
1034
1035 cmdID = *cmd++;
1036 cmdLen -= 1;
1037
1038 if (cmdLen >= 2) {
1039 resultID = (cmd[0] << 8) | cmd[1];
1040 cmd += 2, cmdLen -= 2;
1041
1042 the->echoBuffer[0] = 5;
1043 the->echoBuffer[1] = resultID >> 8;
1044 the->echoBuffer[2] = resultID & 0xff;
1045 the->echoBuffer[3] = 0;
1046 the->echoBuffer[4] = 0;
1047 the->echoOffset = 5;
1048 }
1049
1050 switch (cmdID) {
1051 case 1: // restart
1052 the->wsState = 18;
1053 break;
1054
1055#if MODDEF_XS_MODS
1056 case 2: { // uninstall
1057 uint8_t erase[16] = {0};
1058#if ESP32
1059 const esp_partition_t *partition = esp_partition_find_first(0x40, 1, NULL);
1060 if (!partition || (ESP_OK != esp_partition_write(partition, 0, erase, sizeof(erase))))
1061 resultCode = -2;
1062#else
1063 uint32_t offset = (uintptr_t)kModulesStart - (uintptr_t)kFlashStart;
1064 if (!modSPIWrite(offset, sizeof(erase), erase))
1065 resultCode = -2;
1066#endif
1067 } break;
1068
1069 case 3: { // install some
1070 uint32_t offset = c_read32be(cmd);
1071 cmd += 4, cmdLen -= 4;
1072#if ESP32
1073 const esp_partition_t *partition = esp_partition_find_first(0x40, 1, NULL);
1074 resultCode = -3;
1075 if (partition) {
1076 int firstSector = offset / SPI_FLASH_SEC_SIZE, lastSector = (offset + cmdLen) / SPI_FLASH_SEC_SIZE;
1077 if (!((offset + cmdLen) % SPI_FLASH_SEC_SIZE)) // ends on sector boundary, don't fall into next sector
1078 lastSector -= 1;
1079 if (!(offset % SPI_FLASH_SEC_SIZE)) // starts on sector boundary
1080 esp_partition_erase_range(partition, offset, SPI_FLASH_SEC_SIZE * ((lastSector - firstSector) + 1));
1081 else if (firstSector != lastSector)
1082 esp_partition_erase_range(partition, (firstSector + 1) * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE * (lastSector - firstSector)); // crosses into a new sector

Callers 1

fxReceiveFunction · 0.70

Calls 10

modGetModAtomFunction · 0.85
modSPIWriteFunction · 0.70
modSPIEraseFunction · 0.70
modSetTimeFunction · 0.70
modSetTimeZoneFunction · 0.70
fxSendFunction · 0.70
fxDisconnectFunction · 0.70
modPreferenceSetFunction · 0.50
modPreferenceGetFunction · 0.50

Tested by

no test coverage detected