summaryrefslogtreecommitdiff
path: root/src/antpm-downloader.cpp
blob: 1bdea959dfbabf4435c4135a3ed058e0c511d2f9 (plain)
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
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
// ***** BEGIN LICENSE BLOCK *****
//////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2014 RALOVICH, Kristóf                            //
//                                                                      //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 3 of the License, or    //
// (at your option) any later version.                                  //
//                                                                      //
// This program is distributed in the hope that it will be useful,      //
// but WITHOUT ANY WARRANTY; without even the implied warranty of       //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        //
// GNU General Public License for more details.                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
// ***** END LICENSE BLOCK *****

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

#include "AntMessage.hpp"
#include "AntFr310XT.hpp"
#include "AntFr405.hpp"
#include "common.hpp"
#include "Log.hpp"

#include <boost/program_options.hpp>
#include <boost/bind.hpp>


#ifdef _WIN32
# include <Windows.h>
#elif defined(__linux)
# include <signal.h>
#endif

namespace po = boost::program_options;
using namespace std;
using namespace antpm;


namespace antpm
{

template<>
Log*
ClassInstantiator<Log>::instantiate()
{
  mkDirNoLog(getConfigFolder().c_str());
  std::string l=getConfigFolder() + "/antpm_" + getDateString() + ".txt";
  return new Log(l.c_str());
}

}



boost::function<void(void)> stopFunc;

static
void
stopIt()
{
  if(stopFunc)
    stopFunc();
}

#ifdef _WIN32
BOOL CtrlHandler( DWORD fdwCtrlType )
{
  switch( fdwCtrlType )
  {
    // Handle the CTRL-C signal.
    case CTRL_C_EVENT:
      printf( "Ctrl-C event\n\n" );
      //Beep( 750, 300 );
      stopIt();
      return( TRUE );

      // CTRL-CLOSE: confirm that the user wants to exit.
    case CTRL_CLOSE_EVENT:
      //Beep( 600, 200 );
      printf( "Ctrl-Close event\n\n" );
      stopIt();
      return( TRUE );

      // Pass other signals to the next handler.
    case CTRL_BREAK_EVENT:
      //Beep( 900, 200 );
      printf( "Ctrl-Break event\n\n" );
      stopIt();
      return FALSE;

    case CTRL_LOGOFF_EVENT:
      //Beep( 1000, 200 );
      printf( "Ctrl-Logoff event\n\n" );
      stopIt();
      return FALSE;

    case CTRL_SHUTDOWN_EVENT:
      //Beep( 750, 500 );
      printf( "Ctrl-Shutdown event\n\n" );
      stopIt();
      return FALSE;

    default:
      return FALSE;
  }
}
#elif defined(__linux)
void my_handler(int s)
{
  printf("Caught signal %d\nantpm teardown initiated\n\n",s);
  stopIt();
}
#endif


int
main(int argc, char** argv)
{
  printf("press Ctrl-C to exit\n");
#ifdef _WIN32
  if( !SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) )
  {
    printf( "\nERROR: Could not set control handler...");
  }
#elif defined(__linux)
  signal(SIGINT, my_handler);
#endif

  antpm::Log::instance()->addSink(std::cout);
#ifdef NDEBUG
  antpm::Log::instance()->setLogReportingLevel(antpm::LOG_INF);
#else
  antpm::Log::instance()->setLogReportingLevel(antpm::LOG_DBG);
#endif

  // Declare the supported options.
  bool     pairing        = false;
  bool     dirOnly        = false;
  uint16_t dlFileIdx      = 0x0000;
  uint16_t eraseFileIdx   = 0x0000;
  int      verbosityLevel = antpm::Log::instance()->getLogReportingLevel();
  po::options_description desc("Allowed options");
  std::vector<const char*> args(argv, argv+argc);
  po::variables_map vm;
  try
  {
    desc.add_options()
    ("help,h",                                                                    "produce help message")
    ("pairing,P", po::value<bool>(&pairing)->zero_tokens()->implicit_value(true), "Force pairing first")
    ("dir-only",  po::value<bool>(&dirOnly)->zero_tokens()->implicit_value(true), "Download and list device directory")
    ("download,D",po::value<std::string>(),                                       "Download a single file (hex id e.g. 0x12FB) from device")
    ("erase",     po::value<std::string>(),                                       "Erase a single file (hex id e.g. 0x12FB) from device")
    ("v",                po::value<int>(&verbosityLevel),       "Adjust verbosity level, varies in [1, 6]")
    ("version,V",                                               "Print version information")
    ;

    //po::parsed_options parsed = po::parse_command_line(argc, argv, desc);
    po::parsed_options parsed = po::command_line_parser(argc, argv).options(desc).run();
    po::store(parsed, vm);

    if(vm.find("download")!=vm.end())
    {
      std::stringstream interpreter;
      interpreter << std::hex << vm["download"].as<std::string>();
      interpreter >> dlFileIdx;
    }
    if(vm.find("erase")!=vm.end())
    {
      std::stringstream interpreter;
      interpreter << std::hex << vm["erase"].as<std::string>();
      interpreter >> eraseFileIdx;
    }

    po::notify(vm);
  }
  catch(po::error& error)
  {
    cerr << error.what() << "\n";
    cerr << desc << "\n";
    return EXIT_FAILURE;
  }
  catch(boost::exception& e)
  {
    cerr << boost::diagnostic_information(e) << std::endl;
    return EXIT_FAILURE;
  }
  catch(std::exception& ex)
  {
    cerr << ex.what() << "\n";
    cerr << desc << "\n";
    return EXIT_FAILURE;
  }

  if(vm.count("version") || vm.count("V"))
  {
    cout << argv[0] << " " << antpm::getVersionString() << "\n";
    return EXIT_SUCCESS;
  }

  if(vm.count("v"))
  {
    if(verbosityLevel >= antpm::LOG_ERR && verbosityLevel <= antpm::LOG_DBG3)
    {
      antpm::Log::instance()->setLogReportingLevel(static_cast<antpm::LogLevel>(verbosityLevel));
    }
  }

  if(vm.count("help"))
  {
    logger() << desc << "\n";
    return EXIT_SUCCESS;
  }
  LOG_VAR5(pairing, dirOnly, dlFileIdx, eraseFileIdx, verbosityLevel);

  LOG(antpm::LOG_DBG) << argv[0] << " " << antpm::getVersionString() << "\n";
  for(int i = 0; i < argc; i++)
  {
    LOG(antpm::LOG_DBG2) << "\targv[" << i << "]\t\"" << argv[i] << "\"" << endl;
  }

  if(false && isAntpm405Override())
  {
    logger() << "\n\nApplying ANTPM_405 override mode!\n\n\n";
    AntFr405 watch2;
    stopFunc = boost::bind(&AntFr405::stopAsync, &watch2);
    {
      watch2.setModeDownloadAll();
      if(pairing) watch2.setModeForcePairing();
      if(dirOnly) watch2.setModeDirectoryListing();
      else if(dlFileIdx!=0x0000) watch2.setModeDownloadSingleFile(dlFileIdx);
      else if(eraseFileIdx!=0x000) watch2.setModeEraseSingleFile(eraseFileIdx);

      watch2.start();


      watch2.stop();
    }
  }
  else
  {
    AntFr310XT watch2;
    stopFunc = boost::bind(&AntFr310XT::stopAsync, &watch2);
    {
      watch2.setModeDownloadAll();
      if(pairing) watch2.setModeForcePairing();
      if(dirOnly) watch2.setModeDirectoryListing();
      else if(dlFileIdx!=0x0000) watch2.setModeDownloadSingleFile(dlFileIdx);
      else if(eraseFileIdx!=0x000) watch2.setModeEraseSingleFile(eraseFileIdx);

      watch2.run();


      //watch2.stop();
    }
  }



  return EXIT_SUCCESS;
}