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
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
|
/*
* Copyright (c) 2018 TOYOTA MOTOR CORPORATION
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fstream>
#include <sstream>
#include <istream>
#include <thread>
#include <map>
#include <systemd/sd-event.h>
#include <json-c/json.h>
#include "policy_manager.hpp"
#include "hmi-debug.h"
namespace stm {
extern "C" {
#include "dummy_stm.h"
}
} // namespace stm
namespace pm {
typedef struct AreaState {
std::string name;
std::string category;
std::string role;
} AreaState;
typedef std::vector<AreaState> AreaList;
typedef struct LayoutState {
std::string name;
std::map<std::string, int> category_num;
// int category_num[stm::gStmCategoryNoNum];
AreaList area_list;
std::map<std::string, std::vector<std::string>> role_history;
} LayoutState;
typedef struct LayerState {
std::string name;
LayoutState layout_state;
} LayerState;
struct sd_event* event_loop;
std::map<int, struct sd_event_source*> event_source_list;
std::map<int, std::string> g_event_info_list;
PolicyManager::CallbackTable callback;
std::unordered_map<std::string, LayerState> g_prv_layers;
std::unordered_map<std::string, LayerState> g_crr_layers;
std::unordered_map<std::string, LayerState> g_prv_layers_car_stop;
std::unordered_map<std::string, LayoutState> g_default_layouts;
} // namespace pm
PolicyManager::PolicyManager() :
eventname2no_(),
categoryname2no_(),
areaname2no_(),
role2category_(),
category2role_(),
role2defaultarea_()
{
HMI_DEBUG("wm:pm", "Call");
}
int PolicyManager::initialize() {
HMI_DEBUG("wm:pm", "Call");
int ret = 0;
// Create convert map
for (unsigned int i=0; i<STM_NUM_EVT; i++) {
HMI_DEBUG("wm:pm", "event name:%s no:%d", stm::gStmEventName[i], stm::gStmEventNo[i]);
this->eventname2no_[stm::gStmEventName[i]] = stm::gStmEventNo[i];
}
for (int i = stm::gStmCategoryNoMin; i <= stm::gStmCategoryNoMax; i++) {
HMI_DEBUG("wm:pm", "category name:%s no:%d", stm::gStmCategoryName[i], stm::gStmCategoryNo[i]);
this->categoryname2no_[stm::gStmCategoryName[i]] = stm::gStmCategoryNo[i];
}
for (unsigned int i=0; i<STM_NUM_ARA; i++) {
HMI_DEBUG("wm:pm", "area name:%s no:%d", stm::gStmAreaName[i], stm::gStmAreaNo[i]);
this->areaname2no_[stm::gStmAreaName[i]] = stm::gStmAreaNo[i];
}
// Load role.db
ret = this->loadRoleDb();
if (0 > ret) {
HMI_ERROR("wm:pm", "Load role.db Error!!");
return ret;
}
// Load layout.db
ret = this->loadLayoutDb();
if (0 > ret) {
HMI_ERROR("wm:pm", "Load layout.db Error!!");
return ret;
}
// Initialize current/previous state of layers
pm::AreaState init_area;
pm::LayoutState init_layout;
init_area.name = "none";
init_area.category = "none";
init_area.role = "none";
init_layout.area_list.push_back(init_area);
for (int i = stm::gStmLayerNoMin; i <= stm::gStmLayerNoMax; i++) {
const char* layer_name = stm::gStmLayerName[i];
pm::g_crr_layers[layer_name].name = layer_name;
pm::g_crr_layers[layer_name].layout_state = init_layout;
}
pm::g_prv_layers = pm::g_crr_layers;
// Initialize StateTransitioner
stm::stmInitialize();
// Initialize sd_event loop
ret = this->initializeSdEventLoop();
if (0 > ret) {
HMI_ERROR("wm:pm", "Failed to initializeSdEventLoop!!");
return ret;
}
return ret;
}
int PolicyManager::initializeSdEventLoop() {
// Get default event loop object
int ret = sd_event_new(&(pm::event_loop));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_default: errno:%d", ret);
return -1;
}
// Create thread for sd_event and detach
std::thread sd_event_loop([this]() {
while (1) {
sd_event_run(pm::event_loop, 1000);
}
});
sd_event_loop.detach();
return 0;
}
static void addStateToJson(
const char* key, int is_changed, const char* state, json_object** json_out) {
if ((nullptr == key) || (nullptr == state) || (nullptr == json_out)) {
HMI_ERROR("wm:pm", "Argument is nullptr!!!");
return;
}
json_object* json_obj = json_object_new_object();
json_object_object_add(json_obj, "is_changed", json_object_new_boolean(is_changed));
if (is_changed) {
HMI_DEBUG("wm:pm", "%s: state changed (%s)", key, state);
json_object_object_add(json_obj, "state", json_object_new_string(state));
}
json_object_object_add(*json_out, key, json_obj);
}
static void addStateToJson(const char* layer_name, unsigned int changed,
pm::AreaList area_list, json_object** json_out) {
if ((nullptr == layer_name) || (1 < changed) || (nullptr == json_out)) {
HMI_ERROR("wm:pm", "Invalid argument!!!");
return;
}
json_object* json_areas = json_object_new_array();
json_object* json_tmp;
for (pm::AreaState as : area_list) {
json_tmp = json_object_new_object();
json_object_object_add(json_tmp, "name", json_object_new_string(as.name.c_str()));
json_object_object_add(json_tmp, "role", json_object_new_string(as.role.c_str()));
json_object_array_add(json_areas, json_tmp);
}
json_object_object_add(*json_out, "name", json_object_new_string(layer_name));
json_object_object_add(*json_out, "changed", json_object_new_boolean(changed));
json_object_object_add(*json_out, "areas", json_areas);
}
static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role);
static int checkPolicy(sd_event_source *source, void *data) {
HMI_DEBUG("wm:pm", "Call");
HMI_DEBUG("wm:pm", ">>>>>>>>>> START CHECK POLICY");
int event_data = *((int*)data);
int event_no, category_no, area_no;
event_no = (event_data & STM_MSK_EVT_NO) - 1;
category_no = ((event_data & STM_MSK_CTG_NO) >> 8) - 1;
area_no = ((event_data & STM_MSK_ARA_NO) >> 16) - 1;
HMI_DEBUG("wm:pm", ">>>>>>>>>> event:%s category:%s area:%s",
stm::gStmEventName[event_no],
stm::gStmCategoryName[category_no],
stm::gStmAreaName[area_no]);
// Transition state
stm::stm_state_t crr_state;
int ret = stm::stmTransitionState(event_data, &crr_state);
if (0 > ret) {
HMI_ERROR("wm:pm", "Error!!");
return -1;
}
HMI_DEBUG("wm:pm", "parking brake state (is_changed:%d state:%d:%s)",
crr_state.parking_brake.is_changed,
crr_state.parking_brake.state,
stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state]);
HMI_DEBUG("wm:pm", "accelerator pedal state (is_changed:%d state:%d:%s)",
crr_state.accel_pedal.is_changed,
crr_state.accel_pedal.state,
stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state]);
HMI_DEBUG("wm:pm", "lightstatus brake state (is_changed:%d state:%d:%s)",
crr_state.lightstatus_brake.is_changed,
crr_state.lightstatus_brake.state,
stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state]);
HMI_DEBUG("wm:pm", "car state (is_changed:%d state:%d:%s)",
crr_state.car.is_changed,
crr_state.car.state,
stm::gStmCarStateNo2Name[crr_state.car.state]);
HMI_DEBUG("wm:pm", "lamp state (is_changed:%d state:%d:%s)",
crr_state.lamp.is_changed,
crr_state.lamp.state,
stm::gStmLampStateNo2Name[crr_state.lamp.state]);
HMI_DEBUG("wm:pm", "restriction mode state (is_changed:%d state:%d:%s)",
crr_state.restriction_mode.is_changed,
crr_state.restriction_mode.state,
stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state]);
HMI_DEBUG("wm:pm", "homescreen state (is_changed:%d state:%d:%s)",
crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
crr_state.layer[stm::gStmLayerNoHomescreen].state,
stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state]);
HMI_DEBUG("wm:pm", "apps state (is_changed:%d state:%d:%s)",
crr_state.layer[stm::gStmLayerNoApps].is_changed,
crr_state.layer[stm::gStmLayerNoApps].state,
stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state]);
HMI_DEBUG("wm:pm", "restriction state (is_changed:%d state:%d:%s)",
crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
crr_state.layer[stm::gStmLayerNoRestriction].state,
stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state]);
HMI_DEBUG("wm:pm", "on_screen state (is_changed:%d state:%d:%s)",
crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
crr_state.layer[stm::gStmLayerNoOnScreen].state,
stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state]);
// Store previous layers
pm::g_prv_layers = pm::g_crr_layers;
std::string req_role = pm::g_event_info_list[event_data];
std::string req_evt = std::string(stm::gStmEventName[event_no]);
std::string req_ctg = std::string(stm::gStmCategoryName[category_no]);
std::string req_area = std::string(stm::gStmAreaName[area_no]);
HMI_DEBUG("wm:pm", "REQ: event:%s role%s category:%s area:%s",
req_evt.c_str(), req_role.c_str(), req_ctg.c_str(), req_area.c_str());
// Update layers
for (int layer_no = stm::gStmLayerNoMin;
layer_no <= stm::gStmLayerNoMax; layer_no++) {
const char* layer_name = stm::gStmLayerName[layer_no];
HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
#if 1
// If restriction mode is changed off -> on,
// store current state for state of restriction mode off
if ((crr_state.restriction_mode.is_changed)
&& (stm::gStmRestrictionModeStateNoOn == crr_state.restriction_mode.state)) {
HMI_DEBUG("wm:lm", "Store current state for state of restriction mode off");
pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
}
#else
// If car state is changed car_stop -> car_run,
// store current state for state of car stop
if ((crr_state.car.is_changed)
&& (stm::gStmCarStateNoRun == crr_state.car.state)) {
HMI_DEBUG("wm:lm", "Store current state for state of car stop");
pm::g_prv_layers_car_stop[layer_name] = pm::g_crr_layers[layer_name];
}
#endif
// This layer is changed?
if (crr_state.layer[layer_no].is_changed) {
// Get previous layout name of this layer
pm::LayoutState prv_layout_state = pm::g_prv_layers[layer_name].layout_state;
std::string prv_layout_name = prv_layout_state.name;
// Get current layout name of this layer
int crr_layout_state_no = crr_state.layer[layer_no].state;
std::string crr_layout_name = std::string(stm::gStmLayoutNo2Name[crr_layout_state_no]);
pm::LayoutState crr_layout_state;
#if 1
if ((crr_state.restriction_mode.is_changed)
&& (stm::gStmRestrictionModeStateNoOff == crr_state.restriction_mode.state)) {
// If restriction mode is changed on -> off,
// restore state of restriction mode off
HMI_DEBUG("wm:lm", "Restriction mode is changed on -> off, so restore state of restriction mode off");
crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
#else
if ((crr_state.car.is_changed)
&& (stm::gStmCarStateNoStop == crr_state.car.state)) {
// If car state is changed car_run -> car_stop,
// restore state of car stop
HMI_DEBUG("wm:lm", "Car state is changed car_run -> car_stop, so restore state of car stop");
crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
#endif
}
else {
// Copy previous layout state for current
crr_layout_state = prv_layout_state;
if (prv_layout_name == crr_layout_name) {
HMI_DEBUG("wm:lm", "Previous layout is same with current");
}
else {
// If previous layout is NOT same with current,
// current areas is set with default value
HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
crr_layout_state.name = pm::g_default_layouts[crr_layout_name].name;
crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
crr_layout_state.area_list = pm::g_default_layouts[crr_layout_name].area_list;
}
// Create candidate list
std::map<std::string, pm::AreaList> cand_list;
for (int ctg_no=stm::gStmCategoryNoMin;
ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
const char* ctg = stm::gStmCategoryName[ctg_no];
HMI_DEBUG("wm:pm", "ctg:%s", ctg);
// Create candidate list for category from the previous displayed categories
pm::AreaList tmp_cand_list;
for (pm::AreaState area_state : prv_layout_state.area_list) {
if (std::string(ctg) == area_state.category) {
// If there is the category which is same with new category in previous layout,
// push it to list
HMI_DEBUG("wm:pm", "Push to candidate list category:%s role:%s",
area_state.category.c_str(), area_state.role.c_str());
tmp_cand_list.push_back(area_state);
}
}
int candidate_num = prv_layout_state.category_num[ctg];
int blank_num = crr_layout_state.category_num[ctg];
HMI_DEBUG("wm:pm", "blank_num:%d candidate_num:%d", blank_num, candidate_num);
// If requested event is "activate"
// and there are requested category and area,
// update area with requested role in current layout.
bool request_for_this_layer = false;
bool updated = false;
if ((ctg == req_ctg) && ("activate" == req_evt) ) {
HMI_DEBUG("wm:pm", "requested event is activate");
for (pm::AreaState &as : crr_layout_state.area_list) {
if (as.category == req_ctg) {
request_for_this_layer = true;
if (as.name == req_area) {
HMI_DEBUG("wm:pm", "Update current layout: area:%s category:%s role:%s",
as.name.c_str(), as.category.c_str(), as.role.c_str());
as.role = req_role;
blank_num--;
updated = true;
break;
}
}
}
// If NOT updated: there is not requested area in new layout,
// so push requested role to candidate list
if (request_for_this_layer && (!updated)) {
HMI_DEBUG("wm:pm", "Push request to candidate list");
pm::AreaState area_state;
area_state.name = req_area;
area_state.category = req_ctg;
area_state.role = req_role;
tmp_cand_list.push_back(area_state);
}
}
// Compare number of candidate/blank,
// And remove role in order of the oldest as necessary
if (candidate_num < blank_num) {
// Refer history stack
// and add to the top of tmp_cand_list in order to the newest
while (candidate_num != blank_num) {
pm::AreaState area_state;
area_state.name = "";
area_state.category = ctg;
if (0 != crr_layout_state.role_history[ctg].size()) {
HMI_ERROR("wm:pm", "Use role in history stack:%s",
crr_layout_state.role_history[ctg].back().c_str());
area_state.role = crr_layout_state.role_history[ctg].back();
crr_layout_state.role_history[ctg].pop_back();
}
else {
HMI_ERROR("wm:pm", "There is no role in history stack!!");
area_state.role = "";
}
tmp_cand_list.push_back(area_state);
candidate_num++;
}
}
else if (candidate_num > blank_num) {
HMI_DEBUG("wm:pm", "candidate_num > blank_num");
// Remove the oldest role from candidate list
while (candidate_num != blank_num) {
std::string removed_role = tmp_cand_list.begin()->role;
HMI_DEBUG("wm:pm", "Remove the oldest data(role:%s) from tmp_cand_list",
removed_role.c_str());
tmp_cand_list.erase(tmp_cand_list.begin());
candidate_num--;
// Push removed data to history stack
crr_layout_state.role_history[ctg].push_back(removed_role);
}
}
else { // (candidate_num == blank_num)
// nop
}
cand_list[ctg] = tmp_cand_list;
}
// Update areas
for (pm::AreaState &as : crr_layout_state.area_list) {
HMI_DEBUG("wm:pm", "Current area info area:%s category:%s",
as.name.c_str(), as.category.c_str());
if ("" == as.role) {
HMI_DEBUG("wm:pm", "Update this area with role:%s",
cand_list[as.category].begin()->role.c_str());
as.role = cand_list[as.category].begin()->role;
cand_list[as.category].erase(cand_list[as.category].begin());
}
}
}
// Update current layout of this layer
pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
}
}
// Check
for (auto itr : pm::g_crr_layers) {
pm::LayerState ls = itr.second;
HMI_DEBUG("wm:pm", ">>> LAYER:%s",ls.name.c_str());
HMI_DEBUG("wm:pm", ">>> >>> LAYOUT:%s", ls.layout_state.name.c_str());
for (pm::AreaState as : ls.layout_state.area_list) {
HMI_DEBUG("wm:pm", ">>> >>> >>> AREA:%s", as.name.c_str());
HMI_DEBUG("wm:pm", ">>> >>> >>> >>> CTG:%s", as.category.c_str());
HMI_DEBUG("wm:pm", ">>> >>> >>> >>> ROLE:%s", as.role.c_str());
}
}
json_object* json_out = json_object_new_object();
// Create result
// {
// "parking_brake": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("parking_brake",
crr_state.parking_brake.is_changed,
stm::gStmParkingBrakeStateNo2Name[crr_state.parking_brake.state],
&json_out);
// "accel_pedal": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("accel_pedal",
crr_state.accel_pedal.is_changed,
stm::gStmAccelPedalStateNo2Name[crr_state.accel_pedal.state],
&json_out);
// "lightstatus_brake": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("lightstatus_brake",
crr_state.lightstatus_brake.is_changed,
stm::gStmLightstatusBrakeStateNo2Name[crr_state.lightstatus_brake.state],
&json_out);
// "car": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("car",
crr_state.car.is_changed,
stm::gStmCarStateNo2Name[crr_state.car.state],
&json_out);
// "lamp": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("lamp",
crr_state.lamp.is_changed,
stm::gStmLampStateNo2Name[crr_state.lamp.state],
&json_out);
// "restriction_mode": {
// "is_changed": <bool>,
// "state": <const char*>
// },
addStateToJson("restriction_mode",
crr_state.restriction_mode.is_changed,
stm::gStmRestrictionModeStateNo2Name[crr_state.restriction_mode.state],
&json_out);
// Create layout information
//
// "layers": [
// {
// "homescreen": {
// "changed": <bool>,
// "areas": [
// {
// "name":<const char*>,
// "role":<const char*>
// }.
// ...
// ]
// }
// },
// ...
json_object* json_layer = json_object_new_array();
json_object* json_tmp;
for (int layer_no = stm::gStmLayerNoMin;
layer_no <= stm::gStmLayerNoMax; layer_no++) {
const char* layer_name = stm::gStmLayerName[layer_no];
HMI_DEBUG("wm:pm", "LAYER:%s", layer_name);
json_tmp = json_object_new_object();
addStateToJson(layer_name,
crr_state.layer[layer_no].is_changed,
pm::g_crr_layers[layer_name].layout_state.area_list,
&json_tmp);
json_object_array_add(json_layer, json_tmp);
}
// Add json array of layer
json_object_object_add(json_out, "layers", json_layer);
// Notify state is changed
if (nullptr != pm::callback.onStateTransitioned) {
pm::callback.onStateTransitioned(json_out);
}
if (crr_state.car.is_changed) {
if (stm::gStmCarStateNoRun == crr_state.car.state) {
// Set delay event(restriction mode on)
checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_ON, 3000, nullptr);
}
else if (stm::gStmCarStateNoStop == crr_state.car.state) {
// Set event(restriction mode off)
checkPolicyEntry(STM_EVT_NO_RESTRICTION_MODE_OFF, 0, nullptr);
// Stop timer for restriction on event
if (pm::event_source_list.find(STM_EVT_NO_RESTRICTION_MODE_ON)
!= pm::event_source_list.end()) {
HMI_DEBUG("wm:pm", "Stop timer for restriction on");
sd_event_source *event_source
= pm::event_source_list[STM_EVT_NO_RESTRICTION_MODE_ON];
int ret = sd_event_source_set_enabled(event_source, SD_EVENT_OFF);
if (0 > ret) {
HMI_ERROR("wm:pm", "Failed to stop timer");
}
}
}
}
// Release json_object
json_object_put(json_out);
// Release data
delete (int*)data;
// Destroy sd_event_source object
sd_event_source_unref(source);
// Remove event source from list
if (pm::event_source_list.find(event_data) != pm::event_source_list.end()) {
pm::event_source_list.erase(event_data);
}
HMI_DEBUG("wm:pm", ">>>>>>>>>> FINISH CHECK POLICY");
return 0;
}
static int timerEvent(sd_event_source *source, uint64_t usec, void *data) {
int ret = checkPolicy(source, data);
return ret;
};
static int checkPolicyEntry(int event, uint64_t delay_ms, const char* role)
{
HMI_DEBUG("wm:pm", "Call");
HMI_DEBUG("wm:pm", "event:0x%x", event);
// Store event info
if (nullptr == role) {
pm::g_event_info_list[event] = std::string("");
}
else {
pm::g_event_info_list[event] = std::string(role);
}
if (0 == delay_ms) {
int ret = sd_event_add_defer(pm::event_loop, NULL,
&checkPolicy, new int(event));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_add_defer: errno:%d", ret);
pm::g_event_info_list.erase(event);
return -1;
}
}
else {
// Get current time
struct timespec time_spec;
clock_gettime(CLOCK_MONOTONIC, &time_spec);
// Calculate timer fired time
uint64_t usec = (time_spec.tv_sec * 1000000)
+ (time_spec.tv_nsec / 1000)
+ (delay_ms * 1000);
// Set timer
struct sd_event_source* event_source;
int ret = sd_event_add_time(pm::event_loop, &event_source, CLOCK_MONOTONIC, usec, 1,
&timerEvent, new int(event));
if (0 > ret) {
HMI_ERROR("wm:pm", "Faild to sd_event_add_time: errno:%d", ret);
pm::g_event_info_list.erase(event);
return -1;
}
// Store event source
pm::event_source_list[event] = event_source;
}
return 0;
}
void PolicyManager::registerCallback(CallbackTable callback) {
pm::callback.onStateTransitioned = callback.onStateTransitioned;
pm::callback.onError = callback.onError;
}
int PolicyManager::inputEvent(json_object* json_in) {
HMI_DEBUG("wm:pm", "Call");
// Check arguments
if (nullptr == json_in) {
HMI_ERROR("wm:pm", "Argument is NULL!!");
return -1;
}
// Get event from json_object
const char* event = this->getStringFromJson(json_in, "event");
int event_no = 0;
if (nullptr != event) {
// Convert name to number
event_no = this->eventname2no_[event];
HMI_DEBUG("wm:pm", "event(%s:%d)", event, event_no);
}
// Get role from json_object
const char* role = this->getStringFromJson(json_in, "role");
int category_no = 0;
if (nullptr != role) {
HMI_DEBUG("wm:pm", "role(%s)", role);
// Convert role to category
const char* category = this->role2category_[role].c_str();
if (0 == strcmp("", category)) {
HMI_ERROR("wm:pm", "Error!!");
return -1;
}
HMI_DEBUG("wm:pm", "category(%s)", category);
// Convert name to number
category_no = categoryname2no_[category];
HMI_DEBUG("wm:pm", "role(%s), category(%s:%d)", role, category, category_no);
}
// Get areat from json_object
const char* area = this->getStringFromJson(json_in, "area");
int area_no = 0;
if (nullptr != area) {
// Convert name to number
area_no = areaname2no_[area];
HMI_DEBUG("wm:pm", "area(%s:%d)", area, area_no);
}
// Check policy
checkPolicyEntry((event_no | category_no | area_no), 0, role);
return 0;
}
extern const char* kDefaultRoleDb;
int PolicyManager::loadRoleDb() {
HMI_DEBUG("wm:pm", "Call");
std::string file_name;
// Get afm application installed dir
char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
if (!afm_app_install_dir) {
HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
}
else {
file_name = std::string(afm_app_install_dir) + std::string("/etc/role.db");
}
// Load role.db
json_object* json_obj;
int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
if (0 > ret) {
HMI_ERROR("wm:pm", "Could not open role.db, so use default role information");
json_obj = json_tokener_parse(kDefaultRoleDb);
}
HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
json_object* json_roles;
if (!json_object_object_get_ex(json_obj, "roles", &json_roles)) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
int len = json_object_array_length(json_roles);
HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_roles));
json_object* json_tmp;
const char* category;
const char* roles;
const char* areas;
for (int i=0; i<len; i++) {
json_tmp = json_object_array_get_idx(json_roles, i);
category = this->getStringFromJson(json_tmp, "category");
roles = this->getStringFromJson(json_tmp, "role");
areas = this->getStringFromJson(json_tmp, "area");
if ((nullptr == category) || (nullptr == roles) || (nullptr == areas)) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
// Parse roles by '|'
std::vector<std::string> vct_roles;
vct_roles = this->parseString(std::string(roles), '|');
// Parse areas by '|'
std::vector<std::string> vct_areas;
vct_areas = this->parseString(std::string(areas), '|');
// Set role, category, default area
for (auto itr = vct_roles.begin(); itr != vct_roles.end(); ++itr) {
// Delete space from role and area name
std::string role = this->deleteSpace(*itr);
std::string area = this->deleteSpace(vct_areas[0]);
this->role2category_[role] = std::string(category);
this->role2defaultarea_[role] = area;
}
this->category2role_[std::string(category)] = std::string(roles);
}
// Check
HMI_DEBUG("wm:pm", "Check role2category_");
for (auto& x:this->role2category_){
HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
}
HMI_DEBUG("wm:pm", "Check role2defaultarea_");
for (auto& x:this->role2defaultarea_){
HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
}
HMI_DEBUG("wm:pm", "Check category2role_");
for (auto& x:this->category2role_){
HMI_DEBUG("wm:pm", "key:%s, val:%s", x.first.c_str(), x.second.c_str());
}
return 0;
}
extern const char* kDefaultLayoutDb;
int PolicyManager::loadLayoutDb() {
HMI_DEBUG("wm:lm", "Call");
// Get afm application installed dir
char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
HMI_DEBUG("wm:pm", "afm_app_install_dir:%s", afm_app_install_dir);
std::string file_name;
if (!afm_app_install_dir) {
HMI_ERROR("wm:pm", "AFM_APP_INSTALL_DIR is not defined");
}
else {
file_name = std::string(afm_app_install_dir) + std::string("/etc/layout.db");
}
// Load layout.db
json_object* json_obj;
int ret = this->inputJsonFilie(file_name.c_str(), &json_obj);
if (0 > ret) {
HMI_DEBUG("wm:pm", "Could not open layout.db, so use default layout information");
json_obj = json_tokener_parse(kDefaultLayoutDb);
}
HMI_DEBUG("wm:pm", "json_obj dump:%s", json_object_get_string(json_obj));
// Perse layouts
HMI_DEBUG("wm:pm", "Perse layouts");
json_object* json_cfg;
if (!json_object_object_get_ex(json_obj, "layouts", &json_cfg)) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
int len = json_object_array_length(json_cfg);
HMI_DEBUG("wm:pm", "json_cfg len:%d", len);
HMI_DEBUG("wm:pm", "json_cfg dump:%s", json_object_get_string(json_cfg));
const char* layout;
const char* role;
const char* category;
for (int i=0; i<len; i++) {
json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
layout = this->getStringFromJson(json_tmp, "name");
if (nullptr == layout) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
HMI_DEBUG("wm:pm", "> layout:%s", layout);
json_object* json_area_array;
if (!json_object_object_get_ex(json_tmp, "areas", &json_area_array)) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
int len_area = json_object_array_length(json_area_array);
HMI_DEBUG("wm:pm", "json_area_array len:%d", len_area);
HMI_DEBUG("wm:pm", "json_area_array dump:%s", json_object_get_string(json_area_array));
pm::LayoutState layout_state;
pm::AreaState area_state;
std::map<std::string, int> category_num;
for (int ctg_no = stm::gStmCategoryNoMin;
ctg_no <= stm::gStmCategoryNoMax; ctg_no++) {
const char* ctg_name = stm::gStmCategoryName[ctg_no];
category_num[ctg_name] = 0;
}
for (int j=0; j<len_area; j++) {
json_object* json_area = json_object_array_get_idx(json_area_array, j);
// Get area name
const char* area = this->getStringFromJson(json_area, "name");
if (nullptr == area) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
area_state.name = std::string(area);
HMI_DEBUG("wm:pm", ">> area:%s", area);
// Get app attribute of the area
category = this->getStringFromJson(json_area, "category");
if (nullptr == category) {
HMI_ERROR("wm:pm", "Parse Error!!");
return -1;
}
area_state.category = std::string(category);
category_num[category]++;
HMI_DEBUG("wm:pm", ">>> category:%s", category);
role = this->getStringFromJson(json_area, "role");
if (nullptr != role) {
// Role is NOT essential here
area_state.role = std::string(role);
}
else {
area_state.role = std::string("");
}
HMI_DEBUG("wm:pm", ">>> role:%s", role);
layout_state.area_list.push_back(area_state);
}
layout_state.name = layout;
layout_state.category_num = category_num;
pm::g_default_layouts[layout] = layout_state;
}
// initialize for none layout
pm::LayoutState none_layout_state;
memset(&none_layout_state, 0, sizeof(none_layout_state));
none_layout_state.name = "none";
pm::g_default_layouts["none"] = none_layout_state;
// Check
for(auto itr_layout = pm::g_default_layouts.begin();
itr_layout != pm::g_default_layouts.end(); ++itr_layout) {
HMI_DEBUG("wm:pm", ">>> layout:%s", itr_layout->first.c_str());
for (auto itr_area = itr_layout->second.area_list.begin();
itr_area != itr_layout->second.area_list.end(); ++itr_area) {
HMI_DEBUG("wm:pm", ">>> >>> area :%s", itr_area->name.c_str());
HMI_DEBUG("wm:pm", ">>> >>> category:%s", itr_area->category.c_str());
HMI_DEBUG("wm:pm", ">>> >>> role :%s", itr_area->role.c_str());
#if 0
for (auto itr_role = itr_area->second.begin();
itr_role != itr_area->second.end(); ++itr_role) {
HMI_DEBUG("wm:pm", ">>> >>> >>> attribute:%s, name:%s",
itr_role->first.c_str(), itr_role->second.c_str());
}
#endif
}
}
// Release json_object
json_object_put(json_obj);
return 0;
}
// TODO:
// This function will be removed because json_helper has same function.
// json_helper should be library.
const char* PolicyManager::getStringFromJson(json_object* obj, const char* key) {
if ((nullptr == obj) || (nullptr == key)) {
HMI_ERROR("wm:pm", "Argument is nullptr!!!");
return nullptr;
}
json_object* tmp;
if (!json_object_object_get_ex(obj, key, &tmp)) {
HMI_DEBUG("wm:pm", "Not found key \"%s\"", key);
return nullptr;
}
return json_object_get_string(tmp);
}
// TODO:
// This function will be removed because json_helper has same function.
// json_helper should be library.
int PolicyManager::inputJsonFilie(const char* file, json_object** obj) {
const int input_size = 128;
int ret = -1;
if ((nullptr == file) || (nullptr == obj)) {
HMI_ERROR("wm:jh", "Argument is nullptr!!!");
return ret;
}
HMI_DEBUG("wm:jh", "Input file: %s", file);
// Open json file
FILE *fp = fopen(file, "rb");
if (nullptr == fp) {
HMI_ERROR("wm:jh", "Could not open file");
return ret;
}
// Parse file data
struct json_tokener *tokener = json_tokener_new();
enum json_tokener_error json_error;
char buffer[input_size];
int block_cnt = 1;
while (1) {
size_t len = fread(buffer, sizeof(char), input_size, fp);
*obj = json_tokener_parse_ex(tokener, buffer, len);
if (nullptr != *obj) {
HMI_DEBUG("wm:jh", "File input is success");
ret = 0;
break;
}
json_error = json_tokener_get_error(tokener);
if ((json_tokener_continue != json_error)
|| (input_size > len)) {
HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
(input_size * block_cnt), json_tokener_error_desc(json_error));
HMI_ERROR("wm:jh", "\n%s", buffer);
*obj = nullptr;
break;
}
block_cnt++;
}
// Close json file
fclose(fp);
// Free json_tokener
json_tokener_free(tokener);
return ret;
}
std::vector<std::string> PolicyManager::parseString(std::string str, char delimiter) {
// Parse string by delimiter
std::vector<std::string> vct;
std::stringstream ss{str};
std::string buf;
while (std::getline(ss, buf, delimiter)) {
if (!buf.empty()) {
vct.push_back(buf);
}
}
return vct;
}
std::string PolicyManager::deleteSpace(std::string str) {
std::string ret = str;
size_t pos;
while ((pos = ret.find_first_of(" ")) != std::string::npos) {
ret.erase(pos, 1);
}
return ret;
}
const char* kDefaultRoleDb = "{ \
\"roles\":[ \
{ \
\"category\": \"homescreen\", \
\"role\": \"homescreen\", \
\"area\": \"full\", \
}, \
{ \
\"category\": \"map\", \
\"role\": \"map\", \
\"area\": \"full | normal | split.main\", \
}, \
{ \
\"category\": \"general\", \
\"role\": \"poi | music | video | browser | sdl | settings | mixer | radio | hvac | dashboard | debug\", \
\"area\": \"normal\", \
}, \
{ \
\"category\": \"phone\", \
\"role\": \"phone\", \
\"area\": \"normal\", \
}, \
{ \
\"category\": \"splitable\", \
\"role\": \"splitable1 | splitable2\", \
\"area\": \"normal | split.main | split.sub\", \
}, \
{ \
\"category\": \"popup\", \
\"role\": \"popup\", \
\"area\": \"on_screen\", \
}, \
{ \
\"category\": \"system_alert\", \
\"role\": \"system_alert\", \
\"area\": \"on_screen\", \
}, \
{ \
\"category\": \"tbt\", \
\"role\": \"tbt\", \
\"area\": \"hud\", \
} \
] \
}";
const char* kDefaultLayoutDb = "{ \
\"layouts\": [ \
{ \
\"name\": \"pu\", \
\"layer\": \"on_screen\", \
\"areas\": [ \
{ \
\"name\": \"pop_up\", \
\"role\": \"incomming_call\" \
} \
] \
}, \
{ \
\"name\": \"sa\", \
\"layer\": \"on_screen\", \
\"areas\": [ \
{ \
\"name\": \"system_alert\", \
\"role\": \"system_alert\" \
} \
] \
}, \
{ \
\"name\": \"m1\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"normal\", \
\"role\": \"map\" \
} \
] \
}, \
{ \
\"name\": \"m2\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"split.main\", \
\"role\": \"map\" \
}, \
{ \
\"name\": \"split.sub\", \
\"category\": \"hvac\" \
} \
] \
}, \
{ \
\"name\": \"mf\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"full\", \
\"role\": \"map\" \
} \
] \
}, \
{ \
\"name\": \"s1\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"normal\", \
\"category\": \"splitable\" \
} \
] \
}, \
{ \
\"name\": \"s2\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"split.main\", \
\"category\": \"splitable\" \
}, \
{ \
\"name\": \"split.sub\", \
\"category\": \"splitable\" \
} \
] \
}, \
{ \
\"name\": \"g\", \
\"layer\": \"apps\", \
\"areas\": [ \
{ \
\"name\": \"normal\", \
\"category\": \"general\" \
} \
] \
}, \
{ \
\"name\": \"hs\", \
\"layer\": \"homescreen\", \
\"areas\": [ \
{ \
\"name\": \"full\", \
\"role\": \"homescreen\" \
} \
] \
} \
], \
\"areas\": [ \
{ \
\"name\": \"normal\", \
\"rect\": { \
\"x\": 0, \
\"y\": 218, \
\"w\": 1080, \
\"h\": 1488 \
} \
}, \
{ \
\"name\": \"split.main\", \
\"rect\": { \
\"x\": 0, \
\"y\": 218, \
\"w\": 1080, \
\"h\": 744 \
} \
}, \
{ \
\"name\": \"split.sub\", \
\"rect\": { \
\"x\": 0, \
\"y\": 962, \
\"w\": 1080, \
\"h\": 744 \
} \
}, \
{ \
\"name\": \"full\", \
\"rect\": { \
\"x\": 0, \
\"y\": 0, \
\"w\": 1080, \
\"h\": 1920 \
} \
}, \
{ \
\"name\": \"pop_up\", \
\"rect\": { \
\"x\": 0, \
\"y\": 640, \
\"w\": 1080, \
\"h\": 640 \
} \
}, \
{ \
\"name\": \"system_alert\", \
\"rect\": { \
\"x\": 0, \
\"y\": 640, \
\"w\": 1080, \
\"h\": 640 \
} \
} \
] \
}";
|