1/**
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Control Server
19 */
20function CtrlServer() {
21    // The result returned by the request handlers
22    var result = new Object();
23
24    this.ctrlGetRadioState = function(req) {
25        print('ctrlGetRadioState');
26
27        var rsp = new Object();
28        rsp.state = gRadioState;
29        result.responseProtobuf = ctrlSchema['ril_proto.CtrlRspRadioState'].serialize(rsp);
30
31        return result;
32    }
33
34    this.ctrlSetRadioState = function(req) {
35        print('ctrlSetRadioState');
36
37        var radioReq = new Object();
38
39        // Parse the request protobuf to an object, the returned value is a
40        // string that represents the variable name
41        radioReq = ctrlSchema['ril_proto.CtrlReqRadioState'].parse(req.protobuf);
42
43        setRadioState(radioReq.state);
44
45        // Prepare the response, return the current radio state
46        var rsp = new Object();
47        rsp.state = gRadioState;
48        result.responseProtobuf = ctrlSchema['ril_proto.CtrlRspRadioState'].serialize(rsp);
49        print('gRadioState after setting: ' + gRadioState);
50        return result;
51    }
52
53    /**
54     * Process the request
55     */
56    this.process = function(req) {
57        try {
58            print('CtrlServer E: req.cmd=' + req.cmd + ' req.token=' + req.token);
59
60            // Assume the result will be true, successful and nothing to return
61            result.sendResponse = true;
62            result.ctrlStatus = CTRL_STATUS_OK;
63            result.responseProtobuf = emptyProtobuf;
64
65            // Default result will be success with no response protobuf
66            try {
67                result = (this.ctrlDispatchTable[req.cmd]).call(this, req);
68            } catch (err) {
69                print('ctrlServer: Unknown cmd=' + req.cmd);
70                result.ctrlStatus = 1; //ril_proto.CTRL_STATUS_ERR;
71            }
72
73            if (result.sendResponse) {
74                sendCtrlRequestComplete(result.ctrlStatus, req.cmd,
75                        req.token, result.responseProtobuf);
76            }
77
78            print('CtrlServer X: req.cmd=' + req.cmd + ' req.token=' + req.token);
79        } catch (err) {
80            print('CtrlServer X: Exception req.cmd=' +
81                    req.cmd + ' req.token=' + req.token + ' err=' + err);
82        }
83    }
84
85    print('CtrlServer() ctor E');
86    this.ctrlDispatchTable = new Array();
87    this.ctrlDispatchTable[CTRL_CMD_GET_RADIO_STATE] = this.ctrlGetRadioState;
88    this.ctrlDispatchTable[CTRL_CMD_SET_RADIO_STATE] = this.ctrlSetRadioState;
89    print('CtrlServer() ctor X');
90}
91
92// The control server instance and its associated Worker
93var ctrlServer = new CtrlServer();
94var ctrlWorker = new Worker(function (req) {
95    ctrlServer.process(req);
96});
97ctrlWorker.run();
98
99/**
100 * Add the request to the ctrlServer Worker.
101 */
102function onCtrlServerCmd(cmd, token, protobuf) {
103    try {
104        print('onCtrlServerCmd E cmd=' + cmd + ' token=' + token);
105
106        print('onCtrlServerCmd add the request:');
107
108
109        if (!isCtrlServerDispatchCommand(cmd)) {
110            var ctrlServerReq = new Object();
111            ctrlServerReq.cmd = cmd;
112            ctrlServerReq.token = token;
113            ctrlServerReq.protobuf = protobuf;
114            print('onCtrlServerCmd: command to control server, add to the worker queue');
115            // If it is a command to the control server, add to the control server worker queue
116            ctrlWorker.add(ctrlServerReq);
117        } else {
118            // For other commands, we need to dispatch to the corresponding components
119            try {
120                print('onCtrlServerCmd: get entry from dispatchTable cmd:' + cmd );
121                entry = ctrlServerDispatchTable[cmd];
122                if (typeof entry == 'undefined') {
123                    throw ('entry = dispatchTable[' + cmd + '] was undefined');
124                } else {
125                    var req = new Request(cmd, token, protobuf, ctrlSchema, entry.schemaName);
126                    for(i = 0; i < entry.components.length; i++) {
127                        entry.components[i].add(req);
128                    }
129                }
130            } catch (err) {
131                print('onCtrlServerCmd: Unknown cmd=' + cmd + ' err=' + err);
132                sendCtrlRequestComplete(RIL_E_REQUEST_NOT_SUPPORTED, cmd, token);
133            }
134        }
135        print('onCtrlServerCmd X cmd=' + cmd + ' token=' + token);
136    } catch (err) {
137        print('onCtrlServerCmd X Exception err=' + err);
138    }
139}
140
141function isCtrlServerDispatchCommand(cmd) {
142    return (cmd > CTRL_CMD_DISPATH_BASE)
143}
144
145/**
146 * Dispatch table for request, the control server will send those requests to
147 * the corresponding components.
148 *
149 * Each table entry is index by the CTRL_CMD_xxxx
150 * and contains an array of components this request
151 * is to be sent to and the name of the schema
152 * that converts the incoming protobuf to the
153 * appropriate request data.
154 *
155 * ctrlServerDispatchTable[CTRL_CMD_xxx].components = Array of components
156 * ctrlServerDisptachTable[CTRL_CMD_xxx].Entry.schemaName = 'Name-of-schema';
157 */
158var ctrlServerDispatchTable = new Array();
159
160ctrlServerDispatchTable[CTRL_CMD_SET_MT_CALL] = { // 1001
161    'components' : [simulatedRadioWorker],
162    'schemaName' : 'CtrlReqSetMTCall',
163};
164ctrlServerDispatchTable[CTRL_CMD_HANGUP_CONN_REMOTE] = { // 1002
165    'components' : [simulatedRadioWorker],
166    'schemaName' : 'CtrlHangupConnRemote',
167};
168ctrlServerDispatchTable[CTRL_CMD_SET_CALL_TRANSITION_FLAG] = { // 1003
169    'components' : [simulatedRadioWorker],
170    'schemaName' : 'CtrlSetCallTransitionFlag',
171};
172ctrlServerDispatchTable[CTRL_CMD_SET_CALL_ALERT] = { // 1004
173    'components' : [simulatedRadioWorker],
174};
175ctrlServerDispatchTable[CTRL_CMD_SET_CALL_ACTIVE] = { // 1005
176    'components' : [simulatedRadioWorker],
177};
178ctrlServerDispatchTable[CTRL_CMD_ADD_DIALING_CALL] = { // 1006
179    'components' : [simulatedRadioWorker],
180    'schemaName' : 'CtrlReqAddDialingCall',
181};
182
183/**
184 * Optional tests
185 */
186if (false) {
187    include("ctrl_server_tests.js");
188}
189