wayne roberts / Mbed OS lr1110_wifi_geolocation_gateway

Dependencies:   mbed-http lr1110 sx12xx_hal

Committer:
Wayne Roberts
Date:
Tue Feb 09 10:49:02 2021 -0800
Revision:
1:4a05f91c9c38
add source files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 1:4a05f91c9c38 1 #include "main.h"
Wayne Roberts 1:4a05f91c9c38 2 #include "network-helper.h"
Wayne Roberts 1:4a05f91c9c38 3 #include "radio.h"
Wayne Roberts 1:4a05f91c9c38 4
Wayne Roberts 1:4a05f91c9c38 5 #define TX_DBM 20
Wayne Roberts 1:4a05f91c9c38 6 #define BW_KHZ 500
Wayne Roberts 1:4a05f91c9c38 7 #define SPREADING_FACTOR 11
Wayne Roberts 1:4a05f91c9c38 8 #define CF_HZ 919000000
Wayne Roberts 1:4a05f91c9c38 9
Wayne Roberts 1:4a05f91c9c38 10 /* geolocation provider wont operate with less than 3 wifi access points */
Wayne Roberts 1:4a05f91c9c38 11 #define MINIMUM_REQUIRED_ACCESS_POINTS 2
Wayne Roberts 1:4a05f91c9c38 12
Wayne Roberts 1:4a05f91c9c38 13 bool wifiResultFormatBasic;
Wayne Roberts 1:4a05f91c9c38 14
Wayne Roberts 1:4a05f91c9c38 15 struct location {
Wayne Roberts 1:4a05f91c9c38 16 float lat, lng;
Wayne Roberts 1:4a05f91c9c38 17 int accuracy;
Wayne Roberts 1:4a05f91c9c38 18 };
Wayne Roberts 1:4a05f91c9c38 19
Wayne Roberts 1:4a05f91c9c38 20 typedef struct {
Wayne Roberts 1:4a05f91c9c38 21 const char* const cmd;
Wayne Roberts 1:4a05f91c9c38 22 void (*handler)(uint8_t args_at);
Wayne Roberts 1:4a05f91c9c38 23 const char* const arg_descr;
Wayne Roberts 1:4a05f91c9c38 24 const char* const description;
Wayne Roberts 1:4a05f91c9c38 25 } menu_item_t;
Wayne Roberts 1:4a05f91c9c38 26
Wayne Roberts 1:4a05f91c9c38 27 EventQueue queue(4 * EVENTS_EVENT_SIZE);
Wayne Roberts 1:4a05f91c9c38 28
Wayne Roberts 1:4a05f91c9c38 29 RawSerial pc(USBTX, USBRX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE); // speed from mbed_app.json
Wayne Roberts 1:4a05f91c9c38 30 char pcbuf[64];
Wayne Roberts 1:4a05f91c9c38 31 int pcbuf_len;
Wayne Roberts 1:4a05f91c9c38 32
Wayne Roberts 1:4a05f91c9c38 33 NetworkInterface* network;
Wayne Roberts 1:4a05f91c9c38 34
Wayne Roberts 1:4a05f91c9c38 35 event_callback_t serialEventCb;
Wayne Roberts 1:4a05f91c9c38 36
Wayne Roberts 1:4a05f91c9c38 37 uint8_t wifiScan_buf[9];
Wayne Roberts 1:4a05f91c9c38 38 uint64_t wifi_start_at, wifi_scan_dur;
Wayne Roberts 1:4a05f91c9c38 39 bool post_enable;
Wayne Roberts 1:4a05f91c9c38 40 bool send_reply;
Wayne Roberts 1:4a05f91c9c38 41
Wayne Roberts 1:4a05f91c9c38 42 void cmd_help(uint8_t);
Wayne Roberts 1:4a05f91c9c38 43
Wayne Roberts 1:4a05f91c9c38 44 struct location geoloc_result;
Wayne Roberts 1:4a05f91c9c38 45
Wayne Roberts 1:4a05f91c9c38 46 uint8_t remote_chip_eui[8];
Wayne Roberts 1:4a05f91c9c38 47
Wayne Roberts 1:4a05f91c9c38 48 struct wifidr {
Wayne Roberts 1:4a05f91c9c38 49 const char *txt;
Wayne Roberts 1:4a05f91c9c38 50 float Mbps;
Wayne Roberts 1:4a05f91c9c38 51 };
Wayne Roberts 1:4a05f91c9c38 52
Wayne Roberts 1:4a05f91c9c38 53 const struct wifidr wifiDatarates[] = {
Wayne Roberts 1:4a05f91c9c38 54 /* 0 */ { NULL, 0},
Wayne Roberts 1:4a05f91c9c38 55 /* 1 */ { "DBPSK", 1},
Wayne Roberts 1:4a05f91c9c38 56 /* 2 */ { "DQPSK", 2},
Wayne Roberts 1:4a05f91c9c38 57 /* 3 */ { "BPSK", 6},
Wayne Roberts 1:4a05f91c9c38 58 /* 4 */ { "BPSK", 9},
Wayne Roberts 1:4a05f91c9c38 59 /* 5 */ { "QPSK", 12},
Wayne Roberts 1:4a05f91c9c38 60 /* 6 */ { "QPSK", 18},
Wayne Roberts 1:4a05f91c9c38 61 /* 7 */ { "16-QAM", 24},
Wayne Roberts 1:4a05f91c9c38 62 /* 8 */ { "16-QAM", 36},
Wayne Roberts 1:4a05f91c9c38 63 /* 9 */ { "(9)", 0},
Wayne Roberts 1:4a05f91c9c38 64 /* 10 */ { "(10)", 0},
Wayne Roberts 1:4a05f91c9c38 65 /* 11 */ { "BPSK", 6.5},
Wayne Roberts 1:4a05f91c9c38 66 /* 12 */ { "QPSK", 13},
Wayne Roberts 1:4a05f91c9c38 67 /* 13 */ { "QPSK", 19.5},
Wayne Roberts 1:4a05f91c9c38 68 /* 14 */ { "16-QAM", 26},
Wayne Roberts 1:4a05f91c9c38 69 /* 15 */ { "16-QAM", 39},
Wayne Roberts 1:4a05f91c9c38 70 /* 16 */ { "(16)", 0},
Wayne Roberts 1:4a05f91c9c38 71 /* 17 */ { "(17)", 0},
Wayne Roberts 1:4a05f91c9c38 72 /* 18 */ { "(18)", 0},
Wayne Roberts 1:4a05f91c9c38 73 /* 19 */ { "BPSK", 7.2},
Wayne Roberts 1:4a05f91c9c38 74 /* 20 */ { "QPSK", 14.4},
Wayne Roberts 1:4a05f91c9c38 75 /* 21 */ { "QPSK", 21.7},
Wayne Roberts 1:4a05f91c9c38 76 /* 22 */ { "16-QAM", 28.9},
Wayne Roberts 1:4a05f91c9c38 77 /* 23 */ { "16-QAM", 43.3},
Wayne Roberts 1:4a05f91c9c38 78 };
Wayne Roberts 1:4a05f91c9c38 79
Wayne Roberts 1:4a05f91c9c38 80 char json[1536];
Wayne Roberts 1:4a05f91c9c38 81
Wayne Roberts 1:4a05f91c9c38 82 void dump_response(HttpResponse* res)
Wayne Roberts 1:4a05f91c9c38 83 {
Wayne Roberts 1:4a05f91c9c38 84 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
Wayne Roberts 1:4a05f91c9c38 85
Wayne Roberts 1:4a05f91c9c38 86 printf("Headers:\n");
Wayne Roberts 1:4a05f91c9c38 87 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
Wayne Roberts 1:4a05f91c9c38 88 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
Wayne Roberts 1:4a05f91c9c38 89 }
Wayne Roberts 1:4a05f91c9c38 90 printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
Wayne Roberts 1:4a05f91c9c38 91 }
Wayne Roberts 1:4a05f91c9c38 92
Wayne Roberts 1:4a05f91c9c38 93 void cfg_lora()
Wayne Roberts 1:4a05f91c9c38 94 {
Wayne Roberts 1:4a05f91c9c38 95 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
Wayne Roberts 1:4a05f91c9c38 96 Radio::SetChannel(CF_HZ);
Wayne Roberts 1:4a05f91c9c38 97 Radio::set_tx_dbm(TX_DBM);
Wayne Roberts 1:4a05f91c9c38 98 // preambleLen, fixLen, crcOn, invIQ
Wayne Roberts 1:4a05f91c9c38 99 Radio::LoRaPacketConfig(8, false, true, false);
Wayne Roberts 1:4a05f91c9c38 100 }
Wayne Roberts 1:4a05f91c9c38 101
Wayne Roberts 1:4a05f91c9c38 102 void cmd_wifi_scan(uint8_t idx)
Wayne Roberts 1:4a05f91c9c38 103 {
Wayne Roberts 1:4a05f91c9c38 104 Radio::radio.xfer(OPCODE_WIFI_SCAN, 9, 0, wifiScan_buf);
Wayne Roberts 1:4a05f91c9c38 105 wifi_start_at = Kernel::get_ms_count();
Wayne Roberts 1:4a05f91c9c38 106 printf("wifiScan...\r\n");
Wayne Roberts 1:4a05f91c9c38 107
Wayne Roberts 1:4a05f91c9c38 108 post_enable = pcbuf[idx] == 'p';
Wayne Roberts 1:4a05f91c9c38 109 send_reply = false;
Wayne Roberts 1:4a05f91c9c38 110 }
Wayne Roberts 1:4a05f91c9c38 111
Wayne Roberts 1:4a05f91c9c38 112 /* List of trusted root CA certificates
Wayne Roberts 1:4a05f91c9c38 113 * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
Wayne Roberts 1:4a05f91c9c38 114 *
Wayne Roberts 1:4a05f91c9c38 115 * To add more root certificates, just concatenate them.
Wayne Roberts 1:4a05f91c9c38 116 */
Wayne Roberts 1:4a05f91c9c38 117 const char HTTBIN_ORG_SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
Wayne Roberts 1:4a05f91c9c38 118 "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n"
Wayne Roberts 1:4a05f91c9c38 119 "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n"
Wayne Roberts 1:4a05f91c9c38 120 "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n"
Wayne Roberts 1:4a05f91c9c38 121 "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n"
Wayne Roberts 1:4a05f91c9c38 122 "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n"
Wayne Roberts 1:4a05f91c9c38 123 "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n"
Wayne Roberts 1:4a05f91c9c38 124 "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n"
Wayne Roberts 1:4a05f91c9c38 125 "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n"
Wayne Roberts 1:4a05f91c9c38 126 "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n"
Wayne Roberts 1:4a05f91c9c38 127 "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n"
Wayne Roberts 1:4a05f91c9c38 128 "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n"
Wayne Roberts 1:4a05f91c9c38 129 "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n"
Wayne Roberts 1:4a05f91c9c38 130 "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n"
Wayne Roberts 1:4a05f91c9c38 131 "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n"
Wayne Roberts 1:4a05f91c9c38 132 "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n"
Wayne Roberts 1:4a05f91c9c38 133 "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n"
Wayne Roberts 1:4a05f91c9c38 134 "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n"
Wayne Roberts 1:4a05f91c9c38 135 "rqXRfboQnoZsG4q5WTP468SQvvG5\n"
Wayne Roberts 1:4a05f91c9c38 136 "-----END CERTIFICATE-----\n"
Wayne Roberts 1:4a05f91c9c38 137 "-----BEGIN CERTIFICATE-----\n"
Wayne Roberts 1:4a05f91c9c38 138 "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"
Wayne Roberts 1:4a05f91c9c38 139 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"
Wayne Roberts 1:4a05f91c9c38 140 "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"
Wayne Roberts 1:4a05f91c9c38 141 "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"
Wayne Roberts 1:4a05f91c9c38 142 "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"
Wayne Roberts 1:4a05f91c9c38 143 "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"
Wayne Roberts 1:4a05f91c9c38 144 "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"
Wayne Roberts 1:4a05f91c9c38 145 "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"
Wayne Roberts 1:4a05f91c9c38 146 "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"
Wayne Roberts 1:4a05f91c9c38 147 "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"
Wayne Roberts 1:4a05f91c9c38 148 "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"
Wayne Roberts 1:4a05f91c9c38 149 "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"
Wayne Roberts 1:4a05f91c9c38 150 "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"
Wayne Roberts 1:4a05f91c9c38 151 "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"
Wayne Roberts 1:4a05f91c9c38 152 "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"
Wayne Roberts 1:4a05f91c9c38 153 "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"
Wayne Roberts 1:4a05f91c9c38 154 "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
Wayne Roberts 1:4a05f91c9c38 155 "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
Wayne Roberts 1:4a05f91c9c38 156 "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
Wayne Roberts 1:4a05f91c9c38 157 "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
Wayne Roberts 1:4a05f91c9c38 158 "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
Wayne Roberts 1:4a05f91c9c38 159 "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
Wayne Roberts 1:4a05f91c9c38 160 "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
Wayne Roberts 1:4a05f91c9c38 161 "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
Wayne Roberts 1:4a05f91c9c38 162 "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
Wayne Roberts 1:4a05f91c9c38 163 "-----END CERTIFICATE-----\n";
Wayne Roberts 1:4a05f91c9c38 164
Wayne Roberts 1:4a05f91c9c38 165
Wayne Roberts 1:4a05f91c9c38 166 void cmd_httpbin_post(uint8_t idx)
Wayne Roberts 1:4a05f91c9c38 167 {
Wayne Roberts 1:4a05f91c9c38 168 const char body[] = "{\"hello\":\"world\"}";
Wayne Roberts 1:4a05f91c9c38 169
Wayne Roberts 1:4a05f91c9c38 170 #if DEMO == DEMO_HTTPS
Wayne Roberts 1:4a05f91c9c38 171 printf("\n----- HTTPS POST request -----\n");
Wayne Roberts 1:4a05f91c9c38 172 HttpsRequest* post_req = new HttpsRequest(network, HTTBIN_ORG_SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
Wayne Roberts 1:4a05f91c9c38 173 #elif DEMO == DEMO_HTTP
Wayne Roberts 1:4a05f91c9c38 174 printf("\n----- HTTP POST request -----\n");
Wayne Roberts 1:4a05f91c9c38 175 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
Wayne Roberts 1:4a05f91c9c38 176 #endif
Wayne Roberts 1:4a05f91c9c38 177
Wayne Roberts 1:4a05f91c9c38 178 post_req->set_header("Content-Type", "application/json");
Wayne Roberts 1:4a05f91c9c38 179
Wayne Roberts 1:4a05f91c9c38 180 HttpResponse* post_res = post_req->send(body, strlen(body));
Wayne Roberts 1:4a05f91c9c38 181 if (!post_res) {
Wayne Roberts 1:4a05f91c9c38 182 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
Wayne Roberts 1:4a05f91c9c38 183 return;
Wayne Roberts 1:4a05f91c9c38 184 }
Wayne Roberts 1:4a05f91c9c38 185
Wayne Roberts 1:4a05f91c9c38 186 dump_response(post_res);
Wayne Roberts 1:4a05f91c9c38 187 delete post_req;
Wayne Roberts 1:4a05f91c9c38 188 }
Wayne Roberts 1:4a05f91c9c38 189
Wayne Roberts 1:4a05f91c9c38 190
Wayne Roberts 1:4a05f91c9c38 191 void cmd_print_status(uint8_t idx)
Wayne Roberts 1:4a05f91c9c38 192 {
Wayne Roberts 1:4a05f91c9c38 193 stat_t stat;
Wayne Roberts 1:4a05f91c9c38 194 uint8_t buf[4];
Wayne Roberts 1:4a05f91c9c38 195 printf("[NWKH] IP address: %s\n", network->get_ip_address());
Wayne Roberts 1:4a05f91c9c38 196
Wayne Roberts 1:4a05f91c9c38 197 stat.word = Radio::radio.xfer(OPCODE_GET_VERSION, 0, 0, NULL);
Wayne Roberts 1:4a05f91c9c38 198 stat.word = Radio::radio.xfer(0x0000, 0, 4, buf);
Wayne Roberts 1:4a05f91c9c38 199 if (stat.bits.cmdStatus == CMD_DAT) {
Wayne Roberts 1:4a05f91c9c38 200 printf("LR1110 chip:%02x use:%02x fw-v%u.%u\r\n",
Wayne Roberts 1:4a05f91c9c38 201 buf[0], /* silicon rev */
Wayne Roberts 1:4a05f91c9c38 202 buf[1], /* use case */
Wayne Roberts 1:4a05f91c9c38 203 buf[2], /* firmware major */
Wayne Roberts 1:4a05f91c9c38 204 buf[3] /* firmware minor */
Wayne Roberts 1:4a05f91c9c38 205 );
Wayne Roberts 1:4a05f91c9c38 206 }
Wayne Roberts 1:4a05f91c9c38 207
Wayne Roberts 1:4a05f91c9c38 208 stat.word = Radio::radio.xfer(OPCODE_GET_STATUS, 4, 0, buf);
Wayne Roberts 1:4a05f91c9c38 209 printf("chipMode:%d, cmdStatus:%d\r\n", stat.bits.chipMode, stat.bits.cmdStatus);
Wayne Roberts 1:4a05f91c9c38 210 }
Wayne Roberts 1:4a05f91c9c38 211
Wayne Roberts 1:4a05f91c9c38 212 const menu_item_t menu_items[] =
Wayne Roberts 1:4a05f91c9c38 213 {
Wayne Roberts 1:4a05f91c9c38 214 { "phb", cmd_httpbin_post, "","test post to httpbin.org"},
Wayne Roberts 1:4a05f91c9c38 215 { "ws", cmd_wifi_scan, "","local wifi scan"},
Wayne Roberts 1:4a05f91c9c38 216 { ".", cmd_print_status, "","print status"},
Wayne Roberts 1:4a05f91c9c38 217 { "?", cmd_help, "","this list of commands"},
Wayne Roberts 1:4a05f91c9c38 218 { NULL, NULL, NULL, NULL }
Wayne Roberts 1:4a05f91c9c38 219 };
Wayne Roberts 1:4a05f91c9c38 220
Wayne Roberts 1:4a05f91c9c38 221 void
Wayne Roberts 1:4a05f91c9c38 222 console()
Wayne Roberts 1:4a05f91c9c38 223 {
Wayne Roberts 1:4a05f91c9c38 224 int i;
Wayne Roberts 1:4a05f91c9c38 225 uint8_t user_cmd_len;
Wayne Roberts 1:4a05f91c9c38 226
Wayne Roberts 1:4a05f91c9c38 227 if (pcbuf_len < 0) {
Wayne Roberts 1:4a05f91c9c38 228 printf("abort\r\n");
Wayne Roberts 1:4a05f91c9c38 229 pcbuf_len = 0;
Wayne Roberts 1:4a05f91c9c38 230 return;
Wayne Roberts 1:4a05f91c9c38 231 }
Wayne Roberts 1:4a05f91c9c38 232
Wayne Roberts 1:4a05f91c9c38 233 printf("\r\n");
Wayne Roberts 1:4a05f91c9c38 234
Wayne Roberts 1:4a05f91c9c38 235 if (pcbuf_len > 0) {
Wayne Roberts 1:4a05f91c9c38 236 /* get end of user-entered command */
Wayne Roberts 1:4a05f91c9c38 237 user_cmd_len = 1; // first character can be any character
Wayne Roberts 1:4a05f91c9c38 238 for (i = 1; i <= pcbuf_len; i++) {
Wayne Roberts 1:4a05f91c9c38 239 if (pcbuf[i] < 'A' || (pcbuf[i] > 'Z' && pcbuf[i] < 'a') || pcbuf[i] > 'z') {
Wayne Roberts 1:4a05f91c9c38 240 user_cmd_len = i;
Wayne Roberts 1:4a05f91c9c38 241 break;
Wayne Roberts 1:4a05f91c9c38 242 }
Wayne Roberts 1:4a05f91c9c38 243 }
Wayne Roberts 1:4a05f91c9c38 244
Wayne Roberts 1:4a05f91c9c38 245
Wayne Roberts 1:4a05f91c9c38 246 for (i = 0; menu_items[i].cmd != NULL ; i++) {
Wayne Roberts 1:4a05f91c9c38 247 int mi_len = strlen(menu_items[i].cmd);
Wayne Roberts 1:4a05f91c9c38 248 if (menu_items[i].handler && user_cmd_len == mi_len && (strncmp(pcbuf, menu_items[i].cmd, mi_len) == 0)) {
Wayne Roberts 1:4a05f91c9c38 249 while (pcbuf[mi_len] == ' ') // skip past spaces
Wayne Roberts 1:4a05f91c9c38 250 mi_len++;
Wayne Roberts 1:4a05f91c9c38 251 menu_items[i].handler(mi_len);
Wayne Roberts 1:4a05f91c9c38 252 break;
Wayne Roberts 1:4a05f91c9c38 253 }
Wayne Roberts 1:4a05f91c9c38 254 }
Wayne Roberts 1:4a05f91c9c38 255 }
Wayne Roberts 1:4a05f91c9c38 256
Wayne Roberts 1:4a05f91c9c38 257 pcbuf_len = 0;
Wayne Roberts 1:4a05f91c9c38 258 printf("> ");
Wayne Roberts 1:4a05f91c9c38 259 fflush(stdout);
Wayne Roberts 1:4a05f91c9c38 260 }
Wayne Roberts 1:4a05f91c9c38 261
Wayne Roberts 1:4a05f91c9c38 262 void echo(char c)
Wayne Roberts 1:4a05f91c9c38 263 {
Wayne Roberts 1:4a05f91c9c38 264 if (c == 8) {
Wayne Roberts 1:4a05f91c9c38 265 pc.putc(8);
Wayne Roberts 1:4a05f91c9c38 266 pc.putc(' ');
Wayne Roberts 1:4a05f91c9c38 267 pc.putc(8);
Wayne Roberts 1:4a05f91c9c38 268 } else
Wayne Roberts 1:4a05f91c9c38 269 pc.putc(c);
Wayne Roberts 1:4a05f91c9c38 270 }
Wayne Roberts 1:4a05f91c9c38 271
Wayne Roberts 1:4a05f91c9c38 272 uint8_t serial_rx_buf;
Wayne Roberts 1:4a05f91c9c38 273
Wayne Roberts 1:4a05f91c9c38 274 void serialCb(int events)
Wayne Roberts 1:4a05f91c9c38 275 {
Wayne Roberts 1:4a05f91c9c38 276 if (events & SERIAL_EVENT_RX_COMPLETE) {
Wayne Roberts 1:4a05f91c9c38 277 char c = serial_rx_buf;
Wayne Roberts 1:4a05f91c9c38 278 static uint8_t pcbuf_idx = 0;
Wayne Roberts 1:4a05f91c9c38 279 static uint8_t prev_len = 0;;
Wayne Roberts 1:4a05f91c9c38 280 if (c == 8) {
Wayne Roberts 1:4a05f91c9c38 281 if (pcbuf_idx > 0) {
Wayne Roberts 1:4a05f91c9c38 282 queue.call(echo, 8);
Wayne Roberts 1:4a05f91c9c38 283 pcbuf_idx--;
Wayne Roberts 1:4a05f91c9c38 284 }
Wayne Roberts 1:4a05f91c9c38 285 } else if (c == 3) { // ctrl-C
Wayne Roberts 1:4a05f91c9c38 286 pcbuf_len = -1;
Wayne Roberts 1:4a05f91c9c38 287 queue.call(console);
Wayne Roberts 1:4a05f91c9c38 288 } else if (c == '\r') {
Wayne Roberts 1:4a05f91c9c38 289 if (pcbuf_idx == 0) {
Wayne Roberts 1:4a05f91c9c38 290 pcbuf_len = prev_len;
Wayne Roberts 1:4a05f91c9c38 291 } else {
Wayne Roberts 1:4a05f91c9c38 292 pcbuf[pcbuf_idx] = 0; // null terminate
Wayne Roberts 1:4a05f91c9c38 293 prev_len = pcbuf_idx;
Wayne Roberts 1:4a05f91c9c38 294 pcbuf_idx = 0;
Wayne Roberts 1:4a05f91c9c38 295 pcbuf_len = prev_len;
Wayne Roberts 1:4a05f91c9c38 296 }
Wayne Roberts 1:4a05f91c9c38 297 queue.call(console);
Wayne Roberts 1:4a05f91c9c38 298 } else if (pcbuf_idx < sizeof(pcbuf)) {
Wayne Roberts 1:4a05f91c9c38 299 pcbuf[pcbuf_idx++] = c;
Wayne Roberts 1:4a05f91c9c38 300 queue.call(echo, c);
Wayne Roberts 1:4a05f91c9c38 301 }
Wayne Roberts 1:4a05f91c9c38 302 }
Wayne Roberts 1:4a05f91c9c38 303
Wayne Roberts 1:4a05f91c9c38 304 if (pc.read(&serial_rx_buf, 1, serialCb) != 0)
Wayne Roberts 1:4a05f91c9c38 305 printf("Serial-Read-Fail\r\n");
Wayne Roberts 1:4a05f91c9c38 306 }
Wayne Roberts 1:4a05f91c9c38 307
Wayne Roberts 1:4a05f91c9c38 308 void cmd_help(uint8_t args_at)
Wayne Roberts 1:4a05f91c9c38 309 {
Wayne Roberts 1:4a05f91c9c38 310 int i;
Wayne Roberts 1:4a05f91c9c38 311
Wayne Roberts 1:4a05f91c9c38 312 for (i = 0; menu_items[i].cmd != NULL ; i++) {
Wayne Roberts 1:4a05f91c9c38 313 printf("%s%s\t%s\r\n", menu_items[i].cmd, menu_items[i].arg_descr, menu_items[i].description);
Wayne Roberts 1:4a05f91c9c38 314 }
Wayne Roberts 1:4a05f91c9c38 315 }
Wayne Roberts 1:4a05f91c9c38 316
Wayne Roberts 1:4a05f91c9c38 317
Wayne Roberts 1:4a05f91c9c38 318 void print_wifi_result(const uint8_t *result)
Wayne Roberts 1:4a05f91c9c38 319 {
Wayne Roberts 1:4a05f91c9c38 320 char out[96];
Wayne Roberts 1:4a05f91c9c38 321 char str[24];
Wayne Roberts 1:4a05f91c9c38 322 unsigned n, macStart;
Wayne Roberts 1:4a05f91c9c38 323 wifiType_t wt;
Wayne Roberts 1:4a05f91c9c38 324 wifiChanInfo_t ci;
Wayne Roberts 1:4a05f91c9c38 325 wt.octet = result[0];
Wayne Roberts 1:4a05f91c9c38 326 ci.octet = result[1];
Wayne Roberts 1:4a05f91c9c38 327 out[0] = 0;
Wayne Roberts 1:4a05f91c9c38 328 strcat(out, "802.11");
Wayne Roberts 1:4a05f91c9c38 329 switch (wt.bits.signal) {
Wayne Roberts 1:4a05f91c9c38 330 case 1: strcat(out, "b"); break;
Wayne Roberts 1:4a05f91c9c38 331 case 2: strcat(out, "g"); break;
Wayne Roberts 1:4a05f91c9c38 332 case 3: strcat(out, "n"); break;
Wayne Roberts 1:4a05f91c9c38 333 }
Wayne Roberts 1:4a05f91c9c38 334 sprintf(str, " %s %.1fMbps", wifiDatarates[wt.bits.datarate].txt, wifiDatarates[wt.bits.datarate].Mbps);
Wayne Roberts 1:4a05f91c9c38 335 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 336 strcat(out, " ");
Wayne Roberts 1:4a05f91c9c38 337
Wayne Roberts 1:4a05f91c9c38 338 sprintf(str, "ch%u ", ci.bits.channelID);
Wayne Roberts 1:4a05f91c9c38 339 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 340 switch (ci.bits.channelID) {
Wayne Roberts 1:4a05f91c9c38 341 // table 10-5
Wayne Roberts 1:4a05f91c9c38 342 }
Wayne Roberts 1:4a05f91c9c38 343 strcat(out, " ");
Wayne Roberts 1:4a05f91c9c38 344 sprintf(str, "mv:%u ", ci.bits.macValidationID);
Wayne Roberts 1:4a05f91c9c38 345 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 346 switch (ci.bits.macValidationID) {
Wayne Roberts 1:4a05f91c9c38 347 case 1: strcat(out, "gateway"); break;
Wayne Roberts 1:4a05f91c9c38 348 case 2: strcat(out, "phone"); break;
Wayne Roberts 1:4a05f91c9c38 349 case 3: strcat(out, "?"); break;
Wayne Roberts 1:4a05f91c9c38 350 // table 10.8
Wayne Roberts 1:4a05f91c9c38 351 }
Wayne Roberts 1:4a05f91c9c38 352
Wayne Roberts 1:4a05f91c9c38 353 strcat(out, " ");
Wayne Roberts 1:4a05f91c9c38 354
Wayne Roberts 1:4a05f91c9c38 355 if (wifiResultFormatBasic) {
Wayne Roberts 1:4a05f91c9c38 356 macStart = 3;
Wayne Roberts 1:4a05f91c9c38 357 } else {
Wayne Roberts 1:4a05f91c9c38 358 macStart = 4;
Wayne Roberts 1:4a05f91c9c38 359 }
Wayne Roberts 1:4a05f91c9c38 360 for (n = 0; n < 6; n++) {
Wayne Roberts 1:4a05f91c9c38 361 sprintf(str, "%02x", result[n+macStart]);
Wayne Roberts 1:4a05f91c9c38 362 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 363 if (n < 5)
Wayne Roberts 1:4a05f91c9c38 364 strcat(out, ":");
Wayne Roberts 1:4a05f91c9c38 365 }
Wayne Roberts 1:4a05f91c9c38 366
Wayne Roberts 1:4a05f91c9c38 367 sprintf(str, " rssi:%d ", (int8_t)result[2]);
Wayne Roberts 1:4a05f91c9c38 368 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 369
Wayne Roberts 1:4a05f91c9c38 370 if (!wifiResultFormatBasic) {
Wayne Roberts 1:4a05f91c9c38 371 sprintf(str, "frameCtrl:%02x ", result[3]);
Wayne Roberts 1:4a05f91c9c38 372 strcat(out, str);
Wayne Roberts 1:4a05f91c9c38 373 }
Wayne Roberts 1:4a05f91c9c38 374 printf("%s\r\n", out);
Wayne Roberts 1:4a05f91c9c38 375 }
Wayne Roberts 1:4a05f91c9c38 376
Wayne Roberts 1:4a05f91c9c38 377 void take_result()
Wayne Roberts 1:4a05f91c9c38 378 {
Wayne Roberts 1:4a05f91c9c38 379 printf("result %f, %f, %d\r\n",
Wayne Roberts 1:4a05f91c9c38 380 geoloc_result.lat,
Wayne Roberts 1:4a05f91c9c38 381 geoloc_result.lng,
Wayne Roberts 1:4a05f91c9c38 382 geoloc_result.accuracy
Wayne Roberts 1:4a05f91c9c38 383 );
Wayne Roberts 1:4a05f91c9c38 384
Wayne Roberts 1:4a05f91c9c38 385 /* TODO: store result to database and show on map */
Wayne Roberts 1:4a05f91c9c38 386
Wayne Roberts 1:4a05f91c9c38 387 if (send_reply) {
Wayne Roberts 1:4a05f91c9c38 388 unsigned len;
Wayne Roberts 1:4a05f91c9c38 389 memcpy(Radio::radio.tx_buf, remote_chip_eui, 8);
Wayne Roberts 1:4a05f91c9c38 390 Radio::radio.tx_buf[8] = 0; // rfu
Wayne Roberts 1:4a05f91c9c38 391 Radio::radio.tx_buf[9] = 0; // rfu
Wayne Roberts 1:4a05f91c9c38 392 len = sprintf((char*)(Radio::radio.tx_buf + HEADER_LENGTH), "%f, %f, %u",
Wayne Roberts 1:4a05f91c9c38 393 geoloc_result.lat,
Wayne Roberts 1:4a05f91c9c38 394 geoloc_result.lng,
Wayne Roberts 1:4a05f91c9c38 395 geoloc_result.accuracy
Wayne Roberts 1:4a05f91c9c38 396 );
Wayne Roberts 1:4a05f91c9c38 397 Radio::Send(len + HEADER_LENGTH, 0, 0, 0); /* begin transmission */
Wayne Roberts 1:4a05f91c9c38 398 send_reply = false; // sent
Wayne Roberts 1:4a05f91c9c38 399 }
Wayne Roberts 1:4a05f91c9c38 400 }
Wayne Roberts 1:4a05f91c9c38 401
Wayne Roberts 1:4a05f91c9c38 402 void service()
Wayne Roberts 1:4a05f91c9c38 403 {
Wayne Roberts 1:4a05f91c9c38 404 irq_t irq;
Wayne Roberts 1:4a05f91c9c38 405 irq.dword = Radio::radio.service();
Wayne Roberts 1:4a05f91c9c38 406 if (irq.bits.WifiDone) {
Wayne Roberts 1:4a05f91c9c38 407 stat_t stat;
Wayne Roberts 1:4a05f91c9c38 408 uint8_t nbResults;
Wayne Roberts 1:4a05f91c9c38 409 json_start();
Wayne Roberts 1:4a05f91c9c38 410 stat.word = Radio::radio.xfer(OPCODE_GET_WIFI_NB_RESULTS, 0, 0, NULL);
Wayne Roberts 1:4a05f91c9c38 411 stat.word = Radio::radio.xfer(0x0000, 0, 1, &nbResults);
Wayne Roberts 1:4a05f91c9c38 412 if (stat.bits.cmdStatus == CMD_DAT) {
Wayne Roberts 1:4a05f91c9c38 413 unsigned n;
Wayne Roberts 1:4a05f91c9c38 414 printf("%ums nbResults:%u\r\n", (unsigned)wifi_scan_dur, nbResults);
Wayne Roberts 1:4a05f91c9c38 415 for (n = 0; n < nbResults; n++) {
Wayne Roberts 1:4a05f91c9c38 416 uint8_t buf[3];
Wayne Roberts 1:4a05f91c9c38 417 uint8_t resultBuf[22];
Wayne Roberts 1:4a05f91c9c38 418 buf[0] = n;
Wayne Roberts 1:4a05f91c9c38 419 buf[1] = 1; // number of results in this read
Wayne Roberts 1:4a05f91c9c38 420 buf[2] = wifiResultFormatBasic ? 4 : 1;
Wayne Roberts 1:4a05f91c9c38 421 stat.word = Radio::radio.xfer(OPCODE_WIFI_READ_RESULTS, 3, 0, buf);
Wayne Roberts 1:4a05f91c9c38 422 // basic = 9byte length
Wayne Roberts 1:4a05f91c9c38 423 // full = 22byte length
Wayne Roberts 1:4a05f91c9c38 424 stat.word = Radio::radio.xfer(0x0000, 0, wifiResultFormatBasic ? 9 : 22, resultBuf);
Wayne Roberts 1:4a05f91c9c38 425 if (stat.bits.cmdStatus == CMD_DAT) {
Wayne Roberts 1:4a05f91c9c38 426 wifiChanInfo_t ci;
Wayne Roberts 1:4a05f91c9c38 427 print_wifi_result(resultBuf);
Wayne Roberts 1:4a05f91c9c38 428 ci.octet = resultBuf[1];
Wayne Roberts 1:4a05f91c9c38 429 if (ci.bits.macValidationID == 1) // 1 is AP
Wayne Roberts 1:4a05f91c9c38 430 wifi_result_to_json(n == 0, resultBuf, wifiResultFormatBasic ? 3 : 4, 2);
Wayne Roberts 1:4a05f91c9c38 431 } else
Wayne Roberts 1:4a05f91c9c38 432 printf("readResult:%s\r\n", Radio::radio.cmdStatus_toString(stat.bits.cmdStatus));
Wayne Roberts 1:4a05f91c9c38 433 }
Wayne Roberts 1:4a05f91c9c38 434 }
Wayne Roberts 1:4a05f91c9c38 435 json_end();
Wayne Roberts 1:4a05f91c9c38 436 //printf("JSON %s\r\n", json);
Wayne Roberts 1:4a05f91c9c38 437 if (post_enable) {
Wayne Roberts 1:4a05f91c9c38 438 printf("post_enabled\r\n");
Wayne Roberts 1:4a05f91c9c38 439 if (nbResults > MINIMUM_REQUIRED_ACCESS_POINTS) {
Wayne Roberts 1:4a05f91c9c38 440 post_scan_result(json, &geoloc_result.lat, &geoloc_result.lng, &geoloc_result.accuracy);
Wayne Roberts 1:4a05f91c9c38 441 queue.call(take_result);
Wayne Roberts 1:4a05f91c9c38 442 } else
Wayne Roberts 1:4a05f91c9c38 443 printf("only %u access points\r\n", nbResults);
Wayne Roberts 1:4a05f91c9c38 444 }
Wayne Roberts 1:4a05f91c9c38 445
Wayne Roberts 1:4a05f91c9c38 446 cfg_lora();
Wayne Roberts 1:4a05f91c9c38 447 Radio::Rx(0);
Wayne Roberts 1:4a05f91c9c38 448 } // ..if (irq.bits.WifiDone)
Wayne Roberts 1:4a05f91c9c38 449 }
Wayne Roberts 1:4a05f91c9c38 450
Wayne Roberts 1:4a05f91c9c38 451 void radio_irq_handler()
Wayne Roberts 1:4a05f91c9c38 452 {
Wayne Roberts 1:4a05f91c9c38 453 wifi_scan_dur = Kernel::get_ms_count() - wifi_start_at;
Wayne Roberts 1:4a05f91c9c38 454 queue.call(service);
Wayne Roberts 1:4a05f91c9c38 455 }
Wayne Roberts 1:4a05f91c9c38 456
Wayne Roberts 1:4a05f91c9c38 457 void txDoneCB()
Wayne Roberts 1:4a05f91c9c38 458 {
Wayne Roberts 1:4a05f91c9c38 459 Radio::Rx(0);
Wayne Roberts 1:4a05f91c9c38 460 }
Wayne Roberts 1:4a05f91c9c38 461
Wayne Roberts 1:4a05f91c9c38 462 void parse_remote_wifi_scan(uint8_t pktLen)
Wayne Roberts 1:4a05f91c9c38 463 {
Wayne Roberts 1:4a05f91c9c38 464 uint8_t ap_cnt = 0;
Wayne Roberts 1:4a05f91c9c38 465 uint8_t pkt_idx = HEADER_LENGTH;
Wayne Roberts 1:4a05f91c9c38 466 json_start();
Wayne Roberts 1:4a05f91c9c38 467 while (pkt_idx < pktLen) {
Wayne Roberts 1:4a05f91c9c38 468 wifi_result_to_json(pkt_idx == HEADER_LENGTH, Radio::radio.rx_buf + pkt_idx, 0, 6);
Wayne Roberts 1:4a05f91c9c38 469 if (strlen(json) >= sizeof(json)) {
Wayne Roberts 1:4a05f91c9c38 470 printf("json-overrun\r\n");
Wayne Roberts 1:4a05f91c9c38 471 return;
Wayne Roberts 1:4a05f91c9c38 472 }
Wayne Roberts 1:4a05f91c9c38 473 pkt_idx += 7;
Wayne Roberts 1:4a05f91c9c38 474 ap_cnt++;
Wayne Roberts 1:4a05f91c9c38 475 }
Wayne Roberts 1:4a05f91c9c38 476 json_end();
Wayne Roberts 1:4a05f91c9c38 477
Wayne Roberts 1:4a05f91c9c38 478 if (ap_cnt > MINIMUM_REQUIRED_ACCESS_POINTS) {
Wayne Roberts 1:4a05f91c9c38 479 post_scan_result(json, &geoloc_result.lat, &geoloc_result.lng, &geoloc_result.accuracy);
Wayne Roberts 1:4a05f91c9c38 480 queue.call(take_result);
Wayne Roberts 1:4a05f91c9c38 481 } else
Wayne Roberts 1:4a05f91c9c38 482 printf("only %u access points\r\n", ap_cnt);
Wayne Roberts 1:4a05f91c9c38 483
Wayne Roberts 1:4a05f91c9c38 484 send_reply = true;
Wayne Roberts 1:4a05f91c9c38 485 }
Wayne Roberts 1:4a05f91c9c38 486
Wayne Roberts 1:4a05f91c9c38 487 void rxDoneCB(uint8_t size, float rssi, float snr)
Wayne Roberts 1:4a05f91c9c38 488 {
Wayne Roberts 1:4a05f91c9c38 489 unsigned i;
Wayne Roberts 1:4a05f91c9c38 490 printf("%.1fdBm snr:%.1fdB\t", rssi, snr);
Wayne Roberts 1:4a05f91c9c38 491
Wayne Roberts 1:4a05f91c9c38 492 for (i = 0; i < size; i++) {
Wayne Roberts 1:4a05f91c9c38 493 printf("%02x ", Radio::radio.rx_buf[i]);
Wayne Roberts 1:4a05f91c9c38 494 }
Wayne Roberts 1:4a05f91c9c38 495 printf("\r\n");
Wayne Roberts 1:4a05f91c9c38 496
Wayne Roberts 1:4a05f91c9c38 497 for (i = 0; i < 8; i++)
Wayne Roberts 1:4a05f91c9c38 498 remote_chip_eui[i] = Radio::radio.rx_buf[i];
Wayne Roberts 1:4a05f91c9c38 499
Wayne Roberts 1:4a05f91c9c38 500 parse_remote_wifi_scan(size);
Wayne Roberts 1:4a05f91c9c38 501 }
Wayne Roberts 1:4a05f91c9c38 502
Wayne Roberts 1:4a05f91c9c38 503 const RadioEvents_t rev = {
Wayne Roberts 1:4a05f91c9c38 504 /* Dio0_top_half */ radio_irq_handler,
Wayne Roberts 1:4a05f91c9c38 505 /* TxDone_topHalf */ NULL,
Wayne Roberts 1:4a05f91c9c38 506 /* TxDone_botHalf */ txDoneCB,
Wayne Roberts 1:4a05f91c9c38 507 /* TxTimeout */ NULL,
Wayne Roberts 1:4a05f91c9c38 508 /* RxDone */ rxDoneCB,
Wayne Roberts 1:4a05f91c9c38 509 /* RxTimeout */ NULL,
Wayne Roberts 1:4a05f91c9c38 510 /* RxError */ NULL,
Wayne Roberts 1:4a05f91c9c38 511 /* FhssChangeChannel */NULL,
Wayne Roberts 1:4a05f91c9c38 512 /* CadDone */ NULL
Wayne Roberts 1:4a05f91c9c38 513 };
Wayne Roberts 1:4a05f91c9c38 514
Wayne Roberts 1:4a05f91c9c38 515 int main()
Wayne Roberts 1:4a05f91c9c38 516 {
Wayne Roberts 1:4a05f91c9c38 517 { /* wifi scan defaults, see LR1110 user manual section 10.2 */
Wayne Roberts 1:4a05f91c9c38 518 unsigned chanmask = 0x0421; // ch1, ch6, ch11
Wayne Roberts 1:4a05f91c9c38 519 unsigned timeout = 105; // in milliseconds, 100 wifi TUs (beacon interval)
Wayne Roberts 1:4a05f91c9c38 520
Wayne Roberts 1:4a05f91c9c38 521 wifiScan_buf[0] = 0x01; // wifi type
Wayne Roberts 1:4a05f91c9c38 522 wifiScan_buf[2] = chanmask; // chanmask-lo
Wayne Roberts 1:4a05f91c9c38 523 chanmask >>= 8;
Wayne Roberts 1:4a05f91c9c38 524 wifiScan_buf[1] = chanmask; // chanmask-hi
Wayne Roberts 1:4a05f91c9c38 525 wifiScan_buf[3] = 0x02; // acqMode
Wayne Roberts 1:4a05f91c9c38 526 wifiScan_buf[4] = 0x0a; // NbMaxRes
Wayne Roberts 1:4a05f91c9c38 527 wifiScan_buf[5] = 0x10; // NbScanPerChan
Wayne Roberts 1:4a05f91c9c38 528 wifiScan_buf[7] = timeout; // Timeout-lo
Wayne Roberts 1:4a05f91c9c38 529 timeout >>= 8;
Wayne Roberts 1:4a05f91c9c38 530 wifiScan_buf[6] = timeout; // Timeout-hi
Wayne Roberts 1:4a05f91c9c38 531 wifiScan_buf[8] = 0x00; // AbortOnTimeout
Wayne Roberts 1:4a05f91c9c38 532 }
Wayne Roberts 1:4a05f91c9c38 533
Wayne Roberts 1:4a05f91c9c38 534 serialEventCb = serialCb;
Wayne Roberts 1:4a05f91c9c38 535
Wayne Roberts 1:4a05f91c9c38 536 if (pc.read(&serial_rx_buf, 1, serialCb) != 0)
Wayne Roberts 1:4a05f91c9c38 537 printf("serial-read-fail\r\n");
Wayne Roberts 1:4a05f91c9c38 538
Wayne Roberts 1:4a05f91c9c38 539 // Connect to the network with the default networking interface
Wayne Roberts 1:4a05f91c9c38 540 // if you use WiFi: see mbed_app.json for the credentials
Wayne Roberts 1:4a05f91c9c38 541 network = connect_to_default_network_interface();
Wayne Roberts 1:4a05f91c9c38 542 if (!network) {
Wayne Roberts 1:4a05f91c9c38 543 printf("Cannot connect to the network, see serial output\n");
Wayne Roberts 1:4a05f91c9c38 544 return 1;
Wayne Roberts 1:4a05f91c9c38 545 }
Wayne Roberts 1:4a05f91c9c38 546
Wayne Roberts 1:4a05f91c9c38 547 Radio::Init(&rev);
Wayne Roberts 1:4a05f91c9c38 548
Wayne Roberts 1:4a05f91c9c38 549 Radio::Standby();
Wayne Roberts 1:4a05f91c9c38 550 cfg_lora();
Wayne Roberts 1:4a05f91c9c38 551 Radio::Rx(0);
Wayne Roberts 1:4a05f91c9c38 552
Wayne Roberts 1:4a05f91c9c38 553 queue.dispatch();
Wayne Roberts 1:4a05f91c9c38 554 }
Wayne Roberts 1:4a05f91c9c38 555