Morse Micro IoT SDK  2.10.4
scan.c
Go to the documentation of this file.
1/*
2 * Copyright 2022-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
21#include <string.h>
22#include "mmosal.h"
23#include "mmutils.h"
24#include "mmwlan.h"
25#include "mm_app_loadconfig.h"
26
27/*
28 * ANSI escape characters will be used for rich text in the console. To disable ANSI escape
29 * characters, ANSI_ESCAPE_ENABLED must be defined as 0.
30 */
31#if !(defined(ANSI_ESCAPE_ENABLED) && ANSI_ESCAPE_ENABLED == 0)
33#define ANSI_BOLD "\x1b[1m"
35#define ANSI_RESET "\x1b[0m"
36#else
38#define ANSI_BOLD ""
40#define ANSI_RESET ""
41#endif
42
45#define MAC_ADDR_STR_LEN (18)
46
49
56static void scan_rx_callback(const struct mmwlan_scan_result *result, void *arg)
57{
58 (void)(arg);
59 char bssid_str[MAC_ADDR_STR_LEN];
60 char ssid_str[MMWLAN_SSID_MAXLEN];
61 int ret;
62 struct mm_rsn_information rsn_info;
63
65 snprintf(bssid_str,
67 "%02x:%02x:%02x:%02x:%02x:%02x",
68 result->bssid[0],
69 result->bssid[1],
70 result->bssid[2],
71 result->bssid[3],
72 result->bssid[4],
73 result->bssid[5]);
74 snprintf(ssid_str, (result->ssid_len + 1), "%s", result->ssid);
75
76 printf(ANSI_BOLD "%s" ANSI_RESET "\n", ssid_str);
77 printf(" Operating BW: %u MHz\n", result->op_bw_mhz);
78 printf(" BSSID: %s\n", bssid_str);
79 printf(" RSSI: %3d dBm\n", result->rssi);
80 printf(" Noise: %3d dBm\n", result->noise_dbm);
81 printf(" Beacon Interval(TUs): %u\n", result->beacon_interval);
82 printf(" Capability Info: 0x%04x\n", result->capability_info);
83
84 ret = mm_parse_rsn_information(result->ies, result->ies_len, &rsn_info);
85 if (ret == 0)
86 {
87 unsigned ii;
88 printf(" Security:");
89 for (ii = 0; ii < rsn_info.num_akm_suites; ii++)
90 {
91 printf(" %s", mm_akm_suite_to_string(rsn_info.akm_suites[ii]));
92 }
93 printf("\n");
94 }
95 else if (ret == -1)
96 {
97 printf(" Security: None\n");
98 }
99 else
100 {
101 printf(" Invalid RSN IE in probe response\n");
102 }
103
104 struct mm_s1g_operation s1g_operation;
105 ret = mm_parse_s1g_operation(result->ies, result->ies_len, &s1g_operation);
106 if (ret == 0)
107 {
108 printf(" S1G Operation:\n");
109 printf(" Operating class: %u\n", s1g_operation.operating_class);
110 printf(" Primary channel: %u\n", s1g_operation.primary_channel_number);
111 printf(" Primary channel width: %u MHz\n", s1g_operation.primary_channel_width_mhz);
112 printf(" Operating channel: %u\n", s1g_operation.operating_channel_number);
113 printf(" Operating channel width: %u MHz\n",
114 s1g_operation.operating_channel_width_mhz);
115 }
116}
117
124static void scan_complete_callback(enum mmwlan_scan_state state, void *arg)
125{
126 (void)(state);
127 (void)(arg);
128 printf("Scanning completed.\n");
129}
130
135void app_init(void)
136{
137 enum mmwlan_status status;
138 struct mmwlan_version version;
139
140 printf("\n\nMorse Scan Demo (Built "__DATE__
141 " " __TIME__ ")\n\n");
142
143 mmwlan_init();
144
145 const struct mmwlan_s1g_channel_list *channel_list = load_channel_list();
146 status = mmwlan_set_channel_list(channel_list);
147 if (status != MMWLAN_SUCCESS)
148 {
149 printf("Failed to set country code %s\n", channel_list->country_code);
150 MMOSAL_ASSERT(false);
151 }
152
153 struct mmwlan_boot_args boot_args = MMWLAN_BOOT_ARGS_INIT;
154 status = mmwlan_boot(&boot_args);
155 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
156
157 status = mmwlan_get_version(&version);
158 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
159 printf("Morse firmware version %s, morselib version %s, Morse chip ID 0x%lx\n\n",
160 version.morse_fw_version,
161 version.morselib_version,
162 version.morse_chip_id);
163
165 struct mmwlan_scan_req scan_req = MMWLAN_SCAN_REQ_INIT;
166 scan_req.scan_rx_cb = scan_rx_callback;
168 status = mmwlan_scan_request(&scan_req);
169 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
170 printf("Scan started on %s channels, Waiting for results...\n", channel_list->country_code);
171}
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
Definition: mmosal.h:934
const char * mm_akm_suite_to_string(uint32_t akm_suite_oui)
Get the name of the given AKM Suite as a string.
int mm_parse_s1g_operation(const uint8_t *ies, uint32_t ies_len, struct mm_s1g_operation *output)
Find the S1G Operation information element from within a block of IEs and extract useful information ...
int mm_parse_rsn_information(const uint8_t *ies, uint32_t ies_len, struct mm_rsn_information *output)
Search through the given list of information elements to find the RSN IE then parse it to extract rel...
enum mmwlan_status mmwlan_get_version(struct mmwlan_version *version)
Retrieve version information from morselib and the connected Morse transceiver.
enum mmwlan_status mmwlan_boot(const struct mmwlan_boot_args *args)
Boot the Morse Micro transceiver and leave it in an idle state.
#define MMWLAN_BOOT_ARGS_INIT
Initializer for mmwlan_boot_args.
Definition: mmwlan.h:616
void mmwlan_init(void)
Initialize the MMWLAN subsystem.
enum mmwlan_status mmwlan_set_channel_list(const struct mmwlan_s1g_channel_list *channel_list)
Set the list of channels that are supported by the regulatory domain in which the device resides.
mmwlan_scan_state
Enumeration of states in Scan mode.
Definition: mmwlan.h:801
#define MMWLAN_SCAN_REQ_INIT
Initializer for mmwlan_scan_req.
Definition: mmwlan.h:892
enum mmwlan_status mmwlan_scan_request(const struct mmwlan_scan_req *scan_req)
Request a scan.
mmwlan_status
Enumeration of status return codes.
Definition: mmwlan.h:51
#define MMWLAN_SSID_MAXLEN
Maximum allowable length of an SSID.
Definition: mmwlan.h:86
@ MMWLAN_SUCCESS
The operation was successful.
Definition: mmwlan.h:53
const struct mmwlan_s1g_channel_list * load_channel_list(void)
Looks up country code and returns appropriate channel list.
static void scan_complete_callback(enum mmwlan_scan_state state, void *arg)
Scan complete callback.
Definition: scan.c:124
#define MAC_ADDR_STR_LEN
Length of string representation of a MAC address (i.e., "XX:XX:XX:XX:XX:XX") including null terminato...
Definition: scan.c:45
#define ANSI_BOLD
ANSI escape sequence for bold text.
Definition: scan.c:33
static void scan_rx_callback(const struct mmwlan_scan_result *result, void *arg)
Scan rx callback.
Definition: scan.c:56
void app_init(void)
Main entry point to the application.
Definition: scan.c:135
static int num_scan_results
Number of results found.
Definition: scan.c:48
#define ANSI_RESET
ANSI escape sequence to reset font.
Definition: scan.c:35
Data structure to represent information extracted from an RSN information element.
Definition: mmutils.h:214
uint16_t num_akm_suites
Number of AKM suites in akm_suites.
Definition: mmutils.h:224
uint32_t akm_suites[MM_RSN_INFORMATION_MAX_AKM_SUITES]
AKM suite OUIs.
Definition: mmutils.h:220
Data structure to represent information extracted from an S1G Operation information element.
Definition: mmutils.h:340
uint8_t operating_channel_width_mhz
Width of the operating channel in MHz.
Definition: mmutils.h:344
uint8_t operating_channel_number
Channel number of the operating channel.
Definition: mmutils.h:348
uint8_t primary_channel_width_mhz
Width of the primary channel in MHz.
Definition: mmutils.h:346
uint8_t primary_channel_number
Channel number of the primary channel.
Definition: mmutils.h:350
uint8_t operating_class
Operating class.
Definition: mmutils.h:342
Arguments data structure for mmwlan_boot().
Definition: mmwlan.h:606
A list of S1G channels supported by a given regulatory domain.
Definition: mmwlan.h:179
uint8_t country_code[MMWLAN_COUNTRY_CODE_LEN]
Two character country code (null-terminated) used to identify the regulatory domain.
Definition: mmwlan.h:181
Structure to hold arguments specific to a given instance of a scan.
Definition: mmwlan.h:872
mmwlan_scan_rx_cb_t scan_rx_cb
Scan response receive callback.
Definition: mmwlan.h:874
mmwlan_scan_complete_cb_t scan_complete_cb
Scan complete callback.
Definition: mmwlan.h:876
Result of the scan request.
Definition: mmwlan.h:698
uint8_t ssid_len
Length of the SSID (ssid).
Definition: mmwlan.h:714
const uint8_t * ies
Pointer to the start of the Information Elements within the Probe Response frame.
Definition: mmwlan.h:706
uint16_t capability_info
Value of the Capability Information field.
Definition: mmwlan.h:710
uint16_t ies_len
Length of the Information Elements (ies).
Definition: mmwlan.h:712
const uint8_t * bssid
Pointer to the BSSID field within the Probe Response frame.
Definition: mmwlan.h:702
uint16_t beacon_interval
Value of the Beacon Interval field.
Definition: mmwlan.h:708
uint8_t op_bw_mhz
Operating bandwidth, in MHz, of the access point.
Definition: mmwlan.h:720
int16_t rssi
RSSI of the received frame.
Definition: mmwlan.h:700
int8_t noise_dbm
Background noise measured by the chip on the channel at the time the probe response was received.
Definition: mmwlan.h:725
const uint8_t * ssid
Pointer to the SSID within the SSID IE of the Probe Response frame.
Definition: mmwlan.h:704
Structure for retrieving version information from the mmwlan subsystem.
Definition: mmwlan.h:316
char morselib_version[MMWLAN_MORSELIB_VERSION_MAXLEN]
Morselib version string.
Definition: mmwlan.h:318
char morse_fw_version[MMWLAN_FW_VERSION_MAXLEN]
Morse transceiver firmware version string.
Definition: mmwlan.h:320
uint32_t morse_chip_id
Morse transceiver chip ID.
Definition: mmwlan.h:322