Patrick Barrett / libexositecoap
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers subscribe.c Source File

subscribe.c

00001 /*****************************************************************************
00002 *
00003 *  Copyright (C) 2015 Exosite LLC
00004 *
00005 *  Redistribution and use in source and binary forms, with or without
00006 *  modification, are permitted provided that the following conditions
00007 *  are met:
00008 *
00009 *    Redistributions of source code must retain the above copyright
00010 *    notice, this list of conditions and the following disclaimer.
00011 *
00012 *    Redistributions in binary form must reproduce the above copyright
00013 *    notice, this list of conditions and the following disclaimer in the
00014 *    documentation and/or other materials provided with the
00015 *    distribution.
00016 *
00017 *    Neither the name of Texas Instruments Incorporated nor the names of
00018 *    its contributors may be used to endorse or promote products derived
00019 *    from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 *
00033 *****************************************************************************/
00034 
00035 #include <string.h>
00036 #include <stdio.h>
00037 #include <time.h>
00038 #include "exosite.h"
00039 
00040 const char VENDOR[] = "patrick";
00041 const char MODEL[] =  "generic_test";
00042 const char SERIAL[] = "001";
00043 
00044 void command_handler (char *value)
00045 {
00046     printf("Got Command: %s\n", value);
00047 }
00048 
00049 int main(void)
00050 {
00051     long long unsigned int loopcount = 0, errorcount = 0;
00052     char read_str[32];
00053     char loop_str[16];
00054     char error_str[16];
00055     const uint8_t op_count = 4;
00056     exo_op ops[op_count];
00057 
00058     exo_init(VENDOR, MODEL, SERIAL);
00059 
00060     for (int i = 0; i < op_count; i++){
00061         exo_op_init(&ops[i]);
00062     }
00063 
00064     // only need to setup subscribe once
00065     exo_subscribe(&ops[1], "command", read_str, 32);
00066     
00067     while(1)
00068     {
00069         if (loopcount % 100 == 0){
00070             // prepare data to write
00071             snprintf(loop_str, 15, "%llu", loopcount);
00072 
00073             // queue write operation
00074             exo_write(&ops[2], "uptime", loop_str);
00075         }
00076 
00077         // perform queued operations until all are done or failed
00078         while(exo_operate(ops, op_count) != EXO_IDLE);
00079 
00080         // check if ops succeeded or failed
00081         for (int i = 1; i < op_count; i++){
00082             if (exo_is_op_finished(&ops[i])) {
00083                 if (exo_is_op_success(&ops[i])) {
00084                     if (exo_is_op_read(&ops[i]) || exo_is_op_subscribe(&ops[i])) {
00085                         printf("[SUCCESS] got '%s' = `%s`\n", ops[i].alias, ops[i].value);
00086                     } else if (exo_is_op_write(&ops[i])) {
00087                         printf("[SUCCESS] set '%s' = `%s`\n", ops[i].alias, ops[i].value);
00088                     } else {
00089                         printf("[WARNING] something succeeded, but I don't know what\n");
00090                     }
00091                 } else {
00092                     printf("[ERROR] on '%s'\n", ops[i].alias);
00093                     printf("        error count is now %llu\n", errorcount);
00094 
00095                     // queue a write to error count next time
00096                     snprintf(error_str, 15, "%llu", errorcount);
00097                     exo_write(&ops[3], "errorcount", error_str);
00098                 }
00099 
00100                 exo_op_done(&ops[i]);
00101             }
00102         }
00103 
00104         nanosleep((struct timespec[]){{0, 500000000}}, NULL);
00105         loopcount++;
00106     }
00107     return 0;
00108 }