RDK Documentation (Open Sourced RDK Components)
si_cache_app.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file si_cache_app.cpp
22  * @brief SICache is used for tuning and showing video.
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32 
33 #include "simgr.h"
34 
35 using namespace std;
36 
37 
38 /**
39 * @defgroup rmf_tools rmf tools
40 *
41 * @defgroup SI_CACHE Generate SI cache
42 * @ingroup rmf_tools
43 *
44 * @defgroup SI_CACHE_TYPES SI Cache Data Types
45 * @ingroup SI_CACHE
46 *
47 * @defgroup SI_CACHE_APIS SI Cache APIs
48 * @ingroup SI_CACHE
49 *
50 **/
51 
52 
53 /**
54  * @addtogroup SI_CACHE_TYPES
55  * @{
56  */
57 
58 #define MODE_XML_TO_BINARY "xtob"
59 #define MODE_BINARY_TO_XML "btox"
60 #define MODE_DUMP_TO_CONSOLE "dump"
61 
62 /**
63  * @}
64  */
65 
66 /**
67  * @addtogroup SI_CACHE_APIS
68  * @{
69  */
70 
71 /**
72  * @brief This function displays the brief usage message for the tool.
73  *
74  * It includes the usage of the tool, description, options and examples.
75 
76  * @param[in] app_name Indicates the options for the tools
77  */
78 void printUsage(char *app_name)
79 {
80  printf("\nRDK SI CACHE CONVERTER \t- User's Manual\n");
81  printf("\nUSAGE: \n\n\t%s [-mode MODE] [-input INPUT_FILE] [-output OUTPUT_FILE] [-sioutput SIOUTPUT_FILE] [-snsoutput SNSOUTPUT_FILE] [-help] [-version]\n", app_name);
82  printf("\nDESCRIPTION:\n\n\tRDK SI Cache Converter is a tool to convert the the given SI Cache file from binary to XML format or vice versa based on the specified mode. This tool helps to enhance the existing channel map\n\tto support the development activities at locations where Comcast head-end connectivity is not available. This tool uses Libxml2 XML toolkit (http://xmlsoft.org/) for all XML related operations.\n\nOPTIONS:\n\n");
83  printf("\t-mode\t\t: Operation mode. It can be one of the following:\n");
84  printf("\t\t\t\txtob - Generate SI Cache binaries from the given XML file\n");
85  printf("\t\t\t\tbtox - Generate SI Cache XML file from the given binary file\n");
86  printf("\t\t\t\tdump - Dump channel map from the given binary file to console\n");
87  printf("\t-input \t: Input file. It can be an XML file or a binary SI Cache file depending on the operation mode\n");
88  printf("\t-output \t: Output file. It can be a binary SI Cache file or an XML file depending on the operation mode\n");
89  printf("\t-sioutput \t: Binary SI Cache file to be generated. To use this option, the mode should be set to 'xtob'\n");
90  printf("\t-snsoutput\t: Binary SNS Cache file to be generated. To use this option, the mode should be set to 'xtob'\n");
91  printf("\t-version\t: Prints the tool's version details\n");
92  printf("\t-help\t\t: Prints this user's manual\n");
93  printf("\nEXAMPLES:\n");
94  printf("\n(i) Command to generate binary SI cache from given XML file:\n");
95  printf("\t%s -mode xtob -input si.xml -output SICache\n", app_name);
96  printf("\n(ii) Command to generate both binary SI cache and binary SNS cache from given XML file:\n");
97  printf("\t%s -mode xtob -input si.xml -sioutput SICache -snsoutput SNSCache \n",app_name);
98  printf("\n(iii) Command to generate XML file from binary SI cache:\n");
99  printf("\t%s -mode btox -input SICache -output si.xml\n", app_name);
100  printf("\n(iv) Command to dump channel map from binary SI cache to console:\n");
101  printf("\t%s -mode dump -input SICache \n\n", app_name);
102 
103 }
104 
105 /**
106  * @brief This API scans parameters from the command line.
107  *
108  * @param[in] argc Number of parameters
109  * @param[in] argv Parameter list
110  * @param[in] mode Operation mode. It can be one of the following:
111  * xtob - Generate SI Cache binaries from the given XML file
112  * btox - Generate SI Cache XML file from the given binary file
113  * dump - Dump channel map from the given binary file to console
114  * @param[in] input_file Input file. It can be an XML file or a binary SI Cache file depending on the operation mode
115  * @param[out] output_file Output file. It can be a binary SI Cache file or an XML file depending on the operation mode
116  * @param[out] output_file_sns Binary SNS Cache file to be generated. To use this option, the mode should be set to 'xtob'.
117  *
118  * @return Returns status of the operation.
119  * @retval True on sucess, False on failure.
120  */
121 bool processCommandLine(int argc, char** argv, string& mode, string& input_file, string& output_file, string& output_file_sns)
122 {
123  int idx;
124 
125  if(argc<=1)
126  {
127  printUsage(argv[0]);
128  return false;
129  }
130 
131  for(int i=1; i < argc; ++i)
132  {
133  if(std::string(argv[i]) == "-help")
134  {
135  printUsage(argv[0]);
136  return false;
137  }
138  else if(std::string(argv[i]) == "-version")
139  {
140  printf("RDK SI CACHE CONVERTER - %s\nCopyright 2016 RDK Management, Licensed under the Apache License,Version 2.0\n", PACKAGE_VERSION);
141  return false;
142  }
143  else if(std::string(argv[i]) == "-mode")
144  {
145  if(i < argc-1 && argv[i+1][0] != '-')
146  mode = argv[++i];
147  //verify the mode matches one of our modes
148  if((mode != MODE_XML_TO_BINARY) && (mode != MODE_BINARY_TO_XML) && (mode != MODE_DUMP_TO_CONSOLE))
149  {
150  printf("argv[0]: invalid mode option '%s'. Try '%s -help' for more info.\n", mode.c_str(),argv[0]);
151  return false;
152  }
153  if(i+1 > idx)
154  idx = i+1;
155  }
156  else if(std::string(argv[i]) == "-input")
157  {
158  if(i < argc-1 && argv[i+1][0] != '-')
159  input_file = argv[++i];
160  }
161  else if((std::string(argv[i]) == "-output") && (mode != MODE_DUMP_TO_CONSOLE))
162  {
163  if(i < argc-1 && argv[i+1][0] != '-')
164  output_file = argv[++i];
165  }
166  else if((std::string(argv[i]) == "-sioutput") && (mode == MODE_XML_TO_BINARY))
167  {
168  if(i < argc-1 && argv[i+1][0] != '-')
169  output_file = argv[++i];
170  }
171  else if((std::string(argv[i]) == "-snsoutput") && (mode == MODE_XML_TO_BINARY))
172  {
173  if(i < argc-1 && argv[i+1][0] != '-')
174  output_file_sns = argv[++i];
175  }
176  else
177  {
178  printf("argv[0]: invalid option '%s'. Try '%s -help' for more info.\n", argv[i], argv[0]);
179  return false;
180  }
181 
182  }
183 
184  if(mode.empty())
185  {
186  printf("Mode: unset \n");
187  return false;
188  }
189 
190  if(input_file.empty())
191  {
192  printf("Input file: unset \n");
193  return false;
194  }
195 
196  if(output_file.empty() && (mode != MODE_DUMP_TO_CONSOLE))
197  {
198  printf("Output file: unset \n");
199  return false;
200  }
201 
202  return true;
203 }
204 
205 int main(int argc, char ** argv)
206 {
207  string mode;
208  string input_file;
209  string output_file;
210  string output_sns_file;
211  SiManager *pSiMgr = NULL;
212  bool ret = FALSE;
213 
214 
215  if(!processCommandLine(argc, argv, mode, input_file, output_file, output_sns_file))
216  return -1;
217 
218  if(mode == MODE_XML_TO_BINARY)
219  {
220  pSiMgr = new SiManager();
221 
222  if(pSiMgr->Load(input_file, SI_FILETYPE_XML))
223  {
224  if(output_sns_file.empty())
225  ret = pSiMgr->GenerateSICache(output_file);
226  else
227  ret = pSiMgr->GenerateSICache(output_file, output_sns_file);
228  }
229  }
230  else if(mode == MODE_BINARY_TO_XML)
231  {
232  pSiMgr = new SiManager();
233 
234  if(pSiMgr->Load(input_file, SI_FILETYPE_BINARY))
235  {
236  ret = pSiMgr->GenerateXML(output_file);
237  }
238  }
239  else if(mode == MODE_DUMP_TO_CONSOLE)
240  {
241  pSiMgr = new SiManager();
242 
243  if(pSiMgr->Load(input_file, SI_FILETYPE_BINARY))
244  {
245  pSiMgr->DumpChannelMap();
246  }
247 
248  }
249 
250  return ret;
251 }
252 
253 /**
254  * @}
255  */
256 
257 
processCommandLine
bool processCommandLine(int argc, char **argv, string &mode, string &input_file, string &output_file, string &output_file_sns)
This API scans parameters from the command line.
Definition: si_cache_app.cpp:121
SiManager::DumpChannelMap
void DumpChannelMap()
Dump channel map information from binary SI cache to console.
Definition: simgr.cpp:919
SiManager::GenerateXML
bool GenerateXML(string sixmlFileName)
Function to generate XML file(sidb.xml).
Definition: simgr.cpp:1005
SiManager::Load
bool Load(string file_name, int mode)
This API is to load program data from XML file, if the operation mode selected is XML to Binary....
Definition: simgr.cpp:885
SiManager
Definition: simgr.h:206
SiManager::GenerateSICache
bool GenerateSICache(string sidbFileName)
Generates binary SI cache from the XML file.
Definition: simgr.cpp:939
printUsage
void printUsage(char *app_name)
This function displays the brief usage message for the tool.
Definition: si_cache_app.cpp:78