Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/TESTS/netsocket/socket_sigio/main.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved |
marcozecchini | 0:9fca2b23d0ba | 3 | * SPDX-License-Identifier: Apache-2.0 |
marcozecchini | 0:9fca2b23d0ba | 4 | * |
marcozecchini | 0:9fca2b23d0ba | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
marcozecchini | 0:9fca2b23d0ba | 6 | * not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 7 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 8 | * |
marcozecchini | 0:9fca2b23d0ba | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
marcozecchini | 0:9fca2b23d0ba | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 14 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 15 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 16 | */ |
marcozecchini | 0:9fca2b23d0ba | 17 | |
marcozecchini | 0:9fca2b23d0ba | 18 | #ifndef MBED_CONF_APP_CONNECT_STATEMENT |
marcozecchini | 0:9fca2b23d0ba | 19 | #error [NOT_SUPPORTED] No network configuration found for this target. |
marcozecchini | 0:9fca2b23d0ba | 20 | #endif |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | #include <algorithm> |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "mbed.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | #include MBED_CONF_APP_HEADER_FILE |
marcozecchini | 0:9fca2b23d0ba | 25 | #include "TCPSocket.h" |
marcozecchini | 0:9fca2b23d0ba | 26 | #include "greentea-client/test_env.h" |
marcozecchini | 0:9fca2b23d0ba | 27 | #include "unity/unity.h" |
marcozecchini | 0:9fca2b23d0ba | 28 | #include "utest.h" |
marcozecchini | 0:9fca2b23d0ba | 29 | |
marcozecchini | 0:9fca2b23d0ba | 30 | using namespace utest::v1; |
marcozecchini | 0:9fca2b23d0ba | 31 | |
marcozecchini | 0:9fca2b23d0ba | 32 | |
marcozecchini | 0:9fca2b23d0ba | 33 | namespace { |
marcozecchini | 0:9fca2b23d0ba | 34 | // Test connection information |
marcozecchini | 0:9fca2b23d0ba | 35 | const char *HTTP_SERVER_NAME = "os.mbed.com"; |
marcozecchini | 0:9fca2b23d0ba | 36 | const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt"; |
marcozecchini | 0:9fca2b23d0ba | 37 | const int HTTP_SERVER_PORT = 80; |
marcozecchini | 0:9fca2b23d0ba | 38 | #if defined(TARGET_VK_RZ_A1H) |
marcozecchini | 0:9fca2b23d0ba | 39 | const int RECV_BUFFER_SIZE = 300; |
marcozecchini | 0:9fca2b23d0ba | 40 | #else |
marcozecchini | 0:9fca2b23d0ba | 41 | const int RECV_BUFFER_SIZE = 512; |
marcozecchini | 0:9fca2b23d0ba | 42 | #endif |
marcozecchini | 0:9fca2b23d0ba | 43 | // Test related data |
marcozecchini | 0:9fca2b23d0ba | 44 | const char *HTTP_OK_STR = "200 OK"; |
marcozecchini | 0:9fca2b23d0ba | 45 | const char *HTTP_HELLO_STR = "Hello world!"; |
marcozecchini | 0:9fca2b23d0ba | 46 | |
marcozecchini | 0:9fca2b23d0ba | 47 | // Test buffers |
marcozecchini | 0:9fca2b23d0ba | 48 | char buffer[RECV_BUFFER_SIZE] = {0}; |
marcozecchini | 0:9fca2b23d0ba | 49 | |
marcozecchini | 0:9fca2b23d0ba | 50 | Semaphore recvd; |
marcozecchini | 0:9fca2b23d0ba | 51 | NetworkInterface *net; |
marcozecchini | 0:9fca2b23d0ba | 52 | } |
marcozecchini | 0:9fca2b23d0ba | 53 | |
marcozecchini | 0:9fca2b23d0ba | 54 | void net_bringup() { |
marcozecchini | 0:9fca2b23d0ba | 55 | net = MBED_CONF_APP_OBJECT_CONSTRUCTION; |
marcozecchini | 0:9fca2b23d0ba | 56 | int err = MBED_CONF_APP_CONNECT_STATEMENT; |
marcozecchini | 0:9fca2b23d0ba | 57 | TEST_ASSERT_EQUAL(0, err); |
marcozecchini | 0:9fca2b23d0ba | 58 | } |
marcozecchini | 0:9fca2b23d0ba | 59 | |
marcozecchini | 0:9fca2b23d0ba | 60 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
marcozecchini | 0:9fca2b23d0ba | 61 | const char *f = std::search(first, last, s_first, s_last); |
marcozecchini | 0:9fca2b23d0ba | 62 | return (f != last); |
marcozecchini | 0:9fca2b23d0ba | 63 | } |
marcozecchini | 0:9fca2b23d0ba | 64 | |
marcozecchini | 0:9fca2b23d0ba | 65 | void get_data(TCPSocket* sock){ |
marcozecchini | 0:9fca2b23d0ba | 66 | bool result = false; |
marcozecchini | 0:9fca2b23d0ba | 67 | // Server will respond with HTTP GET's success code |
marcozecchini | 0:9fca2b23d0ba | 68 | const int ret = sock->recv(buffer, sizeof(buffer) - 1); |
marcozecchini | 0:9fca2b23d0ba | 69 | if(ret <= 0) |
marcozecchini | 0:9fca2b23d0ba | 70 | return; |
marcozecchini | 0:9fca2b23d0ba | 71 | |
marcozecchini | 0:9fca2b23d0ba | 72 | buffer[ret] = '\0'; |
marcozecchini | 0:9fca2b23d0ba | 73 | |
marcozecchini | 0:9fca2b23d0ba | 74 | // Find 200 OK HTTP status in reply |
marcozecchini | 0:9fca2b23d0ba | 75 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
marcozecchini | 0:9fca2b23d0ba | 76 | // Find "Hello World!" string in reply |
marcozecchini | 0:9fca2b23d0ba | 77 | bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); |
marcozecchini | 0:9fca2b23d0ba | 78 | |
marcozecchini | 0:9fca2b23d0ba | 79 | TEST_ASSERT_TRUE(found_200_ok); |
marcozecchini | 0:9fca2b23d0ba | 80 | TEST_ASSERT_TRUE(found_hello); |
marcozecchini | 0:9fca2b23d0ba | 81 | |
marcozecchini | 0:9fca2b23d0ba | 82 | if (found_200_ok && found_hello) result = true; |
marcozecchini | 0:9fca2b23d0ba | 83 | |
marcozecchini | 0:9fca2b23d0ba | 84 | TEST_ASSERT_EQUAL(result, true); |
marcozecchini | 0:9fca2b23d0ba | 85 | |
marcozecchini | 0:9fca2b23d0ba | 86 | printf("HTTP: Received %d chars from server\r\n", ret); |
marcozecchini | 0:9fca2b23d0ba | 87 | printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
marcozecchini | 0:9fca2b23d0ba | 88 | printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); |
marcozecchini | 0:9fca2b23d0ba | 89 | printf("HTTP: Received message:\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 90 | printf("%s", buffer); |
marcozecchini | 0:9fca2b23d0ba | 91 | // Signal that we have recvd |
marcozecchini | 0:9fca2b23d0ba | 92 | recvd.release(); |
marcozecchini | 0:9fca2b23d0ba | 93 | } |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | void prep_buffer() { |
marcozecchini | 0:9fca2b23d0ba | 96 | memset(buffer, 0, sizeof(buffer)); |
marcozecchini | 0:9fca2b23d0ba | 97 | // We are constructing GET command like this: |
marcozecchini | 0:9fca2b23d0ba | 98 | // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n |
marcozecchini | 0:9fca2b23d0ba | 99 | strcpy(buffer, "GET http://"); |
marcozecchini | 0:9fca2b23d0ba | 100 | strcat(buffer, HTTP_SERVER_NAME); |
marcozecchini | 0:9fca2b23d0ba | 101 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
marcozecchini | 0:9fca2b23d0ba | 102 | strcat(buffer, " HTTP/1.0\n\n"); |
marcozecchini | 0:9fca2b23d0ba | 103 | } |
marcozecchini | 0:9fca2b23d0ba | 104 | |
marcozecchini | 0:9fca2b23d0ba | 105 | void test_socket_attach() { |
marcozecchini | 0:9fca2b23d0ba | 106 | // Dispatch event queue |
marcozecchini | 0:9fca2b23d0ba | 107 | Thread eventThread; |
marcozecchini | 0:9fca2b23d0ba | 108 | EventQueue queue(4*EVENTS_EVENT_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 109 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
marcozecchini | 0:9fca2b23d0ba | 110 | |
marcozecchini | 0:9fca2b23d0ba | 111 | printf("TCP client IP Address is %s\r\n", net->get_ip_address()); |
marcozecchini | 0:9fca2b23d0ba | 112 | |
marcozecchini | 0:9fca2b23d0ba | 113 | TCPSocket sock(net); |
marcozecchini | 0:9fca2b23d0ba | 114 | printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
marcozecchini | 0:9fca2b23d0ba | 115 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
marcozecchini | 0:9fca2b23d0ba | 116 | printf("HTTP: OK\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 117 | |
marcozecchini | 0:9fca2b23d0ba | 118 | prep_buffer(); |
marcozecchini | 0:9fca2b23d0ba | 119 | // Attach a sigio function that adds function to event queue |
marcozecchini | 0:9fca2b23d0ba | 120 | sock.sigio(queue.event(get_data, &sock)); |
marcozecchini | 0:9fca2b23d0ba | 121 | // Send GET command |
marcozecchini | 0:9fca2b23d0ba | 122 | sock.send(buffer, strlen(buffer)); |
marcozecchini | 0:9fca2b23d0ba | 123 | // wait for recv data |
marcozecchini | 0:9fca2b23d0ba | 124 | recvd.wait(); |
marcozecchini | 0:9fca2b23d0ba | 125 | } else { |
marcozecchini | 0:9fca2b23d0ba | 126 | printf("HTTP: ERROR\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 127 | } |
marcozecchini | 0:9fca2b23d0ba | 128 | sock.close(); |
marcozecchini | 0:9fca2b23d0ba | 129 | } |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | void cb_fail() { |
marcozecchini | 0:9fca2b23d0ba | 132 | TEST_ASSERT(false); |
marcozecchini | 0:9fca2b23d0ba | 133 | } |
marcozecchini | 0:9fca2b23d0ba | 134 | |
marcozecchini | 0:9fca2b23d0ba | 135 | void cb_pass() { |
marcozecchini | 0:9fca2b23d0ba | 136 | recvd.release(); |
marcozecchini | 0:9fca2b23d0ba | 137 | } |
marcozecchini | 0:9fca2b23d0ba | 138 | |
marcozecchini | 0:9fca2b23d0ba | 139 | void test_socket_detach() { |
marcozecchini | 0:9fca2b23d0ba | 140 | // Dispatch event queue |
marcozecchini | 0:9fca2b23d0ba | 141 | Thread eventThread; |
marcozecchini | 0:9fca2b23d0ba | 142 | EventQueue queue(4*EVENTS_EVENT_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 143 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
marcozecchini | 0:9fca2b23d0ba | 144 | |
marcozecchini | 0:9fca2b23d0ba | 145 | printf("TCP client IP Address is %s\r\n", net->get_ip_address()); |
marcozecchini | 0:9fca2b23d0ba | 146 | |
marcozecchini | 0:9fca2b23d0ba | 147 | TCPSocket sock(net); |
marcozecchini | 0:9fca2b23d0ba | 148 | printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
marcozecchini | 0:9fca2b23d0ba | 149 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
marcozecchini | 0:9fca2b23d0ba | 150 | printf("HTTP: OK\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 151 | |
marcozecchini | 0:9fca2b23d0ba | 152 | prep_buffer(); |
marcozecchini | 0:9fca2b23d0ba | 153 | // Attach a sigio function that adds function to event queue |
marcozecchini | 0:9fca2b23d0ba | 154 | sock.sigio(queue.event(cb_fail)); |
marcozecchini | 0:9fca2b23d0ba | 155 | // Detach function |
marcozecchini | 0:9fca2b23d0ba | 156 | sock.sigio(NULL); |
marcozecchini | 0:9fca2b23d0ba | 157 | // Send GET command |
marcozecchini | 0:9fca2b23d0ba | 158 | sock.send(buffer, strlen(buffer)); |
marcozecchini | 0:9fca2b23d0ba | 159 | wait(5); |
marcozecchini | 0:9fca2b23d0ba | 160 | } else { |
marcozecchini | 0:9fca2b23d0ba | 161 | printf("HTTP: ERROR\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 162 | } |
marcozecchini | 0:9fca2b23d0ba | 163 | sock.close(); |
marcozecchini | 0:9fca2b23d0ba | 164 | } |
marcozecchini | 0:9fca2b23d0ba | 165 | |
marcozecchini | 0:9fca2b23d0ba | 166 | void test_socket_reattach() { |
marcozecchini | 0:9fca2b23d0ba | 167 | // Dispatch event queue |
marcozecchini | 0:9fca2b23d0ba | 168 | Thread eventThread; |
marcozecchini | 0:9fca2b23d0ba | 169 | EventQueue queue(4*EVENTS_EVENT_SIZE); |
marcozecchini | 0:9fca2b23d0ba | 170 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
marcozecchini | 0:9fca2b23d0ba | 171 | |
marcozecchini | 0:9fca2b23d0ba | 172 | printf("TCP client IP Address is %s\r\n", net->get_ip_address()); |
marcozecchini | 0:9fca2b23d0ba | 173 | |
marcozecchini | 0:9fca2b23d0ba | 174 | TCPSocket sock(net); |
marcozecchini | 0:9fca2b23d0ba | 175 | printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
marcozecchini | 0:9fca2b23d0ba | 176 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
marcozecchini | 0:9fca2b23d0ba | 177 | printf("HTTP: OK\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 178 | |
marcozecchini | 0:9fca2b23d0ba | 179 | prep_buffer(); |
marcozecchini | 0:9fca2b23d0ba | 180 | // Attach a sigio function that adds function to event queue |
marcozecchini | 0:9fca2b23d0ba | 181 | sock.sigio(queue.event(cb_fail)); |
marcozecchini | 0:9fca2b23d0ba | 182 | // Override previous attach |
marcozecchini | 0:9fca2b23d0ba | 183 | sock.sigio(queue.event(cb_pass)); |
marcozecchini | 0:9fca2b23d0ba | 184 | // Send GET command |
marcozecchini | 0:9fca2b23d0ba | 185 | sock.send(buffer, strlen(buffer)); |
marcozecchini | 0:9fca2b23d0ba | 186 | recvd.wait(); |
marcozecchini | 0:9fca2b23d0ba | 187 | TEST_ASSERT(true); |
marcozecchini | 0:9fca2b23d0ba | 188 | } else { |
marcozecchini | 0:9fca2b23d0ba | 189 | printf("HTTP: ERROR\r\n"); |
marcozecchini | 0:9fca2b23d0ba | 190 | } |
marcozecchini | 0:9fca2b23d0ba | 191 | sock.close(); |
marcozecchini | 0:9fca2b23d0ba | 192 | } |
marcozecchini | 0:9fca2b23d0ba | 193 | |
marcozecchini | 0:9fca2b23d0ba | 194 | |
marcozecchini | 0:9fca2b23d0ba | 195 | // Test setup |
marcozecchini | 0:9fca2b23d0ba | 196 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
marcozecchini | 0:9fca2b23d0ba | 197 | GREENTEA_SETUP(120, "default_auto"); |
marcozecchini | 0:9fca2b23d0ba | 198 | net_bringup(); |
marcozecchini | 0:9fca2b23d0ba | 199 | return verbose_test_setup_handler(number_of_cases); |
marcozecchini | 0:9fca2b23d0ba | 200 | } |
marcozecchini | 0:9fca2b23d0ba | 201 | |
marcozecchini | 0:9fca2b23d0ba | 202 | Case cases[] = { |
marcozecchini | 0:9fca2b23d0ba | 203 | Case("Socket Attach Test", test_socket_attach), |
marcozecchini | 0:9fca2b23d0ba | 204 | Case("Socket Detach Test", test_socket_detach), |
marcozecchini | 0:9fca2b23d0ba | 205 | Case("Socket Reattach Test", test_socket_reattach), |
marcozecchini | 0:9fca2b23d0ba | 206 | }; |
marcozecchini | 0:9fca2b23d0ba | 207 | |
marcozecchini | 0:9fca2b23d0ba | 208 | Specification specification(test_setup, cases); |
marcozecchini | 0:9fca2b23d0ba | 209 | |
marcozecchini | 0:9fca2b23d0ba | 210 | int main() { |
marcozecchini | 0:9fca2b23d0ba | 211 | return !Harness::run(specification); |
marcozecchini | 0:9fca2b23d0ba | 212 | } |