mbed-os/UNITTESTS/stubs/NetworkStack_stub.h

146 lines
4.8 KiB
C
Raw Normal View History

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"
#include <list>
2018-08-17 14:37:11 +00:00
class NetworkStackstub : public NetworkStack {
public:
std::list<nsapi_error_t> return_values;
nsapi_error_t return_value;
SocketAddress return_socketAddress;
NetworkStackstub() :
return_value(0),
return_socketAddress()
2018-11-15 08:16:55 +00:00
{
}
2018-08-17 14:37:11 +00:00
virtual const char *get_ip_address()
{
return "127.0.0.1";
}
virtual nsapi_error_t gethostbyname(const char *host,
SocketAddress *address, nsapi_version_t version, const char *interface_name)
2018-08-17 14:37:11 +00:00
{
return return_value;
}
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;
}
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) {
// 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);
} else {
*handle = NULL;
}
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()) {
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)
{
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()) {
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()) {
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()) {
*address = return_socketAddress;
}
2018-10-10 16:24:31 +00:00
if (!return_values.empty()) {
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