summaryrefslogtreecommitdiff
path: root/src/phonebook.c
blob: fc29d619317234e276a04ca30e841ba6cbd75d28 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Phonebook
 *
 * Copyright (C) 2010 by Multi-Tech Systems
 *
 * Author: James Maki <jmaki@multitech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#include "global.h"
#include "cmd_options.h"
#include "phonebook.h"
#include "utils.h"
#include "atcmd.h"

void phonebook_entry_free(struct phonebook_entry *entry)
{
	free(entry);
}

struct phonebook_entry *phonebook_entry_alloc(void)
{
	return malloc(sizeof(struct phonebook_entry));
}

struct phonebook_entry *phonebook_find_by_name(struct list_head *head,
		const char *name)
{
	struct phonebook_entry *entry;

	list_for_each_entry(entry, head, list) {
		if (!strcmp(entry->name, name)) {
			return entry;
		}
	}

	return NULL;
}

struct phonebook_entry *phonebook_search_names_i(struct list_head *head,
		const char *name)
{
	int tmp;
	struct phonebook_entry *entry;
	int matches_found = 0;

	list_for_each_entry(entry, head, list) {
		if (strstr(entry->name, name)) {
			matches_found++;

			tmp = user_yesno(USER_YESNO_YES, "Select contact '%s' (%s)",
					entry->name, entry->addr);
			if (tmp == USER_YESNO_YES) {
				return entry;
			}
		}
	}

	if (!matches_found) {
		log_notice("no matches for %s found in phonebook", name);
	}

	return NULL;
}

#define CPBR_HEADER_START "+CPBR: "

static int process_header(struct phonebook_list_info *list_info, char *buf, size_t len)
{
	char *save = buf;
	char *token;

	struct phonebook_entry *entry;
	int index;
	char *addr;
	int type;
	char *name;

	token = atcmd_response_brk(&save);
	if (!token) {
		log_debug("response tokenizing failed at start");
		return -1;
	}

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("response tokenizing failed at index");
		return -2;
	}
	index = atoi(token);

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("response tokenizing failed at addr");
		return -3;
	}
	addr = token;

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("response tokenizing failed at type");
		return -4;
	}
	type = atoi(token);

	token = atcmd_value_tok(&save);
	if (!token) {
		log_debug("response tokenizing failed at name");
		return -4;
	}
	name = token;

	entry = phonebook_entry_alloc();
	if (!entry) {
		log_error("out of memory");
		return -6;
	}

	memset(entry, 0, sizeof(*entry));

	entry->index = index;
	snprintf(entry->addr, sizeof(entry->addr), "%s", addr);
	entry->type = type;
	snprintf(entry->name, sizeof(entry->name), "%s", name);

	list_add_tail(&entry->list, &list_info->entry_list);

	list_info->nr_entries++;

	return 0;
}

static int print_list_yaml(struct phonebook_list_info *list_info)
{
	struct phonebook_entry *entry;
	int indent;

	indent = 0;

	indentf(indent, "---\n");
	indentf(indent, "\n");

	indentf(indent, "list:\n");

	list_for_each_entry(entry, &list_info->entry_list, list) {
		indent = YAML_INDENT;

		indentf(indent, "- index: %d\n", entry->index);
		indent += YAML_INDENT;

		indentf(indent, "addr: %s\n", entry->addr);
		indentf(indent, "type: 0x%02X\n", entry->type);
		indentf(indent, "name: %s\n", entry->name);
	}

	indent = 0;

	if (Global.core.verbose) {
		indentf(indent, "\n");

		indentf(indent, "statistics:\n");
		indent += YAML_INDENT;

		indentf(indent, "total: %d\n", list_info->nr_entries);
		indentf(indent, "\n");
	}

	indent = 0;

	indentf(indent, "...\n");

	return 1;
}

enum {
	LIST_STATE_BLANK,
	LIST_STATE_OPEN,
	LIST_STATE_CLOSE,
};

static int list_info_callback(char *buf, size_t len, void *data)
{
	struct phonebook_list_info *list_info = (struct phonebook_list_info *) data;

	list_info->nr_lines++;

	switch (list_info->state) {
	case LIST_STATE_BLANK:
		log_debug("state: LIST_STATE_BLANK");

		if (*buf) {
			list_info->state = LIST_STATE_CLOSE;
			log_debug("AT response error: %s", buf);
			return -1;
		}

		list_info->state = LIST_STATE_OPEN;

		return 0;

	case LIST_STATE_OPEN:
		log_debug("state: LIST_STATE_OPEN");

		if (!strncmp(buf, CPBR_HEADER_START, strlen(CPBR_HEADER_START))) {
			return process_header(list_info, buf, len);
		} else if (!strcmp(buf, "OK")) {
			list_info->state = LIST_STATE_CLOSE;
			return 1;
		} else if (*buf) {
			list_info->state = LIST_STATE_CLOSE;

			log_debug("AT response error: %s", buf);

			return -1;
		} else {
			return 0;
		}

	default:
		return -1;
	}

	return -1;
}

void phonebook_list_free(struct phonebook_list_info *list_info)
{
	struct phonebook_entry *entry;
	struct phonebook_entry *entry_save;

	list_for_each_entry_safe(entry, entry_save, &list_info->entry_list, list) {
		list_del(&entry->list);
		phonebook_entry_free(entry);
	}
}

int phonebook_list_get(int fd, struct phonebook_list_info *list_info)
{
	int tmp;

	INIT_LIST_HEAD(&list_info->entry_list);

	if (*list_info->store_select) {
		tmp = atcmd_plus_cpbs_write(fd, list_info->store_select);
		if (tmp < 0) {
			log_error("failed to get selected store info");
			return -1;
		}
	}
	tmp = atcmd_plus_cpbs_test(fd, &list_info->store.choices);
	if (tmp < 0) {
		log_error("failed to get selected store info");
		return -1;
	}
	tmp = atcmd_plus_cpbs_read(fd, &list_info->store.selected);
	if (tmp < 0) {
		log_error("failed to get selected store info");
		return -1;
	}
	tmp = atcmd_plus_cpbr_test(fd, &list_info->store);
	if (tmp < 0) {
		log_error("failed to get selected store info");
		return -1;
	}

	atcmd_writeline(fd, "AT+CPBR=%d,%d",
			list_info->store.index_min, list_info->store.index_max);
	tmp = atcmd_response_foreach_line(fd, list_info_callback, list_info);
  //CE910 reports an error if empty. Don't report error in this case
	if ((tmp < 0) && strcmp(Global.core.model, "CE910-DUAL")) {
		log_error("listing failed");
		phonebook_list_free(list_info);

		return tmp;
	}

	return 0;
}

int print_phonebook_list(int fd, const char *store)
{
	int tmp;
	struct phonebook_list_info list_info;

	memset(&list_info, 0, sizeof(list_info));

	if (store) {
		strncpy(list_info.store_select, store, STORE_NAME_LEN);
	}

	tmp = phonebook_list_get(fd, &list_info);
	if (tmp < 0) {
		log_error("failed to get entry list");
		return false;
	}

	print_list_yaml(&list_info);

	phonebook_list_free(&list_info);

	return true;
}