From c558faf6da6785844a9423ddf6180eba1519d10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erno=20T=C3=B6rmikoski?= Date: Thu, 28 May 2015 11:06:02 +0300 Subject: [PATCH] coap_service stuff added Change-Id: I8a6f66b5c6b6d3ef3db73f8fb5bf4191963be790 --- .../CoAP_Server_unit_test_app.c | 15 +++ Makefile | 61 ++++++++++ coap-service-linux/coap_server_impl.c | 0 coap-service-mbed/coap_server_impl.c | 0 coap-service-nanomesh/coap_server_impl.c | 76 ++++++++++++ coap-service/coap_service_api.h | 88 ++++++++++---- source/coap_server.c | 113 ++++++++++++++++++ source/coap_service.c | 107 +++++++++++++++++ source/include/coap_server.h | 44 +++++++ 9 files changed, 479 insertions(+), 25 deletions(-) create mode 100644 CoAP_Server_unit_test/CoAP_Server_unit_test_app.c create mode 100644 Makefile create mode 100644 coap-service-linux/coap_server_impl.c create mode 100644 coap-service-mbed/coap_server_impl.c create mode 100644 coap-service-nanomesh/coap_server_impl.c create mode 100644 source/coap_server.c create mode 100644 source/coap_service.c create mode 100644 source/include/coap_server.h diff --git a/CoAP_Server_unit_test/CoAP_Server_unit_test_app.c b/CoAP_Server_unit_test/CoAP_Server_unit_test_app.c new file mode 100644 index 0000000000..c64c2d6728 --- /dev/null +++ b/CoAP_Server_unit_test/CoAP_Server_unit_test_app.c @@ -0,0 +1,15 @@ +#include +#include +#include "ns_types.h" +#include "coap_service_api.h" + +int main(void) { + printf("!!!CoAP Server test!!!"); /* prints !!!Hello World!!! */ + + int8_t service_id = 0; + + service_id = coap_service_initialize(0xff, 666, 0); + + return EXIT_SUCCESS; +} + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..3081500f29 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +# +# Makefile for combined CoAP Service library +# + +# Define compiler toolchain with CC or PLATFORM variables +# Example (GCC toolchains, default $CC and $AR are used) +# make +# +# OR (Cross-compile GCC toolchain) +# make PLATFORM=arm-linux-gnueabi- +# +# OR (ArmCC/Keil) +# make CC=ArmCC AR=ArmAR +# +# OR (IAR-ARM) +# make CC=iccarm + +# +# External sources from libService +# +SERVLIB_DIR := ../libService +override CFLAGS += -I$(SERVLIB_DIR)/libService/ +#override CFLAGS += -I$(SERVLIB_DIR)/libService/platform/ + +NSDLC_DIR := ../nsdl-c +override CFLAGS += -I$(NSDLC_DIR)/nsdl-c + +EVENTLOOP_DIR := ../event-loop +override CFLAGS += -I$(EVENTLOOP_DIR)/nanostack-event-loop/ +#override CFLAGS += -I$(EVENTLOOP_DIR)/event-loop/ + +#override CFLAGS += -I../libService/libService/ +#override CFLAGS += -I../nsdl-c/nsdl-c/ +#override CFLAGS += -I../event-loop/nanostack-event-loop/ + +LIB = libcoap-service.a + +SRCS := \ + source/coap_service.c \ + source/coap_server.c \ + coap-service-nanomesh/coap_server_impl.c \ + +override CFLAGS += -DVERSION='"$(VERSION)"' + + + +COAPSERVICE_DIR := ../coap-service +override CFLAGS += -I$(COAPSERVICE_DIR)/coap-service/ +override CFLAGS += -I$(COAPSERVICE_DIR)/source/include/ + +#override CFLAGS += -Isource/coap-service/source/include/ +#override CFLAGS += -Icoap-service-nanomesh/include/ + + +include ../libService/toolchain_rules.mk + +$(eval $(call generate_rules,$(LIB),$(SRCS))) + +.PHONY: release +release: + 7z a coap-service_$(VERSION).zip *.a *.lib include diff --git a/coap-service-linux/coap_server_impl.c b/coap-service-linux/coap_server_impl.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/coap-service-mbed/coap_server_impl.c b/coap-service-mbed/coap_server_impl.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/coap-service-nanomesh/coap_server_impl.c b/coap-service-nanomesh/coap_server_impl.c new file mode 100644 index 0000000000..747edfa9d5 --- /dev/null +++ b/coap-service-nanomesh/coap_server_impl.c @@ -0,0 +1,76 @@ +#include +#include "ns_types.h" +#include "eventOS_event.h" +#include "eventOS_scheduler.h" +#include "eventOS_event_timer.h" +#include "nsdynmemLIB.h" +#include "ns_list.h" +#include "coap_server_impl.h" + +#define COAP_SERVER_SERVICE_TASKLET_INIT 1 +#define COAP_SERVER_SERVICE_TIMER 2 +#define COAP_SERVER_SERVICE_TIMER_ID 1 +#define COAP_TIMER_UPDATE_PERIOD_IN_SECONDS 10 + +static int8_t coap_service_tasklet = -1; + +void coap_server_service_tasklet(arm_event_s * event); + +int8_t coap_server_start(void) +{ + int8_t ret_val = -1; + + ret_val = coap_server_service_tasklet_generated(); + + return ret_val; +} + + +static int8_t coap_server_service_tasklet_generated(void) +{ + if(coap_service_tasklet == -1) + { + //coap_service_tasklet = eventOS_event_handler_create(&coap_server_service_tasklet,COAP_SERVER_SERVICE_TASKLET_INIT); + } + + return coap_service_tasklet; +} + +void coap_server_service_tasklet(arm_event_s * event) +{ + + if(event->event_type == COAP_SERVER_SERVICE_TASKLET_INIT) + { + //We should define periodically timer service!! + //eventOS_event_timer_request(COAP_SERVER_SERVICE_TIMER_ID, COAP_SERVER_SERVICE_TIMER,coap_service_tasklet, (COAP_TIMER_UPDATE_PERIOD_IN_SECONDS *1000)); + } + else if(event->event_type == COAP_SERVER_SERVICE_TIMER) + { + //eventOS_event_timer_request(COAP_SERVER_SERVICE_TIMER_ID, COAP_SERVER_SERVICE_TIMER,coap_service_tasklet, (COAP_TIMER_UPDATE_PERIOD_IN_SECONDS *1000)); + } + +} + +uint16_t socket_open(uint16_t listen_port, coap_service_request_recv_cb *requst_recv_cb) +{ + + return 0; +} + +void *memory_allocation(uint16_t size) +{ + + + if(size) + return malloc(size); + else + return 0; +} + +void memory_free(void* ptr) +{ + + if(ptr) + free(ptr); + +} diff --git a/coap-service/coap_service_api.h b/coap-service/coap_service_api.h index ca00c0a53f..10595b2a34 100755 --- a/coap-service/coap_service_api.h +++ b/coap-service/coap_service_api.h @@ -13,8 +13,8 @@ extern "C" { #include -#include -#include +#include "ns_types.h" +#include "sn_coap_header.h" /** * This interface is used in sending and receiving of CoAP messages to multicast address and receive multiple responses. @@ -50,7 +50,7 @@ extern "C" { * \param listen_port Port that Application wants to use for communicate with coap server. * \param service_options Options of the current service. * - * \return service_id + * \return service_id / -1 for failure */ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options); @@ -75,7 +75,7 @@ void coap_service_delete( int8_t service_id ); * * \return Status */ -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); +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); /** * \brief Security service start callback @@ -85,9 +85,9 @@ typedef int (*coap_service_request_recv_cb)(int8_t service_id, uint8_t source_ad * \param service_id Id number of the current service. * \param EUI64 64 bit global identifier * - * \return Status + * \return 0 for success / -1 for failure */ -typedef int (*coap_service_security_start_cb)(int8_t service_id, uint8_t EUI64[static 8]); +typedef int coap_service_security_start_cb(int8_t service_id, uint8_t EUI64[static 8]); /** * \brief CoAP service security done callback @@ -98,9 +98,9 @@ typedef int (*coap_service_security_start_cb)(int8_t service_id, uint8_t EUI64[s * \param EUI64 64 bit global identifier * \param keyblock Security key (40 bits) * - * \return Status + * \return 0 for success / -1 for failure */ -typedef int (*coap_service_security_done_cb)(int8_t service_id, uint8_t EUI64[static 8], uint8_t keyblock[static 40]); +typedef int coap_service_security_done_cb(int8_t service_id, uint8_t EUI64[static 8], uint8_t keyblock[static 40]); /** @@ -112,7 +112,7 @@ typedef int (*coap_service_security_done_cb)(int8_t service_id, uint8_t EUI64[st * \param PSKd_ptr Pointer to PSK key. * \param PSKd_len Lenght of PSK key. * - * \return Status + * \return 0 for success / -1 for failure */ int coap_service_security_key_set(int8_t service_id, uint8_t EUI64[static 8], uint8_t *PSKd_ptr, uint8_t PSKd_len); @@ -124,9 +124,9 @@ int coap_service_security_key_set(int8_t service_id, uint8_t EUI64[static 8], ui * \param msg_id Id number of the current message. * \param response_ptr Pointer to CoAP header structure. * - * \return Status + * \return 0 for success / -1 for failure */ -typedef int (*coap_service_response_recv)(uint16_t msg_id, sn_coap_hdr_s *response_ptr); +typedef int coap_service_response_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr); /** * \brief Virtual socket sent callback. @@ -139,9 +139,9 @@ typedef int (*coap_service_response_recv)(uint16_t msg_id, sn_coap_hdr_s *respon * \param *data_ptr Pointer to the data. * \param data_len Lenght of the data. * - * \return Status + * \return 0 for success / -1 for failure */ -typedef int (*coap_service_virtual_socket_send_cb)(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len); +typedef int coap_service_virtual_socket_send_cb(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len); /** * \brief Virtual socket read. @@ -154,7 +154,7 @@ typedef int (*coap_service_virtual_socket_send_cb)(int8_t service_id, uint8_t de * \param *data_ptr Pointer to the data * \param data_len Lenght of the data * - * \return ? + * \return 0 for success / -1 for failure */ 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); @@ -166,7 +166,7 @@ int16_t coap_service_virtual_socket_recv(int8_t service_id, uint8_t source_addr_ * \param service_id Id number of the current service. * \param *send_method_ptr Callback to coap virtual socket. * - * \return TODO + * \return 0 for success / -1 for failure */ int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *send_method_ptr); @@ -176,15 +176,29 @@ int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtu * Register application and informs CoAP services unsecure registery callback function. * * \param service_id Id number of the current service. - * \param *uri Pointer to uri. + * \param *uri_ptr Pointer to uri. * \param uri_len Length of uri. * \param port port that Application wants to use for communicate with coap server. * \param allowed_method Informs method that is allowed to use (used defines described above). * \param *request_recv_cb CoAP service request receive callback function pointer. * - * \return TODO + * \return 0 for success / -1 for failure */ -int16_t coap_service_register_uri(int8_t service_id, uint16_t *uri, uint16_t uri_len, uint16_t port, uint8_t allowed_method, coap_service_request_recv_cb *request_recv_cb); +int8_t coap_service_register_uri(int8_t service_id, uint8_t *uri_ptr, uint16_t uri_len, uint8_t allowed_method, coap_service_request_recv_cb *request_recv_cb); + +/** + * \brief Unregister unsecure callback methods to CoAP server + * + * Register application and informs CoAP services unsecure registery callback function. + * + * \param service_id Id number of the current service. + * \param *uri_ptr Pointer to uri. + * \param uri_len Length of uri. + * + * \return 0 for success / -1 for failure + */ +int8_t coap_service_unregister_uri(int8_t service_id, uint8_t *uri_ptr, uint16_t uri_len); + /** * \brief Register secure callback methods to CoAP server. @@ -195,16 +209,16 @@ int16_t coap_service_register_uri(int8_t service_id, uint16_t *uri, uint16_t uri * \param *start_ptr Callback to inform security handling is started. * \param *security_done_cb Callback to inform security handling is done. * - * \return TODO + * \return -1 for failure + * instance_id For success (is used to identify registery) * - * instance id that is used to identify registery. */ -int16_t coap_service_register_uri_secure_cb_set(int8_t service_id, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb); +int8_t coap_service_register_uri_secure_cb_set(int8_t service_id, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb); /** - * \brief Sends CoAP service request + * \brief Sends CoAP service * - * Build and sends CoAP service request message. + * Build and sends CoAP service message. * * \param service_id Id number of the current service. * \param options Options defined above. @@ -215,7 +229,30 @@ int16_t coap_service_register_uri_secure_cb_set(int8_t service_id, coap_service_ * * \return msg_id Id number of the current message. */ -uint16_t coap_service_request_send(int8_t service_id, uint8_t options, uint8_t addr[static 16], uint16_t destination_port, sn_coap_hdr_s *request_ptr, coap_service_response_recv *request_response_cb); +uint16_t coap_service_send(int8_t service_id, uint8_t options, uint8_t addr[static 16], uint16_t destination_port, sn_coap_hdr_s *request_ptr, coap_service_response_recv *request_response_cb); + +/** + * \brief Sends CoAP service request + * + * Build and sends CoAP service request message. + * + * \param service_id Id number of the current service. + * \param options Options defined above. + * \param destination_addr IPv6 address. + * \param destination_port Destination port + * \param msg_type Message type can be found from sn_coap_header. + * \param msg_code Message code can be found from sn_coap_header. + * \param *uri_ptr Pointer to uri. + * \param uri_len Length of uri. + * \param cont_type Content type can be found from sn_coap_header. + * \param payload_ptr Pointer to message content. + * \param payload_len Lenght of the message. + * \param *request_response_cb Callback to inform result of the request. + * + * \return msg_id Id number of the current message. + */ +uint16_t coap_service_request_send(int8_t service_id, uint8_t options, uint8_t destination_addr[static 16], uint16_t destination_port, uint8_t msg_type, uint8_t msg_code, uint8_t *uri_ptr, uint16_t uri_len, + uint8_t cont_type, uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb); /** * \brief Sends CoAP service response @@ -227,7 +264,8 @@ uint16_t coap_service_request_send(int8_t service_id, uint8_t options, uint8_t a * \param options Options defined above. * \param response_ptr Pointer to CoAP header structure. * - * \return TODO + * \return -1 For failure + *- 0 For success */ 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, int32_t content_type,uint8_t * payload_ptr,uint16_t payload_len); diff --git a/source/coap_server.c b/source/coap_server.c new file mode 100644 index 0000000000..9bc469b70e --- /dev/null +++ b/source/coap_server.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2015 ARM. All rights reserved. + */ + +//#include "include/coap_service_api.h" +#include "ns_types.h" +#include "ns_list.h" +#include "coap_server.h" +#include "coap_server_impl.h" + + +typedef NS_LIST_HEAD(coap_service_session_t, link) instance_list_t; +instance_list_t NS_LIST_NAME_INIT(instance_list); + +//typedef struct sn_coap_hdr_s sn_coap_hdr_s; + +/* Coap service class handlers*/ + +//void *memory_allocation(uint16_t size); + +//void memory_free(void* ptr); + +void coap_server_delete_session(int8_t service_id) +{ + + coap_service_session_t *this = coap_service_find_by_service(service_id); + + if(this) + { + + ns_list_remove(&instance_list,this); + memory_free(this); + + } + +} + +static coap_service_session_t *coap_service_search_from_list(int8_t interface_id) +{ + coap_service_session_t *this = NULL; + + /* + ns_list_foreach(coap_service_session_t,cur_ptr, &instance_list) + { + if(cur_ptr->interface_id == interface_id) + { + this = cur_ptr; + break; + } + } + */ + return this; +} + + +static coap_service_session_t *coap_service_find_by_service(int8_t service_id) +{ + coap_service_session_t *this = NULL; + /* + ns_list_foreach(coap_service_session_t,cur_ptr, &instance_list) + { + if(cur_ptr->coap_service_id == service_id) + { + this = cur_ptr; + break; + } + } + */ + return this; +} + + +/* Initialize CoAP server. + * + * TODO + * + * /param interface interface id of this instance. + * + */ +int8_t coap_server_service_init(int8_t interface_id, uint16_t listen_port, uint8_t service_options) +{ + int retVal = -1; + int8_t socketInstance; + + //allocate Socket Service + socketInstance = coap_server_start(); + + if(socketInstance < 0) + retVal = -2; + else + { + + coap_service_session_t *this = memory_allocation(sizeof(coap_service_session_t)); + if(this) + { + this->interface_id = interface_id; +// this->set_response_cb_ptr = NULL; +// ns_list_add_to_start(&instance_list, this); + retVal = 1; + } + return retVal; + + //allocate server + + } + if(socketInstance > 0) + { + //coap_service_delete(socketInstance); + } + return retVal; +} + + diff --git a/source/coap_service.c b/source/coap_service.c new file mode 100644 index 0000000000..6d7c49473a --- /dev/null +++ b/source/coap_service.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2015 ARM. All rights reserved. + */ + +/* + * \file coap_service.c + * \brief Add short description about this file!!! + * + */ + +#include +#include +#include "ns_list.h" +#include "sn_coap_header.h" +#include "coap_service_api.h" +#include "coap_server.h" + +#include + +int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options) +{ + int8_t retVal = -1; + + retVal = coap_server_service_init(interface_id, listen_port, service_options); + + return retVal; +} + +int8_t coap_service_register_uri(int8_t service_id, uint8_t *uri_ptr, uint16_t uri_len, uint8_t allowed_method, coap_service_request_recv_cb *request_recv_cb) +{ + return -1; +} + +int8_t coap_service_unregister_uri(int8_t service_id, uint8_t *uri_ptr, uint16_t uri_len) +{ + return -1; +} + +uint16_t coap_service_send(int8_t service_id, uint8_t options, uint8_t addr[static 16], uint16_t destination_port, sn_coap_hdr_s *request_ptr, coap_service_response_recv *request_response_cb) +{ + return -1; +} + +void coap_service_delete( int8_t service_id ) +{ + +} + +int requst_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr) +{ + return -1; +} + + +int security_start_ptr(int8_t service_id, uint8_t EUI64[static 8]) +{ + return -1; +} + +int security_done_cb(int8_t service_id, uint8_t EUI64[static 8], uint8_t keyblock[static 40]) +{ + return -1; +} + +int coap_service_security_key_set(int8_t service_id, uint8_t EUI64[static 8], uint8_t *PSKd_ptr, uint8_t PSKd_len) +{ + + return -1; +} + +int requst_response_cb(uint16_t msg_id, sn_coap_hdr_s *response_ptr) +{ + + return -1; +} + +int virtual_socket_send(int8_t service_id, uint8_t destination_addr_ptr[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len) +{ + return -1; +} + +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) +{ + return -1; +} + +int16_t coap_service_virtual_socket_set_cb(int8_t service_id, coap_service_virtual_socket_send_cb *virtual_socket_send) +{ + + return -1; +} + +int8_t coap_service_register_uri_secure_cb_set(int8_t service_id, coap_service_security_start_cb *security_start_ptr, coap_service_security_done_cb *security_done_cb) +{ + return -1; +} + +uint16_t coap_service_request_send(int8_t service_id, uint8_t options, uint8_t destination_addr[static 16], uint16_t destination_port, uint8_t msg_type, uint8_t msg_code, uint8_t *uri_ptr, uint16_t uri_len, + uint8_t cont_type, uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb) +{ + return -1; +} + +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, int32_t content_type,uint8_t * payload_ptr,uint16_t payload_len) +{ + return -1; +} diff --git a/source/include/coap_server.h b/source/include/coap_server.h new file mode 100644 index 0000000000..3ee8ab54d7 --- /dev/null +++ b/source/include/coap_server.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 ARM. All rights reserved. + */ + +/* + * \file - + * \brief Add short description about this file!!! + * + */ + +#ifndef COAP_SERVER_H_ +#define COAP_SERVER_H_ + +/* Brief explain. + * + * Some explain + * + * /param param_name explain. + * + * /return TODO + */ + +#include "ns_list.h" + +typedef struct +{ + uint8_t remote_address[16]; +// management_set_response_cb *set_response_cb_ptr; +// management_get_response_cb *get_response_cb_ptr; + int8_t interface_id; + int8_t coap_service_id; + ns_list_link_t link; +} coap_service_session_t; + +void coap_server_delete_session(int8_t service_id); + +int8_t coap_server_service_init(int8_t interface_id, uint16_t listen_port, uint8_t service_options); + +static coap_service_session_t *coap_service_search_from_list(int8_t interface_id); + +static coap_service_session_t *coap_service_find_by_service(int8_t service_id); + + +#endif /* COAP_SERVER_H_ */