summaryrefslogtreecommitdiffstats
path: root/meta-app-framework/recipes-core/af-main
ModeNameSize
-rw-r--r--af-main_git.bb5739logstatsplain
-rw-r--r--af-main_git.inc1034logstatsplain
-rw-r--r--nativesdk-af-main_git.bb693logstatsplain
11' href='#n111'>111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/*
 * Copyright (C) 2016 The Qt Company Ltd.
 * Copyright (C) 2017 Konsulko Group
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * 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.
 */

import QtQuick 2.6
import QtWebSockets 1.0

WebSocket {
    id: root
    active: true
    url: bindingAddress

    property string apiString: "radio"
    property string smApiString: "soundmanager"
    property var verbs: []
    property string payloadLength: "9999"

    readonly property var msgid: {
         "call": 2,
         "retok": 3,
         "reterr": 4,
         "event": 5
    }

    readonly property int amBand: 0
    readonly property int fmBand: 1

    readonly property int stoppedState: 0
    readonly property int activeState: 1

    property int band: fmBand
    property int frequency
    property int frequencyStep
    property int minimumFrequency
    property int maximumFrequency
    property int state: stoppedState
    property int scanningState: stoppedState
    property bool scanningFreqUpdate: false
    property string stationId: ""

    signal stationFound

    property Connections c : Connections {
        target: root

        onFrequencyChanged: {
            if(scanningState != activeState) {
                // Not scanning, push update
                sendSocketMessage("frequency", { value: frequency })
            } else if(!scanningFreqUpdate) {
                // External change, stop scanning
                sendSocketMessage("scan_stop", 'None')
                scanningState = stoppedState
                sendSocketMessage("frequency", { value: frequency })
            } else {
                // This update was from scanning, clear state
                scanningFreqUpdate = false
            }
        }

        onBandChanged: {
            sendSocketMessage("band", { value: band })
            updateFrequencyRange(band)
            updateFrequencyStep(band)
            frequency = minimumFrequency
        }
    }

    onTextMessageReceived: {
        var json = JSON.parse(message)
        //console.debug("Raw response: " + message)
        var request = json[2].request
        var response = json[2].response

        switch (json[0]) {
        case msgid.call:
            break
        case msgid.retok:
            var verb = verbs.shift()
            if (verb == "frequency_range") {
                minimumFrequency = response.min
                maximumFrequency = response.max
            } else if (verb == "frequency_step") {
                frequencyStep = response.step
            }
            break
        case msgid.event:
            var event = JSON.parse(JSON.stringify(json[2]))
            if (event.event === "radio/frequency") {
                if(scanningState == activeState) {
                    scanningFreqUpdate = true
                    frequency = event.data.value
                }
            } else if (event.event === "radio/station_found") {
                if(scanningState == activeState) {
                    scanningState = stoppedState
                    stationId = freq2str(event.data.value)
                    root.stationFound()
                }
            }
            break
        case msg.reterr:
            console.debug("Bad return value, binding probably not installed")
            break
        case MessageId.event:
            break
        }
    }

    onStatusChanged: {
        switch (status) {
        case WebSocket.Open:
            // Initialize band values now that we're connected to the
            // binding
            updateFrequencyRange(band)
            updateFrequencyStep(band)
            frequency = minimumFrequency
            sendSocketMessage("subscribe", { value: "frequency" })
            sendSocketMessage("subscribe", { value: "station_found" })
            break
        case WebSocket.Error:
            console.debug("WebSocket error: " + root.errorString)
            break
        }
    }

    function freq2str(freq) {
        if (freq > 5000000) {
            return '%1 MHz'.arg((freq / 1000000).toFixed(1))
        } else {
            return '%1 kHz'.arg((freq / 1000).toFixed(0))
        }
    }

    function sendSocketMessage(verb, parameter) {
        var requestJson = [ msgid.call, payloadLength, apiString + '/'
                           + verb, parameter ]
        //console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
        verbs.push(verb)
        sendTextMessage(JSON.stringify(requestJson))
    }

    function start() {
        sendSocketMessage("start", 'None')
        state = activeState
    }

    function stop() {
        sendSocketMessage("stop", 'None')
        state = stoppedState
    }

    function tuneUp() {
        frequency += frequencyStep
	if(frequency > maximumFrequency) {
            frequency = minimumFrequency
        }
    }

    function tuneDown() {
        frequency -= frequencyStep
	if(frequency < minimumFrequency) {
            frequency = maximumFrequency
        }
    }

    function scanUp() {
        scanningState = activeState
        sendSocketMessage("scan_start", { direction: "forward" })
    }

    function scanDown() {
        scanningState = activeState
        sendSocketMessage("scan_start", { direction: "backward" })
    }

    function updateFrequencyRange(band) {
        sendSocketMessage("frequency_range", { band: band })
    }

    function updateFrequencyStep(band) {
        sendSocketMessage("frequency_step", { band: band })
    }

	// Add function for communicating sound manager
	// This is temporary. It is not better to create function 
	// for soundmanager in this scope

    function sendSocketMessageToSoundManager(verb, parameter) {
        var requestJson = [ msgid.call, payloadLength, smApiString + '/'
                           + verb, parameter ]
        //console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
        verbs.push(verb)
        sendTextMessage(JSON.stringify(requestJson))
    }
	function connect(sourceID, sinkID) {
		var str = "{sourceID:" + sourceID + ", sinkID:" + sinkID + "}"
		var JsonArg = JSON.stringify(str)
        console.log(str)
        console.log(JsonArg)
		sendSocketMessageToSoundManager("connect", JsonArg) 	
	}
	function subscribe(event) {
		var str = "{event:" + event + "}"
		var JsonArg = JSON.stringify(str)
		sendSocketMessageToSoundManager("subscribe", JsonArg)
	}
	function unsubscribe(event) {
		var str = "{event:" + event + "}"
		var JsonArg = JSON.stringify(str)
		sendSocketMessageToSoundManager("unsubscribe", JsonArg)
	}
	function registerSource() {
		// Nothing for now
	}
}