Emiel Estiévenart
/
FRDMSocketsTest
test
main.cpp@0:1a1d87c75d25, 2019-02-27 (annotated)
- Committer:
- Perijah
- Date:
- Wed Feb 27 17:02:00 2019 +0000
- Revision:
- 0:1a1d87c75d25
public test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Perijah | 0:1a1d87c75d25 | 1 | /* mbed Microcontroller Library |
Perijah | 0:1a1d87c75d25 | 2 | * Copyright (c) 2018 ARM Limited |
Perijah | 0:1a1d87c75d25 | 3 | * SPDX-License-Identifier: Apache-2.0 |
Perijah | 0:1a1d87c75d25 | 4 | */ |
Perijah | 0:1a1d87c75d25 | 5 | |
Perijah | 0:1a1d87c75d25 | 6 | #include "mbed.h" |
Perijah | 0:1a1d87c75d25 | 7 | #include "stats_report.h" |
Perijah | 0:1a1d87c75d25 | 8 | #include "EthernetInterface.h" |
Perijah | 0:1a1d87c75d25 | 9 | |
Perijah | 0:1a1d87c75d25 | 10 | DigitalOut led1(LED1); |
Perijah | 0:1a1d87c75d25 | 11 | |
Perijah | 0:1a1d87c75d25 | 12 | #define SLEEP_TIME 500 // (msec) |
Perijah | 0:1a1d87c75d25 | 13 | #define PRINT_AFTER_N_LOOPS 20 |
Perijah | 0:1a1d87c75d25 | 14 | |
Perijah | 0:1a1d87c75d25 | 15 | void runNetwork(); |
Perijah | 0:1a1d87c75d25 | 16 | |
Perijah | 0:1a1d87c75d25 | 17 | int main() |
Perijah | 0:1a1d87c75d25 | 18 | { |
Perijah | 0:1a1d87c75d25 | 19 | runNetwork(); |
Perijah | 0:1a1d87c75d25 | 20 | SystemReport sys_state( SLEEP_TIME * PRINT_AFTER_N_LOOPS /* Loop delay time in ms */); |
Perijah | 0:1a1d87c75d25 | 21 | |
Perijah | 0:1a1d87c75d25 | 22 | int count = 0; |
Perijah | 0:1a1d87c75d25 | 23 | while (true) { |
Perijah | 0:1a1d87c75d25 | 24 | // Blink LED and wait 0.5 seconds |
Perijah | 0:1a1d87c75d25 | 25 | led1 = !led1; |
Perijah | 0:1a1d87c75d25 | 26 | wait_ms(SLEEP_TIME); |
Perijah | 0:1a1d87c75d25 | 27 | |
Perijah | 0:1a1d87c75d25 | 28 | if ((0 == count) || (PRINT_AFTER_N_LOOPS == count)) { |
Perijah | 0:1a1d87c75d25 | 29 | // Following the main thread wait, report on the current system status |
Perijah | 0:1a1d87c75d25 | 30 | //sys_state.report_state(); |
Perijah | 0:1a1d87c75d25 | 31 | count = 0; |
Perijah | 0:1a1d87c75d25 | 32 | } |
Perijah | 0:1a1d87c75d25 | 33 | ++count; |
Perijah | 0:1a1d87c75d25 | 34 | } |
Perijah | 0:1a1d87c75d25 | 35 | } |
Perijah | 0:1a1d87c75d25 | 36 | |
Perijah | 0:1a1d87c75d25 | 37 | void runNetwork() |
Perijah | 0:1a1d87c75d25 | 38 | { |
Perijah | 0:1a1d87c75d25 | 39 | EthernetInterface eth; |
Perijah | 0:1a1d87c75d25 | 40 | TCPSocket sock1; |
Perijah | 0:1a1d87c75d25 | 41 | |
Perijah | 0:1a1d87c75d25 | 42 | printf("Running the network\r\n"); |
Perijah | 0:1a1d87c75d25 | 43 | |
Perijah | 0:1a1d87c75d25 | 44 | |
Perijah | 0:1a1d87c75d25 | 45 | eth.connect(); |
Perijah | 0:1a1d87c75d25 | 46 | |
Perijah | 0:1a1d87c75d25 | 47 | const char *ip = eth.get_ip_address(); |
Perijah | 0:1a1d87c75d25 | 48 | const char *netmask = eth.get_netmask(); |
Perijah | 0:1a1d87c75d25 | 49 | const char *gateway = eth.get_gateway(); |
Perijah | 0:1a1d87c75d25 | 50 | printf("IP address: %s\r\n", ip ? ip : "None"); |
Perijah | 0:1a1d87c75d25 | 51 | printf("Netmask: %s\r\n", netmask ? netmask : "None"); |
Perijah | 0:1a1d87c75d25 | 52 | printf("Gateway: %s\r\n", gateway ? gateway : "None"); |
Perijah | 0:1a1d87c75d25 | 53 | |
Perijah | 0:1a1d87c75d25 | 54 | printf("Opening socket\r\n"); |
Perijah | 0:1a1d87c75d25 | 55 | sock1.open(ð); |
Perijah | 0:1a1d87c75d25 | 56 | printf("binding to port\r\n"); |
Perijah | 0:1a1d87c75d25 | 57 | sock1.set_timeout(1500); |
Perijah | 0:1a1d87c75d25 | 58 | sock1.set_blocking(false); |
Perijah | 0:1a1d87c75d25 | 59 | sock1.bind(4000); |
Perijah | 0:1a1d87c75d25 | 60 | printf("setting non blocking\r\n"); |
Perijah | 0:1a1d87c75d25 | 61 | |
Perijah | 0:1a1d87c75d25 | 62 | printf("listening\r\n"); |
Perijah | 0:1a1d87c75d25 | 63 | sock1.listen(); |
Perijah | 0:1a1d87c75d25 | 64 | printf("done listening\r\n"); |
Perijah | 0:1a1d87c75d25 | 65 | |
Perijah | 0:1a1d87c75d25 | 66 | bool connecting = true; |
Perijah | 0:1a1d87c75d25 | 67 | int result = -1; |
Perijah | 0:1a1d87c75d25 | 68 | TCPSocket* newSock = NULL; |
Perijah | 0:1a1d87c75d25 | 69 | while(connecting) |
Perijah | 0:1a1d87c75d25 | 70 | { |
Perijah | 0:1a1d87c75d25 | 71 | wait_ms(1000); |
Perijah | 0:1a1d87c75d25 | 72 | newSock = sock1.accept(&result); |
Perijah | 0:1a1d87c75d25 | 73 | printf("The error code: %d\r\n", result); |
Perijah | 0:1a1d87c75d25 | 74 | if(newSock) |
Perijah | 0:1a1d87c75d25 | 75 | { |
Perijah | 0:1a1d87c75d25 | 76 | char recvedData[20]; |
Perijah | 0:1a1d87c75d25 | 77 | nsapi_size_t size = 20; |
Perijah | 0:1a1d87c75d25 | 78 | newSock->set_blocking(true); |
Perijah | 0:1a1d87c75d25 | 79 | nsapi_size_t nrOfRcvdBytes = 0; |
Perijah | 0:1a1d87c75d25 | 80 | |
Perijah | 0:1a1d87c75d25 | 81 | |
Perijah | 0:1a1d87c75d25 | 82 | nrOfRcvdBytes = newSock->recv((void *)recvedData, size); |
Perijah | 0:1a1d87c75d25 | 83 | if(nrOfRcvdBytes != 0) |
Perijah | 0:1a1d87c75d25 | 84 | { |
Perijah | 0:1a1d87c75d25 | 85 | printf("this is the data that is received\r\n"); |
Perijah | 0:1a1d87c75d25 | 86 | for(int i = 0; i < nrOfRcvdBytes; i++) |
Perijah | 0:1a1d87c75d25 | 87 | printf("%c", recvedData[i]); |
Perijah | 0:1a1d87c75d25 | 88 | printf("\r\n end of the data\r\n"); |
Perijah | 0:1a1d87c75d25 | 89 | |
Perijah | 0:1a1d87c75d25 | 90 | } |
Perijah | 0:1a1d87c75d25 | 91 | printf("made a new sock\r\n"); |
Perijah | 0:1a1d87c75d25 | 92 | connecting = false; |
Perijah | 0:1a1d87c75d25 | 93 | } |
Perijah | 0:1a1d87c75d25 | 94 | } |
Perijah | 0:1a1d87c75d25 | 95 | |
Perijah | 0:1a1d87c75d25 | 96 | printf("succes!"); |
Perijah | 0:1a1d87c75d25 | 97 | } |
Perijah | 0:1a1d87c75d25 | 98 | |
Perijah | 0:1a1d87c75d25 | 99 | void runUDPNetwork() |
Perijah | 0:1a1d87c75d25 | 100 | { |
Perijah | 0:1a1d87c75d25 | 101 | |
Perijah | 0:1a1d87c75d25 | 102 | } |