Fork of KL46Z Wifi version. Moved to KL25Z as umbrella stand now requires 2 SPIs, thus requires 46z.
Dependencies: WIZnet_Library_ASE mbed nanoservice_client_1_12
Fork of Trenton_Doormat_FRDM-KL25Z_ETH by
nsdl_support.cpp@24:ad4d74ed0e4b, 2014-09-09 (annotated)
- Committer:
- erigow01
- Date:
- Tue Sep 09 14:31:24 2014 +0000
- Revision:
- 24:ad4d74ed0e4b
- Parent:
- 23:7304a998959b
Updates - working version for Analyst day.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
erigow01 | 16:3fb612af0dc5 | 1 | // NSDL library support functions |
erigow01 | 16:3fb612af0dc5 | 2 | |
erigow01 | 16:3fb612af0dc5 | 3 | #include "mbed.h" |
erigow01 | 16:3fb612af0dc5 | 4 | #include "nsdl_support.h" |
erigow01 | 16:3fb612af0dc5 | 5 | #include "mbed.h" |
erigow01 | 23:7304a998959b | 6 | #include "WIZnetInterface.h" |
erigow01 | 16:3fb612af0dc5 | 7 | |
erigow01 | 16:3fb612af0dc5 | 8 | extern Endpoint nsp; |
erigow01 | 16:3fb612af0dc5 | 9 | extern UDPSocket server; |
erigow01 | 16:3fb612af0dc5 | 10 | |
erigow01 | 16:3fb612af0dc5 | 11 | |
erigow01 | 16:3fb612af0dc5 | 12 | void *nsdl_alloc(uint16_t size) |
erigow01 | 16:3fb612af0dc5 | 13 | { |
erigow01 | 16:3fb612af0dc5 | 14 | return malloc(size); |
erigow01 | 16:3fb612af0dc5 | 15 | } |
erigow01 | 16:3fb612af0dc5 | 16 | |
erigow01 | 16:3fb612af0dc5 | 17 | void nsdl_free(void* ptr_to_free) |
erigow01 | 16:3fb612af0dc5 | 18 | { |
erigow01 | 16:3fb612af0dc5 | 19 | free(ptr_to_free); |
erigow01 | 16:3fb612af0dc5 | 20 | } |
erigow01 | 16:3fb612af0dc5 | 21 | |
erigow01 | 16:3fb612af0dc5 | 22 | /* |
erigow01 | 16:3fb612af0dc5 | 23 | * Create a static resoure |
erigow01 | 16:3fb612af0dc5 | 24 | * Only get is allowed |
erigow01 | 16:3fb612af0dc5 | 25 | */ |
erigow01 | 16:3fb612af0dc5 | 26 | void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len) |
erigow01 | 16:3fb612af0dc5 | 27 | { |
erigow01 | 16:3fb612af0dc5 | 28 | resource_structure->access = SN_GRS_GET_ALLOWED; |
erigow01 | 16:3fb612af0dc5 | 29 | resource_structure->mode = SN_GRS_STATIC; |
erigow01 | 16:3fb612af0dc5 | 30 | resource_structure->pathlen = pt_len; |
erigow01 | 16:3fb612af0dc5 | 31 | resource_structure->path = pt; |
erigow01 | 16:3fb612af0dc5 | 32 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
erigow01 | 16:3fb612af0dc5 | 33 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
erigow01 | 16:3fb612af0dc5 | 34 | resource_structure->resource = rsc; |
erigow01 | 16:3fb612af0dc5 | 35 | resource_structure->resourcelen = rsc_len; |
erigow01 | 16:3fb612af0dc5 | 36 | sn_nsdl_create_resource(resource_structure); |
erigow01 | 16:3fb612af0dc5 | 37 | } |
erigow01 | 16:3fb612af0dc5 | 38 | |
erigow01 | 16:3fb612af0dc5 | 39 | void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right) |
erigow01 | 16:3fb612af0dc5 | 40 | { |
erigow01 | 16:3fb612af0dc5 | 41 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
erigow01 | 16:3fb612af0dc5 | 42 | resource_structure->resource = 0; |
erigow01 | 16:3fb612af0dc5 | 43 | resource_structure->resourcelen = 0; |
erigow01 | 16:3fb612af0dc5 | 44 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
erigow01 | 16:3fb612af0dc5 | 45 | resource_structure->mode = SN_GRS_DYNAMIC; |
erigow01 | 16:3fb612af0dc5 | 46 | resource_structure->pathlen = pt_len; |
erigow01 | 16:3fb612af0dc5 | 47 | resource_structure->path = pt; |
erigow01 | 16:3fb612af0dc5 | 48 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
erigow01 | 16:3fb612af0dc5 | 49 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
erigow01 | 16:3fb612af0dc5 | 50 | resource_structure->resource_parameters_ptr->observable = is_observable; |
erigow01 | 16:3fb612af0dc5 | 51 | sn_nsdl_create_resource(resource_structure); |
erigow01 | 16:3fb612af0dc5 | 52 | } |
erigow01 | 16:3fb612af0dc5 | 53 | |
erigow01 | 16:3fb612af0dc5 | 54 | sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) |
erigow01 | 16:3fb612af0dc5 | 55 | { |
erigow01 | 16:3fb612af0dc5 | 56 | if (NULL == endpoint_structure) |
erigow01 | 16:3fb612af0dc5 | 57 | { |
erigow01 | 16:3fb612af0dc5 | 58 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
erigow01 | 16:3fb612af0dc5 | 59 | } |
erigow01 | 16:3fb612af0dc5 | 60 | if (endpoint_structure) |
erigow01 | 16:3fb612af0dc5 | 61 | { |
erigow01 | 16:3fb612af0dc5 | 62 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
erigow01 | 16:3fb612af0dc5 | 63 | endpoint_structure->endpoint_name_ptr = name; |
erigow01 | 16:3fb612af0dc5 | 64 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
erigow01 | 16:3fb612af0dc5 | 65 | endpoint_structure->type_ptr = typename_ptr; |
erigow01 | 16:3fb612af0dc5 | 66 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
erigow01 | 16:3fb612af0dc5 | 67 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
erigow01 | 16:3fb612af0dc5 | 68 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
erigow01 | 16:3fb612af0dc5 | 69 | } |
erigow01 | 16:3fb612af0dc5 | 70 | return endpoint_structure; |
erigow01 | 16:3fb612af0dc5 | 71 | } |
erigow01 | 16:3fb612af0dc5 | 72 | |
erigow01 | 16:3fb612af0dc5 | 73 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
erigow01 | 16:3fb612af0dc5 | 74 | { |
erigow01 | 16:3fb612af0dc5 | 75 | if (*endpoint_structure) |
erigow01 | 16:3fb612af0dc5 | 76 | { |
erigow01 | 16:3fb612af0dc5 | 77 | nsdl_free(*endpoint_structure); |
erigow01 | 16:3fb612af0dc5 | 78 | *endpoint_structure = NULL; |
erigow01 | 16:3fb612af0dc5 | 79 | } |
erigow01 | 16:3fb612af0dc5 | 80 | } |
erigow01 | 16:3fb612af0dc5 | 81 | |
erigow01 | 16:3fb612af0dc5 | 82 | static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) |
erigow01 | 16:3fb612af0dc5 | 83 | { |
erigow01 | 16:3fb612af0dc5 | 84 | //UDP |
erigow01 | 16:3fb612af0dc5 | 85 | printf("TX callback!\n\rSending %d bytes\r\n", data_len); |
erigow01 | 16:3fb612af0dc5 | 86 | if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) |
erigow01 | 16:3fb612af0dc5 | 87 | printf("sending failed\n\r"); |
erigow01 | 16:3fb612af0dc5 | 88 | |
erigow01 | 16:3fb612af0dc5 | 89 | return 1; |
erigow01 | 16:3fb612af0dc5 | 90 | |
erigow01 | 16:3fb612af0dc5 | 91 | } |
erigow01 | 16:3fb612af0dc5 | 92 | |
erigow01 | 16:3fb612af0dc5 | 93 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
erigow01 | 16:3fb612af0dc5 | 94 | { |
erigow01 | 16:3fb612af0dc5 | 95 | printf("RX callback!\r\n"); |
erigow01 | 16:3fb612af0dc5 | 96 | return 0; |
erigow01 | 16:3fb612af0dc5 | 97 | } |
erigow01 | 16:3fb612af0dc5 | 98 | |
erigow01 | 16:3fb612af0dc5 | 99 | |
erigow01 | 16:3fb612af0dc5 | 100 | |
erigow01 | 16:3fb612af0dc5 | 101 | void nsdl_init() |
erigow01 | 16:3fb612af0dc5 | 102 | { |
erigow01 | 16:3fb612af0dc5 | 103 | uint8_t nsp_addr[4]; |
erigow01 | 16:3fb612af0dc5 | 104 | sn_nsdl_mem_s memory_cbs; |
erigow01 | 16:3fb612af0dc5 | 105 | |
erigow01 | 16:3fb612af0dc5 | 106 | /* Initialize libNsdl */ |
erigow01 | 16:3fb612af0dc5 | 107 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
erigow01 | 16:3fb612af0dc5 | 108 | memory_cbs.sn_nsdl_free = &nsdl_free; |
erigow01 | 16:3fb612af0dc5 | 109 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) |
erigow01 | 16:3fb612af0dc5 | 110 | printf("libNsdl init failed\r\n"); |
erigow01 | 16:3fb612af0dc5 | 111 | else |
erigow01 | 16:3fb612af0dc5 | 112 | printf("libNsdl init done\r\n"); |
erigow01 | 16:3fb612af0dc5 | 113 | |
erigow01 | 16:3fb612af0dc5 | 114 | /* Set nsp address for library */ |
erigow01 | 16:3fb612af0dc5 | 115 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
erigow01 | 16:3fb612af0dc5 | 116 | } |
erigow01 | 16:3fb612af0dc5 | 117 | |
erigow01 | 16:3fb612af0dc5 | 118 | |
erigow01 | 16:3fb612af0dc5 | 119 |