Imported gammu 0.90.7
[gammu.git] / common / phone / at / siemens.c
blob9ef59efc7dacbe5ceeed8dba81fb0014afda4022
2 #include "../../gsmstate.h"
4 #ifdef GSM_ENABLE_ATGEN
6 #include <string.h>
7 #include <time.h>
8 #include <ctype.h>
10 #include "../../misc/coding/coding.h"
11 #include "../../gsmcomon.h"
12 #include "../../service/sms/gsmsms.h"
13 #include "../pfunc.h"
15 extern GSM_Error ATGEN_HandleCMSError(GSM_StateMachine *s);
17 GSM_Error ATGEN_CMS35ReplySetFunction (GSM_Protocol_Message msg, GSM_StateMachine *s,char *function)
19 if (s->Protocol.Data.AT.EditMode) {
20 s->Protocol.Data.AT.EditMode = false;
21 return GE_NONE;
23 dbgprintf ("Written %s",function);
24 if (s->Phone.Data.Priv.ATGEN.ReplyState == AT_Reply_OK){
25 dbgprintf (" - OK\n");
26 return GE_NONE;
27 } else {
28 dbgprintf (" - error\n");
29 return GE_UNKNOWN;
33 GSM_Error GetSiemensFrame(GSM_Protocol_Message msg, GSM_StateMachine *s, char *templ,
34 unsigned char *buffer, int *len)
36 GSM_Phone_ATGENData *Priv = &s->Phone.Data.Priv.ATGEN;
37 int i=2, pos=0, length=0;
38 unsigned char buf[512];
40 if (strstr(GetLineString(msg.Buffer,Priv->Lines,2),"OK")) return GE_EMPTY;
41 if (!strstr(GetLineString(msg.Buffer,Priv->Lines,2),templ)) return GE_UNKNOWN;
43 while (1) {
44 if (Priv->Lines.numbers[i*2+1]==0) break;
45 if ((!strstr(GetLineString(msg.Buffer,Priv->Lines,i+1),templ)) &&
46 (strstr(GetLineString(msg.Buffer,Priv->Lines,i),templ))){
47 length = strlen(GetLineString(msg.Buffer,Priv->Lines,i+1));
48 DecodeHexBin(buf, GetLineString(msg.Buffer,Priv->Lines,i+1),length);
49 length = length/2;
50 memcpy (buffer+pos,buf,length);
51 pos+=length;
53 i++;
55 *len = pos;
56 return GE_NONE;
59 GSM_Error SetSiemensFrame (GSM_StateMachine *s, unsigned char *buff, char *templ,
60 int Location, GSM_Phone_RequestID RequestID, int len)
62 GSM_Phone_Data *Phone = &s->Phone.Data;
63 GSM_Error error;
64 unsigned char req[20],req1[512],hexreq[2096];
65 int MaxFrame,CurrentFrame,size,sz,pos=0;
67 EncodeHexBin(hexreq,buff,len);
68 size = len * 2;
69 MaxFrame = size / 352;
70 if (size % 352) MaxFrame++;
72 for (CurrentFrame=0;CurrentFrame<MaxFrame;CurrentFrame++)
74 pos=CurrentFrame*352;
75 if (pos+352 < size) sz = 352; else sz = size - pos;
76 sprintf(req, "AT^SBNW=\"%s\",%i,%i,%i\r",templ,Location,CurrentFrame+1,MaxFrame);
77 s->Protocol.Data.AT.EditMode = true;
78 error = GSM_WaitFor (s, req, strlen(req), 0x00, 3, RequestID);
79 s->Phone.Data.DispatchError=GE_TIMEOUT;
80 s->Phone.Data.RequestID=RequestID;
81 if (error!=GE_NONE) return error;
82 memcpy (req1,hexreq+pos,sz);
83 error = s->Protocol.Functions->WriteMessage(s, req1, sz, 0x00);
84 if (error!=GE_NONE) return error;
85 error = s->Protocol.Functions->WriteMessage(s,"\x1A", 1, 0x00);
86 if (error!=GE_NONE) return error;
87 error = GSM_WaitForOnce(s, NULL, 0x00, 0x00, 4);
88 if (error == GE_TIMEOUT) return error;
90 return Phone->DispatchError;
93 GSM_Error ATGEN_CMS35ReplyGetBitmap(GSM_Protocol_Message msg, GSM_StateMachine *s)
95 unsigned char buffer[4096];
96 int length;
97 GSM_Error error;
99 error = GetSiemensFrame(msg,s,"bmp",buffer,&length);
100 if (error!=GE_NONE) return error;
101 dbgprintf ("Operator logo received lenght=%i\n",length);
102 error = BMP2Bitmap (buffer,NULL,s->Phone.Data.Bitmap);
103 if (error==GE_NONE) return error;
104 else return GE_UNKNOWN;
107 GSM_Error ATGEN_CMS35ReplySetBitmap(GSM_Protocol_Message msg, GSM_StateMachine *s)
109 return ATGEN_CMS35ReplySetFunction (msg, s, "Operator Logo");
112 GSM_Error ATGEN_GetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
114 unsigned char req[32];
116 if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
117 if (Bitmap->Type!=GSM_OperatorLogo) return GE_NOTSUPPORTED;
118 if (Bitmap->Location-1 < 0) Bitmap->Location++;
119 s->Phone.Data.Bitmap=Bitmap;
120 sprintf(req, "AT^SBNR=\"bmp\",%i\r", Bitmap->Location-1);
121 smprintf(s, "Getting Bitmap\n");
122 return GSM_WaitFor (s, req, strlen(req), 0x00, 4, ID_GetBitmap);
125 GSM_Error ATGEN_SetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
127 unsigned char buffer[4096];
128 int length;
129 GSM_Error error;
131 if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
132 if (Bitmap->Type!=GSM_OperatorLogo) return GE_NOTSUPPORTED;
134 error = Bitmap2BMP (buffer,NULL,Bitmap);
135 if (error!=GE_NONE) return error;
136 length = 0x100 * buffer[3] + buffer[2];
137 buffer[58]=0xff; buffer[59]=0xff; buffer[60]=0xff;
138 if (Bitmap->Location-1 < 0) Bitmap->Location++;
139 s->Phone.Data.Bitmap=Bitmap;
140 return SetSiemensFrame(s, buffer,"bmp",Bitmap->Location-1,
141 ID_SetBitmap,length);
145 GSM_Error ATGEN_CMS35ReplyGetRingtone(GSM_Protocol_Message msg, GSM_StateMachine *s)
147 unsigned char buffer[32];
148 int length;
149 GSM_Error error;
151 error = GetSiemensFrame(msg,s,"mid",s->Phone.Data.Ringtone->NokiaBinary.Frame,&length);
152 if (error!=GE_NONE) return error;
153 dbgprintf ("Midi ringtone received\n");
155 s->Phone.Data.Ringtone->Format = RING_MIDI;
156 s->Phone.Data.Ringtone->NokiaBinary.Length = length;
157 sprintf(buffer,"Individual");
158 EncodeUnicode (s->Phone.Data.Ringtone->Name,buffer,strlen(buffer));
159 return GE_NONE;
162 GSM_Error ATGEN_GetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, bool PhoneRingtone)
164 unsigned char req[32];
166 if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
168 s->Phone.Data.Ringtone=Ringtone;
169 sprintf(req, "AT^SBNR=\"mid\",%i\r", Ringtone->Location-1);
170 smprintf(s, "Getting RingTone\n");
171 return GSM_WaitFor (s, req, strlen(req), 0x00, 4, ID_GetRingtone);
174 GSM_Error ATGEN_CMS35ReplySetRingtone(GSM_Protocol_Message msg, GSM_StateMachine *s)
176 return ATGEN_CMS35ReplySetFunction (msg, s, "Ringtone");
179 GSM_Error ATGEN_SetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, int *maxlength)
181 GSM_Phone_Data *Phone = &s->Phone.Data;
183 if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
185 if (Ringtone->Location==255) Ringtone->Location=1;
186 if (Ringtone->Location-1 > 1) return GE_INVALIDLOCATION;
188 s->Phone.Data.Ringtone = Ringtone;
189 Phone->Ringtone = Ringtone;
190 return SetSiemensFrame(s, Ringtone->NokiaBinary.Frame,"mid",Ringtone->Location-1,
191 ID_SetRingtone,Ringtone->NokiaBinary.Length);
194 GSM_Error ATGEN_CMS35ReplyGetNextCal(GSM_Protocol_Message msg, GSM_StateMachine *s)
196 GSM_Phone_Data *Data = &s->Phone.Data;
197 GSM_CalendarEntry *Calendar = Data->Cal;
198 GSM_ToDoEntry ToDo;
199 GSM_Error error;
200 unsigned char buffer[354];
201 int len, pos=0;
203 if (Data->Priv.ATGEN.ReplyState != AT_Reply_OK) return GE_UNKNOWN;
205 error = GetSiemensFrame(msg,s,"vcs",buffer,&len);
206 if (error!=GE_NONE) return error;
207 error=GSM_DecodeVCALENDAR_VTODO(buffer,&pos,Calendar,&ToDo,Siemens_VCalendar,0);
209 return error;
212 GSM_Error SIEMENS_GetNextCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Note, bool start)
214 GSM_Phone_ATGENData *Priv = &s->Phone.Data.Priv.ATGEN;
215 GSM_Error error;
216 unsigned char req[32];
217 int Location;
219 if (Priv->Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
221 if (start) Note->Location=Priv->FirstCalendarPos;
222 s->Phone.Data.Cal = Note;
223 Note->EntriesNum = 0;
224 smprintf(s, "Getting VCALENDAR\n");
225 Location = Note->Location;
226 while (1){
227 Location++;
228 sprintf(req, "AT^SBNR=\"vcs\",%i\r",Location);
229 error = GSM_WaitFor (s, req, strlen(req), 0x00, 4, ID_GetCalendarNote);
230 if ((error!=GE_NONE) && (error!=GE_EMPTY)) return GE_INVALIDLOCATION;
231 Note->Location = Location;
232 Priv->FirstCalendarPos = Location;
233 if (Location > MAX_VCALENDAR_LOCATION) return GE_EMPTY;
234 if (error==GE_NONE) return error;
236 return error;
239 GSM_Error ATGEN_CMS35ReplySetCalendar(GSM_Protocol_Message msg, GSM_StateMachine *s)
241 return ATGEN_CMS35ReplySetFunction (msg, s, "Calendar Note");
244 GSM_Error ATGEN_CMS35ReplyDeleteCalendar(GSM_Protocol_Message msg, GSM_StateMachine *s)
246 GSM_Phone_Data *Data = &s->Phone.Data;
248 if (Data->Cal->Location > MAX_VCALENDAR_LOCATION) return GE_UNKNOWN;
250 if (Data->Priv.ATGEN.ReplyState== AT_Reply_OK) {
251 smprintf(s, "Calendar note deleted\n");
252 return GE_NONE;
253 } else {
254 smprintf(s, "Can't delete calendar note\n");
255 return GE_UNKNOWN;
259 GSM_Error SIEMENS_DelCalendarNote(GSM_StateMachine *s, GSM_CalendarEntry *Note)
261 unsigned char req[32];
263 if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
264 s->Phone.Data.Cal = Note;
265 sprintf(req, "AT^SBNW=\"vcs\",%i,0\r",Note->Location);
266 smprintf(s, "Deleting calendar note\n");
267 return GSM_WaitFor (s, req, strlen(req), 0x00, 4, ID_DeleteCalendarNote);
270 GSM_Error SIEMENS_AddCalendarNote(GSM_StateMachine *s, GSM_CalendarEntry *Note)
272 GSM_Phone_ATGENData *Priv = &s->Phone.Data.Priv.ATGEN;
273 GSM_Error error;
274 unsigned char req[500];
275 int size=0;
277 if (Priv->Manufacturer!=AT_Siemens) return GE_NOTSUPPORTED;
278 // if (Note->Location==0x00) return GE_INVALIDLOCATION;
280 s->Phone.Data.Cal = Note;
281 error=GSM_EncodeVCALENDAR(req,&size,Note,true,Siemens_VCalendar);
283 return SetSiemensFrame (s,req,"vcs",Note->Location,ID_SetCalendarNote,size);
286 GSM_Error ATGEN_SL45ReplyGetMemory(GSM_Protocol_Message msg, GSM_StateMachine *s)
288 GSM_Phone_ATGENData *Priv = &s->Phone.Data.Priv.ATGEN;
289 GSM_MemoryEntry *Memory = s->Phone.Data.Memory;
290 unsigned char buffer[500],buffer2[500];
292 switch (Priv->ReplyState) {
293 case AT_Reply_OK:
294 smprintf(s, "Phonebook entry received\n");
295 CopyLineString(buffer, msg.Buffer, Priv->Lines, 3);
296 DecodeHexBin(buffer2,buffer,strlen(buffer));
297 Memory->EntriesNum = 0;
298 Memory->PreferUnicode = false;
299 DecodeVCARD21Text(buffer2, Memory);
300 if (Memory->EntriesNum == 0) return GE_EMPTY;
301 return GE_NONE;
302 case AT_Reply_Error:
303 smprintf(s, "Error - too high location ?\n");
304 return GE_INVALIDLOCATION;
305 case AT_Reply_CMSError:
306 return ATGEN_HandleCMSError(s);
307 default:
308 break;
310 return GE_UNKNOWNRESPONSE;
313 #endif
315 /* How should editor hadle tabs in this file? Add editor commands here.
316 * vim: noexpandtab sw=8 ts=8 sts=8: