Imported gammu 0.90.7
[gammu.git] / common / gsmcomon.c
blob1d049bdd6f637c3c8d1d1ac26c6225b1f4dbbace
2 #include <ctype.h>
3 #include <string.h>
4 #include <time.h>
5 #include <stdio.h>
6 #ifndef __OpenBSD__
7 # include <wchar.h>
8 #endif
9 #ifdef WIN32
10 # include <windows.h>
11 #else
12 # include <stdlib.h>
13 # include <errno.h>
14 # include <fcntl.h>
15 # include <signal.h>
16 #endif
18 #include "gsmcomon.h"
19 #include "misc/coding/coding.h"
21 GSM_Error NoneFunction(void)
23 return GE_NONE;
26 GSM_Error NotImplementedFunction(void)
28 return GE_NOTIMPLEMENTED;
31 GSM_Error NotSupportedFunction(void)
33 return GE_NOTSUPPORTED;
36 unsigned char *GetMsg (INI_Section *cfg, unsigned char *default_string)
38 unsigned char *retval, buffer[40], buff2[40], buff[2000];
39 static unsigned char def_str[2000];
40 INI_Entry *e;
41 INI_Section *h;
42 int num;
44 if (cfg==NULL) return default_string;
46 EncodeUnicode (buff2, "common", 6);
48 /* Set all 0x0a to \n */
49 memset(def_str,0,sizeof(def_str));
50 for (num=0;num<((int)strlen(default_string));num++) {
51 if (default_string[num] == 0x0a) {
52 def_str[strlen(def_str)] = '\\';
53 def_str[strlen(def_str)] = 'n';
54 } else def_str[strlen(def_str)] = default_string[num];
57 e = NULL;
58 /* First find our section */
59 for (h = cfg; h != NULL; h = h->Next) {
60 if (mywstrncasecmp(buff2, h->SectionName, 0)) {
61 e = h->SubEntries;
62 break;
65 while (1) {
66 if (e == NULL) break;
67 num = -1;
68 DecodeUnicode(e->EntryName,buffer);
69 if (strlen(buffer) == 5 && (buffer[0] == 'F' || buffer[0] == 'f')) {
70 num = atoi(buffer+2);
72 if (num!=-1) {
73 DecodeUnicode(e->EntryValue+2,buff);
74 if (strncmp(buff,def_str,strlen(def_str))==0) {
75 sprintf(buff,"T%04i",num);
76 EncodeUnicode (buffer, buff, 5);
77 retval = INI_GetValue(cfg, buff2, buffer, true);
78 if (retval) {
79 sprintf(buff,"%s",DecodeUnicodeConsole(retval+2));
80 buff[strlen(buff)-1] = 0;
81 /* Set all \n to 0x0a */
82 memset(def_str,0,sizeof(def_str));
83 num = 0;
84 while (num != (int)strlen(buff)) {
85 if (num < (int)strlen(buff) - 1) {
86 if (buff[num] == '\\' && buff[num+1] == 'n') {
87 def_str[strlen(def_str)] = 0x0a;
88 num+=2;
89 } else {
90 def_str[strlen(def_str)] = buff[num++];
92 } else {
93 def_str[strlen(def_str)] = buff[num++];
96 retval = def_str;
97 } else {
98 retval = default_string;
100 return retval;
103 e = e->Next;
105 return default_string;
108 typedef struct {
109 GSM_Error ErrorNum;
110 unsigned char *ErrorText;
111 } PrintErrorEntry;
113 static PrintErrorEntry PrintErrorEntries[] = {
114 {GE_NONE, "No error."},
115 {GE_DEVICEOPENERROR, "Error opening device. Unknown/busy or no permissions."},
116 {GE_DEVICELOCKED, "Error opening device. Device locked."},
117 {GE_DEVICENOTEXIST, "Error opening device. Not exist."},
118 {GE_DEVICEBUSY, "Error opening device. Already opened by other application."},
119 {GE_DEVICENOPERMISSION, "Error opening device. No permissions."},
120 {GE_DEVICENODRIVER, "Error opening device. No required driver in operating system."},
121 {GE_DEVICENOTWORK, "Error opening device. Some hardware not connected/wrong configured."},
122 {GE_DEVICEDTRRTSERROR, "Error setting device DTR or RTS."},
123 {GE_DEVICECHANGESPEEDERROR, "Error setting device speed. Maybe speed not supported."},
124 {GE_DEVICEWRITEERROR, "Error writing device."},
125 {GE_DEVICEREADERROR, "Error during reading device"},
126 {GE_DEVICEPARITYERROR, "Can't set parity on device"},
127 {GE_TIMEOUT, "No response in specified timeout. Probably phone not connected."},
128 /* Some missed */
129 {GE_UNKNOWNRESPONSE, "Unknown response from phone. See readme.txt, how to report it."},
130 /* Some missed */
131 {GE_UNKNOWNCONNECTIONTYPESTRING,"Unknown connection type string. Check config file."},
132 {GE_UNKNOWNMODELSTRING, "Unknown model type string. Check config file."},
133 {GE_SOURCENOTAVAILABLE, "Some required functions not compiled for your OS. Please contact."},
134 {GE_NOTSUPPORTED, "Function not supported by phone."},
135 {GE_EMPTY, "Entry is empty"},
136 {GE_SECURITYERROR, "Security error. Maybe no PIN ?"},
137 {GE_INVALIDLOCATION, "Invalid location. Maybe too high ?"},
138 {GE_NOTIMPLEMENTED, "Function not implemented. Help required."},
139 {GE_FULL, "Memory full."},
140 {GE_UNKNOWN, "Unknown error."},
141 /* Some missed */
142 {GE_CANTOPENFILE, "Can't open specified file. Read only ?"},
143 {GE_MOREMEMORY, "More memory required..."},
144 {GE_PERMISSION, "Permission to file/device required..."},
145 {GE_EMPTYSMSC, "Empty SMSC number. Set in phone or use -smscnumber"},
146 {GE_INSIDEPHONEMENU, "You're inside phone menu (during editing ?). Leave it and try again."},
147 {GE_WORKINPROGRESS, "Function is during writing. If want help, please contact with authors."},
148 {GE_PHONEOFF, "Phone is disabled and connected to charger"},
149 {GE_FILENOTSUPPORTED, "File format not supported by Gammu"},
150 {GE_BUG, "Nobody is perfect, some bug appeared in protocol implementation. Please contact authors."},
151 {GE_CANCELED, "Transfer was canceled by phone (you pressed cancel on phone?)."},
152 /* Some missed */
153 {GE_OTHERCONNECTIONREQUIRED, "Current connection type doesn't support called function."},
154 /* Some missed */
155 {GE_INVALIDDATETIME, "Invalid date or time specified."},
157 {0, ""}
160 unsigned char *print_error(GSM_Error e, FILE *df, INI_Section *cfg)
162 unsigned char *def = NULL;
163 int i = 0;
165 while (PrintErrorEntries[i].ErrorNum != 0) {
166 if (PrintErrorEntries[i].ErrorNum == e) {
167 def = PrintErrorEntries[i].ErrorText;
168 break;
170 i++;
172 if (def == NULL) def = "Unknown error.";
173 if (df!=NULL && di.dl!=0) fprintf(df,"[ERROR %i: %s]\n",e,def);
175 return GetMsg(cfg,def);
178 char *GetGammuVersion(void)
180 static char Buffer[1000]="";
182 sprintf(Buffer, "%s",VERSION);
183 return Buffer;
186 GSM_Error GSM_SetDebugFile(char *info, Debug_Info *privdi)
188 FILE *testfile;
190 /* If we should use global file descriptor, use it */
191 if (privdi->use_global) {
192 /* Aren't we the changing the global di? */
193 if (privdi != &di) {
194 if (privdi->df == stdout) privdi->df = di.df;
195 return GE_NONE;
197 } else {
198 /* If we should not use global file descriptor, don't even try use it */
199 if (privdi->df == di.df) privdi->df = stdout;
202 if (info[0]!=0 && privdi->dl != 0) {
203 switch (privdi->dl) {
204 case DL_BINARY:
205 testfile = fopen(info,"wcb");
206 break;
207 case DL_TEXTERROR:
208 case DL_TEXTERRORDATE:
209 testfile = fopen(info,"ac");
210 if (!testfile) {
211 dbgprintf("Can't open debug file\n");
212 return GE_CANTOPENFILE;
214 fseek(testfile, 0, SEEK_END);
215 if (ftell(testfile) > 5000000) {
216 fclose(testfile);
217 testfile = fopen(info,"wc");
219 break;
220 default:
221 testfile = fopen(info,"wc");
223 if (!testfile) {
224 dbgprintf("Can't open debug file\n");
225 return GE_CANTOPENFILE;
226 } else {
227 if (privdi->df && privdi->df != stdout) {
228 fclose(privdi->df);
230 privdi->df = testfile;
233 return GE_NONE;
236 /* How should editor hadle tabs in this file? Add editor commands here.
237 * vim: noexpandtab sw=8 ts=8 sts=8: