AT driver (master) and example
This commit is contained in:
parent
fd15934235
commit
2b30370b42
6 changed files with 833 additions and 0 deletions
1
apps/at-master/Makefile.at-master
Normal file
1
apps/at-master/Makefile.at-master
Normal file
|
@ -0,0 +1 @@
|
|||
at-master_src = at-master.c
|
148
apps/at-master/at-master.c
Normal file
148
apps/at-master/at-master.c
Normal file
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "contiki-lib.h"
|
||||
#include "at-master.h"
|
||||
#include "cpu.h"
|
||||
#include "dev/uart.h"
|
||||
#include "dev/serial-line.h"
|
||||
#include "dev/sys-ctrl.h"
|
||||
#include "lib/list.h"
|
||||
#include "sys/cc.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
/*---------------------------------------------------------------------------*/
|
||||
LIST(at_cmd_list);
|
||||
process_event_t at_cmd_received_event;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static uint8_t at_uart = 0;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(at_process, "AT process");
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(at_process, ev, data)
|
||||
{
|
||||
uint8_t plen;
|
||||
char *pch, *buf;
|
||||
struct at_cmd *a;
|
||||
PROCESS_BEGIN();
|
||||
|
||||
while(1) {
|
||||
PROCESS_WAIT_EVENT_UNTIL(ev == serial_line_event_message && data != NULL);
|
||||
buf = (char *)data;
|
||||
plen = strlen(buf);
|
||||
for(a = list_head(at_cmd_list); a != NULL; a = list_item_next(a)) {
|
||||
pch = strstr(buf, a->cmd_header);
|
||||
if((plen <= a->cmd_max_len) && (pch != NULL)) {
|
||||
if(strncmp(a->cmd_header, pch, a->cmd_hdr_len) == 0) {
|
||||
if((a->cmd_hdr_len == plen) || (a->cmd_max_len > a->cmd_hdr_len)) {
|
||||
a->event_callback(a, plen, (char *)pch);
|
||||
process_post(a->app_process, at_cmd_received_event, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct at_cmd *
|
||||
at_list(void)
|
||||
{
|
||||
return list_head(at_cmd_list);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
uint8_t
|
||||
at_send(char *s, uint8_t len)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
while(s && *s != 0) {
|
||||
if(i >= len) {
|
||||
break;
|
||||
}
|
||||
uart_write_byte(at_uart, *s++);
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
at_init(uint8_t uart_sel)
|
||||
{
|
||||
static uint8_t inited = 0;
|
||||
if(!inited) {
|
||||
list_init(at_cmd_list);
|
||||
at_cmd_received_event = process_alloc_event();
|
||||
inited = 1;
|
||||
|
||||
at_uart = uart_sel;
|
||||
uart_init(at_uart);
|
||||
uart_set_input(at_uart, serial_line_input_byte);
|
||||
serial_line_init();
|
||||
|
||||
process_start(&at_process, NULL);
|
||||
PRINTF("AT: Started (%u)\n", at_uart);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
at_status_t
|
||||
at_register(struct at_cmd *cmd, struct process *app_process,
|
||||
const char *cmd_hdr, const uint8_t hdr_len,
|
||||
const uint8_t cmd_max_len, at_event_callback_t event_callback)
|
||||
{
|
||||
if((hdr_len < 1) || (cmd_max_len < 1) || (!strncmp(cmd_hdr, "AT", 2) == 0) ||
|
||||
(event_callback == NULL)) {
|
||||
PRINTF("AT: Invalid argument\n");
|
||||
return AT_STATUS_INVALID_ARGS_ERROR;
|
||||
}
|
||||
|
||||
memset(cmd, 0, sizeof(struct at_cmd));
|
||||
cmd->event_callback = event_callback;
|
||||
cmd->cmd_header = cmd_hdr;
|
||||
cmd->cmd_hdr_len = hdr_len;
|
||||
cmd->cmd_max_len = cmd_max_len;
|
||||
cmd->app_process = app_process;
|
||||
list_add(at_cmd_list, cmd);
|
||||
PRINTF("AT: registered HDR %s LEN %u MAX %u\n", cmd->cmd_header,
|
||||
cmd->cmd_hdr_len,
|
||||
cmd->cmd_max_len);
|
||||
return AT_STATUS_OK;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
123
apps/at-master/at-master.h
Normal file
123
apps/at-master/at-master.h
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
#ifndef AT_MASTER_H_
|
||||
#define AT_MASTER_H_
|
||||
#include "contiki.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define AT_DEFAULT_RESPONSE_OK "\r\nOK\r\n"
|
||||
#define AT_DEFAULT_RESPONSE_ERROR "\r\nERROR\r\n"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define AT_RESPONSE(x) at_send((x), (strlen(x)))
|
||||
/*---------------------------------------------------------------------------*/
|
||||
extern process_event_t at_cmd_received_event;
|
||||
struct at_cmd;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
typedef enum {
|
||||
AT_STATUS_OK,
|
||||
AT_STATUS_ERROR,
|
||||
AT_STATUS_INVALID_ARGS_ERROR,
|
||||
} at_status_t;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief AT initialization
|
||||
* \param uart selects which UART to use
|
||||
*
|
||||
* The AT driver invokes this function upon registering a command, this will
|
||||
* wait for the serial_line_event_message event
|
||||
*/
|
||||
void at_init(uint8_t uart);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief AT initialization
|
||||
* \param uart selects which UART to use
|
||||
*
|
||||
* The AT driver invokes this function upon registering a command, this will
|
||||
* wait for the serial_line_event_message event
|
||||
*/
|
||||
uint8_t at_send(char *s, uint8_t len);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief AT event callback
|
||||
* \param cmd A pointer to the AT command placeholder
|
||||
* \param len Lenght of the received data (including the AT command header)
|
||||
* \param data A user-defined pointer
|
||||
*
|
||||
* The AT event callback function gets called whenever there is an
|
||||
* event on an incoming AT command
|
||||
*/
|
||||
typedef void (*at_event_callback_t)(struct at_cmd *cmd,
|
||||
uint8_t len,
|
||||
char *data);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct at_cmd {
|
||||
struct at_cmd *next;
|
||||
const char *cmd_header;
|
||||
uint8_t cmd_hdr_len;
|
||||
uint8_t cmd_max_len;
|
||||
at_event_callback_t event_callback;
|
||||
struct process *app_process;
|
||||
};
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Registers the callback to return an AT command
|
||||
* \param cmd A pointer to the CMD placeholder
|
||||
* \param cmd_hdr String to compare when an AT command is received
|
||||
* \param cmd_len Lenght of cmd_hdr
|
||||
* \param event_callback Callback function to handle the AT command
|
||||
* \return AT_STATUS_OK or AT_STATUS_INVALID_ARGS_ERROR
|
||||
*
|
||||
* Register the commands to search for when a valid AT frame has been received
|
||||
*/
|
||||
at_status_t at_register(struct at_cmd *cmd,
|
||||
struct process *app_process,
|
||||
const char *cmd_hdr,
|
||||
const uint8_t cmd_hdr_len,
|
||||
const uint8_t cmd_max_len,
|
||||
at_event_callback_t event_callback);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct at_cmd *at_list(void);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* \brief Registers the callback to return an AT command
|
||||
* \param cmd A pointer to the CMD placeholder
|
||||
* \param cmd_hdr String to compare when an AT command is received
|
||||
* \param cmd_len Lenght of cmd_hdr
|
||||
* \param event_callback Callback function to handle the AT command
|
||||
* \return AT_STATUS_OK or AT_STATUS_INVALID_ARGS_ERROR
|
||||
*
|
||||
* Register the commands to search for when a valid AT frame has been received
|
||||
*/
|
||||
at_status_t at_register(struct at_cmd *cmd,
|
||||
struct process *app_process,
|
||||
const char *cmd_hdr,
|
||||
const uint8_t cmd_hdr_len,
|
||||
const uint8_t cmd_max_len,
|
||||
at_event_callback_t event_callback);
|
||||
#endif /* AT_MASTER_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue