Patrick Barrett / libexositecoap
Committer:
Patrick Barrett
Date:
Mon Dec 22 13:35:49 2014 -0600
Revision:
9:394abe2083e8
Parent:
3:1ea8c0eed00c
Child:
11:08bae8fb9824
clean build for POSIX examples on mac and linux

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 * Copyright (C) 2015 Exosite LLC
Patrick Barrett 0:c7393ae16c16 4 *
Patrick Barrett 0:c7393ae16c16 5 * Redistribution and use in source and binary forms, with or without
Patrick Barrett 0:c7393ae16c16 6 * modification, are permitted provided that the following conditions
Patrick Barrett 0:c7393ae16c16 7 * are met:
Patrick Barrett 0:c7393ae16c16 8 *
Patrick Barrett 0:c7393ae16c16 9 * Redistributions of source code must retain the above copyright
Patrick Barrett 0:c7393ae16c16 10 * notice, this list of conditions and the following disclaimer.
Patrick Barrett 0:c7393ae16c16 11 *
Patrick Barrett 0:c7393ae16c16 12 * Redistributions in binary form must reproduce the above copyright
Patrick Barrett 0:c7393ae16c16 13 * notice, this list of conditions and the following disclaimer in the
Patrick Barrett 0:c7393ae16c16 14 * documentation and/or other materials provided with the
Patrick Barrett 0:c7393ae16c16 15 * distribution.
Patrick Barrett 0:c7393ae16c16 16 *
Patrick Barrett 0:c7393ae16c16 17 * Neither the name of Texas Instruments Incorporated nor the names of
Patrick Barrett 0:c7393ae16c16 18 * its contributors may be used to endorse or promote products derived
Patrick Barrett 0:c7393ae16c16 19 * from this software without specific prior written permission.
Patrick Barrett 0:c7393ae16c16 20 *
Patrick Barrett 0:c7393ae16c16 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Patrick Barrett 0:c7393ae16c16 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Patrick Barrett 0:c7393ae16c16 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Patrick Barrett 0:c7393ae16c16 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Patrick Barrett 0:c7393ae16c16 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Patrick Barrett 0:c7393ae16c16 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Patrick Barrett 0:c7393ae16c16 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Patrick Barrett 0:c7393ae16c16 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Patrick Barrett 0:c7393ae16c16 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Patrick Barrett 0:c7393ae16c16 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Patrick Barrett 0:c7393ae16c16 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Barrett 0:c7393ae16c16 32 *
Patrick Barrett 0:c7393ae16c16 33 *****************************************************************************/
Patrick Barrett 0:c7393ae16c16 34
Patrick Barrett 0:c7393ae16c16 35 #include <string.h>
Patrick Barrett 0:c7393ae16c16 36 #include <stdio.h>
Patrick Barrett 9:394abe2083e8 37 #include <time.h>
Patrick Barrett 9:394abe2083e8 38 #include "exosite.h"
Patrick Barrett 0:c7393ae16c16 39
Patrick Barrett 0:c7393ae16c16 40 const char VENDOR[] = "patrick";
Patrick Barrett 0:c7393ae16c16 41 const char MODEL[] = "generic_test";
Patrick Barrett 0:c7393ae16c16 42 const char SERIAL[] = "001";
Patrick Barrett 0:c7393ae16c16 43
Patrick Barrett 0:c7393ae16c16 44 void command_handler (char *value)
Patrick Barrett 0:c7393ae16c16 45 {
Patrick Barrett 0:c7393ae16c16 46 printf("Got Command: %s\n", value);
Patrick Barrett 0:c7393ae16c16 47 }
Patrick Barrett 0:c7393ae16c16 48
Patrick Barrett 0:c7393ae16c16 49 int main(void)
Patrick Barrett 0:c7393ae16c16 50 {
Patrick Barrett 0:c7393ae16c16 51 long long unsigned int loopcount = 0, errorcount = 0;
Patrick Barrett 0:c7393ae16c16 52 char read_str[32];
Patrick Barrett 0:c7393ae16c16 53 char loop_str[16];
Patrick Barrett 0:c7393ae16c16 54 char error_str[16];
Patrick Barrett 0:c7393ae16c16 55 const uint8_t op_count = 3;
Patrick Barrett 0:c7393ae16c16 56 exo_op ops[op_count];
Patrick Barrett 0:c7393ae16c16 57
Patrick Barrett 0:c7393ae16c16 58 exo_init(VENDOR, MODEL, SERIAL);
Patrick Barrett 0:c7393ae16c16 59
Patrick Barrett 0:c7393ae16c16 60 for (int i = 0; i < op_count; i++){
Patrick Barrett 0:c7393ae16c16 61 exo_op_init(&ops[i]);
Patrick Barrett 0:c7393ae16c16 62 }
Patrick Barrett 0:c7393ae16c16 63
Patrick Barrett 0:c7393ae16c16 64 // only need to setup subscribe once
Patrick Barrett 0:c7393ae16c16 65 exo_subscribe(&ops[0], "command", read_str, 32);
Patrick Barrett 0:c7393ae16c16 66
Patrick Barrett 0:c7393ae16c16 67 while(1)
Patrick Barrett 0:c7393ae16c16 68 {
Patrick Barrett 3:1ea8c0eed00c 69 if (loopcount % 100 == 0){
Patrick Barrett 3:1ea8c0eed00c 70 // prepare data to write
Patrick Barrett 3:1ea8c0eed00c 71 snprintf(loop_str, 15, "%llu", loopcount);
Patrick Barrett 0:c7393ae16c16 72
Patrick Barrett 3:1ea8c0eed00c 73 // queue write operation
Patrick Barrett 3:1ea8c0eed00c 74 exo_write(&ops[1], "uptime", loop_str);
Patrick Barrett 3:1ea8c0eed00c 75 }
Patrick Barrett 0:c7393ae16c16 76
Patrick Barrett 0:c7393ae16c16 77 // perform queued operations until all are done or failed
Patrick Barrett 0:c7393ae16c16 78 while(exo_operate(ops, op_count) != EXO_IDLE);
Patrick Barrett 0:c7393ae16c16 79
Patrick Barrett 0:c7393ae16c16 80 // check if ops succeeded or failed
Patrick Barrett 0:c7393ae16c16 81 for (int i = 0; i < op_count; i++){
Patrick Barrett 0:c7393ae16c16 82 if (exo_is_op_finished(&ops[i])) {
Patrick Barrett 0:c7393ae16c16 83 if (exo_is_op_success(&ops[i])) {
Patrick Barrett 0:c7393ae16c16 84 if (exo_is_op_read(&ops[i]) || exo_is_op_subscribe(&ops[i])) {
Patrick Barrett 0:c7393ae16c16 85 printf("[SUCCESS] got '%s' = `%s`\n", ops[i].alias, ops[i].value);
Patrick Barrett 0:c7393ae16c16 86 } else if (exo_is_op_write(&ops[i])) {
Patrick Barrett 0:c7393ae16c16 87 printf("[SUCCESS] set '%s' = `%s`\n", ops[i].alias, ops[i].value);
Patrick Barrett 0:c7393ae16c16 88 } else {
Patrick Barrett 0:c7393ae16c16 89 printf("[WARNING] something succeeded, but I don't know what\n");
Patrick Barrett 0:c7393ae16c16 90 }
Patrick Barrett 0:c7393ae16c16 91 } else {
Patrick Barrett 0:c7393ae16c16 92 printf("[ERROR] on '%s'\n", ops[i].alias);
Patrick Barrett 0:c7393ae16c16 93 printf(" error count is now %llu\n", errorcount);
Patrick Barrett 3:1ea8c0eed00c 94
Patrick Barrett 3:1ea8c0eed00c 95 // queue a write to error count next time
Patrick Barrett 3:1ea8c0eed00c 96 snprintf(error_str, 15, "%llu", errorcount);
Patrick Barrett 3:1ea8c0eed00c 97 exo_write(&ops[3], "errorcount", error_str);
Patrick Barrett 0:c7393ae16c16 98 }
Patrick Barrett 0:c7393ae16c16 99
Patrick Barrett 0:c7393ae16c16 100 exo_op_done(&ops[i]);
Patrick Barrett 0:c7393ae16c16 101 }
Patrick Barrett 0:c7393ae16c16 102 }
Patrick Barrett 0:c7393ae16c16 103
Patrick Barrett 9:394abe2083e8 104 nanosleep((struct timespec[]){{0, 500000000}}, NULL);
Patrick Barrett 3:1ea8c0eed00c 105 loopcount++;
Patrick Barrett 0:c7393ae16c16 106 }
Patrick Barrett 0:c7393ae16c16 107 return 0;
Patrick Barrett 0:c7393ae16c16 108 }