aboutsummaryrefslogtreecommitdiff
path: root/tools/HardwareConfigure/HardwareConfigure.cpp
blob: 85b127abda9179960839337183810777b1f341d8 (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
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
/*
 *      Copyright (C) 2005-2008 Team XBMC
 *      http://www.xbmc.org
 *
 *  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 2, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "HardwareConfigure.h"
#include <libhal-storage.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

//#define HAL_HANDLEMOUNT

bool CHalManager::NewMessage;
DBusError CHalManager::m_Error;

/* A Removed device, It isn't possible to make a LibHalVolume from a removed device therefor
   we catch the UUID from the udi on the removal */
void CHalManager::DeviceRemoved(LibHalContext *ctx, const char *udi)
{
  NewMessage = true;
  printf("HAL: Device (%s) Removed\n", udi);
//  g_HalManager.RemoveDevice(udi);
}

void CHalManager::DeviceNewCapability(LibHalContext *ctx, const char *udi, const char *capability)
{
  NewMessage = true;
  printf("HAL: Device (%s) gained capability %s\n", udi, capability);
  g_HalManager.ParseDevice(udi);
}

void CHalManager::DeviceLostCapability(LibHalContext *ctx, const char *udi, const char *capability)
{
  NewMessage = true;
  printf("HAL: Device (%s) lost capability %s\n", udi, capability);
  g_HalManager.ParseDevice(udi);
}

/* HAL Property modified callback. If a device is mounted. This is called. */
void CHalManager::DevicePropertyModified(LibHalContext *ctx, const char *udi, const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
{
  NewMessage = true;
  printf("HAL: Device (%s) Property %s modified\n", udi, key);
  g_HalManager.ParseDevice(udi);
}

void CHalManager::DeviceCondition(LibHalContext *ctx, const char *udi, const char *condition_name, const char *condition_details)
{
  printf("HAL: Device (%s) Condition %s | %s\n", udi, condition_name, condition_details);
  NewMessage = true;
  g_HalManager.ParseDevice(udi);
}

/* HAL Device added. This is before mount. And here is the place to mount the volume in the future */
void CHalManager::DeviceAdded(LibHalContext *ctx, const char *udi)
{
  NewMessage = true;
  printf("HAL: Device (%s) Added\n", udi);
  g_HalManager.ParseDevice(udi);
}

CHalManager g_HalManager;

/* Iterate through all devices currently on the computer. Needed mostly at startup */
void CHalManager::GenerateGDL()
{
  if (m_Context == NULL)
    return;

  char **GDL;
  int i = 0;
  printf("HAL: Generating global device list\n");
  GDL = libhal_get_all_devices(g_HalManager.m_Context, &i, &m_Error);

  if (!ReadAvailableRemotes())
    return;

  for (i = 0; GDL[i]; i++)
  {
    if (ParseDevice(GDL[i]))
      break;
  }

  printf("HAL: Generated global device list, found %i\n", i);
}

CHalManager::CHalManager()
{
}

// Shutdown the connection and free the context
CHalManager::~CHalManager()
{
  if (m_Context != NULL)
    libhal_ctx_shutdown(m_Context, NULL);
  if (m_Context != NULL)
    libhal_ctx_free(m_Context);

  if (m_DBusSystemConnection != NULL)
  {
    dbus_connection_unref(m_DBusSystemConnection);
    m_DBusSystemConnection = NULL;
  }
  dbus_error_free(&m_Error); // Needed?
}

// Initialize
void CHalManager::Initialize(const char *LircConfPath)
{
  printf("HAL: Starting initializing\n");
  strcpy(m_LircConfPath, LircConfPath);

  g_HalManager.m_Context = g_HalManager.InitializeHal();
  if (g_HalManager.m_Context == NULL)
  {
    printf("HAL: no Hal context\n");
    return;
  }

  GenerateGDL();

  printf("HAL: Sucessfully initialized\n");
}

// Initialize basic DBus connection
bool CHalManager::InitializeDBus()
{
  if (m_DBusSystemConnection != NULL)
    return true;

  dbus_error_init (&m_Error);
  if (m_DBusSystemConnection == NULL && !(m_DBusSystemConnection = dbus_bus_get (DBUS_BUS_SYSTEM, &m_Error)))
  {
    printf("DBus: Could not get system bus: %s\n", m_Error.message);
    dbus_error_free (&m_Error);
  }

  if (m_DBusSystemConnection != NULL)
    return true;
  else
    return false;
}

// Initialize basic HAL connection
LibHalContext *CHalManager::InitializeHal()
{
  LibHalContext *ctx;
  char **devices;
  int nr;

  if (!InitializeDBus())
    return NULL;

  if (!(ctx = libhal_ctx_new()))
  {
    printf("HAL: failed to create a HAL context!\n");
    return NULL;
  }

  if (!libhal_ctx_set_dbus_connection(ctx, m_DBusSystemConnection))
    printf("HAL: Failed to connect with dbus\n");

  libhal_ctx_set_device_added(ctx, DeviceAdded);
  libhal_ctx_set_device_removed(ctx, DeviceRemoved);
  libhal_ctx_set_device_new_capability(ctx, DeviceNewCapability);
  libhal_ctx_set_device_lost_capability(ctx, DeviceLostCapability);
  libhal_ctx_set_device_property_modified(ctx, DevicePropertyModified);
  libhal_ctx_set_device_condition(ctx, DeviceCondition);

  if (!libhal_device_property_watch_all(ctx, &m_Error))
  {
    printf("HAL: Failed to set property watch %s\n", m_Error.message);
    dbus_error_free(&m_Error);
    libhal_ctx_free(ctx);
    return NULL;
  }

  if (!libhal_ctx_init(ctx, &m_Error))
  {
    printf("HAL: Failed to initialize hal context: %s\n", m_Error.message);
    dbus_error_free(&m_Error);
    libhal_ctx_free(ctx);
    return NULL;
  }

  /*
 * Do something to ping the HAL daemon - the above functions will
 * succeed even if hald is not running, so long as DBUS is.  But we
 * want to exit silently if hald is not running, to behave on
 * pre-2.6 systems.
 */
  if (!(devices = libhal_get_all_devices(ctx, &nr, &m_Error)))
  {
    printf("HAL: seems that Hal daemon is not running: %s\n", m_Error.message);
    dbus_error_free(&m_Error);

    libhal_ctx_shutdown(ctx, NULL);
    libhal_ctx_free(ctx);
    return NULL;
  }

  libhal_free_string_array(devices);

  return ctx;
}

// Called from ProcessSlow to trigger the callbacks from DBus
bool CHalManager::Update()
{
  if (m_Context == NULL)
    return false;

  if (!dbus_connection_read_write_dispatch(m_DBusSystemConnection, 0)) // We choose 0 that means we won't wait for a message
  {
    printf("DBus: System - read/write dispatch\n");
    return false;
  }
  if (NewMessage)
  {
    NewMessage = false;
    return true;
  }
  else
    return false;
}

/* Parse newly found device and add it to our remembered devices */
bool CHalManager::ParseDevice(const char *udi)
{
  int VendorID, ProductID;
  
  VendorID  = libhal_device_get_property_int(m_Context, udi, "usb.vendor_id", NULL);
  ProductID = libhal_device_get_property_int(m_Context, udi, "usb.product_id", NULL);

  const char *name = IsAllowedRemote(VendorID, ProductID);

  if (name != NULL)
  {
    printf("HAL: Found %s - %s\n", name, udi);
    if (MoveConfigs(name))
    {
      printf("HAL: Sucessfully created config for %s\n", name);
      return true;

      RunCommand(name);
    }
    else
      printf("HAL: Failed to create config for %s\n", name);
  }

  return false;
}

void Tokenize(const string& path, vector<string>& tokens, const string& delimiters)
{
  // Tokenize ripped from http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-7.html
  // Skip delimiters at beginning.
  string::size_type lastPos = path.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  string::size_type pos = path.find_first_of(delimiters, lastPos);

  while (string::npos != pos || string::npos != lastPos)
  {
    // Found a token, add it to the vector.
    tokens.push_back(path.substr(lastPos, pos - lastPos));
    // Skip delimiters.  Note the "not_of"
    lastPos = path.find_first_not_of(delimiters, pos);
    // Find next "non-delimiter"
    pos = path.find_first_of(delimiters, lastPos);
  }
}

bool CHalManager::ReadAvailableRemotes()
{
  ifstream inputfile("AvailableRemotes");
  string line;

  m_AllowedRemotes.clear();

  if (inputfile.is_open())
  {
    while (!inputfile.eof())
    {
      getline(inputfile, line);
      if (line.size() > 0)
      {
        vector<string> tokens;
        Tokenize(line, tokens, " ");

        if (tokens[1].size() > 0 && tokens[0].size() > 0)
        {
          CHalDevice dev(atoi(tokens[1].c_str()), atoi(tokens[2].c_str()), tokens[0].c_str());
          printf("AvailableRemote: (%s) (%i) (%i)\n", dev.FriendlyName, dev.VendorID, dev.ProductID);
          m_AllowedRemotes.push_back(dev);
        }
      }
    }
    inputfile.close();

    return true;
  }
  return false;
}

const char *CHalManager::IsAllowedRemote(int VendorID, int ProductID)
{
  for (unsigned int i = 0; i < m_AllowedRemotes.size(); i++)
  {
    if (VendorID == m_AllowedRemotes[i].VendorID && ProductID == m_AllowedRemotes[i].ProductID)
      return m_AllowedRemotes[i].FriendlyName;
  }
  return NULL;
}

void CHalManager::RunCommand(const char *name)
{
  char script[1024];
  sprintf(script, "%s.sh", name);
  if (Exists(script))
  {
    popen(script, "r");
  }
}

bool CHalManager::MoveConfigs(const char *name)
{
  char lircd[1024], hardware[1024], lircdout[1024], hardwareout[1024];

  sprintf(lircd,        "%s.lircd.conf", name);
  sprintf(lircdout,     "%s/lircd.conf", m_LircConfPath);
  sprintf(hardware,     "%s.hardware.conf", name);
  sprintf(hardwareout,  "%s/hardware.conf", m_LircConfPath);

  bool temp = true;
  if (!Exists(lircd))
  {
    temp = false;
    printf("%s didn't exist\n", lircd);
  }
  if (!Exists(hardware))
  {
    temp = false;
    printf("%s didn't exist\n", hardware);
  }
  if (temp)
  {
    bool temp2 = true;
    printf("Copying %s -> %s\n", lircd, lircdout);
    if (!MoveConfig(lircd, lircdout))
    {
      printf("Failed to move %s -> %s\n", lircd, lircdout);
      temp2 = false;
    }
    printf("Copying %s -> %s\n", hardware, hardwareout);
    if (!MoveConfig(hardware, hardwareout))
    {
      printf("Failed to move %s -> %s\n", hardware, hardwareout);
      temp2 = false;
    }
    return temp2;
  }
  else
    return false;
}

bool CHalManager::Exists(const char *path)
{
  struct stat stFileInfo;

  if (stat(path, &stFileInfo) == 0)
    return true;
  else
    return false;
}

bool CHalManager::MoveConfig(const char *InputConfig, const char *OutputConfig)
{
  string line;

  ifstream inputfile(InputConfig);
  ofstream outputfile(OutputConfig, ios_base::out);

  if (inputfile.is_open() && outputfile.is_open())
  {
    while (!inputfile.eof())
    {
      getline(inputfile, line);
      outputfile << line <<"\n";
    }
    inputfile.close();
    outputfile.close();
    return true;
  }
  else
    return false;
}

int main(int argc, char* argv[])
{
  if (argc == 2)
    g_HalManager.Initialize(argv[1]);
  else
    printf("Usage: %s PathToLircConf\n", argv[0]);
}