10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ciimport rpc from '@ohos.rpc';
170704ebd2Sopenharmony_ciimport ApiMessage from '../common/apiMessage.ets';
180704ebd2Sopenharmony_ciimport ApiResult from '../common/apiResult.ets';
190704ebd2Sopenharmony_ciimport ReflectCallApi from './ReflectCallApi.ets';
200704ebd2Sopenharmony_ciimport deviceinfo from '@ohos.deviceInfo'
210704ebd2Sopenharmony_ci
220704ebd2Sopenharmony_cilet CODE_INVOKE = 1;
230704ebd2Sopenharmony_cilet CODE_INVOKE_TESTCASE = 99;
240704ebd2Sopenharmony_cilet CODE_INVOKE_OSVERSION = 88;
250704ebd2Sopenharmony_cilet logTag = "[RpcServer:  ]";
260704ebd2Sopenharmony_ci
270704ebd2Sopenharmony_ciexport default class Stub extends rpc.RemoteObject {
280704ebd2Sopenharmony_ci    constructor(descriptor) {
290704ebd2Sopenharmony_ci        console.log(logTag +" Stub Create");
300704ebd2Sopenharmony_ci        super(descriptor);
310704ebd2Sopenharmony_ci    }
320704ebd2Sopenharmony_ci
330704ebd2Sopenharmony_ci    onRemoteMessageRequest(code, data, reply, option) {
340704ebd2Sopenharmony_ci        try {
350704ebd2Sopenharmony_ci            console.log(logTag +' ===================  onRemoteMessageRequest: code is " + code + "  ===================');
360704ebd2Sopenharmony_ci            switch (code) {
370704ebd2Sopenharmony_ci              case CODE_INVOKE:
380704ebd2Sopenharmony_ci              {
390704ebd2Sopenharmony_ci                console.info(logTag +' case CODE_INVOKE start');
400704ebd2Sopenharmony_ci                let testBundle = new ApiMessage(null, null, null, null, null, null, null);
410704ebd2Sopenharmony_ci                data.readParcelable(testBundle);
420704ebd2Sopenharmony_ci
430704ebd2Sopenharmony_ci                let resultCall = new ApiResult(0,null);
440704ebd2Sopenharmony_ci                var resCallApi = -1;
450704ebd2Sopenharmony_ci                const reflectCallApi = new ReflectCallApi();
460704ebd2Sopenharmony_ci                resCallApi = reflectCallApi.call(testBundle);
470704ebd2Sopenharmony_ci                console.log(logTag + '_methodName  call success,result is ' + resCallApi);
480704ebd2Sopenharmony_ci                if (resCallApi === 1)  {
490704ebd2Sopenharmony_ci                    resultCall._resultCode = 1;
500704ebd2Sopenharmony_ci                    resultCall._result = 1;
510704ebd2Sopenharmony_ci                } else {
520704ebd2Sopenharmony_ci                    resultCall._resultCode = -1;
530704ebd2Sopenharmony_ci                    resultCall._result = -1;
540704ebd2Sopenharmony_ci                } 
550704ebd2Sopenharmony_ci                console.log(logTag + "The server's writeSequenceable result is ' + JSON.stringify(resultCall)");
560704ebd2Sopenharmony_ci
570704ebd2Sopenharmony_ci                testBundle._apiResult=JSON.stringify(resultCall);
580704ebd2Sopenharmony_ci                console.log(logTag + ' The testBundle is ' + JSON.stringify(testBundle));
590704ebd2Sopenharmony_ci                reply.writeParcelable(testBundle);
600704ebd2Sopenharmony_ci
610704ebd2Sopenharmony_ci                return true;
620704ebd2Sopenharmony_ci              }
630704ebd2Sopenharmony_ci              case CODE_INVOKE_TESTCASE:
640704ebd2Sopenharmony_ci              {
650704ebd2Sopenharmony_ci                console.info(logTag +' ===================  case CODE_INVOKE_TESTCASE start ===================  ');
660704ebd2Sopenharmony_ci                let testBundle = new ApiMessage(null, null, null, null, null, null, null);
670704ebd2Sopenharmony_ci                data.readSequenceable(testBundle);
680704ebd2Sopenharmony_ci
690704ebd2Sopenharmony_ci                let resultCall = new ApiResult(0,null);
700704ebd2Sopenharmony_ci                resultCall._resultCode = 1;
710704ebd2Sopenharmony_ci                resultCall._result = 1;
720704ebd2Sopenharmony_ci                testBundle._apiResult=JSON.stringify(resultCall);
730704ebd2Sopenharmony_ci                reply.writeParcelable(testBundle);
740704ebd2Sopenharmony_ci                return true;
750704ebd2Sopenharmony_ci              }
760704ebd2Sopenharmony_ci              case CODE_INVOKE_OSVERSION:
770704ebd2Sopenharmony_ci              {
780704ebd2Sopenharmony_ci                console.info(logTag +' ===================  case CODE_INVOKE_OSVERSION start ===================  ');
790704ebd2Sopenharmony_ci                data.readString();
800704ebd2Sopenharmony_ci                let osFullNameInfo = deviceinfo.osFullName;
810704ebd2Sopenharmony_ci                console.info('the value of the deviceinfo osFullName is :' + osFullNameInfo);
820704ebd2Sopenharmony_ci                reply.writeString(osFullNameInfo);
830704ebd2Sopenharmony_ci                return true;
840704ebd2Sopenharmony_ci              }
850704ebd2Sopenharmony_ci              default:
860704ebd2Sopenharmony_ci                console.error(logTag +' default case ' + code);
870704ebd2Sopenharmony_ci                return super.onRemoteMessageRequest(code, data, reply, option);
880704ebd2Sopenharmony_ci            }
890704ebd2Sopenharmony_ci        } catch (error) {
900704ebd2Sopenharmony_ci            console.log(logTag +'ERROR: onRemoteMessageRequest: ' + error.code + error.message);
910704ebd2Sopenharmony_ci        }
920704ebd2Sopenharmony_ci        return false;
930704ebd2Sopenharmony_ci    }
940704ebd2Sopenharmony_ci}
95