Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers coap_service_api.h Source File

coap_service_api.h

00001 /*
00002  * Copyright (c) 2015-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef COAP_SERVICE_API_H_
00019 #define COAP_SERVICE_API_H_
00020 
00021 #ifdef __cplusplus
00022 extern "C" {
00023 #endif
00024 
00025 #include <string.h>
00026 
00027 #include "ns_types.h"
00028 #include "mbed-coap/sn_coap_header.h"
00029 #include "ns_address.h"
00030 
00031 /**
00032  * This interface is used in sending and receiving of CoAP messages to multicast address and receive multiple responses.
00033  */
00034 
00035 // Allowed_methods
00036 #define COAP_SERVICE_ACCESS_ALL_ALLOWED         0x0F
00037 #define COAP_SERVICE_ACCESS_GET_ALLOWED         0x01
00038 #define COAP_SERVICE_ACCESS_PUT_ALLOWED         0x02
00039 #define COAP_SERVICE_ACCESS_POST_ALLOWED        0x04
00040 #define COAP_SERVICE_ACCESS_DELETE_ALLOWED      0x08
00041 
00042 // Bits for service options
00043 #define COAP_SERVICE_OPTIONS_NONE               0x00
00044 #define COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET     0x01
00045 #define COAP_SERVICE_OPTIONS_SECURE             0x02
00046 #define COAP_SERVICE_OPTIONS_EPHEMERAL_PORT     0x04
00047 /** Coap interface selected as socket interface */
00048 #define COAP_SERVICE_OPTIONS_SELECT_SOCKET_IF   0x08
00049 /** Register to COAP multicast groups */
00050 #define COAP_SERVICE_OPTIONS_MULTICAST_JOIN     0x10
00051 /** Link-layer security bypass option is set*/
00052 #define COAP_SERVICE_OPTIONS_SECURE_BYPASS      0x80
00053 
00054 // Bits for request options
00055 #define COAP_REQUEST_OPTIONS_NONE               0x00
00056 #define COAP_REQUEST_OPTIONS_ADDRESS_DEFAULT    0x00//!< default is not setting either short or long.
00057 #define COAP_REQUEST_OPTIONS_ADDRESS_LONG       0x01
00058 #define COAP_REQUEST_OPTIONS_ADDRESS_SHORT      0x02
00059 #define COAP_REQUEST_OPTIONS_MULTICAST          0x04 //!< indicates that CoAP library support multicasting
00060 #define COAP_REQUEST_OPTIONS_SECURE_BYPASS      0x08
00061 
00062 extern const uint8_t COAP_MULTICAST_ADDR_LINK_LOCAL[16]; //!< ff02::fd, COAP link local multicast address
00063 extern const uint8_t COAP_MULTICAST_ADDR_ADMIN_LOCAL[16]; //!< ff03::fd, COAP admin-local multicast address
00064 extern const uint8_t COAP_MULTICAST_ADDR_SITE_LOCAL[16]; //!> ff05::fd, COAP site-local multicast address
00065 
00066 /**
00067  * \brief Service message response receive callback.
00068  *
00069  * Function that handles CoAP service message receiving and parsing
00070  *
00071  * \param service_id       Service handle.
00072  * \param source_address   IPv6 source address.
00073  * \param source_port      Source port.
00074  * \param response_ptr     Pointer to CoAP header structure.
00075  *
00076  * \return 0 for success / -1 for failure
00077   */
00078 typedef int coap_service_response_recv(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr);
00079 
00080 /**
00081  * \brief CoAP service request callback
00082  *
00083  * CoAP service request message receiving and parsing function
00084  *
00085  * \param service_id         Id number of the current service.
00086  * \param source_address     IPv6 source address.
00087  * \param source_port        Source port.
00088  * \param request_ptr        Pointer to CoAP header structure.
00089  *
00090  * \return -1 = Message ignored, no response will be sent. Transaction will be deleted.
00091  *          0 = Response is either already sent or will be send. Transaction is not deleted.
00092  */
00093 typedef int coap_service_request_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr);
00094 
00095 /**
00096  * \brief Security service start callback
00097  *
00098  * Starts security service handling and fetches device password.
00099  *
00100  * \param service_id         Id number of the current service.
00101  * \param address            Address of sender.
00102  * \param port               Port of the device.
00103  * \param pw                 Pointer where to write the ecjpake password.
00104  * \param pw_len             Pointer where to write length of the ecjpake password.
00105  *
00106  * \return 0 for success / -1 for failure
00107  */
00108 typedef int coap_service_security_start_cb(int8_t service_id, uint8_t address[static 16], uint16_t port, uint8_t* pw, uint8_t *pw_len);
00109 
00110 /**
00111  * \brief CoAP service security done callback
00112  *
00113  * CoAP service security done callback function.
00114  *
00115  * \param service_id         Id number of the current service.
00116  * \param address            Address of sender.
00117  * \param keyblock           Security key (40 bits).
00118  *
00119  * \return 0 for success / -1 for failure
00120  */
00121 typedef int coap_service_security_done_cb(int8_t service_id, uint8_t address[static 16], uint8_t keyblock[static 40]);
00122 
00123 /**
00124  * \brief Initialise server instance.
00125  *
00126  * Initialise Thread services for the registered application.
00127  *
00128  * \param interface_id       Informs registered application interface id. This parameter is passed to socket implementation.
00129  * \param listen_port        Port that Application wants to use for communicate with coap server.
00130  * \param service_options    Options of the current service.
00131  * \param *start_ptr         Callback to inform security handling is started and to fetch device password.
00132  * \param *coap_security_done_cb  Callback to inform security handling is done.
00133  *
00134  *  \return service_id / -1 for failure
00135  */
00136 extern int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb);
00137 
00138 /**
00139  * \brief Service delete
00140  *
00141  * Removes all data related to this instance
00142  *
00143  * \param service_id         Id number of the current service.
00144  */
00145 extern void coap_service_delete( int8_t service_id );
00146 
00147 /**
00148  * \brief Close secure connection
00149  *
00150  * Closes secure connection (if present), but leaves socket open.
00151  *
00152  * \param service_id            Id number of the current service.
00153  * \param destimation_addr_ptr  Connection destination address.
00154  * \param port                  Connection destination port.
00155  */
00156 extern void coap_service_close_secure_connection(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port);
00157 
00158 /**
00159  * \brief Virtual socket sent callback.
00160  *
00161  * Sent data to virtual socket.
00162  *
00163  * \param service_id                       Id number of the current service.
00164  * \param destination_addr_ptr             Receiver IPv6 address.
00165  * \param port                             Receiver port number.
00166  * \param *data_ptr                        Pointer to the data.
00167  * \param data_len                         Lenght of the data.
00168  *
00169  * \return 0 for success / -1 for failure
00170   */
00171 typedef int coap_service_virtual_socket_send_cb(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port, const uint8_t *data_ptr, uint16_t data_len);
00172 
00173 /**
00174  * \brief Virtual socket read.
00175  *
00176  * Receive data from virtual socket.
00177  *
00178  * \param service_id                       Id number of the current service.
00179  * \param source_addr_ptr                  Receiver IPv6 address.
00180  * \param port                             Receiver port number.
00181  * \param *data_ptr                        Pointer to the data
00182  * \param data_len                         Lenght of the data
00183  *
00184  * \return 0 for success / -1 for failure
00185   */
00186 extern int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_ptr[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len);
00187 
00188 /**
00189  * \brief Set virtual socket
00190  *
00191  * Sets virtual socket for CoAP services.
00192  *
00193  * \param service_id         Id number of the current service.
00194  * \param *send_method_ptr   Callback to coap virtual socket.
00195  *
00196  * \return 0 for success / -1 for failure
00197  */
00198 extern int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr);
00199 
00200 /**
00201  * \brief Register unsecure callback methods to CoAP server
00202  *
00203  * Register application and informs CoAP services unsecure registery callback function.
00204  *
00205  * \param service_id       Id number of the current service.
00206  * \param *uri             Uri address.
00207  * \param allowed_method   Informs method that is allowed to use (used defines described above).
00208  * \param *request_recv_cb CoAP service request receive callback function pointer.
00209  *
00210  * \return 0 for success / -1 for failure
00211  */
00212 extern int8_t coap_service_register_uri(int8_t service_id, const char *uri, uint8_t allowed_method, coap_service_request_recv_cb *request_recv_cb);
00213 
00214 /**
00215  * \brief Unregister unsecure callback methods to CoAP server
00216  *
00217  * Register application and informs CoAP services unsecure registery callback function.
00218  *
00219  * \param service_id       Id number of the current service.
00220  * \param *uri             Uri address.
00221  *
00222  * \return 0 for success / -1 for failure
00223  */
00224 extern int8_t coap_service_unregister_uri(int8_t service_id, const char *uri);
00225 
00226 /**
00227  * \brief Sends CoAP service request
00228  *
00229  * Build and sends CoAP service request message.
00230  *
00231  * \param service_id            Id number of the current service.
00232  * \param options               Options defined above.
00233  * \param destination_addr      IPv6 address.
00234  * \param destination_port      Destination port
00235  * \param msg_type              Message type can be found from sn_coap_header.
00236  * \param msg_code              Message code can be found from sn_coap_header.
00237  * \param *uri                  Uri address.
00238  * \param cont_type             Content type can be found from sn_coap_header.
00239  * \param payload_ptr           Pointer to message content.
00240  * \param payload_len           Lenght of the message.
00241  * \param *request_response_cb  Callback to inform result of the request.
00242  *
00243  * \return msg_id               Id number of the current message.
00244  */
00245 extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri,
00246         sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb);
00247 
00248 /**
00249  * \brief Sends CoAP service response
00250  *
00251  * Build and sends CoAP service response message.
00252  *
00253  * \param service_id       Id number of the current service.
00254  * \param options          Options defined above.
00255  * \param request_ptr      Pointer to CoAP request message header structure.
00256  * \param message_code     Message code can be found from sn_coap_header.
00257  * \param content_type     Content type can be found from sn_coap_header.
00258  * \param payload_ptr      Pointer to message content.
00259  * \param payload_len      Lenght of the message.
00260  *
00261  * \return -1              For failure
00262  *-         0              For success
00263  */
00264 extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len);
00265 
00266 /**
00267  * \brief Sends CoAP service response
00268  *
00269  * Build and sends CoAP service response message based on CoAP request message id.
00270  *
00271  * \param service_id       Id number of the current service.
00272  * \param options          Options defined above.
00273  * \param msg_id           Request messages ID.
00274  * \param msg_type         Message type can be found from sn_coap_header.
00275  * \param message_code     Message code can be found from sn_coap_header.
00276  * \param content_type     Content type can be found from sn_coap_header.
00277  * \param payload_ptr      Pointer to message content.
00278  * \param payload_len      Lenght of the message.
00279  *
00280  * \return -1              For failure
00281  *-         0              For success
00282  */
00283 extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len);
00284 
00285 
00286 
00287 /**
00288  * \brief Delete CoAP request transaction
00289  *
00290  * Removes pending CoAP transaction from service.
00291  *
00292  * \param service_id       Id number of the current service.
00293  * \param msg_id           Message ID number.
00294  *
00295  * \return -1              For failure
00296  *-         0              For success
00297  */
00298 extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
00299 
00300 /**
00301  * \brief Set DTLS handshake timeout values
00302  *
00303  * Configures the DTLS handshake timeout values.
00304  *
00305  * \param service_id       Id number of the current service.
00306  * \param min              Initial timeout value.
00307  * \param max              Maximum value of timeout.
00308  *
00309  * \return -1              For failure
00310  *-         0              For success
00311  */
00312 extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
00313 
00314 /**
00315  * \brief Set DTLS handshake limit values
00316  *
00317  * Configures the limits for DTLS sessions. Values must be > 0.
00318  *
00319  * \param handshakes_max        Maximum amount of simultaneous handshakes.
00320  * \param connections_max       Maximum amount of sessions.
00321  *
00322  * \return -1              For failure
00323  *-         0              For success
00324  */
00325 extern int8_t coap_service_handshake_limits_set(uint8_t handshakes_max, uint8_t connections_max);
00326 
00327 /**
00328  * \brief Set CoAP duplication message buffer size
00329  *
00330  * Configures the CoAP duplication message buffer size.
00331  *
00332  * \param service_id       Id number of the current service.
00333  * \param size             Buffer size (messages).
00334  *
00335  * \return -1              For failure
00336  *-         0              For success
00337  */
00338 extern int8_t coap_service_set_duplicate_message_buffer(int8_t service_id, uint8_t size);
00339 
00340 /**
00341  * \brief Set DTLS certificates
00342  *
00343  * Set DTLS certificates.
00344  *
00345  * \param service_id       Id number of the current service.
00346  * \param cert             Pointer to certificate chain.
00347  * \param cert_len         Certificate length.
00348  * \param priv_key         pointer to private key.
00349  * \param priv_key_len     length of private key.
00350  *
00351  * \return -1              For failure
00352  *-         0              For success
00353  */
00354 
00355 extern int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert, uint16_t cert_len, const unsigned char *priv_key, uint8_t priv_key_len);
00356 #ifdef __cplusplus
00357 }
00358 #endif
00359 
00360 #endif /* COAP_SERVICE_API_H_ */