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.6.0, 2019/11/28
6   Copyright (C) 2007 - 2019  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 
28 void readConfigFile(const char *configFilename);
29 void printConfig();
30 int switchSendMessage();
31 int switchConfiguration();
32 int switchAltSetting();
33 void switchHuaweiMode();
34 
35 void switchSierraMode();
36 void switchGCTMode();
37 void switchKobilMode();
38 void switchQisdaMode();
39 void switchQuantaMode();
40 void switchSequansMode();
41 void switchActionMode();
42 void switchBlackberryMode();
43 void switchPantechMode();
44 void switchCiscoMode();
45 int switchSonyMode();
46 int detachDrivers();
47 int checkSuccess();
48 int sendMessage(char* message, int count);
49 int write_bulk(int endpoint, unsigned char *message, int length);
50 int read_bulk(int endpoint, unsigned char *buffer, int length);
51 void release_usb_device(int dummy);
52 struct libusb_device* search_devices( int *numFound, int vendor, char* productList,
53 		int targetClass, int configuration, int mode);
54 int find_first_bulk_endpoint(int direction);
55 int get_current_config_value();
56 int get_interface_class();
57 char* ReadParseParam(const char* FileName, char *VariableName);
58 int hex2num(char c);
59 int hex2byte(const char *hex);
60 int hexstr2bin(const char *hex, unsigned char *buffer, int len);
61 void printVersion();
62 void printHelp();
63 void close_all();
64 void abortExit();
65 int readArguments(int argc, char **argv);
66 void deviceDescription();
67 void resetUSB();
68 void release_usb_device(int dummy);
69 int findMBIMConfig(int vendor, int product, int mode);
70 
71 
72 // Boolean
73 #define  and     &&
74 #define  or      ||
75 #define  not     !
76 
77 // Bitwise
78 #define  bitand  &
79 #define  bitor   |
80 #define  compl   ~
81 #define  xor     ^
82 
83 // Equals
84 #define  and_eq  &=
85 #define  not_eq  !=
86 #define  or_eq   |=
87 #define  xor_eq  ^=
88 
89 extern char* ReadParseParam(const char* FileName, char *VariableName);
90 
91 extern char *TempPP;
92 
93 #define ParseParamString(ParamFileName, Str) \
94 	if ((TempPP=ReadParseParam((ParamFileName), #Str))!=NULL) \
95 		strcpy(Str, TempPP); else Str[0]='\0'
96 
97 #define ParseParamInt(ParamFileName, Int) \
98 	if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
99 		Int=atoi(TempPP)
100 
101 #define ParseParamHex(ParamFileName, Int) \
102 	if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
103 		Int=strtol(TempPP, NULL, 16)
104 
105 #define ParseParamFloat(ParamFileName, Flt) \
106 	if ((TempPP=ReadParseParam((ParamFileName), #Flt))!=NULL) \
107 		Flt=atof(TempPP)
108 
109 #define ParseParamBool(ParamFileName, B) \
110 	if ((TempPP=ReadParseParam((ParamFileName), #B))!=NULL) \
111 		B=(toupper(TempPP[0])=='Y' || toupper(TempPP[0])=='T'|| TempPP[0]=='1'); else B=0
112 
113 #define ParseParamBoolMap(ParamFileName, B, M, Const) \
114 	if ((TempPP=ReadParseParam((ParamFileName), #B))!=NULL) \
115 		if (toupper(TempPP[0])=='Y' || toupper(TempPP[0])=='T'|| TempPP[0]=='1') \
116 			M=M+Const
117