Umar Naeem / C027_SupportTest_coapp

Dependencies:   C027_Support mbed-dev

Fork of C027_SupportTest_coap by Umar Naeem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers coap_client.h Source File

coap_client.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2015 Keith Cullen.
00003  * All Rights Reserved.
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  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00022  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 /**
00029  *  @file coap_client.h
00030  *
00031  *  @brief Include file for the FreeCoAP client library
00032  */
00033 
00034 #ifndef COAP_CLIENT_H
00035 #define COAP_CLIENT_H
00036 
00037 #include <time.h>
00038 #include "coap_msg.h"
00039 
00040 #define EBADMSG                     74              /* Not a data message */
00041 #define ETIMEDOUT                   110             /* Connection timed out */
00042 #define ECONNRESET                  104             /* Connection reset by peer */
00043 #define ENOSPC                      28              /* No space left on device */
00044 
00045 #define COAP_CLIENT_HOST_BUF_LEN    128             /* Buffer length for host addresses */
00046 #define COAP_CLIENT_PORT_BUF_LEN    8               /* Buffer length for port numbers */
00047 
00048 /**
00049  *  @brief Test result enumeration
00050  */
00051 typedef enum {FAIL = 0, PASS} test_result_t;
00052 
00053 /**
00054  *  @brief Test data typedef
00055  */
00056 typedef void *test_data_t;
00057 
00058 /**
00059  *  @brief Test function typedef
00060  */
00061 typedef test_result_t (*test_func_t)(test_data_t);
00062 
00063 /**
00064  *  @brief Message option test data structure
00065  */
00066 typedef struct
00067 {
00068     unsigned num;                                                               /**< Option number */
00069     unsigned len;                                                               /**< Option length */
00070     char *val;                                                                  /**< Pointer to a buffer containing the option value */
00071 }
00072 test_coap_client_msg_op_t;
00073 
00074 /**
00075  *  @brief Client test message data structure
00076  */
00077 typedef struct
00078 {
00079     coap_msg_type_t type;                                                       /**< Message type */
00080     unsigned code_class;                                                        /**< Message code class */
00081     unsigned code_detail;                                                       /**< Message code detail */
00082     test_coap_client_msg_op_t ops;                                             /**< Array of message option test data structures */
00083     unsigned num_ops;                                                           /**< Size of the array of message option test data structures */
00084     char *payload;                                                              /**< Buffer containing the payload */
00085     size_t payload_len;                                                         /**< Length of the buffer containing the payload */
00086 }
00087 test_coap_client_msg_t;
00088 
00089 /**
00090  *  @brief Client test data structure
00091  */
00092 typedef struct
00093 {
00094     const char *desc;                                                           /**< Test description */
00095     const char *host;                                                           /**< Server host address */
00096     const char *port;                                                           /**< Server UDP port */
00097     const char *common_name;                                                    /**< Common name of the server */
00098     test_coap_client_msg_t test_req;                                           /**< Array of test request message structures */
00099     test_coap_client_msg_t test_resp;                                          /**< Array of test response message structures */
00100     size_t num_msg;                                                             /**< Length of the arrays of test message structures */
00101 }
00102 test_coap_client_data_t;
00103 
00104 /**
00105  *  @brief Client structure
00106  */
00107 typedef struct
00108 {
00109     int sd;                                                                     /**< Socket descriptor */
00110     int timer_fd;                                                               /**< Timer file descriptor */
00111     struct tm timeout;                                                          /**< Timeout value */
00112     unsigned num_retrans;                                                       /**< Current number of retransmissions */
00113     int server_sin_len;                                                         /**< Socket structure length */
00114     char server_host[COAP_CLIENT_HOST_BUF_LEN];                                 /**< String to hold the server host address */
00115     char server_port[COAP_CLIENT_PORT_BUF_LEN];                                 /**< String to hold the server port number */
00116 }
00117 coap_client_t;
00118 
00119 /**
00120  *  @brief Initialise a client structure
00121  *
00122  *  @param[out] client Pointer to a client structure
00123  *  @param[in] host Pointer to a string containing the host address of the server
00124  *  @param[in] port Port number of the server
00125  *
00126  *  @returns Operation status
00127  *  @retval 0 Success
00128  *  @retval <0 Error
00129  */
00130 int coap_client_create(coap_client_t *client,
00131                        const char *host,
00132                        const char *port);
00133 
00134 
00135 /**
00136  *  @brief Deinitialise a client structure
00137  *
00138  *  @param[in,out] client Pointer to a client structure
00139  */
00140 void coap_client_destroy(coap_client_t *client);
00141 
00142 /**
00143  *  @brief Send a request to the server and receive the response
00144  *
00145  *  @param[in,out] client Pointer to a client structure
00146  *  @param[in] req Pointer to the request message
00147  *  @param[out] resp Pointer to the response message
00148  *
00149  *  This function sets the message ID and token fields of
00150  *  the request message overriding any values set by the
00151  *  calling function.
00152  *
00153  *  @returns Operation status
00154  *  @retval 0 Success
00155  *  @retval <0 Error
00156  **/
00157 int coap_client_exchange(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp);
00158 
00159 test_result_t test_exchange_func(char* buf,int buf_len,char* returncode);
00160 test_result_t check_resp(test_coap_client_msg_t *test_resp, coap_msg_t *resp);
00161 test_result_t compare_ver_token(coap_msg_t *req, coap_msg_t *resp);
00162 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);
00163 test_result_t populate_req(test_coap_client_msg_t *test_req, coap_msg_t *req);
00164 void print_coap_msg(const char *str, coap_msg_t *msg,char* returncode);
00165 
00166 
00167 
00168 #endif
00169