Home | History | Annotate | Download | only in vehiclehal
      1 /*
      2  * Copyright (C) 2017 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 package com.android.car.vehiclehal;
     18 
     19 import static android.hardware.automotive.vehicle.V2_0.VehicleProperty.OBD2_FREEZE_FRAME;
     20 import static android.hardware.automotive.vehicle.V2_0.VehicleProperty.OBD2_LIVE_FRAME;
     21 
     22 import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
     23 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
     24 import android.util.JsonReader;
     25 import java.io.IOException;
     26 
     27 public class DiagnosticJsonReader {
     28     public static final String FRAME_TYPE_LIVE = "live";
     29     public static final String FRAME_TYPE_FREEZE = "freeze";
     30 
     31     private final DiagnosticEventBuilder mLiveFrameBuilder;
     32     private final DiagnosticEventBuilder mFreezeFrameBuilder;
     33 
     34     public DiagnosticJsonReader(VehiclePropConfig liveConfig, VehiclePropConfig freezeConfig) {
     35         mLiveFrameBuilder =
     36                 new DiagnosticEventBuilder(
     37                         OBD2_LIVE_FRAME,
     38                         liveConfig.configArray.get(0),
     39                         liveConfig.configArray.get(1));
     40         mFreezeFrameBuilder =
     41                 new DiagnosticEventBuilder(
     42                         OBD2_FREEZE_FRAME,
     43                         freezeConfig.configArray.get(0),
     44                         freezeConfig.configArray.get(1));
     45     }
     46 
     47     public DiagnosticJsonReader() {
     48         mLiveFrameBuilder = new DiagnosticEventBuilder(OBD2_LIVE_FRAME);
     49         mFreezeFrameBuilder = new DiagnosticEventBuilder(OBD2_FREEZE_FRAME);
     50     }
     51 
     52     public VehiclePropValue build(JsonReader jsonReader) throws IOException {
     53         DiagnosticJson diagnosticJson = DiagnosticJson.build(jsonReader);
     54         switch (diagnosticJson.type) {
     55             case FRAME_TYPE_LIVE:
     56                 return diagnosticJson.build(mLiveFrameBuilder);
     57             case FRAME_TYPE_FREEZE:
     58                 return diagnosticJson.build(mFreezeFrameBuilder);
     59             default:
     60                 return null;
     61         }
     62     }
     63 }
     64