Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CommandCommon.h Source File

CommandCommon.h

00001 /**
00002  * ACKme WiConnect Host Library is licensed under the BSD licence: 
00003  * 
00004  * Copyright (c)2014 ACKme Networks.
00005  * All rights reserved. 
00006  * 
00007  * Redistribution and use in source and binary forms, with or without modification, 
00008  * are permitted provided that the following conditions are met: 
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * 3. The name of the author may not be used to endorse or promote products 
00016  * derived from this software without specific prior written permission. 
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 
00019  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00020  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00021  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00023  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00026  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00027  * OF SUCH DAMAGE.
00028  */
00029 #pragma once
00030 
00031 
00032 #include <stdio.h>
00033 #include <stdarg.h>
00034 #include "Wiconnect.h"
00035 #include "api/StringUtil.h"
00036 #include "internal/common.h"
00037 
00038 
00039 #define CHECK_NULL_BUFFER(buf) if(buf == NULL) return WICONNECT_NULL_BUFFER
00040 #define CHECK_INITIALIZED() if(!initialized) return WICONNECT_NOT_INITIALIZED
00041 #define RESET_CMD_HEADER(header)  memset(header, 0, sizeof(CommandHeader)); header->bytes_remaining = WICONNECT_HEADER_LENGTH
00042 
00043 #ifdef WICONNECT_ENABLE_DEBUGGING
00044 #define DEBUG_CMD_SEND(cmd)         debugLog(">> CMD: %s", cmd)
00045 #define DEBUG_CMD_LOG(res)          debugLog("<< LOG: %s", res)
00046 #define DEBUG_CMD_RESPONSE(res)     debugLog("<< RES: %s", res)
00047 #define DEBUG_CMD_ERROR(code)       debugLog("<< ERR:(%d) %s", code, getLastCommandResponseCodeStr())
00048 #define DEBUG_ERROR(msg, ...)       debugLog("   ERR: " msg, ## __VA_ARGS__)
00049 #define DEBUG_INFO(msg, ...)        debugLog("   DBG: " msg, ## __VA_ARGS__)
00050 #else
00051 #define DEBUG_CMD_SEND(cmd)
00052 #define DEBUG_CMD_LOG(res)
00053 #define DEBUG_CMD_RESPONSE(res)
00054 #define DEBUG_CMD_ERROR(code)
00055 #define DEBUG_ERROR(msg, ...)
00056 #define DEBUG_INFO(msg, ...)
00057 #endif
00058 
00059 
00060 typedef enum
00061 {
00062     WICONNECT_CMD_TYPE_NULL = 0,
00063     WICONNECT_CMD_TYPE_REPLY = 'R',
00064     WICONNECT_CMD_TYPE_LOG = 'L',
00065     WICONNECT_CMD_TYPE_SAFEMODE = 'S'
00066 } ResponseType;
00067 
00068 typedef enum
00069 {
00070     WICONNECT_CMD_CODE_NULL         = 0,
00071     WICONNECT_CMD_SUCCESS           = 1, // The command was successful
00072     WICONNECT_CMD_FAILED            = 2, // The command failed, most likely in the firmware
00073     WICONNECT_CMD_PARSE_ERROR       = 3, // Failed to parse the command
00074     WICONNECT_CMD_UNKNOWN           = 4, // Unknown command
00075     WICONNECT_CMD_TOO_FEW_ARGS      = 5, // Not enough command arguments
00076     WICONNECT_CMD_TOO_MANY_ARGS     = 6, // Too many command arguments
00077     WICONNECT_CMD_UNKNOWN_OPTION    = 7, // Unknown option (e.g. known 'set' command option)
00078     WICONNECT_CMD_BAD_ARGS          = 8, // Invalid command arguments
00079 } ResponseCode;
00080 
00081 #define WICONNECT_HEADER_LENGTH 9
00082 typedef struct
00083 {
00084     ResponseType response_type;
00085     ResponseCode response_code;
00086     uint16_t response_len;
00087     uint8_t len_buffer[WICONNECT_HEADER_LENGTH];
00088     uint8_t *len_buffer_ptr;
00089     uint8_t bytes_remaining;
00090 } CommandHeader;
00091 
00092 typedef struct
00093 {
00094     char *responseBuffer;
00095     char *responseBufferPtr;
00096     int responseBufferLen;
00097     ReaderFunc reader;
00098     void *user;
00099     char *commandPtr;
00100     int commandLen;
00101     int bytesToWrite;
00102     int bytesToRead;
00103     TimerTimeout timeoutMs;
00104     Callback callback;
00105     bool nonBlocking;
00106 } CommandContext;
00107