BlePeripheral.g.cpp
1 // Autogenerated from Pigeon (v22.7.0), do not edit directly. 2 // See also: https://pub.dev/packages/pigeon 3 4 #undef _HAS_EXCEPTIONS 5 6 #include "BlePeripheral.g.h" 7 8 #include <flutter/basic_message_channel.h> 9 #include <flutter/binary_messenger.h> 10 #include <flutter/encodable_value.h> 11 #include <flutter/standard_message_codec.h> 12 13 #include <map> 14 #include <optional> 15 #include <string> 16 17 namespace ble_peripheral { 18 using flutter::BasicMessageChannel; 19 using flutter::CustomEncodableValue; 20 using flutter::EncodableList; 21 using flutter::EncodableMap; 22 using flutter::EncodableValue; 23 24 FlutterError CreateConnectionError(const std::string channel_name) { 25 return FlutterError( 26 "channel-error", 27 "Unable to establish connection on channel: '" + channel_name + "'.", 28 EncodableValue("")); 29 } 30 31 // BleService 32 33 BleService::BleService( 34 const std::string& uuid, 35 bool primary, 36 const EncodableList& characteristics) 37 : uuid_(uuid), 38 primary_(primary), 39 characteristics_(characteristics) {} 40 41 const std::string& BleService::uuid() const { 42 return uuid_; 43 } 44 45 void BleService::set_uuid(std::string_view value_arg) { 46 uuid_ = value_arg; 47 } 48 49 50 bool BleService::primary() const { 51 return primary_; 52 } 53 54 void BleService::set_primary(bool value_arg) { 55 primary_ = value_arg; 56 } 57 58 59 const EncodableList& BleService::characteristics() const { 60 return characteristics_; 61 } 62 63 void BleService::set_characteristics(const EncodableList& value_arg) { 64 characteristics_ = value_arg; 65 } 66 67 68 EncodableList BleService::ToEncodableList() const { 69 EncodableList list; 70 list.reserve(3); 71 list.push_back(EncodableValue(uuid_)); 72 list.push_back(EncodableValue(primary_)); 73 list.push_back(EncodableValue(characteristics_)); 74 return list; 75 } 76 77 BleService BleService::FromEncodableList(const EncodableList& list) { 78 BleService decoded( 79 std::get<std::string>(list[0]), 80 std::get<bool>(list[1]), 81 std::get<EncodableList>(list[2])); 82 return decoded; 83 } 84 85 // BleCharacteristic 86 87 BleCharacteristic::BleCharacteristic( 88 const std::string& uuid, 89 const EncodableList& properties, 90 const EncodableList& permissions) 91 : uuid_(uuid), 92 properties_(properties), 93 permissions_(permissions) {} 94 95 BleCharacteristic::BleCharacteristic( 96 const std::string& uuid, 97 const EncodableList& properties, 98 const EncodableList& permissions, 99 const EncodableList* descriptors, 100 const std::vector<uint8_t>* value) 101 : uuid_(uuid), 102 properties_(properties), 103 permissions_(permissions), 104 descriptors_(descriptors ? std::optional<EncodableList>(*descriptors) : std::nullopt), 105 value_(value ? std::optional<std::vector<uint8_t>>(*value) : std::nullopt) {} 106 107 const std::string& BleCharacteristic::uuid() const { 108 return uuid_; 109 } 110 111 void BleCharacteristic::set_uuid(std::string_view value_arg) { 112 uuid_ = value_arg; 113 } 114 115 116 const EncodableList& BleCharacteristic::properties() const { 117 return properties_; 118 } 119 120 void BleCharacteristic::set_properties(const EncodableList& value_arg) { 121 properties_ = value_arg; 122 } 123 124 125 const EncodableList& BleCharacteristic::permissions() const { 126 return permissions_; 127 } 128 129 void BleCharacteristic::set_permissions(const EncodableList& value_arg) { 130 permissions_ = value_arg; 131 } 132 133 134 const EncodableList* BleCharacteristic::descriptors() const { 135 return descriptors_ ? &(*descriptors_) : nullptr; 136 } 137 138 void BleCharacteristic::set_descriptors(const EncodableList* value_arg) { 139 descriptors_ = value_arg ? std::optional<EncodableList>(*value_arg) : std::nullopt; 140 } 141 142 void BleCharacteristic::set_descriptors(const EncodableList& value_arg) { 143 descriptors_ = value_arg; 144 } 145 146 147 const std::vector<uint8_t>* BleCharacteristic::value() const { 148 return value_ ? &(*value_) : nullptr; 149 } 150 151 void BleCharacteristic::set_value(const std::vector<uint8_t>* value_arg) { 152 value_ = value_arg ? std::optional<std::vector<uint8_t>>(*value_arg) : std::nullopt; 153 } 154 155 void BleCharacteristic::set_value(const std::vector<uint8_t>& value_arg) { 156 value_ = value_arg; 157 } 158 159 160 EncodableList BleCharacteristic::ToEncodableList() const { 161 EncodableList list; 162 list.reserve(5); 163 list.push_back(EncodableValue(uuid_)); 164 list.push_back(EncodableValue(properties_)); 165 list.push_back(EncodableValue(permissions_)); 166 list.push_back(descriptors_ ? EncodableValue(*descriptors_) : EncodableValue()); 167 list.push_back(value_ ? EncodableValue(*value_) : EncodableValue()); 168 return list; 169 } 170 171 BleCharacteristic BleCharacteristic::FromEncodableList(const EncodableList& list) { 172 BleCharacteristic decoded( 173 std::get<std::string>(list[0]), 174 std::get<EncodableList>(list[1]), 175 std::get<EncodableList>(list[2])); 176 auto& encodable_descriptors = list[3]; 177 if (!encodable_descriptors.IsNull()) { 178 decoded.set_descriptors(std::get<EncodableList>(encodable_descriptors)); 179 } 180 auto& encodable_value = list[4]; 181 if (!encodable_value.IsNull()) { 182 decoded.set_value(std::get<std::vector<uint8_t>>(encodable_value)); 183 } 184 return decoded; 185 } 186 187 // BleDescriptor 188 189 BleDescriptor::BleDescriptor(const std::string& uuid) 190 : uuid_(uuid) {} 191 192 BleDescriptor::BleDescriptor( 193 const std::string& uuid, 194 const std::vector<uint8_t>* value, 195 const EncodableList* permissions) 196 : uuid_(uuid), 197 value_(value ? std::optional<std::vector<uint8_t>>(*value) : std::nullopt), 198 permissions_(permissions ? std::optional<EncodableList>(*permissions) : std::nullopt) {} 199 200 const std::string& BleDescriptor::uuid() const { 201 return uuid_; 202 } 203 204 void BleDescriptor::set_uuid(std::string_view value_arg) { 205 uuid_ = value_arg; 206 } 207 208 209 const std::vector<uint8_t>* BleDescriptor::value() const { 210 return value_ ? &(*value_) : nullptr; 211 } 212 213 void BleDescriptor::set_value(const std::vector<uint8_t>* value_arg) { 214 value_ = value_arg ? std::optional<std::vector<uint8_t>>(*value_arg) : std::nullopt; 215 } 216 217 void BleDescriptor::set_value(const std::vector<uint8_t>& value_arg) { 218 value_ = value_arg; 219 } 220 221 222 const EncodableList* BleDescriptor::permissions() const { 223 return permissions_ ? &(*permissions_) : nullptr; 224 } 225 226 void BleDescriptor::set_permissions(const EncodableList* value_arg) { 227 permissions_ = value_arg ? std::optional<EncodableList>(*value_arg) : std::nullopt; 228 } 229 230 void BleDescriptor::set_permissions(const EncodableList& value_arg) { 231 permissions_ = value_arg; 232 } 233 234 235 EncodableList BleDescriptor::ToEncodableList() const { 236 EncodableList list; 237 list.reserve(3); 238 list.push_back(EncodableValue(uuid_)); 239 list.push_back(value_ ? EncodableValue(*value_) : EncodableValue()); 240 list.push_back(permissions_ ? EncodableValue(*permissions_) : EncodableValue()); 241 return list; 242 } 243 244 BleDescriptor BleDescriptor::FromEncodableList(const EncodableList& list) { 245 BleDescriptor decoded( 246 std::get<std::string>(list[0])); 247 auto& encodable_value = list[1]; 248 if (!encodable_value.IsNull()) { 249 decoded.set_value(std::get<std::vector<uint8_t>>(encodable_value)); 250 } 251 auto& encodable_permissions = list[2]; 252 if (!encodable_permissions.IsNull()) { 253 decoded.set_permissions(std::get<EncodableList>(encodable_permissions)); 254 } 255 return decoded; 256 } 257 258 // ReadRequestResult 259 260 ReadRequestResult::ReadRequestResult(const std::vector<uint8_t>& value) 261 : value_(value) {} 262 263 ReadRequestResult::ReadRequestResult( 264 const std::vector<uint8_t>& value, 265 const int64_t* offset, 266 const int64_t* status) 267 : value_(value), 268 offset_(offset ? std::optional<int64_t>(*offset) : std::nullopt), 269 status_(status ? std::optional<int64_t>(*status) : std::nullopt) {} 270 271 const std::vector<uint8_t>& ReadRequestResult::value() const { 272 return value_; 273 } 274 275 void ReadRequestResult::set_value(const std::vector<uint8_t>& value_arg) { 276 value_ = value_arg; 277 } 278 279 280 const int64_t* ReadRequestResult::offset() const { 281 return offset_ ? &(*offset_) : nullptr; 282 } 283 284 void ReadRequestResult::set_offset(const int64_t* value_arg) { 285 offset_ = value_arg ? std::optional<int64_t>(*value_arg) : std::nullopt; 286 } 287 288 void ReadRequestResult::set_offset(int64_t value_arg) { 289 offset_ = value_arg; 290 } 291 292 293 const int64_t* ReadRequestResult::status() const { 294 return status_ ? &(*status_) : nullptr; 295 } 296 297 void ReadRequestResult::set_status(const int64_t* value_arg) { 298 status_ = value_arg ? std::optional<int64_t>(*value_arg) : std::nullopt; 299 } 300 301 void ReadRequestResult::set_status(int64_t value_arg) { 302 status_ = value_arg; 303 } 304 305 306 EncodableList ReadRequestResult::ToEncodableList() const { 307 EncodableList list; 308 list.reserve(3); 309 list.push_back(EncodableValue(value_)); 310 list.push_back(offset_ ? EncodableValue(*offset_) : EncodableValue()); 311 list.push_back(status_ ? EncodableValue(*status_) : EncodableValue()); 312 return list; 313 } 314 315 ReadRequestResult ReadRequestResult::FromEncodableList(const EncodableList& list) { 316 ReadRequestResult decoded( 317 std::get<std::vector<uint8_t>>(list[0])); 318 auto& encodable_offset = list[1]; 319 if (!encodable_offset.IsNull()) { 320 decoded.set_offset(std::get<int64_t>(encodable_offset)); 321 } 322 auto& encodable_status = list[2]; 323 if (!encodable_status.IsNull()) { 324 decoded.set_status(std::get<int64_t>(encodable_status)); 325 } 326 return decoded; 327 } 328 329 // WriteRequestResult 330 331 WriteRequestResult::WriteRequestResult() {} 332 333 WriteRequestResult::WriteRequestResult( 334 const std::vector<uint8_t>* value, 335 const int64_t* offset, 336 const int64_t* status) 337 : value_(value ? std::optional<std::vector<uint8_t>>(*value) : std::nullopt), 338 offset_(offset ? std::optional<int64_t>(*offset) : std::nullopt), 339 status_(status ? std::optional<int64_t>(*status) : std::nullopt) {} 340 341 const std::vector<uint8_t>* WriteRequestResult::value() const { 342 return value_ ? &(*value_) : nullptr; 343 } 344 345 void WriteRequestResult::set_value(const std::vector<uint8_t>* value_arg) { 346 value_ = value_arg ? std::optional<std::vector<uint8_t>>(*value_arg) : std::nullopt; 347 } 348 349 void WriteRequestResult::set_value(const std::vector<uint8_t>& value_arg) { 350 value_ = value_arg; 351 } 352 353 354 const int64_t* WriteRequestResult::offset() const { 355 return offset_ ? &(*offset_) : nullptr; 356 } 357 358 void WriteRequestResult::set_offset(const int64_t* value_arg) { 359 offset_ = value_arg ? std::optional<int64_t>(*value_arg) : std::nullopt; 360 } 361 362 void WriteRequestResult::set_offset(int64_t value_arg) { 363 offset_ = value_arg; 364 } 365 366 367 const int64_t* WriteRequestResult::status() const { 368 return status_ ? &(*status_) : nullptr; 369 } 370 371 void WriteRequestResult::set_status(const int64_t* value_arg) { 372 status_ = value_arg ? std::optional<int64_t>(*value_arg) : std::nullopt; 373 } 374 375 void WriteRequestResult::set_status(int64_t value_arg) { 376 status_ = value_arg; 377 } 378 379 380 EncodableList WriteRequestResult::ToEncodableList() const { 381 EncodableList list; 382 list.reserve(3); 383 list.push_back(value_ ? EncodableValue(*value_) : EncodableValue()); 384 list.push_back(offset_ ? EncodableValue(*offset_) : EncodableValue()); 385 list.push_back(status_ ? EncodableValue(*status_) : EncodableValue()); 386 return list; 387 } 388 389 WriteRequestResult WriteRequestResult::FromEncodableList(const EncodableList& list) { 390 WriteRequestResult decoded; 391 auto& encodable_value = list[0]; 392 if (!encodable_value.IsNull()) { 393 decoded.set_value(std::get<std::vector<uint8_t>>(encodable_value)); 394 } 395 auto& encodable_offset = list[1]; 396 if (!encodable_offset.IsNull()) { 397 decoded.set_offset(std::get<int64_t>(encodable_offset)); 398 } 399 auto& encodable_status = list[2]; 400 if (!encodable_status.IsNull()) { 401 decoded.set_status(std::get<int64_t>(encodable_status)); 402 } 403 return decoded; 404 } 405 406 // ManufacturerData 407 408 ManufacturerData::ManufacturerData( 409 int64_t manufacturer_id, 410 const std::vector<uint8_t>& data) 411 : manufacturer_id_(manufacturer_id), 412 data_(data) {} 413 414 int64_t ManufacturerData::manufacturer_id() const { 415 return manufacturer_id_; 416 } 417 418 void ManufacturerData::set_manufacturer_id(int64_t value_arg) { 419 manufacturer_id_ = value_arg; 420 } 421 422 423 const std::vector<uint8_t>& ManufacturerData::data() const { 424 return data_; 425 } 426 427 void ManufacturerData::set_data(const std::vector<uint8_t>& value_arg) { 428 data_ = value_arg; 429 } 430 431 432 EncodableList ManufacturerData::ToEncodableList() const { 433 EncodableList list; 434 list.reserve(2); 435 list.push_back(EncodableValue(manufacturer_id_)); 436 list.push_back(EncodableValue(data_)); 437 return list; 438 } 439 440 ManufacturerData ManufacturerData::FromEncodableList(const EncodableList& list) { 441 ManufacturerData decoded( 442 std::get<int64_t>(list[0]), 443 std::get<std::vector<uint8_t>>(list[1])); 444 return decoded; 445 } 446 447 448 PigeonInternalCodecSerializer::PigeonInternalCodecSerializer() {} 449 450 EncodableValue PigeonInternalCodecSerializer::ReadValueOfType( 451 uint8_t type, 452 flutter::ByteStreamReader* stream) const { 453 switch (type) { 454 case 129: { 455 const auto& encodable_enum_arg = ReadValue(stream); 456 const int64_t enum_arg_value = encodable_enum_arg.IsNull() ? 0 : encodable_enum_arg.LongValue(); 457 return encodable_enum_arg.IsNull() ? EncodableValue() : CustomEncodableValue(static_cast<BondState>(enum_arg_value)); 458 } 459 case 130: { 460 return CustomEncodableValue(BleService::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 461 } 462 case 131: { 463 return CustomEncodableValue(BleCharacteristic::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 464 } 465 case 132: { 466 return CustomEncodableValue(BleDescriptor::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 467 } 468 case 133: { 469 return CustomEncodableValue(ReadRequestResult::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 470 } 471 case 134: { 472 return CustomEncodableValue(WriteRequestResult::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 473 } 474 case 135: { 475 return CustomEncodableValue(ManufacturerData::FromEncodableList(std::get<EncodableList>(ReadValue(stream)))); 476 } 477 default: 478 return flutter::StandardCodecSerializer::ReadValueOfType(type, stream); 479 } 480 } 481 482 void PigeonInternalCodecSerializer::WriteValue( 483 const EncodableValue& value, 484 flutter::ByteStreamWriter* stream) const { 485 if (const CustomEncodableValue* custom_value = std::get_if<CustomEncodableValue>(&value)) { 486 if (custom_value->type() == typeid(BondState)) { 487 stream->WriteByte(129); 488 WriteValue(EncodableValue(static_cast<int>(std::any_cast<BondState>(*custom_value))), stream); 489 return; 490 } 491 if (custom_value->type() == typeid(BleService)) { 492 stream->WriteByte(130); 493 WriteValue(EncodableValue(std::any_cast<BleService>(*custom_value).ToEncodableList()), stream); 494 return; 495 } 496 if (custom_value->type() == typeid(BleCharacteristic)) { 497 stream->WriteByte(131); 498 WriteValue(EncodableValue(std::any_cast<BleCharacteristic>(*custom_value).ToEncodableList()), stream); 499 return; 500 } 501 if (custom_value->type() == typeid(BleDescriptor)) { 502 stream->WriteByte(132); 503 WriteValue(EncodableValue(std::any_cast<BleDescriptor>(*custom_value).ToEncodableList()), stream); 504 return; 505 } 506 if (custom_value->type() == typeid(ReadRequestResult)) { 507 stream->WriteByte(133); 508 WriteValue(EncodableValue(std::any_cast<ReadRequestResult>(*custom_value).ToEncodableList()), stream); 509 return; 510 } 511 if (custom_value->type() == typeid(WriteRequestResult)) { 512 stream->WriteByte(134); 513 WriteValue(EncodableValue(std::any_cast<WriteRequestResult>(*custom_value).ToEncodableList()), stream); 514 return; 515 } 516 if (custom_value->type() == typeid(ManufacturerData)) { 517 stream->WriteByte(135); 518 WriteValue(EncodableValue(std::any_cast<ManufacturerData>(*custom_value).ToEncodableList()), stream); 519 return; 520 } 521 } 522 flutter::StandardCodecSerializer::WriteValue(value, stream); 523 } 524 525 /// The codec used by BlePeripheralChannel. 526 const flutter::StandardMessageCodec& BlePeripheralChannel::GetCodec() { 527 return flutter::StandardMessageCodec::GetInstance(&PigeonInternalCodecSerializer::GetInstance()); 528 } 529 530 // Sets up an instance of `BlePeripheralChannel` to handle messages through the `binary_messenger`. 531 void BlePeripheralChannel::SetUp( 532 flutter::BinaryMessenger* binary_messenger, 533 BlePeripheralChannel* api) { 534 BlePeripheralChannel::SetUp(binary_messenger, api, ""); 535 } 536 537 void BlePeripheralChannel::SetUp( 538 flutter::BinaryMessenger* binary_messenger, 539 BlePeripheralChannel* api, 540 const std::string& message_channel_suffix) { 541 const std::string prepended_suffix = message_channel_suffix.length() > 0 ? std::string(".") + message_channel_suffix : ""; 542 { 543 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.initialize" + prepended_suffix, &GetCodec()); 544 if (api != nullptr) { 545 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 546 try { 547 std::optional<FlutterError> output = api->Initialize(); 548 if (output.has_value()) { 549 reply(WrapError(output.value())); 550 return; 551 } 552 EncodableList wrapped; 553 wrapped.push_back(EncodableValue()); 554 reply(EncodableValue(std::move(wrapped))); 555 } catch (const std::exception& exception) { 556 reply(WrapError(exception.what())); 557 } 558 }); 559 } else { 560 channel.SetMessageHandler(nullptr); 561 } 562 } 563 { 564 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.isAdvertising" + prepended_suffix, &GetCodec()); 565 if (api != nullptr) { 566 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 567 try { 568 ErrorOr<std::optional<bool>> output = api->IsAdvertising(); 569 if (output.has_error()) { 570 reply(WrapError(output.error())); 571 return; 572 } 573 EncodableList wrapped; 574 auto output_optional = std::move(output).TakeValue(); 575 if (output_optional) { 576 wrapped.push_back(EncodableValue(std::move(output_optional).value())); 577 } else { 578 wrapped.push_back(EncodableValue()); 579 } 580 reply(EncodableValue(std::move(wrapped))); 581 } catch (const std::exception& exception) { 582 reply(WrapError(exception.what())); 583 } 584 }); 585 } else { 586 channel.SetMessageHandler(nullptr); 587 } 588 } 589 { 590 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.isSupported" + prepended_suffix, &GetCodec()); 591 if (api != nullptr) { 592 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 593 try { 594 ErrorOr<bool> output = api->IsSupported(); 595 if (output.has_error()) { 596 reply(WrapError(output.error())); 597 return; 598 } 599 EncodableList wrapped; 600 wrapped.push_back(EncodableValue(std::move(output).TakeValue())); 601 reply(EncodableValue(std::move(wrapped))); 602 } catch (const std::exception& exception) { 603 reply(WrapError(exception.what())); 604 } 605 }); 606 } else { 607 channel.SetMessageHandler(nullptr); 608 } 609 } 610 { 611 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.stopAdvertising" + prepended_suffix, &GetCodec()); 612 if (api != nullptr) { 613 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 614 try { 615 std::optional<FlutterError> output = api->StopAdvertising(); 616 if (output.has_value()) { 617 reply(WrapError(output.value())); 618 return; 619 } 620 EncodableList wrapped; 621 wrapped.push_back(EncodableValue()); 622 reply(EncodableValue(std::move(wrapped))); 623 } catch (const std::exception& exception) { 624 reply(WrapError(exception.what())); 625 } 626 }); 627 } else { 628 channel.SetMessageHandler(nullptr); 629 } 630 } 631 { 632 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.askBlePermission" + prepended_suffix, &GetCodec()); 633 if (api != nullptr) { 634 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 635 try { 636 ErrorOr<bool> output = api->AskBlePermission(); 637 if (output.has_error()) { 638 reply(WrapError(output.error())); 639 return; 640 } 641 EncodableList wrapped; 642 wrapped.push_back(EncodableValue(std::move(output).TakeValue())); 643 reply(EncodableValue(std::move(wrapped))); 644 } catch (const std::exception& exception) { 645 reply(WrapError(exception.what())); 646 } 647 }); 648 } else { 649 channel.SetMessageHandler(nullptr); 650 } 651 } 652 { 653 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.addService" + prepended_suffix, &GetCodec()); 654 if (api != nullptr) { 655 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 656 try { 657 const auto& args = std::get<EncodableList>(message); 658 const auto& encodable_service_arg = args.at(0); 659 if (encodable_service_arg.IsNull()) { 660 reply(WrapError("service_arg unexpectedly null.")); 661 return; 662 } 663 const auto& service_arg = std::any_cast<const BleService&>(std::get<CustomEncodableValue>(encodable_service_arg)); 664 std::optional<FlutterError> output = api->AddService(service_arg); 665 if (output.has_value()) { 666 reply(WrapError(output.value())); 667 return; 668 } 669 EncodableList wrapped; 670 wrapped.push_back(EncodableValue()); 671 reply(EncodableValue(std::move(wrapped))); 672 } catch (const std::exception& exception) { 673 reply(WrapError(exception.what())); 674 } 675 }); 676 } else { 677 channel.SetMessageHandler(nullptr); 678 } 679 } 680 { 681 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.removeService" + prepended_suffix, &GetCodec()); 682 if (api != nullptr) { 683 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 684 try { 685 const auto& args = std::get<EncodableList>(message); 686 const auto& encodable_service_id_arg = args.at(0); 687 if (encodable_service_id_arg.IsNull()) { 688 reply(WrapError("service_id_arg unexpectedly null.")); 689 return; 690 } 691 const auto& service_id_arg = std::get<std::string>(encodable_service_id_arg); 692 std::optional<FlutterError> output = api->RemoveService(service_id_arg); 693 if (output.has_value()) { 694 reply(WrapError(output.value())); 695 return; 696 } 697 EncodableList wrapped; 698 wrapped.push_back(EncodableValue()); 699 reply(EncodableValue(std::move(wrapped))); 700 } catch (const std::exception& exception) { 701 reply(WrapError(exception.what())); 702 } 703 }); 704 } else { 705 channel.SetMessageHandler(nullptr); 706 } 707 } 708 { 709 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.clearServices" + prepended_suffix, &GetCodec()); 710 if (api != nullptr) { 711 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 712 try { 713 std::optional<FlutterError> output = api->ClearServices(); 714 if (output.has_value()) { 715 reply(WrapError(output.value())); 716 return; 717 } 718 EncodableList wrapped; 719 wrapped.push_back(EncodableValue()); 720 reply(EncodableValue(std::move(wrapped))); 721 } catch (const std::exception& exception) { 722 reply(WrapError(exception.what())); 723 } 724 }); 725 } else { 726 channel.SetMessageHandler(nullptr); 727 } 728 } 729 { 730 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.getServices" + prepended_suffix, &GetCodec()); 731 if (api != nullptr) { 732 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 733 try { 734 ErrorOr<EncodableList> output = api->GetServices(); 735 if (output.has_error()) { 736 reply(WrapError(output.error())); 737 return; 738 } 739 EncodableList wrapped; 740 wrapped.push_back(EncodableValue(std::move(output).TakeValue())); 741 reply(EncodableValue(std::move(wrapped))); 742 } catch (const std::exception& exception) { 743 reply(WrapError(exception.what())); 744 } 745 }); 746 } else { 747 channel.SetMessageHandler(nullptr); 748 } 749 } 750 { 751 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.startAdvertising" + prepended_suffix, &GetCodec()); 752 if (api != nullptr) { 753 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 754 try { 755 const auto& args = std::get<EncodableList>(message); 756 const auto& encodable_services_arg = args.at(0); 757 if (encodable_services_arg.IsNull()) { 758 reply(WrapError("services_arg unexpectedly null.")); 759 return; 760 } 761 const auto& services_arg = std::get<EncodableList>(encodable_services_arg); 762 const auto& encodable_local_name_arg = args.at(1); 763 const auto* local_name_arg = std::get_if<std::string>(&encodable_local_name_arg); 764 const auto& encodable_timeout_arg = args.at(2); 765 const auto* timeout_arg = std::get_if<int64_t>(&encodable_timeout_arg); 766 const auto& encodable_manufacturer_data_arg = args.at(3); 767 const auto* manufacturer_data_arg = encodable_manufacturer_data_arg.IsNull() ? nullptr : &(std::any_cast<const ManufacturerData&>(std::get<CustomEncodableValue>(encodable_manufacturer_data_arg))); 768 const auto& encodable_add_manufacturer_data_in_scan_response_arg = args.at(4); 769 if (encodable_add_manufacturer_data_in_scan_response_arg.IsNull()) { 770 reply(WrapError("add_manufacturer_data_in_scan_response_arg unexpectedly null.")); 771 return; 772 } 773 const auto& add_manufacturer_data_in_scan_response_arg = std::get<bool>(encodable_add_manufacturer_data_in_scan_response_arg); 774 std::optional<FlutterError> output = api->StartAdvertising(services_arg, local_name_arg, timeout_arg, manufacturer_data_arg, add_manufacturer_data_in_scan_response_arg); 775 if (output.has_value()) { 776 reply(WrapError(output.value())); 777 return; 778 } 779 EncodableList wrapped; 780 wrapped.push_back(EncodableValue()); 781 reply(EncodableValue(std::move(wrapped))); 782 } catch (const std::exception& exception) { 783 reply(WrapError(exception.what())); 784 } 785 }); 786 } else { 787 channel.SetMessageHandler(nullptr); 788 } 789 } 790 { 791 BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.ble_peripheral.BlePeripheralChannel.updateCharacteristic" + prepended_suffix, &GetCodec()); 792 if (api != nullptr) { 793 channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) { 794 try { 795 const auto& args = std::get<EncodableList>(message); 796 const auto& encodable_characteristic_id_arg = args.at(0); 797 if (encodable_characteristic_id_arg.IsNull()) { 798 reply(WrapError("characteristic_id_arg unexpectedly null.")); 799 return; 800 } 801 const auto& characteristic_id_arg = std::get<std::string>(encodable_characteristic_id_arg); 802 const auto& encodable_value_arg = args.at(1); 803 if (encodable_value_arg.IsNull()) { 804 reply(WrapError("value_arg unexpectedly null.")); 805 return; 806 } 807 const auto& value_arg = std::get<std::vector<uint8_t>>(encodable_value_arg); 808 const auto& encodable_device_id_arg = args.at(2); 809 const auto* device_id_arg = std::get_if<std::string>(&encodable_device_id_arg); 810 std::optional<FlutterError> output = api->UpdateCharacteristic(characteristic_id_arg, value_arg, device_id_arg); 811 if (output.has_value()) { 812 reply(WrapError(output.value())); 813 return; 814 } 815 EncodableList wrapped; 816 wrapped.push_back(EncodableValue()); 817 reply(EncodableValue(std::move(wrapped))); 818 } catch (const std::exception& exception) { 819 reply(WrapError(exception.what())); 820 } 821 }); 822 } else { 823 channel.SetMessageHandler(nullptr); 824 } 825 } 826 } 827 828 EncodableValue BlePeripheralChannel::WrapError(std::string_view error_message) { 829 return EncodableValue(EncodableList{ 830 EncodableValue(std::string(error_message)), 831 EncodableValue("Error"), 832 EncodableValue() 833 }); 834 } 835 836 EncodableValue BlePeripheralChannel::WrapError(const FlutterError& error) { 837 return EncodableValue(EncodableList{ 838 EncodableValue(error.code()), 839 EncodableValue(error.message()), 840 error.details() 841 }); 842 } 843 844 // Generated class from Pigeon that represents Flutter messages that can be called from C++. 845 BleCallback::BleCallback(flutter::BinaryMessenger* binary_messenger) 846 : binary_messenger_(binary_messenger), 847 message_channel_suffix_("") {} 848 849 BleCallback::BleCallback( 850 flutter::BinaryMessenger* binary_messenger, 851 const std::string& message_channel_suffix) 852 : binary_messenger_(binary_messenger), 853 message_channel_suffix_(message_channel_suffix.length() > 0 ? std::string(".") + message_channel_suffix : "") {} 854 855 const flutter::StandardMessageCodec& BleCallback::GetCodec() { 856 return flutter::StandardMessageCodec::GetInstance(&PigeonInternalCodecSerializer::GetInstance()); 857 } 858 859 void BleCallback::OnReadRequest( 860 const std::string& device_id_arg, 861 const std::string& characteristic_id_arg, 862 int64_t offset_arg, 863 const std::vector<uint8_t>* value_arg, 864 std::function<void(const ReadRequestResult*)>&& on_success, 865 std::function<void(const FlutterError&)>&& on_error) { 866 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onReadRequest" + message_channel_suffix_; 867 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 868 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 869 EncodableValue(device_id_arg), 870 EncodableValue(characteristic_id_arg), 871 EncodableValue(offset_arg), 872 value_arg ? EncodableValue(*value_arg) : EncodableValue(), 873 }); 874 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 875 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 876 const auto& encodable_return_value = *response; 877 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 878 if (list_return_value) { 879 if (list_return_value->size() > 1) { 880 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 881 } else { 882 const auto* return_value = list_return_value->at(0).IsNull() ? nullptr : &(std::any_cast<const ReadRequestResult&>(std::get<CustomEncodableValue>(list_return_value->at(0)))); 883 on_success(return_value); 884 } 885 } else { 886 on_error(CreateConnectionError(channel_name)); 887 } 888 }); 889 } 890 891 void BleCallback::OnWriteRequest( 892 const std::string& device_id_arg, 893 const std::string& characteristic_id_arg, 894 int64_t offset_arg, 895 const std::vector<uint8_t>* value_arg, 896 std::function<void(const WriteRequestResult*)>&& on_success, 897 std::function<void(const FlutterError&)>&& on_error) { 898 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onWriteRequest" + message_channel_suffix_; 899 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 900 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 901 EncodableValue(device_id_arg), 902 EncodableValue(characteristic_id_arg), 903 EncodableValue(offset_arg), 904 value_arg ? EncodableValue(*value_arg) : EncodableValue(), 905 }); 906 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 907 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 908 const auto& encodable_return_value = *response; 909 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 910 if (list_return_value) { 911 if (list_return_value->size() > 1) { 912 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 913 } else { 914 const auto* return_value = list_return_value->at(0).IsNull() ? nullptr : &(std::any_cast<const WriteRequestResult&>(std::get<CustomEncodableValue>(list_return_value->at(0)))); 915 on_success(return_value); 916 } 917 } else { 918 on_error(CreateConnectionError(channel_name)); 919 } 920 }); 921 } 922 923 void BleCallback::OnCharacteristicSubscriptionChange( 924 const std::string& device_id_arg, 925 const std::string& characteristic_id_arg, 926 bool is_subscribed_arg, 927 const std::string* name_arg, 928 std::function<void(void)>&& on_success, 929 std::function<void(const FlutterError&)>&& on_error) { 930 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onCharacteristicSubscriptionChange" + message_channel_suffix_; 931 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 932 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 933 EncodableValue(device_id_arg), 934 EncodableValue(characteristic_id_arg), 935 EncodableValue(is_subscribed_arg), 936 name_arg ? EncodableValue(*name_arg) : EncodableValue(), 937 }); 938 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 939 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 940 const auto& encodable_return_value = *response; 941 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 942 if (list_return_value) { 943 if (list_return_value->size() > 1) { 944 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 945 } else { 946 on_success(); 947 } 948 } else { 949 on_error(CreateConnectionError(channel_name)); 950 } 951 }); 952 } 953 954 void BleCallback::OnAdvertisingStatusUpdate( 955 bool advertising_arg, 956 const std::string* error_arg, 957 std::function<void(void)>&& on_success, 958 std::function<void(const FlutterError&)>&& on_error) { 959 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onAdvertisingStatusUpdate" + message_channel_suffix_; 960 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 961 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 962 EncodableValue(advertising_arg), 963 error_arg ? EncodableValue(*error_arg) : EncodableValue(), 964 }); 965 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 966 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 967 const auto& encodable_return_value = *response; 968 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 969 if (list_return_value) { 970 if (list_return_value->size() > 1) { 971 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 972 } else { 973 on_success(); 974 } 975 } else { 976 on_error(CreateConnectionError(channel_name)); 977 } 978 }); 979 } 980 981 void BleCallback::OnBleStateChange( 982 bool state_arg, 983 std::function<void(void)>&& on_success, 984 std::function<void(const FlutterError&)>&& on_error) { 985 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onBleStateChange" + message_channel_suffix_; 986 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 987 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 988 EncodableValue(state_arg), 989 }); 990 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 991 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 992 const auto& encodable_return_value = *response; 993 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 994 if (list_return_value) { 995 if (list_return_value->size() > 1) { 996 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 997 } else { 998 on_success(); 999 } 1000 } else { 1001 on_error(CreateConnectionError(channel_name)); 1002 } 1003 }); 1004 } 1005 1006 void BleCallback::OnServiceAdded( 1007 const std::string& service_id_arg, 1008 const std::string* error_arg, 1009 std::function<void(void)>&& on_success, 1010 std::function<void(const FlutterError&)>&& on_error) { 1011 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onServiceAdded" + message_channel_suffix_; 1012 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 1013 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 1014 EncodableValue(service_id_arg), 1015 error_arg ? EncodableValue(*error_arg) : EncodableValue(), 1016 }); 1017 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 1018 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 1019 const auto& encodable_return_value = *response; 1020 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 1021 if (list_return_value) { 1022 if (list_return_value->size() > 1) { 1023 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 1024 } else { 1025 on_success(); 1026 } 1027 } else { 1028 on_error(CreateConnectionError(channel_name)); 1029 } 1030 }); 1031 } 1032 1033 void BleCallback::OnMtuChange( 1034 const std::string& device_id_arg, 1035 int64_t mtu_arg, 1036 std::function<void(void)>&& on_success, 1037 std::function<void(const FlutterError&)>&& on_error) { 1038 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onMtuChange" + message_channel_suffix_; 1039 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 1040 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 1041 EncodableValue(device_id_arg), 1042 EncodableValue(mtu_arg), 1043 }); 1044 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 1045 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 1046 const auto& encodable_return_value = *response; 1047 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 1048 if (list_return_value) { 1049 if (list_return_value->size() > 1) { 1050 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 1051 } else { 1052 on_success(); 1053 } 1054 } else { 1055 on_error(CreateConnectionError(channel_name)); 1056 } 1057 }); 1058 } 1059 1060 void BleCallback::OnConnectionStateChange( 1061 const std::string& device_id_arg, 1062 bool connected_arg, 1063 std::function<void(void)>&& on_success, 1064 std::function<void(const FlutterError&)>&& on_error) { 1065 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onConnectionStateChange" + message_channel_suffix_; 1066 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 1067 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 1068 EncodableValue(device_id_arg), 1069 EncodableValue(connected_arg), 1070 }); 1071 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 1072 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 1073 const auto& encodable_return_value = *response; 1074 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 1075 if (list_return_value) { 1076 if (list_return_value->size() > 1) { 1077 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 1078 } else { 1079 on_success(); 1080 } 1081 } else { 1082 on_error(CreateConnectionError(channel_name)); 1083 } 1084 }); 1085 } 1086 1087 void BleCallback::OnBondStateChange( 1088 const std::string& device_id_arg, 1089 const BondState& bond_state_arg, 1090 std::function<void(void)>&& on_success, 1091 std::function<void(const FlutterError&)>&& on_error) { 1092 const std::string channel_name = "dev.flutter.pigeon.ble_peripheral.BleCallback.onBondStateChange" + message_channel_suffix_; 1093 BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec()); 1094 EncodableValue encoded_api_arguments = EncodableValue(EncodableList{ 1095 EncodableValue(device_id_arg), 1096 CustomEncodableValue(bond_state_arg), 1097 }); 1098 channel.Send(encoded_api_arguments, [channel_name, on_success = std::move(on_success), on_error = std::move(on_error)](const uint8_t* reply, size_t reply_size) { 1099 std::unique_ptr<EncodableValue> response = GetCodec().DecodeMessage(reply, reply_size); 1100 const auto& encodable_return_value = *response; 1101 const auto* list_return_value = std::get_if<EncodableList>(&encodable_return_value); 1102 if (list_return_value) { 1103 if (list_return_value->size() > 1) { 1104 on_error(FlutterError(std::get<std::string>(list_return_value->at(0)), std::get<std::string>(list_return_value->at(1)), list_return_value->at(2))); 1105 } else { 1106 on_success(); 1107 } 1108 } else { 1109 on_error(CreateConnectionError(channel_name)); 1110 } 1111 }); 1112 } 1113 1114 } // namespace ble_peripheral