Skip to content

Commit

Permalink
Improve max_power and attributes validation
Browse files Browse the repository at this point in the history
Both must be specified in the JSON file, as they get written together
  • Loading branch information
will-v-pi committed Nov 15, 2024
1 parent b8946b7 commit 1cfba88
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions json/schemas/whitelabel-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@
"description": "Max power consumption, in 2mA units",
"type": ["integer", "string"],
"maximum": 255,
"pattern": "^0x[0-9a-fA-F]{0,2}$"
"pattern": "^0x[0-9a-fA-F]{1,2}$"
},
"attributes": {
"description": "Device attributes: bit 7 must be 1, bit 6 is self-powered, bit 5 is remote wakeup, bits 0-4 must be 0",
"type": ["integer", "string"],
"maximum": 255,
"pattern": "^0x[0-9a-fA-F]{0,2}$"
"minimum": 128,
"maximum": 224,
"pattern": "^0x[0-9a-fA-F]{2}$"
}
},
"dependentRequired": {
"max_power": ["attributes"],
"attributes": ["max_power"]
},
"additionalProperties": false
},
"scsi": {
Expand Down
7 changes: 4 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7278,19 +7278,20 @@ bool otp_white_label_command::execute(device_map &devices) {
// Check for separate max_power and attributes
uint16_t val = 0;
int hex_val = 0;
if (wl_json["device"].contains("max_power")) {
if (wl_json["device"].contains("max_power") && wl_json["device"].contains("attributes")) {
if (!get_json_int(wl_json["device"]["max_power"], hex_val)) {
fail(ERROR_FORMAT, "MaxPower must be an integer");
}
val |= (hex_val << 8);
}
if (wl_json["device"].contains("attributes")) {

if (!get_json_int(wl_json["device"]["attributes"], hex_val)) {
fail(ERROR_FORMAT, "Device Attributes must be an integer");
} else if (hex_val & 0b11111 || ~hex_val & 0x80) {
fail(ERROR_FORMAT, "Device Attributes must have bit 7 set (0x80), and bits 4-0 clear");
}
val |= hex_val;
} else if (wl_json["device"].contains("max_power") || wl_json["device"].contains("attributes")) {
fail(ERROR_INCOMPATIBLE, "Must specify both max_power and attributes in the JSON file");
}
if (val) {
fos << "Setting attributes " << hex_string(val, 4) << "\n";
Expand Down

0 comments on commit 1cfba88

Please sign in to comment.