summaryrefslogtreecommitdiffabout
path: root/gammu/emb/common/gsmcomon.c
blob: d094ef3d11e79f324245aacea6e3221323f393ec (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
/* (c) 2002-2004 by Marcin Wiacek & Michal Cihar */

#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#ifndef __OpenBSD__
#  include <wchar.h>
#endif
#ifdef WIN32
#  include <windows.h>
#else
#  include <stdlib.h>
#  include <errno.h>
#  include <fcntl.h>
#  include <signal.h>
#endif

#include "gsmcomon.h"
#include "misc/coding/coding.h"

GSM_Error NoneFunction(void)
{
	return ERR_NONE;
}

GSM_Error NotImplementedFunction(void)
{
	return ERR_NOTIMPLEMENTED;
}

GSM_Error NotSupportedFunction(void)
{
	return ERR_NOTSUPPORTED;
}

unsigned char *GetMsg (INI_Section *cfg, unsigned char *default_string)
{
	unsigned char 		*retval, buffer[40], buff2[40], buff[2000];
	static unsigned char	def_str[2000];
	INI_Entry		*e;
	INI_Section 		*h;
	int			num;
	int			len;

	if (cfg==NULL) return default_string;

	EncodeUnicode (buff2, "common", 6);

	/* Set all 0x0a to \n */
	memset(def_str,0,sizeof(def_str));
	for (num=0;num<((int)strlen(default_string));num++) {
		if (default_string[num] == 0x0a) {
			def_str[strlen(def_str)] = '\\';
			def_str[strlen(def_str)] = 'n';
		} else def_str[strlen(def_str)] = default_string[num];
	}

	e = NULL;
	/* First find our section */
        for (h = cfg; h != NULL; h = h->Next) {
		if (mywstrncasecmp(buff2, h->SectionName, 0)) {
			e = h->SubEntries;
			break;
		}
	}
	while (e != NULL) {
		num = -1;
		DecodeUnicode(e->EntryName,buffer);
		if (strlen(buffer) == 5 && (buffer[0] == 'F' || buffer[0] == 'f')) {
			num = atoi(buffer+2);
		}
		if (num!=-1) {
			DecodeUnicode(e->EntryValue,buff);
			/* Remove quotes */
			if (buff[0] == '"') {
				len = strlen(buff);
				memmove(buff, buff + 1, len - 1);
				if (buff[len - 2] == '"') buff[len - 2] = 0;
			}
			if (strcmp(buff, def_str) == 0) {
				sprintf(buff,"T%04i",num);
				EncodeUnicode (buffer, buff, 5);
			        retval = INI_GetValue(cfg, buff2, buffer, true);
			        if (retval) {
					sprintf(buff,"%s",DecodeUnicodeConsole(retval+2));
					buff[strlen(buff)-1] = 0;
					/* Set all \n to 0x0a */
					memset(def_str,0,sizeof(def_str));
					num = 0;
					while (num != (int)strlen(buff)) {
						if (num < (int)strlen(buff) - 1) {
							if (buff[num] == '\\' && buff[num+1] == 'n') {
								def_str[strlen(def_str)] = 0x0a;
								num+=2;
							} else {
								def_str[strlen(def_str)] = buff[num++];
							}
						} else {
							def_str[strlen(def_str)] = buff[num++];
						}
					}
					retval = def_str;
				} else {
					retval = default_string;
				}
				return retval;
			}
		}
		e = e->Next;
	}
	return default_string;
}

typedef struct {
	GSM_Error		ErrorNum;
	unsigned char		*ErrorText;
} PrintErrorEntry;

static PrintErrorEntry PrintErrorEntries[] = {
	{ERR_NONE,			"No error."},
	{ERR_DEVICEOPENERROR,		"Error opening device. Unknown/busy or no permissions."},
	{ERR_DEVICELOCKED,		"Error opening device. Device locked."},
	{ERR_DEVICENOTEXIST,		"Error opening device. Not exist."},
	{ERR_DEVICEBUSY,		"Error opening device. Already opened by other application."},
	{ERR_DEVICENOPERMISSION,	"Error opening device. No permissions."},
	{ERR_DEVICENODRIVER,		"Error opening device. No required driver in operating system."},
	{ERR_DEVICENOTWORK,		"Error opening device. Some hardware not connected/wrong configured."},
	{ERR_DEVICEDTRRTSERROR,		"Error setting device DTR or RTS."},
	{ERR_DEVICECHANGESPEEDERROR,	"Error setting device speed. Maybe speed not supported."},
	{ERR_DEVICEWRITEERROR,		"Error writing device."},
	{ERR_DEVICEREADERROR,		"Error during reading device"},
	{ERR_DEVICEPARITYERROR,		"Can't set parity on device"},
	{ERR_TIMEOUT,			"No response in specified timeout. Probably phone not connected."},
	/* Some missed */
	{ERR_UNKNOWNRESPONSE,		"Unknown response from phone. See readme.txt, how to report it."},
	/* Some missed */
	{ERR_UNKNOWNCONNECTIONTYPESTRING,"Unknown connection type string. Check config file."},
	{ERR_UNKNOWNMODELSTRING,	"Unknown model type string. Check config file."},
	{ERR_SOURCENOTAVAILABLE,	"Some required functions not compiled for your OS. Please contact."},
	{ERR_NOTSUPPORTED,		"Function not supported by phone."},
	{ERR_EMPTY,			"Entry is empty"},
	{ERR_SECURITYERROR,		"Security error. Maybe no PIN ?"},
	{ERR_INVALIDLOCATION,		"Invalid location. Maybe too high ?"},
	{ERR_NOTIMPLEMENTED,		"Function not implemented. Help required."},
	{ERR_FULL,			"Memory full."},
	{ERR_UNKNOWN,			"Unknown error."},
	/* Some missed */
	{ERR_CANTOPENFILE,		"Can't open specified file. Read only ?"},
	{ERR_MOREMEMORY,		"More memory required..."},
	{ERR_PERMISSION,		"Permission to file/device required..."},
	{ERR_EMPTYSMSC,			"Empty SMSC number. Set in phone or use -smscnumber"},
	{ERR_INSIDEPHONEMENU,		"You're inside phone menu (during editing ?). Leave it and try again."},
	{ERR_WORKINPROGRESS,		"Function is during writing. If want help, please contact with authors."},
	{ERR_PHONEOFF,			"Phone is disabled and connected to charger"},
	{ERR_FILENOTSUPPORTED,		"File format not supported by Gammu"},
	{ERR_BUG,			"Nobody is perfect, some bug appeared in protocol implementation. Please contact authors."},
	{ERR_CANCELED,			"Transfer was canceled by phone (you pressed cancel on phone?)."},
	/* Some missed */
	{ERR_OTHERCONNECTIONREQUIRED,	"Current connection type doesn't support called function."},
	/* Some missed */
	{ERR_INVALIDDATETIME,		"Invalid date or time specified."},
	{ERR_MEMORY,			"Phone memory error, maybe it is read only"},
	{ERR_INVALIDDATA,		"Invalid data"},

	{0,				""}
};

unsigned char *print_error(GSM_Error e, FILE *df, INI_Section *cfg)
{
	unsigned char 	*def 	= NULL;
	int 		i	= 0;

	while (PrintErrorEntries[i].ErrorNum != 0) {
		if (PrintErrorEntries[i].ErrorNum == e) {
			def 	= PrintErrorEntries[i].ErrorText;
			break;
		}
		i++;
	}
	if (def == NULL) def = "Unknown error.";
	if (df!=NULL && di.dl!=0) fprintf(df,"[ERROR %i: %s]\n",e,def);

	return GetMsg(cfg,def);
}

const char *GetGammuLocalePath(void)
{
#ifdef LOCALE_PATH
	static const char Buffer[] = LOCALE_PATH;
	return Buffer;
#else
	return NULL;
#endif
}

const char *GetGammuVersion(void)
{
	static const char Buffer[] = VERSION;
	return Buffer;
}

GSM_Error GSM_SetDebugFile(char *info, Debug_Info *privdi)
{
	FILE *testfile;

	/* If we should use global file descriptor, use it */
	if (privdi->use_global) {
		/* Aren't we the changing the global di? */
		if (privdi != &di) {
			if (privdi->df != di.df && 
					privdi->dl!=0 && 
					fileno(privdi->df) != 1 && 
					fileno(privdi->df) != 2)
				fclose(privdi->df);
			privdi->df = di.df;
			return ERR_NONE;
        	}
    	} else {
        	/* If we should not use global file descriptor, don't even try use it */
        	if (privdi->df == di.df) privdi->df = stdout;
    	}

	if (info[0]!=0 && privdi->dl != 0) {
		switch (privdi->dl) {
		case DL_BINARY:
			testfile = fopen(info,"wcb");
			break;
		case DL_TEXTERROR:
		case DL_TEXTERRORDATE:
			testfile = fopen(info,"ac");
			if (!testfile) {
				dbgprintf("Can't open debug file\n");
				return ERR_CANTOPENFILE;
			}
			fseek(testfile, 0, SEEK_END);
			if (ftell(testfile) > 5000000) {
				fclose(testfile);
				testfile = fopen(info,"wc");
			}
			break;
		default:
			testfile = fopen(info,"wc");
		}
		if (!testfile) {
			dbgprintf("Can't open debug file\n");
			return ERR_CANTOPENFILE;
		} else {
			if (privdi->df && privdi->df != stdout) {
				fclose(privdi->df);
			}
			privdi->df = testfile;
		}
	}
	return ERR_NONE;
}

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */