Linux Audio

Check our new training course

Loading...
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
 * @file at.c
 * Generic AT command handling library implementation
 */

/*
 * Copyright (c) 2015-2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include <net/buf.h>

#include "at.h"

static void next_list(struct at_client *at)
{
	if (at->buf[at->pos] == ',') {
		at->pos++;
	}
}

int at_check_byte(struct net_buf *buf, char check_byte)
{
	const char *str = buf->data;

	if (*str != check_byte) {
		return -EINVAL;
	}
	net_buf_pull(buf, 1);

	return 0;
}

static void skip_space(struct at_client *at)
{
	while (at->buf[at->pos] == ' ') {
		at->pos++;
	}
}

int at_get_number(struct at_client *at, uint32_t *val)
{
	uint32_t i;

	skip_space(at);

	for (i = 0, *val = 0; isdigit(at->buf[at->pos]); at->pos++, i++) {
		*val = *val * 10 + at->buf[at->pos] - '0';
	}

	if (i == 0) {
		return -ENODATA;
	}

	next_list(at);
	return 0;
}

static bool str_has_prefix(const char *str, const char *prefix)
{
	if (strncmp(str, prefix, strlen(prefix)) != 0) {
		return false;
	}

	return true;
}

static int at_parse_result(const char *str, struct net_buf *buf,
			   enum at_result *result)
{
	/* Map the result and check for end lf */
	if ((!strncmp(str, "OK", 2)) && (at_check_byte(buf, '\n') == 0)) {
		*result = AT_RESULT_OK;
		return 0;
	}

	if ((!strncmp(str, "ERROR", 5)) && (at_check_byte(buf, '\n')) == 0) {
		*result = AT_RESULT_ERROR;
		return 0;
	}

	return -ENOMSG;
}

static int get_cmd_value(struct at_client *at, struct net_buf *buf,
			 char stop_byte, enum at_cmd_state cmd_state)
{
	int cmd_len = 0;
	uint8_t pos = at->pos;
	const char *str = buf->data;

	while (cmd_len < buf->len && at->pos != at->buf_max_len) {
		if (*str != stop_byte) {
			at->buf[at->pos++] = *str;
			cmd_len++;
			str++;
			pos = at->pos;
		} else {
			cmd_len++;
			at->buf[at->pos] = '\0';
			at->pos = 0;
			at->cmd_state = cmd_state;
			break;
		}
	}
	net_buf_pull(buf, cmd_len);

	if (pos == at->buf_max_len) {
		return -ENOBUFS;
	}

	return 0;
}

static int get_response_string(struct at_client *at, struct net_buf *buf,
			       char stop_byte, enum at_state state)
{
	int cmd_len = 0;
	uint8_t pos = at->pos;
	const char *str = buf->data;

	while (cmd_len < buf->len && at->pos != at->buf_max_len) {
		if (*str != stop_byte) {
			at->buf[at->pos++] = *str;
			cmd_len++;
			str++;
			pos = at->pos;
		} else {
			cmd_len++;
			at->buf[at->pos] = '\0';
			at->pos = 0;
			at->state = state;
			break;
		}
	}
	net_buf_pull(buf, cmd_len);

	if (pos == at->buf_max_len) {
		return -ENOBUFS;
	}

	return 0;
}

static void reset_buffer(struct at_client *at)
{
	memset(at->buf, 0, at->buf_max_len);
	at->pos = 0;
}

static int at_state_start(struct at_client *at, struct net_buf *buf)
{
	int err;

	err = at_check_byte(buf, '\r');
	if (err < 0) {
		return err;
	}
	at->state = AT_STATE_START_CR;

	return 0;
}

static int at_state_start_cr(struct at_client *at, struct net_buf *buf)
{
	int err;

	err = at_check_byte(buf, '\n');
	if (err < 0) {
		return err;
	}
	at->state = AT_STATE_START_LF;

	return 0;
}

static int at_state_start_lf(struct at_client *at, struct net_buf *buf)
{
	reset_buffer(at);
	if (at_check_byte(buf, '+') == 0) {
		at->state = AT_STATE_GET_CMD_STRING;
		return 0;
	} else if (isalpha(*buf->data)) {
		at->state = AT_STATE_GET_RESULT_STRING;
		return 0;
	}

	return -ENODATA;
}

static int at_state_get_cmd_string(struct at_client *at, struct net_buf *buf)
{
	return get_response_string(at, buf, ':', AT_STATE_PROCESS_CMD);
}

static int at_state_process_cmd(struct at_client *at, struct net_buf *buf)
{
	if (at->resp) {
		at->resp(at, buf);
		return 0;
	}
	at->state = AT_STATE_UNSOLICITED_CMD;
	return 0;
}

static int at_state_get_result_string(struct at_client *at, struct net_buf *buf)
{
	return get_response_string(at, buf, '\r', AT_STATE_PROCESS_RESULT);
}

static int at_state_process_result(struct at_client *at, struct net_buf *buf)
{
	enum at_result result;

	if (at_parse_result(at->buf, buf, &result) == 0) {
		if (at->finish) {
			at->finish(at, result);
		}
	}

	/* Reset the state to process unsolicited response */
	at->cmd_state = AT_CMD_START;
	at->state = AT_STATE_START;

	return 0;
}

static int at_state_unsolicited_cmd(struct at_client *at, struct net_buf *buf)
{
	if (at->unsolicited) {
		return at->unsolicited(at, buf);
	}

	return -ENODATA;
}

/* The order of handler function should match the enum at_state */
static handle_parse_input_t parser_cb[] = {
	at_state_start, /* AT_STATE_START */
	at_state_start_cr, /* AT_STATE_START_CR */
	at_state_start_lf, /* AT_STATE_START_LF */
	at_state_get_cmd_string, /* AT_STATE_GET_CMD_STRING */
	at_state_process_cmd, /* AT_STATE_PROCESS_CMD */
	at_state_get_result_string, /* AT_STATE_GET_RESULT_STRING */
	at_state_process_result, /* AT_STATE_PROCESS_RESULT */
	at_state_unsolicited_cmd /* AT_STATE_UNSOLICITED_CMD */
};

int at_parse_input(struct at_client *at, struct net_buf *buf)
{
	int ret;

	while (buf->len) {
		if (at->state < AT_STATE_START || at->state >= AT_STATE_END) {
			return -EINVAL;
		}
		ret = parser_cb[at->state](at, buf);
		if (ret < 0) {
			return ret;
		}
	}

	return 0;
}

static int at_cmd_start(struct at_client *at, struct net_buf *buf,
			const char *prefix, parse_val_t func,
			enum at_cmd_type type)
{
	if (!str_has_prefix(at->buf, prefix)) {
		if (type == AT_CMD_TYPE_NORMAL) {
			at->state = AT_STATE_UNSOLICITED_CMD;
		}
		return -ENODATA;
	}

	reset_buffer(at);
	at->cmd_state = AT_CMD_GET_VALUE;
	return 0;
}

static int at_cmd_get_value(struct at_client *at, struct net_buf *buf,
			    const char *prefix, parse_val_t func,
			    enum at_cmd_type type)
{
	return get_cmd_value(at, buf, '\r', AT_CMD_PROCESS_VALUE);
}

static int at_cmd_process_value(struct at_client *at, struct net_buf *buf,
				const char *prefix, parse_val_t func,
				enum at_cmd_type type)
{
	int ret;

	ret = func(at);
	at->cmd_state = AT_CMD_STATE_END_LF;

	return ret;
}

static int at_cmd_state_end_lf(struct at_client *at, struct net_buf *buf,
			       const char *prefix, parse_val_t func,
			       enum at_cmd_type type)
{
	int err;

	err = at_check_byte(buf, '\n');
	if (err < 0) {
		return err;
	}

	at->cmd_state = AT_CMD_START;
	at->state = AT_STATE_START;
	return 0;
}

/* The order of handler function should match the enum at_cmd_state */
static handle_cmd_input_t cmd_parser_cb[] = {
	at_cmd_start, /* AT_CMD_START */
	at_cmd_get_value, /* AT_CMD_GET_VALUE */
	at_cmd_process_value, /* AT_CMD_PROCESS_VALUE */
	at_cmd_state_end_lf /* AT_CMD_STATE_END_LF */
};

int at_parse_cmd_input(struct at_client *at, struct net_buf *buf,
		       const char *prefix, parse_val_t func,
		       enum at_cmd_type type)
{
	int ret;

	while (buf->len) {
		if (at->cmd_state < AT_CMD_START ||
		    at->cmd_state >= AT_CMD_STATE_END) {
			return -EINVAL;
		}
		ret = cmd_parser_cb[at->cmd_state](at, buf, prefix, func, type);
		if (ret < 0) {
			return ret;
		}
		/* Check for main state, the end of cmd parsing and return. */
		if (at->state == AT_STATE_START ||
		    (at->state == AT_STATE_UNSOLICITED_CMD &&
		     type == AT_CMD_TYPE_UNSOLICITED)) {
			return 0;
		}
	}

	return 0;
}

int at_has_next_list(struct at_client *at)
{
	return at->buf[at->pos] != '\0';
}

int at_open_list(struct at_client *at)
{
	skip_space(at);

	/* The list shall start with '(' open parenthesis */
	if (at->buf[at->pos] != '(') {
		return -ENODATA;
	}
	at->pos++;

	return 0;
}

int at_close_list(struct at_client *at)
{
	skip_space(at);

	if (at->buf[at->pos] != ')') {
		return -ENODATA;
	}
	at->pos++;

	next_list(at);

	return 0;
}

int at_list_get_string(struct at_client *at, char *name, uint8_t len)
{
	int i = 0;

	skip_space(at);

	if (at->buf[at->pos] != '"') {
		return -ENODATA;
	}
	at->pos++;

	while (at->buf[at->pos] != '\0' && at->buf[at->pos] != '"') {
		if (i == len) {
			return -ENODATA;
		}
		name[i++] = at->buf[at->pos++];
	}

	if (i == len) {
		return -ENODATA;
	}

	name[i] = '\0';

	if (at->buf[at->pos] != '"') {
		return -ENODATA;
	}
	at->pos++;

	skip_space(at);
	next_list(at);

	return 0;
}

int at_list_get_range(struct at_client *at, uint32_t *min, uint32_t *max)
{
	uint32_t low, high;
	int ret;

	ret = at_get_number(at, &low);
	if (ret < 0) {
		return ret;
	}

	if (at->buf[at->pos] == '-') {
		at->pos++;
		goto out;
	}

	if (!isdigit(at->buf[at->pos])) {
		return -ENODATA;
	}
out:
	ret = at_get_number(at, &high);
	if (ret < 0) {
		return ret;
	}

	*min = low;
	*max = high;

	next_list(at);

	return 0;
}

void at_register_unsolicited(struct at_client *at, at_resp_cb_t unsolicited)
{
	at->unsolicited = unsolicited;
}

void at_register(struct at_client *at, at_resp_cb_t resp, at_finish_cb_t finish)
{
	at->resp = resp;
	at->finish = finish;
	at->state = AT_STATE_START;
}