The (alpha of the) official Exosite library, CoAP version.

NOTE: This mirror repository may be out of date, check the main repo for changes. If there are any remind me to update this mirror.

This is an unstable alpha of the Official Exosite library, there are known issues with the port to this platform. You probably shouldn't use this library yet if you just want to get things done.

This version uses CoAP for the application protocol.

Committer:
Patrick Barrett
Date:
Tue Jan 06 11:22:43 2015 -0600
Revision:
23:e94087cd483d
Parent:
20:e98884244926
Child:
24:cfbd25fc62c4
no more magic number for minimum datagram size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Patrick Barrett 0:c7393ae16c16 1 /*****************************************************************************
Patrick Barrett 0:c7393ae16c16 2 *
Patrick Barrett 0:c7393ae16c16 3 * exosite.c - Exosite Activator Library
Patrick Barrett 0:c7393ae16c16 4 * Copyright (C) 2015 Exosite LLC
Patrick Barrett 0:c7393ae16c16 5 *
Patrick Barrett 0:c7393ae16c16 6 * Redistribution and use in source and binary forms, with or without
Patrick Barrett 0:c7393ae16c16 7 * modification, are permitted provided that the following conditions
Patrick Barrett 0:c7393ae16c16 8 * are met:
Patrick Barrett 0:c7393ae16c16 9 *
Patrick Barrett 0:c7393ae16c16 10 * Redistributions of source code must retain the above copyright
Patrick Barrett 0:c7393ae16c16 11 * notice, this list of conditions and the following disclaimer.
Patrick Barrett 0:c7393ae16c16 12 *
Patrick Barrett 0:c7393ae16c16 13 * Redistributions in binary form must reproduce the above copyright
Patrick Barrett 0:c7393ae16c16 14 * notice, this list of conditions and the following disclaimer in the
Patrick Barrett 0:c7393ae16c16 15 * documentation and/or other materials provided with the
Patrick Barrett 0:c7393ae16c16 16 * distribution.
Patrick Barrett 0:c7393ae16c16 17 *
Patrick Barrett 0:c7393ae16c16 18 * Neither the name of Texas Instruments Incorporated nor the names of
Patrick Barrett 0:c7393ae16c16 19 * its contributors may be used to endorse or promote products derived
Patrick Barrett 0:c7393ae16c16 20 * from this software without specific prior written permission.
Patrick Barrett 0:c7393ae16c16 21 *
Patrick Barrett 0:c7393ae16c16 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Patrick Barrett 0:c7393ae16c16 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Patrick Barrett 0:c7393ae16c16 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Patrick Barrett 0:c7393ae16c16 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Patrick Barrett 0:c7393ae16c16 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Patrick Barrett 0:c7393ae16c16 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Patrick Barrett 0:c7393ae16c16 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Patrick Barrett 0:c7393ae16c16 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Patrick Barrett 0:c7393ae16c16 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Patrick Barrett 0:c7393ae16c16 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Patrick Barrett 0:c7393ae16c16 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Barrett 0:c7393ae16c16 33 *
Patrick Barrett 0:c7393ae16c16 34 *****************************************************************************/
Patrick Barrett 0:c7393ae16c16 35 #include "exosite_pal.h"
Patrick Barrett 0:c7393ae16c16 36 #include "exosite.h"
Patrick Barrett 0:c7393ae16c16 37 #include "coap.h"
Patrick Barrett 0:c7393ae16c16 38
Patrick Barrett 0:c7393ae16c16 39 #include <stdlib.h>
Patrick Barrett 0:c7393ae16c16 40 #include <stdbool.h>
Patrick Barrett 0:c7393ae16c16 41 #include <time.h>
Patrick Barrett 0:c7393ae16c16 42 #include <string.h>
Patrick Barrett 0:c7393ae16c16 43 #include <ctype.h>
Patrick Barrett 0:c7393ae16c16 44
Patrick Barrett 0:c7393ae16c16 45 // Internal Functions
Patrick Barrett 12:68513d2aa2df 46
Patrick Barrett 12:68513d2aa2df 47 static void exo_process_waiting_datagrams(exo_op *op, uint8_t count);
Patrick Barrett 12:68513d2aa2df 48 static void exo_process_active_ops(exo_op *op, uint8_t count);
Patrick Barrett 0:c7393ae16c16 49 exo_error exo_build_msg_activate(coap_pdu *pdu, const char *vendor, const char *model, const char *serial_number);
Patrick Barrett 0:c7393ae16c16 50 exo_error exo_build_msg_read(coap_pdu *pdu, const char *alias);
Patrick Barrett 0:c7393ae16c16 51 exo_error exo_build_msg_observe(coap_pdu *pdu, const char *alias);
Patrick Barrett 0:c7393ae16c16 52 exo_error exo_build_msg_write(coap_pdu *pdu, const char *alias, const char *value);
Patrick Barrett 0:c7393ae16c16 53 exo_error exo_build_msg_rst(coap_pdu *pdu, const uint16_t mid, const uint64_t token, const uint8_t tkl);
Patrick Barrett 0:c7393ae16c16 54 exo_error exo_build_msg_ack(coap_pdu *pdu, const uint16_t mid);
Patrick Barrett 0:c7393ae16c16 55 uint8_t exosite_validate_cik(char *cik);
Patrick Barrett 0:c7393ae16c16 56
Patrick Barrett 0:c7393ae16c16 57 // Local Variables
Patrick Barrett 0:c7393ae16c16 58 static char cik[CIK_LENGTH + 1];
Patrick Barrett 0:c7393ae16c16 59 static const char *vendor;
Patrick Barrett 0:c7393ae16c16 60 static const char *model;
Patrick Barrett 0:c7393ae16c16 61 static const char *serial;
Patrick Barrett 0:c7393ae16c16 62 static uint16_t message_id_counter;
Patrick Barrett 0:c7393ae16c16 63 static exo_device_state device_state = EXO_STATE_UNINITIALIZED;
Patrick Barrett 0:c7393ae16c16 64
Patrick Barrett 23:e94087cd483d 65 // Internal Constants
Patrick Barrett 23:e94087cd483d 66 static const int MINIMUM_DATAGRAM_SIZE = 576; // RFC791: all hosts must accept minimum of 576 octets
Patrick Barrett 23:e94087cd483d 67
Patrick Barrett 0:c7393ae16c16 68 /*!
Patrick Barrett 0:c7393ae16c16 69 * \brief Initializes the Exosite library
Patrick Barrett 0:c7393ae16c16 70 *
Patrick Barrett 0:c7393ae16c16 71 * This **MUST** be called before any other exosite library calls are called.
Patrick Barrett 0:c7393ae16c16 72 *
Patrick Barrett 0:c7393ae16c16 73 * \param[in] *vendor Pointer to string containing the vendor name
Patrick Barrett 0:c7393ae16c16 74 * \param[in] *model Pointer to string containing the model name
Patrick Barrett 0:c7393ae16c16 75 * \param[in] *serial Pointer to string containing the serial number
Patrick Barrett 0:c7393ae16c16 76 *
Patrick Barrett 0:c7393ae16c16 77 * \return EXO_ERROR, EXO_OK on success, else error code
Patrick Barrett 0:c7393ae16c16 78 */
Patrick Barrett 0:c7393ae16c16 79 exo_error exo_init(const char *vendor_in, const char *model_in, const char *serial_in)
Patrick Barrett 0:c7393ae16c16 80 {
Patrick Barrett 0:c7393ae16c16 81 device_state = EXO_STATE_UNINITIALIZED;
Patrick Barrett 0:c7393ae16c16 82
Patrick Barrett 0:c7393ae16c16 83 if (exopal_init() != 0) {
Patrick Barrett 0:c7393ae16c16 84 return EXO_FATAL_ERROR_PAL;
Patrick Barrett 0:c7393ae16c16 85 }
Patrick Barrett 0:c7393ae16c16 86
Patrick Barrett 0:c7393ae16c16 87 srand(time(NULL));
Patrick Barrett 0:c7393ae16c16 88 message_id_counter = rand();
Patrick Barrett 0:c7393ae16c16 89
Patrick Barrett 0:c7393ae16c16 90 serial = serial_in;
Patrick Barrett 0:c7393ae16c16 91 vendor = vendor_in;
Patrick Barrett 0:c7393ae16c16 92 model = model_in;
Patrick Barrett 0:c7393ae16c16 93
Patrick Barrett 0:c7393ae16c16 94 if (exopal_retrieve_cik(cik) > 1){
Patrick Barrett 0:c7393ae16c16 95 return EXO_FATAL_ERROR_PAL;
Patrick Barrett 0:c7393ae16c16 96 } else {
Patrick Barrett 0:c7393ae16c16 97 cik[40] = 0;
Patrick Barrett 0:c7393ae16c16 98 }
Patrick Barrett 0:c7393ae16c16 99
Patrick Barrett 0:c7393ae16c16 100 if (exopal_udp_sock() != 0) {
Patrick Barrett 0:c7393ae16c16 101 return EXO_FATAL_ERROR_PAL;
Patrick Barrett 0:c7393ae16c16 102 }
Patrick Barrett 0:c7393ae16c16 103
Patrick Barrett 0:c7393ae16c16 104 device_state = EXO_STATE_INITIALIZED;
Patrick Barrett 0:c7393ae16c16 105
Patrick Barrett 0:c7393ae16c16 106 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 107 }
Patrick Barrett 0:c7393ae16c16 108
Patrick Barrett 0:c7393ae16c16 109 /*!
Patrick Barrett 0:c7393ae16c16 110 * \brief Queues a Write to the Exosite One Platform
Patrick Barrett 0:c7393ae16c16 111 *
Patrick Barrett 0:c7393ae16c16 112 * Queues a request to write to a dataport.
Patrick Barrett 0:c7393ae16c16 113 *
Patrick Barrett 0:c7393ae16c16 114 * \param[in] *alias Alias of dataport to write to, pointer must remain
Patrick Barrett 0:c7393ae16c16 115 * valid until the request has been sent.
Patrick Barrett 0:c7393ae16c16 116 * \param[in] callback Function pointer to call on success
Patrick Barrett 0:c7393ae16c16 117 *
Patrick Barrett 0:c7393ae16c16 118 * \return EXO_ERROR, EXO_OK on success or error code
Patrick Barrett 0:c7393ae16c16 119 *
Patrick Barrett 0:c7393ae16c16 120 */
Patrick Barrett 0:c7393ae16c16 121
Patrick Barrett 0:c7393ae16c16 122 void exo_write(exo_op *op, const char * alias, const char * value)
Patrick Barrett 0:c7393ae16c16 123 {
Patrick Barrett 0:c7393ae16c16 124 op->type = EXO_WRITE;
Patrick Barrett 0:c7393ae16c16 125 op->state = EXO_REQUEST_NEW;
Patrick Barrett 0:c7393ae16c16 126 op->alias = alias;
Patrick Barrett 0:c7393ae16c16 127 op->value = (char *)value; // this is kinda dirty, I know
Patrick Barrett 0:c7393ae16c16 128 op->value_max = 0;
Patrick Barrett 0:c7393ae16c16 129 op->mid = 0;
Patrick Barrett 0:c7393ae16c16 130 }
Patrick Barrett 0:c7393ae16c16 131
Patrick Barrett 0:c7393ae16c16 132 /*!
Patrick Barrett 0:c7393ae16c16 133 * \brief Queues a Read from the Exosite One Platform
Patrick Barrett 0:c7393ae16c16 134 *
Patrick Barrett 0:c7393ae16c16 135 * Queues a request to read a dataport. If the request is successful, the
Patrick Barrett 0:c7393ae16c16 136 * provided callback will be called with a single parameter, a pointer to the
Patrick Barrett 0:c7393ae16c16 137 * result as a C string.
Patrick Barrett 0:c7393ae16c16 138 *
Patrick Barrett 0:c7393ae16c16 139 * \param[in] *alias Alias of dataport to read from, pointer must remain
Patrick Barrett 0:c7393ae16c16 140 * valid until the request has been sent.
Patrick Barrett 0:c7393ae16c16 141 * \param[in] callback Function pointer to call on success
Patrick Barrett 0:c7393ae16c16 142 *
Patrick Barrett 0:c7393ae16c16 143 * \return EXO_ERROR, EXO_OK on success or error code
Patrick Barrett 0:c7393ae16c16 144 *
Patrick Barrett 0:c7393ae16c16 145 */
Patrick Barrett 0:c7393ae16c16 146 void exo_read(exo_op *op, const char * alias, char * value, const size_t value_max)
Patrick Barrett 0:c7393ae16c16 147 {
Patrick Barrett 0:c7393ae16c16 148 op->type = EXO_READ;
Patrick Barrett 0:c7393ae16c16 149 op->state = EXO_REQUEST_NEW;
Patrick Barrett 0:c7393ae16c16 150 op->alias = alias;
Patrick Barrett 0:c7393ae16c16 151 op->value = value;
Patrick Barrett 0:c7393ae16c16 152 op->value_max = value_max;
Patrick Barrett 0:c7393ae16c16 153 op->mid = 0;
Patrick Barrett 0:c7393ae16c16 154 }
Patrick Barrett 0:c7393ae16c16 155
Patrick Barrett 0:c7393ae16c16 156 /*!
Patrick Barrett 0:c7393ae16c16 157 * \brief Subscribes to a Dataport on the Exosite One Platform
Patrick Barrett 0:c7393ae16c16 158 *
Patrick Barrett 0:c7393ae16c16 159 * Begins a subscription to a dataport The callback is called any time the
Patrick Barrett 0:c7393ae16c16 160 * value is updated as well as when the subscription is started.
Patrick Barrett 0:c7393ae16c16 161 *
Patrick Barrett 0:c7393ae16c16 162 * \param[in] alias Alias of dataport to read from
Patrick Barrett 0:c7393ae16c16 163 * \param[in] callback Function pointer to call on change
Patrick Barrett 0:c7393ae16c16 164 *
Patrick Barrett 0:c7393ae16c16 165 * \return EXO_ERROR, EXO_OK on success or error code
Patrick Barrett 0:c7393ae16c16 166 *
Patrick Barrett 0:c7393ae16c16 167 */
Patrick Barrett 0:c7393ae16c16 168 void exo_subscribe(exo_op *op, const char * alias, char * value, const size_t value_max)
Patrick Barrett 0:c7393ae16c16 169 {
Patrick Barrett 0:c7393ae16c16 170 op->type = EXO_SUBSCRIBE;
Patrick Barrett 0:c7393ae16c16 171 op->state = EXO_REQUEST_NEW;
Patrick Barrett 0:c7393ae16c16 172 op->alias = alias;
Patrick Barrett 0:c7393ae16c16 173 op->value = value;
Patrick Barrett 0:c7393ae16c16 174 op->value_max = value_max;
Patrick Barrett 0:c7393ae16c16 175 op->mid = 0;
Patrick Barrett 0:c7393ae16c16 176 }
Patrick Barrett 0:c7393ae16c16 177
Patrick Barrett 11:08bae8fb9824 178 /*!
Patrick Barrett 11:08bae8fb9824 179 * \brief Activates the Device on the Platform
Patrick Barrett 11:08bae8fb9824 180 *
Patrick Barrett 11:08bae8fb9824 181 * Queues an activation request. Usually only used internally.
Patrick Barrett 11:08bae8fb9824 182 *
Patrick Barrett 11:08bae8fb9824 183 */
Patrick Barrett 11:08bae8fb9824 184 void exo_activate(exo_op *op)
Patrick Barrett 11:08bae8fb9824 185 {
Patrick Barrett 11:08bae8fb9824 186 op->type = EXO_ACTIVATE;
Patrick Barrett 11:08bae8fb9824 187 op->state = EXO_REQUEST_NEW;
Patrick Barrett 11:08bae8fb9824 188 op->alias = NULL;
Patrick Barrett 11:08bae8fb9824 189 op->value = NULL;
Patrick Barrett 11:08bae8fb9824 190 op->value_max = 0;
Patrick Barrett 11:08bae8fb9824 191 op->mid = 0;
Patrick Barrett 11:08bae8fb9824 192 }
Patrick Barrett 11:08bae8fb9824 193
Patrick Barrett 0:c7393ae16c16 194 void exo_op_init(exo_op *op)
Patrick Barrett 0:c7393ae16c16 195 {
Patrick Barrett 0:c7393ae16c16 196 op->type = EXO_NULL;
Patrick Barrett 0:c7393ae16c16 197 op->state = EXO_REQUEST_NULL;
Patrick Barrett 0:c7393ae16c16 198 op->alias = NULL;
Patrick Barrett 0:c7393ae16c16 199 op->value = NULL;
Patrick Barrett 0:c7393ae16c16 200 op->value_max = 0;
Patrick Barrett 0:c7393ae16c16 201 op->mid = 0;
Patrick Barrett 10:76deeaebfafb 202 op->obs_seq = 0;
Patrick Barrett 20:e98884244926 203 op->tkl = 0;
Patrick Barrett 20:e98884244926 204 op->token = 0;
Patrick Barrett 13:df08a86dc06d 205 op->timeout = 0;
Patrick Barrett 0:c7393ae16c16 206 }
Patrick Barrett 0:c7393ae16c16 207
Patrick Barrett 0:c7393ae16c16 208 void exo_op_done(exo_op *op)
Patrick Barrett 0:c7393ae16c16 209 {
Patrick Barrett 0:c7393ae16c16 210 if (exo_is_op_subscribe(op)) {
Patrick Barrett 0:c7393ae16c16 211 op->state = EXO_REQUEST_SUBSCRIBED;
Patrick Barrett 0:c7393ae16c16 212 } else {
Patrick Barrett 0:c7393ae16c16 213 exo_op_init(op);
Patrick Barrett 0:c7393ae16c16 214 }
Patrick Barrett 0:c7393ae16c16 215 }
Patrick Barrett 0:c7393ae16c16 216
Patrick Barrett 0:c7393ae16c16 217 uint8_t exo_is_op_valid(exo_op *op)
Patrick Barrett 0:c7393ae16c16 218 {
Patrick Barrett 0:c7393ae16c16 219 return op->type != EXO_NULL;
Patrick Barrett 0:c7393ae16c16 220 }
Patrick Barrett 0:c7393ae16c16 221
Patrick Barrett 0:c7393ae16c16 222 uint8_t exo_is_op_success(exo_op *op)
Patrick Barrett 0:c7393ae16c16 223 {
Patrick Barrett 0:c7393ae16c16 224 return op->state == EXO_REQUEST_SUCCESS;
Patrick Barrett 0:c7393ae16c16 225 }
Patrick Barrett 0:c7393ae16c16 226
Patrick Barrett 0:c7393ae16c16 227 uint8_t exo_is_op_finished(exo_op *op)
Patrick Barrett 0:c7393ae16c16 228 {
Patrick Barrett 0:c7393ae16c16 229 return op->state == EXO_REQUEST_SUCCESS || op->state == EXO_REQUEST_ERROR;
Patrick Barrett 0:c7393ae16c16 230 }
Patrick Barrett 0:c7393ae16c16 231
Patrick Barrett 0:c7393ae16c16 232 uint8_t exo_is_op_read(exo_op *op)
Patrick Barrett 0:c7393ae16c16 233 {
Patrick Barrett 0:c7393ae16c16 234 return op->type == EXO_READ;
Patrick Barrett 0:c7393ae16c16 235 }
Patrick Barrett 0:c7393ae16c16 236
Patrick Barrett 0:c7393ae16c16 237 uint8_t exo_is_op_subscribe(exo_op *op)
Patrick Barrett 0:c7393ae16c16 238 {
Patrick Barrett 0:c7393ae16c16 239 return op->type == EXO_SUBSCRIBE;
Patrick Barrett 0:c7393ae16c16 240 }
Patrick Barrett 0:c7393ae16c16 241
Patrick Barrett 0:c7393ae16c16 242 uint8_t exo_is_op_write(exo_op *op)
Patrick Barrett 0:c7393ae16c16 243 {
Patrick Barrett 0:c7393ae16c16 244 return op->type == EXO_WRITE;
Patrick Barrett 0:c7393ae16c16 245 }
Patrick Barrett 0:c7393ae16c16 246
Patrick Barrett 0:c7393ae16c16 247
Patrick Barrett 0:c7393ae16c16 248 /*!
Patrick Barrett 0:c7393ae16c16 249 * \brief Performs queued operations with the Exosite One Platform
Patrick Barrett 0:c7393ae16c16 250 *
Patrick Barrett 0:c7393ae16c16 251 * Queues a request to read a dataport from the Exosite One Platform. If the
Patrick Barrett 0:c7393ae16c16 252 * request is successful the provided callback will be called with a single
Patrick Barrett 0:c7393ae16c16 253 * parameter, a pointer to the result as a C string.
Patrick Barrett 0:c7393ae16c16 254 *
Patrick Barrett 0:c7393ae16c16 255 * \param[in] alias Alias of dataport to read from
Patrick Barrett 0:c7393ae16c16 256 * \param[in] callback Function pointer to call on success
Patrick Barrett 0:c7393ae16c16 257 *
Patrick Barrett 0:c7393ae16c16 258 * \return EXO_STATE, EXO_OK on success or error code
Patrick Barrett 0:c7393ae16c16 259 *
Patrick Barrett 0:c7393ae16c16 260 */
Patrick Barrett 0:c7393ae16c16 261 exo_state exo_operate(exo_op *op, uint8_t count)
Patrick Barrett 0:c7393ae16c16 262 {
Patrick Barrett 12:68513d2aa2df 263 int i;
Patrick Barrett 12:68513d2aa2df 264
Patrick Barrett 12:68513d2aa2df 265 switch (device_state){
Patrick Barrett 12:68513d2aa2df 266 case EXO_STATE_UNINITIALIZED:
Patrick Barrett 12:68513d2aa2df 267 return EXO_ERROR;
Patrick Barrett 12:68513d2aa2df 268 case EXO_STATE_INITIALIZED:
Patrick Barrett 12:68513d2aa2df 269 case EXO_STATE_BAD_CIK:
Patrick Barrett 18:c463fb80d81d 270 if (op[0].state == EXO_REQUEST_NULL || op[0].timeout < exopal_get_time())
Patrick Barrett 12:68513d2aa2df 271 exo_activate(&op[0]);
Patrick Barrett 12:68513d2aa2df 272 case EXO_STATE_GOOD:
Patrick Barrett 12:68513d2aa2df 273 break;
Patrick Barrett 12:68513d2aa2df 274 }
Patrick Barrett 12:68513d2aa2df 275
Patrick Barrett 12:68513d2aa2df 276 exo_process_waiting_datagrams(op, count);
Patrick Barrett 12:68513d2aa2df 277 exo_process_active_ops(op, count);
Patrick Barrett 12:68513d2aa2df 278
Patrick Barrett 12:68513d2aa2df 279 for (i = 0; i < count; i++) {
Patrick Barrett 12:68513d2aa2df 280 if (op[i].state == EXO_REQUEST_NEW)
Patrick Barrett 12:68513d2aa2df 281 return EXO_BUSY;
Patrick Barrett 12:68513d2aa2df 282 }
Patrick Barrett 12:68513d2aa2df 283
Patrick Barrett 12:68513d2aa2df 284 for (i = 0; i < count; i++) {
Patrick Barrett 12:68513d2aa2df 285 if (op[i].state == EXO_REQUEST_PENDING)
Patrick Barrett 12:68513d2aa2df 286 return EXO_WAITING;
Patrick Barrett 12:68513d2aa2df 287 }
Patrick Barrett 12:68513d2aa2df 288
Patrick Barrett 12:68513d2aa2df 289 return EXO_IDLE;
Patrick Barrett 12:68513d2aa2df 290 }
Patrick Barrett 12:68513d2aa2df 291
Patrick Barrett 12:68513d2aa2df 292 // Internal Functions
Patrick Barrett 12:68513d2aa2df 293
Patrick Barrett 12:68513d2aa2df 294 static void exo_process_waiting_datagrams(exo_op *op, uint8_t count)
Patrick Barrett 12:68513d2aa2df 295 {
Patrick Barrett 23:e94087cd483d 296 uint8_t buf[MINIMUM_DATAGRAM_SIZE];
Patrick Barrett 0:c7393ae16c16 297 coap_pdu pdu;
Patrick Barrett 0:c7393ae16c16 298 coap_payload payload;
Patrick Barrett 0:c7393ae16c16 299 int i;
Patrick Barrett 0:c7393ae16c16 300
Patrick Barrett 0:c7393ae16c16 301 pdu.buf = buf;
Patrick Barrett 23:e94087cd483d 302 pdu.max = MINIMUM_DATAGRAM_SIZE;
Patrick Barrett 0:c7393ae16c16 303 pdu.len = 0;
Patrick Barrett 0:c7393ae16c16 304
Patrick Barrett 0:c7393ae16c16 305 // receive a UDP packet if one or more waiting
Patrick Barrett 0:c7393ae16c16 306 while (exopal_udp_recv(pdu.buf, pdu.max, &pdu.len) == 0) {
Patrick Barrett 0:c7393ae16c16 307 if (coap_validate_pkt(&pdu) != CE_NONE)
Patrick Barrett 0:c7393ae16c16 308 continue; //Invalid Packet, Ignore
Patrick Barrett 0:c7393ae16c16 309
Patrick Barrett 0:c7393ae16c16 310 for (i = 0; i < count; i++) {
Patrick Barrett 17:661d4a5b7c1b 311 if (coap_get_type(&pdu) == CT_CON || coap_get_type(&pdu) == CT_NON) {
Patrick Barrett 19:323801b9c74e 312 if (op[i].state == EXO_REQUEST_SUBSCRIBED && coap_get_token(&pdu) == op[i].token) {
Patrick Barrett 19:323801b9c74e 313 if (coap_get_code(&pdu) == CC_CONTENT) {
Patrick Barrett 19:323801b9c74e 314 coap_option opt;
Patrick Barrett 19:323801b9c74e 315 uint32_t new_seq = 0;
Patrick Barrett 19:323801b9c74e 316 opt = coap_get_option_by_num(&pdu, CON_OBSERVE, 0);
Patrick Barrett 19:323801b9c74e 317 for (int j = 0; j < opt.len; j++) {
Patrick Barrett 19:323801b9c74e 318 new_seq = (new_seq << (8*j)) | opt.val[j];
Patrick Barrett 19:323801b9c74e 319 }
Patrick Barrett 13:df08a86dc06d 320
Patrick Barrett 19:323801b9c74e 321 payload = coap_get_payload(&pdu);
Patrick Barrett 19:323801b9c74e 322 if (payload.len == 0) {
Patrick Barrett 19:323801b9c74e 323 op[i].value[0] = '\0';
Patrick Barrett 19:323801b9c74e 324 } else if (payload.len+1 > op[i].value_max || op[i].value == 0) {
Patrick Barrett 19:323801b9c74e 325 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 19:323801b9c74e 326 } else{
Patrick Barrett 19:323801b9c74e 327 memcpy(op[i].value, payload.val, payload.len);
Patrick Barrett 19:323801b9c74e 328 op[i].value[payload.len] = 0;
Patrick Barrett 19:323801b9c74e 329 op[i].mid = coap_get_mid(&pdu);
Patrick Barrett 19:323801b9c74e 330 // TODO: User proper logic to ensure it's a new value not a different, but old one.
Patrick Barrett 19:323801b9c74e 331 if (op[i].obs_seq != new_seq) {
Patrick Barrett 19:323801b9c74e 332 op[i].state = EXO_REQUEST_SUB_ACK_NEW;
Patrick Barrett 19:323801b9c74e 333 op[i].obs_seq = new_seq;
Patrick Barrett 19:323801b9c74e 334 } else {
Patrick Barrett 19:323801b9c74e 335 op[i].state = EXO_REQUEST_SUB_ACK;
Patrick Barrett 19:323801b9c74e 336 }
Patrick Barrett 19:323801b9c74e 337 }
Patrick Barrett 19:323801b9c74e 338 } else if (coap_get_code_class(&pdu) != 2) {
Patrick Barrett 13:df08a86dc06d 339 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 11:08bae8fb9824 340 }
Patrick Barrett 13:df08a86dc06d 341 break;
Patrick Barrett 0:c7393ae16c16 342 }
Patrick Barrett 17:661d4a5b7c1b 343 } else if (coap_get_type(&pdu) == CT_ACK) {
Patrick Barrett 13:df08a86dc06d 344 if (op[i].state == EXO_REQUEST_PENDING && op[i].mid == coap_get_mid(&pdu)) {
Patrick Barrett 13:df08a86dc06d 345 if (coap_get_code_class(&pdu) == 2) {
Patrick Barrett 17:661d4a5b7c1b 346 switch (op[i].type) {
Patrick Barrett 17:661d4a5b7c1b 347 case EXO_WRITE:
Patrick Barrett 17:661d4a5b7c1b 348 op[i].state = EXO_REQUEST_SUCCESS;
Patrick Barrett 17:661d4a5b7c1b 349 break;
Patrick Barrett 17:661d4a5b7c1b 350 case EXO_READ:
Patrick Barrett 17:661d4a5b7c1b 351 payload = coap_get_payload(&pdu);
Patrick Barrett 17:661d4a5b7c1b 352 if (payload.len == 0) {
Patrick Barrett 17:661d4a5b7c1b 353 op[i].value[0] = '\0';
Patrick Barrett 17:661d4a5b7c1b 354 } else if (payload.len+1 > op[i].value_max || op[i].value == 0) {
Patrick Barrett 17:661d4a5b7c1b 355 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 17:661d4a5b7c1b 356 } else{
Patrick Barrett 17:661d4a5b7c1b 357 memcpy(op[i].value, payload.val, payload.len);
Patrick Barrett 17:661d4a5b7c1b 358 op[i].value[payload.len] = 0;
Patrick Barrett 17:661d4a5b7c1b 359 op[i].state = EXO_REQUEST_SUCCESS;
Patrick Barrett 17:661d4a5b7c1b 360 }
Patrick Barrett 17:661d4a5b7c1b 361 break;
Patrick Barrett 17:661d4a5b7c1b 362 case EXO_SUBSCRIBE:
Patrick Barrett 17:661d4a5b7c1b 363 payload = coap_get_payload(&pdu);
Patrick Barrett 17:661d4a5b7c1b 364 if (payload.len == 0) {
Patrick Barrett 17:661d4a5b7c1b 365 op[i].value[0] = '\0';
Patrick Barrett 17:661d4a5b7c1b 366 } else if (payload.len+1 > op[i].value_max || op[i].value == 0) {
Patrick Barrett 17:661d4a5b7c1b 367 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 17:661d4a5b7c1b 368 } else{
Patrick Barrett 17:661d4a5b7c1b 369 memcpy(op[i].value, payload.val, payload.len);
Patrick Barrett 17:661d4a5b7c1b 370 op[i].value[payload.len] = 0;
Patrick Barrett 17:661d4a5b7c1b 371 op[i].state = EXO_REQUEST_SUCCESS;
Patrick Barrett 17:661d4a5b7c1b 372 op[i].timeout = exopal_get_time() + 120000000 + (rand() % 15 * 100000);
Patrick Barrett 17:661d4a5b7c1b 373 }
Patrick Barrett 17:661d4a5b7c1b 374 break;
Patrick Barrett 17:661d4a5b7c1b 375 case EXO_ACTIVATE:
Patrick Barrett 17:661d4a5b7c1b 376 payload = coap_get_payload(&pdu);
Patrick Barrett 17:661d4a5b7c1b 377 if (payload.len == CIK_LENGTH) {
Patrick Barrett 17:661d4a5b7c1b 378 memcpy(cik, payload.val, CIK_LENGTH);
Patrick Barrett 17:661d4a5b7c1b 379 cik[CIK_LENGTH] = 0;
Patrick Barrett 17:661d4a5b7c1b 380 op[i].state = EXO_REQUEST_SUCCESS;
Patrick Barrett 17:661d4a5b7c1b 381 exopal_store_cik(cik);
Patrick Barrett 17:661d4a5b7c1b 382 device_state = EXO_STATE_GOOD;
Patrick Barrett 17:661d4a5b7c1b 383 } else {
Patrick Barrett 17:661d4a5b7c1b 384 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 17:661d4a5b7c1b 385 }
Patrick Barrett 17:661d4a5b7c1b 386
Patrick Barrett 17:661d4a5b7c1b 387 // We're done with this op now.
Patrick Barrett 17:661d4a5b7c1b 388 exo_op_init(&op[i]);
Patrick Barrett 17:661d4a5b7c1b 389 break;
Patrick Barrett 17:661d4a5b7c1b 390 case EXO_NULL: // pending null request? shouldn't be possible
Patrick Barrett 17:661d4a5b7c1b 391 continue;
Patrick Barrett 13:df08a86dc06d 392 }
Patrick Barrett 13:df08a86dc06d 393 } else {
Patrick Barrett 13:df08a86dc06d 394 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 17:661d4a5b7c1b 395
Patrick Barrett 17:661d4a5b7c1b 396 if (coap_get_code(&pdu) == CC_UNAUTHORIZED){
Patrick Barrett 18:c463fb80d81d 397 //device_state = EXO_STATE_BAD_CIK;
Patrick Barrett 18:c463fb80d81d 398
Patrick Barrett 18:c463fb80d81d 399 if (op[0].type == EXO_NULL || op[0].timeout < exopal_get_time())
Patrick Barrett 18:c463fb80d81d 400 exo_activate(&op[0]);
Patrick Barrett 17:661d4a5b7c1b 401 } else if (coap_get_code(&pdu) == CC_NOT_FOUND) {
Patrick Barrett 17:661d4a5b7c1b 402 device_state = EXO_STATE_GOOD;
Patrick Barrett 17:661d4a5b7c1b 403 }
Patrick Barrett 13:df08a86dc06d 404 }
Patrick Barrett 0:c7393ae16c16 405
Patrick Barrett 17:661d4a5b7c1b 406 break;
Patrick Barrett 17:661d4a5b7c1b 407 }
Patrick Barrett 17:661d4a5b7c1b 408
Patrick Barrett 17:661d4a5b7c1b 409 } else if (coap_get_type(&pdu) == CT_RST) {
Patrick Barrett 17:661d4a5b7c1b 410 if ((op[i].state == EXO_REQUEST_PENDING || op[i].state == EXO_REQUEST_SUBSCRIBED) &&
Patrick Barrett 17:661d4a5b7c1b 411 (op[i].mid == coap_get_mid(&pdu) && op[i].token == coap_get_token(&pdu))){
Patrick Barrett 17:661d4a5b7c1b 412 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 13:df08a86dc06d 413 break;
Patrick Barrett 0:c7393ae16c16 414 }
Patrick Barrett 0:c7393ae16c16 415 }
Patrick Barrett 0:c7393ae16c16 416 }
Patrick Barrett 0:c7393ae16c16 417
Patrick Barrett 13:df08a86dc06d 418 // if the above loop ends normally we don't recognize message, reply RST
Patrick Barrett 0:c7393ae16c16 419 if (i == count){
Patrick Barrett 0:c7393ae16c16 420 if (coap_get_type(&pdu) == CT_CON) {
Patrick Barrett 0:c7393ae16c16 421 // this can't fail
Patrick Barrett 0:c7393ae16c16 422 exo_build_msg_rst(&pdu, coap_get_mid(&pdu), coap_get_token(&pdu), coap_get_tkl(&pdu));
Patrick Barrett 0:c7393ae16c16 423
Patrick Barrett 0:c7393ae16c16 424 // best effort, don't bother checking if it failed, nothing we can do it
Patrick Barrett 0:c7393ae16c16 425 // it did anyway
Patrick Barrett 0:c7393ae16c16 426 exopal_udp_send(pdu.buf, pdu.len);
Patrick Barrett 0:c7393ae16c16 427 }
Patrick Barrett 0:c7393ae16c16 428
Patrick Barrett 0:c7393ae16c16 429 break;
Patrick Barrett 0:c7393ae16c16 430 }
Patrick Barrett 0:c7393ae16c16 431 }
Patrick Barrett 12:68513d2aa2df 432 }
Patrick Barrett 0:c7393ae16c16 433
Patrick Barrett 12:68513d2aa2df 434 // process all ops that are in an active state
Patrick Barrett 12:68513d2aa2df 435 static void exo_process_active_ops(exo_op *op, uint8_t count)
Patrick Barrett 12:68513d2aa2df 436 {
Patrick Barrett 23:e94087cd483d 437 uint8_t buf[MINIMUM_DATAGRAM_SIZE];
Patrick Barrett 12:68513d2aa2df 438 coap_pdu pdu;
Patrick Barrett 12:68513d2aa2df 439 int i;
Patrick Barrett 12:68513d2aa2df 440 uint64_t now = exopal_get_time();
Patrick Barrett 12:68513d2aa2df 441
Patrick Barrett 12:68513d2aa2df 442 pdu.buf = buf;
Patrick Barrett 23:e94087cd483d 443 pdu.max = MINIMUM_DATAGRAM_SIZE;
Patrick Barrett 12:68513d2aa2df 444 pdu.len = 0;
Patrick Barrett 12:68513d2aa2df 445
Patrick Barrett 0:c7393ae16c16 446 for (i = 0; i < count; i++) {
Patrick Barrett 0:c7393ae16c16 447 switch (op[i].state) {
Patrick Barrett 0:c7393ae16c16 448 case EXO_REQUEST_NEW:
Patrick Barrett 0:c7393ae16c16 449 // Build and Send Request
Patrick Barrett 0:c7393ae16c16 450 switch (op[i].type) {
Patrick Barrett 0:c7393ae16c16 451 case EXO_READ:
Patrick Barrett 0:c7393ae16c16 452 exo_build_msg_read(&pdu, op[i].alias);
Patrick Barrett 0:c7393ae16c16 453 break;
Patrick Barrett 0:c7393ae16c16 454 case EXO_SUBSCRIBE:
Patrick Barrett 0:c7393ae16c16 455 exo_build_msg_observe(&pdu, op[i].alias);
Patrick Barrett 0:c7393ae16c16 456 break;
Patrick Barrett 0:c7393ae16c16 457 case EXO_WRITE:
Patrick Barrett 0:c7393ae16c16 458 exo_build_msg_write(&pdu, op[i].alias, op[i].value);
Patrick Barrett 0:c7393ae16c16 459 break;
Patrick Barrett 11:08bae8fb9824 460 case EXO_ACTIVATE:
Patrick Barrett 11:08bae8fb9824 461 exo_build_msg_activate(&pdu, vendor, model, serial);
Patrick Barrett 11:08bae8fb9824 462 break;
Patrick Barrett 0:c7393ae16c16 463 default:
Patrick Barrett 0:c7393ae16c16 464 op[i].type = EXO_NULL;
Patrick Barrett 0:c7393ae16c16 465 continue;
Patrick Barrett 0:c7393ae16c16 466 }
Patrick Barrett 0:c7393ae16c16 467
Patrick Barrett 0:c7393ae16c16 468 if (exopal_udp_send(pdu.buf, pdu.len) == 0) {
Patrick Barrett 0:c7393ae16c16 469 op[i].state = EXO_REQUEST_PENDING;
Patrick Barrett 0:c7393ae16c16 470 op[i].timeout = exopal_get_time() + 4000000;
Patrick Barrett 0:c7393ae16c16 471 op[i].mid = coap_get_mid(&pdu);
Patrick Barrett 0:c7393ae16c16 472 op[i].token = coap_get_token(&pdu);
Patrick Barrett 0:c7393ae16c16 473 }
Patrick Barrett 0:c7393ae16c16 474
Patrick Barrett 0:c7393ae16c16 475 break;
Patrick Barrett 10:76deeaebfafb 476 case EXO_REQUEST_SUBSCRIBED:
Patrick Barrett 0:c7393ae16c16 477 case EXO_REQUEST_PENDING:
Patrick Barrett 0:c7393ae16c16 478 // check if pending requests have reached timeout
Patrick Barrett 0:c7393ae16c16 479 if (op[i].timeout <= now){
Patrick Barrett 0:c7393ae16c16 480 switch (op[i].type) {
Patrick Barrett 0:c7393ae16c16 481 case EXO_READ:
Patrick Barrett 0:c7393ae16c16 482 case EXO_WRITE:
Patrick Barrett 0:c7393ae16c16 483 // TODO: may want to do some sort of retry system
Patrick Barrett 0:c7393ae16c16 484 op[i].state = EXO_REQUEST_ERROR;
Patrick Barrett 0:c7393ae16c16 485 break;
Patrick Barrett 0:c7393ae16c16 486 case EXO_SUBSCRIBE:
Patrick Barrett 0:c7393ae16c16 487 // force a new observe request
Patrick Barrett 0:c7393ae16c16 488 op[i].state = EXO_REQUEST_NEW;
Patrick Barrett 0:c7393ae16c16 489 break;
Patrick Barrett 0:c7393ae16c16 490 default:
Patrick Barrett 0:c7393ae16c16 491 break;
Patrick Barrett 0:c7393ae16c16 492 }
Patrick Barrett 0:c7393ae16c16 493 }
Patrick Barrett 0:c7393ae16c16 494 break;
Patrick Barrett 0:c7393ae16c16 495 case EXO_REQUEST_SUB_ACK_NEW:
Patrick Barrett 0:c7393ae16c16 496 case EXO_REQUEST_SUB_ACK:
Patrick Barrett 0:c7393ae16c16 497 // send ack for observe notification
Patrick Barrett 0:c7393ae16c16 498 exo_build_msg_ack(&pdu, coap_get_mid(&pdu));
Patrick Barrett 0:c7393ae16c16 499
Patrick Barrett 0:c7393ae16c16 500 if (exopal_udp_send(pdu.buf, pdu.len) == 0) {
Patrick Barrett 0:c7393ae16c16 501 if (op[i].state == EXO_REQUEST_SUB_ACK)
Patrick Barrett 0:c7393ae16c16 502 op[i].state = EXO_REQUEST_SUBSCRIBED;
Patrick Barrett 0:c7393ae16c16 503 else if (op[i].state == EXO_REQUEST_SUB_ACK_NEW)
Patrick Barrett 0:c7393ae16c16 504 op[i].state = EXO_REQUEST_SUCCESS;
Patrick Barrett 0:c7393ae16c16 505
Patrick Barrett 0:c7393ae16c16 506 // TODO: this should get set to Max-Age value, hard coding 120 s
Patrick Barrett 10:76deeaebfafb 507 op[i].timeout = exopal_get_time() + 120000000 + (rand() % 15 * 100000);
Patrick Barrett 0:c7393ae16c16 508 }
Patrick Barrett 0:c7393ae16c16 509 break;
Patrick Barrett 0:c7393ae16c16 510 default:
Patrick Barrett 0:c7393ae16c16 511 break;
Patrick Barrett 0:c7393ae16c16 512 }
Patrick Barrett 0:c7393ae16c16 513 }
Patrick Barrett 0:c7393ae16c16 514 }
Patrick Barrett 0:c7393ae16c16 515
Patrick Barrett 0:c7393ae16c16 516
Patrick Barrett 0:c7393ae16c16 517 exo_error exo_build_msg_activate(coap_pdu *pdu, const char *vendor, const char *model, const char *serial_number)
Patrick Barrett 0:c7393ae16c16 518 {
Patrick Barrett 0:c7393ae16c16 519 coap_error ret;
Patrick Barrett 0:c7393ae16c16 520 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 521 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 522 ret |= coap_set_type(pdu, CT_CON);
Patrick Barrett 0:c7393ae16c16 523 ret |= coap_set_code(pdu, CC_POST);
Patrick Barrett 0:c7393ae16c16 524 ret |= coap_set_mid(pdu, message_id_counter++);
Patrick Barrett 0:c7393ae16c16 525 ret |= coap_set_token(pdu, rand(), 2);
Patrick Barrett 0:c7393ae16c16 526 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)"provision", 9);
Patrick Barrett 0:c7393ae16c16 527 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)"activate", 8);
Patrick Barrett 0:c7393ae16c16 528 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)vendor, strlen(vendor));
Patrick Barrett 0:c7393ae16c16 529 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)model, strlen(model));
Patrick Barrett 0:c7393ae16c16 530 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)serial_number, strlen(serial_number));
Patrick Barrett 0:c7393ae16c16 531
Patrick Barrett 0:c7393ae16c16 532 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 533 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 534
Patrick Barrett 0:c7393ae16c16 535 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 536 }
Patrick Barrett 0:c7393ae16c16 537
Patrick Barrett 0:c7393ae16c16 538 exo_error exo_build_msg_read(coap_pdu *pdu, const char *alias)
Patrick Barrett 0:c7393ae16c16 539 {
Patrick Barrett 0:c7393ae16c16 540 coap_error ret;
Patrick Barrett 0:c7393ae16c16 541 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 542 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 543 ret |= coap_set_type(pdu, CT_CON);
Patrick Barrett 0:c7393ae16c16 544 ret |= coap_set_code(pdu, CC_GET);
Patrick Barrett 0:c7393ae16c16 545 ret |= coap_set_mid(pdu, message_id_counter++);
Patrick Barrett 0:c7393ae16c16 546 ret |= coap_set_token(pdu, rand(), 2);
Patrick Barrett 0:c7393ae16c16 547 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)"1a", 2);
Patrick Barrett 0:c7393ae16c16 548 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)alias, strlen(alias));
Patrick Barrett 0:c7393ae16c16 549 ret |= coap_add_option(pdu, CON_URI_QUERY, (uint8_t*)cik, 40);
Patrick Barrett 0:c7393ae16c16 550
Patrick Barrett 0:c7393ae16c16 551 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 552 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 553
Patrick Barrett 0:c7393ae16c16 554 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 555 }
Patrick Barrett 0:c7393ae16c16 556
Patrick Barrett 0:c7393ae16c16 557 exo_error exo_build_msg_observe(coap_pdu *pdu, const char *alias)
Patrick Barrett 0:c7393ae16c16 558 {
Patrick Barrett 0:c7393ae16c16 559 uint8_t obs_opt = 0;
Patrick Barrett 0:c7393ae16c16 560 coap_error ret;
Patrick Barrett 0:c7393ae16c16 561 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 562 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 563 ret |= coap_set_type(pdu, CT_CON);
Patrick Barrett 0:c7393ae16c16 564 ret |= coap_set_code(pdu, CC_GET);
Patrick Barrett 0:c7393ae16c16 565 ret |= coap_set_mid(pdu, message_id_counter++);
Patrick Barrett 0:c7393ae16c16 566 ret |= coap_set_token(pdu, rand(), 2);
Patrick Barrett 0:c7393ae16c16 567 ret |= coap_add_option(pdu, CON_OBSERVE, &obs_opt, 1);
Patrick Barrett 0:c7393ae16c16 568 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)"1a", 2);
Patrick Barrett 0:c7393ae16c16 569 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)alias, strlen(alias));
Patrick Barrett 0:c7393ae16c16 570 ret |= coap_add_option(pdu, CON_URI_QUERY, (uint8_t*)cik, 40);
Patrick Barrett 0:c7393ae16c16 571
Patrick Barrett 0:c7393ae16c16 572 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 573 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 574
Patrick Barrett 0:c7393ae16c16 575 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 576 }
Patrick Barrett 0:c7393ae16c16 577
Patrick Barrett 0:c7393ae16c16 578 exo_error exo_build_msg_write(coap_pdu *pdu, const char *alias, const char *value)
Patrick Barrett 0:c7393ae16c16 579 {
Patrick Barrett 0:c7393ae16c16 580 coap_error ret;
Patrick Barrett 0:c7393ae16c16 581 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 582 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 583 ret |= coap_set_type(pdu, CT_CON);
Patrick Barrett 0:c7393ae16c16 584 ret |= coap_set_code(pdu, CC_POST);
Patrick Barrett 0:c7393ae16c16 585 ret |= coap_set_mid(pdu, message_id_counter++);
Patrick Barrett 0:c7393ae16c16 586 ret |= coap_set_token(pdu, rand(), 2);
Patrick Barrett 0:c7393ae16c16 587 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)"1a", 2);
Patrick Barrett 0:c7393ae16c16 588 ret |= coap_add_option(pdu, CON_URI_PATH, (uint8_t*)alias, strlen(alias));
Patrick Barrett 0:c7393ae16c16 589 ret |= coap_add_option(pdu, CON_URI_QUERY, (uint8_t*)cik, 40);
Patrick Barrett 0:c7393ae16c16 590 ret |= coap_set_payload(pdu, (uint8_t *)value, strlen(value));
Patrick Barrett 0:c7393ae16c16 591
Patrick Barrett 0:c7393ae16c16 592 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 593 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 594
Patrick Barrett 0:c7393ae16c16 595 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 596 }
Patrick Barrett 0:c7393ae16c16 597
Patrick Barrett 0:c7393ae16c16 598 exo_error exo_build_msg_rst(coap_pdu *pdu, const uint16_t mid, const uint64_t token, const uint8_t tkl)
Patrick Barrett 0:c7393ae16c16 599 {
Patrick Barrett 0:c7393ae16c16 600 coap_error ret;
Patrick Barrett 0:c7393ae16c16 601 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 602 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 603 ret |= coap_set_type(pdu, CT_RST);
Patrick Barrett 0:c7393ae16c16 604 ret |= coap_set_code(pdu, CC_EMPTY);
Patrick Barrett 0:c7393ae16c16 605 ret |= coap_set_mid(pdu, mid);
Patrick Barrett 0:c7393ae16c16 606 ret |= coap_set_token(pdu, token, tkl);
Patrick Barrett 0:c7393ae16c16 607
Patrick Barrett 0:c7393ae16c16 608 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 609 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 610
Patrick Barrett 0:c7393ae16c16 611 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 612 }
Patrick Barrett 0:c7393ae16c16 613
Patrick Barrett 0:c7393ae16c16 614 exo_error exo_build_msg_ack(coap_pdu *pdu, const uint16_t mid)
Patrick Barrett 0:c7393ae16c16 615 {
Patrick Barrett 0:c7393ae16c16 616 coap_error ret;
Patrick Barrett 0:c7393ae16c16 617 coap_init_pdu(pdu);
Patrick Barrett 0:c7393ae16c16 618 ret = coap_set_version(pdu, COAP_V1);
Patrick Barrett 0:c7393ae16c16 619 ret |= coap_set_type(pdu, CT_ACK);
Patrick Barrett 0:c7393ae16c16 620 ret |= coap_set_code(pdu, CC_EMPTY);
Patrick Barrett 0:c7393ae16c16 621 ret |= coap_set_mid(pdu, mid);
Patrick Barrett 0:c7393ae16c16 622
Patrick Barrett 0:c7393ae16c16 623 if (ret != CE_NONE)
Patrick Barrett 0:c7393ae16c16 624 return EXO_GENERAL_ERROR;
Patrick Barrett 0:c7393ae16c16 625
Patrick Barrett 0:c7393ae16c16 626 return EXO_OK;
Patrick Barrett 0:c7393ae16c16 627 }
Patrick Barrett 0:c7393ae16c16 628
Patrick Barrett 11:08bae8fb9824 629 exo_error exo_do_activate()
Patrick Barrett 0:c7393ae16c16 630 {
Patrick Barrett 11:08bae8fb9824 631 exo_op op;
Patrick Barrett 11:08bae8fb9824 632
Patrick Barrett 11:08bae8fb9824 633 exo_op_init(&op);
Patrick Barrett 11:08bae8fb9824 634
Patrick Barrett 0:c7393ae16c16 635
Patrick Barrett 11:08bae8fb9824 636 return EXO_OK;
Patrick Barrett 11:08bae8fb9824 637 }
Patrick Barrett 11:08bae8fb9824 638
Patrick Barrett 11:08bae8fb9824 639 uint8_t exo_util_is_ascii_hex(const char *str, size_t len)
Patrick Barrett 11:08bae8fb9824 640 {
Patrick Barrett 11:08bae8fb9824 641 size_t i;
Patrick Barrett 11:08bae8fb9824 642
Patrick Barrett 11:08bae8fb9824 643 for (i = 0; i < len; i++) {
Patrick Barrett 11:08bae8fb9824 644 if (!((str[i] >= 'a' && str[i] <= 'f') ||
Patrick Barrett 11:08bae8fb9824 645 (str[i] >= '0' && str[i] <= '9'))) {
Patrick Barrett 0:c7393ae16c16 646 return 1;
Patrick Barrett 0:c7393ae16c16 647 }
Patrick Barrett 0:c7393ae16c16 648 }
Patrick Barrett 0:c7393ae16c16 649
Patrick Barrett 0:c7393ae16c16 650 return 0;
Patrick Barrett 0:c7393ae16c16 651 }
Patrick Barrett 0:c7393ae16c16 652