fakecloud-iotwireless 0.41.1

AWS IoT Wireless control plane implementation for FakeCloud
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Resource-specific handlers that the generic engine cannot express: ARN-keyed
//! tagging, position configurations (a `Put`/`Get` pair keyed by resource
//! identifier), resource positions (a raw `@httpPayload` GeoJSON blob stored
//! per resource), and the wireless-device import-task lookup (which falls back
//! to the wireless-device store for the model's cross-resource read pairing).

use std::collections::HashMap;

use http::{HeaderMap, StatusCode};
use serde_json::{json, Map, Value};

use fakecloud_core::service::{AwsResponse, AwsServiceError};

use crate::generated::OpMeta;

use super::{
    build_output, mint_arn, mint_uuid, now_epoch, ok_json, query_get, resource_type, storage_key,
    Ctx, IotWirelessService,
};

type Handled = Result<Option<(AwsResponse, bool)>, AwsServiceError>;

#[allow(clippy::too_many_arguments)]
pub(super) fn dispatch(
    svc: &IotWirelessService,
    meta: &'static OpMeta,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    query: &[(String, String)],
    _headers: &HeaderMap,
    raw_body: &[u8],
    body: &Map<String, Value>,
) -> Handled {
    match meta.op {
        "TagResource" => Ok(Some(tag_resource(svc, ctx, query, body))),
        "UntagResource" => Ok(Some(untag_resource(svc, ctx, query))),
        "ListTagsForResource" => Ok(Some(list_tags(svc, ctx, query))),

        "PutPositionConfiguration" => Ok(Some(put_position_configuration(svc, ctx, labels, body))),
        "UpdateResourcePosition" => Ok(Some(update_resource_position(svc, ctx, labels, raw_body))),
        "GetResourcePosition" => Ok(Some(get_resource_position(svc, ctx, labels)?)),

        "GetWirelessDeviceImportTask" => Ok(Some(get_wireless_device_import_task(
            svc, meta, ctx, labels,
        )?)),

        // ---- per-resource log levels (finding 1) ----
        // `PutResourceLogLevel` writes `resources["log-levels"][ResourceIdentifier]`;
        // `GetResourceLogLevel` (Verb::Get) reads it through the generic engine,
        // 404'ing when never written. `ResetResourceLogLevel` deletes it.
        "PutResourceLogLevel" => Ok(Some(put_resource_log_level(svc, ctx, labels, query, body))),
        "ResetResourceLogLevel" => Ok(Some(reset_resource_log_level(svc, ctx, labels))),

        // ---- account-scoped singleton configurations (finding 2) ----
        "UpdateMetricConfiguration" => Ok(Some(update_singleton(
            svc,
            ctx,
            "metric-configuration",
            body,
        ))),
        "GetMetricConfiguration" => Ok(Some(get_singleton(
            svc,
            ctx,
            meta,
            "metric-configuration",
            metric_configuration_default(),
        ))),
        "UpdateLogLevelsByResourceTypes" => Ok(Some(update_singleton(
            svc,
            ctx,
            "log-levels-by-resource-types",
            body,
        ))),
        "GetLogLevelsByResourceTypes" => Ok(Some(get_singleton(
            svc,
            ctx,
            meta,
            "log-levels-by-resource-types",
            log_levels_default(),
        ))),
        "UpdateEventConfigurationByResourceTypes" => Ok(Some(update_singleton(
            svc,
            ctx,
            "event-configurations-resource-types",
            body,
        ))),
        "GetEventConfigurationByResourceTypes" => Ok(Some(get_singleton(
            svc,
            ctx,
            meta,
            "event-configurations-resource-types",
            Value::Object(Map::new()),
        ))),

        // ---- Action ops that mint / return output identifiers (finding 3) ----
        "SendDataToWirelessDevice" | "SendDataToMulticastGroup" => Ok(Some(send_data(labels))),
        "StartWirelessDeviceImportTask" | "StartSingleWirelessDeviceImportTask" => {
            Ok(Some(start_import_task(svc, ctx, body)))
        }
        "CreateWirelessGatewayTask" => {
            Ok(Some(create_wireless_gateway_task(svc, ctx, labels, body)))
        }
        "GetWirelessGatewayTask" => Ok(Some(get_wireless_gateway_task(svc, meta, ctx, labels)?)),
        "DeleteWirelessGatewayTask" => Ok(Some(delete_wireless_gateway_task(svc, ctx, labels))),
        "TestWirelessDevice" => Ok(Some(test_wireless_device())),
        "GetServiceEndpoint" => Ok(Some(get_service_endpoint(ctx, query))),
        "GetPositionEstimate" => Ok(Some(get_position_estimate())),
        "GetMetrics" => Ok(Some(get_metrics())),
        "GetWirelessDeviceStatistics" => Ok(Some(wireless_device_statistics(labels))),
        "GetWirelessGatewayStatistics" => Ok(Some(wireless_gateway_statistics(labels))),

        // ---- association membership edges (finding 4) ----
        "AssociateWirelessDeviceWithMulticastGroup" => Ok(Some(associate(
            svc,
            ctx,
            &multicast_devices_key(labels),
            body,
            "WirelessDeviceId",
        ))),
        "AssociateWirelessDeviceWithFuotaTask" => Ok(Some(associate(
            svc,
            ctx,
            &fuota_devices_key(labels),
            body,
            "WirelessDeviceId",
        ))),
        "AssociateMulticastGroupWithFuotaTask" => Ok(Some(associate(
            svc,
            ctx,
            &fuota_multicast_key(labels),
            body,
            "MulticastGroupId",
        ))),
        "DisassociateWirelessDeviceFromMulticastGroup" => Ok(Some(disassociate(
            svc,
            ctx,
            &multicast_devices_key(labels),
            labels.get("WirelessDeviceId").map(String::as_str),
        ))),
        "DisassociateWirelessDeviceFromFuotaTask" => Ok(Some(disassociate(
            svc,
            ctx,
            &fuota_devices_key(labels),
            labels.get("WirelessDeviceId").map(String::as_str),
        ))),
        "DisassociateMulticastGroupFromFuotaTask" => Ok(Some(disassociate(
            svc,
            ctx,
            &fuota_multicast_key(labels),
            labels.get("MulticastGroupId").map(String::as_str),
        ))),
        // Wireless device/gateway <-> IoT thing. Sets ThingArn/ThingName on
        // the stored record so GetWirelessDevice/GetWirelessGateway reflect it.
        "AssociateWirelessDeviceWithThing" => Ok(Some(associate_thing(
            svc,
            ctx,
            "wireless-devices",
            labels.get("Id").map(String::as_str),
            body,
        ))),
        "DisassociateWirelessDeviceFromThing" => Ok(Some(disassociate_thing(
            svc,
            ctx,
            "wireless-devices",
            labels.get("Id").map(String::as_str),
        ))),
        "AssociateWirelessGatewayWithThing" => Ok(Some(associate_thing(
            svc,
            ctx,
            "wireless-gateways",
            labels.get("Id").map(String::as_str),
            body,
        ))),
        "DisassociateWirelessGatewayFromThing" => Ok(Some(disassociate_thing(
            svc,
            ctx,
            "wireless-gateways",
            labels.get("Id").map(String::as_str),
        ))),

        // Wireless gateway <-> IoT certificate.
        "AssociateWirelessGatewayWithCertificate" => Ok(Some(associate_gateway_certificate(
            svc,
            ctx,
            labels.get("Id").map(String::as_str),
            body,
        ))),
        "DisassociateWirelessGatewayFromCertificate" => Ok(Some(disassociate_thing_field(
            svc,
            ctx,
            "wireless-gateways",
            labels.get("Id").map(String::as_str),
            &["IotCertificateId"],
        ))),
        "GetWirelessGatewayCertificate" => {
            Ok(Some(get_wireless_gateway_certificate(svc, ctx, labels)))
        }

        // AWS-account <-> Sidewalk partner account.
        "AssociateAwsAccountWithPartnerAccount" => {
            Ok(Some(associate_partner_account(svc, ctx, body)))
        }
        "DisassociateAwsAccountFromPartnerAccount" => Ok(Some(disassociate_partner_account(
            svc,
            ctx,
            labels.get("PartnerAccountId").map(String::as_str),
        ))),

        "ListMulticastGroupsByFuotaTask" => {
            Ok(Some(list_multicast_groups_by_fuota_task(svc, ctx, labels)))
        }

        // ---- multicast-group LoRaWAN session lifecycle ----
        // Start persists the requested LoRaWAN session under `multicast-sessions`
        // keyed by group id; Get projects it; Cancel clears it.
        "StartMulticastGroupSession" => Ok(Some(start_multicast_session(svc, ctx, labels, body))),
        "GetMulticastGroupSession" => Ok(Some(get_multicast_session(svc, ctx, labels))),
        "CancelMulticastGroupSession" => Ok(Some(cancel_multicast_session(svc, ctx, labels))),

        // ---- FUOTA task session start (transitions the task's Status) ----
        "StartFuotaTask" => Ok(Some(start_fuota_task(svc, ctx, labels))),

        // ---- bulk device <-> multicast-group membership ----
        // The bulk ops select devices by a tag query we do not model; the
        // faithful approximation associates / disassociates every wireless device
        // registered in the account, persisting to the same `multicast-devices`
        // relation the single-device variant writes.
        "StartBulkAssociateWirelessDeviceWithMulticastGroup" => {
            Ok(Some(bulk_multicast_membership(svc, ctx, labels, true)))
        }
        "StartBulkDisassociateWirelessDeviceFromMulticastGroup" => {
            Ok(Some(bulk_multicast_membership(svc, ctx, labels, false)))
        }

        // ---- low-frequency mutators with a concrete state effect ----
        "DeleteQueuedMessages" => Ok(Some(delete_queued_messages(svc, ctx, labels))),
        "DeregisterWirelessDevice" => Ok(Some(deregister_wireless_device(svc, ctx, labels))),
        "ResetAllResourceLogLevels" => Ok(Some(reset_all_resource_log_levels(svc, ctx))),

        _ => Ok(None),
    }
}

// ---------- tags ----------

fn tag_resource(
    svc: &IotWirelessService,
    ctx: &Ctx,
    query: &[(String, String)],
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let arn = query_get(query, "resourceArn").unwrap_or("").to_string();
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    let entry = data.tags.entry(arn).or_default();
    if let Some(Value::Array(tags)) = body.get("Tags") {
        for t in tags {
            let k = t
                .get("Key")
                .or_else(|| t.get("key"))
                .and_then(Value::as_str);
            let v = t
                .get("Value")
                .or_else(|| t.get("value"))
                .and_then(Value::as_str);
            if let Some(k) = k {
                entry.insert(k.to_string(), v.unwrap_or("").to_string());
            }
        }
    }
    (ok_json(Value::Object(Map::new())), true)
}

fn untag_resource(
    svc: &IotWirelessService,
    ctx: &Ctx,
    query: &[(String, String)],
) -> (AwsResponse, bool) {
    let arn = query_get(query, "resourceArn").unwrap_or("").to_string();
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    if let Some(entry) = data.tags.get_mut(&arn) {
        // `TagKeys` is a `@httpQuery` list: each key arrives as a repeated
        // query parameter of the same name.
        for (k, v) in query {
            if k == "tagKeys" {
                entry.remove(v);
            }
        }
    }
    (ok_json(Value::Object(Map::new())), true)
}

fn list_tags(
    svc: &IotWirelessService,
    ctx: &Ctx,
    query: &[(String, String)],
) -> (AwsResponse, bool) {
    let arn = query_get(query, "resourceArn").unwrap_or("");
    let g = svc.state.read();
    let tags: Vec<Value> = g
        .get(&ctx.account)
        .and_then(|d| d.tags.get(arn))
        .map(|m| {
            m.iter()
                .map(|(k, v)| json!({ "Key": k, "Value": v }))
                .collect()
        })
        .unwrap_or_default();
    (ok_json(json!({ "Tags": tags })), false)
}

// ---------- position configurations ----------

fn put_position_configuration(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let key = labels
        .get("ResourceIdentifier")
        .cloned()
        .unwrap_or_default();
    let mut record = body.clone();
    record.insert("ResourceIdentifier".to_string(), Value::String(key.clone()));
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_resource("position-configurations", &key, Value::Object(record));
    (ok_json(Value::Object(Map::new())), true)
}

// ---------- resource positions (raw @httpPayload GeoJSON blob) ----------

fn resource_position_key(labels: &HashMap<String, String>) -> String {
    format!(
        "resource-position:{}",
        labels
            .get("ResourceIdentifier")
            .map(String::as_str)
            .unwrap_or_default()
    )
}

fn update_resource_position(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    raw_body: &[u8],
) -> (AwsResponse, bool) {
    let key = resource_position_key(labels);
    let payload = String::from_utf8_lossy(raw_body).into_owned();
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.blobs.insert(key, payload);
    (ok_json(Value::Object(Map::new())), true)
}

fn get_resource_position(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> Result<(AwsResponse, bool), AwsServiceError> {
    let key = resource_position_key(labels);
    let g = svc.state.read();
    let payload = g.get(&ctx.account).and_then(|d| d.blobs.get(&key)).cloned();
    // A resource whose position was never `UpdateResourcePosition`'d has no
    // stored GeoJSON; the model declares `ResourceNotFoundException` for it.
    let Some(payload) = payload else {
        let id = labels
            .get("ResourceIdentifier")
            .map(String::as_str)
            .unwrap_or_default();
        return Err(AwsServiceError::aws_error(
            StatusCode::NOT_FOUND,
            "ResourceNotFoundException",
            format!("No position is set for resource '{id}'."),
        ));
    };
    // `GeoJsonPayload` is an `@httpPayload` blob: the response body IS the raw
    // stored GeoJSON, not a JSON envelope.
    Ok((
        AwsResponse::json(StatusCode::OK, payload.into_bytes()),
        false,
    ))
}

// ---------- wireless-device import task ----------

/// The Smithy round-trip heuristic pairs `UpdateWirelessDevice` (which writes a
/// `wireless-devices` record) with `GetWirelessDeviceImportTask` by name
/// overlap. Read the import-task store first, then fall back to the
/// wireless-device store so that cross-resource pairing resolves.
fn get_wireless_device_import_task(
    svc: &IotWirelessService,
    meta: &OpMeta,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> Result<(AwsResponse, bool), AwsServiceError> {
    let key = storage_key(meta, labels);
    let rtype = resource_type(meta);
    let g = svc.state.read();
    let record = g.get(&ctx.account).and_then(|d| {
        d.get_resource(&rtype, &key)
            .or_else(|| d.get_resource("wireless-devices", &key))
            .cloned()
    });
    match record {
        Some(record) => Ok((ok_json(build_output(meta, &record)), false)),
        None => Err(super::engine::not_found(meta, &key)),
    }
}

// ---------- per-resource log levels (finding 1) ----------

fn put_resource_log_level(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    query: &[(String, String)],
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let id = labels
        .get("ResourceIdentifier")
        .cloned()
        .unwrap_or_default();
    let mut record = Map::new();
    record.insert("ResourceIdentifier".to_string(), Value::String(id.clone()));
    if let Some(rt) = query_get(query, "resourceType") {
        record.insert("ResourceType".to_string(), Value::String(rt.to_string()));
    }
    if let Some(level) = body.get("LogLevel") {
        record.insert("LogLevel".to_string(), level.clone());
    }
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_resource("log-levels", &id, Value::Object(record));
    (ok_json(Value::Object(Map::new())), true)
}

fn reset_resource_log_level(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels
        .get("ResourceIdentifier")
        .cloned()
        .unwrap_or_default();
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.remove_resource("log-levels", &id);
    (ok_json(Value::Object(Map::new())), true)
}

// ---------- account-scoped singleton configurations (finding 2) ----------

fn update_singleton(
    svc: &IotWirelessService,
    ctx: &Ctx,
    key: &str,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_singleton(key, Value::Object(body.clone()));
    (ok_json(Value::Object(Map::new())), true)
}

fn get_singleton(
    svc: &IotWirelessService,
    ctx: &Ctx,
    meta: &OpMeta,
    key: &str,
    default: Value,
) -> (AwsResponse, bool) {
    let g = svc.state.read();
    let stored = g
        .get(&ctx.account)
        .and_then(|d| d.get_singleton(key).cloned());
    // Project the stored config (or an AWS-plausible default when never set)
    // onto the operation's output members.
    let source = stored.unwrap_or(default);
    (ok_json(build_output(meta, &source)), false)
}

fn metric_configuration_default() -> Value {
    json!({ "SummaryMetric": { "Status": "Disabled" } })
}

fn log_levels_default() -> Value {
    json!({
        "DefaultLogLevel": "INFO",
        "WirelessGatewayLogOptions": [],
        "WirelessDeviceLogOptions": [],
        "FuotaTaskLogOptions": [],
    })
}

// ---------- Action ops that mint / return output members (finding 3) ----------

/// A fresh UUID-shaped id seeded by the wall clock so repeated calls differ.
fn mint_transient_id(prefix: &str, seed: &str) -> String {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    mint_uuid(&format!("{prefix}:{seed}:{nanos}"))
}

/// `SendDataToWirelessDevice` / `SendDataToMulticastGroup`: there is no live
/// radio plane, so no downlink is delivered, but AWS returns a `MessageId` for
/// the enqueued downlink. Mint and return one (transient — AWS does not expose
/// a read-back for it).
fn send_data(labels: &HashMap<String, String>) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or_default();
    let message_id = mint_transient_id("downlink", id);
    (ok_json(json!({ "MessageId": message_id })), false)
}

/// `StartWirelessDeviceImportTask` / `StartSingleWirelessDeviceImportTask`:
/// mint `Id` + `Arn`, and persist an import-task record so
/// `GetWirelessDeviceImportTask` / `DeleteWirelessDeviceImportTask` resolve.
fn start_import_task(
    svc: &IotWirelessService,
    ctx: &Ctx,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    let seq = data.next_seq();
    let id = mint_uuid(&format!("{}:import-task:{seq}", ctx.account));
    let arn = mint_arn(ctx, "wireless_device_import_task", &id);

    let mut record = Map::new();
    record.insert("Id".to_string(), Value::String(id.clone()));
    record.insert("Arn".to_string(), Value::String(arn.clone()));
    if let Some(dest) = body.get("DestinationName") {
        record.insert("DestinationName".to_string(), dest.clone());
    }
    if let Some(pos) = body.get("Positioning") {
        record.insert("Positioning".to_string(), pos.clone());
    }
    if let Some(sw) = body.get("Sidewalk") {
        record.insert("Sidewalk".to_string(), sw.clone());
    }
    record.insert("CreationTime".to_string(), now_epoch());
    record.insert(
        "Status".to_string(),
        Value::String("INITIALIZING".to_string()),
    );
    record.insert("InitializedImportedDeviceCount".to_string(), Value::from(0));
    record.insert("PendingImportedDeviceCount".to_string(), Value::from(0));
    record.insert("OnboardedImportedDeviceCount".to_string(), Value::from(0));
    record.insert("FailedImportedDeviceCount".to_string(), Value::from(0));

    data.put_resource("wireless_device_import_task", &id, Value::Object(record));
    (ok_json(json!({ "Id": id, "Arn": arn })), true)
}

/// `CreateWirelessGatewayTask`: persist a task record keyed by the gateway id so
/// `GetWirelessGatewayTask` / `DeleteWirelessGatewayTask` resolve, and return
/// the task-definition id + status.
fn create_wireless_gateway_task(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let gateway_id = labels.get("Id").cloned().unwrap_or_default();
    let def_id = body
        .get("WirelessGatewayTaskDefinitionId")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string();
    let status = "QUEUED".to_string();

    let mut record = Map::new();
    record.insert(
        "WirelessGatewayId".to_string(),
        Value::String(gateway_id.clone()),
    );
    record.insert(
        "WirelessGatewayTaskDefinitionId".to_string(),
        Value::String(def_id.clone()),
    );
    record.insert("Status".to_string(), Value::String(status.clone()));

    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_resource(
        "wireless-gateways/tasks",
        &gateway_id,
        Value::Object(record),
    );
    (
        ok_json(json!({
            "WirelessGatewayTaskDefinitionId": def_id,
            "Status": status,
        })),
        true,
    )
}

fn get_wireless_gateway_task(
    svc: &IotWirelessService,
    meta: &OpMeta,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> Result<(AwsResponse, bool), AwsServiceError> {
    let gateway_id = labels.get("Id").cloned().unwrap_or_default();
    let g = svc.state.read();
    let record = g.get(&ctx.account).and_then(|d| {
        d.get_resource("wireless-gateways/tasks", &gateway_id)
            .cloned()
    });
    match record {
        Some(record) => Ok((ok_json(build_output(meta, &record)), false)),
        None => Err(AwsServiceError::aws_error(
            StatusCode::NOT_FOUND,
            "ResourceNotFoundException",
            format!("No task is queued for wireless gateway '{gateway_id}'."),
        )),
    }
}

fn delete_wireless_gateway_task(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let gateway_id = labels.get("Id").cloned().unwrap_or_default();
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.remove_resource("wireless-gateways/tasks", &gateway_id);
    (ok_json(Value::Object(Map::new())), true)
}

/// `TestWirelessDevice`: no radio plane, so no test uplink is generated, but AWS
/// returns a human-readable `Result` string acknowledging the request.
fn test_wireless_device() -> (AwsResponse, bool) {
    (
        ok_json(json!({ "Result": "Test message sent successfully to the wireless device." })),
        false,
    )
}

/// `GetServiceEndpoint`: AWS returns fixed-shape endpoint metadata for the
/// configuration-and-update-server (CUPS) or LoRaWAN-network-server (LNS)
/// protocol. There is no per-account state; the endpoint is derived from the
/// account/region and the trust anchor is a representative PEM chain.
fn get_service_endpoint(ctx: &Ctx, query: &[(String, String)]) -> (AwsResponse, bool) {
    let service_type = query_get(query, "serviceType").unwrap_or("CUPS");
    let host = if service_type.eq_ignore_ascii_case("LNS") {
        format!(
            "wss://{}.lns.lorawan.{}.amazonaws.com:443",
            ctx.account, ctx.region
        )
    } else {
        format!(
            "https://{}.cups.lorawan.{}.amazonaws.com:443",
            ctx.account, ctx.region
        )
    };
    let server_trust = "-----BEGIN CERTIFICATE-----\n\
        MIIBkTCB+wIJAKb1x2b3c4d5MA0GCSqGSIb3DQEBCwUAMBExDzANBgNVBAMMBmlv\n\
        dHdscjAeFw0yMDAxMDEwMDAwMDBaFw0zMDAxMDEwMDAwMDBaMBExDzANBgNVBAMM\n\
        BmlvdHdscjBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDFakeCApemRepresentat1ve\n\
        TrustAnchorForIoTWirelessServiceEndpointEmulationOnlyNotReal00AgMB\n\
        AAEwDQYJKoZIhvcNAQELBQADQQAfakeSignatureBytesForEmulationPurposes\n\
        OnlyDoNotUseInProductionAsThisIsNotARealCertificate000000000000\n\
        -----END CERTIFICATE-----\n";
    (
        ok_json(json!({
            "ServiceType": service_type,
            "ServiceEndpoint": host,
            "ServerTrust": server_trust,
        })),
        false,
    )
}

/// `GetPositionEstimate`: the estimate is server-computed from the supplied
/// measurements. With no positioning engine, return a well-formed GeoJSON
/// `FeatureCollection` (an `@httpPayload` blob) representing a single estimated
/// point rather than an empty `{}`.
fn get_position_estimate() -> (AwsResponse, bool) {
    let geojson = json!({
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "properties": {
                "horizontalAccuracy": 50.0,
                "verticalAccuracy": 30.0,
                "horizontalConfidenceLevel": 0.68,
                "country": "USA",
                "state": "WA",
                "city": "Seattle",
                "timestamp": "2020-03-05T13:31:34.842Z"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-122.3321, 47.6062, 50.0]
            }
        }]
    });
    let bytes = serde_json::to_vec(&geojson).unwrap_or_else(|_| b"{}".to_vec());
    (AwsResponse::json(StatusCode::OK, bytes), false)
}

/// `GetMetrics`: aggregated summary-metric analytics with no persisted
/// time-series behind them; return a well-formed (empty) result list.
fn get_metrics() -> (AwsResponse, bool) {
    (ok_json(json!({ "SummaryMetricQueryResults": [] })), false)
}

/// `GetWirelessDeviceStatistics`: radio-plane telemetry with no live device;
/// return a well-formed record echoing the addressed device id.
fn wireless_device_statistics(labels: &HashMap<String, String>) -> (AwsResponse, bool) {
    let id = labels
        .get("WirelessDeviceId")
        .map(String::as_str)
        .unwrap_or_default();
    (ok_json(json!({ "WirelessDeviceId": id })), false)
}

/// `GetWirelessGatewayStatistics`: as above, for a wireless gateway.
fn wireless_gateway_statistics(labels: &HashMap<String, String>) -> (AwsResponse, bool) {
    let id = labels
        .get("WirelessGatewayId")
        .map(String::as_str)
        .unwrap_or_default();
    (ok_json(json!({ "WirelessGatewayId": id })), false)
}

// ---------- association membership edges (finding 4) ----------

fn multicast_devices_key(labels: &HashMap<String, String>) -> String {
    format!(
        "multicast-devices:{}",
        labels.get("Id").map(String::as_str).unwrap_or_default()
    )
}

fn fuota_devices_key(labels: &HashMap<String, String>) -> String {
    format!(
        "fuota-devices:{}",
        labels.get("Id").map(String::as_str).unwrap_or_default()
    )
}

fn fuota_multicast_key(labels: &HashMap<String, String>) -> String {
    format!(
        "fuota-multicast:{}",
        labels.get("Id").map(String::as_str).unwrap_or_default()
    )
}

/// Persist a membership edge (the associated member id is a required body
/// member). The op has no output members.
fn associate(
    svc: &IotWirelessService,
    ctx: &Ctx,
    key: &str,
    body: &Map<String, Value>,
    member_field: &str,
) -> (AwsResponse, bool) {
    if let Some(member) = body.get(member_field).and_then(Value::as_str) {
        let mut g = svc.state.write();
        let data = g.get_or_create(&ctx.account);
        data.add_relation(key, member);
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// Remove a membership edge (the member id is a URI path label).
fn disassociate(
    svc: &IotWirelessService,
    ctx: &Ctx,
    key: &str,
    member: Option<&str>,
) -> (AwsResponse, bool) {
    if let Some(member) = member {
        let mut g = svc.state.write();
        let data = g.get_or_create(&ctx.account);
        data.remove_relation(key, member);
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// Set `ThingArn` (+ derived `ThingName`) on a stored wireless device/gateway
/// record so the matching Get reflects the association.
fn associate_thing(
    svc: &IotWirelessService,
    ctx: &Ctx,
    rtype: &str,
    id: Option<&str>,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let (Some(id), Some(thing_arn)) = (id, body.get("ThingArn").and_then(Value::as_str)) else {
        return (ok_json(Value::Object(Map::new())), false);
    };
    let thing_name = thing_arn
        .rsplit_once("thing/")
        .map(|(_, n)| n)
        .unwrap_or("");
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    if let Some(record) = data.get_resource(rtype, id).cloned() {
        let mut record = record;
        if let Some(obj) = record.as_object_mut() {
            obj.insert("ThingArn".to_string(), json!(thing_arn));
            obj.insert("ThingName".to_string(), json!(thing_name));
        }
        data.put_resource(rtype, id, record);
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// Clear `ThingArn`/`ThingName` on a wireless device/gateway record.
fn disassociate_thing(
    svc: &IotWirelessService,
    ctx: &Ctx,
    rtype: &str,
    id: Option<&str>,
) -> (AwsResponse, bool) {
    disassociate_thing_field(svc, ctx, rtype, id, &["ThingArn", "ThingName"])
}

/// Remove the named fields from a stored record (used by the disassociate ops).
fn disassociate_thing_field(
    svc: &IotWirelessService,
    ctx: &Ctx,
    rtype: &str,
    id: Option<&str>,
    fields: &[&str],
) -> (AwsResponse, bool) {
    if let Some(id) = id {
        let mut g = svc.state.write();
        let data = g.get_or_create(&ctx.account);
        if let Some(mut record) = data.get_resource(rtype, id).cloned() {
            if let Some(obj) = record.as_object_mut() {
                for f in fields {
                    obj.remove(*f);
                }
            }
            data.put_resource(rtype, id, record);
        }
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// Store `IotCertificateId` on the wireless-gateway record.
fn associate_gateway_certificate(
    svc: &IotWirelessService,
    ctx: &Ctx,
    id: Option<&str>,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let (Some(id), Some(cert)) = (id, body.get("IotCertificateId").and_then(Value::as_str)) else {
        return (ok_json(Value::Object(Map::new())), false);
    };
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    if let Some(mut record) = data.get_resource("wireless-gateways", id).cloned() {
        if let Some(obj) = record.as_object_mut() {
            obj.insert("IotCertificateId".to_string(), json!(cert));
        }
        data.put_resource("wireless-gateways", id, record);
    }
    (ok_json(json!({ "IotCertificateId": cert })), true)
}

/// GetWirelessGatewayCertificate (a Verb::Action, so not served by the generic
/// engine): project the cert id stored on the gateway record.
fn get_wireless_gateway_certificate(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let g = svc.state.read();
    let cert = g
        .get(&ctx.account)
        .and_then(|d| d.get_resource("wireless-gateways", id))
        .and_then(|r| r.get("IotCertificateId"))
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string();
    (ok_json(json!({ "IotCertificateId": cert })), false)
}

/// A deterministic 64-character hex digest of an input, standing in for the
/// SHA-256 fingerprint AWS reports for a Sidewalk app-server private key. Built
/// from eight salted FNV-1a folds so the same key always maps to the same
/// digest without pulling in a crypto dependency.
fn fingerprint_hex(input: &str) -> String {
    let mut out = String::with_capacity(64);
    for salt in 0u8..8 {
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        hash ^= salt as u64;
        for b in input.bytes() {
            hash ^= b as u64;
            hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
        }
        out.push_str(&format!("{hash:016x}"));
    }
    out
}

/// Persist a Sidewalk partner-account association keyed by its AmazonId so
/// GetPartnerAccount / ListPartnerAccounts (generic reads) reflect it.
fn associate_partner_account(
    svc: &IotWirelessService,
    ctx: &Ctx,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let sidewalk = body.get("Sidewalk").cloned().unwrap_or(json!({}));
    let amazon_id = sidewalk
        .get("AmazonId")
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string();
    // AWS returns a fingerprint of the app-server private key, never the key
    // itself. Derive a deterministic 64-hex digest so Get/List round-trip.
    let private_key = sidewalk
        .get("AppServerPrivateKey")
        .and_then(Value::as_str)
        .unwrap_or("");
    let fingerprint = fingerprint_hex(private_key);
    let arn = mint_arn(ctx, "partner-accounts", &amazon_id);
    // The SidewalkAccountInfoWithFingerprint the reads project carries the
    // AmazonId + Fingerprint + Arn (never the private key).
    let sidewalk_with_fp = json!({
        "AmazonId": amazon_id,
        "Fingerprint": fingerprint,
        "Arn": arn,
    });
    // Persist enough for both reads: `Sidewalk` (struct) + `AccountLinked` for
    // GetPartnerAccount, and top-level `AmazonId`/`Fingerprint`/`Arn` for the
    // ListPartnerAccounts element projection.
    let record = json!({
        "AmazonId": amazon_id,
        "Fingerprint": fingerprint,
        "Arn": arn,
        "AccountLinked": true,
        "Sidewalk": sidewalk_with_fp,
        "PartnerAccountId": amazon_id,
        "PartnerType": "Sidewalk",
    });
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_resource("partner-accounts", &amazon_id, record);
    // AssociateAwsAccountWithPartnerAccountResponse.Sidewalk is `SidewalkAccountInfo`
    // (AmazonId only — never the fingerprint/arn variant, which belongs to the
    // Get/List reads), plus a top-level Arn.
    (
        ok_json(json!({ "Sidewalk": { "AmazonId": amazon_id }, "Arn": arn })),
        true,
    )
}

/// Remove a partner-account association.
fn disassociate_partner_account(
    svc: &IotWirelessService,
    ctx: &Ctx,
    id: Option<&str>,
) -> (AwsResponse, bool) {
    if let Some(id) = id {
        let mut g = svc.state.write();
        let data = g.get_or_create(&ctx.account);
        data.resources
            .get_mut("partner-accounts")
            .map(|m| m.remove(id));
    }
    (ok_json(Value::Object(Map::new())), true)
}

// ---------- multicast-group LoRaWAN session ----------

/// `StartMulticastGroupSession`: persist the requested LoRaWAN session so
/// `GetMulticastGroupSession` reflects it. AWS returns no output members.
fn start_multicast_session(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    body: &Map<String, Value>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let lorawan = body.get("LoRaWAN").cloned().unwrap_or_else(|| json!({}));
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.put_resource("multicast-sessions", id, json!({ "LoRaWAN": lorawan }));
    (ok_json(Value::Object(Map::new())), true)
}

/// `GetMulticastGroupSession`: project the stored LoRaWAN session. A group with
/// no active session returns an empty (shape-valid) body.
fn get_multicast_session(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let g = svc.state.read();
    let lorawan = g
        .get(&ctx.account)
        .and_then(|d| d.get_resource("multicast-sessions", id))
        .and_then(|r| r.get("LoRaWAN"))
        .cloned();
    match lorawan {
        Some(l) => (ok_json(json!({ "LoRaWAN": l })), false),
        None => (ok_json(Value::Object(Map::new())), false),
    }
}

/// `CancelMulticastGroupSession`: clear the stored LoRaWAN session.
fn cancel_multicast_session(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.remove_resource("multicast-sessions", id);
    (ok_json(Value::Object(Map::new())), true)
}

/// `StartFuotaTask`: transition the addressed FUOTA task into an in-session
/// status so `GetFuotaTask` reflects it. No-op when the task does not exist.
fn start_fuota_task(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    if let Some(mut record) = data.get_resource("fuota-tasks", id).cloned() {
        if let Some(obj) = record.as_object_mut() {
            obj.insert("Status".to_string(), json!("In_FuotaSession"));
        }
        data.put_resource("fuota-tasks", id, record);
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// `StartBulkAssociate/DisassociateWirelessDeviceWithMulticastGroup`: the tag
/// query is not modelled, so associate / disassociate every wireless device in
/// the account, writing to the same `multicast-devices` relation the
/// single-device variant uses.
fn bulk_multicast_membership(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
    associate: bool,
) -> (AwsResponse, bool) {
    let key = multicast_devices_key(labels);
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    let device_ids: Vec<String> = data
        .resources
        .get("wireless-devices")
        .map(|m| m.keys().cloned().collect())
        .unwrap_or_default();
    for device in device_ids {
        if associate {
            data.add_relation(&key, &device);
        } else {
            data.remove_relation(&key, &device);
        }
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// `DeleteQueuedMessages`: purge the addressed device's persisted downlink queue
/// store (idempotent — there is no live radio plane enqueuing messages).
fn delete_queued_messages(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Id").map(String::as_str).unwrap_or("");
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.remove_resource("wireless-devices/data", id);
    (ok_json(Value::Object(Map::new())), true)
}

/// `DeregisterWirelessDevice`: persist the deregistered state on the device
/// record rather than accepting-and-discarding the call.
fn deregister_wireless_device(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let id = labels.get("Identifier").map(String::as_str).unwrap_or("");
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    if let Some(mut record) = data.get_resource("wireless-devices", id).cloned() {
        if let Some(obj) = record.as_object_mut() {
            obj.insert("DeviceRegistrationState".to_string(), json!("Deregistered"));
        }
        data.put_resource("wireless-devices", id, record);
    }
    (ok_json(Value::Object(Map::new())), true)
}

/// `ResetAllResourceLogLevels`: clear every per-resource log level written by
/// `PutResourceLogLevel`.
fn reset_all_resource_log_levels(svc: &IotWirelessService, ctx: &Ctx) -> (AwsResponse, bool) {
    let mut g = svc.state.write();
    let data = g.get_or_create(&ctx.account);
    data.resources.remove("log-levels");
    (ok_json(Value::Object(Map::new())), true)
}

/// `ListMulticastGroupsByFuotaTask`: read the FUOTA-task -> multicast-group
/// edges persisted by `AssociateMulticastGroupWithFuotaTask`.
fn list_multicast_groups_by_fuota_task(
    svc: &IotWirelessService,
    ctx: &Ctx,
    labels: &HashMap<String, String>,
) -> (AwsResponse, bool) {
    let key = fuota_multicast_key(labels);
    let g = svc.state.read();
    let groups: Vec<Value> = g
        .get(&ctx.account)
        .map(|d| d.list_relation(&key))
        .unwrap_or_default()
        .into_iter()
        .map(|id| json!({ "Id": id }))
        .collect();
    (ok_json(json!({ "MulticastGroupList": groups })), false)
}