Description
LuaUPNP is a Lua module that provides the minimal functions required for any Lua program to:- Detect and search for UPNP devices on the network
- Get all command information for all known UPNP devices
- Send SOAP commands and receive responses from UPNP devices
Its dependencies are LuaSocket 3.0 or above and LXML (for parsing the UPnP XML responses)
Usage
To use LuaUPNP do the following:This puts the module table in upnp which has the functions required.
API and objects
The module generates and manages 2 types of objects:- UPnP object
- This provides functions necessary to search and find all UPnP objects and contains variables that configure the UPnP search.
- UPnP host object
- This object holds all the information about the UPnP host as well as provides functions to get information about the host or to send SOAP commands to the host.
UPnP object
Creation
To create a new UPnP object use the function:-
new(ip,port,iface) -> (upnp Object: table)
Member functions
- close() - close the server client sockets opened in the object.
- Usage:
-
upnpObject:close()
- The function does not return anything
- pcap() - listen passively for any UPnP announcement packets to detect UPnP devices. If any new hosts are found they are added to the hosts table.
- Usage:
-
upnpObject:pcap(timeout)
- The timeout parameter is optional and specifies the total time for which to listen in seconds. The function does not return anything.
- msearch() - send a search request over the UPnP multicast address to get responses from all UPnP devices. If any new hosts are found they are added to the hosts table.
- Usage:
-
upnpObject:msearch(timeout, searchType, searchName)
- All 3 parameters are optional.
- timeout like in pcap specifies the total time for which to listen
- searchType and searchName if specified are placed in the ST header of the msearch message.
Member Objects
The following objects are in the upnpObject- hosts - array of host objects. This array contains all found UPNP host objects. The structure of this object is described below.
- clnt - client socket used to receive msearch responses
- srvr - server socket used listen for UPnP hosts in pcap
- msearchHeaders - table containing headers sent during msearch. By default it contains the following:
-
MAN = '"ssdp:discover"', MX = "3"
-
Member Variables
- ip - multicast ip for uPnP. Set by default to 239.255.255.250
- port - uPnP port. Set by default to 1900.
- iface - interface for UPnP. This is used by srvr object to join the UPnP multicast group with this interface. Set by default to "*" to bind to all local interfaces.
uPnP host object
Creation
On doing msearch or pcap the hosts array is populated with discovered hostsMember Functions
- getInfo() - get the full information for the host. This populates all the devices tables for the host containing all the services, state variables and actions for the device.
- Usage:
-
hostObject:getInfo()
- If successful then returns true, otherwise nil and a error message.
- send(devName, serviceName, actionName) - send a SOAP command with the info in the parameters
- Usage:
-
hostObj:send(deviceName,serviceName, actionName,{[Arg1]=Arg1_Value, [Arg2]=Arg2_Value})
- If successful it returns the response SOAP XML followed by the return tags that are expected in the message if any.
Member Variables
- xmlFile - full url path of xmlFile describing the service
- host - url of host
- page - xml file name NOTE: Usually:
xmlFile = protocol.."://"..host.."/"..page
- upnpType - info of the SERVER tag in the xmlFile
- protocol - such as http
- enumerated - Boolean variable, if true then all information already enumerated using getInfo()
- serverType - Type of server returned as the server field in the header when the service XML is downloaded
- devices[] - Table containing list of devices with their data structure of their offered services (populated when getInfo() called). Table keys are the device names which are extracted from the text of the deviceType tag by picking the text after 'device:'. This is from the xmlFile of the host object
- .deviceType - Info in the deviceType tag of the device XML file
- .friendlyName - Info from the same name XML tag if existed
- .modelDescription - Info from the same name XML tag if existed
- .modelName - Info from the same name XML tag if existed
- .modelNumber - Info from the same name XML tag if existed
- .modelURL - Info from the same name XML tag if existed
- .presentationURL - Info from the same name XML tag if existed
- .UDN - Info from the same name XML tag if existed
- .UPC - Info from the same name XML tag if existed
- .manufacturer - Info from the same name XML tag if existed
- .manufacturerURL - Info from the same name XML tag if existed
- .services[] - Table containing list of services with their data structure offered by the device. Table keys are the service names which are extracted from the text of the serviceType tag by picking the text after 'service:'. This is from the xmlFile of the host object.
- .serviceType - Info in the serviceType tag of the device XML file
- .serviceId - Info from the same name XML tag if existed
- .controlURL - Info from the same name XML tag if existed
- .eventSubURL - Info from the same name XML tag if existed
- .SCPDURL - Info from the same name XML tag if existed
- .actions[] - Table containing list of actions with their data structure of arguments in the service extracted from the XML in SCPDURL of the service description. Table keys are the action names which is the text of the name tag of the action.
- .arguments[] - Table containing the list of arguments the action takes and their description. Table keys are the argument names which is the text of the name tag of the argument
- .direction - Info from the same name XML tag if existed
- .relatedStateVariable - Info from the same name XML tag if existed
- .serviceStateVariables[] - Table containing list of service state variables with their data structure of values in the service extracted from the XML in SCPDURL of the service description. Table keys are the state variable names which is the text of the name tag of the stateVariable
- .sendEvents - Info from the same name XML tag if existed
- .dataType - Info from the same name XML tag if existed
- .defaultValue - Info from the same name XML tag if existed
- .allowedValues[] - Array containing allowed values for the state variable
- .allowedValueRange - Table with 1st element the minimum value and 2nd element as maximum value
- .arguments[] - Table containing the list of arguments the action takes and their description. Table keys are the argument names which is the text of the name tag of the argument