2018-08-17 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017, Arm Limited and affiliates.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NETWORKSTACKSTUB_H
|
|
|
|
#define NETWORKSTACKSTUB_H
|
|
|
|
|
|
|
|
#include "netsocket/NetworkStack.h"
|
2018-09-12 11:00:20 +00:00
|
|
|
#include <list>
|
2018-08-17 14:37:11 +00:00
|
|
|
|
2019-11-08 07:24:40 +00:00
|
|
|
/*
|
|
|
|
* Note: If you want to:
|
|
|
|
* - control and/or set expectations for the data returned/sent from/to the stack
|
|
|
|
* - set expectations on the calls to NetworkStack
|
|
|
|
* See OnboardNetworkStack_mock.h and its OnboardNetworkStackMock class.
|
|
|
|
*/
|
|
|
|
|
2018-08-17 14:37:11 +00:00
|
|
|
class NetworkStackstub : public NetworkStack {
|
|
|
|
public:
|
2018-09-12 11:00:20 +00:00
|
|
|
std::list<nsapi_error_t> return_values;
|
2018-11-06 11:30:08 +00:00
|
|
|
nsapi_error_t return_value;
|
2018-11-08 16:08:34 +00:00
|
|
|
SocketAddress return_socketAddress;
|
2018-11-06 11:30:08 +00:00
|
|
|
|
2019-02-20 09:50:55 +00:00
|
|
|
NetworkStackstub() :
|
|
|
|
return_value(0),
|
|
|
|
return_socketAddress()
|
2018-11-15 08:16:55 +00:00
|
|
|
{
|
2018-11-06 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:36:00 +00:00
|
|
|
virtual nsapi_error_t get_ip_address(SocketAddress* address)
|
|
|
|
{
|
|
|
|
address->set_ip_address("127.0.0.1");
|
|
|
|
return NSAPI_ERROR_OK;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
virtual nsapi_error_t gethostbyname(const char *host,
|
2019-02-19 14:51:15 +00:00
|
|
|
SocketAddress *address, nsapi_version_t version, const char *interface_name)
|
2018-08-17 14:37:11 +00:00
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
2019-10-08 14:21:52 +00:00
|
|
|
virtual nsapi_value_or_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
2019-02-19 14:51:15 +00:00
|
|
|
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version,
|
|
|
|
const char *interface_name)
|
2018-08-17 14:37:11 +00:00
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
2019-10-08 14:21:52 +00:00
|
|
|
virtual nsapi_value_or_error_t getaddrinfo_async(const char *hostname, SocketAddress *hints, hostbyname_cb_t callback, const char *interface_name)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
virtual nsapi_error_t gethostbyname_async_cancel(int id)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
|
|
|
|
{
|
2018-10-10 16:24:31 +00:00
|
|
|
if (return_value == NSAPI_ERROR_OK && return_values.front() == NSAPI_ERROR_OK) {
|
2018-09-12 11:00:20 +00:00
|
|
|
// Make sure a non-NULL value is returned if error is not expected
|
2018-10-10 16:24:31 +00:00
|
|
|
*handle = reinterpret_cast<nsapi_socket_t *>(1234);
|
2019-02-20 09:50:55 +00:00
|
|
|
} else {
|
|
|
|
*handle = NULL;
|
2018-09-12 11:00:20 +00:00
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_error_t socket_close(nsapi_socket_t handle)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address)
|
|
|
|
{
|
2018-10-10 16:24:31 +00:00
|
|
|
if (!return_values.empty()) {
|
2018-09-12 11:00:20 +00:00
|
|
|
nsapi_error_t ret = return_values.front();
|
|
|
|
return_values.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
|
|
|
|
nsapi_socket_t *handle, SocketAddress *address = 0)
|
|
|
|
{
|
2019-02-20 09:50:55 +00:00
|
|
|
if (return_value == NSAPI_ERROR_OK && return_values.front() == NSAPI_ERROR_OK) {
|
|
|
|
// Make sure a non-NULL value is returned if error is not expected
|
|
|
|
*handle = reinterpret_cast<nsapi_socket_t *>(1234);
|
|
|
|
} else {
|
|
|
|
*handle = NULL;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
|
|
|
|
const void *data, nsapi_size_t size)
|
|
|
|
{
|
2018-10-10 16:24:31 +00:00
|
|
|
if (!return_values.empty()) {
|
2018-09-12 11:00:20 +00:00
|
|
|
nsapi_error_t ret = return_values.front();
|
|
|
|
return_values.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
|
|
|
|
void *data, nsapi_size_t size)
|
|
|
|
{
|
2018-10-10 16:24:31 +00:00
|
|
|
if (!return_values.empty()) {
|
2018-09-12 11:00:20 +00:00
|
|
|
nsapi_error_t ret = return_values.front();
|
|
|
|
return_values.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
|
|
|
|
const void *data, nsapi_size_t size)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
|
|
|
|
void *buffer, nsapi_size_t size)
|
|
|
|
{
|
2018-11-19 13:31:31 +00:00
|
|
|
if (return_socketAddress != SocketAddress()) {
|
2018-11-08 16:08:34 +00:00
|
|
|
*address = return_socketAddress;
|
|
|
|
}
|
2018-10-10 16:24:31 +00:00
|
|
|
if (!return_values.empty()) {
|
2018-09-12 11:00:20 +00:00
|
|
|
nsapi_error_t ret = return_values.front();
|
|
|
|
return_values.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-17 14:37:11 +00:00
|
|
|
return return_value;
|
|
|
|
};
|
|
|
|
virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) {};
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func)
|
|
|
|
{
|
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NETWORKSTACKSTUB_H
|