-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathProjMgrRpcServer.cpp
More file actions
967 lines (872 loc) · 34.5 KB
/
Copy pathProjMgrRpcServer.cpp
File metadata and controls
967 lines (872 loc) · 34.5 KB
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
/*
* Copyright (c) 2025-2026 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ProjMgrRpcServer.h"
#include "ProjMgrRpcServerData.h"
#include "ProjMgrLogger.h"
#include "ProjMgr.h"
#include "ProductInfo.h"
#include "CrossPlatformUtils.h"
#include "RteFsUtils.h"
#include "CollectionUtils.h"
#include <fstream>
#include <iostream>
#include <regex>
using namespace std;
static constexpr const char* CONTENT_LENGTH_HEADER = "Content-Length:";
ProjMgrRpcServer::ProjMgrRpcServer(ProjMgr& manager) :
m_manager(manager) {
}
ProjMgrRpcServer::~ProjMgrRpcServer(void) {
// Reserved
}
const string ProjMgrRpcServer::GetRequestFromStdinWithLength(void) {
string line;
int contentLength = 0;
const string& header = CONTENT_LENGTH_HEADER;
while(getline(cin, line) && !cin.fail()) {
if(line.find(header) == 0) {
contentLength = RteUtils::StringToInt(line.substr(header.length()), 0);
}
if(line.empty() || line.front() == '\r' || line.front() == '\n') {
break;
}
}
string request(contentLength, '\0');
cin.read(&request[0], contentLength);
return request;
}
const string ProjMgrRpcServer::GetRequestFromStdin(void) {
string jsonData;
int braces = 0;
bool inJson = false;
char c;
while(cin.get(c) && !cin.fail()) {
if(c == '{') {
braces++;
inJson = true;
}
if(c == '}') {
braces--;
}
if(inJson) {
jsonData += c;
}
if(inJson && braces == 0) {
break;
}
}
return jsonData;
}
class RpcHandler : public RpcMethods {
public:
RpcHandler(ProjMgrRpcServer& server, JsonRpc2Server& jsonServer) : RpcMethods(jsonServer),
m_server(server),
m_manager(server.GetManager()),
m_worker(server.GetManager().GetWorker()) {}
RpcArgs::GetVersionResult GetVersion(void) override;
RpcArgs::SuccessResult Shutdown(void) override;
RpcArgs::SuccessResult Apply(const string& context) override;
RpcArgs::SuccessResult Resolve(const string& context, const RpcArgs::Options& options) override;
RpcArgs::SuccessResult LoadPacks(void) override;
RpcArgs::SuccessResult LoadSolution(const string& solution, const string& activeTarget) override;
RpcArgs::ContextInfo GetContextInfo(const string& context) override;
RpcArgs::UsedItems GetUsedItems(const string& context) override;
RpcArgs::PacksInfo GetPacksInfo(const string& context, const bool& all) override;
RpcArgs::SuccessResult SelectPack(const string& context, const RpcArgs::PackReference& pack) override;
RpcArgs::VariablesResult GetVariables(const string& context) override;
RpcArgs::DeviceList GetDeviceList(const string& context, const string& namePattern, const string& vendor) override;
RpcArgs::DeviceInfo GetDeviceInfo(const string& id) override;
RpcArgs::BoardList GetBoardList(const string& context, const string& namePattern, const string& vendor) override;
RpcArgs::BoardInfo GetBoardInfo(const string& id) override;
RpcArgs::CtRoot GetComponentsTree(const string& context, const bool& all) override;
RpcArgs::SuccessResult SelectComponent(const string& context, const string& id, const int& count, const RpcArgs::Options& options) override;
RpcArgs::SuccessResult SelectVariant(const string& context, const string& aggregateId, const string& variantName) override;
RpcArgs::SuccessResult SelectBundle(const string& context, const string& className, const string& bundleName) override;
RpcArgs::Results ValidateComponents(const string& context) override;
RpcArgs::LogMessages GetLogMessages(void) override;
RpcArgs::DraftProjectsInfo GetDraftProjects(const RpcArgs::DraftProjectsFilter& filter) override;
RpcArgs::ConvertSolutionResult ConvertSolution(const string& solution, const string& activeTarget, const bool& updateRte) override;
RpcArgs::DiscoverLayersInfo DiscoverLayers(const string& solution, const string& activeTarget) override;
RpcArgs::ListMissingPacksResult ListMissingPacks(const string& solution, const string& activeTarget) override;
protected:
enum Exception
{
SOLUTION_NOT_FOUND = -1,
SOLUTION_NOT_VALID = -2,
SOLUTION_NOT_LOADED = -3,
CONTEXT_NOT_FOUND = -4,
CONTEXT_NOT_VALID = -5,
COMPONENT_NOT_FOUND = -6,
COMPONENT_NOT_RESOLVED = -7,
PACKS_NOT_LOADED = -8,
PACKS_LOADING_FAIL = -9,
RTE_MODEL_ERROR = -10,
};
ProjMgrRpcServer& m_server;
ProjMgr& m_manager;
ProjMgrWorker& m_worker;
bool m_solutionLoaded = false;
bool m_bUseAllPacks = false;
map<std::string, PackReferenceVector> m_packReferences; // packsInfo is used to simplify creation and access to references
PackReferenceVector& GetPackReferences(const string& context);
PackReferenceVector CollectPackReferences(const string& context);
PackReferenceVector GetPackReferencesForPack(const string& context, const RtePackage* pack);
RpcArgs::PackReference& EnsurePackReferenceForPack(const string& context, const string& packId, const string& origin, bool bVersion);
RpcArgs::PackReference& EnsurePackReference(const string& context, const RpcArgs::PackReference& packRef);
void UpdateFilter(const string& context, RteTarget* rteTarget, bool bAll); // returns true if changed
const ContextItem& GetContext(const string& context) const;
RteTarget* GetActiveTarget(const string& context) const;
RteComponentAggregate* GetComponentAggregate(const string& context, const string& id) const;
void SetAggregateOptions(const string& context, RteComponentAggregate* rteAggregate, const RpcArgs::Options& options);
void SetOptionsForNewlySelectedAggregates(const string& context, RteTarget* rteTarget, const RpcArgs::Options& options);
bool CheckSolutionArg(string& solution, optional<string>& message) const;
};
bool ProjMgrRpcServer::Run(void) {
JsonRpc2Server jsonServer;
RpcHandler handler(*this, jsonServer);
while(!m_shutdown && !cin.fail()) {
// Get request
const auto request = m_contextLength ?
GetRequestFromStdinWithLength() :
GetRequestFromStdin();
if(request.empty()) {
continue;
}
ofstream log;
if(m_debug) {
log.open(RteFsUtils::GetCurrentFolder(true) + "csolution-rpc-log.txt", fstream::app);
log << request << std::endl;
}
// Handle request
const auto response = jsonServer.HandleRequest(request);
// Send response
if(m_contextLength) {
// compliant to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseProtocol
cout << CONTENT_LENGTH_HEADER << response.size() <<
CrossPlatformUtils::Crlf() << CrossPlatformUtils::Crlf() << response << std::flush;
} else {
cout << response << std::endl;
}
if(m_debug) {
log << response << std::endl;
log.close();
}
}
return true;
}
bool RpcHandler::CheckSolutionArg(string& solution, optional<string>& message) const {
if(!regex_match(solution, regex(".*\\.csolution\\.(yml|yaml)"))) {
message = solution + " is not a *.csolution.yml file";
return false;
}
solution = RteFsUtils::MakePathCanonical(solution);
if(!RteFsUtils::Exists(solution)) {
message = solution + " file does not exist";
return false;
}
return true;
}
const ContextItem& RpcHandler::GetContext(const string& context) const {
if(!m_solutionLoaded) {
throw JsonRpcException(SOLUTION_NOT_LOADED, "a valid solution must be loaded before proceeding");
}
if(context.empty()) {
throw JsonRpcException(CONTEXT_NOT_VALID, "'context' argument cannot be empty");
}
const auto selected = m_worker.GetSelectedContexts();
if(find(selected.begin(), selected.end(), context) == selected.end()) {
throw JsonRpcException(CONTEXT_NOT_FOUND, context + " was not found among selected contexts");
}
map<string, ContextItem>* contexts = nullptr;
m_worker.GetContexts(contexts);
return (*contexts)[context];
}
RteTarget* RpcHandler::GetActiveTarget(const string& context) const {
const auto& contextItem = GetContext(context);
return contextItem.rteActiveTarget;
}
RteComponentAggregate* RpcHandler::GetComponentAggregate(const string& context, const string& id) const {
auto rteAggregate = GetContext(context).rteActiveTarget->GetComponentAggregate(id);
if(!rteAggregate) {
throw JsonRpcException(COMPONENT_NOT_FOUND, id + ": component not found");
}
return rteAggregate;
}
void RpcHandler::SetAggregateOptions(const string& context, RteComponentAggregate* rteAggregate, const RpcArgs::Options& options) {
auto& layer = options.layer.has_value() ? options.layer.value() : RteUtils::EMPTY_STRING;
rteAggregate->AddAttribute("layer", layer, false);
const bool explicitVendor = options.explicitVendor.has_value() ? options.explicitVendor.value() : false;
rteAggregate->AddAttribute("explicitVendor", explicitVendor ? "1" : "", false);
auto& explicitVersion = options.explicitVersion.has_value() ? options.explicitVersion.value() : RteUtils::EMPTY_STRING;
// TODO: check if version is plausible
rteAggregate->AddAttribute("explicitVersion", explicitVersion, false);
auto rteComponent = rteAggregate->GetComponent();
// ensure Pack reference is added when component is selected
int count = rteAggregate->IsSelected();
if(count > 0 && rteComponent) {
auto& packId = rteComponent->GetPackage()->GetID();
EnsurePackReferenceForPack(context, packId, layer, !explicitVersion.empty());
}
}
void RpcHandler::SetOptionsForNewlySelectedAggregates(const string& context, RteTarget* rteTarget, const RpcArgs::Options& options) {
const auto& selectedAggregates = rteTarget->CollectSelectedComponentAggregates();
for(const auto& [rteAggregate, _] : selectedAggregates) {
if(!rteAggregate->GetComponentInstance()) {
// only set options to newly selected components
SetAggregateOptions(context, rteAggregate, options);
}
}
}
RpcArgs::GetVersionResult RpcHandler::GetVersion(void) {
RpcArgs::GetVersionResult res = {{true}};
res.message = string("Running ") + INTERNAL_NAME + " " + VERSION_STRING;
res.version = VERSION_STRING;
res.apiVersion = RPC_API_VERSION;
return res;
}
RpcArgs::SuccessResult RpcHandler::Shutdown(void) {
m_server.SetShutdown(true);
return {true, "Shutdown initiated..."};
}
RpcArgs::SuccessResult RpcHandler::Apply(const string& context) {
RpcArgs::SuccessResult result = {false};
auto rteProject = GetActiveTarget(context)->GetProject();
if(rteProject) {
rteProject->Apply();
// Apply returns true if list of gpdc files needs to be updated: irrelevant for csolution
result.success = true;
// purge unselected packs
auto& packRefs = GetPackReferences(context);
packRefs.erase(
std::remove_if(packRefs.begin(), packRefs.end(),
[](RpcArgs::PackReference& ref) { return !ref.selected; }),
packRefs.end()
);
}
return result;
}
RpcArgs::SuccessResult RpcHandler::Resolve(const string& context, const RpcArgs::Options& options) {
RpcArgs::SuccessResult result = {false};
auto rteTarget = GetActiveTarget(context);
auto rteProject = rteTarget->GetProject();
if(rteProject) {
result.success = rteProject->ResolveDependencies(rteTarget);
SetOptionsForNewlySelectedAggregates(context, rteTarget, options);
Apply(context);
}
return result;
}
RpcArgs::SuccessResult RpcHandler::LoadPacks(void) {
RpcArgs::SuccessResult result = {false};
m_manager.Clear();
m_solutionLoaded = false;
ProjMgrKernel::Get()->GetGlobalModel()->Clear();
m_worker.InitializeModel();
m_worker.SetLoadPacksPolicy(LoadPacksPolicy::ALL);
result.success = m_worker.LoadAllRelevantPacks();
m_worker.SetLoadPacksPolicy(LoadPacksPolicy::DEFAULT);
if(!result.success) {
result.message = "Packs failed to load";
}
return result;
}
RpcArgs::SuccessResult RpcHandler::LoadSolution(const string& solution, const string& activeTarget) {
m_bUseAllPacks = false; // loading solution will first use only listed packs
m_packReferences.clear();
RpcArgs::SuccessResult result = {false};
const auto csolutionFile = RteFsUtils::MakePathCanonical(solution);
if(!regex_match(csolutionFile, regex(".*\\.csolution\\.(yml|yaml)"))) {
result.message = solution + " is not a *.csolution.yml file";
return result;
}
// we disregard return value of m_manager.LoadSolution() here, because we tolerate some errors
m_manager.LoadSolution(csolutionFile, activeTarget);
map<string, ContextItem>* contexts = nullptr;
m_worker.GetContexts(contexts);
bool hasUsableContext = false;
if (contexts && !contexts->empty()) {
// ensure at least one context has a valid active target
for (const auto& contextItem : *contexts) {
if (contextItem.second.rteActiveTarget != nullptr) {
hasUsableContext = true;
break;
}
}
}
result.success = m_solutionLoaded = hasUsableContext;
if(!result.success) {
// severe situation: contexts were not populated or are unusable
result.message = "failed to load and process solution " + csolutionFile;
}
return result;
}
void RpcHandler::UpdateFilter(const string& context, RteTarget* rteTarget, bool all) {
if(m_bUseAllPacks == all) {
return;
}
m_bUseAllPacks = all;
RtePackageFilter packFilter;
// construct and apply filter
// use resolved pack ID's from selected references
set<string> packIds;
set<string> latestIds;
for(auto& ref : GetPackReferences(context)) {
if(ref.selected && ref.resolvedPack.has_value()) {
auto& packId = ref.resolvedPack.value();
packIds.insert(packId);
string commonId = RtePackage::CommonIdFromId(packId);
latestIds.insert(commonId);
}
}
packFilter.SetSelectedPackages(packIds);
packFilter.SetLatestPacks(latestIds);
packFilter.SetUseAllPacks(all);
// only update filter if differs from current state
if(!packFilter.IsEqual(rteTarget->GetPackageFilter())) {
rteTarget->SetPackageFilter(packFilter);
rteTarget->UpdateFilterModel(); // updates available components
rteTarget->GetProject()->UpdateModel(); // inserts already instantiated components
rteTarget->EvaluateComponentDependencies();
}
}
RpcArgs::SuccessResult RpcHandler::SelectPack(const string& context, const RpcArgs::PackReference& packRef) {
RpcArgs::SuccessResult result = {true};
// find reference if exists, otherwise add new one
auto& ref = EnsurePackReference(context, packRef);
ref.selected = packRef.selected;
if(!ref.resolvedPack.has_value()) {
// TODO: resolve pack
}
return result;
}
RpcArgs::ContextInfo RpcHandler::GetContextInfo(const string& context) {
RpcArgs::ContextInfo contextInfo;
RpcDataCollector dc(GetActiveTarget(context));
contextInfo.success = true;
dc.CollectUsedComponents(contextInfo.components);
// get all references, even if they are not selected because it is useful for client to remove them from files
contextInfo.packs = GetPackReferences(context);
auto& contextItem = GetContext(context);
contextInfo.variables = contextItem.variables;
contextInfo.variables.merge((StrMap)contextItem.absPathSequences);
contextInfo.attributes = contextItem.targetAttributes;
contextInfo.pname = contextItem.deviceItem.pname;
if(contextItem.rteDevice) {
contextInfo.device = dc.FromRteDevice(contextItem.rteDevice, true);
} else {
contextInfo.device.id = contextItem.device;
contextInfo.success = false;
contextInfo.message = "No device is found";
}
if(contextItem.rteBoard) {
contextInfo.board = dc.FromRteBoard(contextItem.rteBoard, true);
}
return contextInfo;
}
RpcArgs::UsedItems RpcHandler::GetUsedItems(const string& context) {
RpcArgs::UsedItems usedItems;
RpcDataCollector dc(GetActiveTarget(context));
dc.CollectUsedComponents(usedItems.components);
// get all references, even if they are not selected , because it is useful for client to remove them from files
usedItems.packs = GetPackReferences(context);
usedItems.success = true;
return usedItems;
}
PackReferenceVector& RpcHandler::GetPackReferences(const string& context) {
auto it = m_packReferences.find(context);
if(it != m_packReferences.end()) {
return it->second;
}
// collect references
// emplace returns pair<iterator, bool>
// return the value from the iterator
return m_packReferences.emplace(context, RpcHandler::CollectPackReferences(context)).first->second;
}
PackReferenceVector RpcHandler::CollectPackReferences(const string& context) {
PackReferenceVector packRefs;
auto contextItem = GetContext(context);
for(const auto& packItem : contextItem.packRequirements) {
const auto& packId = packItem.resolvedTo;
RpcArgs::PackReference packRef;
packRef.pack = packItem.selectedBy;
if(!packItem.path.empty()) {
packRef.resolvedPack = packItem.selectedBy;
packRef.path = packItem.path;
} else if(!packId.empty()) {
packRef.resolvedPack = packId;
}
packRef.origin = packItem.origin;
packRef.selected = true; // initially pack is selected;
if(packItem.path.empty() && !packId.empty()) {
const auto availableVersionIt = contextItem.availablePackVersions.find(packId);
if(availableVersionIt != contextItem.availablePackVersions.end() && !availableVersionIt->second.empty()) {
packRef.upgrade = availableVersionIt->second;
}
}
packRefs.push_back(packRef);
}
return packRefs;
}
PackReferenceVector RpcHandler::GetPackReferencesForPack(const string& context, const RtePackage* pack) {
PackReferenceVector packRefs;
auto& packId = pack->GetID();
auto path = pack->GetRootFilePath(false);
for(auto& ref : GetPackReferences(context)) {
if((ref.resolvedPack.has_value() && ref.resolvedPack == packId) ||
(ref.path.has_value() && RteFsUtils::Equivalent(ref.path.value(), path)) ||
ref.pack == packId) {
packRefs.push_back(ref);
}
}
return packRefs;
}
RpcArgs::PackReference& RpcHandler::EnsurePackReferenceForPack(const string& context, const string& packId, const string& origin, bool bExplicitVersion) {
auto& packRefs = GetPackReferences(context);
for(auto& ref : packRefs) {
if(ref.resolvedPack.has_value() && ref.resolvedPack == packId &&
(origin.empty() || RteFsUtils::Equivalent(ref.origin, origin))) {
ref.selected = true; // ensure selected
return ref;
}
}
// no reference->add one to list of all references
RpcArgs::PackReference newRef;
newRef.pack = bExplicitVersion ? packId : RtePackage::CommonIdFromId(packId);
newRef.resolvedPack = packId;
if(!origin.empty()) {
newRef.origin = origin;
}
newRef.selected = true;
return packRefs.emplace_back(newRef);
}
RpcArgs::PackReference& RpcHandler::EnsurePackReference(const string& context, const RpcArgs::PackReference& packRef) {
auto& packRefs = GetPackReferences(context);
for(auto& ref : packRefs) {
if(ref.pack == packRef.pack &&
(packRef.origin.empty() || RteFsUtils::Equivalent(ref.origin, packRef.origin)) &&
(RteFsUtils::Equivalent(ref.path.value_or(""), packRef.path.value_or("")))) {
return ref;
}
}
// no reference->add supplied
return packRefs.emplace_back(packRef);
}
RpcArgs::PacksInfo RpcHandler::GetPacksInfo(const string& context, const bool& all) {
RteTarget* rteTarget = GetActiveTarget(context);
UpdateFilter(context, rteTarget, all);
RpcDataCollector dc(GetActiveTarget(context));
auto usedPacks = dc.GetUsedPacks();
map<string, RpcArgs::Pack> packsMap;
for(auto& [packId, rtePackage] : rteTarget->GetFilteredModel()->GetPackages()) {
RpcArgs::Pack p;
p.id = rtePackage->GetPackageID(true);
const auto& description = rtePackage->GetDescription();
if(!description.empty()) {
p.description = description;
}
auto overview = rtePackage->GetDocFile();
if(!overview.empty()) {
p.doc = overview;
}
if(contains_key(usedPacks, p.id)) {
p.used = true;
}
auto packRefs = GetPackReferencesForPack(context, rtePackage);
if(!packRefs.empty()) {
p.references = packRefs;
}
packsMap[p.id] = p;
}
// add unresolved packs from unresolved references
for(auto& ref : GetPackReferences(context)) {
if(!ref.resolvedPack.has_value() && !ref.path.has_value()) {
auto it = packsMap.find(ref.pack);
if(it != packsMap.end()) {
// only add reference, the optional references already exists
it->second.references.value().push_back(ref);
continue;
}
RpcArgs::Pack p;
p.id = ref.pack; // use original ID
p.used = true;
p.description = "Pack not installed";
PackReferenceVector packRefs;
packRefs.push_back(ref);
p.references = packRefs;
packsMap[ref.pack] = p;
}
}
RpcArgs::PacksInfo packsInfo;
for(auto& [_, p] : packsMap) {
packsInfo.packs.push_back(p);
}
packsInfo.success = true;
return packsInfo;
}
RpcArgs::VariablesResult RpcHandler::GetVariables(const string& context) {
RpcArgs::VariablesResult res;
res.success = false;
auto& contextItem = GetContext(context);
res.variables = contextItem.variables;
res.variables.merge((StrMap)contextItem.absPathSequences);
res.success = true;
return res;
}
RpcArgs::DeviceList RpcHandler::GetDeviceList(const string& context, const string& namePattern, const string& vendor)
{
RpcArgs::DeviceList deviceList{{true}};
RteTarget* rteTarget = context.empty() ? nullptr : GetActiveTarget(context);
RteModel* rteModel = rteTarget ? rteTarget->GetFilteredModel() : ProjMgrKernel::Get()->GetGlobalModel();
RpcDataCollector dc(rteTarget, rteModel);
dc.CollectDeviceList(deviceList, namePattern, vendor);
return deviceList;
}
RpcArgs::DeviceInfo RpcHandler::GetDeviceInfo(const string& id)
{
RpcArgs::DeviceInfo deviceInfo{{false}};
RpcDataCollector dc(nullptr, ProjMgrKernel::Get()->GetGlobalModel());
dc.CollectDeviceInfo(deviceInfo, id);
return deviceInfo;
}
RpcArgs::BoardList RpcHandler::GetBoardList(const string& context, const string& namePattern, const string& vendor)
{
RpcArgs::BoardList boardList{{false}};
RteTarget* rteTarget = context.empty() ? nullptr : GetActiveTarget(context);
RteModel* rteModel = rteTarget ? rteTarget->GetFilteredModel() : ProjMgrKernel::Get()->GetGlobalModel();
RpcDataCollector dc(rteTarget, rteModel);
dc.CollectBoardList(boardList, namePattern, vendor);
boardList.success = true;
return boardList;
}
RpcArgs::BoardInfo RpcHandler::GetBoardInfo(const string& id)
{
RpcArgs::BoardInfo boardInfo{{false}};
RpcDataCollector dc(nullptr, ProjMgrKernel::Get()->GetGlobalModel());
dc.CollectBoardInfo(boardInfo, id);
return boardInfo;
}
RpcArgs::CtRoot RpcHandler::GetComponentsTree(const string& context, const bool& all) {
RteTarget* rteTarget = GetActiveTarget(context);
UpdateFilter(context, rteTarget, all);
RpcDataCollector dc(rteTarget);
RpcArgs::CtRoot ctRoot;
dc.CollectCtClasses(ctRoot);
ctRoot.success = true;
return ctRoot;
}
RpcArgs::SuccessResult RpcHandler::SelectComponent(const string& context, const string& id, const int& count, const RpcArgs::Options& options) {
// first try full component ID
RteTarget* activeTarget = GetActiveTarget(context);
RteComponent* rteComponent = activeTarget->GetComponent(id);
RteComponentAggregate* rteAggregate = nullptr;
RpcArgs::SuccessResult result = {false};
if(rteComponent) {
result.success = GetActiveTarget(context)->SelectComponent(rteComponent, count, true);
rteAggregate = activeTarget->GetComponentAggregate(rteComponent);
} else {
rteAggregate = GetComponentAggregate(context, id);
result.success = activeTarget->SelectComponent(rteAggregate, count, true);
rteComponent = rteAggregate->GetComponent();
}
SetAggregateOptions(context, rteAggregate, options);
Apply(context);
return result;
}
RpcArgs::SuccessResult RpcHandler::SelectVariant(const string& context, const string& id, const string& variant) {
RpcArgs::SuccessResult result = {false};
RteComponentAggregate* rteAggregate = GetComponentAggregate(context, id);
if(rteAggregate->GetSelectedVariant() == variant) {
return result;
}
auto availableVariants = rteAggregate->GetVariants();
if(std::find(availableVariants.begin(), availableVariants.end(), variant) == availableVariants.end()) {
result.message = "Variant '" + variant + "' is not found for component " + id;
return result;
}
rteAggregate->SetSelectedVariant(variant);
if(rteAggregate->IsSelected()) {
GetActiveTarget(context)->EvaluateComponentDependencies();
}
result.success = true;
Apply(context);
return result;
}
RpcArgs::SuccessResult RpcHandler::SelectBundle(const string& context, const string& className, const string& bundleName) {
RpcArgs::SuccessResult result = {false};
RteTarget* rteTarget = GetActiveTarget(context);
RteComponentClass* rteClass = rteTarget->GetComponentClass(className);
if(!rteClass) {
throw JsonRpcException(COMPONENT_NOT_FOUND, className + ": component class not found");
}
if(rteClass->GetSelectedBundleName() == bundleName) {
return result; // no change => false
}
if(!contains_key(rteClass->GetBundleNames(), bundleName)) {
result.message = "Bundle '" + bundleName + "' is not found for component class '" + className + "'";
return result; // error => false
}
rteClass->SetSelectedBundleName(bundleName, true);
Apply(context);
GetActiveTarget(context)->EvaluateComponentDependencies();
result.success = true;
return result;
}
RpcArgs::Results RpcHandler::ValidateComponents(const string& context) {
auto contextItem = GetContext(context);
RpcArgs::Results results;
auto validationRes = m_worker.ValidateContext(contextItem);
results.result = RteItem::ConditionResultToString(validationRes);
if(validationRes < RteItem::ConditionResult::FULFILLED) {
results.validation = vector<RpcArgs::Result>{};
for(const auto& validation : contextItem.validationResults) {
RpcArgs::Result r;
r.result = RteItem::ConditionResultToString(validation.result);
r.id = validation.id;
if(!validation.aggregates.empty()) {
r.aggregates = vector<string>(validation.aggregates.begin(), validation.aggregates.end());
}
if(!validation.conditions.empty()) {
RpcArgs::Condition c;
r.conditions = vector<RpcArgs::Condition>{};
for(const auto& condition : validation.conditions) {
c.expression = condition.expression;
if(!condition.aggregates.empty()) {
c.aggregates = vector<string>(condition.aggregates.begin(), condition.aggregates.end());
}
r.conditions->push_back(c);
}
}
results.validation->push_back(r);
}
}
results.success = true;
return results;
}
RpcArgs::LogMessages RpcHandler::GetLogMessages(void) {
StrVec infoVec;
for(const auto& [_, info] : ProjMgrLogger::Get().GetInfos()) {
for(const auto& msg : info) {
CollectionUtils::PushBackUniquely(infoVec, msg);
}
}
StrVec errorsVec;
for(const auto& [_, errors] : ProjMgrLogger::Get().GetErrors()) {
for(const auto& msg : errors) {
CollectionUtils::PushBackUniquely(errorsVec, msg);
}
}
StrVec warningsVec;
for(const auto& [_, warnings] : ProjMgrLogger::Get().GetWarns()) {
for(const auto& msg : warnings) {
CollectionUtils::PushBackUniquely(warningsVec, msg);
}
}
RpcArgs::LogMessages messages;
if(!infoVec.empty()) {
messages.info = infoVec;
}
if(!errorsVec.empty()) {
messages.errors = errorsVec;
}
if(!warningsVec.empty()) {
messages.warnings = warningsVec;
}
messages.success = true;
return messages;
}
RpcArgs::DraftProjectsInfo RpcHandler::GetDraftProjects(const RpcArgs::DraftProjectsFilter& filter) {
RpcArgs::DraftProjectsInfo applications;
applications.success = false;
// initialize context and target attributes with board and device
ContextItem context;
m_worker.InitializeTarget(context);
if(filter.board.has_value() || filter.device.has_value()) {
context.board = filter.board.has_value() ? filter.board.value() : "";
context.device = filter.device.has_value() ? filter.device.value() : "";
if(!m_worker.ProcessDevice(context, BoardOrDevice::SkipProcessor)) {
applications.message = "Board or device processing failed";
return applications;
}
if(contains_key(context.targetAttributes, "Dname") && !contains_key(context.targetAttributes, "Bname")) {
context.targetAttributes["Bname"] = "";
}
m_worker.SetTargetAttributes(context, context.targetAttributes);
}
// collect examples, optionally filtered for 'environments'
vector<RpcArgs::ExampleProject> examples, refApps;
const auto& environments = filter.environments.has_value() ? filter.environments.value() : StrVec();
const auto& collectedExamples = m_worker.CollectExamples(context, environments);
for(const auto& example : collectedExamples) {
RpcArgs::ExampleProject e;
e.name = example.name;
e.pack = example.pack;
e.doc = example.doc;
e.description = example.description;
if(!example.version.empty()) {
e.version = example.version;
}
if(!example.archive.empty()) {
e.archive = example.archive;
}
for(const auto& [name, environment] : example.environments) {
RpcArgs::ExampleEnvironment env;
env.name = name;
env.file = environment.load;
env.folder = environment.folder;
e.environments.push_back(env);
}
if(!example.components.empty()) {
e.components = example.components;
}
if(!example.categories.empty()) {
e.categories = example.categories;
}
if(!example.keywords.empty()) {
e.keywords = example.keywords;
}
// classify the example as ref-app if it does not specify boards
auto& ref = example.boards.empty() ? refApps : examples;
ref.push_back(e);
}
if(!examples.empty()) {
applications.examples = examples;
}
if(!refApps.empty()) {
applications.refApps = refApps;
}
// collect templates
vector<RpcArgs::SolutionTemplate> templates;
const auto& csolutionTemplates = m_worker.CollectTemplates(context);
for(const auto& csolutionTemplate : csolutionTemplates) {
RpcArgs::SolutionTemplate t;
t.name = csolutionTemplate.name;
t.pack = csolutionTemplate.pack;
t.description = csolutionTemplate.description;
t.file = csolutionTemplate.file;
t.folder = csolutionTemplate.path;
if(!csolutionTemplate.copyTo.empty()) {
t.copyTo = csolutionTemplate.copyTo;
}
templates.push_back(t);
}
if(!templates.empty()) {
applications.templates = templates;
}
applications.success = true;
return applications;
}
RpcArgs::ConvertSolutionResult RpcHandler::ConvertSolution(const string& solution, const string& activeTarget, const bool& updateRte) {
RpcArgs::ConvertSolutionResult result = {{ false }};
string csolutionFile = solution;
if(!CheckSolutionArg(csolutionFile, result.message)) {
return result;
}
m_bUseAllPacks = false; // loading solution will first use only listed packs
m_packReferences.clear(); // will be updated
if(!m_manager.RunConvert(csolutionFile, activeTarget, updateRte) || !ProjMgrLogger::Get().GetErrors().empty()) {
if(m_worker.HasVarDefineError()) {
const auto& vars = m_worker.GetUndefLayerVars();
result.undefinedLayers = StrVec(vars.begin(), vars.end());
const auto& selectCompiler = m_worker.GetSelectableCompilers();
if(!selectCompiler.empty()) {
result.selectCompiler = selectCompiler;
}
result.message = "Layer variables undefined, names can be found under 'undefinedLayers'";
} else if(m_worker.HasCompilerDefineError()) {
result.selectCompiler = m_worker.GetSelectableCompilers();
result.message = "Compiler undefined, selectable values can be found under 'selectCompiler'";
} else {
result.message = "Convert solution failed, see log messages";
}
return result;
}
result.success = true;
return result;
}
RpcArgs::DiscoverLayersInfo RpcHandler::DiscoverLayers(const string& solution, const string& activeTarget) {
RpcArgs::DiscoverLayersInfo result = {{ false }};
string csolutionFile = solution;
if(!CheckSolutionArg(csolutionFile, result.message)) {
return result;
}
if(!m_manager.SetupContexts(csolutionFile, activeTarget)) {
result.message = "Setup of solution contexts failed";
return result;
}
m_worker.SetUpCommand(true);
StrVec layers;
StrSet fails;
if(!m_worker.ListLayers(layers, "", fails) || !m_worker.ElaborateVariablesConfigurations()) {
result.message = "No compatible software layer was found in the installed packs.\nInstall additional packs containing suitable layers before restarting the 'Create Solution' flow.";
return result;
} else {
// retrieve valid configurations
vector<RpcArgs::VariablesConfiguration> vcVec;
const auto& processedContexts = m_worker.GetProcessedContexts();
for(const auto& context : processedContexts) {
if(!context->variablesConfigurations.empty()) {
for(const auto& configuration : context->variablesConfigurations) {
RpcArgs::VariablesConfiguration vc;
vector<RpcArgs::LayerVariable> lvVec;
for(const auto& variable : configuration.variables) {
RpcArgs::LayerVariable lv = {variable.name, variable.clayer};
vector<RpcArgs::SettingsType> settings;
for(const auto& s : variable.settings) {
if(!s.set.empty()) {
settings.push_back(RpcArgs::SettingsType{s.set});
}
}
if(!settings.empty()) {
lv.settings = settings;
}
if(!variable.path.empty()) {
lv.path = variable.path;
}
if(!variable.file.empty()) {
lv.file = variable.file;
}
if(!variable.description.empty()) {
lv.description = variable.description;
}
if(!variable.copyTo.empty()) {
lv.copyTo = variable.copyTo;
}
lvVec.push_back(lv);
}
vc.variables = lvVec;
vcVec.push_back(vc);
}
// break after first project context with possible variable configurations
// solutions with undefined layer variables over multiple projects are currently not supported
break;
}
}
if(!vcVec.empty()) {
result.configurations = vcVec;
}
result.success = true;
return result;
}
}
RpcArgs::ListMissingPacksResult RpcHandler::ListMissingPacks(const string& solution, const string& activeTarget) {
RpcArgs::ListMissingPacksResult result = {{ false }};
string csolutionFile = solution;
if(!CheckSolutionArg(csolutionFile, result.message)) {
return result;
}
if(!m_manager.SetupContexts(csolutionFile, activeTarget)) {
result.message = "Setup of solution contexts failed";
return result;
}
StrVec missingPacks;
m_worker.ListPacks(missingPacks, true);
if(!missingPacks.empty()) {
result.packs = missingPacks;
}
result.success = true;
return result;
}
// end of ProkMgrRpcServer.cpp