1 |
786 |
skrzyp |
/**
|
2 |
|
|
* @file
|
3 |
|
|
* Chat interface
|
4 |
|
|
*
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
/*
|
8 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
9 |
|
|
* All rights reserved.
|
10 |
|
|
*
|
11 |
|
|
* Redistribution and use in source and binary forms, with or without
|
12 |
|
|
* modification, are permitted provided that the following conditions
|
13 |
|
|
* are met:
|
14 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
15 |
|
|
* notice, this list of conditions and the following disclaimer.
|
16 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
17 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
18 |
|
|
* documentation and/or other materials provided with the distribution.
|
19 |
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
20 |
|
|
* may be used to endorse or promote products derived from this software
|
21 |
|
|
* without specific prior written permission.
|
22 |
|
|
*
|
23 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
24 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
25 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
27 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
29 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
31 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
32 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
33 |
|
|
* SUCH DAMAGE.
|
34 |
|
|
*
|
35 |
|
|
* Author: Simon Kallweit <simon.kallweit(at)intefo.ch>
|
36 |
|
|
*/
|
37 |
|
|
|
38 |
|
|
/*
|
39 |
|
|
* This is an arch independent chat implementation. The specific serial hooks
|
40 |
|
|
* must be provided by another file. They are sio_open, sio_recv and sio_send.
|
41 |
|
|
*/
|
42 |
|
|
|
43 |
|
|
#ifndef CHAT_H
|
44 |
|
|
#define CHAT_H
|
45 |
|
|
|
46 |
|
|
#include "lwip/sio.h"
|
47 |
|
|
|
48 |
|
|
#define CHAT_TMR_INTERVAL 500 /**< Chat timer interval in ms */
|
49 |
|
|
|
50 |
|
|
/** Chat commands */
|
51 |
|
|
typedef enum {
|
52 |
|
|
CHAT_ABORT, /**< Defines a string which aborts the chat conversation */
|
53 |
|
|
CHAT_SAY, /**< Prints an informational text */
|
54 |
|
|
CHAT_SEND, /**< Sends a command to the modem */
|
55 |
|
|
CHAT_SEND_CB, /**< Sends a command to the modem (gets string via callback) */
|
56 |
|
|
CHAT_WAIT, /**< Waits for an answer from the modem */
|
57 |
|
|
CHAT_SLEEP, /**< Sleeps for at least 1 second */
|
58 |
|
|
CHAT_LAST, /**< Last item in chat table */
|
59 |
|
|
} chat_cmd_t;
|
60 |
|
|
|
61 |
|
|
/** Chat item */
|
62 |
|
|
struct chat_item {
|
63 |
|
|
chat_cmd_t cmd; /**< Chat command */
|
64 |
|
|
char *arg; /**< Command argument */
|
65 |
|
|
};
|
66 |
|
|
|
67 |
|
|
/** Chat error codes */
|
68 |
|
|
typedef enum {
|
69 |
|
|
CHAT_ERR_OK, /**< Chat finished successfully */
|
70 |
|
|
CHAT_ERR_TIMEOUT, /**< Chat timeout */
|
71 |
|
|
CHAT_ERR_ABORT, /**< Chat aborted */
|
72 |
|
|
} chat_err_t;
|
73 |
|
|
|
74 |
|
|
/** Chat context */
|
75 |
|
|
typedef struct chat *chat_t;
|
76 |
|
|
|
77 |
|
|
/** Chat callback */
|
78 |
|
|
typedef void (*chat_cb_t)(chat_t chat, chat_err_t err, void *arg);
|
79 |
|
|
|
80 |
|
|
/** Chat send callback */
|
81 |
|
|
typedef void (*chat_send_cb_t)(chat_t chat, int id, char *buf, size_t len, void *arg);
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Creates a new chat. The chat is further processed by calling chat_poll().
|
85 |
|
|
* The result of the chat is posted by the chat_cb() callback function.
|
86 |
|
|
* @param sd Serial io descriptor
|
87 |
|
|
* @param items Chat items
|
88 |
|
|
* @param timeout Timeout in seconds
|
89 |
|
|
* @param chat_cb Chat result callback
|
90 |
|
|
* @param send_cb Chat send callback
|
91 |
|
|
* @param arg Argument used in callback
|
92 |
|
|
* @return Returns the newly allocated chat, or NULL if no memory was available.
|
93 |
|
|
*/
|
94 |
|
|
chat_t chat_new(sio_fd_t sd, const struct chat_item *items, int timeout,
|
95 |
|
|
chat_cb_t chat_cb, chat_send_cb_t send_cb, void *arg);
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Frees a chat.
|
99 |
|
|
* @param chat Chat
|
100 |
|
|
*/
|
101 |
|
|
void chat_free(chat_t chat);
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* Processes the chat. This needs to be called iteratively until the chat_cb()
|
105 |
|
|
* callback function posts success or an error.
|
106 |
|
|
* @param chat Chat
|
107 |
|
|
*/
|
108 |
|
|
void chat_poll(chat_t chat);
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* This should be called every CHAT_TMR_INTERVAL milliseconds.
|
112 |
|
|
*/
|
113 |
|
|
void chat_tmr(void);
|
114 |
|
|
|
115 |
|
|
#endif /* CHAT_H */
|