#include "provisioning/ble_provisioning_codec.h" #include #include #include namespace borla { namespace { bool PrintJson( cJSON* root, char* output, size_t capacity, size_t* output_size) { if (root == nullptr || output == nullptr || capacity < 2 || output_size == nullptr) { cJSON_Delete(root); return false; } const bool printed = cJSON_PrintPreallocated( root, output, static_cast(capacity), false) != 0; cJSON_Delete(root); if (!printed) { output[0] = '\0'; *output_size = 0; return false; } *output_size = std::strlen(output); return true; } bool AddString(cJSON* object, const char* name, const char* value) { return cJSON_AddStringToObject(object, name, value) != nullptr; } } // namespace BleChunkAssemblyResult BleChunkAssembler::Push( const uint8_t* packet, size_t size) { if (packet == nullptr || size < 3) { Reset(); return BleChunkAssemblyResult::kInvalid; } const uint8_t sequence = packet[0]; const uint8_t flag = packet[1]; if (flag > 1 || (!receiving_ && sequence != 0) || (receiving_ && sequence != next_sequence_)) { Reset(); return BleChunkAssemblyResult::kInvalid; } if (sequence == 0) { Reset(); receiving_ = true; } const size_t payload_size = size - 2; if (payload_size == 0 || size_ + payload_size >= buffer_.size()) { Reset(); return BleChunkAssemblyResult::kInvalid; } std::memcpy(buffer_.data() + size_, packet + 2, payload_size); size_ += payload_size; buffer_[size_] = '\0'; next_sequence_ = static_cast(sequence + 1U); if (flag == 0) { return BleChunkAssemblyResult::kAccepted; } receiving_ = false; return BleChunkAssemblyResult::kComplete; } void BleChunkAssembler::Reset() { buffer_.fill('\0'); size_ = 0; next_sequence_ = 0; receiving_ = false; } bool VisitBleNotificationChunks( const char* json, size_t size, uint16_t negotiated_mtu, IBleChunkVisitor& visitor) { if (json == nullptr || size == 0) { return false; } const size_t att_payload = negotiated_mtu > 3 ? static_cast( std::min(negotiated_mtu, 517) - 3) : 20; const size_t chunk_payload = std::max( 1, att_payload > 2 ? att_payload - 2 : 18); if ((size + chunk_payload - 1) / chunk_payload > 256) { return false; } std::array packet{}; size_t offset = 0; uint8_t sequence = 0; while (offset < size) { const size_t bytes = std::min(chunk_payload, size - offset); const bool last = offset + bytes == size; packet[0] = sequence; packet[1] = last ? 1 : 0; std::memcpy(packet.data() + 2, json + offset, bytes); if (!visitor.OnBleChunk(packet.data(), bytes + 2)) { return false; } offset += bytes; sequence++; } return true; } bool ParseProvisioningCredential( const char* json, size_t size, WifiCredential* credential) { if (json == nullptr || credential == nullptr || size == 0 || size >= kBleCredentialJsonCapacity || std::memchr(json, '\0', size) != nullptr) { return false; } std::array copy{}; std::memcpy(copy.data(), json, size); cJSON* root = cJSON_Parse(copy.data()); if (!cJSON_IsObject(root)) { cJSON_Delete(root); return false; } const cJSON* ssid = cJSON_GetObjectItemCaseSensitive(root, "ssid"); const cJSON* password = cJSON_GetObjectItemCaseSensitive(root, "password"); const bool valid = cJSON_IsString(ssid) && ssid->valuestring != nullptr && (password == nullptr || cJSON_IsString(password)); const bool set = valid && credential->Set( ssid->valuestring, password == nullptr ? "" : password->valuestring); cJSON_Delete(root); return set; } bool BuildProvisioningDeviceInfoJson( const DeviceIdentity& identity, char* output, size_t capacity, size_t* output_size) { cJSON* root = cJSON_CreateObject(); if (root == nullptr || !AddString(root, "mac", identity.mac) || !AddString(root, "version", identity.app_version) || !AddString(root, "board", identity.board)) { cJSON_Delete(root); return false; } return PrintJson(root, output, capacity, output_size); } bool BuildProvisioningScanJson( const WifiNetwork* networks, size_t count, char* output, size_t capacity, size_t* output_size) { if ((networks == nullptr && count != 0) || count > kMaxProvisioningScanResults) { return false; } cJSON* root = cJSON_CreateObject(); cJSON* array = root == nullptr ? nullptr : cJSON_AddArrayToObject(root, "networks"); if (array == nullptr) { cJSON_Delete(root); return false; } for (size_t index = 0; index < count; ++index) { cJSON* item = cJSON_CreateObject(); if (item == nullptr || !AddString(item, "ssid", networks[index].ssid.data()) || cJSON_AddNumberToObject( item, "rssi", networks[index].rssi_dbm) == nullptr || cJSON_AddNumberToObject( item, "auth", networks[index].auth_mode) == nullptr) { cJSON_Delete(item); cJSON_Delete(root); return false; } cJSON_AddItemToArray(array, item); } return PrintJson(root, output, capacity, output_size); } bool BuildProvisioningSavedWifiJson( const WifiCredential* credentials, size_t count, char* output, size_t capacity, size_t* output_size) { if ((credentials == nullptr && count != 0) || count > kMaxSavedWifiCredentials) { return false; } cJSON* root = cJSON_CreateObject(); cJSON* array = root == nullptr ? nullptr : cJSON_AddArrayToObject(root, "saved"); if (array == nullptr) { cJSON_Delete(root); return false; } for (size_t index = 0; index < count; ++index) { cJSON* item = cJSON_CreateObject(); if (item == nullptr || cJSON_AddNumberToObject(item, "index", index) == nullptr || !AddString(item, "ssid", credentials[index].ssid.data())) { cJSON_Delete(item); cJSON_Delete(root); return false; } cJSON_AddItemToArray(array, item); } return PrintJson(root, output, capacity, output_size); } bool BuildProvisioningResultJson( bool success, const char* error, char* output, size_t capacity, size_t* output_size) { cJSON* root = cJSON_CreateObject(); if (root == nullptr || cJSON_AddBoolToObject(root, "success", success) == nullptr || (!success && error != nullptr && error[0] != '\0' && !AddString(root, "error", error))) { cJSON_Delete(root); return false; } return PrintJson(root, output, capacity, output_size); } } // namespace borla