HofSpannung / Mbed OS COAP-Client

Fork of coap-example by sandbox

Committer:
Martin1997
Date:
Thu Jun 28 14:11:10 2018 +0000
Revision:
1:0615912bf8ec
Parent:
0:0681e205d0e9
Child:
2:6704302e21c7
funktionierender Client

Who changed what in which revision?

UserRevisionLine numberNew 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
Martin1997 1:0615912bf8ec 25
Martin1997 1:0615912bf8ec 26
Martin1997 1:0615912bf8ec 27 //EDIT Martin Zeitler
Martin1997 1:0615912bf8ec 28 //Definieren der Netzwerkparameter
Martin1997 1:0615912bf8ec 29
Martin1997 1:0615912bf8ec 30 #define TARGET_IP "192.168.0.230"
Martin1997 1:0615912bf8ec 31 //#define TARGET_IP "coap.me"
Martin1997 1:0615912bf8ec 32 #define TARGET_PORT 5683
Martin1997 1:0615912bf8ec 33 /*
Martin1997 1:0615912bf8ec 34 Serial pc(USBTX, USBRX);
Martin1997 1:0615912bf8ec 35 char c;
Martin1997 1:0615912bf8ec 36 bool endless = true;
Martin1997 1:0615912bf8ec 37 */
Martin1997 1:0615912bf8ec 38 //ENIT END
Martin1997 1:0615912bf8ec 39
Martin1997 1:0615912bf8ec 40
Martin1997 1:0615912bf8ec 41
Jan Jongboom 0:0681e205d0e9 42 UDPSocket socket; // Socket to talk CoAP over
Martin1997 1:0615912bf8ec 43
Jan Jongboom 0:0681e205d0e9 44 Thread recvfromThread; // Thread to receive messages over CoAP
Jan Jongboom 0:0681e205d0e9 45
Jan Jongboom 0:0681e205d0e9 46 struct coap_s* coapHandle;
Jan Jongboom 0:0681e205d0e9 47 coap_version_e coapVersion = COAP_VERSION_1;
Jan Jongboom 0:0681e205d0e9 48
Martin1997 1:0615912bf8ec 49
Jan Jongboom 0:0681e205d0e9 50 // CoAP HAL
Jan Jongboom 0:0681e205d0e9 51 void* coap_malloc(uint16_t size) {
Jan Jongboom 0:0681e205d0e9 52 return malloc(size);
Jan Jongboom 0:0681e205d0e9 53 }
Jan Jongboom 0:0681e205d0e9 54
Jan Jongboom 0:0681e205d0e9 55 void coap_free(void* addr) {
Jan Jongboom 0:0681e205d0e9 56 free(addr);
Jan Jongboom 0:0681e205d0e9 57 }
Jan Jongboom 0:0681e205d0e9 58
Jan Jongboom 0:0681e205d0e9 59 // tx_cb and rx_cb are not used in this program
Jan Jongboom 0:0681e205d0e9 60 uint8_t coap_tx_cb(uint8_t *a, uint16_t b, sn_nsdl_addr_s *c, void *d) {
Jan Jongboom 0:0681e205d0e9 61 printf("coap tx cb\n");
Jan Jongboom 0:0681e205d0e9 62 return 0;
Jan Jongboom 0:0681e205d0e9 63 }
Jan Jongboom 0:0681e205d0e9 64
Jan Jongboom 0:0681e205d0e9 65 int8_t coap_rx_cb(sn_coap_hdr_s *a, sn_nsdl_addr_s *b, void *c) {
Jan Jongboom 0:0681e205d0e9 66 printf("coap rx cb\n");
Jan Jongboom 0:0681e205d0e9 67 return 0;
Jan Jongboom 0:0681e205d0e9 68 }
Jan Jongboom 0:0681e205d0e9 69
Martin1997 1:0615912bf8ec 70
Martin1997 1:0615912bf8ec 71
Martin1997 1:0615912bf8ec 72
Martin1997 1:0615912bf8ec 73
Jan Jongboom 0:0681e205d0e9 74 // Main function for the recvfrom thread
Martin1997 1:0615912bf8ec 75
Jan Jongboom 0:0681e205d0e9 76 void recvfromMain() {
Jan Jongboom 0:0681e205d0e9 77 SocketAddress addr;
Jan Jongboom 0:0681e205d0e9 78 uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes
Jan Jongboom 0:0681e205d0e9 79
Jan Jongboom 0:0681e205d0e9 80 nsapi_size_or_error_t ret;
Jan Jongboom 0:0681e205d0e9 81
Jan Jongboom 0:0681e205d0e9 82 while ((ret = socket.recvfrom(&addr, recv_buffer, 1280)) >= 0) {
Jan Jongboom 0:0681e205d0e9 83 // to see where the message came from, inspect addr.get_addr() and addr.get_port()
Jan Jongboom 0:0681e205d0e9 84
Jan Jongboom 0:0681e205d0e9 85 printf("Received a message of length '%d'\n", ret);
Jan Jongboom 0:0681e205d0e9 86
Jan Jongboom 0:0681e205d0e9 87 sn_coap_hdr_s* parsed = sn_coap_parser(coapHandle, ret, recv_buffer, &coapVersion);
Jan Jongboom 0:0681e205d0e9 88
Jan Jongboom 0:0681e205d0e9 89 // We know the payload is going to be a string
Jan Jongboom 0:0681e205d0e9 90 std::string payload((const char*)parsed->payload_ptr, parsed->payload_len);
Jan Jongboom 0:0681e205d0e9 91
Jan Jongboom 0:0681e205d0e9 92 printf("\tmsg_id: %d\n", parsed->msg_id);
Jan Jongboom 0:0681e205d0e9 93 printf("\tmsg_code: %d\n", parsed->msg_code);
Jan Jongboom 0:0681e205d0e9 94 printf("\tcontent_format: %d\n", parsed->content_format);
Jan Jongboom 0:0681e205d0e9 95 printf("\tpayload_len: %d\n", parsed->payload_len);
Jan Jongboom 0:0681e205d0e9 96 printf("\tpayload: %s\n", payload.c_str());
Jan Jongboom 0:0681e205d0e9 97 printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr);
Martin1997 1:0615912bf8ec 98 printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr);
Jan Jongboom 0:0681e205d0e9 99 }
Jan Jongboom 0:0681e205d0e9 100
Jan Jongboom 0:0681e205d0e9 101 free(recv_buffer);
Jan Jongboom 0:0681e205d0e9 102
Jan Jongboom 0:0681e205d0e9 103 printf("UDPSocket::recvfrom failed, error code %d. Shutting down receive thread.\n", ret);
Jan Jongboom 0:0681e205d0e9 104 }
Jan Jongboom 0:0681e205d0e9 105
Martin1997 1:0615912bf8ec 106
Martin1997 1:0615912bf8ec 107
Martin1997 1:0615912bf8ec 108
Jan Jongboom 0:0681e205d0e9 109 int main() {
Martin1997 1:0615912bf8ec 110 //Netzwerkkonfiguration
Jan Jongboom 0:0681e205d0e9 111 NetworkInterface *network = easy_connect(true);
Jan Jongboom 0:0681e205d0e9 112 if (!network) {
Jan Jongboom 0:0681e205d0e9 113 printf("Cannot connect to the network, see serial output");
Jan Jongboom 0:0681e205d0e9 114 return 1;
Jan Jongboom 0:0681e205d0e9 115 }
Jan Jongboom 0:0681e205d0e9 116 printf("Connected to the network. Opening a socket...\n");
Jan Jongboom 0:0681e205d0e9 117 // Open a socket on the network interface
Jan Jongboom 0:0681e205d0e9 118 socket.open(network);
Jan Jongboom 0:0681e205d0e9 119
Martin1997 1:0615912bf8ec 120
Jan Jongboom 0:0681e205d0e9 121 // Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions
Jan Jongboom 0:0681e205d0e9 122 coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb);
Jan Jongboom 0:0681e205d0e9 123
Martin1997 1:0615912bf8ec 124
Jan Jongboom 0:0681e205d0e9 125 // UDPSocket::recvfrom is blocking, so run it in a separate RTOS thread
Jan Jongboom 0:0681e205d0e9 126 recvfromThread.start(&recvfromMain);
Martin1997 1:0615912bf8ec 127
Martin1997 1:0615912bf8ec 128
Martin1997 1:0615912bf8ec 129 /*
Martin1997 1:0615912bf8ec 130 while(endless){
Martin1997 1:0615912bf8ec 131 */
Jan Jongboom 0:0681e205d0e9 132
Jan Jongboom 0:0681e205d0e9 133 // Path to the resource we want to retrieve
Martin1997 1:0615912bf8ec 134 const char* coap_uri_path = "1214";
Jan Jongboom 0:0681e205d0e9 135
Jan Jongboom 0:0681e205d0e9 136 // See ns_coap_header.h
Jan Jongboom 0:0681e205d0e9 137 sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1);
Jan Jongboom 0:0681e205d0e9 138 coap_res_ptr->uri_path_ptr = (uint8_t*)coap_uri_path; // Path
Jan Jongboom 0:0681e205d0e9 139 coap_res_ptr->uri_path_len = strlen(coap_uri_path);
Jan Jongboom 0:0681e205d0e9 140 coap_res_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET; // CoAP method
Jan Jongboom 0:0681e205d0e9 141 coap_res_ptr->content_format = COAP_CT_TEXT_PLAIN; // CoAP content type
Jan Jongboom 0:0681e205d0e9 142 coap_res_ptr->payload_len = 0; // Body length
Jan Jongboom 0:0681e205d0e9 143 coap_res_ptr->payload_ptr = 0; // Body pointer
Jan Jongboom 0:0681e205d0e9 144 coap_res_ptr->options_list_ptr = 0; // Optional: options list
Jan Jongboom 0:0681e205d0e9 145 // Message ID is used to track request->response patterns, because we're using UDP (so everything is unconfirmed).
Jan Jongboom 0:0681e205d0e9 146 // See the receive code to verify that we get the same message ID back
Jan Jongboom 0:0681e205d0e9 147 coap_res_ptr->msg_id = 7;
Jan Jongboom 0:0681e205d0e9 148
Jan Jongboom 0:0681e205d0e9 149 // Calculate the CoAP message size, allocate the memory and build the message
Jan Jongboom 0:0681e205d0e9 150 uint16_t message_len = sn_coap_builder_calc_needed_packet_data_size(coap_res_ptr);
Jan Jongboom 0:0681e205d0e9 151 printf("Calculated message length: %d bytes\n", message_len);
Jan Jongboom 0:0681e205d0e9 152
Jan Jongboom 0:0681e205d0e9 153 uint8_t* message_ptr = (uint8_t*)malloc(message_len);
Jan Jongboom 0:0681e205d0e9 154 sn_coap_builder(message_ptr, coap_res_ptr);
Jan Jongboom 0:0681e205d0e9 155
Jan Jongboom 0:0681e205d0e9 156 // Uncomment to see the raw buffer that will be sent...
Jan Jongboom 0:0681e205d0e9 157 // printf("Message is: ");
Jan Jongboom 0:0681e205d0e9 158 // for (size_t ix = 0; ix < message_len; ix++) {
Jan Jongboom 0:0681e205d0e9 159 // printf("%02x ", message_ptr[ix]);
Jan Jongboom 0:0681e205d0e9 160 // }
Jan Jongboom 0:0681e205d0e9 161 // printf("\n");
Jan Jongboom 0:0681e205d0e9 162
Martin1997 1:0615912bf8ec 163 int scount = socket.sendto(TARGET_IP, TARGET_PORT, message_ptr, message_len);
Martin1997 1:0615912bf8ec 164 printf("Sent %d bytes to %s:%d \n", scount,TARGET_IP, TARGET_PORT);
Jan Jongboom 0:0681e205d0e9 165
Jan Jongboom 0:0681e205d0e9 166 free(coap_res_ptr);
Jan Jongboom 0:0681e205d0e9 167 free(message_ptr);
Martin1997 1:0615912bf8ec 168
Jan Jongboom 0:0681e205d0e9 169 Thread::wait(osWaitForever);
Martin1997 1:0615912bf8ec 170
Martin1997 1:0615912bf8ec 171
Martin1997 1:0615912bf8ec 172
Martin1997 1:0615912bf8ec 173 /*
Martin1997 1:0615912bf8ec 174 printf("Type /'t/' to send again, press /'f/' to stop");
Martin1997 1:0615912bf8ec 175 char c = pc.getc();
Martin1997 1:0615912bf8ec 176
Martin1997 1:0615912bf8ec 177 if(c == 't')
Martin1997 1:0615912bf8ec 178 {
Martin1997 1:0615912bf8ec 179 endless = true;
Martin1997 1:0615912bf8ec 180 }
Martin1997 1:0615912bf8ec 181 if(c == 'f')
Martin1997 1:0615912bf8ec 182 {
Martin1997 1:0615912bf8ec 183 endless = false;
Martin1997 1:0615912bf8ec 184 }
Martin1997 1:0615912bf8ec 185 }
Martin1997 1:0615912bf8ec 186 */
Martin1997 1:0615912bf8ec 187
Martin1997 1:0615912bf8ec 188
Jan Jongboom 0:0681e205d0e9 189 }