![](/media/cache/group/ep_srxDLMd.webp.50x50_q85.jpg)
CoAP Device Server Client mit leshan Server
Dependencies: EthernetInterface mbed-rtos mbed nsdl_lib
Fork of COAPmbed by
Sandbox Server
Eclipse stellt einen Testserver unter http://leshan.eclipse.org/ zur Verfügung. Das Programm verbindet sich mit diesem und ist unter mbed-k64f... erreichbar.
Installation lokale Version leshan
wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-server-demo.jar java -jar ./leshan-server-demo.jar
cURL
Die Funktionen können mittels cURL oder Browser wie folgt getestet werden:
# Alle Clients abfragen (Antwort im JSON Format) curl -X GET http://localhost:8080/api/clients # Wert von LED2 abfragen (Antwort im text/plain Format) curl -X GET http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 # Wert für LED2 setzen curl -X GET -vvv http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 -H "Content-Type: text/plain" -d "10"
main.cpp@18:17bdeb25e0a8, 2015-08-07 (annotated)
- Committer:
- marcel1691
- Date:
- Fri Aug 07 13:11:37 2015 +0000
- Revision:
- 18:17bdeb25e0a8
- Parent:
- 17:8ee737f79492
- Child:
- 19:7830976ac327
Standardmaessig auf Eclipse Server zugesteuert
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcel1691 | 13:3c205b19c540 | 1 | /** CoAP Device Server Client |
stefan1691 | 15:fba25b7e63cd | 2 | Zeile 29: Node Name aendern und auf der Gegenseite leshan Server Verwenden. |
marcel1691 | 13:3c205b19c540 | 3 | */ |
terohoo | 0:2edbfea18d23 | 4 | #include "mbed.h" |
terohoo | 0:2edbfea18d23 | 5 | #include "EthernetInterface.h" |
bogdanm | 2:7e489126fe7a | 6 | #include "nsdl_support.h" |
bogdanm | 2:7e489126fe7a | 7 | #include "dbg.h" |
stefan1691 | 16:b27c9d2b1a98 | 8 | |
bogdanm | 2:7e489126fe7a | 9 | // Include various resources |
stefan1691 | 15:fba25b7e63cd | 10 | #include "PwmOutResource.h" |
stefan1691 | 15:fba25b7e63cd | 11 | |
stefan1691 | 15:fba25b7e63cd | 12 | PwmOutResource led1( D11, "led1", "10/0/0" ); |
stefan1691 | 15:fba25b7e63cd | 13 | PwmOutResource led2( D12, "led2", "10/0/1" ); |
stefan1691 | 15:fba25b7e63cd | 14 | PwmOutResource led3( D13, "led3", "10/0/2" ); |
terohoo | 0:2edbfea18d23 | 15 | |
stefan1691 | 17:8ee737f79492 | 16 | char *staticResources[4] [3] = { { "3/0/0", "Manufacturer", "mc-b" }, |
stefan1691 | 16:b27c9d2b1a98 | 17 | { "3/0/1", "Model Number", "CoAP mbed device" }, |
stefan1691 | 17:8ee737f79492 | 18 | { "5/0/3", "Firmware", "1" }, |
stefan1691 | 17:8ee737f79492 | 19 | { "6/0/0", "GPS Informationen", "67,21" }, |
stefan1691 | 16:b27c9d2b1a98 | 20 | }; |
stefan1691 | 16:b27c9d2b1a98 | 21 | |
bogdanm | 2:7e489126fe7a | 22 | Serial pc(USBTX, USBRX); // tx, rx |
terohoo | 0:2edbfea18d23 | 23 | |
bogdanm | 2:7e489126fe7a | 24 | // **************************************************************************** |
stefan1691 | 16:b27c9d2b1a98 | 25 | // Server configuration |
terohoo | 0:2edbfea18d23 | 26 | /* Change this IP address to that of your NanoService Platform installation */ |
marcel1691 | 18:17bdeb25e0a8 | 27 | static const char* NSP_ADDRESS = "leshan.eclipse.org"; /* Eclipse leshan Server */ |
bogdanm | 2:7e489126fe7a | 28 | static const int NSP_PORT = 5683; |
stefan1691 | 14:54c3708f7c3c | 29 | char endpoint_name[] = "mbed-k64f-1234"; // aendern pro Node |
bogdanm | 2:7e489126fe7a | 30 | uint8_t ep_type[] = {"mbed_device"}; |
bogdanm | 2:7e489126fe7a | 31 | uint8_t lifetime_ptr[] = {"1200"}; |
terohoo | 0:2edbfea18d23 | 32 | |
bogdanm | 2:7e489126fe7a | 33 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 34 | // Ethernet initialization |
terohoo | 0:2edbfea18d23 | 35 | |
terohoo | 0:2edbfea18d23 | 36 | EthernetInterface eth; |
terohoo | 0:2edbfea18d23 | 37 | |
bogdanm | 2:7e489126fe7a | 38 | static void ethernet_init() |
terohoo | 0:2edbfea18d23 | 39 | { |
marcel1691 | 9:18f4959c2bac | 40 | //char mbed_uid[33]; // for creating unique name for the board |
terohoo | 0:2edbfea18d23 | 41 | |
terohoo | 0:2edbfea18d23 | 42 | /* Initialize network */ |
terohoo | 0:2edbfea18d23 | 43 | eth.init(); |
marcel1691 | 9:18f4959c2bac | 44 | |
terohoo | 0:2edbfea18d23 | 45 | if(eth.connect(30000) == 0) |
terohoo | 0:2edbfea18d23 | 46 | pc.printf("Connect OK\n\r"); |
terohoo | 0:2edbfea18d23 | 47 | |
marcel1691 | 9:18f4959c2bac | 48 | //mbed_interface_uid(mbed_uid); // -> Fehlt fuer K64F |
marcel1691 | 9:18f4959c2bac | 49 | //strcpy( mbed_uid, "k64f-1234" ); |
marcel1691 | 9:18f4959c2bac | 50 | //mbed_uid[32] = '\0'; |
marcel1691 | 9:18f4959c2bac | 51 | //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
terohoo | 0:2edbfea18d23 | 52 | |
marcel1691 | 9:18f4959c2bac | 53 | printf("IP:%s", eth.getIPAddress()); |
terohoo | 0:2edbfea18d23 | 54 | |
bogdanm | 2:7e489126fe7a | 55 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
bogdanm | 2:7e489126fe7a | 56 | } |
bogdanm | 2:7e489126fe7a | 57 | |
bogdanm | 2:7e489126fe7a | 58 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 59 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 60 | |
bogdanm | 2:7e489126fe7a | 61 | UDPSocket server; |
bogdanm | 2:7e489126fe7a | 62 | Endpoint nsp; |
bogdanm | 2:7e489126fe7a | 63 | |
bogdanm | 2:7e489126fe7a | 64 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 65 | { |
terohoo | 0:2edbfea18d23 | 66 | server.init(); |
terohoo | 0:2edbfea18d23 | 67 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 68 | |
bogdanm | 2:7e489126fe7a | 69 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 70 | |
bogdanm | 2:7e489126fe7a | 71 | NSDL_DEBUG("name: %s", endpoint_name); |
bogdanm | 2:7e489126fe7a | 72 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 73 | |
marcel1691 | 9:18f4959c2bac | 74 | printf("EP name:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 75 | } |
terohoo | 0:2edbfea18d23 | 76 | |
bogdanm | 2:7e489126fe7a | 77 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 78 | // Resource creation |
terohoo | 0:2edbfea18d23 | 79 | |
bogdanm | 2:7e489126fe7a | 80 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 81 | { |
bogdanm | 2:7e489126fe7a | 82 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 83 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 84 | |
bogdanm | 2:7e489126fe7a | 85 | NSDL_DEBUG("Creating resources"); |
terohoo | 0:2edbfea18d23 | 86 | |
terohoo | 0:2edbfea18d23 | 87 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 88 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 89 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 90 | return 0; |
terohoo | 0:2edbfea18d23 | 91 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 92 | |
bogdanm | 2:7e489126fe7a | 93 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 94 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 95 | { |
bogdanm | 2:7e489126fe7a | 96 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 97 | return 0; |
terohoo | 0:2edbfea18d23 | 98 | } |
terohoo | 0:2edbfea18d23 | 99 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 100 | |
bogdanm | 2:7e489126fe7a | 101 | // Static resources |
stefan1691 | 16:b27c9d2b1a98 | 102 | nsdl_create_static_resource( resource_ptr, strlen(staticResources[0] [0]), (uint8_t*) staticResources[0] [0], 0, 0, |
stefan1691 | 16:b27c9d2b1a98 | 103 | (uint8_t*) staticResources[0] [2], strlen(staticResources[0] [2]) ); |
stefan1691 | 16:b27c9d2b1a98 | 104 | nsdl_create_static_resource( resource_ptr, strlen(staticResources[1] [0]), (uint8_t*) staticResources[1] [0], 0, 0, |
stefan1691 | 16:b27c9d2b1a98 | 105 | (uint8_t*) staticResources[1] [2], strlen(staticResources[1] [2]) ); |
stefan1691 | 17:8ee737f79492 | 106 | |
stefan1691 | 17:8ee737f79492 | 107 | nsdl_create_static_resource( resource_ptr, strlen(staticResources[2] [0]), (uint8_t*) staticResources[2] [0], 0, 0, |
stefan1691 | 17:8ee737f79492 | 108 | (uint8_t*) staticResources[2] [2], strlen(staticResources[2] [2]) ); |
stefan1691 | 17:8ee737f79492 | 109 | nsdl_create_static_resource( resource_ptr, strlen(staticResources[3] [0]), (uint8_t*) staticResources[3] [0], 0, 0, |
stefan1691 | 17:8ee737f79492 | 110 | (uint8_t*) staticResources[3] [2], strlen(staticResources[3] [2]) ); |
terohoo | 0:2edbfea18d23 | 111 | |
bogdanm | 2:7e489126fe7a | 112 | // Dynamic resources |
stefan1691 | 15:fba25b7e63cd | 113 | led1.create( resource_ptr ); |
stefan1691 | 15:fba25b7e63cd | 114 | led2.create( resource_ptr ); |
stefan1691 | 15:fba25b7e63cd | 115 | led3.create( resource_ptr ); |
terohoo | 0:2edbfea18d23 | 116 | |
terohoo | 0:2edbfea18d23 | 117 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 118 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 119 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
terohoo | 0:2edbfea18d23 | 120 | pc.printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 121 | else |
terohoo | 0:2edbfea18d23 | 122 | pc.printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 123 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 124 | |
bogdanm | 2:7e489126fe7a | 125 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 126 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 127 | return 1; |
terohoo | 0:2edbfea18d23 | 128 | } |
terohoo | 0:2edbfea18d23 | 129 | |
bogdanm | 2:7e489126fe7a | 130 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 131 | // Program entry point |
terohoo | 0:2edbfea18d23 | 132 | |
bogdanm | 2:7e489126fe7a | 133 | int main() |
terohoo | 0:2edbfea18d23 | 134 | { |
marcel1691 | 9:18f4959c2bac | 135 | printf("mbed NanoService demo"); |
bogdanm | 2:7e489126fe7a | 136 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
bogdanm | 2:7e489126fe7a | 137 | |
bogdanm | 2:7e489126fe7a | 138 | // Initialize Ethernet interface first |
bogdanm | 2:7e489126fe7a | 139 | ethernet_init(); |
terohoo | 1:e35d7f10999a | 140 | |
bogdanm | 2:7e489126fe7a | 141 | // Initialize NSP node |
bogdanm | 2:7e489126fe7a | 142 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 143 | |
bogdanm | 2:7e489126fe7a | 144 | // Initialize NSDL stack |
bogdanm | 2:7e489126fe7a | 145 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 146 | |
bogdanm | 2:7e489126fe7a | 147 | // Create NSDL resources |
bogdanm | 2:7e489126fe7a | 148 | create_resources(); |
bogdanm | 2:7e489126fe7a | 149 | |
bogdanm | 2:7e489126fe7a | 150 | // Run the NSDL event loop (never returns) |
bogdanm | 2:7e489126fe7a | 151 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 152 | } |