summaryrefslogtreecommitdiffstats
path: root/mock-ril/src/cpp/experiments.cpp
blob: 0bb6b98f95114d5787af8269c1bba1299e7ffb8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
 * Copyright (C) 2010 The Android Open Source Project
 *
 * 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.
 */

#include <v8.h>
#include "ril.h"

#include "logging.h"
#include "status.h"
#include "worker.h"
#include "util.h"

#include "hardware/ril/mock-ril/src/proto/ril.pb.h"

#include "logging.h"
#include "js_support.h"
#include "node_buffer.h"
#include "node_util.h"
#include "protobuf_v8.h"
#include "requests.h"

#include "experiments.h"

void testStlPort() {
    // Test using STLport
    std::queue<int *> q;
    int data[] = {1, 2, 3};

    int *param = data;
    ALOGD("before push q.size=%d", q.size());
    q.push(param);
    ALOGD("after push q.size=%d", q.size());
    void *p = q.front();
    if (p == param) {
        ALOGD("q.push succeeded");
    } else {
        ALOGD("q.push failed");
    }
    q.pop();
    ALOGD("after pop q.size=%d", q.size());
}

v8::Handle<v8::Value> GetReqScreenState(v8::Local<v8::String> property,
                               const v8::AccessorInfo &info) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap =
            v8::Local<v8::External>::Cast(self->GetInternalField(0));
    void *p = wrap->Value();
    int state = static_cast<int *>(p)[0];
    ALOGD("GetReqScreenState state=%d", state);
    return v8::Integer::New(state);
}

bool callOnRilRequest(v8::Handle<v8::Context> context, int request,
                   void *data, size_t datalen, RIL_Token t) {
    v8::HandleScope handle_scope;
    v8::TryCatch try_catch;

    // Get the onRilRequestFunction, making sure its a function
    v8::Handle<v8::String> name = v8::String::New("onRilRequest");
    v8::Handle<v8::Value> onRilRequestFunctionValue = context->Global()->Get(name);
    if(!onRilRequestFunctionValue->IsFunction()) {
        // Wasn't a function
        ALOGD("callOnRilRequest X wasn't a function");
        return false;
    }
    v8::Handle<v8::Function> onRilRequestFunction =
        v8::Handle<v8::Function>::Cast(onRilRequestFunctionValue);

    // Create the request
    v8::Handle<v8::Value> v8RequestValue = v8::Number::New(request);

    // Create the parameter for the request
    v8::Handle<v8::Object> params_obj =
            v8::ObjectTemplate::New()->NewInstance();
    switch(request) {
        case(RIL_REQUEST_SCREEN_STATE): {
            ALOGD("callOnRilRequest RIL_REQUEST_SCREEN_STATE");
            if (datalen < sizeof(int)) {
                ALOGD("callOnRilRequest err size < sizeof int");
            } else {
                v8::Handle<v8::ObjectTemplate> params_obj_template =
                        v8::ObjectTemplate::New();
                params_obj_template->SetInternalFieldCount(1);
                params_obj_template->SetAccessor(v8::String::New(
                            "ReqScreenState"), GetReqScreenState, NULL);
                // How to not leak this pointer!!!
                int *p = new int;
                *p = ((int *)data)[0];
                params_obj = params_obj_template->NewInstance();
                params_obj->SetInternalField(0, v8::External::New(p));
            }
            break;
        }
        default: {
            ALOGD("callOnRilRequest X unknown request");
            break;
        }
    }

    // Invoke onRilRequest
    bool retValue;
    const int argc = 2;
    v8::Handle<v8::Value> argv[argc] = { v8RequestValue, params_obj };
    v8::Handle<v8::Value> result =
        onRilRequestFunction->Call(context->Global(), argc, argv);
    if (try_catch.HasCaught()) {
        ALOGD("callOnRilRequest error");
        ReportException(&try_catch);
        retValue = false;
    } else {
        v8::String::Utf8Value result_string(result);
        ALOGD("callOnRilRequest result=%s", ToCString(result_string));
        retValue = true;
    }
    return retValue;
}

void testOnRilRequestUsingCppRequestObjs(v8::Handle<v8::Context> context) {
    ALOGD("testOnRilRequestUsingCppRequestObjs E:");
    v8::HandleScope handle_scope;

    v8::TryCatch try_catch;
    try_catch.SetVerbose(true);

    runJs(context, &try_catch, "local-string",
        "function onRilRequest(reqNum, params) {\n"
        "  print(\"reqNum=\" + reqNum);\n"
        "  if (reqNum == 61) {\n"
        "      print(\"params.ReqScreenState=\" + params.ReqScreenState);\n"
        "  }\n"
        "  return \"Hello World\";\n"
        "}\n");
    if (!try_catch.HasCaught()) {
        // Call the onRilRequest function
        int data[1] = { 0 };
        callOnRilRequest(context, RIL_REQUEST_SCREEN_STATE, data,
                sizeof(data), NULL);
    }
    ALOGD("testOnRilRequestUsingCppRequestObjs X:");
}

void testReqScreenStateProtobuf() {
    v8::HandleScope handle_scope;
    v8::TryCatch try_catch;

    ALOGD("testReqScreenStateProtobuf E");

    ALOGD("create ReqScreenState");
    ril_proto::ReqScreenState* ss = new ril_proto::ReqScreenState();
    ss->set_state(true);
    bool state = ss->state();
    ALOGD("state=%d", state);
    ss->set_state(false);
    state = ss->state();
    ALOGD("state=%d", state);
    int len = ss->ByteSize();
    ALOGD("create buffer len=%d", len);
    char *buffer = new char[len];
    ALOGD("serialize");
    bool ok = ss->SerializeToArray(buffer, len);
    if (!ok) {
        ALOGD("testReqScreenStateProtobuf X: Could not serialize ss");
        return;
    }
    ALOGD("ReqScreenState serialized ok");
    ril_proto::ReqScreenState *newSs = new ril_proto::ReqScreenState();
    ok = newSs->ParseFromArray(buffer, len);
    if (!ok) {
        ALOGD("testReqScreenStateProtobuf X: Could not deserialize ss");
        return;
    }
    ALOGD("newSs->state=%d", newSs->state());

    delete [] buffer;
    delete ss;
    delete newSs;
    ALOGD("testReqScreenStateProtobuf X");
}

void testReqHangUpProtobuf() {
    v8::HandleScope handle_scope;
    v8::TryCatch try_catch;

    ALOGD("testReqHangUpProtobuf E");

    ALOGD("create ReqHangUp");
    ril_proto::ReqHangUp* hu = new ril_proto::ReqHangUp();
    hu->set_connection_index(3);
    bool connection_index = hu->connection_index();
    ALOGD("connection_index=%d", connection_index);
    hu->set_connection_index(2);
    connection_index = hu->connection_index();
    ALOGD("connection_index=%d", connection_index);
    ALOGD("create buffer");
    int len = hu->ByteSize();
    char *buffer = new char[len];
    ALOGD("serialize");
    bool ok = hu->SerializeToArray(buffer, len);
    if (!ok) {
        ALOGD("testReqHangUpProtobuf X: Could not serialize hu");
        return;
    }
    ALOGD("ReqHangUp serialized ok");
    ril_proto::ReqHangUp *newHu = new ril_proto::ReqHangUp();
    ok = newHu->ParseFromArray(buffer, len);
    if (!ok) {
        ALOGD("testReqHangUpProtobuf X: Could not deserialize hu");
        return;
    }
    ALOGD("newHu->connection_index=%d", newHu->connection_index());

    delete [] buffer;
    delete hu;
    delete newHu;
    ALOGD("testReqHangUpProtobuf X");
}

void testProtobufV8(v8::Handle<v8::Context> context) {
    ALOGD("testProtobufV8 E:");
    v8::HandleScope handle_scope;

    v8::TryCatch try_catch;
    try_catch.SetVerbose(true);

    if (try_catch.HasCaught()) {
        ALOGD("TryCatch.hasCaught is true after protobuf_v8::init");
        ReportException(&try_catch);
    }
    runJs(context, &try_catch, "local-string",
        "fileContents = readFileToString('mock_ril.js');\n"
        "print('fileContents:\\n' + fileContents);\n"
        "\n"
        "buffer = readFileToBuffer('ril.desc');\n"
        "var schema = new Schema(buffer);\n"
        "\n"
        "var originalReqEnterSimPin = { pin : 'hello-the-pin' };\n"
        "print('originalReqEnterSimPin: pin=' + originalReqEnterSimPin.pin);\n"
        "var ReqEnterSimPinSchema = schema['ril_proto.ReqEnterSimPin'];\n"
        "serializedOriginalReqEnterSimPin = ReqEnterSimPinSchema.serialize(originalReqEnterSimPin);\n"
        "print('serializedOriginalReqEnterSimPin.length=' + serializedOriginalReqEnterSimPin.length);\n"
        "newReqEnterSimPin = ReqEnterSimPinSchema.parse(serializedOriginalReqEnterSimPin);\n"
        "print('newReqEnterSimPin: pin=' + newReqEnterSimPin.pin);\n"
        "\n"
        "var originalReqScreenState = { state : true };\n"
        "print('originalReqScreenState: state=' + originalReqScreenState.state);\n"
        "var ReqScreenStateSchema = schema['ril_proto.ReqScreenState'];\n"
        "var serializedOriginalReqScreenState = ReqScreenStateSchema.serialize(originalReqScreenState);\n"
        "print('serializedOriginalReqScreenState.length=' + serializedOriginalReqScreenState.length);\n"
        "var newReqScreenState = ReqScreenStateSchema.parse(serializedOriginalReqScreenState);\n"
        "print('newReqScreenState: state=' + newReqScreenState.state);\n"
        "\n"
        "originalReqScreenState.state = false;\n"
        "print('originalReqScreenState: state=' + originalReqScreenState.state);\n"
        "serializedOriginalReqScreenState = ReqScreenStateSchema.serialize(originalReqScreenState);\n"
        "print('serializedOriginalReqScreenState.length=' + serializedOriginalReqScreenState.length);\n"
        "newReqScreenState = ReqScreenStateSchema.parse(serializedOriginalReqScreenState);\n"
        "print('newReqScreenState: state=' + newReqScreenState.state);\n");
    ALOGD("testProtobufV8 X");
}

void experiments(v8::Handle<v8::Context> context) {
    ALOGD("experiments E: ********");
    testStlPort();
    testReqScreenStateProtobuf();
    testOnRilRequestUsingCppRequestObjs(context);
    testProtobufV8(context);
    ALOGD("experiments X: ********\n");
}