Gammu internals  1.38.0
mobex.c
Go to the documentation of this file.
1 /* (c) 2010 by Michal Cihar */
2 
19 #include <gammu-config.h>
20 
21 #include "../../gsmcomon.h"
22 #include "../../misc/coding/coding.h"
23 #include "../../gsmphones.h"
24 #include "../../gsmstate.h"
25 #include "../../service/gsmmisc.h"
26 #include "../../protocol/obex/obex.h"
27 #include "obexfunc.h"
28 #include "obexgen.h"
29 #include "mobex.h"
30 
31 #include <string.h>
32 
33 #ifdef GSM_ENABLE_OBEXGEN
34 
38 #define MOBEX_TIMEOUT 10
39 
40 GSM_Error MOBEX_GetStatus(GSM_StateMachine *s, const char *path, unsigned char type, int *free_records, int *used)
41 {
42  GSM_Error error;
43  unsigned char *buffer = NULL;
44  size_t len = 0, total;
45  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
46  char appdata[1];
47 
48  appdata[0] = type;
49 
50  Priv->m_obex_appdata = appdata;
51  Priv->m_obex_appdata_len = sizeof(appdata);
52 
53  error = OBEXGEN_GetBinaryFile(s, path, &buffer, &len);
54 
55  Priv->m_obex_appdata = NULL;
56  Priv->m_obex_appdata_len = 0;
57 
58  if (error != ERR_NONE) {
59  free(buffer);
60  return error;
61  }
62 
63  if (len < 2) {
64  smprintf(s, "Unknown length of data file: %ld\n", (long int)len);
65  free(buffer);
66  return ERR_UNKNOWNRESPONSE;
67  }
68 
69  total = (buffer[0] << 8) + buffer[1];
70 
71  *used = (buffer[2] << 8) + buffer[3];
72  *free_records = total - *used;
73  smprintf (s, "total: %d, used: %d\n", (int)total, (int)*used);
74 
75  free(buffer);
76  return ERR_NONE;
77 }
78 
79 GSM_Error MOBEX_CreateEntry(GSM_StateMachine *s, const char *path, unsigned char type, int *location, const char *data)
80 {
81  GSM_Error error;
82  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
83  char appdata[1];
84 
85  appdata[0] = type;
86 
87  Priv->m_obex_newid = -1;
88  Priv->m_obex_appdata = appdata;
89  Priv->m_obex_appdata_len = sizeof(appdata);
90 
91  error = OBEXGEN_SetFile(s, path, data, strlen(data), FALSE);
92 
93  Priv->m_obex_appdata = NULL;
94  Priv->m_obex_appdata_len = 0;
95 
96  if (error != ERR_NONE) {
97  return error;
98  }
99 
100  *location = Priv->m_obex_newid;
101 
102  return ERR_NONE;
103 }
104 
105 GSM_Error MOBEX_UpdateEntry(GSM_StateMachine *s, const char *path, const int location, unsigned char type, const char *data)
106 {
107  GSM_Error error;
108  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
109  char appdata[3];
110 
111  appdata[0] = type;
112  appdata[1] = (location & 0xff00) >> 8;
113  appdata[2] = (location & 0xff);
114 
115  smprintf (s, "appdata: 0x%02x 0x%02x 0x%02x\n", appdata[0], appdata[1], appdata[2]);
116 
117  Priv->m_obex_appdata = appdata;
118  Priv->m_obex_appdata_len = sizeof(appdata);
119 
120  error = OBEXGEN_SetFile(s, path, data, data ? strlen(data) : 0, FALSE);
121 
122  Priv->m_obex_appdata = NULL;
123  Priv->m_obex_appdata_len = 0;
124 
125  if (error != ERR_NONE) {
126  return error;
127  }
128 
129  return ERR_NONE;
130 }
131 
132 GSM_Error MOBEX_GetEntry(GSM_StateMachine *s, const char *path, const int location, unsigned char type, char **data)
133 {
134  GSM_Error error;
135  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
136  char appdata[3];
137 
138  appdata[0] = type;
139  appdata[1] = (location & 0xff00) >> 8;
140  appdata[2] = (location & 0xff);
141 
142  Priv->m_obex_appdata = appdata;
143  Priv->m_obex_appdata_len = sizeof(appdata);
144 
145  error = OBEXGEN_GetTextFile(s, path, data);
146 
147  Priv->m_obex_appdata = NULL;
148  Priv->m_obex_appdata_len = 0;
149 
150  if (error != ERR_NONE) {
151  return error;
152  }
153 
154  return ERR_NONE;
155 }
156 
158 {
159  GSM_Error error;
160  char *data = NULL;
161  size_t pos = 0;
162 
163 
164  error = MOBEX_GetEntry(s, "m-obex/contacts/read", Entry->Location, Entry->MemoryType, &data);
165  if (error != ERR_NONE) {
166  free(data);
167  return error;
168  }
169 
170  error = GSM_DecodeVCARD(&(s->di), data, &pos, Entry, SonyEricsson_VCard21_Phone);
171  free(data);
172  data = NULL;
173  if (error != ERR_NONE) {
174  return error;
175  }
176 
177  return ERR_NONE;
178 }
179 
181 {
182  GSM_Error error;
183  char *data = NULL;
184  size_t pos = 0;
185  GSM_ToDoEntry ToDo;
186 
187 
188  error = MOBEX_GetEntry(s, "m-obex/calendar/read", Entry->Location, 1, &data);
189  if (error != ERR_NONE) {
190  free(data);
191  return error;
192  }
193 
194  error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, Entry, &ToDo, SonyEricsson_VCalendar, SonyEricsson_VToDo);
195  free(data);
196  data = NULL;
197  if (error != ERR_NONE) {
198  return error;
199  }
200 
201  return ERR_NONE;
202 }
203 
205 {
206  GSM_Error error;
207  char *data = NULL;
208  size_t pos = 0;
209  GSM_CalendarEntry skip;
210 
211 
212  error = MOBEX_GetEntry(s, "m-obex/calendar/read", Entry->Location, 2, &data);
213  if (error != ERR_NONE) {
214  free(data);
215  return error;
216  }
217 
218  error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, &skip, Entry, SonyEricsson_VCalendar, SonyEricsson_VToDo);
219  free(data);
220  data = NULL;
221  if (error != ERR_NONE) {
222  return error;
223  }
224 
225  return ERR_NONE;
226 }
227 
228 GSM_Error MOBEX_GetNextEntry(GSM_StateMachine *s, const char *path, const gboolean start, int *nextid, int *nexterror, unsigned char **data, size_t *pos, size_t *size, char **entry, int *location, int type)
229 {
230  GSM_Error error;
231  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
232  char appdata[3];
233 
234  appdata[0] = type;
235 
236  if (start) {
237  *nextid = 0;
238  *nexterror = 2;
239  free(*data);
240  *data = NULL;
241  *pos = 0;
242  *size = 0;
243  }
244 
245  /* Increment to next */
246  if (*data != NULL) {
247  *pos += ((*data)[*pos + 2] << 8) + (*data)[*pos + 3];
248  /* To take in account the space used by the ID and the size
249  * of the record */
250  *pos += 4;
251  }
252 
253  /* Need to fetch new data */
254  if (*pos >= *size) {
255  /* Increment mobex fetch ID only if we really fetch informations */
256  if (! start) {
257  (*nextid)++;
258  }
259  if (*nexterror == 0) {
260  return ERR_EMPTY;
261  }
262  appdata[1] = (*nextid & 0xff00) >> 8;
263  appdata[2] = (*nextid & 0xff);
264  *pos = 0;
265 
266  Priv->m_obex_appdata = appdata;
267  Priv->m_obex_appdata_len = sizeof(appdata);
268 
269  error = OBEXGEN_GetBinaryFile(s, path, data, size);
270 
271  Priv->m_obex_appdata = NULL;
272  Priv->m_obex_appdata_len = 0;
273 
274  *nexterror = Priv->m_obex_error;
275 
276  if (error != ERR_NONE) {
277  return error;
278  }
279  }
280 
281  /* Nothing to return */
282  if (*pos + 4 > *size) {
283  return ERR_EMPTY;
284  }
285  if (*data == NULL) {
286  return ERR_BUG;
287  }
288 
289  /* Return values */
290  *entry = *data + *pos + 4;
291  *location = ((*data)[*pos + 0] << 8) + (*data)[*pos + 1];
292 
293  smprintf(s, "Read data part at %d:\n%s\n", *location, *entry);
294 
295  return ERR_NONE;
296 }
297 
299 {
300  GSM_Error error;
301  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
302  char *data = NULL;
303  size_t pos = 0;
304 
305 
306  error = MOBEX_GetNextEntry(s, "m-obex/contacts/load",
309  &Priv->m_obex_contacts_buffer_size, &data, &(Entry->Location), Entry->MemoryType);
310 
311  smprintf(s, "MOBEX_GetNextEntry returned %s\n", GSM_ErrorString(error));
312  if (error != ERR_NONE) {
313  return error;
314  }
315 
316  error = GSM_DecodeVCARD(&(s->di), data, &pos, Entry, SonyEricsson_VCard21_Phone);
317  /* If we have room in the buffer and next chars are \r and \n then
318  * skip them they have been left by the VCARD parsing
319  */
320 
321  if ((pos < (Priv->m_obex_contacts_buffer_size + 2)) &&
322  data[pos] == 0x0D && data[pos + 1] == 0x0A) {
323  pos += 2;
324  }
325 
326  if (error != ERR_NONE) {
327  return error;
328  }
329 
330  return ERR_NONE;
331 }
332 
334 {
335  GSM_Error error;
336  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
337  char *data = NULL;
338  size_t pos = 0;
339  GSM_ToDoEntry ToDo;
340 
341  error = MOBEX_GetNextEntry(s, "m-obex/calendar/load", start, &Priv->m_obex_calendar_nextid, &Priv->m_obex_calendar_nexterror, &Priv->m_obex_calendar_buffer, &Priv->m_obex_calendar_buffer_pos, &Priv->m_obex_calendar_buffer_size, &data, &(Entry->Location), 1);
342  if (error != ERR_NONE) {
343  return error;
344  }
345 
346  error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, Entry, &ToDo, SonyEricsson_VCalendar, SonyEricsson_VToDo);
347  if (error != ERR_NONE) {
348  return error;
349  }
350 
351  return ERR_NONE;
352 }
353 
355 {
356  GSM_Error error;
357  GSM_Phone_OBEXGENData *Priv = &s->Phone.Data.Priv.OBEXGEN;
358  char *data = NULL;
359  size_t pos = 0;
360  GSM_CalendarEntry skip;
361 
362  error = MOBEX_GetNextEntry(s, "m-obex/calendar/load", start, &Priv->m_obex_calendar_nextid, &Priv->m_obex_calendar_nexterror, &Priv->m_obex_calendar_buffer, &Priv->m_obex_calendar_buffer_pos, &Priv->m_obex_calendar_buffer_size, &data, &(Entry->Location), 2);
363  if (error != ERR_NONE) {
364  return error;
365  }
366 
367  error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, &skip, Entry, SonyEricsson_VCalendar, SonyEricsson_VToDo);
368  if (error != ERR_NONE) {
369  return error;
370  }
371 
372  return ERR_NONE;
373 }
374 
375 
376 #endif
377 
380 
381 /* How should editor hadle tabs in this file? Add editor commands here.
382  * vim: noexpandtab sw=8 ts=8 sts=8:
383  */
GSM_Error MOBEX_GetTodo(GSM_StateMachine *s, GSM_ToDoEntry *Entry)
GSM_Error MOBEX_CreateEntry(GSM_StateMachine *s, const char *path, unsigned char type, int *location, const char *data)
GSM_Error MOBEX_GetMemory(GSM_StateMachine *s, GSM_MemoryEntry *Entry)
size_t m_obex_contacts_buffer_pos
Definition: obexgen.h:271
size_t m_obex_calendar_buffer_size
Definition: obexgen.h:287
GSM_Error MOBEX_UpdateEntry(GSM_StateMachine *s, const char *path, const int location, unsigned char type, const char *data)
unsigned char * m_obex_calendar_buffer
Definition: obexgen.h:279
GSM_MemoryType MemoryType
Definition: gammu-memory.h:415
size_t m_obex_calendar_buffer_pos
Definition: obexgen.h:283
unsigned char * m_obex_appdata
Definition: obexgen.h:243
GSM_Error
Definition: gammu-error.h:23
unsigned char * m_obex_contacts_buffer
Definition: obexgen.h:267
GSM_Debug_Info di
Definition: gsmstate.h:1412
GSM_Error OBEXGEN_SetFile(GSM_StateMachine *s, const char *FileName, const unsigned char *Buffer, size_t Length, gboolean HardDelete)
int gboolean
Definition: gammu-types.h:23
GSM_Phone Phone
Definition: gsmstate.h:1431
#define FALSE
Definition: gammu-types.h:25
struct GSM_Phone_Data::@2 Priv
GSM_Error MOBEX_GetStatus(GSM_StateMachine *s, const char *path, unsigned char type, int *free_records, int *used)
const char * GSM_ErrorString(GSM_Error e)
Definition: gsmcomon.c:183
GSM_Error OBEXGEN_GetTextFile(GSM_StateMachine *s, const char *FileName, char **Buffer)
int m_obex_contacts_nexterror
Definition: obexgen.h:255
int m_obex_calendar_nexterror
Definition: obexgen.h:263
GSM_Error MOBEX_GetNextCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Entry, gboolean start)
GSM_Error GSM_DecodeVCARD(GSM_Debug_Info *di, char *Buffer, size_t *Pos, GSM_MemoryEntry *Pbk, GSM_VCardVersion Version)
Definition: gsmpbk.c:677
GSM_Error GSM_DecodeVCALENDAR_VTODO(GSM_Debug_Info *di, char *Buffer, size_t *Pos, GSM_CalendarEntry *Calendar, GSM_ToDoEntry *ToDo, GSM_VCalendarVersion CalVer, GSM_VToDoVersion ToDoVer)
Definition: gsmcal.c:1673
GSM_Phone_Data Data
Definition: gsmstate.h:1369
size_t m_obex_contacts_buffer_size
Definition: obexgen.h:275
GSM_Error OBEXGEN_GetBinaryFile(GSM_StateMachine *s, const char *FileName, unsigned char **Buffer, size_t *len)
GSM_Error MOBEX_GetNextMemory(GSM_StateMachine *s, GSM_MemoryEntry *Entry, gboolean start)
GSM_Error MOBEX_GetCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Entry)
GSM_Error MOBEX_GetNextTodo(GSM_StateMachine *s, GSM_ToDoEntry *Entry, gboolean start)
int smprintf(GSM_StateMachine *s, const char *format,...)
Definition: debug.c:261