Morse Micro IoT SDK  2.9.7
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, MAC_ADDR_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
66 result->bssid[0], result->bssid[1], result->bssid[2], result->bssid[3],
67 result->bssid[4], result->bssid[5]);
68 snprintf(ssid_str, (result->ssid_len+1), "%s", result->ssid);
69
70 printf(ANSI_BOLD "%s" ANSI_RESET "\n", ssid_str);
71 printf(" Operating BW: %u MHz\n", result->op_bw_mhz);
72 printf(" BSSID: %s\n", bssid_str);
73 printf(" RSSI: %3d\n", result->rssi);
74 printf(" Beacon Interval(TUs): %u\n", result->beacon_interval);
75 printf(" Capability Info: 0x%04x\n", result->capability_info);
76
77 ret = mm_parse_rsn_information(result->ies, result->ies_len, &rsn_info);
78 if (ret == 0)
79 {
80 unsigned ii;
81 printf(" Security:");
82 for (ii = 0; ii < rsn_info.num_akm_suites; ii++)
83 {
84 printf(" %s", mm_akm_suite_to_string(rsn_info.akm_suites[ii]));
85 }
86 printf("\n");
87 }
88 else if (ret == -1)
89 {
90 printf(" Security: None\n");
91 }
92 else
93 {
94 printf(" Invalid RSN IE in probe response\n");
95 }
96
97 struct mm_s1g_operation s1g_operation;
98 ret = mm_parse_s1g_operation(result->ies, result->ies_len, &s1g_operation);
99 if (ret == 0)
100 {
101 printf(" S1G Operation:\n");
102 printf(" Operating class: %u\n",
103 s1g_operation.operating_class);
104 printf(" Primary channel: %u\n",
105 s1g_operation.primary_channel_number);
106 printf(" Primary channel width: %u MHz\n",
107 s1g_operation.primary_channel_width_mhz);
108 printf(" Operating channel: %u\n",
109 s1g_operation.operating_channel_number);
110 printf(" Operating channel width: %u MHz\n",
111 s1g_operation.operating_channel_width_mhz);
112 }
113}
114
121static void scan_complete_callback(enum mmwlan_scan_state state, void *arg)
122{
123 (void)(state);
124 (void)(arg);
125 printf("Scanning completed.\n");
126}
127
132void app_init(void)
133{
134 enum mmwlan_status status;
135 struct mmwlan_version version;
136
137 printf("\n\nMorse Scan Demo (Built "__DATE__ " " __TIME__ ")\n\n");
138
139 mmwlan_init();
140
141 const struct mmwlan_s1g_channel_list* channel_list = load_channel_list();
142 status = mmwlan_set_channel_list(channel_list);
143 if (status != MMWLAN_SUCCESS)
144 {
145 printf("Failed to set country code %s\n", channel_list->country_code);
146 MMOSAL_ASSERT(false);
147 }
148
149 struct mmwlan_boot_args boot_args = MMWLAN_BOOT_ARGS_INIT;
150 status = mmwlan_boot(&boot_args);
151 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
152
153 status = mmwlan_get_version(&version);
154 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
155 printf("Morse firmware version %s, morselib version %s, Morse chip ID 0x%lx\n\n",
156 version.morse_fw_version, version.morselib_version, version.morse_chip_id);
157
159 struct mmwlan_scan_req scan_req = MMWLAN_SCAN_REQ_INIT;
160 scan_req.scan_rx_cb = scan_rx_callback;
162 status = mmwlan_scan_request(&scan_req);
163 MMOSAL_ASSERT(status == MMWLAN_SUCCESS);
164 printf("Scan started on %s channels, Waiting for results...\n", channel_list->country_code);
165}
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
Definition: mmosal.h:927
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...
mmwlan_scan_state
Enumeration of states in Scan mode.
Definition: mmwlan.h:1167
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_SCAN_REQ_INIT
Initializer for mmwlan_scan_req.
Definition: mmwlan.h:1251
#define MMWLAN_BOOT_ARGS_INIT
Initializer for mmwlan_boot_args.
Definition: mmwlan.h:760
enum mmwlan_status mmwlan_scan_request(const struct mmwlan_scan_req *scan_req)
Request a scan.
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_status
Enumeration of status return codes.
Definition: mmwlan.h:50
#define MMWLAN_SSID_MAXLEN
Maximum allowable length of an SSID.
Definition: mmwlan.h:79
@ MMWLAN_SUCCESS
The operation was successful.
Definition: mmwlan.h:52
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:121
#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:132
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:213
uint16_t num_akm_suites
Number of AKM suites in akm_suites.
Definition: mmutils.h:223
uint32_t akm_suites[MM_RSN_INFORMATION_MAX_AKM_SUITES]
AKM suite OUIs.
Definition: mmutils.h:219
Data structure to represent information extracted from an S1G Operation information element.
Definition: mmutils.h:332
uint8_t operating_channel_width_mhz
Width of the operating channel in MHz.
Definition: mmutils.h:336
uint8_t operating_channel_number
Channel number of the operating channel.
Definition: mmutils.h:340
uint8_t primary_channel_width_mhz
Width of the primary channel in MHz.
Definition: mmutils.h:338
uint8_t primary_channel_number
Channel number of the primary channel.
Definition: mmutils.h:342
uint8_t operating_class
Operating class.
Definition: mmutils.h:334
Arguments data structure for mmwlan_boot().
Definition: mmwlan.h:750
A list of S1G channels supported by a given regulatory domain.
Definition: mmwlan.h:274
uint8_t country_code[MMWLAN_COUNTRY_CODE_LEN]
Two character country code (null-terminated) used to identify the regulatory domain.
Definition: mmwlan.h:276
Structure to hold arguments specific to a given instance of a scan.
Definition: mmwlan.h:1231
mmwlan_scan_rx_cb_t scan_rx_cb
Scan response receive callback.
Definition: mmwlan.h:1233
mmwlan_scan_complete_cb_t scan_complete_cb
Scan complete callback.
Definition: mmwlan.h:1235
Result of the scan request.
Definition: mmwlan.h:808
uint8_t ssid_len
Length of the SSID (ssid).
Definition: mmwlan.h:824
const uint8_t * ies
Pointer to the start of the Information Elements within the Probe Response frame.
Definition: mmwlan.h:816
uint16_t capability_info
Value of the Capability Information field.
Definition: mmwlan.h:820
uint16_t ies_len
Length of the Information Elements (ies).
Definition: mmwlan.h:822
const uint8_t * bssid
Pointer to the BSSID field within the Probe Response frame.
Definition: mmwlan.h:812
uint16_t beacon_interval
Value of the Beacon Interval field.
Definition: mmwlan.h:818
uint8_t op_bw_mhz
Operating bandwidth, in MHz, of the access point.
Definition: mmwlan.h:830
int16_t rssi
RSSI of the received frame.
Definition: mmwlan.h:810
const uint8_t * ssid
Pointer to the SSID within the SSID IE of the Probe Response frame.
Definition: mmwlan.h:814
Structure for retrieving version information from the mmwlan subsystem.
Definition: mmwlan.h:362
char morselib_version[MMWLAN_MORSELIB_VERSION_MAXLEN]
Morselib version string.
Definition: mmwlan.h:364
char morse_fw_version[MMWLAN_FW_VERSION_MAXLEN]
Morse transceiver firmware version string.
Definition: mmwlan.h:366
uint32_t morse_chip_id
Morse transceiver chip ID.
Definition: mmwlan.h:368