Remove unused directories and files in video_in_hal
[staging/basesystem.git] / service / system / interface_unified / library / src / ss_last_to_order.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <agl_wakeup_order.h>
21 #include <list>
22 #include <fstream>
23 #include <iostream>
24 #include <stdexcept>
25 #include <string>
26 #include "system_service/ss_last_to_order.h"
27 #include "ss_last_to_order_local.h"
28
29 #include "system_service/ss_templates.h"
30 #include "ss_system_if_interfaceunifiedlog.h"
31 #define SCO_STATE_LOG(...) FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, __VA_ARGS__);
32 #define SCO_ERROR_LOG(...) FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, __VA_ARGS__);
33 #define SCO_ASSERT(x)      SS_ASERT(x);
34
35 //************* CONFIG FILE Format ****************
36 #define ORDERNAME_MAX 64
37 #define CONTENT_NAME_MAX 128
38 #pragma pack(push, 1)
39 typedef struct {
40   char    lastinfo[SS_CCAT_MAX][CONTENT_NAME_MAX];
41   char    orderName[ORDERNAME_MAX];
42 }SS_LASTINFO_TO_OEDER_PACKED_t;
43 #pragma pack(pop)
44 //*************************************************
45
46
47 typedef struct {
48   SS_LAST_INFO_t lastinfo;
49   std::string         orderName;
50 }SS_LASTINFO_TO_OEDER_t;
51
52
53
54 //****************************
55 //  load configuration file
56 //****************************
57 static int
58 loadConfig(std::string &cfgPath, std::list<SS_LASTINFO_TO_OEDER_t> *p_cfgList) throw() {  // NOLINT (runtime/references)
59   int ret = 0;
60
61   try {
62     std::ifstream fin(cfgPath.c_str(), std::ios::in | std::ios::binary);
63     if (!fin) {
64       SCO_ERROR_LOG("cfg file error %s", cfgPath.c_str());
65       throw std::domain_error("open error");
66     }
67     char magic[4];
68
69     fin.read(reinterpret_cast<char*>(&magic), sizeof(magic));
70     if (fin.fail()) {
71       throw std::domain_error("read magic");
72     }
73
74     if (memcmp(magic, "CTOO", sizeof(magic)) != 0) {
75       throw std::domain_error("magic error");
76     }
77
78     uint32_t numOfElement = 0;
79     fin.read(reinterpret_cast<char*>(&numOfElement), sizeof(numOfElement));
80     if ( fin.fail() ) {
81       throw std::domain_error("read numOfElement");
82     }
83
84     for (uint32_t ii = 0; ii < numOfElement; ii++) {
85       SS_LASTINFO_TO_OEDER_PACKED_t element;
86       fin.read(reinterpret_cast<char*>(&element), sizeof(element));
87       if ( fin.fail() ) {
88         throw std::domain_error("read element");
89       }
90       SS_LASTINFO_TO_OEDER_t tmp;
91       tmp.lastinfo[SS_CCAT_F_VIDEO] = element.lastinfo[SS_CCAT_F_VIDEO];
92       tmp.lastinfo[SS_CCAT_F_SUB_VIDEO] = element.lastinfo[SS_CCAT_F_SUB_VIDEO];
93       tmp.lastinfo[SS_CCAT_F_AUDIO] = element.lastinfo[SS_CCAT_F_AUDIO];
94       tmp.lastinfo[SS_CCAT_R_VIDEO] = element.lastinfo[SS_CCAT_R_VIDEO];
95       tmp.lastinfo[SS_CCAT_R_AUDIO] = element.lastinfo[SS_CCAT_R_AUDIO];
96       tmp.orderName = element.orderName;
97       p_cfgList->push_back(tmp);
98     }
99     SCO_STATE_LOG("%s is loaded", cfgPath.c_str());
100   } catch (std::exception &e) {
101     if (ret == 0) ret = -1;
102     SCO_ERROR_LOG("%s", e.what());
103     SCO_ASSERT(0);
104   } catch (...) {
105     SCO_ASSERT(0);
106     ret = -1;
107   }
108   return ret;
109 }
110
111
112
113 static
114 bool
115 isPassCondition(SS_CONT_CATEGORY_t cat, SS_LAST_INFO_t &lastInfo, SS_LASTINFO_TO_OEDER_t &tblElement) {  // NOLINT (runtime/references)
116   std::string &tblStr = tblElement.lastinfo[cat];
117
118   if (tblStr != "EMPTY") {
119     //Category with content specification
120     SS_LAST_INFO_t::iterator iteCont = lastInfo.find(cat);
121     if (iteCont == lastInfo.end() || iteCont->second != tblStr) {
122       return false;
123     }
124   }
125   return true;
126 }
127
128
129
130 //****************************
131 //  SS_ConvLastInfoToOrder
132 //****************************
133 EFrameworkunifiedStatus
134 SS_ConvLastInfoToOrder(SS_LAST_INFO_t &curInfo, std::string &order, const char* p_cfgPath/* = NULL*/) {  // NOLINT (runtime/references)
135   std::string cfgPath = (p_cfgPath) ? p_cfgPath : SS_LAST_TO_ORDER_CONF_PATH;
136
137   static std::list<SS_LASTINFO_TO_OEDER_t> s_convList;
138   static std::string s_readCfgPath = "__default__";
139   bool isHit = false;
140
141   // If the same CONFIG, do not READ after the second.
142   if (s_readCfgPath != cfgPath) {
143     int ret;
144     s_convList.clear();
145
146     ret = loadConfig(cfgPath, &s_convList);
147     if (ret != 0) { goto ERROR; }
148     s_readCfgPath = cfgPath;
149
150 #if 0  // DEBUG
151     for (std::list<SS_LASTINFO_TO_OEDER_PACKED_t>::iterator tblElt = s_convList.begin();
152       tblElt != s_convList.end();
153       tblElt++) {
154       SCO_STATE_LOG("FV:%s FSV:%s FA:%s RV:%s RA:%s O:%s",
155         tblElt->lastinfo[SS_CCAT_F_VIDEO], tblElt->lastinfo[SS_CCAT_F_SUB_VIDEO], tblElt->lastinfo[SS_CCAT_F_AUDIO],
156         tblElt->lastinfo[SS_CCAT_R_VIDEO], tblElt->lastinfo[SS_CCAT_R_AUDIO], tblElt->orderName);
157     }
158 #endif
159   }
160
161   for (std::list<SS_LASTINFO_TO_OEDER_t>::iterator tblElt = s_convList.begin();
162       tblElt != s_convList.end();
163       tblElt++) {
164     if      (isPassCondition(SS_CCAT_F_VIDEO    , curInfo, *tblElt) == false) {
165       continue;
166     } else if (isPassCondition(SS_CCAT_F_SUB_VIDEO, curInfo, *tblElt) == false) {
167       continue;
168     } else if (isPassCondition(SS_CCAT_F_AUDIO    , curInfo, *tblElt) == false) {
169       continue;
170     } else if (isPassCondition(SS_CCAT_R_VIDEO    , curInfo, *tblElt) == false) {
171       continue;
172     } else if (isPassCondition(SS_CCAT_R_AUDIO    , curInfo, *tblElt) == false) {
173       continue;
174     }
175
176     isHit = true;
177     order = tblElt->orderName;
178     break;
179   }
180   if (!isHit) {
181     order = WON_DEFAULT;
182   }
183   return eFrameworkunifiedStatusOK;
184 ERROR:
185   return eFrameworkunifiedStatusFail;
186 }