mbed 5.4 with sleep mode

Dependencies:  

Committer:
ranaumarnaeem
Date:
Thu May 25 11:53:45 2017 +0000
Revision:
39:4f3f7463e55f
Parent:
37:43d48521d4d7
mbed 5.4 with sleep mode

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