<commit_before>/*
 * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
 *
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * This file is designed to be parsed during the build process
 *
 * Contains ESP8266 board specific functions.
 * ----------------------------------------------------------------------------
 */
// Because the ESP8266 JS wrapper is assured to be running on an ESP8266 we
// can assume that inclusion of ESP8266 headers will be acceptable.
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
#include <osapi.h>
#include <ping.h>
#include <espconn.h>
#include <espmissingincludes.h>
#include <uart.h>

#define ESP8266_ON_ACCESS_POINTS "#accessPoints"

#define _GCC_WRAP_STDINT_H
typedef long long int64_t;

#include "jswrap_esp8266.h"
#include "jsinteractive.h" // Pull inn the jsiConsolePrint function
#include "network.h"
#include "network_esp8266.h"
#include "jswrap_net.h"

// Forward declaration of functions.
static void scanCB(void *arg, STATUS status);
static void wifiEventHandler(System_Event_t *event);
static void ipAddrToString(struct ip_addr addr, char *string);
static char *nullTerminateString(char *target, char *source, int sourceLength);
static void setupJsNetwork();
static void pingRecvCB();
static char *wifiConnectStatusToString(uint8 status);


// #NOTE: For callback functions, be sure and unlock them in the `kill` handler.

// A callback function to be invoked when we find a new access point.
static JsVar *g_jsScanCallback;

// A callback function to be invoked when we receive a WiFi event.
static JsVar *g_jsWiFiEventCallback;

// A callback function to be invoked when we have an IP address.
static JsVar *g_jsGotIpCallback;

// A callback function to be invoked on ping responses.
static JsVar *g_jsPingCallback;

// Global data structure for ping request
static struct ping_option pingOpt;

// Reasons for which a connection failed
uint8_t wifiReason = 0;
static char *wifiReasons[] = {
  "", "unspecified", "auth_expire", "auth_leave", "assoc_expire", "assoc_toomany", "not_authed",
  "not_assoced", "assoc_leave", "assoc_not_authed", "disassoc_pwrcap_bad", "disassoc_supchan_bad",
  "ie_invalid", "mic_failure", "4way_handshake_timeout", "group_key_update_timeout",
  "ie_in_4way_differs", "group_cipher_invalid", "pairwise_cipher_invalid", "akmp_invalid",
  "unsupp_rsn_ie_version", "invalid_rsn_ie_cap", "802_1x_auth_failed", "cipher_suite_rejected",
  "beacon_timeout", "no_ap_found" };

static char *wifiGetReason(void) {
  if (wifiReason <= 24) return wifiReasons[wifiReason];
  if (wifiReason >= 200 && wifiReason <= 201) return wifiReasons[wifiReason-200+24];
  return wifiReasons[1];
}

static char *wifiMode[] = { 0, "STA", "AP", "AP+STA" };
static char *wifiPhy[]  = { 0, "11b", "11g", "11n" };

// Let's define the JavaScript class that will contain our `world()` method. We'll call it `Hello`
/*JSON{
  "type"  : "class",
  "class" : "ESP8266WiFi"
}*/

/*JSON{
    "type":     "kill",
    "generate": "jswrap_ESP8266WiFi_kill"
}
*
* Register a handler to be called when the ESP8266 is reset.
*/
void jswrap_ESP8266WiFi_kill() {
  // When the Espruino environment is reset, this callback function will be invoked.
  // The purpose is to reset the environment by cleaning up whatever might be needed
  // to be cleaned up.
  os_printf("> jswrap_esp8266_kill\n");

  // Handle the g_jsGotIpCallback - If it is not null, then it contains a reference
  // to a locked JS variable that is a function pointer for the "got ip" callback.
  // We want to unlock this and delete the reference.
  if (g_jsGotIpCallback != NULL) {
    jsvUnLock(g_jsGotIpCallback);
    g_jsGotIpCallback = NULL;
  }

  // Handle g_jsPingCallback release.
  if (g_jsPingCallback != NULL) {
    jsvUnlock(g_jsPingCallback);
    g_jsPingCallback = NULL;
  }

  // Handle g_jsWiFiEventCallback
  if (g_jsWiFiEventCallback != NULL) {
    jsvUnLock(g_jsWiFiEventCallback);
    g_jsWiFiEventCallback = NULL;
  }

  // Handle g_jsWiFiEventCallback
  if (g_jsScanCallback != NULL) {
    jsvUnlock(g_jsScanCallback);
    g_jsScanCallback = NULL;
  }

  os_printf("< jswrap_esp8266_kill\n");
}

/*JSON{
  "type"     : "staticmethod",
  "class"    : "ESP8266WiFi",
  "name"     : "connect",
  "generate" : "jswrap_ESP8266WiFi_connect",
  "params"   : [
    ["ssid","JsVar","The network id of the access point."],
    ["password","JsVar","<PASSWORD> access point"],<commit_msg>Typo correction.<commit_after>/*
 * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
 *
 * Copyright (C) 2015 <NAME> <<EMAIL>.uk>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * This file is designed to be parsed during the build process
 *
 * Contains ESP8266 board specific functions.
 * ----------------------------------------------------------------------------
 */
// Because the ESP8266 JS wrapper is assured to be running on an ESP8266 we
// can assume that inclusion of ESP8266 headers will be acceptable.
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
#include <osapi.h>
#include <ping.h>
#include <espconn.h>
#include <espmissingincludes.h>
#include <uart.h>

#define ESP8266_ON_ACCESS_POINTS "#accessPoints"

#define _GCC_WRAP_STDINT_H
typedef long long int64_t;

#include "jswrap_esp8266.h"
#include "jsinteractive.h" // Pull inn the jsiConsolePrint function
#include "network.h"
#include "network_esp8266.h"
#include "jswrap_net.h"

// Forward declaration of functions.
static void scanCB(void *arg, STATUS status);
static void wifiEventHandler(System_Event_t *event);
static void ipAddrToString(struct ip_addr addr, char *string);
static char *nullTerminateString(char *target, char *source, int sourceLength);
static void setupJsNetwork();
static void pingRecvCB();
static char *wifiConnectStatusToString(uint8 status);


// #NOTE: For callback functions, be sure and unlock them in the `kill` handler.

// A callback function to be invoked when we find a new access point.
static JsVar *g_jsScanCallback;

// A callback function to be invoked when we receive a WiFi event.
static JsVar *g_jsWiFiEventCallback;

// A callback function to be invoked when we have an IP address.
static JsVar *g_jsGotIpCallback;

// A callback function to be invoked on ping responses.
static JsVar *g_jsPingCallback;

// Global data structure for ping request
static struct ping_option pingOpt;

// Reasons for which a connection failed
uint8_t wifiReason = 0;
static char *wifiReasons[] = {
  "", "unspecified", "auth_expire", "auth_leave", "assoc_expire", "assoc_toomany", "not_authed",
  "not_assoced", "assoc_leave", "assoc_not_authed", "disassoc_pwrcap_bad", "disassoc_supchan_bad",
  "ie_invalid", "mic_failure", "4way_handshake_timeout", "group_key_update_timeout",
  "ie_in_4way_differs", "group_cipher_invalid", "pairwise_cipher_invalid", "akmp_invalid",
  "unsupp_rsn_ie_version", "invalid_rsn_ie_cap", "802_1x_auth_failed", "cipher_suite_rejected",
  "beacon_timeout", "no_ap_found" };

static char *wifiGetReason(void) {
  if (wifiReason <= 24) return wifiReasons[wifiReason];
  if (wifiReason >= 200 && wifiReason <= 201) return wifiReasons[wifiReason-200+24];
  return wifiReasons[1];
}

static char *wifiMode[] = { 0, "STA", "AP", "AP+STA" };
static char *wifiPhy[]  = { 0, "11b", "11g", "11n" };

// Let's define the JavaScript class that will contain our `world()` method. We'll call it `Hello`
/*JSON{
  "type"  : "class",
  "class" : "ESP8266WiFi"
}*/

/*JSON{
    "type":     "kill",
    "generate": "jswrap_ESP8266WiFi_kill"
}
*
* Register a handler to be called when the ESP8266 is reset.
*/
void jswrap_ESP8266WiFi_kill() {
  // When the Espruino environment is reset, this callback function will be invoked.
  // The purpose is to reset the environment by cleaning up whatever might be needed
  // to be cleaned up.
  os_printf("> jswrap_esp8266_kill\n");

  // Handle the g_jsGotIpCallback - If it is not null, then it contains a reference
  // to a locked JS variable that is a function pointer for the "got ip" callback.
  // We want to unlock this and delete the reference.
  if (g_jsGotIpCallback != NULL) {
    jsvUnLock(g_jsGotIpCallback);
    g_jsGotIpCallback = NULL;
  }

  // Handle g_jsPingCallback release.
  if (g_jsPingCallback != NULL) {
    jsvUnLock(g_jsPingCallback);
    g_jsPingCallback = NULL;
  }

  // Handle g_jsWiFiEventCallback
  if (g_jsWiFiEventCallback != NULL) {
    jsvUnLock(g_jsWiFiEventCallback);
    g_jsWiFiEventCallback = NULL;
  }

  // Handle g_jsWiFiEventCallback
  if (g_jsScanCallback != NULL) {
    jsvUnLock(g_jsScanCallback);
    g_jsScanCallback = NULL;
  }

  os_printf("< jswrap_esp8266_kill\n");
}

/*JSON{
  "type"     : "staticmethod",
  "class"    : "ESP8266WiFi",
  "name"     : "connect",
  "generate" : "jswrap_ESP8266WiFi_connect",
  "params"   : [
    ["ssid","JsVar","The network id of the access point."],
    ["password","JsVar","The password to the access point"],
{'fraction_non_alphanumeric': 0.14163407673607442, 'fraction_numerical': 0.02135080858260226, 'mean_word_length': 3.800608828006088, 'pattern_counts': {'":': 4, '<': 32, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6185611', 'n_tokens_mistral': 3303, 'n_tokens_neox': 3104, 'n_words': 961}