summaryrefslogtreecommitdiff
path: root/libgammu/phone/obex/mobex.c
blob: a5803f61a3576c7aad7a762161c8d7e2f0585301 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* (c) 2010 by Michal Cihar */

/**
 * \file obexgen.c
 * @author Michal Čihař
 */
/**
 * @addtogroup Phone
 * @{
 */
/**
 * \defgroup MOBEXPhone m-obex phones communication
 * Implementation of m-obex protocol used by Samsung phones.
 *
 * @author Michal Cihar
 * @{
 */

#include <gammu-config.h>

#include "../../gsmcomon.h"
#include "../../misc/coding/coding.h"
#include "../../gsmphones.h"
#include "../../gsmstate.h"
#include "../../service/gsmmisc.h"
#include "../../protocol/obex/obex.h"
#include "obexfunc.h"
#include "obexgen.h"
#include "mobex.h"

#include <string.h>

#ifdef GSM_ENABLE_OBEXGEN

/**
 * How many read attempts will happen.
 */
#define MOBEX_TIMEOUT 10

GSM_Error MOBEX_GetStatus(GSM_StateMachine *s, const char *path, int *free_records, int *used)
{
	GSM_Error error;
	unsigned char *buffer = NULL;
	int len = 0, total;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char appdata[] = {'\x01'};

	Priv->m_obex_appdata = appdata;
	Priv->m_obex_appdata_len = sizeof(appdata);

	error = OBEXGEN_GetBinaryFile(s, path, &buffer, &len);

	Priv->m_obex_appdata = NULL;
	Priv->m_obex_appdata_len = 0;

	if (error != ERR_NONE) {
		free(buffer);
		return error;
	}

	if (len < 2) {
		smprintf(s, "Unknown length of data file: %d\n", len);
		free(buffer);
		return ERR_UNKNOWNRESPONSE;
	}

	total = (buffer[0] << 8) + buffer[1];

	*used = (buffer[1] << 8) + buffer[3];
	*free_records = total - *used;

	free(buffer);
	return ERR_NONE;
}

GSM_Error MOBEX_CreateEntry(GSM_StateMachine *s, const char *path, int *location, const char *data)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char appdata[] = {'\x01'};

	Priv->m_obex_newid = -1;
	Priv->m_obex_appdata = appdata;
	Priv->m_obex_appdata_len = sizeof(appdata);

	error = OBEXGEN_SetFile(s, path, data, strlen(data), FALSE);

	Priv->m_obex_appdata = NULL;
	Priv->m_obex_appdata_len = 0;

	if (error != ERR_NONE) {
		return error;
	}

	*location = Priv->m_obex_newid;

	return ERR_NONE;
}

GSM_Error MOBEX_UpdateEntry(GSM_StateMachine *s, const char *path, const int location, const char *data)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char appdata[] = {'\x01', 0, 0};

	appdata[1] = (location  && 0xff00) >> 8;
	appdata[2] = (location  && 0xff);

	Priv->m_obex_appdata = appdata;
	Priv->m_obex_appdata_len = sizeof(appdata);

	error = OBEXGEN_SetFile(s, path, data, strlen(data), FALSE);

	Priv->m_obex_appdata = NULL;
	Priv->m_obex_appdata_len = 0;

	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}

GSM_Error MOBEX_GetEntry(GSM_StateMachine *s, const char *path, const int location, char **data)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char appdata[] = {'\x01', 0, 0};

	appdata[1] = (location  && 0xff00) >> 8;
	appdata[2] = (location  && 0xff);

	Priv->m_obex_appdata = appdata;
	Priv->m_obex_appdata_len = sizeof(appdata);

	error = OBEXGEN_GetTextFile(s, path, data);

	Priv->m_obex_appdata = NULL;
	Priv->m_obex_appdata_len = 0;

	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}

GSM_Error MOBEX_GetMemory(GSM_StateMachine *s, GSM_MemoryEntry *Entry)
{
	GSM_Error error;
	char *data = NULL;
	size_t pos = 0;


	error = MOBEX_GetEntry(s, "m-obex/contacts/read", Entry->Location, &data);
	if (error != ERR_NONE) {
		free(data);
		return error;
	}

	error = GSM_DecodeVCARD(&(s->di), data, &pos, Entry, SonyEricsson_VCard21_Phone);
	free(data);
	data = NULL;
	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}

GSM_Error MOBEX_GetCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Entry)
{
	GSM_Error error;
	char *data = NULL;
	size_t pos = 0;
	GSM_ToDoEntry	ToDo;


	error = MOBEX_GetEntry(s, "m-obex/calendar/read", Entry->Location, &data);
	if (error != ERR_NONE) {
		free(data);
		return error;
	}

	error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, Entry, &ToDo, SonyEricsson_VCalendar, SonyEricsson_VToDo);
	free(data);
	data = NULL;
	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}

GSM_Error MOBEX_GetNextEntry(GSM_StateMachine *s, const char *path, const gboolean start, int *nextid, int *nexterror, unsigned char **data, int *pos, int *size, char **entry, int *location)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char appdata[] = {'\x01', 0, 0, '\x0a'};

	if (start) {
		*nextid = 0;
		*nexterror = 2;
		free(*data);
		*data = NULL;
		*pos = 0;
		*size = 0;
	} else {
		*nextid += 1;
	}

	/* Increment to next */
	if (*data != NULL) {
		*pos += (*data[*pos + 2] << 8) + *data[*pos + 3];
	}

	/* Need to fetch new data */
	if (*pos >= *size) {
		if (*nexterror == 0) {
			return ERR_EMPTY;
		}
		appdata[1] = (*nextid && 0xff00) >> 8;
		appdata[2] = (*nextid && 0xff);

		Priv->m_obex_appdata = appdata;
		Priv->m_obex_appdata_len = sizeof(appdata);

		error = OBEXGEN_GetBinaryFile(s, path, data, size);

		Priv->m_obex_appdata = NULL;
		Priv->m_obex_appdata_len = 0;

		*nexterror = Priv->m_obex_error;

		if (error != ERR_NONE) {
			return error;
		}
	}

	/* Nothing to return */
	if (*pos + 4 > *size) {
		return ERR_EMPTY;
	}

	/* Return values */
	*entry = *data + *pos + 4;
	*location = (*data[*pos + 0] << 8) + *data[*pos + 1];

	return ERR_NONE;
}

GSM_Error MOBEX_GetNextMemory(GSM_StateMachine *s, GSM_MemoryEntry *Entry, gboolean start)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char *data = NULL;
	size_t pos = 0;


	error = MOBEX_GetNextEntry(s, "m-obex/contacts/load", start, &Priv->m_obex_contacts_nextid, &Priv->m_obex_contacts_nexterror, &Priv->m_obex_contacts_buffer, &Priv->m_obex_contacts_buffer_pos, &Priv->m_obex_contacts_buffer_size, &data, &(Entry->Location));
	if (error != ERR_NONE) {
		return error;
	}

	error = GSM_DecodeVCARD(&(s->di), data, &pos, Entry, SonyEricsson_VCard21_Phone);
	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}

GSM_Error MOBEX_GetNextCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Entry, gboolean start)
{
	GSM_Error error;
	GSM_Phone_OBEXGENData	*Priv = &s->Phone.Data.Priv.OBEXGEN;
	char *data = NULL;
	size_t pos = 0;
	GSM_ToDoEntry	ToDo;

	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));
	if (error != ERR_NONE) {
		return error;
	}

	error = GSM_DecodeVCALENDAR_VTODO(&(s->di), data, &pos, Entry, &ToDo, SonyEricsson_VCalendar, SonyEricsson_VToDo);
	if (error != ERR_NONE) {
		return error;
	}

	return ERR_NONE;
}


#endif

/*@}*/
/*@}*/

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