summaryrefslogtreecommitdiff
path: root/CDAL/CDAL.cpp
blob: 6b4a45762ebb8cec0574863765829cd7eb075f8b (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
/*
 * CDAL.cpp
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

#include "stdafx.h"
#include "CDAL.h"
#include "Debug.h"
#include "CommDevice.h"
#include "CommDeviceManager.h"
#include "CommException.h"
#include "Event.h"
#include "Thread.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include "LibusbDevice_win.h"
#else
#include "LibusbDevice.h"
#endif
using namespace std;

#ifndef _WIN32
static libusb_context* context;
#endif

static int DUT_VENDOR_ID = 0x04cc;
static int DUT_PRODUCT_ID = 0x8500;
static Thread* workerThread;
static void* LibusbWorker(void* arg);
volatile bool shutdown = false;
int error = 0;


static EventCallback_t OnDeviceCallback = NULL;

void usb_init_driver(const char* vendorId, const char* productId)
{
    if (vendorId != NULL && *vendorId != '\0') {
        DUT_VENDOR_ID = strtol(vendorId, NULL, 16);
    }

    if (productId != NULL && *productId != '\0') {
        DUT_PRODUCT_ID = strtol(productId, NULL, 16);
    }

#ifdef _WIN32
    usb_init();
    usb_set_debug(2);
#else
    libusb_init(&context);
    libusb_set_debug(context, 2);
#endif

    workerThread = new Thread(LibusbWorker, 0);
}

void usb_set_listen_callback(EventCallback_t callback)
{
    OnDeviceCallback = callback;
}

void usb_deinit_driver()
{
    shutdown = true;
    workerThread->wait();
    delete workerThread;

    CommDeviceManager::destroyAll();

#ifndef _WIN32
    libusb_exit(context);
#endif

}

void usb_destroy_device(Device_t device, int error_code)
{
    device->cancel();
    error = error_code;
}

int comm_read(Device_t device, void* buffer, size_t size)
{
    return device->read(buffer, size);
}

int comm_read_nowait(void* buffer, size_t size, DataCallback_t cb, void* param)
{
    CommDevice* device = CommDeviceManager::getDevice<CommDevice>(param);

    if (NULL != device) {
        return device->readNoWait(buffer, size, cb, param);
    } else {
        return COMM_DEVICE_OBJECT_NOT_MAPPED;
    }
}

int comm_write(Device_t device, void* buffer, size_t size)
{
    return device->write(buffer, size);
}

int comm_write_nowait(void* buffer, size_t size, DataCallback_t cb, void* param)
{
    CommDevice* device = CommDeviceManager::getDevice<CommDevice>(param);

    if (NULL != device) {
        return device->writeNoWait(buffer, size, cb, param);
    } else {
        return COMM_DEVICE_OBJECT_NOT_MAPPED;
    }
}

int comm_cancel(void* param)
{
    CommDevice* device = CommDeviceManager::getDevice<CommDevice>(param);

    if (NULL != device) {
        return device->cancel();
    } else {
        return COMM_DEVICE_OBJECT_NOT_MAPPED;
    }
}

unsigned long comm_get_physical_address(Device_t device)
{
    return device->getPhysicalAddress();
}

void** comm_get_object_storage(Device_t device)
{
    return device->getObjectStorage();
}

void comm_progress(void *Communication_p, unsigned long long totalbytes,
                   unsigned long long tranferedbytes)
{
    double precentage = (tranferedbytes * 100) / totalbytes;

    printf("\r%.1f %% ", precentage);
    fflush(stdout);
}

#ifdef _WIN32
// As we are going to use libusb 0.1 version in Windows,
// this function needs to be re-writtten with the API
// related to the 0.1 version. The libusb 1.0 and 0.1
// differs a lot according to their function calls and
// the way USB devices are recognized and handled.
static void* LibusbWorker(void* arg)
{
    struct usb_bus *busses;
    struct usb_bus *bus;

    while (!shutdown) {

        usb_find_busses();
        usb_find_devices();
        busses = usb_get_busses();

        if(busses == NULL ) {
            Debug::info("No USB device connected.");
            Sleep(10);
            continue;
        }

        for (bus = busses; bus; bus = bus->next) {
            struct usb_device *dev = NULL;

            for (dev = bus->devices; dev; dev = dev->next) {
                if ((dev->descriptor.idProduct == DUT_PRODUCT_ID) && (dev->descriptor.idVendor == DUT_VENDOR_ID) && (!shutdown)) {

                LibusbDevice *device;

                device = CommDeviceManager::getDevice<LibusbDevice>(dev);

                if (0 == device) {
                    // new device found
                    try {
                        device = CommDeviceManager::createDevice <LibusbDevice> (dev);
                        Debug::info("Libusb worker: Connected libusb device");
                        OnDeviceCallback(COMM_DEVICE_SUCCESS,
                                         LIBUSB_DEVICE_CONNECTED, device);
                    } catch (CommException e) {
                        Debug::error("Libusb worker: %s", e.what());
                        OnDeviceCallback(e.getError(),
                                         COMM_DEVICE_UNDEFINED_EVENT, 0);
                    }
                 }
              }
           }
        }

        vector<CommDevice*> devices = CommDeviceManager::getAllDevices();

        for (vector<CommDevice*>::iterator i = devices.begin(); i
                != devices.end(); ++i) {
            LibusbDevice* device = dynamic_cast<LibusbDevice*>(*i);

            if (0 == device) {
                continue;
            }

            bool connected = false;
            struct usb_device *dev;
            for (bus = busses; bus; bus = bus->next) {
                for (dev = bus->devices; dev; dev = dev->next) {
                    if (dev == device->getPort()) {
                        connected = true;
                        break;
                    }
                }
                if(connected == true)
                   break;
            }

            if (!connected) {
                Debug::info("Libusb worker: Disconnected device with id %d", *i);
                OnDeviceCallback(COMM_DEVICE_SUCCESS,
                                 LIBUSB_DEVICE_DISCONNECTED, *i);
                shutdown = true;
            }
        }

        Sleep(10);
    }

    return 0;
}
#else
// In Linux, libusb 1.0 version is used. Therefore,
// the below LibusbWorker function is based on API
// for libusb 1.0 for handling USB devices.
static void* LibusbWorker(void* arg __attribute__((unused)))
{
    timespec delay;
    delay.tv_sec = 0;
    delay.tv_nsec = 10 * 1000000; // 10ms

    libusb_device** deviceList;
    ssize_t deviceCount;
    libusb_device_descriptor descriptor;

    while (!shutdown) {
        deviceCount = libusb_get_device_list(context, &deviceList);

        if (deviceCount < 0) {
            nanosleep(&delay, 0);
            continue;
        }

        for (int i = 0; i != deviceCount; ++i) {
            int status = libusb_get_device_descriptor(deviceList[i],
                         &descriptor);

            if (status != LIBUSB_SUCCESS) {
                Debug::error(
                    "Libusb worker: error while getting device descriptor for device %d from %d devices",
                    i, deviceCount);
                continue;
            }

            if (descriptor.idVendor == DUT_VENDOR_ID && descriptor.idProduct
                    == DUT_PRODUCT_ID && (!shutdown)) {
                LibusbDevice* device = CommDeviceManager::getDevice <
                                       LibusbDevice, libusb_device* > (deviceList[i]);

                if (0 == device) {
                    // new device found
                    try {
                        device = CommDeviceManager::createDevice < LibusbDevice,
                                 libusb_device* > (deviceList[i]);
                        Debug::info("Libusb worker: Connected libusb device");
                        OnDeviceCallback(COMM_DEVICE_SUCCESS,
                                         LIBUSB_DEVICE_CONNECTED, device);
                    } catch (CommException e) {
                        Debug::error("Libusb worker: %s", e.what());
                        OnDeviceCallback(e.getError(),
                                         COMM_DEVICE_UNDEFINED_EVENT, 0);
                    }
                }
            }
        }

        vector<CommDevice*> devices = CommDeviceManager::getAllDevices();

        for (vector<CommDevice*>::iterator i = devices.begin(); i
                != devices.end(); ++i) {
            LibusbDevice* device = dynamic_cast<LibusbDevice*>(*i);

            if (0 == device) {
                continue;
            }

            bool connected = false;

            for (int j = 0; j != deviceCount; ++j) {
                if (deviceList[j] == device->getPort()) {
                    if (error == 0)
                        connected = true;

                    break;
                }
            }

            if (!connected) {
                Debug::info("Libusb worker: Disconnected device with id %d", *i);
                OnDeviceCallback(COMM_DEVICE_SUCCESS,
                                 LIBUSB_DEVICE_DISCONNECTED, *i);
                shutdown = true;
            }
        }

        libusb_free_device_list(deviceList, 1);

        nanosleep(&delay, 0);
    }

    return 0;
}
#endif