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

Committer:
dan_ackme
Date:
Mon Oct 27 13:42:26 2014 -0700
Revision:
29:b6af04b77a56
Child:
32:b2bdbc018665
refactored library layout

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 29:b6af04b77a56 1 /**
dan_ackme 29:b6af04b77a56 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 29:b6af04b77a56 3 *
dan_ackme 29:b6af04b77a56 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 29:b6af04b77a56 5 * All rights reserved.
dan_ackme 29:b6af04b77a56 6 *
dan_ackme 29:b6af04b77a56 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 29:b6af04b77a56 8 * are permitted provided that the following conditions are met:
dan_ackme 29:b6af04b77a56 9 *
dan_ackme 29:b6af04b77a56 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 29:b6af04b77a56 11 * this list of conditions and the following disclaimer.
dan_ackme 29:b6af04b77a56 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 29:b6af04b77a56 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 29:b6af04b77a56 14 * and/or other materials provided with the distribution.
dan_ackme 29:b6af04b77a56 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 29:b6af04b77a56 16 * derived from this software without specific prior written permission.
dan_ackme 29:b6af04b77a56 17 *
dan_ackme 29:b6af04b77a56 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 29:b6af04b77a56 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 29:b6af04b77a56 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 29:b6af04b77a56 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 29:b6af04b77a56 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 29:b6af04b77a56 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 29:b6af04b77a56 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 29:b6af04b77a56 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 29:b6af04b77a56 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 29:b6af04b77a56 27 * OF SUCH DAMAGE.
dan_ackme 29:b6af04b77a56 28 */
dan_ackme 29:b6af04b77a56 29
dan_ackme 29:b6af04b77a56 30 #include "Wiconnect.h"
dan_ackme 29:b6af04b77a56 31 #include "internal/common.h"
dan_ackme 29:b6af04b77a56 32 #include "api/StringUtil.h"
dan_ackme 29:b6af04b77a56 33
dan_ackme 29:b6af04b77a56 34
dan_ackme 29:b6af04b77a56 35 #define GHM_HOST_URL "api.gohack.me"
dan_ackme 29:b6af04b77a56 36
dan_ackme 29:b6af04b77a56 37
dan_ackme 29:b6af04b77a56 38 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 39 WiconnectResult GhmInterface::ghmPostMessage(WiconnectSocket &socket, bool jsonFormatted)
dan_ackme 29:b6af04b77a56 40 {
dan_ackme 29:b6af04b77a56 41 WiconnectResult result;
dan_ackme 29:b6af04b77a56 42 int32_t handle;
dan_ackme 29:b6af04b77a56 43 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 44
dan_ackme 29:b6af04b77a56 45 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 46 {
dan_ackme 29:b6af04b77a56 47 strcpy(cmdBuffer, "gme post ");
dan_ackme 29:b6af04b77a56 48 if(jsonFormatted)
dan_ackme 29:b6af04b77a56 49 {
dan_ackme 29:b6af04b77a56 50 strcat(cmdBuffer, "json");
dan_ackme 29:b6af04b77a56 51 }
dan_ackme 29:b6af04b77a56 52 }
dan_ackme 29:b6af04b77a56 53
dan_ackme 29:b6af04b77a56 54 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 55
dan_ackme 29:b6af04b77a56 56 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(cmdBuffer)))
dan_ackme 29:b6af04b77a56 57 {
dan_ackme 29:b6af04b77a56 58 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle)))
dan_ackme 29:b6af04b77a56 59 {
dan_ackme 29:b6af04b77a56 60 socket.init(handle, SOCKET_TYPE_GHM, GHM_HOST_URL, 443, SOCKET_ANY_PORT);
dan_ackme 29:b6af04b77a56 61 }
dan_ackme 29:b6af04b77a56 62 }
dan_ackme 29:b6af04b77a56 63
dan_ackme 29:b6af04b77a56 64 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 65
dan_ackme 29:b6af04b77a56 66 return result;
dan_ackme 29:b6af04b77a56 67 }
dan_ackme 29:b6af04b77a56 68
dan_ackme 29:b6af04b77a56 69 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 70 WiconnectResult GhmInterface::ghmGetMessage(WiconnectSocket &socket, GhmMessageGetType getType)
dan_ackme 29:b6af04b77a56 71 {
dan_ackme 29:b6af04b77a56 72 return ghmGetMessage(socket, 255, NULL, getType);
dan_ackme 29:b6af04b77a56 73 }
dan_ackme 29:b6af04b77a56 74
dan_ackme 29:b6af04b77a56 75 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 76 WiconnectResult GhmInterface::ghmGetMessage(WiconnectSocket &socket, uint8_t listIndex, GhmMessageGetType getType)
dan_ackme 29:b6af04b77a56 77 {
dan_ackme 29:b6af04b77a56 78 return ghmGetMessage(socket, listIndex, NULL, getType);
dan_ackme 29:b6af04b77a56 79 }
dan_ackme 29:b6af04b77a56 80
dan_ackme 29:b6af04b77a56 81 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 82 WiconnectResult GhmInterface::ghmGetMessage(WiconnectSocket &socket, const char *msgId, GhmMessageGetType getType)
dan_ackme 29:b6af04b77a56 83 {
dan_ackme 29:b6af04b77a56 84 return ghmGetMessage(socket, 255, msgId, getType);
dan_ackme 29:b6af04b77a56 85 }
dan_ackme 29:b6af04b77a56 86
dan_ackme 29:b6af04b77a56 87 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 88 WiconnectResult GhmInterface::ghmGetMessage(WiconnectSocket &socket, uint8_t listIndex, const char *msgId, GhmMessageGetType getType)
dan_ackme 29:b6af04b77a56 89 {
dan_ackme 29:b6af04b77a56 90 WiconnectResult result;
dan_ackme 29:b6af04b77a56 91 int32_t handle;
dan_ackme 29:b6af04b77a56 92 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 93
dan_ackme 29:b6af04b77a56 94 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 95 {
dan_ackme 29:b6af04b77a56 96 char *ptr = &cmdBuffer[7];
dan_ackme 29:b6af04b77a56 97 strcpy(cmdBuffer, "gme get");
dan_ackme 29:b6af04b77a56 98 if(listIndex != 255)
dan_ackme 29:b6af04b77a56 99 {
dan_ackme 29:b6af04b77a56 100 sprintf(ptr, " %u", listIndex);
dan_ackme 29:b6af04b77a56 101 }
dan_ackme 29:b6af04b77a56 102 else if(msgId != NULL)
dan_ackme 29:b6af04b77a56 103 {
dan_ackme 29:b6af04b77a56 104 sprintf(ptr, " %s", msgId);
dan_ackme 29:b6af04b77a56 105 }
dan_ackme 29:b6af04b77a56 106
dan_ackme 29:b6af04b77a56 107 if(getType == GHM_MSG_GET_BODY)
dan_ackme 29:b6af04b77a56 108 {
dan_ackme 29:b6af04b77a56 109 strcat(cmdBuffer, " body");
dan_ackme 29:b6af04b77a56 110 }
dan_ackme 29:b6af04b77a56 111 else if(getType == GHM_MSG_GET_ALL)
dan_ackme 29:b6af04b77a56 112 {
dan_ackme 29:b6af04b77a56 113 strcat(cmdBuffer, " all");
dan_ackme 29:b6af04b77a56 114 }
dan_ackme 29:b6af04b77a56 115 }
dan_ackme 29:b6af04b77a56 116
dan_ackme 29:b6af04b77a56 117 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 118
dan_ackme 29:b6af04b77a56 119 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(cmdBuffer)))
dan_ackme 29:b6af04b77a56 120 {
dan_ackme 29:b6af04b77a56 121 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle)))
dan_ackme 29:b6af04b77a56 122 {
dan_ackme 29:b6af04b77a56 123 socket.init(handle, SOCKET_TYPE_GHM, GHM_HOST_URL, 443, SOCKET_ANY_PORT);
dan_ackme 29:b6af04b77a56 124 }
dan_ackme 29:b6af04b77a56 125 }
dan_ackme 29:b6af04b77a56 126
dan_ackme 29:b6af04b77a56 127 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 128
dan_ackme 29:b6af04b77a56 129 return result;
dan_ackme 29:b6af04b77a56 130 }
dan_ackme 29:b6af04b77a56 131
dan_ackme 29:b6af04b77a56 132 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 133 WiconnectResult GhmInterface::ghmDeleteMessage(uint8_t listIndex)
dan_ackme 29:b6af04b77a56 134 {
dan_ackme 29:b6af04b77a56 135 WiconnectResult result;
dan_ackme 29:b6af04b77a56 136 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 137
dan_ackme 29:b6af04b77a56 138 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 139 {
dan_ackme 29:b6af04b77a56 140 sprintf(cmdBuffer, "gme delete %u", listIndex);
dan_ackme 29:b6af04b77a56 141 }
dan_ackme 29:b6af04b77a56 142
dan_ackme 29:b6af04b77a56 143 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 144
dan_ackme 29:b6af04b77a56 145 result = wiconnect->sendCommand(cmdBuffer);
dan_ackme 29:b6af04b77a56 146
dan_ackme 29:b6af04b77a56 147 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 148
dan_ackme 29:b6af04b77a56 149 return result;
dan_ackme 29:b6af04b77a56 150 }
dan_ackme 29:b6af04b77a56 151
dan_ackme 29:b6af04b77a56 152 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 153 WiconnectResult GhmInterface::ghmDeleteMessage(const char *msgId)
dan_ackme 29:b6af04b77a56 154 {
dan_ackme 29:b6af04b77a56 155 WiconnectResult result;
dan_ackme 29:b6af04b77a56 156 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 157
dan_ackme 29:b6af04b77a56 158 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 159 {
dan_ackme 29:b6af04b77a56 160 sprintf(cmdBuffer, "gme delete %s", msgId);
dan_ackme 29:b6af04b77a56 161 }
dan_ackme 29:b6af04b77a56 162
dan_ackme 29:b6af04b77a56 163 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 164
dan_ackme 29:b6af04b77a56 165 result = wiconnect->sendCommand(cmdBuffer);
dan_ackme 29:b6af04b77a56 166
dan_ackme 29:b6af04b77a56 167 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 168
dan_ackme 29:b6af04b77a56 169 return result;
dan_ackme 29:b6af04b77a56 170 }
dan_ackme 29:b6af04b77a56 171
dan_ackme 29:b6af04b77a56 172 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 173 WiconnectResult GhmInterface::ghmListMessages(GhmMessageList &msgList, uint8_t maxCount)
dan_ackme 29:b6af04b77a56 174 {
dan_ackme 29:b6af04b77a56 175 WiconnectResult result;
dan_ackme 29:b6af04b77a56 176 int32_t handle;
dan_ackme 29:b6af04b77a56 177 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 178
dan_ackme 29:b6af04b77a56 179 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 180 {
dan_ackme 29:b6af04b77a56 181 msgList.reset();
dan_ackme 29:b6af04b77a56 182 strcpy(cmdBuffer, "gme list");
dan_ackme 29:b6af04b77a56 183 if(maxCount != 0)
dan_ackme 29:b6af04b77a56 184 {
dan_ackme 29:b6af04b77a56 185 sprintf(&cmdBuffer[8], " -c %u", maxCount);
dan_ackme 29:b6af04b77a56 186 }
dan_ackme 29:b6af04b77a56 187 }
dan_ackme 29:b6af04b77a56 188
dan_ackme 29:b6af04b77a56 189 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 190
dan_ackme 29:b6af04b77a56 191 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(cmdBuffer)))
dan_ackme 29:b6af04b77a56 192 {
dan_ackme 29:b6af04b77a56 193 result = processMessageList(wiconnect->internalBuffer, msgList);
dan_ackme 29:b6af04b77a56 194 }
dan_ackme 29:b6af04b77a56 195
dan_ackme 29:b6af04b77a56 196 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 197
dan_ackme 29:b6af04b77a56 198 return result;
dan_ackme 29:b6af04b77a56 199 }
dan_ackme 29:b6af04b77a56 200
dan_ackme 29:b6af04b77a56 201
dan_ackme 29:b6af04b77a56 202 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 203 WiconnectResult GhmInterface::processMessageList(char *resultStr, GhmMessageList &resultList)
dan_ackme 29:b6af04b77a56 204 {
dan_ackme 29:b6af04b77a56 205 WiconnectResult result = WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 206 char *line, *savedLine;
dan_ackme 29:b6af04b77a56 207
dan_ackme 29:b6af04b77a56 208 for(savedLine = resultStr; (line = StringUtil::strtok_r(savedLine, "\r\n", &savedLine)) != NULL;)
dan_ackme 29:b6af04b77a56 209 {
dan_ackme 29:b6af04b77a56 210 char *toks[4], *savedTok;
dan_ackme 29:b6af04b77a56 211
dan_ackme 29:b6af04b77a56 212 if(*line != '#')
dan_ackme 29:b6af04b77a56 213 {
dan_ackme 29:b6af04b77a56 214 continue;
dan_ackme 29:b6af04b77a56 215 }
dan_ackme 29:b6af04b77a56 216 savedTok = line + 2;
dan_ackme 29:b6af04b77a56 217
dan_ackme 29:b6af04b77a56 218 for(int i = 0; i < 4 && (toks[i] = StringUtil::strtok_r(savedTok, " ", &savedTok)) != NULL; ++i)
dan_ackme 29:b6af04b77a56 219 {
dan_ackme 29:b6af04b77a56 220 if(toks[i] == NULL)
dan_ackme 29:b6af04b77a56 221 {
dan_ackme 29:b6af04b77a56 222 result = WICONNECT_RESPONSE_PARSE_ERROR;
dan_ackme 29:b6af04b77a56 223 goto exit;
dan_ackme 29:b6af04b77a56 224 }
dan_ackme 29:b6af04b77a56 225 }
dan_ackme 29:b6af04b77a56 226
dan_ackme 29:b6af04b77a56 227 if(WICONNECT_FAILED(result, resultList.add(toks[1], toks[2], toks[3])))
dan_ackme 29:b6af04b77a56 228 {
dan_ackme 29:b6af04b77a56 229 goto exit;
dan_ackme 29:b6af04b77a56 230 }
dan_ackme 29:b6af04b77a56 231 }
dan_ackme 29:b6af04b77a56 232
dan_ackme 29:b6af04b77a56 233 exit:
dan_ackme 29:b6af04b77a56 234 return result;
dan_ackme 29:b6af04b77a56 235 }