summaryrefslogtreecommitdiff
path: root/ecos/packages/net/lwip_tcpip/current/src/netif/ppp/chat.c
blob: 141cb7156e714be817d9d19197620e64c015261f (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
327
328
329
330
331
332
/**
 * @file
 * Chat implementation
 *
 */

/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Simon Kallweit <simon.kallweit(at)intefo.ch>
 */

/*
 * This is an arch independent chat implementation. The specific serial hooks
 * must be provided by another file. They are sio_open, sio_recv and sio_send.
 */

#include "netif/ppp/chat.h"
#include "lwip/opt.h"

#include "lwip/sys.h"
#include "lwip/mem.h"
#include "lwip/sio.h"

#define BUFSIZE 64

/** Chat states */
enum chat_state {
  STATE_INITIAL,    /**< Initial state, checks the current item, executes it
                         directly or transitions to a new state to handle the
                         item. */
  STATE_WAIT,       /**< Waits for an answer from the modem. */
  STATE_SLEEP,      /**< Sleep for at least 1 second. */
  STATE_NEXT_ITEM,  /**< Moves to the next item. */
  STATE_IDLE,       /**< Chat finished. */
};

/** Chat */
struct chat {
  struct chat *next;        /**< Pointer to the next chat instance */
  /* Settings */
  sio_fd_t sd;              /**< Serial device descriptor */
  const struct chat_item const *items; /**< Chat items */
  int timeout;              /**< Default timeout in seconds */
  chat_cb_t chat_cb;        /**< Chat callback function */
  chat_send_cb_t send_cb;   /**< Chat send callback function */
  void *arg;                /**< Argument for callback function */
  /* State */
  enum chat_state state;    /**< Current state */
  const struct chat_item *item; /**< Current chat item */
  char buf[BUFSIZE + 1];   /**< Receive buffer */
  int recvd;                /**< Received bytes */
  int timeleft;             /**< Seconds left until timeout */
};

/** Linked list of chats */
static struct chat *chats;

static void chat_send(struct chat *chat, const struct chat_item *item);
static void chat_send_cb(struct chat *chat, const struct chat_item *item);
static int chat_wait(struct chat *chat, const struct chat_item *item);

/*
 * Creates a new chat. The chat is further processed by calling chat_poll().
 * The result of the chat is posted by the chat_cb() callback function.
 */
chat_t chat_new(sio_fd_t sd, const struct chat_item const *items, int timeout,
                chat_cb_t chat_cb, chat_send_cb_t send_cb, void *arg)
{
  struct chat *chat;
  const struct chat_item *item;

  /* Some sanity checks */
  LWIP_ASSERT("chat_new: sd != NULL", sd != NULL);
  LWIP_ASSERT("chat_new: items != NULL", items != NULL);
  LWIP_ASSERT("chat_new: chat_cb != NULL", chat_cb != NULL);

  /* Check for correct chat items */
  for (item = items; item->cmd != CHAT_LAST; item++)
    LWIP_ASSERT("chat_new: invalid chat item or CHAT_LAST missing",
                item->cmd >= CHAT_ABORT && item->cmd < CHAT_LAST);

  /* Allocate chat */
  chat = mem_malloc(sizeof(struct chat));
  if (!chat)
    return NULL;

  /* Iinitialize chat */
  chat->sd = sd;
  chat->items = items;
  chat->timeout = timeout * 1000 + 1;
  chat->chat_cb = chat_cb;
  chat->send_cb = send_cb;
  chat->arg = arg;

  /* Reset state */
  chat->state = STATE_INITIAL;
  chat->item = items;
  chat->recvd = 0;

  /* Add chat to list */
  chat->next = chats;
  chats = chat;

  return chat;
}

/*
 * Frees a chat.
 */
void chat_free(struct chat *chat)
{
  struct chat *t;

  if (!chat)
    return;

  /* Remove chat from list */
  if (chat == chats) {
    chats = chat->next;
  } else {
    for (t = chats; t->next; t = t->next)
      if (t->next == chat) {
        t->next = chat->next;
        break;
      }
  }

  mem_free(chat);
}

/*
 * Processes the chat. This needs to be called iteratively until the chat_cb()
 * callback function posts success or an error.
 */
void chat_poll(struct chat *chat)
{
  int done = 0;
  int err;

  LWIP_ASSERT("chat_poll: chat != NULL", chat != NULL);

  while (!done) {
    switch (chat->state) {
    case STATE_INITIAL:
      /* Check item command */
      switch (chat->item->cmd) {
      case CHAT_ABORT:
        chat->state = STATE_NEXT_ITEM;
        break;
      case CHAT_SAY:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: SAY '%s'\n", chat->item->arg));
        chat->state = STATE_NEXT_ITEM;
        break;
      case CHAT_SEND:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: SEND '%s'\n", chat->item->arg));
        chat_send(chat, chat->item);
        chat->state = STATE_NEXT_ITEM;
        break;
      case CHAT_SEND_CB:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: SEND_CB '%d'\n", (int) chat->item->arg));
        chat_send_cb(chat, chat->item);
        chat->state = STATE_NEXT_ITEM;
        break;
      case CHAT_WAIT:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: WAIT '%s'\n", chat->item->arg));
        chat->timeleft = chat->timeout;
        chat->state = STATE_WAIT;
        break;
      case CHAT_SLEEP:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: SLEEP\n"));
        chat->timeleft = 1000 + 1;
        if (chat->item->arg)
          chat->timeleft = (int) chat->item->arg + 1;
        chat->state = STATE_SLEEP;
        break;
      case CHAT_LAST:
        LWIP_DEBUGF(PPP_DEBUG, ("chat_poll: SUCCESS\n"));
        chat->chat_cb(chat, CHAT_ERR_OK, chat->arg);
        chat->state = STATE_IDLE;
        break;
      }
      break;
    case STATE_WAIT:
      err = chat_wait(chat, chat->item);
      if (err == -1) {
        done = 1;
      } else if (err == CHAT_ERR_OK) {
        chat->state = STATE_NEXT_ITEM;
      } else {
        chat->chat_cb(chat, err, chat->arg);
        chat->state = STATE_IDLE;
      }
      break;
    case STATE_SLEEP:
      if (chat->timeleft == 0)
        chat->state = STATE_NEXT_ITEM;
      else
        done = 1;
      break;
    case STATE_NEXT_ITEM:
      chat->item++;
      chat->state = STATE_INITIAL;
      break;
    case STATE_IDLE:
      done = 1;
      break;
    }
  }
}

/*
 * This should be called every CHAT_TMR_INTERVAL milliseconds.
 */
void chat_tmr(void)
{
  struct chat *chat;

  LWIP_DEBUGF(PPP_DEBUG, ("chat_tmr\n"));

  for (chat = chats; chat; chat = chat->next) {
    if (chat->timeleft > CHAT_TMR_INTERVAL)
      chat->timeleft -= CHAT_TMR_INTERVAL;
    else
      chat->timeleft = 0;
    LWIP_DEBUGF(PPP_DEBUG, ("chat_tmr: %d\n", chat->timeleft));
  }
}


/**
 * Sends a chat message to the modem.
 * @param chat Chat
 * @param item Item to send
 */
static void chat_send(struct chat *chat, const struct chat_item *item)
{
  sio_write(chat->sd, (u8_t *) item->arg, strlen(item->arg));
  chat->recvd = 0;
}

/**
 * Sends a chat message to the modem. Gets the send string via a callback.
 * @param chat Chat
 * @param item Item to send
 */
static void chat_send_cb(struct chat *chat, const struct chat_item *item)
{
  LWIP_ASSERT("chat_poll: chat->send_cb != NULL", chat->send_cb != NULL);
  
  // Get the command to send
  chat->send_cb(chat, (int) item->arg, chat->buf, BUFSIZE, chat->arg);
  chat->buf[BUFSIZE] = '\0';
  
  // Send command
  sio_write(chat->sd, (u8_t *) chat->buf, strlen(chat->buf));
  chat->recvd = 0;
}

/**
 * Waits for an response from the modem. This function returns:
 *   -1: when no more data could be read from the serial (polling)
 *   CHAT_ERR_OK: when the correct response was received
 *   CHAT_ERR_ABORT: when an abort response was received
 *   CHAT_ERR_TIMEOUT: when a timeout occured
 * @param chat Chat
 * @param item Item to wait for
 * @return See above.
 */
static int chat_wait(struct chat *chat, const struct chat_item *item)
{
  int count;
  const struct chat_item *abort;

  while (chat->recvd < BUFSIZE) {
    /* Receive data from serial */
    if ((count = sio_read(chat->sd, (u8_t *) &chat->buf[chat->recvd],
                          BUFSIZE - chat->recvd)) < 1)
      break;
    chat->recvd += count;
    chat->buf[chat->recvd] = '\0';

    /* Check abort strings */
    for (abort = chat->items; abort->cmd != CHAT_LAST; abort++) {
      if (abort->cmd != CHAT_ABORT)
        continue;
      if (strstr(chat->buf, abort->arg)) {
        LWIP_DEBUGF(PPP_DEBUG, ("chat_wait: ABORT '%s'\n", abort->arg));
        return CHAT_ERR_ABORT;
      }
    }

    /* Check wait string */
    if (strstr(chat->buf, item->arg)) {
      LWIP_DEBUGF(PPP_DEBUG, ("chat_wait: SUCCESS\n"));
      return CHAT_ERR_OK;
    }
  }

  /* Check for timeout */
  if (chat->timeleft == 0) {
    LWIP_DEBUGF(PPP_DEBUG, ("chat_wait: TIMEOUT\n"));
    return CHAT_ERR_TIMEOUT;
  }

  return -1;
}