Cross-Platform Android / iOS-Based Smart Switch Control Middleware in a Digital Home

With technological and economic development, people’s lives have been improved substantially, especially their home environments. One of the key aspects of these improvements is home intellectualization, whose core is the smart home control system. Furthermore, as smart phones have become increasingly popular, we can use them to control the home system through Wi-Fi, Bluetooth, and GSM. This means that control with phones is more convenient and fast and now becomes the primary terminal controller in the smart home. In this paper, we propose middleware for developing a cross-platform Android/iOS-based solution for smart switch control software, focus on theWi-Fi based communication protocols between the cellphone and the smart switch, achieved a plugin-based smart switch function, defined and implemented the JavaScript interface, and then implemented the crossplatform Android/iOS-based smart switch control software; also the scenarios are illustrated. Finally, tests were performed after the completed realization of the smart switch control system.


Introduction
With the improvement of living standards and the changes in the concept of consumption, the smart home has quietly entered people's lives.The Internet of things makes it possible for objects to exchange information and control each other through wired or wireless communication networks, forming a massive network system that endows things with "wisdom" and therefore integrates human society with the system of objects.The appearance of the Internet of things provides a new opportunity for the smart home control system [1][2][3][4][5].Normally, the following control plans are included in a smart home control system: controlling home appliances with an infrared remote controller, touch tablet, LCD monitors, keyboards, and other devices.The design of terminal control for the abovementioned systems is complicated and requires matched controllers, which leads to high user cost and does not support remote control.Currently, most smart home control systems adopt the PC as the control terminal, which clearly presents the problems of being bulky, inconvenient, costly, and unportable, not meeting the needs of the modern smart home control system.As smart phones have become increasingly popular, we can use them to control the home system through Wi-Fi, Bluetooth, and GSM.This means that control with phones is more convenient and fast and has become the primary terminal controller of the smart home control system [6][7][8][9][10][11][12][13].Using a mobile smart phone, people can control their home appliances at any time, changing the original static home environment into a dynamic smart digital home.However, the development of mobile applications is not simple; instead, it is a challenge that requires the developers to use multiple programming languages, such as SDK and API, on multiple smart cellphone operating systems.There are several smart phone operating systems that dominate the market, such as Android, iOS, and Windows Phone.However, in order to develop mobile smart switch applications to the majority of the smart phones, it is necessary to develop and deploy the smart switch application to the mainstream smart phone operating system.Therefore, there is a great demand to provide a cross-platform mobile home control software solution, which can develop the application only once, and deploy it to several mobile operating systems.The contributions of this paper are as follows: (i) Proposing a cross-platform smart switch control software development framework for mobile smart phone, from the top to bottom view, which consists of UI layer, HTML Rendering Engine layer, Smart Switch Plugin layer, and Mobile OS layer.
(ii) Adopting the plugin technology to encapsulate the public and user-defined API and realize the basic functions of the smart switch.
(iii) Defining the JavaScript interface for smart switch, such as network configuration, device connection, device switch control, device time synchronization, device information reception, and time setting.
(iv) Developing the smart switch and to utilize the smart phone to control the switch and the timer remotely and prove the possibility of developing cross-platform (Android/iOS) smart switch cellphone controlling software.
The rest of the paper is organized as follows.Section 2 briefly reviews related works.Section 3 describes the architecture of cross-platform Android/iOS-based switch control software middleware.Section 4 illustrates the scenarios.Section 5 is the performance measurements and analysis.Finally, Section 6 concludes the paper and the future work.

Related Works
When developing a smart phone application, there are two possible approaches to determine: the first approach is to develop a native application for each of the smart phone operating systems, and the second approach is to use a cross-platform development framework in order to develop the application only once and deploy it to several operating systems.
The native development approach is certain binary system executable programs installed in the equipment, included in the software development kit (SDK) and spread by the app store.Each mobile operating system has an SDK; unfortunately, different operating systems have different SDKs.For example, we must use iOS SDK and development tools to build an application for iOS and must use Objective-C programming language to code the application.However, an Android application must be developed using the Android SDK and coded in the Java language.Therefore, to build a mobile application, we have to know each SDK and use the corresponding programming language to code the application.Because the SDK of each platform has a steep learning curve, the development of cross-platform applications for smart phones is very complicated.
The cross-platform development approach means that the development of the software does not rely on any specific operating system [14][15][16][17][18][19][20]; instead, the coding set can be used in different operating systems without any modifications.The development approach to the cross-platform smart phone applications is geared to the need for multiple terminal platforms and developing languages.Over the last few years, several cross-platform development technologies became popular with the purpose of deploying a single application to several smart phone operating systems aiming to reduce the amount of time.XMLVM [21] is introduced to be a solution for cross-compilation, which is a byte code level crosscompiler that is based on the Java byte code instructions.DragonRad [22] is a cross-platform mobile application development platform by Seregon Solutions Inc., which supports iPhone, Android, BlackBerry, and Windows mobile OS.RhoMobile [23] is another solution for the cross-compilation, which is a Ruby-based mobile development, which can be used across Linux, Mac, and Windows OS.PhoneGap [24] is an open-source mobile development framework that supports most heterogeneous platforms including iOS and Android.Emerging web standards especially as HTML5 are one possible solution; the difference between a web application and a native application is that the web applications are coded through web techniques (HTML, JavaScript, and CSS) and run in the kernel of the browser regardless of the operating system.Therefore, HTML5 based cross-platform development framework is going to be used to develop the mobile smart switch application to shorten the development period of time, which can write an application once, and deploy it to several operating systems.Cross-platform development approach allows implementing an application and its user interface using web technologies like HTML and CSS.Then the application can be built for several mobile platforms, such as iOS, Android, and Windows Phone.

Proposed System Architecture
The cross-platform smart switch cellphone control software is implemented using web techniques but packed as native applications.The applications are written with programming languages that the developers are familiar with and are applicable across multiple operating systems.Similar to native applications, cross-platform applications can be distributed and installed through the app store.The smart switch application can support both Android and iOS operating systems.The smart switch application can be invoked in the form of a plugin by the terminal developers.The framework of the developing middleware for the cross-platform smart switch cellphone control software is shown in Figure 1.
From the top of the framework down, the UI layer, HTML Rendering Engine layer, Wi-Fi Smart Switch Plugin layer, and Mobile OS layer are shown.The UI layer, which is the interface of the smart switch control software, is programmed with web techniques such as HTML, CSS, and JavaScript and is consistent across all smart phone platforms.
The HTML Rendering Engine layer is the key to the realization of the cross mobile OS.The HTML Rendering Engine layer is also called WebView and is actually the internal browser of the mobile smart phones.The existence of this layer is the foundation for the web application to be packed for the local OS.As it runs in WebView, we can use HTML and CSS to adjust the page layout conveniently.We extended and encapsulated WebView under the different operating  systems to a strong browser that can visit the local API of the equipment and is therefore able to visit the local API via JavaScript.In this case, we can visit the bottom API of the Wi-Fi based smart switch plugin using WebView.
The Wi-Fi Smart Switch Plugin layer is the communications function realization layer of the control software.It realizes the functions of configuring the network, connecting the smart switch, controlling the switch, time synchronization, accessing switch information, and setting timers.Those functions, programmed with native codes, must match corresponding JavaScript API functions.Thus, JavaScript API will expose the native functions to the JavaScript running in the browser, which enables the developer of the crossplatform applications to invoke the Wi-Fi smart switch control function by invoking the uniformed JavaScript interface regardless of the differences for mobile operating systems.
The Mobile OS layer is the basic API.In this control software, we used bottom API interface such as the shared network of Android and iOS, input and output.

Smart Switch Circuit Board.
The smart switch utilizes the SOC (system on chip) power metering chip, which can achieve intellectual detection of the power net and the power usage of the electrical equipment such as LED energy saving lamps, air conditioners, refrigerators, and microwaves.The smart switch circuit board is shown in Figure 2.
The SOC chip is suitable for use in the home, rental house, office, and laboratory.We constructed a very brief wireless network with the CC3000MOD and MCU MSP430F5309 of TI.The CC3000MOD supports the IEEE 802.11 b/g agreement and has an IPv4 TCP/IP protocol stack in itself.The CC3000MOD can form an ideal wireless network with MCU (such as MSP430 series) that is slow, inexpensive, and power efficient.The SimpleLink proposal of TI has provided strong technical support and simplified the design of the hardware and software as well as the development of the application.

Communication Protocols.
The communication is achieved using the master/slave model, which means the equipment (Wi-Fi switch) will not report information conditions or control information on its own initiative.The controlling device (cellphone) and the device controlled (switch) are in the same LAN, through which the data control frames are transferred using the UDP protocol.The control command frame can be divided into the master frame and the slave frame, and the format of each is as follows.
Device.It is mainly the structure used to describe the slave device.The structure is as shown in Algorithm 2. For this application, the device type is 0001H.
Editable Data Block.It usually includes the format, length, meaning, and unit of the data.It normally consists of data and DRH, which includes DIF, DIFE, and VIF.

The Slave Frame.
The format of the slave frame data is the same as that of the master frame.There are certain differences in regard to the device interfaces.The slave device data frame includes the hardware details of the slave device.Its structure is as shown in Algorithm 3.

The Realization of the Smart Switch Plugin Function.
The plugin-based model is used to encapsulate the local API, including the public and the user-defined application with a unified plugin interface for the upper level to invoke the application module conveniently.The realization framework of the plugin-based smart switch function is shown in Figure 3.
Taking the device interface as an example, the public module integrates the common and general functions for invocation, which avoids repeated development and increases the efficiency for the programmer.These functions include small modules such as message receiving/sending, contacts, and camera, which can be realized by invoking the bottom API of each mobile OS.One certainty is that the invocation interfaces, realization languages, and development platforms of these modules are different on different platforms, in this case, the Android and iPhone systems.However, for the design of the user-defined module, taking the smart switch function interface as an example, the module requires repeated usage of the same function on different cellphone platforms targeting different projects to develop background.When the code volume is massive and the realization structure is complicated, the function will be encapsulated in the self-defined module so that the developer can invoke the module flexibly, decreasing the difficulty of the second development and increasing the development efficiency.
Based on the smart switch communication protocol and the SimpleLink Wi-Fi CC3000 solution provided by TI, we realized the basic functions of the smart switch, including network configuring, device connecting, switch control, time synchronization, device information receiving, and timer setting.We also encapsulated the functions of the switch  (ii) Send a set of static timer data.
(iii) Send the information from the data switch.
(iv) Deliver passcodes and accounts to the smart switch.
searchDevice: send out a UDP broadcast package that includes the IP address and the interface code of the mobile OS.The function will receive the UDP package sent back by the smart switch, which includes the IP address, the interface number, the MAC address, and the switch status of the smart switch.The searchDevice function will run in the background without blocking the WebCore; other functions will block the UI path.When we click the searchDevice, the interface will notify the user that it is connecting and can be stopped by clicking.To avoid overtime, we can establish a time for UDP reception.For example, we set the timeout time to s.setSoTimeout (1000).
SetAC: it is to set the hotspot, broadcasting the account and passcode of Wi-Fi; invoke the TI CC3000 interface to search for available devices in the environment.
SetOF: it is to set the on/off status of the smart switch.SetTime: it is to set the timer length of the smart switch.
BuildFrame series are mainly used to build the UDP frame to issue commands to the smart switch.The frame includes the communication interface COM, the control byte CI, and the editable data block.Each frame, including the search-device, device-info, device-time, and set-time frames, is in correspondence with its function realization.
CC3XWi-FiManager series provide the interface to receive the Wi-Fi details, including the SSID network parameters, and can be used to test whether the device has connected to Wi-Fi and to obtain the current IP and gateway address.
The CC3XACConfig series is the solution provided by TI.The series has the IPv4 TCP/IP protocol stack internally.
MyTimeutils, Utils, and DataTools are series designed for the format editing and transmission of the data under this system.

JavaScript Interface of the Smart Switch.
The smart switch allows the smart OS to control the on/off status of the switch, locally or remotely, through a Wi-Fi network connection.With the wireless router and the terminal device in hand, the smart switch achieves the simple function of the remote switch in addition to other functions, such as network configuration, device connection, device switch control, device time synchronization, device information reception, and time setting.Each function was processed through the uniform interface encapsulation and under the invocation of a JavaScript interface.The JavaScript interface use the cordova.exec()function to invoke the code of the current device.When invoking the cordova.exec, the JavaScript may deliver it to the outcome processing function as well as the error process programming function and deliver a parameter set to the current device code.The invocation will also be sent to the current device name and current function name.Cordova will be responsible for communication between JavaScript and the current device (see Algorithm 4).
The smart switch plugin was named UDPPlugin.The cordova.exec accepts a success feedback function, a fail feedback function, a string reference of the current device code, a string reference of the function names to be invoked, and a data block of the parameters for delivery to the current device.Action corresponds to each functional realization, and the args is the parameter from the terminal.The definition of JavaScript interface for the smart switch function is shown in Figure 5.The following sections provide detailed illustrations of the realization of each function.

Configuring the Network Connection. Smart phones may choose any available
Wi-Fi to connect and can send a broadcast frame to all available devices in the network through the broadcasting protocols, which includes the SSID and password of the Wi-Fi network, and the devices receive the broadcast frame and connect to the specified Wi-Fi network, and the corresponding JavaScript API is defined as sendPassword(); the variable action invokes the setAC function of the device; the variable args is the passcode entered by the client.The process is shown in Algorithm 5. includes the IP address and the code of the terminal.The device will return its own IP address and listening interface code to the smart phone.The smart phone then connects to the device.Under the SwitchPlugin getDevice(), the variable args is an empty parameter, and variable action invokes the device function searchDevice (see Algorithm 6).

Control of the Device Switch.
The smart phone controls the on/off status of the smart switch by sending unicast commands; the smart phone invokes the function setOF through the JS API SwitchPlugin-switchState() to control the switch (see Algorithm 7).control the switch and the timer remotely.We use the AES-128 to send encrypted information or controlling commands directly.The smart switch system working scheme is diagrammed in Figure 7.

Synchronizing the
The workflow of the interaction between the smart phone and the Wi-Fi smart switch is shown below: (1) The mobile client connects to Wi-Fi.
(2) The mobile client can then acquire the gateway address of the Wi-Fi based on the connecting information.
(3) The mobile client will calculate the broadcasting IP address based on the IP address, gateway address, subnet mask, and other information and will acquire the SSID and password of Wi-Fi from the user interface.By using the broadcasting protocol of TI CC3000, the client can broadcast the device character information and the AES-128 encrypted SSID and password to the subnetwork.
(4) The device that needs to connect to the Wi-Fi is manually set to debug mode.If the device has the same device character information, after receiving broadcasting frames, it can obtain the SSID and password and connect to the designated Wi-Fi.
(5) After the device connects to the Wi-Fi, the mobile client attempts to broadcast UDP connection request frames via the LAN broadcast protocol.The frames carry the IP address and port number of the mobile client.Connection failure will occur in the event of connection time-out.
(6) After receiving the broadcasting connection request frames, the device can resolve the IP address and port number of the mobile client and resend its own  IP address and port number to the client via UDP frames.(7) The mobile client receives the connection confirmation frames sent by the device and can then resolve and record the IP address of the device in order to send control frames.(8) When the user sends a control order from the mobile client, such as controlling the device switch, synchronizing the device clock, setting the timer, or acquiring device information, the mobile client will construct comparable UDP frames and send them to the IP address and port number of the device.
The smart switch application is installed on the smart phone.The Android and iOS operating system share the same software control interface, as shown in Figures 8(a In this scenario, we use a night light as the electric appliance.The control command is sent by the smart phone.The final results are shown in Figure 9.The left picture shows that the night light is off, and the right picture shows turningon the switch.

Performance Analysis
First, we tested whether the functionalities for control actions of the smart switch are correct.Figure 10 shows the success rates of different functions of the smart switch.From Figure 10, we can see that the success rates for set the passcode, set on/off, acquire information, and set the timer functions are below 100%, and because some experiments are done in the far distance, and there is a disconnection between the devices and the wireless access point.However, the system performance meets the requirements and can perform the corresponding functions in a right way.In the second experiment, we tested the sensibility of the control commands, which means the response time needed by the device to perform the corresponding function actions after a control command on the mobile control client is executed.Functions such as connecting to the device, setting the switch, synchronizing the clock, acquiring information, setting the timer, and others all require sending, receiving, and resolving data packages.
Different functions require different data bits, frames have different lengths, and the complexity of resolving data package information is different; therefore, the communication time delays of processing UDP are different.UDP communication and data resolving create processing time delays from the moment of creation of the DatagramSocket.After the processes of framing, creating data packages, sending data packages, and receiving data packages sent by the device, the time delay test will yield the moment at which useful data are resolved.
Insert the Log.d before creating DatagramSocket and after resolving data packages.Thereafter, we can send different function commands, export debugging information at the corresponding moment to Logcat, and calculate time delays.The response time of smart switch functions is shown in Figure 11.
Figure 11 shows that the time delays of all UDP communication and data resolving functions of the smart switch are less than 200 ms, indicating that the requirements are met.
In the third experiment, we focus on the time for a smart switch application to be developed and deployed, which can be used to measure the time to market for business goal.Here, we compare the development time for smart switch with the same UI on the Android and iOS smart phone using the "native" development and "cross-platform" development framework method, respectively, with one skilled programmer, and the development time is counted at 9:00 a.m. to 17:00 for each working day, respectively.Figure 12 shows the comparison result.
As is observed from Figure 12, we can see that the average development time for traditional native development  approach is double more than the cross-platform approach in our practical experience.Because different operating systems have different SDKs with native development approach separately, however, cross-platform based approach can develop the application only once and deploy it to several operating systems, and such works can always be done in hours or couple of days.

Conclusion and Future Work
In this paper, we propose a cross-platform smart switch control software development framework for mobile smart phone, adopt the plugin technology to encapsulate the public and user-defined API, realize the basic functions of the smart switch, define JavaScript interface for smart switch, such as network configuration, device connection, device switch control, device time synchronization, device information reception, and time setting, also develop the smart switch, utilize the smart phone to control the switch and the timer remotely, and prove the possibility of developing crossplatform smart switch cellphone controlling software.Finally, the scenarios are illustrated, and the performance tests are analyzed.In the future work, we will focus on the networking of home appliances and the security of mobile control software in a digital home.

Figure 2 :
Figure 2: The smart switch circuit board.

Figure 5 :
Figure 5: The definition of JS API of the smart switch plugin.

Figure 6 :Figure 7 :
Figure 6: The sequence diagram of the interactive process.

Figure 10 :
Figure 10: Success rates of different functions of the smart switch.

Figure 11 :
Figure 11: Responding time of smart switch functions.

Figure 12 :
Figure 12: Comparison for development time to market.
Algorithm 6the smart switch, and the device sends back the information regarding the on timer or off timer.The definition of Device Time.The smart phone sends a time synchronization command to the smart switch to synchronize the device time with the terminal in hand.JS API SwitchPlugin synTime() is as shown in Algorithm 8.3.4.5.Acquiring Device Information.The smart phone sends out the command to acquire the device information to