Exemplo n.º 1
0
bool Helpers::_validateConfigRoot(const JsonObject& object) {
  if (!object.containsKey("name") || !object["name"].is<const char*>()) {
    Logger.logln(F("✖ name is not a string"));
    return false;
  }
  if (strlen(object["name"]) + 1 > MAX_FRIENDLY_NAME_LENGTH) {
    Logger.logln(F("✖ name is too long"));
    return false;
  }
  if (object.containsKey("device_id")) {
    if (!object["device_id"].is<const char*>()) {
      Logger.logln(F("✖ device_id is not a string"));
      return false;
    }
    if (strlen(object["device_id"]) + 1 > MAX_DEVICE_ID_LENGTH) {
      Logger.logln(F("✖ device_id is too long"));
      return false;
    }
  }

  const char* name = object["name"];

  if (strcmp_P(name, PSTR("")) == 0) {
    Logger.logln(F("✖ name is empty"));
    return false;
  }

  return true;
}
Exemplo n.º 2
0
ConfigValidationResult Helpers::_validateConfigRoot(const JsonObject& object) {
  ConfigValidationResult result;
  result.valid = false;
  result.reason = nullptr;
  if (!object.containsKey("name") || !object["name"].is<const char*>()) {
    result.reason = F("name is not a string");
    return result;
  }
  if (strlen(object["name"]) + 1 > MAX_FRIENDLY_NAME_LENGTH) {
    result.reason = F("name is too long");
    return result;
  }
  if (object.containsKey("device_id")) {
    if (!object["device_id"].is<const char*>()) {
      result.reason = F("device_id is not a string");
      return result;
    }
    if (strlen(object["device_id"]) + 1 > MAX_DEVICE_ID_LENGTH) {
      result.reason = F("device_id is too long");
      return result;
    }
  }

  const char* name = object["name"];

  if (strcmp_P(name, PSTR("")) == 0) {
    result.reason = F("name is empty");
    return result;
  }

  result.valid = true;
  return result;
}
Exemplo n.º 3
0
int cBurner::setSP( JsonObject& root )
{
	int fail = 0;
	int posReturn =0;
	
	if(root.containsKey("BuTmax")) {
		if(root["BuTmax"].is<double>()) {
			MaxTempOperation =  root["BuTmax"].as<double>();
			posReturn++;
		}
		else fail=1;
	}
	else fail=1;
	
	if(root.containsKey("ButMin")) {
		if(root["ButMin"].is<long>()) {
			MinBurnTime =  static_cast<unsigned long>(root["ButMin"].as<long>());
			posReturn++;
		}
		else fail=1;
	}
	else fail=1;
	
	if (fail == 0) { // If all three parameter objects were successfully filled
		return posReturn;
	}
	else {
		return 0;
	}
}
Exemplo n.º 4
0
int cRooms::setRooms( JsonObject& root )
{
	int posReturn = 0;
	int fail = 0;
	
	if(root.containsKey("RTypes")) {
		if(root["RTypes"].is<JsonArray&>()){
			JsonArray& RoomsTypes = root["RTypes"];
			if (RoomsTypes.size()==nRooms) {
				for (int i = 0; i<nRooms;i++){
					if ( RoomsTypes[i].is<long>())	Room[i].RoomType = static_cast<RoomTypes>(RoomsTypes[i].as<long>());
					else fail=1;
				}
			}
			else fail=1;
		}
		else fail=1;
		posReturn++;
	}
	
	if(root.containsKey("RTs")) {
		if(root["RTs"].is<JsonArray&>()){
			JsonArray& RoomsTemps = root["RTs"];
			if (RoomsTemps.size()==nRoomTypes) {
				for (int i = 0; i<nRoomTypes;i++){
					if ( RoomsTemps[i].is<double>())	MasterSpTemps[i] = RoomsTemps[i].as<double>();
					else fail=1;
				}
			}
			else fail=1;
		}
		else fail=1;
		posReturn++;
	}
	
	if(root.containsKey("SetType")) {
		if(root["SetType"].is<long>()) {
			SetType =  static_cast<SetTypes>(root["SetType"].as<long>());
			posReturn++;
		}
		else fail=1;
	}
	else fail=1;
	
	if (fail == 0) { // If all three parameter objects were successfully filled
		return posReturn;
	}
	else {
		return 0;
	}
}
Exemplo n.º 5
0
bool Helpers::_validateConfigWifi(const JsonObject& object) {
  if (!object.containsKey("wifi") || !object["wifi"].is<JsonObject&>()) {
    Logger.logln(F("✖ wifi is not an object"));
    return false;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("ssid") || !object["wifi"]["ssid"].is<const char*>()) {
    Logger.logln(F("✖ wifi.ssid is not a string"));
    return false;
  }
  if (strlen(object["wifi"]["ssid"]) + 1 > MAX_WIFI_SSID_LENGTH) {
    Logger.logln(F("✖ wifi.ssid is too long"));
    return false;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("password") || !object["wifi"]["password"].is<const char*>()) {
    Logger.logln(F("✖ wifi.password is not a string"));
    return false;
  }
  if (strlen(object["wifi"]["password"]) + 1 > MAX_WIFI_PASSWORD_LENGTH) {
    Logger.logln(F("✖ wifi.password is too long"));
    return false;
  }

  const char* wifiSsid = object["wifi"]["ssid"];
  if (strcmp_P(wifiSsid, PSTR("")) == 0) {
    Logger.logln(F("✖ wifi.ssid is empty"));
    return false;
  }

  return true;
}
Exemplo n.º 6
0
int cRooms::setOffsetTemp( JsonObject& root )
{
	if(root.containsKey("RTs")) {
		if(root["RTs"].is<JsonArray&>()){
			JsonArray& temps = root["RTs"];
			
			if (temps.size()==(nSetTypes*nRoomTypes*nDayTypes*nSwitch))
			{
				for(int iSet = 0; iSet<nSetTypes; iSet++)
				{
					for(int iRoomType = 0; iRoomType<nRoomTypes; iRoomType++) // Iterate over all Roomtypes (Living, sleeping, hallway, side)
					{
						for(int iDayType = 0; iDayType<nDayTypes; iDayType++)
						{
							for(int iSwitch = 0; iSwitch<nSwitch; iSwitch++)
							{
								// Last iteration
								//int idx = (nSwitch-1)+(nDayTypes-1)*(nSwitch)+(nRoomTypes-1)*(nDayTypes)*(nSwitch)+(nSetTypes-1)*(nRoomTypes)*(nDayTypes)*(nSwitch);
								int idx = iSwitch+iDayType*(nSwitch)+iRoomType*(nDayTypes)*(nSwitch)+iSet*(nRoomTypes)*(nDayTypes)*(nSwitch);
								TempOffsetSchedule[iSet][iRoomType][iDayType][iSwitch].temp = temps[idx].as<double>();
							}
						}
					}
				}
				return 1;
			}
		}
	}
	return 0;
}
Exemplo n.º 7
0
ConfigValidationResult Helpers::_validateConfigWifi(const JsonObject& object) {
  ConfigValidationResult result;
  result.valid = false;
  result.reason = nullptr;

  if (!object.containsKey("wifi") || !object["wifi"].is<JsonObject&>()) {
    result.reason = F("wifi is not an object");
    return result;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("ssid") || !object["wifi"]["ssid"].is<const char*>()) {
    result.reason = F("wifi.ssid is not a string");
    return result;
  }
  if (strlen(object["wifi"]["ssid"]) + 1 > MAX_WIFI_SSID_LENGTH) {
    result.reason = F("wifi.ssid is too long");
    return result;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("password") || !object["wifi"]["password"].is<const char*>()) {
    result.reason = F("wifi.password is not a string");
    return result;
  }
  if (strlen(object["wifi"]["password"]) + 1 > MAX_WIFI_PASSWORD_LENGTH) {
    result.reason = F("wifi.password is too long");
    return result;
  }

  const char* wifiSsid = object["wifi"]["ssid"];
  if (strcmp_P(wifiSsid, PSTR("")) == 0) {
    result.reason = F("wifi.ssid is empty");
    return result;
  }

  result.valid = true;
  return result;
}
Exemplo n.º 8
0
int cRooms::setOffsetTemp( JsonObject& root )
{
        int fail=0;
        int succes=0;
	if(root.containsKey("RTo")) {
		if(root["RTo"].is<JsonArray&>()){
			JsonArray& temps = root["RTo"];
			
			if (temps.size()==(nSetTypes*nRoomTypes*nDayTypes*nSwitch))
			{
				for(int iSet = 0; iSet<nSetTypes; iSet++) // Normal
				{
					for(int iRoomType = 0; iRoomType<nRoomTypes; iRoomType++) // (Living, sleeping, hallway, bath, side)
					{
						for(int iDayType = 0; iDayType<nDayTypes; iDayType++) // Weekend, Workday
						{
							for(int iSwitch = 0; iSwitch<nSwitch; iSwitch++) // 4 switch times
							{
								// Last iteration
								int idx = iSwitch+iDayType*(nSwitch)+iRoomType*(nDayTypes)*(nSwitch)+iSet*(nRoomTypes)*(nDayTypes)*(nSwitch);
               if (temps[idx].is<double>()){
                                                                double temp_ =  temps[idx].as<double>();
                                                                if((temp_>-15.0)&&(temp_<15.0)&&(temp_!=TempOffsetSchedule[iSet][iRoomType][iDayType][iSwitch].temp)){
                                                                    TempOffsetSchedule[iSet][iRoomType][iDayType][iSwitch].temp = temp_;
                                                                    succes = 1;
                                                                }
               }
               else fail=1;
							}
						}
					}
				}
			}
		}
	}
 
	return ((!fail)&&(succes));
}
Exemplo n.º 9
0
int cRooms::setOffsetTime( JsonObject& root )
{
        int fail=0;
        int succes=0;
	if(root.containsKey("Rt")) {
		if(root["Rt"].is<JsonArray&>()){
			JsonArray& times = root["Rt"];
			
			if (times.size()==(nSetTypes*nRoomTypes*nDayTypes*nSwitch))
			{
				for(int iSet = 0; iSet<nSetTypes; iSet++)
				{
					for(int iRoomType = 0; iRoomType<nRoomTypes; iRoomType++) // Iterate over all Roomtypes (Living, sleeping, hallway, side)
					{
						for(int iDayType = 0; iDayType<nDayTypes; iDayType++)
						{
							for(int iSwitch = 0; iSwitch<nSwitch; iSwitch++)
							{
								int idx = iSwitch+iDayType*(nSwitch)+iRoomType*(nDayTypes)*(nSwitch)+iSet*(nRoomTypes)*(nDayTypes)*(nSwitch);
                                                                if (times[idx].is<long>()) {
                                                                  unsigned long oldTime = TempOffsetSchedule[iSet][iRoomType][iDayType][iSwitch].time.totalseconds(); 
                                                                  unsigned long newTime = times[idx].as<long>();
                                                                  if((newTime>=0)&&(24*60*60-newTime>=0)&&(newTime!=oldTime)){
                                                                      TempOffsetSchedule[iSet][iRoomType][iDayType][iSwitch].time.set(newTime);
                                                                      succes=1;
                                                                  }
                                                                }
                                                                else fail=1;
							}
						}
					}
				}
			}
		}
	}
 
	return (!fail)&&(succes);
}
Exemplo n.º 10
0
bool DriveCommand::fromJson(JsonObject& cmd) {
  if(!cmd.containsKey("type")) return false;
  
  const char* type = cmd["type"];
  if(strcmp(type, "SET_HEADING") == 0) {
    command = SET_HEADING;
  } else if(strcmp(type, "DRIVE") == 0) {
    command = DRIVE;
  } else {  
    Serial.print("unrecognized type ");
    Serial.println(type);
    return false;
  }
  
  if(!cmd.containsKey("cid") || !cmd.containsKey("pid") || !cmd.containsKey("duration")) {
    return false;
  }
  
  cid = cmd["cid"];
  pid = cmd["pid"];
  duration = cmd["duration"];
  
  if(command == DRIVE) {
    if(!cmd.containsKey("speed") || !cmd.containsKey("heading")) return false;
    
    payload.drive.speed = cmd["speed"];
    payload.drive.heading = cmd["heading"];
    return true;
  } else if(command == SET_HEADING) {
    if(!cmd.containsKey("heading")) return false;
    
    payload.heading.heading = cmd["heading"];
    return true;
  } else {
    return false;
  }
 
}
Exemplo n.º 11
0
int cRooms::setRooms( JsonObject& root )
{
	int posReturn = 0;
	int fail = 0;
	
	if(root.containsKey("RTypes")) {
		if(root["RTypes"].is<JsonArray&>()){
			JsonArray& RoomsTypes = root["RTypes"];
			if (RoomsTypes.size()==nRooms) {
				for (int i = 0; i<nRooms;i++){
					if ( RoomsTypes[i].is<long>()){
                                            RoomTypes RoomType_ = static_cast<RoomTypes>(RoomsTypes[i].as<long>());
                                            if((RoomType_>=0)&&(RoomType_<nRoomTypes)&&(RoomType_!=Room[i].RoomType)){
                                                Room[i].RoomType = RoomType_;
                                            }
                                        }
					else fail=1;
				}
			}
			else fail=1;
		}
		else fail=1;
		posReturn++;
	}
	
	if(root.containsKey("RTs")) {
		if(root["RTs"].is<JsonArray&>()){
			JsonArray& RoomsTemps = root["RTs"];
			if (RoomsTemps.size()==nRoomTypes) {
				for (int i = 0; i<nRoomTypes;i++){
					if ( RoomsTemps[i].is<double>()){
                                            double MasterSpTemps_ = RoomsTemps[i].as<double>();
                                            if ((MasterSpTemps_>10.0)&&(MasterSpTemps_<25.0)&&(MasterSpTemps_!=MasterSpTemps[i])){
                                                MasterSpTemps[i] = MasterSpTemps_;
                                            }
                                        }
					else fail=1;
				}
			}
			else fail=1;
		}
		else fail=1;
		posReturn++;
	}
	
	if(root.containsKey("SetType")) {
		if(root["SetType"].is<long>()) {
			SetTypes SetType_ =  static_cast<SetTypes>(root["SetType"].as<long>());
                        if((SetType_>=0)&&(SetType_<nSetTypes)&&(SetType_!=SetType)){
                            SetType = SetType_;
                            posReturn++;
                        }
		}
		else fail=1;
	}
	
	if (fail == 0) { // If all three parameter objects were successfully filled
		return posReturn;
	}
	else {
		return 0;
	}
}
Exemplo n.º 12
0
bool Helpers::_validateConfigMqtt(const JsonObject& object) {
  if (!object.containsKey("mqtt") || !object["mqtt"].is<JsonObject&>()) {
    Logger.logln(F("✖ mqtt is not an object"));
    return false;
  }
  bool mdns = false;
  if (object["mqtt"].as<JsonObject&>().containsKey("mdns")) {
    if (!object["mqtt"]["mdns"].is<const char*>()) {
      Logger.logln(F("✖ mqtt.mdns is not a string"));
      return false;
    }
    if (strlen(object["mqtt"]["mdns"]) + 1 > MAX_HOSTNAME_LENGTH) {
      Logger.logln(F("✖ mqtt.mdns is too long"));
      return false;
    }
    mdns = true;
  } else {
    if (!object["mqtt"].as<JsonObject&>().containsKey("host") || !object["mqtt"]["host"].is<const char*>()) {
      Logger.logln(F("✖ mqtt.host is not a string"));
      return false;
    }
    if (strlen(object["mqtt"]["host"]) + 1 > MAX_HOSTNAME_LENGTH) {
      Logger.logln(F("✖ mqtt.host is too long"));
      return false;
    }
    if (object["mqtt"].as<JsonObject&>().containsKey("port") && !object["mqtt"]["port"].is<unsigned int>()) {
      Logger.logln(F("✖ mqtt.port is not an unsigned integer"));
      return false;
    }
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("base_topic")) {
    if (!object["mqtt"]["base_topic"].is<const char*>()) {
      Logger.logln(F("✖ mqtt.base_topic is not a string"));
      return false;
    }

    if (strlen(object["mqtt"]["base_topic"]) + 1 > MAX_MQTT_BASE_TOPIC_LENGTH) {
      Logger.logln(F("✖ mqtt.base_topic is too long"));
      return false;
    }
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("auth")) {
    if (!object["mqtt"]["auth"].is<bool>()) {
      Logger.logln(F("✖ mqtt.auth is not a boolean"));
      return false;
    }

    if (object["mqtt"]["auth"]) {
      if (!object["mqtt"].as<JsonObject&>().containsKey("username") || !object["mqtt"]["username"].is<const char*>()) {
        Logger.logln(F("✖ mqtt.username is not a string"));
        return false;
      }
      if (strlen(object["mqtt"]["username"]) + 1 > MAX_MQTT_CREDS_LENGTH) {
        Logger.logln(F("✖ mqtt.username is too long"));
        return false;
      }
      if (!object["mqtt"].as<JsonObject&>().containsKey("password") || !object["mqtt"]["password"].is<const char*>()) {
        Logger.logln(F("✖ mqtt.password is not a string"));
        return false;
      }
      if (strlen(object["mqtt"]["password"]) + 1 > MAX_MQTT_CREDS_LENGTH) {
        Logger.logln(F("✖ mqtt.password is too long"));
        return false;
      }
    }
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("ssl")) {
    if (!object["mqtt"]["ssl"].is<bool>()) {
      Logger.logln(F("✖ mqtt.ssl is not a boolean"));
      return false;
    }

    if (object["mqtt"]["ssl"]) {
      if (object["mqtt"].as<JsonObject&>().containsKey("fingerprint") && !object["mqtt"]["fingerprint"].is<const char*>()) {
        Logger.logln(F("✖ mqtt.fingerprint is not a string"));
        return false;
      }
    }
  }

  if (mdns) {
    const char* mdnsService = object["mqtt"]["mdns"];
    if (strcmp_P(mdnsService, PSTR("")) == 0) {
      Logger.logln(F("✖ mqtt.mdns is empty"));
      return false;
    }
  } else {
    const char* host = object["mqtt"]["host"];
    if (strcmp_P(host, PSTR("")) == 0) {
      Logger.logln(F("✖ mqtt.host is empty"));
      return false;
    }
  }

  return true;
}
Exemplo n.º 13
0
bool Helpers::_validateConfigOta(const JsonObject& object) {
  if (!object.containsKey("ota") || !object["ota"].is<JsonObject&>()) {
    Logger.logln(F("✖ ota is not an object"));
    return false;
  }
  if (!object["ota"].as<JsonObject&>().containsKey("enabled") || !object["ota"]["enabled"].is<bool>()) {
    Logger.logln(F("✖ ota.enabled is not a boolean"));
    return false;
  }
  if (object["ota"]["enabled"]) {
    if (object["ota"].as<JsonObject&>().containsKey("mdns")) {
      if (!object["ota"]["mdns"].is<const char*>()) {
        Logger.logln(F("✖ ota.mdns is not a string"));
        return false;
      }
      if (strlen(object["ota"]["mdns"]) + 1 > MAX_HOSTNAME_LENGTH) {
        Logger.logln(F("✖ ota.mdns is too long"));
        return false;
      }
    } else {
      if (object["ota"].as<JsonObject&>().containsKey("host")) {
        if (!object["ota"]["host"].is<const char*>()) {
          Logger.logln(F("✖ ota.host is not a string"));
          return false;
        }
        if (strlen(object["ota"]["host"]) + 1 > MAX_HOSTNAME_LENGTH) {
          Logger.logln(F("✖ ota.host is too long"));
          return false;
        }
      }
      if (object["ota"].as<JsonObject&>().containsKey("port") && !object["ota"]["port"].is<unsigned int>()) {
        Logger.logln(F("✖ ota.port is not an unsigned integer"));
        return false;
      }
    }
    if (object["ota"].as<JsonObject&>().containsKey("path")) {
      if (!object["ota"]["path"].is<const char*>()) {
        Logger.logln(F("✖ ota.path is not a string"));
        return false;
      }
      if (strlen(object["ota"]["path"]) + 1 > MAX_OTA_PATH_LENGTH) {
        Logger.logln(F("✖ ota.path is too long"));
        return false;
      }
    }
    if (object["ota"].as<JsonObject&>().containsKey("ssl")) {
      if (!object["ota"]["ssl"].is<bool>()) {
        Logger.logln(F("✖ ota.ssl is not a boolean"));
        return false;
      }

      if (object["ota"]["ssl"]) {
        if (object["ota"].as<JsonObject&>().containsKey("fingerprint") && !object["ota"]["fingerprint"].is<const char*>()) {
          Logger.logln(F("✖ ota.fingerprint is not a string"));
          return false;
        }
      }
    }
  }

  return true;
}
Exemplo n.º 14
0
bool HelpersClass::validateConfig(JsonObject& object) {
  if (!object.containsKey("name") || !object["name"].is<const char*>()) {
    Logger.logln("✖ name is not a string");
    return false;
  }

  if (!object.containsKey("wifi") || !object["wifi"].is<JsonObject&>()) {
    Logger.logln("✖ wifi is not an object");
    return false;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("ssid") || !object["wifi"]["ssid"].is<const char*>()) {
    Logger.logln("✖ wifi.ssid is not a string");
    return false;
  }
  if (!object["wifi"].as<JsonObject&>().containsKey("password") || !object["wifi"]["password"].is<const char*>()) {
    Logger.logln("✖ wifi.password is not a string");
    return false;
  }

  if (!object.containsKey("mqtt") || !object["mqtt"].is<JsonObject&>()) {
    Logger.logln("✖ mqtt is not an object");
    return false;
  }
  if (!object["mqtt"].as<JsonObject&>().containsKey("host") || !object["mqtt"]["host"].is<const char*>()) {
    Logger.logln("✖ mqtt.host is not a string");
    return false;
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("port") && !object["mqtt"]["port"].is<uint16_t>()) {
    Logger.logln("✖ mqtt.port is not an unsigned integer");
    return false;
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("auth")) {
    if (!object["mqtt"]["auth"].is<bool>()) {
      Logger.logln("✖ mqtt.auth is not a boolean");
      return false;
    }

    if (object["mqtt"]["auth"]) {
      if (!object["mqtt"].as<JsonObject&>().containsKey("username") || !object["mqtt"]["username"].is<const char*>()) {
        Logger.logln("✖ mqtt.username is not a string");
        return false;
      }
      if (!object["mqtt"].as<JsonObject&>().containsKey("password") || !object["mqtt"]["password"].is<const char*>()) {
        Logger.logln("✖ mqtt.password is not a string");
        return false;
      }
    }
  }
  if (object["mqtt"].as<JsonObject&>().containsKey("ssl")) {
    if (!object["mqtt"]["ssl"].is<bool>()) {
      Logger.logln("✖ mqtt.ssl is not a boolean");
      return false;
    }

    if (object["mqtt"]["ssl"]) {
      if (object["mqtt"].as<JsonObject&>().containsKey("fingerprint") && !object["mqtt"]["fingerprint"].is<const char*>()) {
        Logger.logln("✖ mqtt.fingerprint is not a string");
        return false;
      }
    }
  }

  if (!object.containsKey("ota") || !object["ota"].is<JsonObject&>()) {
    Logger.logln("✖ ota is not an object");
    return false;
  }
  if (!object["ota"].as<JsonObject&>().containsKey("enabled") || !object["ota"]["enabled"].is<bool>()) {
    Logger.logln("✖ ota.enabled is not a boolean");
    return false;
  }
  if (object["ota"]["enabled"]) {
    if (object["ota"].as<JsonObject&>().containsKey("host") && !object["ota"]["host"].is<const char*>()) {
      Logger.logln("✖ ota.host is not a string");
      return false;
    }
    if (object["ota"].as<JsonObject&>().containsKey("port") && !object["ota"]["port"].is<uint16_t>()) {
      Logger.logln("✖ ota.port is not an unsigned integer");
      return false;
    }
    if (object["ota"].as<JsonObject&>().containsKey("path") && !object["ota"]["path"].is<const char*>()) {
      Logger.logln("✖ ota.path is not a string");
      return false;
    }
    if (object["ota"].as<JsonObject&>().containsKey("ssl")) {
      if (!object["ota"]["ssl"].is<bool>()) {
        Logger.logln("✖ ota.ssl is not a boolean");
        return false;
      }

      if (object["ota"]["ssl"]) {
        if (object["ota"].as<JsonObject&>().containsKey("fingerprint") && !object["ota"]["fingerprint"].is<const char*>()) {
          Logger.logln("✖ ota.fingerprint is not a string");
          return false;
        }
      }
    }
  }

  const char* name = object["name"];
  const char* wifi_ssid = object["wifi"]["ssid"];
  const char* mqtt_host = object["mqtt"]["host"];

  if (strcmp(name, "") == 0) {
    Logger.logln("✖ name is empty");
    return false;
  }
  if (strcmp(wifi_ssid, "") == 0) {
    Logger.logln("✖ wifi.ssid is empty");
    return false;
  }
  if (strcmp(mqtt_host, "") == 0) {
    Logger.logln("✖ mqtt.host is empty");
    return false;
  }

  return true;
}
Exemplo n.º 15
0
void handle(JsonObject& resp, JsonObject& req) {
	Serial.begin(BAUDRATE, SerialConfig::SERIAL_8E1, SerialMode::SERIAL_FULL);
	Serial.swap();
	String cmd = req["request"];
	resp["id"] = req["id"];
	resp["reply"] = req["request"];
	uint32_t startTime = millis();
	Erc erc = 0;
	if (cmd.equals("resetBootloader")) {

		erc = stm32.resetSystem();

	} else if (cmd.equals("resetFlash")) {

		erc = stm32.resetFlash();

	} else if (cmd.equals("goFlash")) {

		uint32_t address = req["address"];
		erc = stm32.go(address);
		resp["address"] = address;

	} else if (cmd.equals("status")) {

		resp["esp_id"] = ESP.getChipId();
		resp["heap"] = ESP.getFreeHeap();
		resp["upTime"] = millis();
		resp["version"] = __DATE__ " " __TIME__;
		resp["usart.rxd"] = Stm32::_usartRxd;
		resp["mode"] =
				stm32.getMode() == Stm32::M_FLASH ?
						"APPLICATION" : "BOOTLOADER";
		resp["baudrate"] = BAUDRATE;

	} else if (cmd.equals("getId")) {

		uint16_t chipId;
		erc = stm32.getId(chipId);
		if (erc == E_OK) {
			resp["chipId"] = chipId;
		}

	} else if (cmd.equals("getVersion")) {

		uint8_t version;
		erc = stm32.getVersion(version);
		if (erc == E_OK) {
			resp["version"] = version;
		}

	} else if (cmd.equals("get")) {

		Bytes data(30);
		Str strData(60);
		uint8_t version;
		erc = stm32.get(version, data);
		if (erc == E_OK) {
			erc = Base64::encode(strData, data);
			resp["cmds"] = String(strData.c_str());
			resp["version"] = version;
		}

	} else if (cmd.equals("writeMemory")) {

		Bytes data(256);
		Str str((const char*) req["data"]);
		uint32_t address = req["address"];
		resp["address"] = address;
		erc = Base64::decode(data, str);
		resp["length"] = data.length();
		if (erc == E_OK) {
			erc = stm32.writeMemory(address, data);
		}

	} else if (cmd.equals("eraseMemory")) {

		Bytes pages(256);
		Str str((const char*) req["pages"]);
		erc = Base64::decode(pages, str);
		resp["length"] = pages.length();
		erc = stm32.eraseMemory(pages);

	} else if (cmd.equals("extendedEraseMemory")) {

		erc = stm32.extendedEraseMemory();

	} else if (cmd.equals("eraseAll")) {

		erc = stm32.eraseAll();

	} else if (cmd.equals("readMemory")) {

		Str strData(410);
		Bytes data(256);
		uint32_t address = req["address"];
		uint32_t length = req["length"];
		resp["address"] = address;
		resp["length"] = length;
		erc = stm32.readMemory(address, length, data);
		if (erc == E_OK) {
			erc = Base64::encode(strData, data);
			resp["data"] = String(strData.c_str());
		}

	} else if (cmd.equals("writeProtect")) {

		Bytes data(256);
		Str str((const char*) req["data"]);
		erc = Base64::decode(data, str);
		erc = stm32.writeProtect(data);

	} else if (cmd.equals("writeUnprotect")) {

		erc = stm32.writeUnprotect();

	} else if (cmd.equals("readoutProtect")) {

		erc = stm32.readoutProtect();

	} else if (cmd.equals("readoutUnprotect")) {

		erc = stm32.readoutUnprotect();

	} else if (cmd.equals("settings")) {

		if (req.containsKey("baudrate")) {
			BAUDRATE = req["baudrate"];
			resp["baudrate"] = BAUDRATE;
			Config.set("uart.baudrate", BAUDRATE);
		}
		String config;
		Config.load(config);
		resp["config"] = config;

	} else {
		erc = EINVAL;
	}
	resp["delta"] = millis() - startTime;
	resp["error"] = erc;

	Serial.begin(BAUDRATE, SerialConfig::SERIAL_8N1, SerialMode::SERIAL_FULL);
	Serial.swap();
}