openthread/src/core/coap/coap_server.cpp

147 lines
4.3 KiB
C++

/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file implements the CoAP server message dispatch.
*/
#include <coap/coap_server.hpp>
#include <common/code_utils.hpp>
namespace Thread {
namespace Coap {
Server::Server(Ip6::Udp &aUdp, uint16_t aPort):
mSocket(aUdp)
{
mPort = aPort;
mResources = NULL;
}
ThreadError Server::Start()
{
ThreadError error;
Ip6::SockAddr sockaddr;
sockaddr.mPort = mPort;
SuccessOrExit(error = mSocket.Open(&HandleUdpReceive, this));
SuccessOrExit(error = mSocket.Bind(sockaddr));
exit:
return error;
}
ThreadError Server::Stop()
{
return mSocket.Close();
}
ThreadError Server::AddResource(Resource &aResource)
{
ThreadError error = kThreadError_None;
for (Resource *cur = mResources; cur; cur = cur->mNext)
{
VerifyOrExit(cur != &aResource, error = kThreadError_Busy);
}
aResource.mNext = mResources;
mResources = &aResource;
exit:
return error;
}
void Server::HandleUdpReceive(void *aContext, otMessage aMessage, const otMessageInfo *aMessageInfo)
{
Server *obj = reinterpret_cast<Server *>(aContext);
obj->HandleUdpReceive(*static_cast<Message *>(aMessage), *static_cast<const Ip6::MessageInfo *>(aMessageInfo));
}
void Server::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
Header header;
char uriPath[kMaxReceivedUriPath];
char *curUriPath = uriPath;
const Header::Option *coapOption;
SuccessOrExit(header.FromMessage(aMessage));
aMessage.MoveOffset(header.GetLength());
coapOption = header.GetCurrentOption();
while (coapOption != NULL)
{
switch (coapOption->mNumber)
{
case Header::Option::kOptionUriPath:
VerifyOrExit(coapOption->mLength < sizeof(uriPath) - static_cast<size_t>(curUriPath - uriPath), ;);
memcpy(curUriPath, coapOption->mValue, coapOption->mLength);
curUriPath[coapOption->mLength] = '/';
curUriPath += coapOption->mLength + 1;
break;
case Header::Option::kOptionContentFormat:
break;
default:
ExitNow();
}
coapOption = header.GetNextOption();
}
curUriPath[-1] = '\0';
for (Resource *resource = mResources; resource; resource = resource->mNext)
{
if (strcmp(resource->mUriPath, uriPath) == 0)
{
resource->HandleRequest(header, aMessage, aMessageInfo);
ExitNow();
}
}
exit:
{}
}
Message *Server::NewMessage(uint16_t aReserved)
{
return mSocket.NewMessage(aReserved);
}
ThreadError Server::SendMessage(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
return mSocket.SendTo(aMessage, aMessageInfo);
}
} // namespace Coap
} // namespace Thread