CoAP messages on W5500 used as a NetworkStack. Built for NUCLEO-L476RG that has no built-in networking.
Dependencies: NetworkServices W5500Interface easy-connect
Fork of coap-example by
source/main.cpp@0:0681e205d0e9, 2017-02-15 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Wed Feb 15 21:18:58 2017 +0100
- Revision:
- 0:0681e205d0e9
- Child:
- 1:b56863927244
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
0:0681e205d0e9 | 1 | /* |
Jan Jongboom |
0:0681e205d0e9 | 2 | * PackageLicenseDeclared: Apache-2.0 |
Jan Jongboom |
0:0681e205d0e9 | 3 | * Copyright (c) 2017 ARM Limited |
Jan Jongboom |
0:0681e205d0e9 | 4 | * |
Jan Jongboom |
0:0681e205d0e9 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Jan Jongboom |
0:0681e205d0e9 | 6 | * you may not use this file except in compliance with the License. |
Jan Jongboom |
0:0681e205d0e9 | 7 | * You may obtain a copy of the License at |
Jan Jongboom |
0:0681e205d0e9 | 8 | * |
Jan Jongboom |
0:0681e205d0e9 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jan Jongboom |
0:0681e205d0e9 | 10 | * |
Jan Jongboom |
0:0681e205d0e9 | 11 | * Unless required by applicable law or agreed to in writing, software |
Jan Jongboom |
0:0681e205d0e9 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
Jan Jongboom |
0:0681e205d0e9 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jan Jongboom |
0:0681e205d0e9 | 14 | * See the License for the specific language governing permissions and |
Jan Jongboom |
0:0681e205d0e9 | 15 | * limitations under the License. |
Jan Jongboom |
0:0681e205d0e9 | 16 | */ |
Jan Jongboom |
0:0681e205d0e9 | 17 | |
Jan Jongboom |
0:0681e205d0e9 | 18 | #include <string> |
Jan Jongboom |
0:0681e205d0e9 | 19 | #include "mbed.h" |
Jan Jongboom |
0:0681e205d0e9 | 20 | #include "easy-connect.h" |
Jan Jongboom |
0:0681e205d0e9 | 21 | #include "sn_nsdl.h" |
Jan Jongboom |
0:0681e205d0e9 | 22 | #include "sn_coap_protocol.h" |
Jan Jongboom |
0:0681e205d0e9 | 23 | #include "sn_coap_header.h" |
Jan Jongboom |
0:0681e205d0e9 | 24 | |
Jan Jongboom |
0:0681e205d0e9 | 25 | UDPSocket socket; // Socket to talk CoAP over |
Jan Jongboom |
0:0681e205d0e9 | 26 | Thread recvfromThread; // Thread to receive messages over CoAP |
Jan Jongboom |
0:0681e205d0e9 | 27 | |
Jan Jongboom |
0:0681e205d0e9 | 28 | struct coap_s* coapHandle; |
Jan Jongboom |
0:0681e205d0e9 | 29 | coap_version_e coapVersion = COAP_VERSION_1; |
Jan Jongboom |
0:0681e205d0e9 | 30 | |
Jan Jongboom |
0:0681e205d0e9 | 31 | // CoAP HAL |
Jan Jongboom |
0:0681e205d0e9 | 32 | void* coap_malloc(uint16_t size) { |
Jan Jongboom |
0:0681e205d0e9 | 33 | return malloc(size); |
Jan Jongboom |
0:0681e205d0e9 | 34 | } |
Jan Jongboom |
0:0681e205d0e9 | 35 | |
Jan Jongboom |
0:0681e205d0e9 | 36 | void coap_free(void* addr) { |
Jan Jongboom |
0:0681e205d0e9 | 37 | free(addr); |
Jan Jongboom |
0:0681e205d0e9 | 38 | } |
Jan Jongboom |
0:0681e205d0e9 | 39 | |
Jan Jongboom |
0:0681e205d0e9 | 40 | // tx_cb and rx_cb are not used in this program |
Jan Jongboom |
0:0681e205d0e9 | 41 | uint8_t coap_tx_cb(uint8_t *a, uint16_t b, sn_nsdl_addr_s *c, void *d) { |
Jan Jongboom |
0:0681e205d0e9 | 42 | printf("coap tx cb\n"); |
Jan Jongboom |
0:0681e205d0e9 | 43 | return 0; |
Jan Jongboom |
0:0681e205d0e9 | 44 | } |
Jan Jongboom |
0:0681e205d0e9 | 45 | |
Jan Jongboom |
0:0681e205d0e9 | 46 | int8_t coap_rx_cb(sn_coap_hdr_s *a, sn_nsdl_addr_s *b, void *c) { |
Jan Jongboom |
0:0681e205d0e9 | 47 | printf("coap rx cb\n"); |
Jan Jongboom |
0:0681e205d0e9 | 48 | return 0; |
Jan Jongboom |
0:0681e205d0e9 | 49 | } |
Jan Jongboom |
0:0681e205d0e9 | 50 | |
Jan Jongboom |
0:0681e205d0e9 | 51 | // Main function for the recvfrom thread |
Jan Jongboom |
0:0681e205d0e9 | 52 | void recvfromMain() { |
Jan Jongboom |
0:0681e205d0e9 | 53 | SocketAddress addr; |
Jan Jongboom |
0:0681e205d0e9 | 54 | uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes |
Jan Jongboom |
0:0681e205d0e9 | 55 | |
Jan Jongboom |
0:0681e205d0e9 | 56 | nsapi_size_or_error_t ret; |
Jan Jongboom |
0:0681e205d0e9 | 57 | |
Jan Jongboom |
0:0681e205d0e9 | 58 | while ((ret = socket.recvfrom(&addr, recv_buffer, 1280)) >= 0) { |
Jan Jongboom |
0:0681e205d0e9 | 59 | // to see where the message came from, inspect addr.get_addr() and addr.get_port() |
Jan Jongboom |
0:0681e205d0e9 | 60 | |
Jan Jongboom |
0:0681e205d0e9 | 61 | printf("Received a message of length '%d'\n", ret); |
Jan Jongboom |
0:0681e205d0e9 | 62 | |
Jan Jongboom |
0:0681e205d0e9 | 63 | sn_coap_hdr_s* parsed = sn_coap_parser(coapHandle, ret, recv_buffer, &coapVersion); |
Jan Jongboom |
0:0681e205d0e9 | 64 | |
Jan Jongboom |
0:0681e205d0e9 | 65 | // We know the payload is going to be a string |
Jan Jongboom |
0:0681e205d0e9 | 66 | std::string payload((const char*)parsed->payload_ptr, parsed->payload_len); |
Jan Jongboom |
0:0681e205d0e9 | 67 | |
Jan Jongboom |
0:0681e205d0e9 | 68 | printf("\tmsg_id: %d\n", parsed->msg_id); |
Jan Jongboom |
0:0681e205d0e9 | 69 | printf("\tmsg_code: %d\n", parsed->msg_code); |
Jan Jongboom |
0:0681e205d0e9 | 70 | printf("\tcontent_format: %d\n", parsed->content_format); |
Jan Jongboom |
0:0681e205d0e9 | 71 | printf("\tpayload_len: %d\n", parsed->payload_len); |
Jan Jongboom |
0:0681e205d0e9 | 72 | printf("\tpayload: %s\n", payload.c_str()); |
Jan Jongboom |
0:0681e205d0e9 | 73 | printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr); |
Jan Jongboom |
0:0681e205d0e9 | 74 | } |
Jan Jongboom |
0:0681e205d0e9 | 75 | |
Jan Jongboom |
0:0681e205d0e9 | 76 | free(recv_buffer); |
Jan Jongboom |
0:0681e205d0e9 | 77 | |
Jan Jongboom |
0:0681e205d0e9 | 78 | printf("UDPSocket::recvfrom failed, error code %d. Shutting down receive thread.\n", ret); |
Jan Jongboom |
0:0681e205d0e9 | 79 | } |
Jan Jongboom |
0:0681e205d0e9 | 80 | |
Jan Jongboom |
0:0681e205d0e9 | 81 | int main() { |
Jan Jongboom |
0:0681e205d0e9 | 82 | NetworkInterface *network = easy_connect(true); |
Jan Jongboom |
0:0681e205d0e9 | 83 | if (!network) { |
Jan Jongboom |
0:0681e205d0e9 | 84 | printf("Cannot connect to the network, see serial output"); |
Jan Jongboom |
0:0681e205d0e9 | 85 | return 1; |
Jan Jongboom |
0:0681e205d0e9 | 86 | } |
Jan Jongboom |
0:0681e205d0e9 | 87 | |
Jan Jongboom |
0:0681e205d0e9 | 88 | printf("Connected to the network. Opening a socket...\n"); |
Jan Jongboom |
0:0681e205d0e9 | 89 | |
Jan Jongboom |
0:0681e205d0e9 | 90 | // Open a socket on the network interface |
Jan Jongboom |
0:0681e205d0e9 | 91 | socket.open(network); |
Jan Jongboom |
0:0681e205d0e9 | 92 | |
Jan Jongboom |
0:0681e205d0e9 | 93 | // Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions |
Jan Jongboom |
0:0681e205d0e9 | 94 | coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb); |
Jan Jongboom |
0:0681e205d0e9 | 95 | |
Jan Jongboom |
0:0681e205d0e9 | 96 | // UDPSocket::recvfrom is blocking, so run it in a separate RTOS thread |
Jan Jongboom |
0:0681e205d0e9 | 97 | recvfromThread.start(&recvfromMain); |
Jan Jongboom |
0:0681e205d0e9 | 98 | |
Jan Jongboom |
0:0681e205d0e9 | 99 | // Path to the resource we want to retrieve |
Jan Jongboom |
0:0681e205d0e9 | 100 | const char* coap_uri_path = "/hello"; |
Jan Jongboom |
0:0681e205d0e9 | 101 | |
Jan Jongboom |
0:0681e205d0e9 | 102 | // See ns_coap_header.h |
Jan Jongboom |
0:0681e205d0e9 | 103 | sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1); |
Jan Jongboom |
0:0681e205d0e9 | 104 | coap_res_ptr->uri_path_ptr = (uint8_t*)coap_uri_path; // Path |
Jan Jongboom |
0:0681e205d0e9 | 105 | coap_res_ptr->uri_path_len = strlen(coap_uri_path); |
Jan Jongboom |
0:0681e205d0e9 | 106 | coap_res_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET; // CoAP method |
Jan Jongboom |
0:0681e205d0e9 | 107 | coap_res_ptr->content_format = COAP_CT_TEXT_PLAIN; // CoAP content type |
Jan Jongboom |
0:0681e205d0e9 | 108 | coap_res_ptr->payload_len = 0; // Body length |
Jan Jongboom |
0:0681e205d0e9 | 109 | coap_res_ptr->payload_ptr = 0; // Body pointer |
Jan Jongboom |
0:0681e205d0e9 | 110 | coap_res_ptr->options_list_ptr = 0; // Optional: options list |
Jan Jongboom |
0:0681e205d0e9 | 111 | // Message ID is used to track request->response patterns, because we're using UDP (so everything is unconfirmed). |
Jan Jongboom |
0:0681e205d0e9 | 112 | // See the receive code to verify that we get the same message ID back |
Jan Jongboom |
0:0681e205d0e9 | 113 | coap_res_ptr->msg_id = 7; |
Jan Jongboom |
0:0681e205d0e9 | 114 | |
Jan Jongboom |
0:0681e205d0e9 | 115 | // Calculate the CoAP message size, allocate the memory and build the message |
Jan Jongboom |
0:0681e205d0e9 | 116 | uint16_t message_len = sn_coap_builder_calc_needed_packet_data_size(coap_res_ptr); |
Jan Jongboom |
0:0681e205d0e9 | 117 | printf("Calculated message length: %d bytes\n", message_len); |
Jan Jongboom |
0:0681e205d0e9 | 118 | |
Jan Jongboom |
0:0681e205d0e9 | 119 | uint8_t* message_ptr = (uint8_t*)malloc(message_len); |
Jan Jongboom |
0:0681e205d0e9 | 120 | sn_coap_builder(message_ptr, coap_res_ptr); |
Jan Jongboom |
0:0681e205d0e9 | 121 | |
Jan Jongboom |
0:0681e205d0e9 | 122 | // Uncomment to see the raw buffer that will be sent... |
Jan Jongboom |
0:0681e205d0e9 | 123 | // printf("Message is: "); |
Jan Jongboom |
0:0681e205d0e9 | 124 | // for (size_t ix = 0; ix < message_len; ix++) { |
Jan Jongboom |
0:0681e205d0e9 | 125 | // printf("%02x ", message_ptr[ix]); |
Jan Jongboom |
0:0681e205d0e9 | 126 | // } |
Jan Jongboom |
0:0681e205d0e9 | 127 | // printf("\n"); |
Jan Jongboom |
0:0681e205d0e9 | 128 | |
Jan Jongboom |
0:0681e205d0e9 | 129 | int scount = socket.sendto("coap.me", 5683, message_ptr, message_len); |
Jan Jongboom |
0:0681e205d0e9 | 130 | printf("Sent %d bytes to coap://coap.me:5683\n", scount); |
Jan Jongboom |
0:0681e205d0e9 | 131 | |
Jan Jongboom |
0:0681e205d0e9 | 132 | free(coap_res_ptr); |
Jan Jongboom |
0:0681e205d0e9 | 133 | free(message_ptr); |
Jan Jongboom |
0:0681e205d0e9 | 134 | |
Jan Jongboom |
0:0681e205d0e9 | 135 | Thread::wait(osWaitForever); |
Jan Jongboom |
0:0681e205d0e9 | 136 | } |