summaryrefslogtreecommitdiffabout
path: root/gammu/emb/common/gsmcomon.c
Unidiff
Diffstat (limited to 'gammu/emb/common/gsmcomon.c') (more/less context) (ignore whitespace changes)
-rw-r--r--gammu/emb/common/gsmcomon.c260
1 files changed, 260 insertions, 0 deletions
diff --git a/gammu/emb/common/gsmcomon.c b/gammu/emb/common/gsmcomon.c
new file mode 100644
index 0000000..d094ef3
--- a/dev/null
+++ b/gammu/emb/common/gsmcomon.c
@@ -0,0 +1,260 @@
1/* (c) 2002-2004 by Marcin Wiacek & Michal Cihar */
2
3#include <ctype.h>
4#include <string.h>
5#include <time.h>
6#include <stdio.h>
7#ifndef __OpenBSD__
8# include <wchar.h>
9#endif
10#ifdef WIN32
11# include <windows.h>
12#else
13# include <stdlib.h>
14# include <errno.h>
15# include <fcntl.h>
16# include <signal.h>
17#endif
18
19#include "gsmcomon.h"
20#include "misc/coding/coding.h"
21
22GSM_Error NoneFunction(void)
23{
24 return ERR_NONE;
25}
26
27GSM_Error NotImplementedFunction(void)
28{
29 return ERR_NOTIMPLEMENTED;
30}
31
32GSM_Error NotSupportedFunction(void)
33{
34 return ERR_NOTSUPPORTED;
35}
36
37unsigned char *GetMsg (INI_Section *cfg, unsigned char *default_string)
38{
39 unsigned char *retval, buffer[40], buff2[40], buff[2000];
40 static unsigned chardef_str[2000];
41 INI_Entry *e;
42 INI_Section *h;
43 int num;
44 int len;
45
46 if (cfg==NULL) return default_string;
47
48 EncodeUnicode (buff2, "common", 6);
49
50 /* Set all 0x0a to \n */
51 memset(def_str,0,sizeof(def_str));
52 for (num=0;num<((int)strlen(default_string));num++) {
53 if (default_string[num] == 0x0a) {
54 def_str[strlen(def_str)] = '\\';
55 def_str[strlen(def_str)] = 'n';
56 } else def_str[strlen(def_str)] = default_string[num];
57 }
58
59 e = NULL;
60 /* First find our section */
61 for (h = cfg; h != NULL; h = h->Next) {
62 if (mywstrncasecmp(buff2, h->SectionName, 0)) {
63 e = h->SubEntries;
64 break;
65 }
66 }
67 while (e != NULL) {
68 num = -1;
69 DecodeUnicode(e->EntryName,buffer);
70 if (strlen(buffer) == 5 && (buffer[0] == 'F' || buffer[0] == 'f')) {
71 num = atoi(buffer+2);
72 }
73 if (num!=-1) {
74 DecodeUnicode(e->EntryValue,buff);
75 /* Remove quotes */
76 if (buff[0] == '"') {
77 len = strlen(buff);
78 memmove(buff, buff + 1, len - 1);
79 if (buff[len - 2] == '"') buff[len - 2] = 0;
80 }
81 if (strcmp(buff, def_str) == 0) {
82 sprintf(buff,"T%04i",num);
83 EncodeUnicode (buffer, buff, 5);
84 retval = INI_GetValue(cfg, buff2, buffer, true);
85 if (retval) {
86 sprintf(buff,"%s",DecodeUnicodeConsole(retval+2));
87 buff[strlen(buff)-1] = 0;
88 /* Set all \n to 0x0a */
89 memset(def_str,0,sizeof(def_str));
90 num = 0;
91 while (num != (int)strlen(buff)) {
92 if (num < (int)strlen(buff) - 1) {
93 if (buff[num] == '\\' && buff[num+1] == 'n') {
94 def_str[strlen(def_str)] = 0x0a;
95 num+=2;
96 } else {
97 def_str[strlen(def_str)] = buff[num++];
98 }
99 } else {
100 def_str[strlen(def_str)] = buff[num++];
101 }
102 }
103 retval = def_str;
104 } else {
105 retval = default_string;
106 }
107 return retval;
108 }
109 }
110 e = e->Next;
111 }
112 return default_string;
113}
114
115typedef struct {
116 GSM_Error ErrorNum;
117 unsigned char *ErrorText;
118} PrintErrorEntry;
119
120static PrintErrorEntry PrintErrorEntries[] = {
121 {ERR_NONE, "No error."},
122 {ERR_DEVICEOPENERROR, "Error opening device. Unknown/busy or no permissions."},
123 {ERR_DEVICELOCKED, "Error opening device. Device locked."},
124 {ERR_DEVICENOTEXIST, "Error opening device. Not exist."},
125 {ERR_DEVICEBUSY, "Error opening device. Already opened by other application."},
126 {ERR_DEVICENOPERMISSION,"Error opening device. No permissions."},
127 {ERR_DEVICENODRIVER, "Error opening device. No required driver in operating system."},
128 {ERR_DEVICENOTWORK, "Error opening device. Some hardware not connected/wrong configured."},
129 {ERR_DEVICEDTRRTSERROR, "Error setting device DTR or RTS."},
130 {ERR_DEVICECHANGESPEEDERROR,"Error setting device speed. Maybe speed not supported."},
131 {ERR_DEVICEWRITEERROR, "Error writing device."},
132 {ERR_DEVICEREADERROR, "Error during reading device"},
133 {ERR_DEVICEPARITYERROR, "Can't set parity on device"},
134 {ERR_TIMEOUT, "No response in specified timeout. Probably phone not connected."},
135 /* Some missed */
136 {ERR_UNKNOWNRESPONSE, "Unknown response from phone. See readme.txt, how to report it."},
137 /* Some missed */
138 {ERR_UNKNOWNCONNECTIONTYPESTRING,"Unknown connection type string. Check config file."},
139 {ERR_UNKNOWNMODELSTRING,"Unknown model type string. Check config file."},
140 {ERR_SOURCENOTAVAILABLE,"Some required functions not compiled for your OS. Please contact."},
141 {ERR_NOTSUPPORTED, "Function not supported by phone."},
142 {ERR_EMPTY, "Entry is empty"},
143 {ERR_SECURITYERROR, "Security error. Maybe no PIN ?"},
144 {ERR_INVALIDLOCATION, "Invalid location. Maybe too high ?"},
145 {ERR_NOTIMPLEMENTED, "Function not implemented. Help required."},
146 {ERR_FULL, "Memory full."},
147 {ERR_UNKNOWN, "Unknown error."},
148 /* Some missed */
149 {ERR_CANTOPENFILE, "Can't open specified file. Read only ?"},
150 {ERR_MOREMEMORY, "More memory required..."},
151 {ERR_PERMISSION, "Permission to file/device required..."},
152 {ERR_EMPTYSMSC, "Empty SMSC number. Set in phone or use -smscnumber"},
153 {ERR_INSIDEPHONEMENU, "You're inside phone menu (during editing ?). Leave it and try again."},
154 {ERR_WORKINPROGRESS, "Function is during writing. If want help, please contact with authors."},
155 {ERR_PHONEOFF, "Phone is disabled and connected to charger"},
156 {ERR_FILENOTSUPPORTED, "File format not supported by Gammu"},
157 {ERR_BUG, "Nobody is perfect, some bug appeared in protocol implementation. Please contact authors."},
158 {ERR_CANCELED, "Transfer was canceled by phone (you pressed cancel on phone?)."},
159 /* Some missed */
160 {ERR_OTHERCONNECTIONREQUIRED,"Current connection type doesn't support called function."},
161 /* Some missed */
162 {ERR_INVALIDDATETIME, "Invalid date or time specified."},
163 {ERR_MEMORY, "Phone memory error, maybe it is read only"},
164 {ERR_INVALIDDATA, "Invalid data"},
165
166 {0, ""}
167};
168
169unsigned char *print_error(GSM_Error e, FILE *df, INI_Section *cfg)
170{
171 unsigned char *def = NULL;
172 int i= 0;
173
174 while (PrintErrorEntries[i].ErrorNum != 0) {
175 if (PrintErrorEntries[i].ErrorNum == e) {
176 def = PrintErrorEntries[i].ErrorText;
177 break;
178 }
179 i++;
180 }
181 if (def == NULL) def = "Unknown error.";
182 if (df!=NULL && di.dl!=0) fprintf(df,"[ERROR %i: %s]\n",e,def);
183
184 return GetMsg(cfg,def);
185}
186
187const char *GetGammuLocalePath(void)
188{
189#ifdef LOCALE_PATH
190 static const char Buffer[] = LOCALE_PATH;
191 return Buffer;
192#else
193 return NULL;
194#endif
195}
196
197const char *GetGammuVersion(void)
198{
199 static const char Buffer[] = VERSION;
200 return Buffer;
201}
202
203GSM_Error GSM_SetDebugFile(char *info, Debug_Info *privdi)
204{
205 FILE *testfile;
206
207 /* If we should use global file descriptor, use it */
208 if (privdi->use_global) {
209 /* Aren't we the changing the global di? */
210 if (privdi != &di) {
211 if (privdi->df != di.df &&
212 privdi->dl!=0 &&
213 fileno(privdi->df) != 1 &&
214 fileno(privdi->df) != 2)
215 fclose(privdi->df);
216 privdi->df = di.df;
217 return ERR_NONE;
218 }
219 } else {
220 /* If we should not use global file descriptor, don't even try use it */
221 if (privdi->df == di.df) privdi->df = stdout;
222 }
223
224 if (info[0]!=0 && privdi->dl != 0) {
225 switch (privdi->dl) {
226 case DL_BINARY:
227 testfile = fopen(info,"wcb");
228 break;
229 case DL_TEXTERROR:
230 case DL_TEXTERRORDATE:
231 testfile = fopen(info,"ac");
232 if (!testfile) {
233 dbgprintf("Can't open debug file\n");
234 return ERR_CANTOPENFILE;
235 }
236 fseek(testfile, 0, SEEK_END);
237 if (ftell(testfile) > 5000000) {
238 fclose(testfile);
239 testfile = fopen(info,"wc");
240 }
241 break;
242 default:
243 testfile = fopen(info,"wc");
244 }
245 if (!testfile) {
246 dbgprintf("Can't open debug file\n");
247 return ERR_CANTOPENFILE;
248 } else {
249 if (privdi->df && privdi->df != stdout) {
250 fclose(privdi->df);
251 }
252 privdi->df = testfile;
253 }
254 }
255 return ERR_NONE;
256}
257
258/* How should editor hadle tabs in this file? Add editor commands here.
259 * vim: noexpandtab sw=8 ts=8 sts=8:
260 */