mbed 5.4 with sleep mode

Dependencies:  

Committer:
ranaumarnaeem
Date:
Fri Mar 24 05:42:34 2017 +0000
Revision:
34:d6ce8f961b8b
Child:
37:43d48521d4d7
0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ranaumarnaeem 34:d6ce8f961b8b 1 /*
ranaumarnaeem 34:d6ce8f961b8b 2 * Copyright (c) 2015 Keith Cullen.
ranaumarnaeem 34:d6ce8f961b8b 3 * All Rights Reserved.
ranaumarnaeem 34:d6ce8f961b8b 4 *
ranaumarnaeem 34:d6ce8f961b8b 5 * Redistribution and use in source and binary forms, with or without
ranaumarnaeem 34:d6ce8f961b8b 6 * modification, are permitted provided that the following conditions
ranaumarnaeem 34:d6ce8f961b8b 7 * are met:
ranaumarnaeem 34:d6ce8f961b8b 8 * 1. Redistributions of source code must retain the above copyright
ranaumarnaeem 34:d6ce8f961b8b 9 * notice, this list of conditions and the following disclaimer.
ranaumarnaeem 34:d6ce8f961b8b 10 * 2. Redistributions in binary form must reproduce the above copyright
ranaumarnaeem 34:d6ce8f961b8b 11 * notice, this list of conditions and the following disclaimer in the
ranaumarnaeem 34:d6ce8f961b8b 12 * documentation and/or other materials provided with the distribution.
ranaumarnaeem 34:d6ce8f961b8b 13 * 3. The name of the author may not be used to endorse or promote products
ranaumarnaeem 34:d6ce8f961b8b 14 * derived from this software without specific prior written permission.
ranaumarnaeem 34:d6ce8f961b8b 15 *
ranaumarnaeem 34:d6ce8f961b8b 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
ranaumarnaeem 34:d6ce8f961b8b 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
ranaumarnaeem 34:d6ce8f961b8b 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ranaumarnaeem 34:d6ce8f961b8b 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ranaumarnaeem 34:d6ce8f961b8b 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ranaumarnaeem 34:d6ce8f961b8b 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
ranaumarnaeem 34:d6ce8f961b8b 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ranaumarnaeem 34:d6ce8f961b8b 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ranaumarnaeem 34:d6ce8f961b8b 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ranaumarnaeem 34:d6ce8f961b8b 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ranaumarnaeem 34:d6ce8f961b8b 26 */
ranaumarnaeem 34:d6ce8f961b8b 27
ranaumarnaeem 34:d6ce8f961b8b 28 /**
ranaumarnaeem 34:d6ce8f961b8b 29 * @file coap_client.h
ranaumarnaeem 34:d6ce8f961b8b 30 *
ranaumarnaeem 34:d6ce8f961b8b 31 * @brief Include file for the FreeCoAP client library
ranaumarnaeem 34:d6ce8f961b8b 32 */
ranaumarnaeem 34:d6ce8f961b8b 33
ranaumarnaeem 34:d6ce8f961b8b 34 #ifndef COAP_CLIENT_H
ranaumarnaeem 34:d6ce8f961b8b 35 #define COAP_CLIENT_H
ranaumarnaeem 34:d6ce8f961b8b 36
ranaumarnaeem 34:d6ce8f961b8b 37 #include <time.h>
ranaumarnaeem 34:d6ce8f961b8b 38 #include "coap_msg.h"
ranaumarnaeem 34:d6ce8f961b8b 39
ranaumarnaeem 34:d6ce8f961b8b 40 #define EBADMSG 74 /* Not a data message */
ranaumarnaeem 34:d6ce8f961b8b 41 #define ETIMEDOUT 110 /* Connection timed out */
ranaumarnaeem 34:d6ce8f961b8b 42 #define ECONNRESET 104 /* Connection reset by peer */
ranaumarnaeem 34:d6ce8f961b8b 43 #define ENOSPC 28 /* No space left on device */
ranaumarnaeem 34:d6ce8f961b8b 44
ranaumarnaeem 34:d6ce8f961b8b 45 #define COAP_CLIENT_HOST_BUF_LEN 128 /* Buffer length for host addresses */
ranaumarnaeem 34:d6ce8f961b8b 46 #define COAP_CLIENT_PORT_BUF_LEN 8 /* Buffer length for port numbers */
ranaumarnaeem 34:d6ce8f961b8b 47
ranaumarnaeem 34:d6ce8f961b8b 48
ranaumarnaeem 34:d6ce8f961b8b 49 /**
ranaumarnaeem 34:d6ce8f961b8b 50 * @brief Test result enumeration
ranaumarnaeem 34:d6ce8f961b8b 51 */
ranaumarnaeem 34:d6ce8f961b8b 52 typedef enum {FAIL = 0, PASS} test_result_t;
ranaumarnaeem 34:d6ce8f961b8b 53
ranaumarnaeem 34:d6ce8f961b8b 54 /**
ranaumarnaeem 34:d6ce8f961b8b 55 * @brief Test data typedef
ranaumarnaeem 34:d6ce8f961b8b 56 */
ranaumarnaeem 34:d6ce8f961b8b 57 typedef void *test_data_t;
ranaumarnaeem 34:d6ce8f961b8b 58
ranaumarnaeem 34:d6ce8f961b8b 59 /**
ranaumarnaeem 34:d6ce8f961b8b 60 * @brief Test function typedef
ranaumarnaeem 34:d6ce8f961b8b 61 */
ranaumarnaeem 34:d6ce8f961b8b 62 typedef test_result_t (*test_func_t)(test_data_t);
ranaumarnaeem 34:d6ce8f961b8b 63
ranaumarnaeem 34:d6ce8f961b8b 64 /**
ranaumarnaeem 34:d6ce8f961b8b 65 * @brief Message option test data structure
ranaumarnaeem 34:d6ce8f961b8b 66 */
ranaumarnaeem 34:d6ce8f961b8b 67 typedef struct
ranaumarnaeem 34:d6ce8f961b8b 68 {
ranaumarnaeem 34:d6ce8f961b8b 69 unsigned num; /**< Option number */
ranaumarnaeem 34:d6ce8f961b8b 70 unsigned len; /**< Option length */
ranaumarnaeem 34:d6ce8f961b8b 71 char *val; /**< Pointer to a buffer containing the option value */
ranaumarnaeem 34:d6ce8f961b8b 72 }
ranaumarnaeem 34:d6ce8f961b8b 73 test_coap_client_msg_op_t;
ranaumarnaeem 34:d6ce8f961b8b 74
ranaumarnaeem 34:d6ce8f961b8b 75 /**
ranaumarnaeem 34:d6ce8f961b8b 76 * @brief Client test message data structure
ranaumarnaeem 34:d6ce8f961b8b 77 */
ranaumarnaeem 34:d6ce8f961b8b 78 typedef struct
ranaumarnaeem 34:d6ce8f961b8b 79 {
ranaumarnaeem 34:d6ce8f961b8b 80 coap_msg_type_t type; /**< Message type */
ranaumarnaeem 34:d6ce8f961b8b 81 unsigned code_class; /**< Message code class */
ranaumarnaeem 34:d6ce8f961b8b 82 unsigned code_detail; /**< Message code detail */
ranaumarnaeem 34:d6ce8f961b8b 83 test_coap_client_msg_op_t ops; /**< Array of message option test data structures */
ranaumarnaeem 34:d6ce8f961b8b 84 unsigned num_ops; /**< Size of the array of message option test data structures */
ranaumarnaeem 34:d6ce8f961b8b 85 char *payload; /**< Buffer containing the payload */
ranaumarnaeem 34:d6ce8f961b8b 86 size_t payload_len; /**< Length of the buffer containing the payload */
ranaumarnaeem 34:d6ce8f961b8b 87 }
ranaumarnaeem 34:d6ce8f961b8b 88 test_coap_client_msg_t;
ranaumarnaeem 34:d6ce8f961b8b 89
ranaumarnaeem 34:d6ce8f961b8b 90 /**
ranaumarnaeem 34:d6ce8f961b8b 91 * @brief Client test data structure
ranaumarnaeem 34:d6ce8f961b8b 92 */
ranaumarnaeem 34:d6ce8f961b8b 93 typedef struct
ranaumarnaeem 34:d6ce8f961b8b 94 {
ranaumarnaeem 34:d6ce8f961b8b 95 const char *desc; /**< Test description */
ranaumarnaeem 34:d6ce8f961b8b 96 const char *host; /**< Server host address */
ranaumarnaeem 34:d6ce8f961b8b 97 const char *port; /**< Server UDP port */
ranaumarnaeem 34:d6ce8f961b8b 98 const char *common_name; /**< Common name of the server */
ranaumarnaeem 34:d6ce8f961b8b 99 test_coap_client_msg_t test_req; /**< Array of test request message structures */
ranaumarnaeem 34:d6ce8f961b8b 100 test_coap_client_msg_t test_resp; /**< Array of test response message structures */
ranaumarnaeem 34:d6ce8f961b8b 101 size_t num_msg; /**< Length of the arrays of test message structures */
ranaumarnaeem 34:d6ce8f961b8b 102 }
ranaumarnaeem 34:d6ce8f961b8b 103 test_coap_client_data_t;
ranaumarnaeem 34:d6ce8f961b8b 104
ranaumarnaeem 34:d6ce8f961b8b 105 /**
ranaumarnaeem 34:d6ce8f961b8b 106 * @brief Client structure
ranaumarnaeem 34:d6ce8f961b8b 107 */
ranaumarnaeem 34:d6ce8f961b8b 108 typedef struct
ranaumarnaeem 34:d6ce8f961b8b 109 {
ranaumarnaeem 34:d6ce8f961b8b 110 int sd; /**< Socket descriptor */
ranaumarnaeem 34:d6ce8f961b8b 111 int timer_fd; /**< Timer file descriptor */
ranaumarnaeem 34:d6ce8f961b8b 112 struct tm timeout; /**< Timeout value */
ranaumarnaeem 34:d6ce8f961b8b 113 unsigned num_retrans; /**< Current number of retransmissions */
ranaumarnaeem 34:d6ce8f961b8b 114 int server_sin_len; /**< Socket structure length */
ranaumarnaeem 34:d6ce8f961b8b 115 char server_host[COAP_CLIENT_HOST_BUF_LEN]; /**< String to hold the server host address */
ranaumarnaeem 34:d6ce8f961b8b 116 char server_port[COAP_CLIENT_PORT_BUF_LEN]; /**< String to hold the server port number */
ranaumarnaeem 34:d6ce8f961b8b 117 }
ranaumarnaeem 34:d6ce8f961b8b 118 coap_client_t;
ranaumarnaeem 34:d6ce8f961b8b 119
ranaumarnaeem 34:d6ce8f961b8b 120 /**
ranaumarnaeem 34:d6ce8f961b8b 121 * @brief Initialise a client structure
ranaumarnaeem 34:d6ce8f961b8b 122 *
ranaumarnaeem 34:d6ce8f961b8b 123 * @param[out] client Pointer to a client structure
ranaumarnaeem 34:d6ce8f961b8b 124 * @param[in] host Pointer to a string containing the host address of the server
ranaumarnaeem 34:d6ce8f961b8b 125 * @param[in] port Port number of the server
ranaumarnaeem 34:d6ce8f961b8b 126 *
ranaumarnaeem 34:d6ce8f961b8b 127 * @returns Operation status
ranaumarnaeem 34:d6ce8f961b8b 128 * @retval 0 Success
ranaumarnaeem 34:d6ce8f961b8b 129 * @retval <0 Error
ranaumarnaeem 34:d6ce8f961b8b 130 */
ranaumarnaeem 34:d6ce8f961b8b 131 int coap_client_create(coap_client_t *client,
ranaumarnaeem 34:d6ce8f961b8b 132 const char *host,
ranaumarnaeem 34:d6ce8f961b8b 133 const char *port);
ranaumarnaeem 34:d6ce8f961b8b 134
ranaumarnaeem 34:d6ce8f961b8b 135
ranaumarnaeem 34:d6ce8f961b8b 136 /**
ranaumarnaeem 34:d6ce8f961b8b 137 * @brief Deinitialise a client structure
ranaumarnaeem 34:d6ce8f961b8b 138 *
ranaumarnaeem 34:d6ce8f961b8b 139 * @param[in,out] client Pointer to a client structure
ranaumarnaeem 34:d6ce8f961b8b 140 */
ranaumarnaeem 34:d6ce8f961b8b 141 void coap_client_destroy(coap_client_t *client);
ranaumarnaeem 34:d6ce8f961b8b 142
ranaumarnaeem 34:d6ce8f961b8b 143 /**
ranaumarnaeem 34:d6ce8f961b8b 144 * @brief Send a request to the server and receive the response
ranaumarnaeem 34:d6ce8f961b8b 145 *
ranaumarnaeem 34:d6ce8f961b8b 146 * @param[in,out] client Pointer to a client structure
ranaumarnaeem 34:d6ce8f961b8b 147 * @param[in] req Pointer to the request message
ranaumarnaeem 34:d6ce8f961b8b 148 * @param[out] resp Pointer to the response message
ranaumarnaeem 34:d6ce8f961b8b 149 *
ranaumarnaeem 34:d6ce8f961b8b 150 * This function sets the message ID and token fields of
ranaumarnaeem 34:d6ce8f961b8b 151 * the request message overriding any values set by the
ranaumarnaeem 34:d6ce8f961b8b 152 * calling function.
ranaumarnaeem 34:d6ce8f961b8b 153 *
ranaumarnaeem 34:d6ce8f961b8b 154 * @returns Operation status
ranaumarnaeem 34:d6ce8f961b8b 155 * @retval 0 Success
ranaumarnaeem 34:d6ce8f961b8b 156 * @retval <0 Error
ranaumarnaeem 34:d6ce8f961b8b 157 **/
ranaumarnaeem 34:d6ce8f961b8b 158 int coap_client_exchange(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp);
ranaumarnaeem 34:d6ce8f961b8b 159
ranaumarnaeem 34:d6ce8f961b8b 160 test_result_t test_exchange_func(char* buf,int buf_len);
ranaumarnaeem 34:d6ce8f961b8b 161 test_result_t check_resp(test_coap_client_msg_t *test_resp, coap_msg_t *resp);
ranaumarnaeem 34:d6ce8f961b8b 162 test_result_t compare_ver_token(coap_msg_t *req, coap_msg_t *resp);
ranaumarnaeem 34:d6ce8f961b8b 163 test_result_t exchange(coap_client_t *client, test_coap_client_msg_t *test_req, coap_msg_t *req, coap_msg_t *resp);
ranaumarnaeem 34:d6ce8f961b8b 164 test_result_t populate_req(test_coap_client_msg_t *test_req, coap_msg_t *req);
ranaumarnaeem 34:d6ce8f961b8b 165 void print_coap_msg(const char *str, coap_msg_t *msg);
ranaumarnaeem 34:d6ce8f961b8b 166
ranaumarnaeem 34:d6ce8f961b8b 167
ranaumarnaeem 34:d6ce8f961b8b 168
ranaumarnaeem 34:d6ce8f961b8b 169 #endif
ranaumarnaeem 34:d6ce8f961b8b 170