void InvocationUpnp::WriteRequest(const Uri& aUri) { Sws<1024> writeBuffer(iSocket); WriterHttpRequest writerRequest(writeBuffer); Bwh body; try { Endpoint endpoint(aUri.Port(), aUri.Host()); TUint timeout = iCpStack.Env().InitParams()->TcpConnectTimeoutMs(); iSocket.Connect(endpoint, timeout); } catch (NetworkTimeout&) { iInvocation.SetError(Error::eSocket, Error::eCodeTimeout, Error::kDescriptionSocketTimeout); THROW(NetworkTimeout); } try { InvocationBodyWriter::Write(iInvocation, body); WriteHeaders(writerRequest, aUri, body.Bytes()); writeBuffer.Write(body); writeBuffer.WriteFlush(); } catch (WriterError) { iInvocation.SetError(Error::eHttp, Error::kCodeUnknown, Error::kDescriptionUnknown); THROW(WriterError); } }
virtual void UnorderedRemove(ITopology2Group* aItem) { Bwh* result = new Bwh(100); result->Replace(aItem->Device().Udn()); result->Append(" Group Removed"); iFactory->Destroy(aItem->Device().Udn()); iRunner.Result(result); }
void DviProtocolUpnpServiceXmlWriter::GetRelatedVariableName(Bwh& aName, const Brx& aActionName, const Brx& aParameterName) { static const Brn prefix("A_ARG_TYPE_"); const TUint len = prefix.Bytes() + aActionName.Bytes() + 1 + aParameterName.Bytes(); aName.Grow(len); aName.Append(prefix); aName.Append(aActionName); aName.Append('_'); aName.Append(aParameterName); }
void OpenHome::TestFramework::Runner::Main(TInt aArgc, TChar* aArgv[], InitialisationParams* aInitParams) { OptionParser parser; OptionBool loopback("-l", "--loopback", "Use the loopback adapter only"); parser.AddOption(&loopback); if (!parser.Parse(aArgc, aArgv) || parser.HelpDisplayed()) { return; } if (loopback.Value()) { aInitParams->SetUseLoopbackNetworkInterface(); } UpnpLibrary::Initialise(aInitParams); UpnpLibrary::StartCombined(); //Debug::SetLevel(Debug::kDevice/*Debug::kXmlFetch | Debug::kHttp*/); Print("TestDviDeviceList - starting\n"); DvDevices* devices = new DvDevices; CpDevices* deviceList = new CpDevices; FunctorCpDevice added = MakeFunctorCpDevice(*deviceList, &CpDevices::Added); FunctorCpDevice removed = MakeFunctorCpDevice(*deviceList, &CpDevices::Removed); Print("Count devices implementing service1\n"); Brn domainName("a.b.c"); Brn serviceType("service1"); TUint ver = 1; CpDeviceListUpnpServiceType* list = new CpDeviceListUpnpServiceType(domainName, serviceType, ver, added, removed); Blocker* blocker = new Blocker; blocker->Wait(aInitParams->MsearchTimeSecs()); std::vector<const char*> udns; udns.push_back((const char*)gNameDevice1.Ptr()); udns.push_back((const char*)gNameDevice2.Ptr()); deviceList->Validate(udns); udns.clear(); delete list; deviceList->Clear(); Print("Count devices implementing service2\n"); serviceType.Set("service2"); list = new CpDeviceListUpnpServiceType(domainName, serviceType, ver, added, removed); blocker->Wait(aInitParams->MsearchTimeSecs()); udns.push_back((const char*)gNameDevice1_1.Ptr()); udns.push_back((const char*)gNameDevice2.Ptr()); deviceList->Validate(udns); udns.clear(); delete list; delete blocker; delete deviceList; delete devices; Print("TestDviDeviceList - completed\n"); UpnpLibrary::Close(); }
static void RandomiseUdn(Bwh& aUdn) { aUdn.Grow(aUdn.Bytes() + 1 + Ascii::kMaxUintStringBytes + 1); aUdn.Append('-'); Bws<Ascii::kMaxUintStringBytes> buf; NetworkInterface* nif = Stack::NetworkInterfaceList().CurrentInterface(); TUint max = nif->Address(); delete nif; (void)Ascii::AppendDec(buf, Random(max)); aUdn.Append(buf); aUdn.PtrZ(); }
void OpenHome::TestFramework::RandomiseUdn(Environment& aEnv, Bwh& aUdn) { aUdn.Grow(aUdn.Bytes() + 1 + Ascii::kMaxUintStringBytes + 1); aUdn.Append('-'); Bws<Ascii::kMaxUintStringBytes> buf; std::vector<NetworkAdapter*>* subnetList = aEnv.NetworkAdapterList().CreateSubnetList(); TUint max = (subnetList->size() > 0? (*subnetList)[0]->Address() : UINT_MAX); aEnv.NetworkAdapterList().DestroySubnetList(subnetList); (void)Ascii::AppendDec(buf, aEnv.Random(max)); aUdn.Append(buf); aUdn.PtrZ(); }
static void RandomiseUdn(Bwh& aUdn) { aUdn.Grow(aUdn.Bytes() + 1 + Ascii::kMaxUintStringBytes + 1); aUdn.Append('-'); Bws<Ascii::kMaxUintStringBytes> buf; std::vector<NetworkAdapter*>* subnetList = Stack::NetworkAdapterList().CreateSubnetList(); TUint max = (*subnetList)[0]->Address(); TUint seed = DviStack::ServerUpnp().Port((*subnetList)[0]->Address()); SetRandomSeed(seed); Stack::NetworkAdapterList().DestroySubnetList(subnetList); (void)Ascii::AppendDec(buf, Random(max)); aUdn.Append(buf); aUdn.PtrZ(); }
static void RandomiseUdn(Bwh& aUdn) { aUdn.Grow(aUdn.Bytes() + 1 + Ascii::kMaxUintStringBytes + 1); aUdn.Append('-'); Bws<Ascii::kMaxUintStringBytes> buf; NetworkAdapter* nif = Stack::NetworkAdapterList().CurrentAdapter(kAdapterCookie); TUint max = nif->Address(); TUint seed = DviStack::ServerUpnp().Port(nif->Address()); SetRandomSeed(seed); nif->RemoveRef(kAdapterCookie); (void)Ascii::AppendDec(buf, Random(max)); aUdn.Append(buf); aUdn.PtrZ(); }
void SuiteMulticast::Receiver() { iPortLock.Wait(); SocketUdpMulticast recv(0, Endpoint(iPort, kMulticastAddress)); iPort = recv.Port(); iPortLock.Signal(); iSender.Signal(); // signal ready to begin receiving Bwh buf(kBufBytes); Brn exp = iExp.Split(4); while(1) { recv.Receive(buf); TUint num = *((TUint32*)buf.Ptr()); if(num == kQuit) { break; } Brn exp2 = exp.Split(0, num); Brn buf2 = buf.Split(4); TEST(buf2 == exp2); iSender.Signal(); } iSender.Signal(); }
DvDevices::DvDevices() { RandomiseUdn(gNameDevice1); RandomiseUdn(gNameDevice1_1); RandomiseUdn(gNameDevice1_2); RandomiseUdn(gNameDevice2); DviDeviceStandard* device = new DviDeviceStandard(Brn(gNameDevice1)); iDevices[0] = device; device->SetAttribute("Upnp.Domain", "a.b.c"); device->SetAttribute("Upnp.Type", "test1"); device->SetAttribute("Upnp.Version", "1"); device->SetAttribute("Upnp.FriendlyName", (const TChar*)gNameDevice1.Ptr()); AddService(device, new DviService("a.b.c", "service1", 1)); device = new DviDeviceStandard(Brn(gNameDevice1_1)); iDevices[0]->AddDevice(device); device->SetAttribute("Upnp.Domain", "a.b.c"); device->SetAttribute("Upnp.Type", "test3"); device->SetAttribute("Upnp.Version", "1"); device->SetAttribute("Upnp.FriendlyName", (const TChar*)gNameDevice1_1.Ptr()); AddService(device, new DviService("a.b.c", "service2", 1)); AddService(device, new DviService("a.b.c", "service3", 1)); device->SetEnabled(); device = new DviDeviceStandard(Brn(gNameDevice1_2)); iDevices[0]->AddDevice(device); device->SetAttribute("Upnp.Domain", "a.b.c"); device->SetAttribute("Upnp.Type", "test4"); device->SetAttribute("Upnp.Version", "1"); device->SetAttribute("Upnp.FriendlyName", (const TChar*)gNameDevice1_2.Ptr()); AddService(device, new DviService("a.b.c", "service4", 1)); device->SetEnabled(); iDevices[0]->SetEnabled(); device = new DviDeviceStandard(Brn(gNameDevice2)); iDevices[1] = device; device->SetAttribute("Upnp.Domain", "a.b.c"); device->SetAttribute("Upnp.Type", "test2"); device->SetAttribute("Upnp.Version", "1"); device->SetAttribute("Upnp.FriendlyName", (const TChar*)gNameDevice2.Ptr()); AddService(device, new DviService("a.b.c", "service1", 1)); AddService(device, new DviService("a.b.c", "service2", 1)); iDevices[1]->SetEnabled(); }
void TestDviDeviceList() { InitialisationParams& initParams = Stack::InitParams(); TUint oldMsearchTime = initParams.MsearchTimeSecs(); initParams.SetMsearchTime(1); Debug::SetLevel(Debug::kDevice | Debug::kDvDevice | Debug::kXmlFetch); Print("TestDviDeviceList - starting\n"); DvDevices* devices = new DvDevices; CpDevices* deviceList = new CpDevices; FunctorCpDevice added = MakeFunctorCpDevice(*deviceList, &CpDevices::Added); FunctorCpDevice removed = MakeFunctorCpDevice(*deviceList, &CpDevices::Removed); Print("Count devices implementing service1\n"); Brn domainName("a.b.c"); Brn serviceType("service1"); TUint ver = 1; CpDeviceListUpnpServiceType* list = new CpDeviceListUpnpServiceType(domainName, serviceType, ver, added, removed); std::vector<const char*> udns; udns.push_back((const char*)gNameDevice1.Ptr()); udns.push_back((const char*)gNameDevice2.Ptr()); deviceList->Validate(udns); udns.clear(); delete list; deviceList->Clear(); Print("Count devices implementing service2\n"); serviceType.Set("service2"); list = new CpDeviceListUpnpServiceType(domainName, serviceType, ver, added, removed); udns.push_back((const char*)gNameDevice1_1.Ptr()); udns.push_back((const char*)gNameDevice2.Ptr()); deviceList->Validate(udns); udns.clear(); delete list; delete deviceList; delete devices; Print("TestDviDeviceList - completed\n"); initParams.SetMsearchTime(oldMsearchTime); }
virtual void UnorderedAdd(ITopology2Group* aItem) { Bwh* result = new Bwh(100); result->Replace(aItem->Device().Udn()); result->Append(Brn(" Group Added")); iRunner.Result(result); FunctorGeneric<MockCbData<Brn>*> fRoom = MakeFunctorGeneric(*this, &GroupWatcher::RoomCallback); FunctorGeneric<MockCbData<Brn>*> fName = MakeFunctorGeneric(*this, &GroupWatcher::NameCallback); FunctorGeneric<MockCbData<TUint>*> fSourceIndex = MakeFunctorGeneric(*this, &GroupWatcher::SourceIndexCallback); FunctorGeneric<MockCbData<TBool>*> fStandby = MakeFunctorGeneric(*this, &GroupWatcher::StandbyCallback); iFactory->Create<Brn>(aItem->Device().Udn(), aItem->RoomName(), fRoom); iFactory->Create<Brn>(aItem->Device().Udn(), aItem->Name(), fName); iFactory->Create<TUint>(aItem->Device().Udn(), aItem->SourceIndex(), fSourceIndex); iFactory->Create<TBool>(aItem->Device().Udn(), aItem->Standby(), fStandby); std::vector<Watchable<ITopology2Source*>*> sources = aItem->Sources(); for(TUint i=0; i<sources.size(); i++) { iFactory->Create<ITopology2Source*>(aItem->Device().Udn(), *(sources[i]), MakeFunctorGeneric(*this, &GroupWatcher::SourcesCallback)); } }
void DviDevice::GetUriBase(Bwh& aUriBase, TIpAddress aInterface, TUint aPort, IDvProtocol& aProtocol) { static const TUint kMaxAddressBytes = 21; // xxx.xxx.xxx.xxx:xxxxx const Brx& name = aProtocol.ProtocolName(); aUriBase.Grow(Http::kUriPrefix.Bytes() + kMaxAddressBytes + iUdn.Bytes() + name.Bytes() + 3); // +2 for slashes after port, udn & name aUriBase.Append(Http::kUriPrefix); Endpoint endpt(aPort, aInterface); Endpoint::EndpointBuf buf; endpt.AppendEndpoint(buf); aUriBase.Append(buf); aUriBase.Append('/'); aUriBase.Append(iUdn); aUriBase.Append('/'); aUriBase.Append(name); aUriBase.Append('/'); }
static void RandomiseUdn(std::string& aUdn) { Bwh udn; udn.Grow((TUint)aUdn.length() + 1 + Ascii::kMaxUintStringBytes + 1); Brn buf((const TByte*)aUdn.c_str(), (TUint)aUdn.length()); udn.Append(buf); udn.Append('-'); Bws<Ascii::kMaxUintStringBytes> addr; std::vector<NetworkAdapter*>* subnetList = gEnv->NetworkAdapterList().CreateSubnetList(); TUint max = (*subnetList)[0]->Address(); TUint seed = gDvStack->ServerUpnp().Port((*subnetList)[0]->Address()); SetRandomSeed(seed); gEnv->NetworkAdapterList().DestroySubnetList(subnetList); (void)Ascii::AppendDec(addr, Random(max)); udn.Append(addr); udn.PtrZ(); aUdn.assign((const char*)udn.Ptr(), udn.Bytes()); }
void InvocationUpnp::ReadResponse() { OutputProcessorUpnp outputProcessor; HttpHeaderContentLength headerContentLength; HttpHeaderTransferEncoding headerTransferEncoding; Bwh entity; iReaderResponse.AddHeader(headerContentLength); iReaderResponse.AddHeader(headerTransferEncoding); iReaderResponse.Read(kResponseTimeoutMs); const HttpStatus& status = iReaderResponse.Status(); if (status != HttpStatus::kOk) { LOG2(kService, kError, "InvocationUpnp::ReadResponse, http error %u ", status.Code()); LOG2(kService, kError, status.Reason()); LOG2(kService, kError, "\n"); if (status != HttpStatus::kInternalServerError) { iInvocation.SetError(Error::eHttp, status.Code(), status.Reason()); THROW(HttpError); } } if (headerTransferEncoding.IsChunked()) { ReaderHttpChunked dechunker(iReadBuffer); dechunker.Read(); dechunker.TransferTo(entity); } else { TUint length = headerContentLength.ContentLength(); if (length != 0) { Bwh buf(length); while (length > 0) { TUint readBytes = (length<kMaxReadBytes? length : kMaxReadBytes); buf.Append(iReadBuffer.Read(readBytes)); length -= readBytes; } buf.TransferTo(entity); } else { // no content length - read until connection closed by server try { for (;;) { Brn buf = iReadBuffer.Read(kMaxReadBytes); entity.Grow(entity.Bytes() + kMaxReadBytes); entity.Append(buf); } } catch (ReaderError&) { Brn snaffle = iReadBuffer.Snaffle(); entity.Grow(entity.Bytes() + snaffle.Bytes()); entity.Append(snaffle); } } } if (status == HttpStatus::kInternalServerError) { Brn envelope = XmlParserBasic::Find("Envelope", entity); Brn body = XmlParserBasic::Find("Body", envelope); Brn fault = XmlParserBasic::Find("Fault", body); Brn detail = XmlParserBasic::Find("detail", fault); Brn code = XmlParserBasic::Find("errorCode", detail); Brn description = XmlParserBasic::Find("errorDescription", detail); iInvocation.SetError(Error::eUpnp, Ascii::Uint(code), description); THROW(HttpError); } const Invocation::VectorArguments& outArgs = iInvocation.OutputArguments(); const TUint count = (TUint)outArgs.size(); Brn envelope = XmlParserBasic::Find("Envelope", entity); Brn body = XmlParserBasic::Find("Body", envelope); const Brn responseTagTrailer("Response"); const Brx& actionName = iInvocation.Action().Name(); TUint len = actionName.Bytes() + responseTagTrailer.Bytes(); Bwh responseTag(len); responseTag.Append(actionName); responseTag.Append(responseTagTrailer); Brn response = XmlParserBasic::Find(responseTag, body); for (TUint i=0; i<count; i++) { const Brx& name = outArgs[i]->Parameter().Name(); Brn value = XmlParserBasic::Find(name, response); outArgs[i]->ProcessOutput(outputProcessor, value); } }
void EventSessionUpnp::Run() { CpiSubscription* subscription = NULL; iErrorStatus = &HttpStatus::kOk; try { iReaderRequest->Flush(); iReaderRequest->Read(); // check headers if (iReaderRequest->MethodNotAllowed()) { Error(HttpStatus::kBadRequest); } if (!iHeaderNt.Received() || !iHeaderNts.Received()) { Error(HttpStatus::kBadRequest); } if (iHeaderNt.Value() != kExpectedNt || iHeaderNts.Value() != kExpectedNts || !iHeaderSid.Received() || iHeaderSid.Sid().Bytes() == 0 || !iHeaderSeq.Received()) { Error(HttpStatus::kPreconditionFailed); } subscription = CpiSubscriptionManager::FindSubscription(iHeaderSid.Sid()); if (subscription == NULL) { /* the UPnP spec contains a potential race condition where the first NOTIFY message can be processed ahead of the SUBSCRIBE reply which provides the sid. Wait until any in-progress subscriptions complete and try again in case that's what has happened here */ CpiSubscriptionManager::WaitForPendingAdds(); subscription = CpiSubscriptionManager::FindSubscription(iHeaderSid.Sid()); if (subscription == NULL) { LOG2(kEvent, kError, "notification for unexpected device - ") LOG2(kEvent, kError, iHeaderSid.Sid()); LOG2(kEvent, kError, "\n"); Error(HttpStatus::kPreconditionFailed); } } if (!subscription->UpdateSequenceNumber(iHeaderSeq.Seq())) { subscription->SetNotificationError(); subscription->RemoveRef(); subscription = NULL; } } catch(HttpError) {} catch(ReaderError) {} try { // write response Sws<128> writerBuffer(*this); WriterHttpResponse response(writerBuffer); response.WriteStatus(*iErrorStatus, Http::eHttp11); response.WriteFlush(); // read entity if (subscription != NULL) { Bwh entity; if (iHeaderTransferEncoding.IsChunked()) { ReaderHttpChunked dechunker(*iReadBuffer); dechunker.Read(); dechunker.TransferTo(entity); } else { TUint length = iHeaderContentLength.ContentLength(); if (length == 0) { THROW(HttpError); } entity.Grow(length); while (length > 0) { TUint readBytes = (length<kMaxReadBytes? length : kMaxReadBytes); entity.Append(iReadBuffer->Read(readBytes)); length -= readBytes; } } // process entity LOG(kEvent, "EventSessionUpnp::Run, sid - "); LOG(kEvent, iHeaderSid.Sid()); LOG(kEvent, " seq - %u\n", iHeaderSeq.Seq()); ProcessNotification(*subscription, entity); } } catch(HttpError) { LogError(subscription, "HttpError"); } catch(ReaderError) { LogError(subscription, "ReaderError"); } catch(WriterError) { LogError(subscription, "WriterError"); } catch(NetworkError) { LogError(subscription, "NetworkError"); } catch(XmlError) { LogError(subscription, "XmlError"); } if (subscription != NULL) { subscription->RemoveRef(); } }
Bwh::Bwh(const Bwh& aBuf) : Bwx(aBuf.Bytes(), aBuf.Bytes()) { iPtr = (TByte*)malloc(aBuf.Bytes()); Replace(aBuf); }