mbed 5.4 with sleep mode
Coap/coap_client.h@37:43d48521d4d7, 2017-05-24 (annotated)
- Committer:
- ranaumarnaeem
- Date:
- Wed May 24 07:50:48 2017 +0000
- Revision:
- 37:43d48521d4d7
- Parent:
- 34:d6ce8f961b8b
- Child:
- 39:4f3f7463e55f
coap with mbed-dev 5.4
Who changed what in which revision?
User | Revision | Line number | New 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 | 37:43d48521d4d7 | 48 | //unsigned char ucReturnCode[10]; //Return code array |
ranaumarnaeem | 34:d6ce8f961b8b | 49 | |
ranaumarnaeem | 34:d6ce8f961b8b | 50 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 51 | * @brief Test result enumeration |
ranaumarnaeem | 34:d6ce8f961b8b | 52 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 53 | typedef enum {FAIL = 0, PASS} test_result_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 54 | |
ranaumarnaeem | 34:d6ce8f961b8b | 55 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 56 | * @brief Test data typedef |
ranaumarnaeem | 34:d6ce8f961b8b | 57 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 58 | typedef void *test_data_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 59 | |
ranaumarnaeem | 34:d6ce8f961b8b | 60 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 61 | * @brief Test function typedef |
ranaumarnaeem | 34:d6ce8f961b8b | 62 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 63 | typedef test_result_t (*test_func_t)(test_data_t); |
ranaumarnaeem | 34:d6ce8f961b8b | 64 | |
ranaumarnaeem | 34:d6ce8f961b8b | 65 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 66 | * @brief Message option test data structure |
ranaumarnaeem | 34:d6ce8f961b8b | 67 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 68 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 69 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 70 | unsigned num; /**< Option number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 71 | unsigned len; /**< Option length */ |
ranaumarnaeem | 34:d6ce8f961b8b | 72 | char *val; /**< Pointer to a buffer containing the option value */ |
ranaumarnaeem | 34:d6ce8f961b8b | 73 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 74 | test_coap_client_msg_op_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 75 | |
ranaumarnaeem | 34:d6ce8f961b8b | 76 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 77 | * @brief Client test message data structure |
ranaumarnaeem | 34:d6ce8f961b8b | 78 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 79 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 80 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 81 | coap_msg_type_t type; /**< Message type */ |
ranaumarnaeem | 34:d6ce8f961b8b | 82 | unsigned code_class; /**< Message code class */ |
ranaumarnaeem | 34:d6ce8f961b8b | 83 | unsigned code_detail; /**< Message code detail */ |
ranaumarnaeem | 34:d6ce8f961b8b | 84 | test_coap_client_msg_op_t ops; /**< Array of message option test data structures */ |
ranaumarnaeem | 34:d6ce8f961b8b | 85 | unsigned num_ops; /**< Size of the array of message option test data structures */ |
ranaumarnaeem | 34:d6ce8f961b8b | 86 | char *payload; /**< Buffer containing the payload */ |
ranaumarnaeem | 34:d6ce8f961b8b | 87 | size_t payload_len; /**< Length of the buffer containing the payload */ |
ranaumarnaeem | 34:d6ce8f961b8b | 88 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 89 | test_coap_client_msg_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 90 | |
ranaumarnaeem | 34:d6ce8f961b8b | 91 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 92 | * @brief Client test data structure |
ranaumarnaeem | 34:d6ce8f961b8b | 93 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 94 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 95 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 96 | const char *desc; /**< Test description */ |
ranaumarnaeem | 34:d6ce8f961b8b | 97 | const char *host; /**< Server host address */ |
ranaumarnaeem | 34:d6ce8f961b8b | 98 | const char *port; /**< Server UDP port */ |
ranaumarnaeem | 34:d6ce8f961b8b | 99 | const char *common_name; /**< Common name of the server */ |
ranaumarnaeem | 34:d6ce8f961b8b | 100 | test_coap_client_msg_t test_req; /**< Array of test request message structures */ |
ranaumarnaeem | 34:d6ce8f961b8b | 101 | test_coap_client_msg_t test_resp; /**< Array of test response message structures */ |
ranaumarnaeem | 34:d6ce8f961b8b | 102 | size_t num_msg; /**< Length of the arrays of test message structures */ |
ranaumarnaeem | 34:d6ce8f961b8b | 103 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 104 | test_coap_client_data_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 105 | |
ranaumarnaeem | 34:d6ce8f961b8b | 106 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 107 | * @brief Client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 108 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 109 | typedef struct |
ranaumarnaeem | 34:d6ce8f961b8b | 110 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 111 | int sd; /**< Socket descriptor */ |
ranaumarnaeem | 34:d6ce8f961b8b | 112 | int timer_fd; /**< Timer file descriptor */ |
ranaumarnaeem | 34:d6ce8f961b8b | 113 | struct tm timeout; /**< Timeout value */ |
ranaumarnaeem | 34:d6ce8f961b8b | 114 | unsigned num_retrans; /**< Current number of retransmissions */ |
ranaumarnaeem | 34:d6ce8f961b8b | 115 | int server_sin_len; /**< Socket structure length */ |
ranaumarnaeem | 34:d6ce8f961b8b | 116 | char server_host[COAP_CLIENT_HOST_BUF_LEN]; /**< String to hold the server host address */ |
ranaumarnaeem | 34:d6ce8f961b8b | 117 | char server_port[COAP_CLIENT_PORT_BUF_LEN]; /**< String to hold the server port number */ |
ranaumarnaeem | 34:d6ce8f961b8b | 118 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 119 | coap_client_t; |
ranaumarnaeem | 34:d6ce8f961b8b | 120 | |
ranaumarnaeem | 34:d6ce8f961b8b | 121 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 122 | * @brief Initialise a client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 123 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 124 | * @param[out] client Pointer to a client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 125 | * @param[in] host Pointer to a string containing the host address of the server |
ranaumarnaeem | 34:d6ce8f961b8b | 126 | * @param[in] port Port number of the server |
ranaumarnaeem | 34:d6ce8f961b8b | 127 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 128 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 129 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 130 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 131 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 132 | int coap_client_create(coap_client_t *client, |
ranaumarnaeem | 34:d6ce8f961b8b | 133 | const char *host, |
ranaumarnaeem | 34:d6ce8f961b8b | 134 | const char *port); |
ranaumarnaeem | 34:d6ce8f961b8b | 135 | |
ranaumarnaeem | 34:d6ce8f961b8b | 136 | |
ranaumarnaeem | 34:d6ce8f961b8b | 137 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 138 | * @brief Deinitialise a client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 139 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 140 | * @param[in,out] client Pointer to a client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 141 | */ |
ranaumarnaeem | 34:d6ce8f961b8b | 142 | void coap_client_destroy(coap_client_t *client); |
ranaumarnaeem | 34:d6ce8f961b8b | 143 | |
ranaumarnaeem | 34:d6ce8f961b8b | 144 | /** |
ranaumarnaeem | 34:d6ce8f961b8b | 145 | * @brief Send a request to the server and receive the response |
ranaumarnaeem | 34:d6ce8f961b8b | 146 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 147 | * @param[in,out] client Pointer to a client structure |
ranaumarnaeem | 34:d6ce8f961b8b | 148 | * @param[in] req Pointer to the request message |
ranaumarnaeem | 34:d6ce8f961b8b | 149 | * @param[out] resp Pointer to the response message |
ranaumarnaeem | 34:d6ce8f961b8b | 150 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 151 | * This function sets the message ID and token fields of |
ranaumarnaeem | 34:d6ce8f961b8b | 152 | * the request message overriding any values set by the |
ranaumarnaeem | 34:d6ce8f961b8b | 153 | * calling function. |
ranaumarnaeem | 34:d6ce8f961b8b | 154 | * |
ranaumarnaeem | 34:d6ce8f961b8b | 155 | * @returns Operation status |
ranaumarnaeem | 34:d6ce8f961b8b | 156 | * @retval 0 Success |
ranaumarnaeem | 34:d6ce8f961b8b | 157 | * @retval <0 Error |
ranaumarnaeem | 34:d6ce8f961b8b | 158 | **/ |
ranaumarnaeem | 34:d6ce8f961b8b | 159 | int coap_client_exchange(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp); |
ranaumarnaeem | 34:d6ce8f961b8b | 160 | |
ranaumarnaeem | 34:d6ce8f961b8b | 161 | test_result_t test_exchange_func(char* buf,int buf_len); |
ranaumarnaeem | 34:d6ce8f961b8b | 162 | test_result_t check_resp(test_coap_client_msg_t *test_resp, coap_msg_t *resp); |
ranaumarnaeem | 34:d6ce8f961b8b | 163 | test_result_t compare_ver_token(coap_msg_t *req, coap_msg_t *resp); |
ranaumarnaeem | 34:d6ce8f961b8b | 164 | 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 | 165 | test_result_t populate_req(test_coap_client_msg_t *test_req, coap_msg_t *req); |
ranaumarnaeem | 34:d6ce8f961b8b | 166 | void print_coap_msg(const char *str, coap_msg_t *msg); |
ranaumarnaeem | 34:d6ce8f961b8b | 167 | |
ranaumarnaeem | 34:d6ce8f961b8b | 168 | |
ranaumarnaeem | 34:d6ce8f961b8b | 169 | |
ranaumarnaeem | 34:d6ce8f961b8b | 170 | #endif |
ranaumarnaeem | 34:d6ce8f961b8b | 171 |