<FileType>1</FileType>
<FilePath>..\user\user_wifi.c</FilePath>
</File>
+ <File>
+ <FileName>at_parser.c</FileName>
+ <FileType>1</FileType>
+ <FilePath>..\user\at_parser.c</FilePath>
+ </File>
</Files>
</Group>
<Group>
self.btn_select_fw.released.connect( self.on_btn_select_file )
self.btn_download.released.connect( self.on_btn_fw_download )
+ self.btn_files_select.released.connect( self.on_btn_files_select )
+ self.btn_files_clear.released.connect( self.on_btn_files_clear )
+ self.listWidget_log_files.itemClicked.connect( self.on_show_log )
+
+ def draw_image( self, data ):
+ if len( data ) == 10000:
+ x_data=range( 5000 )
+ y_data = []
+ curr_sum = 0
+ for i in range( 5000 ):
+ curr_raw = struct.unpack( '>h', data[i*2:i*2+2])[0]
+ print( '%d:%02X %02X - %d'%( i, data[i*2], data[i*2+1], curr_raw) )
+ curr_sum += abs( curr_raw )
+ y_data.append( curr_raw )
+ y_average = [curr_sum/5000]*5000
+ self.curve_curr_raw.setData( np.array( x_data ), np.array( y_data ))
+
+ def on_show_log( self, item ):
+ print( "on show log ", item, item.text() )
+ file_path = item.text()
+ with open( file_path, "rb" ) as fd:
+ file_data = fd.read()
+ #self.on_rx_pkt( file_data )
+ print( "to draw image, data size", len( file_data ) )
+ print( file_data.hex() )
+ self.draw_image( file_data )
+ print( "after draw image " )
+ def on_btn_files_select( self ):
+ #select File
+ file , check = QFileDialog.getOpenFileNames(None, u"选择固件", "", "All Files (202*)")
+ print( "file:", file, check )
+ self.listWidget_log_files.addItems( file )
+ pass
+ def on_btn_files_clear( self ):
+ pass
+
def parse_hex( self, path ):
try:
self.lineEdit_fw_path.setText( path )
</rect>
</property>
<property name="currentIndex">
- <number>0</number>
+ <number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<property name="geometry">
<rect>
<x>0</x>
- <y>480</y>
+ <y>240</y>
<width>1151</width>
- <height>321</height>
+ <height>561</height>
</rect>
</property>
<property name="frameShape">
<x>130</x>
<y>0</y>
<width>931</width>
- <height>471</height>
+ <height>231</height>
</rect>
</property>
</widget>
--- /dev/null
+/*
+ * Copyright (C) 2015-2017 Alibaba Group Holding Limited
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "user.h"
+#include "at_parser.h"
+
+#define OOB_MAX 5
+
+#define HAL_MutexCreate() osMutexNew(NULL)
+#define HAL_MutexDestroy(mutex) osMutexRelease(mutex)
+#define HAL_MutexLock(mutex) osMutexAcquire(mutex, portMAX_DELAY )
+#define HAL_MutexUnlock(mutex) osMutexRelease(mutex)
+
+#define HAL_AT_SemaphoreCreate() osSemaphoreNew(10, 0, NULL )
+#define HAL_SemaphoreDestroy(sem) osSemaphoreDelete(sem)
+#define HAL_SemaphoreWait(sem, time ) osSemaphoreAcquire( sem, time )
+#define HAL_SemaphorePost(sem) osSemaphoreRelease(sem)
+
+#define HAL_Printf DBG_PRINTF
+
+extern UART_HandleTypeDef huart5;
+#define wifi_uart huart5
+
+//#define AT_DEBUG_MODE 1 //DEBUG
+typedef UART_HandleTypeDef at_uart_dev_t;
+
+typedef struct oob_s
+{
+ char * prefix;
+ char * postfix;
+ char * oobinputdata;
+ uint32_t reallen;
+ uint32_t maxlen;
+ at_recv_cb cb;
+ void * arg;
+} oob_t;
+
+/*
+ * --> | slist | --> | slist | --> NULL
+ * --------- ---------
+ * | smhr | | smpr |
+ * --------- ---------
+ * | rsp | | rsp |
+ * --------- ---------
+ */
+
+#include "infra_list.h"
+typedef struct at_task_s
+{
+ slist_t next;
+ void * smpr;
+ char * command;
+ char * rsp;
+ char * rsp_prefix;
+ char * rsp_success_postfix;
+ char * rsp_fail_postfix;
+ uint32_t rsp_prefix_len;
+ uint32_t rsp_success_postfix_len;
+ uint32_t rsp_fail_postfix_len;
+ uint32_t rsp_offset;
+ uint32_t rsp_len;
+} at_task_t;
+
+
+/**
+ * Parser structure for parsing AT commands
+ */
+typedef struct
+{
+ at_uart_dev_t *_pstuart;
+ int _timeout;
+ char * _default_recv_prefix;
+ char * _default_recv_success_postfix;
+ char * _default_recv_fail_postfix;
+ char * _send_delimiter;
+ int _recv_prefix_len;
+ int _recv_success_postfix_len;
+ int _recv_fail_postfix_len;
+ int _send_delim_size;
+ oob_t _oobs[OOB_MAX];
+ int _oobs_num;
+ void * at_uart_recv_mutex;
+ void * at_uart_send_mutex;
+ void * task_mutex;
+ slist_t task_l;
+} at_parser_t;
+
+#define TASK_DEFAULT_WAIT_TIME 5000
+
+#ifndef AT_WORKER_STACK_SIZE
+#define AT_WORKER_STACK_SIZE (1024*4) //debug
+#endif
+
+#ifndef AT_UART_TIMEOUT_MS
+#define AT_UART_TIMEOUT_MS 1000
+#endif
+
+#ifndef AT_CMD_DATA_INTERVAL_MS
+#define AT_CMD_DATA_INTERVAL_MS 0
+#endif
+
+#ifdef AT_DEBUG_MODE
+#define atpsr_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
+#define atpsr_warning(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
+#define atpsr_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
+#define atpsr_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
+#else
+#define atpsr_err(...)
+#define atpsr_warning(...)
+#define atpsr_info(...)
+#define atpsr_debug(...)
+#endif
+
+static uint8_t inited = 0;
+
+static at_parser_t at;
+
+static void at_worker(void *arg);
+
+static at_task_t g_at_task;
+
+static int at_init_uart()
+{
+
+ at._pstuart = &wifi_uart;
+ return 0;
+}
+
+static void at_set_timeout(int timeout)
+{
+ at._timeout = timeout;
+}
+
+static void at_set_recv_delimiter(const char *recv_prefix,
+ const char *recv_success_postfix,
+ const char *recv_fail_postfix)
+{
+ at._default_recv_prefix = (char *)recv_prefix;
+ at._default_recv_success_postfix = (char *)recv_success_postfix;
+ at._default_recv_fail_postfix = (char *)recv_fail_postfix;
+ at._recv_prefix_len = strlen(recv_prefix);
+ at._recv_success_postfix_len = strlen(recv_success_postfix);
+ at._recv_fail_postfix_len = strlen(recv_fail_postfix);
+}
+
+static void at_set_send_delimiter(const char *delimiter)
+{
+ at._send_delimiter = (char *)delimiter;
+ at._send_delim_size = strlen(delimiter);
+}
+
+static int at_init_task_mutex()
+{
+ at.task_mutex = HAL_MutexCreate();
+ if (NULL == at.task_mutex) {
+ atpsr_err("Creating task mutex failed\r\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static void at_deinit_task_mutex()
+{
+ if (at.task_mutex) {
+ HAL_MutexDestroy(at.task_mutex);
+ }
+ return;
+}
+
+static int at_init_uart_recv_mutex()
+{
+ at.at_uart_recv_mutex = HAL_MutexCreate();
+ if (NULL == at.at_uart_recv_mutex) {
+ atpsr_err("Creating at_uart_recv_mutex failed\r\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static void at_deinit_uart_recv_mutex()
+{
+ if (at.at_uart_recv_mutex) {
+ HAL_MutexDestroy(at.at_uart_recv_mutex);
+ }
+ return;
+}
+
+static int at_worker_uart_send_mutex_init()
+{
+ at.at_uart_send_mutex = HAL_MutexCreate();
+ if (NULL == at.at_uart_send_mutex) {
+ atpsr_err("Creating at worker sem failed\r\n");
+ return -1;
+ }
+ return 0;
+}
+
+static void at_worker_uart_send_mutex_deinit()
+{
+ if (at.at_uart_send_mutex) {
+ HAL_MutexDestroy(at.at_uart_send_mutex);
+ }
+}
+
+static const osThreadAttr_t atworker_attributes = {
+ .name = "atWork",
+ .stack_size = 1024,
+ .priority = (osPriority_t) osPriorityNormal,
+};
+int at_parser_init(void)
+{
+ char *recv_prefix = AT_RECV_PREFIX;
+ char *recv_success_postfix = AT_RECV_SUCCESS_POSTFIX;
+ char *recv_fail_postfix = AT_RECV_FAIL_POSTFIX;
+ char *send_delimiter = AT_SEND_DELIMITER;
+ int timeout = AT_UART_TIMEOUT_MS;
+
+ void *task;
+ int stack_used;
+
+
+printf( "%s\n", __func__ );
+
+ if (inited == 1) {
+ atpsr_info("have already inited ,it will init again\r\n");
+ return -1;
+ }
+
+ memset(&at, 0, sizeof(at_parser_t));
+
+ if (at_init_uart() != 0) {
+ atpsr_err("at uart init fail \r\n");
+ return -1;
+ }
+
+ memset(at._oobs, 0, sizeof(oob_t) * OOB_MAX);
+
+ at_set_timeout(timeout);
+ at_set_recv_delimiter(recv_prefix, recv_success_postfix, recv_fail_postfix);
+ at_set_send_delimiter(send_delimiter);
+
+ if (at_init_uart_recv_mutex() != 0) {
+ atpsr_err("at_uart_recv_mutex init fail \r\n");
+ return -1;
+ }
+
+ if (at_init_task_mutex() != 0) {
+ at_deinit_uart_recv_mutex();
+ atpsr_err("at mutex init fail \r\n");
+ return -1;
+ }
+
+ if (at_worker_uart_send_mutex_init() != 0) {
+ at_deinit_uart_recv_mutex();
+ at_deinit_task_mutex();
+ atpsr_err("fail to creat at worker sem\r\n");
+ }
+
+ slist_init(&at.task_l);
+
+
+
+ if( !osThreadNew(at_worker, NULL, &atworker_attributes) ){
+ at_deinit_uart_recv_mutex();
+ at_deinit_task_mutex();
+ at_worker_uart_send_mutex_deinit();
+ atpsr_err("fail to creat at task\r\n");
+ return -1;
+ }
+
+ uint32_t start_tick = xTaskGetTickCount();
+ while( xTaskGetTickCount() - start_tick < 1000 && !inited ){
+ //wait inited
+ osDelay(10);
+ }
+ return 0;
+}
+
+static int at_sendto_lower(at_uart_dev_t *uart, void *data, uint32_t size,
+ uint32_t timeout, bool ackreq)
+{
+ int ret = -1;
+
+ (void) ackreq;
+ ret = HAL_UART_Transmit( uart, data, size, timeout );
+ return ret==HAL_OK?0:-1;
+}
+
+static int at_recvfrom_lower(at_uart_dev_t *uart, void *data, uint32_t expect_size,
+ uint32_t *recv_size, uint32_t timeout)
+{
+ extern int32_t wifi_read_byte( uint8_t *rd_byte );
+
+ int ret = -1;
+
+ uint8_t rd_byte;
+ uint32_t rd_size = 0;
+ uint8_t* rd_ptr = (uint8_t*)data;
+ uint32_t t = xTaskGetTickCount();
+
+ while( rd_size < expect_size && xTaskGetTickCount()-t < timeout ){
+ if( wifi_read_byte( &rd_byte ) == 0 ){
+ *rd_ptr++ = rd_byte;
+ rd_size++;
+ t = xTaskGetTickCount();
+ }
+ }
+ if( rd_size == 0 )
+ return -1;
+
+ *recv_size = rd_size;
+ return 0;
+}
+
+static int at_worker_task_add(at_task_t *tsk)
+{
+ if (NULL == tsk) {
+ atpsr_err("invalid input %s \r\n", __func__);
+ return -1;
+ }
+
+ atpsr_err("at_work task add %x \r\n", tsk );
+
+ HAL_MutexLock(at.task_mutex);
+ slist_add_tail(&tsk->next, &at.task_l);
+ HAL_MutexUnlock(at.task_mutex);
+
+ return 0;
+}
+
+static int at_worker_task_del(at_task_t *tsk)
+{
+ if (NULL == tsk) {
+ atpsr_err("invalid input %s \r\n", __func__);
+ return -1;
+ }
+
+ HAL_MutexLock(at.task_mutex);
+ slist_del(&tsk->next, &at.task_l);
+ HAL_MutexUnlock(at.task_mutex);
+ if (tsk->smpr) {
+ HAL_SemaphoreDestroy(tsk->smpr);
+ }
+ if (tsk) {
+ }
+
+ return 0;
+}
+
+
+int at_send_wait_reply(const char *cmd, int cmdlen, bool delimiter,
+ const char *data, int datalen,
+ char *replybuf, int bufsize,
+ const atcmd_config_t *atcmdconfig)
+{
+ int ret = 0;
+ int intval_ms = AT_CMD_DATA_INTERVAL_MS;
+ HAL_Printf( "at_send_wait_replay-- cmd:{%s} data:{%s},len:%d\r\n", cmd, data?data:"Null",datalen );
+ at_task_t *tsk;
+
+ if (inited == 0) {
+ atpsr_err("at have not init yet\r\n");
+ return -1;
+ }
+
+ if (NULL == cmd || cmdlen <= 0) {
+ atpsr_err("%s invalid input \r\n", __FUNCTION__);
+ return -1;
+ }
+
+ if (NULL == replybuf || 0 == bufsize) {
+ atpsr_err("%s invalid input \r\n", __FUNCTION__);
+ return -1;
+ }
+
+ HAL_MutexLock(at.at_uart_send_mutex);
+ tsk = &g_at_task;
+
+ if (NULL == tsk) {
+ atpsr_err("tsk buffer allocating failed");
+ HAL_MutexUnlock(at.at_uart_send_mutex);
+ return -1;
+ }
+ memset(tsk, 0, sizeof(at_task_t));
+
+ //tsk->smpr = HAL_SemaphoreCreate();
+ tsk->smpr = HAL_AT_SemaphoreCreate();
+
+ if (NULL == tsk->smpr) {
+ atpsr_err("failed to allocate semaphore");
+ goto end;
+ }
+
+ if (atcmdconfig) {
+ if (NULL != atcmdconfig->reply_prefix) {
+ tsk->rsp_prefix = atcmdconfig->reply_prefix;
+ tsk->rsp_prefix_len = strlen(atcmdconfig->reply_prefix);
+ }
+
+ if (NULL != atcmdconfig->reply_success_postfix) {
+ tsk->rsp_success_postfix = atcmdconfig->reply_success_postfix;
+ tsk->rsp_success_postfix_len = strlen(atcmdconfig->reply_success_postfix);
+ }
+
+ if (NULL != atcmdconfig->reply_fail_postfix) {
+ tsk->rsp_fail_postfix = atcmdconfig->reply_fail_postfix;
+ tsk->rsp_fail_postfix_len = strlen(atcmdconfig->reply_fail_postfix);
+ }
+ }
+
+ tsk->command = (char *)cmd;
+ tsk->rsp = replybuf;
+ tsk->rsp_len = bufsize;
+
+ at_worker_task_add(tsk);
+
+ if ((ret = at_sendto_lower(at._pstuart, (void *)cmd, cmdlen,
+ at._timeout, true)) != 0) {
+ atpsr_err("uart send command failed");
+ goto end;
+ }
+
+ if (delimiter) {
+ if ((ret = at_sendto_lower(at._pstuart, (void *)at._send_delimiter,
+ strlen(at._send_delimiter), at._timeout, false)) != 0) {
+ atpsr_err("uart send delimiter failed");
+ goto end;
+ }
+ vTaskDelay(200/portTICK_PERIOD_MS);
+ }
+
+ if (data && datalen > 0) {
+ if (intval_ms > 0)
+ osDelay(intval_ms);
+
+ if ((ret = at_sendto_lower(at._pstuart, (void *)data, datalen, at._timeout, true)) != 0) {
+ atpsr_err("uart send delimiter failed");
+ goto end;
+ }
+ }
+
+ if( strstr( cmd, "CWJAP" ) ){
+ ret = HAL_SemaphoreWait(tsk->smpr, 20*1000 ); //wait 20s for get location
+ goto end;
+ }else if( strstr( cmd, "UPDATETIME=1" ) ){
+ printf( "wait 30 s...\n" );
+ ret = HAL_SemaphoreWait(tsk->smpr, 30*1000 ); //wait 30s for get net time
+ printf( "wait 30 s ... done\n" );
+ goto end;
+ }
+
+ if ((ret = HAL_SemaphoreWait(tsk->smpr, TASK_DEFAULT_WAIT_TIME)) != 0) {
+ atpsr_err("sem_wait failed");
+ goto end;
+ }
+
+
+end:
+ at_worker_task_del(tsk);
+ HAL_MutexUnlock(at.at_uart_send_mutex);
+ return ret;
+}
+
+int at_send_no_reply(const char *data, int datalen, bool delimiter)
+{
+ int ret = 0;
+
+ if (inited == 0) {
+ atpsr_err("at have not init yet\r\n");
+ return -1;
+ }
+
+ if (NULL == data || datalen <= 0) {
+ atpsr_err("invalid input \r\n");
+ return -1;
+ }
+
+ HAL_MutexLock(at.at_uart_send_mutex);
+ if ((ret = at_sendto_lower(at._pstuart, (void *)data,
+ datalen, at._timeout, true)) != 0) {
+ atpsr_err("uart send raw content (%s) failed", data);
+ HAL_MutexUnlock(at.at_uart_send_mutex);
+ return -1;
+ }
+
+ if (delimiter) {
+ if ((ret = at_sendto_lower(at._pstuart, (void *)at._send_delimiter,
+ strlen(at._send_delimiter), at._timeout, false)) != 0) {
+ atpsr_err("uart send delimiter failed");
+ HAL_MutexUnlock(at.at_uart_send_mutex);
+ return -1;
+ }
+ }
+ HAL_MutexUnlock(at.at_uart_send_mutex);
+
+ return ret;
+}
+
+static int at_getc(char *c, int timeout_ms)
+{
+ int ret = 0;
+ char data;
+ uint32_t recv_size = 0;
+
+ if (NULL == c) {
+ return -1;
+ }
+
+ if (inited == 0) {
+ atpsr_err("at have not init yet\r\n");
+ return -1;
+ }
+
+ HAL_MutexLock(at.at_uart_recv_mutex);
+ ret = at_recvfrom_lower(at._pstuart, (void *)&data, 1, &recv_size, timeout_ms);
+ HAL_MutexUnlock(at.at_uart_recv_mutex);
+
+ if (ret != 0) {
+ return -1;
+ }
+
+ if (recv_size == 1) {
+ *c = data;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+int at_read(char *outbuf, int readsize)
+{
+ int ret = 0;
+ uint32_t recv_size, total_read = 0;
+
+ if (inited == 0) {
+ atpsr_err("at have not init yet\r\n");
+ return -1;
+ }
+
+ HAL_MutexLock(at.at_uart_recv_mutex);
+ while (total_read < readsize) {
+ ret = at_recvfrom_lower(at._pstuart, (void *)(outbuf + total_read),
+ readsize - total_read, &recv_size, at._timeout);
+ if (ret != 0) {
+ atpsr_err("at_read failed on uart_recv.");
+ break;
+ }
+
+ if (recv_size <= 0) {
+ continue;
+ }
+ total_read += recv_size;
+ if (total_read >= readsize) {
+ break;
+ }
+ }
+ HAL_MutexUnlock(at.at_uart_recv_mutex);
+
+ if (ret != 0) {
+ return -1;
+ }
+
+ return total_read;
+}
+
+#define RECV_BUFFER_SIZE 512
+static char at_rx_buf[RECV_BUFFER_SIZE];
+int at_register_callback(const char *prefix, const char *postfix, char *recvbuf,
+ int bufsize, at_recv_cb cb, void *arg)
+{
+ oob_t *oob = NULL;
+ int i = 0;
+
+ if (bufsize < 0 || bufsize >= RECV_BUFFER_SIZE || NULL == prefix) {
+ atpsr_err("%s invalid input \r\n", __func__);
+ return -1;
+ }
+
+ if (NULL != postfix && (NULL == recvbuf || 0 == bufsize)) {
+ atpsr_err("%s invalid postfix input \r\n", __func__);
+ return -1;
+ }
+
+ if (at._oobs_num >= OOB_MAX) {
+ atpsr_err("No place left in OOB.\r\n");
+ return -1;
+ }
+
+ /*check oob exist*/
+ for (i = 0; i < at._oobs_num; i++) {
+ if (NULL != at._oobs[i].prefix &&
+ strcmp(prefix, at._oobs[i].prefix) == 0) {
+ atpsr_warning("oob prefix %s is already exist.\r\n", prefix);
+ return -1;
+ }
+ }
+
+ oob = &(at._oobs[at._oobs_num++]);
+
+ oob->oobinputdata = recvbuf;
+ if (oob->oobinputdata != NULL) {
+ memset(oob->oobinputdata, 0, bufsize);
+ }
+ oob->maxlen = bufsize;
+ oob->prefix = (char *)prefix;
+ oob->postfix = (char *)postfix;
+ oob->cb = cb;
+ oob->arg = arg;
+ oob->reallen = 0;
+
+ atpsr_debug("New oob registered (%s)", oob->prefix);
+
+ return 0;
+}
+
+static void at_scan_for_callback(char c, char *buf, int *index)
+{
+ int k;
+ oob_t *oob = NULL;
+ int offset = *index;
+
+ if (!buf || offset < 0) {
+ return;
+ }
+
+ for (k = 0; k < at._oobs_num; k++) {
+ oob = &(at._oobs[k]);
+ if (oob->reallen > 0 ||
+ (offset >= strlen(oob->prefix) &&
+ memcmp(oob->prefix, buf + offset - strlen(oob->prefix),
+ strlen(oob->prefix)) == 0)) {
+ atpsr_debug("AT! %s\r\n", oob->prefix);
+ if (oob->postfix == NULL) {
+ oob->cb(oob->arg, NULL, 0);
+ memset(buf, 0, offset);
+ offset = 0;
+ } else {
+ if (oob->reallen == 0) {
+ int len = strlen(oob->prefix) - 1;
+ len = len > 0 ? len : 0;
+ memset(oob->oobinputdata, 0, oob->maxlen);
+ memcpy(oob->oobinputdata, oob->prefix, len);
+ oob->reallen += len;
+ }
+
+ if (oob->reallen < oob->maxlen) {
+ oob->oobinputdata[oob->reallen] = c;
+ oob->reallen++;
+ if ((oob->reallen >=
+ strlen(oob->prefix) + strlen(oob->postfix)) &&
+ (strncmp(oob->oobinputdata + oob->reallen -
+ strlen(oob->postfix),
+ oob->postfix,
+ strlen(oob->postfix)) == 0)) {
+ /*recv postfix*/
+ oob->cb(oob->arg, oob->oobinputdata, oob->reallen);
+ memset(oob->oobinputdata, 0, oob->reallen);
+ oob->reallen = 0;
+ memset(buf, 0, offset);
+ offset = 0;
+ }
+ } else {
+ atpsr_err("invalid oob %s input , for oversize %s \r\n",
+ oob->prefix, oob->oobinputdata);
+ memset(oob->oobinputdata, 0, oob->reallen);
+ oob->reallen = 0;
+ memset(buf, 0, offset);
+ offset = 0;
+ }
+
+ /*oob data maybe more than buf size */
+ if (offset > (RECV_BUFFER_SIZE - 2)) {
+ memset(buf, 0, offset);
+ offset = 0;
+ }
+ }
+ continue;
+ }
+ }
+
+ *index = offset;
+ return;
+}
+
+static void at_worker(void *arg)
+{
+ int offset = 0;
+ int ret = 0;
+ int at_task_empty = 0;
+ int at_task_reponse_begin = 0;
+ int memcpy_size = 0;
+ int rsp_prefix_len = 0;
+ int rsp_success_postfix_len = 0;
+ int rsp_fail_postfix_len = 0;
+ char c = 0;
+ at_task_t *tsk;
+ char *buf = NULL;
+ char *rsp_prefix = NULL;
+ char *rsp_success_postfix = NULL;
+ char *rsp_fail_postfix = NULL;
+
+ atpsr_debug("at_work started.");
+
+ buf = at_rx_buf;
+ if (NULL == buf) {
+ atpsr_err("AT worker fail to malloc ,task exist \r\n");
+ vTaskDelete(NULL);
+ return ;
+ }
+
+ memset(buf, 0, RECV_BUFFER_SIZE);
+ inited = 1;
+
+ while (true) {
+ ret = at_getc(&c, at._timeout);
+ if (ret != 0) {
+ continue;
+ }
+
+ if (offset + 1 >= RECV_BUFFER_SIZE) {
+ atpsr_err("Fatal error, no one is handling AT uart");
+ goto check_buffer;
+ }
+ buf[offset++] = c;
+ buf[offset] = 0;
+
+ at_scan_for_callback(c, buf, &offset);
+
+ HAL_MutexLock(at.task_mutex);
+ at_task_empty = slist_empty(&at.task_l);
+
+ if (!at_task_empty) {
+ tsk = slist_first_entry(&at.task_l, at_task_t, next);
+ }
+ HAL_MutexUnlock(at.task_mutex);
+
+ /* if no task, continue recv */
+ if (at_task_empty) {
+ atpsr_debug("No task in queue");
+ goto check_buffer;
+ }
+
+ if (NULL != tsk->rsp_prefix && 0 != tsk->rsp_prefix_len) {
+ rsp_prefix = tsk->rsp_prefix;
+ rsp_prefix_len = tsk->rsp_prefix_len;
+ } else {
+ rsp_prefix = at._default_recv_prefix;
+ rsp_prefix_len = at._recv_prefix_len;
+ }
+
+ if (NULL != tsk->rsp_success_postfix &&
+ 0 != tsk->rsp_success_postfix_len) {
+ rsp_success_postfix = tsk->rsp_success_postfix;
+ rsp_success_postfix_len = tsk->rsp_success_postfix_len;
+ } else {
+ rsp_success_postfix = at._default_recv_success_postfix;
+ rsp_success_postfix_len = at._recv_success_postfix_len;
+ }
+
+ if (NULL != tsk->rsp_fail_postfix && 0 != tsk->rsp_fail_postfix_len) {
+ rsp_fail_postfix = tsk->rsp_fail_postfix;
+ rsp_fail_postfix_len = tsk->rsp_fail_postfix_len;
+ } else {
+ rsp_fail_postfix = at._default_recv_fail_postfix;
+ rsp_fail_postfix_len = at._recv_fail_postfix_len;
+ }
+
+ if (offset >= rsp_prefix_len && at_task_reponse_begin == 0 &&
+ (strncmp(buf + offset - rsp_prefix_len, rsp_prefix,
+ rsp_prefix_len) == 0)) {
+ //printf( "at_task_reponse_begin --> 1\n" );
+ //printf( "buf: off:%d,[%s]\n",offset, buf );
+ //printf( "prefix len:%d, [%s]\n", rsp_prefix_len, rsp_prefix );
+
+ at_task_reponse_begin = 1;
+ continue;
+ }
+
+ if (at_task_reponse_begin == 1) {
+ if (tsk->rsp_offset < tsk->rsp_len) {
+ tsk->rsp[tsk->rsp_offset] = c;
+ tsk->rsp_offset++;
+
+ if ((tsk->rsp_offset >= rsp_success_postfix_len &&
+ strncmp(
+ tsk->rsp + tsk->rsp_offset - rsp_success_postfix_len,
+ rsp_success_postfix, rsp_success_postfix_len) == 0) ||
+ (tsk->rsp_offset >= rsp_fail_postfix_len &&
+ strncmp(tsk->rsp + tsk->rsp_offset - rsp_fail_postfix_len,
+ rsp_fail_postfix, rsp_fail_postfix_len) == 0)) {
+
+ //printf( "tsk:%p\n", tsk );
+ //printf( "post semaphore\n" );
+ //printf( "tsk->rsp off:%d,[%s]\n", tsk->rsp_offset, tsk->rsp );
+ //printf( "success pf len:%d, [%s]\n", rsp_success_postfix_len, rsp_success_postfix );
+ //printf( "fail pf len:%d, [%s]\n", rsp_fail_postfix_len, rsp_fail_postfix );
+
+ HAL_SemaphorePost(tsk->smpr);
+
+ at_task_reponse_begin = 0;
+ //printf( "at_task_reponse_begin --> 0\n" );
+ memset(buf, 0, offset);
+ offset = 0;
+ }
+ } else {
+ memset(tsk->rsp, 0, tsk->rsp_len);
+ strcpy(tsk->rsp, rsp_fail_postfix);
+ HAL_SemaphorePost(tsk->smpr);
+ at_task_reponse_begin = 0;
+ //printf( "at_task_reponse_begin --> 0 X\n" );
+ memset(buf, 0, offset);
+ offset = 0;
+ }
+ }
+ check_buffer:
+ /* in case buffer is full */
+ if (offset > (RECV_BUFFER_SIZE - 2)) {
+ memcpy_size = rsp_prefix_len > rsp_success_postfix_len
+ ? rsp_prefix_len
+ : rsp_success_postfix_len;
+ memcpy_size = memcpy_size > rsp_fail_postfix_len
+ ? memcpy_size
+ : rsp_fail_postfix_len;
+ memcpy(buf, buf + offset - memcpy_size, memcpy_size);
+ memset(buf + memcpy_size, 0, offset - memcpy_size);
+ offset = memcpy_size;
+ }
+ }
+ vTaskDelete(NULL);
+ return;
+}
+
--- /dev/null
+/*
+ * Copyright (C) 2015-2017 Alibaba Group Holding Limited
+ */
+
+#ifndef _AT_PARSER_H_
+#define _AT_PARSER_H_
+
+#define PLATFORM_HAS_OS 1
+
+//#include "infra_config.h"
+
+//#define AT_DEBUG_MODE 1
+
+/* uart config */
+#define AT_UART_PORT 1
+#define AT_UART_LINUX_DEV "/dev/ttyUSB0"
+#define AT_UART_BAUDRATE 115200
+#define AT_UART_DATA_WIDTH DATA_WIDTH_8BIT
+#define AT_UART_PARITY NO_PARITY
+#define AT_UART_STOP_BITS STOP_BITS_1
+#define AT_UART_FLOW_CONTROL FLOW_CONTROL_DISABLED
+#define AT_UART_MODE MODE_TX_RX
+#define AT_UART_TIMEOUT_MS (1000*5)
+
+/* Delimiter */
+#define AT_RECV_PREFIX "\r\n"
+#define AT_RECV_SUCCESS_POSTFIX "OK\r\n"
+#define AT_RECV_FAIL_POSTFIX "ERROR\r\n"
+#define AT_SEND_DELIMITER "\r\n"
+
+#if defined(AT_TCP_HAL_SIM800)
+#define AT_CMD_DATA_INTERVAL_MS 50
+#endif
+
+#ifdef PLATFORM_HAS_OS
+#define AT_SINGLE_TASK 0
+#else
+#define AT_SINGLE_TASK 1
+#endif
+
+#ifndef bool
+#define bool unsigned char
+#endif
+
+#ifndef true
+#define true 1
+#endif
+#ifndef false
+#define false 0
+#endif
+
+typedef struct {
+ char *reply_prefix;
+ char *reply_success_postfix;
+ char *reply_fail_postfix;
+} atcmd_config_t;
+
+typedef void (*at_recv_cb)(void *arg, char *buf, int buflen);
+
+/**
+ * initialization
+ * Configuration (e.g. AT_UART_PORT, UART_BAUDRATE) can be found
+ * in above macro
+ */
+int at_parser_init(void);
+
+/**
+ * at send (format: command + delimiter + data) and wait reply
+ *
+ * @param cmd at command sending buf. MUST not be NULL.
+ * @param cmdlen at command length.
+ * @param delimiter whether sending delimiter, usually value is true
+ * @param data data sending buf. NULL if no data.
+ * @param datalen data length. Zero if no data.
+ * @param replybuf reply buffer. MUST not be NULL.
+ * @param bufsize reply buffer size
+ * @param atcmdconfig AT cmd reply format config. Use default if NULL
+ */
+int at_send_wait_reply(const char *cmd, int cmdlen, bool delimiter,
+ const char *data, int datalen,
+ char *replybuf, int bufsize,
+ const atcmd_config_t *atcmdconfig);
+
+/**
+ * at send (format: data + delimiter) and does not wait reply
+ *
+ * @param data sending buffer.
+ * @param datalen sending length.
+ * @param delimiter whether sending delimiter, usually value is false
+ */
+int at_send_no_reply(const char *data, int datalen, bool delimiter);
+
+
+/**
+ * at read for certain bytes of data
+ *
+ * @param outbuf output buffer.
+ * @param readsize read size.
+ */
+int at_read(char *outbuf, int readsize);
+
+
+/**
+ * at register callback for recv
+ *
+ * @param prefix interested string. Must not be NULL.
+ * @param postfix intersted postfix. NULL if postfix not provided.
+ * @param recvbuf recv data buffer provided by caller, NULL if postfix not provided
+ * @param bufsize buffer size for recv data, zero if postfix not provided
+ * @param cb callback handle function. Must not be NULL.
+ * @param arg callback handle function args. NULL if not used.
+ */
+int at_register_callback(const char *prefix, const char *postfix, char *recvbuf,
+ int bufsize, at_recv_cb cb, void *arg);
+
+
+/**
+ * at yield receive function. Only used in single task scenario
+ *
+ * @param replybuf reply buffer.
+ * @param bufsize reply buffer size.
+ * @param atcmdconfig AT cmd reply format config. Use default if NULL
+ * @param timeout_ms receive timeout in millisecond
+ */
+int at_yield(char *replybuf, int bufsize, const atcmd_config_t *atcmdconfig,
+ int timeout_ms);
+#endif
+
+
--- /dev/null
+
+#ifndef _INFRA_LIST_H_
+#define _INFRA_LIST_H_
+
+#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) || defined(__GNUC__)) && \
+ !defined(inline) && !defined(__cplusplus)
+ #define inline __inline
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Get offset of a member variable.
+ *
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the variable within the struct.
+ */
+#define aos_offsetof(type, member) ((size_t)&(((type *)0)->member))
+
+/*
+ * Get the struct for this entry.
+ *
+ * @param[in] ptr the list head to take the element from.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the variable within the struct.
+ */
+#define aos_container_of(ptr, type, member) \
+ ((type *) ((char *) (ptr) - aos_offsetof(type, member)))
+
+/* for double link list */
+typedef struct dlist_s {
+ struct dlist_s *prev;
+ struct dlist_s *next;
+} dlist_t;
+
+static inline void __dlist_add(dlist_t *node, dlist_t *prev, dlist_t *next)
+{
+ node->next = next;
+ node->prev = prev;
+
+ prev->next = node;
+ next->prev = node;
+}
+
+/*
+ * Get the struct for this entry.
+ *
+ * @param[in] addr the list head to take the element from.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the dlist_t within the struct.
+ */
+#define dlist_entry(addr, type, member) \
+ ((type *)((long)addr - aos_offsetof(type, member)))
+
+
+static inline void dlist_add(dlist_t *node, dlist_t *queue)
+{
+ __dlist_add(node, queue, queue->next);
+}
+
+static inline void dlist_add_tail(dlist_t *node, dlist_t *queue)
+{
+ __dlist_add(node, queue->prev, queue);
+}
+
+static inline void dlist_del(dlist_t *node)
+{
+ dlist_t *prev = node->prev;
+ dlist_t *next = node->next;
+
+ prev->next = next;
+ next->prev = prev;
+}
+
+static inline void dlist_init(dlist_t *node)
+{
+ node->next = node->prev = node;
+}
+
+static inline void INIT_AOS_DLIST_HEAD(dlist_t *list)
+{
+ list->next = list;
+ list->prev = list;
+}
+
+static inline int dlist_empty(const dlist_t *head)
+{
+ return head->next == head;
+}
+
+/*
+ * Initialise the list.
+ *
+ * @param[in] list the list to be inited.
+ */
+#define AOS_DLIST_INIT(list) {&(list), &(list)}
+
+/*
+ * Get the first element from a list
+ *
+ * @param[in] ptr the list head to take the element from.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the dlist_t within the struct.
+ */
+#define dlist_first_entry(ptr, type, member) \
+ dlist_entry((ptr)->next, type, member)
+
+/*
+ * Iterate over a list.
+ *
+ * @param[in] pos the &struct dlist_t to use as a loop cursor.
+ * @param[in] head he head for your list.
+ */
+#define dlist_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); pos = pos->next)
+
+/*
+ * Iterate over a list safe against removal of list entry.
+ *
+ * @param[in] pos the &struct dlist_t to use as a loop cursor.
+ * @param[in] n another &struct dlist_t to use as temporary storage.
+ * @param[in] head he head for your list.
+ */
+#define dlist_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+/*
+ * Iterate over list of given type.
+ *
+ * @param[in] queue he head for your list.
+ * @param[in] node the &struct dlist_t to use as a loop cursor.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the dlist_t within the struct.
+ */
+#define dlist_for_each_entry(queue, node, type, member) \
+ for (node = aos_container_of((queue)->next, type, member); \
+ &node->member != (queue); \
+ node = aos_container_of(node->member.next, type, member))
+
+/*
+ * Iterate over list of given type safe against removal of list entry.
+ *
+ * @param[in] queue the head for your list.
+ * @param[in] n the type * to use as a temp.
+ * @param[in] node the type * to use as a loop cursor.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the dlist_t within the struct.
+ */
+#define dlist_for_each_entry_safe(queue, n, node, type, member) \
+ for (node = aos_container_of((queue)->next, type, member), \
+ n = (queue)->next ? (queue)->next->next : NULL; \
+ &node->member != (queue); \
+ node = aos_container_of(n, type, member), n = n ? n->next : NULL)
+
+/*
+ * Get the struct for this entry.
+ * @param[in] ptr the list head to take the element from.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the variable within the struct.
+ */
+#define list_entry(ptr, type, member) \
+ aos_container_of(ptr, type, member)
+
+
+/*
+ * Iterate backwards over list of given type.
+ *
+ * @param[in] pos the type * to use as a loop cursor.
+ * @param[in] head he head for your list.
+ * @param[in] member the name of the dlist_t within the struct.
+ * @param[in] type the type of the struct this is embedded in.
+ */
+#define dlist_for_each_entry_reverse(pos, head, member, type) \
+ for (pos = list_entry((head)->prev, type, member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, type, member))
+
+
+/*
+ * Get the list length.
+ *
+ * @param[in] queue the head for your list.
+ */
+static inline int __dlist_entry_number(dlist_t *queue)
+{
+ int num;
+ dlist_t *cur = queue;
+ for (num = 0; cur->next != queue; cur = cur->next, num++)
+ ;
+
+ return num;
+}
+
+/*
+ * Get the list length.
+ *
+ * @param[in] queue the head for your list.
+ */
+#define dlist_entry_number(head) \
+ __dlist_entry_number(head)
+
+/*
+ * Initialise the list.
+ *
+ * @param[in] name the list to be initialized.
+ */
+#define AOS_DLIST_HEAD_INIT(name) { &(name), &(name) }
+
+/*
+ * Initialise the list.
+ *
+ * @param[in] name the list to be initialized.
+ */
+#define AOS_DLIST_HEAD(name) \
+ dlist_t name = AOS_DLIST_HEAD_INIT(name)
+
+/* for single link list */
+typedef struct slist_s {
+ struct slist_s *next;
+} slist_t;
+
+static inline void slist_add(slist_t *node, slist_t *head)
+{
+ node->next = head->next;
+ head->next = node;
+}
+
+static inline void slist_add_tail(slist_t *node, slist_t *head)
+{
+ while (head->next) {
+ head = head->next;
+ }
+
+ slist_add(node, head);
+}
+
+static inline void slist_del(slist_t *node, slist_t *head)
+{
+ while (head->next) {
+ if (head->next == node) {
+ head->next = node->next;
+ break;
+ }
+
+ head = head->next;
+ }
+}
+
+static inline int slist_empty(const slist_t *head)
+{
+ return !head->next;
+}
+
+static inline void slist_init(slist_t *head)
+{
+ head->next = 0;
+}
+
+/*
+* Iterate over list of given type.
+*
+* @param[in] queue he head for your list.
+* @param[in] node the type * to use as a loop cursor.
+* @param[in] type the type of the struct this is embedded in.
+* @param[in] member the name of the slist_t within the struct.
+*/
+#define slist_for_each_entry(queue, node, type, member) \
+ for (node = aos_container_of((queue)->next, type, member); \
+ &node->member; \
+ node = aos_container_of(node->member.next, type, member))
+
+/*
+ * Iterate over list of given type safe against removal of list entry.
+ *
+ * @param[in] queue the head for your list.
+ * @param[in] tmp the type * to use as a temp.
+ * @param[in] node the type * to use as a loop cursor.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the slist_t within the struct.
+ */
+#define slist_for_each_entry_safe(queue, tmp, node, type, member) \
+ for (node = aos_container_of((queue)->next, type, member), \
+ tmp = (queue)->next ? (queue)->next->next : NULL; \
+ &node->member; \
+ node = aos_container_of(tmp, type, member), tmp = tmp ? tmp->next : tmp)
+
+/*
+ * Initialise the list.
+ *
+ * @param[in] name the list to be initialized.
+ */
+#define AOS_SLIST_HEAD_INIT(name) {0}
+
+/*
+ * Initialise the list.
+ *
+ * @param[in] name the list to be initialized.
+ */
+#define AOS_SLIST_HEAD(name) \
+ slist_t name = AOS_SLIST_HEAD_INIT(name)
+
+/*
+ * Get the struct for this entry.
+ * @param[in] addr the list head to take the element from.
+ * @param[in] type the type of the struct this is embedded in.
+ * @param[in] member the name of the slist_t within the struct.
+ */
+#define slist_entry(addr, type, member) ( \
+ addr ? (type *)((long)addr - aos_offsetof(type, member)) : (type *)addr \
+ )
+
+/*
+* Get the first element from a list.
+*
+* @param[in] ptr the list head to take the element from.
+* @param[in] type the type of the struct this is embedded in.
+* @param[in] member the name of the slist_t within the struct.
+*/
+#define slist_first_entry(ptr, type, member) \
+ slist_entry((ptr)->next, type, member)
+
+/*
+ * Get the list length.
+ *
+ * @param[in] queue the head for your list.
+ */
+static inline int slist_entry_number(slist_t *queue)
+{
+ int num;
+ slist_t *cur = queue;
+ for (num = 0; cur->next; cur = cur->next, num++)
+ ;
+
+ return num;
+}
+
+//#include "infra_compat.h"
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* AOS_LIST_H */
+
#define TEMP_ERR ( 1<<3 ) //超温故障
#define SFLASH_ERR ( 1<<4 ) //spi flash 故障
#define VALVE_CTRL_ERROR ( 1<<5 ) //比例阀电流控制 AD5245 故障
+#define WIFI_ERR ( 1<<6 )
+
typedef struct {
//配置信息, 从flash 加载
#include "user.h"
+#include "at_parser.h"
//wifi task
//uart5
//
+#define HAL_Printf DBG_PRINTF
+#define at_conn_hal_debug DBG_PRINTF
+#define at_conn_hal_err DBG_PRINTF
+
#define huart_wifi huart5
extern UART_HandleTypeDef huart5;
-static char wifi_rx_buf[1024];
-static uint32_t wifi_rx_number = 0;
+#define WIFI_RX_BUF_SIZE 512
+#define WIFI_RX_BUF_SIZE_MASK (WIFI_RX_BUF_SIZE-1)
+static char wifi_rx_buf[WIFI_RX_BUF_SIZE];
+static uint32_t rx_buf_rd = 0;
+static uint32_t rx_buf_wr = 0;
+
static uint8_t wifi_rx_byte = 0;
+static char g_wifi_ver[50];
+
void wifi_uart_rx_start( void )
{
HAL_UART_Receive_IT( &huart_wifi, &wifi_rx_byte, 1 );
rx_byte = wifi_rx_byte;
wifi_uart_rx_start();
- if( wifi_rx_number < sizeof( wifi_rx_buf)-1){
- wifi_rx_buf[wifi_rx_number++] = rx_byte;
- //handle pkt
- //get \r\n"
- //event_set
+ if( rx_buf_wr+1==rx_buf_rd ){
+ //full!
+ }else{
+ wifi_rx_buf[rx_buf_wr&WIFI_RX_BUF_SIZE_MASK] = wifi_rx_byte;
+ rx_buf_wr++;
}
return;
}
+int32_t wifi_read_byte( uint8_t *rd_byte )
+{
+ if( rx_buf_wr>rx_buf_rd ){
+ *rd_byte = wifi_rx_buf[ rx_buf_rd&WIFI_RX_BUF_SIZE_MASK ];
+ rx_buf_rd++;
+ return 0;
+ }else{
+ return -1;
+ }
+}
+
char * wifi_tx_cmd( char * cmd, uint32_t timeout_ms )
{
HAL_UART_Transmit( &huart_wifi, (uint8_t *)cmd, strlen( cmd ), timeout_ms );
return NULL;
}
+
int32_t wifi_init( void )
{
- //发送AT 有回应即认为初始化成功
- char * resp;
+ //发送AT+GMR 读取到版本,初始化成功
+ char *cmd;
+ char resp_buf[200];
+ int32_t ret = -1;
+ atcmd_config_t at_cfg;
- while(1){
- HAL_UART_Transmit( &huart_wifi, (uint8_t *)"AT\r\n", 4, 1000 );
- osDelay(1000);
- }
+ at_cfg.reply_prefix = "AT version:"; //AT_CMD_TEST_RESULT;
+ at_cfg.reply_fail_postfix = NULL;
+ at_cfg.reply_success_postfix = NULL;
- resp = wifi_tx_cmd( "AT\r\n", 1000 );
+ for( int i=0; i<10; i++ ){
+ cmd = "AT+GMR";
+ memset( resp_buf, 0, sizeof( resp_buf) );
- if( resp && resp[0] == 1 )
- return 0;
- else
- return -1;
+ if( at_send_wait_reply( cmd, strlen(cmd), true, NULL, 0, resp_buf, sizeof( resp_buf)-1, &at_cfg ) ){
+ DBG_PRINTF( "at send wait reply timeout!\n" );
+ }else{
+ if( strstr( resp_buf, "OK" ) ){
+ char *ver_str = strtok( resp_buf, "\r\n" );
+ if( ver_str ){
+ strncpy( g_wifi_ver, ver_str, sizeof( g_wifi_ver)-1 );
+ DBG_PRINTF( "AT+GMR read ver:%s\n", g_wifi_ver );
+ ret = 0;
+ break;
+ }
+ }
+ }
+ osDelay(2*1000);
+ }
+ return ret;
}
int32_t wifi_get_cfg( char * ssid, char * passwd )
{
strcpy( ssid, "yino305" );
strcpy( passwd, "cc123456789" );
- return -1;
+ return 0;
}
int32_t wifi_connect( char * ssid, char * passwd )
{
- return -1;
+ char cmd[100];
+ atcmd_config_t at_cfg;
+ char resp_buf[200];
+
+ at_cfg.reply_prefix = NULL;
+ at_cfg.reply_fail_postfix = NULL;
+ at_cfg.reply_success_postfix = NULL;
+
+ //1. 配置 WiFi 模式,
+ //AT+CWMODE=3 //softAP+station mode
+ strcpy( cmd, "AT+CWMODE=3" );
+ if( at_send_wait_reply( cmd, strlen(cmd), true, NULL, 0, resp_buf, sizeof( resp_buf )-1, NULL ) ){
+ return -1;
+ }
+
+ //2. 连接路由器
+ //AT+CWJAP="SSID","password" //SSID and password of router
+ at_cfg.reply_prefix = "WIFI";
+ at_cfg.reply_success_postfix="OK"; //"CONNECTED";
+ at_cfg.reply_fail_postfix = "FAIL"; //"DISCONNECT";
+ sprintf( cmd, "AT+CWJAP=\"%s\",\"%s\"", ssid, passwd );
+ if( at_send_wait_reply( cmd, strlen(cmd), true, NULL, 0, resp_buf, sizeof( resp_buf )-1, &at_cfg ) ){
+ return -1;
+ }
+ if( !strstr( resp_buf, "CONNECTED" ) ){
+ return -1;
+ }
+
+ //3. 查询 ESP8266 设备的 IP 地址
+ //AT+CIFSR
+ at_cfg.reply_prefix = "+CIFSR:APIP,";
+ at_cfg.reply_success_postfix = NULL;
+ at_cfg.reply_fail_postfix = NULL;
+ strcpy( cmd, "AT+CIFSR" );
+ if( at_send_wait_reply( cmd, strlen(cmd), true, NULL, 0, resp_buf, sizeof( resp_buf )-1, &at_cfg ) ){
+ return -1;
+ }
+ uint8_t ip[4];
+ if( sscanf( resp_buf, "\"%hhd.%hhd.%hhd.%hhd\"", &ip[0], &ip[1], &ip[2], &ip[3] )!= 4 ){
+ return -1;
+ }
+ DBG_PRINTF( "GET IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );
+
+ //4. 连接TCP服务器
+ //AT+CIPSTART="TCP","192.168.3.116",8080 //protocol, server IP and port
+ at_cfg.reply_prefix = "WIFI";
+ at_cfg.reply_success_postfix="OK"; //"CONNECTED";
+ at_cfg.reply_fail_postfix = "FAIL"; //"DISCONNECT";
+ sprintf( cmd, "AT+CIPSTART=\"TCP\",\"192.168.4.14\",12345" );
+ if( at_send_wait_reply( cmd, strlen(cmd), true, NULL, 0, resp_buf, sizeof( resp_buf )-1, NULL ) ){
+ return -1;
+ }
+ if( !strstr( resp_buf, "OK" ) ){
+ return -1;
+ }
+
+ return 0;
}
int32_t devinfo_report( void )
{
return 0;
}
+#if 0
+int32_t esp_read_imsi( void )
+{
+
+ int ret = 0;
+ char *cmd ;
+ char *rsp = g_rsp;
+ char *buffer = g_rsp;
+ char imsi[20];
+
+ cmd = "AT+CIMI\r\n";
+ bc25_send_with_retry( cmd, strlen(cmd), true, NULL, 0, buffer, BC25_DEFAULT_RSP_LEN, AT_CMD_TEST_RESULT);
+
+ if ( strstr( rsp, "OK" ) && sscanf( rsp, "\r\n%15[^\r]", imsi) == 1 ){
+ imsi[15] = 0;
+ HAL_Printf( "GET IMSI:%s\n", g_IMSI );
+ if( strlen( imsi ) == 15 ){
+ strcpy( g_IMSI, imsi );
+ HAL_ClearGprsFaultCode(1); //clear E1/E5
+ return 0;
+ }
+ }
+ return -1;
+}
+#endif
+
+#define TCP_MAX_PAYLOAD_SIZE 512
+static uint8_t payload[TCP_MAX_PAYLOAD_SIZE] = {0};
+static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen)
+{
+ unsigned char unusesymbol = 0;
+ unsigned char datalen[6] = {0};
+ unsigned char ipaddr[16] = {0};
+
+ int i = 0;
+ int j = 0;
+ int len = 0;
+ int remoteport = 0;
+ int linkid = 0;
+ char *recvdata = NULL;
+
+ //+IPD,20:http://www.cmsoft.cn
+
+ /*eat , char*/
+ at_read((char *)&unusesymbol, 1);
+
+ /* get data len */
+ i = 0;
+ do {
+ at_read((char *)&datalen[i], 1);
+ if (datalen[i] == ':') {
+ break;
+ }
+ if (i >= sizeof(datalen)) {
+ at_conn_hal_err( "Too long length of data.datalen is %s \r\n", datalen);
+ return;
+ }
+ if (datalen[i] > '9' || datalen[i] < '0') {
+ at_conn_hal_err( "Invalid len string!!!, datalen is %s \r\n", datalen);
+ return;
+ }
+ i++;
+ } while (1);
+
+ /* len: string to number */
+ for (j = 0; j < i; j++) {
+ len = len * 10 + datalen[j] - '0';
+ }
+ HAL_Printf( "R:%d\n",len );
+ if (len <= TCP_MAX_PAYLOAD_SIZE) {
+ recvdata = (char *)payload;
+ }
+ memset(recvdata, 0, len);
+ at_read(recvdata, len);
+ return;
+}
+
+int32_t wifi_tcp_tx( uint8_t *data, uint32_t sz )
+{
+ //AT+CIPSEND=4
+ //> xxx
+ char cmd[50];
+ char resp_buf[100];
+ atcmd_config_t at_cfg;
+#if 1
+ at_cfg.reply_prefix = "SEND";
+ at_cfg.reply_success_postfix="OK";
+ at_cfg.reply_fail_postfix = NULL;
+ sprintf( cmd, "AT+CIPSEND=%d", sz );
+ if( at_send_wait_reply( cmd, strlen(cmd), true, data, sz, resp_buf, sizeof( resp_buf )-1, &at_cfg ) ){
+ return -1;
+ }
+#endif
+ return 0;
+}
-
void user_wifi_task( void* unused )
{
char ssid[100];
char passwd[32];
int32_t connected = 0;
-
-
- while(1)
- osDelay(10*1000);
- MX_UART5_Init();
+ MX_UART5_Init();
wifi_uart_rx_start();
- osDelay(5000);
+ at_parser_init();
+
//1. init
- //发送AT命令 有回应
- if( wifi_init() ){
- //wifi error
- vTaskDelete( NULL );
- return;
+ while( wifi_init() ){
+ //wifi init fail
+ g_dev.state |= (WIFI_ERR);
+ osDelay( 10*1000 );
+ //vTaskDelete( NULL );
}
+ at_register_callback("+IPD", NULL, NULL, 0, tcp_rx_data_handle, NULL);
while( 1 ){
-
-
+
while( wifi_get_cfg( ssid, passwd ) ){
osDelay( 1000 );
}
-
+
if( wifi_connect( ssid, passwd ) ){
osDelay( 5000 );
continue;
}
connected = 1;
-
+
while( connected ){
- //ret = wait_rx_event( timeout=0.1 )
- //if( ret ){
- // wifi_rx_data_handler()
- // continue
- //}
osDelay( 5000);
+ wifi_tcp_tx( "test data\r\n", strlen("test data\r\n") );
//定时上报
if( devinfo_report() < 0 ){
//reconnect
void wifi_task_start( void )
{
- wifiTaskHandle = osThreadNew(user_wifi_task, NULL, &wifiTask_attributes);
+/// wifiTaskHandle = osThreadNew(user_wifi_task, NULL, &wifiTask_attributes);
}