My Leaf, null safety and new features

April 2021 ยท 3 minute read

Time for an update on My Leaf!

Null safety

This time it’s all about null safety! For users of My Leaf this update has little value… but for me as a developer it is an big update! As you may know the 2.12 release of Dart introduced sound null safety. What this means is that Dart at compile time (and at runtime with the new late keyword) checks for null values. This means you now have to declare whether a variable can be nullable or not. This changes a lot and it make you think twice about nullable declarations. It sounds simple but it definitely makes you think about null values and the rational behind having nullable declarations and variables. At first it seems like a hassle but it definitely makes for safer code and programming in the end.

How does this affect My Leaf? Well… it affects My Leaf and all of its dependencies. Luckily the third party dependencies My Leaf uses all have converted to null safety; besides one library I used for in-app purchases. I now use the officially supported package for in-app purchases. My Leaf and its components/libraries are all now converted to null safety. I will release a new version of My Leaf soon! There will be no visible changes for existing users but lots of changes under the hood!

I invite you to peek at the source code if you desire so!

Next up

The next big thing is to do some maintenance of My Leaf! There is room for improvements in both readability and the overall quality of code + some extra features!

New features

I will also be adding support for specific vehicle features to the dartnissanconnect library. These will be incorporated into My Leaf down the line. This means stuff like sounding the horn or turning on lights on vehicles will be possible. This is only for vehicles produced after May 2019.

A new tidbit is the new Services class with which you can query a vehicle for a specific service and see if it’s available.

You can use an instance of the NissanConnectVehicle class to query for a service like so;

var hasStatusCheck = vehicle.hasService(Services.VEHICLE_STATUS_CHECK);

For features like locking your vehicle something called a srp (token?) is needed. I haven’t reverse engineered this part of the API yet. If anyone reading this has any insight then please contact me!

The srp “implementation” inside dartnissanconnect currently;

  // Needs implementation
  _initiateSrp() async {
    var userId = await _requestUserId();

    // salt = 20 hex chars, verifier = 512 hex chars
    var salt = '0' * 20;
    var verifier = 'ABCDEFGH' * 50;

    var response = await session.requestWithRetry(
        endpoint:
            '${session.settings['EU']['car_adapter_base_url']}v1/cars/$vin/actions/srp-initiates',
        params: {
          'data': {
            'type': 'SrpInitiates',
            'attributes': {
              's': salt,
              'i': userId,
              'v': verifier,
            }
          }
        });

    return response.body;
  }

  // Needs implementation
  _validateSrp() async {
    var userId = await _requestUserId();

    // 512 hex chars
    var a = '';

    var response = await session.requestWithRetry(
        endpoint:
            '${session.settings['EU']['car_adapter_base_url']}v1/cars/$vin/actions/srp-sets',
        params: {
          'data': {
            'type': 'SrpSets',
            'attributes': {
              'i': userId,
              'a': a,
            }
          }
        });

    return response.body;
  }

This current skeleton implementation is borrowed from Richard Mitchell’s kamereon-python library. We have cannibalized on each others libraries ;)

Links and references;