/*
 * Copyright (c) 2021, Erich Styger
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "platform.h"
#if PL_CONFIG_USE_IDENTIFY
#include "esp32_mac.h"
#include "esp_system.h"
#include "Identify.h"
#include "McuUtility.h"
#include "McuXFormat.h"
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

static const ESP32_Device_t ESP32_devices[] =
{  /* registered at https://eeeportal.hslu.ch */
    {.hostName="UnknownRobot", .robotID=ID_ROBOT_UNKNOWN,  .macStr="00:00:00:00:00:00", .eee_id="eee-0000", .eee_pwd="deadbeef"}, /* fallback entry */
    {.hostName="ADISRobotE0",  .robotID=ID_ROBOT_E0,  .macStr="d8:a0:1d:42:e2:08", .eee_id="eee-01367", .eee_pwd="xq4vZdu3eWLWcSGDuYwU"},
    {.hostName="ADISRobotE1",  .robotID=ID_ROBOT_E1,  .macStr="d8:a0:1d:42:ec:a4", .eee_id="eee-01023", .eee_pwd="i24Z3W5VtDFSkdNtk93f"},
    {.hostName="ADISRobotE2",  .robotID=ID_ROBOT_E2,  .macStr="d8:a0:1d:42:eb:c4", .eee_id="eee-01381", .eee_pwd="DcUYXx0saffo5ST8TihG"},
    {.hostName="ADISRobotE3",  .robotID=ID_ROBOT_E3,  .macStr="d8:a0:1d:42:e6:98", .eee_id="eee-01407", .eee_pwd="OjhpZJrXUPWAC3mR0odX"},
    {.hostName="ADISRobotE4",  .robotID=ID_ROBOT_E4,  .macStr="d8:a0:1d:42:e4:48", .eee_id="eee-01369", .eee_pwd="vDk13U6MzxkwGO879TUy"},
    {.hostName="ADISRobotE5",  .robotID=ID_ROBOT_E5,  .macStr="d8:a0:1d:42:de:9c", .eee_id="eee-01370", .eee_pwd="jCGeKTeWVrcncJO8u73M"},
    {.hostName="ADISRobotE6",  .robotID=ID_ROBOT_E6,  .macStr="d8:a0:1d:42:ec:40", .eee_id="eee-01382", .eee_pwd="fJjX12cWv4U0XfaJ46N0"},
    {.hostName="ADISRobotE7",  .robotID=ID_ROBOT_E7,  .macStr="d8:a0:1d:42:df:14", .eee_id="eee-01387", .eee_pwd="TEkK2FL5Yc8A4dcsf7sk"},
    {.hostName="ADISRobotE8",  .robotID=ID_ROBOT_E8,  .macStr="d8:a0:1d:42:ec:74", .eee_id="eee-01371", .eee_pwd="aaxQfxvPnCMVLvCZ3ays"},
    {.hostName="ADISRobotE9",  .robotID=ID_ROBOT_E9,  .macStr="d8:a0:1d:42:e4:64", .eee_id="eee-01446", .eee_pwd="4dZNLrMCZqsQbsNKTQSL"},
    {.hostName="ADISRobotE10", .robotID=ID_ROBOT_E10, .macStr="d8:a0:1d:42:e6:f8", .eee_id="eee-01383", .eee_pwd="KEsv8fendMWx1nHkdQUj"},
    {.hostName="ADISRobotE11", .robotID=ID_ROBOT_E11, .macStr="d8:a0:1d:42:e0:24", .eee_id="eee-01609", .eee_pwd="WeWLiT7eH01Jqrt4DAfi"}, // issue with EEE!
    {.hostName="ADISRobotE12", .robotID=ID_ROBOT_E12, .macStr="d8:a0:1d:42:df:d4", .eee_id="eee-01393", .eee_pwd="HtcKHGmaMUwwJgqh7Hao"},
    {.hostName="ADISRobotE13", .robotID=ID_ROBOT_E13, .macStr="d8:a0:1d:42:e5:88", .eee_id="eee-01384", .eee_pwd="q320iZDcc6bFPvXYzRxF"},
    {.hostName="ADISRobotE14", .robotID=ID_ROBOT_E14, .macStr="50:02:91:9c:0d:ac", .eee_id="eee-01635", .eee_pwd="V4V6LdNW7WJUnv4zbRKm"},
    {.hostName="ADISRobotE17", .robotID=ID_ROBOT_E17, .macStr="d8:a0:1d:42:e5:98", .eee_id="eee-00535", .eee_pwd="fXB5kVK6qS1khJ7qybYD"},
    {.hostName="ADISRobotE18", .robotID=ID_ROBOT_E18, .macStr="d8:a0:1d:42:e3:b8", .eee_id="eee-01390", .eee_pwd="W4KuRQERUMwHquUHwVot"},

    {.hostName="ADISRobotE27", .robotID=ID_ROBOT_E27, .macStr="d8:a0:1d:42:df:e0", .eee_id="eee-01591", .eee_pwd="28o6wxyGLXmQ9DnUHw1P"},

    {.hostName="ADISRobotL1",  .robotID=ID_ROBOT_L1,  .macStr="d8:a0:1d:42:e5:d0", .eee_id="eee-01372", .eee_pwd="zwtshA1tnqXxYqrt1mgo"},
    {.hostName="ADISRobotL3",  .robotID=ID_ROBOT_L1,  .macStr="50:02:91:a0:f8:ac", .eee_id="eee-01388", .eee_pwd="jhw1KSG1piDP7zthNUDt"},
    {.hostName="ADISRobotL17", .robotID=ID_ROBOT_L17, .macStr="d8:a0:1d:42:ed:24", .eee_id="eee-01373", .eee_pwd="XPiSwZ6Vr6Y5Y3ukvq5a"},
    {.hostName="ADISRobotL20", .robotID=ID_ROBOT_L20, .macStr="d8:a0:1d:42:df:94", .eee_id="eee-01374", .eee_pwd="88kY6j6nurJZpqt2gMMF"},

    {.hostName="ADISRobotR8",  .robotID=ID_ROBOT_R8,  .macStr="d8:a0:1d:42:ed:50", .eee_id="eee-01368", .eee_pwd="xaA8qck6edMS3h2tsHun"},
    {.hostName="ADISRobotR9",  .robotID=ID_ROBOT_R9,  .macStr="d8:a0:1d:42:e3:d8", .eee_id="eee-01392", .eee_pwd="NxJZeTDUpGGXC0L1QxX9"},
    {.hostName="ADISRobotR23", .robotID=ID_ROBOT_R23, .macStr="d8:a0:1d:42:e2:b8", .eee_id="eee-01385", .eee_pwd="deOkgVLPBoh6BQXH3Awf"},
    {.hostName="ADISRobotR27", .robotID=ID_ROBOT_R27, .macStr="50:02:91:a1:0f:78", .eee_id="eee-01375", .eee_pwd="7O4HCSqbWVW0zCvfhyA2"},
    {.hostName="ADISRobotR28", .robotID=ID_ROBOT_R28, .macStr="d8:a0:1d:42:e0:44", .eee_id="eee-01386", .eee_pwd="mzyEvEfx0BvgYTOgF4F2"},
    {.hostName="ADISRobotR29", .robotID=ID_ROBOT_R29, .macStr="d8:a0:1d:42:ec:a8", .eee_id="eee-01410", .eee_pwd="c1nbzEkaEmXz9rEeBEfC"},
    {.hostName="ADISRobotR32", .robotID=ID_ROBOT_R32, .macStr="d8:a0:1d:42:e0:24", .eee_id="eee-01376", .eee_pwd="LWWEBe2Au4Hy472W0pbA"},
    {.hostName="ADISRobotR33", .robotID=ID_ROBOT_R33, .macStr="50:02:91:9f:8e:28", .eee_id="eee-01405", .eee_pwd="iWv6FaKso2ARMsPWpdkv"},
    {.hostName="ADISRobotR34", .robotID=ID_ROBOT_R34, .macStr="50:02:91:9f:8d:dc", .eee_id="eee-01592", .eee_pwd="kmCgbmQjuxubnOAKu5Jo"},
    {.hostName="ADISRobotR36", .robotID=ID_ROBOT_R36, .macStr="d8:a0:1d:42:e3:ac", .eee_id="eee-01406", .eee_pwd="gq3rkAs5VOuSgnFOJ3f5"},
    {.hostName="ADISRobotR37", .robotID=ID_ROBOT_R37, .macStr="d8:a0:1d:42:ec:2c", .eee_id="eee-01377", .eee_pwd="d2GVw71vruR9baZA4EdE"},
    {.hostName="ADISRobotR44", .robotID=ID_ROBOT_R44, .macStr="d8:a0:1d:42:e9:98", .eee_id="eee-01394", .eee_pwd="3gG3dQTyjjsteuv18EY9"},

    {.hostName="ADISRobotR45", .robotID=ID_ROBOT_R45, .macStr="d8:a0:1d:42:e3:bc", .eee_id="eee-01417", .eee_pwd="zD5zCmXSo18qSMq55Lvs"},

    /* ESP32 modules with no robot! */
    {.hostName="ADISRobotE34", .robotID=ID_ROBOT_NONE, .macStr="50:02:91:a0:f8:18", .eee_id="eee-01389", .eee_pwd="yehFaZWK9kKSULwLyPyR"},

	/* ESP32 breakout modules */
    {.hostName="ESP32BRKOUT01", .robotID=ID_ROBOT_NONE, .macStr="d8:a0:1d:62:93:40", .eee_id="eee-01408", .eee_pwd="Gu03RtUCsrE99PafZCTo"},
    {.hostName="ESP32DEVKIT02", .robotID=ID_ROBOT_NONE, .macStr="24:0a:c4:13:8e:30", .eee_id="eee-01411", .eee_pwd="6kagmMLOR3tDiRx6GfXm"},
};

void ESP32_MacToString(uint8_t mac[6], uint8_t *buf, size_t bufSize) {
  McuXFormat_xsnprintf((char*)buf, bufSize, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

void ESP32_MacRead(uint8_t mac[6]) {
  ESP_ERROR_CHECK(esp_read_mac(&mac[0], ESP_MAC_WIFI_STA));
}

bool ESP32_MacAreSame(const uint8_t macA[6], const uint8_t macB[6]) {
  for(int j=0; j<6; j++) {
    if(macA[j] != macB[j]) {
      return false; /* no match */
    }
  }
  return true; /* match! */
}

const ESP32_Device_t *ESP32_MacStrGetDevice(const char *macStr) {
  int i;

  for(i=0; i<sizeof(ESP32_devices)/sizeof(ESP32_devices[0]); i++) {
    if (McuUtility_strcmp(macStr, ESP32_devices[i].macStr)==0) {
      return &ESP32_devices[i];
    }
  }
  return &ESP32_devices[0]; /* unknown robot entry */
}

const ESP32_Device_t *ESP32_GetDeviceConfig(void) {
  uint8_t mac[6];
  uint8_t macStr[sizeof("aa:bb:cc:dd:ee:ff")];

  ESP32_MacRead(mac);
  ESP32_MacToString(mac, macStr, sizeof(macStr));
  return ESP32_MacStrGetDevice((char*)macStr);
}

void ESP32_MacInit(void) {
  /* nothing needed */
}

#endif /* PL_CONFIG_USE_IDENTIFY */
