Morse Micro IoT SDK  2.9.7
mmutils.h
1/*
2 * Copyright 2024 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
15#pragma once
16
17#include <stdarg.h>
18#include <stdint.h>
19#include <stdlib.h>
20#include <stdbool.h>
21#include <string.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
41#define MM_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
42
57#define MM_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
58
64#ifndef MM_FAST_ROUND_UP
65#define MM_FAST_ROUND_UP(x, m) ((((x) - 1) | ((m) - 1)) + 1)
66#endif
67
69#define MM_UNUSED(_x) (void)(_x)
70
72#ifndef MM_PACKED
73#define MM_PACKED __attribute__((packed))
74#endif
75
77#ifndef MM_WEAK
78#define MM_WEAK __attribute__((weak))
79#endif
80
82#ifndef MM_FALLTHROUGH
83#define MM_FALLTHROUGH __attribute__((fallthrough))
84#endif
85
86#ifndef MM_STATIC_ASSERT
98#define MM_STATIC_ASSERT(_expression, _message) _Static_assert((_expression), _message)
99#endif
100
110#define MM_ARRAY_COUNT(_a) (sizeof(_a) / sizeof((_a)[0]))
111
120#define MM_MEMBER_SIZE(_type, _member) (sizeof(((_type *)0)->_member))
121
134static inline char mm_nibble_to_hex_char(uint8_t nibble)
135{
136 nibble &= 0x0f;
137 if (nibble < 0x0a)
138 {
139 return '0' + nibble;
140 }
141 else
142 {
143 return 'A' + nibble - 0x0a;
144 }
145}
146
157{
161 MM_AKM_SUITE_PSK = 0x506f9a02,
163 MM_AKM_SUITE_SAE = 0x000fac08,
165 MM_AKM_SUITE_OWE = 0x000fac12,
168};
169
172{
177};
178
180#ifndef MM_RSN_INFORMATION_MAX_PAIRWISE_CIPHER_SUITES
181#define MM_RSN_INFORMATION_MAX_PAIRWISE_CIPHER_SUITES (2)
182#endif
183
185#ifndef MM_RSN_INFORMATION_MAX_AKM_SUITES
186#define MM_RSN_INFORMATION_MAX_AKM_SUITES (2)
187#endif
188
190#define MM_RSN_INFORMATION_IE_TYPE (48)
192#define MM_VENDOR_SPECIFIC_IE_TYPE (221)
194#define MM_S1G_OPERATION_IE_TYPE (232)
195
199{
200 MM_ENOMEM = 12,
201 MM_EFAULT = 14,
202 MM_ENODEV = 19,
203 MM_EINVAL = 22,
204 MM_ETIMEDOUT = 110,
205};
206
213{
225 uint16_t version;
228};
229
237const char *mm_akm_suite_to_string(uint32_t akm_suite_oui);
238
255int mm_find_ie_from_offset(const uint8_t *ies, uint32_t ies_len,
256 uint32_t search_offset, uint8_t ie_type);
257
270static inline int mm_find_ie(const uint8_t *ies, uint32_t ies_len, uint8_t ie_type)
271{
272 return mm_find_ie_from_offset(ies, ies_len, 0, ie_type);
273}
274
292int mm_find_vendor_specific_ie_from_offset(const uint8_t *ies, uint32_t ies_len,
293 uint32_t search_offset,
294 const uint8_t *id, size_t id_len);
295
309static inline int mm_find_vendor_specific_ie(const uint8_t *ies, uint32_t ies_len,
310 const uint8_t *id, size_t id_len)
311{
312 return mm_find_vendor_specific_ie_from_offset(ies, ies_len, 0, id, id_len);
313}
314
325int mm_parse_rsn_information(const uint8_t *ies, uint32_t ies_len,
326 struct mm_rsn_information *output);
327
332{
343};
344
355int mm_parse_s1g_operation(const uint8_t *ies, uint32_t ies_len,
356 struct mm_s1g_operation *output);
357
365static inline bool mm_mac_addr_is_zero(const uint8_t *mac_addr)
366{
367 return (mac_addr[0] | mac_addr[1] | mac_addr[2] |
368 mac_addr[3] | mac_addr[4] | mac_addr[5]) == 0x00;
369}
370
373#ifdef __cplusplus
374}
375#endif
376
#define MM_RSN_INFORMATION_MAX_AKM_SUITES
Maximum number of AKM suites our parser will process.
Definition: mmutils.h:186
static int mm_find_vendor_specific_ie(const uint8_t *ies, uint32_t ies_len, const uint8_t *id, size_t id_len)
Search through the given list of Information Elements (IEs) to find the first Vendor Specific IE that...
Definition: mmutils.h:309
mm_akm_suite_oui
Enumeration of Authentication Key Management (AKM) Suite OUIs as BE32 integers.
Definition: mmutils.h:157
static bool mm_mac_addr_is_zero(const uint8_t *mac_addr)
Determines if a given MAC address is all zeros.
Definition: mmutils.h:365
static int mm_find_ie(const uint8_t *ies, uint32_t ies_len, uint8_t ie_type)
Search a list of Information Elements (IEs) and find the first instance of matching the given type.
Definition: mmutils.h:270
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_find_vendor_specific_ie_from_offset(const uint8_t *ies, uint32_t ies_len, uint32_t search_offset, const uint8_t *id, size_t id_len)
Search through the given list of Information Elements (IEs) from the given starting offset to find th...
mm_errno
Explicitly defined errno values to obviate the need to include errno.h.
Definition: mmutils.h:199
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...
#define MM_RSN_INFORMATION_MAX_PAIRWISE_CIPHER_SUITES
Maximum number of pairwise cipher suites our parser will process.
Definition: mmutils.h:181
mm_cipher_suite_oui
Enumeration of Cipher Suite OUIs as BE32 integers.
Definition: mmutils.h:172
int mm_find_ie_from_offset(const uint8_t *ies, uint32_t ies_len, uint32_t search_offset, uint8_t ie_type)
Search a list of Information Elements (IEs) from the given starting offset and find the first instanc...
@ MM_AKM_SUITE_SAE
Simultaneous Authentication of Equals (SAE)
Definition: mmutils.h:163
@ MM_AKM_SUITE_NONE
Open (no security)
Definition: mmutils.h:159
@ MM_AKM_SUITE_OWE
OWE.
Definition: mmutils.h:165
@ MM_AKM_SUITE_OTHER
Another suite not in this enum.
Definition: mmutils.h:167
@ MM_AKM_SUITE_PSK
Pre-shared key (WFA OUI)
Definition: mmutils.h:161
@ MM_CIPHER_SUITE_OTHER
Another cipher suite not in this enum.
Definition: mmutils.h:176
@ MM_CIPHER_SUITE_AES_CCM
Open (no security)
Definition: mmutils.h:174
static char mm_nibble_to_hex_char(uint8_t nibble)
Convert the least significant 4 bits of the given argument to a character representing their hexadeci...
Definition: mmutils.h:134
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 group_cipher_suite
The group cipher suite OUI.
Definition: mmutils.h:215
uint16_t version
Version number of the RSN IE.
Definition: mmutils.h:225
uint32_t pairwise_cipher_suites[MM_RSN_INFORMATION_MAX_PAIRWISE_CIPHER_SUITES]
Pairwise cipher suite OUIs.
Definition: mmutils.h:217
uint16_t rsn_capabilities
RSN Capabilities field of the RSN IE (in host order).
Definition: mmutils.h:227
uint16_t num_pairwise_cipher_suites
Number of pairwise cipher suites in pairwise_cipher_suites.
Definition: mmutils.h:221
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