1/*
2  This file is part of usb_modeswitch, a mode switching tool for controlling
3  the mode of 'multi-state' USB devices
4
5  Version 2.2.3, 2015/06/29
6  Copyright (C) 2007 - 2015  Josua Dietze
7
8  Config file parsing stuff borrowed from Guillaume Dargaud
9  (http://www.gdargaud.net/Hack/SourceCode.html)
10
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details:
20
21  http://www.gnu.org/licenses/gpl.txt
22
23*/
24
25#include <stdlib.h>
26#include <libusb.h>
27
28void readConfigFile(const char *configFilename);
29void printConfig();
30int switchSendMessage();
31int switchConfiguration();
32int switchAltSetting();
33void switchHuaweiMode();
34
35void switchSierraMode();
36void switchGCTMode();
37void switchKobilMode();
38void switchQisdaMode();
39void switchQuantaMode();
40void switchSequansMode();
41void switchActionMode();
42void switchBlackberryMode();
43void switchPantechMode();
44void switchCiscoMode();
45int switchSonyMode();
46int detachDriver();
47int checkSuccess();
48int sendMessage(char* message, int count);
49int write_bulk(int endpoint, char *message, int length);
50int read_bulk(int endpoint, char *buffer, int length);
51void release_usb_device(int dummy);
52struct libusb_device* search_devices( int *numFound, int vendor, char* productList,
53		int targetClass, int configuration, int mode);
54int find_first_bulk_endpoint(int direction);
55int get_current_configuration();
56int get_interface_class();
57char* ReadParseParam(const char* FileName, char *VariableName);
58int hex2num(char c);
59int hex2byte(const char *hex);
60int hexstr2bin(const char *hex, char *buffer, int len);
61void printVersion();
62void printHelp();
63int readArguments(int argc, char **argv);
64void deviceDescription();
65int deviceInquire();
66void resetUSB();
67void release_usb_device(int dummy);
68int findMBIMConfig(int vendor, int product, int mode);
69
70
71// Boolean
72#define  and     &&
73#define  or      ||
74#define  not     !
75
76// Bitwise
77#define  bitand  &
78#define  bitor   |
79#define  compl   ~
80#define  xor     ^
81
82// Equals
83#define  and_eq  &=
84#define  not_eq  !=
85#define  or_eq   |=
86#define  xor_eq  ^=
87
88extern char* ReadParseParam(const char* FileName, char *VariableName);
89
90extern char *TempPP;
91
92#define ParseParamString(ParamFileName, Str) \
93	if ((TempPP=ReadParseParam((ParamFileName), #Str))!=NULL) \
94		strcpy(Str, TempPP); else Str[0]='\0'
95
96#define ParseParamInt(ParamFileName, Int) \
97	if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
98		Int=atoi(TempPP)
99
100#define ParseParamHex(ParamFileName, Int) \
101	if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
102		Int=strtol(TempPP, NULL, 16)
103
104#define ParseParamFloat(ParamFileName, Flt) \
105	if ((TempPP=ReadParseParam((ParamFileName), #Flt))!=NULL) \
106		Flt=atof(TempPP)
107
108#define ParseParamBool(ParamFileName, B) \
109	if ((TempPP=ReadParseParam((ParamFileName), #B))!=NULL) \
110		B=(toupper(TempPP[0])=='Y' || toupper(TempPP[0])=='T'|| TempPP[0]=='1'); else B=0
111
112#define ParseParamBoolMap(ParamFileName, B, M, Const) \
113	if ((TempPP=ReadParseParam((ParamFileName), #B))!=NULL) \
114		if (toupper(TempPP[0])=='Y' || toupper(TempPP[0])=='T'|| TempPP[0]=='1') \
115			M=M+Const
116